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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 hits per line

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

84.97
/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 "meta.h"
17

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

27
int32_t metaSnapReaderOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapReader** ppReader) {
19,744✔
28
  int32_t          code = 0;
19,744✔
29
  int32_t          lino;
30
  int32_t          c = 0;
19,744✔
31
  SMetaSnapReader* pReader = NULL;
19,744✔
32

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

42
  // impl
43
  code = tdbTbcOpen(pMeta->pTbDb, &pReader->pTbc, NULL);
19,744✔
44
  TSDB_CHECK_CODE(code, lino, _exit);
19,744✔
45

46
  code = tdbTbcMoveTo(pReader->pTbc, &(STbDbKey){.version = sver, .uid = INT64_MIN}, sizeof(STbDbKey), &c);
19,744✔
47
  TSDB_CHECK_CODE(code, lino, _exit);
19,744✔
48

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

61
void metaSnapReaderClose(SMetaSnapReader** ppReader) {
19,744✔
62
  if (ppReader && *ppReader) {
19,744✔
63
    tdbTbcClose((*ppReader)->pTbc);
19,744✔
64
    taosMemoryFree(*ppReader);
19,744✔
65
    *ppReader = NULL;
19,744✔
66
  }
67
}
19,744✔
68

69
extern int metaDecodeEntryImpl(SDecoder* pCoder, SMetaEntry* pME, bool headerOnly);
70

71
static int32_t metaDecodeEntryHeader(void* data, int32_t size, SMetaEntry* entry) {
106,298✔
72
  SDecoder decoder = {0};
106,298✔
73
  tDecoderInit(&decoder, (uint8_t*)data, size);
106,298✔
74

75
  int32_t code = metaDecodeEntryImpl(&decoder, entry, true);
106,298✔
76
  if (code) {
106,298✔
77
    tDecoderClear(&decoder);
×
78
    return code;
×
79
  }
80

81
  tDecoderClear(&decoder);
106,298✔
82
  return 0;
106,298✔
83
}
84

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

94
  *ppData = NULL;
72,263✔
95
  while (pReader->iLoop < 2) {
165,530✔
96
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
145,786✔
97
      pReader->iLoop++;
39,488✔
98

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

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

116
      continue;
39,488✔
117
    }
118

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

128
    key = ((STbDbKey*)pKey)[0];
106,298✔
129
    if (key.version < pReader->sver                                       //
106,298✔
130
        || (pReader->iLoop == 0 && TABS(entry.type) != TSDB_SUPER_TABLE)  // First loop send super table entry
105,038✔
131
        || (pReader->iLoop == 1 && TABS(entry.type) == TSDB_SUPER_TABLE)  // Second loop send non-super table entry
71,146✔
132
    ) {
133
      if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
53,779✔
134
        metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
135
      }
136
      continue;
53,779✔
137
    }
138

139
    if (!pData || !nData) {
52,519✔
140
      metaError("meta/snap: invalide nData: %" PRId32 " meta snap read failed.", nData);
×
141
      goto _exit;
×
142
    }
143

144
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + nData);
52,519✔
145
    if (*ppData == NULL) {
52,519✔
146
      code = terrno;
×
147
      goto _exit;
×
148
    }
149

150
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
52,519✔
151
    pHdr->type = SNAP_DATA_META;
52,519✔
152
    pHdr->size = nData;
52,519✔
153
    memcpy(pHdr->data, pData, nData);
52,519✔
154

155
    metaDebug("vgId:%d, vnode snapshot meta read data, version:%" PRId64 " uid:%" PRId64 " blockLen:%d",
52,519✔
156
              TD_VID(pReader->pMeta->pVnode), key.version, key.uid, nData);
157

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

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

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

179
int32_t metaSnapWriterOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapWriter** ppWriter) {
16,369✔
180
  int32_t          code = 0;
16,369✔
181
  int32_t          lino;
182
  SMetaSnapWriter* pWriter;
183

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

193
  code = metaBegin(pMeta, META_BEGIN_HEAP_NIL);
16,369✔
194
  TSDB_CHECK_CODE(code, lino, _exit);
16,369✔
195

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

208
int32_t metaSnapWriterClose(SMetaSnapWriter** ppWriter, int8_t rollback) {
16,369✔
209
  int32_t          code = 0;
16,369✔
210
  SMetaSnapWriter* pWriter = *ppWriter;
16,369✔
211

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

227
  return code;
16,369✔
228

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

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

241
  tDecoderInit(pDecoder, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
52,483✔
242
  code = metaDecodeEntry(pDecoder, &metaEntry);
52,483✔
243
  TSDB_CHECK_CODE(code, lino, _exit);
52,483✔
244

245
  metaHandleSyncEntry(pMeta, &metaEntry);
52,483✔
246

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

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

262
static void destroySTableInfoForChildTable(void* data) {
26,982✔
263
  STableInfoForChildTable* pData = (STableInfoForChildTable*)data;
26,982✔
264
  taosMemoryFree(pData->tableName);
26,982✔
265
  tDeleteSchemaWrapper(pData->schemaRow);
26,982✔
266
  tDeleteSchemaWrapper(pData->tagRow);
26,982✔
267
  taosMemoryFreeClear(pData->pExtSchemas);
26,959✔
268
}
26,959✔
269

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

291
static int32_t MoveToPosition(SSnapContext* ctx, int64_t ver, int64_t uid) {
244,777✔
292
  tdbTbcClose((TBC*)ctx->pCur);
244,777✔
293
  int32_t code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
244,718✔
294
  if (code != 0) {
244,605✔
295
    return TAOS_GET_TERRNO(code);
×
296
  }
297
  STbDbKey key = {.version = ver, .uid = uid};
244,605✔
298
  int      c = 0;
244,648✔
299
  code = tdbTbcMoveTo((TBC*)ctx->pCur, &key, sizeof(key), &c);
244,648✔
300
  if (code != 0) {
244,562✔
301
    return TAOS_GET_TERRNO(code);
×
302
  }
303
  return c;
244,562✔
304
}
305

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

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

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

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

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

386
  ctx->index = 0;
20,747✔
387
  ctx->idList = taosArrayInit(100, sizeof(int64_t));
20,747✔
388
  TSDB_CHECK_NULL(ctx->idList, code, lino, END, terrno);
20,747✔
389

390
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
20,747✔
391
  code = MoveToFirst(ctx);
20,747✔
392
  TSDB_CHECK_CODE(code, lino, END);
20,747✔
393
  while (1) {
996,859✔
394
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
1,017,606✔
395
    if (ret < 0) break;
1,017,209✔
396
    STbDbKey* tmp = (STbDbKey*)pKey;
996,742✔
397
    if (tmp->version > ctx->snapVersion) break;
996,742✔
398

399
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
996,515✔
400
    if (idData) {
995,604✔
401
      continue;
13,954✔
402
    }
403

404
    // check if table exist for now, need optimize later
405
    if (tdbTbGet(ctx->pMeta->pUidIdx, &tmp->uid, sizeof(tb_uid_t), NULL, NULL) < 0) {
981,650✔
406
      continue;
11,075✔
407
    }
408

409
    SMetaEntry me = {0};
970,136✔
410
    tDecoderInit(&dc, pVal, vLen);
969,122✔
411
    code = metaDecodeEntry(&dc, &me);
971,460✔
412
    TSDB_CHECK_CODE(code, lino, END);
966,243✔
413
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
966,243✔
414
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
37,164✔
415
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
37,141✔
416
        tDecoderClear(&dc);
92✔
417
        continue;
92✔
418
      }
419
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
933,182✔
420
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
933,230✔
421
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
933,342✔
422
          TABLE_IS_VIRTUAL(me.flags)) {
933,902✔
UNCOV
423
        tDecoderClear(&dc);
×
424
        continue;
46✔
425
      }
426
    }
427

428
    TSDB_CHECK_NULL(taosArrayPush(ctx->idList, &tmp->uid), code, lino, END, terrno);
1,939,660✔
429
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
970,034✔
430
    tDecoderClear(&dc);
981,487✔
431

432
    SIdInfo info = {0};
971,020✔
433
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
971,411✔
434
    TSDB_CHECK_CODE(code, lino, END);
971,679✔
435
  }
436
  taosHashClear(ctx->idVersion);
20,747✔
437

438
  code = MoveToSnapShotVersion(ctx);
20,747✔
439
  TSDB_CHECK_CODE(code, lino, END);
20,657✔
440

441
  while (1) {
982,978✔
442
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
1,003,635✔
443
    if (ret < 0) break;
1,007,999✔
444

445
    STbDbKey* tmp = (STbDbKey*)pKey;
987,252✔
446
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
987,252✔
447
    if (idData) {
980,239✔
448
      continue;
20,745✔
449
    }
450
    SIdInfo info = {.version = tmp->version, .index = 0};
959,494✔
451
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
958,907✔
452
    TSDB_CHECK_CODE(code, lino, END);
964,969✔
453

454
    SMetaEntry me = {0};
964,969✔
455
    tDecoderInit(&dc, pVal, vLen);
959,359✔
456
    code = metaDecodeEntry(&dc, &me);
936,933✔
457
    TSDB_CHECK_CODE(code, lino, END);
944,222✔
458

459
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
944,222✔
460
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
37,184✔
461
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
37,161✔
462
        tDecoderClear(&dc);
92✔
463
        continue;
92✔
464
      }
465
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
919,747✔
466
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
926,947✔
467
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
927,653✔
468
          TABLE_IS_VIRTUAL(me.flags)) {
928,620✔
UNCOV
469
        tDecoderClear(&dc);
×
470
        continue;
46✔
471
      }
472
    }
473

474
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
959,689✔
475
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
954,303✔
476
      code = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
17,323✔
477
      TSDB_CHECK_CODE(code, lino, END);
26,982✔
478
    }
479
    tDecoderClear(&dc);
966,782✔
480

481
  }
482

483
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
993,263✔
484
    int64_t* uid = taosArrayGet(ctx->idList, i);
972,576✔
485
    TSDB_CHECK_NULL(uid, code, lino, END, terrno);
972,486✔
486
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
972,486✔
487
    TSDB_CHECK_NULL(idData, code, lino, END, terrno);
972,456✔
488

489
    idData->index = i;
972,456✔
490
    metaDebug("tmqsnap init idVersion uid:%" PRIi64 " version:%" PRIi64 " index:%d", *uid, idData->version, idData->index);
972,456✔
491
  }
492

493
END:
15,039✔
494
  tdbFree(pKey);
20,747✔
495
  tdbFree(pVal);
20,747✔
496
  tDecoderClear(&dc);
20,747✔
497

498
  if (ctx != NULL) {
20,747✔
499
    tdbTbcClose((TBC*)ctx->pCur);
20,747✔
500
    ctx->pCur = NULL;
20,747✔
501
  }
502
  metaULock(pVnode->pMeta);
20,747✔
503

504
  if(code != 0) {
20,747✔
NEW
505
    destroySnapContext(ctx);
×
NEW
506
    *ctxRet = NULL;
×
NEW
507
    metaError("tmqsnap build snap context failed line:%d since %s", lino, tstrerror(code));
×
508
  }
509
  return code;
20,747✔
510
}
511

512
void destroySnapContext(SSnapContext* ctx) {
150,472✔
513
  if (ctx == NULL) {
150,472✔
514
    return;
130,097✔
515
  }
516
  taosArrayDestroy(ctx->idList);
20,375✔
517
  taosHashCleanup(ctx->idVersion);
20,747✔
518
  taosHashCleanup(ctx->suidInfo);
20,747✔
519
  taosMemoryFree(ctx);
20,747✔
520
}
521

522
static int32_t buildNormalChildTableInfo(SVCreateTbReq* req, void** pBuf, int32_t* contLen) {
3,513✔
523
  int32_t            ret = 0;
3,513✔
524
  SVCreateTbBatchReq reqs = {0};
3,513✔
525

526
  reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
3,513✔
527
  if (NULL == reqs.pArray) {
3,513✔
UNCOV
528
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
UNCOV
529
    goto end;
×
530
  }
531
  if (taosArrayPush(reqs.pArray, req) == NULL) {
7,026✔
UNCOV
532
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
UNCOV
533
    goto end;
×
534
  }
535
  reqs.nReqs = 1;
3,513✔
536

537
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqs, *contLen, ret);
3,513✔
538
  if (ret < 0) {
3,513✔
539
    ret = TAOS_GET_TERRNO(ret);
×
540
    goto end;
×
541
  }
542
  *contLen += sizeof(SMsgHead);
3,513✔
543
  *pBuf = taosMemoryMalloc(*contLen);
3,513✔
544
  if (NULL == *pBuf) {
3,513✔
UNCOV
545
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
UNCOV
546
    goto end;
×
547
  }
548
  SEncoder coder = {0};
3,513✔
549
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
3,513✔
550
  ret = tEncodeSVCreateTbBatchReq(&coder, &reqs);
3,513✔
551
  tEncoderClear(&coder);
3,513✔
552

553
  if (ret < 0) {
3,513✔
UNCOV
554
    taosMemoryFreeClear(*pBuf);
×
UNCOV
555
    ret = TAOS_GET_TERRNO(ret);
×
UNCOV
556
    goto end;
×
557
  }
558

559
end:
3,513✔
560
  taosArrayDestroy(reqs.pArray);
3,513✔
561
  return ret;
3,513✔
562
}
563

564
static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* contLen) {
1,538✔
565
  int32_t ret = 0;
1,538✔
566
  tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret);
1,538✔
567
  if (ret < 0) {
1,538✔
568
    return TAOS_GET_TERRNO(ret);
×
569
  }
570

571
  *contLen += sizeof(SMsgHead);
1,538✔
572
  *pBuf = taosMemoryMalloc(*contLen);
1,538✔
573
  if (NULL == *pBuf) {
1,538✔
UNCOV
574
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
575
  }
576

577
  SEncoder encoder = {0};
1,538✔
578
  tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
1,538✔
579
  ret = tEncodeSVCreateStbReq(&encoder, req);
1,538✔
580
  tEncoderClear(&encoder);
1,538✔
581
  if (ret < 0) {
1,538✔
UNCOV
582
    taosMemoryFreeClear(*pBuf);
×
UNCOV
583
    return TAOS_GET_TERRNO(ret);
×
584
  }
585
  return 0;
1,538✔
586
}
587

588
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
120,409✔
589
  if (uid == 0) {
120,409✔
590
    ctx->index = 0;
2,960✔
591
    return 0;
2,960✔
592
  }
593

594
  SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, &uid, sizeof(tb_uid_t));
117,449✔
595
  if (idInfo == NULL) {
117,449✔
UNCOV
596
    return terrno;
×
597
  }
598

599
  ctx->index = idInfo->index;
117,449✔
600

601
  return 0;
117,449✔
602
}
603

604
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
119,258✔
605
  bool            ret = false;
119,258✔
606
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL, 0);
119,258✔
607
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
119,000✔
UNCOV
608
    ret = true;
×
609
  }
610
  tDeleteSchemaWrapper(schema);
611
  ctx->hasPrimaryKey = ret;
118,898✔
612
}
119,258✔
613

614
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
231,408✔
615

616
int32_t getTableInfoFromSnapshot(SSnapContext* ctx, void** pBuf, int32_t* contLen, int16_t* type, int64_t* uid) {
6,037✔
617
  int32_t ret = 0;
6,037✔
618
  int32_t lino = 0;
6,037✔
619
  void*   pKey = NULL;
6,037✔
620
  void*   pVal = NULL;
6,037✔
621
  int     vLen = 0, kLen = 0;
6,037✔
622
  SDecoder   dc = {0};
6,037✔
623
  SArray* tagName = NULL;
6,037✔
624
  SArray* pTagVals = NULL;
6,037✔
625

626
  metaRLock(ctx->pMeta);
6,037✔
UNCOV
627
  while (1) {
×
628
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
6,037✔
629
      metaDebug("tmqsnap get meta end");
986✔
630
      ctx->index = 0;
986✔
631
      ctx->queryMeta = 0;  // change to get data
986✔
632
      goto END;
986✔
633
    }
634

635
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
5,051✔
636
    TSDB_CHECK_NULL(uidTmp, ret, lino, END, terrno);
4,992✔
637
    ctx->index++;
4,992✔
638
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
5,051✔
639
    TSDB_CHECK_NULL(idInfo, ret, lino, END, terrno);
5,051✔
640

641
    *uid = *uidTmp;
5,051✔
642
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
5,051✔
643
    if (ret == 0) {
5,051✔
644
      break;
5,051✔
645
    }
UNCOV
646
    metaDebug("tmqsnap get meta not exist uid:%" PRIi64 " version:%" PRIi64, *uid, idInfo->version);
×
647
  }
648

649
  ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
5,051✔
650
  TSDB_CHECK_CONDITION(ret >= 0, ret, lino, END, TAOS_GET_TERRNO(ret));
5,051✔
651
  SMetaEntry me = {0};
5,051✔
652
  tDecoderInit(&dc, pVal, vLen);
5,051✔
653
  ret = metaDecodeEntry(&dc, &me);
5,051✔
654
  TSDB_CHECK_CONDITION(ret >= 0, ret, lino, END, TAOS_GET_TERRNO(ret));
5,051✔
655
  metaDebug("tmqsnap get meta uid:%" PRIi64 " name:%s index:%d", *uid, me.name, ctx->index - 1);
5,051✔
656

657
  if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
5,051✔
658
      (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
5,051✔
659
    SVCreateStbReq req = {0};
1,538✔
660
    req.name = me.name;
1,538✔
661
    req.suid = me.uid;
1,538✔
662
    req.schemaRow = me.stbEntry.schemaRow;
1,538✔
663
    req.schemaTag = me.stbEntry.schemaTag;
1,538✔
664
    req.schemaRow.version = 1;
1,538✔
665
    req.schemaTag.version = 1;
1,538✔
666
    req.colCmpr = me.colCmpr;
1,538✔
667
    req.pExtSchemas = me.pExtSchemas;
1,538✔
668

669
    ret = buildSuperTableInfo(&req, pBuf, contLen);
1,538✔
670
    *type = TDMT_VND_CREATE_STB;
1,538✔
671
  } else if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
3,513✔
672
             (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
3,513✔
673
    STableInfoForChildTable* data =
674
        (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
3,283✔
675
    TSDB_CHECK_NULL(data, ret, lino, END, terrno);
3,283✔
676

677
    SVCreateTbReq req = {0};
3,283✔
678

679
    req.type = TSDB_CHILD_TABLE;
3,283✔
680
    req.name = me.name;
3,283✔
681
    req.uid = me.uid;
3,283✔
682
    req.commentLen = -1;
3,283✔
683
    req.ctb.suid = me.ctbEntry.suid;
3,283✔
684
    req.ctb.tagNum = data->tagRow->nCols;
3,283✔
685
    req.ctb.stbName = data->tableName;
3,283✔
686

687
    tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
3,283✔
688
    TSDB_CHECK_NULL(tagName, ret, lino, END, terrno);
3,283✔
689
    STag* p = (STag*)me.ctbEntry.pTags;
3,283✔
690
    if (tTagIsJson(p)) {
3,283✔
691
      if (p->nTag != 0) {
276✔
692
        SSchema* schema = &data->tagRow->pSchema[0];
138✔
693
        TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
276✔
694
      }
695
    } else {
696
      ret = tTagToValArray((const STag*)p, &pTagVals);
3,007✔
697
      TSDB_CHECK_CODE(ret, lino, END);
3,007✔
698
      int16_t nCols = taosArrayGetSize(pTagVals);
3,007✔
699
      for (int j = 0; j < nCols; ++j) {
8,314✔
700
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
5,307✔
701
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
18,618✔
702
          SSchema* schema = &data->tagRow->pSchema[i];
13,311✔
703
          if (schema->colId == pTagVal->cid) {
13,311✔
704
            TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
10,614✔
705
          }
706
        }
707
      }
708
    }
709
    req.ctb.pTag = me.ctbEntry.pTags;
3,283✔
710
    req.ctb.tagName = tagName;
3,283✔
711
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
3,283✔
712
    *type = TDMT_VND_CREATE_TABLE;
3,283✔
713
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
460✔
714
    SVCreateTbReq req = {0};
230✔
715
    req.type = TSDB_NORMAL_TABLE;
230✔
716
    req.name = me.name;
230✔
717
    req.uid = me.uid;
230✔
718
    req.commentLen = -1;
230✔
719
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
230✔
720
    req.colCmpr = me.colCmpr;
230✔
721
    req.pExtSchemas = me.pExtSchemas;
230✔
722
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
230✔
723
    *type = TDMT_VND_CREATE_TABLE;
230✔
724
  } else {
UNCOV
725
    metaError("meta/snap: invalid topic sub type: %" PRId8 " get meta from snap failed.", ctx->subType);
×
UNCOV
726
    ret = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
×
727
  }
728

729
END:
6,037✔
730
  tdbTbcClose((TBC*)ctx->pCur);
6,037✔
731
  ctx->pCur = NULL;
6,037✔
732
  taosArrayDestroy(pTagVals);
6,037✔
733
  taosArrayDestroy(tagName);
6,037✔
734
  tDecoderClear(&dc);
6,037✔
735
  metaULock(ctx->pMeta);
6,037✔
736

737
  if(ret != 0) {
6,037✔
NEW
738
    metaError("tmqsnap get table info from snapshot failed line:%d since %s", lino, tstrerror(ret));
×
739
  }
740
  return ret;
6,037✔
741
}
742

743
int32_t getMetaTableInfoFromSnapshot(SSnapContext* ctx, SMetaTableInfo* result) {
238,846✔
744
  void* pKey = NULL;
238,846✔
745
  void* pVal = NULL;
238,846✔
746
  int   vLen = 0;
238,846✔
747
  int   kLen = 0;
238,846✔
748
  int32_t code = 0;
238,846✔
749
  int32_t lino = 0;
238,846✔
750
  SDecoder   dc = {0};
238,846✔
751

752
  metaRLock(ctx->pMeta);
238,803✔
753
  while (1) {
3,062✔
754
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
241,908✔
755
      metaDebug("tmqsnap get uid info end");
2,139✔
756
      goto END;
2,139✔
757
    }
758
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
239,769✔
759
    TSDB_CHECK_NULL(uidTmp, code, lino, END, terrno);
239,769✔
760
    ctx->index++;
239,769✔
761
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
239,726✔
762
    TSDB_CHECK_NULL(idInfo, code, lino, END, terrno);
239,726✔
763

764
    if (MoveToPosition(ctx, idInfo->version, *uidTmp) != 0) {
239,726✔
UNCOV
765
      metaDebug("tmqsnap getMetaTableInfoFromSnapshot not exist uid:%" PRIi64 " version:%" PRIi64, *uidTmp,
×
766
                idInfo->version);
UNCOV
767
      continue;
×
768
    }
769
    code = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
239,554✔
770
    TSDB_CHECK_CODE(code, lino, END);
239,640✔
771
    SMetaEntry me = {0};
239,640✔
772
    tDecoderInit(&dc, pVal, vLen);
239,640✔
773
    code = metaDecodeEntry(&dc, &me);
239,640✔
774
    TSDB_CHECK_CODE(code, lino, END);
239,640✔
775
    metaDebug("tmqsnap get uid info uid:%" PRIi64 " name:%s index:%d", me.uid, me.name, ctx->index - 1);
239,640✔
776

777
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
239,640✔
778
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
239,683✔
779
      STableInfoForChildTable* data =
780
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
64,729✔
781
      TSDB_CHECK_NULL(data, code, lino, END, terrno);
64,729✔
782
      result->suid = me.ctbEntry.suid;
64,729✔
783
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
129,458✔
784
      if (data->pExtSchemas != NULL) {
64,729✔
785
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * data->schemaRow->nCols);
809✔
786
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
809✔
787
        memcpy(result->pExtSchemas, data->pExtSchemas, sizeof(SExtSchema) * data->schemaRow->nCols);
809✔
788
      }
789
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
174,954✔
790
      result->suid = 0;
171,892✔
791
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
171,849✔
792
      if (me.pExtSchemas != NULL) {
171,806✔
UNCOV
793
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
NEW
794
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
×
UNCOV
795
        memcpy(result->pExtSchemas, me.pExtSchemas, sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
796
      }
797
    } else {
798
      metaDebug("tmqsnap get uid continue");
3,062✔
799
      tDecoderClear(&dc);
3,062✔
800
      continue;
3,062✔
801
    }
802
    result->uid = me.uid;
236,535✔
803
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
236,390✔
804
    TSDB_CHECK_NULL(result->schema, code, lino, END, TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY));
236,320✔
805
    break;
236,320✔
806
  }
807

808
END:
238,717✔
809
  tDecoderClear(&dc);
238,760✔
810
  tdbTbcClose((TBC*)ctx->pCur);
238,760✔
811
  ctx->pCur = NULL;
238,502✔
812
  metaULock(ctx->pMeta);
238,803✔
813

814
  if (code != 0) {
238,459✔
NEW
815
    metaError("tmqsnap get meta table info from snapshot failed line:%d since %s", lino, tstrerror(code));
×
816
  }
817
  return code;
238,459✔
818
}
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