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

taosdata / TDengine / #3655

14 Mar 2025 08:10AM UTC coverage: 59.532% (+22.6%) from 36.951%
#3655

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

139898 of 302527 branches covered (46.24%)

Branch coverage included in aggregate %.

71 of 99 new or added lines in 12 files covered. (71.72%)

2746 existing lines in 57 files now uncovered.

220499 of 302857 relevant lines covered (72.81%)

5509252.7 hits per line

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

73.42
/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) {
452,953✔
64
  int32_t code = 0;
452,953✔
65
  int32_t lino = 0;
452,953✔
66

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

81
  if (committer->sttTrigger == 1) {
452,953✔
82
    config.toSttOnly = false;
4,700✔
83

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

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

96
_exit:
452,970✔
97
  if (code) {
452,970!
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;
452,973✔
102
}
103

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

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

114
  committer->ctx->hasTSData = false;
452,955✔
115

116
  committer->ctx->tbid->suid = 0;
452,955✔
117
  committer->ctx->tbid->uid = 0;
452,955✔
118
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
92,575,910✔
119
    if (row->uid != committer->ctx->tbid->uid) {
91,865,036✔
120
      committer->ctx->tbid->suid = row->suid;
17,218,621✔
121
      committer->ctx->tbid->uid = row->uid;
17,218,621✔
122

123
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
17,218,621✔
124
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
13,537,255!
125
        continue;
13,568,579✔
126
      }
127
    }
128

129
    int64_t ts = TSDBROW_TS(&row->row);
78,330,874✔
130
    if (ts > committer->ctx->maxKey) {
78,330,874✔
131
      TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
3,580,083!
132
      continue;
3,580,015✔
133
    }
134

135
    committer->ctx->hasTSData = true;
74,750,791✔
136
    numOfRow++;
74,750,791✔
137

138
    TAOS_CHECK_GOTO(tsdbFSetWriteRow(committer->writer, row), &lino, _exit);
74,750,791!
139
    TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->dataIterMerger), &lino, _exit);
75,873,879!
140
  }
141

142
_exit:
452,973✔
143
  if (code) {
452,973!
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,
452,973✔
148
              numOfRow);
149
  }
150
  return code;
452,965✔
151
}
152

153
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
452,962✔
154
  int32_t   code = 0;
452,962✔
155
  int32_t   lino = 0;
452,962✔
156
  int64_t   numRecord = 0;
452,962✔
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) {
452,962✔
161
    committer->ctx->tbid->suid = 0;
333,993✔
162
    committer->ctx->tbid->uid = 0;
333,993✔
163
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
340,190✔
164
      if (record->uid != committer->ctx->tbid->uid) {
6,197✔
165
        committer->ctx->tbid->suid = record->suid;
5,258✔
166
        committer->ctx->tbid->uid = record->uid;
5,258✔
167

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

174
      if (record->ekey < committer->ctx->minKey) {
6,193✔
175
        // do nothing
176
      } else if (record->skey > committer->ctx->maxKey) {
6,171✔
177
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
178
      } else {
179
        record->skey = TMAX(record->skey, committer->ctx->minKey);
6,147✔
180
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
6,147✔
181

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

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

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

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

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

210
  if (committer->ctx->info->fset == NULL                        //
452,954✔
211
      || committer->sttTrigger > 1                              //
5,081✔
212
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
338✔
213
  ) {
214
    return 0;
452,924✔
215
  }
216

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

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

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

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

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

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

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

252
static void tsdbCommitCloseIter(SCommitter2 *committer) {
452,982✔
253
  tsdbIterMergerClose(&committer->tombIterMerger);
452,982✔
254
  tsdbIterMergerClose(&committer->dataIterMerger);
453,000✔
255
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
905,994!
256
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
906,034✔
257
  return;
453,002✔
258
}
259

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

264
  STsdbIter      *iter;
265
  STsdbIterConfig config = {0};
452,932✔
266

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

276
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
452,932!
277
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
905,928!
278

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

283
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
452,964!
284
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
906,011!
285

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

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

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

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

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

305
  // open merger
306
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
453,006!
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
452,986!
308

309
_exit:
452,992✔
310
  if (code) {
452,992!
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;
452,988✔
316
}
317

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

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

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

330
  committer->ctx->tbid->suid = 0;
452,979✔
331
  committer->ctx->tbid->uid = 0;
452,979✔
332

333
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
452,979!
334
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
452,965!
335
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
452,975!
336

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

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

352
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
452,958!
353
  tsdbCommitCloseIter(committer);
452,983✔
354
  tsdbCommitCloseReader(committer);
453,001✔
355

356
_exit:
452,972✔
357
  if (code) {
452,972!
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);
452,972✔
362
  }
363
  return code;
452,975✔
364
}
365

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

370
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
452,974!
371
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
452,973!
372
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
452,963!
373
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
452,960!
374

375
_exit:
452,952✔
376
  if (code) {
452,952!
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);
452,952✔
381
  }
382
  return code;
452,953✔
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,985,539✔
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,194,526✔
403
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
8,194,526✔
404
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
8,194,526✔
405
}
406

407
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
7,822✔
408
  if (pTsdb->commitInfo) {
7,822!
409
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
460,832✔
410
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
453,010✔
411
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
453,010✔
412
      tsdbTFileSetClear(&info->fset);
453,010✔
413
      taosMemoryFree(info);
453,010!
414
    }
415

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

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

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

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

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

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

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

452
  SFileSetCommitInfo *tinfo;
453

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

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

462
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
905,819!
463
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
464
  }
465
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
452,889✔
466

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

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

478
  STFileSet  *fset = NULL;
7,822✔
479
  SRBTreeIter iter;
480

481
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
7,822!
482

483
  // scan time-series data
484
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
7,822✔
485
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
81,009✔
486
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
73,187✔
487

488
    // scan time-series data
489
    STsdbRowKey from = {
73,187✔
490
        .key.ts = INT64_MIN,
491
        .key.numOfPKs = 0,
492
        .version = INT64_MIN,
493
    };
494
    for (;;) {
6,712,893✔
495
      int64_t     minKey, maxKey;
496
      STbDataIter tbDataIter = {0};
6,786,080✔
497
      TSDBROW    *row;
498
      int32_t     fid;
499

500
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
6,786,080✔
501
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
6,786,389✔
502
        break;
71,690✔
503
      }
504

505
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
6,714,699!
506
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
6,719,917✔
507

508
      SFileSetCommitInfo *info;
509
      SFileSetCommitInfo  tinfo = {
6,720,009✔
510
           .fid = fid,
511
      };
512
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
6,720,009✔
513
      if (info == NULL) {
6,712,468✔
514
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
452,105!
515
      }
516

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

521
  (void)taosThreadMutexLock(&tsdb->mutex);
7,821✔
522

523
  // scan tomb data
524
  if (tsdb->imem->nDel > 0) {
7,822✔
525
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
6,636✔
526
      if (tsdbTFileSetIsEmpty(fset)) {
3,387!
527
        continue;
2,987✔
528
      }
529

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

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

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

567
  // begin tasks on file set
568
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
460,830✔
569
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
453,009✔
570
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
453,009✔
571
    if (fset) {
453,008✔
572
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
5,081✔
573
      if (code) {
5,081!
574
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
575
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
576
      }
577
    }
578
  }
579

580
  (void)taosThreadMutexUnlock(&tsdb->mutex);
7,821✔
581

582
_exit:
7,822✔
583
  if (code) {
7,822!
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;
7,821✔
588
}
589

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

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

606
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
7,822!
607

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

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

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

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

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

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

656
  int32_t code = 0;
22,081✔
657
  int32_t lino = 0;
22,081✔
658

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

663
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
22,081!
664
    (void)taosThreadMutexLock(&tsdb->mutex);
14,259✔
665
    tsdb->imem = NULL;
14,259✔
666
    (void)taosThreadMutexUnlock(&tsdb->mutex);
14,259✔
667
    tsdbUnrefMemTable(imem, NULL, true);
14,259✔
668
  } else {
669
    SCommitter2 committer = {0};
7,822✔
670

671
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
7,822!
672

673
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
460,774✔
674
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
452,979✔
675
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
452,964!
676
    }
677

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

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

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

694
  if (tsdb->imem) {
22,081✔
695
    SMemTable *pMemTable = tsdb->imem;
7,822✔
696

697
    (void)taosThreadMutexLock(&tsdb->mutex);
7,822✔
698

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

705
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
460,832✔
706
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
453,010✔
707
      if (info->fset) {
453,010✔
708
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
5,081✔
709
      }
710
    }
711

712
    (void)taosThreadMutexUnlock(&tsdb->mutex);
7,822✔
713

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

718
_exit:
14,259✔
719
  if (code) {
22,081!
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__);
22,081✔
723
  }
724
  return code;
22,081✔
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