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

taosdata / TDengine / #3582

16 Jan 2025 05:41AM UTC coverage: 63.803% (-0.006%) from 63.809%
#3582

push

travis-ci

web-flow
Merge pull request #29579 from taosdata/test/3.0/TS-5873

Test(wal): add case for remove wal.

141331 of 284535 branches covered (49.67%)

Branch coverage included in aggregate %.

219941 of 281694 relevant lines covered (78.08%)

19199606.59 hits per line

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

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

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

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

58
  return 0;
12,158✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169
  // fset
170
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
24,634✔
171
  if (!ajson) {
24,633!
172
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
173
  }
174
  const STFileSet *fset;
175
  TARRAY2_FOREACH(arr, fset) {
182,674✔
176
    cJSON *item = cJSON_CreateObject();
158,042✔
177
    if (!item) {
158,056!
178
      TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
×
179
    }
180
    (void)cJSON_AddItemToArray(ajson, item);
158,056✔
181

182
    code = tsdbTFileSetToJson(fset, item);
158,049✔
183
    TSDB_CHECK_CODE(code, lino, _exit);
158,041!
184
  }
185

186
  code = save_json(json, fname);
24,632✔
187
  TSDB_CHECK_CODE(code, lino, _exit);
24,635!
188

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

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

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

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

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

211
  /* fmtv */
212
  item1 = cJSON_GetObjectItem(json, "fmtv");
2,378✔
213
  if (cJSON_IsNumber(item1)) {
2,376!
214
    if (item1->valuedouble != 1) {
2,370!
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,370✔
223
  if (cJSON_IsArray(item1)) {
2,380!
224
    const cJSON *item2;
225
    cJSON_ArrayForEach(item2, item1) {
4,008!
226
      STFileSet *fset;
227
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
1,625✔
228
      TSDB_CHECK_CODE(code, lino, _exit);
1,629!
229

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

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

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

256
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
172,778✔
257
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
157,974✔
258
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
157,974!
259

260
    if (fset1 && fset2) {
157,974!
261
      if (fset1->fid < fset2->fid) {
28,597!
262
        // delete fset1
263
        tsdbTFileSetRemove(fset1);
×
264
        i1++;
×
265
      } else if (fset1->fid > fset2->fid) {
28,597✔
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,571✔
276
        TSDB_CHECK_CODE(code, lino, _exit);
28,571!
277
        i1++;
28,571✔
278
        i2++;
28,571✔
279
      }
280
    } else if (fset1) {
129,377!
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);
129,377✔
287
      TSDB_CHECK_CODE(code, lino, _exit);
129,437!
288
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
129,336✔
289
      TSDB_CHECK_CODE(code, lino, _exit);
129,336!
290
      i1++;
129,336✔
291
      i2++;
129,336✔
292
    }
293
  }
294

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

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

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

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

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

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

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

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

399
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
5,034✔
400

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

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

407
  return 0;
5,035✔
408
}
409

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

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

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

428
  // other
429
  STFileSet *fset = NULL;
2,380✔
430
  TARRAY2_FOREACH(fs->fSetArr, fset) {
4,008✔
431
    // data file
432
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
8,145✔
433
      if (fset->farr[i] != NULL) {
6,516✔
434
        code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
1,026✔
435
        TSDB_CHECK_CODE(code, lino, _exit);
1,026!
436

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

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

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

476
  STFileHashEntry *entry = hash->buckets[idx];
5,036✔
477
  while (entry) {
5,039✔
478
    if (strcmp(entry->fname, fname) == 0) {
5,037✔
479
      return entry;
5,034✔
480
    }
481
    entry = entry->next;
3✔
482
  }
483

484
  return NULL;
2✔
485
}
486

487
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,380✔
488
  for (int32_t i = 0; i < hash->numBucket; i++) {
9,606,214✔
489
    STFileHashEntry *entry = hash->buckets[i];
9,603,834✔
490
    while (entry) {
9,608,869✔
491
      STFileHashEntry *next = entry->next;
5,035✔
492
      taosMemoryFree(entry);
5,035!
493
      entry = next;
5,035✔
494
    }
495
  }
496
  taosMemoryFree(hash->buckets);
2,380!
497
  memset(hash, 0, sizeof(*hash));
2,380✔
498
}
2,380✔
499

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

609
  return 0;
17,222✔
610
}
611

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

625
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
12,169✔
626
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
2,380✔
627
    TSDB_CHECK_CODE(code, lino, _exit);
2,375!
628

629
    if (taosCheckExistFile(cCurrent)) {
2,375!
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,378!
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,379✔
651
    TSDB_CHECK_CODE(code, lino, _exit);
2,378!
652

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

660
_exit:
9,789✔
661
  if (code) {
12,165!
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__);
12,165✔
665
  }
666
  return code;
12,170✔
667
}
668

669
static void close_file_system(STFileSystem *fs) {
12,170✔
670
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
143,294✔
671
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
143,293✔
672
}
12,168✔
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) {
14,845✔
684
  int32_t code = 0;
14,845✔
685
  int32_t lino = 0;
14,845✔
686

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

690
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
14,845✔
691
  STFileSet      *fset = NULL;
14,845✔
692
  const STFileOp *op;
693
  int32_t         fid = INT32_MIN;
14,845✔
694
  TSKEY           now = taosGetTimestampMs();
14,845✔
695
  TARRAY2_FOREACH_PTR(opArray, op) {
180,725✔
696
    if (!fset || fset->fid != op->fid) {
165,878✔
697
      STFileSet tfset = {.fid = op->fid};
143,472✔
698
      fset = &tfset;
143,472✔
699
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
143,428✔
700
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
143,428✔
701

702
      if (!fset) {
143,428✔
703
        code = tsdbTFileSetInit(op->fid, &fset);
129,448✔
704
        TSDB_CHECK_CODE(code, lino, _exit);
129,373!
705

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

711
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
165,760✔
712
    TSDB_CHECK_CODE(code, lino, _exit);
165,880!
713

714
    if (fid != op->fid) {
165,880✔
715
      fid = op->fid;
143,474✔
716
      if (etype == TSDB_FEDIT_COMMIT) {
143,474✔
717
        fset->lastCommit = now;
138,588✔
718
      } else if (etype == TSDB_FEDIT_COMPACT) {
4,886✔
719
        fset->lastCompact = now;
95✔
720
      }
721
    }
722
  }
723

724
  // remove empty empty stt level and empty file set
725
  int32_t i = 0;
14,847✔
726
  while (i < TARRAY2_SIZE(fsetArray)) {
172,917✔
727
    fset = TARRAY2_GET(fsetArray, i);
158,070✔
728

729
    SSttLvl *lvl;
730
    int32_t  j = 0;
158,070✔
731
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
328,315✔
732
      lvl = TARRAY2_GET(fset->lvlArr, j);
170,245✔
733

734
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
170,245✔
735
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
5,689!
736
      } else {
737
        j++;
164,556✔
738
      }
739
    }
740

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

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

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

760
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
12,122✔
761
  TSDB_CHECK_CODE(code, lino, _exit);
12,155!
762

763
  code = create_fs(pTsdb, fs);
12,155✔
764
  TSDB_CHECK_CODE(code, lino, _exit);
12,165!
765

766
  code = open_fs(fs[0], rollback);
12,165✔
767
  TSDB_CHECK_CODE(code, lino, _exit);
12,170!
768

769
_exit:
12,170✔
770
  if (code) {
12,170!
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__);
12,170✔
775
  }
776
  return code;
12,170✔
777
}
778

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

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

789
  (void)taosThreadMutexLock(&pTsdb->mutex);
12,243✔
790

791
  // disable
792
  pTsdb->bgTaskDisabled = true;
12,243✔
793

794
  // collect channel
795
  STFileSet *fset;
796
  TARRAY2_FOREACH(fs->fSetArr, fset) {
143,365✔
797
    if (taosArrayPush(asyncTasks, &fset->mergeTask) == NULL       //
262,246!
798
        || taosArrayPush(asyncTasks, &fset->compactTask) == NULL  //
262,245!
799
        || taosArrayPush(asyncTasks, &fset->retentionTask) == NULL) {
262,244!
800
      taosArrayDestroy(asyncTasks);
×
801
      (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
802
      return terrno;
×
803
    }
804
    tsdbFSSetBlockCommit(fset, false);
131,122✔
805
  }
806

807
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
12,242✔
808

809
  // destroy all channels
810
  for (int32_t k = 0; k < 2; k++) {
36,728✔
811
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
811,148✔
812
      SVATaskID *task = taosArrayGet(asyncTasks, i);
786,658✔
813
      if (k == 0) {
786,664✔
814
        (void)vnodeACancel(task);
393,350✔
815
      } else {
816
        vnodeAWait(task);
393,314✔
817
      }
818
    }
819
  }
820
  taosArrayDestroy(asyncTasks);
12,242✔
821

822
#ifdef TD_ENTERPRISE
823
  tsdbStopAllCompTask(pTsdb);
12,243✔
824
#endif
825
  return 0;
12,243✔
826
}
827

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

834
void tsdbCloseFS(STFileSystem **fs) {
12,170✔
835
  if (fs[0] == NULL) return;
12,170!
836

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

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

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

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

865
  if (etype == TSDB_FEDIT_COMMIT) {
14,845✔
866
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,960✔
867
  } else {
868
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
4,885✔
869
  }
870

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

876
  // edit
877
  code = edit_fs(fs, opArray, etype);
14,845✔
878
  TSDB_CHECK_CODE(code, lino, _exit);
14,844!
879

880
  // save fs
881
  code = save_fs(fs->fSetArrTmp, current_t);
14,844✔
882
  TSDB_CHECK_CODE(code, lino, _exit);
14,845!
883

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

894
static void tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
283,591✔
895
  if (block) {
283,591✔
896
    fset->blockCommit = true;
5✔
897
  } else {
898
    fset->blockCommit = false;
283,586✔
899
    if (fset->numWaitCommit > 0) {
283,586✔
900
      (void)taosThreadCondSignal(&fset->canCommit);
3✔
901
    }
902
  }
903
}
283,591✔
904

905
void tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
138,520✔
906
  (void)taosThreadMutexLock(&tsdb->mutex);
138,520✔
907
  STFileSet *fset;
908
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
138,541✔
909
  if (fset) {
138,491✔
910
    while (fset->blockCommit) {
9,106✔
911
      fset->numWaitCommit++;
3✔
912
      (void)taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
3✔
913
      fset->numWaitCommit--;
3✔
914
    }
915
  }
916
  (void)taosThreadMutexUnlock(&tsdb->mutex);
138,491✔
917
  return;
138,526✔
918
}
919

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

925
  // commit
926
  code = commit_edit(fs);
14,845✔
927
  TSDB_CHECK_CODE(code, lino, _exit);
14,845!
928

929
  // schedule merge
930
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
14,845✔
931
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
14,845✔
932
    STFileSet *fset;
933
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
167,001✔
934
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
152,780✔
935
        tsdbFSSetBlockCommit(fset, false);
2,685✔
936
        continue;
2,685✔
937
      }
938

939
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
150,095✔
940
      if (lvl->level != 0) {
150,095✔
941
        tsdbFSSetBlockCommit(fset, false);
6,428✔
942
        continue;
6,428✔
943
      }
944

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

954
        arg->tsdb = fs->tsdb;
4,791✔
955
        arg->fid = fset->fid;
4,791✔
956

957
        code = vnodeAsync(MERGE_TASK_ASYNC, EVA_PRIORITY_HIGH, tsdbMerge, taosAutoMemoryFree, arg, &fset->mergeTask);
4,791✔
958
        TSDB_CHECK_CODE(code, lino, _exit);
4,791!
959
      }
960

961
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
143,667✔
962
        tsdbFSSetBlockCommit(fset, true);
5✔
963
      } else {
964
        tsdbFSSetBlockCommit(fset, false);
143,662✔
965
      }
966
    }
967
  }
968

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

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

989
void tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
296,498✔
990
  STFileSet   tfset = {.fid = fid};
296,498✔
991
  STFileSet  *pset = &tfset;
296,498✔
992
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
296,498✔
993
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
296,495✔
994
}
296,495✔
995

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

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

1004
  TARRAY2_INIT(fsetArr[0]);
70✔
1005

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

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

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

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

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

1039
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
4,706,034✔
1040
  int32_t    code = 0;
4,706,034✔
1041
  STFileSet *fset, *fset1;
1042

1043
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
4,706,034!
1044
  if (fsetArr[0] == NULL) return terrno;
4,709,988!
1045

1046
  TARRAY2_FOREACH(fs->fSetArr, fset) {
13,472,882✔
1047
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
8,762,184✔
1048
    if (code) break;
8,763,212!
1049

1050
    code = TARRAY2_APPEND(fsetArr[0], fset1);
8,763,212✔
1051
    if (code) {
8,762,894!
1052
      tsdbTFileSetClear(&fset1);
×
1053
      break;
×
1054
    }
1055
  }
1056

1057
  if (code) {
4,710,698!
1058
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1059
    taosMemoryFree(fsetArr[0]);
×
1060
    fsetArr[0] = NULL;
×
1061
  }
1062
  return code;
4,710,698✔
1063
}
1064

1065
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
4,710,107✔
1066
  if (fsetArr[0]) {
4,710,107!
1067
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
13,479,882!
1068
    taosMemoryFreeClear(fsetArr[0]);
4,710,777!
1069
    fsetArr[0] = NULL;
4,711,272✔
1070
  }
1071
}
4,710,680✔
1072

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

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

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

1097
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
×
1098
  if (fsetArr == NULL) return terrno;
×
1099
  TARRAY2_INIT(fsetArr[0]);
×
1100

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

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

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

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

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

1141
void tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { tsdbFSDestroyCopySnapshot(fsetArr); }
×
1142

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

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

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

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

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

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

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

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

1189
    code = TARRAY2_APPEND(fsrArr[0], fsr1);
×
1190
    if (code) break;
×
1191

1192
    fsr1 = NULL;
×
1193
  }
1194
  (void)taosThreadMutexUnlock(&fs->tsdb->mutex);
×
1195

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

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

1210
void tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { tsdbTFileSetRangeArrayDestroy(fsrArr); }
×
1211

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

1216
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
143,728✔
1217
  if (*fset == NULL) {
143,727✔
1218
    return;
129,447✔
1219
  }
1220

1221
  struct STFileSetCond *cond = NULL;
14,280✔
1222
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
14,280✔
1223
    cond = &(*fset)->conds[0];
9,103✔
1224
  } else {
1225
    cond = &(*fset)->conds[1];
5,177✔
1226
  }
1227

1228
  while (1) {
1229
    if (cond->running) {
14,280!
1230
      cond->numWait++;
×
1231
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
×
1232
      cond->numWait--;
×
1233
    } else {
1234
      cond->running = true;
14,280✔
1235
      break;
14,280✔
1236
    }
1237
  }
1238

1239
  tsdbInfo("vgId:%d begin %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
14,280!
1240
  return;
14,281✔
1241
}
1242

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

1247
  STFileSet *fset = NULL;
14,281✔
1248
  tsdbFSGetFSet(tsdb->pFS, fid, &fset);
14,281✔
1249
  if (fset == NULL) {
14,281!
1250
    return;
×
1251
  }
1252

1253
  struct STFileSetCond *cond = NULL;
14,281✔
1254
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
14,281✔
1255
    cond = &fset->conds[0];
9,103✔
1256
  } else {
1257
    cond = &fset->conds[1];
5,178✔
1258
  }
1259

1260
  cond->running = false;
14,281✔
1261
  if (cond->numWait > 0) {
14,281!
1262
    (void)taosThreadCondSignal(&cond->cond);
×
1263
  }
1264

1265
  tsdbInfo("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
14,281!
1266
  return;
14,281✔
1267
}
1268

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

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

1284
  STsdb *pTsdb = ((SVnode *)pVnode)->pTsdb;
×
1285

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

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

1297
  return TSDB_CODE_SUCCESS;
×
1298
}
1299

1300
extern bool tsdbShouldCompact(const STFileSet *pFileSet);
1301

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

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

1310
  tsdbTFileSetClear(&pReader->pFileSet);
×
1311

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

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

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

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

1343
  return code;
×
1344
}
1345

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

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

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

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

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

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

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

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

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

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

1403
  return TSDB_CODE_INVALID_PARA;
×
1404
}
1405

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

1411
  tsdbTFileSetClear(&(*ppReader)->pFileSet);
×
1412
  taosMemoryFree(*ppReader);
×
1413

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