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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

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

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

51.18
/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) {
698,899✔
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
698,899!
42
  if (fs[0] == NULL) {
701,789!
43
    return terrno;
×
44
  }
45

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

53
  fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
701,389✔
54
  fs[0]->neid = 0;
702,128✔
55
  TARRAY2_INIT(fs[0]->fSetArr);
701,329✔
56
  TARRAY2_INIT(fs[0]->fSetArrTmp);
700,989✔
57

58
  return 0;
701,389✔
59
}
60

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

64
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
702,041!
65
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
701,719!
66
  if (tsem_destroy(&fs[0]->canEdit) != 0) {
702,262!
67
    tsdbError("failed to destroy semaphore");
×
68
  }
69
  taosMemoryFree(fs[0]);
701,719!
70
  fs[0] = NULL;
702,072✔
71
}
72

73
void current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
4,260,911✔
74
  int32_t offset = 0;
4,260,911✔
75

76
  vnodeGetPrimaryPath(pTsdb->pVnode, false, fname, TSDB_FILENAME_LEN);
4,260,911✔
77
  offset = strlen(fname);
4,262,176!
78
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s%s", TD_DIRSEP, pTsdb->name, TD_DIRSEP,
4,261,701✔
79
           gCurrentFname[ftype]);
4,261,828✔
80
}
4,262,038✔
81

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

88
  data = cJSON_PrintUnformatted(json);
1,242,729✔
89
  if (data == NULL) {
1,242,471!
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);
1,242,471✔
94
  if (fp == NULL) {
1,241,790!
95
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
96
  }
97

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

102
  if (taosFsyncFile(fp) < 0) {
1,242,390!
103
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
104
  }
105

106
_exit:
1,242,381✔
107
  if (code) {
1,242,381!
108
    tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
×
109
  }
110
  taosMemoryFree(data);
1,242,729!
111
  taosCloseFileWithLog(&fp);
1,241,805!
112
  return code;
1,241,959✔
113
}
114

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

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

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

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

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

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

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

156
int32_t save_fs(const TFileSetArray *arr, const char *fname) {
1,241,485✔
157
  int32_t code = 0;
1,241,485✔
158
  int32_t lino = 0;
1,241,485✔
159

160
  cJSON *json = cJSON_CreateObject();
1,241,485✔
161
  if (json == NULL) {
1,242,231!
162
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
163
  }
164

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

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
1,240,872✔
172
  if (!ajson) {
1,241,790!
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
10,311,399✔
177
    cJSON *item = cJSON_CreateObject();
9,068,763✔
178
    if (!item) {
9,069,487!
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
9,069,487✔
182

183
    code = tsdbTFileSetToJson(fset, item);
9,069,345✔
184
    TSDB_CHECK_CODE(code, lino, _exit);
9,069,609!
185
  }
186

187
  code = save_json(json, fname);
1,241,430✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
1,242,436!
189

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

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

202
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
133,912!
203

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

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

212
  /* fmtv */
213
  item1 = cJSON_GetObjectItem(json, "fmtv");
133,912✔
214
  if (cJSON_IsNumber(item1)) {
133,912!
215
    if (item1->valuedouble != 1) {
133,912!
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");
133,912✔
224
  if (cJSON_IsArray(item1)) {
133,912!
225
    const cJSON *item2;
226
    cJSON_ArrayForEach(item2, item1) {
278,637!
227
      STFileSet *fset;
144,725✔
228
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
144,725✔
229
      TSDB_CHECK_CODE(code, lino, _exit);
144,725!
230

231
      code = TARRAY2_APPEND(arr, fset);
144,725!
232
      TSDB_CHECK_CODE(code, lino, _exit);
144,725!
233
    }
234
    TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
133,587✔
235
  } else {
236
    code = TSDB_CODE_FILE_CORRUPTED;
×
237
    TSDB_CHECK_CODE(code, lino, _exit);
×
238
  }
239

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

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
9,743,431✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
9,069,429✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
9,069,423!
260

261
    if (fset1 && fset2) {
9,069,600!
262
      if (fset1->fid < fset2->fid) {
830,706!
263
        // delete fset1
UNCOV
264
        tsdbTFileSetRemove(fset1);
×
UNCOV
265
        i1++;
×
266
      } else if (fset1->fid > fset2->fid) {
830,895✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
3,424✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
3,424!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
3,424✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
3,424!
272
        i1++;
3,424✔
273
        i2++;
3,424✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
827,471✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
827,288!
278
        i1++;
827,288✔
279
        i2++;
827,288✔
280
      }
281
    } else if (fset1) {
8,238,894!
282
      // delete fset1
UNCOV
283
      tsdbTFileSetRemove(fset1);
×
UNCOV
284
      i1++;
×
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
8,238,894✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
8,238,894!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
8,238,894✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
8,238,894!
291
      i1++;
8,238,894✔
292
      i2++;
8,238,894✔
293
    }
294
  }
295

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

303
static int32_t commit_edit(STFileSystem *fs) {
673,700✔
304
  char current[TSDB_FILENAME_LEN];
673,642✔
305
  char current_t[TSDB_FILENAME_LEN];
674,115✔
306

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
674,173✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
674,173✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
509,421✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
164,752✔
312
  }
313

314
  int32_t code;
315
  int32_t lino;
316
  TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit);
673,996!
317

318
  code = apply_commit(fs);
674,173✔
319
  TSDB_CHECK_CODE(code, lino, _exit);
674,173!
320

321
_exit:
674,173✔
322
  if (code) {
674,054!
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);
674,054✔
327
  }
328
  return code;
674,454✔
329
}
330

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

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

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

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

UNCOV
351
  code = apply_abort(fs);
×
UNCOV
352
  TSDB_CHECK_CODE(code, lino, _exit);
×
353

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

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

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

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

400
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
351,747!
401

402
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
351,747!
403

404
  entry->next = hash->buckets[idx];
351,747✔
405
  hash->buckets[idx] = entry;
351,747✔
406
  hash->numFile++;
351,747✔
407

408
  return 0;
351,747✔
409
}
410

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

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

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

429
  // other
430
  STFileSet *fset = NULL;
133,912✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
278,637✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
723,625✔
434
      if (fset->farr[i] != NULL) {
578,900✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
42,653✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
42,653!
437

438
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
42,653!
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;
144,725✔
457
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
311,619✔
458
      STFileObj *fobj;
459
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
342,076✔
460
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
175,182✔
461
        TSDB_CHECK_CODE(code, lino, _exit);
175,182!
462
      }
463
    }
464
  }
465

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

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

477
  STFileHashEntry *entry = hash->buckets[idx];
351,747✔
478
  while (entry) {
352,288!
479
    if (strcmp(entry->fname, fname) == 0) {
352,288!
480
      return entry;
351,747✔
481
    }
482
    entry = entry->next;
541✔
483
  }
484

UNCOV
485
  return NULL;
×
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
133,912✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
546,445,940✔
490
    STFileHashEntry *entry = hash->buckets[i];
546,383,612✔
491
    while (entry) {
546,663,775✔
492
      STFileHashEntry *next = entry->next;
351,747✔
493
      taosMemoryFree(entry);
351,747!
494
      entry = next;
351,747✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
133,912!
498
  memset(hash, 0, sizeof(*hash));
133,912!
499
}
133,912✔
500

501
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
133,730✔
502
  int32_t code = 0;
133,730✔
503
  int32_t lino = 0;
133,730✔
504
  int32_t corrupt = false;
133,912✔
505

506
  if (fs->tsdb->pVnode->mounted) goto _exit;
133,912!
507

508
  {  // scan each file
509
    STFileSet *fset = NULL;
133,912✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
278,637✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
722,897✔
513
        if (fset->farr[ftype] == NULL) continue;
578,354✔
514
        STFileObj *fobj = fset->farr[ftype];
42,653✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
42,653✔
516
        if (code) {
42,653!
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) {
311,437✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
342,076✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
175,182✔
528
          if (code) {
175,182!
529
            fset->maxVerValid =
×
530
                (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
531
            corrupt = true;
×
532
          }
533
        }
534
      }
535
    }
536
  }
537

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

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

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

553
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
619,571✔
554
      if (taosIsDir(file->aname)) continue;
485,659✔
555

556
      if (tsdbFSGetFileObjHashEntry(&fobjHash, file->aname) == NULL) {
351,747!
UNCOV
557
        tsdbRemoveFile(file->aname);
×
558
      }
559
    }
560

561
    tsdbFSDestroyFileObjHash(&fobjHash);
133,912✔
562

563
  _close_dir:
133,912✔
564
    tfsClosedir(dir);
133,912✔
565
  }
566

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

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

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

581
  // scan and fix
582
  int32_t code = 0;
133,912✔
583
  int32_t lino = 0;
133,912✔
584

585
  code = tsdbFSDoSanAndFix(fs);
133,912✔
586
  TSDB_CHECK_CODE(code, lino, _exit);
133,912!
587

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

595
static int32_t tsdbFSDupState(STFileSystem *fs) {
806,656✔
596
  int32_t code;
597

598
  const TFileSetArray *src = fs->fSetArr;
806,656✔
599
  TFileSetArray       *dst = fs->fSetArrTmp;
806,836✔
600

601
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
1,633,697✔
602

603
  const STFileSet *fset1;
604
  TARRAY2_FOREACH(src, fset1) {
1,779,193✔
605
    STFileSet *fset2;
972,077✔
606
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
972,077✔
607
    if (code) return code;
971,772!
608
    code = TARRAY2_APPEND(dst, fset2);
971,772!
609
    if (code) return code;
972,196!
610
  }
611

612
  return 0;
806,997✔
613
}
614

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

620
  char fCurrent[TSDB_FILENAME_LEN];
701,731✔
621
  char cCurrent[TSDB_FILENAME_LEN];
701,006✔
622
  char mCurrent[TSDB_FILENAME_LEN];
701,006✔
623

624
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
701,064✔
625
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
702,468✔
626
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
702,128✔
627

628
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
702,068✔
629
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
133,912✔
630
    TSDB_CHECK_CODE(code, lino, _exit);
133,912!
631

632
    if (taosCheckExistFile(cCurrent)) {
133,912!
633
      // current.c.json exists
634

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

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

653
    code = tsdbFSDupState(fs);
133,912✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
133,912!
655

656
    code = tsdbFSScanAndFix(fs);
133,912✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
133,912!
658
  } else {
659
    code = save_fs(fs->fSetArr, fCurrent);
568,217✔
660
    TSDB_CHECK_CODE(code, lino, _exit);
568,556!
661
  }
662

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

672
static void close_file_system(STFileSystem *fs) {
702,262✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
9,088,948!
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
9,088,951✔
675
}
702,041✔
676

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

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

690
  code = tsdbFSDupState(fs);
673,393✔
691
  if (code) return code;
672,685!
692

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
672,685✔
694
  STFileSet      *fset = NULL;
672,874✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
672,569✔
697
  TSKEY           now = taosGetTimestampMs();
673,464✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
10,550,425✔
699
    if (!fset || fset->fid != op->fid) {
9,877,823✔
700
      STFileSet tfset = {.fid = op->fid};
8,776,330✔
701
      fset = &tfset;
8,775,622✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
8,776,330✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
8,776,330✔
704

705
      if (!fset) {
8,776,330✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
8,242,016✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
8,241,518!
708

709
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
8,241,918✔
710
        TSDB_CHECK_CODE(code, lino, _exit);
8,241,918!
711
      }
712
    }
713

714
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
9,877,610✔
715
    TSDB_CHECK_CODE(code, lino, _exit);
9,878,348!
716

717
    if (fid != op->fid) {
9,878,348✔
718
      fid = op->fid;
8,776,391✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
8,776,321✔
720
        fset->lastCommit = now;
8,611,758✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
164,563!
UNCOV
722
        fset->lastCompact = now;
×
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
164,563!
724
        fset->lastMigrate = now;
×
725
      } else if (etype == TSDB_FEDIT_ROLLUP) {
164,563!
UNCOV
726
        fset->lastRollupLevel = fs->rollupLevel;
×
UNCOV
727
        fset->lastRollup = now;
×
UNCOV
728
        fset->lastCompact = now;  // rollup implies compact
×
729
      }
730
    }
731
  }
732

733
  // remove empty empty stt level and empty file set
734
  int32_t i = 0;
673,051✔
735
  while (i < TARRAY2_SIZE(fsetArray)) {
9,740,457✔
736
    fset = TARRAY2_GET(fsetArray, i);
9,069,789✔
737

738
    SSttLvl *lvl;
739
    int32_t  j = 0;
9,068,404✔
740
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
18,310,586✔
741
      lvl = TARRAY2_GET(fset->lvlArr, j);
9,243,398✔
742

743
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
9,243,510✔
744
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
102,014!
745
      } else {
746
        j++;
9,140,168✔
747
      }
748
    }
749

750
    if (tsdbTFileSetIsEmpty(fset)) {
9,068,752✔
751
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
116!
752
    } else {
753
      i++;
9,067,406✔
754
    }
755
  }
756

757
_exit:
670,692✔
758
  if (code) {
672,208!
759
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
760
  }
761
  return code;
672,154✔
762
}
763

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

769
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
700,544✔
770
  TSDB_CHECK_CODE(code, lino, _exit);
701,789!
771

772
  code = create_fs(pTsdb, fs);
701,789✔
773
  TSDB_CHECK_CODE(code, lino, _exit);
701,728!
774

775
  code = open_fs(fs[0], rollback);
701,728✔
776
  TSDB_CHECK_CODE(code, lino, _exit);
702,468!
777

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

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

792
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
703,012✔
793
  STFileSystem *fs = pTsdb->pFS;
703,012✔
794
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
703,012✔
795
  if (asyncTasks == NULL) {
702,791!
796
    return terrno;
×
797
  }
798

799
  (void)taosThreadMutexLock(&pTsdb->mutex);
702,791✔
800

801
  // disable
802
  pTsdb->bgTaskDisabled = true;
703,012✔
803

804
  // collect channel
805
  STFileSet *fset;
806
  TARRAY2_FOREACH(fs->fSetArr, fset) {
9,090,055✔
807
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
16,774,086!
808
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
16,773,777!
809
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
16,773,342!
810
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
16,773,525!
811
      taosArrayDestroy(asyncTasks);
×
812
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
813
      return terrno;
×
814
    }
815
    tsdbFSSetBlockCommit(fset, false);
8,386,917✔
816
  }
817

818
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
703,012✔
819

820
  // destroy all channels
821
  for (int32_t k = 0; k < 2; k++) {
2,108,609✔
822
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
68,499,757✔
823
      SVATaskID *task = taosArrayGet(asyncTasks, i);
67,091,136✔
824
      if (k == 0) {
67,090,138✔
825
        (void)vnodeACancel(task);
33,546,168✔
826
      } else {
827
        vnodeAWait(task);
33,543,970✔
828
      }
829
    }
830
  }
831
  taosArrayDestroy(asyncTasks);
703,012✔
832

833
#ifdef TD_ENTERPRISE
834
  tsdbStopAllCompTask(pTsdb);
702,585✔
835
#endif
836
  tsdbStopAllRetentionTask(pTsdb);
703,012✔
837
  return 0;
703,012✔
838
}
839

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

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

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

859
int64_t tsdbFSAllocEid(STFileSystem *fs) {
681,412✔
860
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
681,412✔
861
  int64_t cid = ++fs->neid;
682,090✔
862
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
682,090✔
863
  return cid;
682,090✔
864
}
865

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

872
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
673,393✔
873
  int32_t code = 0;
673,393✔
874
  int32_t lino;
875
  char    current_t[TSDB_FILENAME_LEN];
673,335✔
876

877
  if (etype == TSDB_FEDIT_COMMIT) {
673,393✔
878
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
508,932✔
879
  } else {
880
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
164,461✔
881
  }
882

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

888
  // edit
889
  code = edit_fs(fs, opArray, etype);
673,745✔
890
  TSDB_CHECK_CODE(code, lino, _exit);
672,927!
891

892
  // save fs
893
  code = save_fs(fs->fSetArrTmp, current_t);
672,927✔
894
  TSDB_CHECK_CODE(code, lino, _exit);
674,173!
895

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

906
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
17,138,874✔
907
  if (block) {
17,138,874✔
908
    fset->blockCommit = true;
13,764✔
909
  } else {
910
    fset->blockCommit = false;
17,125,110✔
911
    if (fset->numWaitCommit > 0) {
17,125,110✔
912
      (void)taosThreadCondSignal(&fset->canCommit);
11,766✔
913
    }
914
  }
915
}
17,138,691✔
916

917
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
8,609,145✔
918
  (void)taosThreadMutexLock(&tsdb->mutex);
8,609,145✔
919
  STFileSet *fset;
8,608,618✔
920
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
8,608,605✔
921
  bool blockCommit = false;
8,599,604✔
922
  if (fset) {
8,599,604✔
923
    blockCommit = fset->blockCommit;
370,386!
924
  }
925
  if (fset) {
8,599,604✔
926
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
382,152!
927
      while (fset->blockCommit) {
928
        fset->numWaitCommit++;
929
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
930
        fset->numWaitCommit--;
931
      }
932
    });
933
  }
934
  if (blockCommit) {
8,606,151✔
935
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
11,766!
936
  }
937
  (void)taosThreadMutexUnlock(&tsdb->mutex);
8,606,151✔
938
  return;
8,606,722✔
939
}
940

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

946
  // commit
947
  code = commit_edit(fs);
674,173✔
948
  TSDB_CHECK_CODE(code, lino, _exit);
674,173!
949

950
  // schedule merge
951
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
674,173✔
952
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
674,173!
953
    STFileSet *fset;
954
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
9,393,206✔
955
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
8,751,957✔
956
        tsdbFSSetBlockCommit(fset, false);
21,105✔
957
        continue;
21,105✔
958
      }
959

960
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
8,730,852✔
961
      if (lvl->level != 0) {
8,730,852✔
962
        tsdbFSSetBlockCommit(fset, false);
44,825✔
963
        continue;
44,825✔
964
      }
965

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

975
        arg->tsdb = fs->tsdb;
168,223✔
976
        arg->fid = fset->fid;
168,223✔
977

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

982
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
8,686,027✔
983
        tsdbFSSetBlockCommit(fset, true);
13,764✔
984
      } else {
985
        tsdbFSSetBlockCommit(fset, false);
8,672,263✔
986
      }
987
    }
988
  }
989

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

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

1010
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
17,940,297✔
1011
  STFileSet   tfset = {.fid = fid};
17,940,297✔
1012
  STFileSet  *pset = &tfset;
17,940,346✔
1013
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
17,934,362✔
1014
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
17,937,862✔
1015
}
17,935,290✔
1016

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

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

1025
  TARRAY2_INIT(fsetArr[0]);
544✔
1026

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

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

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

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

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

1060
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
41,302,221✔
1061
  int32_t    code = 0;
41,302,221✔
1062
  STFileSet *fset, *fset1;
41,302,221✔
1063

1064
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
41,312,203!
1065
  if (fsetArr[0] == NULL) return terrno;
41,307,412!
1066

1067
  TARRAY2_FOREACH(fs->fSetArr, fset) {
109,569,744✔
1068
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
68,286,704✔
1069
    if (code) break;
68,282,582!
1070

1071
    code = TARRAY2_APPEND(fsetArr[0], fset1);
68,282,582!
1072
    if (code) {
68,282,086!
1073
      tsdbTFileSetClear(&fset1);
×
1074
      break;
×
1075
    }
1076
  }
1077

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

1086
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
41,320,437✔
1087
  if (fsetArr[0]) {
41,320,437!
1088
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
109,622,399!
1089
    taosMemoryFreeClear(fsetArr[0]);
41,322,802!
1090
    fsetArr[0] = NULL;
41,311,479✔
1091
  }
1092
}
41,314,242✔
1093

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

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

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

UNCOV
1118
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
×
UNCOV
1119
  if (fsetArr == NULL) return terrno;
×
UNCOV
1120
  TARRAY2_INIT(fsetArr[0]);
×
1121

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

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

UNCOV
1141
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
×
UNCOV
1142
    if (code) break;
×
1143

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

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

UNCOV
1162
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
×
1163

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

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

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

UNCOV
1186
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
×
UNCOV
1187
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
UNCOV
1188
    int64_t sver1 = sver;
×
UNCOV
1189
    int64_t ever1 = ever;
×
1190

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

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

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

UNCOV
1207
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
×
UNCOV
1208
    if (code) break;
×
1209

UNCOV
1210
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
×
UNCOV
1211
    if (code) break;
×
1212

UNCOV
1213
    fsr1 = NULL;
×
1214
  }
UNCOV
1215
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
×
1216

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

UNCOV
1223
_out:
×
UNCOV
1224
  if (pHash) {
×
UNCOV
1225
    taosHashCleanup(pHash);
×
UNCOV
1226
    pHash = NULL;
×
1227
  }
UNCOV
1228
  return code;
×
1229
}
1230

UNCOV
1231
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
×
1232

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

1237
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
8,787,825✔
1238
  if (*fset == NULL) {
8,787,654✔
1239
    return;
8,244,599✔
1240
  }
1241

1242
  struct STFileSetCond *cond = NULL;
543,055✔
1243
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
543,055✔
1244
    cond = &(*fset)->conds[0];
381,966✔
1245
  } else {
1246
    cond = &(*fset)->conds[1];
161,089✔
1247
  }
1248

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

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

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

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

1274
  struct STFileSetCond *cond = NULL;
543,055✔
1275
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
543,055✔
1276
    cond = &fset->conds[0];
381,966✔
1277
  } else {
1278
    cond = &fset->conds[1];
161,089✔
1279
  }
1280

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

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

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

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

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

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

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

1318
  return TSDB_CODE_SUCCESS;
×
1319
}
1320

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

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

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

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

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

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

1358
  return code;
×
1359
}
1360

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

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

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

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

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

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

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

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

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

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

1422
  return TSDB_CODE_INVALID_PARA;
×
1423
}
1424

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

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

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

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

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

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

1455
  const STFileSet *fset;
1456
  const SSttLvl   *stt = NULL;
1,430,958✔
1457
  const STFileObj *fObj = NULL;
1,430,958✔
1458

1459
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
1,430,958✔
1460
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
1,430,958✔
1461

1462
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
3,714,935✔
1463
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
11,419,885✔
1464
      getLevelSize(fset->farr[t], levelSize);
9,135,908✔
1465
    }
1466

1467
    TARRAY2_FOREACH(fset->lvlArr, stt) {
3,137,923✔
1468
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
1,724,280✔
1469
    }
1470

1471
    fObj = fset->farr[TSDB_FTYPE_DATA];
2,283,977✔
1472
    if (fObj) {
2,283,977✔
1473
      int32_t lcn = fObj->f->lcn;
1,485,576✔
1474
      if (lcn > 1) {
1,485,576!
1475
        ssSize += ((lcn - 1) * chunksize);
×
1476
      }
1477
    }
1478
  }
1479

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

1489
  (void)taosThreadMutexLock(&tsdb->mutex);
1,430,958✔
1490
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
1,430,958✔
1491
  (void)taosThreadMutexUnlock(&tsdb->mutex);
1,430,958✔
1492
  return code;
1,430,958✔
1493
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc