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

taosdata / TDengine / #4754

25 Sep 2025 05:58AM UTC coverage: 57.946% (-1.0%) from 58.977%
#4754

push

travis-ci

web-flow
enh: taos command line support '-uroot' on windows (#33055)

133189 of 293169 branches covered (45.43%)

Branch coverage included in aggregate %.

201677 of 284720 relevant lines covered (70.83%)

5398749.0 hits per line

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

61.25
/source/dnode/vnode/src/tsdb/tsdbFS2.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "cos.h"
17
#include "tsdbFS2.h"
18
#include "tsdbUpgrade.h"
19
#include "vnd.h"
20

21
#define BLOCK_COMMIT_FACTOR 3
22

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

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

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

40
static int32_t create_fs(STsdb *pTsdb, STFileSystem **fs) {
12,188✔
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
12,188!
42
  if (fs[0] == NULL) {
12,256!
43
    return terrno;
×
44
  }
45

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

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

58
  return 0;
12,257✔
59
}
60

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

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

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

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

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

88
  data = cJSON_PrintUnformatted(json);
22,781✔
89
  if (data == NULL) {
22,782!
90
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
91
  }
92

93
  fp = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
22,782✔
94
  if (fp == NULL) {
22,781!
95
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
96
  }
97

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

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

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

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

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

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

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

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

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

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

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

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

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

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
22,783✔
172
  if (!ajson) {
22,784!
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
50,420✔
177
    cJSON *item = cJSON_CreateObject();
27,636✔
178
    if (!item) {
27,636!
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
27,636✔
182

183
    code = tsdbTFileSetToJson(fset, item);
27,636✔
184
    TSDB_CHECK_CODE(code, lino, _exit);
27,636!
185
  }
186

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

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

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

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

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

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

212
  /* fmtv */
213
  item1 = cJSON_GetObjectItem(json, "fmtv");
2,616✔
214
  if (cJSON_IsNumber(item1)) {
2,612!
215
    if (item1->valuedouble != 1) {
2,613!
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,613✔
224
  if (cJSON_IsArray(item1)) {
2,615!
225
    const cJSON *item2;
226
    cJSON_ArrayForEach(item2, item1) {
5,007!
227
      STFileSet *fset;
228
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
2,392✔
229
      TSDB_CHECK_CODE(code, lino, _exit);
2,392!
230

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

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

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
40,900✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
27,760✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
27,760✔
260

261
    if (fset1 && fset2) {
27,760✔
262
      if (fset1->fid < fset2->fid) {
13,738✔
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
120✔
265
        i1++;
120✔
266
      } else if (fset1->fid > fset2->fid) {
13,618✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
22✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
22!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
22✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
22!
272
        i1++;
22✔
273
        i2++;
22✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
13,596✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
13,596!
278
        i1++;
13,596✔
279
        i2++;
13,596✔
280
      }
281
    } else if (fset1) {
14,022✔
282
      // delete fset1
283
      tsdbTFileSetRemove(fset1);
4✔
284
      i1++;
4✔
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
14,018✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
14,018!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
14,018✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
14,018!
291
      i1++;
14,018✔
292
      i2++;
14,018✔
293
    }
294
  }
295

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

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

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
13,140✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
13,140✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,766✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,374✔
312
  }
313

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

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

321
_exit:
13,140✔
322
  if (code) {
13,140!
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);
13,140!
327
  }
328
  return code;
13,140✔
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,372✔
365
  int32_t code = 0;
4,372✔
366
  int32_t lino = 0;
4,372✔
367

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

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

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

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

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

408
  return 0;
6,981✔
409
}
410

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

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

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

429
  // other
430
  STFileSet *fset = NULL;
2,600✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
4,984✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
11,916✔
434
      if (fset->farr[i] != NULL) {
9,532✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
2,098✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
2,098!
437

438
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
2,098!
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,384✔
457
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
4,471✔
458
      STFileObj *fobj;
459
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,370✔
460
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
2,283✔
461
        TSDB_CHECK_CODE(code, lino, _exit);
2,283!
462
      }
463
    }
464
  }
465

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

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

477
  STFileHashEntry *entry = hash->buckets[idx];
6,979✔
478
  while (entry) {
7,025✔
479
    if (strcmp(entry->fname, fname) == 0) {
7,022✔
480
      return entry;
6,976✔
481
    }
482
    entry = entry->next;
46✔
483
  }
484

485
  return NULL;
3✔
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,600✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
10,614,171✔
490
    STFileHashEntry *entry = hash->buckets[i];
10,611,570✔
491
    while (entry) {
10,618,551✔
492
      STFileHashEntry *next = entry->next;
6,980✔
493
      taosMemoryFree(entry);
6,980!
494
      entry = next;
6,981✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
2,601!
498
  memset(hash, 0, sizeof(*hash));
2,600✔
499
}
2,600✔
500

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

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

508
  {  // scan each file
509
    STFileSet *fset = NULL;
2,595✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
4,978✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
11,899✔
513
        if (fset->farr[ftype] == NULL) continue;
9,518✔
514
        STFileObj *fobj = fset->farr[ftype];
2,095✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
2,095✔
516
        if (code) {
2,097!
517
          fset->maxVerValid = (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
518
          corrupt = true;
×
519
        }
520
      }
521

522
      // stt file
523
      SSttLvl *lvl;
524
      TARRAY2_FOREACH(fset->lvlArr, lvl) {
4,468✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,367✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
2,280✔
528
          if (code) {
2,282!
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,599!
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,599✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2,599!
548

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

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

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

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

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

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

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

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

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

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

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

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

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

601
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
29,379✔
602

603
  const STFileSet *fset1;
604
  TARRAY2_FOREACH(src, fset1) {
31,863✔
605
    STFileSet *fset2;
606
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
16,111✔
607
    if (code) return code;
16,109!
608
    code = TARRAY2_APPEND(dst, fset2);
16,109✔
609
    if (code) return code;
16,108!
610
  }
611

612
  return 0;
15,752✔
613
}
614

615
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
12,256✔
616
  int32_t code = 0;
12,256✔
617
  int32_t lino = 0;
12,256✔
618
  STsdb  *pTsdb = fs->tsdb;
12,256✔
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);
12,256✔
625
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
12,252✔
626
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
12,257✔
627

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

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

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

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

672
static void close_file_system(STFileSystem *fs) {
12,261✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
28,678✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
28,662✔
675
}
12,259✔
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) {
13,140✔
687
  int32_t code = 0;
13,140✔
688
  int32_t lino = 0;
13,140✔
689

690
  code = tsdbFSDupState(fs);
13,140✔
691
  if (code) return code;
13,140!
692

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
13,140✔
694
  STFileSet      *fset = NULL;
13,140✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
13,140✔
697
  TSKEY           now = taosGetTimestampMs();
13,140✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
54,149✔
699
    if (!fset || fset->fid != op->fid) {
41,009✔
700
      STFileSet tfset = {.fid = op->fid};
24,159✔
701
      fset = &tfset;
24,159✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
24,159✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
24,159✔
704

705
      if (!fset) {
24,159✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
14,040✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
14,040!
708

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

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

717
    if (fid != op->fid) {
41,009✔
718
      fid = op->fid;
24,159✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
24,159✔
720
        fset->lastCommit = now;
20,785✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
3,374✔
722
        fset->lastCompact = now;
178✔
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
3,196!
724
        fset->lastMigrate = now;
×
725
      }
726
    }
727
  }
728

729
  // remove empty empty stt level and empty file set
730
  int32_t i = 0;
13,140✔
731
  while (i < TARRAY2_SIZE(fsetArray)) {
40,900✔
732
    fset = TARRAY2_GET(fsetArray, i);
27,760✔
733

734
    SSttLvl *lvl;
735
    int32_t  j = 0;
27,760✔
736
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
64,246✔
737
      lvl = TARRAY2_GET(fset->lvlArr, j);
36,486✔
738

739
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
36,486✔
740
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
4,046!
741
      } else {
742
        j++;
32,440✔
743
      }
744
    }
745

746
    if (tsdbTFileSetIsEmpty(fset)) {
27,760✔
747
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
124!
748
    } else {
749
      i++;
27,636✔
750
    }
751
  }
752

753
_exit:
13,140✔
754
  if (code) {
13,140!
755
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
756
  }
757
  return code;
13,140✔
758
}
759

760
// return error code
761
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
12,230✔
762
  int32_t code;
763
  int32_t lino;
764

765
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
12,230✔
766
  TSDB_CHECK_CODE(code, lino, _exit);
12,261!
767

768
  code = create_fs(pTsdb, fs);
12,261✔
769
  TSDB_CHECK_CODE(code, lino, _exit);
12,259!
770

771
  code = open_fs(fs[0], rollback);
12,259✔
772
  TSDB_CHECK_CODE(code, lino, _exit);
12,261!
773

774
_exit:
12,261✔
775
  if (code) {
12,261!
776
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
777
    destroy_fs(fs);
×
778
  } else {
779
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
12,261✔
780
  }
781
  return code;
12,261✔
782
}
783

784
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block);
785
extern void tsdbStopAllCompTask(STsdb *tsdb);
786

787
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
12,358✔
788
  STFileSystem *fs = pTsdb->pFS;
12,358✔
789
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
12,358✔
790
  if (asyncTasks == NULL) {
12,358!
791
    return terrno;
×
792
  }
793

794
  (void)taosThreadMutexLock(&pTsdb->mutex);
12,358✔
795

796
  // disable
797
  pTsdb->bgTaskDisabled = true;
12,359✔
798

799
  // collect channel
800
  STFileSet *fset;
801
  TARRAY2_FOREACH(fs->fSetArr, fset) {
28,731✔
802
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
32,748!
803
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
32,747!
804
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL) {
32,741!
805
      taosArrayDestroy(asyncTasks);
×
806
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
807
      return terrno;
×
808
    }
809
    tsdbFSSetBlockCommit(fset, false);
16,371✔
810
  }
811

812
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
12,359✔
813

814
  // destroy all channels
815
  for (int32_t k = 0; k < 2; k++) {
37,075✔
816
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
122,121✔
817
      SVATaskID *task = taosArrayGet(asyncTasks, i);
97,396✔
818
      if (k == 0) {
97,404✔
819
        (void)vnodeACancel(task);
48,916✔
820
      } else {
821
        vnodeAWait(task);
48,488✔
822
      }
823
    }
824
  }
825
  taosArrayDestroy(asyncTasks);
12,358✔
826

827
#ifdef TD_ENTERPRISE
828
  tsdbStopAllCompTask(pTsdb);
12,358✔
829
#endif
830
  return 0;
12,359✔
831
}
832

833
void tsdbEnableBgTask(STsdb *pTsdb) {
98✔
834
  (void)taosThreadMutexLock(&pTsdb->mutex);
98✔
835
  pTsdb->bgTaskDisabled = false;
98✔
836
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
98✔
837
}
98✔
838

839
void tsdbCloseFS(STFileSystem **fs) {
12,260✔
840
  if (fs[0] == NULL) return;
12,260!
841

842
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
12,260✔
843
  if (code) {
12,261!
844
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID((*fs)->tsdb->pVnode), __func__, __LINE__,
×
845
              tstrerror(code));
846
  }
847
  close_file_system(fs[0]);
12,261✔
848
  destroy_fs(fs);
12,261✔
849
  return;
12,261✔
850
}
851

852
int64_t tsdbFSAllocEid(STFileSystem *fs) {
13,344✔
853
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
13,344✔
854
  int64_t cid = ++fs->neid;
13,344✔
855
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
13,344✔
856
  return cid;
13,344✔
857
}
858

859
void tsdbFSUpdateEid(STFileSystem *fs, int64_t cid) {
566✔
860
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
566✔
861
  fs->neid = TMAX(fs->neid, cid);
566✔
862
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
566✔
863
}
566✔
864

865
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
13,140✔
866
  int32_t code = 0;
13,140✔
867
  int32_t lino;
868
  char    current_t[TSDB_FILENAME_LEN];
869

870
  if (etype == TSDB_FEDIT_COMMIT) {
13,140✔
871
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,766✔
872
  } else {
873
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,374✔
874
  }
875

876
  if (tsem_wait(&fs->canEdit) != 0) {
13,140!
877
    tsdbError("vgId:%d failed to wait semaphore", TD_VID(fs->tsdb->pVnode));
×
878
  }
879
  fs->etype = etype;
13,140✔
880

881
  // edit
882
  code = edit_fs(fs, opArray, etype);
13,140✔
883
  TSDB_CHECK_CODE(code, lino, _exit);
13,140!
884

885
  // save fs
886
  code = save_fs(fs->fSetArrTmp, current_t);
13,140✔
887
  TSDB_CHECK_CODE(code, lino, _exit);
13,140!
888

889
_exit:
13,140✔
890
  if (code) {
13,140!
891
    tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
×
892
              tstrerror(code), etype);
893
  } else {
894
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
13,140!
895
  }
896
  return code;
13,140✔
897
}
898

899
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
40,641✔
900
  if (block) {
40,641✔
901
    fset->blockCommit = true;
93✔
902
  } else {
903
    fset->blockCommit = false;
40,548✔
904
    if (fset->numWaitCommit > 0) {
40,548✔
905
      (void)taosThreadCondSignal(&fset->canCommit);
53✔
906
    }
907
  }
908
}
40,641✔
909

910
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
20,677✔
911
  (void)taosThreadMutexLock(&tsdb->mutex);
20,677✔
912
  STFileSet *fset;
913
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
20,677✔
914
  bool blockCommit = false;
20,677✔
915
  if (fset) {
20,677✔
916
    blockCommit = fset->blockCommit;
6,782✔
917
  }
918
  if (fset) {
20,677✔
919
    METRICS_TIMING_BLOCK(tsdb->pVnode->writeMetrics.block_commit_time, METRIC_LEVEL_HIGH, {
6,835!
920
      while (fset->blockCommit) {
921
        fset->numWaitCommit++;
922
        (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
923
        fset->numWaitCommit--;
924
      }
925
    });
926
  }
927
  if (blockCommit) {
20,677✔
928
    METRICS_UPDATE(tsdb->pVnode->writeMetrics.blocked_commit_count, METRIC_LEVEL_HIGH, 1);
53!
929
  }
930
  (void)taosThreadMutexUnlock(&tsdb->mutex);
20,677✔
931
  return;
20,677✔
932
}
933

934
// IMPORTANT: the caller must hold fs->tsdb->mutex
935
int32_t tsdbFSEditCommit(STFileSystem *fs) {
13,140✔
936
  int32_t code = 0;
13,140✔
937
  int32_t lino = 0;
13,140✔
938

939
  // commit
940
  code = commit_edit(fs);
13,140✔
941
  TSDB_CHECK_CODE(code, lino, _exit);
13,140!
942

943
  // schedule merge
944
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
13,140✔
945
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
13,140✔
946
    STFileSet *fset;
947
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
36,978✔
948
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
24,270✔
949
        tsdbFSSetBlockCommit(fset, false);
1,161✔
950
        continue;
1,161✔
951
      }
952

953
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
23,109✔
954
      if (lvl->level != 0) {
23,109✔
955
        tsdbFSSetBlockCommit(fset, false);
2,633✔
956
        continue;
2,633✔
957
      }
958

959
      // bool    skipMerge = false;
960
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
20,476✔
961
      if (numFile >= sttTrigger && (!vnodeATaskValid(&fset->mergeTask))) {
20,476✔
962
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
3,199!
963
        if (arg == NULL) {
3,199!
964
          code = terrno;
×
965
          TSDB_CHECK_CODE(code, lino, _exit);
×
966
        }
967

968
        arg->tsdb = fs->tsdb;
3,199✔
969
        arg->fid = fset->fid;
3,199✔
970

971
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
3,199✔
972
        TSDB_CHECK_CODE(code, lino, _exit);
3,199!
973
      }
974

975
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
20,476✔
976
        tsdbFSSetBlockCommit(fset, true);
93✔
977
      } else {
978
        tsdbFSSetBlockCommit(fset, false);
20,383✔
979
      }
980
    }
981
  }
982

983
_exit:
13,140✔
984
  if (code) {
13,140!
985
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
×
986
  } else {
987
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
13,140!
988
  }
989
  if (tsem_post(&fs->canEdit) != 0) {
13,140!
990
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
991
  }
992
  return code;
13,140✔
993
}
994

995
int32_t tsdbFSEditAbort(STFileSystem *fs) {
×
996
  int32_t code = abort_edit(fs);
×
997
  if (tsem_post(&fs->canEdit) != 0) {
×
998
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
999
  }
1000
  return code;
×
1001
}
1002

1003
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
55,300✔
1004
  STFileSet   tfset = {.fid = fid};
55,300✔
1005
  STFileSet  *pset = &tfset;
55,300✔
1006
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
55,300✔
1007
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
55,300✔
1008
}
55,300✔
1009

1010
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
86✔
1011
  int32_t    code = 0;
86✔
1012
  STFileSet *fset;
1013
  STFileSet *fset1;
1014

1015
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
86!
1016
  if (fsetArr[0] == NULL) return terrno;
86!
1017

1018
  TARRAY2_INIT(fsetArr[0]);
86✔
1019

1020
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
86✔
1021
  TARRAY2_FOREACH(fs->fSetArr, fset) {
86!
1022
    code = tsdbTFileSetInitCopy(fs->tsdb, fset, &fset1);
×
1023
    if (code) break;
×
1024

1025
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1026
    if (code) break;
×
1027
  }
1028
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
86✔
1029

1030
  if (code) {
86!
1031
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1032
    taosMemoryFree(fsetArr[0]);
×
1033
    fsetArr[0] = NULL;
×
1034
  }
1035
  return code;
86✔
1036
}
1037

1038
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
89✔
1039
  if (fsetArr[0]) {
89!
1040
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
92!
1041
    taosMemoryFree(fsetArr[0]);
89!
1042
    fsetArr[0] = NULL;
89✔
1043
  }
1044
}
89✔
1045

1046
int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
93✔
1047
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
93✔
1048
  int32_t code = tsdbFSCreateRefSnapshotWithoutLock(fs, fsetArr);
93✔
1049
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
93✔
1050
  return code;
93✔
1051
}
1052

1053
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
1,111,579✔
1054
  int32_t    code = 0;
1,111,579✔
1055
  STFileSet *fset, *fset1;
1056

1057
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
1,111,579!
1058
  if (fsetArr[0] == NULL) return terrno;
1,112,170!
1059

1060
  TARRAY2_FOREACH(fs->fSetArr, fset) {
3,761,444✔
1061
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
2,648,187✔
1062
    if (code) break;
2,649,281!
1063

1064
    code = TARRAY2_APPEND(fsetArr[0], fset1);
2,649,281✔
1065
    if (code) {
2,649,274!
1066
      tsdbTFileSetClear(&fset1);
×
1067
      break;
×
1068
    }
1069
  }
1070

1071
  if (code) {
1,113,257!
1072
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1073
    taosMemoryFree(fsetArr[0]);
×
1074
    fsetArr[0] = NULL;
×
1075
  }
1076
  return code;
1,113,257✔
1077
}
1078

1079
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
1,112,144✔
1080
  if (fsetArr[0]) {
1,112,144!
1081
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
3,764,880!
1082
    taosMemoryFreeClear(fsetArr[0]);
1,112,250!
1083
    fsetArr[0] = NULL;
1,112,259✔
1084
  }
1085
}
1,112,228✔
1086

1087
static SHashObj *tsdbFSetRangeArrayToHash(TFileSetRangeArray *pRanges) {
6✔
1088
  int32_t   capacity = TARRAY2_SIZE(pRanges) * 2;
6✔
1089
  SHashObj *pHash = taosHashInit(capacity, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
6✔
1090
  if (pHash == NULL) {
6!
1091
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1092
    return NULL;
×
1093
  }
1094

1095
  for (int32_t i = 0; i < TARRAY2_SIZE(pRanges); i++) {
12✔
1096
    STFileSetRange *u = TARRAY2_GET(pRanges, i);
6✔
1097
    int32_t         fid = u->fid;
6✔
1098
    int32_t         code = taosHashPut(pHash, &fid, sizeof(fid), u, sizeof(*u));
6✔
1099
    tsdbDebug("range diff hash fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
6!
1100
  }
1101
  return pHash;
6✔
1102
}
1103

1104
int32_t tsdbFSCreateCopyRangedSnapshot(STFileSystem *fs, TFileSetRangeArray *pRanges, TFileSetArray **fsetArr,
3✔
1105
                                       TFileOpArray *fopArr) {
1106
  int32_t    code = 0;
3✔
1107
  STFileSet *fset;
1108
  STFileSet *fset1;
1109
  SHashObj  *pHash = NULL;
3✔
1110

1111
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
3!
1112
  if (fsetArr == NULL) return terrno;
3!
1113
  TARRAY2_INIT(fsetArr[0]);
3✔
1114

1115
  if (pRanges) {
3!
1116
    pHash = tsdbFSetRangeArrayToHash(pRanges);
3✔
1117
    if (pHash == NULL) {
3!
1118
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1119
      goto _out;
×
1120
    }
1121
  }
1122

1123
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
3✔
1124
  TARRAY2_FOREACH(fs->fSetArr, fset) {
6✔
1125
    int64_t ever = VERSION_MAX;
3✔
1126
    if (pHash) {
3!
1127
      int32_t         fid = fset->fid;
3✔
1128
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
3✔
1129
      if (u) {
3!
1130
        ever = u->sver - 1;
3✔
1131
      }
1132
    }
1133

1134
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
3✔
1135
    if (code) break;
3!
1136

1137
    code = TARRAY2_APPEND(fsetArr[0], fset1);
3!
1138
    if (code) break;
3!
1139
  }
1140
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
3✔
1141

1142
_out:
3✔
1143
  if (code) {
3!
1144
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1145
    taosMemoryFree(fsetArr[0]);
×
1146
    fsetArr[0] = NULL;
×
1147
  }
1148
  if (pHash) {
3!
1149
    taosHashCleanup(pHash);
3✔
1150
    pHash = NULL;
3✔
1151
  }
1152
  return code;
3✔
1153
}
1154

1155
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
3✔
1156

1157
int32_t tsdbFSCreateRefRangedSnapshot(STFileSystem *fs, int64_t sver, int64_t ever, TFileSetRangeArray *pRanges,
3✔
1158
                                      TFileSetRangeArray **fsrArr) {
1159
  int32_t         code = 0;
3✔
1160
  STFileSet      *fset;
1161
  STFileSetRange *fsr1 = NULL;
3✔
1162
  SHashObj       *pHash = NULL;
3✔
1163

1164
  fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
3!
1165
  if (fsrArr[0] == NULL) {
3!
1166
    code = terrno;
×
1167
    goto _out;
×
1168
  }
1169

1170
  tsdbInfo("pRanges size:%d", (pRanges == NULL ? 0 : TARRAY2_SIZE(pRanges)));
3!
1171
  if (pRanges) {
3!
1172
    pHash = tsdbFSetRangeArrayToHash(pRanges);
3✔
1173
    if (pHash == NULL) {
3!
1174
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1175
      goto _out;
×
1176
    }
1177
  }
1178

1179
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
3✔
1180
  TARRAY2_FOREACH(fs->fSetArr, fset) {
6✔
1181
    int64_t sver1 = sver;
3✔
1182
    int64_t ever1 = ever;
3✔
1183

1184
    if (pHash) {
3!
1185
      int32_t         fid = fset->fid;
3✔
1186
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
3✔
1187
      if (u) {
3!
1188
        sver1 = u->sver;
3✔
1189
        tsdbDebug("range hash get fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
3!
1190
      }
1191
    }
1192

1193
    if (sver1 > ever1) {
3!
1194
      tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
×
1195
      continue;
×
1196
    }
1197

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

1200
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
3✔
1201
    if (code) break;
3!
1202

1203
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
3!
1204
    if (code) break;
3!
1205

1206
    fsr1 = NULL;
3✔
1207
  }
1208
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
3✔
1209

1210
  if (code) {
3!
1211
    tsdbTFileSetRangeClear(&fsr1);
×
1212
    TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
×
1213
    fsrArr[0] = NULL;
×
1214
  }
1215

1216
_out:
3✔
1217
  if (pHash) {
3!
1218
    taosHashCleanup(pHash);
3✔
1219
    pHash = NULL;
3✔
1220
  }
1221
  return code;
3✔
1222
}
1223

1224
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
3✔
1225

1226
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
24,259✔
1227
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1228
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
24,259✔
1229

1230
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
24,259✔
1231
  if (*fset == NULL) {
24,259✔
1232
    return;
13,895✔
1233
  }
1234

1235
  struct STFileSetCond *cond = NULL;
10,364✔
1236
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,364✔
1237
    cond = &(*fset)->conds[0];
6,890✔
1238
  } else {
1239
    cond = &(*fset)->conds[1];
3,474✔
1240
  }
1241

1242
  while (1) {
1243
    if (cond->running) {
10,374✔
1244
      cond->numWait++;
10✔
1245
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
10✔
1246
      cond->numWait--;
10✔
1247
    } else {
1248
      cond->running = true;
10,364✔
1249
      break;
10,364✔
1250
    }
1251
  }
1252

1253
  tsdbTrace("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
10,364✔
1254
  return;
10,364✔
1255
}
1256

1257
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
10,364✔
1258
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1259
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
10,364✔
1260

1261
  STFileSet *fset = NULL;
10,364✔
1262
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
10,364✔
1263
  if (fset == NULL) {
10,364!
1264
    return;
×
1265
  }
1266

1267
  struct STFileSetCond *cond = NULL;
10,364✔
1268
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,364✔
1269
    cond = &fset->conds[0];
6,890✔
1270
  } else {
1271
    cond = &fset->conds[1];
3,474✔
1272
  }
1273

1274
  cond->running = false;
10,364✔
1275
  if (cond->numWait > 0) {
10,364✔
1276
    (void)taosThreadCondSignal(&cond->cond);
10✔
1277
  }
1278

1279
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
10,364✔
1280
  return;
10,364✔
1281
}
1282

1283
struct SFileSetReader {
1284
  STsdb     *pTsdb;
1285
  STFileSet *pFileSet;
1286
  int32_t    fid;
1287
  int64_t    startTime;
1288
  int64_t    endTime;
1289
  int64_t    lastCompactTime;
1290
  int64_t    totalSize;
1291
};
1292

1293
int32_t tsdbFileSetReaderOpen(void *pVnode, struct SFileSetReader **ppReader) {
×
1294
  if (pVnode == NULL || ppReader == NULL) {
×
1295
    return TSDB_CODE_INVALID_PARA;
×
1296
  }
1297

1298
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
×
1299

1300
  (*ppReader) = taosMemoryCalloc(1, sizeof(struct SFileSetReader));
×
1301
  if (*ppReader == NULL) {
×
1302
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, __LINE__,
×
1303
              tstrerror(terrno));
1304
    return terrno;
×
1305
  }
1306

1307
  (*ppReader)->pTsdb = pTsdb;
×
1308
  (*ppReader)->fid = INT32_MIN;
×
1309
  (*ppReader)->pFileSet = NULL;
×
1310

1311
  return TSDB_CODE_SUCCESS;
×
1312
}
1313

1314
extern bool tsdbShouldCompact(const STFileSet *pFileSet, int32_t vgId);
1315

1316
#ifndef TD_ENTERPRISE
1317
bool tsdbShouldCompact(const STFileSet *pFileSet, int32_t vgId) { return false; }
1318
#endif
1319

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

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

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

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

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

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

1357
  return code;
×
1358
}
1359

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

1368
int32_t tsdbFileSetGetEntryField(struct SFileSetReader *pReader, const char *field, void *value) {
×
1369
  const char *fieldName;
1370

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

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

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

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

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

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

1405
  fieldName = "should_compact";
×
1406
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1407
    *(char *)value = tsdbShouldCompact(pReader->pFileSet, pReader->pTsdb->pVnode->config.vgId);
×
1408
    return TSDB_CODE_SUCCESS;
×
1409
  }
1410

1411
  fieldName = "details";
×
1412
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1413
    // TODO
1414
    return TSDB_CODE_SUCCESS;
×
1415
  }
1416

1417
  return TSDB_CODE_INVALID_PARA;
×
1418
}
1419

1420
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
×
1421
  if (ppReader == NULL || *ppReader == NULL) {
×
1422
    return;
×
1423
  }
1424

1425
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
×
1426
  taosMemoryFree(*ppReader);
×
1427

1428
  *ppReader = NULL;
×
1429
  return;
×
1430
}
1431

1432
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1433
  if (fObj == NULL) return;
44,515!
1434

1435
  int64_t sz = fObj->f->size;
27,927✔
1436
  // level == 0, primary storage
1437
  // level == 1, second storage,
1438
  // level == 2, third storage
1439
  int32_t level = fObj->f->did.level;
27,927✔
1440
  if (level >= 0 && level < TFS_MAX_TIERS) {
27,927!
1441
    szArr[level] += sz;
27,927✔
1442
  }
1443
}
1444

1445
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1446
  int32_t code = 0;
16,379✔
1447
  int64_t levelSize[TFS_MAX_TIERS] = {0};
16,379✔
1448
  int64_t ssSize = 0;
16,379✔
1449

1450
  const STFileSet *fset;
1451
  const SSttLvl   *stt = NULL;
16,379✔
1452
  const STFileObj *fObj = NULL;
16,379✔
1453

1454
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
16,379✔
1455
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
16,379✔
1456

1457
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
30,144✔
1458
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
68,825✔
1459
      getLevelSize(fset->farr[t], levelSize);
55,060✔
1460
    }
1461

1462
    TARRAY2_FOREACH(fset->lvlArr, stt) {
22,164✔
1463
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
17,090✔
1464
    }
1465

1466
    fObj = fset->farr[TSDB_FTYPE_DATA];
13,765✔
1467
    if (fObj) {
13,765✔
1468
      int32_t lcn = fObj->f->lcn;
6,305✔
1469
      if (lcn > 1) {
6,305!
1470
        ssSize += ((lcn - 1) * chunksize);
×
1471
      }
1472
    }
1473
  }
1474

1475
  pInfo->l1Size = levelSize[0];
16,379✔
1476
  pInfo->l2Size = levelSize[1];
16,379✔
1477
  pInfo->l3Size = levelSize[2];
16,379✔
1478
  pInfo->ssSize = ssSize;
16,379✔
1479
  return code;
16,379✔
1480
}
1481
int32_t tsdbGetFsSize(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
16,379✔
1482
  int32_t code = 0;
16,379✔
1483

1484
  (void)taosThreadMutexLock(&tsdb->mutex);
16,379✔
1485
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
16,379✔
1486
  (void)taosThreadMutexUnlock(&tsdb->mutex);
16,379✔
1487
  return code;
16,379✔
1488
}
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