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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

web-flow
Merge pull request #33435 from taosdata/3.0

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

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

35
  pSnode->msgCb = pOption->msgCb;
5,613✔
36

37
  return pSnode;
5,613✔
38
}
39

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

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

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

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

66
_exit:
3,715,468✔
67

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

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

80
  if (code) {
3,715,468✔
81
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
3,593,037!
82
  }
83
  
84
  return code;
3,716,371✔
85
}
86

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

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

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

UNCOV
140
  return 0;
×
141
}
142

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

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

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

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

175
  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
450,478✔
176
  pRetrieve->version = 0;
450,478✔
177
  pRetrieve->precision = precision;
450,478✔
178
  pRetrieve->compressed = 0;
450,478✔
179
  *((int32_t*)(pRetrieve->data)) = blockSize;
450,478✔
180
  *((int32_t*)(pRetrieve->data + INT_BYTES)) = blockSize;
450,478✔
181
  if (pBlock == NULL || pBlock->info.rows == 0) {
450,478!
182
    pRetrieve->numOfRows = 0;
107,035✔
183
    pRetrieve->numOfBlocks = 0;
107,035✔
184
    pRetrieve->completed = 1;
107,035✔
185
  } else {
186
    pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
343,443✔
187
    pRetrieve->numOfBlocks = htonl(1);
343,443✔
188
    int32_t actualLen = blockEncodeInternal(pBlock, pRetrieve->data + INT_BYTES * 2, blockSize, taosArrayGetSize(pBlock->pDataBlock));
343,443✔
189
    if (actualLen < 0) {
343,443!
190
      code = terrno;
×
191
      goto end;
×
192
    }
193
  }
194
  if (finished) {
450,478✔
195
    pRetrieve->completed = 1;
182,294✔
196
  }
197

198
  *data = buf;
450,478✔
199
  *size = dataEncodeBufSize;
450,478✔
200
  buf = NULL;
450,478✔
201

202
end:
450,478✔
203
  rpcFreeCont(buf);
450,478✔
204
  return code;
450,478✔
205
}
206

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

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

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

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

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

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

243
_exit:
22,436✔
244

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

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

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

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

279
  TAOS_CHECK_EXIT(buildStreamFetchRsp(readInfo.pBlock, &buf, &size, 0, finished));
428,042!
280

281
_exit:
428,042✔
282

283
  printDataBlock(readInfo.pBlock, __func__, "fetchFromCache", streamId);
428,042✔
284

285
  stsDebug("task %" PRIx64 " TDMT_STREAM_FETCH_FROM_CACHE_RSP with code:%d rows:%" PRId64 ", size:%d, time range:[%" PRId64 ", %" PRId64 "]", 
427,735✔
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};
427,735✔
289
  tmsgSendRsp(&rsp);
428,042✔
290

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

295
  blockDataDestroy(readInfo.pBlock);
428,042✔
296
  tDestroySResFetchReq(&req);
428,042✔
297
  
298
  return code;
427,735✔
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

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

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

UNCOV
335
  return code;
×
336
}
337

338

339
int32_t sndProcessStreamMsg(SSnode *pSnode, void *pWorkerCb, SRpcMsg *pMsg) {
4,165,645✔
340
  int32_t code = 0, lino = 0;
4,165,645✔
341
  switch (pMsg->msgType) {
4,165,645!
342
    case TDMT_STREAM_TRIGGER_CALC:
3,715,468✔
343
      TAOS_CHECK_EXIT(handleTriggerCalcReq(pSnode, pWorkerCb, pMsg));
3,715,468✔
344
      break;
121,528✔
UNCOV
345
    case TDMT_STREAM_DELETE_CHECKPOINT:
×
UNCOV
346
      TAOS_CHECK_EXIT(handleSyncDeleteCheckPointReq(pSnode, pMsg));
×
UNCOV
347
      break;
×
UNCOV
348
    case TDMT_STREAM_SYNC_CHECKPOINT:
×
UNCOV
349
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointReq(pSnode, pMsg));
×
UNCOV
350
      break;
×
UNCOV
351
    case TDMT_STREAM_SYNC_CHECKPOINT_RSP:
×
UNCOV
352
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointRsp(pSnode, pMsg));
×
UNCOV
353
      break;
×
354
    case TDMT_STREAM_FETCH_FROM_RUNNER:
22,436✔
355
      TAOS_CHECK_EXIT(handleStreamFetchData(pSnode, pWorkerCb, pMsg));
22,436!
356
      break;
22,436✔
357
    case TDMT_STREAM_FETCH_FROM_CACHE:
428,042✔
358
      TAOS_CHECK_EXIT(handleStreamFetchFromCache(pSnode, pMsg));
428,042!
359
      break;
427,735✔
UNCOV
360
      case TDMT_STREAM_TRIGGER_DROP:
×
UNCOV
361
     TAOS_CHECK_EXIT(handleStreamDropTableReq(pSnode, pMsg));
×
UNCOV
362
      break;
×
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) {
4,165,639✔
371
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
3,593,940!
372
  }
373
  
374
  return code;
4,165,639✔
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