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

taosdata / TDengine / #4069

12 May 2025 05:35AM UTC coverage: 63.048% (+0.5%) from 62.547%
#4069

push

travis-ci

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

merge: from main to 3.0 branch

157521 of 317858 branches covered (49.56%)

Branch coverage included in aggregate %.

374 of 573 new or added lines in 31 files covered. (65.27%)

4949 existing lines in 87 files now uncovered.

242707 of 316936 relevant lines covered (76.58%)

18229906.31 hits per line

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

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

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

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

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

174
  metaInitLock(pMeta);
14,628✔
175

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

269
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
43,881✔
270
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, fname, TSDB_FILENAME_LEN);
43,881✔
271
  int32_t offset = strlen(fname);
43,909✔
272
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
43,909✔
273
}
43,909✔
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
UNCOV
298
  for (int i = 0; i < 2; i++) {
×
299
    TBC    *uidCursor = NULL;
×
300
    int32_t counter = 0;
×
301

UNCOV
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

UNCOV
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

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

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

UNCOV
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
      ) {
UNCOV
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 {
UNCOV
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
UNCOV
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) {
UNCOV
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);
UNCOV
355
            } else if (metaHandleEntry2(pNewMeta, &me) != 0) {
×
356
              metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid);
×
357
            }
358
          }
UNCOV
359
          tDecoderClear(&dc);
×
360
        }
UNCOV
361
        tdbFree(value);
×
362
      }
363

UNCOV
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

UNCOV
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

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

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

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

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

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

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

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

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

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

UNCOV
474
  return 0;
×
475
}
476

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

483
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
14,624✔
484
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
14,621✔
485
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
14,628✔
486

487
  bool metaExists = taosCheckExistFile(metaDir);
14,638✔
488
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
14,639✔
489
  bool metaTempExists = taosCheckExistFile(metaTempDir);
14,639✔
490

491
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
14,641!
492
      || (metaBackupExists && !metaExists && !metaTempExists)  //
14,641!
493
      || (metaBackupExists && metaExists && metaTempExists)    //
14,641!
494
  ) {
UNCOV
495
    metaError("vgId:%d, invalid meta state, please check!", TD_VID(pVnode));
×
496
    TAOS_RETURN(TSDB_CODE_FAILED);
×
497
  } else if (!metaBackupExists && metaExists && metaTempExists) {
14,641!
UNCOV
498
    taosRemoveDir(metaTempDir);
×
499
  } else if (metaBackupExists && !metaExists && metaTempExists) {
14,641!
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));
×
UNCOV
503
      TAOS_RETURN(code);
×
504
    }
505
    taosRemoveDir(metaBackupDir);
×
506
  } else if (metaBackupExists && metaExists && !metaTempExists) {
14,641!
UNCOV
507
    taosRemoveDir(metaBackupDir);
×
508
  }
509

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

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

525
  return TSDB_CODE_SUCCESS;
14,639✔
526
}
527

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

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

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

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

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

552
void metaClose(SMeta **ppMeta) {
14,635✔
553
  metaCleanup(ppMeta);
14,635✔
554
  return;
14,641✔
555
}
556

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

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

569
void metaRLock(SMeta *pMeta) {
67,803,461✔
570
  metaTrace("meta rlock %p", &pMeta->lock);
67,803,461✔
571
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
67,806,201!
572
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
573
  }
574
}
67,830,901✔
575

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

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

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

615
    taosMemoryFreeClear(*ppMeta);
14,641!
616
  }
617
}
14,642✔
618

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

623
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
624

625
  if (pTbDbKey1->version > pTbDbKey2->version) {
186,197,178✔
626
    return 1;
85,831,240✔
627
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
100,365,938✔
628
    return -1;
61,191,226✔
629
  }
630

631
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
39,174,712✔
632
    return 1;
533,321✔
633
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
38,641,391✔
634
    return -1;
392,436✔
635
  }
636

637
  return 0;
38,248,955✔
638
}
639

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

644
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
645

646
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
4,091,304✔
647
    return 1;
1,926,078✔
648
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
2,165,226✔
649
    return -1;
577,857✔
650
  }
651

652
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
1,587,369✔
653
    return 1;
76,876✔
654
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
1,510,493✔
655
    return -1;
106,769✔
656
  }
657

658
  return 0;
1,403,724✔
659
}
660

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

665
  if (uid1 > uid2) {
132,964,515✔
666
    return 1;
55,334,258✔
667
  } else if (uid1 < uid2) {
77,630,257✔
668
    return -1;
62,639,233✔
669
  }
670

671
  return 0;
14,991,024✔
672
}
673

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

678
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
679

680
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
10,831,383✔
681
    return 1;
4,212,363✔
682
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
6,619,020✔
683
    return -1;
1,392,404✔
684
  }
685

686
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
5,226,616✔
687
    return 1;
511,851✔
688
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
4,714,765✔
689
    return -1;
4,458,975✔
690
  }
691

692
  return 0;
255,790✔
693
}
694

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

701
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
702

703
  // compare suid
704
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
1,065,476✔
705
    return 1;
61,366✔
706
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
1,004,110✔
707
    return -1;
12,031✔
708
  }
709

710
  // compare column id
711
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
992,079✔
712
    return 1;
566✔
713
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
991,513✔
714
    return -1;
286✔
715
  }
716

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

722
  // check NULL, NULL is always the smallest
723
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
991,227✔
724
    return -1;
580✔
725
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
990,647✔
726
    return 1;
1,594✔
727
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
989,053!
728
    // all not NULL, compr tag vals
729
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
987,493✔
730
    if (func == NULL) {
987,435!
UNCOV
731
      metaError("meta/open: %s", terrstr());
×
UNCOV
732
      return TSDB_CODE_FAILED;
×
733
    }
734
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
987,435✔
735
    if (c) return c;
987,455✔
736
  }
737

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

747
  // compare uid
748
  if (uid1 < uid2) {
153,314✔
749
    return -1;
3,197✔
750
  } else if (uid1 > uid2) {
150,117✔
751
    return 1;
125,394✔
752
  } else {
753
    return 0;
24,723✔
754
  }
755

756
  return 0;
757
}
758

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

763
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
764

765
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
618,269✔
766
    return 1;
497,337✔
767
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
120,932✔
768
    return -1;
21,735✔
769
  }
770

771
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
99,197✔
772
    return 1;
76,338✔
773
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
22,859✔
774
    return -1;
1,567✔
775
  }
776

777
  return 0;
21,292✔
778
}
779

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

784
  TDB_KEY_ALIGN(pNcolIdxKey1, pNcolIdxKey2, SNcolIdxKey);
785

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

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

UNCOV
798
  return 0;
×
799
}
800

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

805
  TDB_KEY_ALIGN(pSmaIdxKey1, pSmaIdxKey2, SSmaIdxKey);
806

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

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

UNCOV
819
  return 0;
×
820
}
821

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

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

UNCOV
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