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

taosdata / TDengine / #4103

17 May 2025 02:18AM UTC coverage: 63.264% (+0.4%) from 62.905%
#4103

push

travis-ci

web-flow
Merge pull request #31110 from taosdata/3.0

merge 3.0

158149 of 318142 branches covered (49.71%)

Branch coverage included in aggregate %.

3 of 5 new or added lines in 1 file covered. (60.0%)

1725 existing lines in 138 files now uncovered.

243642 of 316962 relevant lines covered (76.87%)

16346281.8 hits per line

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

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

160
  // create handle
161
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
15,703✔
162
  offset = strlen(path);
15,722✔
163
  snprintf(path + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
15,722✔
164

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

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

174
  metaInitLock(pMeta);
15,708✔
175

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

180
  pMeta->pVnode = pVnode;
15,702✔
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

249
  code = metaInitTbFilterCache(pMeta);
15,727✔
250
  TSDB_CHECK_CODE(code, lino, _exit);
15,727!
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:
15,727✔
258
  if (code) {
15,727!
259
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
260
    metaCleanup(&pMeta);
×
261
    *ppMeta = NULL;
×
262
  } else {
263
    metaDebug("vgId:%d %s success", TD_VID(pVnode), __func__);
15,727✔
264
    *ppMeta = pMeta;
15,727✔
265
  }
266
  TAOS_RETURN(code);
15,727✔
267
}
268

269
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
47,141✔
270
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, fname, TSDB_FILENAME_LEN);
47,141✔
271
  int32_t offset = strlen(fname);
47,172✔
272
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
47,172✔
273
}
47,172✔
274

275
bool generateNewMeta = false;
276

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

423
  tdbTbcClose(cursor);
424

425
#endif
426

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

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

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

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

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

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

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

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

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

474
  return 0;
×
475
}
476

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

483
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
15,729✔
484
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
15,723✔
485
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
15,718✔
486

487
  bool metaExists = taosCheckExistFile(metaDir);
15,729✔
488
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
15,720✔
489
  bool metaTempExists = taosCheckExistFile(metaTempDir);
15,727✔
490

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

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

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

525
  return TSDB_CODE_SUCCESS;
15,727✔
526
}
527

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

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

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

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

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

552
void metaClose(SMeta **ppMeta) {
15,716✔
553
  metaCleanup(ppMeta);
15,716✔
554
  return;
15,729✔
555
}
556

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

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

569
void metaRLock(SMeta *pMeta) {
67,838,970✔
570
  metaTrace("meta rlock %p", &pMeta->lock);
67,838,970✔
571
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
67,841,364!
572
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
573
  }
574
}
67,863,811✔
575

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

583
void metaULock(SMeta *pMeta) {
68,213,027✔
584
  metaTrace("meta ulock %p", &pMeta->lock);
68,213,027✔
585
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
68,213,361!
586
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
587
  }
588
}
68,224,015✔
589

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

615
    taosMemoryFreeClear(*ppMeta);
15,729!
616
  }
617
}
15,729✔
618

619
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
186,516,628✔
620
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
186,516,628✔
621
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
186,516,628✔
622

623
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
624

625
  if (pTbDbKey1->version > pTbDbKey2->version) {
186,516,628✔
626
    return 1;
86,856,416✔
627
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
99,660,212✔
628
    return -1;
61,963,305✔
629
  }
630

631
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
37,696,907✔
632
    return 1;
527,967✔
633
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
37,168,940✔
634
    return -1;
396,043✔
635
  }
636

637
  return 0;
36,772,897✔
638
}
639

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

644
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
645

646
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
4,446,123✔
647
    return 1;
2,015,116✔
648
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
2,431,007✔
649
    return -1;
668,263✔
650
  }
651

652
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
1,762,744✔
653
    return 1;
86,225✔
654
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
1,676,519✔
655
    return -1;
254,262✔
656
  }
657

658
  return 0;
1,422,257✔
659
}
660

661
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
150,376,717✔
662
  tb_uid_t uid1 = taosGetInt64Aligned((int64_t*)pKey1);
150,376,717✔
663
  tb_uid_t uid2 = taosGetInt64Aligned((int64_t*)pKey2);
150,376,717✔
664

665
  if (uid1 > uid2) {
150,376,717✔
666
    return 1;
71,594,553✔
667
  } else if (uid1 < uid2) {
78,782,164✔
668
    return -1;
63,433,452✔
669
  }
670

671
  return 0;
15,348,712✔
672
}
673

674
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
11,525,567✔
675
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
11,525,567✔
676
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
11,525,567✔
677

678
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
679

680
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
11,525,567✔
681
    return 1;
4,508,933✔
682
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
7,016,634✔
683
    return -1;
1,678,952✔
684
  }
685

686
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
5,337,682✔
687
    return 1;
506,719✔
688
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
4,830,963✔
689
    return -1;
4,571,007✔
690
  }
691

692
  return 0;
259,956✔
693
}
694

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

701
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
702

703
  // compare suid
704
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
1,320,004✔
705
    return 1;
72,598✔
706
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
1,247,406✔
707
    return -1;
10,152✔
708
  }
709

710
  // compare column id
711
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
1,237,254✔
712
    return 1;
66,907✔
713
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
1,170,347✔
714
    return -1;
35,726✔
715
  }
716

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

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

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

747
  // compare uid
748
  if (uid1 < uid2) {
185,247✔
749
    return -1;
6,431✔
750
  } else if (uid1 > uid2) {
178,816✔
751
    return 1;
139,149✔
752
  } else {
753
    return 0;
39,667✔
754
  }
755

756
  return 0;
757
}
758

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

763
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
764

765
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
625,249✔
766
    return 1;
499,467✔
767
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
125,782✔
768
    return -1;
25,306✔
769
  }
770

771
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
100,476✔
772
    return 1;
75,619✔
773
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
24,857✔
774
    return -1;
1,799✔
775
  }
776

777
  return 0;
23,058✔
778
}
779

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

784
  TDB_KEY_ALIGN(pNcolIdxKey1, pNcolIdxKey2, SNcolIdxKey);
785

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

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

798
  return 0;
×
799
}
800

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

805
  TDB_KEY_ALIGN(pSmaIdxKey1, pSmaIdxKey2, SSmaIdxKey);
806

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

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

819
  return 0;
×
820
}
821

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

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

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