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

taosdata / TDengine / #4893

20 Dec 2025 01:15PM UTC coverage: 65.57% (-0.001%) from 65.571%
#4893

push

travis-ci

web-flow
feat: support taos_connect_with func (#33952)

* feat: support taos_connect_with

* refactor: enhance connection options and add tests for taos_set_option and taos_connect_with

* fix: handle NULL keys and values in taos_connect_with options

* fix: revert TAOSWS_GIT_TAG to default value "main"

* docs: add TLS configuration options for WebSocket connections in documentation

* docs: modify zh docs and add en docs

* chore: update taos.cfg

* docs: add examples

* docs: add error handling for connection failure in example code

2 of 82 new or added lines in 3 files covered. (2.44%)

1158 existing lines in 109 files now uncovered.

182854 of 278870 relevant lines covered (65.57%)

105213271.06 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) {
15,485,259✔
64
  int32_t code = 0;
15,485,259✔
65
  int32_t lino = 0;
15,485,259✔
66

67
  SFSetWriterConfig config = {
15,493,303✔
68
      .tsdb = committer->tsdb,
15,496,911✔
69
      .toSttOnly = true,
70
      .compactVersion = committer->compactVersion,
15,498,904✔
71
      .minRow = committer->minRow,
15,501,972✔
72
      .maxRow = committer->maxRow,
15,497,982✔
73
      .szPage = committer->szPage,
15,494,469✔
74
      .cmprAlg = committer->cmprAlg,
15,490,605✔
75
      .fid = committer->ctx->info->fid,
15,490,988✔
76
      .cid = committer->cid,
15,479,155✔
77
      .expLevel = committer->ctx->expLevel,
15,497,023✔
78
      .level = 0,
79
  };
80

81
  if (committer->sttTrigger == 1) {
15,497,918✔
82
    config.toSttOnly = false;
393,367✔
83

84
    if (committer->ctx->info->fset) {
393,367✔
85
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
879,410✔
86
        if (committer->ctx->info->fset->farr[ftype] != NULL) {
703,528✔
87
          config.files[ftype].exist = true;
519,452✔
88
          config.files[ftype].file = committer->ctx->info->fset->farr[ftype]->f[0];
519,452✔
89
        }
90
      }
91
    }
92
  }
93

94
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
15,496,016✔
95

96
_exit:
15,499,280✔
97
  if (code) {
15,500,746✔
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;
15,500,746✔
102
}
103

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

108
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
15,485,184✔
109
  int32_t   code = 0;
15,485,184✔
110
  int32_t   lino = 0;
15,485,184✔
111
  int64_t   numOfRow = 0;
15,494,200✔
112
  SMetaInfo info;
15,493,703✔
113

114
  committer->ctx->hasTSData = false;
15,496,364✔
115

116
  committer->ctx->tbid->suid = 0;
15,494,765✔
117
  committer->ctx->tbid->uid = 0;
15,502,070✔
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;
83,013,200✔
121
      committer->ctx->tbid->uid = row->uid;
83,017,926✔
122

123
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
82,998,703✔
124
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
997,848✔
125
        continue;
997,457✔
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);
38,506,452✔
132
      continue;
38,501,564✔
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:
15,507,650✔
143
  if (code) {
15,499,343✔
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,
15,499,343✔
148
              numOfRow);
149
  }
150
  return code;
15,499,343✔
151
}
152

153
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
15,499,873✔
154
  int32_t   code = 0;
15,499,873✔
155
  int32_t   lino = 0;
15,499,873✔
156
  int64_t   numRecord = 0;
15,501,346✔
157
  SMetaInfo info;
15,500,849✔
158

159
  // if no history data and no new timestamp data, skip tomb data
160
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
15,501,722✔
161
    committer->ctx->tbid->suid = 0;
15,490,231✔
162
    committer->ctx->tbid->uid = 0;
15,493,487✔
163
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
17,300,215✔
164
      if (record->uid != committer->ctx->tbid->uid) {
1,806,728✔
165
        committer->ctx->tbid->suid = record->suid;
1,773,129✔
166
        committer->ctx->tbid->uid = record->uid;
1,773,129✔
167

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

174
      if (record->ekey < committer->ctx->minKey) {
1,789,166✔
175
        // do nothing
176
      } else if (record->skey > committer->ctx->maxKey) {
1,776,246✔
177
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
178
      } else {
179
        record->skey = TMAX(record->skey, committer->ctx->minKey);
1,768,734✔
180
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
1,768,734✔
181

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

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

190
_exit:
15,509,802✔
191
  if (code) {
15,506,653✔
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),
15,506,653✔
196
              committer->ctx->info->fid, numRecord);
197
  }
198
  return code;
15,506,653✔
199
}
200

201
static void tsdbCommitCloseReader(SCommitter2 *committer) {
15,489,578✔
202
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
15,494,829✔
203
  return;
15,496,550✔
204
}
205

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

210
  if (committer->ctx->info->fset == NULL                        //
15,491,562✔
211
      || committer->sttTrigger > 1                              //
2,095,744✔
212
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
175,882✔
213
  ) {
214
    return 0;
15,476,018✔
215
  }
216

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

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

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

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

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

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

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

252
static void tsdbCommitCloseIter(SCommitter2 *committer) {
15,491,424✔
253
  tsdbIterMergerClose(&committer->tombIterMerger);
15,491,424✔
254
  tsdbIterMergerClose(&committer->dataIterMerger);
15,499,899✔
255
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
30,993,443✔
256
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
30,995,362✔
257
  return;
15,505,284✔
258
}
259

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

264
  STsdbIter      *iter;
15,493,009✔
265
  STsdbIterConfig config = {0};
15,498,149✔
266

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

276
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
15,502,021✔
277
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
31,013,170✔
278

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

283
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
15,501,824✔
284
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
31,004,557✔
285

286
  // STT
287
  SSttFileReader *sttReader;
288
  TARRAY2_FOREACH(committer->sttReaderArray, sttReader) {
15,509,868✔
289
    // data iter
290
    config.type = TSDB_ITER_TYPE_STT;
5,251✔
291
    config.sttReader = sttReader;
5,251✔
292

293
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
5,251✔
294
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
10,502✔
295

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

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

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

305
  // open merger
306
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
15,498,328✔
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
15,493,964✔
308

309
_exit:
15,505,355✔
310
  if (code) {
15,507,919✔
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;
15,507,919✔
316
}
317

318
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
15,497,380✔
319
  int32_t code = 0;
15,497,380✔
320
  int32_t lino = 0;
15,497,380✔
321
  STsdb  *tsdb = committer->tsdb;
15,501,728✔
322

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

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

330
  committer->ctx->tbid->suid = 0;
15,486,850✔
331
  committer->ctx->tbid->uid = 0;
15,495,467✔
332

333
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
15,492,691✔
334
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
15,490,562✔
335
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
15,495,566✔
336

337
_exit:
15,490,621✔
338
  if (code) {
15,497,241✔
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),
15,497,241✔
342
              __func__, committer->ctx->info->fid, committer->ctx->minKey, committer->ctx->maxKey,
343
              committer->ctx->expLevel);
344
  }
345
  return code;
15,497,818✔
346
}
347

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

352
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
15,501,496✔
353
  tsdbCommitCloseIter(committer);
15,492,143✔
354
  tsdbCommitCloseReader(committer);
15,492,027✔
355

356
_exit:
15,495,263✔
357
  if (code) {
15,497,682✔
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);
15,497,682✔
362
  }
363
  return code;
15,497,682✔
364
}
365

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

370
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
15,502,600✔
371
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
15,490,055✔
372
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
15,499,189✔
373
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
15,505,879✔
374

375
_exit:
15,487,778✔
376
  if (code) {
15,490,706✔
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);
15,490,706✔
381
  }
382
  return code;
15,490,706✔
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) {
89,526,923✔
392
    return 1;
8,531,148✔
393
  } else {
394
    return 0;
80,994,633✔
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) {
122,209,606✔
403
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
122,209,606✔
404
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
122,209,606✔
405
}
406

407
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
2,714,063✔
408
  if (pTsdb->commitInfo) {
2,714,063✔
409
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
18,224,311✔
410
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
15,511,028✔
411
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
15,511,028✔
412
      tsdbTFileSetClear(&info->fset);
15,511,028✔
413
      taosMemoryFree(info);
15,511,028✔
414
    }
415

416
    vHashDestroy(&pTsdb->commitInfo->ht);
2,713,283✔
417
    taosArrayDestroy(pTsdb->commitInfo->arr);
2,713,283✔
418
    pTsdb->commitInfo->arr = NULL;
2,714,063✔
419
    taosMemoryFreeClear(pTsdb->commitInfo);
2,714,063✔
420
  }
421
  return;
2,714,063✔
422
}
423

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

428
  pTsdb->commitInfo = taosMemoryCalloc(1, sizeof(*pTsdb->commitInfo));
2,713,650✔
429
  if (pTsdb->commitInfo == NULL) {
2,714,063✔
430
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
431
  }
432

433
  TAOS_CHECK_GOTO(vHashInit(&pTsdb->commitInfo->ht, tFileSetCommitInfoHash, tFileSetCommitInfoCompare), &lino, _exit);
2,713,457✔
434

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

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

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

452
  SFileSetCommitInfo *tinfo;
15,507,314✔
453

454
  if ((tinfo = taosMemoryMalloc(sizeof(*tinfo))) == NULL) {
15,506,759✔
455
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
456
  }
457
  tinfo->fid = fid;
15,507,362✔
458
  tinfo->fset = NULL;
15,505,127✔
459

460
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
15,507,796✔
461

462
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
31,013,618✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
464
  }
465
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
15,505,042✔
466

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

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

478
  STFileSet  *fset = NULL;
2,714,063✔
479
  SRBTreeIter iter;
2,712,657✔
480

481
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
2,712,313✔
482

483
  // scan time-series data
484
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
2,714,063✔
485
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
46,968,430✔
486
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
44,342,369✔
487

488
    // scan time-series data
489
    STsdbRowKey from = {
44,342,536✔
490
        .key.ts = INT64_MIN,
491
        .key.numOfPKs = 0,
492
        .version = INT64_MIN,
493
    };
494
    for (;;) {
79,891,488✔
495
      int64_t     minKey, maxKey;
124,229,724✔
496
      STbDataIter tbDataIter = {0};
123,984,238✔
497
      TSDBROW    *row;
498
      int32_t     fid;
499

500
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
124,016,510✔
501
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
124,201,780✔
502
        break;
44,252,313✔
503
      }
504

505
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
79,949,467✔
506
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
79,894,106✔
507

508
      SFileSetCommitInfo *info;
79,887,985✔
509
      SFileSetCommitInfo  tinfo = {
79,890,726✔
510
           .fid = fid,
511
      };
512
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
79,890,224✔
513
      if (info == NULL) {
79,890,292✔
514
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
15,440,143✔
515
      }
516

517
      from.key.ts = maxKey + 1;
79,888,335✔
518
    }
519
  }
520

521
  (void)taosThreadMutexLock(&tsdb->mutex);
2,714,063✔
522

523
  // scan tomb data
524
  if (tsdb->imem->nDel > 0) {
2,714,063✔
525
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
2,265,276✔
526
      if (tsdbTFileSetIsEmpty(fset)) {
1,155,873✔
527
        continue;
×
528
      }
529

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

541
      int64_t minKey, maxKey;
110,151✔
542
      bool    hasDataToCommit = false;
110,151✔
543
      tsdbFidKeyRange(fset->fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
110,151✔
544
      iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
110,151✔
545
      for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
212,713✔
546
        STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
169,387✔
547
        for (SDelData *pDelData = pTbData->pHead; pDelData; pDelData = pDelData->pNext) {
223,991✔
548
          if (pDelData->sKey > maxKey || pDelData->eKey < minKey) {
121,429✔
549
            continue;
54,604✔
550
          } else {
551
            hasDataToCommit = true;
66,825✔
552
            if ((code = tsdbCommitInfoAdd(tsdb, fset->fid))) {
66,825✔
553
              (void)taosThreadMutexUnlock(&tsdb->mutex);
×
554
              TSDB_CHECK_CODE(code, lino, _exit);
×
555
            }
556
            break;
66,825✔
557
          }
558
        }
559

560
        if (hasDataToCommit) {
169,387✔
561
          break;
66,825✔
562
        }
563
      }
564
    }
565
  }
566

567
  // begin tasks on file set
568
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
18,222,212✔
569
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
15,509,896✔
570
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
15,510,451✔
571
    if (fset) {
15,509,360✔
572
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
2,095,744✔
573
      if (code) {
2,095,744✔
574
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
UNCOV
575
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
576
      }
577
    }
578
  }
579

580
  (void)taosThreadMutexUnlock(&tsdb->mutex);
2,712,893✔
581

582
_exit:
2,713,238✔
583
  if (code) {
2,713,058✔
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;
2,713,058✔
588
}
589

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

594
  committer->tsdb = tsdb;
2,714,063✔
595
  committer->minutes = tsdb->keepCfg.days;
2,714,063✔
596
  committer->precision = tsdb->keepCfg.precision;
2,714,063✔
597
  committer->minRow = info->info.config.tsdbCfg.minRows;
2,713,491✔
598
  committer->maxRow = info->info.config.tsdbCfg.maxRows;
2,712,885✔
599
  committer->cmprAlg = info->info.config.tsdbCfg.compression;
2,713,078✔
600
  committer->sttTrigger = info->info.config.sttTrigger;
2,713,684✔
601
  committer->szPage = info->info.config.tsdbPageSize;
2,713,305✔
602
  committer->compactVersion = INT64_MAX;
2,714,063✔
603
  committer->cid = tsdbFSAllocEid(tsdb->pFS);
2,713,684✔
604
  committer->now = taosGetTimestampSec();
2,714,063✔
605

606
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
2,713,564✔
607

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

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

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

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

644
int32_t tsdbPreCommit(STsdb *tsdb) {
5,248,410✔
645
  (void)taosThreadMutexLock(&tsdb->mutex);
5,248,410✔
646
  ASSERT_CORE(tsdb->imem == NULL, "imem should be null to commit mem");
5,261,907✔
647
  tsdb->imem = tsdb->mem;
5,261,831✔
648
  tsdb->mem = NULL;
5,261,907✔
649
  (void)taosThreadMutexUnlock(&tsdb->mutex);
5,260,654✔
650
  return 0;
5,261,660✔
651
}
652

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

656
  int32_t code = 0;
5,255,800✔
657
  int32_t lino = 0;
5,255,800✔
658

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

663
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
5,261,528✔
664
    (void)taosThreadMutexLock(&tsdb->mutex);
2,547,465✔
665
    tsdb->imem = NULL;
2,547,844✔
666
    (void)taosThreadMutexUnlock(&tsdb->mutex);
2,547,844✔
667
    tsdbUnrefMemTable(imem, NULL, true);
2,547,844✔
668
  } else {
669
    SCommitter2 committer = {0};
2,714,063✔
670

671
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
2,714,063✔
672

673
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
18,203,034✔
674
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
15,507,359✔
675
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
15,502,625✔
676
    }
677

678
    TAOS_CHECK_GOTO(tsdbCloseCommitter(&committer, code), &lino, _exit);
2,714,063✔
679
  }
680

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

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

694
  if (tsdb->imem) {
5,261,907✔
695
    SMemTable *pMemTable = tsdb->imem;
2,714,063✔
696

697
    (void)taosThreadMutexLock(&tsdb->mutex);
2,714,063✔
698

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

705
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
18,225,091✔
706
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
15,511,028✔
707
      if (info->fset) {
15,511,028✔
708
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
2,095,744✔
709
      }
710
    }
711

712
    (void)taosThreadMutexUnlock(&tsdb->mutex);
2,714,063✔
713

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

718
_exit:
2,547,844✔
719
  if (code) {
5,261,907✔
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,261,907✔
723
  }
724
  return code;
5,261,907✔
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