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

taosdata / TDengine / #4828

29 Oct 2025 11:10AM UTC coverage: 61.071% (-0.3%) from 61.383%
#4828

push

travis-ci

web-flow
Merge pull request #33419 from taosdata/3.0

3.0

155924 of 324872 branches covered (48.0%)

Branch coverage included in aggregate %.

207404 of 270051 relevant lines covered (76.8%)

243478715.98 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,340,013✔
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
12,340,013!
42
  if (fs[0] == NULL) {
12,350,563!
43
    return terrno;
×
44
  }
45

46
  fs[0]->tsdb = pTsdb;
12,351,829✔
47
  int32_t code = tsem_init(&fs[0]->canEdit, 0, 1);
12,351,808✔
48
  if (code) {
12,345,435!
49
    taosMemoryFree(fs[0]);
×
50
    return code;
×
51
  }
52

53
  fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
12,345,435✔
54
  fs[0]->neid = 0;
12,348,739✔
55
  TARRAY2_INIT(fs[0]->fSetArr);
12,347,584✔
56
  TARRAY2_INIT(fs[0]->fSetArrTmp);
12,349,927✔
57

58
  return 0;
12,342,003✔
59
}
60

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

64
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
12,350,314!
65
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
12,352,112!
66
  if (tsem_destroy(&fs[0]->canEdit) != 0) {
12,353,017!
67
    tsdbError("failed to destroy semaphore");
×
68
  }
69
  taosMemoryFree(fs[0]);
12,347,942!
70
  fs[0] = NULL;
12,352,524✔
71
}
72

73
void current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
78,719,543✔
74
  int32_t offset = 0;
78,719,543✔
75

76
  vnodeGetPrimaryPath(pTsdb->pVnode, false, fname, TSDB_FILENAME_LEN);
78,719,543✔
77
  offset = strlen(fname);
78,729,569!
78
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s%s", TD_DIRSEP, pTsdb->name, TD_DIRSEP,
78,731,803✔
79
           gCurrentFname[ftype]);
78,702,897✔
80
}
78,734,272✔
81

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

88
  data = cJSON_PrintUnformatted(json);
22,039,716✔
89
  if (data == NULL) {
22,041,165!
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);
22,041,165✔
94
  if (fp == NULL) {
22,032,213!
95
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
96
  }
97

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

102
  if (taosFsyncFile(fp) < 0) {
22,040,836!
103
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
104
  }
105

106
_exit:
22,042,246✔
107
  if (code) {
22,042,246!
108
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
109
  }
110
  taosMemoryFree(data);
22,041,342!
111
  taosCloseFileWithLog(&fp);
22,040,963!
112
  return code;
22,039,375✔
113
}
114

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

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

125
  int64_t size;
3,155,543✔
126
  code = taosFStatFile(fp, &size, NULL);
3,155,648✔
127
  if (code != 0) {
3,156,469!
128
    TSDB_CHECK_CODE(code, lino, _exit);
×
129
  }
130

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

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

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

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

156
int32_t save_fs(const TFileSetArray *arr, const char *fname) {
22,038,577✔
157
  int32_t code = 0;
22,038,577✔
158
  int32_t lino = 0;
22,038,577✔
159

160
  cJSON *json = cJSON_CreateObject();
22,038,577✔
161
  if (json == NULL) {
22,037,892!
162
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
163
  }
164

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

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
22,039,381✔
172
  if (!ajson) {
22,039,716!
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
158,868,078✔
177
    cJSON *item = cJSON_CreateObject();
136,826,001✔
178
    if (!item) {
136,826,836!
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
136,826,836✔
182

183
    code = tsdbTFileSetToJson(fset, item);
136,828,185✔
184
    TSDB_CHECK_CODE(code, lino, _exit);
136,828,362!
185
  }
186

187
  code = save_json(json, fname);
22,039,716✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
22,039,375!
189

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

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

202
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
3,156,469!
203

204
  // load json
205
  cJSON *json = NULL;
3,156,469✔
206
  code = load_json(fname, &json);
3,155,648✔
207
  TSDB_CHECK_CODE(code, lino, _exit);
3,156,469!
208

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

212
  /* fmtv */
213
  item1 = cJSON_GetObjectItem(json, "fmtv");
3,156,469✔
214
  if (cJSON_IsNumber(item1)) {
3,155,514!
215
    if (item1->valuedouble != 1) {
3,155,097!
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,156,469✔
224
  if (cJSON_IsArray(item1)) {
3,156,469!
225
    const cJSON *item2;
226
    cJSON_ArrayForEach(item2, item1) {
5,659,873!
227
      STFileSet *fset;
2,502,054✔
228
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
2,502,410✔
229
      TSDB_CHECK_CODE(code, lino, _exit);
2,503,821!
230

231
      code = TARRAY2_APPEND(arr, fset);
2,503,821!
232
      TSDB_CHECK_CODE(code, lino, _exit);
2,501,993!
233
    }
234
    TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
3,157,463✔
235
  } else {
236
    code = TSDB_CODE_FILE_CORRUPTED;
×
237
    TSDB_CHECK_CODE(code, lino, _exit);
×
238
  }
239

240
_exit:
3,155,543✔
241
  if (code) {
3,156,469!
242
    tsdbError("%s failed at %s:%d since %s, fname:%s", __func__, __FILE__, lino, tstrerror(code), fname);
×
243
  }
244
  if (json) {
3,156,469!
245
    cJSON_Delete(json);
3,156,469✔
246
  }
247
  return code;
3,156,469✔
248
}
249

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
149,873,042✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
137,029,372✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
137,030,167✔
260

261
    if (fset1 && fset2) {
137,001,733✔
262
      if (fset1->fid < fset2->fid) {
16,111,273✔
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
194,962✔
265
        i1++;
194,962✔
266
      } else if (fset1->fid > fset2->fid) {
15,916,311✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
25,313✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
25,313!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
25,313✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
25,313!
272
        i1++;
25,313✔
273
        i2++;
25,313✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
15,890,998✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
15,890,998!
278
        i1++;
15,890,998✔
279
        i2++;
15,890,998✔
280
      }
281
    } else if (fset1) {
120,890,460✔
282
      // delete fset1
283
      tsdbTFileSetRemove(fset1);
7,251✔
284
      i1++;
7,251✔
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
120,883,209✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
120,912,329!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
120,911,418✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
120,911,418!
291
      i1++;
120,911,418✔
292
      i2++;
120,911,418✔
293
    }
294
  }
295

296
_exit:
12,843,670✔
297
  if (code) {
12,843,670!
298
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
299
  }
300
  return code;
12,842,875✔
301
}
302

303
static int32_t commit_edit(STFileSystem *fs) {
12,844,514✔
304
  char current[TSDB_FILENAME_LEN];
12,842,917✔
305
  char current_t[TSDB_FILENAME_LEN];
12,842,917✔
306

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
12,844,514✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
12,844,514✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,328,685✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,515,829✔
312
  }
313

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

318
  code = apply_commit(fs);
12,844,514✔
319
  TSDB_CHECK_CODE(code, lino, _exit);
12,842,875!
320

321
_exit:
12,842,875✔
322
  if (code) {
12,842,875!
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,842,875!
327
  }
328
  return code;
12,844,514✔
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,184✔
334

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

338
  if (fs->etype == TSDB_FEDIT_COMMIT) {
1,184!
339
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
1,184✔
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,184!
347
    code = TAOS_SYSTEM_ERROR(code);
×
348
    TSDB_CHECK_CODE(code, lino, _exit);
×
349
  }
350

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

354
_exit:
1,184✔
355
  if (code) {
1,184!
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,184!
360
  }
361
  return code;
1,184✔
362
}
363

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

368
  // check file existence
369
  if (!taosCheckExistFile(fobj->fname)) {
4,228,523!
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,228,940✔
392
}
393

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

400
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
7,371,417!
401

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

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

408
  return 0;
7,371,417✔
409
}
410

411
static int32_t tsdbFSCreateFileObjHash(STFileSystem *fs, STFileHash *hash) {
3,142,477✔
412
  int32_t code = 0;
3,142,477✔
413
  int32_t lino;
414
  char    fname[TSDB_FILENAME_LEN];
3,141,551✔
415

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

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

429
  // other
430
  STFileSet *fset = NULL;
3,142,477✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
5,641,303✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
12,489,897✔
434
      if (fset->farr[i] != NULL) {
9,991,071✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
1,874,767✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
1,874,767!
437

438
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
1,874,767!
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,498,826✔
457
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
4,776,056✔
458
      STFileObj *fobj;
459
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,629,992✔
460
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
2,352,762✔
461
        TSDB_CHECK_CODE(code, lino, _exit);
2,354,173!
462
      }
463
    }
464
  }
465

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

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

477
  STFileHashEntry *entry = hash->buckets[idx];
7,374,969✔
478
  while (entry) {
7,376,858✔
479
    if (strcmp(entry->fname, fname) == 0) {
7,373,306!
480
      return entry;
7,368,599✔
481
    }
482
    entry = entry->next;
3,298✔
483
  }
484

485
  return NULL;
3,552✔
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
3,142,477✔
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,371,417✔
493
      taosMemoryFree(entry);
7,371,417!
494
      entry = next;
7,371,417✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
3,142,477!
498
  memset(hash, 0, sizeof(*hash));
3,142,477!
499
}
3,142,477✔
500

501
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
3,156,281✔
502
  int32_t code = 0;
3,156,281✔
503
  int32_t lino = 0;
3,156,281✔
504
  int32_t corrupt = false;
3,157,236✔
505

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

508
  {  // scan each file
509
    STFileSet *fset = NULL;
3,142,477✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
5,640,886✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
12,490,636✔
513
        if (fset->farr[ftype] == NULL) continue;
9,990,818✔
514
        STFileObj *fobj = fset->farr[ftype];
1,874,767✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
1,874,767✔
516
        if (code) {
1,874,767!
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,776,631✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,630,569✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
2,354,173✔
528
          if (code) {
2,354,173!
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,142,477!
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,142,477✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
3,142,477!
548

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

553
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
13,658,514✔
554
      if (taosIsDir(file->aname)) continue;
10,516,037✔
555

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

561
    tsdbFSDestroyFileObjHash(&fobjHash);
3,142,477✔
562

563
  _close_dir:
3,142,477✔
564
    tfsClosedir(dir);
3,142,477✔
565
  }
566

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

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

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

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

585
  code = tsdbFSDoSanAndFix(fs);
3,156,469✔
586
  TSDB_CHECK_CODE(code, lino, _exit);
3,156,469!
587

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

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

598
  const TFileSetArray *src = fs->fSetArr;
15,997,219✔
599
  TFileSetArray       *dst = fs->fSetArrTmp;
15,998,858✔
600

601
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
31,929,360✔
602

603
  const STFileSet *fset1;
604
  TARRAY2_FOREACH(src, fset1) {
34,577,955✔
605
    STFileSet *fset2;
18,597,438✔
606
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
18,598,214✔
607
    if (code) return code;
18,598,544!
608
    code = TARRAY2_APPEND(dst, fset2);
18,598,544✔
609
    if (code) return code;
18,597,725!
610
  }
611

612
  return 0;
15,998,562✔
613
}
614

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

620
  char fCurrent[TSDB_FILENAME_LEN];
12,339,691✔
621
  char cCurrent[TSDB_FILENAME_LEN];
12,341,770✔
622
  char mCurrent[TSDB_FILENAME_LEN];
12,342,942✔
623

624
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
12,346,165✔
625
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
12,353,017✔
626
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
12,353,017✔
627

628
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
12,352,196✔
629
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
3,156,469✔
630
    TSDB_CHECK_CODE(code, lino, _exit);
3,156,469!
631

632
    if (taosCheckExistFile(cCurrent)) {
3,156,469!
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,156,469!
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,155,060✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
3,156,052!
655

656
    code = tsdbFSScanAndFix(fs);
3,156,052✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
3,156,469!
658
  } else {
659
    code = save_fs(fs->fSetArr, fCurrent);
9,195,657✔
660
    TSDB_CHECK_CODE(code, lino, _exit);
9,193,677!
661
  }
662

663
_exit:
12,349,220✔
664
  if (code) {
12,350,146!
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,350,146✔
668
  }
669
  return code;
12,353,017✔
670
}
671

672
static void close_file_system(STFileSystem *fs) {
12,350,203✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
135,788,199✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
135,743,839✔
675
}
12,349,895✔
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,845,698✔
687
  int32_t code = 0;
12,845,698✔
688
  int32_t lino = 0;
12,845,698✔
689

690
  code = tsdbFSDupState(fs);
12,845,698✔
691
  if (code) return code;
12,840,841!
692

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
12,840,841✔
694
  STFileSet      *fset = NULL;
12,844,215✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
12,844,215✔
697
  TSKEY           now = taosGetTimestampMs();
12,844,215✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
161,342,721✔
699
    if (!fset || fset->fid != op->fid) {
148,504,924✔
700
      STFileSet tfset = {.fid = op->fid};
131,269,453✔
701
      fset = &tfset;
131,264,595✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
131,269,548✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
131,269,548✔
704

705
      if (!fset) {
131,269,548✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
120,938,370✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
120,922,824!
708

709
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
120,937,882✔
710
        TSDB_CHECK_CODE(code, lino, _exit);
120,937,882!
711
      }
712
    }
713

714
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
148,504,924✔
715
    TSDB_CHECK_CODE(code, lino, _exit);
148,499,599!
716

717
    if (fid != op->fid) {
148,499,599✔
718
      fid = op->fid;
131,269,548✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
131,269,548✔
720
        fset->lastCommit = now;
127,753,719✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
3,515,829✔
722
        fset->lastCompact = now;
288,425✔
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
3,227,404!
724
        fset->lastMigrate = now;
×
725
      } else if (etype == TSDB_FEDIT_ROLLUP) {
3,227,404✔
726
        fset->lastRollupLevel = fs->rollupLevel;
26,040✔
727
        fset->lastRollup = now;
26,040✔
728
        fset->lastCompact = now;  // rollup implies compact
26,040✔
729
      }
730
    }
731
  }
732

733
  // remove empty empty stt level and empty file set
734
  int32_t i = 0;
12,837,655✔
735
  while (i < TARRAY2_SIZE(fsetArray)) {
149,845,227✔
736
    fset = TARRAY2_GET(fsetArray, i);
137,020,385✔
737

738
    SSttLvl *lvl;
739
    int32_t  j = 0;
137,018,952✔
740
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
281,404,807✔
741
      lvl = TARRAY2_GET(fset->lvlArr, j);
144,391,299✔
742

743
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
144,393,388✔
744
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
3,881,270!
745
      } else {
746
        j++;
140,504,585✔
747
      }
748
    }
749

750
    if (tsdbTFileSetIsEmpty(fset)) {
137,020,032✔
751
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
205,192!
752
    } else {
753
      i++;
136,805,359✔
754
    }
755
  }
756

757
_exit:
12,833,102✔
758
  if (code) {
12,833,102!
759
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
760
  }
761
  return code;
12,839,072✔
762
}
763

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

769
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
12,342,771✔
770
  TSDB_CHECK_CODE(code, lino, _exit);
12,353,017!
771

772
  code = create_fs(pTsdb, fs);
12,353,017✔
773
  TSDB_CHECK_CODE(code, lino, _exit);
12,342,920!
774

775
  code = open_fs(fs[0], rollback);
12,342,920✔
776
  TSDB_CHECK_CODE(code, lino, _exit);
12,353,017!
777

778
_exit:
12,353,017✔
779
  if (code) {
12,353,017!
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,353,017✔
784
  }
785
  return code;
12,353,017✔
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,440,084✔
793
  STFileSystem *fs = pTsdb->pFS;
12,440,084✔
794
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
12,441,303✔
795
  if (asyncTasks == NULL) {
12,439,462!
796
    return terrno;
×
797
  }
798

799
  (void)taosThreadMutexLock(&pTsdb->mutex);
12,439,462✔
800

801
  // disable
802
  pTsdb->bgTaskDisabled = true;
12,443,139✔
803

804
  // collect channel
805
  STFileSet *fset;
806
  TARRAY2_FOREACH(fs->fSetArr, fset) {
135,886,859✔
807
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
246,898,579!
808
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
246,899,992!
809
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
246,900,916!
810
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
246,900,916!
811
      taosArrayDestroy(asyncTasks);
×
812
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
813
      return terrno;
×
814
    }
815
    tsdbFSSetBlockCommit(fset, false);
123,450,458✔
816
  }
817

818
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
12,439,470✔
819

820
  // destroy all channels
821
  for (int32_t k = 0; k < 2; k++) {
37,325,634✔
822
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
1,012,472,155✔
823
      SVATaskID *task = taosArrayGet(asyncTasks, i);
987,580,249✔
824
      if (k == 0) {
987,581,954✔
825
        (void)vnodeACancel(task);
493,792,129✔
826
      } else {
827
        vnodeAWait(task);
493,789,825✔
828
      }
829
    }
830
  }
831
  taosArrayDestroy(asyncTasks);
12,443,274✔
832

833
#ifdef TD_ENTERPRISE
834
  tsdbStopAllCompTask(pTsdb);
12,439,462✔
835
#endif
836
  tsdbStopAllRetentionTask(pTsdb);
12,444,493✔
837
  return 0;
12,444,493✔
838
}
839

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

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

849
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
12,349,827✔
850
  if (code) {
12,353,017!
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,353,017✔
855
  destroy_fs(fs);
12,349,895✔
856
  return;
12,352,524✔
857
}
858

859
int64_t tsdbFSAllocEid(STFileSystem *fs) {
13,140,274✔
860
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
13,140,274✔
861
  int64_t cid = ++fs->neid;
13,138,744✔
862
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
13,140,274✔
863
  return cid;
13,140,274✔
864
}
865

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

872
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
12,845,698✔
873
  int32_t code = 0;
12,845,698✔
874
  int32_t lino;
875
  char    current_t[TSDB_FILENAME_LEN];
12,844,101✔
876

877
  if (etype == TSDB_FEDIT_COMMIT) {
12,845,698✔
878
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,328,930✔
879
  } else {
880
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,516,768✔
881
  }
882

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

888
  // edit
889
  code = edit_fs(fs, opArray, etype);
12,844,552✔
890
  TSDB_CHECK_CODE(code, lino, _exit);
12,843,276!
891

892
  // save fs
893
  code = save_fs(fs->fSetArrTmp, current_t);
12,843,276✔
894
  TSDB_CHECK_CODE(code, lino, _exit);
12,842,849!
895

896
_exit:
12,842,849✔
897
  if (code) {
12,842,849!
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,842,849!
902
  }
903
  return code;
12,844,488✔
904
}
905

906
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
256,862,434✔
907
  if (block) {
256,862,434✔
908
    fset->blockCommit = true;
93,489✔
909
  } else {
910
    fset->blockCommit = false;
256,768,945✔
911
    if (fset->numWaitCommit > 0) {
256,768,945✔
912
      (void)taosThreadCondSignal(&fset->canCommit);
67,856✔
913
    }
914
  }
915
}
256,858,510✔
916

917
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
127,625,381✔
918
  (void)taosThreadMutexLock(&tsdb->mutex);
127,625,381✔
919
  STFileSet *fset;
127,604,001✔
920
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
127,591,168✔
921
  bool blockCommit = false;
127,564,718✔
922
  if (fset) {
127,564,718✔
923
    blockCommit = fset->blockCommit;
6,870,054!
924
  }
925
  if (fset) {
127,564,718✔
926
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
6,938,605!
927
      while (fset->blockCommit) {
928
        fset->numWaitCommit++;
929
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
930
        fset->numWaitCommit--;
931
      }
932
    });
933
  }
934
  if (blockCommit) {
127,569,764✔
935
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
67,856!
936
  }
937
  (void)taosThreadMutexUnlock(&tsdb->mutex);
127,569,764✔
938
  return;
127,551,872✔
939
}
940

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

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

950
  // schedule merge
951
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
12,844,514✔
952
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
12,844,514✔
953
    STFileSet *fset;
954
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
145,801,269✔
955
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
133,411,976✔
956
        tsdbFSSetBlockCommit(fset, false);
2,041,404✔
957
        continue;
2,041,404✔
958
      }
959

960
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
131,370,572✔
961
      if (lvl->level != 0) {
131,370,572✔
962
        tsdbFSSetBlockCommit(fset, false);
2,515,478✔
963
        continue;
2,515,478✔
964
      }
965

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

975
        arg->tsdb = fs->tsdb;
3,179,823✔
976
        arg->fid = fset->fid;
3,179,823✔
977

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

982
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
128,855,094✔
983
        tsdbFSSetBlockCommit(fset, true);
93,489✔
984
      } else {
985
        tsdbFSSetBlockCommit(fset, false);
128,761,605✔
986
      }
987
    }
988
  }
989

990
_exit:
12,844,514✔
991
  if (code) {
12,844,514!
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,844,514!
995
  }
996
  if (tsem_post(&fs->canEdit) != 0) {
12,844,514!
997
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
998
  }
999
  return code;
12,844,514✔
1000
}
1001

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

1010
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
269,665,714✔
1011
  STFileSet   tfset = {.fid = fid};
269,665,714✔
1012
  STFileSet  *pset = &tfset;
269,685,841✔
1013
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
269,701,985✔
1014
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
269,672,281✔
1015
}
269,692,962✔
1016

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

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

1025
  TARRAY2_INIT(fsetArr[0]);
74,725✔
1026

1027
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
74,725✔
1028
  TARRAY2_FOREACH(fs->fSetArr, fset) {
74,725!
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);
74,725✔
1036

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

1045
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
80,129✔
1046
  if (fsetArr[0]) {
80,129!
1047
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
85,533!
1048
    taosMemoryFree(fsetArr[0]);
80,129!
1049
    fsetArr[0] = NULL;
80,129✔
1050
  }
1051
}
80,129✔
1052

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

1060
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
855,193,692✔
1061
  int32_t    code = 0;
855,193,692✔
1062
  STFileSet *fset, *fset1;
855,186,838✔
1063

1064
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
855,299,972✔
1065
  if (fsetArr[0] == NULL) return terrno;
855,146,898!
1066

1067
  TARRAY2_FOREACH(fs->fSetArr, fset) {
2,147,483,647✔
1068
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
1,367,162,120✔
1069
    if (code) break;
1,367,155,996!
1070

1071
    code = TARRAY2_APPEND(fsetArr[0], fset1);
1,367,155,996!
1072
    if (code) {
1,367,169,516!
1073
      tsdbTFileSetClear(&fset1);
×
1074
      break;
×
1075
    }
1076
  }
1077

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

1086
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
855,413,339✔
1087
  if (fsetArr[0]) {
855,413,339!
1088
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2,147,483,647✔
1089
    taosMemoryFreeClear(fsetArr[0]);
855,446,237!
1090
    fsetArr[0] = NULL;
855,367,446✔
1091
  }
1092
}
855,361,565✔
1093

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

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

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

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

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

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

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

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

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

1162
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
5,404✔
1163

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

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

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

1186
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
5,407✔
1187
  TARRAY2_FOREACH(fs->fSetArr, fset) {
10,814✔
1188
    int64_t sver1 = sver;
5,407✔
1189
    int64_t ever1 = ever;
5,407✔
1190

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

1200
    if (sver1 > ever1) {
5,407!
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);
5,407!
1206

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

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

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

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

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

1231
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
5,407✔
1232

1233
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
131,444,396✔
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,444,396✔
1236

1237
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
131,444,396✔
1238
  if (*fset == NULL) {
131,444,941✔
1239
    return;
120,796,027✔
1240
  }
1241

1242
  struct STFileSetCond *cond = NULL;
10,648,914✔
1243
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,648,914✔
1244
    cond = &(*fset)->conds[0];
7,006,290✔
1245
  } else {
1246
    cond = &(*fset)->conds[1];
3,642,624✔
1247
  }
1248

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

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

1264
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
10,648,914✔
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,648,914✔
1267

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

1274
  struct STFileSetCond *cond = NULL;
10,648,914✔
1275
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,648,914✔
1276
    cond = &fset->conds[0];
7,006,290✔
1277
  } else {
1278
    cond = &fset->conds[1];
3,642,624✔
1279
  }
1280

1281
  cond->running = false;
10,648,914✔
1282
  if (cond->numWait > 0) {
10,648,914!
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,648,914✔
1287
  return;
10,648,914✔
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;
98,191,958!
1439

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

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

1455
  const STFileSet *fset;
1456
  const SSttLvl   *stt = NULL;
23,086,972✔
1457
  const STFileObj *fObj = NULL;
23,086,972✔
1458

1459
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
23,086,972✔
1460
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
23,086,972✔
1461

1462
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
61,447,085✔
1463
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
191,800,565✔
1464
      getLevelSize(fset->farr[t], levelSize);
153,440,452✔
1465
    }
1466

1467
    TARRAY2_FOREACH(fset->lvlArr, stt) {
54,159,976✔
1468
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
31,889,231✔
1469
    }
1470

1471
    fObj = fset->farr[TSDB_FTYPE_DATA];
38,360,113✔
1472
    if (fObj) {
38,360,113✔
1473
      int32_t lcn = fObj->f->lcn;
23,599,071✔
1474
      if (lcn > 1) {
23,599,071!
1475
        ssSize += ((lcn - 1) * chunksize);
×
1476
      }
1477
    }
1478
  }
1479

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

1489
  (void)taosThreadMutexLock(&tsdb->mutex);
23,086,972✔
1490
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
23,086,972✔
1491
  (void)taosThreadMutexUnlock(&tsdb->mutex);
23,086,972✔
1492
  return code;
23,086,972✔
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