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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

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

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

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

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

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

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

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

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

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

81
  tDecoderClear(&decoder);
311,370✔
82
  return 0;
311,370✔
83
}
84

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

94
  *ppData = NULL;
182,747✔
95
  while (pReader->iLoop < 2) {
393,621✔
96
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
366,204✔
97
      pReader->iLoop++;
54,834✔
98

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

245
  metaHandleSyncEntry(pMeta, &metaEntry);
155,365✔
246

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

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

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

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

319
static int32_t saveSuperTableInfoForChildTable(SMetaEntry* me, SHashObj* suidInfo) {
159,223✔
320
  STableInfoForChildTable* data = (STableInfoForChildTable*)taosHashGet(suidInfo, &me->uid, sizeof(tb_uid_t));
159,223✔
321
  if (data) {
159,223✔
322
    return 0;
×
323
  }
324
  int32_t                 code = 0;
159,223✔
325
  STableInfoForChildTable dataTmp = {0};
159,223✔
326
  dataTmp.tableName = taosStrdup(me->name);
159,223✔
327
  if (dataTmp.tableName == NULL) {
159,223✔
328
    code = terrno;
×
329
    goto END;
×
330
  }
331
  dataTmp.schemaRow = tCloneSSchemaWrapper(&me->stbEntry.schemaRow);
159,223✔
332
  if (dataTmp.schemaRow == NULL) {
159,223✔
333
    code = TSDB_CODE_OUT_OF_MEMORY;
×
334
    goto END;
×
335
  }
336
  dataTmp.tagRow = tCloneSSchemaWrapper(&me->stbEntry.schemaTag);
159,223✔
337
  if (dataTmp.tagRow == NULL) {
159,223✔
338
    code = TSDB_CODE_OUT_OF_MEMORY;
×
339
    goto END;
×
340
  }
341
  if (me->pExtSchemas != NULL) {
159,223✔
342
    dataTmp.pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me->stbEntry.schemaRow.nCols);
7,455✔
343
    if (dataTmp.pExtSchemas == NULL) {
7,455✔
344
      code = TSDB_CODE_OUT_OF_MEMORY;
×
345
      goto END;
×
346
    }
347
    memcpy(dataTmp.pExtSchemas, me->pExtSchemas, sizeof(SExtSchema) * me->stbEntry.schemaRow.nCols);
7,455✔
348
  }
349
  
350
  code = taosHashPut(suidInfo, &me->uid, sizeof(tb_uid_t), &dataTmp, sizeof(STableInfoForChildTable));
159,223✔
351
  if (code != 0) {
159,223✔
352
    goto END;
×
353
  }
354
  return 0;
159,223✔
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,
111,358✔
362
                         SSnapContext** ctxRet) {
363
  int32_t code = 0;
111,358✔
364
  int32_t lino = 0;
111,358✔
365
  SDecoder   dc = {0};
111,358✔
366
  void* pKey = NULL;
111,647✔
367
  void* pVal = NULL;
111,647✔
368
  int   vLen = 0, kLen = 0;
111,647✔
369

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

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

390
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
111,647✔
391
  code = MoveToFirst(ctx);
111,647✔
392
  TSDB_CHECK_CODE(code, lino, END);
111,647✔
393
  while (1) {
8,551,916✔
394
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
8,663,563✔
395
    if (ret < 0) break;
8,660,658✔
396
    STbDbKey* tmp = (STbDbKey*)pKey;
8,550,213✔
397
    if (tmp->version > ctx->snapVersion) break;
8,550,213✔
398

399
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
8,549,073✔
400
    if (idData) {
8,552,230✔
401
      continue;
83,020✔
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) {
8,469,210✔
406
      continue;
86,400✔
407
    }
408

409
    SMetaEntry me = {0};
8,374,685✔
410
    tDecoderInit(&dc, pVal, vLen);
8,372,249✔
411
    code = metaDecodeEntry(&dc, &me);
8,375,072✔
412
    TSDB_CHECK_CODE(code, lino, END);
8,370,659✔
413
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
8,370,659✔
414
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
230,773✔
415
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
230,521✔
416
        tDecoderClear(&dc);
1,008✔
417
        continue;
1,008✔
418
      }
419
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
8,137,478✔
420
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
8,139,278✔
421
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
8,145,615✔
422
          TABLE_IS_VIRTUAL(me.flags)) {
8,147,722✔
423
        tDecoderClear(&dc);
×
424
        continue;
×
425
      }
426
    }
427

428
    TSDB_CHECK_NULL(taosArrayPush(ctx->idList, &tmp->uid), code, lino, END, terrno);
16,747,635✔
429
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
8,370,449✔
430
    tDecoderClear(&dc);
8,384,933✔
431

432
    SIdInfo info = {0};
8,382,955✔
433
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
8,383,835✔
434
    TSDB_CHECK_CODE(code, lino, END);
8,382,378✔
435
  }
436
  taosHashClear(ctx->idVersion);
111,647✔
437

438
  code = MoveToSnapShotVersion(ctx);
111,647✔
439
  TSDB_CHECK_CODE(code, lino, END);
111,647✔
440

441
  while (1) {
8,518,376✔
442
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
8,630,023✔
443
    if (ret < 0) break;
8,646,187✔
444

445
    STbDbKey* tmp = (STbDbKey*)pKey;
8,534,540✔
446
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
8,534,540✔
447
    if (idData) {
8,525,326✔
448
      continue;
139,954✔
449
    }
450
    SIdInfo info = {.version = tmp->version, .index = 0};
8,385,372✔
451
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
8,383,648✔
452
    TSDB_CHECK_CODE(code, lino, END);
8,394,100✔
453

454
    SMetaEntry me = {0};
8,394,100✔
455
    tDecoderInit(&dc, pVal, vLen);
8,382,339✔
456
    code = metaDecodeEntry(&dc, &me);
8,354,712✔
457
    TSDB_CHECK_CODE(code, lino, END);
8,284,498✔
458

459
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
8,284,498✔
460
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
230,773✔
461
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
230,521✔
462
        tDecoderClear(&dc);
1,008✔
463
        continue;
1,008✔
464
      }
465
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
8,119,579✔
466
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
8,140,755✔
467
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
8,141,710✔
468
          TABLE_IS_VIRTUAL(me.flags)) {
8,145,610✔
UNCOV
469
        tDecoderClear(&dc);
×
470
        continue;
×
471
      }
472
    }
473

474
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
8,347,027✔
475
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
8,244,368✔
476
      code = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
156,230✔
477
      TSDB_CHECK_CODE(code, lino, END);
159,223✔
478
    }
479
    tDecoderClear(&dc);
8,385,570✔
480

481
  }
482

483
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
8,495,485✔
484
    int64_t* uid = taosArrayGet(ctx->idList, i);
8,381,225✔
485
    TSDB_CHECK_NULL(uid, code, lino, END, terrno);
8,380,635✔
486
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
8,380,635✔
487
    TSDB_CHECK_NULL(idData, code, lino, END, terrno);
8,382,598✔
488

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

493
END:
84,637✔
494
  tdbFree(pKey);
111,647✔
495
  tdbFree(pVal);
111,647✔
496
  tDecoderClear(&dc);
111,647✔
497

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

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

512
void destroySnapContext(SSnapContext* ctx) {
534,968✔
513
  if (ctx == NULL) {
534,968✔
514
    return;
423,321✔
515
  }
516
  taosArrayDestroy(ctx->idList);
111,647✔
517
  taosHashCleanup(ctx->idVersion);
111,647✔
518
  taosHashCleanup(ctx->suidInfo);
111,647✔
519
  taosMemoryFree(ctx);
111,647✔
520
}
521

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

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

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

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

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

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

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

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

588
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
730,601✔
589
  if (uid == 0) {
730,601✔
590
    ctx->index = 0;
14,826✔
591
    return 0;
14,826✔
592
  }
593

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

599
  ctx->index = idInfo->index;
716,106✔
600

601
  return 0;
716,106✔
602
}
603

604
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
725,548✔
605
  bool            ret = false;
725,548✔
606
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL, 0);
725,548✔
607
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
725,260✔
608
    ret = true;
1,252✔
609
  }
610
  tDeleteSchemaWrapper(schema);
611
  ctx->hasPrimaryKey = ret;
724,963✔
612
}
724,963✔
613

614
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
1,458,553✔
615

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

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

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

641
    *uid = *uidTmp;
33,503✔
642
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
33,503✔
643
    if (ret == 0) {
33,503✔
644
      break;
33,503✔
645
    }
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);
33,503✔
650
  TSDB_CHECK_CONDITION(ret >= 0, ret, lino, END, TAOS_GET_TERRNO(ret));
33,503✔
651
  SMetaEntry me = {0};
33,503✔
652
  tDecoderInit(&dc, pVal, vLen);
33,503✔
653
  ret = metaDecodeEntry(&dc, &me);
33,503✔
654
  TSDB_CHECK_CONDITION(ret >= 0, ret, lino, END, TAOS_GET_TERRNO(ret));
33,503✔
655
  metaDebug("tmqsnap get meta uid:%" PRIi64 " name:%s index:%d", *uid, me.name, ctx->index - 1);
33,503✔
656

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

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

677
    SVCreateTbReq req = {0};
20,356✔
678

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

687
    tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
20,356✔
688
    TSDB_CHECK_NULL(tagName, ret, lino, END, terrno);
20,356✔
689
    STag* p = (STag*)me.ctbEntry.pTags;
20,356✔
690
    if (tTagIsJson(p)) {
20,356✔
691
      if (p->nTag != 0) {
3,024✔
692
        SSchema* schema = &data->tagRow->pSchema[0];
1,512✔
693
        TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
3,024✔
694
      }
695
    } else {
696
      ret = tTagToValArray((const STag*)p, &pTagVals);
17,332✔
697
      TSDB_CHECK_CODE(ret, lino, END);
17,332✔
698
      int16_t nCols = taosArrayGetSize(pTagVals);
17,332✔
699
      for (int j = 0; j < nCols; ++j) {
59,864✔
700
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
42,532✔
701
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
171,500✔
702
          SSchema* schema = &data->tagRow->pSchema[i];
128,968✔
703
          if (schema->colId == pTagVal->cid) {
129,220✔
704
            TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
84,812✔
705
          }
706
        }
707
      }
708
    }
709
    req.ctb.pTag = me.ctbEntry.pTags;
20,356✔
710
    req.ctb.tagName = tagName;
20,356✔
711
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
20,356✔
712
    *type = TDMT_VND_CREATE_TABLE;
20,356✔
713
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
6,344✔
714
    SVCreateTbReq req = {0};
3,172✔
715
    req.type = TSDB_NORMAL_TABLE;
3,172✔
716
    req.name = me.name;
3,172✔
717
    req.uid = me.uid;
3,172✔
718
    req.commentLen = -1;
3,172✔
719
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
3,172✔
720
    req.colCmpr = me.colCmpr;
3,172✔
721
    req.pExtSchemas = me.pExtSchemas;
3,172✔
722
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
3,172✔
723
    *type = TDMT_VND_CREATE_TABLE;
3,172✔
724
  } else {
725
    metaError("meta/snap: invalid topic sub type: %" PRId8 " get meta from snap failed.", ctx->subType);
×
726
    ret = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
×
727
  }
728

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

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

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

752
  metaRLock(ctx->pMeta);
1,451,192✔
753
  while (1) {
17,565✔
754
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
1,468,757✔
755
      metaDebug("tmqsnap get uid info end");
9,812✔
756
      goto END;
9,812✔
757
    }
758
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
1,458,945✔
759
    TSDB_CHECK_NULL(uidTmp, code, lino, END, terrno);
1,458,648✔
760
    ctx->index++;
1,458,648✔
761
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
1,458,945✔
762
    TSDB_CHECK_NULL(idInfo, code, lino, END, terrno);
1,458,657✔
763

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

777
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
1,458,063✔
778
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
1,458,686✔
779
      STableInfoForChildTable* data =
780
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
286,636✔
781
      TSDB_CHECK_NULL(data, code, lino, END, terrno);
286,933✔
782
      result->suid = me.ctbEntry.suid;
286,933✔
783
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
574,163✔
784
      if (data->pExtSchemas != NULL) {
287,230✔
785
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * data->schemaRow->nCols);
7,861✔
786
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
7,861✔
787
        memcpy(result->pExtSchemas, data->pExtSchemas, sizeof(SExtSchema) * data->schemaRow->nCols);
7,861✔
788
      }
789
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
1,171,715✔
790
      result->suid = 0;
1,154,150✔
791
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
1,153,286✔
792
      if (me.pExtSchemas != NULL) {
1,153,286✔
793
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
794
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
×
795
        memcpy(result->pExtSchemas, me.pExtSchemas, sizeof(SExtSchema) * me.ntbEntry.schemaRow.nCols);
×
796
      }
797
    } else {
798
      metaDebug("tmqsnap get uid continue");
17,565✔
799
      tDecoderClear(&dc);
17,565✔
800
      continue;
17,565✔
801
    }
802
    result->uid = me.uid;
1,440,219✔
803
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
1,439,600✔
804
    TSDB_CHECK_NULL(result->schema, code, lino, END, TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY));
1,440,210✔
805
    break;
1,439,922✔
806
  }
807

808
END:
1,450,564✔
809
  tDecoderClear(&dc);
1,450,861✔
810
  tdbTbcClose((TBC*)ctx->pCur);
1,451,192✔
811
  ctx->pCur = NULL;
1,450,904✔
812
  metaULock(ctx->pMeta);
1,450,904✔
813

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