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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

51.94
/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
};
25

26
int32_t metaSnapReaderOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapReader** ppReader) {
1✔
27
  int32_t          code = 0;
1✔
28
  int32_t          lino;
29
  int32_t          c = 0;
1✔
30
  SMetaSnapReader* pReader = NULL;
1✔
31

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

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

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

48
_exit:
1✔
49
  if (code) {
1!
50
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
51
    metaSnapReaderClose(&pReader);
×
52
    *ppReader = NULL;
×
53
  } else {
54
    metaInfo("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
1!
55
    *ppReader = pReader;
1✔
56
  }
57
  return code;
1✔
58
}
59

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

68
int32_t metaSnapRead(SMetaSnapReader* pReader, uint8_t** ppData) {
13✔
69
  int32_t     code = 0;
13✔
70
  const void* pKey = NULL;
13✔
71
  const void* pData = NULL;
13✔
72
  int32_t     nKey = 0;
13✔
73
  int32_t     nData = 0;
13✔
74
  STbDbKey    key;
75
  SMetaInfo   info;
76

77
  *ppData = NULL;
13✔
78
  for (;;) {
×
79
    if (tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData)) {
13✔
80
      goto _exit;
1✔
81
    }
82

83
    key = ((STbDbKey*)pKey)[0];
12✔
84
    if (key.version > pReader->ever) {
12!
85
      goto _exit;
×
86
    }
87

88
    if (key.version < pReader->sver  //
12!
89
        || metaGetInfo(pReader->pMeta, key.uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
12!
90
      if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
×
91
        metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
92
      }
93
      continue;
×
94
    }
95

96
    if (!pData || !nData) {
12!
97
      metaError("meta/snap: invalide nData: %" PRId32 " meta snap read failed.", nData);
×
98
      goto _exit;
×
99
    }
100

101
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + nData);
12✔
102
    if (*ppData == NULL) {
12!
103
      code = terrno;
×
104
      goto _exit;
×
105
    }
106

107
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
12✔
108
    pHdr->type = SNAP_DATA_META;
12✔
109
    pHdr->size = nData;
12✔
110
    memcpy(pHdr->data, pData, nData);
12✔
111

112
    metaDebug("vgId:%d, vnode snapshot meta read data, version:%" PRId64 " uid:%" PRId64 " blockLen:%d",
12!
113
              TD_VID(pReader->pMeta->pVnode), key.version, key.uid, nData);
114

115
    if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
12!
116
      metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
117
    }
118
    break;
12✔
119
  }
120

121
_exit:
13✔
122
  if (code) {
13!
123
    metaError("vgId:%d, vnode snapshot meta read data failed since %s", TD_VID(pReader->pMeta->pVnode),
×
124
              tstrerror(code));
125
  }
126
  return code;
13✔
127
}
128

129
// SMetaSnapWriter ========================================
130
struct SMetaSnapWriter {
131
  SMeta*  pMeta;
132
  int64_t sver;
133
  int64_t ever;
134
};
135

136
int32_t metaSnapWriterOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapWriter** ppWriter) {
1✔
137
  int32_t          code = 0;
1✔
138
  int32_t          lino;
139
  SMetaSnapWriter* pWriter;
140

141
  // alloc
142
  pWriter = (SMetaSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
1✔
143
  if (pWriter == NULL) {
1!
144
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
145
  }
146
  pWriter->pMeta = pMeta;
1✔
147
  pWriter->sver = sver;
1✔
148
  pWriter->ever = ever;
1✔
149

150
  code = metaBegin(pMeta, META_BEGIN_HEAP_NIL);
1✔
151
  TSDB_CHECK_CODE(code, lino, _exit);
1!
152

153
_exit:
1✔
154
  if (code) {
1!
155
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
156
    taosMemoryFree(pWriter);
×
157
    *ppWriter = NULL;
×
158
  } else {
159
    metaDebug("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
1!
160
    *ppWriter = pWriter;
1✔
161
  }
162
  return code;
1✔
163
}
164

165
int32_t metaSnapWriterClose(SMetaSnapWriter** ppWriter, int8_t rollback) {
1✔
166
  int32_t          code = 0;
1✔
167
  SMetaSnapWriter* pWriter = *ppWriter;
1✔
168

169
  if (rollback) {
1!
170
    metaInfo("vgId:%d, meta snapshot writer close and rollback start ", TD_VID(pWriter->pMeta->pVnode));
×
171
    code = metaAbort(pWriter->pMeta);
×
172
    metaInfo("vgId:%d, meta snapshot writer close and rollback finished, code:0x%x", TD_VID(pWriter->pMeta->pVnode),
×
173
             code);
174
    if (code) goto _err;
×
175
  } else {
176
    code = metaCommit(pWriter->pMeta, pWriter->pMeta->txn);
1✔
177
    if (code) goto _err;
1!
178
    code = metaFinishCommit(pWriter->pMeta, pWriter->pMeta->txn);
1✔
179
    if (code) goto _err;
1!
180
  }
181
  taosMemoryFree(pWriter);
1✔
182
  *ppWriter = NULL;
1✔
183

184
  return code;
1✔
185

186
_err:
×
187
  metaError("vgId:%d, meta snapshot writer close failed since %s", TD_VID(pWriter->pMeta->pVnode), tstrerror(code));
×
188
  return code;
×
189
}
190

191
int32_t metaSnapWrite(SMetaSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
519✔
192
  int32_t    code = 0;
519✔
193
  int32_t    lino = 0;
519✔
194
  SMeta*     pMeta = pWriter->pMeta;
519✔
195
  SMetaEntry metaEntry = {0};
519✔
196
  SDecoder*  pDecoder = &(SDecoder){0};
519✔
197

198
  tDecoderInit(pDecoder, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
519✔
199
  code = metaDecodeEntry(pDecoder, &metaEntry);
519✔
200
  TSDB_CHECK_CODE(code, lino, _exit);
519!
201

202
  code = metaHandleEntry(pMeta, &metaEntry);
519✔
203
  TSDB_CHECK_CODE(code, lino, _exit);
519!
204

205
_exit:
519✔
206
  if (code) {
519!
207
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
208
  }
209
  tDecoderClear(pDecoder);
519✔
210
  return code;
519✔
211
}
212

213
typedef struct STableInfoForChildTable {
214
  char*           tableName;
215
  SSchemaWrapper* schemaRow;
216
  SSchemaWrapper* tagRow;
217
} STableInfoForChildTable;
218

219
static void destroySTableInfoForChildTable(void* data) {
9✔
220
  STableInfoForChildTable* pData = (STableInfoForChildTable*)data;
9✔
221
  taosMemoryFree(pData->tableName);
9✔
222
  tDeleteSchemaWrapper(pData->schemaRow);
9!
223
  tDeleteSchemaWrapper(pData->tagRow);
9!
224
}
9✔
225

226
static int32_t MoveToSnapShotVersion(SSnapContext* ctx) {
11✔
227
  int32_t code = 0;
11✔
228
  tdbTbcClose((TBC*)ctx->pCur);
11✔
229
  code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
11✔
230
  if (code != 0) {
11!
231
    return TAOS_GET_TERRNO(code);
×
232
  }
233
  STbDbKey key = {.version = ctx->snapVersion, .uid = INT64_MAX};
11✔
234
  int      c = 0;
11✔
235
  code = tdbTbcMoveTo((TBC*)ctx->pCur, &key, sizeof(key), &c);
11✔
236
  if (code != 0) {
11!
237
    return TAOS_GET_TERRNO(code);
×
238
  }
239
  if (c < 0) {
11!
UNCOV
240
    if (tdbTbcMoveToPrev((TBC*)ctx->pCur) != 0) {
×
241
      metaTrace("vgId:%d, vnode snapshot move to prev failed", TD_VID(ctx->pMeta->pVnode));
×
242
    }
243
  }
244
  return 0;
11✔
245
}
246

247
static int32_t MoveToPosition(SSnapContext* ctx, int64_t ver, int64_t uid) {
7✔
248
  tdbTbcClose((TBC*)ctx->pCur);
7✔
249
  int32_t code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
7✔
250
  if (code != 0) {
7!
251
    return TAOS_GET_TERRNO(code);
×
252
  }
253
  STbDbKey key = {.version = ver, .uid = uid};
7✔
254
  int      c = 0;
7✔
255
  code = tdbTbcMoveTo((TBC*)ctx->pCur, &key, sizeof(key), &c);
7✔
256
  if (code != 0) {
7!
257
    return TAOS_GET_TERRNO(code);
×
258
  }
259
  return c;
7✔
260
}
261

262
static int32_t MoveToFirst(SSnapContext* ctx) {
11✔
263
  tdbTbcClose((TBC*)ctx->pCur);
11✔
264
  int32_t code = tdbTbcOpen(ctx->pMeta->pTbDb, (TBC**)&ctx->pCur, NULL);
11✔
265
  if (code != 0) {
11!
266
    return TAOS_GET_TERRNO(code);
×
267
  }
268
  code = tdbTbcMoveToFirst((TBC*)ctx->pCur);
11✔
269
  if (code != 0) {
11!
270
    return TAOS_GET_TERRNO(code);
×
271
  }
272
  return 0;
11✔
273
}
274

275
static int32_t saveSuperTableInfoForChildTable(SMetaEntry* me, SHashObj* suidInfo) {
9✔
276
  STableInfoForChildTable* data = (STableInfoForChildTable*)taosHashGet(suidInfo, &me->uid, sizeof(tb_uid_t));
9✔
277
  if (data) {
9!
278
    return 0;
×
279
  }
280
  int32_t                 code = 0;
9✔
281
  STableInfoForChildTable dataTmp = {0};
9✔
282
  dataTmp.tableName = taosStrdup(me->name);
9✔
283
  if (dataTmp.tableName == NULL) {
9!
284
    code = terrno;
×
285
    goto END;
×
286
  }
287
  dataTmp.schemaRow = tCloneSSchemaWrapper(&me->stbEntry.schemaRow);
9!
288
  if (dataTmp.schemaRow == NULL) {
9!
289
    code = TSDB_CODE_OUT_OF_MEMORY;
×
290
    goto END;
×
291
  }
292
  dataTmp.tagRow = tCloneSSchemaWrapper(&me->stbEntry.schemaTag);
9!
293
  if (dataTmp.tagRow == NULL) {
9!
294
    code = TSDB_CODE_OUT_OF_MEMORY;
×
295
    goto END;
×
296
  }
297
  code = taosHashPut(suidInfo, &me->uid, sizeof(tb_uid_t), &dataTmp, sizeof(STableInfoForChildTable));
9✔
298
  if (code != 0) {
9!
299
    goto END;
×
300
  }
301
  return 0;
9✔
302

303
END:
×
304
  destroySTableInfoForChildTable(&dataTmp);
×
305
  return TAOS_GET_TERRNO(code);
×
306
  ;
307
}
308

309
int32_t buildSnapContext(SVnode* pVnode, int64_t snapVersion, int64_t suid, int8_t subType, int8_t withMeta,
11✔
310
                         SSnapContext** ctxRet) {
311
  SSnapContext* ctx = taosMemoryCalloc(1, sizeof(SSnapContext));
11✔
312
  if (ctx == NULL) {
11!
313
    return terrno;
×
314
  }
315
  *ctxRet = ctx;
11✔
316
  ctx->pMeta = pVnode->pMeta;
11✔
317
  ctx->snapVersion = snapVersion;
11✔
318
  ctx->suid = suid;
11✔
319
  ctx->subType = subType;
11✔
320
  ctx->queryMeta = withMeta;
11✔
321
  ctx->withMeta = withMeta;
11✔
322
  ctx->idVersion = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
11✔
323
  if (ctx->idVersion == NULL) {
11!
324
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
325
  }
326

327
  ctx->suidInfo = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
11✔
328
  if (ctx->suidInfo == NULL) {
11!
329
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
330
    ;
331
  }
332
  taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable);
11✔
333

334
  ctx->index = 0;
11✔
335
  ctx->idList = taosArrayInit(100, sizeof(int64_t));
11✔
336
  if (ctx->idList == NULL) {
11!
337
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
338
    ;
339
  }
340
  void* pKey = NULL;
11✔
341
  void* pVal = NULL;
11✔
342
  int   vLen = 0, kLen = 0;
11✔
343

344
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
11✔
345
  int32_t code = MoveToFirst(ctx);
11✔
346
  if (code != 0) {
11!
347
    return code;
×
348
  }
349
  while (1) {
275✔
350
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
286✔
351
    if (ret < 0) break;
280✔
352
    STbDbKey* tmp = (STbDbKey*)pKey;
269✔
353
    if (tmp->version > ctx->snapVersion) break;
269!
354

355
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
269✔
356
    if (idData) {
270!
357
      continue;
×
358
    }
359

360
    if (tdbTbGet(ctx->pMeta->pUidIdx, &tmp->uid, sizeof(tb_uid_t), NULL, NULL) <
270!
361
        0) {  // check if table exist for now, need optimize later
362
      continue;
×
363
    }
364

365
    SDecoder   dc = {0};
275✔
366
    SMetaEntry me = {0};
275✔
367
    tDecoderInit(&dc, pVal, vLen);
275✔
368
    ret = metaDecodeEntry(&dc, &me);
275✔
369
    if (ret < 0) {
271!
370
      tDecoderClear(&dc);
×
371
      return TAOS_GET_TERRNO(ret);
×
372
    }
373
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
271✔
374
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
25!
375
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
25!
376
        tDecoderClear(&dc);
×
377
        continue;
×
378
      }
379
    }
380

381
    if (taosArrayPush(ctx->idList, &tmp->uid) == NULL) {
542!
382
      tDecoderClear(&dc);
×
383
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
384
    }
385
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
271✔
386
    tDecoderClear(&dc);
271✔
387

388
    SIdInfo info = {0};
274✔
389
    if (taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo)) != 0) {
274!
390
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
391
    }
392
  }
393
  taosHashClear(ctx->idVersion);
11✔
394

395
  code = MoveToSnapShotVersion(ctx);
11✔
396
  if (code != 0) {
11!
397
    return code;
×
398
  }
399
  while (1) {
275✔
400
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
286✔
401
    if (ret < 0) break;
276✔
402

403
    STbDbKey* tmp = (STbDbKey*)pKey;
265✔
404
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
265✔
405
    if (idData) {
269!
406
      continue;
×
407
    }
408
    SIdInfo info = {.version = tmp->version, .index = 0};
269✔
409
    ret = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
269✔
410
    if (ret != 0) {
275!
411
      return TAOS_GET_TERRNO(ret);
×
412
    }
413

414
    SDecoder   dc = {0};
275✔
415
    SMetaEntry me = {0};
275✔
416
    tDecoderInit(&dc, pVal, vLen);
275✔
417
    ret = metaDecodeEntry(&dc, &me);
275✔
418
    if (ret < 0) {
271!
419
      tDecoderClear(&dc);
×
420
      return TAOS_GET_TERRNO(ret);
×
421
    }
422

423
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
271✔
424
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
25!
425
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
25!
426
        tDecoderClear(&dc);
×
427
        continue;
×
428
      }
429
    }
430

431
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
271✔
432
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
265✔
433
      ret = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
9✔
434
      if (ret != 0) {
9!
435
        tDecoderClear(&dc);
×
436
        return ret;
×
437
      }
438
    }
439
    tDecoderClear(&dc);
271✔
440
  }
441

442
  for (int i = 0; i < taosArrayGetSize(ctx->idList); i++) {
286✔
443
    int64_t* uid = taosArrayGet(ctx->idList, i);
275✔
444
    if (uid == NULL) {
275!
445
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
446
    }
447
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, uid, sizeof(int64_t));
275✔
448
    if (!idData) {
275!
449
      metaError("meta/snap: null idData");
×
450
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
451
    }
452

453
    idData->index = i;
275✔
454
    metaDebug("tmqsnap init idVersion uid:%" PRIi64 " version:%" PRIi64 " index:%d", *uid, idData->version,
275✔
455
              idData->index);
456
  }
457

458
  tdbFree(pKey);
11✔
459
  tdbFree(pVal);
11✔
460
  return TDB_CODE_SUCCESS;
11✔
461
}
462

463
void destroySnapContext(SSnapContext* ctx) {
11✔
464
  tdbTbcClose((TBC*)ctx->pCur);
11✔
465
  taosArrayDestroy(ctx->idList);
11✔
466
  taosHashCleanup(ctx->idVersion);
11✔
467
  taosHashCleanup(ctx->suidInfo);
11✔
468
  taosMemoryFree(ctx);
11✔
469
}
11✔
470

471
static int32_t buildNormalChildTableInfo(SVCreateTbReq* req, void** pBuf, int32_t* contLen) {
2✔
472
  int32_t            ret = 0;
2✔
473
  SVCreateTbBatchReq reqs = {0};
2✔
474

475
  reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
2✔
476
  if (NULL == reqs.pArray) {
2!
477
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
478
    goto end;
×
479
  }
480
  if (taosArrayPush(reqs.pArray, req) == NULL) {
4!
481
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
482
    goto end;
×
483
  }
484
  reqs.nReqs = 1;
2✔
485

486
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqs, *contLen, ret);
2!
487
  if (ret < 0) {
2!
488
    ret = TAOS_GET_TERRNO(ret);
×
489
    goto end;
×
490
  }
491
  *contLen += sizeof(SMsgHead);
2✔
492
  *pBuf = taosMemoryMalloc(*contLen);
2✔
493
  if (NULL == *pBuf) {
2!
494
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
495
    goto end;
×
496
  }
497
  SEncoder coder = {0};
2✔
498
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
2✔
499
  ret = tEncodeSVCreateTbBatchReq(&coder, &reqs);
2✔
500
  tEncoderClear(&coder);
2✔
501

502
  if (ret < 0) {
2!
503
    taosMemoryFreeClear(*pBuf);
×
504
    ret = TAOS_GET_TERRNO(ret);
×
505
    goto end;
×
506
  }
507

508
end:
2✔
509
  taosArrayDestroy(reqs.pArray);
2✔
510
  return ret;
2✔
511
}
512

513
static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* contLen) {
×
514
  int32_t ret = 0;
×
515
  tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret);
×
516
  if (ret < 0) {
×
517
    return TAOS_GET_TERRNO(ret);
×
518
  }
519

520
  *contLen += sizeof(SMsgHead);
×
521
  *pBuf = taosMemoryMalloc(*contLen);
×
522
  if (NULL == *pBuf) {
×
523
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
524
  }
525

526
  SEncoder encoder = {0};
×
527
  tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
×
528
  ret = tEncodeSVCreateStbReq(&encoder, req);
×
529
  tEncoderClear(&encoder);
×
530
  if (ret < 0) {
×
531
    taosMemoryFreeClear(*pBuf);
×
532
    return TAOS_GET_TERRNO(ret);
×
533
  }
534
  return 0;
×
535
}
536

537
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
6✔
538
  if (uid == 0) {
6✔
539
    ctx->index = 0;
5✔
540
    return 0;
5✔
541
  }
542

543
  SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, &uid, sizeof(tb_uid_t));
1✔
544
  if (idInfo == NULL) {
1!
545
    return terrno;
×
546
  }
547

548
  ctx->index = idInfo->index;
1✔
549

550
  return 0;
1✔
551
}
552

553
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
4✔
554
  bool            ret = false;
4✔
555
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1, NULL);
4✔
556
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
4!
557
    ret = true;
4✔
558
  }
559
  tDeleteSchemaWrapper(schema);
560
  ctx->hasPrimaryKey = ret;
4✔
561
}
4✔
562

563
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
10✔
564

565
int32_t getTableInfoFromSnapshot(SSnapContext* ctx, void** pBuf, int32_t* contLen, int16_t* type, int64_t* uid) {
4✔
566
  int32_t ret = 0;
4✔
567
  void*   pKey = NULL;
4✔
568
  void*   pVal = NULL;
4✔
569
  int     vLen = 0, kLen = 0;
4✔
570

571
  while (1) {
×
572
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
4✔
573
      metaDebug("tmqsnap get meta end");
2!
574
      ctx->index = 0;
2✔
575
      ctx->queryMeta = 0;  // change to get data
2✔
576
      return 0;
2✔
577
    }
578

579
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
2✔
580
    if (uidTmp == NULL) {
2!
581
      metaError("tmqsnap get meta null uid");
×
582
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
583
    }
584
    ctx->index++;
2✔
585
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
2✔
586
    if (!idInfo) {
2!
587
      metaError("meta/snap: null idInfo");
×
588
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
589
    }
590

591
    *uid = *uidTmp;
2✔
592
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
2✔
593
    if (ret == 0) {
2!
594
      break;
2✔
595
    }
596
    metaDebug("tmqsnap get meta not exist uid:%" PRIi64 " version:%" PRIi64, *uid, idInfo->version);
×
597
  }
598

599
  ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
2✔
600
  if (ret < 0) {
2!
601
    return TAOS_GET_TERRNO(ret);
×
602
  }
603
  SDecoder   dc = {0};
2✔
604
  SMetaEntry me = {0};
2✔
605
  tDecoderInit(&dc, pVal, vLen);
2✔
606
  ret = metaDecodeEntry(&dc, &me);
2✔
607
  if (ret < 0) {
2!
608
    tDecoderClear(&dc);
×
609
    ret = TAOS_GET_TERRNO(ret);
×
610
    goto END;
×
611
  }
612
  metaDebug("tmqsnap get meta uid:%" PRIi64 " name:%s index:%d", *uid, me.name, ctx->index - 1);
2!
613

614
  if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
2!
615
      (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
2!
616
    SVCreateStbReq req = {0};
×
617
    req.name = me.name;
×
618
    req.suid = me.uid;
×
619
    req.schemaRow = me.stbEntry.schemaRow;
×
620
    req.schemaTag = me.stbEntry.schemaTag;
×
621
    req.schemaRow.version = 1;
×
622
    req.schemaTag.version = 1;
×
623
    req.colCmpr = me.colCmpr;
×
624

625
    ret = buildSuperTableInfo(&req, pBuf, contLen);
×
626
    *type = TDMT_VND_CREATE_STB;
×
627
  } else if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
2!
628
             (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
2!
629
    STableInfoForChildTable* data =
630
        (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
×
631
    if (!data) {
×
632
      metaError("meta/snap: null data");
×
633
      ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
634
      goto END;
×
635
    }
636

637
    SVCreateTbReq req = {0};
×
638

639
    req.type = TSDB_CHILD_TABLE;
×
640
    req.name = me.name;
×
641
    req.uid = me.uid;
×
642
    req.commentLen = -1;
×
643
    req.ctb.suid = me.ctbEntry.suid;
×
644
    req.ctb.tagNum = data->tagRow->nCols;
×
645
    req.ctb.stbName = data->tableName;
×
646

647
    SArray* tagName = taosArrayInit(req.ctb.tagNum, TSDB_COL_NAME_LEN);
×
648
    if (tagName == NULL) {
×
649
      metaError("meta/snap: init tag name failed.");
×
650
      ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
651
      goto END;
×
652
    }
653
    STag* p = (STag*)me.ctbEntry.pTags;
×
654
    if (tTagIsJson(p)) {
×
655
      if (p->nTag != 0) {
×
656
        SSchema* schema = &data->tagRow->pSchema[0];
×
657
        if (taosArrayPush(tagName, schema->name) == NULL) {
×
658
          ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
659
          taosArrayDestroy(tagName);
×
660
          goto END;
×
661
        }
662
      }
663
    } else {
664
      SArray* pTagVals = NULL;
×
665
      ret = tTagToValArray((const STag*)p, &pTagVals);
×
666
      if (ret != 0) {
×
667
        metaError("meta/snap: tag to val array failed.");
×
668
        taosArrayDestroy(pTagVals);
×
669
        taosArrayDestroy(tagName);
×
670
        goto END;
×
671
      }
672
      int16_t nCols = taosArrayGetSize(pTagVals);
×
673
      for (int j = 0; j < nCols; ++j) {
×
674
        STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
×
675
        for (int i = 0; pTagVal && i < data->tagRow->nCols; i++) {
×
676
          SSchema* schema = &data->tagRow->pSchema[i];
×
677
          if (schema->colId == pTagVal->cid) {
×
678
            if (taosArrayPush(tagName, schema->name) == NULL) {
×
679
              ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
680
              taosArrayDestroy(pTagVals);
×
681
              taosArrayDestroy(tagName);
×
682
              goto END;
×
683
            }
684
          }
685
        }
686
      }
687
      taosArrayDestroy(pTagVals);
×
688
    }
689
    req.ctb.pTag = me.ctbEntry.pTags;
×
690
    req.ctb.tagName = tagName;
×
691
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
×
692
    *type = TDMT_VND_CREATE_TABLE;
×
693
    taosArrayDestroy(tagName);
×
694
  } else if (ctx->subType == TOPIC_SUB_TYPE__DB) {
2!
695
    SVCreateTbReq req = {0};
2✔
696
    req.type = TSDB_NORMAL_TABLE;
2✔
697
    req.name = me.name;
2✔
698
    req.uid = me.uid;
2✔
699
    req.commentLen = -1;
2✔
700
    req.ntb.schemaRow = me.ntbEntry.schemaRow;
2✔
701
    req.colCmpr = me.colCmpr;
2✔
702
    ret = buildNormalChildTableInfo(&req, pBuf, contLen);
2✔
703
    *type = TDMT_VND_CREATE_TABLE;
2✔
704
  } else {
705
    metaError("meta/snap: invalid topic sub type: %" PRId8 " get meta from snap failed.", ctx->subType);
×
706
    ret = TSDB_CODE_SDB_INVALID_TABLE_TYPE;
×
707
  }
708

709
END:
2✔
710
  tDecoderClear(&dc);
2✔
711
  return ret;
2✔
712
}
713

714
int32_t getMetaTableInfoFromSnapshot(SSnapContext* ctx, SMetaTableInfo* result) {
7✔
715
  void* pKey = NULL;
7✔
716
  void* pVal = NULL;
7✔
717
  int   vLen, kLen;
718

719
  while (1) {
1✔
720
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
8✔
721
      metaDebug("tmqsnap get uid info end");
3!
722
      return 0;
3✔
723
    }
724
    int64_t* uidTmp = taosArrayGet(ctx->idList, ctx->index);
5✔
725
    if (uidTmp == NULL) {
5!
726
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
727
    }
728
    ctx->index++;
5✔
729
    SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, uidTmp, sizeof(tb_uid_t));
5✔
730
    if (!idInfo) {
5!
731
      metaError("meta/snap: null idInfo");
×
732
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
733
    }
734

735
    int32_t ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
5✔
736
    if (ret != 0) {
5!
737
      metaDebug("tmqsnap getMetaTableInfoFromSnapshot not exist uid:%" PRIi64 " version:%" PRIi64, *uidTmp,
×
738
                idInfo->version);
739
      continue;
1✔
740
    }
741
    ret = tdbTbcGet((TBC*)ctx->pCur, (const void**)&pKey, &kLen, (const void**)&pVal, &vLen);
5✔
742
    if (ret != 0) {
5!
743
      return TAOS_GET_TERRNO(ret);
×
744
    }
745
    SDecoder   dc = {0};
5✔
746
    SMetaEntry me = {0};
5✔
747
    tDecoderInit(&dc, pVal, vLen);
5✔
748
    ret = metaDecodeEntry(&dc, &me);
5✔
749
    if (ret != 0) {
5!
750
      tDecoderClear(&dc);
×
751
      return TAOS_GET_TERRNO(ret);
×
752
    }
753
    metaDebug("tmqsnap get uid info uid:%" PRIi64 " name:%s index:%d", me.uid, me.name, ctx->index - 1);
5!
754

755
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_CHILD_TABLE) ||
5!
756
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.type == TSDB_CHILD_TABLE && me.ctbEntry.suid == ctx->suid)) {
5!
757
      STableInfoForChildTable* data =
758
          (STableInfoForChildTable*)taosHashGet(ctx->suidInfo, &me.ctbEntry.suid, sizeof(tb_uid_t));
1✔
759
      if (data == NULL) {
1!
760
        tDecoderClear(&dc);
×
761
        metaError("meta/snap: null data");
×
762
        return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
763
      }
764
      result->suid = me.ctbEntry.suid;
1✔
765
      result->schema = tCloneSSchemaWrapper(data->schemaRow);
2!
766
    } else if (ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_NORMAL_TABLE) {
4!
767
      result->suid = 0;
3!
768
      result->schema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
3✔
769
    } else {
770
      metaDebug("tmqsnap get uid continue");
1!
771
      tDecoderClear(&dc);
1✔
772
      continue;
1✔
773
    }
774
    result->uid = me.uid;
4✔
775
    tstrncpy(result->tbName, me.name, TSDB_TABLE_NAME_LEN);
4✔
776
    tDecoderClear(&dc);
4✔
777
    if (result->schema == NULL) {
4!
778
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
779
    }
780
    break;
4✔
781
  }
782
  return 0;
4✔
783
}
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