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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

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

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

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

58
  return 0;
553✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

201
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
452!
202

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

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

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

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

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

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

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

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

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

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

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

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

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

320
_exit:
15✔
321
  if (code) {
15!
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);
15!
326
  }
327
  return code;
15✔
328
}
329

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

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

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

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

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

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

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

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

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

380
      found = true;
×
381
    }
382

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

390
  return 0;
2✔
391
}
392

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

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

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

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

407
  return 0;
454✔
408
}
409

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

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

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

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

UNCOV
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;
2✔
456
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
4✔
457
      STFileObj *fobj;
458
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
4✔
459
        code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
2✔
460
        TSDB_CHECK_CODE(code, lino, _exit);
2!
461
      }
462
    }
463
  }
464

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

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

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

484
  return NULL;
×
485
}
486

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

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

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

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

550
    for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
1,358✔
551
      if (taosIsDir(file->aname)) continue;
906✔
552

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

558
    tsdbFSDestroyFileObjHash(&fobjHash);
452✔
559

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

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

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

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

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

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

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

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

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

598
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
491✔
599

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

609
  return 0;
467✔
610
}
611

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

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

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

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

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

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

687
  code = tsdbFSDupState(fs);
15✔
688
  if (code) return code;
15!
689

690
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
15✔
691
  STFileSet      *fset = NULL;
15✔
692
  const STFileOp *op;
693
  TARRAY2_FOREACH_PTR(opArray, op) {
58✔
694
    if (!fset || fset->fid != op->fid) {
43✔
695
      STFileSet tfset = {.fid = op->fid};
26✔
696
      fset = &tfset;
26✔
697
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
26✔
698
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
26✔
699

700
      if (!fset) {
26✔
701
        code = tsdbTFileSetInit(op->fid, &fset);
16✔
702
        TSDB_CHECK_CODE(code, lino, _exit);
16!
703

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

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

713
  // remove empty empty stt level and empty file set
714
  int32_t i = 0;
15✔
715
  while (i < TARRAY2_SIZE(fsetArray)) {
55✔
716
    fset = TARRAY2_GET(fsetArray, i);
40✔
717

718
    SSttLvl *lvl;
719
    int32_t  j = 0;
40✔
720
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
82✔
721
      lvl = TARRAY2_GET(fset->lvlArr, j);
42✔
722

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

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

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

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

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

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

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

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

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

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

778
  (void)taosThreadMutexLock(&pTsdb->mutex);
553✔
779

780
  // disable
781
  pTsdb->bgTaskDisabled = true;
553✔
782

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

797
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
553✔
798

799
  // destroy all channels
800
  for (int32_t k = 0; k < 2; k++) {
1,659✔
801
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
1,214✔
802
      SVATaskID *task = taosArrayGet(asyncTasks, i);
108✔
803
      if (k == 0) {
108✔
804
        (void)vnodeACancel(task);
54✔
805
      } else {
806
        (void)vnodeAWait(task);
54✔
807
      }
808
    }
809
  }
810
  taosArrayDestroy(asyncTasks);
553✔
811

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

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

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

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

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

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

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

855
  if (etype == TSDB_FEDIT_COMMIT) {
15✔
856
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
10✔
857
  } else {
858
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
5✔
859
  }
860

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

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

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

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

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

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

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

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

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

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

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

944
        arg->tsdb = fs->tsdb;
7✔
945
        arg->fid = fset->fid;
7✔
946

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

952
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
33!
953
        tsdbFSSetBlockCommit(fset, true);
×
954
      } else {
955
        tsdbFSSetBlockCommit(fset, false);
33✔
956
      }
957
    }
958
  }
959

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

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

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

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

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

UNCOV
995
  TARRAY2_INIT(fsetArr[0]);
×
996

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

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

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

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

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

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

1034
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
21,676!
1035
  if (fsetArr[0] == NULL) return terrno;
21,687!
1036

1037
  TARRAY2_FOREACH(fs->fSetArr, fset) {
57,153✔
1038
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
35,460✔
1039
    if (code) break;
35,466!
1040

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

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

1056
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
21,686✔
1057
  if (fsetArr[0]) {
21,686!
1058
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
57,185!
1059
    taosMemoryFreeClear(fsetArr[0]);
21,685!
1060
    fsetArr[0] = NULL;
21,688✔
1061
  }
1062
}
21,688✔
1063

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1207
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
28✔
1208
  if (*fset == NULL) {
28✔
1209
    return;
16✔
1210
  }
1211

1212
  struct STFileSetCond *cond = NULL;
12✔
1213
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
12!
1214
    cond = &(*fset)->conds[0];
5✔
1215
  } else {
1216
    cond = &(*fset)->conds[1];
7✔
1217
  }
1218

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

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

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

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

1244
  struct STFileSetCond *cond = NULL;
12✔
1245
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
12!
1246
    cond = &fset->conds[0];
5✔
1247
  } else {
1248
    cond = &fset->conds[1];
7✔
1249
  }
1250

1251
  cond->running = false;
12✔
1252
  if (cond->numWait > 0) {
12✔
1253
    (void)taosThreadCondSignal(&cond->cond);
2✔
1254
  }
1255

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

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

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

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

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

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

1288
  return TSDB_CODE_SUCCESS;
×
1289
}
1290

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

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

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

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

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

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

1328
  return code;
×
1329
}
1330

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

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

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

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

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

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

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

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

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

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

1388
  return TSDB_CODE_INVALID_PARA;
×
1389
}
1390

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

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

1399
  *ppReader = NULL;
×
1400
  return;
×
1401
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc