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

taosdata / TDengine / #5049

11 May 2026 06:30AM UTC coverage: 73.313% (+0.09%) from 73.222%
#5049

push

travis-ci

web-flow
feat: refactor taosdump code to improve backup speed and compression ratio (#35292)

6625 of 8435 new or added lines in 28 files covered. (78.54%)

2491 existing lines in 142 files now uncovered.

281233 of 383605 relevant lines covered (73.31%)

132489999.79 hits per line

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

66.3
/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 "dmRepair.h"
17
#include "meta.h"
18
#include "thash.h"
19
#include "vnd.h"
20

21
#ifndef NO_UNALIGNED_ACCESS
22
#define TDB_KEY_ALIGN(k1, k2, kType)
23
#else
24
#define TDB_KEY_ALIGN(k1, k2, kType)   \
25
  kType _k1, _k2;                      \
26
  if (((uintptr_t)(k1) & 7)) {         \
27
    memcpy(&_k1, (k1), sizeof(kType)); \
28
    (k1) = &_k1;                       \
29
  }                                    \
30
  if (((uintptr_t)(k2) & 7)) {         \
31
    memcpy(&_k2, (k2), sizeof(kType)); \
32
    (k2) = &_k2;                       \
33
  }
34
#endif
35

36
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
37
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
38
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
39
int        tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
40
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
41
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
42
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
43

44
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
45
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
46

47
static void metaInitLock(SMeta *pMeta) {
5,006,613✔
48
  TdThreadRwlockAttr attr;
5,001,872✔
49
  (void)taosThreadRwlockAttrInit(&attr);
5,006,613✔
50
  (void)taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
5,005,816✔
51
  (void)taosThreadRwlockInit(&pMeta->lock, &attr);
5,006,613✔
52
  (void)taosThreadRwlockAttrDestroy(&attr);
5,005,794✔
53
  return;
5,004,935✔
54
}
55
static void metaDestroyLock(SMeta *pMeta) { (void)taosThreadRwlockDestroy(&pMeta->lock); }
5,007,040✔
56

57
static void metaCleanup(SMeta **ppMeta);
58

59
static void doScan(SMeta *pMeta) {
×
60
  TBC    *cursor = NULL;
×
61
  int32_t code;
62

63
  // open file to write
64
  char path[TSDB_FILENAME_LEN] = {0};
×
65
  snprintf(path, TSDB_FILENAME_LEN - 1, "%s%s", pMeta->path, TD_DIRSEP "scan.txt");
×
66
  TdFilePtr fp = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
×
67
  if (fp == NULL) {
×
68
    metaError("failed to open file:%s, reason:%s", path, tstrerror(terrno));
×
69
    return;
×
70
  }
71

72
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
×
73
  if (code) {
×
74
    if (taosCloseFile(&fp) != 0) {
×
75
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
76
    }
77
    metaError("failed to open table.db cursor, reason:%s", tstrerror(terrno));
×
78
    return;
×
79
  }
80

81
  code = tdbTbcMoveToFirst(cursor);
×
82
  if (code) {
×
83
    if (taosCloseFile(&fp) != 0) {
×
84
      metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
85
    }
86
    tdbTbcClose(cursor);
×
87
    metaError("failed to move to first, reason:%s", tstrerror(terrno));
×
88
    return;
×
89
  }
90

91
  for (;;) {
×
92
    const void *pKey;
×
93
    int         kLen;
×
94
    const void *pVal;
×
95
    int         vLen;
×
96
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
97
      break;
×
98
    }
99

100
    // decode entry
101
    SDecoder   dc = {0};
×
102
    SMetaEntry me = {0};
×
103

104
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
×
105

106
    if (metaDecodeEntry(&dc, &me) < 0) {
×
107
      metaError("Failed to decode entry, uid:%" PRId64 ", reason:%s", me.uid, tstrerror(terrno));
×
108
      tDecoderClear(&dc);
×
109
      break;
×
110
    }
111

112
    // skip deleted entry
113
    if (tdbTbGet(pMeta->pUidIdx, &me.uid, sizeof(me.uid), NULL, NULL) == 0) {
×
114
      // print entry
115
      char buf[1024] = {0};
×
116
      if (me.type == TSDB_SUPER_TABLE) {
×
117
        snprintf(buf, sizeof(buf) - 1, "type: super table, version:%" PRId64 " uid: %" PRId64 " name: %s\n", me.version,
×
118
                 me.uid, me.name);
119

120
      } else if (me.type == TSDB_CHILD_TABLE) {
×
121
        snprintf(buf, sizeof(buf) - 1,
×
122
                 "type: child table, version:%" PRId64 " uid: %" PRId64 " name: %s suid:%" PRId64 "\n", me.version,
123
                 me.uid, me.name, me.ctbEntry.suid);
124
      } else {
125
        snprintf(buf, sizeof(buf) - 1, "type: normal table, version:%" PRId64 " uid: %" PRId64 " name: %s\n",
×
126
                 me.version, me.uid, me.name);
127
      }
128

129
      if (taosWriteFile(fp, buf, strlen(buf)) < 0) {
×
130
        metaError("failed to write file:%s, reason:%s", path, tstrerror(terrno));
×
131
        tDecoderClear(&dc);
×
132
        break;
×
133
      }
134
    }
135

136
    tDecoderClear(&dc);
×
137

138
    if (tdbTbcMoveToNext(cursor) < 0) {
×
139
      break;
×
140
    }
141
  }
142

143
  tdbTbcClose(cursor);
×
144

145
  // close file
146
  if (taosFsyncFile(fp) < 0) {
×
147
    metaError("failed to fsync file:%s, reason:%s", path, tstrerror(terrno));
×
148
  }
149
  if (taosCloseFile(&fp) < 0) {
×
150
    metaError("failed to close file:%s, reason:%s", path, tstrerror(terrno));
×
151
  }
152
}
153

154
int32_t metaOpenImpl(SVnode *pVnode, SMeta **ppMeta, const char *metaDir, int8_t rollback) {
5,004,825✔
155
  SMeta  *pMeta = NULL;
5,004,825✔
156
  int32_t code = 0;
5,005,877✔
157
  int32_t lino = 0;
5,005,877✔
158
  int32_t offset;
159
  int32_t pathLen = 0;
5,005,877✔
160
  char    path[TSDB_FILENAME_LEN] = {0};
5,005,877✔
161
  char    indexFullPath[128] = {0};
5,005,252✔
162

163
  // create handle
164
  vnodeGetPrimaryPath(pVnode, false, path, TSDB_FILENAME_LEN);
5,002,830✔
165
  offset = strlen(path);
5,007,040✔
166
  snprintf(path + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
5,007,040✔
167

168
  if (strncmp(metaDir, VNODE_META_TMP_DIR, strlen(VNODE_META_TMP_DIR)) == 0) {
5,005,960✔
169
    taosRemoveDir(path);
1,844✔
170
  }
171

172
  pathLen = strlen(path) + 1;
5,005,960✔
173
  if ((pMeta = taosMemoryCalloc(1, sizeof(*pMeta) + pathLen)) == NULL) {
5,005,960✔
174
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
175
  }
176

177
  metaInitLock(pMeta);
5,005,765✔
178

179
  pMeta->path = (char *)&pMeta[1];
5,004,935✔
180
  tstrncpy(pMeta->path, path, pathLen);
5,005,761✔
181
  int32_t ret = taosRealPath(pMeta->path, NULL, strlen(path) + 1);
5,005,765✔
182

183
  pMeta->pVnode = pVnode;
5,005,739✔
184

185
  // create path if not created yet
186
  code = taosMkDir(pMeta->path);
5,005,739✔
187
  TSDB_CHECK_CODE(code, lino, _exit);
5,004,801✔
188

189
  // open env
190
  code = tdbOpen(pMeta->path, pVnode->config.szPage, pVnode->config.szCache, &pMeta->pEnv, rollback,
5,004,801✔
191
                 &(pVnode->config.tdbEncryptData));
192
  TSDB_CHECK_CODE(code, lino, _exit);
5,004,597✔
193

194
  // open pTbDb
195
  code = tdbTbOpen("table.db", sizeof(STbDbKey), -1, tbDbKeyCmpr, pMeta->pEnv, &pMeta->pTbDb, 0);
5,004,597✔
196
  TSDB_CHECK_CODE(code, lino, _exit);
5,005,031✔
197

198
  // open pSkmDb
199
  code = tdbTbOpen("schema.db", sizeof(SSkmDbKey), -1, skmDbKeyCmpr, pMeta->pEnv, &pMeta->pSkmDb, 0);
5,005,031✔
200
  TSDB_CHECK_CODE(code, lino, _exit);
5,005,686✔
201

202
  // open pSkmExtDb
203
  code = tdbTbOpen("schema_ext.db", sizeof(SSkmDbKey), -1, skmDbKeyCmpr, pMeta->pEnv, &pMeta->pSkmExtDb, 0);
5,005,686✔
204
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,728✔
205

206
  // open pUidIdx
207
  code = tdbTbOpen("uid.idx", sizeof(tb_uid_t), sizeof(SUidIdxVal), uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pUidIdx, 0);
5,006,728✔
208
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,115✔
209

210
  // open pNameIdx
211
  code = tdbTbOpen("name.idx", -1, sizeof(tb_uid_t), NULL, pMeta->pEnv, &pMeta->pNameIdx, 0);
5,006,115✔
212
  TSDB_CHECK_CODE(code, lino, _exit);
5,005,325✔
213

214
  // open pCtbIdx
215
  code = tdbTbOpen("ctb.idx", sizeof(SCtbIdxKey), -1, ctbIdxKeyCmpr, pMeta->pEnv, &pMeta->pCtbIdx, 0);
5,005,325✔
216
  TSDB_CHECK_CODE(code, lino, _exit);
5,005,691✔
217

218
  // open pSuidIdx
219
  code = tdbTbOpen("suid.idx", sizeof(tb_uid_t), 0, uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pSuidIdx, 0);
5,005,691✔
220
  TSDB_CHECK_CODE(code, lino, _exit);
5,007,040✔
221

222
  (void)snprintf(indexFullPath, sizeof(indexFullPath), "%s/%s", pMeta->path, "invert");
5,007,040✔
223
  ret = taosMkDir(indexFullPath);
5,006,961✔
224

225
  SIndexOpts opts = {.cacheSize = 8 * 1024 * 1024};
5,006,170✔
226
  code = indexOpen(&opts, indexFullPath, (SIndex **)&pMeta->pTagIvtIdx);
5,006,961✔
227
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,961✔
228

229
  code = tdbTbOpen("tag.idx", -1, 0, tagIdxKeyCmpr, pMeta->pEnv, &pMeta->pTagIdx, 0);
5,006,961✔
230
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,961✔
231

232
  // open pTtlMgr ("ttlv1.idx")
233
  char logPrefix[128] = {0};
5,006,961✔
234
  (void)snprintf(logPrefix, sizeof(logPrefix), "vgId:%d", TD_VID(pVnode));
5,006,961✔
235
  code = ttlMgrOpen(&pMeta->pTtlMgr, pMeta->pEnv, 0, logPrefix, tsTtlFlushThreshold);
5,001,623✔
236
  TSDB_CHECK_CODE(code, lino, _exit);
5,007,040✔
237

238
  // open pSmaIdx
239
  code = tdbTbOpen("sma.idx", sizeof(SSmaIdxKey), 0, smaIdxKeyCmpr, pMeta->pEnv, &pMeta->pSmaIdx, 0);
5,007,040✔
240
  TSDB_CHECK_CODE(code, lino, _exit);
5,004,616✔
241

242
  // idx table create time
243
  code = tdbTbOpen("ctime.idx", sizeof(SBtimeIdxKey), 0, btimeIdxCmpr, pMeta->pEnv, &pMeta->pBtimeIdx, 0);
5,004,616✔
244
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,371✔
245

246
  // idx num of col, normal table only
247
  code = tdbTbOpen("ncol.idx", sizeof(SNcolIdxKey), 0, ncolIdxCmpr, pMeta->pEnv, &pMeta->pNcolIdx, 0);
5,006,371✔
248
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,024✔
249

250
  code = tdbTbOpen("stream.task.db", sizeof(int64_t), -1, taskIdxKeyCmpr, pMeta->pEnv, &pMeta->pStreamDb, 0);
5,006,024✔
251
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,361✔
252

253
  code = metaCacheOpen(pMeta);
5,006,361✔
254
  TSDB_CHECK_CODE(code, lino, _exit);
5,007,040✔
255

256
  code = metaInitTbFilterCache(pMeta);
5,007,040✔
257
  TSDB_CHECK_CODE(code, lino, _exit);
5,006,361✔
258

259
  pMeta->uidSuidHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
5,006,361✔
260
  TSDB_CHECK_NULL(pMeta->uidSuidHash, code, lino, _exit, terrno);
5,007,040✔
261

262
  pMeta->uidNameHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
5,006,361✔
263
  TSDB_CHECK_NULL(pMeta->uidNameHash, code, lino, _exit, terrno);
5,007,040✔
264

265
#if 0
266
  // Do NOT remove this code, it is used to do debug stuff
267
  doScan(pMeta);
268
#endif
269

270
_exit:
5,007,040✔
271
  if (code) {
5,007,040✔
272
    metaError("vgId:%d %s failed at %s:%d since %s, path:%s", TD_VID(pVnode), __func__, __FILE__, lino, tstrerror(code),
×
273
              path);
274
    metaCleanup(&pMeta);
×
275
    *ppMeta = NULL;
×
276
  } else {
277
    metaDebug("vgId:%d %s success", TD_VID(pVnode), __func__);
5,007,040✔
278
    *ppMeta = pMeta;
5,007,040✔
279
  }
280
  TAOS_RETURN(code);
5,006,361✔
281
}
282

283
void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) {
15,009,809✔
284
  vnodeGetPrimaryPath(pVnode, false, fname, TSDB_FILENAME_LEN);
15,009,809✔
285
  int32_t offset = strlen(fname);
15,021,746✔
286
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir);
15,021,746✔
287
}
15,021,398✔
288

289
static bool metaShouldForceRepair(SVnode *pVnode, EDmRepairStrategy *pStrategy) {
5,004,168✔
290
  if (!dmRepairFlowEnabled()) {
5,004,168✔
291
    return false;
4,999,554✔
292
  }
293

294
  int32_t vgId = TD_VID(pVnode);
5,262✔
295
  const SRepairMetaVnodeOpt *pOpt = dmRepairGetMetaVnodeOpt(vgId);
5,372✔
296
  if (pOpt == NULL) {
5,372✔
297
    return false;
5,050✔
298
  }
299

300
  if (pStrategy != NULL) {
322✔
301
    *pStrategy = pOpt->strategy;
322✔
302
  }
303
  return true;
322✔
304
}
305

306
static int32_t metaCopyDirRecursive(const char *srcDir, const char *dstDir) {
428✔
307
  int32_t code = taosMulMkDir(dstDir);
428✔
308
  if (code != 0) {
428✔
309
    return code;
×
310
  }
311

312
  TdDirPtr pDir = taosOpenDir(srcDir);
428✔
313
  if (pDir == NULL) {
428✔
314
    return terrno;
×
315
  }
316

317
  while (true) {
1,284✔
318
    TdDirEntryPtr pEntry = taosReadDir(pDir);
1,712✔
319
    if (pEntry == NULL) {
1,712✔
320
      break;
428✔
321
    }
322

323
    char *name = taosGetDirEntryName(pEntry);
1,284✔
324
    if (name == NULL || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
1,284✔
325
      continue;
856✔
326
    }
327

328
    char srcPath[TSDB_FILENAME_LEN] = {0};
428✔
329
    char dstPath[TSDB_FILENAME_LEN] = {0};
428✔
330
    snprintf(srcPath, sizeof(srcPath), "%s%s%s", srcDir, TD_DIRSEP, name);
428✔
331
    snprintf(dstPath, sizeof(dstPath), "%s%s%s", dstDir, TD_DIRSEP, name);
428✔
332

333
    if (taosDirEntryIsDir(pEntry)) {
428✔
334
      code = metaCopyDirRecursive(srcPath, dstPath);
214✔
335
    } else {
336
      if (taosCopyFile(srcPath, dstPath) < 0) {
214✔
337
        code = terrno != 0 ? terrno : TSDB_CODE_FAILED;
×
338
      } else {
339
        code = 0;
214✔
340
      }
341
    }
342

343
    if (code != 0) {
428✔
344
      (void)taosCloseDir(&pDir);
×
345
      return code;
×
346
    }
347
  }
348

349
  return taosCloseDir(&pDir);
428✔
350
}
351

352
static int32_t metaBuildRepairBackupDir(SVnode *pVnode, char *buf, int32_t bufLen) {
322✔
353
  const char *root = dmRepairHasBackupPath() ? dmRepairBackupPath() : TD_TMP_DIR_PATH;
322✔
354
  const char *sep = root[strlen(root) - 1] == TD_DIRSEP[0] ? "" : TD_DIRSEP;
322✔
355
  time_t      now = (time_t)taosGetTimestampSec();
322✔
356
  struct tm   tmInfo = {0};
322✔
357
  if (taosLocalTime(&now, &tmInfo, NULL, 0, NULL) == NULL) {
322✔
358
    return TSDB_CODE_FAILED;
×
359
  }
360

361
  char dateBuf[16] = {0};
322✔
362
  if (taosStrfTime(dateBuf, sizeof(dateBuf), "%Y%m%d", &tmInfo) == 0) {
322✔
363
    return TSDB_CODE_FAILED;
×
364
  }
365

366
  snprintf(buf, bufLen, "%s%staos_backup_%s%svnode%d%smeta", root, sep, dateBuf, TD_DIRSEP, TD_VID(pVnode), TD_DIRSEP);
322✔
367
  return TSDB_CODE_SUCCESS;
322✔
368
}
369

370
static int32_t metaBackupCurrentMeta(SVnode *pVnode) {
322✔
371
  char metaDir[TSDB_FILENAME_LEN] = {0};
322✔
372
  char backupDir[TSDB_FILENAME_LEN] = {0};
322✔
373

374
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
322✔
375

376
  int32_t code = metaBuildRepairBackupDir(pVnode, backupDir, sizeof(backupDir));
322✔
377
  if (code != 0) {
322✔
378
    return code;
×
379
  }
380

381
  if (taosCheckExistFile(backupDir)) {
322✔
382
    metaError("vgId:%d repair backup dir already exists: %s", TD_VID(pVnode), backupDir);
108✔
383
    return TSDB_CODE_FS_FILE_ALREADY_EXISTS;
108✔
384
  }
385

386
  code = metaCopyDirRecursive(metaDir, backupDir);
214✔
387
  if (code != 0) {
214✔
388
    metaError("vgId:%d failed to back up meta from %s to %s, reason:%s", TD_VID(pVnode), metaDir, backupDir,
×
389
              tstrerror(code));
390
    return code;
×
391
  }
392

393
  metaInfo("vgId:%d backed up meta to %s", TD_VID(pVnode), backupDir);
214✔
394
  return TSDB_CODE_SUCCESS;
214✔
395
}
396

397
static void metaResetStatisInfo(SMeta *pMeta) {
214✔
398
  pMeta->pVnode->config.vndStats.numOfSTables = 0;
214✔
399
  pMeta->pVnode->config.vndStats.numOfCTables = 0;
214✔
400
  pMeta->pVnode->config.vndStats.numOfNTables = 0;
214✔
401
  pMeta->pVnode->config.vndStats.numOfVTables = 0;
214✔
402
  pMeta->pVnode->config.vndStats.numOfVCTables = 0;
214✔
403
  pMeta->pVnode->config.vndStats.numOfNTimeSeries = 0;
214✔
404
  pMeta->pVnode->config.vndStats.numOfTimeSeries = 0;
214✔
405
  pMeta->pVnode->config.vndStats.numOfRSMAs = 0;
214✔
406
}
214✔
407

408
typedef enum {
409
  E_META_REPAIR_FROM_UID,
410
  E_META_REPAIR_FROM_REDO,
411
} EMetaRepairStrategy;
412

413
static EMetaRepairStrategy metaGetRepairStrategy(EDmRepairStrategy strategy) {
214✔
414
  if (strategy == DM_REPAIR_STRATEGY_META_FROM_REDO) {
214✔
415
    return E_META_REPAIR_FROM_REDO;
×
416
  }
417

418
  return E_META_REPAIR_FROM_UID;
214✔
419
}
420

421
static int32_t metaForceRepairFromUid(SVnode *pVnode, SMeta *pMeta, SMeta *pNewMeta) {
214✔
422
  int32_t code = TSDB_CODE_SUCCESS;
214✔
423

424
  // i == 0, scan super table
425
  // i == 1, scan normal table and child table
426
  for (int i = 0; i < 2; i++) {
642✔
427
    TBC    *uidCursor = NULL;
428✔
428
    int32_t counter = 0;
428✔
429

430
    code = tdbTbcOpen(pMeta->pUidIdx, &uidCursor, NULL);
428✔
431
    if (code) {
428✔
432
      metaError("vgId:%d failed to open uid index cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
×
433
      return code;
×
434
    }
435

436
    code = tdbTbcMoveToFirst(uidCursor);
428✔
437
    if (code) {
428✔
438
      metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
×
439
      tdbTbcClose(uidCursor);
×
440
      return code;
×
441
    }
442

443
    for (;;) {
428✔
444
      const void *pKey;
856✔
445
      int         kLen;
856✔
446
      const void *pVal;
856✔
447
      int         vLen;
856✔
448

449
      if (tdbTbcGet(uidCursor, &pKey, &kLen, &pVal, &vLen) < 0) {
856✔
450
        break;
428✔
451
      }
452

453
      tb_uid_t    uid = *(tb_uid_t *)pKey;
428✔
454
      SUidIdxVal *pUidIdxVal = (SUidIdxVal *)pVal;
428✔
455
      if ((i == 0 && (pUidIdxVal->suid && pUidIdxVal->suid == uid))          // super table
428✔
456
          || (i == 1 && (pUidIdxVal->suid == 0 || pUidIdxVal->suid != uid))  // normal table and child table
428✔
457
      ) {
458
        counter++;
214✔
459
        if (i == 0) {
214✔
460
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, "super", uid);
×
461
        } else {
462
          metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter,
214✔
463
                   pUidIdxVal->suid == 0 ? "normal" : "child", uid);
464
        }
465

466
        // fetch table entry
467
        void *value = NULL;
214✔
468
        int   valueSize = 0;
214✔
469
        if (tdbTbGet(pMeta->pTbDb,
214✔
470
                     &(STbDbKey){
214✔
471
                         .version = pUidIdxVal->version,
214✔
472
                         .uid = uid,
473
                     },
474
                     sizeof(uid), &value, &valueSize) == 0) {
475
          SDecoder   dc = {0};
214✔
476
          SMetaEntry me = {0};
214✔
477
          tDecoderInit(&dc, value, valueSize);
214✔
478
          if (metaDecodeEntry(&dc, &me) == 0) {
214✔
479
            if (me.type == TSDB_CHILD_TABLE &&
214✔
480
                tdbTbGet(pMeta->pUidIdx, &me.ctbEntry.suid, sizeof(me.ctbEntry.suid), NULL, NULL) != 0) {
×
481
              metaError("vgId:%d failed to get super table uid:%" PRId64 " for child table uid:%" PRId64,
×
482
                        TD_VID(pVnode), me.ctbEntry.suid, uid);
483
            } else if (metaHandleEntry2(pNewMeta, &me) != 0) {
214✔
484
              metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid);
×
485
            }
486
          }
487
          tDecoderClear(&dc);
214✔
488
        }
489
        tdbFree(value);
214✔
490
      }
491

492
      code = tdbTbcMoveToNext(uidCursor);
428✔
493
      if (code) {
428✔
494
        metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
×
495
        return code;
×
496
      }
497
    }
498

499
    tdbTbcClose(uidCursor);
428✔
500
  }
501

502
  return code;
214✔
503
}
504

505
static int32_t metaForceRepairFromRedo(SVnode *pVnode, SMeta *pMeta, SMeta *pNewMeta) {
×
506
  int32_t code = TSDB_CODE_SUCCESS;
×
507

508
  TBC *cursor = NULL;
×
509

510
  code = tdbTbcOpen(pMeta->pTbDb, &cursor, NULL);
×
511
  if (code) {
×
512
    metaError("vgId:%d failed to open table.db cursor, reason:%s", TD_VID(pVnode), tstrerror(code));
×
513
    return code;
×
514
  }
515

516
  code = tdbTbcMoveToFirst(cursor);
×
517
  if (code) {
×
518
    metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code));
×
519
    tdbTbcClose(cursor);
×
520
    return code;
×
521
  }
522

523
  while (true) {
×
524
    const void *pKey;
×
525
    int         kLen;
×
526
    const void *pVal;
×
527
    int         vLen;
×
528

529
    if (tdbTbcGet(cursor, &pKey, &kLen, &pVal, &vLen) < 0) {
×
530
      break;
×
531
    }
532

533
    STbDbKey  *pKeyEntry = (STbDbKey *)pKey;
×
534
    SDecoder   dc = {0};
×
535
    SMetaEntry me = {0};
×
536

537
    tDecoderInit(&dc, (uint8_t *)pVal, vLen);
×
538
    if (metaDecodeEntry(&dc, &me) < 0) {
×
539
      tDecoderClear(&dc);
×
540
      break;
×
541
    }
542

543
    if (metaHandleEntry2(pNewMeta, &me) != 0) {
×
544
      metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), pKeyEntry->uid);
×
545
      tDecoderClear(&dc);
×
546
      break;
×
547
    }
548
    tDecoderClear(&dc);
×
549

550
    code = tdbTbcMoveToNext(cursor);
×
551
    if (code) {
×
552
      metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code));
×
553
      break;
×
554
    }
555
  }
556

557
  tdbTbcClose(cursor);
×
558
  return code;
×
559
}
560

561
int32_t metaForceRepairCleanupTmpDir(const char *tmpDir) {
×
562
  if (tmpDir == NULL || tmpDir[0] == '\0' || !taosCheckExistFile(tmpDir)) {
×
563
    return 0;
×
564
  }
565

566
  taosRemoveDir(tmpDir);
×
567
  return 0;
×
568
}
569

570
static int32_t metaForceRepair(SMeta **ppMeta, EDmRepairStrategy repairStrategy) {
322✔
571
  int32_t code = TSDB_CODE_SUCCESS;
322✔
572
  SMeta  *pNewMeta = NULL;
322✔
573
  SMeta  *pMeta = *ppMeta;
322✔
574
  SVnode *pVnode = pMeta->pVnode;
322✔
575
  bool    cleanupTmpDirOnError = false;
322✔
576
  char    metaDir[TSDB_FILENAME_LEN] = {0};
322✔
577
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
322✔
578
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
322✔
579

580
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
322✔
581
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
322✔
582
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
322✔
583

584
  metaInfo("vgId:%d start to generate new meta", TD_VID(pMeta->pVnode));
322✔
585

586
  code = metaBackupCurrentMeta(pVnode);
322✔
587
  if (code != 0) {
322✔
588
    metaError("vgId:%d failed to back up current meta, reason:%s", TD_VID(pVnode), tstrerror(code));
108✔
589
    return code;
108✔
590
  }
591
  // Reset statistics info
592
  metaResetStatisInfo(pMeta);
214✔
593

594
  // Open a new meta for organization
595
  code = metaOpenImpl(pMeta->pVnode, &pNewMeta, VNODE_META_TMP_DIR, false);
214✔
596
  if (code) {
214✔
597
    goto _exit;
×
598
  }
599
  cleanupTmpDirOnError = true;
214✔
600

601
  code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL);
214✔
602
  if (code) {
214✔
603
    goto _exit;
×
604
  }
605

606
  EMetaRepairStrategy strategy = metaGetRepairStrategy(repairStrategy);
214✔
607
  if (strategy == E_META_REPAIR_FROM_UID) {
214✔
608
    code = metaForceRepairFromUid(pVnode, pMeta, pNewMeta);
214✔
609
    if (code) {
214✔
610
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
611
      goto _exit;
×
612
    }
613
  } else if (strategy == E_META_REPAIR_FROM_REDO) {
×
614
    code = metaForceRepairFromRedo(pVnode, pMeta, pNewMeta);
×
615
    if (code) {
×
616
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
617
      goto _exit;
×
618
    }
619
  }
620

621
  code = metaCommit(pNewMeta, pNewMeta->txn);
214✔
622
  if (code) {
214✔
623
    metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
624
    goto _exit;
×
625
  }
626

627
  code = metaFinishCommit(pNewMeta, pNewMeta->txn);
214✔
628
  if (code) {
214✔
629
    metaError("vgId:%d failed to finish commit, reason:%s", TD_VID(pVnode), tstrerror(code));
×
630
    goto _exit;
×
631
  }
632

633
  if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) {
214✔
634
    metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
635
  }
636
  metaClose(&pNewMeta);
214✔
637
  cleanupTmpDirOnError = false;
214✔
638
  metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode));
214✔
639

640
  // Commit the new metadata
641
  metaClose(ppMeta);
214✔
642
  if (taosRenameFile(metaDir, metaBackupDir) != 0) {
214✔
643
    metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
644
    return terrno;
×
645
  }
646

647
  // rename the new meta to old meta
648
  if (taosRenameFile(metaTempDir, metaDir) != 0) {
214✔
649
    metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno));
×
650
    return terrno;
×
651
  }
652

653
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false);
214✔
654
  if (code) {
214✔
655
    metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code));
×
656
    return code;
×
657
  }
658

659
  metaInfo("vgId:%d successfully opened new meta", TD_VID(pVnode));
214✔
660

661
  return 0;
214✔
662

663
_exit:
×
664
  if (pNewMeta != NULL) {
×
665
    metaClose(&pNewMeta);
×
666
  }
667

668
  if (cleanupTmpDirOnError) {
×
669
    int32_t cleanupCode = metaForceRepairCleanupTmpDir(metaTempDir);
×
670
    if (cleanupCode != 0) {
×
671
      metaError("vgId:%d failed to clean tmp meta dir:%s, reason:%s", TD_VID(pVnode), metaTempDir,
×
672
                tstrerror(cleanupCode));
673
      if (code == 0) {
×
674
        code = cleanupCode;
×
675
      }
676
    }
677
  }
678

679
  return code;
×
680
}
681

682
static int32_t metaRollbackFromRepair(SVnode *pVnode) {
4,998,325✔
683
  int32_t code = TSDB_CODE_SUCCESS;
4,998,325✔
684
  char    metaDir[TSDB_FILENAME_LEN] = {0};
4,998,325✔
685
  char    metaBackupDir[TSDB_FILENAME_LEN] = {0};
5,004,982✔
686
  char    metaTempDir[TSDB_FILENAME_LEN] = {0};
5,003,475✔
687

688
  vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir);
5,003,930✔
689
  vnodeGetMetaPath(pVnode, VNODE_META_BACKUP_DIR, metaBackupDir);
5,004,756✔
690
  vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir);
5,004,982✔
691

692
  bool metaExists = taosCheckExistFile(metaDir);
5,004,982✔
693
  bool metaBackupExists = taosCheckExistFile(metaBackupDir);
5,004,051✔
694
  bool metaTempExists = taosCheckExistFile(metaTempDir);
5,003,613✔
695

696
  if ((!metaBackupExists && !metaExists && metaTempExists)     //
5,004,982✔
697
      || (metaBackupExists && !metaExists && !metaTempExists)  //
5,004,982✔
698
      || (metaBackupExists && metaExists && metaTempExists)    //
5,004,982✔
699
  ) {
UNCOV
700
    metaError("vgId:%d, invalid meta state, please check!", TD_VID(pVnode));
×
UNCOV
701
    TAOS_RETURN(TSDB_CODE_FAILED);
×
702
  } else if (!metaBackupExists && metaExists && metaTempExists) {
5,004,982✔
703
    taosRemoveDir(metaTempDir);
×
704
  } else if (metaBackupExists && !metaExists && metaTempExists) {
5,004,982✔
705
    code = taosRenameFile(metaTempDir, metaDir);
×
706
    if (code) {
×
707
      metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
708
      TAOS_RETURN(code);
×
709
    }
710
    taosRemoveDir(metaBackupDir);
×
711
  } else if (metaBackupExists && metaExists && !metaTempExists) {
5,004,982✔
712
    taosRemoveDir(metaBackupDir);
211✔
713
  }
714
  return code;
5,003,310✔
715
}
716

717
static int32_t metaForceRepairIfShould(SVnode *pVnode, SMeta **ppMeta) {
5,004,168✔
718
  int32_t           code = TSDB_CODE_SUCCESS;
5,004,168✔
719
  EDmRepairStrategy strategy = DM_REPAIR_STRATEGY_META_FROM_UID;
5,004,168✔
720
  bool              shouldForceRepair = metaShouldForceRepair(pVnode, &strategy);
5,004,168✔
721

722
  // Check if meta should repair
723
  if (!shouldForceRepair) {
5,004,224✔
724
    metaDebug("vgId:%d, meta should not repair!", TD_VID(pVnode));
5,003,902✔
725
    return code;
5,003,902✔
726
  }
727

728
  // Do repair
729
  code = metaForceRepair(ppMeta, strategy);
322✔
730
  if (code) {
322✔
731
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
108✔
732
    return code;
108✔
733
  }
734

735
  return code;
214✔
736
}
737

738
int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) {
5,000,639✔
739
  int32_t code = TSDB_CODE_SUCCESS;
5,000,639✔
740

741
  // Rollback if in the middle stage of a repair mode
742
  code = metaRollbackFromRepair(pVnode);
5,000,639✔
743
  if (code) {
5,003,310✔
744
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
745
    TAOS_RETURN(code);
×
746
  }
747

748
  // Do open meta
749
  code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback);
5,003,310✔
750
  if (code) {
5,004,224✔
751
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
752
    TAOS_RETURN(code);
×
753
  }
754

755
  // Repair meta data if should
756
  code = metaForceRepairIfShould(pVnode, ppMeta);
5,004,224✔
757
  if (code) {
5,004,224✔
758
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
108✔
759
    TAOS_RETURN(code);
108✔
760
  }
761

762
  return TSDB_CODE_SUCCESS;
5,004,116✔
763
}
764

765
int32_t metaUpgrade(SVnode *pVnode, SMeta **ppMeta) {
4,999,431✔
766
  int32_t code = TSDB_CODE_SUCCESS;
4,999,431✔
767
  int32_t lino;
768
  SMeta  *pMeta = *ppMeta;
4,999,431✔
769

770
  if (ttlMgrNeedUpgrade(pMeta->pEnv)) {
4,999,510✔
771
    code = metaBegin(pMeta, META_BEGIN_HEAP_OS);
×
772
    TSDB_CHECK_CODE(code, lino, _exit);
×
773

774
    code = ttlMgrUpgrade(pMeta->pTtlMgr, pMeta);
×
775
    TSDB_CHECK_CODE(code, lino, _exit);
×
776

777
    code = metaCommit(pMeta, pMeta->txn);
×
778
    TSDB_CHECK_CODE(code, lino, _exit);
×
779
  }
780

781
_exit:
4,999,017✔
782
  if (code) {
4,999,017✔
783
    metaError("vgId:%d %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code));
×
784
    metaCleanup(ppMeta);
×
785
  }
786
  return code;
4,999,017✔
787
}
788

789
void metaClose(SMeta **ppMeta) {
5,006,522✔
790
  metaCleanup(ppMeta);
5,006,522✔
791
  return;
5,007,040✔
792
}
793

794
int metaAlterCache(SMeta *pMeta, int32_t nPage) {
5,112✔
795
  int32_t code = 0;
5,112✔
796
  metaWLock(pMeta);
5,112✔
797
  code = tdbAlter(pMeta->pEnv, nPage);
5,112✔
798
  metaULock(pMeta);
5,112✔
799

800
  if (code) {
5,112✔
801
    metaError("vgId:%d %s failed since %s", TD_VID(pMeta->pVnode), __func__, tstrerror(code));
×
802
  }
803
  return code;
5,112✔
804
}
805

806
void metaRLock(SMeta *pMeta) {
2,147,483,647✔
807
  metaTrace("meta rlock %p", &pMeta->lock);
2,147,483,647✔
808
  if (taosThreadRwlockRdlock(&pMeta->lock) != 0) {
2,147,483,647✔
809
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
810
  }
811
}
2,147,483,647✔
812

813
void metaWLock(SMeta *pMeta) {
175,062,459✔
814
  metaTrace("meta wlock %p", &pMeta->lock);
175,062,459✔
815
  if (taosThreadRwlockWrlock(&pMeta->lock) != 0) {
175,062,459✔
816
    metaError("vgId:%d failed to lock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
817
  }
818
}
175,073,452✔
819

820
void metaULock(SMeta *pMeta) {
2,147,483,647✔
821
  metaTrace("meta ulock %p", &pMeta->lock);
2,147,483,647✔
822
  if (taosThreadRwlockUnlock(&pMeta->lock) != 0) {
2,147,483,647✔
823
    metaError("vgId:%d failed to unlock %p", TD_VID(pMeta->pVnode), &pMeta->lock);
×
824
  }
825
}
2,147,483,647✔
826

827
static void metaCleanup(SMeta **ppMeta) {
5,006,522✔
828
  SMeta *pMeta = *ppMeta;
5,006,522✔
829
  if (pMeta) {
5,006,522✔
830
    metaInfo("vgId:%d meta clean up, path:%s", TD_VID(pMeta->pVnode), pMeta->path);
5,006,262✔
831
    if (pMeta->pEnv) metaAbort(pMeta);
5,007,255✔
832
    if (pMeta->pCache) metaCacheClose(pMeta);
5,007,040✔
833
#ifdef BUILD_NO_CALL
834
    if (pMeta->pIdx) metaCloseIdx(pMeta);
835
#endif
836
    if (pMeta->pStreamDb) tdbTbClose(pMeta->pStreamDb);
5,007,040✔
837
    if (pMeta->pNcolIdx) tdbTbClose(pMeta->pNcolIdx);
5,007,040✔
838
    if (pMeta->pBtimeIdx) tdbTbClose(pMeta->pBtimeIdx);
5,007,040✔
839
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
5,007,040✔
840
    if (pMeta->pTtlMgr) ttlMgrClose(pMeta->pTtlMgr);
5,007,040✔
841
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
5,007,040✔
842
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
5,007,040✔
843
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
5,007,040✔
844
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
5,007,040✔
845
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
5,007,040✔
846
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
5,007,040✔
847
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
5,007,040✔
848
    if (pMeta->pSkmExtDb) tdbTbClose(pMeta->pSkmExtDb);
5,007,040✔
849
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
5,007,040✔
850
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
5,007,040✔
851
    metaDestroyLock(pMeta);
5,007,040✔
852

853
    taosHashCleanup(pMeta->uidSuidHash);
5,007,040✔
854
    taosHashCleanup(pMeta->uidNameHash);
5,007,040✔
855
    taosMemoryFreeClear(*ppMeta);
5,007,040✔
856
  }
857
}
5,007,300✔
858

859
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
2,147,483,647✔
860
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
2,147,483,647✔
861
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
2,147,483,647✔
862

863
  TDB_KEY_ALIGN(pTbDbKey1, pTbDbKey2, STbDbKey);
864

865
  if (pTbDbKey1->version > pTbDbKey2->version) {
2,147,483,647✔
866
    return 1;
2,147,483,647✔
867
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
2,147,483,647✔
868
    return -1;
2,147,483,647✔
869
  }
870

871
  if (pTbDbKey1->uid > pTbDbKey2->uid) {
2,147,483,647✔
872
    return 1;
893,566,768✔
873
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
2,147,483,647✔
874
    return -1;
719,577,493✔
875
  }
876

877
  return 0;
2,147,483,647✔
878
}
879

880
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
537,544,205✔
881
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
537,544,205✔
882
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;
537,544,205✔
883

884
  TDB_KEY_ALIGN(pSkmDbKey1, pSkmDbKey2, SSkmDbKey);
885

886
  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
537,544,205✔
887
    return 1;
171,917,343✔
888
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
365,826,173✔
889
    return -1;
119,208,272✔
890
  }
891

892
  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
246,618,265✔
893
    return 1;
134,913,638✔
894
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
111,681,045✔
895
    return -1;
28,547,149✔
896
  }
897

898
  return 0;
83,150,561✔
899
}
900

901
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
2,147,483,647✔
902
  tb_uid_t uid1 = taosGetInt64Aligned((int64_t *)pKey1);
2,147,483,647✔
903
  tb_uid_t uid2 = taosGetInt64Aligned((int64_t *)pKey2);
2,147,483,647✔
904

905
  if (uid1 > uid2) {
2,147,483,647✔
906
    return 1;
2,147,483,647✔
907
  } else if (uid1 < uid2) {
2,147,483,647✔
908
    return -1;
1,614,886,072✔
909
  }
910

911
  return 0;
1,278,343,289✔
912
}
913

914
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
1,220,295,321✔
915
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
1,220,295,321✔
916
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;
1,220,295,321✔
917

918
  TDB_KEY_ALIGN(pCtbIdxKey1, pCtbIdxKey2, SCtbIdxKey);
919

920
  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
1,220,295,321✔
921
    return 1;
141,653,021✔
922
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
1,078,743,695✔
923
    return -1;
45,066,944✔
924
  }
925

926
  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
1,033,679,252✔
927
    return 1;
538,810,235✔
928
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
494,978,259✔
929
    return -1;
427,090,479✔
930
  }
931

932
  return 0;
67,898,903✔
933
}
934

935
int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
939,892,854✔
936
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
939,892,854✔
937
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
939,892,854✔
938
  tb_uid_t    uid1 = 0, uid2 = 0;
939,892,854✔
939
  int         c;
940

941
  TDB_KEY_ALIGN(pTagIdxKey1, pTagIdxKey2, STagIdxKey);
942

943
  // compare suid
944
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
939,892,854✔
945
    return 1;
2,917,439✔
946
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
936,993,407✔
947
    return -1;
16,127,610✔
948
  }
949

950
  // compare column id
951
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
920,860,544✔
952
    return 1;
33,486,279✔
953
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
887,373,570✔
954
    return -1;
16,986,187✔
955
  }
956

957
  if (pTagIdxKey1->type != pTagIdxKey2->type) {
870,383,650✔
958
    metaError("meta/open: incorrect tag idx type.");
×
959
    return TSDB_CODE_FAILED;
×
960
  }
961

962
  // check NULL, NULL is always the smallest
963
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
870,369,407✔
964
    return -1;
255,475✔
965
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
870,128,114✔
966
    return 1;
827,291✔
967
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
869,307,340✔
968
    // all not NULL, compr tag vals
969
    __compar_fn_t func = getComparFunc(pTagIdxKey1->type, 0);
857,532,821✔
970
    if (func == NULL) {
857,534,830✔
971
      metaError("meta/open: %s", terrstr());
×
972
      return TSDB_CODE_FAILED;
×
973
    }
974
    c = func(pTagIdxKey1->data, pTagIdxKey2->data);
857,534,830✔
975
    if (c) return c;
857,517,499✔
976
  }
977

978
  // both null or tag values are equal, then continue to compare uids
979
  if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
140,803,341✔
980
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
57,873,553✔
981
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
57,878,563✔
982
  } else {
983
    uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
82,921,635✔
984
    uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
82,919,906✔
985
  }
986

987
  // compare uid
988
  if (uid1 < uid2) {
140,799,214✔
989
    return -1;
12,054,105✔
990
  } else if (uid1 > uid2) {
128,745,109✔
991
    return 1;
111,703,869✔
992
  } else {
993
    return 0;
17,041,240✔
994
  }
995

996
  return 0;
997
}
998

999
static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
289,911,046✔
1000
  SBtimeIdxKey *pBtimeIdxKey1 = (SBtimeIdxKey *)pKey1;
289,911,046✔
1001
  SBtimeIdxKey *pBtimeIdxKey2 = (SBtimeIdxKey *)pKey2;
289,911,046✔
1002

1003
  TDB_KEY_ALIGN(pBtimeIdxKey1, pBtimeIdxKey2, SBtimeIdxKey);
1004

1005
  if (pBtimeIdxKey1->btime > pBtimeIdxKey2->btime) {
289,911,046✔
1006
    return 1;
260,455,681✔
1007
  } else if (pBtimeIdxKey1->btime < pBtimeIdxKey2->btime) {
29,458,960✔
1008
    return -1;
2,429,017✔
1009
  }
1010

1011
  if (pBtimeIdxKey1->uid > pBtimeIdxKey2->uid) {
27,030,539✔
1012
    return 1;
23,630,999✔
1013
  } else if (pBtimeIdxKey1->uid < pBtimeIdxKey2->uid) {
3,399,160✔
1014
    return -1;
552,737✔
1015
  }
1016

1017
  return 0;
2,846,423✔
1018
}
1019

1020
static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
1021
  SNcolIdxKey *pNcolIdxKey1 = (SNcolIdxKey *)pKey1;
×
1022
  SNcolIdxKey *pNcolIdxKey2 = (SNcolIdxKey *)pKey2;
×
1023

1024
  TDB_KEY_ALIGN(pNcolIdxKey1, pNcolIdxKey2, SNcolIdxKey);
1025

1026
  if (pNcolIdxKey1->ncol > pNcolIdxKey2->ncol) {
×
1027
    return 1;
×
1028
  } else if (pNcolIdxKey1->ncol < pNcolIdxKey2->ncol) {
×
1029
    return -1;
×
1030
  }
1031

1032
  if (pNcolIdxKey1->uid > pNcolIdxKey2->uid) {
×
1033
    return 1;
×
1034
  } else if (pNcolIdxKey1->uid < pNcolIdxKey2->uid) {
×
1035
    return -1;
×
1036
  }
1037

1038
  return 0;
×
1039
}
1040

1041
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
1042
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
×
1043
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;
×
1044

1045
  TDB_KEY_ALIGN(pSmaIdxKey1, pSmaIdxKey2, SSmaIdxKey);
1046

1047
  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
×
1048
    return 1;
×
1049
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
×
1050
    return -1;
×
1051
  }
1052

1053
  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
×
1054
    return 1;
×
1055
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
×
1056
    return -1;
×
1057
  }
1058

1059
  return 0;
×
1060
}
1061

1062
static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
×
1063
  int32_t uid1 = *(int32_t *)pKey1;
×
1064
  int32_t uid2 = *(int32_t *)pKey2;
×
1065

1066
  if (uid1 > uid2) {
×
1067
    return 1;
×
1068
  } else if (uid1 < uid2) {
×
1069
    return -1;
×
1070
  }
1071

1072
  return 0;
×
1073
}
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