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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

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

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

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

58
  return 0;
14,592✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

170
  // fset
171
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
23,046✔
172
  if (!ajson) {
23,046!
173
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
174
  }
175
  const STFileSet *fset;
176
  TARRAY2_FOREACH(arr, fset) {
368,898✔
177
    cJSON *item = cJSON_CreateObject();
345,855✔
178
    if (!item) {
345,860!
179
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
180
    }
181
    (void)cJSON_AddItemToArray(ajson, item);
345,860✔
182

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

187
  code = save_json(json, fname);
23,043✔
188
  TSDB_CHECK_CODE(code, lino, _exit);
23,051!
189

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

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

202
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
3,339!
203

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

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

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

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

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

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
356,727✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
344,945✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
344,945!
260

261
    if (fset1 && fset2) {
344,945!
262
      if (fset1->fid < fset2->fid) {
10,266!
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
×
265
        i1++;
×
266
      } else if (fset1->fid > fset2->fid) {
10,266✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
29✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
29!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
29✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
29!
272
        i1++;
29✔
273
        i2++;
29✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
10,237✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
10,237!
278
        i1++;
10,237✔
279
        i2++;
10,237✔
280
      }
281
    } else if (fset1) {
334,679!
282
      // delete fset1
283
      tsdbTFileSetRemove(fset1);
×
284
      i1++;
×
285
    } else {
286
      // create new file set with fid of fset2->fid
287
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
334,679✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
335,372!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
334,675✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
334,675!
291
      i1++;
334,675✔
292
      i2++;
334,675✔
293
    }
294
  }
295

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

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

307
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
11,786✔
308
  if (fs->etype == TSDB_FEDIT_COMMIT) {
11,786✔
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,161✔
310
  } else {
311
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2,625✔
312
  }
313

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

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

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

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

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

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

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

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

408
  return 0;
6,062✔
409
}
410

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

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

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

429
  // other
430
  STFileSet *fset = NULL;
3,344✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
5,365✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
10,101✔
434
      if (fset->farr[i] != NULL) {
8,080✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
598✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
598!
437

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

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

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

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

485
  return NULL;
×
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
3,344✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
13,564,104✔
490
    STFileHashEntry *entry = hash->buckets[i];
13,560,760✔
491
    while (entry) {
13,566,822✔
492
      STFileHashEntry *next = entry->next;
6,062✔
493
      taosMemoryFree(entry);
6,062!
494
      entry = next;
6,062✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
3,344!
498
  memset(hash, 0, sizeof(*hash));
3,344✔
499
}
3,344✔
500

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

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

508
  {  // scan each file
509
    STFileSet *fset = NULL;
3,335✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
5,351✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
10,061✔
513
        if (fset->farr[ftype] == NULL) continue;
8,049✔
514
        STFileObj *fobj = fset->farr[ftype];
598✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
598✔
516
        if (code) {
598!
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,045✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,146✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
2,113✔
528
          if (code) {
2,117!
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) {
3,339!
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;
3,339✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
3,339!
548

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

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

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

561
    tsdbFSDestroyFileObjHash(&fobjHash);
3,341✔
562

563
  _close_dir:
3,343✔
564
    tfsClosedir(dir);
3,343✔
565
  }
566

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

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

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

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

585
  code = tsdbFSDoSanAndFix(fs);
3,340✔
586
  TSDB_CHECK_CODE(code, lino, _exit);
3,342!
587

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

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

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

601
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
25,364✔
602

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

612
  return 0;
15,127✔
613
}
614

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

628
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
14,605✔
629
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
3,343✔
630
    TSDB_CHECK_CODE(code, lino, _exit);
3,342!
631

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

656
    code = tsdbFSScanAndFix(fs);
3,337✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
3,342!
658
  } else {
659
    code = save_fs(fs->fSetArr, fCurrent);
11,264✔
660
    TSDB_CHECK_CODE(code, lino, _exit);
11,265!
661
  }
662

663
_exit:
11,265✔
664
  if (code) {
14,607!
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__);
14,607✔
668
  }
669
  return code;
14,609✔
670
}
671

672
static void close_file_system(STFileSystem *fs) {
14,609✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
352,247✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
352,254✔
675
}
14,607✔
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) {
11,786✔
687
  int32_t code = 0;
11,786✔
688
  int32_t lino = 0;
11,786✔
689

690
  code = tsdbFSDupState(fs);
11,786✔
691
  if (code) return code;
11,786!
692

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
11,786✔
694
  STFileSet      *fset = NULL;
11,786✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
11,786✔
697
  TSKEY           now = taosGetTimestampMs();
11,786✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
367,010✔
699
    if (!fset || fset->fid != op->fid) {
355,225✔
700
      STFileSet tfset = {.fid = op->fid};
343,526✔
701
      fset = &tfset;
343,526✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
343,398✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
343,398✔
704

705
      if (!fset) {
343,398✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
335,480✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
335,411!
708

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

714
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
354,977✔
715
    TSDB_CHECK_CODE(code, lino, _exit);
355,224!
716

717
    if (fid != op->fid) {
355,224✔
718
      fid = op->fid;
343,526✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
343,526✔
720
        fset->lastCommit = now;
340,901✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
2,625✔
722
        fset->lastCompact = now;
24✔
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
2,601!
724
        fset->lastMigrate = now;
×
725
      }
726
    }
727
  }
728

729
  // remove empty empty stt level and empty file set
730
  int32_t i = 0;
11,785✔
731
  while (i < TARRAY2_SIZE(fsetArray)) {
357,653✔
732
    fset = TARRAY2_GET(fsetArray, i);
345,868✔
733

734
    SSttLvl *lvl;
735
    int32_t  j = 0;
345,868✔
736
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
701,086✔
737
      lvl = TARRAY2_GET(fset->lvlArr, j);
355,218✔
738

739
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
355,218✔
740
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
3,716!
741
      } else {
742
        j++;
351,502✔
743
      }
744
    }
745

746
    if (tsdbTFileSetIsEmpty(fset)) {
345,868!
747
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
×
748
    } else {
749
      i++;
345,868✔
750
    }
751
  }
752

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

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

765
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
14,561✔
766
  TSDB_CHECK_CODE(code, lino, _exit);
14,602!
767

768
  code = create_fs(pTsdb, fs);
14,602✔
769
  TSDB_CHECK_CODE(code, lino, _exit);
14,596!
770

771
  code = open_fs(fs[0], rollback);
14,596✔
772
  TSDB_CHECK_CODE(code, lino, _exit);
14,609!
773

774
_exit:
14,609✔
775
  if (code) {
14,609!
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__);
14,609✔
780
  }
781
  return code;
14,609✔
782
}
783

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

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

794
  (void)taosThreadMutexLock(&pTsdb->mutex);
14,662✔
795

796
  // disable
797
  pTsdb->bgTaskDisabled = true;
14,662✔
798

799
  // collect channel
800
  STFileSet *fset;
801
  TARRAY2_FOREACH(fs->fSetArr, fset) {
352,292✔
802
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
675,257!
803
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
675,258!
804
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL) {
675,255!
805
      taosArrayDestroy(asyncTasks);
×
806
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
807
      return terrno;
×
808
    }
809
    tsdbFSSetBlockCommit(fset, false);
337,627✔
810
  }
811

812
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
14,662✔
813

814
  // destroy all channels
815
  for (int32_t k = 0; k < 2; k++) {
43,986✔
816
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
2,054,810✔
817
      SVATaskID *task = taosArrayGet(asyncTasks, i);
2,025,476✔
818
      if (k == 0) {
2,025,486✔
819
        (void)vnodeACancel(task);
1,012,862✔
820
      } else {
821
        vnodeAWait(task);
1,012,624✔
822
      }
823
    }
824
  }
825
  taosArrayDestroy(asyncTasks);
14,662✔
826

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

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

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

842
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
14,609✔
843
  if (code) {
14,609!
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]);
14,609✔
848
  destroy_fs(fs);
14,609✔
849
  return;
14,609✔
850
}
851

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

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

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

870
  if (etype == TSDB_FEDIT_COMMIT) {
11,786✔
871
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,161✔
872
  } else {
873
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2,625✔
874
  }
875

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

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

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

889
_exit:
11,786✔
890
  if (code) {
11,786!
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);
11,786!
895
  }
896
  return code;
11,786✔
897
}
898

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

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

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

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

943
  // schedule merge
944
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
11,786✔
945
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
11,786✔
946
    STFileSet *fset;
947
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
350,302✔
948
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
339,357✔
949
        tsdbFSSetBlockCommit(fset, false);
566✔
950
        continue;
566✔
951
      }
952

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

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

968
        arg->tsdb = fs->tsdb;
2,608✔
969
        arg->fid = fset->fid;
2,608✔
970

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

975
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
335,947!
976
        tsdbFSSetBlockCommit(fset, true);
×
977
      } else {
978
        tsdbFSSetBlockCommit(fset, false);
335,947✔
979
      }
980
    }
981
  }
982

983
_exit:
11,786✔
984
  if (code) {
11,786!
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);
11,786!
988
  }
989
  if (tsem_post(&fs->canEdit) != 0) {
11,786!
990
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
991
  }
992
  return code;
11,786✔
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) {
930,042✔
1004
  STFileSet   tfset = {.fid = fid};
930,042✔
1005
  STFileSet  *pset = &tfset;
930,042✔
1006
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
930,042✔
1007
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
930,209✔
1008
}
930,209✔
1009

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

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

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

1020
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
49✔
1021
  TARRAY2_FOREACH(fs->fSetArr, fset) {
49!
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);
49✔
1029

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

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

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

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

1057
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
1,333,779!
1058
  if (fsetArr[0] == NULL) return terrno;
1,334,323!
1059

1060
  TARRAY2_FOREACH(fs->fSetArr, fset) {
5,058,481✔
1061
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
3,722,342✔
1062
    if (code) break;
3,724,145!
1063

1064
    code = TARRAY2_APPEND(fsetArr[0], fset1);
3,724,145✔
1065
    if (code) {
3,724,158!
1066
      tsdbTFileSetClear(&fset1);
×
1067
      break;
×
1068
    }
1069
  }
1070

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

1079
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
1,333,960✔
1080
  if (fsetArr[0]) {
1,333,960!
1081
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
5,065,721!
1082
    taosMemoryFreeClear(fsetArr[0]);
1,334,268!
1083
    fsetArr[0] = NULL;
1,334,434✔
1084
  }
1085
}
1,334,241✔
1086

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1193
    if (sver1 > ever1) {
×
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);
×
1199

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

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

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

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

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

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

1226
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
462,391✔
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;
462,391✔
1229

1230
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
462,391✔
1231
  if (*fset == NULL) {
462,439✔
1232
    return;
454,426✔
1233
  }
1234

1235
  struct STFileSetCond *cond = NULL;
8,013✔
1236
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
8,013✔
1237
    cond = &(*fset)->conds[0];
5,299✔
1238
  } else {
1239
    cond = &(*fset)->conds[1];
2,714✔
1240
  }
1241

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

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

1257
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
8,013✔
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;
8,013✔
1260

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

1267
  struct STFileSetCond *cond = NULL;
8,013✔
1268
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
8,013✔
1269
    cond = &fset->conds[0];
5,299✔
1270
  } else {
1271
    cond = &fset->conds[1];
2,714✔
1272
  }
1273

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

1279
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
8,013✔
1280
  return;
8,013✔
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) {
1✔
1294
  if (pVnode == NULL || ppReader == NULL) {
1!
1295
    return TSDB_CODE_INVALID_PARA;
×
1296
  }
1297

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

1300
  (*ppReader) = taosMemoryCalloc(1, sizeof(struct SFileSetReader));
1!
1301
  if (*ppReader == NULL) {
1!
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;
1✔
1308
  (*ppReader)->fid = INT32_MIN;
1✔
1309
  (*ppReader)->pFileSet = NULL;
1✔
1310

1311
  return TSDB_CODE_SUCCESS;
1✔
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) {
2✔
1321
  STsdb  *pTsdb = pReader->pTsdb;
2✔
1322
  int32_t code = TSDB_CODE_SUCCESS;
2✔
1323

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

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

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

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

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

1357
  return code;
1✔
1358
}
1359

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

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

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

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

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

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

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

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

1405
  fieldName = "should_compact";
1✔
1406
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
1!
1407
    *(char *)value = tsdbShouldCompact(pReader->pFileSet, pReader->pTsdb->pVnode->config.vgId);
1✔
1408
    return TSDB_CODE_SUCCESS;
1✔
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) {
1✔
1421
  if (ppReader == NULL || *ppReader == NULL) {
1!
1422
    return;
×
1423
  }
1424

1425
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
1✔
1426
  taosMemoryFree(*ppReader);
1!
1427

1428
  *ppReader = NULL;
1✔
1429
  return;
1✔
1430
}
1431

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

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

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

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

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

1457
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
27,536✔
1458
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
48,805✔
1459
      getLevelSize(fset->farr[t], levelSize);
39,044✔
1460
    }
1461

1462
    TARRAY2_FOREACH(fset->lvlArr, stt) {
15,884✔
1463
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
12,452✔
1464
    }
1465

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

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

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