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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/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

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

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

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

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

UNCOV
48
_exit:
×
UNCOV
49
  if (code) {
×
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 {
UNCOV
54
    metaInfo("vgId:%d, %s success", TD_VID(pMeta->pVnode), __func__);
×
UNCOV
55
    *ppReader = pReader;
×
56
  }
UNCOV
57
  return code;
×
58
}
59

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

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

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

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

UNCOV
87
    if (key.version < pReader->sver) {
×
88
      if (tdbTbcMoveToNext(pReader->pTbc) != 0) {
×
89
        metaTrace("vgId:%d, vnode snapshot meta read data done", TD_VID(pReader->pMeta->pVnode));
×
90
      }
91
      continue;
×
92
    }
93

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

UNCOV
99
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + nData);
×
UNCOV
100
    if (*ppData == NULL) {
×
101
      code = terrno;
×
102
      goto _exit;
×
103
    }
104

UNCOV
105
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
×
UNCOV
106
    pHdr->type = SNAP_DATA_META;
×
UNCOV
107
    pHdr->size = nData;
×
UNCOV
108
    memcpy(pHdr->data, pData, nData);
×
109

UNCOV
110
    metaDebug("vgId:%d, vnode snapshot meta read data, version:%" PRId64 " uid:%" PRId64 " blockLen:%d",
×
111
              TD_VID(pReader->pMeta->pVnode), key.version, key.uid, nData);
112

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

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

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

UNCOV
134
int32_t metaSnapWriterOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapWriter** ppWriter) {
×
UNCOV
135
  int32_t          code = 0;
×
136
  int32_t          lino;
137
  SMetaSnapWriter* pWriter;
138

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

UNCOV
148
  code = metaBegin(pMeta, META_BEGIN_HEAP_NIL);
×
UNCOV
149
  TSDB_CHECK_CODE(code, lino, _exit);
×
150

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

UNCOV
163
int32_t metaSnapWriterClose(SMetaSnapWriter** ppWriter, int8_t rollback) {
×
UNCOV
164
  int32_t          code = 0;
×
UNCOV
165
  SMetaSnapWriter* pWriter = *ppWriter;
×
166

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

UNCOV
182
  return code;
×
183

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

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

UNCOV
196
  tDecoderInit(pDecoder, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
×
UNCOV
197
  code = metaDecodeEntry(pDecoder, &metaEntry);
×
UNCOV
198
  TSDB_CHECK_CODE(code, lino, _exit);
×
199

UNCOV
200
  metaHandleSyncEntry(pMeta, &metaEntry);
×
201

UNCOV
202
_exit:
×
UNCOV
203
  if (code) {
×
204
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
205
  }
UNCOV
206
  tDecoderClear(pDecoder);
×
UNCOV
207
  return code;
×
208
}
209

210
typedef struct STableInfoForChildTable {
211
  char*           tableName;
212
  SSchemaWrapper* schemaRow;
213
  SSchemaWrapper* tagRow;
214
} STableInfoForChildTable;
215

UNCOV
216
static void destroySTableInfoForChildTable(void* data) {
×
UNCOV
217
  STableInfoForChildTable* pData = (STableInfoForChildTable*)data;
×
UNCOV
218
  taosMemoryFree(pData->tableName);
×
UNCOV
219
  tDeleteSchemaWrapper(pData->schemaRow);
×
UNCOV
220
  tDeleteSchemaWrapper(pData->tagRow);
×
UNCOV
221
}
×
222

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

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

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

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

300
END:
×
301
  destroySTableInfoForChildTable(&dataTmp);
×
302
  return TAOS_GET_TERRNO(code);
×
303
  ;
304
}
305

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

UNCOV
324
  ctx->suidInfo = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
×
UNCOV
325
  if (ctx->suidInfo == NULL) {
×
326
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
327
  }
UNCOV
328
  taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable);
×
329

UNCOV
330
  ctx->index = 0;
×
UNCOV
331
  ctx->idList = taosArrayInit(100, sizeof(int64_t));
×
UNCOV
332
  if (ctx->idList == NULL) {
×
333
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
334
    ;
335
  }
UNCOV
336
  void* pKey = NULL;
×
UNCOV
337
  void* pVal = NULL;
×
UNCOV
338
  int   vLen = 0, kLen = 0;
×
339

UNCOV
340
  metaDebug("tmqsnap init snapVersion:%" PRIi64, ctx->snapVersion);
×
UNCOV
341
  int32_t code = MoveToFirst(ctx);
×
UNCOV
342
  if (code != 0) {
×
343
    return code;
×
344
  }
UNCOV
345
  while (1) {
×
UNCOV
346
    int32_t ret = tdbTbcNext((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
×
UNCOV
347
    if (ret < 0) break;
×
UNCOV
348
    STbDbKey* tmp = (STbDbKey*)pKey;
×
UNCOV
349
    if (tmp->version > ctx->snapVersion) break;
×
350

UNCOV
351
    SIdInfo* idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
×
UNCOV
352
    if (idData) {
×
UNCOV
353
      continue;
×
354
    }
355

UNCOV
356
    if (tdbTbGet(ctx->pMeta->pUidIdx, &tmp->uid, sizeof(tb_uid_t), NULL, NULL) <
×
357
        0) {  // check if table exist for now, need optimize later
UNCOV
358
      continue;
×
359
    }
360

UNCOV
361
    SDecoder   dc = {0};
×
UNCOV
362
    SMetaEntry me = {0};
×
UNCOV
363
    tDecoderInit(&dc, pVal, vLen);
×
UNCOV
364
    ret = metaDecodeEntry(&dc, &me);
×
UNCOV
365
    if (ret < 0) {
×
366
      tDecoderClear(&dc);
×
367
      return TAOS_GET_TERRNO(ret);
×
368
    }
UNCOV
369
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
×
UNCOV
370
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
×
UNCOV
371
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
×
UNCOV
372
        tDecoderClear(&dc);
×
UNCOV
373
        continue;
×
374
      }
375
    }
376

UNCOV
377
    if (taosArrayPush(ctx->idList, &tmp->uid) == NULL) {
×
378
      tDecoderClear(&dc);
×
379
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
380
    }
UNCOV
381
    metaDebug("tmqsnap init idlist name:%s, uid:%" PRIi64, me.name, tmp->uid);
×
UNCOV
382
    tDecoderClear(&dc);
×
383

UNCOV
384
    SIdInfo info = {0};
×
UNCOV
385
    if (taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo)) != 0) {
×
386
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
387
    }
388
  }
UNCOV
389
  taosHashClear(ctx->idVersion);
×
390

UNCOV
391
  code = MoveToSnapShotVersion(ctx);
×
UNCOV
392
  if (code != 0) {
×
393
    return code;
×
394
  }
UNCOV
395
  while (1) {
×
UNCOV
396
    int32_t ret = tdbTbcPrev((TBC*)ctx->pCur, &pKey, &kLen, &pVal, &vLen);
×
UNCOV
397
    if (ret < 0) break;
×
398

UNCOV
399
    STbDbKey* tmp = (STbDbKey*)pKey;
×
UNCOV
400
    SIdInfo*  idData = (SIdInfo*)taosHashGet(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t));
×
UNCOV
401
    if (idData) {
×
UNCOV
402
      continue;
×
403
    }
UNCOV
404
    SIdInfo info = {.version = tmp->version, .index = 0};
×
UNCOV
405
    ret = taosHashPut(ctx->idVersion, &tmp->uid, sizeof(tb_uid_t), &info, sizeof(SIdInfo));
×
UNCOV
406
    if (ret != 0) {
×
407
      return TAOS_GET_TERRNO(ret);
×
408
    }
409

UNCOV
410
    SDecoder   dc = {0};
×
UNCOV
411
    SMetaEntry me = {0};
×
UNCOV
412
    tDecoderInit(&dc, pVal, vLen);
×
UNCOV
413
    ret = metaDecodeEntry(&dc, &me);
×
UNCOV
414
    if (ret < 0) {
×
415
      tDecoderClear(&dc);
×
416
      return TAOS_GET_TERRNO(ret);
×
417
    }
418

UNCOV
419
    if (ctx->subType == TOPIC_SUB_TYPE__TABLE) {
×
UNCOV
420
      if ((me.uid != ctx->suid && me.type == TSDB_SUPER_TABLE) ||
×
UNCOV
421
          (me.ctbEntry.suid != ctx->suid && me.type == TSDB_CHILD_TABLE)) {
×
UNCOV
422
        tDecoderClear(&dc);
×
UNCOV
423
        continue;
×
424
      }
425
    }
426

UNCOV
427
    if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
×
UNCOV
428
        (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
×
UNCOV
429
      ret = saveSuperTableInfoForChildTable(&me, ctx->suidInfo);
×
UNCOV
430
      if (ret != 0) {
×
431
        tDecoderClear(&dc);
×
432
        return ret;
×
433
      }
434
    }
UNCOV
435
    tDecoderClear(&dc);
×
436
  }
437

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

UNCOV
449
    idData->index = i;
×
UNCOV
450
    metaDebug("tmqsnap init idVersion uid:%" PRIi64 " version:%" PRIi64 " index:%d", *uid, idData->version,
×
451
              idData->index);
452
  }
453

UNCOV
454
  tdbFree(pKey);
×
UNCOV
455
  tdbFree(pVal);
×
UNCOV
456
  return TDB_CODE_SUCCESS;
×
457
}
458

UNCOV
459
void destroySnapContext(SSnapContext* ctx) {
×
UNCOV
460
  tdbTbcClose((TBC*)ctx->pCur);
×
UNCOV
461
  taosArrayDestroy(ctx->idList);
×
UNCOV
462
  taosHashCleanup(ctx->idVersion);
×
UNCOV
463
  taosHashCleanup(ctx->suidInfo);
×
UNCOV
464
  taosMemoryFree(ctx);
×
UNCOV
465
}
×
466

UNCOV
467
static int32_t buildNormalChildTableInfo(SVCreateTbReq* req, void** pBuf, int32_t* contLen) {
×
UNCOV
468
  int32_t            ret = 0;
×
UNCOV
469
  SVCreateTbBatchReq reqs = {0};
×
470

UNCOV
471
  reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
×
UNCOV
472
  if (NULL == reqs.pArray) {
×
473
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
474
    goto end;
×
475
  }
UNCOV
476
  if (taosArrayPush(reqs.pArray, req) == NULL) {
×
477
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
478
    goto end;
×
479
  }
UNCOV
480
  reqs.nReqs = 1;
×
481

UNCOV
482
  tEncodeSize(tEncodeSVCreateTbBatchReq, &reqs, *contLen, ret);
×
UNCOV
483
  if (ret < 0) {
×
484
    ret = TAOS_GET_TERRNO(ret);
×
485
    goto end;
×
486
  }
UNCOV
487
  *contLen += sizeof(SMsgHead);
×
UNCOV
488
  *pBuf = taosMemoryMalloc(*contLen);
×
UNCOV
489
  if (NULL == *pBuf) {
×
490
    ret = TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
491
    goto end;
×
492
  }
UNCOV
493
  SEncoder coder = {0};
×
UNCOV
494
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
×
UNCOV
495
  ret = tEncodeSVCreateTbBatchReq(&coder, &reqs);
×
UNCOV
496
  tEncoderClear(&coder);
×
497

UNCOV
498
  if (ret < 0) {
×
499
    taosMemoryFreeClear(*pBuf);
×
500
    ret = TAOS_GET_TERRNO(ret);
×
501
    goto end;
×
502
  }
503

UNCOV
504
end:
×
UNCOV
505
  taosArrayDestroy(reqs.pArray);
×
UNCOV
506
  return ret;
×
507
}
508

UNCOV
509
static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* contLen) {
×
UNCOV
510
  int32_t ret = 0;
×
UNCOV
511
  tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret);
×
UNCOV
512
  if (ret < 0) {
×
513
    return TAOS_GET_TERRNO(ret);
×
514
  }
515

UNCOV
516
  *contLen += sizeof(SMsgHead);
×
UNCOV
517
  *pBuf = taosMemoryMalloc(*contLen);
×
UNCOV
518
  if (NULL == *pBuf) {
×
519
    return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
520
  }
521

UNCOV
522
  SEncoder encoder = {0};
×
UNCOV
523
  tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen);
×
UNCOV
524
  ret = tEncodeSVCreateStbReq(&encoder, req);
×
UNCOV
525
  tEncoderClear(&encoder);
×
UNCOV
526
  if (ret < 0) {
×
527
    taosMemoryFreeClear(*pBuf);
×
528
    return TAOS_GET_TERRNO(ret);
×
529
  }
UNCOV
530
  return 0;
×
531
}
532

UNCOV
533
int32_t setForSnapShot(SSnapContext* ctx, int64_t uid) {
×
UNCOV
534
  if (uid == 0) {
×
UNCOV
535
    ctx->index = 0;
×
UNCOV
536
    return 0;
×
537
  }
538

UNCOV
539
  SIdInfo* idInfo = (SIdInfo*)taosHashGet(ctx->idVersion, &uid, sizeof(tb_uid_t));
×
UNCOV
540
  if (idInfo == NULL) {
×
541
    return terrno;
×
542
  }
543

UNCOV
544
  ctx->index = idInfo->index;
×
545

UNCOV
546
  return 0;
×
547
}
548

UNCOV
549
void taosXSetTablePrimaryKey(SSnapContext* ctx, int64_t uid) {
×
UNCOV
550
  bool            ret = false;
×
UNCOV
551
  SSchemaWrapper* schema = metaGetTableSchema(ctx->pMeta, uid, -1, 1);
×
UNCOV
552
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
×
553
    ret = true;
×
554
  }
555
  tDeleteSchemaWrapper(schema);
UNCOV
556
  ctx->hasPrimaryKey = ret;
×
UNCOV
557
}
×
558

UNCOV
559
bool taosXGetTablePrimaryKey(SSnapContext* ctx) { return ctx->hasPrimaryKey; }
×
560

UNCOV
561
int32_t getTableInfoFromSnapshot(SSnapContext* ctx, void** pBuf, int32_t* contLen, int16_t* type, int64_t* uid) {
×
UNCOV
562
  int32_t ret = 0;
×
UNCOV
563
  void*   pKey = NULL;
×
UNCOV
564
  void*   pVal = NULL;
×
UNCOV
565
  int     vLen = 0, kLen = 0;
×
566

567
  while (1) {
×
UNCOV
568
    if (ctx->index >= taosArrayGetSize(ctx->idList)) {
×
UNCOV
569
      metaDebug("tmqsnap get meta end");
×
UNCOV
570
      ctx->index = 0;
×
UNCOV
571
      ctx->queryMeta = 0;  // change to get data
×
UNCOV
572
      return 0;
×
573
    }
574

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

UNCOV
587
    *uid = *uidTmp;
×
UNCOV
588
    ret = MoveToPosition(ctx, idInfo->version, *uidTmp);
×
UNCOV
589
    if (ret == 0) {
×
UNCOV
590
      break;
×
591
    }
592
    metaDebug("tmqsnap get meta not exist uid:%" PRIi64 " version:%" PRIi64, *uid, idInfo->version);
×
593
  }
594

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

UNCOV
610
  if ((ctx->subType == TOPIC_SUB_TYPE__DB && me.type == TSDB_SUPER_TABLE) ||
×
UNCOV
611
      (ctx->subType == TOPIC_SUB_TYPE__TABLE && me.uid == ctx->suid)) {
×
UNCOV
612
    SVCreateStbReq req = {0};
×
UNCOV
613
    req.name = me.name;
×
UNCOV
614
    req.suid = me.uid;
×
UNCOV
615
    req.schemaRow = me.stbEntry.schemaRow;
×
UNCOV
616
    req.schemaTag = me.stbEntry.schemaTag;
×
UNCOV
617
    req.schemaRow.version = 1;
×
UNCOV
618
    req.schemaTag.version = 1;
×
UNCOV
619
    req.colCmpr = me.colCmpr;
×
620

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

UNCOV
633
    SVCreateTbReq req = {0};
×
634

UNCOV
635
    req.type = TSDB_CHILD_TABLE;
×
UNCOV
636
    req.name = me.name;
×
UNCOV
637
    req.uid = me.uid;
×
UNCOV
638
    req.commentLen = -1;
×
UNCOV
639
    req.ctb.suid = me.ctbEntry.suid;
×
UNCOV
640
    req.ctb.tagNum = data->tagRow->nCols;
×
UNCOV
641
    req.ctb.stbName = data->tableName;
×
642

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

UNCOV
705
END:
×
UNCOV
706
  tDecoderClear(&dc);
×
UNCOV
707
  return ret;
×
708
}
709

UNCOV
710
int32_t getMetaTableInfoFromSnapshot(SSnapContext* ctx, SMetaTableInfo* result) {
×
UNCOV
711
  void* pKey = NULL;
×
UNCOV
712
  void* pVal = NULL;
×
713
  int   vLen, kLen;
714

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

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

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