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

taosdata / TDengine / #4792

15 Oct 2025 05:45AM UTC coverage: 60.915% (-0.08%) from 60.992%
#4792

push

travis-ci

web-flow
Merge 4a01db54d into 19574fe21

154646 of 324376 branches covered (47.67%)

Branch coverage included in aggregate %.

31 of 62 new or added lines in 4 files covered. (50.0%)

440 existing lines in 117 files now uncovered.

207143 of 269552 relevant lines covered (76.85%)

127426675.82 hits per line

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

59.5
/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) {
11,009✔
28
  int32_t          code = 0;
11,009✔
29
  int32_t          lino;
30
  int32_t          c = 0;
11,009✔
31
  SMetaSnapReader* pReader = NULL;
11,009✔
32

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

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

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

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

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

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

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

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

81
  tDecoderClear(&decoder);
76,336✔
82
  return 0;
76,336✔
83
}
84

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

94
  *ppData = NULL;
47,756✔
95
  while (pReader->iLoop < 2) {
109,363✔
96
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
98,354!
97
      pReader->iLoop++;
22,018✔
98

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

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

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

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

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

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

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

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

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

193
  code = metaBegin(pMeta, META_BEGIN_HEAP_NIL);
8,236✔
194
  TSDB_CHECK_CODE(code, lino, _exit);
8,236!
195

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

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

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

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

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

245
  metaHandleSyncEntry(pMeta, &metaEntry);
34,632✔
246

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

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

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

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

319
static int32_t saveSuperTableInfoForChildTable(SMetaEntry* me, SHashObj* suidInfo) {
27,606✔
320
  STableInfoForChildTable* data = (STableInfoForChildTable*)taosHashGet(suidInfo, &me->uid, sizeof(tb_uid_t));
27,606✔
321
  if (data) {
27,516!
322
    return 0;
×
323
  }
324
  int32_t                 code = 0;
27,516✔
325
  STableInfoForChildTable dataTmp = {0};
27,516✔
326
  dataTmp.tableName = taosStrdup(me->name);
27,516!
327
  if (dataTmp.tableName == NULL) {
27,606!
328
    code = terrno;
×
329
    goto END;
×
330
  }
331
  dataTmp.schemaRow = tCloneSSchemaWrapper(&me->stbEntry.schemaRow);
27,606!
332
  if (dataTmp.schemaRow == NULL) {
27,606!
333
    code = TSDB_CODE_OUT_OF_MEMORY;
×
334
    goto END;
×
335
  }
336
  dataTmp.tagRow = tCloneSSchemaWrapper(&me->stbEntry.schemaTag);
27,606!
337
  if (dataTmp.tagRow == NULL) {
27,606!
338
    code = TSDB_CODE_OUT_OF_MEMORY;
×
339
    goto END;
×
340
  }
341
  if (me->pExtSchemas != NULL) {
27,606✔
342
    dataTmp.pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me->stbEntry.schemaRow.nCols);
1,030!
343
    if (dataTmp.pExtSchemas == NULL) {
1,030!
344
      code = TSDB_CODE_OUT_OF_MEMORY;
×
345
      goto END;
×
346
    }
347
    memcpy(dataTmp.pExtSchemas, me->pExtSchemas, sizeof(SExtSchema) * me->stbEntry.schemaRow.nCols);
1,030!
348
  }
349
  
350
  code = taosHashPut(suidInfo, &me->uid, sizeof(tb_uid_t), &dataTmp, sizeof(STableInfoForChildTable));
27,606✔
351
  if (code != 0) {
27,606!
352
    goto END;
×
353
  }
354
  return 0;
27,606✔
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,
20,314✔
363
                         SSnapContext** ctxRet) {
364
  SSnapContext* ctx = taosMemoryCalloc(1, sizeof(SSnapContext));
20,314!
365
  if (ctx == NULL) {
20,390!
366
    return terrno;
×
367
  }
368
  *ctxRet = ctx;
20,390✔
369
  ctx->pMeta = pVnode->pMeta;
20,390✔
370
  ctx->snapVersion = snapVersion;
20,390✔
371
  ctx->suid = suid;
20,390✔
372
  ctx->subType = subType;
20,390✔
373
  ctx->queryMeta = withMeta;
20,390✔
374
  ctx->withMeta = withMeta;
20,390✔
375
  ctx->idVersion = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
20,390✔
376
  if (ctx->idVersion == NULL) {
20,390!
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);
20,390✔
381
  if (ctx->suidInfo == NULL) {
20,390!
382
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
383
  }
384
  taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable);
20,390✔
385

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

396
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
20,390✔
397
  int32_t code = MoveToFirst(ctx);
20,390✔
398
  if (code != 0) {
20,390!
399
    return code;
×
400
  }
401
  while (1) {
1,082,040✔
402
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
1,102,430✔
403
    if (ret < 0) break;
1,099,413✔
404
    STbDbKey* tmp = (STbDbKey*)pKey;
1,079,295✔
405
    if (tmp->version > ctx->snapVersion) break;
1,079,295✔
406

407
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
1,080,201✔
408
    if (idData) {
1,080,987✔
409
      continue;
14,914✔
410
    }
411

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

417
    SDecoder   dc = {0};
1,050,670✔
418
    SMetaEntry me = {0};
1,051,559✔
419
    tDecoderInit(&dc, pVal, vLen);
1,050,709✔
420
    ret = metaDecodeEntry(&dc, &me);
1,050,568✔
421
    if (ret < 0) {
1,044,597!
422
      tDecoderClear(&dc);
×
423
      return TAOS_GET_TERRNO(ret);
×
424
    }
425
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
1,044,597✔
426
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
38,455✔
427
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
38,418✔
428
        tDecoderClear(&dc);
124✔
429
        continue;
124✔
430
      }
431
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
1,008,746!
432
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
1,011,489✔
433
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
1,008,623!
434
          TABLE_IS_VIRTUAL(me.flags)) {
1,010,696✔
435
        tDecoderClear(&dc);
824✔
436
        continue;
62✔
437
      }
438
    }
439

440
    if (taosArrayPush(ctx->idList, &tmp->uid) == NULL) {
2,096,942!
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);
1,049,207✔
445
    tDecoderClear(&dc);
1,052,705✔
446

447
    SIdInfo info = {0};
1,050,819✔
448
    if (taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo)) != 0) {
1,051,707!
449
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
450
    }
451
  }
452
  taosHashClear(ctx->idVersion);
20,390✔
453

454
  code = MoveToSnapShotVersion(ctx);
20,390✔
455
  if (code != 0) {
20,390!
456
    return code;
×
457
  }
458
  while (1) {
1,079,142✔
459
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
1,099,532✔
460
    if (ret < 0) break;
1,096,660✔
461

462
    STbDbKey* tmp = (STbDbKey*)pKey;
1,076,270✔
463
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
1,076,270✔
464
    if (idData) {
1,077,724✔
465
      continue;
23,091✔
466
    }
467
    SIdInfo info = {.version = tmp->version, .index = 0};
1,054,633✔
468
    ret = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
1,054,771✔
469
    if (ret != 0) {
1,055,266!
470
      return TAOS_GET_TERRNO(ret);
×
471
    }
472

473
    SDecoder   dc = {0};
1,055,266✔
474
    SMetaEntry me = {0};
1,054,990✔
475
    tDecoderInit(&dc, pVal, vLen);
1,055,172✔
476
    ret = metaDecodeEntry(&dc, &me);
1,052,781✔
477
    if (ret < 0) {
1,050,650!
478
      tDecoderClear(&dc);
×
479
      return TAOS_GET_TERRNO(ret);
×
480
    }
481

482
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
1,050,650✔
483
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
38,505✔
484
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
38,474✔
485
        tDecoderClear(&dc);
124✔
486
        continue;
124✔
487
      }
488
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
1,014,900!
489
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
1,015,440!
490
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
1,015,638!
491
          TABLE_IS_VIRTUAL(me.flags)) {
1,015,699✔
492
        tDecoderClear(&dc);
×
493
        continue;
62✔
494
      }
495
    }
496

497
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
1,053,786✔
498
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
1,030,432✔
499
      ret = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
28,033✔
500
      if (ret != 0) {
27,606!
501
        tDecoderClear(&dc);
×
502
        return ret;
×
503
      }
504
    }
505
    tDecoderClear(&dc);
1,055,870✔
506
  }
507

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

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

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

529
void destroySnapContext(SSnapContext* ctx) {
20,390✔
530
  tdbTbcClose((TBC*)ctx->pCur);
20,390✔
531
  taosArrayDestroy(ctx->idList);
20,390✔
532
  taosHashCleanup(ctx->idVersion);
20,390✔
533
  taosHashCleanup(ctx->suidInfo);
20,390✔
534
  taosMemoryFree(ctx);
20,344!
535
}
20,359✔
536

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

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

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

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

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

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

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

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

603
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
127,461✔
604
  if (uid == 0) {
127,461✔
605
    ctx->index = 0;
3,051✔
606
    return 0;
3,036✔
607
  }
608

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

614
  ctx->index = idInfo->index;
124,410✔
615

616
  return 0;
124,410✔
617
}
618

619
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
126,042✔
620
  bool            ret = false;
126,042✔
621
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL, 0);
126,042✔
622
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
125,964!
623
    ret = true;
×
624
  }
625
  tDeleteSchemaWrapper(schema);
626
  ctx->hasPrimaryKey = ret;
125,582✔
627
}
126,056✔
628

629
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
246,008!
630

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

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

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

657
    *uid = *uidTmp;
5,862✔
658
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
5,862✔
659
    if (ret == 0) {
5,862!
660
      break;
5,862✔
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);
5,862✔
666
  if (ret < 0) {
5,831!
667
    return TAOS_GET_TERRNO(ret);
×
668
  }
669
  SDecoder   dc = {0};
5,831✔
670
  SMetaEntry me = {0};
5,862✔
671
  tDecoderInit(&dc, pVal, vLen);
5,831✔
672
  ret = metaDecodeEntry(&dc, &me);
5,862✔
673
  if (ret < 0) {
5,831!
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);
5,831!
679

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

692
    ret = buildSuperTableInfo(&req, pBuf, contLen);
1,776✔
693
    *type = TDMT_VND_CREATE_STB;
1,724✔
694
  } else if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
4,086!
695
             (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
4,086!
696
    STableInfoForChildTable* data =
697
        (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
3,776✔
698
    if (!data) {
3,776!
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};
3,776✔
705

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

714
    SArray* tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
3,776✔
715
    if (tagName == NULL) {
3,776!
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;
3,776✔
721
    if (tTagIsJson(p)) {
3,776✔
722
      if (p->nTag != 0) {
372✔
723
        SSchema* schema = &data->tagRow->pSchema[0];
186✔
724
        if (taosArrayPush(tagName, schema->name) == NULL) {
372!
725
          ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
726
          taosArrayDestroy(tagName);
×
727
          goto END;
×
728
        }
729
      }
730
    } else {
731
      SArray* pTagVals = NULL;
3,404✔
732
      ret = tTagToValArray((const STag*)p, &pTagVals);
3,404✔
733
      if (ret != 0) {
3,404!
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);
3,404✔
740
      for (int j = 0; j < nCols; ++j) {
9,908✔
741
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
6,504✔
742
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
23,796!
743
          SSchema* schema = &data->tagRow->pSchema[i];
17,292✔
744
          if (schema->colId == pTagVal->cid) {
17,292✔
745
            if (taosArrayPush(tagName, schema->name) == NULL) {
13,008!
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);
3,404✔
755
    }
756
    req.ctb.pTag = me.ctbEntry.pTags;
3,776✔
757
    req.ctb.tagName = tagName;
3,776✔
758
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
3,776✔
759
    *type = TDMT_VND_CREATE_TABLE;
3,776✔
760
    taosArrayDestroy(tagName);
3,776✔
761
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
620!
762
    SVCreateTbReq req = {0};
310✔
763
    req.type = TSDB_NORMAL_TABLE;
310✔
764
    req.name = me.name;
310✔
765
    req.uid = me.uid;
310✔
766
    req.commentLen = -1;
310✔
767
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
310✔
768
    req.colCmpr = me.colCmpr;
310✔
769
    req.pExtSchemas = me.pExtSchemas;
310✔
770
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
310✔
771
    *type = TDMT_VND_CREATE_TABLE;
310✔
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:
5,810✔
778
  tDecoderClear(&dc);
5,810✔
779
  return ret;
5,862✔
780
}
781

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

787
  while (1) {
3,316✔
788
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
256,144✔
789
      metaDebug("tmqsnap get uid info end");
2,224✔
790
      return 0;
2,224✔
791
    }
792
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
254,012✔
793
    if (uidTmp == NULL) {
253,952!
794
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
795
    }
796
    ctx->index++;
253,952✔
797
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
254,012✔
798
    if (!idInfo) {
253,952!
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);
253,952✔
804
    if (ret != 0) {
253,874!
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);
253,874✔
810
    if (ret != 0) {
253,952!
811
      return TAOS_GET_TERRNO(ret);
×
812
    }
813
    SDecoder   dc = {0};
253,952✔
814
    SMetaEntry me = {0};
253,874✔
815
    tDecoderInit(&dc, pVal, vLen);
253,875✔
816
    ret = metaDecodeEntry(&dc, &me);
253,814✔
817
    if (ret != 0) {
252,406!
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);
252,406✔
822

823
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
252,406✔
824
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
253,290!
825
      STableInfoForChildTable* data =
826
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
66,008✔
827
      if (data == NULL) {
66,570!
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;
66,570✔
833
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
133,080!
834
      if (data->pExtSchemas != NULL) {
66,510✔
835
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * data->schemaRow->nCols);
1,042!
836
        if (result->pExtSchemas == NULL) {
1,042!
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);
1,042!
842
      }
843
      
844
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
186,186!
845
      result->suid = 0;
183,560!
846
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
183,560✔
847
      if (me.pExtSchemas != NULL) {
182,870!
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,902✔
858
      tDecoderClear(&dc);
2,902✔
859
      continue;
3,316✔
860
    }
861
    result->uid = me.uid;
249,500✔
862
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
249,716!
863
    tDecoderClear(&dc);
249,624✔
864
    if (result->schema == NULL) {
250,024!
865
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
866
    }
867
    break;
249,334✔
868
  }
869
  return 0;
250,102✔
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