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

taosdata / TDengine / #4488

12 Jul 2025 07:47AM UTC coverage: 62.207% (-0.7%) from 62.948%
#4488

push

travis-ci

web-flow
docs: update stream docs (#31822)

157961 of 324087 branches covered (48.74%)

Branch coverage included in aggregate %.

244465 of 322830 relevant lines covered (75.73%)

6561668.76 hits per line

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

44.14
/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) {
14,507✔
46
  TdThreadRwlockAttr attr;
47
  (void)taosThreadRwlockAttrInit(&attr);
14,507✔
48
  (void)taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
14,514✔
49
  (void)taosThreadRwlockInit(&pMeta->lock, &attr);
14,510✔
50
  (void)taosThreadRwlockAttrDestroy(&attr);
14,519✔
51
  return;
14,504✔
52
}
53
static void metaDestroyLock(SMeta *pMeta) { (void)taosThreadRwlockDestroy(&pMeta->lock); }
14,528✔
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) {
14,514✔
152
  SMeta  *pMeta = NULL;
14,514✔
153
  int32_t code = 0;
14,514✔
154
  int32_t lino = 0;
14,514✔
155
  int32_t offset;
156
  int32_t pathLen = 0;
14,514✔
157
  char    path[TSDB_FILENAME_LEN] = {0};
14,514✔
158
  char    indexFullPath[128] = {0};
14,514✔
159

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

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

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

174
  metaInitLock(pMeta);
14,512✔
175

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

180
  pMeta->pVnode = pVnode;
14,512✔
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

249
  code = metaInitTbFilterCache(pMeta);
14,527✔
250
  TSDB_CHECK_CODE(code, lino, _exit);
14,528!
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:
14,528✔
258
  if (code) {
14,528!
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__);
14,528✔
265
    *ppMeta = pMeta;
14,528✔
266
  }
267
  TAOS_RETURN(code);
14,528✔
268
}
269

270
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
43,537✔
271
  vnodeGetPrimaryPath(pVnode, false, fname, TSDB_FILENAME_LEN);
43,537✔
272
  int32_t offset = strlen(fname);
43,580✔
273
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
43,580✔
274
}
43,580✔
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) {
14,528✔
479
  int32_t code = TSDB_CODE_SUCCESS;
14,528✔
480
  char    metaDir[TSDB_FILENAME_LEN] = {0};
14,528✔
481
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
14,528✔
482
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
14,528✔
483

484
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
14,528✔
485
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
14,520✔
486
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
14,524✔
487

488
  bool metaExists = taosCheckExistFile(metaDir);
14,528✔
489
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
14,525✔
490
  bool metaTempExists = taosCheckExistFile(metaTempDir);
14,525✔
491

492
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
14,528!
493
      || (metaBackupExists && !metaExists && !metaTempExists)  //
14,528!
494
      || (metaBackupExists && metaExists && metaTempExists)    //
14,528!
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) {
14,528!
499
    taosRemoveDir(metaTempDir);
×
500
  } else if (metaBackupExists && !metaExists && metaTempExists) {
14,528!
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) {
14,528!
508
    taosRemoveDir(metaBackupDir);
×
509
  }
510

511
  // Do open meta
512
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
14,528✔
513
  if (code) {
14,527!
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) {
14,527!
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;
14,527✔
527
}
528

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

534
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
14,528!
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:
14,528✔
546
  if (code) {
14,528!
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;
14,528✔
551
}
552

553
void metaClose(SMeta **ppMeta) {
14,506✔
554
  metaCleanup(ppMeta);
14,506✔
555
  return;
14,528✔
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) {
33,096,930✔
571
  metaTrace("meta rlock %p", &pMeta->lock);
33,096,930✔
572
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
33,098,243!
573
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
574
  }
575
}
33,109,043✔
576

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

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

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

616
    taosMemoryFreeClear(*ppMeta);
14,528!
617
  }
618
}
14,526✔
619

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

624
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
625

626
  if (pTbDbKey1->version > pTbDbKey2->version) {
32,111,033✔
627
    return 1;
13,614,736✔
628
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
18,496,297✔
629
    return -1;
11,047,620✔
630
  }
631

632
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
7,448,677✔
633
    return 1;
219,355✔
634
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
7,229,322✔
635
    return -1;
167,673✔
636
  }
637

638
  return 0;
7,061,649✔
639
}
640

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

645
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
646

647
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
4,595,354✔
648
    return 1;
1,557,279✔
649
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
3,038,075✔
650
    return -1;
1,446,673✔
651
  }
652

653
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
1,591,402✔
654
    return 1;
102,924✔
655
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
1,488,478✔
656
    return -1;
172,206✔
657
  }
658

659
  return 0;
1,316,272✔
660
}
661

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

666
  if (uid1 > uid2) {
95,233,265✔
667
    return 1;
46,136,659✔
668
  } else if (uid1 < uid2) {
49,096,606✔
669
    return -1;
45,142,683✔
670
  }
671

672
  return 0;
3,953,923✔
673
}
674

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

679
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
680

681
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
3,259,712✔
682
    return 1;
923,351✔
683
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
2,336,361✔
684
    return -1;
231,622✔
685
  }
686

687
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
2,104,739✔
688
    return 1;
571,409✔
689
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
1,533,330✔
690
    return -1;
1,488,306✔
691
  }
692

693
  return 0;
45,024✔
694
}
695

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

702
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
703

704
  // compare suid
705
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
729,218✔
706
    return 1;
8,301✔
707
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
720,917✔
708
    return -1;
49,295✔
709
  }
710

711
  // compare column id
712
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
671,622✔
713
    return 1;
2,210✔
714
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
669,412✔
715
    return -1;
1,351✔
716
  }
717

718
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
668,061!
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) {
668,061✔
725
    return -1;
673✔
726
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
667,388✔
727
    return 1;
2,102✔
728
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
665,286!
729
    // all not NULL, compr tag vals
730
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
663,592✔
731
    if (func == NULL) {
663,586!
732
      metaError("meta/open: %s", terrstr());
×
733
      return TSDB_CODE_FAILED;
×
734
    }
735
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
663,586✔
736
    if (c) return c;
663,583✔
737
  }
738

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

748
  // compare uid
749
  if (uid1 < uid2) {
155,033✔
750
    return -1;
7,663✔
751
  } else if (uid1 > uid2) {
147,370✔
752
    return 1;
127,819✔
753
  } else {
754
    return 0;
19,551✔
755
  }
756

757
  return 0;
758
}
759

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

764
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
765

766
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
529,775✔
767
    return 1;
464,049✔
768
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
65,726✔
769
    return -1;
12,434✔
770
  }
771

772
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
53,292✔
773
    return 1;
34,816✔
774
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
18,476✔
775
    return -1;
291✔
776
  }
777

778
  return 0;
18,185✔
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