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

taosdata / TDengine / #4688

26 Aug 2025 02:05PM UTC coverage: 56.997% (-0.9%) from 57.894%
#4688

push

travis-ci

web-flow
fix: modify the prompt language of the taos-shell (#32758)

* fix: modify prompt language

* fix: add shell test case

* fix: modify comments

* fix: modify test case for TDengine TSDB

130660 of 292423 branches covered (44.68%)

Branch coverage included in aggregate %.

16 of 17 new or added lines in 2 files covered. (94.12%)

9459 existing lines in 157 files now uncovered.

198294 of 284715 relevant lines covered (69.65%)

4532552.29 hits per line

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

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

35
  pSnode->msgCb = pOption->msgCb;
210✔
36

37
  return pSnode;
210✔
38
}
39

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

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

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

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

66
_exit:
1,423✔
67

68
  tDestroySTriggerCalcRequest(&req);
1,496✔
69
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_TRIGGER_CALC_RSP, .contLen = 0, .pCont = NULL, .info = pRpcMsg->info};
1,496✔
70
  rpcSendResponse(&rsp);
1,496✔
71

72
  streamReleaseTask(taskAddr);
1,496✔
73

74
  if (code) {
1,496✔
75
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
76!
76
  }
77
  
78
  return code;
1,496✔
79
}
80

81
static int32_t handleSyncDeleteCheckPointReq(SSnode* pSnode, SRpcMsg* pRpcMsg) {
6✔
82
  int64_t streamId = *(int64_t*)POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead));
6✔
83
  streamDeleteCheckPoint(streamId);
6✔
84
  return 0;
6✔
85
}
86

87
static int32_t handleSyncWriteCheckPointReq(SSnode* pSnode, SRpcMsg* pRpcMsg) {
19✔
88
  int32_t ver = *(int32_t*)POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead) + INT_BYTES);
19✔
89
  int64_t streamId = *(int64_t*)POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead) + 2 * INT_BYTES);
19✔
90
  SRpcMsg rsp = {.code = 0, .msgType = TDMT_STREAM_SYNC_CHECKPOINT_RSP, .info = pRpcMsg->info};
19✔
91

92
  stDebug("[checkpoint] handleSyncWriteCheckPointReq streamId:%" PRIx64 ",ver:%d", streamId, ver);
19!
93
  void*   data = NULL;
19✔
94
  int64_t dataLen = 0;
19✔
95
  int32_t code = streamReadCheckPoint(streamId, &data, &dataLen);
19✔
96
  if (code != 0 || (terrno == TAOS_SYSTEM_ERROR(ENOENT) && ver == -1)){
19!
97
    goto end;
9✔
98
  }
99
  if (terrno == TAOS_SYSTEM_ERROR(ENOENT) || ver > *(int32_t*)POINTER_SHIFT(data, INT_BYTES)) {
10✔
100
    int32_t ret = streamWriteCheckPoint(streamId, POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead)), pRpcMsg->contLen - sizeof(SMsgHead));
8✔
101
    stDebug("[checkpoint] streamId:%" PRIx64 ", checkpoint local updated, ver:%d, dataLen:%" PRId64 ", ret:%d", streamId, ver, dataLen, ret);
8!
102
  }
103
  if (terrno == TAOS_SYSTEM_ERROR(ENOENT) || ver >= *(int32_t*)POINTER_SHIFT(data, INT_BYTES)) {
10!
104
    stDebug("[checkpoint] streamId:%" PRIx64 ", checkpoint no need send back, ver:%d, dataLen:%" PRId64, streamId, ver, dataLen);
10!
105
    dataLen = 0;
10✔
106
    taosMemoryFreeClear(data);
10!
107
  }
108
  
109
end:
6✔
110
  if (data == NULL) {
19!
111
    rsp.contLen = 2 * INT_BYTES + LONG_BYTES;
19✔
112
    rsp.pCont = rpcMallocCont(rsp.contLen);
19✔
113
    if (rsp.pCont == NULL) {
19!
114
      rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
115
    } else {
116
      *(int32_t*)(POINTER_SHIFT(rsp.pCont, INT_BYTES)) = -1;  // no checkpoint
19✔
117
      *(int64_t*)(POINTER_SHIFT(rsp.pCont, 2 * INT_BYTES)) = streamId;
19✔
118
    }
119
  } else {
UNCOV
120
    rsp.pCont = rpcMallocCont(dataLen);
×
UNCOV
121
    if (rsp.pCont == NULL) {
×
122
      rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
123
    } else {
UNCOV
124
      memcpy(rsp.pCont, data, dataLen);
×
UNCOV
125
      rsp.contLen = dataLen;
×
UNCOV
126
      taosMemoryFreeClear(data); 
×
127
    } 
128
  }
129
  
130
  rpcSendResponse(&rsp);
19✔
131
  return 0;
19✔
132
}
133

134
static int32_t handleSyncWriteCheckPointRsp(SSnode* pSnode, SRpcMsg* pRpcMsg) {
19✔
135
  if (pRpcMsg->code != 0) {
19!
136
    stError("[checkpoint] handleSyncWriteCheckPointRsp, code:%d, msgType:%d", pRpcMsg->code, pRpcMsg->msgType);
×
137
    return pRpcMsg->code;
×
138
  } 
139
  void* data = pRpcMsg->pCont;
19✔
140
  int32_t dataLen = pRpcMsg->contLen;
19✔
141
  
142
  int32_t ver = *(int32_t*)(POINTER_SHIFT(data, INT_BYTES));
19✔
143
  int64_t streamId = *(int64_t*)(POINTER_SHIFT(data, 2 * INT_BYTES));
19✔
144
  stDebug("[checkpoint] handleSyncWriteCheckPointRsp, ver:%d, streamId:%"PRIx64",dataLen:%d", 
19!
145
    ver, streamId, dataLen);
146

147
  if (ver != -1){
19!
UNCOV
148
    (void)streamWriteCheckPoint(streamId, data, dataLen);
×
149
  }
150
  return streamCheckpointSetReady(streamId);
19✔
151
}
152

153
static int32_t buildFetchRsp(SSDataBlock* pBlock, void** data, size_t* size, int8_t precision, bool finished) {
13,887✔
154
  int32_t code = 0;
13,887✔
155
  int32_t lino = 0;
13,887✔
156
  void*   buf =  NULL;
13,887✔
157

158
  int32_t blockSize = pBlock == NULL ? 0 : blockGetEncodeSize(pBlock);
13,887✔
159
  size_t dataEncodeBufSize = sizeof(SRetrieveTableRsp) + INT_BYTES * 2 + blockSize;
13,888✔
160
  buf = rpcMallocCont(dataEncodeBufSize);
13,888✔
161
  if (!buf) {
13,887!
162
    code = terrno;
×
163
    goto end;
×
164
  }
165

166
  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
13,888✔
167
  pRetrieve->version = 0;
13,888✔
168
  pRetrieve->precision = precision;
13,888✔
169
  pRetrieve->compressed = 0;
13,888✔
170
  *((int32_t*)(pRetrieve->data)) = blockSize;
13,888✔
171
  *((int32_t*)(pRetrieve->data + INT_BYTES)) = blockSize;
13,888✔
172
  if (pBlock == NULL || pBlock->info.rows == 0) {
13,888!
173
    pRetrieve->numOfRows = 0;
7,610✔
174
    pRetrieve->numOfBlocks = 0;
7,610✔
175
    pRetrieve->completed = 1;
7,610✔
176
  } else {
177
    pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
6,278✔
178
    pRetrieve->numOfBlocks = htonl(1);
6,279✔
179
    int32_t actualLen = blockEncode(pBlock, pRetrieve->data + INT_BYTES * 2, blockSize, taosArrayGetSize(pBlock->pDataBlock));
6,279✔
180
    if (actualLen < 0) {
6,279!
181
      code = terrno;
×
182
      goto end;
×
183
    }
184
  }
185
  if (finished) {
13,889✔
186
    pRetrieve->completed = 1;
8,642✔
187
  }
188

189
  *data = buf;
13,889✔
190
  *size = dataEncodeBufSize;
13,889✔
191
  buf = NULL;
13,889✔
192

193
end:
13,889✔
194
  rpcFreeCont(buf);
13,889✔
195
  return code;
13,886✔
196
}
197

198
static int32_t handleStreamFetchData(SSnode* pSnode, void *pWorkerCb, SRpcMsg* pRpcMsg) {
×
199
  int32_t code = 0, lino = 0;
×
200
  void* taskAddr = NULL;
×
201
  SResFetchReq req = {0};
×
202
  SSTriggerCalcRequest calcReq = {0};
×
203
  SStreamRunnerTask* pTask = NULL;
×
204
  void* buf = NULL;
×
205
  size_t size = 0;
×
206

207
  stDebug("handleStreamFetchData, msgType:%s, contLen:%d 0x%" PRIx64 ":0x%" PRIx64, 
×
208
      TMSG_INFO(pRpcMsg->msgType), pRpcMsg->contLen, TRACE_GET_ROOTID(&pRpcMsg->info.traceId), TRACE_GET_MSGID(&pRpcMsg->info.traceId));
209
  
210
  TAOS_CHECK_EXIT(tDeserializeSResFetchReq(pRpcMsg->pCont,pRpcMsg->contLen, &req));
×
211

212
  calcReq.streamId = req.queryId;
×
213
  calcReq.runnerTaskId = req.taskId;
×
214
  calcReq.brandNew = req.reset;
×
215
  calcReq.execId = req.execId;
×
216
  calcReq.sessionId = req.pStRtFuncInfo->sessionId;
×
217
  calcReq.triggerType = req.pStRtFuncInfo->triggerType;
×
218
  TSWAP(calcReq.groupColVals, req.pStRtFuncInfo->pStreamPartColVals);
×
219
  TSWAP(calcReq.params, req.pStRtFuncInfo->pStreamPesudoFuncVals);
×
220
  calcReq.gid = req.pStRtFuncInfo->groupId;
×
221
  calcReq.curWinIdx = req.pStRtFuncInfo->curIdx;
×
222
  calcReq.pOutBlock = NULL;
×
223

224
  TAOS_CHECK_EXIT(streamAcquireTask(calcReq.streamId, calcReq.runnerTaskId, (SStreamTask**)&pTask, &taskAddr));
×
225

226
  pTask->msgCb = pSnode->msgCb;
×
227
  //pTask->pMsgCb = &pSnode->msgCb;
228
  pTask->pWorkerCb = pWorkerCb;
×
229
  
230
  TAOS_CHECK_EXIT(stRunnerTaskExecute(pTask, &calcReq));
×
231

232
  TAOS_CHECK_EXIT(buildFetchRsp(calcReq.pOutBlock, &buf, &size, 0, false));
×
233

234
_exit:
×
235

236
  tDestroySTriggerCalcRequest(&calcReq);
×
237
  tDestroySResFetchReq(&req);
×
238
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_FETCH_FROM_RUNNER_RSP, .contLen = size, .pCont = buf, .info = pRpcMsg->info};
×
239
  tmsgSendRsp(&rsp);
×
240
  
241
  streamReleaseTask(taskAddr);
×
242

243
  if (code) {
×
244
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
245
  }
246
  
247
  return code;
×
248
}
249

250
static int32_t handleStreamFetchFromCache(SSnode* pSnode, SRpcMsg* pRpcMsg) {
13,888✔
251
  int32_t code = 0, lino = 0;
13,888✔
252
  SResFetchReq req = {0};
13,888✔
253
  SStreamCacheReadInfo readInfo = {0};
13,888✔
254
  void* buf = NULL;
13,888✔
255
  int64_t streamId = 0;
13,888✔
256
  size_t size = 0;
13,888✔
257
  TAOS_CHECK_EXIT(tDeserializeSResFetchReq(pRpcMsg->pCont, pRpcMsg->contLen, &req));
13,888!
258

259
  streamId = req.queryId;
13,890✔
260
  readInfo.taskInfo.streamId = req.queryId;
13,890✔
261
  readInfo.taskInfo.taskId = req.taskId;
13,890✔
262
  readInfo.taskInfo.sessionId = req.pStRtFuncInfo->sessionId;
13,890✔
263
  readInfo.gid = req.pStRtFuncInfo->groupId;
13,890✔
264
  SSTriggerCalcParam* pParam = taosArrayGet(req.pStRtFuncInfo->pStreamPesudoFuncVals, req.pStRtFuncInfo->curIdx);
13,890✔
265
  readInfo.start = pParam->wstart;
13,890✔
266
  readInfo.end = pParam->wend;
13,890✔
267
  bool finished;
268
  TAOS_CHECK_EXIT(stRunnerFetchDataFromCache(&readInfo,&finished));
13,890!
269

270
  TAOS_CHECK_EXIT(buildFetchRsp(readInfo.pBlock, &buf, &size, 0, finished));
13,888!
271

272
_exit:
13,886✔
273

274
  stsDebug("task %" PRIx64 " TDMT_STREAM_FETCH_FROM_CACHE_RSP with code:%d rows:%" PRId64 ", size:%d", req.taskId, code, readInfo.pBlock ? readInfo.pBlock->info.rows : 0, (int32_t)size);  
13,886✔
275
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_FETCH_FROM_CACHE_RSP, .contLen = size, .pCont = buf, .info = pRpcMsg->info};
13,886✔
276
  tmsgSendRsp(&rsp);
13,886✔
277

278
  if (code) {
13,888!
279
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
280
  }
281

282
  blockDataDestroy(readInfo.pBlock);
13,888✔
283
  tDestroySResFetchReq(&req);
13,889✔
284
  
285
  return code;
13,889✔
286
}
287

288
static void sndSendErrorRrsp(SRpcMsg *pMsg, int32_t errCode) {
×
289
  SRpcMsg             rspMsg = {0};
×
290

291
  rspMsg.info = pMsg->info;
×
292
  rspMsg.pCont = NULL;
×
293
  rspMsg.contLen = 0;
×
294
  rspMsg.code = errCode;
×
295
  rspMsg.msgType = pMsg->msgType;
×
296

297
  tmsgSendRsp(&rspMsg);
×
298
}
×
299

300

301
int32_t sndProcessStreamMsg(SSnode *pSnode, void *pWorkerCb, SRpcMsg *pMsg) {
15,428✔
302
  int32_t code = 0, lino = 0;
15,428✔
303
  switch (pMsg->msgType) {
15,428!
304
    case TDMT_STREAM_TRIGGER_CALC:
1,496✔
305
      TAOS_CHECK_EXIT(handleTriggerCalcReq(pSnode, pWorkerCb, pMsg));
1,496✔
306
      break;
1,423✔
307
    case TDMT_STREAM_DELETE_CHECKPOINT:
6✔
308
      TAOS_CHECK_EXIT(handleSyncDeleteCheckPointReq(pSnode, pMsg));
6!
309
      break;
6✔
310
    case TDMT_STREAM_SYNC_CHECKPOINT:
19✔
311
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointReq(pSnode, pMsg));
19!
312
      break;
19✔
313
    case TDMT_STREAM_SYNC_CHECKPOINT_RSP:
19✔
314
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointRsp(pSnode, pMsg));
19!
315
      break;
19✔
316
    case TDMT_STREAM_FETCH_FROM_RUNNER:
×
317
      TAOS_CHECK_EXIT(handleStreamFetchData(pSnode, pWorkerCb, pMsg));
×
318
      break;
×
319
    case TDMT_STREAM_FETCH_FROM_CACHE:
13,889✔
320
      TAOS_CHECK_EXIT(handleStreamFetchFromCache(pSnode, pMsg));
13,889!
321
      break;
13,889✔
322
    default:
×
323
      sndError("invalid snode msg:%d", pMsg->msgType);
×
324
      TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
325
  }
326

327
_exit:
×
328

329
  if (code) {
15,429✔
330
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
76!
331
  }
332
  
333
  return code;
15,429✔
334
}
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