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

taosdata / TDengine / #4973

03 Mar 2026 09:57AM UTC coverage: 67.69% (+0.05%) from 67.642%
#4973

push

travis-ci

web-flow
test(query): fix result of explain cases (#34658)

208339 of 307783 relevant lines covered (67.69%)

131201157.41 hits per line

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

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

35
  pSnode->msgCb = pOption->msgCb;
63,637✔
36

37
  return pSnode;
63,637✔
38
}
39

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

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

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

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

66
_exit:
2,607,304✔
67

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

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

80
  if (code) {
2,607,521✔
81
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
1,968,693✔
82
  }
83
  
84
  return code;
2,607,521✔
85
}
86

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

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

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

140
  return 0;
4,264✔
141
}
142

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

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

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

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

175
  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
5,070,883✔
176
  pRetrieve->version = 0;
5,070,883✔
177
  pRetrieve->precision = precision;
5,070,883✔
178
  pRetrieve->compressed = 0;
5,070,883✔
179
  *((int32_t*)(pRetrieve->data)) = blockSize;
5,070,883✔
180
  *((int32_t*)(pRetrieve->data + INT_BYTES)) = blockSize;
5,070,883✔
181
  if (pBlock == NULL || pBlock->info.rows == 0) {
5,070,883✔
182
    pRetrieve->numOfRows = 0;
2,808,074✔
183
    pRetrieve->numOfBlocks = 0;
2,807,642✔
184
    pRetrieve->completed = 1;
2,807,642✔
185
  } else {
186
    pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
2,263,025✔
187
    pRetrieve->numOfBlocks = htonl(1);
2,263,457✔
188
    int32_t actualLen = blockEncodeInternal(pBlock, pRetrieve->data + INT_BYTES * 2, blockSize, taosArrayGetSize(pBlock->pDataBlock));
2,263,241✔
189
    if (actualLen < 0) {
2,262,752✔
190
      code = terrno;
×
191
      goto end;
×
192
    }
193
  }
194
  if (finished) {
5,070,394✔
195
    pRetrieve->completed = 1;
3,050,442✔
196
  }
197

198
  *data = buf;
5,070,394✔
199
  *size = dataEncodeBufSize;
5,069,689✔
200
  buf = NULL;
5,069,473✔
201

202
end:
5,069,473✔
203
  rpcFreeCont(buf);
5,069,473✔
204
  return code;
5,069,473✔
205
}
206

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

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

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

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

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

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

245
_exit:
131,332✔
246

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

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

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

270
  streamId = req.queryId;
4,939,333✔
271
  readInfo.taskInfo.streamId = req.queryId;
4,939,333✔
272
  readInfo.taskInfo.taskId = req.taskId;
4,939,333✔
273
  readInfo.taskInfo.sessionId = req.pStRtFuncInfo->sessionId;
4,939,333✔
274
  readInfo.gid = req.pStRtFuncInfo->groupId;
4,939,549✔
275
  //SSTriggerCalcParam* pParam = taosArrayGet(req.pStRtFuncInfo->pStreamPesudoFuncVals, req.pStRtFuncInfo->curIdx);
276
  readInfo.start = req.pStRtFuncInfo->curWindow.skey;
4,939,549✔
277
  readInfo.end = req.pStRtFuncInfo->curWindow.ekey;
4,939,549✔
278
  bool finished;
4,938,621✔
279
  TAOS_CHECK_EXIT(stRunnerFetchDataFromCache(&readInfo,&finished));
4,939,767✔
280

281
  TAOS_CHECK_EXIT(buildStreamFetchRsp(readInfo.pBlock, &buf, &size, 0, finished));
4,939,551✔
282

283
_exit:
4,938,141✔
284

285
  printDataBlock(readInfo.pBlock, __func__, "fetchFromCache", streamId);
4,938,141✔
286

287
  stsDebug("task %" PRIx64 " TDMT_STREAM_FETCH_FROM_CACHE_RSP with code:%d rows:%" PRId64 ", size:%d, time range:[%" PRId64 ", %" PRId64 "]", 
4,939,062✔
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};
4,939,767✔
291
  tmsgSendRsp(&rsp);
4,939,335✔
292

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

297
  blockDataDestroy(readInfo.pBlock);
4,939,549✔
298
  tDestroySResFetchReq(&req);
4,939,767✔
299
  
300
  return code;
4,939,062✔
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) {
246✔
316
  SSTriggerDropRequest req = {0};
246✔
317
  SStreamRunnerTask* pTask = NULL;
246✔
318
  void* taskAddr = NULL;
246✔
319
  int32_t code = 0, lino = 0;
246✔
320
  TAOS_CHECK_EXIT(tDeserializeSTriggerDropTableRequest(POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead)), pRpcMsg->contLen - sizeof(SMsgHead), &req));
246✔
321
  TAOS_CHECK_EXIT(streamAcquireTask(req.streamId, req.runnerTaskId, (SStreamTask**)&pTask, &taskAddr));
246✔
322
  
323
  pTask->msgCb = pSnode->msgCb;
246✔
324
  TAOS_CHECK_EXIT(stRunnerTaskDropTable(pTask, &req));
246✔
325

326
_exit:
246✔
327
  tDestroySSTriggerDropRequest(&req);
246✔
328
  if (code) {
246✔
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};
246✔
333
    tmsgSendRsp(&rsp);
246✔
334
  }
335
  streamReleaseTask(taskAddr);
246✔
336

337
  return code;
246✔
338
}
339

340

341
int32_t sndProcessStreamMsg(SSnode *pSnode, void *pWorkerCb, SRpcMsg *pMsg) {
7,688,704✔
342
  int32_t code = 0, lino = 0;
7,688,704✔
343
  switch (pMsg->msgType) {
7,688,704✔
344
    case TDMT_STREAM_TRIGGER_CALC:
2,607,521✔
345
      TAOS_CHECK_EXIT(handleTriggerCalcReq(pSnode, pWorkerCb, pMsg));
2,607,521✔
346
      break;
638,828✔
347
    case TDMT_STREAM_DELETE_CHECKPOINT:
1,256✔
348
      TAOS_CHECK_EXIT(handleSyncDeleteCheckPointReq(pSnode, pMsg));
1,256✔
349
      break;
1,256✔
350
    case TDMT_STREAM_SYNC_CHECKPOINT:
4,264✔
351
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointReq(pSnode, pMsg));
4,264✔
352
      break;
4,264✔
353
    case TDMT_STREAM_SYNC_CHECKPOINT_RSP:
4,318✔
354
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointRsp(pSnode, pMsg));
4,318✔
355
      break;
3,288✔
356
    case TDMT_STREAM_FETCH_FROM_RUNNER:
131,332✔
357
      TAOS_CHECK_EXIT(handleStreamFetchData(pSnode, pWorkerCb, pMsg));
131,332✔
358
      break;
131,332✔
359
    case TDMT_STREAM_FETCH_FROM_CACHE:
4,939,767✔
360
      TAOS_CHECK_EXIT(handleStreamFetchFromCache(pSnode, pMsg));
4,939,767✔
361
      break;
4,939,062✔
362
      case TDMT_STREAM_TRIGGER_DROP:
246✔
363
     TAOS_CHECK_EXIT(handleStreamDropTableReq(pSnode, pMsg));
246✔
364
      break;
246✔
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) {
7,687,999✔
373
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
1,969,723✔
374
  }
375
  
376
  return code;
7,688,704✔
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