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

taosdata / TDengine / #4488

12 Jul 2025 07:47AM UTC coverage: 62.207% (-0.7%) from 62.948%
#4488

push

travis-ci

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

157961 of 324087 branches covered (48.74%)

Branch coverage included in aggregate %.

244465 of 322830 relevant lines covered (75.73%)

6561668.76 hits per line

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

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

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

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

58
  return 0;
14,567✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

257
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
357,451✔
258
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
345,602✔
259
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
345,602!
260

261
    if (fset1 && fset2) {
345,602!
262
      if (fset1->fid < fset2->fid) {
10,621!
263
        // delete fset1
264
        tsdbTFileSetRemove(fset1);
×
265
        i1++;
×
266
      } else if (fset1->fid > fset2->fid) {
10,621✔
267
        // create new file set with fid of fset2->fid
268
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
30✔
269
        TSDB_CHECK_CODE(code, lino, _exit);
30!
270
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
30✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
30!
272
        i1++;
30✔
273
        i2++;
30✔
274
      } else {
275
        // edit
276
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
10,591✔
277
        TSDB_CHECK_CODE(code, lino, _exit);
10,591!
278
        i1++;
10,591✔
279
        i2++;
10,591✔
280
      }
281
    } else if (fset1) {
334,981!
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,981✔
288
      TSDB_CHECK_CODE(code, lino, _exit);
335,000!
289
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
334,960✔
290
      TSDB_CHECK_CODE(code, lino, _exit);
334,960!
291
      i1++;
334,960✔
292
      i2++;
334,960✔
293
    }
294
  }
295

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

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

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

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

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

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

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

372
    if (tsS3Enabled && 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,557✔
392
}
393

394
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
395

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

400
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
5,865✔
401

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

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

408
  return 0;
5,865✔
409
}
410

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

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

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

429
  // other
430
  STFileSet *fset = NULL;
3,308✔
431
  TARRAY2_FOREACH(fs->fSetArr, fset) {
5,305✔
432
    // data file
433
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
9,990✔
434
      if (fset->farr[i] != NULL) {
7,992✔
435
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
497✔
436
        TSDB_CHECK_CODE(code, lino, _exit);
497!
437

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

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

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

477
  STFileHashEntry *entry = hash->buckets[idx];
5,868✔
478
  while (entry) {
5,907✔
479
    if (strcmp(entry->fname, fname) == 0) {
5,906✔
480
      return entry;
5,867✔
481
    }
482
    entry = entry->next;
39✔
483
  }
484

485
  return NULL;
1✔
486
}
487

488
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
3,309✔
489
  for (int32_t i = 0; i < hash->numBucket; i++) {
13,370,520✔
490
    STFileHashEntry *entry = hash->buckets[i];
13,367,211✔
491
    while (entry) {
13,373,076✔
492
      STFileHashEntry *next = entry->next;
5,865✔
493
      taosMemoryFree(entry);
5,865!
494
      entry = next;
5,865✔
495
    }
496
  }
497
  taosMemoryFree(hash->buckets);
3,309!
498
  memset(hash, 0, sizeof(*hash));
3,309✔
499
}
3,309✔
500

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

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

508
  {  // scan each file
509
    STFileSet *fset = NULL;
3,301✔
510
    TARRAY2_FOREACH(fs->fSetArr, fset) {
5,300✔
511
      // data file
512
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
9,976✔
513
        if (fset->farr[ftype] == NULL) continue;
7,979✔
514
        STFileObj *fobj = fset->farr[ftype];
496✔
515
        code = tsdbFSDoScanAndFixFile(fs, fobj);
496✔
516
        if (code) {
497!
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,057✔
525
        STFileObj *fobj;
526
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4,118✔
527
          code = tsdbFSDoScanAndFixFile(fs, fobj);
2,058✔
528
          if (code) {
2,060!
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,304!
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,304✔
547
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
3,304!
548

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

612
  return 0;
15,172✔
613
}
614

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

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

632
    if (taosCheckExistFile(cCurrent)) {
3,303!
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,307!
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,306✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
3,305!
655

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

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

672
static void close_file_system(STFileSystem *fs) {
14,576✔
673
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
351,582✔
674
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
351,594✔
675
}
14,570✔
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,870✔
687
  int32_t code = 0;
11,870✔
688
  int32_t lino = 0;
11,870✔
689

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

693
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
11,870✔
694
  STFileSet      *fset = NULL;
11,870✔
695
  const STFileOp *op;
696
  int32_t         fid = INT32_MIN;
11,870✔
697
  TSKEY           now = taosGetTimestampMs();
11,870✔
698
  TARRAY2_FOREACH_PTR(opArray, op) {
366,637✔
699
    if (!fset || fset->fid != op->fid) {
354,770✔
700
      STFileSet tfset = {.fid = op->fid};
342,935✔
701
      fset = &tfset;
342,935✔
702
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
342,891✔
703
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
342,891✔
704

705
      if (!fset) {
342,891✔
706
        code = tsdbTFileSetInit(op->fid, &fset);
334,871✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
334,579!
708

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

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

717
    if (fid != op->fid) {
354,767✔
718
      fid = op->fid;
342,932✔
719
      if (etype == TSDB_FEDIT_COMMIT) {
342,932✔
720
        fset->lastCommit = now;
340,264✔
721
      } else if (etype == TSDB_FEDIT_COMPACT) {
2,668✔
722
        fset->lastCompact = now;
26✔
723
      }
724
    }
725
  }
726

727
  // remove empty empty stt level and empty file set
728
  int32_t i = 0;
11,867✔
729
  while (i < TARRAY2_SIZE(fsetArray)) {
357,490✔
730
    fset = TARRAY2_GET(fsetArray, i);
345,623✔
731

732
    SSttLvl *lvl;
733
    int32_t  j = 0;
345,623✔
734
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
700,626✔
735
      lvl = TARRAY2_GET(fset->lvlArr, j);
355,003✔
736

737
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
355,003✔
738
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
3,756!
739
      } else {
740
        j++;
351,247✔
741
      }
742
    }
743

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

751
_exit:
11,867✔
752
  if (code) {
11,867!
753
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
754
  }
755
  return code;
11,870✔
756
}
757

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

763
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
14,503✔
764
  TSDB_CHECK_CODE(code, lino, _exit);
14,568!
765

766
  code = create_fs(pTsdb, fs);
14,568✔
767
  TSDB_CHECK_CODE(code, lino, _exit);
14,570!
768

769
  code = open_fs(fs[0], rollback);
14,570✔
770
  TSDB_CHECK_CODE(code, lino, _exit);
14,576!
771

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

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

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

792
  (void)taosThreadMutexLock(&pTsdb->mutex);
14,629✔
793

794
  // disable
795
  pTsdb->bgTaskDisabled = true;
14,629✔
796

797
  // collect channel
798
  STFileSet *fset;
799
  TARRAY2_FOREACH(fs->fSetArr, fset) {
351,604✔
800
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
673,954!
801
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
673,954!
802
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL) {
673,946!
803
      taosArrayDestroy(asyncTasks);
×
804
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
805
      return terrno;
×
806
    }
807
    tsdbFSSetBlockCommit(fset, false);
336,974✔
808
  }
809

810
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
14,627✔
811

812
  // destroy all channels
813
  for (int32_t k = 0; k < 2; k++) {
43,886✔
814
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
2,051,128✔
815
      SVATaskID *task = taosArrayGet(asyncTasks, i);
2,021,863✔
816
      if (k == 0) {
2,021,871✔
817
        (void)vnodeACancel(task);
1,010,962✔
818
      } else {
819
        vnodeAWait(task);
1,010,909✔
820
      }
821
    }
822
  }
823
  taosArrayDestroy(asyncTasks);
14,628✔
824

825
#ifdef TD_ENTERPRISE
826
  tsdbStopAllCompTask(pTsdb);
14,628✔
827
#endif
828
  return 0;
14,629✔
829
}
830

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

837
void tsdbCloseFS(STFileSystem **fs) {
14,575✔
838
  if (fs[0] == NULL) return;
14,575!
839

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

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

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

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

868
  if (etype == TSDB_FEDIT_COMMIT) {
11,870✔
869
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,202✔
870
  } else {
871
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2,668✔
872
  }
873

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

879
  // edit
880
  code = edit_fs(fs, opArray, etype);
11,870✔
881
  TSDB_CHECK_CODE(code, lino, _exit);
11,870!
882

883
  // save fs
884
  code = save_fs(fs->fSetArrTmp, current_t);
11,870✔
885
  TSDB_CHECK_CODE(code, lino, _exit);
11,870!
886

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

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

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

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

937
  // commit
938
  code = commit_edit(fs);
11,870✔
939
  TSDB_CHECK_CODE(code, lino, _exit);
11,870!
940

941
  // schedule merge
942
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
11,870✔
943
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
11,870✔
944
    STFileSet *fset;
945
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
350,242✔
946
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
339,205✔
947
        tsdbFSSetBlockCommit(fset, false);
622✔
948
        continue;
622✔
949
      }
950

951
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
338,583✔
952
      if (lvl->level != 0) {
338,583✔
953
        tsdbFSSetBlockCommit(fset, false);
2,866✔
954
        continue;
2,866✔
955
      }
956

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

966
        arg->tsdb = fs->tsdb;
2,642✔
967
        arg->fid = fset->fid;
2,642✔
968

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

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

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

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

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

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

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

1016
  TARRAY2_INIT(fsetArr[0]);
49✔
1017

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

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

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

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

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

1051
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
1,306,983✔
1052
  int32_t    code = 0;
1,306,983✔
1053
  STFileSet *fset, *fset1;
1054

1055
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
1,306,983!
1056
  if (fsetArr[0] == NULL) return terrno;
1,307,710!
1057

1058
  TARRAY2_FOREACH(fs->fSetArr, fset) {
4,355,959✔
1059
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
3,046,858✔
1060
    if (code) break;
3,048,257!
1061

1062
    code = TARRAY2_APPEND(fsetArr[0], fset1);
3,048,257✔
1063
    if (code) {
3,048,249!
1064
      tsdbTFileSetClear(&fset1);
×
1065
      break;
×
1066
    }
1067
  }
1068

1069
  if (code) {
1,309,101!
1070
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1071
    taosMemoryFree(fsetArr[0]);
×
1072
    fsetArr[0] = NULL;
×
1073
  }
1074
  return code;
1,309,101✔
1075
}
1076

1077
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
1,307,341✔
1078
  if (fsetArr[0]) {
1,307,341!
1079
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
4,357,395!
1080
    taosMemoryFreeClear(fsetArr[0]);
1,307,465!
1081
    fsetArr[0] = NULL;
1,307,601✔
1082
  }
1083
}
1,307,411✔
1084

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

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

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

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

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

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

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

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

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

1153
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
×
1154

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

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

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

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

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

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

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

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

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

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

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

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

1222
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
×
1223

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

1228
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
461,995✔
1229
  if (*fset == NULL) {
462,003✔
1230
    return;
453,912✔
1231
  }
1232

1233
  struct STFileSetCond *cond = NULL;
8,091✔
1234
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
8,091✔
1235
    cond = &(*fset)->conds[0];
5,358✔
1236
  } else {
1237
    cond = &(*fset)->conds[1];
2,733✔
1238
  }
1239

1240
  while (1) {
1241
    if (cond->running) {
8,090!
1242
      cond->numWait++;
×
1243
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
×
1244
      cond->numWait--;
×
1245
    } else {
1246
      cond->running = true;
8,090✔
1247
      break;
8,090✔
1248
    }
1249
  }
1250

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

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

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

1265
  struct STFileSetCond *cond = NULL;
8,090✔
1266
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
8,090✔
1267
    cond = &fset->conds[0];
5,357✔
1268
  } else {
1269
    cond = &fset->conds[1];
2,733✔
1270
  }
1271

1272
  cond->running = false;
8,090✔
1273
  if (cond->numWait > 0) {
8,090!
1274
    (void)taosThreadCondSignal(&cond->cond);
×
1275
  }
1276

1277
  tsdbTrace("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
8,090✔
1278
  return;
8,090✔
1279
}
1280

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

1291
int32_t tsdbFileSetReaderOpen(void *pVnode, struct SFileSetReader **ppReader) {
1✔
1292
  if (pVnode == NULL || ppReader == NULL) {
1!
1293
    return TSDB_CODE_INVALID_PARA;
×
1294
  }
1295

1296
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
1✔
1297

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

1305
  (*ppReader)->pTsdb = pTsdb;
1✔
1306
  (*ppReader)->fid = INT32_MIN;
1✔
1307
  (*ppReader)->pFileSet = NULL;
1✔
1308

1309
  return TSDB_CODE_SUCCESS;
1✔
1310
}
1311

1312
extern bool tsdbShouldCompact(const STFileSet *pFileSet, int32_t vgId);
1313

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

1318
static int32_t tsdbFileSetReaderNextNoLock(struct SFileSetReader *pReader) {
2✔
1319
  STsdb  *pTsdb = pReader->pTsdb;
2✔
1320
  int32_t code = TSDB_CODE_SUCCESS;
2✔
1321

1322
  tsdbTFileSetClear(&pReader->pFileSet);
2✔
1323

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

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

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

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

1355
  return code;
1✔
1356
}
1357

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

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

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

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

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

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

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

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

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

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

1415
  return TSDB_CODE_INVALID_PARA;
×
1416
}
1417

1418
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
1✔
1419
  if (ppReader == NULL || *ppReader == NULL) {
1!
1420
    return;
×
1421
  }
1422

1423
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
1✔
1424
  taosMemoryFree(*ppReader);
1!
1425

1426
  *ppReader = NULL;
1✔
1427
  return;
1✔
1428
}
1429

1430
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1431
  if (fObj == NULL) return;
27,533!
1432

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

1443
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1444
  int32_t code = 0;
17,687✔
1445
  int64_t levelSize[TFS_MAX_TIERS] = {0};
17,687✔
1446
  int64_t s3Size = 0;
17,687✔
1447

1448
  const STFileSet *fset;
1449
  const SSttLvl   *stt = NULL;
17,687✔
1450
  const STFileObj *fObj = NULL;
17,687✔
1451

1452
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
17,687✔
1453
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
17,687✔
1454

1455
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
26,417✔
1456
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
43,650✔
1457
      getLevelSize(fset->farr[t], levelSize);
34,920✔
1458
    }
1459

1460
    TARRAY2_FOREACH(fset->lvlArr, stt) {
13,830✔
1461
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
10,336✔
1462
    }
1463

1464
    fObj = fset->farr[TSDB_FTYPE_DATA];
8,730✔
1465
    if (fObj) {
8,730✔
1466
      int32_t lcn = fObj->f->lcn;
4,157✔
1467
      if (lcn > 1) {
4,157!
1468
        s3Size += ((lcn - 1) * chunksize);
×
1469
      }
1470
    }
1471
  }
1472

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

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