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

taosdata / TDengine / #3646

12 Mar 2025 12:34PM UTC coverage: 28.375% (-27.8%) from 56.156%
#3646

push

travis-ci

web-flow
Merge pull request #30119 from taosdata/ciup30

ci: Update workflow to fix param issue of run_tdgpt_test

59085 of 286935 branches covered (20.59%)

Branch coverage included in aggregate %.

102775 of 283490 relevant lines covered (36.25%)

55149.72 hits per line

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

36.37
/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
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
20
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
21
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
22
int        tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
23
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
24
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
25
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
26

27
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
28
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
29

30
static void metaInitLock(SMeta *pMeta) {
256✔
31
  TdThreadRwlockAttr attr;
32
  (void)taosThreadRwlockAttrInit(&attr);
256✔
33
  (void)taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
256✔
34
  (void)taosThreadRwlockInit(&pMeta->lock, &attr);
256✔
35
  (void)taosThreadRwlockAttrDestroy(&attr);
256✔
36
  return;
256✔
37
}
38
static void metaDestroyLock(SMeta *pMeta) { (void)taosThreadRwlockDestroy(&pMeta->lock); }
256✔
39

40
static void metaCleanup(SMeta **ppMeta);
41

42
static void doScan(SMeta *pMeta) {
×
43
  TBC    *cursor = NULL;
×
44
  int32_t code;
45

46
  // open file to write
47
  char path[TSDB_FILENAME_LEN] = {0};
×
48
  snprintf(path, TSDB_FILENAME_LEN - 1, "%s%s", pMeta->path, TD_DIRSEP "scan.txt");
×
49
  TdFilePtr fp = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
×
50
  if (fp == NULL) {
×
51
    metaError("failed to open file:%s, reason:%s", path, tstrerror(terrno));
×
52
    return;
×
53
  }
54

55
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
×
56
  if (code) {
×
57
    if (taosCloseFile(&fp) != 0) {
×
58
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
59
    }
60
    metaError("failed to open table.db cursor, reason:%s", tstrerror(terrno));
×
61
    return;
×
62
  }
63

64
  code = tdbTbcMoveToFirst(cursor);
×
65
  if (code) {
×
66
    if (taosCloseFile(&fp) != 0) {
×
67
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
68
    }
69
    tdbTbcClose(cursor);
×
70
    metaError("failed to move to first, reason:%s", tstrerror(terrno));
×
71
    return;
×
72
  }
73

74
  for (;;) {
×
75
    const void *pKey;
76
    int         kLen;
77
    const void *pVal;
78
    int         vLen;
79
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
80
      break;
×
81
    }
82

83
    // decode entry
84
    SDecoder   dc = {0};
×
85
    SMetaEntry me = {0};
×
86

87
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
×
88

89
    if (metaDecodeEntry(&dc, &me) < 0) {
×
90
      tDecoderClear(&dc);
×
91
      break;
×
92
    }
93

94
    // skip deleted entry
95
    if (tdbTbGet(pMeta->pUidIdx, &me.uid, sizeof(me.uid), NULL, NULL) == 0) {
×
96
      // print entry
97
      char buf[1024] = {0};
×
98
      if (me.type == TSDB_SUPER_TABLE) {
×
99
        snprintf(buf, sizeof(buf) - 1, "type: super table, version:%" PRId64 " uid: %" PRId64 " name: %s\n", me.version,
×
100
                 me.uid, me.name);
101

102
      } else if (me.type == TSDB_CHILD_TABLE) {
×
103
        snprintf(buf, sizeof(buf) - 1,
×
104
                 "type: child table, version:%" PRId64 " uid: %" PRId64 " name: %s suid:%" PRId64 "\n", me.version,
105
                 me.uid, me.name, me.ctbEntry.suid);
106
      } else {
107
        snprintf(buf, sizeof(buf) - 1, "type: normal table, version:%" PRId64 " uid: %" PRId64 " name: %s\n",
×
108
                 me.version, me.uid, me.name);
109
      }
110

111
      if (taosWriteFile(fp, buf, strlen(buf)) < 0) {
×
112
        metaError("failed to write file:%s, reason:%s", path, tstrerror(terrno));
×
113
        tDecoderClear(&dc);
×
114
        break;
×
115
      }
116
    }
117

118
    tDecoderClear(&dc);
×
119

120
    if (tdbTbcMoveToNext(cursor) < 0) {
×
121
      break;
×
122
    }
123
  }
124

125
  tdbTbcClose(cursor);
×
126

127
  // close file
128
  if (taosFsyncFile(fp) < 0) {
×
129
    metaError("failed to fsync file:%s, reason:%s", path, tstrerror(terrno));
×
130
  }
131
  if (taosCloseFile(&fp) < 0) {
×
132
    metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
133
  }
134
}
135

136
int32_t metaOpenImpl(SVnode *pVnode, SMeta **ppMeta, const char *metaDir, int8_t rollback) {
256✔
137
  SMeta  *pMeta = NULL;
256✔
138
  int32_t code = 0;
256✔
139
  int32_t lino;
140
  int32_t offset;
141
  int32_t pathLen = 0;
256✔
142
  char    path[TSDB_FILENAME_LEN] = {0};
256✔
143
  char    indexFullPath[128] = {0};
256✔
144

145
  // create handle
146
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
256✔
147
  offset = strlen(path);
256✔
148
  snprintf(path + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
256✔
149

150
  if (strncmp(metaDir, VNODE_META_TMP_DIR, strlen(VNODE_META_TMP_DIR)) == 0) {
256!
151
    taosRemoveDir(path);
×
152
  }
153

154
  pathLen = strlen(path) + 1;
256✔
155
  if ((pMeta = taosMemoryCalloc(1, sizeof(*pMeta) + pathLen)) == NULL) {
256!
156
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
157
  }
158

159
  metaInitLock(pMeta);
256✔
160

161
  pMeta->path = (char *)&pMeta[1];
256✔
162
  tstrncpy(pMeta->path, path, pathLen);
256✔
163
  int32_t ret = taosRealPath(pMeta->path, NULL, strlen(path) + 1);
256✔
164

165
  pMeta->pVnode = pVnode;
256✔
166

167
  // create path if not created yet
168
  code = taosMkDir(pMeta->path);
256✔
169
  TSDB_CHECK_CODE(code, lino, _exit);
256!
170

171
  // open env
172
  code = tdbOpen(pMeta->path, pVnode->config.szPage, pVnode->config.szCache, &pMeta->pEnv, rollback,
256✔
173
                 pVnode->config.tdbEncryptAlgorithm, pVnode->config.tdbEncryptKey);
256✔
174
  TSDB_CHECK_CODE(code, lino, _exit);
256!
175

176
  // open pTbDb
177
  code = tdbTbOpen("table.db", sizeof(STbDbKey), -1, tbDbKeyCmpr, pMeta->pEnv, &pMeta->pTbDb, 0);
256✔
178
  TSDB_CHECK_CODE(code, lino, _exit);
256!
179

180
  // open pSkmDb
181
  code = tdbTbOpen("schema.db", sizeof(SSkmDbKey), -1, skmDbKeyCmpr, pMeta->pEnv, &pMeta->pSkmDb, 0);
256✔
182
  TSDB_CHECK_CODE(code, lino, _exit);
256!
183

184
  // open pUidIdx
185
  code = tdbTbOpen("uid.idx", sizeof(tb_uid_t), sizeof(SUidIdxVal), uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pUidIdx, 0);
256✔
186
  TSDB_CHECK_CODE(code, lino, _exit);
256!
187

188
  // open pNameIdx
189
  code = tdbTbOpen("name.idx", -1, sizeof(tb_uid_t), NULL, pMeta->pEnv, &pMeta->pNameIdx, 0);
256✔
190
  TSDB_CHECK_CODE(code, lino, _exit);
255!
191

192
  // open pCtbIdx
193
  code = tdbTbOpen("ctb.idx", sizeof(SCtbIdxKey), -1, ctbIdxKeyCmpr, pMeta->pEnv, &pMeta->pCtbIdx, 0);
255✔
194
  TSDB_CHECK_CODE(code, lino, _exit);
256!
195

196
  // open pSuidIdx
197
  code = tdbTbOpen("suid.idx", sizeof(tb_uid_t), 0, uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pSuidIdx, 0);
256✔
198
  TSDB_CHECK_CODE(code, lino, _exit);
256!
199

200
  (void)tsnprintf(indexFullPath, sizeof(indexFullPath), "%s/%s", pMeta->path, "invert");
256✔
201
  ret = taosMkDir(indexFullPath);
256✔
202

203
  SIndexOpts opts = {.cacheSize = 8 * 1024 * 1024};
256✔
204
  code = indexOpen(&opts, indexFullPath, (SIndex **)&pMeta->pTagIvtIdx);
256✔
205
  TSDB_CHECK_CODE(code, lino, _exit);
256!
206

207
  code = tdbTbOpen("tag.idx", -1, 0, tagIdxKeyCmpr, pMeta->pEnv, &pMeta->pTagIdx, 0);
256✔
208
  TSDB_CHECK_CODE(code, lino, _exit);
256!
209

210
  // open pTtlMgr ("ttlv1.idx")
211
  char logPrefix[128] = {0};
256✔
212
  (void)tsnprintf(logPrefix, sizeof(logPrefix), "vgId:%d", TD_VID(pVnode));
256✔
213
  code = ttlMgrOpen(&pMeta->pTtlMgr, pMeta->pEnv, 0, logPrefix, tsTtlFlushThreshold);
256✔
214
  TSDB_CHECK_CODE(code, lino, _exit);
256!
215

216
  // open pSmaIdx
217
  code = tdbTbOpen("sma.idx", sizeof(SSmaIdxKey), 0, smaIdxKeyCmpr, pMeta->pEnv, &pMeta->pSmaIdx, 0);
256✔
218
  TSDB_CHECK_CODE(code, lino, _exit);
256!
219

220
  // idx table create time
221
  code = tdbTbOpen("ctime.idx", sizeof(SBtimeIdxKey), 0, btimeIdxCmpr, pMeta->pEnv, &pMeta->pBtimeIdx, 0);
256✔
222
  TSDB_CHECK_CODE(code, lino, _exit);
256!
223

224
  // idx num of col, normal table only
225
  code = tdbTbOpen("ncol.idx", sizeof(SNcolIdxKey), 0, ncolIdxCmpr, pMeta->pEnv, &pMeta->pNcolIdx, 0);
256✔
226
  TSDB_CHECK_CODE(code, lino, _exit);
256!
227

228
  code = tdbTbOpen("stream.task.db", sizeof(int64_t), -1, taskIdxKeyCmpr, pMeta->pEnv, &pMeta->pStreamDb, 0);
256✔
229
  TSDB_CHECK_CODE(code, lino, _exit);
256!
230

231
  code = metaCacheOpen(pMeta);
256✔
232
  TSDB_CHECK_CODE(code, lino, _exit);
256!
233

234
  code = metaInitTbFilterCache(pMeta);
256✔
235
  TSDB_CHECK_CODE(code, lino, _exit);
256!
236

237
#if 0
238
  // Do NOT remove this code, it is used to do debug stuff
239
  doScan(pMeta);
240
#endif
241

242
_exit:
256✔
243
  if (code) {
256!
244
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
245
    metaCleanup(&pMeta);
×
246
    *ppMeta = NULL;
×
247
  } else {
248
    metaDebug("vgId:%d %s success", TD_VID(pVnode), __func__);
256✔
249
    *ppMeta = pMeta;
256✔
250
  }
251
  return code;
256✔
252
}
253

254
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
768✔
255
  vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, fname, TSDB_FILENAME_LEN);
768✔
256
  int32_t offset = strlen(fname);
768✔
257
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
768✔
258
}
768✔
259

260
bool generateNewMeta = false;
261

262
static int32_t metaGenerateNewMeta(SMeta **ppMeta) {
×
263
  SMeta  *pNewMeta = NULL;
×
264
  SMeta  *pMeta = *ppMeta;
×
265
  SVnode *pVnode = pMeta->pVnode;
×
266

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

269
  // Open a new meta for organization
270
  int32_t code = metaOpenImpl(pMeta->pVnode, &pNewMeta, VNODE_META_TMP_DIR, false);
×
271
  if (code) {
×
272
    return code;
×
273
  }
274

275
  code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL);
×
276
  if (code) {
×
277
    return code;
×
278
  }
279

280
  // i == 0, scan super table
281
  // i == 1, scan normal table and child table
282
  for (int i = 0; i < 2; i++) {
×
283
    TBC    *uidCursor = NULL;
×
284
    int32_t counter = 0;
×
285

286
    code = tdbTbcOpen(pMeta->pUidIdx, &uidCursor, NULL);
×
287
    if (code) {
×
288
      metaError("vgId:%d failed to open uid index cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
×
289
      return code;
×
290
    }
291

292
    code = tdbTbcMoveToFirst(uidCursor);
×
293
    if (code) {
×
294
      metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
×
295
      tdbTbcClose(uidCursor);
×
296
      return code;
×
297
    }
298

299
    for (;;) {
×
300
      const void *pKey;
301
      int         kLen;
302
      const void *pVal;
303
      int         vLen;
304

305
      if (tdbTbcGet(uidCursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
306
        break;
×
307
      }
308

309
      tb_uid_t    uid = *(tb_uid_t *)pKey;
×
310
      SUidIdxVal *pUidIdxVal = (SUidIdxVal *)pVal;
×
311
      if ((i == 0 && (pUidIdxVal->suid && pUidIdxVal->suid == uid))          // super table
×
312
          || (i == 1 && (pUidIdxVal->suid == 0 || pUidIdxVal->suid != uid))  // normal table and child table
×
313
      ) {
314
        counter++;
×
315
        if (i == 0) {
×
316
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, "super", uid);
×
317
        } else {
318
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter,
×
319
                   pUidIdxVal->suid == 0 ? "normal" : "child", uid);
320
        }
321

322
        // fetch table entry
323
        void *value = NULL;
×
324
        int   valueSize = 0;
×
325
        if (tdbTbGet(pMeta->pTbDb,
×
326
                     &(STbDbKey){
×
327
                         .version = pUidIdxVal->version,
×
328
                         .uid = uid,
329
                     },
330
                     sizeof(uid), &value, &valueSize) == 0) {
331
          SDecoder   dc = {0};
×
332
          SMetaEntry me = {0};
×
333
          tDecoderInit(&dc, value, valueSize);
×
334
          if (metaDecodeEntry(&dc, &me) == 0) {
×
335
            if (me.type == TSDB_CHILD_TABLE &&
×
336
                tdbTbGet(pMeta->pUidIdx, &me.ctbEntry.suid, sizeof(me.ctbEntry.suid), NULL, NULL) != 0) {
×
337
              metaError("vgId:%d failed to get super table uid:%" PRId64 " for child table uid:%" PRId64,
×
338
                        TD_VID(pVnode), me.ctbEntry.suid, uid);
339
            } else if (metaHandleEntry2(pNewMeta, &me) != 0) {
×
340
              metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid);
×
341
            }
342
          }
343
          tDecoderClear(&dc);
×
344
        }
345
        tdbFree(value);
×
346
      }
347

348
      code = tdbTbcMoveToNext(uidCursor);
×
349
      if (code) {
×
350
        metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
×
351
        return code;
×
352
      }
353
    }
354

355
    tdbTbcClose(uidCursor);
×
356
  }
357

358
  code = metaCommit(pNewMeta, pNewMeta->txn);
×
359
  if (code) {
×
360
    metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
361
    return code;
×
362
  }
363

364
  code = metaFinishCommit(pNewMeta, pNewMeta->txn);
×
365
  if (code) {
×
366
    metaError("vgId:%d failed to finish commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
367
    return code;
×
368
  }
369

370
  if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) {
×
371
    metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
372
  }
373
  metaClose(&pNewMeta);
×
374
  metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode));
×
375

376
  // Commit the new metadata
377
  char metaDir[TSDB_FILENAME_LEN] = {0};
×
378
  char metaTempDir[TSDB_FILENAME_LEN] = {0};
×
379
  char metaBackupDir[TSDB_FILENAME_LEN] = {0};
×
380

381
  vnodeGetMetaPath(pVnode, metaDir, VNODE_META_DIR);
×
382
  vnodeGetMetaPath(pVnode, metaTempDir, VNODE_META_TMP_DIR);
×
383
  vnodeGetMetaPath(pVnode, metaBackupDir, VNODE_META_BACKUP_DIR);
×
384

385
  metaClose(ppMeta);
×
386
  if (taosRenameFile(metaDir, metaBackupDir) != 0) {
×
387
    metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
388
    return terrno;
×
389
  }
390

391
  // rename the new meta to old meta
392
  if (taosRenameFile(metaTempDir, metaDir) != 0) {
×
393
    metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
394
    return terrno;
×
395
  }
396

397
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false);
×
398
  if (code) {
×
399
    metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
400
    return code;
×
401
  }
402

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

405
  return 0;
×
406
}
407

408
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
256✔
409
  int32_t code = TSDB_CODE_SUCCESS;
256✔
410
  char    metaDir[TSDB_FILENAME_LEN] = {0};
256✔
411
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
256✔
412
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
256✔
413

414
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
256✔
415
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
256✔
416
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
256✔
417

418
  bool metaExists = taosCheckExistFile(metaDir);
256✔
419
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
256✔
420
  bool metaTempExists = taosCheckExistFile(metaTempDir);
256✔
421

422
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
256!
423
      || (metaBackupExists && !metaExists && !metaTempExists)  //
256!
424
      || (metaBackupExists && metaExists && metaTempExists)    //
256!
425
  ) {
426
    metaError("vgId:%d, invalid meta state, please check!", TD_VID(pVnode));
×
427
    return TSDB_CODE_FAILED;
×
428
  } else if (!metaBackupExists && metaExists && metaTempExists) {
256!
429
    taosRemoveDir(metaTempDir);
×
430
  } else if (metaBackupExists && !metaExists && metaTempExists) {
256!
431
    code = taosRenameFile(metaTempDir, metaDir);
×
432
    if (code) {
×
433
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
434
      return code;
×
435
    }
436
    taosRemoveDir(metaBackupDir);
×
437
  } else if (metaBackupExists && metaExists && !metaTempExists) {
256!
438
    taosRemoveDir(metaBackupDir);
×
439
  }
440

441
  // Do open meta
442
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
256✔
443
  if (code) {
256!
444
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
445
    return code;
×
446
  }
447

448
  if (generateNewMeta) {
256!
449
    code = metaGenerateNewMeta(ppMeta);
×
450
    if (code) {
×
451
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
452
      return code;
×
453
    }
454
  }
455

456
  return TSDB_CODE_SUCCESS;
256✔
457
}
458

459
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
256✔
460
  int32_t code = TSDB_CODE_SUCCESS;
256✔
461
  int32_t lino;
462
  SMeta  *pMeta = *ppMeta;
256✔
463

464
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
256!
465
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
466
    TSDB_CHECK_CODE(code, lino, _exit);
×
467

468
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
469
    TSDB_CHECK_CODE(code, lino, _exit);
×
470

471
    code = metaCommit(pMeta, pMeta->txn);
×
472
    TSDB_CHECK_CODE(code, lino, _exit);
×
473
  }
474

475
_exit:
256✔
476
  if (code) {
256!
477
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
478
    metaCleanup(ppMeta);
×
479
  }
480
  return code;
256✔
481
}
482

483
void metaClose(SMeta **ppMeta) {
256✔
484
  metaCleanup(ppMeta);
256✔
485
  return;
256✔
486
}
487

488
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
×
489
  int32_t code = 0;
×
490
  metaWLock(pMeta);
×
491
  code = tdbAlter(pMeta->pEnv, nPage);
×
492
  metaULock(pMeta);
×
493

494
  if (code) {
×
495
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
496
  }
497
  return code;
×
498
}
499

500
void metaRLock(SMeta *pMeta) {
102,072✔
501
  metaTrace("meta rlock %p", &pMeta->lock);
102,072!
502
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
102,072!
503
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
504
  }
505
}
102,129✔
506

507
void metaWLock(SMeta *pMeta) {
993✔
508
  metaTrace("meta wlock %p", &pMeta->lock);
993!
509
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
993!
510
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
511
  }
512
}
998✔
513

514
void metaULock(SMeta *pMeta) {
103,103✔
515
  metaTrace("meta ulock %p", &pMeta->lock);
103,103!
516
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
103,103!
517
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
518
  }
519
}
103,126✔
520

521
static void metaCleanup(SMeta **ppMeta) {
256✔
522
  SMeta *pMeta = *ppMeta;
256✔
523
  if (pMeta) {
256!
524
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
256!
525
    if (pMeta->pEnv) metaAbort(pMeta);
256!
526
    if (pMeta->pCache) metaCacheClose(pMeta);
256!
527
#ifdef BUILD_NO_CALL
528
    if (pMeta->pIdx) metaCloseIdx(pMeta);
529
#endif
530
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
256!
531
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
256!
532
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
256!
533
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
256!
534
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
256!
535
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
256!
536
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
256!
537
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
256!
538
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
256!
539
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
256!
540
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
256!
541
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
256!
542
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
256!
543
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
256!
544
    metaDestroyLock(pMeta);
256✔
545

546
    taosMemoryFreeClear(*ppMeta);
256!
547
  }
548
}
256✔
549

550
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
35,180✔
551
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
35,180✔
552
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
35,180✔
553

554
  if (pTbDbKey1->version > pTbDbKey2->version) {
35,180✔
555
    return 1;
5,304✔
556
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
29,876✔
557
    return -1;
1,093✔
558
  }
559

560
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
28,783!
561
    return 1;
×
562
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
28,783!
563
    return -1;
×
564
  }
565

566
  return 0;
28,783✔
567
}
568

569
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
695✔
570
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
695✔
571
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
695✔
572

573
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
695✔
574
    return 1;
528✔
575
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
167✔
576
    return -1;
59✔
577
  }
578

579
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
108!
580
    return 1;
×
581
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
108!
582
    return -1;
×
583
  }
584

585
  return 0;
108✔
586
}
587

588
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
14,768✔
589
  tb_uid_t uid1 = *(tb_uid_t *)pKey1;
14,768✔
590
  tb_uid_t uid2 = *(tb_uid_t *)pKey2;
14,768✔
591

592
  if (uid1 > uid2) {
14,768✔
593
    return 1;
2,545✔
594
  } else if (uid1 < uid2) {
12,223✔
595
    return -1;
834✔
596
  }
597

598
  return 0;
11,389✔
599
}
600

601
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
9,883✔
602
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
9,883✔
603
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
9,883✔
604

605
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
9,883✔
606
    return 1;
241✔
607
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
9,642✔
608
    return -1;
87✔
609
  }
610

611
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
9,555✔
612
    return 1;
123✔
613
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
9,432!
614
    return -1;
9,432✔
615
  }
616

617
  return 0;
×
618
}
619

620
int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
200✔
621
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
200✔
622
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
200✔
623
  tb_uid_t    uid1 = 0, uid2 = 0;
200✔
624
  int         c;
625

626
  // compare suid
627
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
200✔
628
    return 1;
39✔
629
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
161✔
630
    return -1;
41✔
631
  }
632

633
  // compare column id
634
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
120!
635
    return 1;
×
636
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
120!
637
    return -1;
×
638
  }
639

640
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
120!
641
    metaError("meta/open: incorrect tag idx type.");
×
642
    return TSDB_CODE_FAILED;
×
643
  }
644

645
  // check NULL, NULL is always the smallest
646
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
120!
647
    return -1;
×
648
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
120!
649
    return 1;
×
650
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
120!
651
    // all not NULL, compr tag vals
652
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
120✔
653
    if (func == NULL) {
120!
654
      metaError("meta/open: %s", terrstr());
×
655
      return TSDB_CODE_FAILED;
×
656
    }
657
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
120✔
658
    if (c) return c;
120!
659
  }
660

661
  // both null or tag values are equal, then continue to compare uids
662
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
×
663
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
×
664
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
×
665
  } else {
666
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
×
667
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
×
668
  }
669

670
  // compare uid
671
  if (uid1 < uid2) {
×
672
    return -1;
×
673
  } else if (uid1 > uid2) {
×
674
    return 1;
×
675
  } else {
676
    return 0;
×
677
  }
678

679
  return 0;
680
}
681

682
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
352✔
683
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
352✔
684
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
352✔
685
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
352!
686
    return 1;
352✔
687
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
×
688
    return -1;
×
689
  }
690

691
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
×
692
    return 1;
×
693
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
×
694
    return -1;
×
695
  }
696

697
  return 0;
×
698
}
699

700
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
701
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
×
702
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
×
703

704
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
×
705
    return 1;
×
706
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
×
707
    return -1;
×
708
  }
709

710
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
×
711
    return 1;
×
712
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
×
713
    return -1;
×
714
  }
715

716
  return 0;
×
717
}
718

719
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
720
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
721
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
722

723
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
724
    return 1;
×
725
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
726
    return -1;
×
727
  }
728

729
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
730
    return 1;
×
731
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
732
    return -1;
×
733
  }
734

735
  return 0;
×
736
}
737

738
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
739
  int32_t uid1 = *(int32_t *)pKey1;
×
740
  int32_t uid2 = *(int32_t *)pKey2;
×
741

742
  if (uid1 > uid2) {
×
743
    return 1;
×
744
  } else if (uid1 < uid2) {
×
745
    return -1;
×
746
  }
747

748
  return 0;
×
749
}
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