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

taosdata / TDengine / #4966

09 Feb 2026 01:16AM UTC coverage: 66.832% (-0.05%) from 66.884%
#4966

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205639 of 307696 relevant lines covered (66.83%)

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

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

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

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

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

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

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

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

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

81
  tDecoderClear(&decoder);
322,776✔
82
  return 0;
322,776✔
83
}
84

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

94
  *ppData = NULL;
189,200✔
95
  while (pReader->iLoop < 2) {
407,148✔
96
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
379,024✔
97
      pReader->iLoop++;
56,248✔
98

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

245
  metaHandleSyncEntry(pMeta, &metaEntry);
161,077✔
246

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

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

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

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

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

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

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

390
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
119,624✔
391
  code = MoveToFirst(ctx);
119,624✔
392
  TSDB_CHECK_CODE(code, lino, END);
119,624✔
393
  while (1) {
11,919,275✔
394
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
12,038,899✔
395
    if (ret < 0) break;
12,045,020✔
396
    STbDbKey* tmp = (STbDbKey*)pKey;
11,926,626✔
397
    if (tmp->version > ctx->snapVersion) break;
11,926,626✔
398

399
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
11,928,080✔
400
    if (idData) {
11,927,820✔
401
      continue;
86,231✔
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) {
11,841,589✔
406
      continue;
87,416✔
407
    }
408

409
    SMetaEntry me = {0};
11,739,540✔
410
    tDecoderInit(&dc, pVal, vLen);
11,736,674✔
411
    code = metaDecodeEntry(&dc, &me);
11,753,220✔
412
    TSDB_CHECK_CODE(code, lino, END);
11,711,653✔
413
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
11,711,653✔
414
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
243,890✔
415
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
243,631✔
416
        tDecoderClear(&dc);
1,036✔
417
        continue;
1,036✔
418
      }
419
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
11,474,050✔
420
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
11,493,525✔
421
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
11,490,850✔
422
          TABLE_IS_VIRTUAL(me.flags)) {
11,497,860✔
423
        tDecoderClear(&dc);
×
424
        continue;
×
425
      }
426
    }
427

428
    TSDB_CHECK_NULL(taosArrayPush(ctx->idList, &tmp->uid), code, lino, END, terrno);
23,457,834✔
429
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
11,723,081✔
430
    tDecoderClear(&dc);
11,727,867✔
431

432
    SIdInfo info = {0};
11,711,031✔
433
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
11,724,159✔
434
    TSDB_CHECK_CODE(code, lino, END);
11,745,157✔
435
  }
436
  taosHashClear(ctx->idVersion);
119,624✔
437

438
  code = MoveToSnapShotVersion(ctx);
119,624✔
439
  TSDB_CHECK_CODE(code, lino, END);
119,624✔
440

441
  while (1) {
11,918,201✔
442
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
12,037,825✔
443
    if (ret < 0) break;
12,036,460✔
444

445
    STbDbKey* tmp = (STbDbKey*)pKey;
11,916,836✔
446
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
11,916,836✔
447
    if (idData) {
11,919,453✔
448
      continue;
144,484✔
449
    }
450
    SIdInfo info = {.version = tmp->version, .index = 0};
11,774,969✔
451
    code = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
11,775,842✔
452
    TSDB_CHECK_CODE(code, lino, END);
11,775,781✔
453

454
    SMetaEntry me = {0};
11,775,781✔
455
    tDecoderInit(&dc, pVal, vLen);
11,771,317✔
456
    code = metaDecodeEntry(&dc, &me);
11,784,813✔
457
    TSDB_CHECK_CODE(code, lino, END);
11,755,430✔
458

459
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
11,755,430✔
460
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
243,890✔
461
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
243,631✔
462
        tDecoderClear(&dc);
1,036✔
463
        continue;
1,036✔
464
      }
465
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
11,514,720✔
466
      if (me.type == TSDB_VIRTUAL_NORMAL_TABLE ||
11,525,613✔
467
          me.type == TSDB_VIRTUAL_CHILD_TABLE ||
11,528,626✔
468
          TABLE_IS_VIRTUAL(me.flags)) {
11,533,534✔
469
        tDecoderClear(&dc);
×
470
        continue;
×
471
      }
472
    }
473

474
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
11,770,514✔
475
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
11,613,573✔
476
      code = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
176,083✔
477
      TSDB_CHECK_CODE(code, lino, END);
172,018✔
478
    }
479
    tDecoderClear(&dc);
11,779,633✔
480

481
  }
482

483
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
11,877,820✔
484
    int64_t* uid = taosArrayGet(ctx->idList, i);
11,758,833✔
485
    TSDB_CHECK_NULL(uid, code, lino, END, terrno);
11,759,104✔
486
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
11,759,104✔
487
    TSDB_CHECK_NULL(idData, code, lino, END, terrno);
11,758,215✔
488

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

493
END:
116,360✔
494
  tdbFree(pKey);
119,624✔
495
  tdbFree(pVal);
119,624✔
496
  tDecoderClear(&dc);
119,624✔
497

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

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

512
void destroySnapContext(SSnapContext* ctx) {
557,860✔
513
  if (ctx == NULL) {
557,860✔
514
    return;
439,105✔
515
  }
516
  taosArrayDestroy(ctx->idList);
118,755✔
517
  taosHashCleanup(ctx->idVersion);
119,624✔
518
  taosHashCleanup(ctx->suidInfo);
119,624✔
519
  taosMemoryFree(ctx);
119,290✔
520
}
521

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

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

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

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

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

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

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

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

588
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
737,080✔
589
  if (uid == 0) {
737,080✔
590
    ctx->index = 0;
15,131✔
591
    return 0;
15,131✔
592
  }
593

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

599
  ctx->index = idInfo->index;
721,949✔
600

601
  return 0;
721,949✔
602
}
603

604
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
730,962✔
605
  bool            ret = false;
730,962✔
606
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL, 0);
730,962✔
607
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
730,682✔
608
    ret = true;
1,282✔
609
  }
610
  tDeleteSchemaWrapper(schema);
611
  ctx->hasPrimaryKey = ret;
730,102✔
612
}
731,262✔
613

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

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

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

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

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

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

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

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

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

687
    tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
20,920✔
688
    TSDB_CHECK_NULL(tagName, ret, lino, END, terrno);
20,920✔
689
    STag* p = (STag*)me.ctbEntry.pTags;
20,920✔
690
    if (tTagIsJson(p)) {
20,920✔
691
      if (p->nTag != 0) {
3,108✔
692
        SSchema* schema = &data->tagRow->pSchema[0];
1,554✔
693
        TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
3,108✔
694
      }
695
    } else {
696
      ret = tTagToValArray((const STag*)p, &pTagVals);
17,812✔
697
      TSDB_CHECK_CODE(ret, lino, END);
17,812✔
698
      int16_t nCols = taosArrayGetSize(pTagVals);
17,812✔
699
      for (int j = 0; j < nCols; ++j) {
61,524✔
700
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
43,712✔
701
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
177,556✔
702
          SSchema* schema = &data->tagRow->pSchema[i];
133,844✔
703
          if (schema->colId == pTagVal->cid) {
133,844✔
704
            TSDB_CHECK_NULL(taosArrayPush(tagName, schema->name), ret, lino, END, terrno);
87,424✔
705
          }
706
        }
707
      }
708
    }
709
    req.ctb.pTag = me.ctbEntry.pTags;
20,920✔
710
    req.ctb.tagName = tagName;
20,920✔
711
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
20,920✔
712
    *type = TDMT_VND_CREATE_TABLE;
20,920✔
713
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
6,512✔
714
    SVCreateTbReq req = {0};
3,256✔
715
    req.type = TSDB_NORMAL_TABLE;
3,256✔
716
    req.name = me.name;
3,256✔
717
    req.uid = me.uid;
3,256✔
718
    req.commentLen = -1;
3,256✔
719
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
3,256✔
720
    req.colCmpr = me.colCmpr;
3,256✔
721
    req.pExtSchemas = me.pExtSchemas;
3,256✔
722
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
3,256✔
723
    *type = TDMT_VND_CREATE_TABLE;
3,256✔
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:
39,128✔
730
  tdbTbcClose((TBC*)ctx->pCur);
39,128✔
731
  ctx->pCur = NULL;
39,128✔
732
  taosArrayDestroy(pTagVals);
39,128✔
733
  taosArrayDestroy(tagName);
39,128✔
734
  tDecoderClear(&dc);
39,128✔
735
  metaULock(ctx->pMeta);
39,128✔
736

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

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

752
  metaRLock(ctx->pMeta);
1,462,925✔
753
  while (1) {
17,936✔
754
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
1,480,581✔
755
      metaDebug("tmqsnap get uid info end");
10,007✔
756
      goto END;
10,007✔
757
    }
758
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
1,470,516✔
759
    TSDB_CHECK_NULL(uidTmp, code, lino, END, terrno);
1,470,216✔
760
    ctx->index++;
1,470,216✔
761
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
1,470,516✔
762
    TSDB_CHECK_NULL(idInfo, code, lino, END, terrno);
1,470,854✔
763

764
    if (MoveToPosition(ctx, idInfo->version, *uidTmp) != 0) {
1,470,854✔
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,471,154✔
770
    TSDB_CHECK_CODE(code, lino, END);
1,470,854✔
771
    SMetaEntry me = {0};
1,470,854✔
772
    tDecoderInit(&dc, pVal, vLen);
1,470,854✔
773
    code = metaDecodeEntry(&dc, &me);
1,470,854✔
774
    TSDB_CHECK_CODE(code, lino, END);
1,468,254✔
775
    metaDebug("tmqsnap get uid info uid:%" PRIi64 " name:%s index:%d", me.uid, me.name, ctx->index - 1);
1,468,254✔
776

777
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
1,468,254✔
778
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
1,470,017✔
779
      STableInfoForChildTable* data =
780
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
289,513✔
781
      TSDB_CHECK_NULL(data, code, lino, END, terrno);
290,963✔
782
      result->suid = me.ctbEntry.suid;
290,963✔
783
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
581,626✔
784
      if (data->pExtSchemas != NULL) {
290,663✔
785
        result->pExtSchemas = taosMemoryMalloc(sizeof(SExtSchema) * data->schemaRow->nCols);
8,079✔
786
        TSDB_CHECK_NULL(result->pExtSchemas, code, lino, END, terrno);
8,079✔
787
        memcpy(result->pExtSchemas, data->pExtSchemas, sizeof(SExtSchema) * data->schemaRow->nCols);
8,079✔
788
      }
789
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
1,179,901✔
790
      result->suid = 0;
1,161,675✔
791
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
1,161,095✔
792
      if (me.pExtSchemas != NULL) {
1,159,645✔
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,888✔
799
      tDecoderClear(&dc);
17,888✔
800
      continue;
17,936✔
801
    }
802
    result->uid = me.uid;
1,450,608✔
803
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
1,450,898✔
804
    TSDB_CHECK_NULL(result->schema, code, lino, END, TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY));
1,449,690✔
805
    break;
1,450,028✔
806
  }
807

808
END:
1,461,485✔
809
  tDecoderClear(&dc);
1,462,645✔
810
  tdbTbcClose((TBC*)ctx->pCur);
1,462,925✔
811
  ctx->pCur = NULL;
1,461,755✔
812
  metaULock(ctx->pMeta);
1,462,635✔
813

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