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

taosdata / TDengine / #4868

26 Nov 2025 05:46AM UTC coverage: 64.629% (+0.2%) from 64.473%
#4868

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

770 of 945 new or added lines in 33 files covered. (81.48%)

3010 existing lines in 120 files now uncovered.

158425 of 245129 relevant lines covered (64.63%)

113048242.66 hits per line

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

83.5
/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) {
4,070,017✔
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
4,070,017✔
42
  if (fs[0] == NULL) {
4,087,520✔
43
    return terrno;
×
44
  }
45

46
  fs[0]->tsdb = pTsdb;
4,089,547✔
47
  int32_t code = tsem_init(&fs[0]->canEdit, 0, 1);
4,089,059✔
48
  if (code) {
4,085,732✔
49
    taosMemoryFree(fs[0]);
×
50
    return code;
×
51
  }
52

53
  fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
4,085,732✔
54
  fs[0]->neid = 0;
4,083,297✔
55
  TARRAY2_INIT(fs[0]->fSetArr);
4,083,963✔
56
  TARRAY2_INIT(fs[0]->fSetArrTmp);
4,086,339✔
57

58
  return 0;
4,086,994✔
59
}
60

61
static void destroy_fs(STFileSystem **fs) {
4,089,808✔
62
  if (fs[0] == NULL) return;
4,089,808✔
63

64
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
4,090,302✔
65
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
4,088,762✔
66
  if (tsem_destroy(&fs[0]->canEdit) != 0) {
4,090,606✔
67
    tsdbError("failed to destroy semaphore");
×
68
  }
69
  taosMemoryFree(fs[0]);
4,087,661✔
70
  fs[0] = NULL;
4,089,808✔
71
}
72

73
void current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
25,082,051✔
74
  int32_t offset = 0;
25,082,051✔
75

76
  vnodeGetPrimaryPath(pTsdb->pVnode, false, fname, TSDB_FILENAME_LEN);
25,082,051✔
77
  offset = strlen(fname);
25,084,291✔
78
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s%s", TD_DIRSEP, pTsdb->name, TD_DIRSEP,
25,081,923✔
79
           gCurrentFname[ftype]);
25,057,476✔
80
}
25,085,364✔
81

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

88
  data = cJSON_PrintUnformatted(json);
6,843,389✔
89
  if (data == NULL) {
6,847,127✔
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);
6,847,127✔
94
  if (fp == NULL) {
6,845,692✔
95
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
96
  }
97

98
  if (taosWriteFile(fp, data, strlen(data)) < 0) {
6,845,692✔
99
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
100
  }
101

102
  if (taosFsyncFile(fp) < 0) {
6,846,840✔
103
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
104
  }
105

106
_exit:
6,848,239✔
107
  if (code) {
6,847,901✔
108
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
109
  }
110
  taosMemoryFree(data);
6,846,976✔
111
  taosCloseFileWithLog(&fp);
6,847,901✔
112
  return code;
6,845,035✔
113
}
114

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

120
  TdFilePtr fp = taosOpenFile(fname, TD_FILE_READ);
1,136,967✔
121
  if (fp == NULL) {
1,137,684✔
122
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
123
  }
124

125
  int64_t size;
1,136,779✔
126
  code = taosFStatFile(fp, &size, NULL);
1,137,684✔
127
  if (code != 0) {
1,137,406✔
128
    TSDB_CHECK_CODE(code, lino, _exit);
×
129
  }
130

131
  data = taosMemoryMalloc(size + 1);
1,137,406✔
132
  if (data == NULL) {
1,137,131✔
133
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
134
  }
135

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

141
  json[0] = cJSON_Parse(data);
1,136,701✔
142
  if (json[0] == NULL) {
1,137,684✔
143
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
144
  }
145

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

156
int32_t save_fs(const TFileSetArray *arr, const char *fname) {
6,844,908✔
157
  int32_t code = 0;
6,844,908✔
158
  int32_t lino = 0;
6,844,908✔
159

160
  cJSON *json = cJSON_CreateObject();
6,844,908✔
161
  if (json == NULL) {
6,847,058✔
162
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
163
  }
164

165
  // fmtv
166
  if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
6,847,058✔
167
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
168
  }
169

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
6,845,424✔
172
  if (!ajson) {
6,839,352✔
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
25,026,799✔
177
    cJSON *item = cJSON_CreateObject();
18,187,447✔
178
    if (!item) {
18,185,146✔
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
18,185,146✔
182

183
    code = tsdbTFileSetToJson(fset, item);
18,185,247✔
184
    TSDB_CHECK_CODE(code, lino, _exit);
18,187,447✔
185
  }
186

187
  code = save_json(json, fname);
6,842,868✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
6,845,476✔
189

190
_exit:
6,845,476✔
191
  if (code) {
6,845,476✔
192
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
193
  }
194
  cJSON_Delete(json);
6,845,476✔
195
  return code;
6,846,511✔
196
}
197

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

202
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
1,134,706✔
203

204
  // load json
205
  cJSON *json = NULL;
1,137,684✔
206
  code = load_json(fname, &json);
1,137,684✔
207
  TSDB_CHECK_CODE(code, lino, _exit);
1,137,684✔
208

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

212
  /* fmtv */
213
  item1 = cJSON_GetObjectItem(json, "fmtv");
1,137,684✔
214
  if (cJSON_IsNumber(item1)) {
1,137,398✔
215
    if (item1->valuedouble != 1) {
1,137,094✔
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");
1,137,684✔
224
  if (cJSON_IsArray(item1)) {
1,137,684✔
225
    const cJSON *item2;
226
    cJSON_ArrayForEach(item2, item1) {
1,860,393✔
227
      STFileSet *fset;
722,048✔
228
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
722,709✔
229
      TSDB_CHECK_CODE(code, lino, _exit);
722,709✔
230

231
      code = TARRAY2_APPEND(arr, fset);
722,709✔
232
      TSDB_CHECK_CODE(code, lino, _exit);
722,709✔
233
    }
234
    TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
1,137,684✔
235
  } else {
236
    code = TSDB_CODE_FILE_CORRUPTED;
×
237
    TSDB_CHECK_CODE(code, lino, _exit);
×
238
  }
239

240
_exit:
1,136,779✔
241
  if (code) {
1,137,684✔
242
    tsdbError("%s failed at %s:%d since %s, fname:%s", __func__, __FILE__, lino, tstrerror(code), fname);
×
243
  }
244
  if (json) {
1,137,684✔
245
    cJSON_Delete(json);
1,137,684✔
246
  }
247
  return code;
1,137,684✔
248
}
249

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
22,162,203✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
18,268,073✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
18,268,718✔
260

261
    if (fset1 && fset2) {
18,268,326✔
262
      if (fset1->fid < fset2->fid) {
5,055,961✔
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
76,310✔
265
        i1++;
76,310✔
266
      } else if (fset1->fid > fset2->fid) {
4,979,651✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
10,069✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
10,069✔
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
10,069✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
10,069✔
272
        i1++;
10,069✔
273
        i2++;
10,069✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
4,969,582✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
4,969,582✔
278
        i1++;
4,969,582✔
279
        i2++;
4,969,582✔
280
      }
281
    } else if (fset1) {
13,212,365✔
282
      // delete fset1
283
      tsdbTFileSetRemove(fset1);
5,173✔
284
      i1++;
5,173✔
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
13,207,192✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
13,206,498✔
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
13,206,296✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
13,206,296✔
291
      i1++;
13,206,296✔
292
      i2++;
13,206,296✔
293
    }
294
  }
295

296
_exit:
3,894,773✔
297
  if (code) {
3,895,024✔
298
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
299
  }
300
  return code;
3,894,392✔
301
}
302

303
static int32_t commit_edit(STFileSystem *fs) {
3,892,917✔
304
  char current[TSDB_FILENAME_LEN];
3,889,917✔
305
  char current_t[TSDB_FILENAME_LEN];
3,892,024✔
306

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
3,895,024✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
3,895,024✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
2,742,825✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
1,152,199✔
312
  }
313

314
  int32_t code;
315
  int32_t lino;
316
  TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit);
3,895,024✔
317

318
  code = apply_commit(fs);
3,895,024✔
319
  TSDB_CHECK_CODE(code, lino, _exit);
3,894,392✔
320

321
_exit:
3,894,392✔
322
  if (code) {
3,894,141✔
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);
3,894,141✔
327
  }
328
  return code;
3,894,773✔
329
}
330

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

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

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

351
  code = apply_abort(fs);
293✔
352
  TSDB_CHECK_CODE(code, lino, _exit);
293✔
353

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

364
static int32_t tsdbFSDoScanAndFixFile(STFileSystem *fs, const STFileObj *fobj) {
1,291,175✔
365
  int32_t code = 0;
1,291,175✔
366
  int32_t lino = 0;
1,291,175✔
367

368
  // check file existence
369
  if (!taosCheckExistFile(fobj->fname)) {
1,291,175✔
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;
1,291,728✔
392
}
393

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

400
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
2,424,617✔
401

402
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
2,424,617✔
403

404
  entry->next = hash->buckets[idx];
2,424,617✔
405
  hash->buckets[idx] = entry;
2,424,617✔
406
  hash->numFile++;
2,424,617✔
407

408
  return 0;
2,424,617✔
409
}
410

411
static int32_t tsdbFSCreateFileObjHash(STFileSystem *fs, STFileHash *hash) {
1,132,889✔
412
  int32_t code = 0;
1,132,889✔
413
  int32_t lino;
414
  char    fname[TSDB_FILENAME_LEN];
1,131,984✔
415

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

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

429
  // other
430
  STFileSet *fset = NULL;
1,132,889✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
1,853,347✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
3,602,290✔
434
      if (fset->farr[i] != NULL) {
2,881,832✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
576,051✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
576,051✔
437

438
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
576,051✔
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;
720,458✔
457
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
1,414,549✔
458
      STFileObj *fobj;
459
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
1,409,768✔
460
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
715,677✔
461
        TSDB_CHECK_CODE(code, lino, _exit);
715,677✔
462
      }
463
    }
464
  }
465

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

474
static const STFileHashEntry *tsdbFSGetFileObjHashEntry(STFileHash *hash, const char *fname) {
2,425,496✔
475
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
2,425,496✔
476

477
  STFileHashEntry *entry = hash->buckets[idx];
2,425,496✔
478
  while (entry) {
2,425,008✔
479
    if (strcmp(entry->fname, fname) == 0) {
2,424,129✔
480
      return entry;
2,424,617✔
481
    }
UNCOV
482
    entry = entry->next;
×
483
  }
484

485
  return NULL;
879✔
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
1,132,889✔
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;
2,424,617✔
493
      taosMemoryFree(entry);
2,424,617✔
494
      entry = next;
2,424,617✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
1,132,889✔
498
  memset(hash, 0, sizeof(*hash));
1,132,889✔
499
}
1,132,889✔
500

501
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
1,136,708✔
502
  int32_t code = 0;
1,136,708✔
503
  int32_t lino = 0;
1,136,708✔
504
  int32_t corrupt = false;
1,136,708✔
505

506
  if (fs->tsdb->pVnode->mounted) goto _exit;
1,136,708✔
507

508
  {  // scan each file
509
    STFileSet *fset = NULL;
1,132,889✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
1,853,347✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
3,600,078✔
513
        if (fset->farr[ftype] == NULL) continue;
2,879,620✔
514
        STFileObj *fobj = fset->farr[ftype];
576,051✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
576,051✔
516
        if (code) {
576,051✔
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) {
1,414,549✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
1,409,215✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
715,124✔
528
          if (code) {
715,677✔
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
  {  // clear unreferenced files
539
    STfsDir *dir = NULL;
1,131,311✔
540
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
1,131,906✔
541

542
    STFileHash fobjHash = {0};
1,132,889✔
543
    code = tsdbFSCreateFileObjHash(fs, &fobjHash);
1,132,889✔
544
    if (code) goto _close_dir;
1,132,889✔
545

546
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
4,691,274✔
547
      if (taosIsDir(file->aname)) continue;
3,558,385✔
548

549
      if (tsdbFSGetFileObjHashEntry(&fobjHash, file->aname) == NULL) {
2,425,218✔
550
        tsdbRemoveFile(file->aname);
879✔
551
      }
552
    }
553

554
    tsdbFSDestroyFileObjHash(&fobjHash);
1,132,889✔
555

556
  _close_dir:
1,132,889✔
557
    tfsClosedir(dir);
1,132,889✔
558
  }
559

560
_exit:
1,137,977✔
561
  if (corrupt) {
1,137,977✔
562
    tsdbError("vgId:%d, TSDB file system is corrupted", TD_VID(fs->tsdb->pVnode));
×
563
    fs->fsstate = TSDB_FS_STATE_INCOMPLETE;
×
564
    code = 0;
×
565
  }
566

567
  if (code) {
1,137,977✔
568
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
569
  }
570
  return code;
1,137,977✔
571
}
572

573
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
1,137,406✔
574
  fs->neid = 0;
1,137,406✔
575

576
  // get max commit id
577
  const STFileSet *fset;
578
  TARRAY2_FOREACH(fs->fSetArr, fset) { fs->neid = TMAX(fs->neid, tsdbTFileSetMaxCid(fset)); }
1,859,287✔
579

580
  // scan and fix
581
  int32_t code = 0;
1,137,401✔
582
  int32_t lino = 0;
1,137,401✔
583

584
  code = tsdbFSDoSanAndFix(fs);
1,137,401✔
585
  TSDB_CHECK_CODE(code, lino, _exit);
1,137,684✔
586

587
_exit:
1,137,684✔
588
  if (code) {
1,137,684✔
589
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
590
  }
591
  return code;
1,137,684✔
592
}
593

594
static int32_t tsdbFSDupState(STFileSystem *fs) {
5,027,464✔
595
  int32_t code;
596

597
  const TFileSetArray *src = fs->fSetArr;
5,027,464✔
598
  TFileSetArray       *dst = fs->fSetArrTmp;
5,026,475✔
599

600
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
10,013,256✔
601

602
  const STFileSet *fset1;
603
  TARRAY2_FOREACH(src, fset1) {
10,799,245✔
604
    STFileSet *fset2;
5,772,490✔
605
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
5,774,067✔
606
    if (code) return code;
5,773,337✔
607
    code = TARRAY2_APPEND(dst, fset2);
5,773,337✔
608
    if (code) return code;
5,772,723✔
609
  }
610

611
  return 0;
5,026,718✔
612
}
613

614
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
4,069,697✔
615
  int32_t code = 0;
4,069,697✔
616
  int32_t lino = 0;
4,069,697✔
617
  STsdb  *pTsdb = fs->tsdb;
4,069,697✔
618

619
  char fCurrent[TSDB_FILENAME_LEN];
4,083,857✔
620
  char cCurrent[TSDB_FILENAME_LEN];
4,084,224✔
621
  char mCurrent[TSDB_FILENAME_LEN];
4,084,224✔
622

623
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
4,089,889✔
624
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
4,089,889✔
625
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
4,090,606✔
626

627
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
4,090,606✔
628
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
1,137,398✔
629
    TSDB_CHECK_CODE(code, lino, _exit);
1,137,684✔
630

631
    if (taosCheckExistFile(cCurrent)) {
1,137,684✔
632
      // current.c.json exists
633

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

642
        code = commit_edit(fs);
×
643
        TSDB_CHECK_CODE(code, lino, _exit);
×
644
      }
645
    } else if (taosCheckExistFile(mCurrent)) {
1,137,684✔
646
      // current.m.json exists
647
      fs->etype = TSDB_FEDIT_MERGE;
×
648
      code = abort_edit(fs);
×
649
      TSDB_CHECK_CODE(code, lino, _exit);
×
650
    }
651

652
    code = tsdbFSDupState(fs);
1,137,684✔
653
    TSDB_CHECK_CODE(code, lino, _exit);
1,136,853✔
654

655
    code = tsdbFSScanAndFix(fs);
1,136,853✔
656
    TSDB_CHECK_CODE(code, lino, _exit);
1,137,196✔
657
  } else {
658
    code = save_fs(fs->fSetArr, fCurrent);
2,952,230✔
659
    TSDB_CHECK_CODE(code, lino, _exit);
2,950,480✔
660
  }
661

662
_exit:
4,086,771✔
663
  if (code) {
4,088,825✔
664
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
665
  } else {
666
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
4,088,825✔
667
  }
668
  return code;
4,091,366✔
669
}
670

671
static void close_file_system(STFileSystem *fs) {
4,089,808✔
672
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
18,029,411✔
673
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
18,004,958✔
674
}
4,090,606✔
675

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

685
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
3,891,904✔
686
  int32_t code = 0;
3,891,904✔
687
  int32_t lino = 0;
3,891,904✔
688

689
  code = tsdbFSDupState(fs);
3,891,904✔
690
  if (code) return code;
3,889,744✔
691

692
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
3,889,744✔
693
  STFileSet      *fset = NULL;
3,888,756✔
694
  const STFileOp *op;
695
  int32_t         fid = INT32_MIN;
3,888,837✔
696
  TSKEY           now = taosGetTimestampMs();
3,890,004✔
697
  TARRAY2_FOREACH_PTR(opArray, op) {
25,335,474✔
698
    if (!fset || fset->fid != op->fid) {
21,444,382✔
699
      STFileSet tfset = {.fid = op->fid};
16,458,329✔
700
      fset = &tfset;
16,458,367✔
701
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
16,457,883✔
702
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
16,457,883✔
703

704
      if (!fset) {
16,457,883✔
705
        code = tsdbTFileSetInit(op->fid, &fset);
13,213,384✔
706
        TSDB_CHECK_CODE(code, lino, _exit);
13,215,711✔
707

708
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
13,215,674✔
709
        TSDB_CHECK_CODE(code, lino, _exit);
13,215,674✔
710
      }
711
    }
712

713
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
21,444,760✔
714
    TSDB_CHECK_CODE(code, lino, _exit);
21,447,169✔
715

716
    if (fid != op->fid) {
21,447,169✔
717
      fid = op->fid;
16,460,665✔
718
      if (etype == TSDB_FEDIT_COMMIT) {
16,459,362✔
719
        fset->lastCommit = now;
15,307,163✔
720
      } else if (etype == TSDB_FEDIT_COMPACT) {
1,152,199✔
721
        fset->lastCompact = now;
118,506✔
722
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
1,033,693✔
723
        fset->lastMigrate = now;
×
724
      } else if (etype == TSDB_FEDIT_ROLLUP) {
1,033,693✔
725
        fset->lastRollupLevel = fs->rollupLevel;
34,960✔
726
        fset->lastRollup = now;
34,960✔
727
        fset->lastCompact = now;  // rollup implies compact
34,960✔
728
      }
729
    }
730
  }
731

732
  // remove empty empty stt level and empty file set
733
  int32_t i = 0;
3,892,717✔
734
  while (i < TARRAY2_SIZE(fsetArray)) {
22,156,558✔
735
    fset = TARRAY2_GET(fsetArray, i);
18,269,011✔
736

737
    SSttLvl *lvl;
738
    int32_t  j = 0;
18,266,757✔
739
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
39,470,935✔
740
      lvl = TARRAY2_GET(fset->lvlArr, j);
21,211,834✔
741

742
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
21,209,338✔
743
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
1,391,974✔
744
      } else {
745
        j++;
19,812,204✔
746
      }
747
    }
748

749
    if (tsdbTFileSetIsEmpty(fset)) {
18,261,461✔
750
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
81,942✔
751
    } else {
752
      i++;
18,182,358✔
753
    }
754
  }
755

756
_exit:
3,887,713✔
757
  if (code) {
3,889,753✔
758
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
759
  }
760
  return code;
3,893,035✔
761
}
762

763
// return error code
764
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
4,077,158✔
765
  int32_t code;
766
  int32_t lino;
767

768
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
4,077,158✔
769
  TSDB_CHECK_CODE(code, lino, _exit);
4,089,182✔
770

771
  code = create_fs(pTsdb, fs);
4,089,182✔
772
  TSDB_CHECK_CODE(code, lino, _exit);
4,084,938✔
773

774
  code = open_fs(fs[0], rollback);
4,084,938✔
775
  TSDB_CHECK_CODE(code, lino, _exit);
4,090,606✔
776

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

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

791
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
4,109,549✔
792
  STFileSystem *fs = pTsdb->pFS;
4,109,549✔
793
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
4,111,206✔
794
  if (asyncTasks == NULL) {
4,111,409✔
795
    return terrno;
×
796
  }
797

798
  (void)taosThreadMutexLock(&pTsdb->mutex);
4,111,409✔
799

800
  // disable
801
  pTsdb->bgTaskDisabled = true;
4,111,409✔
802

803
  // collect channel
804
  STFileSet *fset;
805
  TARRAY2_FOREACH(fs->fSetArr, fset) {
18,053,455✔
806
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
27,883,804✔
807
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
27,883,804✔
808
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
27,885,062✔
809
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
27,885,062✔
810
      taosArrayDestroy(asyncTasks);
313✔
811
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
812
      return terrno;
×
813
    }
814
    tsdbFSSetBlockCommit(fset, false);
13,942,218✔
815
  }
816

817
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
4,110,918✔
818

819
  // destroy all channels
820
  for (int32_t k = 0; k < 2; k++) {
12,330,483✔
821
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
119,757,624✔
822
      SVATaskID *task = taosArrayGet(asyncTasks, i);
111,537,187✔
823
      if (k == 0) {
111,537,938✔
824
        (void)vnodeACancel(task);
55,770,115✔
825
      } else {
826
        vnodeAWait(task);
55,767,823✔
827
      }
828
    }
829
  }
830
  taosArrayDestroy(asyncTasks);
4,110,800✔
831

832
#ifdef TD_ENTERPRISE
833
  tsdbStopAllCompTask(pTsdb);
4,110,314✔
834
#endif
835
  tsdbStopAllRetentionTask(pTsdb);
4,111,409✔
836
  return 0;
4,110,921✔
837
}
838

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

845
void tsdbCloseFS(STFileSystem **fs) {
4,089,808✔
846
  if (fs[0] == NULL) return;
4,089,808✔
847

848
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
4,090,425✔
849
  if (code) {
4,090,118✔
850
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID((*fs)->tsdb->pVnode), __func__, __LINE__,
×
851
              tstrerror(code));
852
  }
853
  close_file_system(fs[0]);
4,090,118✔
854
  destroy_fs(fs);
4,089,568✔
855
  return;
4,090,606✔
856
}
857

858
int64_t tsdbFSAllocEid(STFileSystem *fs) {
4,096,826✔
859
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
4,096,826✔
860
  int64_t cid = ++fs->neid;
4,097,550✔
861
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
4,097,550✔
862
  return cid;
4,097,550✔
863
}
864

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

871
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
3,892,261✔
872
  int32_t code = 0;
3,892,261✔
873
  int32_t lino;
874
  char    current_t[TSDB_FILENAME_LEN];
3,889,398✔
875

876
  if (etype == TSDB_FEDIT_COMMIT) {
3,892,226✔
877
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
2,740,757✔
878
  } else {
879
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
1,151,469✔
880
  }
881

882
  if (tsem_wait(&fs->canEdit) != 0) {
3,891,227✔
883
    tsdbError("vgId:%d failed to wait semaphore", TD_VID(fs->tsdb->pVnode));
×
884
  }
885
  fs->etype = etype;
3,891,708✔
886

887
  // edit
888
  code = edit_fs(fs, opArray, etype);
3,892,792✔
889
  TSDB_CHECK_CODE(code, lino, _exit);
3,891,727✔
890

891
  // save fs
892
  code = save_fs(fs->fSetArrTmp, current_t);
3,891,727✔
893
  TSDB_CHECK_CODE(code, lino, _exit);
3,895,317✔
894

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

905
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
31,306,820✔
906
  if (block) {
31,306,820✔
907
    fset->blockCommit = true;
2,544✔
908
  } else {
909
    fset->blockCommit = false;
31,304,276✔
910
    if (fset->numWaitCommit > 0) {
31,303,919✔
911
      (void)taosThreadCondSignal(&fset->canCommit);
2,544✔
912
    }
913
  }
914
}
31,305,698✔
915

916
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
15,297,737✔
917
  (void)taosThreadMutexLock(&tsdb->mutex);
15,297,737✔
918
  STFileSet *fset;
15,293,367✔
919
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
15,297,178✔
920
  bool blockCommit = false;
15,276,761✔
921
  if (fset) {
15,276,761✔
922
    blockCommit = fset->blockCommit;
2,118,443✔
923
  }
924
  if (fset) {
15,276,761✔
925
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
2,120,987✔
926
      while (fset->blockCommit) {
927
        fset->numWaitCommit++;
928
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
929
        fset->numWaitCommit--;
930
      }
931
    });
932
  }
933
  if (blockCommit) {
15,288,154✔
934
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
2,544✔
935
  }
936
  (void)taosThreadMutexUnlock(&tsdb->mutex);
15,288,154✔
937
  return;
15,289,555✔
938
}
939

940
// IMPORTANT: the caller must hold fs->tsdb->mutex
941
int32_t tsdbFSEditCommit(STFileSystem *fs) {
3,895,024✔
942
  int32_t code = 0;
3,895,024✔
943
  int32_t lino = 0;
3,895,024✔
944

945
  // commit
946
  code = commit_edit(fs);
3,895,024✔
947
  TSDB_CHECK_CODE(code, lino, _exit);
3,895,024✔
948

949
  // schedule merge
950
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
3,895,024✔
951
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
3,895,024✔
952
    STFileSet *fset;
953
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
21,066,740✔
954
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
17,364,602✔
955
        tsdbFSSetBlockCommit(fset, false);
818,568✔
956
        continue;
818,568✔
957
      }
958

959
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
16,546,034✔
960
      if (lvl->level != 0) {
16,546,034✔
961
        tsdbFSSetBlockCommit(fset, false);
919,955✔
962
        continue;
919,955✔
963
      }
964

965
      // bool    skipMerge = false;
966
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
15,626,079✔
967
      if (numFile >= sttTrigger && (!vnodeATaskValid(&fset->mergeTask))) {
15,626,079✔
968
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
983,604✔
969
        if (arg == NULL) {
983,604✔
970
          code = terrno;
×
971
          TSDB_CHECK_CODE(code, lino, _exit);
×
972
        }
973

974
        arg->tsdb = fs->tsdb;
983,604✔
975
        arg->fid = fset->fid;
983,604✔
976

977
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
983,604✔
978
        TSDB_CHECK_CODE(code, lino, _exit);
983,604✔
979
      }
980

981
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
15,626,079✔
982
        tsdbFSSetBlockCommit(fset, true);
2,544✔
983
      } else {
984
        tsdbFSSetBlockCommit(fset, false);
15,623,535✔
985
      }
986
    }
987
  }
988

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

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

1009
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
35,349,226✔
1010
  STFileSet   tfset = {.fid = fid};
35,349,226✔
1011
  STFileSet  *pset = &tfset;
35,353,301✔
1012
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
35,349,270✔
1013
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
35,337,599✔
1014
}
35,344,458✔
1015

1016
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
15,405✔
1017
  int32_t    code = 0;
15,405✔
1018
  STFileSet *fset;
1019
  STFileSet *fset1;
15,405✔
1020

1021
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
15,405✔
1022
  if (fsetArr[0] == NULL) return terrno;
15,405✔
1023

1024
  TARRAY2_INIT(fsetArr[0]);
15,405✔
1025

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

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

1036
  if (code) {
15,405✔
1037
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1038
    taosMemoryFree(fsetArr[0]);
×
1039
    fsetArr[0] = NULL;
×
1040
  }
1041
  return code;
15,405✔
1042
}
1043

1044
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
16,989✔
1045
  if (fsetArr[0]) {
16,989✔
1046
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
18,573✔
1047
    taosMemoryFree(fsetArr[0]);
16,989✔
1048
    fsetArr[0] = NULL;
16,989✔
1049
  }
1050
}
16,989✔
1051

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

1059
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
126,917,053✔
1060
  int32_t    code = 0;
126,917,053✔
1061
  STFileSet *fset, *fset1;
126,883,758✔
1062

1063
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
126,939,038✔
1064
  if (fsetArr[0] == NULL) return terrno;
126,909,578✔
1065

1066
  TARRAY2_FOREACH(fs->fSetArr, fset) {
323,456,515✔
1067
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
196,581,306✔
1068
    if (code) break;
196,577,401✔
1069

1070
    code = TARRAY2_APPEND(fsetArr[0], fset1);
196,577,401✔
1071
    if (code) {
196,569,880✔
1072
      tsdbTFileSetClear(&fset1);
×
1073
      break;
×
1074
    }
1075
  }
1076

1077
  if (code) {
126,925,240✔
1078
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1079
    taosMemoryFree(fsetArr[0]);
×
1080
    fsetArr[0] = NULL;
×
1081
  }
1082
  return code;
126,925,240✔
1083
}
1084

1085
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
126,945,811✔
1086
  if (fsetArr[0]) {
126,945,811✔
1087
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
323,554,536✔
1088
    taosMemoryFreeClear(fsetArr[0]);
126,959,645✔
1089
    fsetArr[0] = NULL;
126,946,349✔
1090
  }
1091
}
126,929,043✔
1092

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

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

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

1117
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
1,584✔
1118
  if (fsetArr == NULL) return terrno;
1,584✔
1119
  TARRAY2_INIT(fsetArr[0]);
1,584✔
1120

1121
  if (pRanges) {
1,584✔
1122
    pHash = tsdbFSetRangeArrayToHash(pRanges);
1,584✔
1123
    if (pHash == NULL) {
1,584✔
1124
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1125
      goto _out;
×
1126
    }
1127
  }
1128

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

1140
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
1,584✔
1141
    if (code) break;
1,584✔
1142

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

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

1161
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
1,584✔
1162

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

1170
  fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
1,592✔
1171
  if (fsrArr[0] == NULL) {
1,592✔
1172
    code = terrno;
×
1173
    goto _out;
×
1174
  }
1175

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

1185
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
1,592✔
1186
  TARRAY2_FOREACH(fs->fSetArr, fset) {
3,184✔
1187
    int64_t sver1 = sver;
1,592✔
1188
    int64_t ever1 = ever;
1,592✔
1189

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

1199
    if (sver1 > ever1) {
1,592✔
1200
      tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
×
1201
      continue;
×
1202
    }
1203

1204
    tsdbDebug("fsrArr:%p, fid:%d, sver:%" PRId64 ", ever:%" PRId64, fsrArr, fset->fid, sver1, ever1);
1,592✔
1205

1206
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
1,592✔
1207
    if (code) break;
1,592✔
1208

1209
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
1,592✔
1210
    if (code) break;
1,592✔
1211

1212
    fsr1 = NULL;
1,592✔
1213
  }
1214
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
1,592✔
1215

1216
  if (code) {
1,592✔
1217
    tsdbTFileSetRangeClear(&fsr1);
×
1218
    TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
×
1219
    fsrArr[0] = NULL;
×
1220
  }
1221

1222
_out:
1,592✔
1223
  if (pHash) {
1,592✔
1224
    taosHashCleanup(pHash);
1,592✔
1225
    pHash = NULL;
1,592✔
1226
  }
1227
  return code;
1,592✔
1228
}
1229

1230
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
1,592✔
1231

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

1236
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
16,625,295✔
1237
  if (*fset == NULL) {
16,623,317✔
1238
    return;
13,191,788✔
1239
  }
1240

1241
  struct STFileSetCond *cond = NULL;
3,431,903✔
1242
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
3,431,903✔
1243
    cond = &(*fset)->conds[0];
2,203,155✔
1244
  } else {
1245
    cond = &(*fset)->conds[1];
1,228,748✔
1246
  }
1247

1248
  while (1) {
1249
    if (cond->running) {
3,439,403✔
1250
      cond->numWait++;
7,500✔
1251
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
7,500✔
1252
      cond->numWait--;
7,500✔
1253
    } else {
1254
      cond->running = true;
3,431,903✔
1255
      break;
3,431,903✔
1256
    }
1257
  }
1258

1259
  tsdbTrace("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
3,431,903✔
1260
  return;
3,431,903✔
1261
}
1262

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

1267
  STFileSet *fset = NULL;
3,431,903✔
1268
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
3,431,903✔
1269
  if (fset == NULL) {
3,431,903✔
1270
    return;
×
1271
  }
1272

1273
  struct STFileSetCond *cond = NULL;
3,431,903✔
1274
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
3,431,903✔
1275
    cond = &fset->conds[0];
2,203,155✔
1276
  } else {
1277
    cond = &fset->conds[1];
1,228,748✔
1278
  }
1279

1280
  cond->running = false;
3,431,903✔
1281
  if (cond->numWait > 0) {
3,431,903✔
1282
    (void)taosThreadCondSignal(&cond->cond);
7,500✔
1283
  }
1284

1285
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
3,431,903✔
1286
  return;
3,431,903✔
1287
}
1288

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

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

1304
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
338✔
1305

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

1313
  (*ppReader)->pTsdb = pTsdb;
338✔
1314
  (*ppReader)->fid = INT32_MIN;
338✔
1315
  (*ppReader)->pFileSet = NULL;
338✔
1316

1317
  return TSDB_CODE_SUCCESS;
338✔
1318
}
1319

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

1324
  tsdbTFileSetClear(&pReader->pFileSet);
676✔
1325

1326
  STFileSet *fset = &(STFileSet){
676✔
1327
      .fid = pReader->fid,
676✔
1328
  };
1329

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

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

1340
  // get file set details
1341
  pReader->fid = pReader->pFileSet->fid;
338✔
1342
  tsdbFidKeyRange(pReader->fid, pTsdb->keepCfg.days, pTsdb->keepCfg.precision, &pReader->startTime, &pReader->endTime);
338✔
1343
  if (pTsdb->keepCfg.precision == TSDB_TIME_PRECISION_MICRO) {
338✔
NEW
1344
    pReader->startTime /= 1000;
×
NEW
1345
    pReader->endTime /= 1000;
×
1346
  } else if (pTsdb->keepCfg.precision == TSDB_TIME_PRECISION_NANO) {
338✔
NEW
1347
    pReader->startTime /= 1000000;
×
NEW
1348
    pReader->endTime /= 1000000;
×
1349
  }
1350
  pReader->lastCompactTime = pReader->pFileSet->lastCompact;
338✔
1351
  pReader->totalSize = 0;
338✔
1352
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
1,690✔
1353
    STFileObj *fobj = pReader->pFileSet->farr[i];
1,352✔
1354
    if (fobj) {
1,352✔
1355
      pReader->totalSize += fobj->f->size;
×
1356
    }
1357
  }
1358
  SSttLvl *lvl;
1359
  TARRAY2_FOREACH(pReader->pFileSet->lvlArr, lvl) {
676✔
1360
    STFileObj *fobj;
1361
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { pReader->totalSize += fobj->f->size; }
676✔
1362
  }
1363

1364
  return code;
338✔
1365
}
1366

1367
int32_t tsdbFileSetReaderNext(struct SFileSetReader *pReader) {
676✔
1368
  int32_t code = TSDB_CODE_SUCCESS;
676✔
1369
  (void)taosThreadMutexLock(&pReader->pTsdb->mutex);
676✔
1370
  code = tsdbFileSetReaderNextNoLock(pReader);
676✔
1371
  (void)taosThreadMutexUnlock(&pReader->pTsdb->mutex);
676✔
1372
  return code;
676✔
1373
}
1374

1375
extern bool tsdbShouldCompact(STFileSet *fset, int32_t vgId, int32_t expLevel, ETsdbOpType type);
1376
int32_t tsdbFileSetGetEntryField(struct SFileSetReader *pReader, const char *field, void *value) {
2,028✔
1377
  const char *fieldName;
1378

1379
  if (pReader->fid == INT32_MIN || pReader->fid == INT32_MAX) {
2,028✔
UNCOV
1380
    return TSDB_CODE_INVALID_PARA;
×
1381
  }
1382

1383
  fieldName = "fileset_id";
2,028✔
1384
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
2,028✔
1385
    *(int32_t *)value = pReader->fid;
338✔
1386
    return TSDB_CODE_SUCCESS;
338✔
1387
  }
1388

1389
  fieldName = "start_time";
1,690✔
1390
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
1,690✔
1391
    *(int64_t *)value = pReader->startTime;
338✔
1392
    return TSDB_CODE_SUCCESS;
338✔
1393
  }
1394

1395
  fieldName = "end_time";
1,352✔
1396
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
1,352✔
1397
    *(int64_t *)value = pReader->endTime;
338✔
1398
    return TSDB_CODE_SUCCESS;
338✔
1399
  }
1400

1401
  fieldName = "total_size";
1,014✔
1402
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
1,014✔
1403
    *(int64_t *)value = pReader->totalSize;
338✔
1404
    return TSDB_CODE_SUCCESS;
338✔
1405
  }
1406

1407
  fieldName = "last_compact_time";
676✔
1408
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
676✔
1409
    *(int64_t *)value = pReader->lastCompactTime;
338✔
1410
    return TSDB_CODE_SUCCESS;
338✔
1411
  }
1412

1413
  fieldName = "should_compact";
338✔
1414
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
338✔
1415
    *(bool *)value = false;
338✔
1416
#ifdef TD_ENTERPRISE
1417
    *(bool *)value = tsdbShouldCompact(pReader->pFileSet, pReader->pTsdb->pVnode->config.vgId, 0, TSDB_OPTR_NORMAL);
338✔
1418
#endif
1419
    return TSDB_CODE_SUCCESS;
338✔
1420
  }
1421

UNCOV
1422
  fieldName = "details";
×
UNCOV
1423
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1424
    // TODO
UNCOV
1425
    return TSDB_CODE_SUCCESS;
×
1426
  }
1427

UNCOV
1428
  return TSDB_CODE_INVALID_PARA;
×
1429
}
1430

1431
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
338✔
1432
  if (ppReader == NULL || *ppReader == NULL) {
338✔
UNCOV
1433
    return;
×
1434
  }
1435

1436
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
338✔
1437
  taosMemoryFree(*ppReader);
338✔
1438

1439
  *ppReader = NULL;
338✔
1440
  return;
338✔
1441
}
1442

1443
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1444
  if (fObj == NULL) return;
11,938,998✔
1445

1446
  int64_t sz = fObj->f->size;
10,057,376✔
1447
  // level == 0, primary storage
1448
  // level == 1, second storage,
1449
  // level == 2, third storage
1450
  int32_t level = fObj->f->did.level;
10,057,376✔
1451
  if (level >= 0 && level < TFS_MAX_TIERS) {
10,057,376✔
1452
    szArr[level] += sz;
10,057,376✔
1453
  }
1454
}
1455

1456
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1457
  int32_t code = 0;
6,763,021✔
1458
  int64_t levelSize[TFS_MAX_TIERS] = {0};
6,763,021✔
1459
  int64_t ssSize = 0;
6,763,021✔
1460

1461
  const STFileSet *fset;
1462
  const SSttLvl   *stt = NULL;
6,763,021✔
1463
  const STFileObj *fObj = NULL;
6,763,021✔
1464

1465
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
6,763,021✔
1466
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
6,763,021✔
1467

1468
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
11,115,444✔
1469
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
21,762,115✔
1470
      getLevelSize(fset->farr[t], levelSize);
17,409,692✔
1471
    }
1472

1473
    TARRAY2_FOREACH(fset->lvlArr, stt) {
6,531,941✔
1474
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
4,472,859✔
1475
    }
1476

1477
    fObj = fset->farr[TSDB_FTYPE_DATA];
4,352,423✔
1478
    if (fObj) {
4,352,423✔
1479
      int32_t lcn = fObj->f->lcn;
2,573,698✔
1480
      if (lcn > 1) {
2,573,698✔
UNCOV
1481
        ssSize += ((lcn - 1) * chunksize);
×
1482
      }
1483
    }
1484
  }
1485

1486
  pInfo->l1Size = levelSize[0];
6,763,021✔
1487
  pInfo->l2Size = levelSize[1];
6,763,021✔
1488
  pInfo->l3Size = levelSize[2];
6,763,021✔
1489
  pInfo->ssSize = ssSize;
6,763,021✔
1490
  return code;
6,763,021✔
1491
}
1492
int32_t tsdbGetFsSize(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
6,763,021✔
1493
  int32_t code = 0;
6,763,021✔
1494

1495
  (void)taosThreadMutexLock(&tsdb->mutex);
6,763,021✔
1496
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
6,763,021✔
1497
  (void)taosThreadMutexUnlock(&tsdb->mutex);
6,763,021✔
1498
  return code;
6,763,021✔
1499
}
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