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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

38.82
/source/dnode/vnode/src/meta/metaOpen.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
#include "vnd.h"
18

19
#ifndef NO_UNALIGNED_ACCESS
20
#define TDB_KEY_ALIGN(k1, k2, kType)
21
#else
22
#define TDB_KEY_ALIGN(k1, k2, kType)   \
23
  kType _k1, _k2;                      \
24
  if (((uintptr_t)(k1) & 7)) {         \
25
    memcpy(&_k1, (k1), sizeof(kType)); \
26
    (k1) = &_k1;                       \
27
  }                                    \
28
  if (((uintptr_t)(k2) & 7)) {         \
29
    memcpy(&_k2, (k2), sizeof(kType)); \
30
    (k2) = &_k2;                       \
31
  }
32
#endif
33

34
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
35
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
36
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
37
int        tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
38
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
39
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
40
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
41

42
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
43
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
44

45
static void metaInitLock(SMeta *pMeta) {
53✔
46
  TdThreadRwlockAttr attr;
47
  (void)taosThreadRwlockAttrInit(&attr);
53✔
48
  (void)taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
53✔
49
  (void)taosThreadRwlockInit(&pMeta->lock, &attr);
53✔
50
  (void)taosThreadRwlockAttrDestroy(&attr);
53✔
51
  return;
53✔
52
}
53
static void metaDestroyLock(SMeta *pMeta) { (void)taosThreadRwlockDestroy(&pMeta->lock); }
53✔
54

55
static void metaCleanup(SMeta **ppMeta);
56

57
static void doScan(SMeta *pMeta) {
×
58
  TBC    *cursor = NULL;
×
59
  int32_t code;
60

61
  // open file to write
62
  char path[TSDB_FILENAME_LEN] = {0};
×
63
  snprintf(path, TSDB_FILENAME_LEN - 1, "%s%s", pMeta->path, TD_DIRSEP "scan.txt");
×
64
  TdFilePtr fp = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
×
65
  if (fp == NULL) {
×
66
    metaError("failed to open file:%s, reason:%s", path, tstrerror(terrno));
×
67
    return;
×
68
  }
69

70
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
×
71
  if (code) {
×
72
    if (taosCloseFile(&fp) != 0) {
×
73
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
74
    }
75
    metaError("failed to open table.db cursor, reason:%s", tstrerror(terrno));
×
76
    return;
×
77
  }
78

79
  code = tdbTbcMoveToFirst(cursor);
×
80
  if (code) {
×
81
    if (taosCloseFile(&fp) != 0) {
×
82
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
83
    }
84
    tdbTbcClose(cursor);
×
85
    metaError("failed to move to first, reason:%s", tstrerror(terrno));
×
86
    return;
×
87
  }
88

89
  for (;;) {
×
90
    const void *pKey;
91
    int         kLen;
92
    const void *pVal;
93
    int         vLen;
94
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
95
      break;
×
96
    }
97

98
    // decode entry
99
    SDecoder   dc = {0};
×
100
    SMetaEntry me = {0};
×
101

102
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
×
103

104
    if (metaDecodeEntry(&dc, &me) < 0) {
×
105
      tDecoderClear(&dc);
×
106
      break;
×
107
    }
108

109
    // skip deleted entry
110
    if (tdbTbGet(pMeta->pUidIdx, &me.uid, sizeof(me.uid), NULL, NULL) == 0) {
×
111
      // print entry
112
      char buf[1024] = {0};
×
113
      if (me.type == TSDB_SUPER_TABLE) {
×
114
        snprintf(buf, sizeof(buf) - 1, "type: super table, version:%" PRId64 " uid: %" PRId64 " name: %s\n", me.version,
×
115
                 me.uid, me.name);
116

117
      } else if (me.type == TSDB_CHILD_TABLE) {
×
118
        snprintf(buf, sizeof(buf) - 1,
×
119
                 "type: child table, version:%" PRId64 " uid: %" PRId64 " name: %s suid:%" PRId64 "\n", me.version,
120
                 me.uid, me.name, me.ctbEntry.suid);
121
      } else {
122
        snprintf(buf, sizeof(buf) - 1, "type: normal table, version:%" PRId64 " uid: %" PRId64 " name: %s\n",
×
123
                 me.version, me.uid, me.name);
124
      }
125

126
      if (taosWriteFile(fp, buf, strlen(buf)) < 0) {
×
127
        metaError("failed to write file:%s, reason:%s", path, tstrerror(terrno));
×
128
        tDecoderClear(&dc);
×
129
        break;
×
130
      }
131
    }
132

133
    tDecoderClear(&dc);
×
134

135
    if (tdbTbcMoveToNext(cursor) < 0) {
×
136
      break;
×
137
    }
138
  }
139

140
  tdbTbcClose(cursor);
×
141

142
  // close file
143
  if (taosFsyncFile(fp) < 0) {
×
144
    metaError("failed to fsync file:%s, reason:%s", path, tstrerror(terrno));
×
145
  }
146
  if (taosCloseFile(&fp) < 0) {
×
147
    metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
148
  }
149
}
150

151
int32_t metaOpenImpl(SVnode *pVnode, SMeta **ppMeta, const char *metaDir, int8_t rollback) {
53✔
152
  SMeta  *pMeta = NULL;
53✔
153
  int32_t code = 0;
53✔
154
  int32_t lino = 0;
53✔
155
  int32_t offset;
156
  int32_t pathLen = 0;
53✔
157
  char    path[TSDB_FILENAME_LEN] = {0};
53✔
158
  char    indexFullPath[128] = {0};
53✔
159

160
  // create handle
161
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
53✔
162
  offset = strlen(path);
53✔
163
  snprintf(path + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
53✔
164

165
  if (strncmp(metaDir, VNODE_META_TMP_DIR, strlen(VNODE_META_TMP_DIR)) == 0) {
53!
166
    taosRemoveDir(path);
×
167
  }
168

169
  pathLen = strlen(path) + 1;
53✔
170
  if ((pMeta = taosMemoryCalloc(1, sizeof(*pMeta) + pathLen)) == NULL) {
53!
171
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
172
  }
173

174
  metaInitLock(pMeta);
53✔
175

176
  pMeta->path = (char *)&pMeta[1];
53✔
177
  tstrncpy(pMeta->path, path, pathLen);
53✔
178
  int32_t ret = taosRealPath(pMeta->path, NULL, strlen(path) + 1);
53✔
179

180
  pMeta->pVnode = pVnode;
53✔
181

182
  // create path if not created yet
183
  code = taosMkDir(pMeta->path);
53✔
184
  TSDB_CHECK_CODE(code, lino, _exit);
53!
185

186
  // open env
187
  code = tdbOpen(pMeta->path, pVnode->config.szPage, pVnode->config.szCache, &pMeta->pEnv, rollback,
53✔
188
                 pVnode->config.tdbEncryptAlgorithm, pVnode->config.tdbEncryptKey);
53✔
189
  TSDB_CHECK_CODE(code, lino, _exit);
53!
190

191
  // open pTbDb
192
  code = tdbTbOpen("table.db", sizeof(STbDbKey), -1, tbDbKeyCmpr, pMeta->pEnv, &pMeta->pTbDb, 0);
53✔
193
  TSDB_CHECK_CODE(code, lino, _exit);
53!
194

195
  // open pSkmDb
196
  code = tdbTbOpen("schema.db", sizeof(SSkmDbKey), -1, skmDbKeyCmpr, pMeta->pEnv, &pMeta->pSkmDb, 0);
53✔
197
  TSDB_CHECK_CODE(code, lino, _exit);
53!
198

199
  // open pUidIdx
200
  code = tdbTbOpen("uid.idx", sizeof(tb_uid_t), sizeof(SUidIdxVal), uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pUidIdx, 0);
53✔
201
  TSDB_CHECK_CODE(code, lino, _exit);
53!
202

203
  // open pNameIdx
204
  code = tdbTbOpen("name.idx", -1, sizeof(tb_uid_t), NULL, pMeta->pEnv, &pMeta->pNameIdx, 0);
53✔
205
  TSDB_CHECK_CODE(code, lino, _exit);
53!
206

207
  // open pCtbIdx
208
  code = tdbTbOpen("ctb.idx", sizeof(SCtbIdxKey), -1, ctbIdxKeyCmpr, pMeta->pEnv, &pMeta->pCtbIdx, 0);
53✔
209
  TSDB_CHECK_CODE(code, lino, _exit);
53!
210

211
  // open pSuidIdx
212
  code = tdbTbOpen("suid.idx", sizeof(tb_uid_t), 0, uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pSuidIdx, 0);
53✔
213
  TSDB_CHECK_CODE(code, lino, _exit);
53!
214

215
  (void)tsnprintf(indexFullPath, sizeof(indexFullPath), "%s/%s", pMeta->path, "invert");
53✔
216
  ret = taosMkDir(indexFullPath);
53✔
217

218
  SIndexOpts opts = {.cacheSize = 8 * 1024 * 1024};
53✔
219
  code = indexOpen(&opts, indexFullPath, (SIndex **)&pMeta->pTagIvtIdx);
53✔
220
  TSDB_CHECK_CODE(code, lino, _exit);
53!
221

222
  code = tdbTbOpen("tag.idx", -1, 0, tagIdxKeyCmpr, pMeta->pEnv, &pMeta->pTagIdx, 0);
53✔
223
  TSDB_CHECK_CODE(code, lino, _exit);
53!
224

225
  // open pTtlMgr ("ttlv1.idx")
226
  char logPrefix[128] = {0};
53✔
227
  (void)tsnprintf(logPrefix, sizeof(logPrefix), "vgId:%d", TD_VID(pVnode));
53✔
228
  code = ttlMgrOpen(&pMeta->pTtlMgr, pMeta->pEnv, 0, logPrefix, tsTtlFlushThreshold);
53✔
229
  TSDB_CHECK_CODE(code, lino, _exit);
53!
230

231
  // open pSmaIdx
232
  code = tdbTbOpen("sma.idx", sizeof(SSmaIdxKey), 0, smaIdxKeyCmpr, pMeta->pEnv, &pMeta->pSmaIdx, 0);
53✔
233
  TSDB_CHECK_CODE(code, lino, _exit);
53!
234

235
  // idx table create time
236
  code = tdbTbOpen("ctime.idx", sizeof(SBtimeIdxKey), 0, btimeIdxCmpr, pMeta->pEnv, &pMeta->pBtimeIdx, 0);
53✔
237
  TSDB_CHECK_CODE(code, lino, _exit);
53!
238

239
  // idx num of col, normal table only
240
  code = tdbTbOpen("ncol.idx", sizeof(SNcolIdxKey), 0, ncolIdxCmpr, pMeta->pEnv, &pMeta->pNcolIdx, 0);
53✔
241
  TSDB_CHECK_CODE(code, lino, _exit);
53!
242

243
  code = tdbTbOpen("stream.task.db", sizeof(int64_t), -1, taskIdxKeyCmpr, pMeta->pEnv, &pMeta->pStreamDb, 0);
53✔
244
  TSDB_CHECK_CODE(code, lino, _exit);
53!
245

246
  code = metaCacheOpen(pMeta);
53✔
247
  TSDB_CHECK_CODE(code, lino, _exit);
53!
248

249
  code = metaInitTbFilterCache(pMeta);
53✔
250
  TSDB_CHECK_CODE(code, lino, _exit);
53!
251

252
#if 0
253
  // Do NOT remove this code, it is used to do debug stuff
254
  doScan(pMeta);
255
#endif
256

257
_exit:
53✔
258
  if (code) {
53!
259
    metaError("vgId:%d %s failed at %s:%d since %s, path:%s", TD_VID(pVnode), __func__, __FILE__, lino, tstrerror(code),
×
260
              path);
261
    metaCleanup(&pMeta);
×
262
    *ppMeta = NULL;
×
263
  } else {
264
    metaDebug("vgId:%d %s success", TD_VID(pVnode), __func__);
53✔
265
    *ppMeta = pMeta;
53✔
266
  }
267
  TAOS_RETURN(code);
53✔
268
}
269

270
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
159✔
271
  vnodeGetPrimaryPath(pVnode, false, fname, TSDB_FILENAME_LEN);
159✔
272
  int32_t offset = strlen(fname);
159✔
273
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
159✔
274
}
159✔
275

276
bool generateNewMeta = false;
277

278
static int32_t metaGenerateNewMeta(SMeta **ppMeta) {
×
279
  SMeta  *pNewMeta = NULL;
×
280
  SMeta  *pMeta = *ppMeta;
×
281
  SVnode *pVnode = pMeta->pVnode;
×
282

283
  metaInfo("vgId:%d start to generate new meta", TD_VID(pMeta->pVnode));
×
284

285
  // Open a new meta for organization
286
  int32_t code = metaOpenImpl(pMeta->pVnode, &pNewMeta, VNODE_META_TMP_DIR, false);
×
287
  if (code) {
×
288
    return code;
×
289
  }
290

291
  code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL);
×
292
  if (code) {
×
293
    return code;
×
294
  }
295

296
#if 1
297
  // i == 0, scan super table
298
  // i == 1, scan normal table and child table
299
  for (int i = 0; i < 2; i++) {
×
300
    TBC    *uidCursor = NULL;
×
301
    int32_t counter = 0;
×
302

303
    code = tdbTbcOpen(pMeta->pUidIdx, &uidCursor, NULL);
×
304
    if (code) {
×
305
      metaError("vgId:%d failed to open uid index cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
×
306
      return code;
×
307
    }
308

309
    code = tdbTbcMoveToFirst(uidCursor);
×
310
    if (code) {
×
311
      metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
×
312
      tdbTbcClose(uidCursor);
×
313
      return code;
×
314
    }
315

316
    for (;;) {
×
317
      const void *pKey;
318
      int         kLen;
319
      const void *pVal;
320
      int         vLen;
321

322
      if (tdbTbcGet(uidCursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
323
        break;
×
324
      }
325

326
      tb_uid_t    uid = *(tb_uid_t *)pKey;
×
327
      SUidIdxVal *pUidIdxVal = (SUidIdxVal *)pVal;
×
328
      if ((i == 0 && (pUidIdxVal->suid && pUidIdxVal->suid == uid))          // super table
×
329
          || (i == 1 && (pUidIdxVal->suid == 0 || pUidIdxVal->suid != uid))  // normal table and child table
×
330
      ) {
331
        counter++;
×
332
        if (i == 0) {
×
333
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, "super", uid);
×
334
        } else {
335
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter,
×
336
                   pUidIdxVal->suid == 0 ? "normal" : "child", uid);
337
        }
338

339
        // fetch table entry
340
        void *value = NULL;
×
341
        int   valueSize = 0;
×
342
        if (tdbTbGet(pMeta->pTbDb,
×
343
                     &(STbDbKey){
×
344
                         .version = pUidIdxVal->version,
×
345
                         .uid = uid,
346
                     },
347
                     sizeof(uid), &value, &valueSize) == 0) {
348
          SDecoder   dc = {0};
×
349
          SMetaEntry me = {0};
×
350
          tDecoderInit(&dc, value, valueSize);
×
351
          if (metaDecodeEntry(&dc, &me) == 0) {
×
352
            if (me.type == TSDB_CHILD_TABLE &&
×
353
                tdbTbGet(pMeta->pUidIdx, &me.ctbEntry.suid, sizeof(me.ctbEntry.suid), NULL, NULL) != 0) {
×
354
              metaError("vgId:%d failed to get super table uid:%" PRId64 " for child table uid:%" PRId64,
×
355
                        TD_VID(pVnode), me.ctbEntry.suid, uid);
356
            } else if (metaHandleEntry2(pNewMeta, &me) != 0) {
×
357
              metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid);
×
358
            }
359
          }
360
          tDecoderClear(&dc);
×
361
        }
362
        tdbFree(value);
×
363
      }
364

365
      code = tdbTbcMoveToNext(uidCursor);
×
366
      if (code) {
×
367
        metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
×
368
        return code;
×
369
      }
370
    }
371

372
    tdbTbcClose(uidCursor);
×
373
  }
374
#else
375
  TBC *cursor = NULL;
376

377
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
378
  if (code) {
379
    metaError("vgId:%d failed to open table.db cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
380
    return code;
381
  }
382

383
  code = tdbTbcMoveToFirst(cursor);
384
  if (code) {
385
    metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
386
    tdbTbcClose(cursor);
387
    return code;
388
  }
389

390
  while (true) {
391
    const void *pKey;
392
    int         kLen;
393
    const void *pVal;
394
    int         vLen;
395

396
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
397
      break;
398
    }
399

400
    STbDbKey  *pKeyEntry = (STbDbKey *)pKey;
401
    SDecoder   dc = {0};
402
    SMetaEntry me = {0};
403

404
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
405
    if (metaDecodeEntry(&dc, &me) < 0) {
406
      tDecoderClear(&dc);
407
      break;
408
    }
409

410
    if (metaHandleEntry2(pNewMeta, &me) != 0) {
411
      metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), pKeyEntry->uid);
412
      tDecoderClear(&dc);
413
      break;
414
    }
415
    tDecoderClear(&dc);
416

417
    code = tdbTbcMoveToNext(cursor);
418
    if (code) {
419
      metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
420
      break;
421
    }
422
  }
423

424
  tdbTbcClose(cursor);
425

426
#endif
427

428
  code = metaCommit(pNewMeta, pNewMeta->txn);
×
429
  if (code) {
×
430
    metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
431
    return code;
×
432
  }
433

434
  code = metaFinishCommit(pNewMeta, pNewMeta->txn);
×
435
  if (code) {
×
436
    metaError("vgId:%d failed to finish commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
437
    return code;
×
438
  }
439

440
  if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) {
×
441
    metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
442
  }
443
  metaClose(&pNewMeta);
×
444
  metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode));
×
445

446
  // Commit the new metadata
447
  char metaDir[TSDB_FILENAME_LEN] = {0};
×
448
  char metaTempDir[TSDB_FILENAME_LEN] = {0};
×
449
  char metaBackupDir[TSDB_FILENAME_LEN] = {0};
×
450

451
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
×
452
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
×
453
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
×
454

455
  metaClose(ppMeta);
×
456
  if (taosRenameFile(metaDir, metaBackupDir) != 0) {
×
457
    metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
458
    return terrno;
×
459
  }
460

461
  // rename the new meta to old meta
462
  if (taosRenameFile(metaTempDir, metaDir) != 0) {
×
463
    metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
464
    return terrno;
×
465
  }
466

467
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false);
×
468
  if (code) {
×
469
    metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
470
    return code;
×
471
  }
472

473
  metaInfo("vgId:%d successfully opened new meta", TD_VID(pVnode));
×
474

475
  return 0;
×
476
}
477

478
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
53✔
479
  int32_t code = TSDB_CODE_SUCCESS;
53✔
480
  char    metaDir[TSDB_FILENAME_LEN] = {0};
53✔
481
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
53✔
482
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
53✔
483

484
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
53✔
485
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
53✔
486
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
53✔
487

488
  bool metaExists = taosCheckExistFile(metaDir);
53✔
489
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
53✔
490
  bool metaTempExists = taosCheckExistFile(metaTempDir);
53✔
491

492
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
53!
493
      || (metaBackupExists && !metaExists && !metaTempExists)  //
53!
494
      || (metaBackupExists && metaExists && metaTempExists)    //
53!
495
  ) {
496
    metaError("vgId:%d, invalid meta state, please check!", TD_VID(pVnode));
×
497
    TAOS_RETURN(TSDB_CODE_FAILED);
×
498
  } else if (!metaBackupExists && metaExists && metaTempExists) {
53!
499
    taosRemoveDir(metaTempDir);
×
500
  } else if (metaBackupExists && !metaExists && metaTempExists) {
53!
501
    code = taosRenameFile(metaTempDir, metaDir);
×
502
    if (code) {
×
503
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
504
      TAOS_RETURN(code);
×
505
    }
506
    taosRemoveDir(metaBackupDir);
×
507
  } else if (metaBackupExists && metaExists && !metaTempExists) {
53!
508
    taosRemoveDir(metaBackupDir);
×
509
  }
510

511
  // Do open meta
512
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
53✔
513
  if (code) {
53!
514
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
515
    TAOS_RETURN(code);
×
516
  }
517

518
  if (generateNewMeta) {
53!
519
    code = metaGenerateNewMeta(ppMeta);
×
520
    if (code) {
×
521
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
522
      TAOS_RETURN(code);
×
523
    }
524
  }
525

526
  return TSDB_CODE_SUCCESS;
53✔
527
}
528

529
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
53✔
530
  int32_t code = TSDB_CODE_SUCCESS;
53✔
531
  int32_t lino;
532
  SMeta  *pMeta = *ppMeta;
53✔
533

534
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
53!
535
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
536
    TSDB_CHECK_CODE(code, lino, _exit);
×
537

538
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
539
    TSDB_CHECK_CODE(code, lino, _exit);
×
540

541
    code = metaCommit(pMeta, pMeta->txn);
×
542
    TSDB_CHECK_CODE(code, lino, _exit);
×
543
  }
544

545
_exit:
53✔
546
  if (code) {
53!
547
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
548
    metaCleanup(ppMeta);
×
549
  }
550
  return code;
53✔
551
}
552

553
void metaClose(SMeta **ppMeta) {
53✔
554
  metaCleanup(ppMeta);
53✔
555
  return;
53✔
556
}
557

558
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
×
559
  int32_t code = 0;
×
560
  metaWLock(pMeta);
×
561
  code = tdbAlter(pMeta->pEnv, nPage);
×
562
  metaULock(pMeta);
×
563

564
  if (code) {
×
565
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
566
  }
567
  return code;
×
568
}
569

570
void metaRLock(SMeta *pMeta) {
100,147✔
571
  metaTrace("meta rlock %p", &pMeta->lock);
100,147!
572
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
100,147!
573
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
574
  }
575
}
100,150✔
576

577
void metaWLock(SMeta *pMeta) {
357✔
578
  metaTrace("meta wlock %p", &pMeta->lock);
357!
579
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
357!
580
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
581
  }
582
}
357✔
583

584
void metaULock(SMeta *pMeta) {
100,506✔
585
  metaTrace("meta ulock %p", &pMeta->lock);
100,506!
586
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
100,506!
587
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
588
  }
589
}
100,511✔
590

591
static void metaCleanup(SMeta **ppMeta) {
53✔
592
  SMeta *pMeta = *ppMeta;
53✔
593
  if (pMeta) {
53!
594
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
53!
595
    if (pMeta->pEnv) metaAbort(pMeta);
53!
596
    if (pMeta->pCache) metaCacheClose(pMeta);
53!
597
#ifdef BUILD_NO_CALL
598
    if (pMeta->pIdx) metaCloseIdx(pMeta);
599
#endif
600
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
53!
601
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
53!
602
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
53!
603
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
53!
604
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
53!
605
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
53!
606
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
53!
607
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
53!
608
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
53!
609
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
53!
610
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
53!
611
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
53!
612
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
53!
613
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
53!
614
    metaDestroyLock(pMeta);
53✔
615

616
    taosMemoryFreeClear(*ppMeta);
53!
617
  }
618
}
53✔
619

620
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
35,292✔
621
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
35,292✔
622
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
35,292✔
623

624
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
625

626
  if (pTbDbKey1->version > pTbDbKey2->version) {
35,292✔
627
    return 1;
5,059✔
628
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
30,233✔
629
    return -1;
1,188✔
630
  }
631

632
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
29,045!
633
    return 1;
×
634
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
29,045!
635
    return -1;
×
636
  }
637

638
  return 0;
29,045✔
639
}
640

641
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
382✔
642
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
382✔
643
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
382✔
644

645
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
646

647
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
382✔
648
    return 1;
202✔
649
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
180✔
650
    return -1;
52✔
651
  }
652

653
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
128✔
654
    return 1;
20✔
655
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
108!
656
    return -1;
×
657
  }
658

659
  return 0;
108✔
660
}
661

662
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
13,771✔
663
  tb_uid_t uid1 = taosGetInt64Aligned((int64_t*)pKey1);
13,771✔
664
  tb_uid_t uid2 = taosGetInt64Aligned((int64_t*)pKey2);
13,771✔
665

666
  if (uid1 > uid2) {
13,771✔
667
    return 1;
1,723✔
668
  } else if (uid1 < uid2) {
12,048✔
669
    return -1;
881✔
670
  }
671

672
  return 0;
11,167✔
673
}
674

675
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
9,676✔
676
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
9,676✔
677
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
9,676✔
678

679
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
680

681
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
9,676✔
682
    return 1;
241✔
683
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
9,435✔
684
    return -1;
87✔
685
  }
686

687
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
9,348✔
688
    return 1;
121✔
689
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
9,227✔
690
    return -1;
9,223✔
691
  }
692

693
  return 0;
4✔
694
}
695

696
int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
210✔
697
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
210✔
698
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
210✔
699
  tb_uid_t    uid1 = 0, uid2 = 0;
210✔
700
  int         c;
701

702
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
703

704
  // compare suid
705
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
210✔
706
    return 1;
39✔
707
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
171✔
708
    return -1;
41✔
709
  }
710

711
  // compare column id
712
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
130!
713
    return 1;
×
714
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
130!
715
    return -1;
×
716
  }
717

718
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
130!
719
    metaError("meta/open: incorrect tag idx type.");
×
720
    return TSDB_CODE_FAILED;
×
721
  }
722

723
  // check NULL, NULL is always the smallest
724
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
130!
725
    return -1;
×
726
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
130!
727
    return 1;
×
728
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
130!
729
    // all not NULL, compr tag vals
730
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
130✔
731
    if (func == NULL) {
130!
732
      metaError("meta/open: %s", terrstr());
×
733
      return TSDB_CODE_FAILED;
×
734
    }
735
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
130✔
736
    if (c) return c;
130✔
737
  }
738

739
  // both null or tag values are equal, then continue to compare uids
740
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
5!
741
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
×
742
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
×
743
  } else {
744
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
5✔
745
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
5✔
746
  }
747

748
  // compare uid
749
  if (uid1 < uid2) {
5✔
750
    return -1;
4✔
751
  } else if (uid1 > uid2) {
1!
752
    return 1;
1✔
753
  } else {
754
    return 0;
×
755
  }
756

757
  return 0;
758
}
759

760
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
182✔
761
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
182✔
762
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
182✔
763

764
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
765

766
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
182!
767
    return 1;
182✔
768
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
×
769
    return -1;
×
770
  }
771

772
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
×
773
    return 1;
×
774
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
×
775
    return -1;
×
776
  }
777

778
  return 0;
×
779
}
780

781
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
782
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
×
783
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
×
784

785
  TDB_KEY_ALIGN(pNcolIdxKey1, pNcolIdxKey2, SNcolIdxKey);
786

787
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
×
788
    return 1;
×
789
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
×
790
    return -1;
×
791
  }
792

793
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
×
794
    return 1;
×
795
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
×
796
    return -1;
×
797
  }
798

799
  return 0;
×
800
}
801

802
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
803
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
804
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
805

806
  TDB_KEY_ALIGN(pSmaIdxKey1, pSmaIdxKey2, SSmaIdxKey);
807

808
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
809
    return 1;
×
810
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
811
    return -1;
×
812
  }
813

814
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
815
    return 1;
×
816
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
817
    return -1;
×
818
  }
819

820
  return 0;
×
821
}
822

823
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
824
  int32_t uid1 = *(int32_t *)pKey1;
×
825
  int32_t uid2 = *(int32_t *)pKey2;
×
826

827
  if (uid1 > uid2) {
×
828
    return 1;
×
829
  } else if (uid1 < uid2) {
×
830
    return -1;
×
831
  }
832

833
  return 0;
×
834
}
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