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

taosdata / TDengine / #5012

03 Apr 2026 03:59PM UTC coverage: 72.305% (+0.005%) from 72.3%
#5012

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

13105 existing lines in 173 files now uncovered.

257446 of 356056 relevant lines covered (72.3%)

131779375.07 hits per line

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

88.38
/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) {
18,348,494✔
64
  int32_t code = 0;
18,348,494✔
65
  int32_t lino = 0;
18,348,494✔
66

67
  SFSetWriterConfig config = {
18,361,909✔
68
      .tsdb = committer->tsdb,
18,369,097✔
69
      .toSttOnly = true,
70
      .compactVersion = committer->compactVersion,
18,360,744✔
71
      .minRow = committer->minRow,
18,364,654✔
72
      .maxRow = committer->maxRow,
18,363,641✔
73
      .szPage = committer->szPage,
18,362,648✔
74
      .cmprAlg = committer->cmprAlg,
18,361,349✔
75
      .fid = committer->ctx->info->fid,
18,361,549✔
76
      .cid = committer->cid,
18,342,966✔
77
      .expLevel = committer->ctx->expLevel,
18,360,015✔
78
      .level = 0,
79
  };
80

81
  if (committer->sttTrigger == 1) {
18,357,161✔
82
    config.toSttOnly = false;
3,696,176✔
83

84
    if (committer->ctx->info->fset) {
3,696,176✔
85
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
945,485✔
86
        if (committer->ctx->info->fset->farr[ftype] != NULL) {
756,388✔
87
          config.files[ftype].exist = true;
558,979✔
88
          config.files[ftype].file = committer->ctx->info->fset->farr[ftype]->f[0];
558,979✔
89
        }
90
      }
91
    }
92
  }
93

94
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
18,357,487✔
95

96
_exit:
18,354,977✔
97
  if (code) {
18,359,078✔
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;
18,359,078✔
102
}
103

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

108
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
18,356,397✔
109
  int32_t   code = 0;
18,356,397✔
110
  int32_t   lino = 0;
18,356,397✔
111
  int64_t   numOfRow = 0;
18,361,108✔
112
  SMetaInfo info;
18,360,722✔
113

114
  committer->ctx->hasTSData = false;
18,361,721✔
115

116
  committer->ctx->tbid->suid = 0;
18,365,447✔
117
  committer->ctx->tbid->uid = 0;
18,363,421✔
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;
112,175,406✔
121
      committer->ctx->tbid->uid = row->uid;
112,180,124✔
122

123
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
112,168,068✔
124
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
1,552,031✔
125
        continue;
1,551,716✔
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);
47,410,246✔
132
      continue;
47,377,079✔
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:
18,376,208✔
143
  if (code) {
18,372,267✔
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,
18,372,267✔
148
              numOfRow);
149
  }
150
  return code;
18,372,267✔
151
}
152

153
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
18,370,347✔
154
  int32_t   code = 0;
18,370,347✔
155
  int32_t   lino = 0;
18,370,347✔
156
  int64_t   numRecord = 0;
18,370,347✔
157
  SMetaInfo info;
18,369,961✔
158

159
  // if no history data and no new timestamp data, skip tomb data
160
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
18,370,756✔
161
    committer->ctx->tbid->suid = 0;
18,356,551✔
162
    committer->ctx->tbid->uid = 0;
18,358,230✔
163
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
20,293,276✔
164
      if (record->uid != committer->ctx->tbid->uid) {
1,935,390✔
165
        committer->ctx->tbid->suid = record->suid;
1,897,172✔
166
        committer->ctx->tbid->uid = record->uid;
1,897,497✔
167

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

174
      if (record->ekey < committer->ctx->minKey) {
1,917,652✔
175
        // do nothing
176
      } else if (record->skey > committer->ctx->maxKey) {
1,905,669✔
177
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
178
      } else {
179
        record->skey = TMAX(record->skey, committer->ctx->minKey);
1,897,024✔
180
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
1,897,024✔
181

182
        numRecord++;
1,897,024✔
183
        TAOS_CHECK_GOTO(tsdbFSetWriteTombRecord(committer->writer, record), &lino, _exit);
1,897,024✔
184
      }
185

186
      TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->tombIterMerger), &lino, _exit);
1,917,327✔
187
    }
188
  }
189

190
_exit:
18,374,095✔
191
  if (code) {
18,374,712✔
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),
18,374,712✔
196
              committer->ctx->info->fid, numRecord);
197
  }
198
  return code;
18,374,712✔
199
}
200

201
static void tsdbCommitCloseReader(SCommitter2 *committer) {
18,360,241✔
202
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
18,366,527✔
203
  return;
18,361,945✔
204
}
205

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

210
  if (committer->ctx->info->fset == NULL                        //
18,356,777✔
211
      || committer->sttTrigger > 1                              //
2,223,438✔
212
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
189,097✔
213
  ) {
214
    return 0;
18,344,611✔
215
  }
216

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

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

229
      TAOS_CHECK_GOTO(tsdbSttFileReaderOpen(fobj->fname, &config, &sttReader), &lino, _exit);
6,286✔
230

231
      TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->sttReaderArray, sttReader), &lino, _exit);
12,572✔
232

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

239
      TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->fopArray, op), &lino, _exit);
12,572✔
240
    }
241
  }
242

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

252
static void tsdbCommitCloseIter(SCommitter2 *committer) {
18,364,620✔
253
  tsdbIterMergerClose(&committer->tombIterMerger);
18,364,620✔
254
  tsdbIterMergerClose(&committer->dataIterMerger);
18,358,714✔
255
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
36,730,728✔
256
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
36,729,914✔
257
  return;
18,368,948✔
258
}
259

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

264
  STsdbIter      *iter;
18,359,748✔
265
  STsdbIterConfig config = {0};
18,361,741✔
266

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

276
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
18,352,392✔
277
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
36,732,453✔
278

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

283
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
18,364,252✔
284
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
36,732,963✔
285

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

293
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
6,286✔
294
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
12,572✔
295

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

300
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
6,286✔
301

302
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
12,572✔
303
  }
304

305
  // open merger
306
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
18,354,388✔
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
18,355,198✔
308

309
_exit:
18,373,654✔
310
  if (code) {
18,374,284✔
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;
18,374,284✔
316
}
317

318
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
18,369,901✔
319
  int32_t code = 0;
18,369,901✔
320
  int32_t lino = 0;
18,369,901✔
321
  STsdb  *tsdb = committer->tsdb;
18,370,985✔
322

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

326
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->info->fid, &tsdb->keepCfg, committer->now);
18,359,663✔
327
  tsdbFidKeyRange(committer->ctx->info->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
18,361,253✔
328
                  &committer->ctx->maxKey);
329

330
  committer->ctx->tbid->suid = 0;
18,349,161✔
331
  committer->ctx->tbid->uid = 0;
18,354,666✔
332

333
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
18,357,682✔
334
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
18,346,124✔
335
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
18,369,153✔
336

337
_exit:
18,360,834✔
338
  if (code) {
18,361,400✔
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),
18,361,400✔
342
              __func__, committer->ctx->info->fid, committer->ctx->minKey, committer->ctx->maxKey,
343
              committer->ctx->expLevel);
344
  }
345
  return code;
18,361,400✔
346
}
347

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

352
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
18,370,264✔
353
  tsdbCommitCloseIter(committer);
18,362,200✔
354
  tsdbCommitCloseReader(committer);
18,363,018✔
355

356
_exit:
18,357,850✔
357
  if (code) {
18,361,007✔
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);
18,361,007✔
362
  }
363
  return code;
18,361,007✔
364
}
365

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

370
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
18,369,920✔
371
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
18,356,334✔
372
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
18,370,347✔
373
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
18,372,869✔
374

375
_exit:
18,357,233✔
376
  if (code) {
18,358,642✔
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);
18,358,642✔
381
  }
382
  return code;
18,358,642✔
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) {
117,044,195✔
392
    return 1;
10,394,542✔
393
  } else {
394
    return 0;
106,648,431✔
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) {
155,607,516✔
403
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
155,607,516✔
404
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
155,607,516✔
405
}
406

407
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
3,057,795✔
408
  if (pTsdb->commitInfo) {
3,057,795✔
409
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
21,434,611✔
410
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
18,376,816✔
411
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
18,376,816✔
412
      tsdbTFileSetClear(&info->fset);
18,376,816✔
413
      taosMemoryFree(info);
18,376,816✔
414
    }
415

416
    vHashDestroy(&pTsdb->commitInfo->ht);
3,057,795✔
417
    taosArrayDestroy(pTsdb->commitInfo->arr);
3,057,318✔
418
    pTsdb->commitInfo->arr = NULL;
3,057,318✔
419
    taosMemoryFreeClear(pTsdb->commitInfo);
3,057,318✔
420
  }
421
  return;
3,057,795✔
422
}
423

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

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

433
  TAOS_CHECK_GOTO(vHashInit(&pTsdb->commitInfo->ht, tFileSetCommitInfoHash, tFileSetCommitInfoCompare), &lino, _exit);
3,057,795✔
434

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

440
_exit:
3,057,060✔
441
  if (code) {
3,057,060✔
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;
3,057,060✔
446
}
447

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

452
  SFileSetCommitInfo *tinfo;
18,375,363✔
453

454
  if ((tinfo = taosMemoryMalloc(sizeof(*tinfo))) == NULL) {
18,375,596✔
455
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
456
  }
457
  tinfo->fid = fid;
18,376,119✔
458
  tinfo->fset = NULL;
18,375,869✔
459

460
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
18,376,081✔
461

462
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
36,750,280✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
464
  }
465
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
18,375,270✔
466

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

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

478
  STFileSet  *fset = NULL;
3,057,246✔
479
  SRBTreeIter iter;
3,056,977✔
480

481
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
3,057,470✔
482

483
  // scan time-series data
484
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
3,057,060✔
485
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
67,669,462✔
486
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
64,655,313✔
487

488
    // scan time-series data
489
    STsdbRowKey from = {
64,655,211✔
490
        .key.ts = INT64_MIN,
491
        .key.numOfPKs = 0,
492
        .version = INT64_MIN,
493
    };
494
    for (;;) {
105,534,306✔
495
      int64_t     minKey, maxKey;
170,186,314✔
496
      STbDataIter tbDataIter = {0};
170,039,795✔
497
      TSDBROW    *row;
498
      int32_t     fid;
499

500
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
170,050,199✔
501
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
170,175,769✔
502
        break;
64,611,618✔
503
      }
504

505
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
105,564,151✔
506
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
105,554,894✔
507

508
      SFileSetCommitInfo *info;
105,542,300✔
509
      SFileSetCommitInfo  tinfo = {
105,543,003✔
510
           .fid = fid,
511
      };
512
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
105,542,082✔
513
      if (info == NULL) {
105,539,841✔
514
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
18,288,617✔
515
      }
516

517
      from.key.ts = maxKey + 1;
105,536,897✔
518
    }
519
  }
520

521
  (void)taosThreadMutexLock(&tsdb->mutex);
3,057,795✔
522

523
  // scan tomb data
524
  if (tsdb->imem->nDel > 0) {
3,057,795✔
525
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
2,208,867✔
526
      if (tsdbTFileSetIsEmpty(fset)) {
1,135,852✔
UNCOV
527
        continue;
×
528
      }
529

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

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

541
      int64_t minKey, maxKey;
130,889✔
542
      bool    hasDataToCommit = false;
130,889✔
543
      tsdbFidKeyRange(fset->fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
130,889✔
544
      iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
130,889✔
545
      for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
240,902✔
546
        STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
196,454✔
547
        for (SDelData *pDelData = pTbData->pHead; pDelData; pDelData = pDelData->pNext) {
262,166✔
548
          if (pDelData->sKey > maxKey || pDelData->eKey < minKey) {
152,153✔
549
            continue;
65,712✔
550
          } else {
551
            hasDataToCommit = true;
86,441✔
552
            if ((code = tsdbCommitInfoAdd(tsdb, fset->fid))) {
86,441✔
553
              (void)taosThreadMutexUnlock(&tsdb->mutex);
×
554
              TSDB_CHECK_CODE(code, lino, _exit);
×
555
            }
556
            break;
86,441✔
557
          }
558
        }
559

560
        if (hasDataToCommit) {
196,454✔
561
          break;
86,441✔
562
        }
563
      }
564
    }
565
  }
566

567
  // begin tasks on file set
568
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
21,434,611✔
569
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
18,376,816✔
570
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
18,376,816✔
571
    if (fset) {
18,376,816✔
572
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
2,223,438✔
573
      if (code) {
2,223,438✔
574
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
575
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
576
      }
577
    }
578
  }
579

580
  (void)taosThreadMutexUnlock(&tsdb->mutex);
3,057,795✔
581

582
_exit:
3,057,658✔
583
  if (code) {
3,057,795✔
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;
3,057,795✔
588
}
589

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

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

606
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
3,056,946✔
607

608
_exit:
3,057,795✔
609
  if (code) {
3,057,795✔
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__);
3,057,795✔
613
  }
614
  return code;
3,057,795✔
615
}
616

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

621
  if (eno == 0) {
3,056,910✔
622
    TAOS_CHECK_GOTO(tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT), &lino, _exit);
3,056,910✔
623
  } else {
UNCOV
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);
3,057,795✔
629
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
3,057,795✔
630
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
3,057,795✔
631
  TARRAY2_DESTROY(committer->fopArray, NULL);
3,057,795✔
632
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
3,057,795✔
633

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

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

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

656
  int32_t code = 0;
5,423,891✔
657
  int32_t lino = 0;
5,423,891✔
658

659
  SMemTable *imem = tsdb->imem;
5,423,891✔
660
  int64_t    nRow = imem->nRow;
5,423,891✔
661
  int64_t    nDel = imem->nDel;
5,423,891✔
662

663
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
5,423,891✔
664
    (void)taosThreadMutexLock(&tsdb->mutex);
2,366,096✔
665
    tsdb->imem = NULL;
2,366,096✔
666
    (void)taosThreadMutexUnlock(&tsdb->mutex);
2,366,096✔
667
    tsdbUnrefMemTable(imem, NULL, true);
2,366,096✔
668
  } else {
669
    SCommitter2 committer = {0};
3,057,795✔
670

671
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
3,057,795✔
672

673
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
21,416,204✔
674
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
18,373,686✔
675
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
18,367,645✔
676
    }
677

678
    TAOS_CHECK_GOTO(tsdbCloseCommitter(&committer, code), &lino, _exit);
3,057,180✔
679
  }
680

681
_exit:
5,423,891✔
682
  if (code) {
5,423,891✔
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);
5,423,891✔
686
  }
687
  return code;
5,423,891✔
688
}
689

690
int32_t tsdbCommitCommit(STsdb *tsdb) {
5,423,891✔
691
  int32_t code = 0;
5,423,891✔
692
  int32_t lino = 0;
5,423,891✔
693

694
  if (tsdb->imem) {
5,423,891✔
695
    SMemTable *pMemTable = tsdb->imem;
3,057,795✔
696

697
    (void)taosThreadMutexLock(&tsdb->mutex);
3,057,795✔
698

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

705
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
21,434,611✔
706
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
18,376,816✔
707
      if (info->fset) {
18,376,816✔
708
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
2,223,438✔
709
      }
710
    }
711

712
    (void)taosThreadMutexUnlock(&tsdb->mutex);
3,057,795✔
713

714
    tsdbCommitInfoDestroy(tsdb);
3,057,795✔
715
    tsdbUnrefMemTable(pMemTable, NULL, true);
3,057,795✔
716
  }
717

718
_exit:
2,366,096✔
719
  if (code) {
5,423,414✔
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__);
5,423,414✔
723
  }
724
  return code;
5,423,891✔
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