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

taosdata / TDengine / #3569

28 Dec 2024 03:56AM UTC coverage: 62.96% (-0.3%) from 63.286%
#3569

push

travis-ci

web-flow
Merge pull request #29369 from taosdata/enh/TS-4994-more-test-case

enh: add more cases

139174 of 284303 branches covered (48.95%)

Branch coverage included in aggregate %.

26 of 32 new or added lines in 3 files covered. (81.25%)

1505 existing lines in 138 files now uncovered.

217071 of 281522 relevant lines covered (77.11%)

18800589.91 hits per line

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

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

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

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

58
  return 0;
11,239✔
59
}
60

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

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

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

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

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

87
  data = cJSON_PrintUnformatted(json);
18,966✔
88
  if (data == NULL) {
18,967!
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);
18,967✔
93
  if (fp == NULL) {
18,967!
94
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
95
  }
96

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

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

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

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

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

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

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

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

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

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

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

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

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

169
  // fset
170
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
18,968✔
171
  if (!ajson) {
18,966!
172
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
173
  }
174
  const STFileSet *fset;
175
  TARRAY2_FOREACH(arr, fset) {
169,611✔
176
    cJSON *item = cJSON_CreateObject();
150,645✔
177
    if (!item) {
150,635!
178
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
179
    }
180
    (void)cJSON_AddItemToArray(ajson, item);
150,635✔
181

182
    code = tsdbTFileSetToJson(fset, item);
150,629✔
183
    TSDB_CHECK_CODE(code, lino, _exit);
150,645!
184
  }
185

186
  code = save_json(json, fname);
18,966✔
187
  TSDB_CHECK_CODE(code, lino, _exit);
18,968!
188

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

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

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

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

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

211
  /* fmtv */
212
  item1 = cJSON_GetObjectItem(json, "fmtv");
2,292✔
213
  if (cJSON_IsNumber(item1)) {
2,296!
214
    if (item1->valuedouble != 1) {
2,282!
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,282✔
223
  if (cJSON_IsArray(item1)) {
2,297!
224
    const cJSON *item2;
225
    cJSON_ArrayForEach(item2, item1) {
3,812!
226
      STFileSet *fset;
227
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
1,521✔
228
      TSDB_CHECK_CODE(code, lino, _exit);
1,522!
229

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

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

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

256
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
160,615✔
257
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
150,611✔
258
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
150,611!
259

260
    if (fset1 && fset2) {
150,611!
261
      if (fset1->fid < fset2->fid) {
22,140!
262
        // delete fset1
263
        tsdbTFileSetRemove(fset1);
×
264
        i1++;
×
265
      } else if (fset1->fid > fset2->fid) {
22,140✔
266
        // create new file set with fid of fset2->fid
267
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
27✔
268
        TSDB_CHECK_CODE(code, lino, _exit);
27!
269
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
27✔
270
        TSDB_CHECK_CODE(code, lino, _exit);
27!
271
        i1++;
27✔
272
        i2++;
27✔
273
      } else {
274
        // edit
275
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
22,113✔
276
        TSDB_CHECK_CODE(code, lino, _exit);
22,112!
277
        i1++;
22,112✔
278
        i2++;
22,112✔
279
      }
280
    } else if (fset1) {
128,471!
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,471✔
287
      TSDB_CHECK_CODE(code, lino, _exit);
128,496!
288
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
128,457✔
289
      TSDB_CHECK_CODE(code, lino, _exit);
128,457!
290
      i1++;
128,457✔
291
      i2++;
128,457✔
292
    }
293
  }
294

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

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

306
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
10,019✔
307
  if (fs->etype == TSDB_FEDIT_COMMIT) {
10,019✔
308
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
6,705✔
309
  } else {
310
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,314✔
311
  }
312

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

317
  code = apply_commit(fs);
10,019✔
318
  TSDB_CHECK_CODE(code, lino, _exit);
10,019!
319

320
_exit:
10,019✔
321
  if (code) {
10,019!
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);
10,019✔
326
  }
327
  return code;
10,019✔
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,519✔
364
  int32_t code = 0;
2,519✔
365
  int32_t lino = 0;
2,519✔
366

367
  // check file existence
368
  if (!taosCheckExistFile(fobj->fname)) {
2,519!
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,522✔
391
}
392

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

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

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

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

407
  return 0;
4,817✔
408
}
409

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

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

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

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

437
        if (TSDB_FTYPE_DATA == i && fset->farr[i]->f->lcn > 0) {
974!
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,523✔
456
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
3,067✔
457
      STFileObj *fobj;
458
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,092✔
459
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
1,548✔
460
        TSDB_CHECK_CODE(code, lino, _exit);
1,548!
461
      }
462
    }
463
  }
464

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

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

476
  STFileHashEntry *entry = hash->buckets[idx];
4,819✔
477
  while (entry) {
4,820✔
478
    if (strcmp(entry->fname, fname) == 0) {
4,819✔
479
      return entry;
4,818✔
480
    }
481
    entry = entry->next;
1✔
482
  }
483

484
  return NULL;
1✔
485
}
486

487
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,297✔
488
  for (int32_t i = 0; i < hash->numBucket; i++) {
9,268,655✔
489
    STFileHashEntry *entry = hash->buckets[i];
9,266,358✔
490
    while (entry) {
9,271,174✔
491
      STFileHashEntry *next = entry->next;
4,816✔
492
      taosMemoryFree(entry);
4,816!
493
      entry = next;
4,816✔
494
    }
495
  }
496
  taosMemoryFree(hash->buckets);
2,297!
497
  memset(hash, 0, sizeof(*hash));
2,297✔
498
}
2,297✔
499

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

505
  {  // scan each file
506
    STFileSet *fset = NULL;
2,283✔
507
    TARRAY2_FOREACH(fs->fSetArr, fset) {
3,803✔
508
      // data file
509
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
7,604✔
510
        if (fset->farr[ftype] == NULL) continue;
6,084✔
511
        STFileObj *fobj = fset->farr[ftype];
974✔
512
        code = tsdbFSDoScanAndFixFile(fs, fobj);
974✔
513
        if (code) {
974!
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,062✔
522
        STFileObj *fobj;
523
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,089✔
524
          code = tsdbFSDoScanAndFixFile(fs, fobj);
1,547✔
525
          if (code) {
1,547!
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,283!
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,283✔
544
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2,283!
545

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

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

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

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

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

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

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

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

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

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

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

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

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

598
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
34,427✔
599

600
  const STFileSet *fset1;
601
  TARRAY2_FOREACH(src, fset1) {
35,948✔
602
    STFileSet *fset2;
603
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
23,635✔
604
    if (code) return code;
23,633!
605
    code = TARRAY2_APPEND(dst, fset2);
23,633✔
606
    if (code) return code;
23,634!
607
  }
608

609
  return 0;
12,313✔
610
}
611

612
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
11,231✔
613
  int32_t code = 0;
11,231✔
614
  int32_t lino = 0;
11,231✔
615
  STsdb  *pTsdb = fs->tsdb;
11,231✔
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,231✔
622
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
11,229✔
623
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
11,239✔
624

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

629
    if (taosCheckExistFile(cCurrent)) {
2,284!
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,293!
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,296✔
651
    TSDB_CHECK_CODE(code, lino, _exit);
2,290!
652

653
    code = tsdbFSScanAndFix(fs);
2,290✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
2,294!
655
  } else {
656
    code = save_fs(fs->fSetArr, fCurrent);
8,948✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
8,949!
658
  }
659

660
_exit:
8,949✔
661
  if (code) {
11,243!
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,243✔
665
  }
666
  return code;
11,246✔
667
}
668

669
static void close_file_system(STFileSystem *fs) {
11,246✔
670
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
141,296✔
671
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
141,293✔
672
}
11,247✔
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, EFEditT etype) {
10,019✔
684
  int32_t code = 0;
10,019✔
685
  int32_t lino = 0;
10,019✔
686

687
  code = tsdbFSDupState(fs);
10,019✔
688
  if (code) return code;
10,019!
689

690
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
10,019✔
691
  STFileSet      *fset = NULL;
10,019✔
692
  const STFileOp *op;
693
  int32_t         fid = INT32_MIN;
10,019✔
694
  TSKEY           now = taosGetTimestampMs();
10,019✔
695
  TARRAY2_FOREACH_PTR(opArray, op) {
164,511✔
696
    if (!fset || fset->fid != op->fid) {
154,495✔
697
      STFileSet tfset = {.fid = op->fid};
137,909✔
698
      fset = &tfset;
137,909✔
699
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
137,817✔
700
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
137,817✔
701

702
      if (!fset) {
137,817✔
703
        code = tsdbTFileSetInit(op->fid, &fset);
128,432✔
704
        TSDB_CHECK_CODE(code, lino, _exit);
128,406!
705

706
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
128,377✔
707
        TSDB_CHECK_CODE(code, lino, _exit);
128,377!
708
      }
709
    }
710

711
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
154,348✔
712
    TSDB_CHECK_CODE(code, lino, _exit);
154,492!
713

714
    if (fid != op->fid) {
154,492✔
715
      fid = op->fid;
137,908✔
716
      if (etype == TSDB_FEDIT_COMMIT) {
137,908✔
717
        fset->lastCommit = now;
134,595✔
718
      } else if (etype == TSDB_FEDIT_COMPACT) {
3,313✔
719
        fset->lastCompact = now;
74✔
720
      }
721
    }
722
  }
723

724
  // remove empty empty stt level and empty file set
725
  int32_t i = 0;
10,016✔
726
  while (i < TARRAY2_SIZE(fsetArray)) {
160,670✔
727
    fset = TARRAY2_GET(fsetArray, i);
150,655✔
728

729
    SSttLvl *lvl;
730
    int32_t  j = 0;
150,655✔
731
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
305,401✔
732
      lvl = TARRAY2_GET(fset->lvlArr, j);
154,746✔
733

734
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
154,746✔
735
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
3,422!
736
      } else {
737
        j++;
151,324✔
738
      }
739
    }
740

741
    if (tsdbTFileSetIsEmpty(fset)) {
150,655!
742
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
×
743
    } else {
744
      i++;
150,654✔
745
    }
746
  }
747

748
_exit:
10,015✔
749
  if (code) {
10,015!
750
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
×
751
  }
752
  return code;
10,018✔
753
}
754

755
// return error code
756
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
11,185✔
757
  int32_t code;
758
  int32_t lino;
759

760
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
11,185✔
761
  TSDB_CHECK_CODE(code, lino, _exit);
11,235!
762

763
  code = create_fs(pTsdb, fs);
11,235✔
764
  TSDB_CHECK_CODE(code, lino, _exit);
11,241!
765

766
  code = open_fs(fs[0], rollback);
11,241✔
767
  TSDB_CHECK_CODE(code, lino, _exit);
11,246!
768

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

779
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block);
780
extern void tsdbStopAllCompTask(STsdb *tsdb);
781

782
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
11,319✔
783
  STFileSystem *fs = pTsdb->pFS;
11,319✔
784
  SArray       *asyncTasks = taosArrayInit(0, sizeof(SVATaskID));
11,319✔
785
  if (asyncTasks == NULL) {
11,319!
786
    return terrno;
×
787
  }
788

789
  (void)taosThreadMutexLock(&pTsdb->mutex);
11,319✔
790

791
  // disable
792
  pTsdb->bgTaskDisabled = true;
11,319✔
793

794
  // collect channel
795
  STFileSet *fset;
796
  TARRAY2_FOREACH(fs->fSetArr, fset) {
141,376✔
797
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
260,111!
798
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
260,111!
799
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL) {
260,111!
800
      taosArrayDestroy(asyncTasks);
×
801
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
802
      return terrno;
×
803
    }
804
    fset->mergeScheduled = false;
130,056✔
805
    tsdbFSSetBlockCommit(fset, false);
130,056✔
806
  }
807

808
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
11,321✔
809

810
  // destroy all channels
811
  for (int32_t k = 0; k < 2; k++) {
33,957✔
812
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
803,025✔
813
      SVATaskID *task = taosArrayGet(asyncTasks, i);
780,385✔
814
      if (k == 0) {
780,387✔
815
        (void)vnodeACancel(task);
390,195✔
816
      } else {
817
        vnodeAWait(task);
390,192✔
818
      }
819
    }
820
  }
821
  taosArrayDestroy(asyncTasks);
11,319✔
822

823
#ifdef TD_ENTERPRISE
824
  tsdbStopAllCompTask(pTsdb);
11,319✔
825
#endif
826
  return 0;
11,319✔
827
}
828

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

835
void tsdbCloseFS(STFileSystem **fs) {
11,246✔
836
  if (fs[0] == NULL) return;
11,246!
837

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

848
int64_t tsdbFSAllocEid(STFileSystem *fs) {
10,307✔
849
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
10,307✔
850
  int64_t cid = ++fs->neid;
10,307✔
851
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
10,307✔
852
  return cid;
10,307✔
853
}
854

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

861
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
10,019✔
862
  int32_t code = 0;
10,019✔
863
  int32_t lino;
864
  char    current_t[TSDB_FILENAME_LEN];
865

866
  if (etype == TSDB_FEDIT_COMMIT) {
10,019✔
867
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
6,705✔
868
  } else {
869
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
3,314✔
870
  }
871

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

877
  // edit
878
  code = edit_fs(fs, opArray, etype);
10,019✔
879
  TSDB_CHECK_CODE(code, lino, _exit);
10,018!
880

881
  // save fs
882
  code = save_fs(fs->fSetArrTmp, current_t);
10,018✔
883
  TSDB_CHECK_CODE(code, lino, _exit);
10,019!
884

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

895
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
276,108✔
896
  if (block) {
276,108✔
897
    fset->blockCommit = true;
5✔
898
  } else {
899
    fset->blockCommit = false;
276,103✔
900
    if (fset->numWaitCommit > 0) {
276,103✔
901
      (void)taosThreadCondSignal(&fset->canCommit);
2✔
902
    }
903
  }
904
}
276,108✔
905

906
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
134,525✔
907
  (void)taosThreadMutexLock(&tsdb->mutex);
134,525✔
908
  STFileSet *fset;
909
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
134,543✔
910
  if (fset) {
134,509✔
911
    while (fset->blockCommit) {
6,078✔
912
      fset->numWaitCommit++;
2✔
913
      (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
2✔
914
      fset->numWaitCommit--;
2✔
915
    }
916
  }
917
  (void)taosThreadMutexUnlock(&tsdb->mutex);
134,509✔
918
  return;
134,526✔
919
}
920

921
// IMPORTANT: the caller must hold fs->tsdb->mutex
922
int32_t tsdbFSEditCommit(STFileSystem *fs) {
10,019✔
923
  int32_t code = 0;
10,019✔
924
  int32_t lino = 0;
10,019✔
925

926
  // commit
927
  code = commit_edit(fs);
10,019✔
928
  TSDB_CHECK_CODE(code, lino, _exit);
10,019!
929

930
  // schedule merge
931
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
10,019✔
932
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
10,019✔
933
    STFileSet *fset;
934
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
155,571✔
935
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
146,052✔
936
        tsdbFSSetBlockCommit(fset, false);
2,561✔
937
        continue;
2,561✔
938
      }
939

940
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
143,491✔
941
      if (lvl->level != 0) {
143,491✔
942
        tsdbFSSetBlockCommit(fset, false);
4,442✔
943
        continue;
4,442✔
944
      }
945

946
      // bool    skipMerge = false;
947
      int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
139,049✔
948
      if (numFile >= sttTrigger && (!fset->mergeScheduled)) {
139,049✔
949
        SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
4,626!
950
        if (arg == NULL) {
4,626!
951
          code = terrno;
×
952
          TSDB_CHECK_CODE(code, lino, _exit);
×
953
        }
954

955
        arg->tsdb = fs->tsdb;
4,626✔
956
        arg->fid = fset->fid;
4,626✔
957

958
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
4,626✔
959
        TSDB_CHECK_CODE(code, lino, _exit);
4,626!
960
        fset->mergeScheduled = true;
4,626✔
961
      }
962

963
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
139,049✔
964
        tsdbFSSetBlockCommit(fset, true);
5✔
965
      } else {
966
        tsdbFSSetBlockCommit(fset, false);
139,044✔
967
      }
968
    }
969
  }
970

971
_exit:
10,019✔
972
  if (code) {
10,019!
973
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
×
974
  } else {
975
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
10,019!
976
  }
977
  if (tsem_post(&fs->canEdit) != 0) {
10,019!
978
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
979
  }
980
  return code;
10,019✔
981
}
982

983
int32_t tsdbFSEditAbort(STFileSystem *fs) {
×
984
  int32_t code = abort_edit(fs);
×
985
  if (tsem_post(&fs->canEdit) != 0) {
×
986
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
987
  }
988
  return code;
×
989
}
990

991
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
285,118✔
992
  STFileSet   tfset = {.fid = fid};
285,118✔
993
  STFileSet  *pset = &tfset;
285,118✔
994
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
285,118✔
995
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
285,119✔
996
}
285,119✔
997

998
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
70✔
999
  int32_t    code = 0;
70✔
1000
  STFileSet *fset;
1001
  STFileSet *fset1;
1002

1003
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
70!
1004
  if (fsetArr[0] == NULL) return terrno;
70!
1005

1006
  TARRAY2_INIT(fsetArr[0]);
70✔
1007

1008
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
70✔
1009
  TARRAY2_FOREACH(fs->fSetArr, fset) {
70!
1010
    code = tsdbTFileSetInitCopy(fs->tsdb, fset, &fset1);
×
1011
    if (code) break;
×
1012

1013
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1014
    if (code) break;
×
1015
  }
1016
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
70✔
1017

1018
  if (code) {
70!
1019
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1020
    taosMemoryFree(fsetArr[0]);
×
1021
    fsetArr[0] = NULL;
×
1022
  }
1023
  return code;
70✔
1024
}
1025

1026
void tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
70✔
1027
  if (fsetArr[0]) {
70!
1028
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
70!
1029
    taosMemoryFree(fsetArr[0]);
70!
1030
    fsetArr[0] = NULL;
70✔
1031
  }
1032
}
70✔
1033

1034
int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
73✔
1035
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
73✔
1036
  int32_t code = tsdbFSCreateRefSnapshotWithoutLock(fs, fsetArr);
73✔
1037
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
73✔
1038
  return code;
73✔
1039
}
1040

1041
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
4,323,158✔
1042
  int32_t    code = 0;
4,323,158✔
1043
  STFileSet *fset, *fset1;
1044

1045
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
4,323,158!
1046
  if (fsetArr[0] == NULL) return terrno;
4,324,712!
1047

1048
  TARRAY2_FOREACH(fs->fSetArr, fset) {
12,052,114✔
1049
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
7,727,255✔
1050
    if (code) break;
7,727,382!
1051

1052
    code = TARRAY2_APPEND(fsetArr[0], fset1);
7,727,382✔
1053
    if (code) {
7,727,402!
1054
      tsdbTFileSetClear(&fset1);
×
1055
      break;
×
1056
    }
1057
  }
1058

1059
  if (code) {
4,324,859!
1060
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1061
    taosMemoryFree(fsetArr[0]);
×
1062
    fsetArr[0] = NULL;
×
1063
  }
1064
  return code;
4,324,859✔
1065
}
1066

1067
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
4,324,822✔
1068
  if (fsetArr[0]) {
4,324,822!
1069
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
12,057,865!
1070
    taosMemoryFreeClear(fsetArr[0]);
4,325,208!
1071
    fsetArr[0] = NULL;
4,325,420✔
1072
  }
1073
}
4,325,093✔
1074

UNCOV
1075
static SHashObj *tsdbFSetRangeArrayToHash(TFileSetRangeArray *pRanges) {
×
UNCOV
1076
  int32_t   capacity = TARRAY2_SIZE(pRanges) * 2;
×
UNCOV
1077
  SHashObj *pHash = taosHashInit(capacity, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
×
UNCOV
1078
  if (pHash == NULL) {
×
1079
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1080
    return NULL;
×
1081
  }
1082

UNCOV
1083
  for (int32_t i = 0; i < TARRAY2_SIZE(pRanges); i++) {
×
UNCOV
1084
    STFileSetRange *u = TARRAY2_GET(pRanges, i);
×
UNCOV
1085
    int32_t         fid = u->fid;
×
UNCOV
1086
    int32_t         code = taosHashPut(pHash, &fid, sizeof(fid), u, sizeof(*u));
×
UNCOV
1087
    tsdbDebug("range diff hash fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
×
1088
  }
UNCOV
1089
  return pHash;
×
1090
}
1091

UNCOV
1092
int32_t tsdbFSCreateCopyRangedSnapshot(STFileSystem *fs, TFileSetRangeArray *pRanges, TFileSetArray **fsetArr,
×
1093
                                       TFileOpArray *fopArr) {
UNCOV
1094
  int32_t    code = 0;
×
1095
  STFileSet *fset;
1096
  STFileSet *fset1;
UNCOV
1097
  SHashObj  *pHash = NULL;
×
1098

UNCOV
1099
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
×
UNCOV
1100
  if (fsetArr == NULL) return terrno;
×
UNCOV
1101
  TARRAY2_INIT(fsetArr[0]);
×
1102

UNCOV
1103
  if (pRanges) {
×
UNCOV
1104
    pHash = tsdbFSetRangeArrayToHash(pRanges);
×
UNCOV
1105
    if (pHash == NULL) {
×
1106
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1107
      goto _out;
×
1108
    }
1109
  }
1110

UNCOV
1111
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
×
UNCOV
1112
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
UNCOV
1113
    int64_t ever = VERSION_MAX;
×
UNCOV
1114
    if (pHash) {
×
UNCOV
1115
      int32_t         fid = fset->fid;
×
UNCOV
1116
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
×
UNCOV
1117
      if (u) {
×
UNCOV
1118
        ever = u->sver - 1;
×
1119
      }
1120
    }
1121

UNCOV
1122
    code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
×
UNCOV
1123
    if (code) break;
×
1124

UNCOV
1125
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
UNCOV
1126
    if (code) break;
×
1127
  }
UNCOV
1128
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
×
1129

UNCOV
1130
_out:
×
UNCOV
1131
  if (code) {
×
1132
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1133
    taosMemoryFree(fsetArr[0]);
×
1134
    fsetArr[0] = NULL;
×
1135
  }
UNCOV
1136
  if (pHash) {
×
UNCOV
1137
    taosHashCleanup(pHash);
×
UNCOV
1138
    pHash = NULL;
×
1139
  }
UNCOV
1140
  return code;
×
1141
}
1142

UNCOV
1143
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
×
1144

UNCOV
1145
int32_t tsdbFSCreateRefRangedSnapshot(STFileSystem *fs, int64_t sver, int64_t ever, TFileSetRangeArray *pRanges,
×
1146
                                      TFileSetRangeArray **fsrArr) {
UNCOV
1147
  int32_t         code = 0;
×
1148
  STFileSet      *fset;
UNCOV
1149
  STFileSetRange *fsr1 = NULL;
×
UNCOV
1150
  SHashObj       *pHash = NULL;
×
1151

UNCOV
1152
  fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
×
UNCOV
1153
  if (fsrArr[0] == NULL) {
×
1154
    code = terrno;
×
1155
    goto _out;
×
1156
  }
1157

UNCOV
1158
  tsdbInfo("pRanges size:%d", (pRanges == NULL ? 0 : TARRAY2_SIZE(pRanges)));
×
UNCOV
1159
  if (pRanges) {
×
UNCOV
1160
    pHash = tsdbFSetRangeArrayToHash(pRanges);
×
UNCOV
1161
    if (pHash == NULL) {
×
1162
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1163
      goto _out;
×
1164
    }
1165
  }
1166

UNCOV
1167
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
×
UNCOV
1168
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
UNCOV
1169
    int64_t sver1 = sver;
×
UNCOV
1170
    int64_t ever1 = ever;
×
1171

UNCOV
1172
    if (pHash) {
×
UNCOV
1173
      int32_t         fid = fset->fid;
×
UNCOV
1174
      STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
×
UNCOV
1175
      if (u) {
×
UNCOV
1176
        sver1 = u->sver;
×
UNCOV
1177
        tsdbDebug("range hash get fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
×
1178
      }
1179
    }
1180

UNCOV
1181
    if (sver1 > ever1) {
×
1182
      tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
×
1183
      continue;
×
1184
    }
1185

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

UNCOV
1188
    code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
×
UNCOV
1189
    if (code) break;
×
1190

UNCOV
1191
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
×
UNCOV
1192
    if (code) break;
×
1193

UNCOV
1194
    fsr1 = NULL;
×
1195
  }
UNCOV
1196
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
×
1197

UNCOV
1198
  if (code) {
×
1199
    tsdbTFileSetRangeClear(&fsr1);
×
1200
    TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
×
1201
    fsrArr[0] = NULL;
×
1202
  }
1203

UNCOV
1204
_out:
×
UNCOV
1205
  if (pHash) {
×
UNCOV
1206
    taosHashCleanup(pHash);
×
UNCOV
1207
    pHash = NULL;
×
1208
  }
UNCOV
1209
  return code;
×
1210
}
1211

UNCOV
1212
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
×
1213

1214
void tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task, STFileSet **fset) {
139,556✔
1215
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1216
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
139,556✔
1217

1218
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
139,556✔
1219
  if (*fset == NULL) {
139,557✔
1220
    return;
128,494✔
1221
  }
1222

1223
  struct STFileSetCond *cond = NULL;
11,063✔
1224
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
11,063✔
1225
    cond = &(*fset)->conds[0];
6,076✔
1226
  } else {
1227
    cond = &(*fset)->conds[1];
4,987✔
1228
  }
1229

1230
  while (1) {
1231
    if (cond->running) {
12,041✔
1232
      cond->numWait++;
978✔
1233
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
978✔
1234
      cond->numWait--;
978✔
1235
    } else {
1236
      cond->running = true;
11,063✔
1237
      break;
11,063✔
1238
    }
1239
  }
1240

1241
  tsdbInfo("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
11,063!
1242
  return;
11,063✔
1243
}
1244

1245
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
11,063✔
1246
  // Here, sttTrigger is protected by tsdb->mutex, so it is safe to read it without lock
1247
  int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
11,063✔
1248

1249
  STFileSet *fset = NULL;
11,063✔
1250
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
11,063✔
1251
  if (fset == NULL) {
11,063!
1252
    return;
×
1253
  }
1254

1255
  struct STFileSetCond *cond = NULL;
11,063✔
1256
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
11,063✔
1257
    cond = &fset->conds[0];
6,076✔
1258
  } else {
1259
    cond = &fset->conds[1];
4,987✔
1260
  }
1261

1262
  cond->running = false;
11,063✔
1263
  if (cond->numWait > 0) {
11,063✔
1264
    (void)taosThreadCondSignal(&cond->cond);
978✔
1265
  }
1266

1267
  tsdbInfo("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
11,063!
1268
  return;
11,063✔
1269
}
1270

1271
struct SFileSetReader {
1272
  STsdb     *pTsdb;
1273
  STFileSet *pFileSet;
1274
  int32_t    fid;
1275
  int64_t    startTime;
1276
  int64_t    endTime;
1277
  int64_t    lastCompactTime;
1278
  int64_t    totalSize;
1279
};
1280

1281
int32_t tsdbFileSetReaderOpen(void *pVnode, struct SFileSetReader **ppReader) {
×
1282
  if (pVnode == NULL || ppReader == NULL) {
×
1283
    return TSDB_CODE_INVALID_PARA;
×
1284
  }
1285

1286
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
×
1287

1288
  (*ppReader) = taosMemoryCalloc(1, sizeof(struct SFileSetReader));
×
1289
  if (*ppReader == NULL) {
×
1290
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, __LINE__,
×
1291
              tstrerror(terrno));
1292
    return terrno;
×
1293
  }
1294

1295
  (*ppReader)->pTsdb = pTsdb;
×
1296
  (*ppReader)->fid = INT32_MIN;
×
1297
  (*ppReader)->pFileSet = NULL;
×
1298

1299
  return TSDB_CODE_SUCCESS;
×
1300
}
1301

1302
extern bool tsdbShouldCompact(const STFileSet *pFileSet);
1303

1304
#ifndef TD_ENTERPRISE
1305
bool tsdbShouldCompact(const STFileSet *pFileSet) { return false; }
1306
#endif
1307

1308
static int32_t tsdbFileSetReaderNextNoLock(struct SFileSetReader *pReader) {
×
1309
  STsdb  *pTsdb = pReader->pTsdb;
×
1310
  int32_t code = TSDB_CODE_SUCCESS;
×
1311

1312
  tsdbTFileSetClear(&pReader->pFileSet);
×
1313

1314
  STFileSet *fset = &(STFileSet){
×
1315
      .fid = pReader->fid,
×
1316
  };
1317

1318
  STFileSet **fsetPtr = TARRAY2_SEARCH(pReader->pTsdb->pFS->fSetArr, &fset, tsdbTFileSetCmprFn, TD_GT);
×
1319
  if (fsetPtr == NULL) {
×
1320
    pReader->fid = INT32_MAX;
×
1321
    return TSDB_CODE_NOT_FOUND;
×
1322
  }
1323

1324
  // ref file set
1325
  code = tsdbTFileSetInitRef(pReader->pTsdb, *fsetPtr, &pReader->pFileSet);
×
1326
  if (code) return code;
×
1327

1328
  // get file set details
1329
  pReader->fid = pReader->pFileSet->fid;
×
1330
  tsdbFidKeyRange(pReader->fid, pTsdb->keepCfg.days, pTsdb->keepCfg.precision, &pReader->startTime, &pReader->endTime);
×
NEW
1331
  pReader->lastCompactTime = pReader->pFileSet->lastCompact;
×
1332
  pReader->totalSize = 0;
×
1333
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
×
1334
    STFileObj *fobj = pReader->pFileSet->farr[i];
×
1335
    if (fobj) {
×
1336
      pReader->totalSize += fobj->f->size;
×
1337
    }
1338
  }
1339
  SSttLvl *lvl;
1340
  TARRAY2_FOREACH(pReader->pFileSet->lvlArr, lvl) {
×
1341
    STFileObj *fobj;
1342
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { pReader->totalSize += fobj->f->size; }
×
1343
  }
1344

1345
  return code;
×
1346
}
1347

1348
int32_t tsdbFileSetReaderNext(struct SFileSetReader *pReader) {
×
1349
  int32_t code = TSDB_CODE_SUCCESS;
×
1350
  (void)taosThreadMutexLock(&pReader->pTsdb->mutex);
×
1351
  code = tsdbFileSetReaderNextNoLock(pReader);
×
1352
  (void)taosThreadMutexUnlock(&pReader->pTsdb->mutex);
×
1353
  return code;
×
1354
}
1355

1356
int32_t tsdbFileSetGetEntryField(struct SFileSetReader *pReader, const char *field, void *value) {
×
1357
  const char *fieldName;
1358

1359
  if (pReader->fid == INT32_MIN || pReader->fid == INT32_MAX) {
×
1360
    return TSDB_CODE_INVALID_PARA;
×
1361
  }
1362

1363
  fieldName = "fileset_id";
×
1364
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1365
    *(int32_t *)value = pReader->fid;
×
1366
    return TSDB_CODE_SUCCESS;
×
1367
  }
1368

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

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

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

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

1393
  fieldName = "should_compact";
×
1394
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
NEW
1395
    *(char *)value = tsdbShouldCompact(pReader->pFileSet);
×
1396
    return TSDB_CODE_SUCCESS;
×
1397
  }
1398

1399
  fieldName = "details";
×
1400
  if (strncmp(field, fieldName, strlen(fieldName) + 1) == 0) {
×
1401
    // TODO
1402
    return TSDB_CODE_SUCCESS;
×
1403
  }
1404

1405
  return TSDB_CODE_INVALID_PARA;
×
1406
}
1407

1408
void tsdbFileSetReaderClose(struct SFileSetReader **ppReader) {
×
1409
  if (ppReader == NULL || *ppReader == NULL) {
×
1410
    return;
×
1411
  }
1412

1413
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
×
1414
  taosMemoryFree(*ppReader);
×
1415

1416
  *ppReader = NULL;
×
1417
  return;
×
1418
}
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