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

taosdata / TDengine / #4863

26 Nov 2025 05:46AM UTC coverage: 64.539% (+0.2%) from 64.294%
#4863

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

767 of 945 new or added lines in 33 files covered. (81.16%)

701 existing lines in 109 files now uncovered.

158204 of 245129 relevant lines covered (64.54%)

111903199.95 hits per line

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

76.57
/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,022✔
28
  int32_t          code = 0;
19,022✔
29
  int32_t          lino;
30
  int32_t          c = 0;
19,022✔
31
  SMetaSnapReader* pReader = NULL;
19,022✔
32

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

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

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

49
_exit:
19,022✔
50
  if (code) {
19,022✔
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,022✔
56
    *ppReader = pReader;
19,022✔
57
  }
58
  return code;
19,022✔
59
}
60

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

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

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

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

81
  tDecoderClear(&decoder);
105,668✔
82
  return 0;
105,668✔
83
}
84

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

94
  *ppData = NULL;
70,931✔
95
  while (pReader->iLoop < 2) {
162,734✔
96
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
143,712✔
97
      pReader->iLoop++;
38,044✔
98

99
      // Reopen the cursor to read from the beginning
100
      tdbTbcClose(pReader->pTbc);
38,044✔
101
      pReader->pTbc = NULL;
38,044✔
102
      code = tdbTbcOpen(pReader->pMeta->pTbDb, &pReader->pTbc, NULL);
38,044✔
103
      if (code) {
38,044✔
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);
38,044✔
110
      if (code) {
38,044✔
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;
38,044✔
117
    }
118

119
    // Decode meta entry
120
    SMetaEntry entry = {0};
105,668✔
121
    code = metaDecodeEntryHeader((void*)pData, nData, &entry);
105,668✔
122
    if (code) {
105,668✔
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];
105,668✔
129
    if (key.version < pReader->sver                                       //
105,668✔
130
        || (pReader->iLoop == 0 && TABS(entry.type) != TSDB_SUPER_TABLE)  // First loop send super table entry
103,818✔
131
        || (pReader->iLoop == 1 && TABS(entry.type) == TSDB_SUPER_TABLE)  // Second loop send non-super table entry
69,407✔
132
    ) {
133
      if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
53,759✔
134
        metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
135
      }
136
      continue;
53,759✔
137
    }
138

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

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

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

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

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

164
_exit:
70,931✔
165
  if (code) {
70,931✔
166
    metaError("vgId:%d, vnode snapshot meta read data failed since %s", TD_VID(pReader->pMeta->pVnode),
×
167
              tstrerror(code));
168
  }
169
  return code;
70,931✔
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) {
15,419✔
180
  int32_t          code = 0;
15,419✔
181
  int32_t          lino;
182
  SMetaSnapWriter* pWriter;
183

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

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

196
_exit:
15,419✔
197
  if (code) {
15,419✔
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__);
15,419✔
203
    *ppWriter = pWriter;
15,419✔
204
  }
205
  return code;
15,419✔
206
}
207

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

212
  if (rollback) {
15,419✔
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);
15,419✔
220
    if (code) goto _err;
15,419✔
221
    code = metaFinishCommit(pWriter->pMeta, pWriter->pMeta->txn);
15,419✔
222
    if (code) goto _err;
15,419✔
223
  }
224
  taosMemoryFree(pWriter);
15,419✔
225
  *ppWriter = NULL;
15,419✔
226

227
  return code;
15,419✔
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) {
51,857✔
235
  int32_t    code = 0;
51,857✔
236
  int32_t    lino = 0;
51,857✔
237
  SMeta*     pMeta = pWriter->pMeta;
51,857✔
238
  SMetaEntry metaEntry = {0};
51,857✔
239
  SDecoder*  pDecoder = &(SDecoder){0};
51,857✔
240

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

245
  metaHandleSyncEntry(pMeta, &metaEntry);
51,857✔
246

247
_exit:
51,857✔
248
  if (code) {
51,857✔
249
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
250
  }
251
  tDecoderClear(pDecoder);
51,857✔
252
  return code;
51,857✔
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) {
25,196✔
263
  STableInfoForChildTable* pData = (STableInfoForChildTable*)data;
25,196✔
264
  taosMemoryFree(pData->tableName);
25,196✔
265
  tDeleteSchemaWrapper(pData->schemaRow);
25,196✔
266
  tDeleteSchemaWrapper(pData->tagRow);
25,196✔
267
  taosMemoryFreeClear(pData->pExtSchemas);
25,196✔
268
}
25,196✔
269

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

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

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

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

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

362
int32_t buildSnapContext(SVnode* pVnode, int64_t snapVersion, int64_t suid, int8_t subType, int8_t withMeta,
19,517✔
363
                         SSnapContext** ctxRet) {
364
  SSnapContext* ctx = taosMemoryCalloc(1, sizeof(SSnapContext));
19,517✔
365
  if (ctx == NULL) {
19,593✔
366
    return terrno;
×
367
  }
368
  *ctxRet = ctx;
19,593✔
369
  ctx->pMeta = pVnode->pMeta;
19,593✔
370
  ctx->snapVersion = snapVersion;
19,593✔
371
  ctx->suid = suid;
19,593✔
372
  ctx->subType = subType;
19,593✔
373
  ctx->queryMeta = withMeta;
19,593✔
374
  ctx->withMeta = withMeta;
19,593✔
375
  ctx->idVersion = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
19,567✔
376
  if (ctx->idVersion == NULL) {
19,593✔
377
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
378
  }
379

380
  ctx->suidInfo = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
19,593✔
381
  if (ctx->suidInfo == NULL) {
19,593✔
382
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
383
  }
384
  taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable);
19,593✔
385

386
  ctx->index = 0;
19,593✔
387
  ctx->idList = taosArrayInit(100, sizeof(int64_t));
19,593✔
388
  if (ctx->idList == NULL) {
19,593✔
389
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
390
    ;
391
  }
392
  void* pKey = NULL;
19,593✔
393
  void* pVal = NULL;
19,593✔
394
  int   vLen = 0, kLen = 0;
19,593✔
395

396
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
19,593✔
397
  int32_t code = MoveToFirst(ctx);
19,593✔
398
  if (code != 0) {
19,575✔
399
    return code;
×
400
  }
401
  while (1) {
897,342✔
402
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
916,917✔
403
    if (ret < 0) break;
915,556✔
404
    STbDbKey* tmp = (STbDbKey*)pKey;
896,231✔
405
    if (tmp->version > ctx->snapVersion) break;
896,231✔
406

407
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
896,132✔
408
    if (idData) {
896,819✔
409
      continue;
12,444✔
410
    }
411

412
    if (tdbTbGet(ctx->pMeta->pUidIdx, &tmp->uid, sizeof(tb_uid_t), NULL, NULL) <
884,375✔
413
        0) {  // check if table exist for now, need optimize later
414
      continue;
9,950✔
415
    }
416

417
    SDecoder   dc = {0};
872,556✔
418
    SMetaEntry me = {0};
873,002✔
419
    tDecoderInit(&dc, pVal, vLen);
872,699✔
420
    ret = metaDecodeEntry(&dc, &me);
871,727✔
421
    if (ret < 0) {
868,337✔
422
      tDecoderClear(&dc);
×
423
      return TAOS_GET_TERRNO(ret);
×
424
    }
425
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
868,337✔
426
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
38,284✔
427
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
38,266✔
428
        tDecoderClear(&dc);
72✔
429
        continue;
72✔
430
      }
431
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
832,949✔
432
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
833,113✔
433
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
832,731✔
434
          TABLE_IS_VIRTUAL(me.flags)) {
833,695✔
435
        tDecoderClear(&dc);
24✔
436
        continue;
36✔
437
      }
438
    }
439

440
    if (taosArrayPush(ctx->idList, &tmp->uid) == NULL) {
1,742,626✔
441
      tDecoderClear(&dc);
×
442
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
443
    }
444
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
871,845✔
445
    tDecoderClear(&dc);
873,735✔
446

447
    SIdInfo info = {0};
871,227✔
448
    if (taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo)) != 0) {
872,545✔
449
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
450
    }
451
  }
452
  taosHashClear(ctx->idVersion);
19,593✔
453

454
  code = MoveToSnapShotVersion(ctx);
19,593✔
455
  if (code != 0) {
19,593✔
456
    return code;
×
457
  }
458
  while (1) {
884,698✔
459
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
904,291✔
460
    if (ret < 0) break;
910,087✔
461

462
    STbDbKey* tmp = (STbDbKey*)pKey;
890,494✔
463
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
890,494✔
464
    if (idData) {
891,504✔
465
      continue;
18,400✔
466
    }
467
    SIdInfo info = {.version = tmp->version, .index = 0};
873,104✔
468
    ret = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
873,208✔
469
    if (ret != 0) {
871,444✔
470
      return TAOS_GET_TERRNO(ret);
×
471
    }
472

473
    SDecoder   dc = {0};
871,444✔
474
    SMetaEntry me = {0};
871,080✔
475
    tDecoderInit(&dc, pVal, vLen);
873,560✔
476
    ret = metaDecodeEntry(&dc, &me);
869,934✔
477
    if (ret < 0) {
860,420✔
478
      tDecoderClear(&dc);
×
479
      return TAOS_GET_TERRNO(ret);
×
480
    }
481

482
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
860,420✔
483
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
38,284✔
484
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
38,266✔
485
        tDecoderClear(&dc);
72✔
486
        continue;
72✔
487
      }
488
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
827,972✔
489
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
829,295✔
490
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
830,213✔
491
          TABLE_IS_VIRTUAL(me.flags)) {
830,677✔
UNCOV
492
        tDecoderClear(&dc);
×
493
        continue;
36✔
494
      }
495
    }
496

497
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
867,919✔
498
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
846,613✔
499
      ret = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
24,135✔
500
      if (ret != 0) {
25,196✔
501
        tDecoderClear(&dc);
×
502
        return ret;
×
503
      }
504
    }
505
    tDecoderClear(&dc);
874,894✔
506
  }
507

508
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
891,662✔
509
    int64_t* uid = taosArrayGet(ctx->idList, i);
874,852✔
510
    if (uid == NULL) {
874,988✔
511
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
512
    }
513
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
874,988✔
514
    if (!idData) {
874,747✔
515
      metaError("meta/snap: null idData");
×
516
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
517
    }
518

519
    idData->index = i;
874,747✔
520
    metaDebug("tmqsnap init idVersion uid:%" PRIi64 " version:%" PRIi64 " index:%d", *uid, idData->version,
874,851✔
521
              idData->index);
522
  }
523

524
  tdbFree(pKey);
19,593✔
525
  tdbFree(pVal);
19,593✔
526
  return TDB_CODE_SUCCESS;
19,593✔
527
}
528

529
void destroySnapContext(SSnapContext* ctx) {
19,593✔
530
  tdbTbcClose((TBC*)ctx->pCur);
19,593✔
531
  taosArrayDestroy(ctx->idList);
19,567✔
532
  taosHashCleanup(ctx->idVersion);
19,593✔
533
  taosHashCleanup(ctx->suidInfo);
19,593✔
534
  taosMemoryFree(ctx);
19,593✔
535
}
19,593✔
536

537
static int32_t buildNormalChildTableInfo(SVCreateTbReq* req, void** pBuf, int32_t* contLen) {
3,041✔
538
  int32_t            ret = 0;
3,041✔
539
  SVCreateTbBatchReq reqs = {0};
3,041✔
540

541
  reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
3,041✔
542
  if (NULL == reqs.pArray) {
3,041✔
543
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
544
    goto end;
×
545
  }
546
  if (taosArrayPush(reqs.pArray, req) == NULL) {
6,082✔
547
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
548
    goto end;
×
549
  }
550
  reqs.nReqs = 1;
3,041✔
551

552
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqs, *contLen, ret);
3,041✔
553
  if (ret < 0) {
3,041✔
554
    ret = TAOS_GET_TERRNO(ret);
×
555
    goto end;
×
556
  }
557
  *contLen += sizeof(SMsgHead);
3,041✔
558
  *pBuf = taosMemoryMalloc(*contLen);
3,041✔
559
  if (NULL == *pBuf) {
3,041✔
560
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
561
    goto end;
×
562
  }
563
  SEncoder coder = {0};
3,041✔
564
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
3,041✔
565
  ret = tEncodeSVCreateTbBatchReq(&coder, &reqs);
3,041✔
566
  tEncoderClear(&coder);
3,041✔
567

568
  if (ret < 0) {
3,041✔
569
    taosMemoryFreeClear(*pBuf);
×
570
    ret = TAOS_GET_TERRNO(ret);
×
571
    goto end;
×
572
  }
573

574
end:
3,041✔
575
  taosArrayDestroy(reqs.pArray);
3,041✔
576
  return ret;
3,041✔
577
}
578

579
static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* contLen) {
1,324✔
580
  int32_t ret = 0;
1,324✔
581
  tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret);
1,324✔
582
  if (ret < 0) {
1,324✔
583
    return TAOS_GET_TERRNO(ret);
×
584
  }
585

586
  *contLen += sizeof(SMsgHead);
1,324✔
587
  *pBuf = taosMemoryMalloc(*contLen);
1,324✔
588
  if (NULL == *pBuf) {
1,324✔
589
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
590
  }
591

592
  SEncoder encoder = {0};
1,324✔
593
  tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
1,324✔
594
  ret = tEncodeSVCreateStbReq(&encoder, req);
1,324✔
595
  tEncoderClear(&encoder);
1,324✔
596
  if (ret < 0) {
1,324✔
597
    taosMemoryFreeClear(*pBuf);
×
598
    return TAOS_GET_TERRNO(ret);
×
599
  }
600
  return 0;
1,324✔
601
}
602

603
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
109,316✔
604
  if (uid == 0) {
109,316✔
605
    ctx->index = 0;
2,740✔
606
    return 0;
2,740✔
607
  }
608

609
  SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, &uid, sizeof(tb_uid_t));
106,576✔
610
  if (idInfo == NULL) {
106,576✔
611
    return terrno;
×
612
  }
613

614
  ctx->index = idInfo->index;
106,576✔
615

616
  return 0;
106,576✔
617
}
618

619
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
108,281✔
620
  bool            ret = false;
108,281✔
621
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL, 0);
108,281✔
622
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
107,982✔
623
    ret = true;
×
624
  }
625
  tDeleteSchemaWrapper(schema);
626
  ctx->hasPrimaryKey = ret;
107,881✔
627
}
108,121✔
628

629
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
217,080✔
630

631
int32_t getTableInfoFromSnapshot(SSnapContext* ctx, void** pBuf, int32_t* contLen, int16_t* type, int64_t* uid) {
5,257✔
632
  int32_t ret = 0;
5,257✔
633
  void*   pKey = NULL;
5,257✔
634
  void*   pVal = NULL;
5,257✔
635
  int     vLen = 0, kLen = 0;
5,257✔
636

637
  while (1) {
×
638
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
5,257✔
639
      metaDebug("tmqsnap get meta end");
892✔
640
      ctx->index = 0;
892✔
641
      ctx->queryMeta = 0;  // change to get data
892✔
642
      return 0;
892✔
643
    }
644

645
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
4,365✔
646
    if (uidTmp == NULL) {
4,365✔
647
      metaError("tmqsnap get meta null uid");
×
648
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
649
    }
650
    ctx->index++;
4,365✔
651
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
4,365✔
652
    if (!idInfo) {
4,365✔
653
      metaError("meta/snap: null idInfo");
×
654
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
655
    }
656

657
    *uid = *uidTmp;
4,365✔
658
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
4,365✔
659
    if (ret == 0) {
4,365✔
660
      break;
4,365✔
661
    }
662
    metaDebug("tmqsnap get meta not exist uid:%" PRIi64 " version:%" PRIi64, *uid, idInfo->version);
×
663
  }
664

665
  ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
4,365✔
666
  if (ret < 0) {
4,365✔
667
    return TAOS_GET_TERRNO(ret);
×
668
  }
669
  SDecoder   dc = {0};
4,365✔
670
  SMetaEntry me = {0};
4,365✔
671
  tDecoderInit(&dc, pVal, vLen);
4,365✔
672
  ret = metaDecodeEntry(&dc, &me);
4,365✔
673
  if (ret < 0) {
4,365✔
674
    tDecoderClear(&dc);
×
675
    ret = TAOS_GET_TERRNO(ret);
×
676
    goto END;
×
677
  }
678
  metaDebug("tmqsnap get meta uid:%" PRIi64 " name:%s index:%d", *uid, me.name, ctx->index - 1);
4,365✔
679

680
  if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
4,365✔
681
      (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
4,365✔
682
    SVCreateStbReq req = {0};
1,324✔
683
    req.name = me.name;
1,324✔
684
    req.suid = me.uid;
1,324✔
685
    req.schemaRow = me.stbEntry.schemaRow;
1,324✔
686
    req.schemaTag = me.stbEntry.schemaTag;
1,324✔
687
    req.schemaRow.version = 1;
1,324✔
688
    req.schemaTag.version = 1;
1,324✔
689
    req.colCmpr = me.colCmpr;
1,324✔
690
    req.pExtSchemas = me.pExtSchemas;
1,324✔
691

692
    ret = buildSuperTableInfo(&req, pBuf, contLen);
1,324✔
693
    *type = TDMT_VND_CREATE_STB;
1,324✔
694
  } else if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
3,041✔
695
             (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
3,041✔
696
    STableInfoForChildTable* data =
697
        (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
2,861✔
698
    if (!data) {
2,861✔
699
      metaError("meta/snap: null data");
×
700
      ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
701
      goto END;
×
702
    }
703

704
    SVCreateTbReq req = {0};
2,861✔
705

706
    req.type = TSDB_CHILD_TABLE;
2,861✔
707
    req.name = me.name;
2,861✔
708
    req.uid = me.uid;
2,861✔
709
    req.commentLen = -1;
2,861✔
710
    req.ctb.suid = me.ctbEntry.suid;
2,861✔
711
    req.ctb.tagNum = data->tagRow->nCols;
2,861✔
712
    req.ctb.stbName = data->tableName;
2,861✔
713

714
    SArray* tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
2,861✔
715
    if (tagName == NULL) {
2,861✔
716
      metaError("meta/snap: init tag name failed.");
×
717
      ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
718
      goto END;
×
719
    }
720
    STag* p = (STag*)me.ctbEntry.pTags;
2,861✔
721
    if (tTagIsJson(p)) {
2,861✔
722
      if (p->nTag != 0) {
216✔
723
        SSchema* schema = &data->tagRow->pSchema[0];
108✔
724
        if (taosArrayPush(tagName, schema->name) == NULL) {
216✔
725
          ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
726
          taosArrayDestroy(tagName);
×
727
          goto END;
×
728
        }
729
      }
730
    } else {
731
      SArray* pTagVals = NULL;
2,645✔
732
      ret = tTagToValArray((const STag*)p, &pTagVals);
2,645✔
733
      if (ret != 0) {
2,645✔
734
        metaError("meta/snap: tag to val array failed.");
×
735
        taosArrayDestroy(pTagVals);
×
736
        taosArrayDestroy(tagName);
×
737
        goto END;
×
738
      }
739
      int16_t nCols = taosArrayGetSize(pTagVals);
2,645✔
740
      for (int j = 0; j < nCols; ++j) {
7,090✔
741
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
4,445✔
742
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
15,154✔
743
          SSchema* schema = &data->tagRow->pSchema[i];
10,709✔
744
          if (schema->colId == pTagVal->cid) {
10,709✔
745
            if (taosArrayPush(tagName, schema->name) == NULL) {
8,890✔
746
              ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
747
              taosArrayDestroy(pTagVals);
×
748
              taosArrayDestroy(tagName);
×
749
              goto END;
×
750
            }
751
          }
752
        }
753
      }
754
      taosArrayDestroy(pTagVals);
2,645✔
755
    }
756
    req.ctb.pTag = me.ctbEntry.pTags;
2,861✔
757
    req.ctb.tagName = tagName;
2,861✔
758
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
2,861✔
759
    *type = TDMT_VND_CREATE_TABLE;
2,861✔
760
    taosArrayDestroy(tagName);
2,861✔
761
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
360✔
762
    SVCreateTbReq req = {0};
180✔
763
    req.type = TSDB_NORMAL_TABLE;
180✔
764
    req.name = me.name;
180✔
765
    req.uid = me.uid;
180✔
766
    req.commentLen = -1;
180✔
767
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
180✔
768
    req.colCmpr = me.colCmpr;
180✔
769
    req.pExtSchemas = me.pExtSchemas;
180✔
770
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
180✔
771
    *type = TDMT_VND_CREATE_TABLE;
180✔
772
  } else {
773
    metaError("meta/snap: invalid topic sub type: %" PRId8 " get meta from snap failed.", ctx->subType);
×
774
    ret = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
×
775
  }
776

777
END:
4,365✔
778
  tDecoderClear(&dc);
4,365✔
779
  return ret;
4,365✔
780
}
781

782
int32_t getMetaTableInfoFromSnapshot(SSnapContext* ctx, SMetaTableInfo* result) {
216,612✔
783
  void* pKey = NULL;
216,612✔
784
  void* pVal = NULL;
216,612✔
785
  int   vLen, kLen;
216,612✔
786

787
  while (1) {
2,832✔
788
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
219,444✔
789
      metaDebug("tmqsnap get uid info end");
1,755✔
790
      return 0;
1,755✔
791
    }
792
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
217,689✔
793
    if (uidTmp == NULL) {
217,689✔
794
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
795
    }
796
    ctx->index++;
217,689✔
797
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
217,689✔
798
    if (!idInfo) {
217,689✔
799
      metaError("meta/snap: null idInfo");
×
800
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
801
    }
802

803
    int32_t ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
217,689✔
804
    if (ret != 0) {
217,649✔
805
      metaDebug("tmqsnap getMetaTableInfoFromSnapshot not exist uid:%" PRIi64 " version:%" PRIi64, *uidTmp,
×
806
                idInfo->version);
807
      continue;
×
808
    }
809
    ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
217,649✔
810
    if (ret != 0) {
217,591✔
811
      return TAOS_GET_TERRNO(ret);
×
812
    }
813
    SDecoder   dc = {0};
217,591✔
814
    SMetaEntry me = {0};
217,591✔
815
    tDecoderInit(&dc, pVal, vLen);
217,591✔
816
    ret = metaDecodeEntry(&dc, &me);
217,649✔
817
    if (ret != 0) {
217,209✔
818
      tDecoderClear(&dc);
×
819
      return TAOS_GET_TERRNO(ret);
×
820
    }
821
    metaDebug("tmqsnap get uid info uid:%" PRIi64 " name:%s index:%d", me.uid, me.name, ctx->index - 1);
217,209✔
822

823
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
217,209✔
824
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
217,249✔
825
      STableInfoForChildTable* data =
826
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
54,925✔
827
      if (data == NULL) {
54,925✔
828
        tDecoderClear(&dc);
×
829
        metaError("meta/snap: null data");
×
830
        return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
831
      }
832
      result->suid = me.ctbEntry.suid;
54,925✔
833
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
109,791✔
834
      if (data->pExtSchemas != NULL) {
54,866✔
835
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * data->schemaRow->nCols);
647✔
836
        if (result->pExtSchemas == NULL) {
647✔
837
          tDecoderClear(&dc);
×
838
          metaError("result->pExtSchemas malloc failed");
×
839
          return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
840
        }
841
        memcpy(result->pExtSchemas, data->pExtSchemas, sizeof(SExtSchema) * data->schemaRow->nCols);
647✔
842
      }
843
      
844
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
162,404✔
845
      result->suid = 0;
159,652✔
846
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
159,852✔
847
      if (me.pExtSchemas != NULL) {
159,532✔
848
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
849
        if (result->pExtSchemas == NULL) {
×
850
          tDecoderClear(&dc);
×
851
          metaError("result->pExtSchemas malloc failed");
×
852
          return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
853
        }
854
        memcpy(result->pExtSchemas, me.pExtSchemas, sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
855
      }
856
    } else {
857
      metaDebug("tmqsnap get uid continue");
2,712✔
858
      tDecoderClear(&dc);
2,712✔
859
      continue;
2,832✔
860
    }
861
    result->uid = me.uid;
214,457✔
862
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
214,577✔
863
    tDecoderClear(&dc);
214,537✔
864
    if (result->schema == NULL) {
214,657✔
865
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
866
    }
867
    break;
214,417✔
868
  }
869
  return 0;
214,657✔
870
}
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