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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

73.9
/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

64
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
152,121✔
65
  int32_t code = 0;
152,121✔
66
  int32_t lino = 0;
152,121✔
67

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

82
  if (committer->sttTrigger == 1) {
152,121✔
83
    config.toSttOnly = false;
1,443✔
84

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

95
  TAOS_CHECK_GOTO(tsdbFSetWriterOpen(&config, &committer->writer), &lino, _exit);
152,121!
96

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

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

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

115
  committer->ctx->hasTSData = false;
152,111✔
116

117
  committer->ctx->tbid->suid = 0;
152,111✔
118
  committer->ctx->tbid->uid = 0;
152,111✔
119
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
659,786,912✔
120
    if (row->uid != committer->ctx->tbid->uid) {
659,163,142✔
121
      committer->ctx->tbid->suid = row->suid;
570,119✔
122
      committer->ctx->tbid->uid = row->uid;
570,119✔
123

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

130
    int64_t ts = TSDBROW_TS(&row->row);
659,199,759✔
131
    if (ts > committer->ctx->maxKey) {
659,199,759✔
132
      TAOS_CHECK_GOTO(tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid), &lino, _exit);
384,094!
133
      continue;
384,087✔
134
    }
135

136
    committer->ctx->hasTSData = true;
658,815,665✔
137
    numOfRow++;
658,815,665✔
138

139
    TAOS_CHECK_GOTO(tsdbFSetWriteRow(committer->writer, row), &lino, _exit);
658,815,665!
140
    TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->dataIterMerger), &lino, _exit);
660,332,354!
141
  }
142

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

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

160
  // if no history data and no new timestamp data, skip tomb data
161
  if (committer->ctx->info->fset || committer->ctx->hasTSData) {
152,101✔
162
    committer->ctx->tbid->suid = 0;
152,020✔
163
    committer->ctx->tbid->uid = 0;
152,020✔
164
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
323,921✔
165
      if (record->uid != committer->ctx->tbid->uid) {
171,903✔
166
        committer->ctx->tbid->suid = record->suid;
36,813✔
167
        committer->ctx->tbid->uid = record->uid;
36,813✔
168

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

175
      if (record->ekey < committer->ctx->minKey) {
171,875✔
176
        // do nothing
177
      } else if (record->skey > committer->ctx->maxKey) {
112,770✔
178
        // committer->ctx->nextKey = TMIN(record->skey, committer->ctx->nextKey);
179
      } else {
180
        record->skey = TMAX(record->skey, committer->ctx->minKey);
62,740✔
181
        record->ekey = TMIN(record->ekey, committer->ctx->maxKey);
62,740✔
182

183
        numRecord++;
62,740✔
184
        TAOS_CHECK_GOTO(tsdbFSetWriteTombRecord(committer->writer, record), &lino, _exit);
62,740!
185
      }
186

187
      TAOS_CHECK_GOTO(tsdbIterMergerNext(committer->tombIterMerger), &lino, _exit);
171,875!
188
    }
189
  }
190

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

202
static void tsdbCommitCloseReader(SCommitter2 *committer) {
152,128✔
203
  TARRAY2_CLEAR(committer->sttReaderArray, tsdbSttFileReaderClose);
152,134✔
204
  return;
152,128✔
205
}
206

207
static int32_t tsdbCommitOpenReader(SCommitter2 *committer) {
152,109✔
208
  int32_t code = 0;
152,109✔
209
  int32_t lino = 0;
152,109✔
210

211
  if (committer->ctx->info->fset == NULL                        //
152,109✔
212
      || committer->sttTrigger > 1                              //
10,168✔
213
      || TARRAY2_SIZE(committer->ctx->info->fset->lvlArr) == 0  //
301✔
214
  ) {
215
    return 0;
152,103✔
216
  }
217

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

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

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

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

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

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

244
_exit:
6✔
245
  if (code) {
6!
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
  }
250
  return code;
6✔
251
}
252

253
static void tsdbCommitCloseIter(SCommitter2 *committer) {
152,122✔
254
  tsdbIterMergerClose(&committer->tombIterMerger);
152,122✔
255
  tsdbIterMergerClose(&committer->dataIterMerger);
152,164✔
256
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
304,281!
257
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
304,330!
258
  return;
152,166✔
259
}
260

261
static int32_t tsdbCommitOpenIter(SCommitter2 *committer) {
152,085✔
262
  int32_t code = 0;
152,085✔
263
  int32_t lino = 0;
152,085✔
264

265
  STsdbIter      *iter;
266
  STsdbIterConfig config = {0};
152,085✔
267

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

277
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
152,085!
278
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
304,278!
279

280
  // mem tomb iter
281
  config.type = TSDB_ITER_TYPE_MEMT_TOMB;
152,140✔
282
  config.memt = committer->tsdb->imem;
152,140✔
283

284
  TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
152,140!
285
  TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->tombIterArray, iter), &lino, _exit);
304,334!
286

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

294
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
6!
295
    TAOS_CHECK_GOTO(TARRAY2_APPEND(committer->dataIterArray, iter), &lino, _exit);
12!
296

297
    // tomb iter
298
    config.type = TSDB_ITER_TYPE_STT_TOMB;
6✔
299
    config.sttReader = sttReader;
6✔
300

301
    TAOS_CHECK_GOTO(tsdbIterOpen(&config, &iter), &lino, _exit);
6!
302

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

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

310
_exit:
152,162✔
311
  if (code) {
152,162!
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
  }
316
  return code;
152,151✔
317
}
318

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

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

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

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

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

340
  TAOS_CHECK_GOTO(tsdbCommitOpenReader(committer), &lino, _exit);
152,120!
341
  TAOS_CHECK_GOTO(tsdbCommitOpenIter(committer), &lino, _exit);
152,124!
342
  TAOS_CHECK_GOTO(tsdbCommitOpenWriter(committer), &lino, _exit);
152,126!
343

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

355
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
152,101✔
356
  int32_t code = 0;
152,101✔
357
  int32_t lino = 0;
152,101✔
358

359
  TAOS_CHECK_GOTO(tsdbCommitCloseWriter(committer), &lino, _exit);
152,101!
360
  tsdbCommitCloseIter(committer);
152,125✔
361
  tsdbCommitCloseReader(committer);
152,165✔
362

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

373
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
152,137✔
374
  int32_t code = 0;
152,137✔
375
  int32_t lino = 0;
152,137✔
376

377
  TAOS_CHECK_GOTO(tsdbCommitFileSetBegin(committer), &lino, _exit);
152,137!
378
  TAOS_CHECK_GOTO(tsdbCommitTSData(committer), &lino, _exit);
152,124!
379
  TAOS_CHECK_GOTO(tsdbCommitTombData(committer), &lino, _exit);
152,099!
380
  TAOS_CHECK_GOTO(tsdbCommitFileSetEnd(committer), &lino, _exit);
152,108!
381

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

392
static int32_t tFileSetCommitInfoCompare(const void *arg1, const void *arg2) {
201,233,547✔
393
  SFileSetCommitInfo *info1 = (SFileSetCommitInfo *)arg1;
201,233,547✔
394
  SFileSetCommitInfo *info2 = (SFileSetCommitInfo *)arg2;
201,233,547✔
395

396
  if (info1->fid < info2->fid) {
201,233,547!
397
    return -1;
202,723,170✔
UNCOV
398
  } else if (info1->fid > info2->fid) {
×
399
    return 1;
84,336✔
400
  } else {
UNCOV
401
    return 0;
×
402
  }
403
}
404

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

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

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

423
    vHashDestroy(&pTsdb->commitInfo->ht);
11,551✔
424
    taosArrayDestroy(pTsdb->commitInfo->arr);
11,551✔
425
    pTsdb->commitInfo->arr = NULL;
11,551✔
426
    taosMemoryFreeClear(pTsdb->commitInfo);
11,551!
427
  }
428
  return;
11,551✔
429
}
430

431
static int32_t tsdbCommitInfoInit(STsdb *pTsdb) {
11,549✔
432
  int32_t code = 0;
11,549✔
433
  int32_t lino = 0;
11,549✔
434

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

440
  TAOS_CHECK_GOTO(vHashInit(&pTsdb->commitInfo->ht, tFileSetCommitInfoHash, tFileSetCommitInfoCompare), &lino, _exit);
11,550!
441

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

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

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

459
  SFileSetCommitInfo *tinfo;
460

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

467
  TAOS_CHECK_GOTO(vHashPut(tsdb->commitInfo->ht, tinfo), &lino, _exit);
152,139!
468

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

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

481
static int32_t tsdbCommitInfoBuild(STsdb *tsdb) {
11,551✔
482
  int32_t code = 0;
11,551✔
483
  int32_t lino = 0;
11,551✔
484

485
  STFileSet  *fset = NULL;
11,551✔
486
  SRBTreeIter iter;
487

488
  TAOS_CHECK_GOTO(tsdbCommitInfoInit(tsdb), &lino, _exit);
11,551!
489

490
  // scan time-series data
491
  iter = tRBTreeIterCreate(tsdb->imem->tbDataTree, 1);
11,551✔
492
  for (SRBTreeNode *node = tRBTreeIterNext(&iter); node; node = tRBTreeIterNext(&iter)) {
183,529✔
493
    STbData *pTbData = TCONTAINER_OF(node, STbData, rbtn);
171,978✔
494

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

507
      tsdbTbDataIterOpen(pTbData, &from, 0, &tbDataIter);
690,987✔
508
      if ((row = tsdbTbDataIterGet(&tbDataIter)) == NULL) {
683,484✔
509
        break;
171,169✔
510
      }
511

512
      fid = tsdbKeyFid(TSDBROW_TS(row), tsdb->keepCfg.days, tsdb->keepCfg.precision);
512,315!
513
      tsdbFidKeyRange(fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &minKey, &maxKey);
517,838✔
514

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

524
      from.key.ts = maxKey + 1;
519,009✔
525
    }
526
  }
527

528
  (void)taosThreadMutexLock(&tsdb->mutex);
11,551✔
529

530
  // scan tomb data
531
  if (tsdb->imem->nDel > 0) {
11,551✔
532
    TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
11,784✔
533
      if (tsdbTFileSetIsEmpty(fset)) {
7,043!
534
        continue;
3,975✔
535
      }
536

537
      SFileSetCommitInfo *info;
538
      SFileSetCommitInfo  tinfo = {
7,043✔
539
           .fid = fset->fid,
7,043✔
540
      };
541

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

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

567
        if (hasDataToCommit) {
23,310✔
568
          break;
2,750✔
569
        }
570
      }
571
    }
572
  }
573

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

587
  (void)taosThreadMutexUnlock(&tsdb->mutex);
11,551✔
588

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

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

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

613
  TAOS_CHECK_GOTO(tsdbCommitInfoBuild(tsdb), &lino, _exit);
11,551!
614

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

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

628
  if (eno == 0) {
11,551!
629
    TAOS_CHECK_GOTO(tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT), &lino, _exit);
11,551!
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

635
  TARRAY2_DESTROY(committer->dataIterArray, NULL);
11,551!
636
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
11,551!
637
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
11,551✔
638
  TARRAY2_DESTROY(committer->fopArray, NULL);
11,551✔
639
  TARRAY2_DESTROY(committer->sttReaderArray, NULL);
11,551!
640

641
_exit:
11,551✔
642
  if (code) {
11,551!
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 {
646
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, committer->cid);
11,551✔
647
  }
648
  return code;
11,551✔
649
}
650

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

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

663
  int32_t code = 0;
32,479✔
664
  int32_t lino = 0;
32,479✔
665

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

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

678
    TAOS_CHECK_GOTO(tsdbOpenCommitter(tsdb, info, &committer), &lino, _exit);
11,551!
679

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

685
    TAOS_CHECK_GOTO(tsdbCloseCommitter(&committer, code), &lino, _exit);
11,549!
686
  }
687

688
_exit:
32,478✔
689
  if (code) {
32,478!
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);
32,478!
693
  }
694
  return code;
32,479✔
695
}
696

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

701
  if (tsdb->imem) {
32,479✔
702
    SMemTable *pMemTable = tsdb->imem;
11,551✔
703

704
    (void)taosThreadMutexLock(&tsdb->mutex);
11,551✔
705

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

712
    for (int32_t i = 0; i < taosArrayGetSize(tsdb->commitInfo->arr); i++) {
163,727✔
713
      SFileSetCommitInfo *info = *(SFileSetCommitInfo **)taosArrayGet(tsdb->commitInfo->arr, i);
152,176✔
714
      if (info->fset) {
152,176✔
715
        tsdbFinishTaskOnFileSet(tsdb, info->fid);
10,168✔
716
      }
717
    }
718

719
    (void)taosThreadMutexUnlock(&tsdb->mutex);
11,551✔
720

721
    tsdbCommitInfoDestroy(tsdb);
11,551✔
722
    tsdbUnrefMemTable(pMemTable, NULL, true);
11,551✔
723
  }
724

725
_exit:
20,928✔
726
  if (code) {
32,479!
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__);
32,479!
730
  }
731
  return code;
32,479✔
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);
×
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

© 2025 Coveralls, Inc