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

taosdata / TDengine / #4761

28 Sep 2025 10:49AM UTC coverage: 57.837% (-1.0%) from 58.866%
#4761

push

travis-ci

web-flow
merge: set version (#33122)

136913 of 302095 branches covered (45.32%)

Branch coverage included in aggregate %.

207750 of 293830 relevant lines covered (70.7%)

5673932.16 hits per line

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

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

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

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

58
  return 0;
12,582✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

160
  cJSON *json = cJSON_CreateObject();
23,016✔
161
  if (json == NULL) {
23,014!
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,014!
167
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
168
  }
169

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

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

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

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

408
  return 0;
6,674✔
409
}
410

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

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

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

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

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

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

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

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

485
  return NULL;
×
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,672✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
10,909,597✔
490
    STFileHashEntry *entry = hash->buckets[i];
10,906,924✔
491
    while (entry) {
10,913,599✔
492
      STFileHashEntry *next = entry->next;
6,674✔
493
      taosMemoryFree(entry);
6,674!
494
      entry = next;
6,675✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
2,673!
498
  memset(hash, 0, sizeof(*hash));
2,672✔
499
}
2,672✔
500

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

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

508
  {  // scan each file
509
    STFileSet *fset = NULL;
2,670✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
5,028✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
11,775✔
513
        if (fset->farr[ftype] == NULL) continue;
9,420✔
514
        STFileObj *fobj = fset->farr[ftype];
1,734✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
1,734✔
516
        if (code) {
1,734!
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,556✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,467✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
2,266✔
528
          if (code) {
2,269!
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,673!
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,673✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2,673!
548

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

612
  return 0;
15,806✔
613
}
614

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

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

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

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

663
_exit:
9,897✔
664
  if (code) {
12,584!
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,584✔
668
  }
669
  return code;
12,585✔
670
}
671

672
static void close_file_system(STFileSystem *fs) {
12,585✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
29,142✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
29,104✔
675
}
12,586✔
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,119✔
687
  int32_t code = 0;
13,119✔
688
  int32_t lino = 0;
13,119✔
689

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

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

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

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

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

717
    if (fid != op->fid) {
40,993✔
718
      fid = op->fid;
24,298✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
24,298✔
720
        fset->lastCommit = now;
20,961✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
3,337✔
722
        fset->lastCompact = now;
156✔
723
      } else if (etype == TSDB_FEDIT_SSMIGRATE) {
3,181!
724
        fset->lastMigrate = now;
×
725
      } else if (etype == TSDB_FEDIT_ROLLUP) {
3,181✔
726
        fset->lastRollup = now;
5✔
727
        fset->lastCompact = now;  // rollup implies compact
5✔
728
      }
729
    }
730
  }
731

732
  // remove empty empty stt level and empty file set
733
  int32_t i = 0;
13,119✔
734
  while (i < TARRAY2_SIZE(fsetArray)) {
40,791✔
735
    fset = TARRAY2_GET(fsetArray, i);
27,672✔
736

737
    SSttLvl *lvl;
738
    int32_t  j = 0;
27,672✔
739
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
64,491✔
740
      lvl = TARRAY2_GET(fset->lvlArr, j);
36,819✔
741

742
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
36,819✔
743
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
4,020!
744
      } else {
745
        j++;
32,799✔
746
      }
747
    }
748

749
    if (tsdbTFileSetIsEmpty(fset)) {
27,672✔
750
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
120!
751
    } else {
752
      i++;
27,552✔
753
    }
754
  }
755

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

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

768
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
12,546✔
769
  TSDB_CHECK_CODE(code, lino, _exit);
12,582!
770

771
  code = create_fs(pTsdb, fs);
12,582✔
772
  TSDB_CHECK_CODE(code, lino, _exit);
12,582!
773

774
  code = open_fs(fs[0], rollback);
12,582✔
775
  TSDB_CHECK_CODE(code, lino, _exit);
12,585!
776

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

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

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

798
  (void)taosThreadMutexLock(&pTsdb->mutex);
12,679✔
799

800
  // disable
801
  pTsdb->bgTaskDisabled = true;
12,679✔
802

803
  // collect channel
804
  STFileSet *fset;
805
  TARRAY2_FOREACH(fs->fSetArr, fset) {
29,229✔
806
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
33,104!
807
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
33,104!
808
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL
33,102!
809
        || taosArrayPush(asyncTasks, &fset->migrateTask) == NULL) {
33,099✔
810
      taosArrayDestroy(asyncTasks);
×
811
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
812
      return terrno;
×
813
    }
814
    tsdbFSSetBlockCommit(fset, false);
16,548✔
815
  }
816

817
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
12,676✔
818

819
  // destroy all channels
820
  for (int32_t k = 0; k < 2; k++) {
38,037✔
821
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
157,690✔
822
      SVATaskID *task = taosArrayGet(asyncTasks, i);
132,358✔
823
      if (k == 0) {
132,338✔
824
        (void)vnodeACancel(task);
66,235✔
825
      } else {
826
        vnodeAWait(task);
66,103✔
827
      }
828
    }
829
  }
830
  taosArrayDestroy(asyncTasks);
12,679✔
831

832
#ifdef TD_ENTERPRISE
833
  tsdbStopAllCompTask(pTsdb);
12,678✔
834
  tsdbStopAllRetentionTask(pTsdb);
12,679✔
835
#endif
836
  return 0;
12,679✔
837
}
838

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

845
void tsdbCloseFS(STFileSystem **fs) {
12,585✔
846
  if (fs[0] == NULL) return;
12,585!
847

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

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

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

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

876
  if (etype == TSDB_FEDIT_COMMIT) {
13,119✔
877
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,782✔
878
  } else {
879
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,337✔
880
  }
881

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

887
  // edit
888
  code = edit_fs(fs, opArray, etype);
13,119✔
889
  TSDB_CHECK_CODE(code, lino, _exit);
13,119!
890

891
  // save fs
892
  code = save_fs(fs->fSetArrTmp, current_t);
13,119✔
893
  TSDB_CHECK_CODE(code, lino, _exit);
13,119!
894

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

905
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
40,757✔
906
  if (block) {
40,757✔
907
    fset->blockCommit = true;
95✔
908
  } else {
909
    fset->blockCommit = false;
40,662✔
910
    if (fset->numWaitCommit > 0) {
40,662✔
911
      (void)taosThreadCondSignal(&fset->canCommit);
66✔
912
    }
913
  }
914
}
40,757✔
915

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

943
// IMPORTANT: the caller must hold fs->tsdb->mutex
944
int32_t tsdbFSEditCommit(STFileSystem *fs) {
13,119✔
945
  int32_t code = 0;
13,119✔
946
  int32_t lino = 0;
13,119✔
947

948
  // commit
949
  code = commit_edit(fs);
13,119✔
950
  TSDB_CHECK_CODE(code, lino, _exit);
13,119!
951

952
  // schedule merge
953
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
13,119✔
954
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
13,119✔
955
    STFileSet *fset;
956
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
36,907✔
957
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
24,209✔
958
        tsdbFSSetBlockCommit(fset, false);
790✔
959
        continue;
790✔
960
      }
961

962
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
23,419✔
963
      if (lvl->level != 0) {
23,419✔
964
        tsdbFSSetBlockCommit(fset, false);
2,636✔
965
        continue;
2,636✔
966
      }
967

968
      // bool    skipMerge = false;
969
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
20,783✔
970
      if (numFile >= sttTrigger && (!vnodeATaskValid(&fset->mergeTask))) {
20,783✔
971
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
3,153!
972
        if (arg == NULL) {
3,153!
973
          code = terrno;
×
974
          TSDB_CHECK_CODE(code, lino, _exit);
×
975
        }
976

977
        arg->tsdb = fs->tsdb;
3,153✔
978
        arg->fid = fset->fid;
3,153✔
979

980
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
3,153✔
981
        TSDB_CHECK_CODE(code, lino, _exit);
3,153!
982
      }
983

984
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
20,783✔
985
        tsdbFSSetBlockCommit(fset, true);
95✔
986
      } else {
987
        tsdbFSSetBlockCommit(fset, false);
20,688✔
988
      }
989
    }
990
  }
991

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

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

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

1019
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
82✔
1020
  int32_t    code = 0;
82✔
1021
  STFileSet *fset;
1022
  STFileSet *fset1;
1023

1024
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
82!
1025
  if (fsetArr[0] == NULL) return terrno;
82!
1026

1027
  TARRAY2_INIT(fsetArr[0]);
82✔
1028

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

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

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

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

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

1062
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
1,167,782✔
1063
  int32_t    code = 0;
1,167,782✔
1064
  STFileSet *fset, *fset1;
1065

1066
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
1,167,782!
1067
  if (fsetArr[0] == NULL) return terrno;
1,168,269!
1068

1069
  TARRAY2_FOREACH(fs->fSetArr, fset) {
4,303,160✔
1070
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
3,133,712✔
1071
    if (code) break;
3,134,901!
1072

1073
    code = TARRAY2_APPEND(fsetArr[0], fset1);
3,134,901✔
1074
    if (code) {
3,134,891!
1075
      tsdbTFileSetClear(&fset1);
×
1076
      break;
×
1077
    }
1078
  }
1079

1080
  if (code) {
1,169,448!
1081
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1082
    taosMemoryFree(fsetArr[0]);
×
1083
    fsetArr[0] = NULL;
×
1084
  }
1085
  return code;
1,169,448✔
1086
}
1087

1088
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
1,168,278✔
1089
  if (fsetArr[0]) {
1,168,278!
1090
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
4,308,351!
1091
    taosMemoryFreeClear(fsetArr[0]);
1,168,392!
1092
    fsetArr[0] = NULL;
1,168,390✔
1093
  }
1094
}
1,168,361✔
1095

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

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

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

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

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

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

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

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

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

1164
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
3✔
1165

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

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

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

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

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

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

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

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

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

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

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

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

1233
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
3✔
1234

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

1239
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
24,405✔
1240
  if (*fset == NULL) {
24,405✔
1241
    return;
14,086✔
1242
  }
1243

1244
  struct STFileSetCond *cond = NULL;
10,319✔
1245
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,319✔
1246
    cond = &(*fset)->conds[0];
6,937✔
1247
  } else {
1248
    cond = &(*fset)->conds[1];
3,382✔
1249
  }
1250

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

1262
  tsdbTrace("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
10,319✔
1263
  return;
10,319✔
1264
}
1265

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

1270
  STFileSet *fset = NULL;
10,319✔
1271
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
10,319✔
1272
  if (fset == NULL) {
10,319!
1273
    return;
×
1274
  }
1275

1276
  struct STFileSetCond *cond = NULL;
10,319✔
1277
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
10,319✔
1278
    cond = &fset->conds[0];
6,937✔
1279
  } else {
1280
    cond = &fset->conds[1];
3,382✔
1281
  }
1282

1283
  cond->running = false;
10,319✔
1284
  if (cond->numWait > 0) {
10,319!
1285
    (void)taosThreadCondSignal(&cond->cond);
×
1286
  }
1287

1288
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
10,319✔
1289
  return;
10,319✔
1290
}
1291

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

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

1307
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
×
1308

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

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

1320
  return TSDB_CODE_SUCCESS;
×
1321
}
1322

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

1327
  tsdbTFileSetClear(&pReader->pFileSet);
×
1328

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

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

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

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

1360
  return code;
×
1361
}
1362

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

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

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

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

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

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

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

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

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

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

1424
  return TSDB_CODE_INVALID_PARA;
×
1425
}
1426

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

1432
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
×
1433
  taosMemoryFree(*ppReader);
×
1434

1435
  *ppReader = NULL;
×
1436
  return;
×
1437
}
1438

1439
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1440
  if (fObj == NULL) return;
53,239!
1441

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

1452
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1453
  int32_t code = 0;
17,136✔
1454
  int64_t levelSize[TFS_MAX_TIERS] = {0};
17,136✔
1455
  int64_t ssSize = 0;
17,136✔
1456

1457
  const STFileSet *fset;
1458
  const SSttLvl   *stt = NULL;
17,136✔
1459
  const STFileObj *fObj = NULL;
17,136✔
1460

1461
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
17,136✔
1462
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->ssChunkSize;
17,136✔
1463

1464
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
32,427✔
1465
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
76,455✔
1466
      getLevelSize(fset->farr[t], levelSize);
61,164✔
1467
    }
1468

1469
    TARRAY2_FOREACH(fset->lvlArr, stt) {
25,534✔
1470
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
20,637✔
1471
    }
1472

1473
    fObj = fset->farr[TSDB_FTYPE_DATA];
15,291✔
1474
    if (fObj) {
15,291✔
1475
      int32_t lcn = fObj->f->lcn;
5,997✔
1476
      if (lcn > 1) {
5,997!
1477
        ssSize += ((lcn - 1) * chunksize);
×
1478
      }
1479
    }
1480
  }
1481

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

1491
  (void)taosThreadMutexLock(&tsdb->mutex);
17,136✔
1492
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
17,136✔
1493
  (void)taosThreadMutexUnlock(&tsdb->mutex);
17,136✔
1494
  return code;
17,136✔
1495
}
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