• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4895

23 Dec 2025 01:08PM UTC coverage: 65.513% (-0.2%) from 65.72%
#4895

push

travis-ci

web-flow
fix: mem leak (#34023)

6 of 9 new or added lines in 1 file covered. (66.67%)

7770 existing lines in 123 files now uncovered.

184705 of 281937 relevant lines covered (65.51%)

112009834.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.16
/source/dnode/snode/src/snode.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include "sndInt.h"
18
#include "tdatablock.h"
19
#include "tuuid.h"
20
#include "stream.h"
21
#include "streamRunner.h"
22

23
// clang-format off
24
#define sndError(...) do {  if (sndDebugFlag & DEBUG_ERROR) { taosPrintLog("SND ERROR ", DEBUG_ERROR, sndDebugFlag, __VA_ARGS__);}} while (0)
25
#define sndInfo(...)  do {  if (sndDebugFlag & DEBUG_INFO)  { taosPrintLog("SND INFO  ", DEBUG_INFO,  sndDebugFlag, __VA_ARGS__);}} while (0)
26
#define sndDebug(...) do {  if (sndDebugFlag & DEBUG_DEBUG) { taosPrintLog("SND DEBUG ", DEBUG_DEBUG, sndDebugFlag, __VA_ARGS__);}} while (0)
27

28
SSnode *sndOpen(const char *path, const SSnodeOpt *pOption) {
109,252✔
29
  int32_t code = 0;
109,252✔
30
  SSnode *pSnode = taosMemoryCalloc(1, sizeof(SSnode));
109,252✔
31
  if (pSnode == NULL) {
109,252✔
32
    return NULL;
×
33
  }
34

35
  pSnode->msgCb = pOption->msgCb;
109,252✔
36

37
  return pSnode;
109,252✔
38
}
39

40
int32_t sndInit(SSnode *pSnode) {
109,252✔
41
  streamSetSnodeEnabled(&pSnode->msgCb);
109,252✔
42
  return 0;
109,252✔
43
}
44

45
void sndClose(SSnode *pSnode) {
109,252✔
46
  streamSetSnodeDisabled(false);
109,252✔
47
  taosMemoryFree(pSnode);
109,252✔
48
}
109,252✔
49

50
static int32_t handleTriggerCalcReq(SSnode* pSnode, void* pWorkerCb, SRpcMsg* pRpcMsg) {
3,841,215✔
51
  SSTriggerCalcRequest req = {0};
3,841,215✔
52
  SStreamRunnerTask* pTask = NULL;
3,841,215✔
53
  void* taskAddr = NULL;
3,841,215✔
54
  int32_t code = 0, lino = 0;
3,841,215✔
55
  TAOS_CHECK_EXIT(tDeserializeSTriggerCalcRequest(POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead)), pRpcMsg->contLen - sizeof(SMsgHead), &req));
3,841,215✔
56
  TAOS_CHECK_EXIT(streamAcquireTask(req.streamId, req.runnerTaskId, (SStreamTask**)&pTask, &taskAddr));
3,841,215✔
57

58
  req.brandNew = true;
3,840,819✔
59
  req.execId = -1;
3,840,819✔
60
  pTask->msgCb = pSnode->msgCb;
3,840,819✔
61
  //pTask->pMsgCb = &pSnode->msgCb;
62
  pTask->pWorkerCb = pWorkerCb;
3,841,215✔
63
  req.curWinIdx = 0;
3,840,819✔
64
  TAOS_CHECK_EXIT(stRunnerTaskExecute(pTask, &req));
3,840,819✔
65

66
_exit:
3,840,416✔
67

68
  tDestroySTriggerCalcRequest(&req);
3,841,215✔
69
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_TRIGGER_CALC_RSP, .contLen = 0, .pCont = NULL, .info = pRpcMsg->info};
3,841,215✔
70
  if (rpcSendResponse(&rsp) != 0) {
3,841,215✔
UNCOV
71
    sndError("failed to send response, msg:%p", &rsp);
×
72
  }
73
  
74
  streamReleaseTask(taskAddr);
3,839,239✔
75

76
  if(code == TSDB_CODE_MND_STREAM_TABLE_NOT_CREATE) {
3,840,423✔
77
    code = 0; // not real error, just notify trigger the table is not created
×
78
  }
79

80
  if (code) {
3,840,423✔
81
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2,974,960✔
82
  }
83
  
84
  return code;
3,841,611✔
85
}
86

87
static int32_t handleSyncDeleteCheckPointReq(SSnode* pSnode, SRpcMsg* pRpcMsg) {
3,585✔
88
  int64_t streamId = *(int64_t*)POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead));
3,585✔
89
  streamDeleteCheckPoint(streamId);
3,585✔
90
  return 0;
3,585✔
91
}
92

93
static int32_t handleSyncWriteCheckPointReq(SSnode* pSnode, SRpcMsg* pRpcMsg) {
11,653✔
94
  int32_t ver = *(int32_t*)POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead) + INT_BYTES);
11,653✔
95
  int64_t streamId = *(int64_t*)POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead) + 2 * INT_BYTES);
11,653✔
96
  SRpcMsg rsp = {.code = 0, .msgType = TDMT_STREAM_SYNC_CHECKPOINT_RSP, .info = pRpcMsg->info};
11,653✔
97

98
  stDebug("[checkpoint] handleSyncWriteCheckPointReq streamId:%" PRIx64 ",ver:%d", streamId, ver);
11,653✔
99
  void*   data = NULL;
11,653✔
100
  int64_t dataLen = 0;
11,653✔
101
  int32_t code = streamReadCheckPoint(streamId, &data, &dataLen);
11,653✔
102
  if (code != 0 || (terrno == TAOS_SYSTEM_ERROR(ENOENT) && ver == -1)){
11,653✔
103
    goto end;
5,167✔
104
  }
105
  if (terrno == TAOS_SYSTEM_ERROR(ENOENT) || ver > *(int32_t*)POINTER_SHIFT(data, INT_BYTES)) {
6,486✔
106
    int32_t ret = streamWriteCheckPoint(streamId, POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead)), pRpcMsg->contLen - sizeof(SMsgHead));
3,964✔
107
    stDebug("[checkpoint] streamId:%" PRIx64 ", checkpoint local updated, ver:%d, dataLen:%" PRId64 ", ret:%d", streamId, ver, dataLen, ret);
3,964✔
108
  }
109
  if (terrno == TAOS_SYSTEM_ERROR(ENOENT) || ver >= *(int32_t*)POINTER_SHIFT(data, INT_BYTES)) {
6,486✔
110
    stDebug("[checkpoint] streamId:%" PRIx64 ", checkpoint no need send back, ver:%d, dataLen:%" PRId64, streamId, ver, dataLen);
5,764✔
111
    dataLen = 0;
5,764✔
112
    taosMemoryFreeClear(data);
5,764✔
113
  }
114
  
115
end:
11,653✔
116
  if (data == NULL) {
11,653✔
117
    rsp.contLen = 2 * INT_BYTES + LONG_BYTES;
10,931✔
118
    rsp.pCont = rpcMallocCont(rsp.contLen);
10,931✔
119
    if (rsp.pCont == NULL) {
10,931✔
120
      rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
121
    } else {
122
      *(int32_t*)(POINTER_SHIFT(rsp.pCont, INT_BYTES)) = -1;  // no checkpoint
10,931✔
123
      *(int64_t*)(POINTER_SHIFT(rsp.pCont, 2 * INT_BYTES)) = streamId;
10,931✔
124
    }
125
  } else {
126
    rsp.pCont = rpcMallocCont(dataLen);
722✔
127
    if (rsp.pCont == NULL) {
722✔
128
      rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
129
    } else {
130
      memcpy(rsp.pCont, data, dataLen);
722✔
131
      rsp.contLen = dataLen;
722✔
132
      taosMemoryFreeClear(data); 
722✔
133
    } 
134
  }
135
  
136
  if (rpcSendResponse(&rsp) != 0) {
11,653✔
137
    sndError("failed to send write checkpoint response, msg:%p", &rsp);
×
138
  }
139

140
  return 0;
11,653✔
141
}
142

143
static int32_t handleSyncWriteCheckPointRsp(SSnode* pSnode, SRpcMsg* pRpcMsg) {
11,651✔
144
  if (pRpcMsg->code != 0) {
11,651✔
145
    stError("[checkpoint] handleSyncWriteCheckPointRsp, code:%d, msgType:%d", pRpcMsg->code, pRpcMsg->msgType);
×
146
    return pRpcMsg->code;
×
147
  } 
148
  void* data = pRpcMsg->pCont;
11,651✔
149
  int32_t dataLen = pRpcMsg->contLen;
11,651✔
150
  
151
  int32_t ver = *(int32_t*)(POINTER_SHIFT(data, INT_BYTES));
11,651✔
152
  int64_t streamId = *(int64_t*)(POINTER_SHIFT(data, 2 * INT_BYTES));
11,651✔
153
  stDebug("[checkpoint] handleSyncWriteCheckPointRsp, ver:%d, streamId:%"PRIx64",dataLen:%d", 
11,651✔
154
    ver, streamId, dataLen);
155

156
  if (ver != -1){
11,651✔
157
    (void)streamWriteCheckPoint(streamId, data, dataLen);
720✔
158
  }
159
  return streamCheckpointSetReady(streamId);
11,651✔
160
}
161

162
static int32_t buildStreamFetchRsp(SSDataBlock* pBlock, void** data, size_t* size, int8_t precision, bool finished) {
6,137,714✔
163
  int32_t code = 0;
6,137,714✔
164
  int32_t lino = 0;
6,137,714✔
165
  void*   buf =  NULL;
6,137,714✔
166

167
  int32_t blockSize = pBlock == NULL ? 0 : blockGetInternalEncodeSize(pBlock);
6,137,714✔
168
  size_t dataEncodeBufSize = sizeof(SRetrieveTableRsp) + INT_BYTES * 2 + blockSize;
6,137,714✔
169
  buf = rpcMallocCont(dataEncodeBufSize);
6,137,714✔
170
  if (!buf) {
6,138,117✔
171
    code = terrno;
×
172
    goto end;
×
173
  }
174

175
  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
6,138,117✔
176
  pRetrieve->version = 0;
6,138,117✔
177
  pRetrieve->precision = precision;
6,138,117✔
178
  pRetrieve->compressed = 0;
6,138,117✔
179
  *((int32_t*)(pRetrieve->data)) = blockSize;
6,138,117✔
180
  *((int32_t*)(pRetrieve->data + INT_BYTES)) = blockSize;
6,138,117✔
181
  if (pBlock == NULL || pBlock->info.rows == 0) {
6,138,117✔
182
    pRetrieve->numOfRows = 0;
3,655,942✔
183
    pRetrieve->numOfBlocks = 0;
3,655,942✔
184
    pRetrieve->completed = 1;
3,655,942✔
185
  } else {
186
    pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
2,482,175✔
187
    pRetrieve->numOfBlocks = htonl(1);
2,482,175✔
188
    int32_t actualLen = blockEncodeInternal(pBlock, pRetrieve->data + INT_BYTES * 2, blockSize, taosArrayGetSize(pBlock->pDataBlock));
2,482,175✔
189
    if (actualLen < 0) {
2,481,783✔
190
      code = terrno;
×
191
      goto end;
×
192
    }
193
  }
194
  if (finished) {
6,137,725✔
195
    pRetrieve->completed = 1;
4,098,169✔
196
  }
197

198
  *data = buf;
6,137,725✔
199
  *size = dataEncodeBufSize;
6,138,117✔
200
  buf = NULL;
6,138,117✔
201

202
end:
6,138,117✔
203
  rpcFreeCont(buf);
6,138,117✔
204
  return code;
6,137,739✔
205
}
206

207
static int32_t handleStreamFetchData(SSnode* pSnode, void *pWorkerCb, SRpcMsg* pRpcMsg) {
37,603✔
208
  int32_t code = 0, lino = 0;
37,603✔
209
  void* taskAddr = NULL;
37,603✔
210
  SResFetchReq req = {0};
37,603✔
211
  SSTriggerCalcRequest calcReq = {0};
37,603✔
212
  SStreamRunnerTask* pTask = NULL;
37,603✔
213
  void* buf = NULL;
37,603✔
214
  size_t size = 0;
37,603✔
215

216
  stDebug("handleStreamFetchData, msgType:%s, contLen:%d 0x%" PRIx64 ":0x%" PRIx64, 
37,603✔
217
      TMSG_INFO(pRpcMsg->msgType), pRpcMsg->contLen, TRACE_GET_ROOTID(&pRpcMsg->info.traceId), TRACE_GET_MSGID(&pRpcMsg->info.traceId));
218
  
219
  TAOS_CHECK_EXIT(tDeserializeSResFetchReq(pRpcMsg->pCont,pRpcMsg->contLen, &req));
37,603✔
220

221
  calcReq.streamId = req.queryId;
37,603✔
222
  calcReq.runnerTaskId = req.taskId;
37,603✔
223
  calcReq.brandNew = req.reset;
37,603✔
224
  calcReq.execId = req.execId;
37,603✔
225
  calcReq.sessionId = req.pStRtFuncInfo->sessionId;
37,603✔
226
  calcReq.triggerType = req.pStRtFuncInfo->triggerType;
37,603✔
227
  calcReq.isWindowTrigger = req.pStRtFuncInfo->isWindowTrigger;
37,603✔
228
  calcReq.precision = req.pStRtFuncInfo->precision;
37,603✔
229
  TSWAP(calcReq.groupColVals, req.pStRtFuncInfo->pStreamPartColVals);
37,603✔
230
  TSWAP(calcReq.params, req.pStRtFuncInfo->pStreamPesudoFuncVals);
37,603✔
231
  calcReq.gid = req.pStRtFuncInfo->groupId;
37,603✔
232
  calcReq.curWinIdx = req.pStRtFuncInfo->curIdx;
37,603✔
233
  calcReq.pOutBlock = NULL;
37,603✔
234

235
  TAOS_CHECK_EXIT(streamAcquireTask(calcReq.streamId, calcReq.runnerTaskId, (SStreamTask**)&pTask, &taskAddr));
37,603✔
236

237
  pTask->msgCb = pSnode->msgCb;
37,603✔
238
  //pTask->pMsgCb = &pSnode->msgCb;
239
  pTask->pWorkerCb = pWorkerCb;
37,603✔
240
  
241
  TAOS_CHECK_EXIT(stRunnerTaskExecute(pTask, &calcReq));
37,603✔
242

243
  TAOS_CHECK_EXIT(buildStreamFetchRsp(calcReq.pOutBlock, &buf, &size, 0, false));
37,603✔
244

245
_exit:
37,603✔
246

247
  tDestroySTriggerCalcRequest(&calcReq);
37,603✔
248
  tDestroySResFetchReq(&req);
37,603✔
249
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_FETCH_FROM_RUNNER_RSP, .contLen = size, .pCont = buf, .info = pRpcMsg->info};
37,603✔
250
  tmsgSendRsp(&rsp);
37,603✔
251
  
252
  streamReleaseTask(taskAddr);
37,603✔
253

254
  if (code) {
37,603✔
255
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
256
  }
257
  
258
  return code;
37,603✔
259
}
260

261
static int32_t handleStreamFetchFromCache(SSnode* pSnode, SRpcMsg* pRpcMsg) {
6,100,514✔
262
  int32_t code = 0, lino = 0;
6,100,514✔
263
  SResFetchReq req = {0};
6,100,514✔
264
  SStreamCacheReadInfo readInfo = {0};
6,100,514✔
265
  void* buf = NULL;
6,100,514✔
266
  int64_t streamId = 0;
6,100,514✔
267
  size_t size = 0;
6,100,514✔
268
  TAOS_CHECK_EXIT(tDeserializeSResFetchReq(pRpcMsg->pCont, pRpcMsg->contLen, &req));
6,100,514✔
269

270
  streamId = req.queryId;
6,100,514✔
271
  readInfo.taskInfo.streamId = req.queryId;
6,100,514✔
272
  readInfo.taskInfo.taskId = req.taskId;
6,100,514✔
273
  readInfo.taskInfo.sessionId = req.pStRtFuncInfo->sessionId;
6,100,514✔
274
  readInfo.gid = req.pStRtFuncInfo->groupId;
6,100,514✔
275
  //SSTriggerCalcParam* pParam = taosArrayGet(req.pStRtFuncInfo->pStreamPesudoFuncVals, req.pStRtFuncInfo->curIdx);
276
  readInfo.start = req.pStRtFuncInfo->curWindow.skey;
6,100,514✔
277
  readInfo.end = req.pStRtFuncInfo->curWindow.ekey;
6,099,723✔
278
  bool finished;
6,099,614✔
279
  TAOS_CHECK_EXIT(stRunnerFetchDataFromCache(&readInfo,&finished));
6,099,723✔
280

281
  TAOS_CHECK_EXIT(buildStreamFetchRsp(readInfo.pBlock, &buf, &size, 0, finished));
6,100,514✔
282

283
_exit:
6,100,514✔
284

285
  printDataBlock(readInfo.pBlock, __func__, "fetchFromCache", streamId);
6,100,514✔
286

287
  stsDebug("task %" PRIx64 " TDMT_STREAM_FETCH_FROM_CACHE_RSP with code:%d rows:%" PRId64 ", size:%d, time range:[%" PRId64 ", %" PRId64 "]", 
6,100,514✔
288
      req.taskId, code, readInfo.pBlock ? readInfo.pBlock->info.rows : 0, (int32_t)size, readInfo.start, readInfo.end);  
289
      
290
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_FETCH_FROM_CACHE_RSP, .contLen = size, .pCont = buf, .info = pRpcMsg->info};
6,100,514✔
291
  tmsgSendRsp(&rsp);
6,100,514✔
292

293
  if (code) {
6,100,514✔
294
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
295
  }
296

297
  blockDataDestroy(readInfo.pBlock);
6,100,514✔
298
  tDestroySResFetchReq(&req);
6,100,514✔
299
  
300
  return code;
6,100,514✔
301
}
302

303
static void sndSendErrorRrsp(SRpcMsg *pMsg, int32_t errCode) {
×
304
  SRpcMsg             rspMsg = {0};
×
305

306
  rspMsg.info = pMsg->info;
×
307
  rspMsg.pCont = NULL;
×
308
  rspMsg.contLen = 0;
×
309
  rspMsg.code = errCode;
×
310
  rspMsg.msgType = pMsg->msgType;
×
311

312
  tmsgSendRsp(&rspMsg);
×
313
}
×
314

315
static int32_t handleStreamDropTableReq(SSnode* pSnode, SRpcMsg* pRpcMsg) {
429✔
316
  SSTriggerDropRequest req = {0};
429✔
317
  SStreamRunnerTask* pTask = NULL;
429✔
318
  void* taskAddr = NULL;
429✔
319
  int32_t code = 0, lino = 0;
429✔
320
  TAOS_CHECK_EXIT(tDeserializeSTriggerDropTableRequest(POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead)), pRpcMsg->contLen - sizeof(SMsgHead), &req));
429✔
321
  TAOS_CHECK_EXIT(streamAcquireTask(req.streamId, req.runnerTaskId, (SStreamTask**)&pTask, &taskAddr));
429✔
322
  
323
  pTask->msgCb = pSnode->msgCb;
429✔
324
  TAOS_CHECK_EXIT(stRunnerTaskDropTable(pTask, &req));
429✔
325

326
_exit:
429✔
327
  tDestroySSTriggerDropRequest(&req);
429✔
328
  if (code) {
429✔
329
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
330
    sndSendErrorRrsp(pRpcMsg, code);
×
331
  } else {
332
    SRpcMsg rsp = {.code = 0, .msgType = TDMT_STREAM_TRIGGER_DROP_RSP, .contLen = 0, .pCont = NULL, .info = pRpcMsg->info};
429✔
333
    tmsgSendRsp(&rsp);
429✔
334
  }
335
  streamReleaseTask(taskAddr);
429✔
336

337
  return code;
429✔
338
}
339

340

341
int32_t sndProcessStreamMsg(SSnode *pSnode, void *pWorkerCb, SRpcMsg *pMsg) {
10,006,181✔
342
  int32_t code = 0, lino = 0;
10,006,181✔
343
  switch (pMsg->msgType) {
10,006,181✔
344
    case TDMT_STREAM_TRIGGER_CALC:
3,841,215✔
345
      TAOS_CHECK_EXIT(handleTriggerCalcReq(pSnode, pWorkerCb, pMsg));
3,841,215✔
346
      break;
865,859✔
347
    case TDMT_STREAM_DELETE_CHECKPOINT:
3,585✔
348
      TAOS_CHECK_EXIT(handleSyncDeleteCheckPointReq(pSnode, pMsg));
3,585✔
349
      break;
3,585✔
350
    case TDMT_STREAM_SYNC_CHECKPOINT:
11,653✔
351
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointReq(pSnode, pMsg));
11,653✔
352
      break;
11,653✔
353
    case TDMT_STREAM_SYNC_CHECKPOINT_RSP:
11,651✔
354
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointRsp(pSnode, pMsg));
11,651✔
355
      break;
7,331✔
356
    case TDMT_STREAM_FETCH_FROM_RUNNER:
37,603✔
357
      TAOS_CHECK_EXIT(handleStreamFetchData(pSnode, pWorkerCb, pMsg));
37,603✔
358
      break;
37,603✔
359
    case TDMT_STREAM_FETCH_FROM_CACHE:
6,100,514✔
360
      TAOS_CHECK_EXIT(handleStreamFetchFromCache(pSnode, pMsg));
6,100,514✔
361
      break;
6,100,122✔
362
      case TDMT_STREAM_TRIGGER_DROP:
429✔
363
     TAOS_CHECK_EXIT(handleStreamDropTableReq(pSnode, pMsg));
429✔
364
      break;
429✔
365
    default:
×
366
      sndError("invalid snode msg:%d", pMsg->msgType);
×
367
      TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
368
  }
369

370
_exit:
×
371

372
  if (code) {
10,006,258✔
373
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2,979,676✔
374
  }
375
  
376
  return code;
10,006,258✔
377
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc