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

taosdata / TDengine / #4733

15 Sep 2025 08:54AM UTC coverage: 58.074% (-0.9%) from 59.006%
#4733

push

travis-ci

web-flow
test: submit stream case test_idmp_privilege.py (#32954)

133369 of 292975 branches covered (45.52%)

Branch coverage included in aggregate %.

202038 of 284577 relevant lines covered (71.0%)

5476618.03 hits per line

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

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

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

37
  return pSnode;
213✔
38
}
39

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

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

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

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

66
_exit:
1,033✔
67

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

72
  streamReleaseTask(taskAddr);
1,095✔
73

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

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

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

92
  stDebug("[checkpoint] handleSyncWriteCheckPointReq streamId:%" PRIx64 ",ver:%d", streamId, ver);
31!
93
  void*   data = NULL;
31✔
94
  int64_t dataLen = 0;
31✔
95
  int32_t code = streamReadCheckPoint(streamId, &data, &dataLen);
31✔
96
  if (code != 0 || (terrno == TAOS_SYSTEM_ERROR(ENOENT) && ver == -1)){
31!
97
    goto end;
16✔
98
  }
99
  if (terrno == TAOS_SYSTEM_ERROR(ENOENT) || ver > *(int32_t*)POINTER_SHIFT(data, INT_BYTES)) {
15✔
100
    int32_t ret = streamWriteCheckPoint(streamId, POINTER_SHIFT(pRpcMsg->pCont, sizeof(SMsgHead)), pRpcMsg->contLen - sizeof(SMsgHead));
11✔
101
    stDebug("[checkpoint] streamId:%" PRIx64 ", checkpoint local updated, ver:%d, dataLen:%" PRId64 ", ret:%d", streamId, ver, dataLen, ret);
11!
102
  }
103
  if (terrno == TAOS_SYSTEM_ERROR(ENOENT) || ver >= *(int32_t*)POINTER_SHIFT(data, INT_BYTES)) {
15!
104
    stDebug("[checkpoint] streamId:%" PRIx64 ", checkpoint no need send back, ver:%d, dataLen:%" PRId64, streamId, ver, dataLen);
15!
105
    dataLen = 0;
15✔
106
    taosMemoryFreeClear(data);
15!
107
  }
108
  
109
end:
6✔
110
  if (data == NULL) {
31!
111
    rsp.contLen = 2 * INT_BYTES + LONG_BYTES;
31✔
112
    rsp.pCont = rpcMallocCont(rsp.contLen);
31✔
113
    if (rsp.pCont == NULL) {
31!
114
      rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
115
    } else {
116
      *(int32_t*)(POINTER_SHIFT(rsp.pCont, INT_BYTES)) = -1;  // no checkpoint
31✔
117
      *(int64_t*)(POINTER_SHIFT(rsp.pCont, 2 * INT_BYTES)) = streamId;
31✔
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);
31✔
131
  return 0;
31✔
132
}
133

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

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

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

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

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

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

193
end:
10,352✔
194
  rpcFreeCont(buf);
10,352✔
195
  return code;
10,352✔
196
}
197

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

207
  stDebug("handleStreamFetchData, msgType:%s, contLen:%d 0x%" PRIx64 ":0x%" PRIx64, 
11!
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));
11!
211

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

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

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

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

234
_exit:
11✔
235

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

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

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

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

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

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

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

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