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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

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

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

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

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

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

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

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

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

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

81
  tDecoderClear(&decoder);
11,852✔
82
  return 0;
11,852✔
83
}
84

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

94
  *ppData = NULL;
5,985✔
95
  while (pReader->iLoop < 2) {
12,029✔
96
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData) != 0 || ((STbDbKey*)pKey)->version > pReader->ever) {
11,970!
97
      pReader->iLoop++;
118✔
98

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

245
  metaHandleSyncEntry(pMeta, &metaEntry);
5,918✔
246

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

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

261
static void destroySTableInfoForChildTable(void* data) {
469✔
262
  STableInfoForChildTable* pData = (STableInfoForChildTable*)data;
469✔
263
  taosMemoryFree(pData->tableName);
469!
264
  tDeleteSchemaWrapper(pData->schemaRow);
470!
265
  tDeleteSchemaWrapper(pData->tagRow);
470!
266
}
470✔
267

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

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

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

317
static int32_t saveSuperTableInfoForChildTable(SMetaEntry* me, SHashObj* suidInfo) {
470✔
318
  STableInfoForChildTable* data = (STableInfoForChildTable*)taosHashGet(suidInfo, &me->uid, sizeof(tb_uid_t));
470✔
319
  if (data) {
470!
320
    return 0;
×
321
  }
322
  int32_t                 code = 0;
470✔
323
  STableInfoForChildTable dataTmp = {0};
470✔
324
  dataTmp.tableName = taosStrdup(me->name);
470!
325
  if (dataTmp.tableName == NULL) {
470!
326
    code = terrno;
×
327
    goto END;
×
328
  }
329
  dataTmp.schemaRow = tCloneSSchemaWrapper(&me->stbEntry.schemaRow);
470!
330
  if (dataTmp.schemaRow == NULL) {
470!
331
    code = TSDB_CODE_OUT_OF_MEMORY;
×
332
    goto END;
×
333
  }
334
  dataTmp.tagRow = tCloneSSchemaWrapper(&me->stbEntry.schemaTag);
470!
335
  if (dataTmp.tagRow == NULL) {
470!
336
    code = TSDB_CODE_OUT_OF_MEMORY;
×
337
    goto END;
×
338
  }
339
  code = taosHashPut(suidInfo, &me->uid, sizeof(tb_uid_t), &dataTmp, sizeof(STableInfoForChildTable));
470✔
340
  if (code != 0) {
470!
341
    goto END;
×
342
  }
343
  return 0;
470✔
344

345
END:
×
346
  destroySTableInfoForChildTable(&dataTmp);
×
347
  return TAOS_GET_TERRNO(code);
×
348
  ;
349
}
350

351
int32_t buildSnapContext(SVnode* pVnode, int64_t snapVersion, int64_t suid, int8_t subType, int8_t withMeta,
305✔
352
                         SSnapContext** ctxRet) {
353
  SSnapContext* ctx = taosMemoryCalloc(1, sizeof(SSnapContext));
305!
354
  if (ctx == NULL) {
305!
355
    return terrno;
×
356
  }
357
  *ctxRet = ctx;
305✔
358
  ctx->pMeta = pVnode->pMeta;
305✔
359
  ctx->snapVersion = snapVersion;
305✔
360
  ctx->suid = suid;
305✔
361
  ctx->subType = subType;
305✔
362
  ctx->queryMeta = withMeta;
305✔
363
  ctx->withMeta = withMeta;
305✔
364
  ctx->idVersion = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
305✔
365
  if (ctx->idVersion == NULL) {
305!
366
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
367
  }
368

369
  ctx->suidInfo = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
305✔
370
  if (ctx->suidInfo == NULL) {
305!
371
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
372
  }
373
  taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable);
305✔
374

375
  ctx->index = 0;
305✔
376
  ctx->idList = taosArrayInit(100, sizeof(int64_t));
305✔
377
  if (ctx->idList == NULL) {
305!
378
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
379
    ;
380
  }
381
  void* pKey = NULL;
305✔
382
  void* pVal = NULL;
305✔
383
  int   vLen = 0, kLen = 0;
305✔
384

385
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
305✔
386
  int32_t code = MoveToFirst(ctx);
305✔
387
  if (code != 0) {
305!
388
    return code;
×
389
  }
390
  while (1) {
7,359✔
391
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
7,664✔
392
    if (ret < 0) break;
7,547✔
393
    STbDbKey* tmp = (STbDbKey*)pKey;
7,246✔
394
    if (tmp->version > ctx->snapVersion) break;
7,246✔
395

396
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
7,242✔
397
    if (idData) {
7,230✔
398
      continue;
574✔
399
    }
400

401
    if (tdbTbGet(ctx->pMeta->pUidIdx, &tmp->uid, sizeof(tb_uid_t), NULL, NULL) <
6,894✔
402
        0) {  // check if table exist for now, need optimize later
403
      continue;
225✔
404
    }
405

406
    SDecoder   dc = {0};
6,791✔
407
    SMetaEntry me = {0};
6,791✔
408
    tDecoderInit(&dc, pVal, vLen);
6,791✔
409
    ret = metaDecodeEntry(&dc, &me);
6,794✔
410
    if (ret < 0) {
6,619!
411
      tDecoderClear(&dc);
×
412
      return TAOS_GET_TERRNO(ret);
×
413
    }
414
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
6,619✔
415
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
795✔
416
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
791✔
417
        tDecoderClear(&dc);
13✔
418
        continue;
13✔
419
      }
420
    }
421

422
    if (taosArrayPush(ctx->idList, &tmp->uid) == NULL) {
13,210!
423
      tDecoderClear(&dc);
×
424
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
425
    }
426
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
6,604✔
427
    tDecoderClear(&dc);
6,604✔
428

429
    SIdInfo info = {0};
6,742✔
430
    if (taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo)) != 0) {
6,742!
431
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
432
    }
433
  }
434
  taosHashClear(ctx->idVersion);
305✔
435

436
  code = MoveToSnapShotVersion(ctx);
305✔
437
  if (code != 0) {
304!
438
    return code;
×
439
  }
440
  while (1) {
7,334✔
441
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
7,638✔
442
    if (ret < 0) break;
7,556✔
443

444
    STbDbKey* tmp = (STbDbKey*)pKey;
7,251✔
445
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
7,251✔
446
    if (idData) {
7,185✔
447
      continue;
510✔
448
    }
449
    SIdInfo info = {.version = tmp->version, .index = 0};
6,688✔
450
    ret = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
6,688✔
451
    if (ret != 0) {
6,818!
452
      return TAOS_GET_TERRNO(ret);
×
453
    }
454

455
    SDecoder   dc = {0};
6,818✔
456
    SMetaEntry me = {0};
6,818✔
457
    tDecoderInit(&dc, pVal, vLen);
6,818✔
458
    ret = metaDecodeEntry(&dc, &me);
6,807✔
459
    if (ret < 0) {
6,588!
460
      tDecoderClear(&dc);
×
461
      return TAOS_GET_TERRNO(ret);
×
462
    }
463

464
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
6,588✔
465
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
794✔
466
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
790✔
467
        tDecoderClear(&dc);
13✔
468
        continue;
13✔
469
      }
470
    }
471

472
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
6,575✔
473
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
6,169✔
474
      ret = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
470✔
475
      if (ret != 0) {
470!
476
        tDecoderClear(&dc);
×
477
        return ret;
×
478
      }
479
    }
480
    tDecoderClear(&dc);
6,575✔
481
  }
482

483
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
7,078✔
484
    int64_t* uid = taosArrayGet(ctx->idList, i);
6,772✔
485
    if (uid == NULL) {
6,771!
486
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
487
    }
488
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
6,771✔
489
    if (!idData) {
6,773!
490
      metaError("meta/snap: null idData");
×
491
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
492
    }
493

494
    idData->index = i;
6,773✔
495
    metaDebug("tmqsnap init idVersion uid:%" PRIi64 " version:%" PRIi64 " index:%d", *uid, idData->version,
6,773✔
496
              idData->index);
497
  }
498

499
  tdbFree(pKey);
305✔
500
  tdbFree(pVal);
305✔
501
  return TDB_CODE_SUCCESS;
305✔
502
}
503

504
void destroySnapContext(SSnapContext* ctx) {
305✔
505
  tdbTbcClose((TBC*)ctx->pCur);
305✔
506
  taosArrayDestroy(ctx->idList);
305✔
507
  taosHashCleanup(ctx->idVersion);
305✔
508
  taosHashCleanup(ctx->suidInfo);
305✔
509
  taosMemoryFree(ctx);
305!
510
}
305✔
511

512
static int32_t buildNormalChildTableInfo(SVCreateTbReq* req, void** pBuf, int32_t* contLen) {
87✔
513
  int32_t            ret = 0;
87✔
514
  SVCreateTbBatchReq reqs = {0};
87✔
515

516
  reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
87✔
517
  if (NULL == reqs.pArray) {
87!
518
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
519
    goto end;
×
520
  }
521
  if (taosArrayPush(reqs.pArray, req) == NULL) {
174!
522
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
523
    goto end;
×
524
  }
525
  reqs.nReqs = 1;
87✔
526

527
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqs, *contLen, ret);
87!
528
  if (ret < 0) {
87!
529
    ret = TAOS_GET_TERRNO(ret);
×
530
    goto end;
×
531
  }
532
  *contLen += sizeof(SMsgHead);
87✔
533
  *pBuf = taosMemoryMalloc(*contLen);
87!
534
  if (NULL == *pBuf) {
87!
535
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
536
    goto end;
×
537
  }
538
  SEncoder coder = {0};
87✔
539
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
87✔
540
  ret = tEncodeSVCreateTbBatchReq(&coder, &reqs);
87✔
541
  tEncoderClear(&coder);
87✔
542

543
  if (ret < 0) {
87!
544
    taosMemoryFreeClear(*pBuf);
×
545
    ret = TAOS_GET_TERRNO(ret);
×
546
    goto end;
×
547
  }
548

549
end:
87✔
550
  taosArrayDestroy(reqs.pArray);
87✔
551
  return ret;
87✔
552
}
553

554
static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* contLen) {
40✔
555
  int32_t ret = 0;
40✔
556
  tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret);
40!
557
  if (ret < 0) {
40!
558
    return TAOS_GET_TERRNO(ret);
×
559
  }
560

561
  *contLen += sizeof(SMsgHead);
40✔
562
  *pBuf = taosMemoryMalloc(*contLen);
40!
563
  if (NULL == *pBuf) {
40!
564
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
565
  }
566

567
  SEncoder encoder = {0};
40✔
568
  tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
40✔
569
  ret = tEncodeSVCreateStbReq(&encoder, req);
40✔
570
  tEncoderClear(&encoder);
40✔
571
  if (ret < 0) {
40!
572
    taosMemoryFreeClear(*pBuf);
×
573
    return TAOS_GET_TERRNO(ret);
×
574
  }
575
  return 0;
40✔
576
}
577

578
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
2,126✔
579
  if (uid == 0) {
2,126✔
580
    ctx->index = 0;
42✔
581
    return 0;
42✔
582
  }
583

584
  SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, &uid, sizeof(tb_uid_t));
2,084✔
585
  if (idInfo == NULL) {
2,083!
586
    return terrno;
×
587
  }
588

589
  ctx->index = idInfo->index;
2,083✔
590

591
  return 0;
2,083✔
592
}
593

594
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
2,106✔
595
  bool            ret = false;
2,106✔
596
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1);
2,106✔
597
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
2,106!
598
    ret = true;
×
599
  }
600
  tDeleteSchemaWrapper(schema);
601
  ctx->hasPrimaryKey = ret;
2,105✔
602
}
2,105✔
603

604
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
4,227✔
605

606
int32_t getTableInfoFromSnapshot(SSnapContext* ctx, void** pBuf, int32_t* contLen, int16_t* type, int64_t* uid) {
143✔
607
  int32_t ret = 0;
143✔
608
  void*   pKey = NULL;
143✔
609
  void*   pVal = NULL;
143✔
610
  int     vLen = 0, kLen = 0;
143✔
611

612
  while (1) {
×
613
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
143✔
614
      metaDebug("tmqsnap get meta end");
16!
615
      ctx->index = 0;
16✔
616
      ctx->queryMeta = 0;  // change to get data
16✔
617
      return 0;
16✔
618
    }
619

620
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
127✔
621
    if (uidTmp == NULL) {
127!
622
      metaError("tmqsnap get meta null uid");
×
623
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
624
    }
625
    ctx->index++;
127✔
626
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
127✔
627
    if (!idInfo) {
127!
628
      metaError("meta/snap: null idInfo");
×
629
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
630
    }
631

632
    *uid = *uidTmp;
127✔
633
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
127✔
634
    if (ret == 0) {
127!
635
      break;
127✔
636
    }
637
    metaDebug("tmqsnap get meta not exist uid:%" PRIi64 " version:%" PRIi64, *uid, idInfo->version);
×
638
  }
639

640
  ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
127✔
641
  if (ret < 0) {
127!
642
    return TAOS_GET_TERRNO(ret);
×
643
  }
644
  SDecoder   dc = {0};
127✔
645
  SMetaEntry me = {0};
127✔
646
  tDecoderInit(&dc, pVal, vLen);
127✔
647
  ret = metaDecodeEntry(&dc, &me);
127✔
648
  if (ret < 0) {
127!
649
    tDecoderClear(&dc);
×
650
    ret = TAOS_GET_TERRNO(ret);
×
651
    goto END;
×
652
  }
653
  metaDebug("tmqsnap get meta uid:%" PRIi64 " name:%s index:%d", *uid, me.name, ctx->index - 1);
127!
654

655
  if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
127✔
656
      (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
89!
657
    SVCreateStbReq req = {0};
40✔
658
    req.name = me.name;
40✔
659
    req.suid = me.uid;
40✔
660
    req.schemaRow = me.stbEntry.schemaRow;
40✔
661
    req.schemaTag = me.stbEntry.schemaTag;
40✔
662
    req.schemaRow.version = 1;
40✔
663
    req.schemaTag.version = 1;
40✔
664
    req.colCmpr = me.colCmpr;
40✔
665

666
    ret = buildSuperTableInfo(&req, pBuf, contLen);
40✔
667
    *type = TDMT_VND_CREATE_STB;
40✔
668
  } else if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
87!
669
             (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
87!
670
    STableInfoForChildTable* data =
671
        (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
77✔
672
    if (!data) {
77!
673
      metaError("meta/snap: null data");
×
674
      ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
675
      goto END;
×
676
    }
677

678
    SVCreateTbReq req = {0};
77✔
679

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

688
    SArray* tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
77✔
689
    if (tagName == NULL) {
77!
690
      metaError("meta/snap: init tag name failed.");
×
691
      ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
692
      goto END;
×
693
    }
694
    STag* p = (STag*)me.ctbEntry.pTags;
77✔
695
    if (tTagIsJson(p)) {
77✔
696
      if (p->nTag != 0) {
12✔
697
        SSchema* schema = &data->tagRow->pSchema[0];
6✔
698
        if (taosArrayPush(tagName, schema->name) == NULL) {
12!
699
          ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
700
          taosArrayDestroy(tagName);
×
701
          goto END;
×
702
        }
703
      }
704
    } else {
705
      SArray* pTagVals = NULL;
65✔
706
      ret = tTagToValArray((const STag*)p, &pTagVals);
65✔
707
      if (ret != 0) {
65!
708
        metaError("meta/snap: tag to val array failed.");
×
709
        taosArrayDestroy(pTagVals);
×
710
        taosArrayDestroy(tagName);
×
711
        goto END;
×
712
      }
713
      int16_t nCols = taosArrayGetSize(pTagVals);
65✔
714
      for (int j = 0; j < nCols; ++j) {
230✔
715
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
165✔
716
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
678!
717
          SSchema* schema = &data->tagRow->pSchema[i];
513✔
718
          if (schema->colId == pTagVal->cid) {
513✔
719
            if (taosArrayPush(tagName, schema->name) == NULL) {
330!
720
              ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
721
              taosArrayDestroy(pTagVals);
×
722
              taosArrayDestroy(tagName);
×
723
              goto END;
×
724
            }
725
          }
726
        }
727
      }
728
      taosArrayDestroy(pTagVals);
65✔
729
    }
730
    req.ctb.pTag = me.ctbEntry.pTags;
77✔
731
    req.ctb.tagName = tagName;
77✔
732
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
77✔
733
    *type = TDMT_VND_CREATE_TABLE;
77✔
734
    taosArrayDestroy(tagName);
77✔
735
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
10!
736
    SVCreateTbReq req = {0};
10✔
737
    req.type = TSDB_NORMAL_TABLE;
10✔
738
    req.name = me.name;
10✔
739
    req.uid = me.uid;
10✔
740
    req.commentLen = -1;
10✔
741
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
10✔
742
    req.colCmpr = me.colCmpr;
10✔
743
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
10✔
744
    *type = TDMT_VND_CREATE_TABLE;
10✔
745
  } else {
746
    metaError("meta/snap: invalid topic sub type: %" PRId8 " get meta from snap failed.", ctx->subType);
×
747
    ret = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
×
748
  }
749

750
END:
127✔
751
  tDecoderClear(&dc);
127✔
752
  return ret;
127✔
753
}
754

755
int32_t getMetaTableInfoFromSnapshot(SSnapContext* ctx, SMetaTableInfo* result) {
4,208✔
756
  void* pKey = NULL;
4,208✔
757
  void* pVal = NULL;
4,208✔
758
  int   vLen, kLen;
759

760
  while (1) {
44✔
761
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
4,252✔
762
      metaDebug("tmqsnap get uid info end");
31✔
763
      return 0;
31✔
764
    }
765
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
4,221✔
766
    if (uidTmp == NULL) {
4,223!
767
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
768
    }
769
    ctx->index++;
4,223✔
770
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
4,223✔
771
    if (!idInfo) {
4,223!
772
      metaError("meta/snap: null idInfo");
×
773
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
774
    }
775

776
    int32_t ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
4,223✔
777
    if (ret != 0) {
4,220!
778
      metaDebug("tmqsnap getMetaTableInfoFromSnapshot not exist uid:%" PRIi64 " version:%" PRIi64, *uidTmp,
×
779
                idInfo->version);
780
      continue;
44✔
781
    }
782
    ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
4,220✔
783
    if (ret != 0) {
4,219!
784
      return TAOS_GET_TERRNO(ret);
×
785
    }
786
    SDecoder   dc = {0};
4,219✔
787
    SMetaEntry me = {0};
4,219✔
788
    tDecoderInit(&dc, pVal, vLen);
4,219✔
789
    ret = metaDecodeEntry(&dc, &me);
4,223✔
790
    if (ret != 0) {
4,232!
791
      tDecoderClear(&dc);
×
792
      return TAOS_GET_TERRNO(ret);
×
793
    }
794
    metaDebug("tmqsnap get uid info uid:%" PRIi64 " name:%s index:%d", me.uid, me.name, ctx->index - 1);
4,232✔
795

796
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
4,232✔
797
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
4,045!
798
      STableInfoForChildTable* data =
799
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
187✔
800
      if (data == NULL) {
187!
801
        tDecoderClear(&dc);
×
802
        metaError("meta/snap: null data");
×
803
        return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
804
      }
805
      result->suid = me.ctbEntry.suid;
187✔
806
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
374!
807
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
4,045✔
808
      result->suid = 0;
4,001!
809
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
4,005✔
810
    } else {
811
      metaDebug("tmqsnap get uid continue");
44✔
812
      tDecoderClear(&dc);
44✔
813
      continue;
44✔
814
    }
815
    result->uid = me.uid;
4,192✔
816
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
4,192✔
817
    tDecoderClear(&dc);
4,192✔
818
    if (result->schema == NULL) {
4,192!
819
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
820
    }
821
    break;
4,192✔
822
  }
823
  return 0;
4,192✔
824
}
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