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

taosdata / TDengine / #5032

24 Apr 2026 11:25AM UTC coverage: 73.076% (+0.2%) from 72.876%
#5032

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1344 of 1975 new or added lines in 48 files covered. (68.05%)

527 existing lines in 138 files now uncovered.

275965 of 377640 relevant lines covered (73.08%)

132797765.0 hits per line

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

85.54
/source/dnode/vnode/src/meta/metaSnapshot.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 <string.h>
17
#include "meta.h"
18
#include "tdataformat.h"
19

20
// SMetaSnapReader ========================================
21
struct SMetaSnapReader {
22
  SMeta*  pMeta;
23
  int64_t sver;
24
  int64_t ever;
25
  TBC*    pTbc;
26
  int32_t iLoop;
27
};
28

29
int32_t metaSnapReaderOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapReader** ppReader) {
30,714✔
30
  int32_t          code = 0;
30,714✔
31
  int32_t          lino;
32
  int32_t          c = 0;
30,714✔
33
  SMetaSnapReader* pReader = NULL;
30,714✔
34

35
  // alloc
36
  pReader = (SMetaSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
30,714✔
37
  if (pReader == NULL) {
30,714✔
38
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
39
  }
40
  pReader->pMeta = pMeta;
30,714✔
41
  pReader->sver = sver;
30,714✔
42
  pReader->ever = ever;
30,714✔
43

44
  // impl
45
  code = tdbTbcOpen(pMeta->pTbDb, &pReader->pTbc, NULL);
30,714✔
46
  TSDB_CHECK_CODE(code, lino, _exit);
30,714✔
47

48
  code = tdbTbcMoveTo(pReader->pTbc, &(STbDbKey){.version = sver, .uid = INT64_MIN}, sizeof(STbDbKey), &c);
30,714✔
49
  TSDB_CHECK_CODE(code, lino, _exit);
30,714✔
50

51
_exit:
30,714✔
52
  if (code) {
30,714✔
53
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
54
    metaSnapReaderClose(&pReader);
×
55
    *ppReader = NULL;
×
56
  } else {
57
    metaInfo("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
30,714✔
58
    *ppReader = pReader;
30,714✔
59
  }
60
  return code;
30,714✔
61
}
62

63
void metaSnapReaderClose(SMetaSnapReader** ppReader) {
30,714✔
64
  if (ppReader && *ppReader) {
30,714✔
65
    tdbTbcClose((*ppReader)->pTbc);
30,714✔
66
    taosMemoryFree(*ppReader);
30,714✔
67
    *ppReader = NULL;
30,714✔
68
  }
69
}
30,714✔
70

71
extern int metaDecodeEntryImpl(SDecoder* pCoder, SMetaEntry* pME, bool headerOnly);
72

73
static int32_t metaDecodeEntryHeader(void* data, int32_t size, SMetaEntry* entry) {
339,074✔
74
  SDecoder decoder = {0};
339,074✔
75
  tDecoderInit(&decoder, (uint8_t*)data, size);
339,074✔
76

77
  int32_t code = metaDecodeEntryImpl(&decoder, entry, true);
339,074✔
78
  if (code) {
339,074✔
79
    tDecoderClear(&decoder);
×
80
    return code;
×
81
  }
82

83
  tDecoderClear(&decoder);
339,074✔
84
  return 0;
339,074✔
85
}
86

87
int32_t metaSnapRead(SMetaSnapReader* pReader, uint8_t** ppData) {
199,224✔
88
  int32_t     code = 0;
199,224✔
89
  const void* pKey = NULL;
199,224✔
90
  const void* pData = NULL;
199,224✔
91
  int32_t     nKey = 0;
199,224✔
92
  int32_t     nData = 0;
199,224✔
93
  STbDbKey    key;
94
  int32_t     c;
199,224✔
95

96
  *ppData = NULL;
199,224✔
97
  while (pReader->iLoop < 2) {
431,216✔
98
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
400,502✔
99
      pReader->iLoop++;
61,428✔
100

101
      // Reopen the cursor to read from the beginning
102
      tdbTbcClose(pReader->pTbc);
61,428✔
103
      pReader->pTbc = NULL;
61,428✔
104
      code = tdbTbcOpen(pReader->pMeta->pTbDb, &pReader->pTbc, NULL);
61,428✔
105
      if (code) {
61,428✔
106
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pReader->pMeta->pVnode), __func__, __FILE__, __LINE__,
×
107
                  tstrerror(code));
108
        goto _exit;
×
109
      }
110

111
      code = tdbTbcMoveTo(pReader->pTbc, &(STbDbKey){.version = pReader->sver, .uid = INT64_MIN}, sizeof(STbDbKey), &c);
61,428✔
112
      if (code) {
61,428✔
113
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pReader->pMeta->pVnode), __func__, __FILE__, __LINE__,
×
114
                  tstrerror(code));
115
        goto _exit;
×
116
      }
117

118
      continue;
61,428✔
119
    }
120

121
    // Decode meta entry
122
    SMetaEntry entry = {0};
339,074✔
123
    code = metaDecodeEntryHeader((void*)pData, nData, &entry);
339,074✔
124
    if (code) {
339,074✔
125
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pReader->pMeta->pVnode), __func__, __FILE__, __LINE__,
×
126
                tstrerror(code));
127
      goto _exit;
×
128
    }
129

130
    key = ((STbDbKey*)pKey)[0];
339,074✔
131
    if (key.version < pReader->sver                                       //
339,074✔
132
        || (pReader->iLoop == 0 && TABS(entry.type) != TSDB_SUPER_TABLE)  // First loop send super table entry
337,020✔
133
        || (pReader->iLoop == 1 && TABS(entry.type) == TSDB_SUPER_TABLE)  // Second loop send non-super table entry
194,784✔
134
    ) {
135
      if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
170,564✔
136
        metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
137
      }
138
      continue;
170,564✔
139
    }
140

141
    if (!pData || !nData) {
168,510✔
142
      metaError("meta/snap: invalide nData: %" PRId32 " meta snap read failed.", nData);
×
143
      goto _exit;
×
144
    }
145

146
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + nData);
168,510✔
147
    if (*ppData == NULL) {
168,510✔
148
      code = terrno;
×
149
      goto _exit;
×
150
    }
151

152
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
168,510✔
153
    pHdr->type = SNAP_DATA_META;
168,510✔
154
    pHdr->size = nData;
168,510✔
155
    memcpy(pHdr->data, pData, nData);
168,510✔
156

157
    metaDebug("vgId:%d, vnode snapshot meta read data, version:%" PRId64 " uid:%" PRId64 " blockLen:%d",
168,510✔
158
              TD_VID(pReader->pMeta->pVnode), key.version, key.uid, nData);
159

160
    if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
168,510✔
161
      metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
162
    }
163
    break;
168,510✔
164
  }
165

166
_exit:
199,224✔
167
  if (code) {
199,224✔
168
    metaError("vgId:%d, vnode snapshot meta read data failed since %s", TD_VID(pReader->pMeta->pVnode),
×
169
              tstrerror(code));
170
  }
171
  return code;
199,224✔
172
}
173

174
// SMetaSnapWriter ========================================
175
struct SMetaSnapWriter {
176
  SMeta*  pMeta;
177
  int64_t sver;
178
  int64_t ever;
179
};
180

181
int32_t metaSnapWriterOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapWriter** ppWriter) {
26,236✔
182
  int32_t          code = 0;
26,236✔
183
  int32_t          lino;
184
  SMetaSnapWriter* pWriter;
185

186
  // alloc
187
  pWriter = (SMetaSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
26,236✔
188
  if (pWriter == NULL) {
26,236✔
189
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
190
  }
191
  pWriter->pMeta = pMeta;
26,236✔
192
  pWriter->sver = sver;
26,236✔
193
  pWriter->ever = ever;
26,236✔
194

195
  code = metaBegin(pMeta, META_BEGIN_HEAP_NIL);
26,236✔
196
  TSDB_CHECK_CODE(code, lino, _exit);
26,236✔
197

198
_exit:
26,236✔
199
  if (code) {
26,236✔
200
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
201
    taosMemoryFree(pWriter);
×
202
    *ppWriter = NULL;
×
203
  } else {
204
    metaDebug("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
26,236✔
205
    *ppWriter = pWriter;
26,236✔
206
  }
207
  return code;
26,236✔
208
}
209

210
int32_t metaSnapWriterClose(SMetaSnapWriter** ppWriter, int8_t rollback) {
26,236✔
211
  int32_t          code = 0;
26,236✔
212
  SMetaSnapWriter* pWriter = *ppWriter;
26,236✔
213

214
  if (rollback) {
26,236✔
UNCOV
215
    metaInfo("vgId:%d, meta snapshot writer close and rollback start ", TD_VID(pWriter->pMeta->pVnode));
×
UNCOV
216
    code = metaAbort(pWriter->pMeta);
×
UNCOV
217
    metaInfo("vgId:%d, meta snapshot writer close and rollback finished, code:0x%x", TD_VID(pWriter->pMeta->pVnode),
×
218
             code);
UNCOV
219
    if (code) goto _err;
×
220
  } else {
221
    code = metaCommit(pWriter->pMeta, pWriter->pMeta->txn);
26,236✔
222
    if (code) goto _err;
26,236✔
223
    code = metaFinishCommit(pWriter->pMeta, pWriter->pMeta->txn);
26,236✔
224
    if (code) goto _err;
26,236✔
225
  }
226
  taosMemoryFree(pWriter);
26,236✔
227
  *ppWriter = NULL;
26,236✔
228

229
  return code;
26,236✔
230

231
_err:
×
232
  metaError("vgId:%d, meta snapshot writer close failed since %s", TD_VID(pWriter->pMeta->pVnode), tstrerror(code));
×
233
  return code;
×
234
}
235

236
int32_t metaSnapWrite(SMetaSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
168,374✔
237
  int32_t    code = 0;
168,374✔
238
  int32_t    lino = 0;
168,374✔
239
  SMeta*     pMeta = pWriter->pMeta;
168,374✔
240
  SMetaEntry metaEntry = {0};
168,374✔
241
  SDecoder*  pDecoder = &(SDecoder){0};
168,374✔
242

243
  tDecoderInit(pDecoder, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
168,374✔
244
  code = metaDecodeEntry(pDecoder, &metaEntry);
168,374✔
245
  TSDB_CHECK_CODE(code, lino, _exit);
168,374✔
246

247
  metaHandleSyncEntry(pMeta, &metaEntry);
168,374✔
248

249
_exit:
168,374✔
250
  if (code) {
168,374✔
251
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
252
  }
253
  tDecoderClear(pDecoder);
168,374✔
254
  return code;
168,374✔
255
}
256

257
typedef struct STableInfoForChildTable {
258
  char*           tableName;
259
  SSchemaWrapper* schemaRow;
260
  SSchemaWrapper* tagRow;
261
  SExtSchema*     pExtSchemas;
262
} STableInfoForChildTable;
263

264
static void destroySTableInfoForChildTable(void* data) {
198,057✔
265
  STableInfoForChildTable* pData = (STableInfoForChildTable*)data;
198,057✔
266
  taosMemoryFree(pData->tableName);
198,057✔
267
  tDeleteSchemaWrapper(pData->schemaRow);
198,057✔
268
  tDeleteSchemaWrapper(pData->tagRow);
198,057✔
269
  taosMemoryFreeClear(pData->pExtSchemas);
198,057✔
270
}
198,057✔
271

272
static int32_t MoveToSnapShotVersion(SSnapContext* ctx) {
134,799✔
273
  int32_t code = 0;
134,799✔
274
  tdbTbcClose((TBC*)ctx->pCur);
134,799✔
275
  code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
134,799✔
276
  if (code != 0) {
134,799✔
277
    return TAOS_GET_TERRNO(code);
×
278
  }
279
  STbDbKey key = {.version = ctx->snapVersion, .uid = INT64_MAX};
134,799✔
280
  int      c = 0;
135,175✔
281
  code = tdbTbcMoveTo((TBC*)ctx->pCur, &key, sizeof(key), &c);
135,175✔
282
  if (code != 0) {
134,799✔
283
    return TAOS_GET_TERRNO(code);
×
284
  }
285
  if (c < 0) {
134,799✔
286
    if (tdbTbcMoveToPrev((TBC*)ctx->pCur) != 0) {
4,711✔
287
      metaTrace("vgId:%d, vnode snapshot move to prev failed", TD_VID(ctx->pMeta->pVnode));
×
288
    }
289
  }
290
  return 0;
134,799✔
291
}
292

293
static int32_t MoveToPosition(SSnapContext* ctx, int64_t ver, int64_t uid) {
1,725,577✔
294
  tdbTbcClose((TBC*)ctx->pCur);
1,725,577✔
295
  int32_t code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
1,725,577✔
296
  if (code != 0) {
1,725,249✔
297
    return TAOS_GET_TERRNO(code);
×
298
  }
299
  STbDbKey key = {.version = ver, .uid = uid};
1,725,249✔
300
  int      c = 0;
1,725,249✔
301
  code = tdbTbcMoveTo((TBC*)ctx->pCur, &key, sizeof(key), &c);
1,725,577✔
302
  if (code != 0) {
1,725,577✔
303
    return TAOS_GET_TERRNO(code);
×
304
  }
305
  return c;
1,725,577✔
306
}
307

308
static int32_t MoveToFirst(SSnapContext* ctx) {
135,175✔
309
  tdbTbcClose((TBC*)ctx->pCur);
135,175✔
310
  int32_t code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
135,175✔
311
  if (code != 0) {
135,175✔
312
    return TAOS_GET_TERRNO(code);
×
313
  }
314
  code = tdbTbcMoveToFirst((TBC*)ctx->pCur);
135,175✔
315
  if (code != 0) {
135,175✔
316
    return TAOS_GET_TERRNO(code);
×
317
  }
318
  return 0;
135,175✔
319
}
320

321
static int32_t saveSuperTableInfoForChildTable(SMetaEntry* me, SHashObj* suidInfo) {
198,057✔
322
  STableInfoForChildTable* data = (STableInfoForChildTable*)taosHashGet(suidInfo, &me->uid, sizeof(tb_uid_t));
198,057✔
323
  if (data) {
198,057✔
324
    return 0;
×
325
  }
326
  int32_t                 code = 0;
198,057✔
327
  STableInfoForChildTable dataTmp = {0};
198,057✔
328
  dataTmp.tableName = taosStrdup(me->name);
198,057✔
329
  if (dataTmp.tableName == NULL) {
198,057✔
330
    code = terrno;
×
331
    goto END;
×
332
  }
333
  dataTmp.schemaRow = tCloneSSchemaWrapper(&me->stbEntry.schemaRow);
198,057✔
334
  if (dataTmp.schemaRow == NULL) {
198,057✔
335
    code = TSDB_CODE_OUT_OF_MEMORY;
×
336
    goto END;
×
337
  }
338
  dataTmp.tagRow = tCloneSSchemaWrapper(&me->stbEntry.schemaTag);
198,057✔
339
  if (dataTmp.tagRow == NULL) {
198,057✔
340
    code = TSDB_CODE_OUT_OF_MEMORY;
×
341
    goto END;
×
342
  }
343
  if (me->pExtSchemas != NULL) {
198,057✔
344
    dataTmp.pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me->stbEntry.schemaRow.nCols);
8,667✔
345
    if (dataTmp.pExtSchemas == NULL) {
8,667✔
346
      code = TSDB_CODE_OUT_OF_MEMORY;
×
347
      goto END;
×
348
    }
349
    memcpy(dataTmp.pExtSchemas, me->pExtSchemas, sizeof(SExtSchema) * me->stbEntry.schemaRow.nCols);
8,667✔
350
  }
351
  
352
  code = taosHashPut(suidInfo, &me->uid, sizeof(tb_uid_t), &dataTmp, sizeof(STableInfoForChildTable));
198,057✔
353
  if (code != 0) {
198,057✔
354
    goto END;
×
355
  }
356
  return 0;
198,057✔
357

358
END:
×
359
  destroySTableInfoForChildTable(&dataTmp);
×
360
  return TAOS_GET_TERRNO(code);
×
361
}
362

363
int32_t buildSnapContext(SVnode* pVnode, int64_t snapVersion, int64_t suid, int8_t subType, int8_t withMeta,
135,175✔
364
                         SSnapContext** ctxRet) {
365
  int32_t code = 0;
135,175✔
366
  int32_t lino = 0;
135,175✔
367
  SDecoder   dc = {0};
135,175✔
368
  void* pKey = NULL;
135,175✔
369
  void* pVal = NULL;
135,175✔
370
  int   vLen = 0, kLen = 0;
135,175✔
371

372
  metaRLock(pVnode->pMeta);
135,175✔
373
  SSnapContext* ctx = taosMemoryCalloc(1, sizeof(SSnapContext));
135,175✔
374
  TSDB_CHECK_NULL(ctx, code, lino, END, terrno);
135,175✔
375
  *ctxRet = ctx;
135,175✔
376
  ctx->pMeta = pVnode->pMeta;
135,175✔
377
  ctx->snapVersion = snapVersion;
135,175✔
378
  ctx->suid = suid;
135,175✔
379
  ctx->subType = subType;
135,175✔
380
  ctx->queryMeta = withMeta;
135,175✔
381
  ctx->withMeta = withMeta;
135,175✔
382
  ctx->idVersion = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
135,175✔
383
  TSDB_CHECK_NULL(ctx->idVersion, code, lino, END, terrno);
135,175✔
384
  ctx->suidInfo = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
135,175✔
385
  TSDB_CHECK_NULL(ctx->suidInfo, code, lino, END, terrno);
135,175✔
386
  taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable);
135,175✔
387

388
  ctx->index = 0;
135,175✔
389
  ctx->idList = taosArrayInit(100, sizeof(int64_t));
135,175✔
390
  TSDB_CHECK_NULL(ctx->idList, code, lino, END, terrno);
135,175✔
391

392
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
135,175✔
393
  code = MoveToFirst(ctx);
135,175✔
394
  TSDB_CHECK_CODE(code, lino, END);
135,175✔
395
  while (1) {
12,935,416✔
396
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
13,070,591✔
397
    if (ret < 0) break;
13,066,893✔
398
    STbDbKey* tmp = (STbDbKey*)pKey;
12,937,120✔
399
    if (tmp->version > ctx->snapVersion) break;
12,937,120✔
400

401
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
12,931,711✔
402
    if (idData) {
12,927,737✔
403
      continue;
110,850✔
404
    }
405

406
    // check if table exist for now, need optimize later
407
    if (tdbTbGet(ctx->pMeta->pUidIdx, &tmp->uid, sizeof(tb_uid_t), NULL, NULL) < 0) {
12,816,887✔
408
      continue;
104,700✔
409
    }
410

411
    SMetaEntry me = {0};
12,712,471✔
412
    tDecoderInit(&dc, pVal, vLen);
12,712,150✔
413
    code = metaDecodeEntry(&dc, &me);
12,714,143✔
414
    TSDB_CHECK_CODE(code, lino, END);
12,705,712✔
415
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
12,705,712✔
416
      if (!((me.uid == ctx->suid && me.type == TSDB_SUPER_TABLE) ||
283,100✔
417
          (me.ctbEntry.suid == ctx->suid && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)))) {
257,362✔
418
        tDecoderClear(&dc);
27,962✔
419
        continue;
27,962✔
420
      }
421
    }
422

423
    TSDB_CHECK_NULL(taosArrayPush(ctx->idList, &tmp->uid), code, lino, END, terrno);
25,364,047✔
424
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
12,684,573✔
425
    tDecoderClear(&dc);
12,669,539✔
426

427
    SIdInfo info = {0};
12,692,516✔
428
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
12,692,516✔
429
    TSDB_CHECK_CODE(code, lino, END);
12,692,199✔
430
  }
431
  taosHashClear(ctx->idVersion);
135,175✔
432

433
  code = MoveToSnapShotVersion(ctx);
135,175✔
434
  TSDB_CHECK_CODE(code, lino, END);
134,799✔
435

436
  while (1) {
12,912,834✔
437
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
13,047,633✔
438
    if (ret < 0) break;
13,062,520✔
439

440
    STbDbKey* tmp = (STbDbKey*)pKey;
12,927,345✔
441
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
12,927,345✔
442
    if (idData) {
12,911,402✔
443
      continue;
186,560✔
444
    }
445
    SIdInfo info = {.version = tmp->version, .index = 0};
12,724,842✔
446
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
12,725,174✔
447
    TSDB_CHECK_CODE(code, lino, END);
12,742,529✔
448

449
    SMetaEntry me = {0};
12,742,529✔
450
    tDecoderInit(&dc, pVal, vLen);
12,728,591✔
451
    code = metaDecodeEntry(&dc, &me);
12,745,183✔
452
    TSDB_CHECK_CODE(code, lino, END);
12,727,224✔
453

454
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
12,727,224✔
455
      if (!((me.uid == ctx->suid && me.type == TSDB_SUPER_TABLE) ||
278,030✔
456
          (me.ctbEntry.suid == ctx->suid && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)))) {
252,292✔
457
        tDecoderClear(&dc);
22,892✔
458
        continue;
22,892✔
459
      }
460
    }
461

462
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
12,671,962✔
463
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
12,538,992✔
464
      code = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
189,750✔
465
      TSDB_CHECK_CODE(code, lino, END);
198,057✔
466
    }
467
    tDecoderClear(&dc);
12,714,630✔
468

469
  }
470

471
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
12,829,036✔
472
    int64_t* uid = taosArrayGet(ctx->idList, i);
12,693,861✔
473
    TSDB_CHECK_NULL(uid, code, lino, END, terrno);
12,693,861✔
474
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
12,693,861✔
475
    TSDB_CHECK_NULL(idData, code, lino, END, terrno);
12,693,529✔
476

477
    idData->index = i;
12,693,529✔
478
    metaDebug("tmqsnap init idVersion uid:%" PRIi64 " version:%" PRIi64 " index:%d", *uid, idData->version, idData->index);
12,693,529✔
479
  }
480

481
END:
128,866✔
482
  tdbFree(pKey);
135,175✔
483
  tdbFree(pVal);
135,175✔
484
  tDecoderClear(&dc);
135,175✔
485

486
  if (ctx != NULL) {
135,175✔
487
    tdbTbcClose((TBC*)ctx->pCur);
135,175✔
488
    ctx->pCur = NULL;
135,175✔
489
  }
490
  metaULock(pVnode->pMeta);
135,175✔
491

492
  if(code != 0) {
135,175✔
493
    destroySnapContext(ctx);
×
494
    *ctxRet = NULL;
×
495
    metaError("tmqsnap build snap context failed line:%d since %s", lino, tstrerror(code));
×
496
  }
497
  return code;
135,175✔
498
}
499

500
void destroySnapContext(SSnapContext* ctx) {
625,321✔
501
  if (ctx == NULL) {
625,321✔
502
    return;
491,548✔
503
  }
504
  taosArrayDestroy(ctx->idList);
133,773✔
505
  taosHashCleanup(ctx->idVersion);
135,175✔
506
  taosHashCleanup(ctx->suidInfo);
135,175✔
507
  taosMemoryFree(ctx);
135,175✔
508
}
509

510
static int32_t buildNormalChildTableInfo(SVCreateTbReq* req, void** pBuf, int32_t* contLen) {
35,124✔
511
  int32_t            ret = 0;
35,124✔
512
  SVCreateTbBatchReq reqs = {0};
35,124✔
513

514
  reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
35,124✔
515
  if (NULL == reqs.pArray) {
35,124✔
516
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
517
    goto end;
×
518
  }
519
  if (taosArrayPush(reqs.pArray, req) == NULL) {
70,248✔
520
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
521
    goto end;
×
522
  }
523
  reqs.nReqs = 1;
35,124✔
524

525
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqs, *contLen, ret);
35,124✔
526
  if (ret < 0) {
35,124✔
527
    ret = TAOS_GET_TERRNO(ret);
×
528
    goto end;
×
529
  }
530
  *contLen += sizeof(SMsgHead);
35,124✔
531
  *pBuf = taosMemoryMalloc(*contLen);
35,124✔
532
  if (NULL == *pBuf) {
35,124✔
533
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
534
    goto end;
×
535
  }
536
  SEncoder coder = {0};
35,124✔
537
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
35,124✔
538
  ret = tEncodeSVCreateTbBatchReq(&coder, &reqs);
35,124✔
539
  tEncoderClear(&coder);
35,124✔
540

541
  if (ret < 0) {
35,124✔
542
    taosMemoryFreeClear(*pBuf);
×
543
    ret = TAOS_GET_TERRNO(ret);
×
544
    goto end;
×
545
  }
546

547
end:
35,124✔
548
  taosArrayDestroy(reqs.pArray);
35,124✔
549
  return ret;
35,124✔
550
}
551

552
static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* contLen) {
14,318✔
553
  int32_t ret = 0;
14,318✔
554
  tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret);
14,318✔
555
  if (ret < 0) {
14,318✔
556
    return TAOS_GET_TERRNO(ret);
×
557
  }
558

559
  *contLen += sizeof(SMsgHead);
14,318✔
560
  *pBuf = taosMemoryMalloc(*contLen);
14,318✔
561
  if (NULL == *pBuf) {
14,318✔
562
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
563
  }
564

565
  SEncoder encoder = {0};
14,318✔
566
  tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
14,318✔
567
  ret = tEncodeSVCreateStbReq(&encoder, req);
14,318✔
568
  tEncoderClear(&encoder);
14,318✔
569
  if (ret < 0) {
14,318✔
570
    taosMemoryFreeClear(*pBuf);
×
571
    return TAOS_GET_TERRNO(ret);
×
572
  }
573
  return 0;
14,318✔
574
}
575

576
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
838,966✔
577
  if (uid == 0) {
838,966✔
578
    ctx->index = 0;
20,299✔
579
    return 0;
20,299✔
580
  }
581

582
  SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, &uid, sizeof(tb_uid_t));
818,667✔
583
  if (idInfo == NULL) {
818,667✔
584
    return terrno;
×
585
  }
586

587
  ctx->index = idInfo->index;
818,667✔
588

589
  return 0;
818,667✔
590
}
591

592
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
830,739✔
593
  bool            ret = false;
830,739✔
594
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL, 0, false);
830,739✔
595
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
829,755✔
596
    ret = true;
1,432✔
597
  }
598
  tDeleteSchemaWrapper(schema);
599
  ctx->hasPrimaryKey = ret;
829,427✔
600
}
830,739✔
601

602
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
1,642,882✔
603

604
int32_t getTableInfoFromSnapshot(SSnapContext* ctx, void** pBuf, int32_t* contLen, int16_t* type, int64_t* uid) {
56,367✔
605
  int32_t ret = 0;
56,367✔
606
  int32_t lino = 0;
56,367✔
607
  void*   pKey = NULL;
56,367✔
608
  void*   pVal = NULL;
56,367✔
609
  int     vLen = 0, kLen = 0;
56,367✔
610
  SDecoder   dc = {0};
56,367✔
611
  SArray* tagName = NULL;
56,367✔
612
  SArray* pTagVals = NULL;
56,367✔
613

614
  metaRLock(ctx->pMeta);
56,367✔
615
  while (1) {
×
616
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
56,367✔
617
      metaDebug("tmqsnap get meta end");
6,925✔
618
      ctx->index = 0;
6,925✔
619
      ctx->queryMeta = 0;  // change to get data
6,925✔
620
      goto END;
6,925✔
621
    }
622

623
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
49,442✔
624
    TSDB_CHECK_NULL(uidTmp, ret, lino, END, terrno);
49,442✔
625
    ctx->index++;
49,442✔
626
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
49,442✔
627
    TSDB_CHECK_NULL(idInfo, ret, lino, END, terrno);
49,442✔
628

629
    *uid = *uidTmp;
49,442✔
630
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
49,442✔
631
    if (ret == 0) {
49,442✔
632
      break;
49,442✔
633
    }
634
    metaDebug("tmqsnap get meta not exist uid:%" PRIi64 " version:%" PRIi64, *uid, idInfo->version);
×
635
  }
636

637
  ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
49,442✔
638
  TSDB_CHECK_CONDITION(ret >= 0, ret, lino, END, TAOS_GET_TERRNO(ret));
49,442✔
639
  SMetaEntry me = {0};
49,442✔
640
  tDecoderInit(&dc, pVal, vLen);
49,442✔
641
  ret = metaDecodeEntry(&dc, &me);
49,442✔
642
  TSDB_CHECK_CONDITION(ret >= 0, ret, lino, END, TAOS_GET_TERRNO(ret));
49,442✔
643
  metaDebug("tmqsnap get meta uid:%" PRIi64 " name:%s index:%d", *uid, me.name, ctx->index - 1);
49,442✔
644

645
  if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
49,442✔
646
      (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
50,412✔
647
    SVCreateStbReq req = {0};
14,318✔
648
    req.name = me.name;
14,318✔
649
    req.suid = me.uid;
14,318✔
650
    req.schemaRow = me.stbEntry.schemaRow;
14,318✔
651
    req.schemaTag = me.stbEntry.schemaTag;
14,318✔
652
    req.schemaRow.version = 1;
14,318✔
653
    req.schemaTag.version = 1;
14,318✔
654
    req.colCmpr = me.colCmpr;
14,318✔
655
    req.pExtSchemas = me.pExtSchemas;
14,318✔
656
    req.virtualStb = TABLE_IS_VIRTUAL(me.flags);
14,318✔
657

658
    ret = buildSuperTableInfo(&req, pBuf, contLen);
14,318✔
659
    *type = TDMT_VND_CREATE_STB;
14,318✔
660
  } else if ((ctx->subType == TOPIC_SUB_TYPE__DB && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)) ||
35,124✔
661
             (ctx->subType == TOPIC_SUB_TYPE__TABLE && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) && me.ctbEntry.suid == ctx->suid)) {
37,064✔
662
    STableInfoForChildTable* data =
663
        (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
29,152✔
664
    TSDB_CHECK_NULL(data, ret, lino, END, terrno);
29,152✔
665

666
    SVCreateTbReq req = {0};
29,152✔
667

668
    req.type = me.type;
29,152✔
669
    req.name = me.name;
29,152✔
670
    req.uid = me.uid;
29,152✔
671
    req.commentLen = -1;
29,152✔
672
    req.ctb.suid = me.ctbEntry.suid;
29,152✔
673
    req.ctb.tagNum = data->tagRow->nCols;
29,152✔
674
    req.ctb.stbName = data->tableName;
29,152✔
675

676
    tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
29,152✔
677
    TSDB_CHECK_NULL(tagName, ret, lino, END, terrno);
29,152✔
678
    STag* p = (STag*)me.ctbEntry.pTags;
29,152✔
679
    if (tTagIsJson(p)) {
29,152✔
680
      if (p->nTag != 0) {
3,540✔
681
        SSchema* schema = &data->tagRow->pSchema[0];
1,770✔
682
        TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
3,540✔
683
      }
684
    } else {
685
      ret = tTagToValArray((const STag*)p, &pTagVals);
25,612✔
686
      TSDB_CHECK_CODE(ret, lino, END);
25,612✔
687
      int16_t nCols = taosArrayGetSize(pTagVals);
25,612✔
688
      for (int j = 0; j < nCols; ++j) {
81,904✔
689
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
56,292✔
690
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
219,964✔
691
          SSchema* schema = &data->tagRow->pSchema[i];
163,672✔
692
          if (schema->colId == pTagVal->cid) {
163,672✔
693
            TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
112,584✔
694
          }
695
        }
696
      }
697
    }
698
    req.ctb.pTag = me.ctbEntry.pTags;
29,152✔
699
    req.ctb.tagName = tagName;
29,152✔
700
    req.colRef = me.colRef;
29,152✔
701
    if (me.type == TSDB_VIRTUAL_CHILD_TABLE) {
29,152✔
702
      SMetaEntry *pSuper = NULL;
2,575✔
703
      int32_t code = metaFetchEntryByUid(ctx->pMeta, me.ctbEntry.suid, &pSuper);
2,575✔
704
      if (code == 0) {
2,575✔
705
        for (int i = 0; i < req.colRef.nCols && i < pSuper->stbEntry.schemaRow.nCols; i++) {
12,285✔
706
          SColRef *p = &req.colRef.pColRef[i];
9,710✔
707
          if (p->hasRef) {
9,710✔
708
            SSchema *schema = &pSuper->stbEntry.schemaRow.pSchema[i];
4,855✔
709
            tstrncpy(p->colName, schema->name, TSDB_COL_NAME_LEN);
4,855✔
710
          }
711
        }
712
      }
713
      metaFetchEntryFree(&pSuper);
2,575✔
714
    }
715
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
29,152✔
716
    *type = TDMT_VND_CREATE_TABLE;
29,152✔
717
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB && (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE)) {
11,944✔
718
    SVCreateTbReq req = {0};
5,972✔
719
    req.type = me.type;
5,972✔
720
    req.name = me.name;
5,972✔
721
    req.uid = me.uid;
5,972✔
722
    req.commentLen = -1;
5,972✔
723
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
5,972✔
724
    req.colCmpr = me.colCmpr;
5,972✔
725
    req.pExtSchemas = me.pExtSchemas;
5,972✔
726
    req.colRef = me.colRef;
5,972✔
727
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
5,972✔
728
    *type = TDMT_VND_CREATE_TABLE;
5,972✔
729
  } else {
730
    metaError("meta/snap: invalid topic sub type: %" PRId8 " get meta from snap failed.", ctx->subType);
×
731
    ret = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
×
732
  }
733

734
END:
56,367✔
735
  tdbTbcClose((TBC*)ctx->pCur);
56,367✔
736
  ctx->pCur = NULL;
56,367✔
737
  taosArrayDestroy(pTagVals);
56,367✔
738
  taosArrayDestroy(tagName);
56,367✔
739
  tDecoderClear(&dc);
56,367✔
740
  metaULock(ctx->pMeta);
56,367✔
741

742
  if(ret != 0) {
56,367✔
743
    metaError("tmqsnap get table info from snapshot failed line:%d since %s", lino, tstrerror(ret));
×
744
  }
745
  return ret;
56,367✔
746
}
747

748
int32_t getMetaTableInfoFromSnapshot(SSnapContext* ctx, SMetaTableInfo* result) {
1,662,355✔
749
  void* pKey = NULL;
1,662,355✔
750
  void* pVal = NULL;
1,662,355✔
751
  int   vLen = 0;
1,662,355✔
752
  int   kLen = 0;
1,662,355✔
753
  int32_t code = 0;
1,662,355✔
754
  int32_t lino = 0;
1,662,355✔
755
  SDecoder   dc = {0};
1,662,355✔
756

757
  metaRLock(ctx->pMeta);
1,662,355✔
758
  while (1) {
27,048✔
759
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
1,689,403✔
760
      metaDebug("tmqsnap get uid info end");
13,268✔
761
      goto END;
13,268✔
762
    }
763
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
1,676,135✔
764
    TSDB_CHECK_NULL(uidTmp, code, lino, END, terrno);
1,676,135✔
765
    ctx->index++;
1,676,135✔
766
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
1,676,135✔
767
    TSDB_CHECK_NULL(idInfo, code, lino, END, terrno);
1,676,135✔
768

769
    if (MoveToPosition(ctx, idInfo->version, *uidTmp) != 0) {
1,676,135✔
770
      metaDebug("tmqsnap getMetaTableInfoFromSnapshot not exist uid:%" PRIi64 " version:%" PRIi64, *uidTmp,
×
771
                idInfo->version);
772
      continue;
×
773
    }
774
    code = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
1,676,135✔
775
    TSDB_CHECK_CODE(code, lino, END);
1,676,135✔
776
    SMetaEntry me = {0};
1,676,135✔
777
    tDecoderInit(&dc, pVal, vLen);
1,676,135✔
778
    code = metaDecodeEntry(&dc, &me);
1,676,135✔
779
    TSDB_CHECK_CODE(code, lino, END);
1,675,807✔
780
    metaDebug("tmqsnap get uid info uid:%" PRIi64 " name:%s index:%d", me.uid, me.name, ctx->index - 1);
1,675,807✔
781

782
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
1,675,807✔
783
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
1,677,281✔
784
      STableInfoForChildTable* data =
785
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
332,672✔
786
      TSDB_CHECK_NULL(data, code, lino, END, terrno);
333,000✔
787
      result->suid = me.ctbEntry.suid;
333,000✔
788
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
666,000✔
789
      if (data->pExtSchemas != NULL) {
333,000✔
790
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * data->schemaRow->nCols);
9,183✔
791
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
9,183✔
792
        memcpy(result->pExtSchemas, data->pExtSchemas, sizeof(SExtSchema) * data->schemaRow->nCols);
9,183✔
793
      }
794
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
1,342,479✔
795
      result->suid = 0;
1,316,087✔
796
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
1,315,759✔
797
      if (me.pExtSchemas != NULL) {
1,314,775✔
798
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
799
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
×
800
        memcpy(result->pExtSchemas, me.pExtSchemas, sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
801
      }
802
    } else {
803
      metaDebug("tmqsnap get uid continue");
26,392✔
804
      tDecoderClear(&dc);
26,392✔
805
      continue;
27,048✔
806
    }
807
    result->uid = me.uid;
1,647,775✔
808
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
1,647,775✔
809
    TSDB_CHECK_NULL(result->schema, code, lino, END, TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY));
1,648,103✔
810
    break;
1,647,119✔
811
  }
812

813
END:
1,661,043✔
814
  tDecoderClear(&dc);
1,662,027✔
815
  tdbTbcClose((TBC*)ctx->pCur);
1,661,699✔
816
  ctx->pCur = NULL;
1,660,715✔
817
  metaULock(ctx->pMeta);
1,661,699✔
818

819
  if (code != 0) {
1,660,715✔
820
    metaError("tmqsnap get meta table info from snapshot failed line:%d since %s", lino, tstrerror(code));
×
821
  }
822
  return code;
1,660,715✔
823
}
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