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

taosdata / TDengine / #4703

01 Sep 2025 11:36AM UTC coverage: 58.16% (-0.9%) from 59.056%
#4703

push

travis-ci

web-flow
Merge pull request #32834 from taosdata/docs/wangxu/simplify-get-started

refactor: simplify get started

133101 of 291897 branches covered (45.6%)

Branch coverage included in aggregate %.

201587 of 283568 relevant lines covered (71.09%)

5506801.45 hits per line

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

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

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

37
  return pSnode;
212✔
38
}
39

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

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

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

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

66
_exit:
1,137✔
67

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

72
  streamReleaseTask(taskAddr);
1,198✔
73

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

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

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

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

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

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

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

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

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

189
  *data = buf;
10,911✔
190
  *size = dataEncodeBufSize;
10,911✔
191
  buf = NULL;
10,911✔
192

193
end:
10,911✔
194
  rpcFreeCont(buf);
10,911✔
195
  return code;
10,911✔
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) {
10,912✔
251
  int32_t code = 0, lino = 0;
10,912✔
252
  SResFetchReq req = {0};
10,912✔
253
  SStreamCacheReadInfo readInfo = {0};
10,912✔
254
  void* buf = NULL;
10,912✔
255
  int64_t streamId = 0;
10,912✔
256
  size_t size = 0;
10,912✔
257
  TAOS_CHECK_EXIT(tDeserializeSResFetchReq(pRpcMsg->pCont, pRpcMsg->contLen, &req));
10,912!
258

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

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

272
_exit:
10,911✔
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);  
10,911✔
275
  SRpcMsg rsp = {.code = code, .msgType = TDMT_STREAM_FETCH_FROM_CACHE_RSP, .contLen = size, .pCont = buf, .info = pRpcMsg->info};
10,911✔
276
  tmsgSendRsp(&rsp);
10,911✔
277

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

282
  blockDataDestroy(readInfo.pBlock);
10,912✔
283
  tDestroySResFetchReq(&req);
10,912✔
284
  
285
  return code;
10,911✔
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) {
12,171✔
302
  int32_t code = 0, lino = 0;
12,171✔
303
  switch (pMsg->msgType) {
12,171!
304
    case TDMT_STREAM_TRIGGER_CALC:
1,198✔
305
      TAOS_CHECK_EXIT(handleTriggerCalcReq(pSnode, pWorkerCb, pMsg));
1,198✔
306
      break;
1,137✔
307
    case TDMT_STREAM_DELETE_CHECKPOINT:
13✔
308
      TAOS_CHECK_EXIT(handleSyncDeleteCheckPointReq(pSnode, pMsg));
13!
309
      break;
13✔
310
    case TDMT_STREAM_SYNC_CHECKPOINT:
24✔
311
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointReq(pSnode, pMsg));
24!
312
      break;
24✔
313
    case TDMT_STREAM_SYNC_CHECKPOINT_RSP:
24✔
314
      TAOS_CHECK_EXIT(handleSyncWriteCheckPointRsp(pSnode, pMsg));
24✔
315
      break;
23✔
316
    case TDMT_STREAM_FETCH_FROM_RUNNER:
×
317
      TAOS_CHECK_EXIT(handleStreamFetchData(pSnode, pWorkerCb, pMsg));
×
318
      break;
×
319
    case TDMT_STREAM_FETCH_FROM_CACHE:
10,912✔
320
      TAOS_CHECK_EXIT(handleStreamFetchFromCache(pSnode, pMsg));
10,912!
321
      break;
10,911✔
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) {
12,170✔
330
    sndError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
64!
331
  }
332
  
333
  return code;
12,170✔
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