• 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

53.18
/source/dnode/vnode/src/tsdb/tsdbFSet2.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 "tsdbFSet2.h"
17
#include "vnd.h"
18

19
int32_t tsdbSttLvlInit(int32_t level, SSttLvl **lvl) {
35,494✔
20
  if (!(lvl[0] = taosMemoryMalloc(sizeof(SSttLvl)))) {
35,494!
21
    return terrno;
×
22
  }
23
  lvl[0]->level = level;
35,536✔
24
  TARRAY2_INIT(lvl[0]->fobjArr);
35,536✔
25
  return 0;
35,536✔
26
}
27

28
static void tsdbSttLvlClearFObj(void *data) { TAOS_UNUSED(tsdbTFileObjUnref(*(STFileObj **)data)); }
35,586✔
29

30
void tsdbSttLvlClear(SSttLvl **lvl) {
35,570✔
31
  if (lvl[0] != NULL) {
35,570!
32
    TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
71,147!
33
    taosMemoryFree(lvl[0]);
35,570!
34
    lvl[0] = NULL;
35,573✔
35
  }
36
}
35,573✔
37

38
static int32_t tsdbSttLvlInitEx(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
55✔
39
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
55✔
40
  if (code) return code;
55!
41

42
  const STFileObj *fobj1;
43
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
122✔
44
    STFileObj *fobj;
45
    code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
67✔
46
    if (code) {
67!
UNCOV
47
      tsdbSttLvlClear(lvl);
×
48
      return code;
×
49
    }
50

51
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
67✔
52
    if (code) {
67!
53
      tsdbSttLvlClear(lvl);
×
54
      taosMemoryFree(fobj);
×
55
      return code;
×
56
    }
57
  }
58
  return 0;
55✔
59
}
60

61
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
35,411✔
62
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
35,411✔
63
  if (code) return code;
35,459!
64

65
  STFileObj *fobj1;
66
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
70,940✔
67
    code = tsdbTFileObjRef(fobj1);
35,443✔
68
    if (code) {
35,496!
UNCOV
69
      tsdbSttLvlClear(lvl);
×
70
      return code;
×
71
    }
72
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
35,496✔
73
    if (code) {
35,481!
74
      if (tsdbTFileObjUnref(fobj1) != 0) {
×
75
        tsdbError("failed to unref file obj, fobj:%p", fobj1);
×
76
      }
77
      tsdbSttLvlClear(lvl);
×
78
      return code;
×
79
    }
80
  }
81
  return 0;
35,497✔
82
}
83

84
static int32_t tsdbSttLvlFilteredInitEx(STsdb *pTsdb, const SSttLvl *lvl1, int64_t ever, SSttLvl **lvl,
×
85
                                        TFileOpArray *fopArr) {
86
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
×
87
  if (code) return code;
×
88

89
  const STFileObj *fobj1;
90
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
×
91
    if (fobj1->f->maxVer <= ever) {
×
92
      STFileObj *fobj;
93
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
×
94
      if (code) {
×
95
        tsdbSttLvlClear(lvl);
×
96
        return code;
×
97
      }
98

99
      TAOS_CHECK_RETURN(TARRAY2_APPEND(lvl[0]->fobjArr, fobj));
×
100
    } else {
101
      STFileOp op = {
×
102
          .optype = TSDB_FOP_REMOVE,
103
          .fid = fobj1->f->fid,
×
104
          .of = fobj1->f[0],
105
      };
106
      TAOS_CHECK_RETURN(TARRAY2_APPEND(fopArr, op));
×
107
    }
108
  }
109
  return 0;
×
110
}
111

112
static void tsdbSttLvlRemoveFObj(void *data) {
10✔
113
  int32_t code = tsdbTFileObjRemove(*(STFileObj **)data);
10✔
114
  if (code) {
10!
115
    tsdbError("failed to remove file obj, code:%d, error:%s", code, tstrerror(code));
×
116
  }
117
}
10✔
118
static void tsdbSttLvlRemove(SSttLvl **lvl) {
5✔
119
  TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlRemoveFObj);
15!
120
  taosMemoryFree(lvl[0]);
5!
121
  lvl[0] = NULL;
5✔
122
}
5✔
123

124
static int32_t tsdbSttLvlApplyEdit(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl *lvl2) {
18✔
125
  int32_t code = 0;
18✔
126

127
  if (lvl1->level != lvl2->level) {
18!
128
    return TSDB_CODE_INVALID_PARA;
×
129
  }
130

131
  int32_t i1 = 0, i2 = 0;
18✔
132
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
43!
133
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
25!
134
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
25✔
135

136
    if (fobj1 && fobj2) {
25!
137
      if (fobj1->f->cid < fobj2->f->cid) {
20!
138
        // create a file obj
139
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
×
140
        if (code) return code;
×
141
        code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
×
142
        if (code) return code;
×
143
        i1++;
×
144
        i2++;
×
145
      } else if (fobj1->f->cid > fobj2->f->cid) {
20!
146
        // remove a file obj
UNCOV
147
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
148
      } else {
149
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
20!
150
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
20!
151
            fobj2->f[0] = fobj1->f[0];
×
152
          }
153
        } else {
154
          TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
155
          code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
×
156
          if (code) return code;
×
157
          code = TARRAY2_SORT_INSERT(lvl2->fobjArr, fobj2, tsdbTFileObjCmpr);
×
158
          if (code) return code;
×
159
        }
160
        i1++;
20✔
161
        i2++;
20✔
162
      }
163
    } else if (fobj1) {
5!
164
      // create a file obj
165
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
5✔
166
      if (code) return code;
5!
167
      code = TARRAY2_INSERT_PTR(lvl2->fobjArr, i2, &fobj2);
5!
168
      if (code) return code;
5!
169
      i1++;
5✔
170
      i2++;
5✔
171
    } else {
172
      // remove a file obj
173
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
×
174
    }
175
  }
176
  return 0;
18✔
177
}
178

179
static int32_t tsdbSttLvlCmprFn(const SSttLvl **lvl1, const SSttLvl **lvl2) {
21✔
180
  if (lvl1[0]->level < lvl2[0]->level) return -1;
21!
181
  if (lvl1[0]->level > lvl2[0]->level) return 1;
21✔
182
  return 0;
15✔
183
}
184

185
static int32_t tsdbSttLvlToJson(const SSttLvl *lvl, cJSON *json) {
37✔
186
  if (cJSON_AddNumberToObject(json, "level", lvl->level) == NULL) {
37!
187
    return TSDB_CODE_OUT_OF_MEMORY;
×
188
  }
189

190
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
37✔
191
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
37!
192
  const STFileObj *fobj;
193
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
81✔
194
    cJSON *item = cJSON_CreateObject();
44✔
195
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
44!
196
    (void)cJSON_AddItemToArray(ajson, item);
44✔
197

198
    int32_t code = tsdbTFileToJson(fobj->f, item);
44✔
199
    if (code) return code;
44!
200
  }
201

202
  return 0;
37✔
203
}
204

205
static int32_t tsdbJsonToSttLvl(STsdb *pTsdb, const cJSON *json, SSttLvl **lvl) {
2✔
206
  const cJSON *item1, *item2;
207
  int32_t      level;
208

209
  item1 = cJSON_GetObjectItem(json, "level");
2✔
210
  if (cJSON_IsNumber(item1)) {
2!
211
    level = item1->valuedouble;
2✔
212
  } else {
213
    return TSDB_CODE_FILE_CORRUPTED;
×
214
  }
215

216
  int32_t code = tsdbSttLvlInit(level, lvl);
2✔
217
  if (code) return code;
2!
218

219
  item1 = cJSON_GetObjectItem(json, "files");
2✔
220
  if (!cJSON_IsArray(item1)) {
2!
221
    tsdbSttLvlClear(lvl);
×
222
    return TSDB_CODE_FILE_CORRUPTED;
×
223
  }
224

225
  cJSON_ArrayForEach(item2, item1) {
4!
226
    STFile tf;
227
    code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &tf);
2✔
228
    if (code) {
2!
229
      tsdbSttLvlClear(lvl);
×
230
      return code;
×
231
    }
232

233
    STFileObj *fobj;
234
    code = tsdbTFileObjInit(pTsdb, &tf, &fobj);
2✔
235
    if (code) {
2!
236
      tsdbSttLvlClear(lvl);
×
237
      return code;
×
238
    }
239

240
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
2!
241
    if (code) return code;
2!
242
  }
243
  TARRAY2_SORT(lvl[0]->fobjArr, tsdbTFileObjCmpr);
2!
244
  return 0;
2✔
245
}
246

247
int32_t tsdbTFileSetToJson(const STFileSet *fset, cJSON *json) {
40✔
248
  int32_t code = 0;
40✔
249
  cJSON  *item1, *item2;
250

251
  // fid
252
  if (cJSON_AddNumberToObject(json, "fid", fset->fid) == NULL) {
40!
253
    return TSDB_CODE_OUT_OF_MEMORY;
×
254
  }
255

256
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
200✔
257
    if (fset->farr[ftype] == NULL) continue;
160✔
258

259
    code = tsdbTFileToJson(fset->farr[ftype]->f, json);
15✔
260
    if (code) return code;
15!
261
  }
262

263
  // each level
264
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
40✔
265
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
40!
266
  const SSttLvl *lvl;
267
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
77✔
268
    item2 = cJSON_CreateObject();
37✔
269
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
37!
270
    (void)cJSON_AddItemToArray(item1, item2);
37✔
271

272
    code = tsdbSttLvlToJson(lvl, item2);
37✔
273
    if (code) return code;
37!
274
  }
275

276
  return 0;
40✔
277
}
278

279
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
2✔
280
  int32_t      code;
281
  const cJSON *item1, *item2;
282
  int32_t      fid;
283
  STFile       tf;
284

285
  // fid
286
  item1 = cJSON_GetObjectItem(json, "fid");
2✔
287
  if (cJSON_IsNumber(item1)) {
2!
288
    fid = item1->valuedouble;
2✔
289
  } else {
290
    return TSDB_CODE_FILE_CORRUPTED;
×
291
  }
292

293
  code = tsdbTFileSetInit(fid, fset);
2✔
294
  if (code) return code;
2!
295

296
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
10✔
297
    code = tsdbJsonToTFile(json, ftype, &tf);
8✔
298
    if (code == TSDB_CODE_NOT_FOUND) {
8!
299
      continue;
8✔
UNCOV
300
    } else if (code) {
×
301
      tsdbTFileSetClear(fset);
×
302
      return code;
×
303
    } else {
UNCOV
304
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
×
UNCOV
305
      if (code) return code;
×
306
    }
307
  }
308

309
  // each level
310
  item1 = cJSON_GetObjectItem(json, "stt lvl");
2✔
311
  if (cJSON_IsArray(item1)) {
2!
312
    cJSON_ArrayForEach(item2, item1) {
4!
313
      SSttLvl *lvl;
314
      code = tsdbJsonToSttLvl(pTsdb, item2, &lvl);
2✔
315
      if (code) {
2!
316
        tsdbTFileSetClear(fset);
×
317
        return code;
×
318
      }
319

320
      code = TARRAY2_APPEND((*fset)->lvlArr, lvl);
2!
321
      if (code) return code;
2!
322
    }
323
    TARRAY2_SORT((*fset)->lvlArr, tsdbSttLvlCmprFn);
2!
324
  } else {
325
    return TSDB_CODE_FILE_CORRUPTED;
×
326
  }
327

328
  return 0;
2✔
329
}
330

331
// NOTE: the api does not remove file, only do memory operation
332
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
43✔
333
  int32_t code = 0;
43✔
334

335
  if (op->optype == TSDB_FOP_CREATE) {
43✔
336
    // create a new file
337
    STFileObj *fobj;
338
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
33✔
339
    if (code) return code;
33!
340

341
    if (fobj->f->type == TSDB_FTYPE_STT) {
33✔
342
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
24✔
343
      if (!lvl) {
24✔
344
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
19✔
345
        if (code) return code;
19!
346

347
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
19✔
348
        if (code) return code;
19!
349
      }
350

351
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
24✔
352
      if (code) return code;
24!
353
    } else {
354
      fset->farr[fobj->f->type] = fobj;
9✔
355
    }
356
  } else if (op->optype == TSDB_FOP_REMOVE) {
10!
357
    // delete a file
358
    if (op->of.type == TSDB_FTYPE_STT) {
10!
359
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
10✔
360

361
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
10✔
362
      STFileObj *tfobjp = &tfobj;
10✔
363
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
10✔
364
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlClearFObj);
10!
365
    } else {
UNCOV
366
      code = tsdbTFileObjUnref(fset->farr[op->of.type]);
×
UNCOV
367
      if (code) return code;
×
UNCOV
368
      fset->farr[op->of.type] = NULL;
×
369
    }
370
  } else {
UNCOV
371
    if (op->nf.type == TSDB_FTYPE_STT) {
×
372
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
×
373

374
      STFileObj   tfobj = {.f[0] = {.cid = op->of.cid}}, *tfobjp = &tfobj;
×
375
      STFileObj **fobjPtr = TARRAY2_SEARCH(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
×
376
      if (fobjPtr) {
×
377
        tfobjp = *fobjPtr;
×
378
        tfobjp->f[0] = op->nf;
×
379
      } else {
380
        tsdbError("file not found, cid:%" PRId64, op->of.cid);
×
381
      }
382
    } else {
UNCOV
383
      fset->farr[op->nf.type]->f[0] = op->nf;
×
384
    }
385
  }
386

387
  return 0;
43✔
388
}
389

390
int32_t tsdbTFileSetApplyEdit(STsdb *pTsdb, const STFileSet *fset1, STFileSet *fset2) {
24✔
391
  int32_t code = 0;
24✔
392

393
  if (fset1->fid != fset2->fid) {
24!
394
    return TSDB_CODE_INVALID_PARA;
×
395
  }
396

397
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
120✔
398
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;
96!
399

400
    STFileObj *fobj1 = fset1->farr[ftype];
15✔
401
    STFileObj *fobj2 = fset2->farr[ftype];
15✔
402

403
    if (fobj1 && fobj2) {
15!
404
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
6!
405
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
6!
UNCOV
406
          fobj2->f[0] = fobj1->f[0];
×
407
        }
408
      } else {
UNCOV
409
        if (fobj1->f->cid != fobj2->f->cid) {
×
UNCOV
410
          code = tsdbTFileObjRemove(fobj2);
×
UNCOV
411
          if (code) return code;
×
412
        } else {
413
          code = tsdbTFileObjRemoveUpdateLC(fobj2);
×
414
          if (code) return code;
×
415
        }
UNCOV
416
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
×
UNCOV
417
        if (code) return code;
×
418
      }
419
    } else if (fobj1) {
9!
420
      // create a new file
421
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
9✔
422
      if (code) return code;
9!
423
    } else {
424
      // remove the file
425
      code = tsdbTFileObjRemove(fobj2);
×
426
      if (code) return code;
×
427
      fset2->farr[ftype] = NULL;
×
428
    }
429
  }
430

431
  // stt part
432
  int32_t i1 = 0, i2 = 0;
24✔
433
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
50✔
434
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
26✔
435
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
26✔
436

437
    if (lvl1 && lvl2) {
26✔
438
      if (lvl1->level < lvl2->level) {
21!
439
        // add a new stt level
UNCOV
440
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
×
UNCOV
441
        if (code) return code;
×
UNCOV
442
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
×
UNCOV
443
        if (code) return code;
×
UNCOV
444
        i1++;
×
UNCOV
445
        i2++;
×
446
      } else if (lvl1->level > lvl2->level) {
21✔
447
        // remove the stt level
448
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
3!
449
      } else {
450
        // apply edit on stt level
451
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
18✔
452
        if (code) return code;
18!
453
        i1++;
18✔
454
        i2++;
18✔
455
      }
456
    } else if (lvl1) {
5✔
457
      // add a new stt level
458
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
3✔
459
      if (code) return code;
3!
460
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
3✔
461
      if (code) return code;
3!
462
      i1++;
3✔
463
      i2++;
3✔
464
    } else {
465
      // remove the stt level
466
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
2!
467
    }
468
  }
469

470
  return 0;
24✔
471
}
472

473
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
35,526✔
474
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
35,526!
475
  if (fset[0] == NULL) {
35,504!
476
    return terrno;
×
477
  }
478

479
  fset[0]->fid = fid;
35,504✔
480
  fset[0]->maxVerValid = VERSION_MAX;
35,504✔
481
  TARRAY2_INIT(fset[0]->lvlArr);
35,504✔
482

483
  // block commit variables
484
  (void)taosThreadCondInit(&fset[0]->canCommit, NULL);
35,504✔
485
  (*fset)->numWaitCommit = 0;
35,501✔
486
  (*fset)->blockCommit = false;
35,501✔
487

488
  for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
106,461✔
489
    struct STFileSetCond *cond = &(*fset)->conds[i];
70,963✔
490
    cond->running = false;
70,963✔
491
    cond->numWait = 0;
70,963✔
492
    (void)taosThreadCondInit(&cond->cond, NULL);
70,963✔
493
  }
494

495
  return 0;
35,498✔
496
}
497

498
int32_t tsdbTFileSetInitCopy(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
54✔
499
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
54✔
500
  if (code) return code;
54!
501

502
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
270✔
503
    if (fset1->farr[ftype] == NULL) continue;
216✔
504

505
    code = tsdbTFileObjInit(pTsdb, fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
9✔
506
    if (code) {
9!
UNCOV
507
      tsdbTFileSetClear(fset);
×
508
      return code;
×
509
    }
510
  }
511

512
  const SSttLvl *lvl1;
513
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
106✔
514
    SSttLvl *lvl;
515
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
52✔
516
    if (code) {
52!
517
      tsdbTFileSetClear(fset);
×
518
      return code;
×
519
    }
520

521
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
52!
522
    if (code) return code;
52!
523
  }
524

525
  return 0;
54✔
526
}
527

528
int32_t tsdbTFileSetFilteredInitDup(STsdb *pTsdb, const STFileSet *fset1, int64_t ever, STFileSet **fset,
×
529
                                    TFileOpArray *fopArr) {
530
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
×
531
  if (code) return code;
×
532

533
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
534
    if (fset1->farr[ftype] == NULL) continue;
×
535
    STFileObj *fobj = fset1->farr[ftype];
×
536
    if (fobj->f->maxVer <= ever) {
×
537
      code = tsdbTFileObjInit(pTsdb, fobj->f, &fset[0]->farr[ftype]);
×
538
      if (code) {
×
539
        tsdbTFileSetClear(fset);
×
540
        return code;
×
541
      }
542
    } else {
543
      STFileOp op = {
×
544
          .optype = TSDB_FOP_REMOVE,
545
          .fid = fobj->f->fid,
×
546
          .of = fobj->f[0],
547
      };
548
      code = TARRAY2_APPEND(fopArr, op);
×
549
      if (code) return code;
×
550
    }
551
  }
552

553
  const SSttLvl *lvl1;
554
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
×
555
    SSttLvl *lvl;
556
    code = tsdbSttLvlFilteredInitEx(pTsdb, lvl1, ever, &lvl, fopArr);
×
557
    if (code) {
×
558
      tsdbTFileSetClear(fset);
×
559
      return code;
×
560
    }
561

562
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
×
563
    if (code) return code;
×
564
  }
565

566
  return 0;
×
567
}
568

569
int32_t tsdbTFileSetRangeInitRef(STsdb *pTsdb, const STFileSet *fset1, int64_t sver, int64_t ever,
×
570
                                 STFileSetRange **fsr) {
571
  fsr[0] = taosMemoryCalloc(1, sizeof(*fsr[0]));
×
572
  if (fsr[0] == NULL) {
×
573
    return terrno;
×
574
  }
575
  fsr[0]->fid = fset1->fid;
×
576
  fsr[0]->sver = sver;
×
577
  fsr[0]->ever = ever;
×
578

579
  int32_t code = tsdbTFileSetInitRef(pTsdb, fset1, &fsr[0]->fset);
×
580
  if (code) {
×
581
    taosMemoryFree(fsr[0]);
×
582
    fsr[0] = NULL;
×
583
  }
584
  return code;
×
585
}
586

587
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
35,456✔
588
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
35,456✔
589
  if (code) return code;
35,412!
590

591
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
177,056✔
592
    if (fset1->farr[ftype] == NULL) continue;
141,644!
593

UNCOV
594
    code = tsdbTFileObjRef(fset1->farr[ftype]);
×
UNCOV
595
    if (code) {
×
596
      tsdbTFileSetClear(fset);
×
597
      return code;
×
598
    }
UNCOV
599
    fset[0]->farr[ftype] = fset1->farr[ftype];
×
600
  }
601

602
  const SSttLvl *lvl1;
603
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
70,877✔
604
    SSttLvl *lvl;
605
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
35,403✔
606
    if (code) {
35,468!
607
      tsdbSttLvlClear(&lvl);
×
608
      tsdbTFileSetClear(fset);
×
609
      return code;
×
610
    }
611

612
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
35,471!
613
    if (code) {
35,465!
614
      tsdbSttLvlClear(&lvl);
×
615
      tsdbTFileSetClear(fset);
×
616
      return code;
×
617
    }
618
  }
619

620
  return 0;
35,474✔
621
}
622

623
void tsdbTFileSetRangeClear(STFileSetRange **fsr) {
×
624
  if (!fsr[0]) return;
×
625

626
  tsdbTFileSetClear(&fsr[0]->fset);
×
627
  taosMemoryFree(fsr[0]);
×
628
  fsr[0] = NULL;
×
629
  return;
×
630
}
631

UNCOV
632
void tsdbTFileSetRangeArrayDestroy(TFileSetRangeArray **ppArr) {
×
UNCOV
633
  if (ppArr && ppArr[0]) {
×
UNCOV
634
    TARRAY2_DESTROY(ppArr[0], tsdbTFileSetRangeClear);
×
UNCOV
635
    taosMemoryFree(ppArr[0]);
×
UNCOV
636
    ppArr[0] = NULL;
×
637
  }
UNCOV
638
}
×
639

640
void tsdbTFileSetClear(STFileSet **fset) {
35,587✔
641
  if (fset && *fset) {
35,587!
642
    for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
177,855✔
643
      if ((*fset)->farr[ftype] == NULL) continue;
142,283✔
644
      int32_t code = tsdbTFileObjUnref((*fset)->farr[ftype]);
27✔
645
      if (code) {
27!
646
        tsdbError("failed to unref file, fid:%d, ftype:%d", (*fset)->fid, ftype);
×
647
      }
648
      (*fset)->farr[ftype] = NULL;
27✔
649
    }
650

651
    TARRAY2_DESTROY((*fset)->lvlArr, tsdbSttLvlClear);
71,136!
652

653
    (void)taosThreadCondDestroy(&(*fset)->canCommit);
35,572✔
654
    for (int32_t i = 0; i < sizeof((*fset)->conds) / sizeof((*fset)->conds[0]); ++i) {
106,703✔
655
      (void)taosThreadCondDestroy(&(*fset)->conds[i].cond);
71,133✔
656
    }
657
    taosMemoryFreeClear(*fset);
35,570!
658
  }
659
}
35,583✔
660

661
void tsdbTFileSetRemove(STFileSet *fset) {
×
662
  if (fset == NULL) return;
×
663

664
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
×
665
    if (fset->farr[ftype] != NULL) {
×
666
      int32_t code = tsdbTFileObjRemove(fset->farr[ftype]);
×
667
      if (code) {
×
668
        tsdbError("failed to remove file, fid:%d, ftype:%d", fset->fid, ftype);
×
669
      }
670
      fset->farr[ftype] = NULL;
×
671
    }
672
  }
673

674
  TARRAY2_DESTROY(fset->lvlArr, tsdbSttLvlRemove);
×
675
}
676

677
SSttLvl *tsdbTFileSetGetSttLvl(STFileSet *fset, int32_t level) {
34✔
678
  SSttLvl   sttLvl = {.level = level};
34✔
679
  SSttLvl  *lvl = &sttLvl;
34✔
680
  SSttLvl **lvlPtr = TARRAY2_SEARCH(fset->lvlArr, &lvl, tsdbSttLvlCmprFn, TD_EQ);
34✔
681
  return lvlPtr ? lvlPtr[0] : NULL;
34✔
682
}
683

684
int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
108✔
685
  if (fset1[0]->fid < fset2[0]->fid) return -1;
108✔
686
  if (fset1[0]->fid > fset2[0]->fid) return 1;
92✔
687
  return 0;
39✔
688
}
689

690
int64_t tsdbTFileSetMaxCid(const STFileSet *fset) {
2✔
691
  int64_t maxCid = 0;
2✔
692
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
10✔
693
    if (fset->farr[ftype] == NULL) continue;
8!
UNCOV
694
    maxCid = TMAX(maxCid, fset->farr[ftype]->f->cid);
×
695
  }
696
  const SSttLvl   *lvl;
697
  const STFileObj *fobj;
698
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
4✔
699
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { maxCid = TMAX(maxCid, fobj->f->cid); }
4✔
700
  }
701
  return maxCid;
2✔
702
}
703

704
bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
40✔
705
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
180✔
706
    if (fset->farr[ftype] != NULL) return false;
145✔
707
  }
708
  return TARRAY2_SIZE(fset->lvlArr) == 0;
35✔
709
}
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