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

taosdata / TDengine / #3570

30 Dec 2024 10:10AM UTC coverage: 62.94% (-0.02%) from 62.96%
#3570

push

travis-ci

web-flow
Merge pull request #29414 from taosdata/fix/keeper/3.0/gitinfo

[TD-33393] fix(keeper): add gitinfo

139195 of 284325 branches covered (48.96%)

Branch coverage included in aggregate %.

216954 of 281527 relevant lines covered (77.06%)

19045397.76 hits per line

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

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

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

21
#define BLOCK_COMMIT_FACTOR 3
22

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

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

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

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

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

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

58
  return 0;
11,618✔
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

211
  /* fmtv */
212
  item1 = cJSON_GetObjectItem(json, "fmtv");
2,303✔
213
  if (cJSON_IsNumber(item1)) {
2,306!
214
    if (item1->valuedouble != 1) {
2,292!
215
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
216
    }
217
  } else {
218
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
219
  }
220

221
  /* fset */
222
  item1 = cJSON_GetObjectItem(json, "fset");
2,292✔
223
  if (cJSON_IsArray(item1)) {
2,305!
224
    const cJSON *item2;
225
    cJSON_ArrayForEach(item2, item1) {
3,958!
226
      STFileSet *fset;
227
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
1,655✔
228
      TSDB_CHECK_CODE(code, lino, _exit);
1,660!
229

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

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

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

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

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

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

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

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

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

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

320
_exit:
14,665✔
321
  if (code) {
14,665!
322
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, lino,
×
323
              tstrerror(code));
324
  } else {
325
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
14,665!
326
  }
327
  return code;
14,665✔
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,861✔
364
  int32_t code = 0;
2,861✔
365
  int32_t lino = 0;
2,861✔
366

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

393
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
394

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

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

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

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

407
  return 0;
5,169✔
408
}
409

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

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

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

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

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

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

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

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

484
  return NULL;
×
485
}
486

487
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
2,306✔
488
  for (int32_t i = 0; i < hash->numBucket; i++) {
9,302,276✔
489
    STFileHashEntry *entry = hash->buckets[i];
9,299,970✔
490
    while (entry) {
9,305,141✔
491
      STFileHashEntry *next = entry->next;
5,171✔
492
      taosMemoryFree(entry);
5,171!
493
      entry = next;
5,171✔
494
    }
495
  }
496
  taosMemoryFree(hash->buckets);
2,306!
497
  memset(hash, 0, sizeof(*hash));
2,306✔
498
}
2,306✔
499

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

505
  {  // scan each file
506
    STFileSet *fset = NULL;
2,290✔
507
    TARRAY2_FOREACH(fs->fSetArr, fset) {
3,949✔
508
      // data file
509
      for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
8,290✔
510
        if (fset->farr[ftype] == NULL) continue;
6,633✔
511
        STFileObj *fobj = fset->farr[ftype];
1,078✔
512
        code = tsdbFSDoScanAndFixFile(fs, fobj);
1,078✔
513
        if (code) {
1,079!
514
          fset->maxVerValid = (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
515
          corrupt = true;
×
516
        }
517
      }
518

519
      // stt file
520
      SSttLvl *lvl;
521
      TARRAY2_FOREACH(fset->lvlArr, lvl) {
3,323✔
522
        STFileObj *fobj;
523
        TARRAY2_FOREACH(lvl->fobjArr, fobj) {
3,448✔
524
          code = tsdbFSDoScanAndFixFile(fs, fobj);
1,782✔
525
          if (code) {
1,784!
526
            fset->maxVerValid =
×
527
                (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
×
528
            corrupt = true;
×
529
          }
530
        }
531
      }
532
    }
533
  }
534

535
  if (corrupt) {
2,293!
536
    tsdbError("vgId:%d, not to clear dangling files due to fset incompleteness", TD_VID(fs->tsdb->pVnode));
×
537
    fs->fsstate = TSDB_FS_STATE_INCOMPLETE;
×
538
    code = 0;
×
539
    goto _exit;
×
540
  }
541

542
  {  // clear unreferenced files
543
    STfsDir *dir = NULL;
2,293✔
544
    TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2,293!
545

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

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

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

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

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

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

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

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

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

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

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

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

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

598
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);
44,364✔
599

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

609
  return 0;
16,967✔
610
}
611

612
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
11,606✔
613
  int32_t code = 0;
11,606✔
614
  int32_t lino = 0;
11,606✔
615
  STsdb  *pTsdb = fs->tsdb;
11,606✔
616

617
  char fCurrent[TSDB_FILENAME_LEN];
618
  char cCurrent[TSDB_FILENAME_LEN];
619
  char mCurrent[TSDB_FILENAME_LEN];
620

621
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
11,606✔
622
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
11,610✔
623
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
11,623✔
624

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

629
    if (taosCheckExistFile(cCurrent)) {
2,294!
630
      // current.c.json exists
631

632
      fs->etype = TSDB_FEDIT_COMMIT;
×
633
      if (rollback) {
×
634
        code = abort_edit(fs);
×
635
        TSDB_CHECK_CODE(code, lino, _exit);
×
636
      } else {
637
        code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
×
638
        TSDB_CHECK_CODE(code, lino, _exit);
×
639

640
        code = commit_edit(fs);
×
641
        TSDB_CHECK_CODE(code, lino, _exit);
×
642
      }
643
    } else if (taosCheckExistFile(mCurrent)) {
2,301!
644
      // current.m.json exists
645
      fs->etype = TSDB_FEDIT_MERGE;
×
646
      code = abort_edit(fs);
×
647
      TSDB_CHECK_CODE(code, lino, _exit);
×
648
    }
649

650
    code = tsdbFSDupState(fs);
2,305✔
651
    TSDB_CHECK_CODE(code, lino, _exit);
2,301!
652

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

660
_exit:
9,320✔
661
  if (code) {
11,626!
662
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
663
  } else {
664
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
11,626✔
665
  }
666
  return code;
11,626✔
667
}
668

669
static void close_file_system(STFileSystem *fs) {
11,625✔
670
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
142,235✔
671
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
142,229✔
672
}
11,626✔
673

674
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
×
675
  if (pSet1->fid < pSet2->fid) {
×
676
    return -1;
×
677
  } else if (pSet1->fid > pSet2->fid) {
×
678
    return 1;
×
679
  }
680
  return 0;
×
681
}
682

683
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
14,665✔
684
  int32_t code = 0;
14,665✔
685
  int32_t lino = 0;
14,665✔
686

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

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

702
      if (!fset) {
142,696✔
703
        code = tsdbTFileSetInit(op->fid, &fset);
128,830✔
704
        TSDB_CHECK_CODE(code, lino, _exit);
128,778!
705

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

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

714
    if (fid != op->fid) {
165,055✔
715
      fid = op->fid;
142,795✔
716
      if (etype == TSDB_FEDIT_COMMIT) {
142,795✔
717
        fset->lastCommit = now;
137,963✔
718
      } else if (etype == TSDB_FEDIT_COMPACT) {
4,832✔
719
        fset->lastCompact = now;
102✔
720
      }
721
    }
722
  }
723

724
  // remove empty empty stt level and empty file set
725
  int32_t i = 0;
14,665✔
726
  while (i < TARRAY2_SIZE(fsetArray)) {
171,013✔
727
    fset = TARRAY2_GET(fsetArray, i);
156,347✔
728

729
    SSttLvl *lvl;
730
    int32_t  j = 0;
156,347✔
731
    while (j < TARRAY2_SIZE(fset->lvlArr)) {
324,352✔
732
      lvl = TARRAY2_GET(fset->lvlArr, j);
168,005✔
733

734
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
168,005✔
735
        TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
5,675!
736
      } else {
737
        j++;
162,330✔
738
      }
739
    }
740

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

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

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

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

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

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

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

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

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

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

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

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

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

810
  // destroy all channels
811
  for (int32_t k = 0; k < 2; k++) {
35,097✔
812
    for (int32_t i = 0; i < taosArrayGetSize(asyncTasks); i++) {
807,004✔
813
      SVATaskID *task = taosArrayGet(asyncTasks, i);
783,604✔
814
      if (k == 0) {
783,606✔
815
        (void)vnodeACancel(task);
391,803✔
816
      } else {
817
        vnodeAWait(task);
391,803✔
818
      }
819
    }
820
  }
821
  taosArrayDestroy(asyncTasks);
11,699✔
822

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

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

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

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

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

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

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

866
  if (etype == TSDB_FEDIT_COMMIT) {
14,665✔
867
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
9,835✔
868
  } else {
869
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
4,830✔
870
  }
871

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

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

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

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

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

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

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

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

930
  // schedule merge
931
  int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
14,665✔
932
  if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
14,665✔
933
    STFileSet *fset;
934
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
165,422✔
935
      if (TARRAY2_SIZE(fset->lvlArr) == 0) {
151,321✔
936
        tsdbFSSetBlockCommit(fset, false);
2,599✔
937
        continue;
2,599✔
938
      }
939

940
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
148,722✔
941
      if (lvl->level != 0) {
148,722✔
942
        tsdbFSSetBlockCommit(fset, false);
6,441✔
943
        continue;
6,441✔
944
      }
945

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

955
        arg->tsdb = fs->tsdb;
6,139✔
956
        arg->fid = fset->fid;
6,139✔
957

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

963
      if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
142,280✔
964
        tsdbFSSetBlockCommit(fset, true);
2✔
965
      } else {
966
        tsdbFSSetBlockCommit(fset, false);
142,278✔
967
      }
968
    }
969
  }
970

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

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

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

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

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

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

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

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

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

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

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

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

1045
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
4,503,543!
1046
  if (fsetArr[0] == NULL) return terrno;
4,505,614!
1047

1048
  TARRAY2_FOREACH(fs->fSetArr, fset) {
12,539,005✔
1049
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
8,033,698✔
1050
    if (code) break;
8,033,700!
1051

1052
    code = TARRAY2_APPEND(fsetArr[0], fset1);
8,033,700✔
1053
    if (code) {
8,033,391!
1054
      tsdbTFileSetClear(&fset1);
×
1055
      break;
×
1056
    }
1057
  }
1058

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

1067
void tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
4,505,217✔
1068
  if (fsetArr[0]) {
4,505,217!
1069
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
12,543,657!
1070
    taosMemoryFreeClear(fsetArr[0]);
4,505,861!
1071
    fsetArr[0] = NULL;
4,506,208✔
1072
  }
1073
}
4,505,719✔
1074

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1218
  tsdbFSGetFSet(tsdb->pFS, fid, fset);
144,549✔
1219
  if (*fset == NULL) {
144,550✔
1220
    return;
128,905✔
1221
  }
1222

1223
  struct STFileSetCond *cond = NULL;
15,645✔
1224
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
15,645✔
1225
    cond = &(*fset)->conds[0];
9,043✔
1226
  } else {
1227
    cond = &(*fset)->conds[1];
6,602✔
1228
  }
1229

1230
  while (1) {
1231
    if (cond->running) {
16,650✔
1232
      cond->numWait++;
1,005✔
1233
      (void)taosThreadCondWait(&cond->cond, &tsdb->mutex);
1,005✔
1234
      cond->numWait--;
1,005✔
1235
    } else {
1236
      cond->running = true;
15,645✔
1237
      break;
15,645✔
1238
    }
1239
  }
1240

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

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

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

1255
  struct STFileSetCond *cond = NULL;
15,645✔
1256
  if (sttTrigger == 1 || task == EVA_TASK_COMMIT) {
15,645✔
1257
    cond = &fset->conds[0];
9,043✔
1258
  } else {
1259
    cond = &fset->conds[1];
6,602✔
1260
  }
1261

1262
  cond->running = false;
15,645✔
1263
  if (cond->numWait > 0) {
15,645✔
1264
    (void)taosThreadCondSignal(&cond->cond);
1,005✔
1265
  }
1266

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

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

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

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

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

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

1299
  return TSDB_CODE_SUCCESS;
×
1300
}
1301

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

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

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

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

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

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

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

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

1345
  return code;
×
1346
}
1347

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

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

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

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

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

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

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

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

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

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

1405
  return TSDB_CODE_INVALID_PARA;
×
1406
}
1407

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

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

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

© 2026 Coveralls, Inc