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

taosdata / TDengine / #3548

04 Dec 2024 01:03PM UTC coverage: 59.846% (-0.8%) from 60.691%
#3548

push

travis-ci

web-flow
Merge pull request #29033 from taosdata/fix/calculate-vnode-memory-used

fix/calculate-vnode-memory-used

118484 of 254183 branches covered (46.61%)

Branch coverage included in aggregate %.

199691 of 277471 relevant lines covered (71.97%)

18794141.86 hits per line

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

44.98
/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
static 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) {
11,727✔
31
  TdThreadRwlockAttr attr;
32
  (void)taosThreadRwlockAttrInit(&attr);
11,727✔
33
  (void)taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
11,726✔
34
  (void)taosThreadRwlockInit(&pMeta->lock, &attr);
11,725✔
35
  (void)taosThreadRwlockAttrDestroy(&attr);
11,722✔
36
  return;
11,722✔
37
}
38
static void metaDestroyLock(SMeta *pMeta) { (void)taosThreadRwlockDestroy(&pMeta->lock); }
11,739✔
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
static int32_t metaOpenImpl(SVnode *pVnode, SMeta **ppMeta, const char *metaDir, int8_t rollback) {
11,718✔
137
  SMeta  *pMeta = NULL;
11,718✔
138
  int32_t code = 0;
11,718✔
139
  int32_t lino;
140
  int32_t offset;
141
  char    path[TSDB_FILENAME_LEN] = {0};
11,718✔
142
  char    indexFullPath[128] = {0};
11,718✔
143

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

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

153
  if ((pMeta = taosMemoryCalloc(1, sizeof(*pMeta) + strlen(path) + 1)) == NULL) {
11,738!
154
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
155
  }
156

157
  metaInitLock(pMeta);
11,729✔
158

159
  pMeta->path = (char *)&pMeta[1];
11,723✔
160
  strcpy(pMeta->path, path);
11,723✔
161
  int32_t ret = taosRealPath(pMeta->path, NULL, strlen(path) + 1);
11,723✔
162

163
  pMeta->pVnode = pVnode;
11,734✔
164

165
  // create path if not created yet
166
  code = taosMkDir(pMeta->path);
11,734✔
167
  TSDB_CHECK_CODE(code, lino, _exit);
11,736!
168

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

174
  // open pTbDb
175
  code = tdbTbOpen("table.db", sizeof(STbDbKey), -1, tbDbKeyCmpr, pMeta->pEnv, &pMeta->pTbDb, 0);
11,737✔
176
  TSDB_CHECK_CODE(code, lino, _exit);
11,739!
177

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

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

186
  // open pNameIdx
187
  code = tdbTbOpen("name.idx", -1, sizeof(tb_uid_t), NULL, pMeta->pEnv, &pMeta->pNameIdx, 0);
11,739✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
11,738!
189

190
  // open pCtbIdx
191
  code = tdbTbOpen("ctb.idx", sizeof(SCtbIdxKey), -1, ctbIdxKeyCmpr, pMeta->pEnv, &pMeta->pCtbIdx, 0);
11,738✔
192
  TSDB_CHECK_CODE(code, lino, _exit);
11,738!
193

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

198
  sprintf(indexFullPath, "%s/%s", pMeta->path, "invert");
11,738✔
199
  ret = taosMkDir(indexFullPath);
11,738✔
200

201
  SIndexOpts opts = {.cacheSize = 8 * 1024 * 1024};
11,737✔
202
  code = indexOpen(&opts, indexFullPath, (SIndex **)&pMeta->pTagIvtIdx);
11,737✔
203
  TSDB_CHECK_CODE(code, lino, _exit);
11,725!
204

205
  code = tdbTbOpen("tag.idx", -1, 0, tagIdxKeyCmpr, pMeta->pEnv, &pMeta->pTagIdx, 0);
11,725✔
206
  TSDB_CHECK_CODE(code, lino, _exit);
11,739!
207

208
  // open pTtlMgr ("ttlv1.idx")
209
  char logPrefix[128] = {0};
11,739✔
210
  sprintf(logPrefix, "vgId:%d", TD_VID(pVnode));
11,739✔
211
  code = ttlMgrOpen(&pMeta->pTtlMgr, pMeta->pEnv, 0, logPrefix, tsTtlFlushThreshold);
11,739✔
212
  TSDB_CHECK_CODE(code, lino, _exit);
11,739!
213

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

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

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

226
  code = tdbTbOpen("stream.task.db", sizeof(int64_t), -1, taskIdxKeyCmpr, pMeta->pEnv, &pMeta->pStreamDb, 0);
11,738✔
227
  TSDB_CHECK_CODE(code, lino, _exit);
11,737!
228

229
  code = metaCacheOpen(pMeta);
11,737✔
230
  TSDB_CHECK_CODE(code, lino, _exit);
11,738!
231

232
  code = metaInitTbFilterCache(pMeta);
11,738✔
233
  TSDB_CHECK_CODE(code, lino, _exit);
11,738!
234

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

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

252
bool generateNewMeta = false;
253

254
static int32_t metaGenerateNewMeta(SMeta **ppMeta) {
×
255
  SMeta  *pNewMeta = NULL;
×
256
  SMeta  *pMeta = *ppMeta;
×
257
  SVnode *pVnode = pMeta->pVnode;
×
258

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

261
  // Open a new meta for orgainzation
262
  int32_t code = metaOpenImpl(pMeta->pVnode, &pNewMeta, VNODE_META_TMP_DIR, false);
×
263
  if (code) {
×
264
    return code;
×
265
  }
266

267
  code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL);
×
268
  if (code) {
×
269
    return code;
×
270
  }
271

272
  // i == 0, scan super table
273
  // i == 1, scan normal table and child table
274
  for (int i = 0; i < 2; i++) {
×
275
    TBC    *uidCursor = NULL;
×
276
    int32_t counter = 0;
×
277

278
    code = tdbTbcOpen(pMeta->pUidIdx, &uidCursor, NULL);
×
279
    if (code) {
×
280
      metaError("vgId:%d failed to open uid index cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
×
281
      return code;
×
282
    }
283

284
    code = tdbTbcMoveToFirst(uidCursor);
×
285
    if (code) {
×
286
      metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
×
287
      tdbTbcClose(uidCursor);
×
288
      return code;
×
289
    }
290

291
    for (;;) {
×
292
      const void *pKey;
293
      int         kLen;
294
      const void *pVal;
295
      int         vLen;
296

297
      if (tdbTbcGet(uidCursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
298
        break;
×
299
      }
300

301
      tb_uid_t    uid = *(tb_uid_t *)pKey;
×
302
      SUidIdxVal *pUidIdxVal = (SUidIdxVal *)pVal;
×
303
      if ((i == 0 && (pUidIdxVal->suid && pUidIdxVal->suid == uid))          // super table
×
304
          || (i == 1 && (pUidIdxVal->suid == 0 || pUidIdxVal->suid != uid))  // normal table and child table
×
305
      ) {
306
        counter++;
×
307
        if (i == 0) {
×
308
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, "super", uid);
×
309
        } else {
310
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter,
×
311
                   pUidIdxVal->suid == 0 ? "normal" : "child", uid);
312
        }
313

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

340
      code = tdbTbcMoveToNext(uidCursor);
×
341
      if (code) {
×
342
        metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
×
343
        return code;
×
344
      }
345
    }
346

347
    tdbTbcClose(uidCursor);
×
348
  }
349

350
  code = metaCommit(pNewMeta, pNewMeta->txn);
×
351
  if (code) {
×
352
    metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
353
    return code;
×
354
  }
355

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

362
  if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) {
×
363
    metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
364
  }
365
  metaClose(&pNewMeta);
×
366
  metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode));
×
367
  return 0;
×
368
}
369

370
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
11,738✔
371
  if (generateNewMeta) {
11,738!
372
    char path[TSDB_FILENAME_LEN] = {0};
×
373
    char oldMetaPath[TSDB_FILENAME_LEN] = {0};
×
374
    char newMetaPath[TSDB_FILENAME_LEN] = {0};
×
375
    char backupMetaPath[TSDB_FILENAME_LEN] = {0};
×
376

377
    vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN);
×
378
    snprintf(oldMetaPath, sizeof(oldMetaPath) - 1, "%s%s%s", path, TD_DIRSEP, VNODE_META_DIR);
×
379
    snprintf(newMetaPath, sizeof(newMetaPath) - 1, "%s%s%s", path, TD_DIRSEP, VNODE_META_TMP_DIR);
×
380
    snprintf(backupMetaPath, sizeof(backupMetaPath) - 1, "%s%s%s", path, TD_DIRSEP, VNODE_META_BACKUP_DIR);
×
381

382
    bool oldMetaExist = taosCheckExistFile(oldMetaPath);
×
383
    bool newMetaExist = taosCheckExistFile(newMetaPath);
×
384
    bool backupMetaExist = taosCheckExistFile(backupMetaPath);
×
385

386
    if ((!backupMetaExist && !oldMetaExist && newMetaExist)     // case 2
×
387
        || (backupMetaExist && !oldMetaExist && !newMetaExist)  // case 4
×
388
        || (backupMetaExist && oldMetaExist && newMetaExist)    // case 8
×
389
    ) {
390
      metaError("vgId:%d invalid meta state, please check", TD_VID(pVnode));
×
391
      return TSDB_CODE_FAILED;
×
392
    } else if ((backupMetaExist && oldMetaExist && !newMetaExist)       // case 7
×
393
               || (!backupMetaExist && !oldMetaExist && !newMetaExist)  // case 1
×
394
    ) {
395
      return metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
×
396
    } else if (backupMetaExist && !oldMetaExist && newMetaExist) {
×
397
      if (taosRenameFile(newMetaPath, oldMetaPath) != 0) {
×
398
        metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
399
        return terrno;
×
400
      }
401
      return metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
×
402
    } else {
403
      int32_t code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
×
404
      if (code) {
×
405
        return code;
×
406
      }
407

408
      code = metaGenerateNewMeta(ppMeta);
×
409
      if (code) {
×
410
        metaError("vgId:%d failed to generate new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
411
      }
412

413
      metaClose(ppMeta);
×
414
      if (taosRenameFile(oldMetaPath, backupMetaPath) != 0) {
×
415
        metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
416
        return terrno;
×
417
      }
418

419
      // rename the new meta to old meta
420
      if (taosRenameFile(newMetaPath, oldMetaPath) != 0) {
×
421
        metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
422
        return terrno;
×
423
      }
424
      code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false);
×
425
      if (code) {
×
426
        metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
427
        return code;
×
428
      }
429
    }
430

431
  } else {
432
    return metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
11,738✔
433
  }
434

435
  return TSDB_CODE_SUCCESS;
×
436
}
437

438
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
11,739✔
439
  int32_t code = TSDB_CODE_SUCCESS;
11,739✔
440
  int32_t lino;
441
  SMeta  *pMeta = *ppMeta;
11,739✔
442

443
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
11,739!
444
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
445
    TSDB_CHECK_CODE(code, lino, _exit);
×
446

447
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
448
    TSDB_CHECK_CODE(code, lino, _exit);
×
449

450
    code = metaCommit(pMeta, pMeta->txn);
×
451
    TSDB_CHECK_CODE(code, lino, _exit);
×
452
  }
453

454
_exit:
11,738✔
455
  if (code) {
11,738!
456
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
457
    metaCleanup(ppMeta);
×
458
  }
459
  return code;
11,738✔
460
}
461

462
void metaClose(SMeta **ppMeta) {
11,730✔
463
  metaCleanup(ppMeta);
11,730✔
464
  return;
11,738✔
465
}
466

467
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
×
468
  int32_t code = 0;
×
469
  metaWLock(pMeta);
×
470
  code = tdbAlter(pMeta->pEnv, nPage);
×
471
  metaULock(pMeta);
×
472

473
  if (code) {
×
474
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
475
  }
476
  return code;
×
477
}
478

479
void metaRLock(SMeta *pMeta) {
49,453,481✔
480
  metaTrace("meta rlock %p", &pMeta->lock);
49,453,481✔
481
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
49,453,486!
482
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
483
  }
484
}
49,475,033✔
485

486
void metaWLock(SMeta *pMeta) {
619,831✔
487
  metaTrace("meta wlock %p", &pMeta->lock);
619,831✔
488
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
619,831!
489
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
490
  }
491
}
620,104✔
492

493
void metaULock(SMeta *pMeta) {
50,082,035✔
494
  metaTrace("meta ulock %p", &pMeta->lock);
50,082,035✔
495
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
50,082,036!
496
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
497
  }
498
}
50,091,645✔
499

500
static void metaCleanup(SMeta **ppMeta) {
11,724✔
501
  SMeta *pMeta = *ppMeta;
11,724✔
502
  if (pMeta) {
11,724✔
503
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
11,721✔
504
    if (pMeta->pEnv) metaAbort(pMeta);
11,740✔
505
    if (pMeta->pCache) metaCacheClose(pMeta);
11,739✔
506
#ifdef BUILD_NO_CALL
507
    if (pMeta->pIdx) metaCloseIdx(pMeta);
508
#endif
509
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
11,740✔
510
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
11,740✔
511
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
11,740✔
512
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
11,740✔
513
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
11,740✔
514
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
11,740✔
515
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
11,740✔
516
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
11,740✔
517
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
11,740✔
518
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
11,740✔
519
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
11,740✔
520
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
11,740✔
521
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
11,740✔
522
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
11,740✔
523
    metaDestroyLock(pMeta);
11,740✔
524

525
    taosMemoryFreeClear(*ppMeta);
11,739!
526
  }
527
}
11,742✔
528

529
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
167,799,100✔
530
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
167,799,100✔
531
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
167,799,100✔
532

533
  if (pTbDbKey1->version > pTbDbKey2->version) {
167,799,100✔
534
    return 1;
77,205,961✔
535
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
90,593,139✔
536
    return -1;
55,085,594✔
537
  }
538

539
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
35,507,545✔
540
    return 1;
786,155✔
541
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
34,721,390✔
542
    return -1;
512,426✔
543
  }
544

545
  return 0;
34,208,964✔
546
}
547

548
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
1,547,004✔
549
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
1,547,004✔
550
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
1,547,004✔
551

552
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
1,547,004✔
553
    return 1;
541,259✔
554
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
1,005,745✔
555
    return -1;
196,209✔
556
  }
557

558
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
809,536✔
559
    return 1;
31,911✔
560
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
777,625✔
561
    return -1;
169,448✔
562
  }
563

564
  return 0;
608,177✔
565
}
566

567
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
59,054,706✔
568
  tb_uid_t uid1 = *(tb_uid_t *)pKey1;
59,054,706✔
569
  tb_uid_t uid2 = *(tb_uid_t *)pKey2;
59,054,706✔
570

571
  if (uid1 > uid2) {
59,054,706✔
572
    return 1;
34,410,652✔
573
  } else if (uid1 < uid2) {
24,644,054✔
574
    return -1;
11,316,398✔
575
  }
576

577
  return 0;
13,327,656✔
578
}
579

580
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
17,633,156✔
581
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
17,633,156✔
582
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
17,633,156✔
583

584
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
17,633,156✔
585
    return 1;
6,349,858✔
586
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
11,283,298✔
587
    return -1;
1,961,933✔
588
  }
589

590
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
9,321,365✔
591
    return 1;
1,973,914✔
592
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
7,347,451✔
593
    return -1;
7,105,208✔
594
  }
595

596
  return 0;
242,243✔
597
}
598

599
static int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
4,088,174✔
600
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
4,088,174✔
601
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
4,088,174✔
602
  tb_uid_t    uid1 = 0, uid2 = 0;
4,088,174✔
603
  int         c;
604

605
  // compare suid
606
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
4,088,174✔
607
    return 1;
12,716✔
608
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
4,075,458✔
609
    return -1;
501,703✔
610
  }
611

612
  // compare column id
613
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
3,573,755✔
614
    return 1;
733✔
615
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
3,573,022✔
616
    return -1;
572✔
617
  }
618

619
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
3,572,450!
620
    metaError("meta/open: incorrect tag idx type.");
×
621
    return TSDB_CODE_FAILED;
×
622
  }
623

624
  // check NULL, NULL is always the smallest
625
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
3,572,450✔
626
    return -1;
657✔
627
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
3,571,793✔
628
    return 1;
1,902✔
629
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
3,569,891!
630
    // all not NULL, compr tag vals
631
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
3,569,752✔
632
    if (func == NULL) {
3,569,734!
633
      metaError("meta/open: %s", terrstr());
×
634
      return TSDB_CODE_FAILED;
×
635
    }
636
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
3,569,734✔
637
    if (c) return c;
3,569,710✔
638
  }
639

640
  // both null or tag values are equal, then continue to compare uids
641
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
213,580✔
642
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
162,226✔
643
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
162,226✔
644
  } else {
645
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
51,354✔
646
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
51,354✔
647
  }
648

649
  // compare uid
650
  if (uid1 < uid2) {
213,580✔
651
    return -1;
1,293✔
652
  } else if (uid1 > uid2) {
212,287✔
653
    return 1;
199,642✔
654
  } else {
655
    return 0;
12,645✔
656
  }
657

658
  return 0;
659
}
660

661
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
1,527,881✔
662
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
1,527,881✔
663
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
1,527,881✔
664
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
1,527,881✔
665
    return 1;
1,094,245✔
666
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
433,636✔
667
    return -1;
26,032✔
668
  }
669

670
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
407,604✔
671
    return 1;
391,641✔
672
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
15,963✔
673
    return -1;
5,099✔
674
  }
675

676
  return 0;
10,864✔
677
}
678

679
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
59,721✔
680
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
59,721✔
681
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
59,721✔
682

683
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
59,721✔
684
    return 1;
1,098✔
685
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
58,623✔
686
    return -1;
4,222✔
687
  }
688

689
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
54,401✔
690
    return 1;
48,612✔
691
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
5,789✔
692
    return -1;
3,854✔
693
  }
694

695
  return 0;
1,935✔
696
}
697

698
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
699
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
700
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
701

702
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
703
    return 1;
×
704
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
705
    return -1;
×
706
  }
707

708
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
709
    return 1;
×
710
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
711
    return -1;
×
712
  }
713

714
  return 0;
×
715
}
716

717
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
718
  int32_t uid1 = *(int32_t *)pKey1;
×
719
  int32_t uid2 = *(int32_t *)pKey2;
×
720

721
  if (uid1 > uid2) {
×
722
    return 1;
×
723
  } else if (uid1 < uid2) {
×
724
    return -1;
×
725
  }
726

727
  return 0;
×
728
}
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

© 2025 Coveralls, Inc