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

taosdata / TDengine / #3565

25 Dec 2024 05:34AM UTC coverage: 51.098% (-11.1%) from 62.21%
#3565

push

travis-ci

web-flow
Merge pull request #29316 from taosdata/enh/3.0/TD-33266

enh(ut):Add wal & config UT.

111558 of 284773 branches covered (39.17%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 2 files covered. (50.0%)

39015 existing lines in 102 files now uncovered.

177882 of 281666 relevant lines covered (63.15%)

15090998.35 hits per line

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

5.11
/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
    SDiskID did;
42
    TSKEY   minKey;
43
    TSKEY   maxKey;
44
    TABLEID tbid[1];
45
    bool    hasTSData;
46

47
    bool      skipTsRow;
48
    SHashObj *pColCmprObj;
49
  } ctx[1];
50

51
  // reader
52
  TSttFileReaderArray sttReaderArray[1];
53
  // iter
54
  TTsdbIterArray dataIterArray[1];
55
  SIterMerger   *dataIterMerger;
56
  TTsdbIterArray tombIterArray[1];
57
  SIterMerger   *tombIterMerger;
58
  // writer
59
  SFSetWriter *writer;
60

61
  TFileOpArray fopArray[1];
62
} SCommitter2;
63

UNCOV
64
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
×
UNCOV
65
  int32_t code = 0;
×
UNCOV
66
  int32_t lino = 0;
×
67

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

UNCOV
82
  if (committer->sttTrigger == 1) {
×
UNCOV
83
    config.toSttOnly = false;
×
84

UNCOV
85
    if (committer->ctx->info->fset) {
×
UNCOV
86
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
×
UNCOV
87
        if (committer->ctx->info->fset->farr[ftype] != NULL) {
×
UNCOV
88
          config.files[ftype].exist = true;
×
UNCOV
89
          config.files[ftype].file = committer->ctx->info->fset->farr[ftype]->f[0];
×
90
        }
91
      }
92
    }
93
  }
94

UNCOV
95
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
×
96

UNCOV
97
_exit:
×
UNCOV
98
  if (code) {
×
99
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
100
              tstrerror(code));
101
  }
UNCOV
102
  return code;
×
103
}
104

UNCOV
105
static int32_t tsdbCommitCloseWriter(SCommitter2 *committer) {
×
UNCOV
106
  return tsdbFSetWriterClose(&committer->writer, 0, committer->fopArray);
×
107
}
108

UNCOV
109
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
×
UNCOV
110
  int32_t   code = 0;
×
UNCOV
111
  int32_t   lino = 0;
×
UNCOV
112
  int64_t   numOfRow = 0;
×
113
  SMetaInfo info;
114

UNCOV
115
  committer->ctx->hasTSData = false;
×
116

UNCOV
117
  committer->ctx->tbid->suid = 0;
×
UNCOV
118
  committer->ctx->tbid->uid = 0;
×
UNCOV
119
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
×
UNCOV
120
    if (row->uid != committer->ctx->tbid->uid) {
×
UNCOV
121
      committer->ctx->tbid->suid = row->suid;
×
UNCOV
122
      committer->ctx->tbid->uid = row->uid;
×
123

UNCOV
124
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
×
125
        TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
×
UNCOV
126
        continue;
×
127
      }
128
    }
129

UNCOV
130
    int64_t ts = TSDBROW_TS(&row->row);
×
UNCOV
131
    if (ts > committer->ctx->maxKey) {
×
UNCOV
132
      TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
×
UNCOV
133
      continue;
×
134
    }
135

UNCOV
136
    committer->ctx->hasTSData = true;
×
UNCOV
137
    numOfRow++;
×
138

UNCOV
139
    TAOS_CHECK_GOTO(tsdbFSetWriteRow(committer->writer, row), &lino, _exit);
×
UNCOV
140
    TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->dataIterMerger), &lino, _exit);
×
141
  }
142

UNCOV
143
_exit:
×
UNCOV
144
  if (code) {
×
145
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
146
              tstrerror(code));
147
  } else {
UNCOV
148
    tsdbDebug("vgId:%d fid:%d commit %" PRId64 " rows", TD_VID(committer->tsdb->pVnode), committer->ctx->info->fid,
×
149
              numOfRow);
150
  }
UNCOV
151
  return code;
×
152
}
153

UNCOV
154
static int32_t tsdbCommitTombData(SCommitter2 *committer) {
×
UNCOV
155
  int32_t   code = 0;
×
UNCOV
156
  int32_t   lino = 0;
×
UNCOV
157
  int64_t   numRecord = 0;
×
158
  SMetaInfo info;
159

160
  // if no history data and no new timestamp data, skip tomb data
UNCOV
161
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
×
UNCOV
162
    committer->ctx->tbid->suid = 0;
×
UNCOV
163
    committer->ctx->tbid->uid = 0;
×
UNCOV
164
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
×
UNCOV
165
      if (record->uid != committer->ctx->tbid->uid) {
×
UNCOV
166
        committer->ctx->tbid->suid = record->suid;
×
UNCOV
167
        committer->ctx->tbid->uid = record->uid;
×
168

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

UNCOV
175
      if (record->ekey < committer->ctx->minKey) {
×
176
        // do nothing
UNCOV
177
      } else if (record->skey > committer->ctx->maxKey) {
×
178
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
179
      } else {
UNCOV
180
        record->skey = TMAX(record->skey, committer->ctx->minKey);
×
UNCOV
181
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
×
182

UNCOV
183
        numRecord++;
×
UNCOV
184
        TAOS_CHECK_GOTO(tsdbFSetWriteTombRecord(committer->writer, record), &lino, _exit);
×
185
      }
186

UNCOV
187
      TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->tombIterMerger), &lino, _exit);
×
188
    }
189
  }
190

UNCOV
191
_exit:
×
UNCOV
192
  if (code) {
×
193
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
194
              tstrerror(code));
195
  } else {
UNCOV
196
    tsdbDebug("vgId:%d fid:%d commit %" PRId64 " tomb records", TD_VID(committer->tsdb->pVnode),
×
197
              committer->ctx->info->fid, numRecord);
198
  }
UNCOV
199
  return code;
×
200
}
201

UNCOV
202
static void tsdbCommitCloseReader(SCommitter2 *committer) {
×
UNCOV
203
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
×
UNCOV
204
  return;
×
205
}
206

UNCOV
207
static int32_t tsdbCommitOpenReader(SCommitter2 *committer) {
×
UNCOV
208
  int32_t code = 0;
×
UNCOV
209
  int32_t lino = 0;
×
210

UNCOV
211
  if (committer->ctx->info->fset == NULL                        //
×
UNCOV
212
      || committer->sttTrigger > 1                              //
×
UNCOV
213
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
×
214
  ) {
UNCOV
215
    return 0;
×
216
  }
217

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

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

UNCOV
230
      TAOS_CHECK_GOTO(tsdbSttFileReaderOpen(fobj->fname, &config, &sttReader), &lino, _exit);
×
231

UNCOV
232
      TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->sttReaderArray, sttReader), &lino, _exit);
×
233

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

UNCOV
240
      TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->fopArray, op), &lino, _exit);
×
241
    }
242
  }
243

UNCOV
244
_exit:
×
UNCOV
245
  if (code) {
×
246
    tsdbCommitCloseReader(committer);
×
247
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
248
              tstrerror(code));
249
  }
UNCOV
250
  return code;
×
251
}
252

UNCOV
253
static void tsdbCommitCloseIter(SCommitter2 *committer) {
×
UNCOV
254
  tsdbIterMergerClose(&committer->tombIterMerger);
×
UNCOV
255
  tsdbIterMergerClose(&committer->dataIterMerger);
×
UNCOV
256
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
×
UNCOV
257
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
×
UNCOV
258
  return;
×
259
}
260

UNCOV
261
static int32_t tsdbCommitOpenIter(SCommitter2 *committer) {
×
UNCOV
262
  int32_t code = 0;
×
UNCOV
263
  int32_t lino = 0;
×
264

265
  STsdbIter      *iter;
UNCOV
266
  STsdbIterConfig config = {0};
×
267

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

UNCOV
277
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
×
UNCOV
278
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
×
279

280
  // mem tomb iter
UNCOV
281
  config.type = TSDB_ITER_TYPE_MEMT_TOMB;
×
UNCOV
282
  config.memt = committer->tsdb->imem;
×
283

UNCOV
284
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
×
UNCOV
285
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
×
286

287
  // STT
288
  SSttFileReader *sttReader;
UNCOV
289
  TARRAY2_FOREACH(committer->sttReaderArray, sttReader) {
×
290
    // data iter
UNCOV
291
    config.type = TSDB_ITER_TYPE_STT;
×
UNCOV
292
    config.sttReader = sttReader;
×
293

UNCOV
294
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
×
UNCOV
295
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
×
296

297
    // tomb iter
UNCOV
298
    config.type = TSDB_ITER_TYPE_STT_TOMB;
×
UNCOV
299
    config.sttReader = sttReader;
×
300

UNCOV
301
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
×
302

UNCOV
303
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
×
304
  }
305

306
  // open merger
UNCOV
307
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false), &lino, _exit);
×
UNCOV
308
  TAOS_CHECK_GOTO(tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true), &lino, _exit);
×
309

UNCOV
310
_exit:
×
UNCOV
311
  if (code) {
×
312
    tsdbCommitCloseIter(committer);
×
313
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
314
              tstrerror(code));
315
  }
UNCOV
316
  return code;
×
317
}
318

UNCOV
319
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
×
UNCOV
320
  int32_t code = 0;
×
UNCOV
321
  int32_t lino = 0;
×
UNCOV
322
  STsdb  *tsdb = committer->tsdb;
×
323

324
  // check if can commit
UNCOV
325
  tsdbFSCheckCommit(tsdb, committer->ctx->info->fid);
×
326

UNCOV
327
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->info->fid, &tsdb->keepCfg, committer->now);
×
UNCOV
328
  tsdbFidKeyRange(committer->ctx->info->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
×
329
                  &committer->ctx->maxKey);
330

UNCOV
331
  TAOS_CHECK_GOTO(tfsAllocDisk(committer->tsdb->pVnode->pTfs, committer->ctx->expLevel, &committer->ctx->did), &lino,
×
332
                  _exit);
333

UNCOV
334
  if (tfsMkdirRecurAt(committer->tsdb->pVnode->pTfs, committer->tsdb->path, committer->ctx->did) != 0) {
×
335
    tsdbError("vgId:%d failed to create directory %s", TD_VID(committer->tsdb->pVnode), committer->tsdb->path);
×
336
  }
UNCOV
337
  committer->ctx->tbid->suid = 0;
×
UNCOV
338
  committer->ctx->tbid->uid = 0;
×
339

UNCOV
340
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
×
UNCOV
341
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
×
UNCOV
342
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
×
343

UNCOV
344
_exit:
×
UNCOV
345
  if (code) {
×
346
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
347
  } else {
UNCOV
348
    tsdbDebug("vgId:%d %s done, fid:%d minKey:%" PRId64 " maxKey:%" PRId64 " expLevel:%d", TD_VID(tsdb->pVnode),
×
349
              __func__, committer->ctx->info->fid, committer->ctx->minKey, committer->ctx->maxKey,
350
              committer->ctx->expLevel);
351
  }
UNCOV
352
  return code;
×
353
}
354

UNCOV
355
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
×
UNCOV
356
  int32_t code = 0;
×
UNCOV
357
  int32_t lino = 0;
×
358

UNCOV
359
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
×
UNCOV
360
  tsdbCommitCloseIter(committer);
×
UNCOV
361
  tsdbCommitCloseReader(committer);
×
362

UNCOV
363
_exit:
×
UNCOV
364
  if (code) {
×
365
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
366
              tstrerror(code));
367
  } else {
UNCOV
368
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->info->fid);
×
369
  }
UNCOV
370
  return code;
×
371
}
372

UNCOV
373
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
×
UNCOV
374
  int32_t code = 0;
×
UNCOV
375
  int32_t lino = 0;
×
376

UNCOV
377
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
×
UNCOV
378
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
×
UNCOV
379
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
×
UNCOV
380
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
×
381

UNCOV
382
_exit:
×
UNCOV
383
  if (code) {
×
384
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
385
              tstrerror(code));
386
  } else {
UNCOV
387
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->info->fid);
×
388
  }
UNCOV
389
  return code;
×
390
}
391

UNCOV
392
static int32_t tFileSetCommitInfoCompare(const void *arg1, const void *arg2) {
×
UNCOV
393
  SFileSetCommitInfo *info1 = (SFileSetCommitInfo *)arg1;
×
UNCOV
394
  SFileSetCommitInfo *info2 = (SFileSetCommitInfo *)arg2;
×
395

UNCOV
396
  if (info1->fid < info2->fid) {
×
UNCOV
397
    return -1;
×
398
  } else if (info1->fid > info2->fid) {
×
UNCOV
399
    return 1;
×
400
  } else {
401
    return 0;
×
402
  }
403
}
404

UNCOV
405
static int32_t tFileSetCommitInfoPCompare(const void *arg1, const void *arg2) {
×
UNCOV
406
  return tFileSetCommitInfoCompare(*(SFileSetCommitInfo **)arg1, *(SFileSetCommitInfo **)arg2);
×
407
}
408

UNCOV
409
static uint32_t tFileSetCommitInfoHash(const void *arg) {
×
UNCOV
410
  SFileSetCommitInfo *info = (SFileSetCommitInfo *)arg;
×
UNCOV
411
  return MurmurHash3_32((const char *)&info->fid, sizeof(info->fid));
×
412
}
413

UNCOV
414
static void tsdbCommitInfoDestroy(STsdb *pTsdb) {
×
UNCOV
415
  if (pTsdb->commitInfo) {
×
UNCOV
416
    for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
×
UNCOV
417
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
×
UNCOV
418
      int32_t             ret = vHashDrop(pTsdb->commitInfo->ht, info);
×
UNCOV
419
      tsdbTFileSetClear(&info->fset);
×
UNCOV
420
      taosMemoryFree(info);
×
421
    }
422

UNCOV
423
    vHashDestroy(&pTsdb->commitInfo->ht);
×
UNCOV
424
    taosArrayDestroy(pTsdb->commitInfo->arr);
×
UNCOV
425
    pTsdb->commitInfo->arr = NULL;
×
UNCOV
426
    taosMemoryFreeClear(pTsdb->commitInfo);
×
427
  }
UNCOV
428
  return;
×
429
}
430

UNCOV
431
static int32_t tsdbCommitInfoInit(STsdb *pTsdb) {
×
UNCOV
432
  int32_t code = 0;
×
UNCOV
433
  int32_t lino = 0;
×
434

UNCOV
435
  pTsdb->commitInfo = taosMemoryCalloc(1, sizeof(*pTsdb->commitInfo));
×
UNCOV
436
  if (pTsdb->commitInfo == NULL) {
×
437
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
438
  }
439

UNCOV
440
  TAOS_CHECK_GOTO(vHashInit(&pTsdb->commitInfo->ht, tFileSetCommitInfoHash, tFileSetCommitInfoCompare), &lino, _exit);
×
441

UNCOV
442
  pTsdb->commitInfo->arr = taosArrayInit(0, sizeof(SFileSetCommitInfo *));
×
UNCOV
443
  if (pTsdb->commitInfo->arr == NULL) {
×
444
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
445
  }
446

UNCOV
447
_exit:
×
UNCOV
448
  if (code) {
×
449
    tsdbCommitInfoDestroy(pTsdb);
×
450
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
451
  }
UNCOV
452
  return code;
×
453
}
454

UNCOV
455
static int32_t tsdbCommitInfoAdd(STsdb *tsdb, int32_t fid) {
×
UNCOV
456
  int32_t code = 0;
×
UNCOV
457
  int32_t lino = 0;
×
458

459
  SFileSetCommitInfo *tinfo;
460

UNCOV
461
  if ((tinfo = taosMemoryMalloc(sizeof(*tinfo))) == NULL) {
×
462
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
463
  }
UNCOV
464
  tinfo->fid = fid;
×
UNCOV
465
  tinfo->fset = NULL;
×
466

UNCOV
467
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
×
468

UNCOV
469
  if ((taosArrayPush(tsdb->commitInfo->arr, &tinfo)) == NULL) {
×
470
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
471
  }
UNCOV
472
  taosArraySort(tsdb->commitInfo->arr, tFileSetCommitInfoPCompare);
×
473

UNCOV
474
_exit:
×
UNCOV
475
  if (code) {
×
476
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
477
  }
UNCOV
478
  return code;
×
479
}
480

UNCOV
481
static int32_t tsdbCommitInfoBuild(STsdb *tsdb) {
×
UNCOV
482
  int32_t code = 0;
×
UNCOV
483
  int32_t lino = 0;
×
484

UNCOV
485
  STFileSet  *fset = NULL;
×
486
  SRBTreeIter iter;
487

UNCOV
488
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
×
489

490
  // scan time-series data
UNCOV
491
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
×
UNCOV
492
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
×
UNCOV
493
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
×
494

495
    // scan time-series data
UNCOV
496
    STsdbRowKey from = {
×
497
        .key.ts = INT64_MIN,
498
        .key.numOfPKs = 0,
499
        .version = INT64_MIN,
500
    };
UNCOV
501
    for (;;) {
×
502
      int64_t     minKey, maxKey;
UNCOV
503
      STbDataIter tbDataIter = {0};
×
504
      TSDBROW    *row;
505
      int32_t     fid;
506

UNCOV
507
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
×
UNCOV
508
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
×
UNCOV
509
        break;
×
510
      }
511

UNCOV
512
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
×
UNCOV
513
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
×
514

515
      SFileSetCommitInfo *info;
UNCOV
516
      SFileSetCommitInfo  tinfo = {
×
517
           .fid = fid,
518
      };
UNCOV
519
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
×
UNCOV
520
      if (info == NULL) {
×
UNCOV
521
        TAOS_CHECK_GOTO(tsdbCommitInfoAdd(tsdb, fid), &lino, _exit);
×
522
      }
523

UNCOV
524
      from.key.ts = maxKey + 1;
×
525
    }
526
  }
527

UNCOV
528
  (void)taosThreadMutexLock(&tsdb->mutex);
×
529

530
  // scan tomb data
UNCOV
531
  if (tsdb->imem->nDel > 0) {
×
UNCOV
532
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
×
UNCOV
533
      if (tsdbTFileSetIsEmpty(fset)) {
×
UNCOV
534
        continue;
×
535
      }
536

537
      SFileSetCommitInfo *info;
UNCOV
538
      SFileSetCommitInfo  tinfo = {
×
UNCOV
539
           .fid = fset->fid,
×
540
      };
541

542
      // check if the file set already on the commit list
UNCOV
543
      int32_t ret = vHashGet(tsdb->commitInfo->ht, &tinfo, (void **)&info);
×
UNCOV
544
      if (info != NULL) {
×
UNCOV
545
        continue;
×
546
      }
547

548
      int64_t minKey, maxKey;
UNCOV
549
      bool    hasDataToCommit = false;
×
UNCOV
550
      tsdbFidKeyRange(fset->fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
×
UNCOV
551
      iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
×
UNCOV
552
      for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
×
UNCOV
553
        STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
×
UNCOV
554
        for (SDelData *pDelData = pTbData->pHead; pDelData; pDelData = pDelData->pNext) {
×
UNCOV
555
          if (pDelData->sKey > maxKey || pDelData->eKey < minKey) {
×
UNCOV
556
            continue;
×
557
          } else {
UNCOV
558
            hasDataToCommit = true;
×
UNCOV
559
            if ((code = tsdbCommitInfoAdd(tsdb, fset->fid))) {
×
560
              (void)taosThreadMutexUnlock(&tsdb->mutex);
×
561
              TSDB_CHECK_CODE(code, lino, _exit);
×
562
            }
UNCOV
563
            break;
×
564
          }
565
        }
566

UNCOV
567
        if (hasDataToCommit) {
×
UNCOV
568
          break;
×
569
        }
570
      }
571
    }
572
  }
573

574
  // begin tasks on file set
UNCOV
575
  for (int i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
×
UNCOV
576
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
×
UNCOV
577
    tsdbBeginTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT, &fset);
×
UNCOV
578
    if (fset) {
×
UNCOV
579
      code = tsdbTFileSetInitCopy(tsdb, fset, &info->fset);
×
UNCOV
580
      if (code) {
×
581
        (void)taosThreadMutexUnlock(&tsdb->mutex);
×
582
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
583
      }
584
    }
585
  }
586

UNCOV
587
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
588

UNCOV
589
_exit:
×
UNCOV
590
  if (code) {
×
591
    tsdbCommitInfoDestroy(tsdb);
×
592
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
593
  }
UNCOV
594
  return code;
×
595
}
596

UNCOV
597
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
×
UNCOV
598
  int32_t code = 0;
×
UNCOV
599
  int32_t lino = 0;
×
600

UNCOV
601
  committer->tsdb = tsdb;
×
UNCOV
602
  committer->minutes = tsdb->keepCfg.days;
×
UNCOV
603
  committer->precision = tsdb->keepCfg.precision;
×
UNCOV
604
  committer->minRow = info->info.config.tsdbCfg.minRows;
×
UNCOV
605
  committer->maxRow = info->info.config.tsdbCfg.maxRows;
×
UNCOV
606
  committer->cmprAlg = info->info.config.tsdbCfg.compression;
×
UNCOV
607
  committer->sttTrigger = info->info.config.sttTrigger;
×
UNCOV
608
  committer->szPage = info->info.config.tsdbPageSize;
×
UNCOV
609
  committer->compactVersion = INT64_MAX;
×
UNCOV
610
  committer->cid = tsdbFSAllocEid(tsdb->pFS);
×
UNCOV
611
  committer->now = taosGetTimestampSec();
×
612

UNCOV
613
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
×
614

UNCOV
615
_exit:
×
UNCOV
616
  if (code) {
×
617
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
618
  } else {
UNCOV
619
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
×
620
  }
UNCOV
621
  return code;
×
622
}
623

UNCOV
624
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
×
UNCOV
625
  int32_t code = 0;
×
UNCOV
626
  int32_t lino = 0;
×
627

UNCOV
628
  if (eno == 0) {
×
UNCOV
629
    TAOS_CHECK_GOTO(tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT), &lino, _exit);
×
630
  } else {
631
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(committer->tsdb->pVnode), __func__, __FILE__, lino,
×
632
              tstrerror(eno));
633
  }
634

UNCOV
635
  TARRAY2_DESTROY(committer->dataIterArray, NULL);
×
UNCOV
636
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
×
UNCOV
637
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
×
UNCOV
638
  TARRAY2_DESTROY(committer->fopArray, NULL);
×
UNCOV
639
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
×
640

UNCOV
641
_exit:
×
UNCOV
642
  if (code) {
×
643
    tsdbError("vgId:%d %s failed at %s:%d since %s, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, __FILE__,
×
644
              lino, tstrerror(code), committer->cid);
645
  } else {
UNCOV
646
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, committer->cid);
×
647
  }
UNCOV
648
  return code;
×
649
}
650

651
int32_t tsdbPreCommit(STsdb *tsdb) {
2✔
652
  (void)taosThreadMutexLock(&tsdb->mutex);
2✔
653
  ASSERT_CORE(tsdb->imem == NULL, "imem should be null to commit mem");
2!
654
  tsdb->imem = tsdb->mem;
2✔
655
  tsdb->mem = NULL;
2✔
656
  (void)taosThreadMutexUnlock(&tsdb->mutex);
2✔
657
  return 0;
2✔
658
}
659

660
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
2✔
661
  if (!tsdb) return 0;
2!
662

663
  int32_t code = 0;
2✔
664
  int32_t lino = 0;
2✔
665

666
  SMemTable *imem = tsdb->imem;
2✔
667
  int64_t    nRow = imem->nRow;
2✔
668
  int64_t    nDel = imem->nDel;
2✔
669

670
  if ((nRow == 0 && nDel == 0) || (tsBypassFlag & TSDB_BYPASS_RB_TSDB_COMMIT)) {
2!
671
    (void)taosThreadMutexLock(&tsdb->mutex);
2✔
672
    tsdb->imem = NULL;
2✔
673
    (void)taosThreadMutexUnlock(&tsdb->mutex);
2✔
674
    tsdbUnrefMemTable(imem, NULL, true);
2✔
675
  } else {
UNCOV
676
    SCommitter2 committer = {0};
×
677

UNCOV
678
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
×
679

UNCOV
680
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
×
UNCOV
681
      committer.ctx->info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
×
UNCOV
682
      TAOS_CHECK_GOTO(tsdbCommitFileSet(&committer), &lino, _exit);
×
683
    }
684

UNCOV
685
    TAOS_CHECK_GOTO(tsdbCloseCommitter(&committer, code), &lino, _exit);
×
686
  }
687

688
_exit:
2✔
689
  if (code) {
2!
690
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
691
  } else {
692
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, TD_VID(tsdb->pVnode), __func__, nRow, nDel);
2!
693
  }
694
  return code;
2✔
695
}
696

697
int32_t tsdbCommitCommit(STsdb *tsdb) {
2✔
698
  int32_t code = 0;
2✔
699
  int32_t lino = 0;
2✔
700

701
  if (tsdb->imem) {
2!
UNCOV
702
    SMemTable *pMemTable = tsdb->imem;
×
703

UNCOV
704
    (void)taosThreadMutexLock(&tsdb->mutex);
×
705

UNCOV
706
    if ((code = tsdbFSEditCommit(tsdb->pFS))) {
×
707
      (void)taosThreadMutexUnlock(&tsdb->mutex);
×
708
      TSDB_CHECK_CODE(code, lino, _exit);
×
709
    }
UNCOV
710
    tsdb->imem = NULL;
×
711

UNCOV
712
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
×
UNCOV
713
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
×
UNCOV
714
      if (info->fset) {
×
UNCOV
715
        tsdbFinishTaskOnFileSet(tsdb, info->fid, EVA_TASK_COMMIT);
×
716
      }
717
    }
718

UNCOV
719
    (void)taosThreadMutexUnlock(&tsdb->mutex);
×
720

UNCOV
721
    tsdbCommitInfoDestroy(tsdb);
×
UNCOV
722
    tsdbUnrefMemTable(pMemTable, NULL, true);
×
723
  }
724

725
_exit:
2✔
726
  if (code) {
2!
727
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
728
  } else {
729
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
2!
730
  }
731
  return code;
2✔
732
}
733

734
int32_t tsdbCommitAbort(STsdb *pTsdb) {
×
735
  int32_t code = 0;
×
736
  int32_t lino = 0;
×
737

738
  if (pTsdb->imem == NULL) goto _exit;
×
739

740
  TAOS_CHECK_GOTO(tsdbFSEditAbort(pTsdb->pFS), &lino, _exit);
×
741

742
  (void)taosThreadMutexLock(&pTsdb->mutex);
×
743
  for (int32_t i = 0; i < taosArrayGetSize(pTsdb->commitInfo->arr); i++) {
×
744
    SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(pTsdb->commitInfo->arr, i);
×
745
    if (info->fset) {
×
746
      tsdbFinishTaskOnFileSet(pTsdb, info->fid, EVA_TASK_COMMIT);
×
747
    }
748
  }
749
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
750
  tsdbCommitInfoDestroy(pTsdb);
×
751

752
_exit:
×
753
  if (code) {
×
754
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
×
755
  } else {
756
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
×
757
  }
758
  return code;
×
759
}
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