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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

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

0.0
/source/dnode/vnode/src/tsdb/tsdbFS2.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdbFS2.h"
17
#include "cos.h"
18
#include "tsdbUpgrade.h"
19
#include "vnd.h"
20

21
#define BLOCK_COMMIT_FACTOR 3
22

23
typedef struct STFileHashEntry {
24
  struct STFileHashEntry *next;
25
  char                    fname[TSDB_FILENAME_LEN];
26
} STFileHashEntry;
27

28
typedef struct {
29
  int32_t           numFile;
30
  int32_t           numBucket;
31
  STFileHashEntry **buckets;
32
} STFileHash;
33

34
static const char *gCurrentFname[] = {
35
    [TSDB_FCURRENT] = "current.json",
36
    [TSDB_FCURRENT_C] = "current.c.json",
37
    [TSDB_FCURRENT_M] = "current.m.json",
38
};
39

40
static int32_t create_fs(STsdb *pTsdb, STFileSystem **fs) {
×
41
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
×
42
  if (fs[0] == NULL) {
×
43
    return terrno;
×
44
  }
45

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

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

58
  return 0;
×
59
}
60

61
static void destroy_fs(STFileSystem **fs) {
×
62
  if (fs[0] == NULL) return;
×
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

182
    code = tsdbTFileSetToJson(fset, item);
×
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
  code = save_json(json, fname);
×
187
  TSDB_CHECK_CODE(code, lino, _exit);
×
188

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

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

201
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
×
202

203
  // load json
204
  cJSON *json = NULL;
×
205
  code = load_json(fname, &json);
×
206
  TSDB_CHECK_CODE(code, lino, _exit);
×
207

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

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

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

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

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

256
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
×
257
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
×
258
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
×
259

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

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

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

306
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
×
307
  if (fs->etype == TSDB_FEDIT_COMMIT) {
×
308
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
×
309
  } else {
310
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
×
311
  }
312

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

317
  code = apply_commit(fs);
×
318
  TSDB_CHECK_CODE(code, lino, _exit);
×
319

320
_exit:
×
321
  if (code) {
×
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);
×
326
  }
327
  return code;
×
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) {
×
364
  int32_t code = 0;
×
365
  int32_t lino = 0;
×
366

367
  // check file existence
368
  if (!taosCheckExistFile(fobj->fname)) {
×
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;
×
391
}
392

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

399
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
×
400

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

403
  entry->next = hash->buckets[idx];
×
404
  hash->buckets[idx] = entry;
×
405
  hash->numFile++;
×
406

407
  return 0;
×
408
}
409

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

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

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

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

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

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

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

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

484
  return NULL;
×
485
}
486

487
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
×
488
  for (int32_t i = 0; i < hash->numBucket; i++) {
×
489
    STFileHashEntry *entry = hash->buckets[i];
×
490
    while (entry) {
×
491
      STFileHashEntry *next = entry->next;
×
492
      taosMemoryFree(entry);
×
493
      entry = next;
×
494
    }
495
  }
496
  taosMemoryFree(hash->buckets);
×
497
  memset(hash, 0, sizeof(*hash));
×
498
}
×
499

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

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

546
    STFileHash fobjHash = {0};
×
547
    code = tsdbFSCreateFileObjHash(fs, &fobjHash);
×
548
    if (code) goto _close_dir;
×
549

550
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
×
551
      if (taosIsDir(file->aname)) continue;
×
552

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

558
    tsdbFSDestroyFileObjHash(&fobjHash);
×
559

560
  _close_dir:
×
561
    tfsClosedir(dir);
×
562
  }
563

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

571
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
×
572
  fs->neid = 0;
×
573

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

578
  // scan and fix
579
  int32_t code = 0;
×
580
  int32_t lino = 0;
×
581

582
  code = tsdbFSDoSanAndFix(fs);
×
583
  TSDB_CHECK_CODE(code, lino, _exit);
×
584

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

592
static int32_t tsdbFSDupState(STFileSystem *fs) {
×
593
  int32_t code;
594

595
  const TFileSetArray *src = fs->fSetArr;
×
596
  TFileSetArray       *dst = fs->fSetArrTmp;
×
597

598
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
×
599

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

609
  return 0;
×
610
}
611

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

625
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
×
626
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
×
627
    TSDB_CHECK_CODE(code, lino, _exit);
×
628

629
    if (taosCheckExistFile(cCurrent)) {
×
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)) {
×
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);
×
651
    TSDB_CHECK_CODE(code, lino, _exit);
×
652

653
    code = tsdbFSScanAndFix(fs);
×
654
    TSDB_CHECK_CODE(code, lino, _exit);
×
655
  } else {
656
    code = save_fs(fs->fSetArr, fCurrent);
×
657
    TSDB_CHECK_CODE(code, lino, _exit);
×
658
  }
659

660
_exit:
×
661
  if (code) {
×
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__);
×
665
  }
666
  return code;
×
667
}
668

669
static void close_file_system(STFileSystem *fs) {
×
670
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
×
671
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
×
672
}
×
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) {
×
684
  int32_t code = 0;
×
685
  int32_t lino = 0;
×
686

687
  code = tsdbFSDupState(fs);
×
688
  if (code) return code;
×
689

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

702
      if (!fset) {
×
703
        code = tsdbTFileSetInit(op->fid, &fset);
×
704
        TSDB_CHECK_CODE(code, lino, _exit);
×
705

706
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
×
707
        TSDB_CHECK_CODE(code, lino, _exit);
×
708
      }
709
    }
710

711
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
×
712
    TSDB_CHECK_CODE(code, lino, _exit);
×
713

714
    if (fid != op->fid) {
×
715
      fid = op->fid;
×
716
      if (etype == TSDB_FEDIT_COMMIT) {
×
717
        fset->lastCommit = now;
×
718
      } else if (etype == TSDB_FEDIT_COMPACT) {
×
719
        fset->lastCompact = now;
×
720
      }
721
    }
722
  }
723

724
  // remove empty empty stt level and empty file set
725
  int32_t i = 0;
×
726
  while (i < TARRAY2_SIZE(fsetArray)) {
×
727
    fset = TARRAY2_GET(fsetArray, i);
×
728

729
    SSttLvl *lvl;
730
    int32_t  j = 0;
×
731
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
×
732
      lvl = TARRAY2_GET(fset->lvlArr, j);
×
733

734
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
×
735
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
×
736
      } else {
737
        j++;
×
738
      }
739
    }
740

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

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

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

760
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
×
761
  TSDB_CHECK_CODE(code, lino, _exit);
×
762

763
  code = create_fs(pTsdb, fs);
×
764
  TSDB_CHECK_CODE(code, lino, _exit);
×
765

766
  code = open_fs(fs[0], rollback);
×
767
  TSDB_CHECK_CODE(code, lino, _exit);
×
768

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

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

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

789
  (void)taosThreadMutexLock(&pTsdb->mutex);
×
790

791
  // disable
792
  pTsdb->bgTaskDisabled = true;
×
793

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

807
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
808

809
  // destroy all channels
810
  for (int32_t k = 0; k < 2; k++) {
×
811
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
×
812
      SVATaskID *task = taosArrayGet(asyncTasks, i);
×
813
      if (k == 0) {
×
814
        (void)vnodeACancel(task);
×
815
      } else {
816
        vnodeAWait(task);
×
817
      }
818
    }
819
  }
820
  taosArrayDestroy(asyncTasks);
×
821

822
#ifdef TD_ENTERPRISE
823
  tsdbStopAllCompTask(pTsdb);
×
824
#endif
825
  return 0;
×
826
}
827

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

834
void tsdbCloseFS(STFileSystem **fs) {
×
835
  if (fs[0] == NULL) return;
×
836

837
  int32_t code = tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
×
838
  if (code) {
×
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]);
×
843
  destroy_fs(fs);
×
844
  return;
×
845
}
846

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

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

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

865
  if (etype == TSDB_FEDIT_COMMIT) {
×
866
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
×
867
  } else {
868
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
×
869
  }
870

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

876
  // edit
877
  code = edit_fs(fs, opArray, etype);
×
878
  TSDB_CHECK_CODE(code, lino, _exit);
×
879

880
  // save fs
881
  code = save_fs(fs->fSetArrTmp, current_t);
×
882
  TSDB_CHECK_CODE(code, lino, _exit);
×
883

884
_exit:
×
885
  if (code) {
×
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);
×
890
  }
891
  return code;
×
892
}
893

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

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

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

925
  // commit
926
  code = commit_edit(fs);
×
927
  TSDB_CHECK_CODE(code, lino, _exit);
×
928

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

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

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

954
        arg->tsdb = fs->tsdb;
×
955
        arg->fid = fset->fid;
×
956

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

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

969
_exit:
×
970
  if (code) {
×
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);
×
974
  }
975
  if (tsem_post(&fs->canEdit) != 0) {
×
976
    tsdbError("vgId:%d failed to post semaphore", TD_VID(fs->tsdb->pVnode));
×
977
  }
978
  return code;
×
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) {
×
990
  STFileSet   tfset = {.fid = fid};
×
991
  STFileSet  *pset = &tfset;
×
992
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
×
993
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
×
994
}
×
995

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

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

1004
  TARRAY2_INIT(fsetArr[0]);
×
1005

1006
  (void)taosThreadMutexLock(&fs->tsdb->mutex);
×
1007
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
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);
×
1015

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

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

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

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

1043
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
×
1044
  if (fsetArr[0] == NULL) return terrno;
×
1045

1046
  TARRAY2_FOREACH(fs->fSetArr, fset) {
×
1047
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
×
1048
    if (code) break;
×
1049

1050
    code = TARRAY2_APPEND(fsetArr[0], fset1);
×
1051
    if (code) {
×
1052
      tsdbTFileSetClear(&fset1);
×
1053
      break;
×
1054
    }
1055
  }
1056

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

1065
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
×
1066
  if (fsetArr[0]) {
×
1067
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
×
1068
    taosMemoryFreeClear(fsetArr[0]);
×
1069
    fsetArr[0] = NULL;
×
1070
  }
1071
}
×
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) {
×
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;
×
1215

1216
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
×
1217
  if (*fset == NULL) {
×
1218
    return;
×
1219
  }
1220

1221
  struct STFileSetCond *cond = NULL;
×
1222
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
×
1223
    cond = &(*fset)->conds[0];
×
1224
  } else {
1225
    cond = &(*fset)->conds[1];
×
1226
  }
1227

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

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

1243
void tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid, EVATaskT task) {
×
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;
×
1246

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

1253
  struct STFileSetCond *cond = NULL;
×
1254
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
×
1255
    cond = &fset->conds[0];
×
1256
  } else {
1257
    cond = &fset->conds[1];
×
1258
  }
1259

1260
  cond->running = false;
×
1261
  if (cond->numWait > 0) {
×
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);
×
1266
  return;
×
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
}
1417

1418
static FORCE_INLINE void getLevelSize(const STFileObj *fObj, int64_t szArr[TFS_MAX_TIERS]) {
1419
  if (fObj == NULL) return;
×
1420

1421
  int64_t sz = fObj->f->size;
×
1422
  // level == 0, primary storage
1423
  // level == 1, second storage,
1424
  // level == 2, third storage
1425
  int32_t level = fObj->f->did.level;
×
1426
  if (level >= 0 && level < TFS_MAX_TIERS) {
×
1427
    szArr[level] += sz;
×
1428
  }
1429
}
1430

1431
static FORCE_INLINE int32_t tsdbGetFsSizeImpl(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
1432
  int32_t code = 0;
×
1433
  int64_t levelSize[TFS_MAX_TIERS] = {0};
×
1434
  int64_t s3Size = 0;
×
1435

1436
  const STFileSet *fset;
1437
  const SSttLvl   *stt = NULL;
×
1438
  const STFileObj *fObj = NULL;
×
1439

1440
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
×
1441
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
×
1442

1443
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
×
1444
    for (int32_t t = TSDB_FTYPE_MIN; t < TSDB_FTYPE_MAX; ++t) {
×
1445
      getLevelSize(fset->farr[t], levelSize);
×
1446
    }
1447

1448
    TARRAY2_FOREACH(fset->lvlArr, stt) {
×
1449
      TARRAY2_FOREACH(stt->fobjArr, fObj) { getLevelSize(fObj, levelSize); }
×
1450
    }
1451

1452
    fObj = fset->farr[TSDB_FTYPE_DATA];
×
1453
    if (fObj) {
×
1454
      int32_t lcn = fObj->f->lcn;
×
1455
      if (lcn > 1) {
×
1456
        s3Size += ((lcn - 1) * chunksize);
×
1457
      }
1458
    }
1459
  }
1460

1461
  pInfo->l1Size = levelSize[0];
×
1462
  pInfo->l2Size = levelSize[1];
×
1463
  pInfo->l3Size = levelSize[2];
×
1464
  pInfo->s3Size = s3Size;
×
1465
  return code;
×
1466
}
1467
int32_t tsdbGetFsSize(STsdb *tsdb, SDbSizeStatisInfo *pInfo) {
×
1468
  int32_t code = 0;
×
1469

1470
  (void)taosThreadMutexLock(&tsdb->mutex);
×
1471
  code = tsdbGetFsSizeImpl(tsdb, pInfo);
×
1472
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
1473
  return code;
×
1474
}
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