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

taosdata / TDengine / #3567

26 Dec 2024 02:00AM UTC coverage: 62.733% (+0.3%) from 62.422%
#3567

push

travis-ci

web-flow
Merge pull request #29290 from taosdata/enh/TD-33262-3.0

enh: test coverage of tfs

138620 of 284159 branches covered (48.78%)

Branch coverage included in aggregate %.

20 of 25 new or added lines in 3 files covered. (80.0%)

606 existing lines in 128 files now uncovered.

216140 of 281347 relevant lines covered (76.82%)

18804943.25 hits per line

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

52.0
/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 "tsdbFS2.h"
17
#include "cos.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) {
11,475✔
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
11,475!
42
  if (fs[0] == NULL) {
11,564!
43
    return terrno;
×
44
  }
45

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

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

58
  return 0;
11,563✔
59
}
60

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

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

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

76
  vnodeGetPrimaryDir(pTsdb->path, pTsdb->pVnode->diskPrimary, pTsdb->pVnode->pTfs, fname, TSDB_FILENAME_LEN);
81,178✔
77
  offset = strlen(fname);
81,207✔
78
  snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, gCurrentFname[ftype]);
81,207✔
79
}
81,207✔
80

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

87
  data = cJSON_PrintUnformatted(json);
23,944✔
88
  if (data == NULL) {
23,952!
89
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
90
  }
91

92
  fp = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
23,952✔
93
  if (fp == NULL) {
23,944!
94
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
95
  }
96

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

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

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

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

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

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

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

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

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

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

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

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

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

169
  // fset
170
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
23,953✔
171
  if (!ajson) {
23,948!
172
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
173
  }
174
  const STFileSet *fset;
175
  TARRAY2_FOREACH(arr, fset) {
181,160✔
176
    cJSON *item = cJSON_CreateObject();
157,212✔
177
    if (!item) {
157,231!
178
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
179
    }
180
    (void)cJSON_AddItemToArray(ajson, item);
157,231✔
181

182
    code = tsdbTFileSetToJson(fset, item);
157,195✔
183
    TSDB_CHECK_CODE(code, lino, _exit);
157,212!
184
  }
185

186
  code = save_json(json, fname);
23,948✔
187
  TSDB_CHECK_CODE(code, lino, _exit);
23,954!
188

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

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

201
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
2,335!
202

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

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

211
  /* fmtv */
212
  item1 = cJSON_GetObjectItem(json, "fmtv");
2,339✔
213
  if (cJSON_IsNumber(item1)) {
2,340!
214
    if (item1->valuedouble != 1) {
2,332!
215
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
216
    }
217
  } else {
218
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
219
  }
220

221
  /* fset */
222
  item1 = cJSON_GetObjectItem(json, "fset");
2,332✔
223
  if (cJSON_IsArray(item1)) {
2,340!
224
    const cJSON *item2;
225
    cJSON_ArrayForEach(item2, item1) {
3,943!
226
      STFileSet *fset;
227
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
1,602✔
228
      TSDB_CHECK_CODE(code, lino, _exit);
1,603!
229

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

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

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

256
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
171,562✔
257
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
156,924✔
258
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
156,924!
259

260
    if (fset1 && fset2) {
156,924!
261
      if (fset1->fid < fset2->fid) {
28,480!
262
        // delete fset1
263
        tsdbTFileSetRemove(fset1);
×
264
        i1++;
×
265
      } else if (fset1->fid > fset2->fid) {
28,480✔
266
        // create new file set with fid of fset2->fid
267
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
26✔
268
        TSDB_CHECK_CODE(code, lino, _exit);
26!
269
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
26✔
270
        TSDB_CHECK_CODE(code, lino, _exit);
26!
271
        i1++;
26✔
272
        i2++;
26✔
273
      } else {
274
        // edit
275
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
28,454✔
276
        TSDB_CHECK_CODE(code, lino, _exit);
28,453!
277
        i1++;
28,453✔
278
        i2++;
28,453✔
279
      }
280
    } else if (fset1) {
128,444!
281
      // delete fset1
282
      tsdbTFileSetRemove(fset1);
×
283
      i1++;
×
284
    } else {
285
      // create new file set with fid of fset2->fid
286
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
128,444✔
287
      TSDB_CHECK_CODE(code, lino, _exit);
128,688!
288
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
128,360✔
289
      TSDB_CHECK_CODE(code, lino, _exit);
128,360!
290
      i1++;
128,360✔
291
      i2++;
128,360✔
292
    }
293
  }
294

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

302
static int32_t commit_edit(STFileSystem *fs) {
14,723✔
303
  char current[TSDB_FILENAME_LEN];
304
  char current_t[TSDB_FILENAME_LEN];
305

306
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
14,723✔
307
  if (fs->etype == TSDB_FEDIT_COMMIT) {
14,723✔
308
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,808✔
309
  } else {
310
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
4,915✔
311
  }
312

313
  int32_t code;
314
  int32_t lino;
315
  TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit);
14,723!
316

317
  code = apply_commit(fs);
14,723✔
318
  TSDB_CHECK_CODE(code, lino, _exit);
14,722!
319

320
_exit:
14,722✔
321
  if (code) {
14,722!
322
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
323
              tstrerror(code));
324
  } else {
325
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
14,722!
326
  }
327
  return code;
14,723✔
328
}
329

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

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

337
  if (fs->etype == TSDB_FEDIT_COMMIT) {
×
338
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
×
339
  } else {
340
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
×
341
  }
342

343
  int32_t code;
344
  int32_t lino;
345
  if ((code = taosRemoveFile(fname))) {
×
346
    code = TAOS_SYSTEM_ERROR(code);
×
347
    TSDB_CHECK_CODE(code, lino, _exit);
×
348
  }
349

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

353
_exit:
×
354
  if (code) {
×
355
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
356
              tstrerror(code));
357
  } else {
358
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
×
359
  }
360
  return code;
×
361
}
362

363
static int32_t tsdbFSDoScanAndFixFile(STFileSystem *fs, const STFileObj *fobj) {
2,496✔
364
  int32_t code = 0;
2,496✔
365
  int32_t lino = 0;
2,496✔
366

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

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

380
      found = true;
×
381
    }
382

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

390
  return 0;
2,498✔
391
}
392

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

399
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
4,839✔
400

401
  uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
4,839✔
402

403
  entry->next = hash->buckets[idx];
4,838✔
404
  hash->buckets[idx] = entry;
4,838✔
405
  hash->numFile++;
4,838✔
406

407
  return 0;
4,838✔
408
}
409

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

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

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

428
  // other
429
  STFileSet *fset = NULL;
2,341✔
430
  TARRAY2_FOREACH(fs->fSetArr, fset) {
3,946✔
431
    // data file
432
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
8,025✔
433
      if (fset->farr[i] != NULL) {
6,420✔
434
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
902✔
435
        TSDB_CHECK_CODE(code, lino, _exit);
902!
436

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

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

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

454
    // stt file
455
    SSttLvl *lvl = NULL;
1,605✔
456
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
3,197✔
457
      STFileObj *fobj;
458
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,188✔
459
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
1,596✔
460
        TSDB_CHECK_CODE(code, lino, _exit);
1,596!
461
      }
462
    }
463
  }
464

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

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

476
  STFileHashEntry *entry = hash->buckets[idx];
4,839✔
477
  while (entry) {
4,839✔
478
    if (strcmp(entry->fname, fname) == 0) {
4,836!
479
      return entry;
4,836✔
480
    }
UNCOV
481
    entry = entry->next;
×
482
  }
483

484
  return NULL;
3✔
485
}
486

487
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,341✔
488
  for (int32_t i = 0; i < hash->numBucket; i++) {
9,443,987✔
489
    STFileHashEntry *entry = hash->buckets[i];
9,441,646✔
490
    while (entry) {
9,446,484✔
491
      STFileHashEntry *next = entry->next;
4,838✔
492
      taosMemoryFree(entry);
4,838!
493
      entry = next;
4,838✔
494
    }
495
  }
496
  taosMemoryFree(hash->buckets);
2,341!
497
  memset(hash, 0, sizeof(*hash));
2,341✔
498
}
2,341✔
499

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

505
  {  // scan each file
506
    STFileSet *fset = NULL;
2,332✔
507
    TARRAY2_FOREACH(fs->fSetArr, fset) {
3,936✔
508
      // data file
509
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
8,018✔
510
        if (fset->farr[ftype] == NULL) continue;
6,414✔
511
        STFileObj *fobj = fset->farr[ftype];
902✔
512
        code = tsdbFSDoScanAndFixFile(fs, fobj);
902✔
513
        if (code) {
902!
514
          fset->maxVerValid = (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
515
          corrupt = true;
×
516
        }
517
      }
518

519
      // stt file
520
      SSttLvl *lvl;
521
      TARRAY2_FOREACH(fset->lvlArr, lvl) {
3,196✔
522
        STFileObj *fobj;
523
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,188✔
524
          code = tsdbFSDoScanAndFixFile(fs, fobj);
1,596✔
525
          if (code) {
1,596!
526
            fset->maxVerValid =
×
527
                (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
528
            corrupt = true;
×
529
          }
530
        }
531
      }
532
    }
533
  }
534

535
  if (corrupt) {
2,332!
536
    tsdbError("vgId:%d, not to clear dangling files due to fset incompleteness", TD_VID(fs->tsdb->pVnode));
×
537
    fs->fsstate = TSDB_FS_STATE_INCOMPLETE;
×
538
    code = 0;
×
539
    goto _exit;
×
540
  }
541

542
  {  // clear unreferenced files
543
    STfsDir *dir = NULL;
2,332✔
544
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2,332!
545

546
    STFileHash fobjHash = {0};
2,335✔
547
    code = tsdbFSCreateFileObjHash(fs, &fobjHash);
2,335✔
548
    if (code) goto _close_dir;
2,341!
549

550
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
9,520✔
551
      if (taosIsDir(file->aname)) continue;
7,174✔
552

553
      if (tsdbFSGetFileObjHashEntry(&fobjHash, file->aname) == NULL) {
4,837!
554
        tsdbRemoveFile(file->aname);
×
555
      }
556
    }
557

558
    tsdbFSDestroyFileObjHash(&fobjHash);
2,338✔
559

560
  _close_dir:
2,341✔
561
    tfsClosedir(dir);
2,341✔
562
  }
563

564
_exit:
2,341✔
565
  if (code) {
2,341!
566
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
567
  }
568
  return code;
2,341✔
569
}
570

571
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
2,335✔
572
  fs->neid = 0;
2,335✔
573

574
  // get max commit id
575
  const STFileSet *fset;
576
  TARRAY2_FOREACH(fs->fSetArr, fset) { fs->neid = TMAX(fs->neid, tsdbTFileSetMaxCid(fset)); }
3,938✔
577

578
  // scan and fix
579
  int32_t code = 0;
2,334✔
580
  int32_t lino = 0;
2,334✔
581

582
  code = tsdbFSDoSanAndFix(fs);
2,334✔
583
  TSDB_CHECK_CODE(code, lino, _exit);
2,341!
584

585
_exit:
2,341✔
586
  if (code) {
2,341!
587
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
588
  }
589
  return code;
2,340✔
590
}
591

592
static int32_t tsdbFSDupState(STFileSystem *fs) {
17,062✔
593
  int32_t code;
594

595
  const TFileSetArray *src = fs->fSetArr;
17,062✔
596
  TFileSetArray       *dst = fs->fSetArrTmp;
17,062✔
597

598
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
45,516✔
599

600
  const STFileSet *fset1;
601
  TARRAY2_FOREACH(src, fset1) {
47,120✔
602
    STFileSet *fset2;
603
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
30,057✔
604
    if (code) return code;
30,057!
605
    code = TARRAY2_APPEND(dst, fset2);
30,057✔
606
    if (code) return code;
30,058!
607
  }
608

609
  return 0;
17,063✔
610
}
611

612
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
11,556✔
613
  int32_t code = 0;
11,556✔
614
  int32_t lino = 0;
11,556✔
615
  STsdb  *pTsdb = fs->tsdb;
11,556✔
616

617
  char fCurrent[TSDB_FILENAME_LEN];
618
  char cCurrent[TSDB_FILENAME_LEN];
619
  char mCurrent[TSDB_FILENAME_LEN];
620

621
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
11,556✔
622
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
11,555✔
623
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
11,565✔
624

625
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
11,569✔
626
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
2,339✔
627
    TSDB_CHECK_CODE(code, lino, _exit);
2,335!
628

629
    if (taosCheckExistFile(cCurrent)) {
2,335!
630
      // current.c.json exists
631

632
      fs->etype = TSDB_FEDIT_COMMIT;
×
633
      if (rollback) {
×
634
        code = abort_edit(fs);
×
635
        TSDB_CHECK_CODE(code, lino, _exit);
×
636
      } else {
637
        code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
×
638
        TSDB_CHECK_CODE(code, lino, _exit);
×
639

640
        code = commit_edit(fs);
×
641
        TSDB_CHECK_CODE(code, lino, _exit);
×
642
      }
643
    } else if (taosCheckExistFile(mCurrent)) {
2,338!
644
      // current.m.json exists
645
      fs->etype = TSDB_FEDIT_MERGE;
×
646
      code = abort_edit(fs);
×
647
      TSDB_CHECK_CODE(code, lino, _exit);
×
648
    }
649

650
    code = tsdbFSDupState(fs);
2,338✔
651
    TSDB_CHECK_CODE(code, lino, _exit);
2,337!
652

653
    code = tsdbFSScanAndFix(fs);
2,337✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
2,339!
655
  } else {
656
    code = save_fs(fs->fSetArr, fCurrent);
9,229✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
9,230!
658
  }
659

660
_exit:
9,230✔
661
  if (code) {
11,569!
662
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
663
  } else {
664
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
11,569✔
665
  }
666
  return code;
11,572✔
667
}
668

669
static void close_file_system(STFileSystem *fs) {
11,572✔
670
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
141,838✔
671
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
141,882✔
672
}
11,578✔
673

674
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
×
675
  if (pSet1->fid < pSet2->fid) {
×
676
    return -1;
×
677
  } else if (pSet1->fid > pSet2->fid) {
×
678
    return 1;
×
679
  }
680
  return 0;
×
681
}
682

683
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray) {
14,723✔
684
  int32_t code = 0;
14,723✔
685
  int32_t lino = 0;
14,723✔
686

687
  code = tsdbFSDupState(fs);
14,723✔
688
  if (code) return code;
14,722!
689

690
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
14,722✔
691
  STFileSet      *fset = NULL;
14,722✔
692
  const STFileOp *op;
693
  TARRAY2_FOREACH_PTR(opArray, op) {
180,086✔
694
    if (!fset || fset->fid != op->fid) {
165,364✔
695
      STFileSet tfset = {.fid = op->fid};
142,833✔
696
      fset = &tfset;
142,833✔
697
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
142,346✔
698
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
142,346✔
699

700
      if (!fset) {
142,346✔
701
        code = tsdbTFileSetInit(op->fid, &fset);
128,292✔
702
        TSDB_CHECK_CODE(code, lino, _exit);
128,386!
703

704
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
128,271✔
705
        TSDB_CHECK_CODE(code, lino, _exit);
128,271!
706
      }
707
    }
708

709
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
164,856✔
710
    TSDB_CHECK_CODE(code, lino, _exit);
165,364!
711
  }
712

713
  // remove empty empty stt level and empty file set
714
  int32_t i = 0;
14,722✔
715
  while (i < TARRAY2_SIZE(fsetArray)) {
172,016✔
716
    fset = TARRAY2_GET(fsetArray, i);
157,294✔
717

718
    SSttLvl *lvl;
719
    int32_t  j = 0;
157,294✔
720
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
326,819✔
721
      lvl = TARRAY2_GET(fset->lvlArr, j);
169,525✔
722

723
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
169,525✔
724
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
5,778!
725
      } else {
726
        j++;
163,747✔
727
      }
728
    }
729

730
    if (tsdbTFileSetIsEmpty(fset)) {
157,294!
731
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
×
732
    } else {
733
      i++;
157,294✔
734
    }
735
  }
736

737
_exit:
14,722✔
738
  if (code) {
14,722!
739
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
740
  }
741
  return code;
14,723✔
742
}
743

744
// return error code
745
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
11,505✔
746
  int32_t code;
747
  int32_t lino;
748

749
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
11,505✔
750
  TSDB_CHECK_CODE(code, lino, _exit);
11,562!
751

752
  code = create_fs(pTsdb, fs);
11,562✔
753
  TSDB_CHECK_CODE(code, lino, _exit);
11,567!
754

755
  code = open_fs(fs[0], rollback);
11,567✔
756
  TSDB_CHECK_CODE(code, lino, _exit);
11,572!
757

758
_exit:
11,572✔
759
  if (code) {
11,572!
760
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
761
    destroy_fs(fs);
×
762
  } else {
763
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
11,572✔
764
  }
765
  return code;
11,572✔
766
}
767

768
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block);
769
extern void tsdbStopAllCompTask(STsdb *tsdb);
770

771
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
11,638✔
772
  STFileSystem *fs = pTsdb->pFS;
11,638✔
773
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
11,638✔
774
  if (asyncTasks == NULL) {
11,638!
775
    return terrno;
×
776
  }
777

778
  (void)taosThreadMutexLock(&pTsdb->mutex);
11,638✔
779

780
  // disable
781
  pTsdb->bgTaskDisabled = true;
11,638✔
782

783
  // collect channel
784
  STFileSet *fset;
785
  TARRAY2_FOREACH(fs->fSetArr, fset) {
141,506✔
786
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
259,869!
787
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
259,909!
788
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL) {
259,802!
789
      taosArrayDestroy(asyncTasks);
×
790
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
791
      return terrno;
×
792
    }
793
    fset->mergeScheduled = false;
129,839✔
794
    tsdbFSSetBlockCommit(fset, false);
129,839✔
795
  }
796

797
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
11,637✔
798

799
  // destroy all channels
800
  for (int32_t k = 0; k < 2; k++) {
34,914✔
801
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
799,491✔
802
      SVATaskID *task = taosArrayGet(asyncTasks, i);
776,204✔
803
      if (k == 0) {
776,211✔
804
        (void)vnodeACancel(task);
387,782✔
805
      } else {
806
        vnodeAWait(task);
388,429✔
807
      }
808
    }
809
  }
810
  taosArrayDestroy(asyncTasks);
11,638✔
811

812
#ifdef TD_ENTERPRISE
813
  tsdbStopAllCompTask(pTsdb);
11,638✔
814
#endif
815
  return 0;
11,638✔
816
}
817

818
void tsdbEnableBgTask(STsdb *pTsdb) {
66✔
819
  (void)taosThreadMutexLock(&pTsdb->mutex);
66✔
820
  pTsdb->bgTaskDisabled = false;
66✔
821
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
66✔
822
}
66✔
823

824
void tsdbCloseFS(STFileSystem **fs) {
11,572✔
825
  if (fs[0] == NULL) return;
11,572!
826

827
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
11,572✔
828
  if (code) {
11,572!
829
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID((*fs)->tsdb->pVnode), __func__, __LINE__,
×
830
              tstrerror(code));
831
  }
832
  close_file_system(fs[0]);
11,572✔
833
  destroy_fs(fs);
11,572✔
834
  return;
11,572✔
835
}
836

837
int64_t tsdbFSAllocEid(STFileSystem *fs) {
15,016✔
838
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
15,016✔
839
  int64_t cid = ++fs->neid;
15,016✔
840
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
15,016✔
841
  return cid;
15,014✔
842
}
843

844
void tsdbFSUpdateEid(STFileSystem *fs, int64_t cid) {
251✔
845
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
251✔
846
  fs->neid = TMAX(fs->neid, cid);
251✔
847
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
251✔
848
}
251✔
849

850
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
14,723✔
851
  int32_t code = 0;
14,723✔
852
  int32_t lino;
853
  char    current_t[TSDB_FILENAME_LEN];
854

855
  if (etype == TSDB_FEDIT_COMMIT) {
14,723✔
856
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,808✔
857
  } else {
858
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
4,915✔
859
  }
860

861
  if (tsem_wait(&fs->canEdit) != 0) {
14,723!
862
    tsdbError("vgId:%d failed to wait semaphore", TD_VID(fs->tsdb->pVnode));
×
863
  }
864
  fs->etype = etype;
14,722✔
865

866
  // edit
867
  code = edit_fs(fs, opArray);
14,722✔
868
  TSDB_CHECK_CODE(code, lino, _exit);
14,723!
869

870
  // save fs
871
  code = save_fs(fs->fSetArrTmp, current_t);
14,723✔
872
  TSDB_CHECK_CODE(code, lino, _exit);
14,723!
873

874
_exit:
14,723✔
875
  if (code) {
14,723!
876
    tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
×
877
              tstrerror(code), etype);
878
  } else {
879
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
14,723!
880
  }
881
  return code;
14,723✔
882
}
883

884
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
284,645✔
885
  if (block) {
284,645✔
886
    fset->blockCommit = true;
6✔
887
  } else {
888
    fset->blockCommit = false;
284,639✔
889
    if (fset->numWaitCommit > 0) {
284,639✔
890
      (void)taosThreadCondSignal(&fset->canCommit);
3✔
891
    }
892
  }
893
}
284,645✔
894

895
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
137,907✔
896
  (void)taosThreadMutexLock(&tsdb->mutex);
137,907✔
897
  STFileSet *fset;
898
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
137,932✔
899
  if (fset) {
137,904✔
900
    while (fset->blockCommit) {
9,149✔
901
      fset->numWaitCommit++;
3✔
902
      (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
3✔
903
      fset->numWaitCommit--;
3✔
904
    }
905
  }
906
  (void)taosThreadMutexUnlock(&tsdb->mutex);
137,904✔
907
  return;
137,880✔
908
}
909

910
// IMPORTANT: the caller must hold fs->tsdb->mutex
911
int32_t tsdbFSEditCommit(STFileSystem *fs) {
14,723✔
912
  int32_t code = 0;
14,723✔
913
  int32_t lino = 0;
14,723✔
914

915
  // commit
916
  code = commit_edit(fs);
14,723✔
917
  TSDB_CHECK_CODE(code, lino, _exit);
14,723!
918

919
  // schedule merge
920
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
14,723✔
921
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
14,723✔
922
    STFileSet *fset;
923
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
169,723✔
924
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
155,132✔
925
        tsdbFSSetBlockCommit(fset, false);
2,804✔
926
        continue;
2,804✔
927
      }
928

929
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
152,328✔
930
      if (lvl->level != 0) {
152,328✔
931
        tsdbFSSetBlockCommit(fset, false);
6,474✔
932
        continue;
6,474✔
933
      }
934

935
      // bool    skipMerge = false;
936
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
145,854✔
937
      if (numFile >= sttTrigger && (!fset->mergeScheduled)) {
145,854✔
938
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
6,227!
939
        if (arg == NULL) {
6,227!
940
          code = terrno;
×
941
          TSDB_CHECK_CODE(code, lino, _exit);
×
942
        }
943

944
        arg->tsdb = fs->tsdb;
6,227✔
945
        arg->fid = fset->fid;
6,227✔
946

947
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
6,227✔
948
        TSDB_CHECK_CODE(code, lino, _exit);
6,227!
949
        fset->mergeScheduled = true;
6,227✔
950
      }
951

952
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
145,854✔
953
        tsdbFSSetBlockCommit(fset, true);
6✔
954
      } else {
955
        tsdbFSSetBlockCommit(fset, false);
145,848✔
956
      }
957
    }
958
  }
959

960
_exit:
14,723✔
961
  if (code) {
14,723!
962
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
×
963
  } else {
964
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
14,723!
965
  }
966
  if (tsem_post(&fs->canEdit) != 0) {
14,723!
967
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
968
  }
969
  return code;
14,723✔
970
}
971

972
int32_t tsdbFSEditAbort(STFileSystem *fs) {
×
973
  int32_t code = abort_edit(fs);
×
974
  if (tsem_post(&fs->canEdit) != 0) {
×
975
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
976
  }
977
  return code;
×
978
}
979

980
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
298,221✔
981
  STFileSet   tfset = {.fid = fid};
298,221✔
982
  STFileSet  *pset = &tfset;
298,221✔
983
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
298,221✔
984
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
298,231✔
985
}
298,231✔
986

987
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
63✔
988
  int32_t    code = 0;
63✔
989
  STFileSet *fset;
990
  STFileSet *fset1;
991

992
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
63!
993
  if (fsetArr[0] == NULL) return terrno;
63!
994

995
  TARRAY2_INIT(fsetArr[0]);
63✔
996

997
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
63✔
998
  TARRAY2_FOREACH(fs->fSetArr, fset) {
63!
999
    code = tsdbTFileSetInitCopy(fs->tsdb, fset, &fset1);
×
1000
    if (code) break;
×
1001

1002
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1003
    if (code) break;
×
1004
  }
1005
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
63✔
1006

1007
  if (code) {
63!
1008
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1009
    taosMemoryFree(fsetArr[0]);
×
1010
    fsetArr[0] = NULL;
×
1011
  }
1012
  return code;
63✔
1013
}
1014

1015
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
63✔
1016
  if (fsetArr[0]) {
63!
1017
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
63!
1018
    taosMemoryFree(fsetArr[0]);
63!
1019
    fsetArr[0] = NULL;
63✔
1020
  }
1021
}
63✔
1022

1023
int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
67✔
1024
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
67✔
1025
  int32_t code = tsdbFSCreateRefSnapshotWithoutLock(fs, fsetArr);
67✔
1026
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
67✔
1027
  return code;
67✔
1028
}
1029

1030
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
4,536,177✔
1031
  int32_t    code = 0;
4,536,177✔
1032
  STFileSet *fset, *fset1;
1033

1034
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
4,536,177!
1035
  if (fsetArr[0] == NULL) return terrno;
4,539,257!
1036

1037
  TARRAY2_FOREACH(fs->fSetArr, fset) {
13,480,686✔
1038
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
8,941,044✔
1039
    if (code) break;
8,942,033!
1040

1041
    code = TARRAY2_APPEND(fsetArr[0], fset1);
8,942,033✔
1042
    if (code) {
8,941,429!
1043
      tsdbTFileSetClear(&fset1);
×
1044
      break;
×
1045
    }
1046
  }
1047

1048
  if (code) {
4,539,642!
1049
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1050
    taosMemoryFree(fsetArr[0]);
×
1051
    fsetArr[0] = NULL;
×
1052
  }
1053
  return code;
4,539,642✔
1054
}
1055

1056
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
4,539,366✔
1057
  if (fsetArr[0]) {
4,539,366!
1058
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
13,487,131!
1059
    taosMemoryFreeClear(fsetArr[0]);
4,540,196!
1060
    fsetArr[0] = NULL;
4,540,438✔
1061
  }
1062
}
4,539,836✔
1063

1064
static SHashObj *tsdbFSetRangeArrayToHash(TFileSetRangeArray *pRanges) {
×
1065
  int32_t   capacity = TARRAY2_SIZE(pRanges) * 2;
×
1066
  SHashObj *pHash = taosHashInit(capacity, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
×
1067
  if (pHash == NULL) {
×
1068
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1069
    return NULL;
×
1070
  }
1071

1072
  for (int32_t i = 0; i < TARRAY2_SIZE(pRanges); i++) {
×
1073
    STFileSetRange *u = TARRAY2_GET(pRanges, i);
×
1074
    int32_t         fid = u->fid;
×
1075
    int32_t         code = taosHashPut(pHash, &fid, sizeof(fid), u, sizeof(*u));
×
1076
    tsdbDebug("range diff hash fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
×
1077
  }
1078
  return pHash;
×
1079
}
1080

1081
int32_t tsdbFSCreateCopyRangedSnapshot(STFileSystem *fs, TFileSetRangeArray *pRanges, TFileSetArray **fsetArr,
×
1082
                                       TFileOpArray *fopArr) {
1083
  int32_t    code = 0;
×
1084
  STFileSet *fset;
1085
  STFileSet *fset1;
1086
  SHashObj  *pHash = NULL;
×
1087

1088
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
×
1089
  if (fsetArr == NULL) return terrno;
×
1090
  TARRAY2_INIT(fsetArr[0]);
×
1091

1092
  if (pRanges) {
×
1093
    pHash = tsdbFSetRangeArrayToHash(pRanges);
×
1094
    if (pHash == NULL) {
×
1095
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1096
      goto _out;
×
1097
    }
1098
  }
1099

1100
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
×
1101
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
1102
    int64_t ever = VERSION_MAX;
×
1103
    if (pHash) {
×
1104
      int32_t         fid = fset->fid;
×
1105
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
×
1106
      if (u) {
×
1107
        ever = u->sver - 1;
×
1108
      }
1109
    }
1110

1111
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
×
1112
    if (code) break;
×
1113

1114
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1115
    if (code) break;
×
1116
  }
1117
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
×
1118

1119
_out:
×
1120
  if (code) {
×
1121
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1122
    taosMemoryFree(fsetArr[0]);
×
1123
    fsetArr[0] = NULL;
×
1124
  }
1125
  if (pHash) {
×
1126
    taosHashCleanup(pHash);
×
1127
    pHash = NULL;
×
1128
  }
1129
  return code;
×
1130
}
1131

1132
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
×
1133

1134
int32_t tsdbFSCreateRefRangedSnapshot(STFileSystem *fs, int64_t sver, int64_t ever, TFileSetRangeArray *pRanges,
×
1135
                                      TFileSetRangeArray **fsrArr) {
1136
  int32_t         code = 0;
×
1137
  STFileSet      *fset;
1138
  STFileSetRange *fsr1 = NULL;
×
1139
  SHashObj       *pHash = NULL;
×
1140

1141
  fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
×
1142
  if (fsrArr[0] == NULL) {
×
1143
    code = terrno;
×
1144
    goto _out;
×
1145
  }
1146

1147
  tsdbInfo("pRanges size:%d", (pRanges == NULL ? 0 : TARRAY2_SIZE(pRanges)));
×
1148
  if (pRanges) {
×
1149
    pHash = tsdbFSetRangeArrayToHash(pRanges);
×
1150
    if (pHash == NULL) {
×
1151
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1152
      goto _out;
×
1153
    }
1154
  }
1155

1156
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
×
1157
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
1158
    int64_t sver1 = sver;
×
1159
    int64_t ever1 = ever;
×
1160

1161
    if (pHash) {
×
1162
      int32_t         fid = fset->fid;
×
1163
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
×
1164
      if (u) {
×
1165
        sver1 = u->sver;
×
1166
        tsdbDebug("range hash get fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
×
1167
      }
1168
    }
1169

1170
    if (sver1 > ever1) {
×
1171
      tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
×
1172
      continue;
×
1173
    }
1174

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

1177
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
×
1178
    if (code) break;
×
1179

1180
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
×
1181
    if (code) break;
×
1182

1183
    fsr1 = NULL;
×
1184
  }
1185
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
×
1186

1187
  if (code) {
×
1188
    tsdbTFileSetRangeClear(&fsr1);
×
1189
    TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
×
1190
    fsrArr[0] = NULL;
×
1191
  }
1192

1193
_out:
×
1194
  if (pHash) {
×
1195
    taosHashCleanup(pHash);
×
1196
    pHash = NULL;
×
1197
  }
1198
  return code;
×
1199
}
1200

1201
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
×
1202

1203
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
144,568✔
1204
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1205
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
144,568✔
1206

1207
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
144,568✔
1208
  if (*fset == NULL) {
144,570✔
1209
    return;
128,804✔
1210
  }
1211

1212
  struct STFileSetCond *cond = NULL;
15,766✔
1213
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
15,766✔
1214
    cond = &(*fset)->conds[0];
9,145✔
1215
  } else {
1216
    cond = &(*fset)->conds[1];
6,621✔
1217
  }
1218

1219
  while (1) {
1220
    if (cond->running) {
16,761✔
1221
      cond->numWait++;
995✔
1222
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
995✔
1223
      cond->numWait--;
995✔
1224
    } else {
1225
      cond->running = true;
15,766✔
1226
      break;
15,766✔
1227
    }
1228
  }
1229

1230
  tsdbInfo("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
15,766!
1231
  return;
15,767✔
1232
}
1233

1234
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
15,767✔
1235
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1236
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
15,767✔
1237

1238
  STFileSet *fset = NULL;
15,767✔
1239
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
15,767✔
1240
  if (fset == NULL) {
15,767!
1241
    return;
×
1242
  }
1243

1244
  struct STFileSetCond *cond = NULL;
15,767✔
1245
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
15,767✔
1246
    cond = &fset->conds[0];
9,146✔
1247
  } else {
1248
    cond = &fset->conds[1];
6,621✔
1249
  }
1250

1251
  cond->running = false;
15,767✔
1252
  if (cond->numWait > 0) {
15,767✔
1253
    (void)taosThreadCondSignal(&cond->cond);
995✔
1254
  }
1255

1256
  tsdbInfo("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
15,767!
1257
  return;
15,767✔
1258
}
1259

1260
struct SFileSetReader {
1261
  STsdb     *pTsdb;
1262
  STFileSet *pFileSet;
1263
  int32_t    fid;
1264
  int64_t    startTime;
1265
  int64_t    endTime;
1266
  int64_t    lastCompactTime;
1267
  int64_t    totalSize;
1268
};
1269

1270
int32_t tsdbFileSetReaderOpen(void *pVnode, struct SFileSetReader **ppReader) {
×
1271
  if (pVnode == NULL || ppReader == NULL) {
×
1272
    return TSDB_CODE_INVALID_PARA;
×
1273
  }
1274

1275
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
×
1276

1277
  (*ppReader) = taosMemoryCalloc(1, sizeof(struct SFileSetReader));
×
1278
  if (*ppReader == NULL) {
×
1279
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, __LINE__,
×
1280
              tstrerror(terrno));
1281
    return terrno;
×
1282
  }
1283

1284
  (*ppReader)->pTsdb = pTsdb;
×
1285
  (*ppReader)->fid = INT32_MIN;
×
1286
  (*ppReader)->pFileSet = NULL;
×
1287

1288
  return TSDB_CODE_SUCCESS;
×
1289
}
1290

1291
static int32_t tsdbFileSetReaderNextNoLock(struct SFileSetReader *pReader) {
×
1292
  STsdb  *pTsdb = pReader->pTsdb;
×
1293
  int32_t code = TSDB_CODE_SUCCESS;
×
1294

1295
  tsdbTFileSetClear(&pReader->pFileSet);
×
1296

1297
  STFileSet *fset = &(STFileSet){
×
1298
      .fid = pReader->fid,
×
1299
  };
1300

1301
  STFileSet **fsetPtr = TARRAY2_SEARCH(pReader->pTsdb->pFS->fSetArr, &fset, tsdbTFileSetCmprFn, TD_GT);
×
1302
  if (fsetPtr == NULL) {
×
1303
    pReader->fid = INT32_MAX;
×
1304
    return TSDB_CODE_NOT_FOUND;
×
1305
  }
1306

1307
  // ref file set
1308
  code = tsdbTFileSetInitRef(pReader->pTsdb, *fsetPtr, &pReader->pFileSet);
×
1309
  if (code) return code;
×
1310

1311
  // get file set details
1312
  pReader->fid = pReader->pFileSet->fid;
×
1313
  tsdbFidKeyRange(pReader->fid, pTsdb->keepCfg.days, pTsdb->keepCfg.precision, &pReader->startTime, &pReader->endTime);
×
1314
  pReader->lastCompactTime = 0;  // TODO
×
1315
  pReader->totalSize = 0;
×
1316
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
×
1317
    STFileObj *fobj = pReader->pFileSet->farr[i];
×
1318
    if (fobj) {
×
1319
      pReader->totalSize += fobj->f->size;
×
1320
    }
1321
  }
1322
  SSttLvl *lvl;
1323
  TARRAY2_FOREACH(pReader->pFileSet->lvlArr, lvl) {
×
1324
    STFileObj *fobj;
1325
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { pReader->totalSize += fobj->f->size; }
×
1326
  }
1327

1328
  return code;
×
1329
}
1330

1331
int32_t tsdbFileSetReaderNext(struct SFileSetReader *pReader) {
×
1332
  int32_t code = TSDB_CODE_SUCCESS;
×
1333
  (void)taosThreadMutexLock(&pReader->pTsdb->mutex);
×
1334
  code = tsdbFileSetReaderNextNoLock(pReader);
×
1335
  (void)taosThreadMutexUnlock(&pReader->pTsdb->mutex);
×
1336
  return code;
×
1337
}
1338

1339
int32_t tsdbFileSetGetEntryField(struct SFileSetReader *pReader, const char *field, void *value) {
×
1340
  const char *fieldName;
1341

1342
  if (pReader->fid == INT32_MIN || pReader->fid == INT32_MAX) {
×
1343
    return TSDB_CODE_INVALID_PARA;
×
1344
  }
1345

1346
  fieldName = "fileset_id";
×
1347
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1348
    *(int32_t *)value = pReader->fid;
×
1349
    return TSDB_CODE_SUCCESS;
×
1350
  }
1351

1352
  fieldName = "start_time";
×
1353
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1354
    *(int64_t *)value = pReader->startTime;
×
1355
    return TSDB_CODE_SUCCESS;
×
1356
  }
1357

1358
  fieldName = "end_time";
×
1359
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1360
    *(int64_t *)value = pReader->endTime;
×
1361
    return TSDB_CODE_SUCCESS;
×
1362
  }
1363

1364
  fieldName = "total_size";
×
1365
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1366
    *(int64_t *)value = pReader->totalSize;
×
1367
    return TSDB_CODE_SUCCESS;
×
1368
  }
1369

1370
  fieldName = "last_compact_time";
×
1371
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1372
    *(int64_t *)value = pReader->lastCompactTime;
×
1373
    return TSDB_CODE_SUCCESS;
×
1374
  }
1375

1376
  fieldName = "should_compact";
×
1377
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1378
    *(char *)value = 0;  // TODO
×
1379
    return TSDB_CODE_SUCCESS;
×
1380
  }
1381

1382
  fieldName = "details";
×
1383
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1384
    // TODO
1385
    return TSDB_CODE_SUCCESS;
×
1386
  }
1387

1388
  return TSDB_CODE_INVALID_PARA;
×
1389
}
1390

1391
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
×
1392
  if (ppReader == NULL || *ppReader == NULL) {
×
1393
    return;
×
1394
  }
1395

1396
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
×
1397
  taosMemoryFree(*ppReader);
×
1398

1399
  *ppReader = NULL;
×
1400
  return;
×
1401
}
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