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

taosdata / TDengine / #4827

28 Oct 2025 01:21AM UTC coverage: 59.754% (+0.02%) from 59.732%
#4827

push

travis-ci

web-flow
test: tidy up cases on main branch(10-27) (#33381)

121341 of 258518 branches covered (46.94%)

Branch coverage included in aggregate %.

193621 of 268583 relevant lines covered (72.09%)

4339204.91 hits per line

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

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

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

53
  fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
9,414✔
54
  fs[0]->neid = 0;
9,414✔
55
  TARRAY2_INIT(fs[0]->fSetArr);
9,414✔
56
  TARRAY2_INIT(fs[0]->fSetArrTmp);
9,414✔
57

58
  return 0;
9,414✔
59
}
60

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

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

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

76
  vnodeGetPrimaryPath(pTsdb->pVnode, false, fname, TSDB_FILENAME_LEN);
57,557✔
77
  offset = strlen(fname);
57,567✔
78
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s%s", TD_DIRSEP, pTsdb->name, TD_DIRSEP,
57,567✔
79
           gCurrentFname[ftype]);
80
}
57,567✔
81

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

88
  data = cJSON_PrintUnformatted(json);
15,653✔
89
  if (data == NULL) {
15,646!
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);
15,646✔
94
  if (fp == NULL) {
15,651!
95
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
96
  }
97

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

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

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

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

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

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

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

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

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

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

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

160
  cJSON *json = cJSON_CreateObject();
15,654✔
161
  if (json == NULL) {
15,653!
162
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
163
  }
164

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

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
15,653✔
172
  if (!ajson) {
15,654!
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
31,927✔
177
    cJSON *item = cJSON_CreateObject();
16,273✔
178
    if (!item) {
16,272!
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
16,272✔
182

183
    code = tsdbTFileSetToJson(fset, item);
16,271✔
184
    TSDB_CHECK_CODE(code, lino, _exit);
16,273!
185
  }
186

187
  code = save_json(json, fname);
15,654✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
15,655!
189

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

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

202
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
2,656!
203

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

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

212
  /* fmtv */
213
  item1 = cJSON_GetObjectItem(json, "fmtv");
2,657✔
214
  if (cJSON_IsNumber(item1)) {
2,650!
215
    if (item1->valuedouble != 1) {
2,651!
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");
2,651✔
224
  if (cJSON_IsArray(item1)) {
2,645!
225
    const cJSON *item2;
226
    cJSON_ArrayForEach(item2, item1) {
4,668!
227
      STFileSet *fset;
228
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
2,013✔
229
      TSDB_CHECK_CODE(code, lino, _exit);
2,014!
230

231
      code = TARRAY2_APPEND(arr, fset);
2,014✔
232
      TSDB_CHECK_CODE(code, lino, _exit);
2,015!
233
    }
234
    TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
2,655✔
235
  } else {
236
    code = TSDB_CODE_FILE_CORRUPTED;
×
237
    TSDB_CHECK_CODE(code, lino, _exit);
×
238
  }
239

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

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
25,287✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
16,391✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
16,391✔
260

261
    if (fset1 && fset2) {
16,391✔
262
      if (fset1->fid < fset2->fid) {
10,507✔
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
111✔
265
        i1++;
111✔
266
      } else if (fset1->fid > fset2->fid) {
10,396✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
15✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
15!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
15✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
15!
272
        i1++;
15✔
273
        i2++;
15✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
10,381✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
10,381!
278
        i1++;
10,381✔
279
        i2++;
10,381✔
280
      }
281
    } else if (fset1) {
5,884✔
282
      // delete fset1
283
      tsdbTFileSetRemove(fset1);
7✔
284
      i1++;
7✔
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
5,877✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
5,877!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
5,877✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
5,877!
291
      i1++;
5,877✔
292
      i2++;
5,877✔
293
    }
294
  }
295

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

303
static int32_t commit_edit(STFileSystem *fs) {
8,896✔
304
  char current[TSDB_FILENAME_LEN];
305
  char current_t[TSDB_FILENAME_LEN];
306

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
8,896✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
8,896✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
6,457✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2,439✔
312
  }
313

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

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

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

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

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

338
  if (fs->etype == TSDB_FEDIT_COMMIT) {
×
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;
346
  if ((code = taosRemoveFile(fname))) {
×
347
    code = TAOS_SYSTEM_ERROR(code);
×
348
    TSDB_CHECK_CODE(code, lino, _exit);
×
349
  }
350

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

354
_exit:
×
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 {
359
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
×
360
  }
361
  return code;
×
362
}
363

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

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

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

381
      found = true;
×
382
    }
383

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

391
  return 0;
4,027✔
392
}
393

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

400
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
6,669✔
401

402
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
6,669✔
403

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

408
  return 0;
6,668✔
409
}
410

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

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

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

429
  // other
430
  STFileSet *fset = NULL;
2,640✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
4,647✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
10,035✔
434
      if (fset->farr[i] != NULL) {
8,028✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
2,085✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
2,085!
437

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

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

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

455
    // stt file
456
    SSttLvl *lvl = NULL;
2,007✔
457
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
3,738✔
458
      STFileObj *fobj;
459
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,674✔
460
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
1,943✔
461
        TSDB_CHECK_CODE(code, lino, _exit);
1,943!
462
      }
463
    }
464
  }
465

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

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

477
  STFileHashEntry *entry = hash->buckets[idx];
6,669✔
478
  while (entry) {
6,680!
479
    if (strcmp(entry->fname, fname) == 0) {
6,680✔
480
      return entry;
6,669✔
481
    }
482
    entry = entry->next;
11✔
483
  }
484

485
  return NULL;
×
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,641✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
10,787,291✔
490
    STFileHashEntry *entry = hash->buckets[i];
10,784,650✔
491
    while (entry) {
10,791,319✔
492
      STFileHashEntry *next = entry->next;
6,669✔
493
      taosMemoryFree(entry);
6,669!
494
      entry = next;
6,669✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
2,641!
498
  memset(hash, 0, sizeof(*hash));
2,640✔
499
}
2,640✔
500

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

506
  if (fs->tsdb->pVnode->mounted) goto _exit;
2,650✔
507

508
  {  // scan each file
509
    STFileSet *fset = NULL;
2,634✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
4,640✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
10,015✔
513
        if (fset->farr[ftype] == NULL) continue;
8,011✔
514
        STFileObj *fobj = fset->farr[ftype];
2,084✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
2,084✔
516
        if (code) {
2,084!
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) {
3,734✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,671✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
1,941✔
528
          if (code) {
1,943!
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) {
2,636!
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;
2,636✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2,636!
548

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

553
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
11,950✔
554
      if (taosIsDir(file->aname)) continue;
9,310✔
555

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

561
    tsdbFSDestroyFileObjHash(&fobjHash);
2,641✔
562

563
  _close_dir:
2,641✔
564
    tfsClosedir(dir);
2,641✔
565
  }
566

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

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

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

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

585
  code = tsdbFSDoSanAndFix(fs);
2,653✔
586
  TSDB_CHECK_CODE(code, lino, _exit);
2,656!
587

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

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

598
  const TFileSetArray *src = fs->fSetArr;
11,553✔
599
  TFileSetArray       *dst = fs->fSetArrTmp;
11,553✔
600

601
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
21,968✔
602

603
  const STFileSet *fset1;
604
  TARRAY2_FOREACH(src, fset1) {
24,067✔
605
    STFileSet *fset2;
606
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
12,514✔
607
    if (code) return code;
12,514!
608
    code = TARRAY2_APPEND(dst, fset2);
12,514✔
609
    if (code) return code;
12,514!
610
  }
611

612
  return 0;
11,553✔
613
}
614

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

620
  char fCurrent[TSDB_FILENAME_LEN];
621
  char cCurrent[TSDB_FILENAME_LEN];
622
  char mCurrent[TSDB_FILENAME_LEN];
623

624
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
9,413✔
625
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
9,408✔
626
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
9,414✔
627

628
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
9,410✔
629
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
2,657✔
630
    TSDB_CHECK_CODE(code, lino, _exit);
2,657!
631

632
    if (taosCheckExistFile(cCurrent)) {
2,657!
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)) {
2,657!
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);
2,657✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
2,655!
655

656
    code = tsdbFSScanAndFix(fs);
2,655✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
2,656!
658
  } else {
659
    code = save_fs(fs->fSetArr, fCurrent);
6,758✔
660
    TSDB_CHECK_CODE(code, lino, _exit);
6,758!
661
  }
662

663
_exit:
6,758✔
664
  if (code) {
9,414!
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__);
9,414✔
668
  }
669
  return code;
9,416✔
670
}
671

672
static void close_file_system(STFileSystem *fs) {
9,416✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
17,318✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
17,286✔
675
}
9,416✔
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) {
8,896✔
687
  int32_t code = 0;
8,896✔
688
  int32_t lino = 0;
8,896✔
689

690
  code = tsdbFSDupState(fs);
8,896✔
691
  if (code) return code;
8,896!
692

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
8,896✔
694
  STFileSet      *fset = NULL;
8,896✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
8,896✔
697
  TSKEY           now = taosGetTimestampMs();
8,896✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
33,849✔
699
    if (!fset || fset->fid != op->fid) {
24,953✔
700
      STFileSet tfset = {.fid = op->fid};
12,982✔
701
      fset = &tfset;
12,982✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
12,982✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
12,982✔
704

705
      if (!fset) {
12,982✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
5,892✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
5,892!
708

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

714
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
24,953✔
715
    TSDB_CHECK_CODE(code, lino, _exit);
24,953!
716

717
    if (fid != op->fid) {
24,953✔
718
      fid = op->fid;
12,982✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
12,982✔
720
        fset->lastCommit = now;
10,543✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
2,439✔
722
        fset->lastCompact = now;
171✔
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
2,268!
724
        fset->lastMigrate = now;
×
725
      } else if (etype == TSDB_FEDIT_ROLLUP) {
2,268✔
726
        fset->lastRollupLevel = fs->rollupLevel;
21✔
727
        fset->lastRollup = now;
21✔
728
        fset->lastCompact = now;  // rollup implies compact
21✔
729
      }
730
    }
731
  }
732

733
  // remove empty empty stt level and empty file set
734
  int32_t i = 0;
8,896✔
735
  while (i < TARRAY2_SIZE(fsetArray)) {
25,287✔
736
    fset = TARRAY2_GET(fsetArray, i);
16,391✔
737

738
    SSttLvl *lvl;
739
    int32_t  j = 0;
16,391✔
740
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
38,055✔
741
      lvl = TARRAY2_GET(fset->lvlArr, j);
21,664✔
742

743
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
21,664✔
744
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
2,616!
745
      } else {
746
        j++;
19,048✔
747
      }
748
    }
749

750
    if (tsdbTFileSetIsEmpty(fset)) {
16,391✔
751
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
118!
752
    } else {
753
      i++;
16,273✔
754
    }
755
  }
756

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

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

769
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
9,386✔
770
  TSDB_CHECK_CODE(code, lino, _exit);
9,414!
771

772
  code = create_fs(pTsdb, fs);
9,414✔
773
  TSDB_CHECK_CODE(code, lino, _exit);
9,414!
774

775
  code = open_fs(fs[0], rollback);
9,414✔
776
  TSDB_CHECK_CODE(code, lino, _exit);
9,416!
777

778
_exit:
9,416✔
779
  if (code) {
9,416!
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__);
9,416✔
784
  }
785
  return code;
9,416✔
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) {
9,513✔
793
  STFileSystem *fs = pTsdb->pFS;
9,513✔
794
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
9,513✔
795
  if (asyncTasks == NULL) {
9,513!
796
    return terrno;
×
797
  }
798

799
  (void)taosThreadMutexLock(&pTsdb->mutex);
9,513✔
800

801
  // disable
802
  pTsdb->bgTaskDisabled = true;
9,514✔
803

804
  // collect channel
805
  STFileSet *fset;
806
  TARRAY2_FOREACH(fs->fSetArr, fset) {
17,420✔
807
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
15,812!
808
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
15,812!
809
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
15,810!
810
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
15,809!
811
      taosArrayDestroy(asyncTasks);
×
812
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
813
      return terrno;
×
814
    }
815
    tsdbFSSetBlockCommit(fset, false);
7,905✔
816
  }
817

818
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
9,514✔
819

820
  // destroy all channels
821
  for (int32_t k = 0; k < 2; k++) {
28,541✔
822
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
82,044✔
823
      SVATaskID *task = taosArrayGet(asyncTasks, i);
63,015✔
824
      if (k == 0) {
63,019✔
825
        (void)vnodeACancel(task);
31,603✔
826
      } else {
827
        vnodeAWait(task);
31,416✔
828
      }
829
    }
830
  }
831
  taosArrayDestroy(asyncTasks);
9,514✔
832

833
#ifdef TD_ENTERPRISE
834
  tsdbStopAllCompTask(pTsdb);
9,514✔
835
#endif
836
  tsdbStopAllRetentionTask(pTsdb);
9,514✔
837
  return 0;
9,514✔
838
}
839

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

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

849
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
9,416✔
850
  if (code) {
9,416!
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]);
9,416✔
855
  destroy_fs(fs);
9,416✔
856
  return;
9,416✔
857
}
858

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

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

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

877
  if (etype == TSDB_FEDIT_COMMIT) {
8,896✔
878
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
6,457✔
879
  } else {
880
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2,439✔
881
  }
882

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

888
  // edit
889
  code = edit_fs(fs, opArray, etype);
8,896✔
890
  TSDB_CHECK_CODE(code, lino, _exit);
8,896!
891

892
  // save fs
893
  code = save_fs(fs->fSetArrTmp, current_t);
8,896✔
894
  TSDB_CHECK_CODE(code, lino, _exit);
8,896!
895

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

906
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
22,177✔
907
  if (block) {
22,177✔
908
    fset->blockCommit = true;
88✔
909
  } else {
910
    fset->blockCommit = false;
22,089✔
911
    if (fset->numWaitCommit > 0) {
22,089✔
912
      (void)taosThreadCondSignal(&fset->canCommit);
53✔
913
    }
914
  }
915
}
22,177✔
916

917
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
10,422✔
918
  (void)taosThreadMutexLock(&tsdb->mutex);
10,422✔
919
  STFileSet *fset;
920
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
10,422✔
921
  bool blockCommit = false;
10,422✔
922
  if (fset) {
10,422✔
923
    blockCommit = fset->blockCommit;
4,688✔
924
  }
925
  if (fset) {
10,422✔
926
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
4,741!
927
      while (fset->blockCommit) {
928
        fset->numWaitCommit++;
929
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
930
        fset->numWaitCommit--;
931
      }
932
    });
933
  }
934
  if (blockCommit) {
10,422✔
935
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
53!
936
  }
937
  (void)taosThreadMutexUnlock(&tsdb->mutex);
10,422✔
938
  return;
10,422✔
939
}
940

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

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

950
  // schedule merge
951
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
8,896✔
952
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
8,896✔
953
    STFileSet *fset;
954
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
22,777✔
955
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
14,272✔
956
        tsdbFSSetBlockCommit(fset, false);
1,296✔
957
        continue;
1,296✔
958
      }
959

960
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
12,976✔
961
      if (lvl->level != 0) {
12,976✔
962
        tsdbFSSetBlockCommit(fset, false);
1,651✔
963
        continue;
1,651✔
964
      }
965

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

975
        arg->tsdb = fs->tsdb;
2,248✔
976
        arg->fid = fset->fid;
2,248✔
977

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

982
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
11,325✔
983
        tsdbFSSetBlockCommit(fset, true);
88✔
984
      } else {
985
        tsdbFSSetBlockCommit(fset, false);
11,237✔
986
      }
987
    }
988
  }
989

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

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

1010
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
30,882✔
1011
  STFileSet   tfset = {.fid = fid};
30,882✔
1012
  STFileSet  *pset = &tfset;
30,882✔
1013
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
30,882✔
1014
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
30,882✔
1015
}
30,882✔
1016

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

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

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

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

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

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

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

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

1064
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
375,728!
1065
  if (fsetArr[0] == NULL) return terrno;
375,900!
1066

1067
  TARRAY2_FOREACH(fs->fSetArr, fset) {
873,608✔
1068
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
497,596✔
1069
    if (code) break;
497,713!
1070

1071
    code = TARRAY2_APPEND(fsetArr[0], fset1);
497,713✔
1072
    if (code) {
497,708!
1073
      tsdbTFileSetClear(&fset1);
×
1074
      break;
×
1075
    }
1076
  }
1077

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

1086
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
375,890✔
1087
  if (fsetArr[0]) {
375,890!
1088
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
873,847!
1089
    taosMemoryFreeClear(fsetArr[0]);
375,905!
1090
    fsetArr[0] = NULL;
375,905✔
1091
  }
1092
}
375,892✔
1093

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1237
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
13,097✔
1238
  if (*fset == NULL) {
13,097✔
1239
    return;
5,734✔
1240
  }
1241

1242
  struct STFileSetCond *cond = NULL;
7,363✔
1243
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
7,363✔
1244
    cond = &(*fset)->conds[0];
4,832✔
1245
  } else {
1246
    cond = &(*fset)->conds[1];
2,531✔
1247
  }
1248

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

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

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

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

1274
  struct STFileSetCond *cond = NULL;
7,363✔
1275
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
7,363✔
1276
    cond = &fset->conds[0];
4,832✔
1277
  } else {
1278
    cond = &fset->conds[1];
2,531✔
1279
  }
1280

1281
  cond->running = false;
7,363✔
1282
  if (cond->numWait > 0) {
7,363✔
1283
    (void)taosThreadCondSignal(&cond->cond);
11✔
1284
  }
1285

1286
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
7,363✔
1287
  return;
7,363✔
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;
20,157!
1439

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

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

1455
  const STFileSet *fset;
1456
  const SSttLvl   *stt = NULL;
13,807✔
1457
  const STFileObj *fObj = NULL;
13,807✔
1458

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

1462
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
19,926✔
1463
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
30,595✔
1464
      getLevelSize(fset->farr[t], levelSize);
24,476✔
1465
    }
1466

1467
    TARRAY2_FOREACH(fset->lvlArr, stt) {
10,302✔
1468
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
8,665✔
1469
    }
1470

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

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

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