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

taosdata / TDengine / #3818

01 Apr 2025 07:46AM UTC coverage: 34.065% (-0.02%) from 34.08%
#3818

push

travis-ci

happyguoxy
test:alter gcda dir

148531 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222425 of 489446 relevant lines covered (45.44%)

762721.74 hits per line

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

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

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

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

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

174
  metaInitLock(pMeta);
277✔
175

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

180
  pMeta->pVnode = pVnode;
277✔
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

269
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
831✔
270
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, fname, TSDB_FILENAME_LEN);
831✔
271
  int32_t offset = strlen(fname);
831✔
272
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
831✔
273
}
831✔
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
  // i == 0, scan super table
296
  // i == 1, scan normal table and child table
297
  for (int i = 0; i < 2; i++) {
×
298
    TBC    *uidCursor = NULL;
×
299
    int32_t counter = 0;
×
300

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

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

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

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

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

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

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

370
    tdbTbcClose(uidCursor);
×
371
  }
372

373
  code = metaCommit(pNewMeta, pNewMeta->txn);
×
374
  if (code) {
×
375
    metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
376
    return code;
×
377
  }
378

379
  code = metaFinishCommit(pNewMeta, pNewMeta->txn);
×
380
  if (code) {
×
381
    metaError("vgId:%d failed to finish commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
382
    return code;
×
383
  }
384

385
  if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) {
×
386
    metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
387
  }
388
  metaClose(&pNewMeta);
×
389
  metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode));
×
390

391
  // Commit the new metadata
392
  char metaDir[TSDB_FILENAME_LEN] = {0};
×
393
  char metaTempDir[TSDB_FILENAME_LEN] = {0};
×
394
  char metaBackupDir[TSDB_FILENAME_LEN] = {0};
×
395

396
  vnodeGetMetaPath(pVnode, metaDir, VNODE_META_DIR);
×
397
  vnodeGetMetaPath(pVnode, metaTempDir, VNODE_META_TMP_DIR);
×
398
  vnodeGetMetaPath(pVnode, metaBackupDir, VNODE_META_BACKUP_DIR);
×
399

400
  metaClose(ppMeta);
×
401
  if (taosRenameFile(metaDir, metaBackupDir) != 0) {
×
402
    metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
403
    return terrno;
×
404
  }
405

406
  // rename the new meta to old meta
407
  if (taosRenameFile(metaTempDir, metaDir) != 0) {
×
408
    metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
409
    return terrno;
×
410
  }
411

412
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false);
×
413
  if (code) {
×
414
    metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
415
    return code;
×
416
  }
417

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

420
  return 0;
×
421
}
422

423
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
277✔
424
  int32_t code = TSDB_CODE_SUCCESS;
277✔
425
  char    metaDir[TSDB_FILENAME_LEN] = {0};
277✔
426
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
277✔
427
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
277✔
428

429
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
277✔
430
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
277✔
431
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
277✔
432

433
  bool metaExists = taosCheckExistFile(metaDir);
277✔
434
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
277✔
435
  bool metaTempExists = taosCheckExistFile(metaTempDir);
277✔
436

437
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
277!
438
      || (metaBackupExists && !metaExists && !metaTempExists)  //
277!
439
      || (metaBackupExists && metaExists && metaTempExists)    //
277!
440
  ) {
441
    metaError("vgId:%d, invalid meta state, please check!", TD_VID(pVnode));
×
442
    TAOS_RETURN(TSDB_CODE_FAILED);
×
443
  } else if (!metaBackupExists && metaExists && metaTempExists) {
277!
444
    taosRemoveDir(metaTempDir);
×
445
  } else if (metaBackupExists && !metaExists && metaTempExists) {
277!
446
    code = taosRenameFile(metaTempDir, metaDir);
×
447
    if (code) {
×
448
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
449
      TAOS_RETURN(code);
×
450
    }
451
    taosRemoveDir(metaBackupDir);
×
452
  } else if (metaBackupExists && metaExists && !metaTempExists) {
277!
453
    taosRemoveDir(metaBackupDir);
×
454
  }
455

456
  // Do open meta
457
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
277✔
458
  if (code) {
277!
459
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
460
    TAOS_RETURN(code);
×
461
  }
462

463
  if (generateNewMeta) {
277!
464
    code = metaGenerateNewMeta(ppMeta);
×
465
    if (code) {
×
466
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
467
      TAOS_RETURN(code);
×
468
    }
469
  }
470

471
  return TSDB_CODE_SUCCESS;
277✔
472
}
473

474
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
277✔
475
  int32_t code = TSDB_CODE_SUCCESS;
277✔
476
  int32_t lino;
477
  SMeta  *pMeta = *ppMeta;
277✔
478

479
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
277!
480
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
481
    TSDB_CHECK_CODE(code, lino, _exit);
×
482

483
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
484
    TSDB_CHECK_CODE(code, lino, _exit);
×
485

486
    code = metaCommit(pMeta, pMeta->txn);
×
487
    TSDB_CHECK_CODE(code, lino, _exit);
×
488
  }
489

490
_exit:
277✔
491
  if (code) {
277!
492
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
493
    metaCleanup(ppMeta);
×
494
  }
495
  return code;
277✔
496
}
497

498
void metaClose(SMeta **ppMeta) {
275✔
499
  metaCleanup(ppMeta);
275✔
500
  return;
277✔
501
}
502

503
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
×
504
  int32_t code = 0;
×
505
  metaWLock(pMeta);
×
506
  code = tdbAlter(pMeta->pEnv, nPage);
×
507
  metaULock(pMeta);
×
508

509
  if (code) {
×
510
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
511
  }
512
  return code;
×
513
}
514

515
void metaRLock(SMeta *pMeta) {
1,481,931✔
516
  metaTrace("meta rlock %p", &pMeta->lock);
1,481,931✔
517
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
1,481,931!
518
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
519
  }
520
}
1,484,836✔
521

522
void metaWLock(SMeta *pMeta) {
5,090✔
523
  metaTrace("meta wlock %p", &pMeta->lock);
5,090✔
524
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
5,090!
525
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
526
  }
527
}
5,091✔
528

529
void metaULock(SMeta *pMeta) {
1,487,419✔
530
  metaTrace("meta ulock %p", &pMeta->lock);
1,487,419✔
531
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
1,487,419!
532
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
533
  }
534
}
1,489,857✔
535

536
static void metaCleanup(SMeta **ppMeta) {
275✔
537
  SMeta *pMeta = *ppMeta;
275✔
538
  if (pMeta) {
275!
539
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
275!
540
    if (pMeta->pEnv) metaAbort(pMeta);
277!
541
    if (pMeta->pCache) metaCacheClose(pMeta);
275!
542
#ifdef BUILD_NO_CALL
543
    if (pMeta->pIdx) metaCloseIdx(pMeta);
544
#endif
545
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
275!
546
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
275!
547
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
275!
548
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
275!
549
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
275!
550
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
275!
551
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
275!
552
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
275!
553
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
275!
554
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
275!
555
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
275!
556
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
275!
557
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
275!
558
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
275!
559
    metaDestroyLock(pMeta);
275✔
560

561
    taosMemoryFreeClear(*ppMeta);
277!
562
  }
563
}
277✔
564

565
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
368,137✔
566
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
368,137✔
567
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
368,137✔
568

569
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
570

571
  if (pTbDbKey1->version > pTbDbKey2->version) {
368,137✔
572
    return 1;
171,526✔
573
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
196,611✔
574
    return -1;
150,695✔
575
  }
576

577
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
45,916✔
578
    return 1;
1,727✔
579
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
44,189✔
580
    return -1;
708✔
581
  }
582

583
  return 0;
43,481✔
584
}
585

586
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
3,411✔
587
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
3,411✔
588
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
3,411✔
589

590
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
591

592
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
3,411✔
593
    return 1;
414✔
594
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
2,997✔
595
    return -1;
553✔
596
  }
597

598
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
2,444✔
599
    return 1;
281✔
600
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
2,163✔
601
    return -1;
83✔
602
  }
603

604
  return 0;
2,080✔
605
}
606

607
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
363,663✔
608
  tb_uid_t uid1 = taosGetInt64Aligned((int64_t*)pKey1);
363,663✔
609
  tb_uid_t uid2 = taosGetInt64Aligned((int64_t*)pKey2);
363,663✔
610

611
  if (uid1 > uid2) {
363,663✔
612
    return 1;
194,518✔
613
  } else if (uid1 < uid2) {
169,145✔
614
    return -1;
130,039✔
615
  }
616

617
  return 0;
39,106✔
618
}
619

620
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
15,816✔
621
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
15,816✔
622
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
15,816✔
623

624
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
625

626
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
15,816✔
627
    return 1;
1,343✔
628
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
14,473✔
629
    return -1;
1,507✔
630
  }
631

632
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
12,966✔
633
    return 1;
10,472✔
634
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
2,494✔
635
    return -1;
2,407✔
636
  }
637

638
  return 0;
87✔
639
}
640

641
int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
260,038✔
642
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
260,038✔
643
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
260,038✔
644
  tb_uid_t    uid1 = 0, uid2 = 0;
260,038✔
645
  int         c;
646

647
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
648

649
  // compare suid
650
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
260,038✔
651
    return 1;
291✔
652
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
259,747✔
653
    return -1;
1,285✔
654
  }
655

656
  // compare column id
657
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
258,462✔
658
    return 1;
69,704✔
659
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
188,758✔
660
    return -1;
36,921✔
661
  }
662

663
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
151,837!
664
    metaError("meta/open: incorrect tag idx type.");
×
665
    return TSDB_CODE_FAILED;
×
666
  }
667

668
  // check NULL, NULL is always the smallest
669
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
151,837!
670
    return -1;
×
671
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
151,837!
672
    return 1;
×
673
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
151,837!
674
    // all not NULL, compr tag vals
675
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
151,838✔
676
    if (func == NULL) {
151,833!
677
      metaError("meta/open: %s", terrstr());
×
678
      return TSDB_CODE_FAILED;
×
679
    }
680
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
151,833✔
681
    if (c) return c;
151,834✔
682
  }
683

684
  // both null or tag values are equal, then continue to compare uids
685
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
28,681!
686
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
2,047✔
687
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
2,047✔
688
  } else {
689
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
26,634✔
690
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
26,634✔
691
  }
692

693
  // compare uid
694
  if (uid1 < uid2) {
28,681✔
695
    return -1;
3,604✔
696
  } else if (uid1 > uid2) {
25,077✔
697
    return 1;
10,842✔
698
  } else {
699
    return 0;
14,235✔
700
  }
701

702
  return 0;
703
}
704

705
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
8,508✔
706
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
8,508✔
707
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
8,508✔
708

709
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
710

711
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
8,508✔
712
    return 1;
7,041✔
713
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
1,467✔
714
    return -1;
290✔
715
  }
716

717
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
1,177✔
718
    return 1;
1,086✔
719
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
91!
720
    return -1;
×
721
  }
722

723
  return 0;
91✔
724
}
725

726
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
727
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
×
728
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
×
729

730
  TDB_KEY_ALIGN(pNcolIdxKey1, pNcolIdxKey2, SNcolIdxKey);
731

732
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
×
733
    return 1;
×
734
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
×
735
    return -1;
×
736
  }
737

738
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
×
739
    return 1;
×
740
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
×
741
    return -1;
×
742
  }
743

744
  return 0;
×
745
}
746

747
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
748
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
749
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
750

751
  TDB_KEY_ALIGN(pSmaIdxKey1, pSmaIdxKey2, SSmaIdxKey);
752

753
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
754
    return 1;
×
755
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
756
    return -1;
×
757
  }
758

759
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
760
    return 1;
×
761
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
762
    return -1;
×
763
  }
764

765
  return 0;
×
766
}
767

768
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
769
  int32_t uid1 = *(int32_t *)pKey1;
×
770
  int32_t uid2 = *(int32_t *)pKey2;
×
771

772
  if (uid1 > uid2) {
×
773
    return 1;
×
774
  } else if (uid1 < uid2) {
×
775
    return -1;
×
776
  }
777

778
  return 0;
×
779
}
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