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

taosdata / TDengine / #4835

31 Oct 2025 03:37AM UTC coverage: 58.506% (-0.3%) from 58.764%
#4835

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149104 of 324176 branches covered (45.99%)

Branch coverage included in aggregate %.

198232 of 269498 relevant lines covered (73.56%)

236558065.91 hits per line

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

74.72
/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) {
127,517,526✔
64
  int32_t code = 0;
127,517,526✔
65
  int32_t lino = 0;
127,517,526✔
66

67
  SFSetWriterConfig config = {
127,579,150✔
68
      .tsdb = committer->tsdb,
127,604,530✔
69
      .toSttOnly = true,
70
      .compactVersion = committer->compactVersion,
127,624,088✔
71
      .minRow = committer->minRow,
127,627,708✔
72
      .maxRow = committer->maxRow,
127,637,687✔
73
      .szPage = committer->szPage,
127,623,064✔
74
      .cmprAlg = committer->cmprAlg,
127,612,192✔
75
      .fid = committer->ctx->info->fid,
127,601,968✔
76
      .cid = committer->cid,
127,614,769✔
77
      .expLevel = committer->ctx->expLevel,
127,612,181✔
78
      .level = 0,
79
  };
80

81
  if (committer->sttTrigger == 1) {
127,598,122✔
82
    config.toSttOnly = false;
2,080,986✔
83

84
    if (committer->ctx->info->fset) {
2,080,986✔
85
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
3,146,705✔
86
        if (committer->ctx->info->fset->farr[ftype] != NULL) {
2,517,364✔
87
          config.files[ftype].exist = true;
1,866,833✔
88
          config.files[ftype].file = committer->ctx->info->fset->farr[ftype]->f[0];
1,866,833✔
89
        }
90
      }
91
    }
92
  }
93

94
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
127,601,159!
95

96
_exit:
127,594,214✔
97
  if (code) {
127,610,602!
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;
127,610,602✔
102
}
103

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

108
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
127,539,526✔
109
  int32_t   code = 0;
127,539,526✔
110
  int32_t   lino = 0;
127,539,526✔
111
  int64_t   numOfRow = 0;
127,573,420✔
112
  SMetaInfo info;
127,571,990✔
113

114
  committer->ctx->hasTSData = false;
127,590,536✔
115

116
  committer->ctx->tbid->suid = 0;
127,628,860✔
117
  committer->ctx->tbid->uid = 0;
127,609,578✔
118
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
2,147,483,647✔
119
    if (row->uid != committer->ctx->tbid->uid) {
2,147,483,647✔
120
      committer->ctx->tbid->suid = row->suid;
450,531,075✔
121
      committer->ctx->tbid->uid = row->uid;
450,490,841✔
122

123
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
450,582,397✔
124
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
3,875,458!
125
        continue;
3,735,429✔
126
      }
127
    }
128

129
    int64_t ts = TSDBROW_TS(&row->row);
2,147,483,647✔
130
    if (ts > committer->ctx->maxKey) {
2,147,483,647✔
131
      TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
321,132,774!
132
      continue;
321,109,559✔
133
    }
134

135
    committer->ctx->hasTSData = true;
2,147,483,647✔
136
    numOfRow++;
2,147,483,647✔
137

138
    TAOS_CHECK_GOTO(tsdbFSetWriteRow(committer->writer, row), &lino, _exit);
2,147,483,647!
139
    TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->dataIterMerger), &lino, _exit);
2,147,483,647!
140
  }
141

142
_exit:
127,666,313✔
143
  if (code) {
127,637,561!
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,
127,637,561✔
148
              numOfRow);
149
  }
150
  return code;
127,637,561✔
151
}
152

153
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
127,640,135✔
154
  int32_t   code = 0;
127,640,135✔
155
  int32_t   lino = 0;
127,640,135✔
156
  int64_t   numRecord = 0;
127,648,620✔
157
  SMetaInfo info;
127,647,190✔
158

159
  // if no history data and no new timestamp data, skip tomb data
160
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
127,650,725!
161
    committer->ctx->tbid->suid = 0;
127,614,039✔
162
    committer->ctx->tbid->uid = 0;
127,624,889✔
163
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
133,981,732✔
164
      if (record->uid != committer->ctx->tbid->uid) {
6,358,430✔
165
        committer->ctx->tbid->suid = record->suid;
6,213,948✔
166
        committer->ctx->tbid->uid = record->uid;
6,213,948✔
167

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

174
      if (record->ekey < committer->ctx->minKey) {
6,317,230✔
175
        // do nothing
176
      } else if (record->skey > committer->ctx->maxKey) {
6,286,150✔
177
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
178
      } else {
179
        record->skey = TMAX(record->skey, committer->ctx->minKey);
6,254,770✔
180
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
6,254,770✔
181

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

186
      TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->tombIterMerger), &lino, _exit);
6,317,230!
187
    }
188
  }
189

190
_exit:
127,667,355✔
191
  if (code) {
127,663,414!
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),
127,663,414✔
196
              committer->ctx->info->fid, numRecord);
197
  }
198
  return code;
127,663,414✔
199
}
200

201
static void tsdbCommitCloseReader(SCommitter2 *committer) {
127,572,903✔
202
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
127,583,946✔
203
  return;
127,584,645✔
204
}
205

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

210
  if (committer->ctx->info->fset == NULL                        //
127,589,174✔
211
      || committer->sttTrigger > 1                              //
6,641,592✔
212
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
629,341✔
213
  ) {
214
    return 0;
127,536,371✔
215
  }
216

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

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

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

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

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

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

243
_exit:
11,043✔
244
  if (code) {
11,043!
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;
11,043✔
250
}
251

252
static void tsdbCommitCloseIter(SCommitter2 *committer) {
127,596,776✔
253
  tsdbIterMergerClose(&committer->tombIterMerger);
127,596,776✔
254
  tsdbIterMergerClose(&committer->dataIterMerger);
127,598,190✔
255
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
255,246,921!
256
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
255,256,010!
257
  return;
127,637,301✔
258
}
259

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

264
  STsdbIter      *iter;
127,586,504✔
265
  STsdbIterConfig config = {0};
127,608,130✔
266

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

276
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
127,612,755!
277
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
255,278,420!
278

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

283
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
127,630,949!
284
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
255,261,575!
285

286
  // STT
287
  SSttFileReader *sttReader;
288
  TARRAY2_FOREACH(committer->sttReaderArray, sttReader) {
127,643,187✔
289
    // data iter
290
    config.type = TSDB_ITER_TYPE_STT;
11,043✔
291
    config.sttReader = sttReader;
11,043✔
292

293
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
11,043!
294
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
22,086!
295

296
    // tomb iter
297
    config.type = TSDB_ITER_TYPE_STT_TOMB;
11,043✔
298
    config.sttReader = sttReader;
11,043✔
299

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

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

305
  // open merger
306
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
127,607,588!
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
127,603,005!
308

309
_exit:
127,652,550✔
310
  if (code) {
127,598,666!
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;
127,598,666✔
316
}
317

318
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
127,622,267✔
319
  int32_t code = 0;
127,622,267✔
320
  int32_t lino = 0;
127,622,267✔
321
  STsdb  *tsdb = committer->tsdb;
127,635,961✔
322

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

326
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->info->fid, &tsdb->keepCfg, committer->now);
127,557,104✔
327
  tsdbFidKeyRange(committer->ctx->info->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
127,560,478✔
328
                  &committer->ctx->maxKey);
329

330
  committer->ctx->tbid->suid = 0;
127,606,373✔
331
  committer->ctx->tbid->uid = 0;
127,614,335✔
332

333
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
127,562,627!
334
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
127,552,827!
335
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
127,589,188!
336

337
_exit:
127,572,775✔
338
  if (code) {
127,577,004!
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),
127,577,004✔
342
              __func__, committer->ctx->info->fid, committer->ctx->minKey, committer->ctx->maxKey,
343
              committer->ctx->expLevel);
344
  }
345
  return code;
127,577,026✔
346
}
347

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

352
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
127,642,853!
353
  tsdbCommitCloseIter(committer);
127,602,227✔
354
  tsdbCommitCloseReader(committer);
127,632,832✔
355

356
_exit:
127,582,208✔
357
  if (code) {
127,589,815!
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);
127,589,815✔
362
  }
363
  return code;
127,589,815✔
364
}
365

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

370
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
127,635,915!
371
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
127,549,290!
372
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
127,630,168!
373
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
127,657,037!
374

375
_exit:
127,571,968✔
376
  if (code) {
127,597,206!
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);
127,597,206✔
381
  }
382
  return code;
127,597,206✔
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✔
391
  } else if (info1->fid > info2->fid) {
512,644,170✔
392
    return 1;
78,576,129✔
393
  } else {
394
    return 0;
434,088,919✔
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) {
791,240,076✔
403
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
791,240,076✔
404
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
791,240,076✔
405
}
406

407
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
8,955,642✔
408
  if (pTsdb->commitInfo) {
8,955,642!
409
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
136,629,323✔
410
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
127,675,205✔
411
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
127,675,205✔
412
      tsdbTFileSetClear(&info->fset);
127,673,681✔
413
      taosMemoryFree(info);
127,675,205!
414
    }
415

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

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

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

433
  TAOS_CHECK_GOTO(vHashInit(&pTsdb->commitInfo->ht, tFileSetCommitInfoHash, tFileSetCommitInfoCompare), &lino, _exit);
8,950,955!
434

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

440
_exit:
8,955,494✔
441
  if (code) {
8,953,855!
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;
8,953,855✔
446
}
447

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

452
  SFileSetCommitInfo *tinfo;
127,651,577✔
453

454
  if ((tinfo = taosMemoryMalloc(sizeof(*tinfo))) == NULL) {
127,655,754!
455
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
456
  }
457
  tinfo->fid = fid;
127,646,736✔
458
  tinfo->fset = NULL;
127,634,379✔
459

460
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
127,634,628!
461

462
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
255,282,351!
463
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
464
  }
465
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
127,634,836✔
466

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

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

478
  STFileSet  *fset = NULL;
8,953,261✔
479
  SRBTreeIter iter;
8,951,866✔
480

481
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
8,953,398!
482

483
  // scan time-series data
484
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
8,953,720✔
485
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
138,857,890✔
486
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
129,941,696✔
487

488
    // scan time-series data
489
    STsdbRowKey from = {
129,944,190✔
490
        .key.ts = INT64_MIN,
491
        .key.numOfPKs = 0,
492
        .version = INT64_MIN,
493
    };
494
    for (;;) {
430,391,765✔
495
      int64_t     minKey, maxKey;
560,320,898✔
496
      STbDataIter tbDataIter = {0};
559,698,905✔
497
      TSDBROW    *row;
498
      int32_t     fid;
499

500
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
560,165,517✔
501
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
560,371,671✔
502
        break;
129,924,348✔
503
      }
504

505
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
430,447,323✔
506
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
430,439,584✔
507

508
      SFileSetCommitInfo *info;
430,390,596✔
509
      SFileSetCommitInfo  tinfo = {
430,402,491✔
510
           .fid = fid,
511
      };
512
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
430,411,117✔
513
      if (info == NULL) {
430,317,041✔
514
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
126,995,456!
515
      }
516

517
      from.key.ts = maxKey + 1;
430,289,647✔
518
    }
519
  }
520

521
  (void)taosThreadMutexLock(&tsdb->mutex);
8,955,642✔
522

523
  // scan tomb data
524
  if (tsdb->imem->nDel > 0) {
8,955,642✔
525
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
7,168,621✔
526
      if (tsdbTFileSetIsEmpty(fset)) {
3,738,807!
527
        continue;
×
528
      }
529

530
      SFileSetCommitInfo *info;
3,738,807✔
531
      SFileSetCommitInfo  tinfo = {
3,738,807✔
532
           .fid = fset->fid,
3,738,807✔
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,738,807✔
537
      if (info != NULL) {
3,738,807✔
538
        continue;
2,978,635✔
539
      }
540

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

560
        if (hasDataToCommit) {
909,770✔
561
          break;
658,280✔
562
        }
563
      }
564
    }
565
  }
566

567
  // begin tasks on file set
568
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
136,628,489✔
569
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
127,673,143✔
570
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
127,672,112✔
571
    if (fset) {
127,672,995✔
572
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
6,641,592✔
573
      if (code) {
6,640,911!
574
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
575
        TAOS_CHECK_GOTO(code, &lino, _exit);
681!
576
      }
577
    }
578
  }
579

580
  (void)taosThreadMutexUnlock(&tsdb->mutex);
8,954,961✔
581

582
_exit:
8,952,780✔
583
  if (code) {
8,954,961!
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;
8,954,961✔
588
}
589

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

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

606
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
8,955,642!
607

608
_exit:
8,955,642✔
609
  if (code) {
8,955,642!
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__);
8,955,642✔
613
  }
614
  return code;
8,955,642✔
615
}
616

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

621
  if (eno == 0) {
8,954,003!
622
    TAOS_CHECK_GOTO(tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT), &lino, _exit);
8,954,003!
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);
8,955,642!
629
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
8,955,642!
630
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
8,955,642!
631
  TARRAY2_DESTROY(committer->fopArray, NULL);
8,955,642!
632
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
8,955,642!
633

634
_exit:
8,955,642✔
635
  if (code) {
8,955,642!
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);
8,955,642✔
640
  }
641
  return code;
8,955,642✔
642
}
643

644
int32_t tsdbPreCommit(STsdb *tsdb) {
20,784,781✔
645
  (void)taosThreadMutexLock(&tsdb->mutex);
20,784,781✔
646
  ASSERT_CORE(tsdb->imem == NULL, "imem should be null to commit mem");
20,839,812!
647
  tsdb->imem = tsdb->mem;
20,841,417✔
648
  tsdb->mem = NULL;
20,839,986✔
649
  (void)taosThreadMutexUnlock(&tsdb->mutex);
20,840,027✔
650
  return 0;
20,842,874✔
651
}
652

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

656
  int32_t code = 0;
20,841,058✔
657
  int32_t lino = 0;
20,841,058✔
658

659
  SMemTable *imem = tsdb->imem;
20,842,874✔
660
  int64_t    nRow = imem->nRow;
20,842,874✔
661
  int64_t    nDel = imem->nDel;
20,842,874✔
662

663
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
20,842,874✔
664
    (void)taosThreadMutexLock(&tsdb->mutex);
11,887,232✔
665
    tsdb->imem = NULL;
11,887,232✔
666
    (void)taosThreadMutexUnlock(&tsdb->mutex);
11,887,232✔
667
    tsdbUnrefMemTable(imem, NULL, true);
11,887,232✔
668
  } else {
669
    SCommitter2 committer = {0};
8,955,642✔
670

671
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
8,955,642!
672

673
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
136,533,945✔
674
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
127,635,947✔
675
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
127,644,327!
676
    }
677

678
    TAOS_CHECK_GOTO(tsdbCloseCommitter(&committer, code), &lino, _exit);
8,954,586!
679
  }
680

681
_exit:
20,841,420✔
682
  if (code) {
20,841,420!
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);
20,841,420!
686
  }
687
  return code;
20,842,874✔
688
}
689

690
int32_t tsdbCommitCommit(STsdb *tsdb) {
20,842,874✔
691
  int32_t code = 0;
20,842,874✔
692
  int32_t lino = 0;
20,842,874✔
693

694
  if (tsdb->imem) {
20,842,874✔
695
    SMemTable *pMemTable = tsdb->imem;
8,955,642✔
696

697
    (void)taosThreadMutexLock(&tsdb->mutex);
8,955,642✔
698

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

705
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
136,630,847✔
706
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
127,675,205✔
707
      if (info->fset) {
127,675,205✔
708
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
6,641,592✔
709
      }
710
    }
711

712
    (void)taosThreadMutexUnlock(&tsdb->mutex);
8,955,642✔
713

714
    tsdbCommitInfoDestroy(tsdb);
8,955,642✔
715
    tsdbUnrefMemTable(pMemTable, NULL, true);
8,955,642✔
716
  }
717

718
_exit:
11,887,232✔
719
  if (code) {
20,839,786!
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__);
20,839,786!
723
  }
724
  return code;
20,842,555✔
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