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

taosdata / TDengine / #4835

31 Oct 2025 03:37AM UTC coverage: 58.506% (-0.3%) from 58.764%
#4835

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149104 of 324176 branches covered (45.99%)

Branch coverage included in aggregate %.

198232 of 269498 relevant lines covered (73.56%)

236558065.91 hits per line

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

67.87
/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) {
280,363✔
29
  int32_t code = 0;
280,363✔
30
  SSnode *pSnode = taosMemoryCalloc(1, sizeof(SSnode));
280,363!
31
  if (pSnode == NULL) {
280,363!
32
    return NULL;
×
33
  }
34

35
  pSnode->msgCb = pOption->msgCb;
280,363✔
36

37
  return pSnode;
280,363✔
38
}
39

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

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

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

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

66
_exit:
8,056,760✔
67

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

76
  if(code == TSDB_CODE_MND_STREAM_TABLE_NOT_CREATE) {
8,056,760✔
77
    code = 0; // not real error, just notify trigger the table is not created
101,761✔
78
  }
79

80
  if (code) {
8,056,760✔
81
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
5,940,536✔
82
  }
83
  
84
  return code;
8,058,032✔
85
}
86

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

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

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

140
  return 0;
7,752✔
141
}
142

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

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

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

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

175
  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
14,452,363✔
176
  pRetrieve->version = 0;
14,452,363✔
177
  pRetrieve->precision = precision;
14,451,084✔
178
  pRetrieve->compressed = 0;
14,452,363✔
179
  *((int32_t*)(pRetrieve->data)) = blockSize;
14,452,363✔
180
  *((int32_t*)(pRetrieve->data + INT_BYTES)) = blockSize;
14,452,363✔
181
  if (pBlock == NULL || pBlock->info.rows == 0) {
14,452,363!
182
    pRetrieve->numOfRows = 0;
9,152,790✔
183
    pRetrieve->numOfBlocks = 0;
9,152,790✔
184
    pRetrieve->completed = 1;
9,152,790✔
185
  } else {
186
    pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
5,299,573✔
187
    pRetrieve->numOfBlocks = htonl(1);
5,299,573✔
188
    int32_t actualLen = blockEncodeInternal(pBlock, pRetrieve->data + INT_BYTES * 2, blockSize, taosArrayGetSize(pBlock->pDataBlock));
5,299,573✔
189
    if (actualLen < 0) {
5,299,573!
190
      code = terrno;
×
191
      goto end;
×
192
    }
193
  }
194
  if (finished) {
14,452,363✔
195
    pRetrieve->completed = 1;
10,366,427✔
196
  }
197

198
  *data = buf;
14,452,363✔
199
  *size = dataEncodeBufSize;
14,452,363✔
200
  buf = NULL;
14,452,363✔
201

202
end:
14,452,363✔
203
  rpcFreeCont(buf);
14,452,363✔
204
  return code;
14,452,363✔
205
}
206

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

216
  stDebug("handleStreamFetchData, msgType:%s, contLen:%d 0x%" PRIx64 ":0x%" PRIx64, 
113,690!
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));
113,690!
220

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

233
  TAOS_CHECK_EXIT(streamAcquireTask(calcReq.streamId, calcReq.runnerTaskId, (SStreamTask**)&pTask, &taskAddr));
113,690!
234

235
  pTask->msgCb = pSnode->msgCb;
113,690✔
236
  //pTask->pMsgCb = &pSnode->msgCb;
237
  pTask->pWorkerCb = pWorkerCb;
113,690✔
238
  
239
  TAOS_CHECK_EXIT(stRunnerTaskExecute(pTask, &calcReq));
113,690!
240

241
  TAOS_CHECK_EXIT(buildStreamFetchRsp(calcReq.pOutBlock, &buf, &size, 0, false));
113,690!
242

243
_exit:
113,690✔
244

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

252
  if (code) {
113,690!
253
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
254
  }
255
  
256
  return code;
113,690✔
257
}
258

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

268
  streamId = req.queryId;
14,337,394✔
269
  readInfo.taskInfo.streamId = req.queryId;
14,337,394✔
270
  readInfo.taskInfo.taskId = req.taskId;
14,337,394✔
271
  readInfo.taskInfo.sessionId = req.pStRtFuncInfo->sessionId;
14,337,394✔
272
  readInfo.gid = req.pStRtFuncInfo->groupId;
14,337,394✔
273
  //SSTriggerCalcParam* pParam = taosArrayGet(req.pStRtFuncInfo->pStreamPesudoFuncVals, req.pStRtFuncInfo->curIdx);
274
  readInfo.start = req.pStRtFuncInfo->curWindow.skey;
14,337,394✔
275
  readInfo.end = req.pStRtFuncInfo->curWindow.ekey;
14,337,394✔
276
  bool finished;
14,334,358✔
277
  TAOS_CHECK_EXIT(stRunnerFetchDataFromCache(&readInfo,&finished));
14,337,394!
278

279
  TAOS_CHECK_EXIT(buildStreamFetchRsp(readInfo.pBlock, &buf, &size, 0, finished));
14,338,673!
280

281
_exit:
14,338,673✔
282

283
  printDataBlock(readInfo.pBlock, __func__, "fetchFromCache", streamId);
14,338,673✔
284

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

291
  if (code) {
14,338,673!
292
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
293
  }
294

295
  blockDataDestroy(readInfo.pBlock);
14,338,673✔
296
  tDestroySResFetchReq(&req);
14,338,673✔
297
  
298
  return code;
14,338,673✔
299
}
300

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

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

310
  tmsgSendRsp(&rspMsg);
×
311
}
×
312

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

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

335
  return code;
1,301✔
336
}
337

338

339
int32_t sndProcessStreamMsg(SSnode *pSnode, void *pWorkerCb, SRpcMsg *pMsg) {
22,533,680✔
340
  int32_t code = 0, lino = 0;
22,533,680✔
341
  switch (pMsg->msgType) {
22,533,680!
342
    case TDMT_STREAM_TRIGGER_CALC:
8,056,760✔
343
      TAOS_CHECK_EXIT(handleTriggerCalcReq(pSnode, pWorkerCb, pMsg));
8,056,760✔
344
      break;
2,116,224✔
345
    case TDMT_STREAM_DELETE_CHECKPOINT:
7,752✔
346
      TAOS_CHECK_EXIT(handleSyncDeleteCheckPointReq(pSnode, pMsg));
7,752!
347
      break;
7,752✔
348
    case TDMT_STREAM_SYNC_CHECKPOINT:
7,752✔
349
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointReq(pSnode, pMsg));
7,752!
350
      break;
7,752✔
351
    case TDMT_STREAM_SYNC_CHECKPOINT_RSP:
7,752✔
352
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointRsp(pSnode, pMsg));
7,752!
353
      break;
7,752✔
354
    case TDMT_STREAM_FETCH_FROM_RUNNER:
113,690✔
355
      TAOS_CHECK_EXIT(handleStreamFetchData(pSnode, pWorkerCb, pMsg));
113,690!
356
      break;
113,690✔
357
    case TDMT_STREAM_FETCH_FROM_CACHE:
14,338,673✔
358
      TAOS_CHECK_EXIT(handleStreamFetchFromCache(pSnode, pMsg));
14,338,673!
359
      break;
14,338,673✔
360
      case TDMT_STREAM_TRIGGER_DROP:
1,301✔
361
     TAOS_CHECK_EXIT(handleStreamDropTableReq(pSnode, pMsg));
1,301!
362
      break;
1,301✔
363
    default:
×
364
      sndError("invalid snode msg:%d", pMsg->msgType);
×
365
      TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
366
  }
367

368
_exit:
×
369

370
  if (code) {
22,533,680✔
371
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
5,940,536!
372
  }
373
  
374
  return code;
22,533,680✔
375
}
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