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

taosdata / TDengine / #4835

31 Oct 2025 03:37AM UTC coverage: 58.506% (-0.3%) from 58.764%
#4835

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149104 of 324176 branches covered (45.99%)

Branch coverage included in aggregate %.

198232 of 269498 relevant lines covered (73.56%)

236558065.91 hits per line

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

61.34
/source/dnode/vnode/src/tsdb/tsdbFS2.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 "cos.h"
17
#include "tsdbFS2.h"
18
#include "tsdbUpgrade.h"
19
#include "vnd.h"
20

21
#define BLOCK_COMMIT_FACTOR 3
22

23
typedef struct STFileHashEntry {
24
  struct STFileHashEntry *next;
25
  char                    fname[TSDB_FILENAME_LEN];
26
} STFileHashEntry;
27

28
typedef struct {
29
  int32_t           numFile;
30
  int32_t           numBucket;
31
  STFileHashEntry **buckets;
32
} STFileHash;
33

34
static const char *gCurrentFname[] = {
35
    [TSDB_FCURRENT] = "current.json",
36
    [TSDB_FCURRENT_C] = "current.c.json",
37
    [TSDB_FCURRENT_M] = "current.m.json",
38
};
39

40
static int32_t create_fs(STsdb *pTsdb, STFileSystem **fs) {
12,203,963✔
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
12,203,963!
42
  if (fs[0] == NULL) {
12,236,213!
43
    return terrno;
×
44
  }
45

46
  fs[0]->tsdb = pTsdb;
12,235,250✔
47
  int32_t code = tsem_init(&fs[0]->canEdit, 0, 1);
12,235,250✔
48
  if (code) {
12,224,862!
49
    taosMemoryFree(fs[0]);
×
50
    return code;
×
51
  }
52

53
  fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
12,224,862✔
54
  fs[0]->neid = 0;
12,229,281✔
55
  TARRAY2_INIT(fs[0]->fSetArr);
12,226,661✔
56
  TARRAY2_INIT(fs[0]->fSetArrTmp);
12,229,358✔
57

58
  return 0;
12,224,204✔
59
}
60

61
static void destroy_fs(STFileSystem **fs) {
12,236,472✔
62
  if (fs[0] == NULL) return;
12,236,472!
63

64
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
12,238,253!
65
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
12,238,413!
66
  if (tsem_destroy(&fs[0]->canEdit) != 0) {
12,238,413!
67
    tsdbError("failed to destroy semaphore");
×
68
  }
69
  taosMemoryFree(fs[0]);
12,235,472!
70
  fs[0] = NULL;
12,239,226✔
71
}
72

73
void current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
76,904,520✔
74
  int32_t offset = 0;
76,904,520✔
75

76
  vnodeGetPrimaryPath(pTsdb->pVnode, false, fname, TSDB_FILENAME_LEN);
76,904,520✔
77
  offset = strlen(fname);
76,915,275!
78
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s%s", TD_DIRSEP, pTsdb->name, TD_DIRSEP,
76,911,145✔
79
           gCurrentFname[ftype]);
76,900,909✔
80
}
76,909,667✔
81

82
static int32_t save_json(const cJSON *json, const char *fname) {
21,462,922✔
83
  int32_t   code = 0;
21,462,922✔
84
  int32_t   lino;
85
  char     *data = NULL;
21,462,922✔
86
  TdFilePtr fp = NULL;
21,462,922✔
87

88
  data = cJSON_PrintUnformatted(json);
21,462,922✔
89
  if (data == NULL) {
21,471,285!
90
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
91
  }
92

93
  fp = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
21,471,285✔
94
  if (fp == NULL) {
21,467,201!
95
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
96
  }
97

98
  if (taosWriteFile(fp, data, strlen(data)) < 0) {
21,467,201!
99
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
100
  }
101

102
  if (taosFsyncFile(fp) < 0) {
21,468,723!
103
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
104
  }
105

106
_exit:
21,467,970✔
107
  if (code) {
21,468,650!
108
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
109
  }
110
  taosMemoryFree(data);
21,470,690!
111
  taosCloseFileWithLog(&fp);
21,471,387!
112
  return code;
21,468,323✔
113
}
114

115
static int32_t load_json(const char *fname, cJSON **json) {
3,123,708✔
116
  int32_t code = 0;
3,123,708✔
117
  int32_t lino;
118
  char   *data = NULL;
3,123,708✔
119

120
  TdFilePtr fp = taosOpenFile(fname, TD_FILE_READ);
3,123,708✔
121
  if (fp == NULL) {
3,123,708!
122
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
123
  }
124

125
  int64_t size;
3,122,767✔
126
  code = taosFStatFile(fp, &size, NULL);
3,123,708✔
127
  if (code != 0) {
3,123,708!
128
    TSDB_CHECK_CODE(code, lino, _exit);
×
129
  }
130

131
  data = taosMemoryMalloc(size + 1);
3,123,708!
132
  if (data == NULL) {
3,127,583!
133
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
134
  }
135

136
  if (taosReadFile(fp, data, size) < 0) {
3,127,583!
137
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
138
  }
139
  data[size] = '\0';
3,128,200✔
140

141
  json[0] = cJSON_Parse(data);
3,126,620✔
142
  if (json[0] == NULL) {
3,125,669!
143
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
144
  }
145

146
_exit:
3,129,163✔
147
  if (code) {
3,129,163!
148
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
149
    json[0] = NULL;
×
150
  }
151
  taosCloseFileWithLog(&fp);
3,129,163!
152
  taosMemoryFree(data);
3,129,163!
153
  return code;
3,129,163✔
154
}
155

156
int32_t save_fs(const TFileSetArray *arr, const char *fname) {
21,467,402✔
157
  int32_t code = 0;
21,467,402✔
158
  int32_t lino = 0;
21,467,402✔
159

160
  cJSON *json = cJSON_CreateObject();
21,467,402✔
161
  if (json == NULL) {
21,469,853!
162
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
163
  }
164

165
  // fmtv
166
  if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
21,469,853!
167
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
168
  }
169

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
21,465,389✔
172
  if (!ajson) {
21,467,271!
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
156,978,178✔
177
    cJSON *item = cJSON_CreateObject();
135,508,977✔
178
    if (!item) {
135,506,876!
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
135,506,876✔
182

183
    code = tsdbTFileSetToJson(fset, item);
135,509,856✔
184
    TSDB_CHECK_CODE(code, lino, _exit);
135,510,907!
185
  }
186

187
  code = save_json(json, fname);
21,464,386✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
21,463,791!
189

190
_exit:
21,463,791✔
191
  if (code) {
21,463,791!
192
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
193
  }
194
  cJSON_Delete(json);
21,463,791✔
195
  return code;
21,465,916✔
196
}
197

198
static int32_t load_fs(STsdb *pTsdb, const char *fname, TFileSetArray *arr) {
3,123,708✔
199
  int32_t code = 0;
3,123,708✔
200
  int32_t lino = 0;
3,123,708✔
201

202
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
3,123,708!
203

204
  // load json
205
  cJSON *json = NULL;
3,123,708✔
206
  code = load_json(fname, &json);
3,123,708✔
207
  TSDB_CHECK_CODE(code, lino, _exit);
3,129,163!
208

209
  // parse json
210
  const cJSON *item1;
211

212
  /* fmtv */
213
  item1 = cJSON_GetObjectItem(json, "fmtv");
3,129,163✔
214
  if (cJSON_IsNumber(item1)) {
3,128,212!
215
    if (item1->valuedouble != 1) {
3,125,669!
216
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
217
    }
218
  } else {
219
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
220
  }
221

222
  /* fset */
223
  item1 = cJSON_GetObjectItem(json, "fset");
3,127,583✔
224
  if (cJSON_IsArray(item1)) {
3,129,163!
225
    const cJSON *item2;
226
    cJSON_ArrayForEach(item2, item1) {
5,591,921!
227
      STFileSet *fset;
2,462,388✔
228
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
2,462,758✔
229
      TSDB_CHECK_CODE(code, lino, _exit);
2,461,382!
230

231
      code = TARRAY2_APPEND(arr, fset);
2,461,382!
232
      TSDB_CHECK_CODE(code, lino, _exit);
2,461,382!
233
    }
234
    TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
3,126,207✔
235
  } else {
236
    code = TSDB_CODE_FILE_CORRUPTED;
×
237
    TSDB_CHECK_CODE(code, lino, _exit);
×
238
  }
239

240
_exit:
3,125,691✔
241
  if (code) {
3,128,212!
242
    tsdbError("%s failed at %s:%d since %s, fname:%s", __func__, __FILE__, lino, tstrerror(code), fname);
×
243
  }
244
  if (json) {
3,126,632!
245
    cJSON_Delete(json);
3,126,632✔
246
  }
247
  return code;
3,129,163✔
248
}
249

250
static int32_t apply_commit(STFileSystem *fs) {
12,357,492✔
251
  int32_t        code = 0;
12,357,492✔
252
  int32_t        lino;
253
  TFileSetArray *fsetArray1 = fs->fSetArr;
12,357,492✔
254
  TFileSetArray *fsetArray2 = fs->fSetArrTmp;
12,357,492✔
255
  int32_t        i1 = 0, i2 = 0;
12,357,492✔
256

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
148,066,731✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
135,709,485✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
135,713,959✔
260

261
    if (fset1 && fset2) {
135,712,561✔
262
      if (fset1->fid < fset2->fid) {
14,597,214✔
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
196,123✔
265
        i1++;
196,123✔
266
      } else if (fset1->fid > fset2->fid) {
14,401,091✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
23,651✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
23,651!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
23,651✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
23,651!
272
        i1++;
23,651✔
273
        i2++;
23,651✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
14,377,440✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
14,377,440!
278
        i1++;
14,377,440✔
279
        i2++;
14,377,440✔
280
      }
281
    } else if (fset1) {
121,115,347✔
282
      // delete fset1
283
      tsdbTFileSetRemove(fset1);
8,217✔
284
      i1++;
8,217✔
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
121,107,130✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
121,102,151!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
121,105,752✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
121,105,752!
291
      i1++;
121,105,752✔
292
      i2++;
121,105,752✔
293
    }
294
  }
295

296
_exit:
12,357,382✔
297
  if (code) {
12,357,382!
298
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
299
  }
300
  return code;
12,356,551✔
301
}
302

303
static int32_t commit_edit(STFileSystem *fs) {
12,357,357✔
304
  char current[TSDB_FILENAME_LEN];
12,355,740✔
305
  char current_t[TSDB_FILENAME_LEN];
12,358,312✔
306

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
12,359,929✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
12,359,929✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,028,962✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,330,735✔
312
  }
313

314
  int32_t code;
315
  int32_t lino;
316
  TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit);
12,359,929!
317

318
  code = apply_commit(fs);
12,358,890✔
319
  TSDB_CHECK_CODE(code, lino, _exit);
12,356,310!
320

321
_exit:
12,356,310✔
322
  if (code) {
12,357,812!
323
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
324
              tstrerror(code));
325
  } else {
326
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
12,357,812✔
327
  }
328
  return code;
12,362,580✔
329
}
330

331
// static int32_t
332
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs);
333
static int32_t apply_abort(STFileSystem *fs) { return tsdbFSDoSanAndFix(fs); }
1,190✔
334

335
static int32_t abort_edit(STFileSystem *fs) {
1,190✔
336
  char fname[TSDB_FILENAME_LEN];
1,190✔
337

338
  if (fs->etype == TSDB_FEDIT_COMMIT) {
1,190!
339
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
1,190✔
340
  } else {
341
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
×
342
  }
343

344
  int32_t code;
345
  int32_t lino;
346
  if ((code = taosRemoveFile(fname))) {
1,190!
347
    code = TAOS_SYSTEM_ERROR(code);
×
348
    TSDB_CHECK_CODE(code, lino, _exit);
×
349
  }
350

351
  code = apply_abort(fs);
1,190✔
352
  TSDB_CHECK_CODE(code, lino, _exit);
1,190!
353

354
_exit:
1,190✔
355
  if (code) {
1,190!
356
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
357
              tstrerror(code));
358
  } else {
359
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
1,190!
360
  }
361
  return code;
1,190✔
362
}
363

364
static int32_t tsdbFSDoScanAndFixFile(STFileSystem *fs, const STFileObj *fobj) {
4,128,749✔
365
  int32_t code = 0;
4,128,749✔
366
  int32_t lino = 0;
4,128,749✔
367

368
  // check file existence
369
  if (!taosCheckExistFile(fobj->fname)) {
4,128,749!
370
    bool found = false;
×
371

372
    if (tsSsEnabled && fobj->f->lcn > 1) {
×
373
      char fname1[TSDB_FILENAME_LEN];
×
374
      tsdbTFileLastChunkName(fs->tsdb, fobj->f, fname1);
×
375
      if (!taosCheckExistFile(fname1)) {
×
376
        code = TSDB_CODE_FILE_CORRUPTED;
×
377
        tsdbError("vgId:%d %s failed since file:%s does not exist", TD_VID(fs->tsdb->pVnode), __func__, fname1);
×
378
        return code;
×
379
      }
380

381
      found = true;
×
382
    }
383

384
    if (!found) {
×
385
      code = TSDB_CODE_FILE_CORRUPTED;
×
386
      tsdbError("vgId:%d %s failed since file:%s does not exist", TD_VID(fs->tsdb->pVnode), __func__, fobj->fname);
×
387
      return code;
×
388
    }
389
  }
390

391
  return 0;
4,128,749✔
392
}
393

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

396
static int32_t tsdbFSAddEntryToFileObjHash(STFileHash *hash, const char *fname) {
7,245,178✔
397
  STFileHashEntry *entry = taosMemoryMalloc(sizeof(*entry));
7,245,178!
398
  if (entry == NULL) return terrno;
7,245,178!
399

400
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
7,245,178!
401

402
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
7,245,178!
403

404
  entry->next = hash->buckets[idx];
7,245,178✔
405
  hash->buckets[idx] = entry;
7,245,178✔
406
  hash->numFile++;
7,245,178✔
407

408
  return 0;
7,245,178✔
409
}
410

411
static int32_t tsdbFSCreateFileObjHash(STFileSystem *fs, STFileHash *hash) {
3,115,053✔
412
  int32_t code = 0;
3,115,053✔
413
  int32_t lino;
414
  char    fname[TSDB_FILENAME_LEN];
3,114,112✔
415

416
  // init hash table
417
  hash->numFile = 0;
3,115,053✔
418
  hash->numBucket = 4096;
3,115,053✔
419
  hash->buckets = taosMemoryCalloc(hash->numBucket, sizeof(STFileHashEntry *));
3,115,053!
420
  if (hash->buckets == NULL) {
3,115,053!
421
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
422
  }
423

424
  // vnode.json
425
  current_fname(fs->tsdb, fname, TSDB_FCURRENT);
3,115,053✔
426
  code = tsdbFSAddEntryToFileObjHash(hash, fname);
3,115,053✔
427
  TSDB_CHECK_CODE(code, lino, _exit);
3,115,053!
428

429
  // other
430
  STFileSet *fset = NULL;
3,115,053✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
5,571,351✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
12,281,490✔
434
      if (fset->farr[i] != NULL) {
9,825,192✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
1,824,206✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
1,824,206!
437

438
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
1,824,206!
439
          STFileObj *fobj = fset->farr[i];
×
440
          int32_t    lcn = fobj->f->lcn;
×
441
          char       lcn_name[TSDB_FILENAME_LEN];
×
442

443
          snprintf(lcn_name, TSDB_FQDN_LEN, "%s", fobj->fname);
×
444
          char *dot = strrchr(lcn_name, '.');
×
445
          if (dot) {
×
446
            snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - lcn_name), "%d.data", lcn);
×
447

448
            code = tsdbFSAddEntryToFileObjHash(hash, lcn_name);
×
449
            TSDB_CHECK_CODE(code, lino, _exit);
×
450
          }
451
        }
452
      }
453
    }
454

455
    // stt file
456
    SSttLvl *lvl = NULL;
2,456,298✔
457
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
4,705,946✔
458
      STFileObj *fobj;
459
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,555,567✔
460
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
2,305,919✔
461
        TSDB_CHECK_CODE(code, lino, _exit);
2,305,919!
462
      }
463
    }
464
  }
465

466
_exit:
3,115,053✔
467
  if (code) {
3,115,053!
468
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
469
    tsdbFSDestroyFileObjHash(hash);
×
470
  }
471
  return code;
3,115,053✔
472
}
473

474
static const STFileHashEntry *tsdbFSGetFileObjHashEntry(STFileHash *hash, const char *fname) {
7,248,748✔
475
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
7,248,748!
476

477
  STFileHashEntry *entry = hash->buckets[idx];
7,248,748✔
478
  while (entry) {
7,261,487✔
479
    if (strcmp(entry->fname, fname) == 0) {
7,257,917!
480
      return entry;
7,245,178✔
481
    }
482
    entry = entry->next;
12,739✔
483
  }
484

485
  return NULL;
3,570✔
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
3,115,053✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
2,147,483,647✔
490
    STFileHashEntry *entry = hash->buckets[i];
2,147,483,647✔
491
    while (entry) {
2,147,483,647✔
492
      STFileHashEntry *next = entry->next;
7,245,178✔
493
      taosMemoryFree(entry);
7,245,178!
494
      entry = next;
7,245,178✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
3,115,053!
498
  memset(hash, 0, sizeof(*hash));
3,115,053!
499
}
3,115,053✔
500

501
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
3,123,072✔
502
  int32_t code = 0;
3,123,072✔
503
  int32_t lino = 0;
3,123,072✔
504
  int32_t corrupt = false;
3,130,353✔
505

506
  if (fs->tsdb->pVnode->mounted) goto _exit;
3,130,353!
507

508
  {  // scan each file
509
    STFileSet *fset = NULL;
3,115,053✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
5,569,677✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
12,267,441✔
513
        if (fset->farr[ftype] == NULL) continue;
9,812,842✔
514
        STFileObj *fobj = fset->farr[ftype];
1,824,206✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
1,824,206✔
516
        if (code) {
1,824,206!
517
          fset->maxVerValid = (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
518
          corrupt = true;
×
519
        }
520
      }
521

522
      // stt file
523
      SSttLvl *lvl;
524
      TARRAY2_FOREACH(fset->lvlArr, lvl) {
4,702,573✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,550,818✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
2,304,543✔
528
          if (code) {
2,304,245!
529
            fset->maxVerValid =
×
530
                (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
531
            corrupt = true;
×
532
          }
533
        }
534
      }
535
    }
536
  }
537

538
  if (corrupt) {
3,113,473!
539
    tsdbError("vgId:%d, not to clear dangling files due to fset incompleteness", TD_VID(fs->tsdb->pVnode));
×
540
    fs->fsstate = TSDB_FS_STATE_INCOMPLETE;
×
541
    code = 0;
×
542
    goto _exit;
×
543
  }
544

545
  {  // clear unreferenced files
546
    STfsDir *dir = NULL;
3,113,473✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
3,113,473!
548

549
    STFileHash fobjHash = {0};
3,115,053✔
550
    code = tsdbFSCreateFileObjHash(fs, &fobjHash);
3,115,053✔
551
    if (code) goto _close_dir;
3,115,053!
552

553
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
13,478,854✔
554
      if (taosIsDir(file->aname)) continue;
10,363,801✔
555

556
      if (tsdbFSGetFileObjHashEntry(&fobjHash, file->aname) == NULL) {
7,248,748✔
557
        tsdbRemoveFile(file->aname);
3,570✔
558
      }
559
    }
560

561
    tsdbFSDestroyFileObjHash(&fobjHash);
3,115,053✔
562

563
  _close_dir:
3,115,053✔
564
    tfsClosedir(dir);
3,115,053✔
565
  }
566

567
_exit:
3,130,353✔
568
  if (code) {
3,130,353!
569
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
570
  }
571
  return code;
3,130,353✔
572
}
573

574
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
3,126,620✔
575
  fs->neid = 0;
3,126,620✔
576

577
  // get max commit id
578
  const STFileSet *fset;
579
  TARRAY2_FOREACH(fs->fSetArr, fset) { fs->neid = TMAX(fs->neid, tsdbTFileSetMaxCid(fset)); }
5,589,378✔
580

581
  // scan and fix
582
  int32_t code = 0;
3,126,620✔
583
  int32_t lino = 0;
3,126,620✔
584

585
  code = tsdbFSDoSanAndFix(fs);
3,126,620✔
586
  TSDB_CHECK_CODE(code, lino, _exit);
3,129,163!
587

588
_exit:
3,129,163✔
589
  if (code) {
3,129,163!
590
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
591
  }
592
  return code;
3,129,163✔
593
}
594

595
static int32_t tsdbFSDupState(STFileSystem *fs) {
15,483,303✔
596
  int32_t code;
597

598
  const TFileSetArray *src = fs->fSetArr;
15,483,303✔
599
  TFileSetArray       *dst = fs->fSetArrTmp;
15,483,303✔
600

601
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
29,904,781✔
602

603
  const STFileSet *fset1;
604
  TARRAY2_FOREACH(src, fset1) {
32,523,609✔
605
    STFileSet *fset2;
17,044,910✔
606
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
17,045,728✔
607
    if (code) return code;
17,045,728!
608
    code = TARRAY2_APPEND(dst, fset2);
17,045,728✔
609
    if (code) return code;
17,045,728!
610
  }
611

612
  return 0;
15,482,364✔
613
}
614

615
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
12,233,162✔
616
  int32_t code = 0;
12,233,162✔
617
  int32_t lino = 0;
12,233,162✔
618
  STsdb  *pTsdb = fs->tsdb;
12,233,162✔
619

620
  char fCurrent[TSDB_FILENAME_LEN];
12,231,111✔
621
  char cCurrent[TSDB_FILENAME_LEN];
12,232,752✔
622
  char mCurrent[TSDB_FILENAME_LEN];
12,232,752✔
623

624
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
12,232,603✔
625
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
12,240,187✔
626
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
12,240,187✔
627

628
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
12,240,187✔
629
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
3,129,163✔
630
    TSDB_CHECK_CODE(code, lino, _exit);
3,127,583!
631

632
    if (taosCheckExistFile(cCurrent)) {
3,127,583!
633
      // current.c.json exists
634

635
      fs->etype = TSDB_FEDIT_COMMIT;
×
636
      if (rollback) {
×
637
        code = abort_edit(fs);
×
638
        TSDB_CHECK_CODE(code, lino, _exit);
×
639
      } else {
640
        code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
×
641
        TSDB_CHECK_CODE(code, lino, _exit);
×
642

643
        code = commit_edit(fs);
×
644
        TSDB_CHECK_CODE(code, lino, _exit);
×
645
      }
646
    } else if (taosCheckExistFile(mCurrent)) {
3,129,163!
647
      // current.m.json exists
648
      fs->etype = TSDB_FEDIT_MERGE;
×
649
      code = abort_edit(fs);
×
650
      TSDB_CHECK_CODE(code, lino, _exit);
×
651
    }
652

653
    code = tsdbFSDupState(fs);
3,127,583✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
3,126,620!
655

656
    code = tsdbFSScanAndFix(fs);
3,126,620✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
3,129,163!
658
  } else {
659
    code = save_fs(fs->fSetArr, fCurrent);
9,111,024✔
660
    TSDB_CHECK_CODE(code, lino, _exit);
9,106,553!
661
  }
662

663
_exit:
12,234,775✔
664
  if (code) {
12,235,716!
665
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
666
  } else {
667
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
12,235,716!
668
  }
669
  return code;
12,240,187✔
670
}
671

672
static void close_file_system(STFileSystem *fs) {
12,234,642✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
135,827,020✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
135,778,208✔
675
}
12,235,100✔
676

677
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
×
678
  if (pSet1->fid < pSet2->fid) {
×
679
    return -1;
×
680
  } else if (pSet1->fid > pSet2->fid) {
×
681
    return 1;
×
682
  }
683
  return 0;
×
684
}
685

686
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
12,357,249✔
687
  int32_t code = 0;
12,357,249✔
688
  int32_t lino = 0;
12,357,249✔
689

690
  code = tsdbFSDupState(fs);
12,357,249✔
691
  if (code) return code;
12,351,047!
692

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
12,351,047✔
694
  STFileSet      *fset = NULL;
12,356,426✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
12,357,323✔
697
  TSKEY           now = taosGetTimestampMs();
12,359,697✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
159,641,533✔
699
    if (!fset || fset->fid != op->fid) {
147,276,878✔
700
      STFileSet tfset = {.fid = op->fid};
131,031,697✔
701
      fset = &tfset;
131,039,364✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
131,039,321✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
131,039,321✔
704

705
      if (!fset) {
131,039,321✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
121,130,302✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
121,125,484!
708

709
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
121,130,700✔
710
        TSDB_CHECK_CODE(code, lino, _exit);
121,130,700!
711
      }
712
    }
713

714
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
147,287,503✔
715
    TSDB_CHECK_CODE(code, lino, _exit);
147,285,378!
716

717
    if (fid != op->fid) {
147,285,378✔
718
      fid = op->fid;
131,039,856✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
131,041,109✔
720
        fset->lastCommit = now;
127,710,374✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
3,330,735✔
722
        fset->lastCompact = now;
231,305✔
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
3,099,430!
724
        fset->lastMigrate = now;
×
725
      } else if (etype == TSDB_FEDIT_ROLLUP) {
3,099,430✔
726
        fset->lastRollupLevel = fs->rollupLevel;
33,873✔
727
        fset->lastRollup = now;
33,873✔
728
        fset->lastCompact = now;  // rollup implies compact
33,873✔
729
      }
730
    }
731
  }
732

733
  // remove empty empty stt level and empty file set
734
  int32_t i = 0;
12,352,182✔
735
  while (i < TARRAY2_SIZE(fsetArray)) {
148,053,111✔
736
    fset = TARRAY2_GET(fsetArray, i);
135,707,310✔
737

738
    SSttLvl *lvl;
739
    int32_t  j = 0;
135,705,852✔
740
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
279,542,193✔
741
      lvl = TARRAY2_GET(fset->lvlArr, j);
143,838,188✔
742

743
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
143,838,626✔
744
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
3,768,718!
745
      } else {
746
        j++;
140,067,623✔
747
      }
748
    }
749

750
    if (tsdbTFileSetIsEmpty(fset)) {
135,701,888✔
751
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
204,340!
752
    } else {
753
      i++;
135,496,589✔
754
    }
755
  }
756

757
_exit:
12,339,361✔
758
  if (code) {
12,345,860!
759
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
760
  }
761
  return code;
12,345,860✔
762
}
763

764
// return error code
765
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
12,211,832✔
766
  int32_t code;
767
  int32_t lino;
768

769
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
12,211,832✔
770
  TSDB_CHECK_CODE(code, lino, _exit);
12,237,793!
771

772
  code = create_fs(pTsdb, fs);
12,237,793✔
773
  TSDB_CHECK_CODE(code, lino, _exit);
12,220,636!
774

775
  code = open_fs(fs[0], rollback);
12,220,636✔
776
  TSDB_CHECK_CODE(code, lino, _exit);
12,240,187!
777

778
_exit:
12,240,187✔
779
  if (code) {
12,240,187!
780
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
781
    destroy_fs(fs);
×
782
  } else {
783
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
12,240,187!
784
  }
785
  return code;
12,240,187✔
786
}
787

788
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block);
789
extern void tsdbStopAllCompTask(STsdb *tsdb);
790
extern void tsdbStopAllRetentionTask(STsdb *tsdb);
791

792
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
12,325,070✔
793
  STFileSystem *fs = pTsdb->pFS;
12,325,070✔
794
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
12,327,860✔
795
  if (asyncTasks == NULL) {
12,327,633!
796
    return terrno;
×
797
  }
798

799
  (void)taosThreadMutexLock(&pTsdb->mutex);
12,327,633✔
800

801
  // disable
802
  pTsdb->bgTaskDisabled = true;
12,327,860✔
803

804
  // collect channel
805
  STFileSet *fset;
806
  TARRAY2_FOREACH(fs->fSetArr, fset) {
135,924,151✔
807
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
247,201,137!
808
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
247,202,078!
809
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
247,202,078!
810
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
247,202,078✔
811
      taosArrayDestroy(asyncTasks);
3,407✔
812
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
813
      return terrno;
×
814
    }
815
    tsdbFSSetBlockCommit(fset, false);
123,597,632✔
816
  }
817

818
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
12,326,892✔
819

820
  // destroy all channels
821
  for (int32_t k = 0; k < 2; k++) {
36,978,972✔
822
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
1,013,441,504✔
823
      SVATaskID *task = taosArrayGet(asyncTasks, i);
988,782,387✔
824
      if (k == 0) {
988,786,527✔
825
        (void)vnodeACancel(task);
494,392,805✔
826
      } else {
827
        vnodeAWait(task);
494,393,722✔
828
      }
829
    }
830
  }
831
  taosArrayDestroy(asyncTasks);
12,327,860✔
832

833
#ifdef TD_ENTERPRISE
834
  tsdbStopAllCompTask(pTsdb);
12,322,909✔
835
#endif
836
  tsdbStopAllRetentionTask(pTsdb);
12,326,899✔
837
  return 0;
12,326,250✔
838
}
839

840
void tsdbEnableBgTask(STsdb *pTsdb) {
87,673✔
841
  (void)taosThreadMutexLock(&pTsdb->mutex);
87,673✔
842
  pTsdb->bgTaskDisabled = false;
87,673✔
843
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
87,673✔
844
}
87,673✔
845

846
void tsdbCloseFS(STFileSystem **fs) {
12,228,983✔
847
  if (fs[0] == NULL) return;
12,228,983!
848

849
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
12,237,397✔
850
  if (code) {
12,237,616!
851
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID((*fs)->tsdb->pVnode), __func__, __LINE__,
×
852
              tstrerror(code));
853
  }
854
  close_file_system(fs[0]);
12,237,616✔
855
  destroy_fs(fs);
12,234,830✔
856
  return;
12,240,187✔
857
}
858

859
int64_t tsdbFSAllocEid(STFileSystem *fs) {
12,592,065✔
860
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
12,592,065✔
861
  int64_t cid = ++fs->neid;
12,608,654✔
862
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
12,608,654✔
863
  return cid;
12,608,654✔
864
}
865

866
void tsdbFSUpdateEid(STFileSystem *fs, int64_t cid) {
458,848✔
867
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
458,848✔
868
  fs->neid = TMAX(fs->neid, cid);
460,021✔
869
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
460,021✔
870
}
458,848✔
871

872
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
12,358,641✔
873
  int32_t code = 0;
12,358,641✔
874
  int32_t lino;
875
  char    current_t[TSDB_FILENAME_LEN];
12,357,024✔
876

877
  if (etype == TSDB_FEDIT_COMMIT) {
12,359,875✔
878
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,029,140✔
879
  } else {
880
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,330,735✔
881
  }
882

883
  if (tsem_wait(&fs->canEdit) != 0) {
12,354,823!
884
    tsdbError("vgId:%d failed to wait semaphore", TD_VID(fs->tsdb->pVnode));
×
885
  }
886
  fs->etype = etype;
12,358,633✔
887

888
  // edit
889
  code = edit_fs(fs, opArray, etype);
12,357,209✔
890
  TSDB_CHECK_CODE(code, lino, _exit);
12,353,728!
891

892
  // save fs
893
  code = save_fs(fs->fSetArrTmp, current_t);
12,353,728✔
894
  TSDB_CHECK_CODE(code, lino, _exit);
12,359,873!
895

896
_exit:
12,359,873✔
897
  if (code) {
12,359,873!
898
    tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
×
899
              tstrerror(code), etype);
900
  } else {
901
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
12,359,873!
902
  }
903
  return code;
12,361,119✔
904
}
905

906
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
255,786,125✔
907
  if (block) {
255,786,125✔
908
    fset->blockCommit = true;
140,507✔
909
  } else {
910
    fset->blockCommit = false;
255,645,618✔
911
    if (fset->numWaitCommit > 0) {
255,645,618✔
912
      (void)taosThreadCondSignal(&fset->canCommit);
114,127✔
913
    }
914
  }
915
}
255,785,848✔
916

917
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
127,575,516✔
918
  (void)taosThreadMutexLock(&tsdb->mutex);
127,575,516✔
919
  STFileSet *fset;
127,595,134✔
920
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
127,596,109✔
921
  bool blockCommit = false;
127,551,361✔
922
  if (fset) {
127,551,361✔
923
    blockCommit = fset->blockCommit;
6,639,560!
924
  }
925
  if (fset) {
127,551,361✔
926
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
6,753,687!
927
      while (fset->blockCommit) {
928
        fset->numWaitCommit++;
929
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
930
        fset->numWaitCommit--;
931
      }
932
    });
933
  }
934
  if (blockCommit) {
127,510,084✔
935
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
114,127!
936
  }
937
  (void)taosThreadMutexUnlock(&tsdb->mutex);
127,510,084✔
938
  return;
127,519,391✔
939
}
940

941
// IMPORTANT: the caller must hold fs->tsdb->mutex
942
int32_t tsdbFSEditCommit(STFileSystem *fs) {
12,359,929✔
943
  int32_t code = 0;
12,359,929✔
944
  int32_t lino = 0;
12,359,929✔
945

946
  // commit
947
  code = commit_edit(fs);
12,359,929✔
948
  TSDB_CHECK_CODE(code, lino, _exit);
12,359,929!
949

950
  // schedule merge
951
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
12,359,929✔
952
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
12,359,929!
953
    STFileSet *fset;
954
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
144,101,505✔
955
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
132,188,493✔
956
        tsdbFSSetBlockCommit(fset, false);
1,196,182✔
957
        continue;
1,196,182✔
958
      }
959

960
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
130,992,311✔
961
      if (lvl->level != 0) {
130,992,311✔
962
        tsdbFSSetBlockCommit(fset, false);
2,568,454✔
963
        continue;
2,568,454✔
964
      }
965

966
      // bool    skipMerge = false;
967
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
128,423,857✔
968
      if (numFile >= sttTrigger && (!vnodeATaskValid(&fset->mergeTask))) {
128,423,857✔
969
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
3,025,894!
970
        if (arg == NULL) {
3,025,894!
971
          code = terrno;
×
972
          TSDB_CHECK_CODE(code, lino, _exit);
×
973
        }
974

975
        arg->tsdb = fs->tsdb;
3,025,894✔
976
        arg->fid = fset->fid;
3,025,894✔
977

978
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
3,025,894✔
979
        TSDB_CHECK_CODE(code, lino, _exit);
3,025,894!
980
      }
981

982
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
128,423,857✔
983
        tsdbFSSetBlockCommit(fset, true);
140,507✔
984
      } else {
985
        tsdbFSSetBlockCommit(fset, false);
128,283,350✔
986
      }
987
    }
988
  }
989

990
_exit:
12,359,929✔
991
  if (code) {
12,359,929!
992
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
×
993
  } else {
994
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
12,359,929!
995
  }
996
  if (tsem_post(&fs->canEdit) != 0) {
12,359,929!
997
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
998
  }
999
  return code;
12,359,929✔
1000
}
1001

1002
int32_t tsdbFSEditAbort(STFileSystem *fs) {
1,190✔
1003
  int32_t code = abort_edit(fs);
1,190✔
1004
  if (tsem_post(&fs->canEdit) != 0) {
1,190!
1005
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
1006
  }
1007
  return code;
1,190✔
1008
}
1009

1010
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
268,881,049✔
1011
  STFileSet   tfset = {.fid = fid};
268,881,049✔
1012
  STFileSet  *pset = &tfset;
268,893,100✔
1013
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
268,914,283✔
1014
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
268,893,624✔
1015
}
268,905,820✔
1016

1017
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
73,855✔
1018
  int32_t    code = 0;
73,855✔
1019
  STFileSet *fset;
1020
  STFileSet *fset1;
73,855✔
1021

1022
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
73,855!
1023
  if (fsetArr[0] == NULL) return terrno;
73,855!
1024

1025
  TARRAY2_INIT(fsetArr[0]);
73,855✔
1026

1027
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
73,855✔
1028
  TARRAY2_FOREACH(fs->fSetArr, fset) {
73,855!
1029
    code = tsdbTFileSetInitCopy(fs->tsdb, fset, &fset1);
×
1030
    if (code) break;
×
1031

1032
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1033
    if (code) break;
×
1034
  }
1035
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
73,855✔
1036

1037
  if (code) {
73,855!
1038
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1039
    taosMemoryFree(fsetArr[0]);
×
1040
    fsetArr[0] = NULL;
×
1041
  }
1042
  return code;
73,855✔
1043
}
1044

1045
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
77,459✔
1046
  if (fsetArr[0]) {
77,459!
1047
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
81,063!
1048
    taosMemoryFree(fsetArr[0]);
77,459!
1049
    fsetArr[0] = NULL;
77,459✔
1050
  }
1051
}
77,459✔
1052

1053
int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
81,171✔
1054
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
81,171✔
1055
  int32_t code = tsdbFSCreateRefSnapshotWithoutLock(fs, fsetArr);
81,171✔
1056
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
81,171✔
1057
  return code;
81,171✔
1058
}
1059

1060
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
748,456,524✔
1061
  int32_t    code = 0;
748,456,524✔
1062
  STFileSet *fset, *fset1;
748,449,995✔
1063

1064
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
748,618,822✔
1065
  if (fsetArr[0] == NULL) return terrno;
748,658,864!
1066

1067
  TARRAY2_FOREACH(fs->fSetArr, fset) {
2,117,394,353✔
1068
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
1,369,084,650✔
1069
    if (code) break;
1,369,007,733!
1070

1071
    code = TARRAY2_APPEND(fsetArr[0], fset1);
1,369,007,733!
1072
    if (code) {
1,369,039,946!
1073
      tsdbTFileSetClear(&fset1);
×
1074
      break;
×
1075
    }
1076
  }
1077

1078
  if (code) {
748,549,958!
1079
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1080
    taosMemoryFree(fsetArr[0]);
×
1081
    fsetArr[0] = NULL;
×
1082
  }
1083
  return code;
748,549,958✔
1084
}
1085

1086
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
748,681,136✔
1087
  if (fsetArr[0]) {
748,681,136!
1088
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2,117,864,873✔
1089
    taosMemoryFreeClear(fsetArr[0]);
748,741,398!
1090
    fsetArr[0] = NULL;
748,687,316✔
1091
  }
1092
}
748,665,157✔
1093

1094
static SHashObj *tsdbFSetRangeArrayToHash(TFileSetRangeArray *pRanges) {
7,213✔
1095
  int32_t   capacity = TARRAY2_SIZE(pRanges) * 2;
7,213✔
1096
  SHashObj *pHash = taosHashInit(capacity, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
7,213✔
1097
  if (pHash == NULL) {
7,213!
1098
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1099
    return NULL;
×
1100
  }
1101

1102
  for (int32_t i = 0; i < TARRAY2_SIZE(pRanges); i++) {
14,426✔
1103
    STFileSetRange *u = TARRAY2_GET(pRanges, i);
7,213✔
1104
    int32_t         fid = u->fid;
7,213✔
1105
    int32_t         code = taosHashPut(pHash, &fid, sizeof(fid), u, sizeof(*u));
7,213✔
1106
    tsdbDebug("range diff hash fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
7,213!
1107
  }
1108
  return pHash;
7,213✔
1109
}
1110

1111
int32_t tsdbFSCreateCopyRangedSnapshot(STFileSystem *fs, TFileSetRangeArray *pRanges, TFileSetArray **fsetArr,
3,604✔
1112
                                       TFileOpArray *fopArr) {
1113
  int32_t    code = 0;
3,604✔
1114
  STFileSet *fset;
1115
  STFileSet *fset1;
3,604✔
1116
  SHashObj  *pHash = NULL;
3,604✔
1117

1118
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
3,604!
1119
  if (fsetArr == NULL) return terrno;
3,604!
1120
  TARRAY2_INIT(fsetArr[0]);
3,604✔
1121

1122
  if (pRanges) {
3,604!
1123
    pHash = tsdbFSetRangeArrayToHash(pRanges);
3,604✔
1124
    if (pHash == NULL) {
3,604!
1125
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1126
      goto _out;
×
1127
    }
1128
  }
1129

1130
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
3,604✔
1131
  TARRAY2_FOREACH(fs->fSetArr, fset) {
7,208✔
1132
    int64_t ever = VERSION_MAX;
3,604✔
1133
    if (pHash) {
3,604!
1134
      int32_t         fid = fset->fid;
3,604✔
1135
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
3,604✔
1136
      if (u) {
3,604!
1137
        ever = u->sver - 1;
3,604✔
1138
      }
1139
    }
1140

1141
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
3,604✔
1142
    if (code) break;
3,604!
1143

1144
    code = TARRAY2_APPEND(fsetArr[0], fset1);
3,604!
1145
    if (code) break;
3,604!
1146
  }
1147
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
3,604✔
1148

1149
_out:
3,604✔
1150
  if (code) {
3,604!
1151
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1152
    taosMemoryFree(fsetArr[0]);
×
1153
    fsetArr[0] = NULL;
×
1154
  }
1155
  if (pHash) {
3,604!
1156
    taosHashCleanup(pHash);
3,604✔
1157
    pHash = NULL;
3,604✔
1158
  }
1159
  return code;
3,604✔
1160
}
1161

1162
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
3,604✔
1163

1164
int32_t tsdbFSCreateRefRangedSnapshot(STFileSystem *fs, int64_t sver, int64_t ever, TFileSetRangeArray *pRanges,
3,609✔
1165
                                      TFileSetRangeArray **fsrArr) {
1166
  int32_t         code = 0;
3,609✔
1167
  STFileSet      *fset;
1168
  STFileSetRange *fsr1 = NULL;
3,609✔
1169
  SHashObj       *pHash = NULL;
3,609✔
1170

1171
  fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
3,609!
1172
  if (fsrArr[0] == NULL) {
3,609!
1173
    code = terrno;
×
1174
    goto _out;
×
1175
  }
1176

1177
  tsdbInfo("pRanges size:%d", (pRanges == NULL ? 0 : TARRAY2_SIZE(pRanges)));
3,609!
1178
  if (pRanges) {
3,609!
1179
    pHash = tsdbFSetRangeArrayToHash(pRanges);
3,609✔
1180
    if (pHash == NULL) {
3,609!
1181
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1182
      goto _out;
×
1183
    }
1184
  }
1185

1186
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
3,609✔
1187
  TARRAY2_FOREACH(fs->fSetArr, fset) {
7,218✔
1188
    int64_t sver1 = sver;
3,609✔
1189
    int64_t ever1 = ever;
3,609✔
1190

1191
    if (pHash) {
3,609!
1192
      int32_t         fid = fset->fid;
3,609✔
1193
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
3,609✔
1194
      if (u) {
3,609!
1195
        sver1 = u->sver;
3,609✔
1196
        tsdbDebug("range hash get fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
3,609!
1197
      }
1198
    }
1199

1200
    if (sver1 > ever1) {
3,609!
1201
      tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
×
1202
      continue;
×
1203
    }
1204

1205
    tsdbDebug("fsrArr:%p, fid:%d, sver:%" PRId64 ", ever:%" PRId64, fsrArr, fset->fid, sver1, ever1);
3,609!
1206

1207
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
3,609✔
1208
    if (code) break;
3,609!
1209

1210
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
3,609!
1211
    if (code) break;
3,609!
1212

1213
    fsr1 = NULL;
3,609✔
1214
  }
1215
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
3,609✔
1216

1217
  if (code) {
3,609!
1218
    tsdbTFileSetRangeClear(&fsr1);
×
1219
    TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
×
1220
    fsrArr[0] = NULL;
×
1221
  }
1222

1223
_out:
3,609✔
1224
  if (pHash) {
3,609!
1225
    taosHashCleanup(pHash);
3,609✔
1226
    pHash = NULL;
3,609✔
1227
  }
1228
  return code;
3,609✔
1229
}
1230

1231
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
3,609✔
1232

1233
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
131,168,702✔
1234
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1235
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
131,168,702✔
1236

1237
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
131,169,880✔
1238
  if (*fset == NULL) {
131,169,732✔
1239
    return;
120,993,840✔
1240
  }
1241

1242
  struct STFileSetCond *cond = NULL;
10,175,892✔
1243
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,175,892✔
1244
    cond = &(*fset)->conds[0];
6,777,510✔
1245
  } else {
1246
    cond = &(*fset)->conds[1];
3,398,382✔
1247
  }
1248

1249
  while (1) {
1250
    if (cond->running) {
10,175,892!
1251
      cond->numWait++;
×
1252
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
×
1253
      cond->numWait--;
×
1254
    } else {
1255
      cond->running = true;
10,175,892✔
1256
      break;
10,175,892✔
1257
    }
1258
  }
1259

1260
  tsdbTrace("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
10,175,892✔
1261
  return;
10,175,892✔
1262
}
1263

1264
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
10,175,892✔
1265
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1266
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
10,175,892✔
1267

1268
  STFileSet *fset = NULL;
10,175,892✔
1269
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
10,175,892✔
1270
  if (fset == NULL) {
10,175,892!
1271
    return;
×
1272
  }
1273

1274
  struct STFileSetCond *cond = NULL;
10,175,892✔
1275
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,175,892✔
1276
    cond = &fset->conds[0];
6,777,510✔
1277
  } else {
1278
    cond = &fset->conds[1];
3,398,382✔
1279
  }
1280

1281
  cond->running = false;
10,175,892✔
1282
  if (cond->numWait > 0) {
10,175,892!
1283
    (void)taosThreadCondSignal(&cond->cond);
×
1284
  }
1285

1286
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
10,175,892✔
1287
  return;
10,175,892✔
1288
}
1289

1290
struct SFileSetReader {
1291
  STsdb     *pTsdb;
1292
  STFileSet *pFileSet;
1293
  int32_t    fid;
1294
  int64_t    startTime;
1295
  int64_t    endTime;
1296
  int64_t    lastCompactTime;
1297
  int64_t    totalSize;
1298
};
1299

1300
int32_t tsdbFileSetReaderOpen(void *pVnode, struct SFileSetReader **ppReader) {
×
1301
  if (pVnode == NULL || ppReader == NULL) {
×
1302
    return TSDB_CODE_INVALID_PARA;
×
1303
  }
1304

1305
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
×
1306

1307
  (*ppReader) = taosMemoryCalloc(1, sizeof(struct SFileSetReader));
×
1308
  if (*ppReader == NULL) {
×
1309
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, __LINE__,
×
1310
              tstrerror(terrno));
1311
    return terrno;
×
1312
  }
1313

1314
  (*ppReader)->pTsdb = pTsdb;
×
1315
  (*ppReader)->fid = INT32_MIN;
×
1316
  (*ppReader)->pFileSet = NULL;
×
1317

1318
  return TSDB_CODE_SUCCESS;
×
1319
}
1320

1321
static int32_t tsdbFileSetReaderNextNoLock(struct SFileSetReader *pReader) {
×
1322
  STsdb  *pTsdb = pReader->pTsdb;
×
1323
  int32_t code = TSDB_CODE_SUCCESS;
×
1324

1325
  tsdbTFileSetClear(&pReader->pFileSet);
×
1326

1327
  STFileSet *fset = &(STFileSet){
×
1328
      .fid = pReader->fid,
×
1329
  };
1330

1331
  STFileSet **fsetPtr = TARRAY2_SEARCH(pReader->pTsdb->pFS->fSetArr, &fset, tsdbTFileSetCmprFn, TD_GT);
×
1332
  if (fsetPtr == NULL) {
×
1333
    pReader->fid = INT32_MAX;
×
1334
    return TSDB_CODE_NOT_FOUND;
×
1335
  }
1336

1337
  // ref file set
1338
  code = tsdbTFileSetInitRef(pReader->pTsdb, *fsetPtr, &pReader->pFileSet);
×
1339
  if (code) return code;
×
1340

1341
  // get file set details
1342
  pReader->fid = pReader->pFileSet->fid;
×
1343
  tsdbFidKeyRange(pReader->fid, pTsdb->keepCfg.days, pTsdb->keepCfg.precision, &pReader->startTime, &pReader->endTime);
×
1344
  pReader->lastCompactTime = pReader->pFileSet->lastCompact;
×
1345
  pReader->totalSize = 0;
×
1346
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
×
1347
    STFileObj *fobj = pReader->pFileSet->farr[i];
×
1348
    if (fobj) {
×
1349
      pReader->totalSize += fobj->f->size;
×
1350
    }
1351
  }
1352
  SSttLvl *lvl;
1353
  TARRAY2_FOREACH(pReader->pFileSet->lvlArr, lvl) {
×
1354
    STFileObj *fobj;
1355
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { pReader->totalSize += fobj->f->size; }
×
1356
  }
1357

1358
  return code;
×
1359
}
1360

1361
int32_t tsdbFileSetReaderNext(struct SFileSetReader *pReader) {
×
1362
  int32_t code = TSDB_CODE_SUCCESS;
×
1363
  (void)taosThreadMutexLock(&pReader->pTsdb->mutex);
×
1364
  code = tsdbFileSetReaderNextNoLock(pReader);
×
1365
  (void)taosThreadMutexUnlock(&pReader->pTsdb->mutex);
×
1366
  return code;
×
1367
}
1368

1369
extern bool tsdbShouldCompact(STFileSet *fset, int32_t vgId, int32_t expLevel, ETsdbOpType type);
1370
int32_t tsdbFileSetGetEntryField(struct SFileSetReader *pReader, const char *field, void *value) {
×
1371
  const char *fieldName;
1372

1373
  if (pReader->fid == INT32_MIN || pReader->fid == INT32_MAX) {
×
1374
    return TSDB_CODE_INVALID_PARA;
×
1375
  }
1376

1377
  fieldName = "fileset_id";
×
1378
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1379
    *(int32_t *)value = pReader->fid;
×
1380
    return TSDB_CODE_SUCCESS;
×
1381
  }
1382

1383
  fieldName = "start_time";
×
1384
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1385
    *(int64_t *)value = pReader->startTime;
×
1386
    return TSDB_CODE_SUCCESS;
×
1387
  }
1388

1389
  fieldName = "end_time";
×
1390
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1391
    *(int64_t *)value = pReader->endTime;
×
1392
    return TSDB_CODE_SUCCESS;
×
1393
  }
1394

1395
  fieldName = "total_size";
×
1396
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1397
    *(int64_t *)value = pReader->totalSize;
×
1398
    return TSDB_CODE_SUCCESS;
×
1399
  }
1400

1401
  fieldName = "last_compact_time";
×
1402
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1403
    *(int64_t *)value = pReader->lastCompactTime;
×
1404
    return TSDB_CODE_SUCCESS;
×
1405
  }
1406

1407
  fieldName = "should_compact";
×
1408
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1409
    *(bool *)value = false;
×
1410
#ifdef TD_ENTERPRISE
1411
    *(bool *)value = tsdbShouldCompact(pReader->pFileSet, pReader->pTsdb->pVnode->config.vgId, 0, TSDB_OPTR_NORMAL);
×
1412
#endif
1413
    return TSDB_CODE_SUCCESS;
×
1414
  }
1415

1416
  fieldName = "details";
×
1417
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1418
    // TODO
1419
    return TSDB_CODE_SUCCESS;
×
1420
  }
1421

1422
  return TSDB_CODE_INVALID_PARA;
×
1423
}
1424

1425
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
×
1426
  if (ppReader == NULL || *ppReader == NULL) {
×
1427
    return;
×
1428
  }
1429

1430
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
×
1431
  taosMemoryFree(*ppReader);
×
1432

1433
  *ppReader = NULL;
×
1434
  return;
×
1435
}
1436

1437
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1438
  if (fObj == NULL) return;
160,218,231!
1439

1440
  int64_t sz = fObj->f->size;
146,298,647✔
1441
  // level == 0, primary storage
1442
  // level == 1, second storage,
1443
  // level == 2, third storage
1444
  int32_t level = fObj->f->did.level;
146,298,647✔
1445
  if (level >= 0 && level < TFS_MAX_TIERS) {
146,298,647!
1446
    szArr[level] += sz;
146,298,647✔
1447
  }
1448
}
1449

1450
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1451
  int32_t code = 0;
29,542,415✔
1452
  int64_t levelSize[TFS_MAX_TIERS] = {0};
29,542,415✔
1453
  int64_t ssSize = 0;
29,542,415✔
1454

1455
  const STFileSet *fset;
1456
  const SSttLvl   *stt = NULL;
29,542,415✔
1457
  const STFileObj *fObj = NULL;
29,542,415✔
1458

1459
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
29,542,415✔
1460
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
29,542,415✔
1461

1462
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
93,443,738✔
1463
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
319,506,615✔
1464
      getLevelSize(fset->farr[t], levelSize);
255,605,292✔
1465
    }
1466

1467
    TARRAY2_FOREACH(fset->lvlArr, stt) {
89,013,431✔
1468
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
50,567,901✔
1469
    }
1470

1471
    fObj = fset->farr[TSDB_FTYPE_DATA];
63,901,323✔
1472
    if (fObj) {
63,901,323✔
1473
      int32_t lcn = fObj->f->lcn;
40,027,694✔
1474
      if (lcn > 1) {
40,027,694!
1475
        ssSize += ((lcn - 1) * chunksize);
×
1476
      }
1477
    }
1478
  }
1479

1480
  pInfo->l1Size = levelSize[0];
29,542,415✔
1481
  pInfo->l2Size = levelSize[1];
29,542,415✔
1482
  pInfo->l3Size = levelSize[2];
29,542,415✔
1483
  pInfo->ssSize = ssSize;
29,542,415✔
1484
  return code;
29,542,415✔
1485
}
1486
int32_t tsdbGetFsSize(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
29,542,415✔
1487
  int32_t code = 0;
29,542,415✔
1488

1489
  (void)taosThreadMutexLock(&tsdb->mutex);
29,542,415✔
1490
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
29,542,415✔
1491
  (void)taosThreadMutexUnlock(&tsdb->mutex);
29,542,415✔
1492
  return code;
29,542,415✔
1493
}
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