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

taosdata / TDengine / #4754

25 Sep 2025 05:58AM UTC coverage: 57.946% (-1.0%) from 58.977%
#4754

push

travis-ci

web-flow
enh: taos command line support '-uroot' on windows (#33055)

133189 of 293169 branches covered (45.43%)

Branch coverage included in aggregate %.

201677 of 284720 relevant lines covered (70.83%)

5398749.0 hits per line

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

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

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

165
  if (strncmp(metaDir, VNODE_META_TMP_DIR, strlen(VNODE_META_TMP_DIR)) == 0) {
12,268✔
166
    taosRemoveDir(path);
2✔
167
  }
168

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

174
  metaInitLock(pMeta);
12,266✔
175

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

180
  pMeta->pVnode = pVnode;
12,265✔
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

276
bool generateNewMeta = false;
277

278
static void metaResetStatisInfo(SMeta *pMeta) {
×
279
  pMeta->pVnode->config.vndStats.numOfSTables = 0;
×
280
  pMeta->pVnode->config.vndStats.numOfCTables = 0;
×
281
  pMeta->pVnode->config.vndStats.numOfNTables = 0;
×
282
  pMeta->pVnode->config.vndStats.numOfVTables = 0;
×
283
  pMeta->pVnode->config.vndStats.numOfVCTables = 0;
×
284
  pMeta->pVnode->config.vndStats.numOfNTimeSeries = 0;
×
285
  pMeta->pVnode->config.vndStats.numOfTimeSeries = 0;
×
286
}
×
287

288
static int32_t metaGenerateNewMeta(SMeta **ppMeta) {
×
289
  SMeta  *pNewMeta = NULL;
×
290
  SMeta  *pMeta = *ppMeta;
×
291
  SVnode *pVnode = pMeta->pVnode;
×
292

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

295
  // Reset statistics info
296
  metaResetStatisInfo(pMeta);
×
297

298
  // Open a new meta for organization
299
  int32_t code = metaOpenImpl(pMeta->pVnode, &pNewMeta, VNODE_META_TMP_DIR, false);
×
300
  if (code) {
×
301
    return code;
×
302
  }
303

304
  code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL);
×
305
  if (code) {
×
306
    return code;
×
307
  }
308

309
#if 1
310
  // i == 0, scan super table
311
  // i == 1, scan normal table and child table
312
  for (int i = 0; i < 2; i++) {
×
313
    TBC    *uidCursor = NULL;
×
314
    int32_t counter = 0;
×
315

316
    code = tdbTbcOpen(pMeta->pUidIdx, &uidCursor, NULL);
×
317
    if (code) {
×
318
      metaError("vgId:%d failed to open uid index cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
×
319
      return code;
×
320
    }
321

322
    code = tdbTbcMoveToFirst(uidCursor);
×
323
    if (code) {
×
324
      metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
×
325
      tdbTbcClose(uidCursor);
×
326
      return code;
×
327
    }
328

329
    for (;;) {
×
330
      const void *pKey;
331
      int         kLen;
332
      const void *pVal;
333
      int         vLen;
334

335
      if (tdbTbcGet(uidCursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
336
        break;
×
337
      }
338

339
      tb_uid_t    uid = *(tb_uid_t *)pKey;
×
340
      SUidIdxVal *pUidIdxVal = (SUidIdxVal *)pVal;
×
341
      if ((i == 0 && (pUidIdxVal->suid && pUidIdxVal->suid == uid))          // super table
×
342
          || (i == 1 && (pUidIdxVal->suid == 0 || pUidIdxVal->suid != uid))  // normal table and child table
×
343
      ) {
344
        counter++;
×
345
        if (i == 0) {
×
346
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, "super", uid);
×
347
        } else {
348
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter,
×
349
                   pUidIdxVal->suid == 0 ? "normal" : "child", uid);
350
        }
351

352
        // fetch table entry
353
        void *value = NULL;
×
354
        int   valueSize = 0;
×
355
        if (tdbTbGet(pMeta->pTbDb,
×
356
                     &(STbDbKey){
×
357
                         .version = pUidIdxVal->version,
×
358
                         .uid = uid,
359
                     },
360
                     sizeof(uid), &value, &valueSize) == 0) {
361
          SDecoder   dc = {0};
×
362
          SMetaEntry me = {0};
×
363
          tDecoderInit(&dc, value, valueSize);
×
364
          if (metaDecodeEntry(&dc, &me) == 0) {
×
365
            if (me.type == TSDB_CHILD_TABLE &&
×
366
                tdbTbGet(pMeta->pUidIdx, &me.ctbEntry.suid, sizeof(me.ctbEntry.suid), NULL, NULL) != 0) {
×
367
              metaError("vgId:%d failed to get super table uid:%" PRId64 " for child table uid:%" PRId64,
×
368
                        TD_VID(pVnode), me.ctbEntry.suid, uid);
369
            } else if (metaHandleEntry2(pNewMeta, &me) != 0) {
×
370
              metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid);
×
371
            }
372
          }
373
          tDecoderClear(&dc);
×
374
        }
375
        tdbFree(value);
×
376
      }
377

378
      code = tdbTbcMoveToNext(uidCursor);
×
379
      if (code) {
×
380
        metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
×
381
        return code;
×
382
      }
383
    }
384

385
    tdbTbcClose(uidCursor);
×
386
  }
387
#else
388
  TBC *cursor = NULL;
389

390
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
391
  if (code) {
392
    metaError("vgId:%d failed to open table.db cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
393
    return code;
394
  }
395

396
  code = tdbTbcMoveToFirst(cursor);
397
  if (code) {
398
    metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
399
    tdbTbcClose(cursor);
400
    return code;
401
  }
402

403
  while (true) {
404
    const void *pKey;
405
    int         kLen;
406
    const void *pVal;
407
    int         vLen;
408

409
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
410
      break;
411
    }
412

413
    STbDbKey  *pKeyEntry = (STbDbKey *)pKey;
414
    SDecoder   dc = {0};
415
    SMetaEntry me = {0};
416

417
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
418
    if (metaDecodeEntry(&dc, &me) < 0) {
419
      tDecoderClear(&dc);
420
      break;
421
    }
422

423
    if (metaHandleEntry2(pNewMeta, &me) != 0) {
424
      metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), pKeyEntry->uid);
425
      tDecoderClear(&dc);
426
      break;
427
    }
428
    tDecoderClear(&dc);
429

430
    code = tdbTbcMoveToNext(cursor);
431
    if (code) {
432
      metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
433
      break;
434
    }
435
  }
436

437
  tdbTbcClose(cursor);
438

439
#endif
440

441
  code = metaCommit(pNewMeta, pNewMeta->txn);
×
442
  if (code) {
×
443
    metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
444
    return code;
×
445
  }
446

447
  code = metaFinishCommit(pNewMeta, pNewMeta->txn);
×
448
  if (code) {
×
449
    metaError("vgId:%d failed to finish commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
450
    return code;
×
451
  }
452

453
  if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) {
×
454
    metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
455
  }
456
  metaClose(&pNewMeta);
×
457
  metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode));
×
458

459
  // Commit the new metadata
460
  char metaDir[TSDB_FILENAME_LEN] = {0};
×
461
  char metaTempDir[TSDB_FILENAME_LEN] = {0};
×
462
  char metaBackupDir[TSDB_FILENAME_LEN] = {0};
×
463

464
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
×
465
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
×
466
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
×
467

468
  metaClose(ppMeta);
×
469
  if (taosRenameFile(metaDir, metaBackupDir) != 0) {
×
470
    metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
471
    return terrno;
×
472
  }
473

474
  // rename the new meta to old meta
475
  if (taosRenameFile(metaTempDir, metaDir) != 0) {
×
476
    metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
477
    return terrno;
×
478
  }
479

480
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false);
×
481
  if (code) {
×
482
    metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
483
    return code;
×
484
  }
485

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

488
  return 0;
×
489
}
490

491
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
12,262✔
492
  int32_t code = TSDB_CODE_SUCCESS;
12,262✔
493
  char    metaDir[TSDB_FILENAME_LEN] = {0};
12,262✔
494
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
12,262✔
495
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
12,262✔
496

497
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
12,262✔
498
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
12,265✔
499
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
12,264✔
500

501
  bool metaExists = taosCheckExistFile(metaDir);
12,266✔
502
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
12,263✔
503
  bool metaTempExists = taosCheckExistFile(metaTempDir);
12,266✔
504

505
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
12,267!
506
      || (metaBackupExists && !metaExists && !metaTempExists)  //
12,267!
507
      || (metaBackupExists && metaExists && metaTempExists)    //
12,267!
508
  ) {
509
    metaError("vgId:%d, invalid meta state, please check!", TD_VID(pVnode));
×
510
    TAOS_RETURN(TSDB_CODE_FAILED);
×
511
  } else if (!metaBackupExists && metaExists && metaTempExists) {
12,267!
512
    taosRemoveDir(metaTempDir);
×
513
  } else if (metaBackupExists && !metaExists && metaTempExists) {
12,267!
514
    code = taosRenameFile(metaTempDir, metaDir);
×
515
    if (code) {
×
516
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
517
      TAOS_RETURN(code);
×
518
    }
519
    taosRemoveDir(metaBackupDir);
×
520
  } else if (metaBackupExists && metaExists && !metaTempExists) {
12,267!
521
    taosRemoveDir(metaBackupDir);
×
522
  }
523

524
  // Do open meta
525
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
12,267✔
526
  if (code) {
12,265!
527
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
528
    TAOS_RETURN(code);
×
529
  }
530

531
  if (generateNewMeta) {
12,265!
532
    code = metaGenerateNewMeta(ppMeta);
×
533
    if (code) {
×
534
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
535
      TAOS_RETURN(code);
×
536
    }
537
  }
538

539
  return TSDB_CODE_SUCCESS;
12,266✔
540
}
541

542
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
12,245✔
543
  int32_t code = TSDB_CODE_SUCCESS;
12,245✔
544
  int32_t lino;
545
  SMeta  *pMeta = *ppMeta;
12,245✔
546

547
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
12,245!
548
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
549
    TSDB_CHECK_CODE(code, lino, _exit);
×
550

551
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
552
    TSDB_CHECK_CODE(code, lino, _exit);
×
553

554
    code = metaCommit(pMeta, pMeta->txn);
×
555
    TSDB_CHECK_CODE(code, lino, _exit);
×
556
  }
557

558
_exit:
12,245✔
559
  if (code) {
12,245!
560
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
561
    metaCleanup(ppMeta);
×
562
  }
563
  return code;
12,245✔
564
}
565

566
void metaClose(SMeta **ppMeta) {
12,259✔
567
  metaCleanup(ppMeta);
12,259✔
568
  return;
12,268✔
569
}
570

571
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
6✔
572
  int32_t code = 0;
6✔
573
  metaWLock(pMeta);
6✔
574
  code = tdbAlter(pMeta->pEnv, nPage);
6✔
575
  metaULock(pMeta);
6✔
576

577
  if (code) {
6!
578
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
579
  }
580
  return code;
6✔
581
}
582

583
void metaRLock(SMeta *pMeta) {
10,840,513✔
584
  metaTrace("meta rlock %p", &pMeta->lock);
10,840,513✔
585
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
10,840,517!
586
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
587
  }
588
}
10,842,705✔
589

590
void metaWLock(SMeta *pMeta) {
316,226✔
591
  metaTrace("meta wlock %p", &pMeta->lock);
316,226✔
592
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
316,226!
593
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
594
  }
595
}
316,276✔
596

597
void metaULock(SMeta *pMeta) {
11,155,250✔
598
  metaTrace("meta ulock %p", &pMeta->lock);
11,155,250✔
599
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
11,155,252!
600
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
601
  }
602
}
11,158,639✔
603

604
static void metaCleanup(SMeta **ppMeta) {
12,257✔
605
  SMeta *pMeta = *ppMeta;
12,257✔
606
  if (pMeta) {
12,257✔
607
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
12,256✔
608
    if (pMeta->pEnv) metaAbort(pMeta);
12,272✔
609
    if (pMeta->pCache) metaCacheClose(pMeta);
12,272✔
610
#ifdef BUILD_NO_CALL
611
    if (pMeta->pIdx) metaCloseIdx(pMeta);
612
#endif
613
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
12,272✔
614
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
12,272✔
615
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
12,272✔
616
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
12,272✔
617
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
12,272✔
618
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
12,271✔
619
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
12,272✔
620
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
12,272✔
621
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
12,272✔
622
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
12,272✔
623
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
12,272✔
624
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
12,272✔
625
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
12,272✔
626
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
12,272✔
627
    metaDestroyLock(pMeta);
12,270✔
628

629
    taosMemoryFreeClear(*ppMeta);
12,268!
630
  }
631
}
12,269✔
632

633
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
28,226,397✔
634
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
28,226,397✔
635
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
28,226,397✔
636

637
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
638

639
  if (pTbDbKey1->version > pTbDbKey2->version) {
28,226,397✔
640
    return 1;
12,176,016✔
641
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
16,050,381✔
642
    return -1;
9,835,275✔
643
  }
644

645
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
6,215,106✔
646
    return 1;
272,959✔
647
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
5,942,147✔
648
    return -1;
175,797✔
649
  }
650

651
  return 0;
5,766,350✔
652
}
653

654
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
1,013,307✔
655
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
1,013,307✔
656
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
1,013,307✔
657

658
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
659

660
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
1,013,307✔
661
    return 1;
388,571✔
662
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
624,736✔
663
    return -1;
169,023✔
664
  }
665

666
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
455,713✔
667
    return 1;
118,089✔
668
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
337,624✔
669
    return -1;
174,850✔
670
  }
671

672
  return 0;
162,774✔
673
}
674

675
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
13,060,122✔
676
  tb_uid_t uid1 = taosGetInt64Aligned((int64_t*)pKey1);
13,060,122✔
677
  tb_uid_t uid2 = taosGetInt64Aligned((int64_t*)pKey2);
13,060,122✔
678

679
  if (uid1 > uid2) {
13,060,122✔
680
    return 1;
6,841,125✔
681
  } else if (uid1 < uid2) {
6,218,997✔
682
    return -1;
3,222,745✔
683
  }
684

685
  return 0;
2,996,252✔
686
}
687

688
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
3,229,277✔
689
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
3,229,277✔
690
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
3,229,277✔
691

692
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
693

694
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
3,229,277✔
695
    return 1;
674,974✔
696
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
2,554,303✔
697
    return -1;
130,005✔
698
  }
699

700
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
2,424,298✔
701
    return 1;
838,679✔
702
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
1,585,619✔
703
    return -1;
1,507,912✔
704
  }
705

706
  return 0;
77,707✔
707
}
708

709
int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
995,979✔
710
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
995,979✔
711
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
995,979✔
712
  tb_uid_t    uid1 = 0, uid2 = 0;
995,979✔
713
  int         c;
714

715
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
716

717
  // compare suid
718
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
995,979✔
719
    return 1;
6,531✔
720
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
989,448✔
721
    return -1;
48,045✔
722
  }
723

724
  // compare column id
725
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
941,403✔
726
    return 1;
6,735✔
727
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
934,668✔
728
    return -1;
1,907✔
729
  }
730

731
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
932,761!
732
    metaError("meta/open: incorrect tag idx type.");
×
733
    return TSDB_CODE_FAILED;
×
734
  }
735

736
  // check NULL, NULL is always the smallest
737
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
932,761✔
738
    return -1;
615✔
739
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
932,146✔
740
    return 1;
1,663✔
741
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
930,483!
742
    // all not NULL, compr tag vals
743
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
890,703✔
744
    if (func == NULL) {
890,684!
745
      metaError("meta/open: %s", terrstr());
×
746
      return TSDB_CODE_FAILED;
×
747
    }
748
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
890,684✔
749
    if (c) return c;
890,768✔
750
  }
751

752
  // both null or tag values are equal, then continue to compare uids
753
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
193,748!
754
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
88,875✔
755
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
88,875✔
756
  } else {
757
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
104,873✔
758
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
104,873✔
759
  }
760

761
  // compare uid
762
  if (uid1 < uid2) {
193,748✔
763
    return -1;
15,699✔
764
  } else if (uid1 > uid2) {
178,049✔
765
    return 1;
150,336✔
766
  } else {
767
    return 0;
27,713✔
768
  }
769

770
  return 0;
771
}
772

773
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
517,149✔
774
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
517,149✔
775
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
517,149✔
776

777
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
778

779
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
517,149✔
780
    return 1;
410,357✔
781
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
106,792✔
782
    return -1;
12,813✔
783
  }
784

785
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
93,979✔
786
    return 1;
75,958✔
787
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
18,021✔
788
    return -1;
306✔
789
  }
790

791
  return 0;
17,715✔
792
}
793

794
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
795
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
×
796
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
×
797

798
  TDB_KEY_ALIGN(pNcolIdxKey1, pNcolIdxKey2, SNcolIdxKey);
799

800
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
×
801
    return 1;
×
802
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
×
803
    return -1;
×
804
  }
805

806
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
×
807
    return 1;
×
808
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
×
809
    return -1;
×
810
  }
811

812
  return 0;
×
813
}
814

815
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
816
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
817
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
818

819
  TDB_KEY_ALIGN(pSmaIdxKey1, pSmaIdxKey2, SSmaIdxKey);
820

821
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
822
    return 1;
×
823
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
824
    return -1;
×
825
  }
826

827
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
828
    return 1;
×
829
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
830
    return -1;
×
831
  }
832

833
  return 0;
×
834
}
835

836
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
837
  int32_t uid1 = *(int32_t *)pKey1;
×
838
  int32_t uid2 = *(int32_t *)pKey2;
×
839

840
  if (uid1 > uid2) {
×
841
    return 1;
×
842
  } else if (uid1 < uid2) {
×
843
    return -1;
×
844
  }
845

846
  return 0;
×
847
}
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