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

taosdata / TDengine / #5065

17 May 2026 01:15AM UTC coverage: 73.407% (+0.04%) from 73.37%
#5065

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281734 of 383795 relevant lines covered (73.41%)

138006873.5 hits per line

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

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

67
  SFSetWriterConfig config = {
19,963,306✔
68
      .tsdb = committer->tsdb,
19,974,661✔
69
      .toSttOnly = true,
70
      .compactVersion = committer->compactVersion,
19,963,812✔
71
      .minRow = committer->minRow,
19,965,655✔
72
      .maxRow = committer->maxRow,
19,980,787✔
73
      .szPage = committer->szPage,
19,953,989✔
74
      .cmprAlg = committer->cmprAlg,
19,959,176✔
75
      .fid = committer->ctx->info->fid,
19,940,222✔
76
      .cid = committer->cid,
19,958,164✔
77
      .expLevel = committer->ctx->expLevel,
19,967,012✔
78
      .level = 0,
79
  };
80

81
  if (committer->sttTrigger == 1) {
19,909,242✔
82
    config.toSttOnly = false;
4,125,212✔
83

84
    if (committer->ctx->info->fset) {
4,125,212✔
85
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
1,024,850✔
86
        if (committer->ctx->info->fset->farr[ftype] != NULL) {
821,176✔
87
          config.files[ftype].exist = true;
606,908✔
88
          config.files[ftype].file = committer->ctx->info->fset->farr[ftype]->f[0];
606,908✔
89
        }
90
      }
91
    }
92
  }
93

94
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
19,944,495✔
95

96
_exit:
19,927,423✔
97
  if (code) {
19,930,771✔
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;
19,930,771✔
102
}
103

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

108
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
19,934,802✔
109
  int32_t   code = 0;
19,934,802✔
110
  int32_t   lino = 0;
19,934,802✔
111
  int64_t   numOfRow = 0;
19,942,398✔
112
  SMetaInfo info;
19,942,030✔
113

114
  committer->ctx->hasTSData = false;
19,947,997✔
115

116
  committer->ctx->tbid->suid = 0;
19,970,171✔
117
  committer->ctx->tbid->uid = 0;
19,944,200✔
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;
117,889,256✔
121
      committer->ctx->tbid->uid = row->uid;
117,824,259✔
122

123
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
117,890,083✔
124
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
1,798,686✔
125
        continue;
1,792,116✔
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);
52,112,552✔
132
      continue;
52,095,886✔
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:
20,033,767✔
143
  if (code) {
20,028,629✔
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,
20,028,629✔
148
              numOfRow);
149
  }
150
  return code;
20,028,629✔
151
}
152

153
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
20,026,968✔
154
  int32_t   code = 0;
20,026,968✔
155
  int32_t   lino = 0;
20,026,968✔
156
  int64_t   numRecord = 0;
20,028,371✔
157
  SMetaInfo info;
20,028,003✔
158

159
  // if no history data and no new timestamp data, skip tomb data
160
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
20,026,613✔
161
    committer->ctx->tbid->suid = 0;
20,014,595✔
162
    committer->ctx->tbid->uid = 0;
20,014,696✔
163
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
22,136,468✔
164
      if (record->uid != committer->ctx->tbid->uid) {
2,121,048✔
165
        committer->ctx->tbid->suid = record->suid;
2,079,836✔
166
        committer->ctx->tbid->uid = record->uid;
2,079,836✔
167

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

174
      if (record->ekey < committer->ctx->minKey) {
2,101,251✔
175
        // do nothing
176
      } else if (record->skey > committer->ctx->maxKey) {
2,088,649✔
177
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
178
      } else {
179
        record->skey = TMAX(record->skey, committer->ctx->minKey);
2,079,261✔
180
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
2,079,261✔
181

182
        numRecord++;
2,079,261✔
183
        TAOS_CHECK_GOTO(tsdbFSetWriteTombRecord(committer->writer, record), &lino, _exit);
2,079,261✔
184
      }
185

186
      TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->tombIterMerger), &lino, _exit);
2,101,251✔
187
    }
188
  }
189

190
_exit:
20,033,578✔
191
  if (code) {
20,032,488✔
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),
20,032,488✔
196
              committer->ctx->info->fid, numRecord);
197
  }
198
  return code;
20,032,488✔
199
}
200

201
static void tsdbCommitCloseReader(SCommitter2 *committer) {
19,949,071✔
202
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
19,955,014✔
203
  return;
19,922,543✔
204
}
205

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

210
  if (committer->ctx->info->fset == NULL                        //
19,924,156✔
211
      || committer->sttTrigger > 1                              //
2,402,484✔
212
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
205,294✔
213
  ) {
214
    return 0;
19,886,155✔
215
  }
216

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

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

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

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

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

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

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

252
static void tsdbCommitCloseIter(SCommitter2 *committer) {
19,970,256✔
253
  tsdbIterMergerClose(&committer->tombIterMerger);
19,970,256✔
254
  tsdbIterMergerClose(&committer->dataIterMerger);
19,921,082✔
255
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
39,894,834✔
256
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
39,894,362✔
257
  return;
19,968,858✔
258
}
259

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

264
  STsdbIter      *iter;
19,930,221✔
265
  STsdbIterConfig config = {0};
19,938,081✔
266

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

276
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
19,936,868✔
277
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
39,966,161✔
278

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

283
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
19,994,479✔
284
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
39,924,063✔
285

286
  // STT
287
  SSttFileReader *sttReader;
288
  TARRAY2_FOREACH(committer->sttReaderArray, sttReader) {
19,965,097✔
289
    // data iter
290
    config.type = TSDB_ITER_TYPE_STT;
5,943✔
291
    config.sttReader = sttReader;
5,943✔
292

293
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
5,943✔
294
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
11,886✔
295

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

300
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
5,943✔
301

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

305
  // open merger
306
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
19,936,412✔
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
19,941,417✔
308

309
_exit:
20,003,469✔
310
  if (code) {
19,988,365✔
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;
19,988,365✔
316
}
317

318
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
19,981,673✔
319
  int32_t code = 0;
19,981,673✔
320
  int32_t lino = 0;
19,981,673✔
321
  STsdb  *tsdb = committer->tsdb;
19,985,801✔
322

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

326
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->info->fid, &tsdb->keepCfg, committer->now);
19,948,311✔
327
  tsdbFidKeyRange(committer->ctx->info->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
19,895,383✔
328
                  &committer->ctx->maxKey);
329

330
  committer->ctx->tbid->suid = 0;
19,903,379✔
331
  committer->ctx->tbid->uid = 0;
19,907,810✔
332

333
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
19,904,041✔
334
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
19,866,340✔
335
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
19,970,692✔
336

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

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

352
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
20,027,902✔
353
  tsdbCommitCloseIter(committer);
19,976,895✔
354
  tsdbCommitCloseReader(committer);
19,963,667✔
355

356
_exit:
19,906,751✔
357
  if (code) {
19,913,260✔
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);
19,913,260✔
362
  }
363
  return code;
19,913,529✔
364
}
365

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

370
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
19,978,029✔
371
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
19,921,548✔
372
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
20,028,916✔
373
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
20,025,714✔
374

375
_exit:
19,946,724✔
376
  if (code) {
19,954,116✔
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);
19,954,116✔
381
  }
382
  return code;
19,954,116✔
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) {
122,634,422✔
392
    return 1;
11,047,776✔
393
  } else {
394
    return 0;
111,586,758✔
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) {
165,143,071✔
403
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
165,143,071✔
404
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
165,143,071✔
405
}
406

407
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
3,342,449✔
408
  if (pTsdb->commitInfo) {
3,342,449✔
409
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
23,380,946✔
410
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
20,038,497✔
411
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
20,038,497✔
412
      tsdbTFileSetClear(&info->fset);
20,038,465✔
413
      taosMemoryFree(info);
20,037,928✔
414
    }
415

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

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

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

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

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

440
_exit:
3,341,527✔
441
  if (code) {
3,341,491✔
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,341,491✔
446
}
447

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

452
  SFileSetCommitInfo *tinfo;
20,028,592✔
453

454
  if ((tinfo = taosMemoryMalloc(sizeof(*tinfo))) == NULL) {
20,032,664✔
455
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
456
  }
457
  tinfo->fid = fid;
20,035,603✔
458
  tinfo->fset = NULL;
20,032,325✔
459

460
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
20,029,548✔
461

462
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
40,059,520✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
464
  }
465
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
20,028,397✔
466

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

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

478
  STFileSet  *fset = NULL;
3,339,104✔
479
  SRBTreeIter iter;
3,337,842✔
480

481
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
3,338,659✔
482

483
  // scan time-series data
484
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
3,340,215✔
485
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
68,606,416✔
486
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
65,336,188✔
487

488
    // scan time-series data
489
    STsdbRowKey from = {
65,329,934✔
490
        .key.ts = INT64_MIN,
491
        .key.numOfPKs = 0,
492
        .version = INT64_MIN,
493
    };
494
    for (;;) {
110,317,465✔
495
      int64_t     minKey, maxKey;
175,645,753✔
496
      STbDataIter tbDataIter = {0};
175,455,836✔
497
      TSDBROW    *row;
498
      int32_t     fid;
499

500
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
175,496,210✔
501
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
175,696,892✔
502
        break;
65,250,456✔
503
      }
504

505
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
110,446,436✔
506
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
110,456,324✔
507

508
      SFileSetCommitInfo *info;
110,343,950✔
509
      SFileSetCommitInfo  tinfo = {
110,357,685✔
510
           .fid = fid,
511
      };
512
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
110,357,491✔
513
      if (info == NULL) {
110,301,301✔
514
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
19,931,606✔
515
      }
516

517
      from.key.ts = maxKey + 1;
110,304,303✔
518
    }
519
  }
520

521
  (void)taosThreadMutexLock(&tsdb->mutex);
3,342,449✔
522

523
  // scan tomb data
524
  if (tsdb->imem->nDel > 0) {
3,342,449✔
525
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
2,427,239✔
526
      if (tsdbTFileSetIsEmpty(fset)) {
1,247,878✔
527
        continue;
×
528
      }
529

530
      SFileSetCommitInfo *info;
1,247,878✔
531
      SFileSetCommitInfo  tinfo = {
1,247,878✔
532
           .fid = fset->fid,
1,247,878✔
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,247,878✔
537
      if (info != NULL) {
1,247,878✔
538
        continue;
1,104,360✔
539
      }
540

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

560
        if (hasDataToCommit) {
214,281✔
561
          break;
95,557✔
562
        }
563
      }
564
    }
565
  }
566

567
  // begin tasks on file set
568
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
23,372,518✔
569
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
20,034,045✔
570
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
20,034,728✔
571
    if (fset) {
20,032,998✔
572
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
2,405,262✔
573
      if (code) {
2,403,950✔
574
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
575
        TAOS_CHECK_GOTO(code, &lino, _exit);
45✔
576
      }
577
    }
578
  }
579

580
  (void)taosThreadMutexUnlock(&tsdb->mutex);
3,339,911✔
581

582
_exit:
3,342,205✔
583
  if (code) {
3,340,977✔
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,340,977✔
588
}
589

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

594
  committer->tsdb = tsdb;
3,341,949✔
595
  committer->minutes = tsdb->keepCfg.days;
3,341,949✔
596
  committer->precision = tsdb->keepCfg.precision;
3,342,194✔
597
  committer->minRow = info->info.config.tsdbCfg.minRows;
3,340,080✔
598
  committer->maxRow = info->info.config.tsdbCfg.maxRows;
3,340,954✔
599
  committer->cmprAlg = info->info.config.tsdbCfg.compression;
3,342,071✔
600
  committer->sttTrigger = info->info.config.sttTrigger;
3,340,962✔
601
  committer->szPage = info->info.config.tsdbPageSize;
3,341,018✔
602
  committer->compactVersion = INT64_MAX;
3,341,151✔
603
  committer->cid = tsdbFSAllocEid(tsdb->pFS);
3,341,132✔
604
  committer->now = taosGetTimestampSec();
3,340,596✔
605

606
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
3,340,153✔
607

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

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

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

628
  TARRAY2_DESTROY(committer->dataIterArray, NULL);
3,343,284✔
629
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
3,341,614✔
630
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
3,341,614✔
631
  TARRAY2_DESTROY(committer->fopArray, NULL);
3,342,449✔
632
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
3,341,614✔
633

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

644
int32_t tsdbPreCommit(STsdb *tsdb) {
5,966,802✔
645
  (void)taosThreadMutexLock(&tsdb->mutex);
5,966,802✔
646
  ASSERT_CORE(tsdb->imem == NULL, "imem should be null to commit mem");
5,997,176✔
647
  tsdb->imem = tsdb->mem;
5,997,343✔
648
  tsdb->mem = NULL;
5,995,901✔
649
  (void)taosThreadMutexUnlock(&tsdb->mutex);
5,997,082✔
650
  return 0;
5,998,397✔
651
}
652

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

656
  int32_t code = 0;
5,987,174✔
657
  int32_t lino = 0;
5,987,174✔
658

659
  SMemTable *imem = tsdb->imem;
5,998,055✔
660
  int64_t    nRow = imem->nRow;
5,998,055✔
661
  int64_t    nDel = imem->nDel;
5,998,641✔
662

663
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
5,998,641✔
664
    (void)taosThreadMutexLock(&tsdb->mutex);
2,656,192✔
665
    tsdb->imem = NULL;
2,656,192✔
666
    (void)taosThreadMutexUnlock(&tsdb->mutex);
2,656,192✔
667
    tsdbUnrefMemTable(imem, NULL, true);
2,656,109✔
668
  } else {
669
    SCommitter2 committer = {0};
3,342,449✔
670

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

673
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
23,281,048✔
674
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
19,968,340✔
675
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
19,977,290✔
676
    }
677

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

681
_exit:
5,997,518✔
682
  if (code) {
5,997,468✔
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,997,468✔
686
  }
687
  return code;
5,998,641✔
688
}
689

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

694
  if (tsdb->imem) {
5,998,641✔
695
    SMemTable *pMemTable = tsdb->imem;
3,342,449✔
696

697
    (void)taosThreadMutexLock(&tsdb->mutex);
3,342,449✔
698

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

705
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
23,380,946✔
706
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
20,038,497✔
707
      if (info->fset) {
20,038,497✔
708
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
2,405,262✔
709
      }
710
    }
711

712
    (void)taosThreadMutexUnlock(&tsdb->mutex);
3,342,449✔
713

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

718
_exit:
2,656,192✔
719
  if (code) {
5,998,641✔
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,998,641✔
723
  }
724
  return code;
5,998,641✔
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