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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

60.73
/source/dnode/vnode/src/vnd/vnodeSvr.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 "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

29
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
30
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
31
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
32
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
33
                                       SRpcMsg *pOriginRpc);
34
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
36
                                     SRpcMsg *pOriginRpc);
37
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
38
                                     SRpcMsg *pOriginalMsg);
39
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
40
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
41
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
42
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
43
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
44
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
45
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
46
                                     SRpcMsg *pOriginalMsg);
47
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
48
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
51
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
52
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
54
                                          SRpcMsg *pOriginRpc);
55

56
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
58
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59

60
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
61
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
62

63
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
128,119✔
64
  int32_t code = 0;
128,119✔
65
  int32_t lino = 0;
128,119✔
66

67
  if (tStartDecode(pCoder) < 0) {
128,119!
68
    code = TSDB_CODE_INVALID_MSG;
×
69
    TSDB_CHECK_CODE(code, lino, _exit);
×
70
  }
71

72
  // flags
73
  if (tDecodeI32v(pCoder, NULL) < 0) {
128,166!
74
    code = TSDB_CODE_INVALID_MSG;
×
75
    TSDB_CHECK_CODE(code, lino, _exit);
×
76
  }
77

78
  // name
79
  char *name = NULL;
128,166✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
128,116!
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
128,116✔
87
  if (uid == 0) {
128,158✔
88
    uid = tGenIdPI64();
120,776✔
89
  }
90
  *(int64_t *)(pCoder->data + pCoder->pos) = uid;
128,167✔
91

92
  // btime
93
  *(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
128,167✔
94

95
  tEndDecode(pCoder);
128,167✔
96

97
_exit:
128,126✔
98
  if (code) {
128,126!
99
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
100
  } else {
101
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
128,126✔
102
    if (pUid) *pUid = uid;
128,126✔
103
  }
104
  return code;
128,128✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
60,497✔
107
  int32_t code = 0;
60,497✔
108
  int32_t lino = 0;
60,497✔
109

110
  int64_t  btime = taosGetTimestampMs();
60,508✔
111
  SDecoder dc = {0};
60,508✔
112
  int32_t  nReqs;
113

114
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
60,508✔
115
  if (tStartDecode(&dc) < 0) {
60,496!
116
    code = TSDB_CODE_INVALID_MSG;
×
117
    return code;
×
118
  }
119

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
60,501!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
145,359✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
84,892✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
84,858!
127
  }
128

129
  tEndDecode(&dc);
60,467✔
130

131
_exit:
60,501✔
132
  tDecoderClear(&dc);
60,501✔
133
  if (code) {
60,516!
134
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
135
           tstrerror(code), TMSG_INFO(pMsg->msgType));
136
  }
137
  return code;
60,514✔
138
}
139

140
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
985✔
141
  int32_t code = TSDB_CODE_INVALID_MSG;
985✔
142
  int32_t lino = 0;
985✔
143

144
  SDecoder dc = {0};
985✔
145
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
985✔
146

147
  SVAlterTbReq vAlterTbReq = {0};
985✔
148
  int64_t      ctimeMs = taosGetTimestampMs();
985✔
149
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
985!
150
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
151
    vAlterTbReq.pMultiTag = NULL;
×
152
    goto _exit;
×
153
  }
154
  taosArrayDestroy(vAlterTbReq.pMultiTag);
985✔
155
  vAlterTbReq.pMultiTag = NULL;
985✔
156

157
  code = 0;
985✔
158

159
_exit:
985✔
160
  tDecoderClear(&dc);
985✔
161
  if (code) {
985!
162
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
163
  } else {
164
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
985✔
165
           ctimeMs);
166
  }
167
  return code;
985✔
168
}
169

170
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
16,792✔
171
  int32_t code = TSDB_CODE_INVALID_MSG;
16,792✔
172
  int32_t lino = 0;
16,792✔
173

174
  SMsgHead *pContOld = pMsg->pCont;
16,792✔
175
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
16,792✔
176

177
  SArray *tbUids = NULL;
16,792✔
178
  int64_t timestampMs = 0;
16,792✔
179

180
  SVDropTtlTableReq ttlReq = {0};
16,792✔
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
16,792!
182
    code = TSDB_CODE_INVALID_MSG;
×
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
  {  // find expired uids
187
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
16,795✔
188
    if (tbUids == NULL) {
16,797!
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

193
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
16,797✔
194
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
16,797✔
195
    if (code != 0) {
16,780!
196
      code = TSDB_CODE_INVALID_MSG;
×
197
      TSDB_CHECK_CODE(code, lino, _exit);
×
198
    }
199

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
16,780✔
201
    ttlReq.pTbUids = tbUids;
16,775✔
202
  }
203

204
  if (ttlReq.nUids == 0) {
16,775✔
205
    code = TSDB_CODE_MSG_PREPROCESSED;
16,770✔
206
    TSDB_CHECK_CODE(code, lino, _exit);
16,770!
207
  }
208

209
  {  // prepare new content
210
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
211
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
5✔
212

213
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
5✔
214
    if (pContNew == NULL) {
5!
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

219
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
5!
220
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
5!
221
    }
222
    pContNew->contLen = htonl(reqLenNew);
5✔
223
    pContNew->vgId = pContOld->vgId;
5✔
224

225
    rpcFreeCont(pContOld);
5✔
226
    pMsg->pCont = pContNew;
5✔
227
    pMsg->contLen = contLenNew;
5✔
228
  }
229

230
  code = 0;
5✔
231

232
_exit:
16,782✔
233
  taosArrayDestroy(tbUids);
16,782✔
234

235
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
16,777!
236
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
237
           tstrerror(code), TMSG_INFO(pMsg->msgType));
238
  } else {
239
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
16,777✔
240
  }
241

242
  return code;
16,780✔
243
}
244

245
extern int64_t tsMaxKeyByPrecision[];
246
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
10,156,675✔
247
  int32_t code = 0;
10,156,675✔
248
  int32_t lino = 0;
10,156,675✔
249

250
  if (tStartDecode(pCoder) < 0) {
10,156,675!
251
    code = TSDB_CODE_INVALID_MSG;
×
252
    TSDB_CHECK_CODE(code, lino, _exit);
×
253
  }
254

255
  SSubmitTbData submitTbData;
256
  uint8_t       version;
257
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
10,177,438!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
10,177,438✔
262
  submitTbData.flags = submitTbData.flags & 0xff;
10,177,438✔
263

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,177,438✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
43,236✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
43,255!
268
  }
269

270
  // submit data
271
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,162,292!
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,162,292✔
277
    *(int64_t *)(pCoder->data + pCoder->pos) = uid;
43,251✔
278
    pCoder->pos += sizeof(int64_t);
43,251✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,113,149!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

286
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,145,625!
287
    code = TSDB_CODE_INVALID_MSG;
×
288
    TSDB_CHECK_CODE(code, lino, _exit);
×
289
  }
290

291
  // scan and check
292
  TSKEY now = btimeMs;
10,145,625✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,145,625✔
294
    now *= 1000;
22,345✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,123,280✔
296
    now *= 1000000;
3,557✔
297
  }
298

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,145,625✔
300
  /*
301
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
302
  if (nlevel > 1 && tsS3Enabled) {
303
    if (nlevel == 3) {
304
      keep = pVnode->config.tsdbCfg.keep1;
305
    } else if (nlevel == 2) {
306
      keep = pVnode->config.tsdbCfg.keep0;
307
    }
308
  }
309
  */
310

311
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
10,145,625✔
312
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,145,625✔
313
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,145,625✔
314
    uint64_t nColData;
315
    if (tDecodeU64v(pCoder, &nColData) < 0) {
129!
316
      code = TSDB_CODE_INVALID_MSG;
×
317
      TSDB_CHECK_CODE(code, lino, _exit);
1!
318
    }
319

320
    SColData colData = {0};
129✔
321
    code = tDecodeColData(version, pCoder, &colData);
129✔
322
    if (code) {
129!
323
      code = TSDB_CODE_INVALID_MSG;
×
324
      TSDB_CHECK_CODE(code, lino, _exit);
×
325
    }
326

327
    if (colData.flag != HAS_VALUE) {
129!
328
      code = TSDB_CODE_INVALID_MSG;
×
329
      TSDB_CHECK_CODE(code, lino, _exit);
×
330
    }
331

332
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
361✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
233!
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
1✔
335
        TSDB_CHECK_CODE(code, lino, _exit);
1!
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
561✔
340
      code = tDecodeColData(version, pCoder, &colData);
432✔
341
      if (code) {
433!
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        TSDB_CHECK_CODE(code, lino, _exit);
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,156,451!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
487!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
344,792,301✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
334,636,337✔
355
      pCoder->pos += pRow->len;
334,636,337✔
356

357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
334,636,337✔
358
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
2,650✔
359
        TSDB_CHECK_CODE(code, lino, _exit);
2,650✔
360
      }
361
    }
362
  }
363

364
  if (!tDecodeIsEnd(pCoder)) {
10,156,093!
365
    *(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
10,160,518✔
366
    pCoder->pos += sizeof(int64_t);
10,160,518✔
367
  }
368

369
  tEndDecode(pCoder);
10,156,093✔
370

371
_exit:
10,156,039✔
372
  if (code) {
10,156,039✔
373
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
488!
374
  }
375
  return code;
10,156,304✔
376
}
377
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,502,033✔
378
  int32_t code = 0;
9,502,033✔
379
  int32_t lino = 0;
9,502,033✔
380

381
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
9,502,033!
382
    return TSDB_CODE_MSG_PREPROCESSED;
×
383
  }
384

385
  SDecoder *pCoder = &(SDecoder){0};
9,502,033✔
386

387
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
9,502,033!
388
    code = TSDB_CODE_INVALID_MSG;
×
389
    TSDB_CHECK_CODE(code, lino, _exit);
×
390
  }
391

392
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
9,502,050✔
393

394
  if (tStartDecode(pCoder) < 0) {
9,502,022!
395
    code = TSDB_CODE_INVALID_MSG;
×
396
    TSDB_CHECK_CODE(code, lino, _exit);
×
397
  }
398

399
  uint64_t nSubmitTbData;
400
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
9,502,025!
401
    code = TSDB_CODE_INVALID_MSG;
×
402
    TSDB_CHECK_CODE(code, lino, _exit);
×
403
  }
404

405
  int64_t btimeMs = taosGetTimestampMs();
9,502,117✔
406
  int64_t ctimeMs = btimeMs;
9,502,117✔
407
  for (int32_t i = 0; i < nSubmitTbData; i++) {
19,657,819✔
408
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,157,089✔
409
    TSDB_CHECK_CODE(code, lino, _exit);
10,156,190✔
410
  }
411

412
  tEndDecode(pCoder);
9,500,730✔
413

414
_exit:
9,502,065✔
415
  tDecoderClear(pCoder);
9,502,065✔
416
  if (code) {
9,502,034✔
417
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
488!
418
           tstrerror(code), TMSG_INFO(pMsg->msgType));
419
  }
420
  return code;
9,502,042✔
421
}
422

423
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
63,823✔
424
  int32_t code = 0;
63,823✔
425

426
  int32_t    size;
427
  int32_t    ret;
428
  uint8_t   *pCont;
429
  SEncoder  *pCoder = &(SEncoder){0};
63,823✔
430
  SDeleteRes res = {0};
63,823✔
431

432
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
63,823✔
433
  initStorageAPI(&handle.api);
63,823✔
434

435
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
63,823✔
436
  if (code) goto _exit;
63,822!
437

438
  res.ctimeMs = taosGetTimestampMs();
63,822✔
439
  // malloc and encode
440
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
63,822!
441
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
63,822✔
442

443
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
63,823✔
444
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
63,823✔
445

446
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
63,823✔
447
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
63,823!
448
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
449
  }
450
  tEncoderClear(pCoder);
63,823✔
451

452
  rpcFreeCont(pMsg->pCont);
63,823✔
453
  pMsg->pCont = pCont;
63,823✔
454
  pMsg->contLen = size + sizeof(SMsgHead);
63,823✔
455

456
  taosArrayDestroy(res.uidList);
63,823✔
457

458
_exit:
63,823✔
459
  return code;
63,823✔
460
}
461

462
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,850✔
463
  int32_t code = 0;
1,850✔
464
  int32_t lino = 0;
1,850✔
465

466
  int64_t         ctimeMs = taosGetTimestampMs();
1,850✔
467
  SBatchDeleteReq pReq = {0};
1,850✔
468
  SDecoder       *pCoder = &(SDecoder){0};
1,850✔
469

470
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
1,850✔
471

472
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
1,850!
473
    code = TSDB_CODE_INVALID_MSG;
×
474
  }
475

476
  tDecoderClear(pCoder);
1,850✔
477
  taosArrayDestroy(pReq.deleteReqs);
1,850✔
478

479
  if (code) {
1,850!
480
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
481
  } else {
482
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
1,850✔
483
  }
484
  return code;
1,850✔
485
}
486

487
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
5✔
488
  SVArbCheckSyncReq syncReq = {0};
5✔
489

490
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
5!
491
                                    &syncReq) != 0) {
492
    return TSDB_CODE_INVALID_MSG;
×
493
  }
494

495
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
5✔
496
  if (ret != 0) {
5✔
497
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
2!
498
  }
499

500
  int32_t code = terrno;
5✔
501
  tFreeSVArbCheckSyncReq(&syncReq);
5✔
502

503
  return code;
5✔
504
}
505

506
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,551✔
507
  int32_t          code = TSDB_CODE_SUCCESS;
2,551✔
508
  int32_t          lino = 0;
2,551✔
509
  int32_t          size = 0;
2,551✔
510
  SDecoder         dc = {0};
2,551✔
511
  SEncoder         ec = {0};
2,551✔
512
  SVDropTbBatchReq receivedBatchReqs = {0};
2,551✔
513
  SVDropTbBatchReq sentBatchReqs = {0};
2,551✔
514

515
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
2,551✔
516

517
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
2,551✔
518
  if (code < 0) {
2,551!
519
    terrno = code;
×
520
    TSDB_CHECK_CODE(code, lino, _exit);
×
521
  }
522
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
2,551✔
523
  if (!sentBatchReqs.pArray) {
2,551!
524
    code = terrno;
×
525
    goto _exit;
×
526
  }
527

528
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
5,127✔
529
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
2,576✔
530
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
2,576✔
531
    if (uid == 0) {
2,576!
532
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
533
      continue;
×
534
    }
535
    pReq->uid = uid;
2,576✔
536
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
2,576✔
537
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
5,152!
538
      code = terrno;
×
539
      goto _exit;
×
540
    }
541
  }
542
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
2,551✔
543

544
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
2,551!
545
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
2,551✔
546
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
2,551✔
547
  tEncoderClear(&ec);
2,551✔
548
  if (code != TSDB_CODE_SUCCESS) {
2,551!
549
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
550
    TSDB_CHECK_CODE(code, lino, _exit);
×
551
  }
552

553
_exit:
2,551✔
554
  tDecoderClear(&dc);
2,551✔
555
  if (sentBatchReqs.pArray) {
2,551!
556
    taosArrayDestroy(sentBatchReqs.pArray);
2,551✔
557
  }
558
  return code;
2,551✔
559
}
560

561
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,734,099✔
562
  int32_t code = 0;
9,734,099✔
563

564
  switch (pMsg->msgType) {
9,734,099✔
565
    case TDMT_VND_CREATE_TABLE: {
60,512✔
566
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
60,512✔
567
    } break;
60,514✔
568
    case TDMT_VND_ALTER_TABLE: {
985✔
569
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
985✔
570
    } break;
985✔
571
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
16,788✔
572
    case TDMT_VND_DROP_TTL_TABLE: {
573
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
16,788✔
574
    } break;
16,770✔
575
    case TDMT_VND_SUBMIT: {
9,502,108✔
576
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
9,502,108✔
577
    } break;
9,502,023✔
578
    case TDMT_VND_DELETE: {
63,823✔
579
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
63,823✔
580
    } break;
63,823✔
581
    case TDMT_VND_BATCH_DEL: {
1,850✔
582
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
1,850✔
583
    } break;
1,850✔
584
    case TDMT_VND_ARB_CHECK_SYNC: {
5✔
585
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
5✔
586
    } break;
5✔
587
    case TDMT_VND_DROP_TABLE: {
2,551✔
588
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
2,551✔
589
    } break;
2,551✔
590
    default:
85,477✔
591
      break;
85,477✔
592
  }
593

594
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
9,733,998✔
595
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
490!
596
           TMSG_INFO(pMsg->msgType));
597
  }
598
  return code;
9,734,003✔
599
}
600

601
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
11,171,840✔
602
  int32_t code = 0;
11,171,840✔
603
  void   *ptr = NULL;
11,171,840✔
604
  void   *pReq;
605
  int32_t len;
606

607
  (void)taosThreadMutexLock(&pVnode->mutex);
11,171,840✔
608
  if (pVnode->disableWrite) {
11,172,071!
609
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
610
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
611
    return TSDB_CODE_VND_WRITE_DISABLED;
×
612
  }
613
  (void)taosThreadMutexUnlock(&pVnode->mutex);
11,172,071✔
614

615
  if (ver <= pVnode->state.applied) {
11,172,083!
616
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
×
617
           pVnode->state.applied);
618
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
619
  }
620

621
  vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
11,172,083!
622
         ", conn.applyTerm:%" PRId64 ", contLen:%d",
623
         TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
624
         pMsg->info.conn.applyTerm, pMsg->contLen);
625

626
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
11,171,812!
627
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
628
  }
629

630
  if (!(pVnode->state.applied + 1 == ver)) {
11,171,812!
631
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
632
  }
633

634
  atomic_store_64(&pVnode->state.applied, ver);
11,171,812✔
635
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
11,172,110✔
636

637
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
11,172,041✔
638

639
  // skip header
640
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
11,147,780✔
641
  len = pMsg->contLen - sizeof(SMsgHead);
11,147,780✔
642
  bool needCommit = false;
11,147,780✔
643

644
  switch (pMsg->msgType) {
11,147,780!
645
    /* META */
646
    case TDMT_VND_CREATE_STB:
22,237✔
647
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
22,237✔
648
      break;
22,247✔
649
    case TDMT_VND_ALTER_STB:
6,403✔
650
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
6,403!
651
      break;
6,406✔
652
    case TDMT_VND_DROP_STB:
2,261✔
653
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2,261!
654
      break;
2,265✔
655
    case TDMT_VND_CREATE_TABLE:
68,805✔
656
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
68,805!
657
      break;
68,839✔
658
    case TDMT_VND_ALTER_TABLE:
968✔
659
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
968!
660
      break;
968✔
661
    case TDMT_VND_DROP_TABLE:
2,593✔
662
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,593!
663
      break;
2,593✔
664
    case TDMT_VND_DROP_TTL_TABLE:
×
665
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
666
      break;
×
667
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
5✔
668
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5!
669
      break;
5✔
670
    case TDMT_VND_TRIM:
15✔
671
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
15!
672
      break;
15✔
673
    case TDMT_VND_S3MIGRATE:
×
674
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
675
      break;
×
676
    case TDMT_VND_CREATE_SMA:
25✔
677
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
25!
678
      break;
25✔
679
    /* TSDB */
680
    case TDMT_VND_SUBMIT:
10,900,668✔
681
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
10,900,668✔
682
      break;
10,900,606✔
683
    case TDMT_VND_DELETE:
65,022✔
684
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
65,022!
685
      break;
65,021✔
686
    case TDMT_VND_BATCH_DEL:
1,849✔
687
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,849!
688
      break;
1,850✔
689
    /* TQ */
690
    case TDMT_VND_TMQ_SUBSCRIBE:
3,136✔
691
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,136✔
692
        goto _err;
1✔
693
      }
694
      break;
3,140✔
695
    case TDMT_VND_TMQ_DELETE_SUB:
905✔
696
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
905!
697
        goto _err;
×
698
      }
699
      break;
907✔
700
    case TDMT_VND_TMQ_COMMIT_OFFSET:
15,620✔
701
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
15,620!
702
        goto _err;
×
703
      }
704
      break;
15,630✔
705
    case TDMT_VND_TMQ_ADD_CHECKINFO:
115✔
706
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
115!
707
        goto _err;
×
708
      }
709
      break;
116✔
710
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
711
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
10!
712
        goto _err;
×
713
      }
714
      break;
10✔
715
    case TDMT_STREAM_TASK_DEPLOY: {
11,065✔
716
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
11,065!
717
        goto _err;
×
718
      }
719
    } break;
11,074✔
720
    case TDMT_STREAM_TASK_DROP: {
5,076✔
721
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
5,076!
722
        goto _err;
×
723
      }
724
    } break;
5,087✔
725
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
2,759✔
726
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
2,759!
727
        goto _err;
×
728
      }
729
    } break;
2,763✔
730
    case TDMT_STREAM_CONSEN_CHKPT: {
171✔
731
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
171!
732
        goto _err;
×
733
      }
734

735
    } break;
171✔
736
    case TDMT_STREAM_TASK_PAUSE: {
895✔
737
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
1,700!
738
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
805✔
739
        goto _err;
×
740
      }
741
    } break;
895✔
742
    case TDMT_STREAM_TASK_RESUME: {
1,560✔
743
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
3,030!
744
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
1,470✔
745
        goto _err;
×
746
      }
747
    } break;
1,560✔
748
    case TDMT_VND_STREAM_TASK_RESET: {
×
749
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
750
        goto _err;
×
751
      }
752

753
    } break;
×
754
    case TDMT_VND_STREAM_ALL_STOP: {
3,035✔
755
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
3,035!
756
        goto _err;
×
757
      }
758

759
    } break;
3,036✔
760
    case TDMT_VND_ALTER_CONFIRM:
7,987✔
761
      needCommit = pVnode->config.hashChange;
7,987✔
762
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
7,987!
763
        goto _err;
×
764
      }
765
      break;
7,987✔
766
    case TDMT_VND_ALTER_CONFIG:
396✔
767
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
396✔
768
      break;
396✔
769
    case TDMT_VND_COMMIT:
22,086✔
770
      needCommit = true;
22,086✔
771
      break;
22,086✔
772
    case TDMT_VND_CREATE_INDEX:
27✔
773
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
27✔
774
      break;
27✔
775
    case TDMT_VND_DROP_INDEX:
10✔
776
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
10✔
777
      break;
10✔
778
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
1,816✔
779
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
1,816✔
780
      break;
1,821✔
781
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
50✔
782
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
50✔
783
      break;
50✔
784
    case TDMT_VND_COMPACT:
203✔
785
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
203✔
786
      goto _exit;
203✔
787
    case TDMT_SYNC_CONFIG_CHANGE:
×
788
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
789
      break;
×
790
#ifdef TD_ENTERPRISE
791
    case TDMT_VND_KILL_COMPACT:
×
792
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
793
      break;
×
794
#endif
795
    /* ARB */
796
    case TDMT_VND_ARB_CHECK_SYNC:
7✔
797
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
7✔
798
      break;
7✔
799
    default:
×
800
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
801
      return TSDB_CODE_INVALID_MSG;
×
802
  }
803

804
  vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
11,147,613!
805
         ver);
806

807
  walApplyVer(pVnode->pWal, ver);
11,147,613✔
808

809
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
11,147,552✔
810
  if (code) {
11,147,488!
811
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
812
    return code;
×
813
  }
814

815
  // commit if need
816
  if (needCommit) {
11,147,488✔
817
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
22,118✔
818
    code = vnodeAsyncCommit(pVnode);
22,161✔
819
    if (code) {
22,160!
820
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
821
      goto _err;
×
822
    }
823

824
    // start a new one
825
    code = vnodeBegin(pVnode);
22,160✔
826
    if (code) {
22,156✔
827
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
26!
828
      goto _err;
×
829
    }
830
  }
831

832
_exit:
11,147,500✔
833
  return 0;
11,171,572✔
834

835
_err:
13✔
836
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
13!
837
         tstrerror(terrno), ver);
838
  return code;
13✔
839
}
840

841
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
4,950,874✔
842
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
4,950,874✔
843
    return 0;
3,561,185✔
844
  }
845

846
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
1,389,689✔
847
}
848

849
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
4,953,694✔
850
  vTrace("message in vnode query queue is processing");
4,953,694✔
851
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
4,953,694✔
852
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
3,110✔
853
    return 0;
3,110✔
854
  }
855

856
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
4,950,233!
857
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
858
    return 0;
×
859
  }
860

861
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
4,950,233✔
862
  initStorageAPI(&handle.api);
4,950,233✔
863
  int32_t code = TSDB_CODE_SUCCESS;
4,950,003✔
864
  bool    redirected = false;
4,950,003✔
865

866
  switch (pMsg->msgType) {
4,950,003✔
867
    case TDMT_SCH_QUERY:
1,254,560✔
868
      if (!syncIsReadyForRead(pVnode->sync)) {
1,254,560✔
869
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,663!
870
        redirected = true;
2,663✔
871
      }
872
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,254,652✔
873
      if (redirected) {
1,254,280✔
874
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,502✔
875
        return 0;
2,663✔
876
      }
877
      return code;
1,251,778✔
878
    case TDMT_SCH_MERGE_QUERY:
137,586✔
879
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
137,586✔
880
    case TDMT_SCH_QUERY_CONTINUE:
12,942✔
881
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
12,942✔
882
    case TDMT_VND_TMQ_CONSUME:
3,519,573✔
883
      return tqProcessPollReq(pVnode->pTq, pMsg);
3,519,573✔
884
    case TDMT_VND_TMQ_CONSUME_PUSH:
25,179✔
885
      return tqProcessPollPush(pVnode->pTq, pMsg);
25,179✔
886
    default:
163✔
887
      vError("unknown msg type:%d in query queue", pMsg->msgType);
163!
888
      return TSDB_CODE_APP_ERROR;
×
889
  }
890
}
891

892
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
3,695,478✔
893
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
3,695,478✔
894
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
3,695,482!
895
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
2,443,318✔
896
      !syncIsReadyForRead(pVnode->sync)) {
1,348,624✔
897
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2,746✔
898
    return 0;
2,746✔
899
  }
900

901
  switch (pMsg->msgType) {
3,692,848!
902
    case TDMT_SCH_FETCH:
1,401,183✔
903
    case TDMT_SCH_MERGE_FETCH:
904
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,401,183✔
905
    case TDMT_SCH_FETCH_RSP:
×
906
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
907
    // case TDMT_SCH_CANCEL_TASK:
908
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
909
    case TDMT_SCH_DROP_TASK:
1,390,910✔
910
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,390,910✔
911
    case TDMT_SCH_TASK_NOTIFY:
26✔
912
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
26✔
913
    case TDMT_SCH_QUERY_HEARTBEAT:
806,108✔
914
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
806,108✔
915
    case TDMT_VND_TABLE_META:
1,780✔
916
    case TDMT_VND_TABLE_NAME:
917
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,780✔
918
    case TDMT_VND_TABLE_CFG:
×
919
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
920
    case TDMT_VND_BATCH_META:
92,756✔
921
      return vnodeGetBatchMeta(pVnode, pMsg);
92,756✔
922
#ifdef TD_ENTERPRISE
923
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
46✔
924
      return vnodeQueryCompactProgress(pVnode, pMsg);
46✔
925
#endif
926
      //    case TDMT_VND_TMQ_CONSUME:
927
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
928
    case TDMT_VND_TMQ_VG_WALINFO:
13✔
929
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
13✔
930
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
931
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
932
    case TDMT_VND_TMQ_SEEK:
24✔
933
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
934

935
    default:
×
936
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
937
      return TSDB_CODE_APP_ERROR;
×
938
  }
939
}
940

941
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
144,897✔
942
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
144,897✔
943
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
144,897!
944
       pMsg->msgType == TDMT_VND_BATCH_META) &&
144,937!
945
      !syncIsReadyForRead(pVnode->sync)) {
×
946
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
947
    return 0;
×
948
  }
949

950
  switch (pMsg->msgType) {
144,937!
951
    case TDMT_STREAM_TASK_RUN:
144,065✔
952
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
144,065✔
953
    case TDMT_STREAM_RETRIEVE:
454✔
954
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
454✔
955
    case TDMT_STREAM_RETRIEVE_RSP:
418✔
956
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
418✔
957
    case TDMT_VND_GET_STREAM_PROGRESS:
×
958
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
959
    default:
×
960
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
961
      return TSDB_CODE_APP_ERROR;
×
962
  }
963
}
964

965
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
159,853✔
966
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
159,853✔
967
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
159,853!
968
       pMsg->msgType == TDMT_VND_BATCH_META) &&
159,853!
969
      !syncIsReadyForRead(pVnode->sync)) {
×
970
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
971
    return 0;
×
972
  }
973

974
  switch (pMsg->msgType) {
159,853!
975
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
6,296✔
976
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
6,296✔
977
    case TDMT_STREAM_TASK_DISPATCH:
50,090✔
978
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
50,090✔
979
    case TDMT_STREAM_TASK_DISPATCH_RSP:
50,025✔
980
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
50,025✔
981
    case TDMT_VND_STREAM_TASK_CHECK:
17,656✔
982
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
17,656✔
983
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
17,978✔
984
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
17,978✔
985
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
5,418✔
986
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
5,418✔
987
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
5,415✔
988
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
5,415✔
989
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
990
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
991
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
992
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
993
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
3,485✔
994
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
3,485✔
995
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
3,490✔
996
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
3,490✔
997
    default:
×
998
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
999
      return TSDB_CODE_APP_ERROR;
×
1000
  }
1001
}
1002

1003
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
2,004✔
1004
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
2,004✔
1005
  if (!syncIsReadyForRead(pVnode->sync)) {
2,004!
1006
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1007
    return 0;
×
1008
  }
1009

1010
  switch (pMsg->msgType) {
2,004!
1011
    case TDMT_VND_STREAM_SCAN_HISTORY:
2,004✔
1012
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2,004✔
1013
    default:
×
1014
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1015
      return TSDB_CODE_APP_ERROR;
×
1016
  }
1017
}
1018

1019
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
2✔
1020
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
2✔
1021
  if (code) {
2!
1022
    vError("failed to process sma result since %s", tstrerror(code));
×
1023
  }
1024
}
2✔
1025

1026
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
144,555✔
1027
  if (NULL == pMetaRsp) {
144,555!
1028
    return;
×
1029
  }
1030

1031
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
144,555✔
1032
  pMetaRsp->dbId = pVnode->config.dbId;
144,555✔
1033
  pMetaRsp->vgId = TD_VID(pVnode);
144,555✔
1034
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
144,555✔
1035
}
1036

1037
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1038

1039
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15✔
1040
  if (!pVnode->restored) {
15✔
1041
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
7!
1042
    return 0;
7✔
1043
  }
1044

1045
  int32_t     code = 0;
8✔
1046
  SVTrimDbReq trimReq = {0};
8✔
1047

1048
  // decode
1049
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
8!
1050
    code = TSDB_CODE_INVALID_MSG;
×
1051
    goto _exit;
×
1052
  }
1053

1054
  vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
8!
1055

1056
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
8✔
1057

1058
_exit:
8✔
1059
  return code;
8✔
1060
}
1061

1062
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1063

1064
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1065
  int32_t          code = 0;
×
1066
  SVS3MigrateDbReq s3migrateReq = {0};
×
1067

1068
  // decode
1069
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1070
    code = TSDB_CODE_INVALID_MSG;
×
1071
    goto _exit;
×
1072
  }
1073

1074
  vInfo("vgId:%d, s3migrate vnode request will be processed, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1075

1076
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1077

1078
_exit:
×
1079
  return code;
×
1080
}
1081

1082
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1083
  int               ret = 0;
×
1084
  SVDropTtlTableReq ttlReq = {0};
×
1085
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1086
    ret = TSDB_CODE_INVALID_MSG;
×
1087
    goto end;
×
1088
  }
1089

1090
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1091
    ret = TSDB_CODE_INVALID_MSG;
×
1092
    goto end;
×
1093
  }
1094

1095
  if (ttlReq.nUids != 0) {
×
1096
    vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1097
          ttlReq.timestampSec, ttlReq.nUids);
1098
  }
1099

1100
  if (ttlReq.nUids > 0) {
×
1101
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1102
    if (code) return code;
×
1103

1104
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1105
    if (code) {
×
1106
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1107
    }
1108
  }
1109

1110
end:
×
1111
  taosArrayDestroy(ttlReq.pTbUids);
×
1112
  return ret;
×
1113
}
1114

1115
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5✔
1116
  int32_t                 code = -1;
5✔
1117
  SMetaReader             mr = {0};
5✔
1118
  SVDropTtlTableReq       ttlReq = {0};
5✔
1119
  SVFetchTtlExpiredTbsRsp rsp = {0};
5✔
1120
  SEncoder                encoder = {0};
5✔
1121
  SArray                 *pNames = NULL;
5✔
1122
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
5✔
1123
  pRsp->code = TSDB_CODE_SUCCESS;
5✔
1124
  pRsp->pCont = NULL;
5✔
1125
  pRsp->contLen = 0;
5✔
1126

1127
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
5!
1128
    terrno = TSDB_CODE_INVALID_MSG;
×
1129
    goto _end;
×
1130
  }
1131

1132
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
5!
1133
    terrno = TSDB_CODE_INVALID_MSG;
×
1134
    goto _end;
×
1135
  }
1136

1137
  tb_uid_t    suid;
1138
  char        ctbName[TSDB_TABLE_NAME_LEN];
1139
  SVDropTbReq expiredTb = {.igNotExists = true};
5✔
1140
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
5✔
1141
  rsp.vgId = TD_VID(pVnode);
5✔
1142
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
5✔
1143
  if (!rsp.pExpiredTbs) goto _end;
5!
1144

1145
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
5✔
1146
  if (!pNames) {
5!
1147
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1148
    goto _end;
×
1149
  }
1150
  char buf[TSDB_TABLE_NAME_LEN];
1151
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
10✔
1152
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
5✔
1153
    expiredTb.suid = *uid;
5✔
1154
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
5✔
1155
    if (terrno < 0) goto _end;
5!
1156
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
5✔
1157
    void *p = taosArrayPush(pNames, buf);
5✔
1158
    if (p == NULL) {
5!
1159
      goto _end;
×
1160
    }
1161

1162
    expiredTb.name = p;
5✔
1163
    if (mr.me.type == TSDB_CHILD_TABLE) {
5!
1164
      expiredTb.suid = mr.me.ctbEntry.suid;
5✔
1165
    }
1166

1167
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
10!
1168
      goto _end;
×
1169
    }
1170
  }
1171

1172
  int32_t ret = 0;
5✔
1173
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
5!
1174
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5✔
1175
  if (pRsp->pCont == NULL) {
5!
1176
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1177
    code = -1;
×
1178
    goto _end;
×
1179
  }
1180
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
5✔
1181
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
5✔
1182
  tEncoderClear(&encoder);
5✔
1183

1184
  if (terrno == 0) code = 0;
5!
1185
_end:
×
1186
  metaReaderClear(&mr);
5✔
1187
  tFreeFetchTtlExpiredTbsRsp(&rsp);
5✔
1188
  taosArrayDestroy(ttlReq.pTbUids);
5✔
1189
  if (pNames) taosArrayDestroy(pNames);
5!
1190
  pRsp->code = terrno;
5✔
1191
  return code;
5✔
1192
}
1193

1194
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
22,130✔
1195
  int32_t        code = 0;
22,130✔
1196
  SVCreateStbReq req = {0};
22,130✔
1197
  SDecoder       coder;
1198

1199
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
22,130✔
1200
  pRsp->code = TSDB_CODE_SUCCESS;
22,130✔
1201
  pRsp->pCont = NULL;
22,130✔
1202
  pRsp->contLen = 0;
22,130✔
1203

1204
  // decode and process req
1205
  tDecoderInit(&coder, pReq, len);
22,130✔
1206

1207
  code = tDecodeSVCreateStbReq(&coder, &req);
22,248✔
1208
  if (code) {
22,127!
1209
    pRsp->code = code;
×
1210
    goto _err;
×
1211
  }
1212

1213
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
22,127✔
1214
  if (code) {
22,278✔
1215
    pRsp->code = code;
4✔
1216
    goto _err;
4✔
1217
  }
1218

1219
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
22,274!
1220
    pRsp->code = code;
×
1221
    goto _err;
×
1222
  }
1223

1224
  tDecoderClear(&coder);
22,260✔
1225
  return 0;
22,202✔
1226

1227
_err:
4✔
1228
  tDecoderClear(&coder);
4✔
1229
  return code;
4✔
1230
}
1231

1232
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
68,799✔
1233
                                       SRpcMsg *pOriginRpc) {
1234
  SDecoder           decoder = {0};
68,799✔
1235
  SEncoder           encoder = {0};
68,799✔
1236
  int32_t            rcode = 0;
68,799✔
1237
  SVCreateTbBatchReq req = {0};
68,799✔
1238
  SVCreateTbReq     *pCreateReq;
1239
  SVCreateTbBatchRsp rsp = {0};
68,799✔
1240
  SVCreateTbRsp      cRsp = {0};
68,799✔
1241
  char               tbName[TSDB_TABLE_FNAME_LEN];
1242
  STbUidStore       *pStore = NULL;
68,799✔
1243
  SArray            *tbUids = NULL;
68,799✔
1244
  SArray            *tbNames = NULL;
68,799✔
1245
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
68,799✔
1246
  pRsp->code = TSDB_CODE_SUCCESS;
68,799✔
1247
  pRsp->pCont = NULL;
68,799✔
1248
  pRsp->contLen = 0;
68,799✔
1249

1250
  // decode
1251
  tDecoderInit(&decoder, pReq, len);
68,799✔
1252
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
68,804!
1253
    rcode = -1;
×
1254
    terrno = TSDB_CODE_INVALID_MSG;
×
1255
    goto _exit;
×
1256
  }
1257

1258
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
68,835✔
1259
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
68,834✔
1260
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
68,840✔
1261
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
68,841!
1262
    rcode = -1;
×
1263
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1264
    goto _exit;
×
1265
  }
1266

1267
  // loop to create table
1268
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
177,269✔
1269
    pCreateReq = req.pReqs + iReq;
108,418✔
1270
    memset(&cRsp, 0, sizeof(cRsp));
108,418✔
1271

1272
    if (tsEnableAudit && tsEnableAuditCreateTable) {
108,418!
1273
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
108,421!
1274
      if (str == NULL) {
108,424!
1275
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1276
        rcode = -1;
×
1277
        goto _exit;
×
1278
      }
1279
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
108,424✔
1280
      if (taosArrayPush(tbNames, &str) == NULL) {
108,421!
1281
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1282
        rcode = -1;
×
1283
        goto _exit;
×
1284
      }
1285
    }
1286

1287
    // validate hash
1288
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
108,418✔
1289
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
108,422!
1290
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1291
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1292
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1293
        rcode = -1;
×
1294
        goto _exit;
×
1295
      }
1296
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1297
      continue;
×
1298
    }
1299

1300
    // do create table
1301
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
108,422✔
1302
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
725!
1303
        cRsp.code = TSDB_CODE_SUCCESS;
3✔
1304
      } else {
1305
        cRsp.code = terrno;
722✔
1306
      }
1307
    } else {
1308
      cRsp.code = TSDB_CODE_SUCCESS;
107,705✔
1309
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
107,705!
1310
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1311
      }
1312
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
215,406!
1313
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1314
        rcode = -1;
×
1315
        goto _exit;
×
1316
      }
1317
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
107,702✔
1318
    }
1319

1320
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
216,856!
1321
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1322
      rcode = -1;
×
1323
      goto _exit;
×
1324
    }
1325
  }
1326

1327
  vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
68,851✔
1328
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
68,851!
1329
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1330
  }
1331
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
68,844!
1332
    goto _exit;
×
1333
  }
1334
  pStore = tdUidStoreFree(pStore);
68,844✔
1335

1336
  // prepare rsp
1337
  int32_t ret = 0;
68,843✔
1338
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
68,843!
1339
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
68,839✔
1340
  if (pRsp->pCont == NULL) {
68,840!
1341
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1342
    rcode = -1;
×
1343
    goto _exit;
×
1344
  }
1345
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
68,840✔
1346
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
68,840!
1347
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1348
  }
1349

1350
  if (tsEnableAudit && tsEnableAuditCreateTable) {
68,841!
1351
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
68,839✔
1352

1353
    SName name = {0};
68,839✔
1354
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
68,839!
1355
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1356
    }
1357

1358
    SStringBuilder sb = {0};
68,838✔
1359
    for (int32_t i = 0; i < tbNames->size; i++) {
177,256✔
1360
      char **key = (char **)taosArrayGet(tbNames, i);
108,421✔
1361
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
108,420✔
1362
      if (i < tbNames->size - 1) {
108,419✔
1363
        taosStringBuilderAppendChar(&sb, ',');
39,585✔
1364
      }
1365
      // taosMemoryFreeClear(*key);
1366
    }
1367

1368
    size_t len = 0;
68,835✔
1369
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
68,835✔
1370

1371
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
68,837!
1372
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
56,494✔
1373
    }
1374

1375
    taosStringBuilderDestroy(&sb);
68,837✔
1376
  }
1377

1378
_exit:
2✔
1379
  tDeleteSVCreateTbBatchReq(&req);
68,839✔
1380
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
68,839✔
1381
  taosArrayDestroy(tbUids);
68,842✔
1382
  tDecoderClear(&decoder);
68,841✔
1383
  tEncoderClear(&encoder);
68,843✔
1384
  taosArrayDestroyP(tbNames, NULL);
68,843✔
1385
  return rcode;
68,843✔
1386
}
1387

1388
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
6,403✔
1389
  int32_t        code = 0;
6,403✔
1390
  SVCreateStbReq req = {0};
6,403✔
1391
  SDecoder       dc = {0};
6,403✔
1392

1393
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
6,403✔
1394
  pRsp->code = TSDB_CODE_SUCCESS;
6,403✔
1395
  pRsp->pCont = NULL;
6,403✔
1396
  pRsp->contLen = 0;
6,403✔
1397

1398
  tDecoderInit(&dc, pReq, len);
6,403✔
1399

1400
  // decode req
1401
  code = tDecodeSVCreateStbReq(&dc, &req);
6,403✔
1402
  if (code) {
6,404!
1403
    tDecoderClear(&dc);
×
1404
    return code;
×
1405
  }
1406

1407
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
6,404✔
1408
  if (code) {
6,406!
1409
    pRsp->code = code;
×
1410
    tDecoderClear(&dc);
×
1411
    return code;
×
1412
  }
1413

1414
  tDecoderClear(&dc);
6,406✔
1415

1416
  return 0;
6,406✔
1417
}
1418

1419
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,261✔
1420
  SVDropStbReq req = {0};
2,261✔
1421
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,261✔
1422
  SDecoder     decoder = {0};
2,261✔
1423
  SArray      *tbUidList = NULL;
2,261✔
1424

1425
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,261✔
1426
  pRsp->pCont = NULL;
2,261✔
1427
  pRsp->contLen = 0;
2,261✔
1428

1429
  // decode request
1430
  tDecoderInit(&decoder, pReq, len);
2,261✔
1431
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,260!
1432
    rcode = TSDB_CODE_INVALID_MSG;
×
1433
    goto _exit;
×
1434
  }
1435

1436
  // process request
1437
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,264✔
1438
  if (tbUidList == NULL) goto _exit;
2,263!
1439
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,263✔
1440
    rcode = terrno;
5✔
1441
    goto _exit;
5✔
1442
  }
1443

1444
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,260!
1445
    rcode = terrno;
×
1446
    goto _exit;
×
1447
  }
1448

1449
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,260!
1450
    rcode = terrno;
×
1451
    goto _exit;
×
1452
  }
1453

1454
  // return rsp
1455
_exit:
2,260✔
1456
  if (tbUidList) taosArrayDestroy(tbUidList);
2,265!
1457
  pRsp->code = rcode;
2,265✔
1458
  tDecoderClear(&decoder);
2,265✔
1459
  return 0;
2,265✔
1460
}
1461

1462
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
968✔
1463
  SVAlterTbReq  vAlterTbReq = {0};
968✔
1464
  SVAlterTbRsp  vAlterTbRsp = {0};
968✔
1465
  SDecoder      dc = {0};
968✔
1466
  int32_t       code = 0;
968✔
1467
  int32_t       lino = 0;
968✔
1468
  int32_t       ret;
1469
  SEncoder      ec = {0};
968✔
1470
  STableMetaRsp vMetaRsp = {0};
968✔
1471

1472
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
968✔
1473
  pRsp->pCont = NULL;
968✔
1474
  pRsp->contLen = 0;
968✔
1475
  pRsp->code = TSDB_CODE_SUCCESS;
968✔
1476

1477
  tDecoderInit(&dc, pReq, len);
968✔
1478

1479
  // decode
1480
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
968!
1481
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1482
    tDecoderClear(&dc);
×
1483
    goto _exit;
×
1484
  }
1485

1486
  // process
1487
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
968✔
1488
    vAlterTbRsp.code = terrno;
38✔
1489
    tDecoderClear(&dc);
38✔
1490
    goto _exit;
38✔
1491
  }
1492
  tDecoderClear(&dc);
930✔
1493

1494
  if (NULL != vMetaRsp.pSchemas) {
930✔
1495
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
190✔
1496
    vAlterTbRsp.pMeta = &vMetaRsp;
190✔
1497
  }
1498

1499
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
930✔
1500
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
657✔
1501
    if (uid == 0) {
657!
1502
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1503
             vAlterTbReq.tbName);
1504
      goto _exit;
×
1505
    }
1506

1507
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
657✔
1508
    void* p = taosArrayPush(tbUids, &uid);
657✔
1509
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
657!
1510

1511
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
657✔
1512
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
657!
1513
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1514
    }
1515

1516
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
657✔
1517
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
657!
1518
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1519
    }
1520

1521
    taosArrayDestroy(tbUids);
657✔
1522
  }
1523

1524
_exit:
273✔
1525
  taosArrayDestroy(vAlterTbReq.pMultiTag);
968✔
1526
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
968!
1527
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
968✔
1528
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
968✔
1529
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
968!
1530
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1531
  }
1532

1533
  tEncoderClear(&ec);
968✔
1534
  if (vMetaRsp.pSchemas) {
968✔
1535
    taosMemoryFree(vMetaRsp.pSchemas);
190!
1536
    taosMemoryFree(vMetaRsp.pSchemaExt);
190!
1537
  }
1538
  return 0;
968✔
1539
}
1540

1541
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,593✔
1542
                                     SRpcMsg *pOriginRpc) {
1543
  SVDropTbBatchReq req = {0};
2,593✔
1544
  SVDropTbBatchRsp rsp = {0};
2,593✔
1545
  SDecoder         decoder = {0};
2,593✔
1546
  SEncoder         encoder = {0};
2,593✔
1547
  int32_t          ret;
1548
  SArray          *tbUids = NULL;
2,593✔
1549
  STbUidStore     *pStore = NULL;
2,593✔
1550
  SArray          *tbNames = NULL;
2,593✔
1551

1552
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
2,593✔
1553
  pRsp->pCont = NULL;
2,593✔
1554
  pRsp->contLen = 0;
2,593✔
1555
  pRsp->code = TSDB_CODE_SUCCESS;
2,593✔
1556

1557
  // decode req
1558
  tDecoderInit(&decoder, pReq, len);
2,593✔
1559
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2,593✔
1560
  if (ret < 0) {
2,593!
1561
    terrno = TSDB_CODE_INVALID_MSG;
×
1562
    pRsp->code = terrno;
×
1563
    goto _exit;
×
1564
  }
1565

1566
  // process req
1567
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,593✔
1568
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
2,593✔
1569
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,593✔
1570
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
2,593!
1571

1572
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,211✔
1573
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
2,618✔
1574
    SVDropTbRsp  dropTbRsp = {0};
2,618✔
1575
    tb_uid_t     tbUid = 0;
2,618✔
1576

1577
    /* code */
1578
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
2,618✔
1579
    if (ret < 0) {
2,618!
1580
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1581
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1582
      } else {
1583
        dropTbRsp.code = terrno;
×
1584
      }
1585
    } else {
1586
      dropTbRsp.code = TSDB_CODE_SUCCESS;
2,618✔
1587
      if (tbUid > 0) {
2,618!
1588
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1589
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1590
        }
1591
      }
1592
    }
1593

1594
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
5,236!
1595
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1596
      pRsp->code = terrno;
×
1597
      goto _exit;
×
1598
    }
1599

1600
    if (tsEnableAuditCreateTable) {
2,618!
1601
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2,618!
1602
      if (str == NULL) {
2,618!
1603
        pRsp->code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
2,618✔
1607
      if (taosArrayPush(tbNames, &str) == NULL) {
2,618!
1608
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1609
        pRsp->code = terrno;
×
1610
        goto _exit;
×
1611
      }
1612
    }
1613
  }
1614

1615
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
2,593!
1616
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1617
  }
1618

1619
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
2,593!
1620
    goto _exit;
×
1621
  }
1622

1623
  if (tsEnableAuditCreateTable) {
2,593!
1624
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2,593✔
1625

1626
    SName name = {0};
2,593✔
1627
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
2,593!
1628
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1629
    }
1630

1631
    SStringBuilder sb = {0};
2,593✔
1632
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
5,211✔
1633
      char **key = (char **)taosArrayGet(tbNames, iReq);
2,618✔
1634
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
2,618✔
1635
      if (iReq < req.nReqs - 1) {
2,618✔
1636
        taosStringBuilderAppendChar(&sb, ',');
25✔
1637
      }
1638
      taosMemoryFreeClear(*key);
2,618!
1639
    }
1640

1641
    size_t len = 0;
2,593✔
1642
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
2,593✔
1643

1644
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2,593!
1645
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
2,496✔
1646
    }
1647

1648
    taosStringBuilderDestroy(&sb);
2,593✔
1649
  }
1650

1651
_exit:
×
1652
  taosArrayDestroy(tbUids);
2,593✔
1653
  pStore = tdUidStoreFree(pStore);
2,593✔
1654
  tDecoderClear(&decoder);
2,593✔
1655
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
2,593!
1656
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,593✔
1657
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,593✔
1658
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
2,593!
1659
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1660
  }
1661
  tEncoderClear(&encoder);
2,593✔
1662
  taosArrayDestroy(rsp.pArray);
2,593✔
1663
  taosArrayDestroy(tbNames);
2,593✔
1664
  return 0;
2,593✔
1665
}
1666

1667
#ifdef BUILD_NO_CALL
1668
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1669
                                              const char *tags) {
1670
  SSubmitBlkIter blkIter = {0};
1671
  STSchema      *pSchema = NULL;
1672
  tb_uid_t       suid = 0;
1673
  STSRow        *row = NULL;
1674
  int32_t        rv = -1;
1675

1676
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1677
  if (blkIter.row == NULL) return 0;
1678

1679
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1680
                                         &pSchema);  // TODO: use the real schema
1681
  if (TSDB_CODE_SUCCESS != code) {
1682
    printf("%s:%d no valid schema\n", tags, __LINE__);
1683
    return code;
1684
  }
1685

1686
  suid = msgIter->suid;
1687
  rv = TD_ROW_SVER(blkIter.row);
1688

1689
  char __tags[128] = {0};
1690
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1691
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1692
    tdSRowPrint(row, pSchema, __tags);
1693
  }
1694

1695
  taosMemoryFreeClear(pSchema);
1696

1697
  return TSDB_CODE_SUCCESS;
1698
}
1699
#endif
1700
typedef struct SSubmitReqConvertCxt {
1701
  SSubmitMsgIter msgIter;
1702
  SSubmitBlk    *pBlock;
1703
  SSubmitBlkIter blkIter;
1704
  STSRow        *pRow;
1705
  STSRowIter     rowIter;
1706
  SSubmitTbData *pTbData;
1707
  STSchema      *pTbSchema;
1708
  SArray        *pColValues;
1709
} SSubmitReqConvertCxt;
1710

1711
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1712
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1713
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1714
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1715
  if (TSDB_CODE_SUCCESS != code) {
×
1716
    return code;
×
1717
  }
1718
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1719

1720
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1721
  if (NULL == pCxt->pTbData) {
×
1722
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1723
    if (NULL == pCxt->pTbData) {
×
1724
      return terrno;
×
1725
    }
1726
  }
1727
  pCxt->pTbData->flags = 0;
×
1728
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1729
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1730
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1731
  pCxt->pTbData->pCreateTbReq = NULL;
×
1732
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1733
  if (NULL == pCxt->pTbData->aRowP) {
×
1734
    return terrno;
×
1735
  }
1736

1737
  taosArrayDestroy(pCxt->pColValues);
×
1738
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1739
  if (NULL == pCxt->pColValues) {
×
1740
    return terrno;
×
1741
  }
1742
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1743
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1744
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1745
      return terrno;
×
1746
    }
1747
  }
1748

1749
  return TSDB_CODE_SUCCESS;
×
1750
}
1751

1752
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1753
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1754
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1755
  taosMemoryFreeClear(pCxt->pTbData);
×
1756
  taosArrayDestroy(pCxt->pColValues);
×
1757
}
×
1758

1759
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1760
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1761
    pColVal->flag = CV_FLAG_NONE;
×
1762
    return TSDB_CODE_SUCCESS;
×
1763
  }
1764

1765
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1766
    pColVal->flag = CV_FLAG_NULL;
×
1767
    return TSDB_CODE_SUCCESS;
×
1768
  }
1769

1770
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1771
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1772
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1773
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1774
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1775
    memcpy(&pColVal->value.val, &f, sizeof(f));
×
1776
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1777
    pColVal->value.val = *(int64_t *)pCellVal->val;
×
1778
  } else {
1779
    GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
×
1780
  }
1781

1782
  pColVal->flag = CV_FLAG_VALUE;
×
1783
  return TSDB_CODE_SUCCESS;
×
1784
}
1785

1786
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1787
  int32_t code = TSDB_CODE_SUCCESS;
×
1788
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1789
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1790
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1791
    SCellVal  cellVal = {0};
×
1792
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1793
      break;
×
1794
    }
1795
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1796
  }
1797
  return code;
×
1798
}
1799

1800
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1801
  if (pCxt->msgIter.schemaLen <= 0) {
×
1802
    return TSDB_CODE_SUCCESS;
×
1803
  }
1804

1805
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1806
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1807
    return terrno;
×
1808
  }
1809

1810
  SDecoder decoder = {0};
×
1811
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1812
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1813
  tDecoderClear(&decoder);
×
1814

1815
  return code;
×
1816
}
1817

1818
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1819
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1820
  if (NULL == pReq2->aSubmitTbData) {
×
1821
    return terrno;
×
1822
  }
1823

1824
  SSubmitReqConvertCxt cxt = {0};
×
1825

1826
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1827
  while (TSDB_CODE_SUCCESS == code) {
×
1828
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1829
    if (TSDB_CODE_SUCCESS == code) {
×
1830
      if (NULL == cxt.pBlock) {
×
1831
        break;
×
1832
      }
1833
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1834
    }
1835
    if (TSDB_CODE_SUCCESS == code) {
×
1836
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1837
    }
1838
    if (TSDB_CODE_SUCCESS == code) {
×
1839
      code = vnodeDecodeCreateTbReq(&cxt);
×
1840
    }
1841
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1842
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1843
      if (TSDB_CODE_SUCCESS == code) {
×
1844
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1845
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1846
      }
1847
    }
1848
    if (TSDB_CODE_SUCCESS == code) {
×
1849
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1850
    }
1851
    if (TSDB_CODE_SUCCESS == code) {
×
1852
      taosMemoryFreeClear(cxt.pTbData);
×
1853
    }
1854
  }
1855

1856
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1857
  return code;
×
1858
}
1859

1860
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1861
  int32_t  code = TSDB_CODE_SUCCESS;
×
1862
  char    *pMsg = NULL;
×
1863
  uint32_t msglen = 0;
×
1864
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1865
  if (TSDB_CODE_SUCCESS == code) {
×
1866
    pMsg = taosMemoryMalloc(msglen);
×
1867
    if (NULL == pMsg) {
×
1868
      code = terrno;
×
1869
    }
1870
  }
1871
  if (TSDB_CODE_SUCCESS == code) {
×
1872
    SEncoder encoder;
1873
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1874
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1875
    tEncoderClear(&encoder);
×
1876
  }
1877
  if (TSDB_CODE_SUCCESS == code) {
×
1878
    *ppMsg = pMsg;
×
1879
  }
1880
  return code;
×
1881
}
1882

1883
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
10,900,675✔
1884
                                     SRpcMsg *pOriginalMsg) {
1885
  int32_t code = 0;
10,900,675✔
1886
  terrno = 0;
10,900,675✔
1887

1888
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
10,900,830✔
1889
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
10,900,830✔
1890
  SArray      *newTbUids = NULL;
10,900,830✔
1891
  int32_t      ret;
1892
  SEncoder     ec = {0};
10,900,830✔
1893

1894
  pRsp->code = TSDB_CODE_SUCCESS;
10,900,830✔
1895

1896
  void           *pAllocMsg = NULL;
10,900,830✔
1897
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
10,900,830✔
1898
  if (0 == pMsg->version) {
10,900,830!
1899
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1900
    if (TSDB_CODE_SUCCESS == code) {
×
1901
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1902
    }
1903
    if (TSDB_CODE_SUCCESS == code) {
×
1904
      pAllocMsg = pReq;
×
1905
    }
1906
    if (TSDB_CODE_SUCCESS != code) {
×
1907
      goto _exit;
×
1908
    }
1909
  } else {
1910
    // decode
1911
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
10,900,830✔
1912
    len -= sizeof(SSubmitReq2Msg);
10,900,830✔
1913
    SDecoder dc = {0};
10,900,830✔
1914
    tDecoderInit(&dc, pReq, len);
10,900,830✔
1915
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
10,900,712!
1916
      code = TSDB_CODE_INVALID_MSG;
×
1917
      goto _exit;
×
1918
    }
1919
    tDecoderClear(&dc);
10,900,548✔
1920
  }
1921

1922
  // scan
1923
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
10,900,760✔
1924
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
10,900,772✔
1925
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,900,772✔
1926
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
22,492,218✔
1927
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
11,591,071✔
1928

1929
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
11,591,050!
1930
      code = TSDB_CODE_INVALID_MSG;
×
1931
      goto _exit;
×
1932
    }
1933

1934
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,591,050✔
1935
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
129!
1936
        code = TSDB_CODE_INVALID_MSG;
×
1937
        goto _exit;
×
1938
      }
1939

1940
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
129✔
1941
      SRowKey   lastKey;
1942
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
129✔
1943
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
233✔
1944
        SRowKey key;
1945
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
104✔
1946
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
104!
1947
          code = TSDB_CODE_INVALID_MSG;
×
1948
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1949
          goto _exit;
×
1950
        }
1951
      }
1952
    } else {
1953
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
11,590,921✔
1954
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
11,590,921✔
1955
      SRowKey lastRowKey;
1956
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
382,114,814✔
1957
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
370,523,497!
1958
          code = TSDB_CODE_INVALID_MSG;
19,986✔
1959
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
19,986!
1960
          goto _exit;
×
1961
        }
1962
        if (iRow == 0) {
370,503,511✔
1963
          tRowGetKey(aRow[iRow], &lastRowKey);
11,590,912✔
1964
        } else {
1965
          SRowKey rowKey;
1966
          tRowGetKey(aRow[iRow], &rowKey);
358,912,599✔
1967

1968
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
358,907,201!
1969
            code = TSDB_CODE_INVALID_MSG;
×
1970
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
1971
            goto _exit;
×
1972
          }
1973
          lastRowKey = rowKey;
358,929,507✔
1974
        }
1975
      }
1976
    }
1977
  }
1978

1979
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
22,492,754✔
1980
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
11,591,091✔
1981

1982
    if (pSubmitTbData->pCreateTbReq) {
11,590,935✔
1983
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
43,297✔
1984
    } else {
1985
      SMetaInfo info = {0};
11,547,638✔
1986

1987
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
11,547,638✔
1988
      if (code) {
11,548,734!
1989
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1990
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
1991
        goto _exit;
×
1992
      }
1993

1994
      if (info.suid != pSubmitTbData->suid) {
11,548,773!
1995
        code = TSDB_CODE_INVALID_MSG;
×
1996
        goto _exit;
×
1997
      }
1998

1999
      if (info.suid) {
11,548,773✔
2000
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
10,805,302!
2001
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2002
        }
2003
      }
2004

2005
      if (pSubmitTbData->sver != info.skmVer) {
11,548,310!
2006
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2007
        goto _exit;
×
2008
      }
2009
    }
2010

2011
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,591,607✔
2012
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
129✔
2013
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
129✔
2014

2015
      if (nColData <= 0) {
129!
2016
        code = TSDB_CODE_INVALID_MSG;
×
2017
        goto _exit;
×
2018
      }
2019

2020
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
129!
2021
          aColData[0].nVal <= 0) {
129!
2022
        code = TSDB_CODE_INVALID_MSG;
×
2023
        goto _exit;
×
2024
      }
2025

2026
      for (int32_t j = 1; j < nColData; j++) {
564✔
2027
        if (aColData[j].nVal != aColData[0].nVal) {
435!
2028
          code = TSDB_CODE_INVALID_MSG;
×
2029
          goto _exit;
×
2030
        }
2031
      }
2032
    }
2033
  }
2034

2035
  vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
10,901,663✔
2036

2037
  // loop to handle
2038
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
22,491,945✔
2039
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
11,590,988✔
2040

2041
    // create table
2042
    if (pSubmitTbData->pCreateTbReq) {
11,590,737✔
2043
      // alloc if need
2044
      if (pSubmitRsp->aCreateTbRsp == NULL &&
43,316✔
2045
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
26,793!
2046
              NULL) {
2047
        code = terrno;
×
2048
        goto _exit;
8✔
2049
      }
2050

2051
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
43,316✔
2052

2053
      // create table
2054
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
43,313✔
2055
        // create table success
2056

2057
        if (newTbUids == NULL &&
62,626!
2058
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
25,964✔
2059
          code = terrno;
×
2060
          goto _exit;
×
2061
        }
2062

2063
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
73,325!
2064
          code = terrno;
×
2065
          goto _exit;
×
2066
        }
2067

2068
        if (pCreateTbRsp->pMeta) {
36,663!
2069
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
36,663✔
2070
        }
2071
      } else {  // create table failed
2072
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
6,656✔
2073
          code = terrno;
8✔
2074
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2075
                 tstrerror(terrno));
2076
          goto _exit;
8✔
2077
        }
2078
        terrno = 0;
6,648✔
2079
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
6,648✔
2080
      }
2081
    }
2082

2083
    // insert data
2084
    int32_t affectedRows;
2085
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
11,590,732✔
2086
    if (code) goto _exit;
11,591,228!
2087

2088
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
11,591,228✔
2089
    if (code) goto _exit;
11,591,077!
2090

2091
    pSubmitRsp->affectedRows += affectedRows;
11,591,077✔
2092
  }
2093

2094
  // update the affected table uid list
2095
  if (taosArrayGetSize(newTbUids) > 0) {
10,900,957✔
2096
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
25,964✔
2097
           (int32_t)taosArrayGetSize(newTbUids));
2098
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
25,964!
2099
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2100
    }
2101
  }
2102

2103
_exit:
10,900,754✔
2104
  // message
2105
  pRsp->code = code;
10,900,762✔
2106
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
10,900,762!
2107
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
10,900,572✔
2108
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
10,900,787✔
2109
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
10,900,830!
2110
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2111
  }
2112
  tEncoderClear(&ec);
10,900,710✔
2113

2114
  // update statistics
2115
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
10,900,855✔
2116
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
10,900,897✔
2117
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
10,900,881✔
2118

2119
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
10,900,879!
2120
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2121
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2122
                                   pVnode->monitor.strClusterId,
×
2123
                                   pVnode->monitor.strDnodeId,
×
2124
                                   tsLocalEp,
2125
                                   pVnode->monitor.strVgId,
×
2126
                                   pOriginalMsg->info.conn.user,
×
2127
                                   "Success"};
2128
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2129
  }
2130

2131
  if (code == 0) {
10,900,879✔
2132
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
10,900,867✔
2133
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
10,900,883✔
2134
  }
2135
  /*
2136
  if (code == 0) {
2137
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2138
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2139

2140
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2141
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2142
                                          pOriginalMsg->info.conn.user, "Success"};
2143
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2144
  }
2145
  else{
2146
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2147
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2148
                                        pOriginalMsg->info.conn.user, "Failed"};
2149
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2150
  }
2151
  */
2152

2153
  // clear
2154
  taosArrayDestroy(newTbUids);
10,900,797✔
2155
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
10,900,673!
2156
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
10,900,880✔
2157

2158
  if (code) terrno = code;
10,900,790✔
2159

2160
  taosMemoryFree(pAllocMsg);
10,900,790!
2161

2162
  return code;
10,900,776✔
2163
}
2164

2165
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
25✔
2166
  SVCreateTSmaReq req = {0};
25✔
2167
  SDecoder        coder = {0};
25✔
2168

2169
  if (pRsp) {
25!
2170
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
25✔
2171
    pRsp->code = TSDB_CODE_SUCCESS;
25✔
2172
    pRsp->pCont = NULL;
25✔
2173
    pRsp->contLen = 0;
25✔
2174
  }
2175

2176
  // decode and process req
2177
  tDecoderInit(&coder, pReq, len);
25✔
2178

2179
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
25!
2180
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2181
    if (pRsp) pRsp->code = terrno;
×
2182
    goto _err;
×
2183
  }
2184

2185
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
25!
2186
    if (pRsp) pRsp->code = terrno;
×
2187
    goto _err;
×
2188
  }
2189

2190
  tDecoderClear(&coder);
25✔
2191
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
25!
2192
         req.indexName, req.indexUid, ver, req.tableUid);
2193
  return 0;
25✔
2194

2195
_err:
×
2196
  tDecoderClear(&coder);
×
2197
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2198
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2199
  return terrno;
×
2200
}
2201

2202
/**
2203
 * @brief specific for smaDstVnode
2204
 *
2205
 * @param pVnode
2206
 * @param pCont
2207
 * @param contLen
2208
 * @return int32_t
2209
 */
2210
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2211
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2212
}
2213

2214
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
32✔
2215
  int32_t code = TSDB_CODE_SUCCESS;
32✔
2216

2217
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
32!
2218
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2219

2220
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2221
  code = metaTrimTables(pVnode->pMeta, ver);
32✔
2222

2223
  return code;
32✔
2224
}
2225

2226
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,987✔
2227
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
7,987!
2228
  int32_t code = TSDB_CODE_SUCCESS;
7,987✔
2229
  if (!pVnode->config.hashChange) {
7,987✔
2230
    goto _exit;
7,955✔
2231
  }
2232

2233
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
32✔
2234
  if (code < 0) {
32!
2235
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2236
    goto _exit;
×
2237
  }
2238
  pVnode->config.hashChange = false;
32✔
2239

2240
_exit:
7,987✔
2241
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
7,987✔
2242
  pRsp->code = code;
7,987✔
2243
  pRsp->pCont = NULL;
7,987✔
2244
  pRsp->contLen = 0;
7,987✔
2245

2246
  return code;
7,987✔
2247
}
2248

2249
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2250
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2251

2252
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
394✔
2253
  bool walChanged = false;
394✔
2254
  bool tsdbChanged = false;
394✔
2255

2256
  SAlterVnodeConfigReq req = {0};
394✔
2257
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
394!
2258
    terrno = TSDB_CODE_INVALID_MSG;
×
2259
    return TSDB_CODE_INVALID_MSG;
×
2260
  }
2261

2262
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
395!
2263
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2264
        "s3Compact:%d fsync:%d level:%d "
2265
        "walRetentionPeriod:%d walRetentionSize:%d",
2266
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2267
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2268
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2269
        req.walRetentionSize);
2270

2271
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
395✔
2272
    pVnode->config.cacheLastSize = req.cacheLastSize;
2✔
2273
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
2✔
2274
  }
2275

2276
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
395!
2277
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2278
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2279
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2280
  }
2281

2282
  if (pVnode->config.szCache != req.pages) {
395!
2283
    if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
×
2284
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2285
             pVnode->config.szCache, req.pages, tstrerror(errno));
2286
      return errno;
×
2287
    } else {
2288
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2289
      pVnode->config.szCache = req.pages;
×
2290
    }
2291
  }
2292

2293
  if (pVnode->config.cacheLast != req.cacheLast) {
395✔
2294
    pVnode->config.cacheLast = req.cacheLast;
114✔
2295
  }
2296

2297
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
395✔
2298
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2299
    walChanged = true;
54✔
2300
  }
2301

2302
  if (pVnode->config.walCfg.level != req.walLevel) {
395✔
2303
    if (pVnode->config.walCfg.level == 0) {
36!
2304
      pVnode->config.walCfg.clearFiles = 1;
×
2305
    }
2306
    pVnode->config.walCfg.level = req.walLevel;
36✔
2307
    walChanged = true;
36✔
2308
  }
2309

2310
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
395✔
2311
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2312
    walChanged = true;
132✔
2313
  }
2314

2315
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
395!
2316
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2317
    walChanged = true;
×
2318
  }
2319

2320
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
395✔
2321
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
54✔
2322
    if (!VND_IS_RSMA(pVnode)) {
54!
2323
      tsdbChanged = true;
54✔
2324
    }
2325
  }
2326

2327
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
395✔
2328
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
54✔
2329
    if (!VND_IS_RSMA(pVnode)) {
54!
2330
      tsdbChanged = true;
54✔
2331
    }
2332
  }
2333

2334
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
395✔
2335
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
56✔
2336
    if (!VND_IS_RSMA(pVnode)) {
56!
2337
      tsdbChanged = true;
56✔
2338
    }
2339
  }
2340

2341
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
395✔
2342
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2343
    if (!VND_IS_RSMA(pVnode)) {
2!
2344
      tsdbChanged = true;
2✔
2345
    }
2346
  }
2347

2348
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
395!
2349
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2350
      pVnode->config.sttTrigger = req.sttTrigger;
×
2351
    } else {
2352
      vnodeAWait(&pVnode->commitTask);
×
2353

2354
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2355
      if (ret != 0) {
×
2356
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
×
2357
      }
2358

2359
      pVnode->config.sttTrigger = req.sttTrigger;
×
2360
      tsdbEnableBgTask(pVnode->pTsdb);
×
2361
    }
2362
  }
2363

2364
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
395!
2365
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2366
  }
2367

2368
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
395!
2369
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2370
  }
2371
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
395!
2372
    pVnode->config.s3Compact = req.s3Compact;
×
2373
  }
2374

2375
  if (walChanged) {
395✔
2376
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
222!
2377
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
×
2378
    }
2379
  }
2380

2381
  if (tsdbChanged) {
396✔
2382
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
58✔
2383
  }
2384

2385
  return 0;
396✔
2386
}
2387

2388
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,849✔
2389
  SBatchDeleteReq deleteReq;
2390
  SDecoder        decoder;
2391
  tDecoderInit(&decoder, pReq, len);
1,849✔
2392
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
1,850!
2393
    tDecoderClear(&decoder);
×
2394
    return terrno = TSDB_CODE_INVALID_MSG;
×
2395
  }
2396

2397
  SMetaReader mr = {0};
1,849✔
2398
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
1,849✔
2399
  STsdb *pTsdb = pVnode->pTsdb;
1,848✔
2400

2401
  if (deleteReq.level) {
1,848✔
2402
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2403
  }
2404

2405
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
1,848✔
2406
  for (int32_t i = 0; i < sz; i++) {
4,511✔
2407
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
2,660✔
2408
    char             *name = pOneReq->tbname;
2,660✔
2409
    if (metaGetTableEntryByName(&mr, name) < 0) {
2,660✔
2410
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
1,197✔
2411
      continue;
1,197✔
2412
    }
2413

2414
    int64_t uid = mr.me.uid;
1,466✔
2415

2416
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,466✔
2417
    if (code < 0) {
1,466!
2418
      terrno = code;
×
2419
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2420
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2421
    }
2422

2423
    if (deleteReq.level == 0) {
1,466✔
2424
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
850✔
2425
      if (code < 0) {
850!
2426
        terrno = code;
×
2427
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2428
               ", end ts:%" PRId64,
2429
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2430
      }
2431
    }
2432
    tDecoderClear(&mr.coder);
1,466✔
2433
  }
2434
  metaReaderClear(&mr);
1,851✔
2435
  taosArrayDestroy(deleteReq.deleteReqs);
1,850✔
2436
  return 0;
1,850✔
2437
}
2438

2439
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
65,022✔
2440
                                     SRpcMsg *pOriginalMsg) {
2441
  int32_t     code = 0;
65,022✔
2442
  SDecoder   *pCoder = &(SDecoder){0};
65,022✔
2443
  SDeleteRes *pRes = &(SDeleteRes){0};
65,022✔
2444

2445
  pRsp->msgType = TDMT_VND_DELETE_RSP;
65,022✔
2446
  pRsp->pCont = NULL;
65,022✔
2447
  pRsp->contLen = 0;
65,022✔
2448
  pRsp->code = TSDB_CODE_SUCCESS;
65,022✔
2449

2450
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
65,022✔
2451
  if (pRes->uidList == NULL) {
65,022!
2452
    code = terrno;
×
2453
    goto _err;
×
2454
  }
2455

2456
  tDecoderInit(pCoder, pReq, len);
65,022✔
2457
  code = tDecodeDeleteRes(pCoder, pRes);
65,022✔
2458
  if (code) goto _err;
65,023!
2459

2460
  if (pRes->affectedRows > 0) {
65,023✔
2461
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
126,546✔
2462
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
63,768✔
2463
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
63,768✔
2464
      if (code) goto _err;
63,767!
2465
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
63,767✔
2466
      if (code) goto _err;
63,767!
2467
    }
2468
  }
2469

2470
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
65,021✔
2471

2472
  tDecoderClear(pCoder);
65,022✔
2473
  taosArrayDestroy(pRes->uidList);
65,023✔
2474

2475
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
65,023✔
2476
  int32_t     ret = 0;
65,023✔
2477
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
65,023!
2478
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
65,022✔
2479
  SEncoder ec = {0};
65,023✔
2480
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
65,023✔
2481
  code = tEncodeSVDeleteRsp(&ec, &rsp);
65,023✔
2482
  if (code) goto _err;
65,022!
2483
  tEncoderClear(&ec);
65,022✔
2484
  return code;
65,023✔
2485

2486
_err:
×
2487
  /*
2488
  if(code == TSDB_CODE_SUCCESS){
2489
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2490
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2491
                                        pOriginalMsg->info.conn.user, "Success"};
2492
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2493
  }
2494
  else{
2495
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2496
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2497
                                        pOriginalMsg->info.conn.user, "Failed"};
2498
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2499
  }
2500
  */
2501

2502
  return code;
×
2503
}
2504
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
27✔
2505
  SVCreateStbReq req = {0};
27✔
2506
  SDecoder       dc = {0};
27✔
2507
  int32_t        code = 0;
27✔
2508

2509
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
27✔
2510
  pRsp->code = TSDB_CODE_SUCCESS;
27✔
2511
  pRsp->pCont = NULL;
27✔
2512
  pRsp->contLen = 0;
27✔
2513

2514
  tDecoderInit(&dc, pReq, len);
27✔
2515
  // decode req
2516
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
27!
2517
    tDecoderClear(&dc);
×
2518
    return terrno = TSDB_CODE_INVALID_MSG;
×
2519
  }
2520

2521
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
27✔
2522
  if (code) {
27!
2523
    pRsp->code = code;
×
2524
    goto _err;
×
2525
  }
2526
  tDecoderClear(&dc);
27✔
2527
  return 0;
27✔
2528

2529
_err:
×
2530
  tDecoderClear(&dc);
×
2531
  return code;
×
2532
}
2533
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
10✔
2534
  SDropIndexReq req = {0};
10✔
2535
  int32_t       code = 0;
10✔
2536
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
10✔
2537
  pRsp->code = TSDB_CODE_SUCCESS;
10✔
2538
  pRsp->pCont = NULL;
10✔
2539
  pRsp->contLen = 0;
10✔
2540

2541
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
10!
2542
    pRsp->code = code;
×
2543
    return code;
×
2544
  }
2545

2546
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
10✔
2547
  if (code) {
10!
2548
    pRsp->code = code;
×
2549
    return code;
×
2550
  }
2551
  return TSDB_CODE_SUCCESS;
10✔
2552
}
2553

2554
extern int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2555

2556
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
203✔
2557
  if (!pVnode->restored) {
203✔
2558
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
173!
2559
    return 0;
173✔
2560
  }
2561
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
30✔
2562
}
2563

2564
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2565
  if (syncCheckMember(pVnode->sync) != 0) {
×
2566
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2567
  }
2568

2569
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2570
  pRsp->code = TSDB_CODE_SUCCESS;
×
2571
  pRsp->pCont = NULL;
×
2572
  pRsp->contLen = 0;
×
2573

2574
  return 0;
×
2575
}
2576

2577
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
12✔
2578
  SSyncState syncState = syncGetState(pVnode->sync);
12✔
2579
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
12✔
2580
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
4✔
2581
  }
2582

2583
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
8✔
2584
  if (vnodeGetArbToken(pVnode, token) != 0) {
8!
2585
    return terrno = TSDB_CODE_NOT_FOUND;
×
2586
  }
2587

2588
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
8✔
2589
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
6✔
2590
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
2✔
2591
  }
2592

2593
  terrno = TSDB_CODE_SUCCESS;
6✔
2594
  return 0;
6✔
2595
}
2596

2597
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
7✔
2598
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
7✔
2599
  if (code != 0) {
7✔
2600
    return code;
4✔
2601
  }
2602

2603
  return syncCheckSynced(pVnode->sync);
3✔
2604
}
2605

2606
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
7✔
2607
  int32_t code = 0;
7✔
2608

2609
  SVArbCheckSyncReq syncReq = {0};
7✔
2610

2611
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
7✔
2612
  if (code) {
7!
2613
    return terrno = code;
×
2614
  }
2615

2616
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
7✔
2617
  pRsp->code = TSDB_CODE_SUCCESS;
7✔
2618
  pRsp->pCont = NULL;
7✔
2619
  pRsp->contLen = 0;
7✔
2620

2621
  SVArbCheckSyncRsp syncRsp = {0};
7✔
2622
  syncRsp.arbToken = syncReq.arbToken;
7✔
2623
  syncRsp.member0Token = syncReq.member0Token;
7✔
2624
  syncRsp.member1Token = syncReq.member1Token;
7✔
2625
  syncRsp.vgId = TD_VID(pVnode);
7✔
2626

2627
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
7✔
2628
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
4!
2629
  }
2630
  syncRsp.errCode = terrno;
7✔
2631

2632
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
7!
2633
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2634
    code = -1;
×
2635
    goto _OVER;
×
2636
  }
2637

2638
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
7✔
2639
  if (contLen <= 0) {
7!
2640
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2641
    code = -1;
×
2642
    goto _OVER;
×
2643
  }
2644
  void *pHead = rpcMallocCont(contLen);
7✔
2645
  if (!pHead) {
7!
2646
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2647
    code = -1;
×
2648
    goto _OVER;
×
2649
  }
2650

2651
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
7!
2652
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2653
    rpcFreeCont(pHead);
×
2654
    code = -1;
×
2655
    goto _OVER;
×
2656
  }
2657

2658
  pRsp->pCont = pHead;
7✔
2659
  pRsp->contLen = contLen;
7✔
2660

2661
  terrno = TSDB_CODE_SUCCESS;
7✔
2662

2663
_OVER:
7✔
2664
  tFreeSVArbCheckSyncReq(&syncReq);
7✔
2665
  return code;
7✔
2666
}
2667

2668
#ifndef TD_ENTERPRISE
2669
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2670
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2671
#endif
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