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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

83.8
/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 "tencrypt.h"
18
#include "tsdbFS2.h"
19
#include "tsdbUpgrade.h"
20
#include "vnd.h"
21

22
#define BLOCK_COMMIT_FACTOR 3
23

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

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

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

41
static int32_t create_fs(STsdb *pTsdb, STFileSystem **fs) {
3,604,396✔
42
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
3,604,396✔
43
  if (fs[0] == NULL) {
3,613,882✔
44
    return terrno;
×
45
  }
46

47
  fs[0]->tsdb = pTsdb;
3,613,807✔
48
  int32_t code = tsem_init(&fs[0]->canEdit, 0, 1);
3,612,673✔
49
  if (code) {
3,610,981✔
UNCOV
50
    taosMemoryFree(fs[0]);
×
51
    return code;
×
52
  }
53

54
  fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
3,610,981✔
55
  fs[0]->neid = 0;
3,612,414✔
56
  TARRAY2_INIT(fs[0]->fSetArr);
3,612,349✔
57
  TARRAY2_INIT(fs[0]->fSetArrTmp);
3,612,349✔
58

59
  return 0;
3,611,126✔
60
}
61

62
static void destroy_fs(STFileSystem **fs) {
3,613,998✔
63
  if (fs[0] == NULL) return;
3,613,998✔
64

65
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
3,613,998✔
66
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
3,614,294✔
67
  if (tsem_destroy(&fs[0]->canEdit) != 0) {
3,613,472✔
68
    tsdbError("failed to destroy semaphore");
×
69
  }
70
  taosMemoryFree(fs[0]);
3,613,930✔
71
  fs[0] = NULL;
3,614,294✔
72
}
73

74
void current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
23,039,033✔
75
  int32_t offset = 0;
23,039,033✔
76

77
  vnodeGetPrimaryPath(pTsdb->pVnode, false, fname, TSDB_FILENAME_LEN);
23,039,033✔
78
  offset = strlen(fname);
23,041,731✔
79
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s%s", TD_DIRSEP, pTsdb->name, TD_DIRSEP,
23,040,980✔
80
           gCurrentFname[ftype]);
23,039,634✔
81
}
23,040,848✔
82

83
static int32_t save_json(const cJSON *json, const char *fname) {
6,305,798✔
84
  int32_t   code = 0;
6,305,798✔
85
  int32_t   lino;
86
  char     *data = NULL;
6,305,798✔
87

88
  data = cJSON_PrintUnformatted(json);
6,305,798✔
89
  if (data == NULL) {
6,305,438✔
90
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
91
  }
92

93
  int32_t len = strlen(data);
6,305,438✔
94
  
95
  // Use encrypted write if tsCfgKey is enabled
96
  code = taosWriteCfgFile(fname, data, len);
6,305,438✔
97
  if (code != 0) {
6,306,120✔
98
    TSDB_CHECK_CODE(code, lino, _exit);
×
99
  }
100

101
_exit:
6,306,120✔
102
  if (code) {
6,306,120✔
103
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
104
  }
105
  taosMemoryFree(data);
6,306,398✔
106
  return code;
6,307,033✔
107
}
108

109
static int32_t load_json(const char *fname, cJSON **json) {
1,030,616✔
110
  int32_t code = 0;
1,030,616✔
111
  int32_t lino;
112
  char   *data = NULL;
1,030,616✔
113
  int32_t dataLen = 0;
1,031,178✔
114

115
  // Use taosReadCfgFile for automatic decryption support (returns null-terminated string)
116
  code = taosReadCfgFile(fname, &data, &dataLen);
1,030,616✔
117
  if (code != 0) {
1,031,178✔
118
    TSDB_CHECK_CODE(code, lino, _exit);
×
119
  }
120

121
  json[0] = cJSON_Parse(data);
1,031,178✔
122
  if (json[0] == NULL) {
1,031,178✔
123
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
124
  }
125

126
_exit:
1,031,178✔
127
  if (code) {
1,031,253✔
128
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
129
    json[0] = NULL;
×
130
  }
131
  taosMemoryFree(data);
1,031,253✔
132
  return code;
1,031,253✔
133
}
134

135
int32_t save_fs(const TFileSetArray *arr, const char *fname) {
6,304,538✔
136
  int32_t code = 0;
6,304,538✔
137
  int32_t lino = 0;
6,304,538✔
138

139
  cJSON *json = cJSON_CreateObject();
6,304,538✔
140
  if (json == NULL) {
6,306,247✔
141
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
142
  }
143

144
  // fmtv
145
  if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
6,306,247✔
146
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
147
  }
148

149
  // fset
150
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
6,304,944✔
151
  if (!ajson) {
6,306,247✔
152
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
153
  }
154
  const STFileSet *fset;
155
  TARRAY2_FOREACH(arr, fset) {
24,471,535✔
156
    cJSON *item = cJSON_CreateObject();
18,165,104✔
157
    if (!item) {
18,165,287✔
158
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
159
    }
160
    (void)cJSON_AddItemToArray(ajson, item);
18,165,287✔
161

162
    code = tsdbTFileSetToJson(fset, item);
18,165,287✔
163
    TSDB_CHECK_CODE(code, lino, _exit);
18,165,288✔
164
  }
165

166
  code = save_json(json, fname);
6,305,273✔
167
  TSDB_CHECK_CODE(code, lino, _exit);
6,307,033✔
168

169
_exit:
6,307,033✔
170
  if (code) {
6,307,033✔
171
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
172
  }
173
  cJSON_Delete(json);
6,307,033✔
174
  return code;
6,305,630✔
175
}
176

177
static int32_t load_fs(STsdb *pTsdb, const char *fname, TFileSetArray *arr) {
1,030,616✔
178
  int32_t code = 0;
1,030,616✔
179
  int32_t lino = 0;
1,030,616✔
180

181
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
1,030,616✔
182

183
  // load json
184
  cJSON *json = NULL;
1,030,616✔
185
  code = load_json(fname, &json);
1,030,691✔
186
  TSDB_CHECK_CODE(code, lino, _exit);
1,031,253✔
187

188
  // parse json
189
  const cJSON *item1;
190

191
  /* fmtv */
192
  item1 = cJSON_GetObjectItem(json, "fmtv");
1,031,253✔
193
  if (cJSON_IsNumber(item1)) {
1,031,253✔
194
    if (item1->valuedouble != 1) {
1,031,178✔
195
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
196
    }
197
  } else {
198
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
199
  }
200

201
  /* fset */
202
  item1 = cJSON_GetObjectItem(json, "fset");
1,031,253✔
203
  if (cJSON_IsArray(item1)) {
1,031,253✔
204
    const cJSON *item2;
205
    cJSON_ArrayForEach(item2, item1) {
1,647,206✔
206
      STFileSet *fset;
615,884✔
207
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
615,953✔
208
      TSDB_CHECK_CODE(code, lino, _exit);
615,953✔
209

210
      code = TARRAY2_APPEND(arr, fset);
615,953✔
211
      TSDB_CHECK_CODE(code, lino, _exit);
615,953✔
212
    }
213
    TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
1,031,253✔
214
  } else {
215
    code = TSDB_CODE_FILE_CORRUPTED;
×
216
    TSDB_CHECK_CODE(code, lino, _exit);
×
217
  }
218

219
_exit:
1,031,190✔
220
  if (code) {
1,031,253✔
221
    tsdbError("%s failed at %s:%d since %s, fname:%s", __func__, __FILE__, lino, tstrerror(code), fname);
×
222
  }
223
  if (json) {
1,031,253✔
224
    cJSON_Delete(json);
1,031,253✔
225
  }
226
  return code;
1,031,253✔
227
}
228

229
static int32_t apply_commit(STFileSystem *fs) {
3,723,905✔
230
  int32_t        code = 0;
3,723,905✔
231
  int32_t        lino;
232
  TFileSetArray *fsetArray1 = fs->fSetArr;
3,723,905✔
233
  TFileSetArray *fsetArray2 = fs->fSetArrTmp;
3,723,721✔
234
  int32_t        i1 = 0, i2 = 0;
3,723,721✔
235

236
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
21,966,221✔
237
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
18,242,709✔
238
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
18,243,882✔
239

240
    if (fset1 && fset2) {
18,243,882✔
241
      if (fset1->fid < fset2->fid) {
4,457,652✔
242
        // delete fset1
243
        tsdbTFileSetRemove(fset1);
72,846✔
244
        i1++;
72,846✔
245
      } else if (fset1->fid > fset2->fid) {
4,384,806✔
246
        // create new file set with fid of fset2->fid
247
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
9,060✔
248
        TSDB_CHECK_CODE(code, lino, _exit);
9,060✔
249
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
9,060✔
250
        TSDB_CHECK_CODE(code, lino, _exit);
9,060✔
251
        i1++;
9,060✔
252
        i2++;
9,060✔
253
      } else {
254
        // edit
255
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
4,375,746✔
256
        TSDB_CHECK_CODE(code, lino, _exit);
4,375,746✔
257
        i1++;
4,375,746✔
258
        i2++;
4,375,746✔
259
      }
260
    } else if (fset1) {
13,786,230✔
261
      // delete fset1
262
      tsdbTFileSetRemove(fset1);
6,019✔
263
      i1++;
6,019✔
264
    } else {
265
      // create new file set with fid of fset2->fid
266
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
13,780,211✔
267
      TSDB_CHECK_CODE(code, lino, _exit);
13,779,765✔
268
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
13,780,211✔
269
      TSDB_CHECK_CODE(code, lino, _exit);
13,780,211✔
270
      i1++;
13,780,211✔
271
      i2++;
13,780,211✔
272
    }
273
  }
274

275
_exit:
3,723,328✔
276
  if (code) {
3,723,328✔
277
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
278
  }
279
  return code;
3,723,721✔
280
}
281

282
static int32_t commit_edit(STFileSystem *fs) {
3,722,322✔
283
  char current[TSDB_FILENAME_LEN];
3,722,033✔
284
  char current_t[TSDB_FILENAME_LEN];
3,723,616✔
285

286
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
3,723,905✔
287
  if (fs->etype == TSDB_FEDIT_COMMIT) {
3,723,459✔
288
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
2,643,031✔
289
  } else {
290
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
1,080,690✔
291
  }
292

293
  int32_t code;
294
  int32_t lino;
295
  TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit);
3,723,905✔
296

297
  code = apply_commit(fs);
3,723,721✔
298
  TSDB_CHECK_CODE(code, lino, _exit);
3,723,023✔
299

300
_exit:
3,723,023✔
301
  if (code) {
3,722,839✔
302
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
303
              tstrerror(code));
304
  } else {
305
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
3,722,839✔
306
  }
307
  return code;
3,723,207✔
308
}
309

310
// static int32_t
311
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs);
312
static int32_t apply_abort(STFileSystem *fs) { return tsdbFSDoSanAndFix(fs); }
87✔
313

314
static int32_t abort_edit(STFileSystem *fs) {
87✔
315
  char fname[TSDB_FILENAME_LEN];
87✔
316

317
  if (fs->etype == TSDB_FEDIT_COMMIT) {
87✔
318
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
87✔
319
  } else {
320
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
×
321
  }
322

323
  int32_t code;
324
  int32_t lino;
325
  if ((code = taosRemoveFile(fname))) {
87✔
326
    code = TAOS_SYSTEM_ERROR(code);
×
327
    TSDB_CHECK_CODE(code, lino, _exit);
×
328
  }
329

330
  code = apply_abort(fs);
87✔
331
  TSDB_CHECK_CODE(code, lino, _exit);
87✔
332

333
_exit:
87✔
334
  if (code) {
87✔
335
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
336
              tstrerror(code));
337
  } else {
338
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
87✔
339
  }
340
  return code;
87✔
341
}
342

343
static int32_t tsdbFSDoScanAndFixFile(STFileSystem *fs, const STFileObj *fobj) {
1,121,262✔
344
  int32_t code = 0;
1,121,262✔
345
  int32_t lino = 0;
1,121,262✔
346

347
  // check file existence
348
  if (!taosCheckExistFile(fobj->fname)) {
1,121,262✔
349
    bool found = false;
×
350

351
    if (tsSsEnabled && fobj->f->lcn > 1) {
×
352
      char fname1[TSDB_FILENAME_LEN];
×
353
      tsdbTFileLastChunkName(fs->tsdb, fobj->f, fname1);
×
354
      if (!taosCheckExistFile(fname1)) {
×
355
        code = TSDB_CODE_FILE_CORRUPTED;
×
356
        tsdbError("vgId:%d %s failed since file:%s does not exist", TD_VID(fs->tsdb->pVnode), __func__, fname1);
×
357
        return code;
×
358
      }
359

360
      found = true;
×
361
    }
362

363
    if (!found) {
×
364
      code = TSDB_CODE_FILE_CORRUPTED;
×
365
      tsdbError("vgId:%d %s failed since file:%s does not exist", TD_VID(fs->tsdb->pVnode), __func__, fobj->fname);
×
366
      return code;
×
367
    }
368
  }
369

370
  return 0;
1,121,813✔
371
}
372

373
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
374

375
static int32_t tsdbFSAddEntryToFileObjHash(STFileHash *hash, const char *fname) {
2,151,329✔
376
  STFileHashEntry *entry = taosMemoryMalloc(sizeof(*entry));
2,151,329✔
377
  if (entry == NULL) return terrno;
2,151,329✔
378

379
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
2,151,329✔
380

381
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
2,151,329✔
382

383
  entry->next = hash->buckets[idx];
2,151,329✔
384
  hash->buckets[idx] = entry;
2,151,329✔
385
  hash->numFile++;
2,151,329✔
386

387
  return 0;
2,151,329✔
388
}
389

390
static int32_t tsdbFSCreateFileObjHash(STFileSystem *fs, STFileHash *hash) {
1,029,516✔
391
  int32_t code = 0;
1,029,516✔
392
  int32_t lino;
393
  char    fname[TSDB_FILENAME_LEN];
1,029,453✔
394

395
  // init hash table
396
  hash->numFile = 0;
1,029,516✔
397
  hash->numBucket = 4096;
1,029,516✔
398
  hash->buckets = taosMemoryCalloc(hash->numBucket, sizeof(STFileHashEntry *));
1,029,516✔
399
  if (hash->buckets == NULL) {
1,029,516✔
400
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
401
  }
402

403
  // vnode.json
404
  current_fname(fs->tsdb, fname, TSDB_FCURRENT);
1,029,516✔
405
  code = tsdbFSAddEntryToFileObjHash(hash, fname);
1,029,516✔
406
  TSDB_CHECK_CODE(code, lino, _exit);
1,029,516✔
407

408
  // other
409
  STFileSet *fset = NULL;
1,029,516✔
410
  TARRAY2_FOREACH(fs->fSetArr, fset) {
1,644,644✔
411
    // data file
412
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
3,075,640✔
413
      if (fset->farr[i] != NULL) {
2,460,512✔
414
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
483,608✔
415
        TSDB_CHECK_CODE(code, lino, _exit);
483,608✔
416

417
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
483,608✔
418
          STFileObj *fobj = fset->farr[i];
×
419
          int32_t    lcn = fobj->f->lcn;
×
420
          char       lcn_name[TSDB_FILENAME_LEN];
×
421

422
          snprintf(lcn_name, TSDB_FQDN_LEN, "%s", fobj->fname);
×
423
          char *dot = strrchr(lcn_name, '.');
×
424
          if (dot) {
×
425
            snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - lcn_name), "%d.data", lcn);
×
426

427
            code = tsdbFSAddEntryToFileObjHash(hash, lcn_name);
×
428
            TSDB_CHECK_CODE(code, lino, _exit);
×
429
          }
430
        }
431
      }
432
    }
433

434
    // stt file
435
    SSttLvl *lvl = NULL;
615,128✔
436
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
1,232,829✔
437
      STFileObj *fobj;
438
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
1,255,906✔
439
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
638,205✔
440
        TSDB_CHECK_CODE(code, lino, _exit);
638,205✔
441
      }
442
    }
443
  }
444

445
_exit:
1,029,516✔
446
  if (code) {
1,029,516✔
447
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
448
    tsdbFSDestroyFileObjHash(hash);
×
449
  }
450
  return code;
1,029,516✔
451
}
452

453
static const STFileHashEntry *tsdbFSGetFileObjHashEntry(STFileHash *hash, const char *fname) {
2,151,590✔
454
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
2,151,590✔
455

456
  STFileHashEntry *entry = hash->buckets[idx];
2,151,590✔
457
  while (entry) {
2,153,084✔
458
    if (strcmp(entry->fname, fname) == 0) {
2,152,823✔
459
      return entry;
2,151,329✔
460
    }
461
    entry = entry->next;
1,494✔
462
  }
463

464
  return NULL;
261✔
465
}
466

467
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
1,029,516✔
468
  for (int32_t i = 0; i < hash->numBucket; i++) {
2,147,483,647✔
469
    STFileHashEntry *entry = hash->buckets[i];
2,147,483,647✔
470
    while (entry) {
2,147,483,647✔
471
      STFileHashEntry *next = entry->next;
2,151,329✔
472
      taosMemoryFree(entry);
2,151,329✔
473
      entry = next;
2,151,329✔
474
    }
475
  }
476
  taosMemoryFree(hash->buckets);
1,029,516✔
477
  memset(hash, 0, sizeof(*hash));
1,029,516✔
478
}
1,029,516✔
479

480
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
1,030,714✔
481
  int32_t code = 0;
1,030,714✔
482
  int32_t lino = 0;
1,030,714✔
483
  int32_t corrupt = false;
1,031,340✔
484

485
  if (fs->tsdb->pVnode->mounted) goto _exit;
1,031,340✔
486

487
  {  // scan each file
488
    STFileSet *fset = NULL;
1,029,516✔
489
    TARRAY2_FOREACH(fs->fSetArr, fset) {
1,644,644✔
490
      // data file
491
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
3,072,885✔
492
        if (fset->farr[ftype] == NULL) continue;
2,458,308✔
493
        STFileObj *fobj = fset->farr[ftype];
483,608✔
494
        code = tsdbFSDoScanAndFixFile(fs, fobj);
483,608✔
495
        if (code) {
483,608✔
496
          fset->maxVerValid = (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
497
          corrupt = true;
×
498
        }
499
      }
500

501
      // stt file
502
      SSttLvl *lvl;
503
      TARRAY2_FOREACH(fset->lvlArr, lvl) {
1,232,278✔
504
        STFileObj *fobj;
505
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
1,255,355✔
506
          code = tsdbFSDoScanAndFixFile(fs, fobj);
638,205✔
507
          if (code) {
638,205✔
508
            fset->maxVerValid =
×
509
                (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
510
            corrupt = true;
×
511
          }
512
        }
513
      }
514
    }
515
  }
516

517
  {  // clear unreferenced files
518
    STfsDir *dir = NULL;
1,029,516✔
519
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
1,029,516✔
520

521
    STFileHash fobjHash = {0};
1,029,516✔
522
    code = tsdbFSCreateFileObjHash(fs, &fobjHash);
1,029,516✔
523
    if (code) goto _close_dir;
1,029,516✔
524

525
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
4,210,622✔
526
      if (taosIsDir(file->aname)) continue;
3,181,106✔
527

528
      if (tsdbFSGetFileObjHashEntry(&fobjHash, file->aname) == NULL) {
2,151,590✔
529
        tsdbRemoveFile(file->aname);
261✔
530
      }
531
    }
532

533
    tsdbFSDestroyFileObjHash(&fobjHash);
1,028,852✔
534

535
  _close_dir:
1,029,516✔
536
    tfsClosedir(dir);
1,029,516✔
537
  }
538

539
_exit:
1,031,340✔
540
  if (corrupt) {
1,031,340✔
541
    tsdbError("vgId:%d, TSDB file system is corrupted", TD_VID(fs->tsdb->pVnode));
×
542
    fs->fsstate = TSDB_FS_STATE_INCOMPLETE;
×
543
    code = 0;
×
544
  }
545

546
  if (code) {
1,031,340✔
547
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
548
  }
549
  return code;
1,031,340✔
550
}
551

552
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
1,030,627✔
553
  fs->neid = 0;
1,030,627✔
554

555
  // get max commit id
556
  const STFileSet *fset;
557
  TARRAY2_FOREACH(fs->fSetArr, fset) { fs->neid = TMAX(fs->neid, tsdbTFileSetMaxCid(fset)); }
1,646,655✔
558

559
  // scan and fix
560
  int32_t code = 0;
1,031,178✔
561
  int32_t lino = 0;
1,031,178✔
562

563
  code = tsdbFSDoSanAndFix(fs);
1,031,178✔
564
  TSDB_CHECK_CODE(code, lino, _exit);
1,031,253✔
565

566
_exit:
1,031,253✔
567
  if (code) {
1,031,253✔
568
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
569
  }
570
  return code;
1,031,253✔
571
}
572

573
static int32_t tsdbFSDupState(STFileSystem *fs) {
4,754,154✔
574
  int32_t code;
575

576
  const TFileSetArray *src = fs->fSetArr;
4,754,154✔
577
  TFileSetArray       *dst = fs->fSetArrTmp;
4,754,154✔
578

579
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
9,151,864✔
580

581
  const STFileSet *fset1;
582
  TARRAY2_FOREACH(src, fset1) {
9,824,962✔
583
    STFileSet *fset2;
5,070,398✔
584
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
5,070,651✔
585
    if (code) return code;
5,069,976✔
586
    code = TARRAY2_APPEND(dst, fset2);
5,069,976✔
587
    if (code) return code;
5,070,651✔
588
  }
589

590
  return 0;
4,753,085✔
591
}
592

593
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
3,613,891✔
594
  int32_t code = 0;
3,613,891✔
595
  int32_t lino = 0;
3,613,891✔
596
  STsdb  *pTsdb = fs->tsdb;
3,613,891✔
597

598
  char fCurrent[TSDB_FILENAME_LEN];
3,611,978✔
599
  char cCurrent[TSDB_FILENAME_LEN];
3,612,146✔
600
  char mCurrent[TSDB_FILENAME_LEN];
3,612,146✔
601

602
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
3,611,090✔
603
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
3,613,989✔
604
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
3,613,261✔
605

606
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
3,613,565✔
607
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
1,031,253✔
608
    TSDB_CHECK_CODE(code, lino, _exit);
1,031,253✔
609

610
    if (taosCheckExistFile(cCurrent)) {
1,031,253✔
611
      // current.c.json exists
612

613
      fs->etype = TSDB_FEDIT_COMMIT;
×
614
      if (rollback) {
×
615
        code = abort_edit(fs);
×
616
        TSDB_CHECK_CODE(code, lino, _exit);
×
617
      } else {
618
        code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
×
619
        TSDB_CHECK_CODE(code, lino, _exit);
×
620

621
        code = commit_edit(fs);
×
622
        TSDB_CHECK_CODE(code, lino, _exit);
×
623
      }
624
    } else if (taosCheckExistFile(mCurrent)) {
1,031,178✔
625
      // current.m.json exists
626
      fs->etype = TSDB_FEDIT_MERGE;
×
627
      code = abort_edit(fs);
×
628
      TSDB_CHECK_CODE(code, lino, _exit);
×
629
    }
630

631
    code = tsdbFSDupState(fs);
1,031,253✔
632
    TSDB_CHECK_CODE(code, lino, _exit);
1,030,627✔
633

634
    code = tsdbFSScanAndFix(fs);
1,030,627✔
635
    TSDB_CHECK_CODE(code, lino, _exit);
1,031,253✔
636
  } else {
637
    code = save_fs(fs->fSetArr, fCurrent);
2,581,900✔
638
    TSDB_CHECK_CODE(code, lino, _exit);
2,583,041✔
639
  }
640

641
_exit:
3,614,231✔
642
  if (code) {
3,614,294✔
643
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
644
  } else {
645
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
3,614,294✔
646
  }
647
  return code;
3,614,762✔
648
}
649

650
static void close_file_system(STFileSystem *fs) {
3,614,180✔
651
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
18,019,237✔
652
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
17,996,646✔
653
}
3,614,294✔
654

655
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
×
656
  if (pSet1->fid < pSet2->fid) {
×
657
    return -1;
×
658
  } else if (pSet1->fid > pSet2->fid) {
×
659
    return 1;
×
660
  }
661
  return 0;
×
662
}
663

664
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
3,723,808✔
665
  int32_t code = 0;
3,723,808✔
666
  int32_t lino = 0;
3,723,808✔
667

668
  code = tsdbFSDupState(fs);
3,723,808✔
669
  if (code) return code;
3,722,548✔
670

671
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
3,722,548✔
672
  STFileSet      *fset = NULL;
3,723,317✔
673
  const STFileOp *op;
674
  int32_t         fid = INT32_MIN;
3,723,317✔
675
  TSKEY           now = taosGetTimestampMs();
3,723,133✔
676
  TARRAY2_FOREACH_PTR(opArray, op) {
25,149,862✔
677
    if (!fset || fset->fid != op->fid) {
21,426,730✔
678
      STFileSet tfset = {.fid = op->fid};
16,863,896✔
679
      fset = &tfset;
16,863,353✔
680
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
16,863,896✔
681
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
16,863,896✔
682

683
      if (!fset) {
16,863,896✔
684
        code = tsdbTFileSetInit(op->fid, &fset);
13,788,544✔
685
        TSDB_CHECK_CODE(code, lino, _exit);
13,789,271✔
686

687
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
13,789,271✔
688
        TSDB_CHECK_CODE(code, lino, _exit);
13,789,271✔
689
      }
690
    }
691

692
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
21,426,540✔
693
    TSDB_CHECK_CODE(code, lino, _exit);
21,426,862✔
694

695
    if (fid != op->fid) {
21,426,862✔
696
      fid = op->fid;
16,864,381✔
697
      if (etype == TSDB_FEDIT_COMMIT) {
16,864,571✔
698
        fset->lastCommit = now;
15,783,881✔
699
      } else if (etype == TSDB_FEDIT_COMPACT) {
1,080,690✔
700
        fset->lastCompact = now;
88,754✔
701
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
991,936✔
702
        fset->lastMigrate = now;
×
703
      } else if (etype == TSDB_FEDIT_ROLLUP) {
991,936✔
704
        fset->lastRollupLevel = fs->rollupLevel;
16,261✔
705
        fset->lastRollup = now;
16,261✔
706
        fset->lastCompact = now;  // rollup implies compact
16,261✔
707
      }
708
    }
709
  }
710

711
  // remove empty empty stt level and empty file set
712
  int32_t i = 0;
3,723,132✔
713
  while (i < TARRAY2_SIZE(fsetArray)) {
21,966,551✔
714
    fset = TARRAY2_GET(fsetArray, i);
18,244,152✔
715

716
    SSttLvl *lvl;
717
    int32_t  j = 0;
18,243,477✔
718
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
39,580,337✔
719
      lvl = TARRAY2_GET(fset->lvlArr, j);
21,338,321✔
720

721
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
21,337,778✔
722
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
1,349,455✔
723
      } else {
724
        j++;
19,987,405✔
725
      }
726
    }
727

728
    if (tsdbTFileSetIsEmpty(fset)) {
18,242,744✔
729
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
78,866✔
730
    } else {
731
      i++;
18,164,554✔
732
    }
733
  }
734

735
_exit:
3,723,259✔
736
  if (code) {
3,723,448✔
737
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
738
  }
739
  return code;
3,723,802✔
740
}
741

742
// return error code
743
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
3,607,590✔
744
  int32_t code;
745
  int32_t lino;
746

747
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
3,607,590✔
748
  TSDB_CHECK_CODE(code, lino, _exit);
3,613,153✔
749

750
  code = create_fs(pTsdb, fs);
3,613,153✔
751
  TSDB_CHECK_CODE(code, lino, _exit);
3,609,106✔
752

753
  code = open_fs(fs[0], rollback);
3,609,106✔
754
  TSDB_CHECK_CODE(code, lino, _exit);
3,614,294✔
755

756
_exit:
3,614,294✔
757
  if (code) {
3,614,294✔
758
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
759
    destroy_fs(fs);
×
760
  } else {
761
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
3,614,294✔
762
  }
763
  return code;
3,614,294✔
764
}
765

766
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block);
767
extern void tsdbStopAllCompTask(STsdb *tsdb);
768
extern void tsdbStopAllRetentionTask(STsdb *tsdb);
769

770
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
3,642,876✔
771
  STFileSystem *fs = pTsdb->pFS;
3,642,876✔
772
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
3,642,876✔
773
  if (asyncTasks == NULL) {
3,642,876✔
774
    return terrno;
×
775
  }
776

777
  (void)taosThreadMutexLock(&pTsdb->mutex);
3,642,876✔
778

779
  // disable
780
  pTsdb->bgTaskDisabled = true;
3,642,876✔
781

782
  // collect channel
783
  STFileSet *fset;
784
  TARRAY2_FOREACH(fs->fSetArr, fset) {
18,048,255✔
785
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
28,811,040✔
786
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
28,812,444✔
787
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
28,812,444✔
788
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
28,812,444✔
UNCOV
789
      taosArrayDestroy(asyncTasks);
×
790
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
791
      return terrno;
×
792
    }
793
    tsdbFSSetBlockCommit(fset, false);
14,406,222✔
794
  }
795

796
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
3,642,147✔
797

798
  // destroy all channels
799
  for (int32_t k = 0; k < 2; k++) {
10,928,187✔
800
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
122,532,979✔
801
      SVATaskID *task = taosArrayGet(asyncTasks, i);
115,245,402✔
802
      if (k == 0) {
115,246,131✔
803
        (void)vnodeACancel(task);
57,624,159✔
804
      } else {
805
        vnodeAWait(task);
57,621,972✔
806
      }
807
    }
808
  }
809
  taosArrayDestroy(asyncTasks);
3,642,797✔
810

811
#ifdef TD_ENTERPRISE
812
  tsdbStopAllCompTask(pTsdb);
3,642,876✔
813
#endif
814
  tsdbStopAllRetentionTask(pTsdb);
3,642,876✔
815
  return 0;
3,642,876✔
816
}
817

818
void tsdbEnableBgTask(STsdb *pTsdb) {
28,582✔
819
  (void)taosThreadMutexLock(&pTsdb->mutex);
28,582✔
820
  pTsdb->bgTaskDisabled = false;
28,582✔
821
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
28,582✔
822
}
28,582✔
823

824
void tsdbCloseFS(STFileSystem **fs) {
3,613,247✔
825
  if (fs[0] == NULL) return;
3,613,247✔
826

827
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
3,614,294✔
828
  if (code) {
3,614,294✔
829
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID((*fs)->tsdb->pVnode), __func__, __LINE__,
×
830
              tstrerror(code));
831
  }
832
  close_file_system(fs[0]);
3,614,294✔
833
  destroy_fs(fs);
3,613,998✔
834
  return;
3,614,294✔
835
}
836

837
int64_t tsdbFSAllocEid(STFileSystem *fs) {
3,815,684✔
838
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
3,815,684✔
839
  int64_t cid = ++fs->neid;
3,816,045✔
840
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
3,816,045✔
841
  return cid;
3,816,045✔
842
}
843

844
void tsdbFSUpdateEid(STFileSystem *fs, int64_t cid) {
111,731✔
845
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
111,731✔
846
  fs->neid = TMAX(fs->neid, cid);
111,731✔
847
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
111,731✔
848
}
111,731✔
849

850
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
3,723,808✔
851
  int32_t code = 0;
3,723,808✔
852
  int32_t lino;
853
  char    current_t[TSDB_FILENAME_LEN];
3,723,519✔
854

855
  if (etype == TSDB_FEDIT_COMMIT) {
3,723,808✔
856
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
2,642,575✔
857
  } else {
858
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
1,081,233✔
859
  }
860

861
  if (tsem_wait(&fs->canEdit) != 0) {
3,723,624✔
862
    tsdbError("vgId:%d failed to wait semaphore", TD_VID(fs->tsdb->pVnode));
×
863
  }
864
  fs->etype = etype;
3,723,623✔
865

866
  // edit
867
  code = edit_fs(fs, opArray, etype);
3,723,808✔
868
  TSDB_CHECK_CODE(code, lino, _exit);
3,723,802✔
869

870
  // save fs
871
  code = save_fs(fs->fSetArrTmp, current_t);
3,723,802✔
872
  TSDB_CHECK_CODE(code, lino, _exit);
3,723,992✔
873

874
_exit:
3,723,992✔
875
  if (code) {
3,723,992✔
876
    tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
×
877
              tstrerror(code), etype);
878
  } else {
879
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
3,723,992✔
880
  }
881
  return code;
3,723,992✔
882
}
883

884
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
28,913,619✔
885
  if (block) {
28,913,619✔
886
    fset->blockCommit = true;
15✔
887
  } else {
888
    fset->blockCommit = false;
28,913,604✔
889
    if (fset->numWaitCommit > 0) {
28,913,604✔
890
      (void)taosThreadCondSignal(&fset->canCommit);
14✔
891
    }
892
  }
893
}
28,912,944✔
894

895
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
15,784,041✔
896
  (void)taosThreadMutexLock(&tsdb->mutex);
15,784,041✔
897
  STFileSet *fset;
15,787,103✔
898
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
15,785,441✔
899
  bool blockCommit = false;
15,780,863✔
900
  if (fset) {
15,780,863✔
901
    blockCommit = fset->blockCommit;
2,019,021✔
902
  }
903
  if (fset) {
15,780,863✔
904
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
2,019,035✔
905
      while (fset->blockCommit) {
906
        fset->numWaitCommit++;
907
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
908
        fset->numWaitCommit--;
909
      }
910
    });
911
  }
912
  if (blockCommit) {
15,780,843✔
913
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
14✔
914
  }
915
  (void)taosThreadMutexUnlock(&tsdb->mutex);
15,780,843✔
916
  return;
15,780,127✔
917
}
918

919
// IMPORTANT: the caller must hold fs->tsdb->mutex
920
int32_t tsdbFSEditCommit(STFileSystem *fs) {
3,723,905✔
921
  int32_t code = 0;
3,723,905✔
922
  int32_t lino = 0;
3,723,905✔
923

924
  // commit
925
  code = commit_edit(fs);
3,723,905✔
926
  TSDB_CHECK_CODE(code, lino, _exit);
3,723,905✔
927

928
  // schedule merge
929
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
3,723,905✔
930
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
3,723,905✔
931
    STFileSet *fset;
932
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
18,056,698✔
933
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
14,507,397✔
934
        tsdbFSSetBlockCommit(fset, false);
438,311✔
935
        continue;
438,311✔
936
      }
937

938
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
14,069,086✔
939
      if (lvl->level != 0) {
14,069,086✔
940
        tsdbFSSetBlockCommit(fset, false);
873,318✔
941
        continue;
873,318✔
942
      }
943

944
      // bool    skipMerge = false;
945
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
13,195,768✔
946
      if (numFile >= sttTrigger && (!vnodeATaskValid(&fset->mergeTask))) {
13,195,768✔
947
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
956,210✔
948
        if (arg == NULL) {
956,210✔
949
          code = terrno;
×
950
          TSDB_CHECK_CODE(code, lino, _exit);
×
951
        }
952

953
        arg->tsdb = fs->tsdb;
956,210✔
954
        arg->fid = fset->fid;
956,210✔
955

956
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
956,210✔
957
        TSDB_CHECK_CODE(code, lino, _exit);
956,210✔
958
      }
959

960
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
13,195,768✔
961
        tsdbFSSetBlockCommit(fset, true);
15✔
962
      } else {
963
        tsdbFSSetBlockCommit(fset, false);
13,195,753✔
964
      }
965
    }
966
  }
967

968
_exit:
3,723,905✔
969
  if (code) {
3,723,905✔
970
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
×
971
  } else {
972
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
3,723,905✔
973
  }
974
  if (tsem_post(&fs->canEdit) != 0) {
3,723,905✔
975
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
976
  }
977
  return code;
3,723,905✔
978
}
979

980
int32_t tsdbFSEditAbort(STFileSystem *fs) {
87✔
981
  int32_t code = abort_edit(fs);
87✔
982
  if (tsem_post(&fs->canEdit) != 0) {
87✔
983
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
984
  }
985
  return code;
87✔
986
}
987

988
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
35,899,907✔
989
  STFileSet   tfset = {.fid = fid};
35,899,907✔
990
  STFileSet  *pset = &tfset;
35,902,032✔
991
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
35,902,808✔
992
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
35,896,285✔
993
}
35,898,758✔
994

995
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
24,585✔
996
  int32_t    code = 0;
24,585✔
997
  STFileSet *fset;
998
  STFileSet *fset1;
24,585✔
999

1000
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
24,585✔
1001
  if (fsetArr[0] == NULL) return terrno;
24,585✔
1002

1003
  TARRAY2_INIT(fsetArr[0]);
24,585✔
1004

1005
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
24,585✔
1006
  TARRAY2_FOREACH(fs->fSetArr, fset) {
24,585✔
1007
    code = tsdbTFileSetInitCopy(fs->tsdb, fset, &fset1);
×
1008
    if (code) break;
×
1009

1010
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1011
    if (code) break;
×
1012
  }
1013
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
24,585✔
1014

1015
  if (code) {
24,585✔
1016
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1017
    taosMemoryFree(fsetArr[0]);
×
1018
    fsetArr[0] = NULL;
×
1019
  }
1020
  return code;
24,585✔
1021
}
1022

1023
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
24,890✔
1024
  if (fsetArr[0]) {
24,890✔
1025
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
25,195✔
1026
    taosMemoryFree(fsetArr[0]);
24,890✔
1027
    fsetArr[0] = NULL;
24,890✔
1028
  }
1029
}
24,890✔
1030

1031
int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
27,263✔
1032
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
27,263✔
1033
  int32_t code = tsdbFSCreateRefSnapshotWithoutLock(fs, fsetArr);
27,263✔
1034
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
27,263✔
1035
  return code;
27,263✔
1036
}
1037

1038
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
220,090,555✔
1039
  int32_t    code = 0;
220,090,555✔
1040
  STFileSet *fset, *fset1;
220,087,752✔
1041

1042
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
220,110,630✔
1043
  if (fsetArr[0] == NULL) return terrno;
220,088,079✔
1044

1045
  TARRAY2_FOREACH(fs->fSetArr, fset) {
695,320,226✔
1046
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
475,268,577✔
1047
    if (code) break;
475,254,503✔
1048

1049
    code = TARRAY2_APPEND(fsetArr[0], fset1);
475,254,503✔
1050
    if (code) {
475,257,207✔
1051
      tsdbTFileSetClear(&fset1);
×
1052
      break;
×
1053
    }
1054
  }
1055

1056
  if (code) {
220,102,291✔
1057
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1058
    taosMemoryFree(fsetArr[0]);
×
1059
    fsetArr[0] = NULL;
×
1060
  }
1061
  return code;
220,102,291✔
1062
}
1063

1064
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
220,120,619✔
1065
  if (fsetArr[0]) {
220,120,619✔
1066
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
695,378,703✔
1067
    taosMemoryFreeClear(fsetArr[0]);
220,126,144✔
1068
    fsetArr[0] = NULL;
220,076,613✔
1069
  }
1070
}
220,094,519✔
1071

1072
static SHashObj *tsdbFSetRangeArrayToHash(TFileSetRangeArray *pRanges) {
612✔
1073
  int32_t   capacity = TARRAY2_SIZE(pRanges) * 2;
612✔
1074
  SHashObj *pHash = taosHashInit(capacity, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
612✔
1075
  if (pHash == NULL) {
612✔
1076
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1077
    return NULL;
×
1078
  }
1079

1080
  for (int32_t i = 0; i < TARRAY2_SIZE(pRanges); i++) {
1,224✔
1081
    STFileSetRange *u = TARRAY2_GET(pRanges, i);
612✔
1082
    int32_t         fid = u->fid;
612✔
1083
    int32_t         code = taosHashPut(pHash, &fid, sizeof(fid), u, sizeof(*u));
612✔
1084
    tsdbDebug("range diff hash fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
612✔
1085
  }
1086
  return pHash;
612✔
1087
}
1088

1089
int32_t tsdbFSCreateCopyRangedSnapshot(STFileSystem *fs, TFileSetRangeArray *pRanges, TFileSetArray **fsetArr,
305✔
1090
                                       TFileOpArray *fopArr) {
1091
  int32_t    code = 0;
305✔
1092
  STFileSet *fset;
1093
  STFileSet *fset1;
305✔
1094
  SHashObj  *pHash = NULL;
305✔
1095

1096
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
305✔
1097
  if (fsetArr == NULL) return terrno;
305✔
1098
  TARRAY2_INIT(fsetArr[0]);
305✔
1099

1100
  if (pRanges) {
305✔
1101
    pHash = tsdbFSetRangeArrayToHash(pRanges);
305✔
1102
    if (pHash == NULL) {
305✔
1103
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1104
      goto _out;
×
1105
    }
1106
  }
1107

1108
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
305✔
1109
  TARRAY2_FOREACH(fs->fSetArr, fset) {
610✔
1110
    int64_t ever = VERSION_MAX;
305✔
1111
    if (pHash) {
305✔
1112
      int32_t         fid = fset->fid;
305✔
1113
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
305✔
1114
      if (u) {
305✔
1115
        ever = u->sver - 1;
305✔
1116
      }
1117
    }
1118

1119
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
305✔
1120
    if (code) break;
305✔
1121

1122
    code = TARRAY2_APPEND(fsetArr[0], fset1);
305✔
1123
    if (code) break;
305✔
1124
  }
1125
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
305✔
1126

1127
_out:
305✔
1128
  if (code) {
305✔
1129
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1130
    taosMemoryFree(fsetArr[0]);
×
1131
    fsetArr[0] = NULL;
×
1132
  }
1133
  if (pHash) {
305✔
1134
    taosHashCleanup(pHash);
305✔
1135
    pHash = NULL;
305✔
1136
  }
1137
  return code;
305✔
1138
}
1139

1140
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
305✔
1141

1142
int32_t tsdbFSCreateRefRangedSnapshot(STFileSystem *fs, int64_t sver, int64_t ever, TFileSetRangeArray *pRanges,
307✔
1143
                                      TFileSetRangeArray **fsrArr) {
1144
  int32_t         code = 0;
307✔
1145
  STFileSet      *fset;
1146
  STFileSetRange *fsr1 = NULL;
307✔
1147
  SHashObj       *pHash = NULL;
307✔
1148

1149
  fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
307✔
1150
  if (fsrArr[0] == NULL) {
307✔
1151
    code = terrno;
×
1152
    goto _out;
×
1153
  }
1154

1155
  tsdbInfo("pRanges size:%d", (pRanges == NULL ? 0 : TARRAY2_SIZE(pRanges)));
307✔
1156
  if (pRanges) {
307✔
1157
    pHash = tsdbFSetRangeArrayToHash(pRanges);
307✔
1158
    if (pHash == NULL) {
307✔
1159
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1160
      goto _out;
×
1161
    }
1162
  }
1163

1164
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
307✔
1165
  TARRAY2_FOREACH(fs->fSetArr, fset) {
614✔
1166
    int64_t sver1 = sver;
307✔
1167
    int64_t ever1 = ever;
307✔
1168

1169
    if (pHash) {
307✔
1170
      int32_t         fid = fset->fid;
307✔
1171
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
307✔
1172
      if (u) {
307✔
1173
        sver1 = u->sver;
307✔
1174
        tsdbDebug("range hash get fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
307✔
1175
      }
1176
    }
1177

1178
    if (sver1 > ever1) {
307✔
1179
      tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
×
1180
      continue;
×
1181
    }
1182

1183
    tsdbDebug("fsrArr:%p, fid:%d, sver:%" PRId64 ", ever:%" PRId64, fsrArr, fset->fid, sver1, ever1);
307✔
1184

1185
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
307✔
1186
    if (code) break;
307✔
1187

1188
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
307✔
1189
    if (code) break;
307✔
1190

1191
    fsr1 = NULL;
307✔
1192
  }
1193
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
307✔
1194

1195
  if (code) {
307✔
1196
    tsdbTFileSetRangeClear(&fsr1);
×
1197
    TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
×
1198
    fsrArr[0] = NULL;
×
1199
  }
1200

1201
_out:
307✔
1202
  if (pHash) {
307✔
1203
    taosHashCleanup(pHash);
307✔
1204
    pHash = NULL;
307✔
1205
  }
1206
  return code;
307✔
1207
}
1208

1209
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
307✔
1210

1211
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
16,944,997✔
1212
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1213
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
16,944,997✔
1214

1215
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
16,944,997✔
1216
  if (*fset == NULL) {
16,944,997✔
1217
    return;
13,772,786✔
1218
  }
1219

1220
  struct STFileSetCond *cond = NULL;
3,172,211✔
1221
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
3,172,211✔
1222
    cond = &(*fset)->conds[0];
2,081,433✔
1223
  } else {
1224
    cond = &(*fset)->conds[1];
1,090,778✔
1225
  }
1226

1227
  while (1) {
1228
    if (cond->running) {
3,172,847✔
1229
      cond->numWait++;
636✔
1230
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
636✔
1231
      cond->numWait--;
636✔
1232
    } else {
1233
      cond->running = true;
3,172,211✔
1234
      break;
3,172,211✔
1235
    }
1236
  }
1237

1238
  tsdbTrace("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
3,172,211✔
1239
  return;
3,172,211✔
1240
}
1241

1242
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
3,172,211✔
1243
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1244
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
3,172,211✔
1245

1246
  STFileSet *fset = NULL;
3,172,211✔
1247
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
3,172,211✔
1248
  if (fset == NULL) {
3,172,211✔
1249
    return;
×
1250
  }
1251

1252
  struct STFileSetCond *cond = NULL;
3,172,211✔
1253
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
3,172,211✔
1254
    cond = &fset->conds[0];
2,081,433✔
1255
  } else {
1256
    cond = &fset->conds[1];
1,090,778✔
1257
  }
1258

1259
  cond->running = false;
3,172,211✔
1260
  if (cond->numWait > 0) {
3,172,211✔
1261
    (void)taosThreadCondSignal(&cond->cond);
636✔
1262
  }
1263

1264
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
3,172,211✔
1265
  return;
3,172,211✔
1266
}
1267

1268
struct SFileSetReader {
1269
  STsdb     *pTsdb;
1270
  STFileSet *pFileSet;
1271
  int32_t    fid;
1272
  int64_t    startTime;
1273
  int64_t    endTime;
1274
  int64_t    lastCompactTime;
1275
  int64_t    totalSize;
1276
};
1277

1278
int32_t tsdbFileSetReaderOpen(void *pVnode, struct SFileSetReader **ppReader) {
139✔
1279
  if (pVnode == NULL || ppReader == NULL) {
139✔
1280
    return TSDB_CODE_INVALID_PARA;
×
1281
  }
1282

1283
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
139✔
1284

1285
  (*ppReader) = taosMemoryCalloc(1, sizeof(struct SFileSetReader));
139✔
1286
  if (*ppReader == NULL) {
139✔
1287
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, __LINE__,
×
1288
              tstrerror(terrno));
1289
    return terrno;
×
1290
  }
1291

1292
  (*ppReader)->pTsdb = pTsdb;
139✔
1293
  (*ppReader)->fid = INT32_MIN;
139✔
1294
  (*ppReader)->pFileSet = NULL;
139✔
1295

1296
  return TSDB_CODE_SUCCESS;
139✔
1297
}
1298

1299
static int32_t tsdbFileSetReaderNextNoLock(struct SFileSetReader *pReader) {
278✔
1300
  STsdb  *pTsdb = pReader->pTsdb;
278✔
1301
  int32_t code = TSDB_CODE_SUCCESS;
278✔
1302

1303
  tsdbTFileSetClear(&pReader->pFileSet);
278✔
1304

1305
  STFileSet *fset = &(STFileSet){
278✔
1306
      .fid = pReader->fid,
278✔
1307
  };
1308

1309
  STFileSet **fsetPtr = TARRAY2_SEARCH(pReader->pTsdb->pFS->fSetArr, &fset, tsdbTFileSetCmprFn, TD_GT);
278✔
1310
  if (fsetPtr == NULL) {
278✔
1311
    pReader->fid = INT32_MAX;
139✔
1312
    return TSDB_CODE_NOT_FOUND;
139✔
1313
  }
1314

1315
  // ref file set
1316
  code = tsdbTFileSetInitRef(pReader->pTsdb, *fsetPtr, &pReader->pFileSet);
139✔
1317
  if (code) return code;
139✔
1318

1319
  // get file set details
1320
  pReader->fid = pReader->pFileSet->fid;
139✔
1321
  tsdbFidKeyRange(pReader->fid, pTsdb->keepCfg.days, pTsdb->keepCfg.precision, &pReader->startTime, &pReader->endTime);
139✔
1322
  if (pTsdb->keepCfg.precision == TSDB_TIME_PRECISION_MICRO) {
139✔
1323
    pReader->startTime /= 1000;
×
1324
    pReader->endTime /= 1000;
×
1325
  } else if (pTsdb->keepCfg.precision == TSDB_TIME_PRECISION_NANO) {
139✔
1326
    pReader->startTime /= 1000000;
×
1327
    pReader->endTime /= 1000000;
×
1328
  }
1329
  pReader->lastCompactTime = pReader->pFileSet->lastCompact;
139✔
1330
  pReader->totalSize = 0;
139✔
1331
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
695✔
1332
    STFileObj *fobj = pReader->pFileSet->farr[i];
556✔
1333
    if (fobj) {
556✔
1334
      pReader->totalSize += fobj->f->size;
×
1335
    }
1336
  }
1337
  SSttLvl *lvl;
1338
  TARRAY2_FOREACH(pReader->pFileSet->lvlArr, lvl) {
278✔
1339
    STFileObj *fobj;
1340
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { pReader->totalSize += fobj->f->size; }
278✔
1341
  }
1342

1343
  return code;
139✔
1344
}
1345

1346
int32_t tsdbFileSetReaderNext(struct SFileSetReader *pReader) {
278✔
1347
  int32_t code = TSDB_CODE_SUCCESS;
278✔
1348
  (void)taosThreadMutexLock(&pReader->pTsdb->mutex);
278✔
1349
  code = tsdbFileSetReaderNextNoLock(pReader);
278✔
1350
  (void)taosThreadMutexUnlock(&pReader->pTsdb->mutex);
278✔
1351
  return code;
278✔
1352
}
1353

1354
extern bool tsdbShouldCompact(STFileSet *fset, int32_t vgId, int32_t expLevel, ETsdbOpType type);
1355
int32_t tsdbFileSetGetEntryField(struct SFileSetReader *pReader, const char *field, void *value) {
834✔
1356
  const char *fieldName;
1357

1358
  if (pReader->fid == INT32_MIN || pReader->fid == INT32_MAX) {
834✔
1359
    return TSDB_CODE_INVALID_PARA;
×
1360
  }
1361

1362
  fieldName = "fileset_id";
834✔
1363
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
834✔
1364
    *(int32_t *)value = pReader->fid;
139✔
1365
    return TSDB_CODE_SUCCESS;
139✔
1366
  }
1367

1368
  fieldName = "start_time";
695✔
1369
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
695✔
1370
    *(int64_t *)value = pReader->startTime;
139✔
1371
    return TSDB_CODE_SUCCESS;
139✔
1372
  }
1373

1374
  fieldName = "end_time";
556✔
1375
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
556✔
1376
    *(int64_t *)value = pReader->endTime;
139✔
1377
    return TSDB_CODE_SUCCESS;
139✔
1378
  }
1379

1380
  fieldName = "total_size";
417✔
1381
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
417✔
1382
    *(int64_t *)value = pReader->totalSize;
139✔
1383
    return TSDB_CODE_SUCCESS;
139✔
1384
  }
1385

1386
  fieldName = "last_compact_time";
278✔
1387
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
278✔
1388
    *(int64_t *)value = pReader->lastCompactTime;
139✔
1389
    return TSDB_CODE_SUCCESS;
139✔
1390
  }
1391

1392
  fieldName = "should_compact";
139✔
1393
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
139✔
1394
    *(bool *)value = false;
139✔
1395
#ifdef TD_ENTERPRISE
1396
    *(bool *)value = tsdbShouldCompact(pReader->pFileSet, pReader->pTsdb->pVnode->config.vgId, 0, TSDB_OPTR_NORMAL);
139✔
1397
#endif
1398
    return TSDB_CODE_SUCCESS;
139✔
1399
  }
1400

1401
  fieldName = "details";
×
1402
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1403
    // TODO
1404
    return TSDB_CODE_SUCCESS;
×
1405
  }
1406

1407
  return TSDB_CODE_INVALID_PARA;
×
1408
}
1409

1410
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
139✔
1411
  if (ppReader == NULL || *ppReader == NULL) {
139✔
1412
    return;
×
1413
  }
1414

1415
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
139✔
1416
  taosMemoryFree(*ppReader);
139✔
1417

1418
  *ppReader = NULL;
139✔
1419
  return;
139✔
1420
}
1421

1422
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1423
  if (fObj == NULL) return;
22,528,976✔
1424

1425
  int64_t sz = fObj->f->size;
12,479,486✔
1426
  // level == 0, primary storage
1427
  // level == 1, second storage,
1428
  // level == 2, third storage
1429
  int32_t level = fObj->f->did.level;
12,479,486✔
1430
  if (level >= 0 && level < TFS_MAX_TIERS) {
12,479,486✔
1431
    szArr[level] += sz;
12,479,486✔
1432
  }
1433
}
1434

1435
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1436
  int32_t code = 0;
5,882,804✔
1437
  int64_t levelSize[TFS_MAX_TIERS] = {0};
5,882,804✔
1438
  int64_t ssSize = 0;
5,882,804✔
1439

1440
  const STFileSet *fset;
1441
  const SSttLvl   *stt = NULL;
5,882,804✔
1442
  const STFileObj *fObj = NULL;
5,882,804✔
1443

1444
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
5,882,804✔
1445
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
5,882,804✔
1446

1447
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
12,478,015✔
1448
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
32,976,055✔
1449
      getLevelSize(fset->farr[t], levelSize);
26,380,844✔
1450
    }
1451

1452
    TARRAY2_FOREACH(fset->lvlArr, stt) {
10,830,636✔
1453
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
8,549,234✔
1454
    }
1455

1456
    fObj = fset->farr[TSDB_FTYPE_DATA];
6,595,211✔
1457
    if (fObj) {
6,595,211✔
1458
      int32_t lcn = fObj->f->lcn;
2,719,079✔
1459
      if (lcn > 1) {
2,719,079✔
1460
        ssSize += ((lcn - 1) * chunksize);
×
1461
      }
1462
    }
1463
  }
1464

1465
  pInfo->l1Size = levelSize[0];
5,882,804✔
1466
  pInfo->l2Size = levelSize[1];
5,882,804✔
1467
  pInfo->l3Size = levelSize[2];
5,882,804✔
1468
  pInfo->ssSize = ssSize;
5,882,804✔
1469
  return code;
5,882,804✔
1470
}
1471
int32_t tsdbGetFsSize(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
5,882,804✔
1472
  int32_t code = 0;
5,882,804✔
1473

1474
  (void)taosThreadMutexLock(&tsdb->mutex);
5,882,804✔
1475
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
5,882,804✔
1476
  (void)taosThreadMutexUnlock(&tsdb->mutex);
5,882,804✔
1477
  return code;
5,882,804✔
1478
}
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