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

taosdata / TDengine / #3610

12 Feb 2025 09:54AM UTC coverage: 54.713% (-8.4%) from 63.066%
#3610

push

travis-ci

web-flow
Merge pull request #29745 from taosdata/fix/TD33664-3.0

fix: --version show information check for 3.0

120957 of 286549 branches covered (42.21%)

Branch coverage included in aggregate %.

190849 of 283342 relevant lines covered (67.36%)

4969786.97 hits per line

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

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

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

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

58
  return 0;
6,751✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

182
    code = tsdbTFileSetToJson(fset, item);
134,077✔
183
    TSDB_CHECK_CODE(code, lino, _exit);
134,081!
184
  }
185

186
  code = save_json(json, fname);
12,854✔
187
  TSDB_CHECK_CODE(code, lino, _exit);
12,842!
188

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

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

201
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
926!
202

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

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

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

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

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

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

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

260
    if (fset1 && fset2) {
134,005✔
261
      if (fset1->fid < fset2->fid) {
6,863!
262
        // delete fset1
263
        tsdbTFileSetRemove(fset1);
×
264
        i1++;
×
265
      } else if (fset1->fid > fset2->fid) {
6,863✔
266
        // create new file set with fid of fset2->fid
267
        code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
10✔
268
        TSDB_CHECK_CODE(code, lino, _exit);
10!
269
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
10✔
270
        TSDB_CHECK_CODE(code, lino, _exit);
10!
271
        i1++;
10✔
272
        i2++;
10✔
273
      } else {
274
        // edit
275
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
6,853✔
276
        TSDB_CHECK_CODE(code, lino, _exit);
6,853!
277
        i1++;
6,853✔
278
        i2++;
6,853✔
279
      }
280
    } else if (fset1) {
127,142✔
281
      // delete fset1
282
      tsdbTFileSetRemove(fset1);
11✔
283
      i1++;
11✔
284
    } else {
285
      // create new file set with fid of fset2->fid
286
      code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
127,131✔
287
      TSDB_CHECK_CODE(code, lino, _exit);
127,191!
288
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
127,131✔
289
      TSDB_CHECK_CODE(code, lino, _exit);
127,131!
290
      i1++;
127,131✔
291
      i2++;
127,131✔
292
    }
293
  }
294

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

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

306
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
7,029✔
307
  if (fs->etype == TSDB_FEDIT_COMMIT) {
7,029✔
308
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
5,341✔
309
  } else {
310
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
1,688✔
311
  }
312

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

317
  code = apply_commit(fs);
7,029✔
318
  TSDB_CHECK_CODE(code, lino, _exit);
7,027!
319

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

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

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

399
  tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
2,468✔
400

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

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

407
  return 0;
2,472✔
408
}
409

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

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

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

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

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

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

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

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

484
  return NULL;
2✔
485
}
486

487
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
936✔
488
  for (int32_t i = 0; i < hash->numBucket; i++) {
3,704,045✔
489
    STFileHashEntry *entry = hash->buckets[i];
3,703,109✔
490
    while (entry) {
3,705,582✔
491
      STFileHashEntry *next = entry->next;
2,473✔
492
      taosMemoryFree(entry);
2,473!
493
      entry = next;
2,473✔
494
    }
495
  }
496
  taosMemoryFree(hash->buckets);
936!
497
  memset(hash, 0, sizeof(*hash));
937✔
498
}
937✔
499

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

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

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

550
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
4,343✔
551
      if (taosIsDir(file->aname)) continue;
3,403✔
552

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

558
    tsdbFSDestroyFileObjHash(&fobjHash);
932✔
559

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

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

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

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

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

582
  code = tsdbFSDoSanAndFix(fs);
925✔
583
  TSDB_CHECK_CODE(code, lino, _exit);
934!
584

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

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

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

598
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
14,807✔
599

600
  const STFileSet *fset1;
601
  TARRAY2_FOREACH(src, fset1) {
15,831✔
602
    STFileSet *fset2;
603
    code = tsdbTFileSetInitCopy(fs->tsdb, fset1, &fset2);
7,870✔
604
    if (code) return code;
7,872!
605
    code = TARRAY2_APPEND(dst, fset2);
7,872✔
606
    if (code) return code;
7,873!
607
  }
608

609
  return 0;
7,961✔
610
}
611

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

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

629
    if (taosCheckExistFile(cCurrent)) {
927!
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)) {
931!
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);
937✔
651
    TSDB_CHECK_CODE(code, lino, _exit);
923!
652

653
    code = tsdbFSScanAndFix(fs);
923✔
654
    TSDB_CHECK_CODE(code, lino, _exit);
934!
655
  } else {
656
    code = save_fs(fs->fSetArr, fCurrent);
5,830✔
657
    TSDB_CHECK_CODE(code, lino, _exit);
5,817!
658
  }
659

660
_exit:
5,817✔
661
  if (code) {
6,751!
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__);
6,751!
665
  }
666
  return code;
6,773✔
667
}
668

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

687
  code = tsdbFSDupState(fs);
7,023✔
688
  if (code) return code;
7,022!
689

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

702
      if (!fset) {
132,316✔
703
        code = tsdbTFileSetInit(op->fid, &fset);
127,167✔
704
        TSDB_CHECK_CODE(code, lino, _exit);
127,184!
705

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

711
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
140,431✔
712
    TSDB_CHECK_CODE(code, lino, _exit);
140,467!
713

714
    if (fid != op->fid) {
140,467✔
715
      fid = op->fid;
132,360✔
716
      if (etype == TSDB_FEDIT_COMMIT) {
132,360✔
717
        fset->lastCommit = now;
130,673✔
718
      } else if (etype == TSDB_FEDIT_COMPACT) {
1,687✔
719
        fset->lastCompact = now;
26✔
720
      }
721
    }
722
  }
723

724
  // remove empty empty stt level and empty file set
725
  int32_t i = 0;
7,021✔
726
  while (i < TARRAY2_SIZE(fsetArray)) {
141,116✔
727
    fset = TARRAY2_GET(fsetArray, i);
134,094✔
728

729
    SSttLvl *lvl;
730
    int32_t  j = 0;
134,094✔
731
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
272,696✔
732
      lvl = TARRAY2_GET(fset->lvlArr, j);
138,602✔
733

734
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
138,602✔
735
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
2,375✔
736
      } else {
737
        j++;
136,227✔
738
      }
739
    }
740

741
    if (tsdbTFileSetIsEmpty(fset)) {
134,094✔
742
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
11!
743
    } else {
744
      i++;
134,084✔
745
    }
746
  }
747

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

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

760
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
6,735✔
761
  TSDB_CHECK_CODE(code, lino, _exit);
6,763!
762

763
  code = create_fs(pTsdb, fs);
6,763✔
764
  TSDB_CHECK_CODE(code, lino, _exit);
6,760!
765

766
  code = open_fs(fs[0], rollback);
6,760✔
767
  TSDB_CHECK_CODE(code, lino, _exit);
6,773!
768

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

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

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

789
  (void)taosThreadMutexLock(&pTsdb->mutex);
6,823✔
790

791
  // disable
792
  pTsdb->bgTaskDisabled = true;
6,824✔
793

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

807
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
6,823✔
808

809
  // destroy all channels
810
  for (int32_t k = 0; k < 2; k++) {
20,471✔
811
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
782,782✔
812
      SVATaskID *task = taosArrayGet(asyncTasks, i);
769,133✔
813
      if (k == 0) {
769,135✔
814
        (void)vnodeACancel(task);
384,741✔
815
      } else {
816
        vnodeAWait(task);
384,394✔
817
      }
818
    }
819
  }
820
  taosArrayDestroy(asyncTasks);
6,824✔
821

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

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

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

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

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

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

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

865
  if (etype == TSDB_FEDIT_COMMIT) {
7,027✔
866
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
5,339✔
867
  } else {
868
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
1,688✔
869
  }
870

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

876
  // edit
877
  code = edit_fs(fs, opArray, etype);
7,022✔
878
  TSDB_CHECK_CODE(code, lino, _exit);
7,019!
879

880
  // save fs
881
  code = save_fs(fs->fSetArrTmp, current_t);
7,019✔
882
  TSDB_CHECK_CODE(code, lino, _exit);
7,028!
883

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

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

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

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

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

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

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

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

954
        arg->tsdb = fs->tsdb;
1,656✔
955
        arg->fid = fset->fid;
1,656✔
956

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

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

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

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

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

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

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

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

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

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

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

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

1046
  TARRAY2_FOREACH(fs->fSetArr, fset) {
2,364,365✔
1047
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
1,343,151✔
1048
    if (code) break;
1,343,717!
1049

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

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

1065
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
1,020,439✔
1066
  if (fsetArr[0]) {
1,020,439!
1067
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2,365,486!
1068
    taosMemoryFreeClear(fsetArr[0]);
1,020,937!
1069
    fsetArr[0] = NULL;
1,021,216✔
1070
  }
1071
}
1,020,998✔
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) {
132,425✔
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;
132,425✔
1215

1216
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
132,425✔
1217
  if (*fset == NULL) {
132,425✔
1218
    return;
127,212✔
1219
  }
1220

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

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

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

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

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

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

1260
  cond->running = false;
5,213✔
1261
  if (cond->numWait > 0) {
5,213✔
1262
    (void)taosThreadCondSignal(&cond->cond);
4✔
1263
  }
1264

1265
  tsdbInfo("vgId:%d finish %s task on file set:%d", TD_VID(tsdb->pVnode), vnodeGetATaskName(task), fid);
5,213!
1266
  return;
5,213✔
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