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

taosdata / TDengine / #4440

04 Jul 2025 02:10AM UTC coverage: 63.29% (-0.4%) from 63.643%
#4440

push

travis-ci

web-flow
fix:(stmt2) heap buffer overflow (#31607)

159782 of 321690 branches covered (49.67%)

Branch coverage included in aggregate %.

19 of 22 new or added lines in 3 files covered. (86.36%)

5735 existing lines in 195 files now uncovered.

246739 of 320626 relevant lines covered (76.96%)

6757056.21 hits per line

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

73.55
/source/dnode/vnode/src/tsdb/tsdbCommit2.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 "tsdbCommit2.h"
17

18
// extern dependencies
19
typedef struct {
20
  int32_t    fid;
21
  STFileSet *fset;
22
} SFileSetCommitInfo;
23

24
typedef struct {
25
  STsdb  *tsdb;
26
  int32_t minutes;
27
  int8_t  precision;
28
  int32_t minRow;
29
  int32_t maxRow;
30
  int8_t  cmprAlg;
31
  int32_t sttTrigger;
32
  int32_t szPage;
33
  int64_t compactVersion;
34
  int64_t cid;
35
  int64_t now;
36

37
  struct {
38
    SFileSetCommitInfo *info;
39

40
    int32_t expLevel;
41
    TSKEY   minKey;
42
    TSKEY   maxKey;
43
    TABLEID tbid[1];
44
    bool    hasTSData;
45

46
    bool      skipTsRow;
47
    SHashObj *pColCmprObj;
48
  } ctx[1];
49

50
  // reader
51
  TSttFileReaderArray sttReaderArray[1];
52
  // iter
53
  TTsdbIterArray dataIterArray[1];
54
  SIterMerger   *dataIterMerger;
55
  TTsdbIterArray tombIterArray[1];
56
  SIterMerger   *tombIterMerger;
57
  // writer
58
  SFSetWriter *writer;
59

60
  TFileOpArray fopArray[1];
61
} SCommitter2;
62

63
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
467,460✔
64
  int32_t code = 0;
467,460✔
65
  int32_t lino = 0;
467,460✔
66

67
  SFSetWriterConfig config = {
467,460✔
68
      .tsdb = committer->tsdb,
467,460✔
69
      .toSttOnly = true,
70
      .compactVersion = committer->compactVersion,
467,460✔
71
      .minRow = committer->minRow,
467,460✔
72
      .maxRow = committer->maxRow,
467,460✔
73
      .szPage = committer->szPage,
467,460✔
74
      .cmprAlg = committer->cmprAlg,
467,460✔
75
      .fid = committer->ctx->info->fid,
467,460✔
76
      .cid = committer->cid,
467,460✔
77
      .expLevel = committer->ctx->expLevel,
467,460✔
78
      .level = 0,
79
  };
80

81
  if (committer->sttTrigger == 1) {
467,460✔
82
    config.toSttOnly = false;
6,027✔
83

84
    if (committer->ctx->info->fset) {
6,027✔
85
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
2,360✔
86
        if (committer->ctx->info->fset->farr[ftype] != NULL) {
1,888✔
87
          config.files[ftype].exist = true;
1,305✔
88
          config.files[ftype].file = committer->ctx->info->fset->farr[ftype]->f[0];
1,305✔
89
        }
90
      }
91
    }
92
  }
93

94
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
467,460!
95

96
_exit:
467,483✔
97
  if (code) {
467,483!
98
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
99
              tstrerror(code));
100
  }
101
  return code;
467,506✔
102
}
103

104
static int32_t tsdbCommitCloseWriter(SCommitter2 *committer) {
467,490✔
105
  return tsdbFSetWriterClose(&committer->writer, 0, committer->fopArray);
467,490✔
106
}
107

108
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
467,460✔
109
  int32_t   code = 0;
467,460✔
110
  int32_t   lino = 0;
467,460✔
111
  int64_t   numOfRow = 0;
467,460✔
112
  SMetaInfo info;
113

114
  committer->ctx->hasTSData = false;
467,460✔
115

116
  committer->ctx->tbid->suid = 0;
467,460✔
117
  committer->ctx->tbid->uid = 0;
467,460✔
118
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
174,410,094✔
119
    if (row->uid != committer->ctx->tbid->uid) {
173,166,997✔
120
      committer->ctx->tbid->suid = row->suid;
17,348,822✔
121
      committer->ctx->tbid->uid = row->uid;
17,348,822✔
122

123
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
17,348,822✔
124
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
13,374,208!
125
        continue;
13,593,381✔
126
      }
127
    }
128

129
    int64_t ts = TSDBROW_TS(&row->row);
159,796,521✔
130
    if (ts > committer->ctx->maxKey) {
159,796,521✔
131
      TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
3,640,681!
132
      continue;
3,640,559✔
133
    }
134

135
    committer->ctx->hasTSData = true;
156,155,840✔
136
    numOfRow++;
156,155,840✔
137

138
    TAOS_CHECK_GOTO(tsdbFSetWriteRow(committer->writer, row), &lino, _exit);
156,155,840!
139
    TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->dataIterMerger), &lino, _exit);
158,029,847!
140
  }
141

142
_exit:
467,498✔
143
  if (code) {
467,498!
144
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
145
              tstrerror(code));
146
  } else {
147
    tsdbDebug("vgId:%d fid:%d commit %" PRId64 " rows", TD_VID(committer->tsdb->pVnode), committer->ctx->info->fid,
467,498✔
148
              numOfRow);
149
  }
150
  return code;
467,493✔
151
}
152

153
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
467,489✔
154
  int32_t   code = 0;
467,489✔
155
  int32_t   lino = 0;
467,489✔
156
  int64_t   numRecord = 0;
467,489✔
157
  SMetaInfo info;
158

159
  // if no history data and no new timestamp data, skip tomb data
160
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
467,489✔
161
    committer->ctx->tbid->suid = 0;
348,518✔
162
    committer->ctx->tbid->uid = 0;
348,518✔
163
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
357,177✔
164
      if (record->uid != committer->ctx->tbid->uid) {
8,654✔
165
        committer->ctx->tbid->suid = record->suid;
5,475✔
166
        committer->ctx->tbid->uid = record->uid;
5,475✔
167

168
        if (metaGetInfo(committer->tsdb->pVnode->pMeta, record->uid, &info, NULL) != 0) {
5,475✔
169
          TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->tombIterMerger, committer->ctx->tbid), &lino, _exit);
30!
170
          continue;
30✔
171
        }
172
      }
173

174
      if (record->ekey < committer->ctx->minKey) {
8,624✔
175
        // do nothing
176
      } else if (record->skey > committer->ctx->maxKey) {
8,592✔
177
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
178
      } else {
179
        record->skey = TMAX(record->skey, committer->ctx->minKey);
7,427✔
180
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
7,427✔
181

182
        numRecord++;
7,427✔
183
        TAOS_CHECK_GOTO(tsdbFSetWriteTombRecord(committer->writer, record), &lino, _exit);
7,427!
184
      }
185

186
      TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->tombIterMerger), &lino, _exit);
8,623!
187
    }
188
  }
189

190
_exit:
467,499✔
191
  if (code) {
467,499!
192
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
193
              tstrerror(code));
194
  } else {
195
    tsdbDebug("vgId:%d fid:%d commit %" PRId64 " tomb records", TD_VID(committer->tsdb->pVnode),
467,499✔
196
              committer->ctx->info->fid, numRecord);
197
  }
198
  return code;
467,499✔
199
}
200

201
static void tsdbCommitCloseReader(SCommitter2 *committer) {
467,482✔
202
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
467,523✔
203
  return;
467,482✔
204
}
205

206
static int32_t tsdbCommitOpenReader(SCommitter2 *committer) {
467,466✔
207
  int32_t code = 0;
467,466✔
208
  int32_t lino = 0;
467,466✔
209

210
  if (committer->ctx->info->fset == NULL                        //
467,466✔
211
      || committer->sttTrigger > 1                              //
6,425✔
212
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
472✔
213
  ) {
214
    return 0;
467,425✔
215
  }
216

217
  SSttLvl *lvl;
218
  TARRAY2_FOREACH(committer->ctx->info->fset->lvlArr, lvl) {
82✔
219
    STFileObj *fobj = NULL;
41✔
220
    TARRAY2_FOREACH(lvl->fobjArr, fobj) {
82✔
221
      SSttFileReader *sttReader;
222

223
      SSttFileReaderConfig config = {
41✔
224
          .tsdb = committer->tsdb,
41✔
225
          .szPage = committer->szPage,
41✔
226
          .file = fobj->f[0],
227
      };
228

229
      TAOS_CHECK_GOTO(tsdbSttFileReaderOpen(fobj->fname, &config, &sttReader), &lino, _exit);
41!
230

231
      TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->sttReaderArray, sttReader), &lino, _exit);
82!
232

233
      STFileOp op = {
41✔
234
          .optype = TSDB_FOP_REMOVE,
235
          .fid = fobj->f->fid,
41✔
236
          .of = fobj->f[0],
237
      };
238

239
      TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->fopArray, op), &lino, _exit);
82!
240
    }
241
  }
242

243
_exit:
41✔
244
  if (code) {
41!
245
    tsdbCommitCloseReader(committer);
×
246
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
247
              tstrerror(code));
248
  }
249
  return code;
41✔
250
}
251

252
static void tsdbCommitCloseIter(SCommitter2 *committer) {
467,491✔
253
  tsdbIterMergerClose(&committer->tombIterMerger);
467,491✔
254
  tsdbIterMergerClose(&committer->dataIterMerger);
467,531✔
255
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
935,050!
256
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
935,096✔
257
  return;
467,528✔
258
}
259

260
static int32_t tsdbCommitOpenIter(SCommitter2 *committer) {
467,437✔
261
  int32_t code = 0;
467,437✔
262
  int32_t lino = 0;
467,437✔
263

264
  STsdbIter      *iter;
265
  STsdbIterConfig config = {0};
467,437✔
266

267
  // mem data iter
268
  config.type = TSDB_ITER_TYPE_MEMT;
467,437✔
269
  config.memt = committer->tsdb->imem;
467,437✔
270
  config.from->version = VERSION_MIN;
467,437✔
271
  config.from->key = (SRowKey){
467,437✔
272
      .ts = committer->ctx->minKey,
467,437✔
273
      .numOfPKs = 0,
274
  };
275

276
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
467,437!
277
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
934,977!
278

279
  // mem tomb iter
280
  config.type = TSDB_ITER_TYPE_MEMT_TOMB;
467,489✔
281
  config.memt = committer->tsdb->imem;
467,489✔
282

283
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
467,489!
284
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
935,070!
285

286
  // STT
287
  SSttFileReader *sttReader;
288
  TARRAY2_FOREACH(committer->sttReaderArray, sttReader) {
467,576✔
289
    // data iter
290
    config.type = TSDB_ITER_TYPE_STT;
41✔
291
    config.sttReader = sttReader;
41✔
292

293
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
41!
294
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
82!
295

296
    // tomb iter
297
    config.type = TSDB_ITER_TYPE_STT_TOMB;
41✔
298
    config.sttReader = sttReader;
41✔
299

300
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
41!
301

302
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
82!
303
  }
304

305
  // open merger
306
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
467,535!
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
467,488!
308

309
_exit:
467,519✔
310
  if (code) {
467,519!
311
    tsdbCommitCloseIter(committer);
×
312
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
313
              tstrerror(code));
314
  }
315
  return code;
467,506✔
316
}
317

318
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
467,472✔
319
  int32_t code = 0;
467,472✔
320
  int32_t lino = 0;
467,472✔
321
  STsdb  *tsdb = committer->tsdb;
467,472✔
322

323
  // check if can commit
324
  tsdbFSCheckCommit(tsdb, committer->ctx->info->fid);
467,472✔
325

326
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->info->fid, &tsdb->keepCfg, committer->now);
467,495✔
327
  tsdbFidKeyRange(committer->ctx->info->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
467,514✔
328
                  &committer->ctx->maxKey);
329

330
  committer->ctx->tbid->suid = 0;
467,505✔
331
  committer->ctx->tbid->uid = 0;
467,505✔
332

333
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
467,505!
334
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
467,482!
335
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
467,509!
336

337
_exit:
467,479✔
338
  if (code) {
467,479!
339
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
340
  } else {
341
    tsdbDebug("vgId:%d %s done, fid:%d minKey:%" PRId64 " maxKey:%" PRId64 " expLevel:%d", TD_VID(tsdb->pVnode),
467,479✔
342
              __func__, committer->ctx->info->fid, committer->ctx->minKey, committer->ctx->maxKey,
343
              committer->ctx->expLevel);
344
  }
345
  return code;
467,496✔
346
}
347

348
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
467,491✔
349
  int32_t code = 0;
467,491✔
350
  int32_t lino = 0;
467,491✔
351

352
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
467,491!
353
  tsdbCommitCloseIter(committer);
467,497✔
354
  tsdbCommitCloseReader(committer);
467,528✔
355

356
_exit:
467,477✔
357
  if (code) {
467,477!
358
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
359
              tstrerror(code));
360
  } else {
361
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->info->fid);
467,477✔
362
  }
363
  return code;
467,492✔
364
}
365

366
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
467,480✔
367
  int32_t code = 0;
467,480✔
368
  int32_t lino = 0;
467,480✔
369

370
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
467,480!
371
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
467,486!
372
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
467,489!
373
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
467,492!
374

375
_exit:
467,461✔
376
  if (code) {
467,461!
377
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
378
              tstrerror(code));
379
  } else {
380
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->info->fid);
467,461✔
381
  }
382
  return code;
467,473✔
383
}
384

385
static int32_t tFileSetCommitInfoCompare(const void *arg1, const void *arg2) {
2,147,483,647✔
386
  SFileSetCommitInfo *info1 = (SFileSetCommitInfo *)arg1;
2,147,483,647✔
387
  SFileSetCommitInfo *info2 = (SFileSetCommitInfo *)arg2;
2,147,483,647✔
388

389
  if (info1->fid < info2->fid) {
2,147,483,647!
390
    return -1;
2,147,483,647✔
UNCOV
391
  } else if (info1->fid > info2->fid) {
×
392
    return 1;
2,993,718✔
393
  } else {
UNCOV
394
    return 0;
×
395
  }
396
}
397

398
static int32_t tFileSetCommitInfoPCompare(const void *arg1, const void *arg2) {
2,147,483,647✔
399
  return tFileSetCommitInfoCompare(*(SFileSetCommitInfo **)arg1, *(SFileSetCommitInfo **)arg2);
2,147,483,647✔
400
}
401

402
static uint32_t tFileSetCommitInfoHash(const void *arg) {
8,304,918✔
403
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
8,304,918✔
404
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
8,304,918✔
405
}
406

407
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
10,258✔
408
  if (pTsdb->commitInfo) {
10,258!
409
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
477,721✔
410
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
467,464✔
411
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
467,473✔
412
      tsdbTFileSetClear(&info->fset);
467,440✔
413
      taosMemoryFree(info);
467,439!
414
    }
415

416
    vHashDestroy(&pTsdb->commitInfo->ht);
10,257✔
417
    taosArrayDestroy(pTsdb->commitInfo->arr);
10,258✔
418
    pTsdb->commitInfo->arr = NULL;
10,258✔
419
    taosMemoryFreeClear(pTsdb->commitInfo);
10,258!
420
  }
421
  return;
10,258✔
422
}
423

424
static int32_t tsdbCommitInfoInit(STsdb *pTsdb) {
10,258✔
425
  int32_t code = 0;
10,258✔
426
  int32_t lino = 0;
10,258✔
427

428
  pTsdb->commitInfo = taosMemoryCalloc(1, sizeof(*pTsdb->commitInfo));
10,258!
429
  if (pTsdb->commitInfo == NULL) {
10,258!
430
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
431
  }
432

433
  TAOS_CHECK_GOTO(vHashInit(&pTsdb->commitInfo->ht, tFileSetCommitInfoHash, tFileSetCommitInfoCompare), &lino, _exit);
10,258!
434

435
  pTsdb->commitInfo->arr = taosArrayInit(0, sizeof(SFileSetCommitInfo *));
10,258✔
436
  if (pTsdb->commitInfo->arr == NULL) {
10,258!
437
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
438
  }
439

440
_exit:
10,258✔
441
  if (code) {
10,258!
442
    tsdbCommitInfoDestroy(pTsdb);
×
443
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
444
  }
445
  return code;
10,258✔
446
}
447

448
static int32_t tsdbCommitInfoAdd(STsdb *tsdb, int32_t fid) {
466,938✔
449
  int32_t code = 0;
466,938✔
450
  int32_t lino = 0;
466,938✔
451

452
  SFileSetCommitInfo *tinfo;
453

454
  if ((tinfo = taosMemoryMalloc(sizeof(*tinfo))) == NULL) {
466,938!
455
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
456
  }
457
  tinfo->fid = fid;
467,485✔
458
  tinfo->fset = NULL;
467,485✔
459

460
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
467,485!
461

462
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
934,867!
463
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
464
  }
465
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
467,413✔
466

467
_exit:
467,426✔
468
  if (code) {
467,426!
469
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
470
  }
471
  return code;
467,432✔
472
}
473

474
static int32_t tsdbCommitInfoBuild(STsdb *tsdb) {
10,258✔
475
  int32_t code = 0;
10,258✔
476
  int32_t lino = 0;
10,258✔
477

478
  STFileSet  *fset = NULL;
10,258✔
479
  SRBTreeIter iter;
480

481
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
10,258!
482

483
  // scan time-series data
484
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
10,258✔
485
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
140,689✔
486
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
130,431✔
487

488
    // scan time-series data
489
    STsdbRowKey from = {
130,431✔
490
        .key.ts = INT64_MIN,
491
        .key.numOfPKs = 0,
492
        .version = INT64_MIN,
493
    };
494
    for (;;) {
6,780,418✔
495
      int64_t     minKey, maxKey;
496
      STbDataIter tbDataIter = {0};
6,910,849✔
497
      TSDBROW    *row;
498
      int32_t     fid;
499

500
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
6,910,849✔
501
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
6,925,498✔
502
        break;
129,357✔
503
      }
504

505
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
6,796,141✔
506
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
6,805,298✔
507

508
      SFileSetCommitInfo *info;
509
      SFileSetCommitInfo  tinfo = {
6,805,160✔
510
           .fid = fid,
511
      };
512
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
6,805,160✔
513
      if (info == NULL) {
6,779,916✔
514
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
466,521!
515
      }
516

517
      from.key.ts = maxKey + 1;
6,780,418✔
518
    }
519
  }
520

521
  (void)taosThreadMutexLock(&tsdb->mutex);
10,258✔
522

523
  // scan tomb data
524
  if (tsdb->imem->nDel > 0) {
10,258✔
525
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
6,845✔
526
      if (tsdbTFileSetIsEmpty(fset)) {
3,517!
527
        continue;
3,040✔
528
      }
529

530
      SFileSetCommitInfo *info;
531
      SFileSetCommitInfo  tinfo = {
3,517✔
532
           .fid = fset->fid,
3,517✔
533
      };
534

535
      // check if the file set already on the commit list
536
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
3,517✔
537
      if (info != NULL) {
3,517✔
538
        continue;
3,040✔
539
      }
540

541
      int64_t minKey, maxKey;
542
      bool    hasDataToCommit = false;
477✔
543
      tsdbFidKeyRange(fset->fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
477✔
544
      iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
477✔
545
      for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
651✔
546
        STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
577✔
547
        for (SDelData *pDelData = pTbData->pHead; pDelData; pDelData = pDelData->pNext) {
721✔
548
          if (pDelData->sKey > maxKey || pDelData->eKey < minKey) {
547!
549
            continue;
144✔
550
          } else {
551
            hasDataToCommit = true;
403✔
552
            if ((code = tsdbCommitInfoAdd(tsdb, fset->fid))) {
403!
553
              (void)taosThreadMutexUnlock(&tsdb->mutex);
×
554
              TSDB_CHECK_CODE(code, lino, _exit);
×
555
            }
556
            break;
403✔
557
          }
558
        }
559

560
        if (hasDataToCommit) {
577✔
561
          break;
403✔
562
        }
563
      }
564
    }
565
  }
566

567
  // begin tasks on file set
568
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
477,773✔
569
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
467,473✔
570
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
467,474✔
571
    if (fset) {
467,515✔
572
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
6,425✔
573
      if (code) {
6,425!
574
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
575
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
576
      }
577
    }
578
  }
579

580
  (void)taosThreadMutexUnlock(&tsdb->mutex);
10,257✔
581

582
_exit:
10,258✔
583
  if (code) {
10,258!
584
    tsdbCommitInfoDestroy(tsdb);
×
585
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
586
  }
587
  return code;
10,258✔
588
}
589

590
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
10,258✔
591
  int32_t code = 0;
10,258✔
592
  int32_t lino = 0;
10,258✔
593

594
  committer->tsdb = tsdb;
10,258✔
595
  committer->minutes = tsdb->keepCfg.days;
10,258✔
596
  committer->precision = tsdb->keepCfg.precision;
10,258✔
597
  committer->minRow = info->info.config.tsdbCfg.minRows;
10,258✔
598
  committer->maxRow = info->info.config.tsdbCfg.maxRows;
10,258✔
599
  committer->cmprAlg = info->info.config.tsdbCfg.compression;
10,258✔
600
  committer->sttTrigger = info->info.config.sttTrigger;
10,258✔
601
  committer->szPage = info->info.config.tsdbPageSize;
10,258✔
602
  committer->compactVersion = INT64_MAX;
10,258✔
603
  committer->cid = tsdbFSAllocEid(tsdb->pFS);
10,258✔
604
  committer->now = taosGetTimestampSec();
10,258✔
605

606
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
10,258!
607

608
_exit:
10,258✔
609
  if (code) {
10,258!
610
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
611
  } else {
612
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
10,258✔
613
  }
614
  return code;
10,258✔
615
}
616

617
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
10,258✔
618
  int32_t code = 0;
10,258✔
619
  int32_t lino = 0;
10,258✔
620

621
  if (eno == 0) {
10,258!
622
    TAOS_CHECK_GOTO(tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT), &lino, _exit);
10,258!
623
  } else {
624
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
625
              tstrerror(eno));
626
  }
627

628
  TARRAY2_DESTROY(committer->dataIterArray, NULL);
10,258!
629
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
10,258!
630
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
10,258!
631
  TARRAY2_DESTROY(committer->fopArray, NULL);
10,258!
632
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
10,258!
633

634
_exit:
10,258✔
635
  if (code) {
10,258!
636
    tsdbError("vgId:%d %s failed at %s:%d since %s, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, __FILE__,
×
637
              lino, tstrerror(code), committer->cid);
638
  } else {
639
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, committer->cid);
10,258✔
640
  }
641
  return code;
10,258✔
642
}
643

644
int32_t tsdbPreCommit(STsdb *tsdb) {
25,655✔
645
  (void)taosThreadMutexLock(&tsdb->mutex);
25,655✔
646
  ASSERT_CORE(tsdb->imem == NULL, "imem should be null to commit mem");
25,655!
647
  tsdb->imem = tsdb->mem;
25,655✔
648
  tsdb->mem = NULL;
25,655✔
649
  (void)taosThreadMutexUnlock(&tsdb->mutex);
25,655✔
650
  return 0;
25,655✔
651
}
652

653
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
25,654✔
654
  if (!tsdb) return 0;
25,654!
655

656
  int32_t code = 0;
25,654✔
657
  int32_t lino = 0;
25,654✔
658

659
  SMemTable *imem = tsdb->imem;
25,654✔
660
  int64_t    nRow = imem->nRow;
25,654✔
661
  int64_t    nDel = imem->nDel;
25,654✔
662

663
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
25,654!
664
    (void)taosThreadMutexLock(&tsdb->mutex);
15,396✔
665
    tsdb->imem = NULL;
15,397✔
666
    (void)taosThreadMutexUnlock(&tsdb->mutex);
15,397✔
667
    tsdbUnrefMemTable(imem, NULL, true);
15,397✔
668
  } else {
669
    SCommitter2 committer = {0};
10,258✔
670

671
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
10,258!
672

673
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
477,724✔
674
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
467,491✔
675
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
467,468!
676
    }
677

678
    TAOS_CHECK_GOTO(tsdbCloseCommitter(&committer, code), &lino, _exit);
10,257!
679
  }
680

681
_exit:
25,655✔
682
  if (code) {
25,655!
683
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
684
  } else {
685
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, TD_VID(tsdb->pVnode), __func__, nRow, nDel);
25,655✔
686
  }
687
  return code;
25,655✔
688
}
689

690
int32_t tsdbCommitCommit(STsdb *tsdb) {
25,655✔
691
  int32_t code = 0;
25,655✔
692
  int32_t lino = 0;
25,655✔
693

694
  if (tsdb->imem) {
25,655✔
695
    SMemTable *pMemTable = tsdb->imem;
10,258✔
696

697
    (void)taosThreadMutexLock(&tsdb->mutex);
10,258✔
698

699
    if ((code = tsdbFSEditCommit(tsdb->pFS))) {
10,258!
700
      (void)taosThreadMutexUnlock(&tsdb->mutex);
×
701
      TSDB_CHECK_CODE(code, lino, _exit);
×
702
    }
703
    tsdb->imem = NULL;
10,258✔
704

705
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
477,797✔
706
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
467,539✔
707
      if (info->fset) {
467,539✔
708
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
6,425✔
709
      }
710
    }
711

712
    (void)taosThreadMutexUnlock(&tsdb->mutex);
10,258✔
713

714
    tsdbCommitInfoDestroy(tsdb);
10,258✔
715
    tsdbUnrefMemTable(pMemTable, NULL, true);
10,258✔
716
  }
717

718
_exit:
15,397✔
719
  if (code) {
25,655!
720
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
721
  } else {
722
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
25,655✔
723
  }
724
  return code;
25,655✔
725
}
726

727
int32_t tsdbCommitAbort(STsdb *pTsdb) {
×
728
  int32_t code = 0;
×
729
  int32_t lino = 0;
×
730

731
  if (pTsdb->imem == NULL) goto _exit;
×
732

733
  TAOS_CHECK_GOTO(tsdbFSEditAbort(pTsdb->pFS), &lino, _exit);
×
734

735
  (void)taosThreadMutexLock(&pTsdb->mutex);
×
736
  for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
×
737
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
×
738
    if (info->fset) {
×
739
      tsdbFinishTaskOnFileSet(pTsdb, info->fid, EVA_TASK_COMMIT);
×
740
    }
741
  }
742
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
743
  tsdbCommitInfoDestroy(pTsdb);
×
744

745
_exit:
×
746
  if (code) {
×
747
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
748
  } else {
749
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
×
750
  }
751
  return code;
×
752
}
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