tsdbCommit2.c 15.6 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdbCommit2.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
// extern dependencies
H
Hongze Cheng 已提交
19
typedef struct {
H
Hongze Cheng 已提交
20 21
  STsdb         *tsdb;
  TFileSetArray *fsetArr;
H
Hongze Cheng 已提交
22 23
  TFileOpArray   fopArray[1];

H
Hongze Cheng 已提交
24 25
  // SSkmInfo skmTb[1];
  // SSkmInfo skmRow[1];
H
Hongze Cheng 已提交
26

H
Hongze Cheng 已提交
27 28 29 30 31
  int32_t minutes;
  int8_t  precision;
  int32_t minRow;
  int32_t maxRow;
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
32 33
  int32_t sttTrigger;
  int32_t szPage;
H
Hongze Cheng 已提交
34 35 36
  int64_t compactVersion;

  struct {
H
Hongze Cheng 已提交
37
    int64_t    cid;
H
Hongze Cheng 已提交
38 39 40 41
    int64_t    now;
    TSKEY      nextKey;
    int32_t    fid;
    int32_t    expLevel;
H
Hongze Cheng 已提交
42
    SDiskID    did;
H
Hongze Cheng 已提交
43 44 45 46 47 48
    TSKEY      minKey;
    TSKEY      maxKey;
    STFileSet *fset;
    TABLEID    tbid[1];
  } ctx[1];

49
  // reader
H
Hongze Cheng 已提交
50
  SSttFileReader *sttReader;
51 52 53 54 55 56

  // iter
  TTsdbIterArray dataIterArray[1];
  SIterMerger   *dataIterMerger;
  TTsdbIterArray tombIterArray[1];
  SIterMerger   *tombIterMerger;
H
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58
  // writer
H
Hongze Cheng 已提交
59
  SFSetWriter *writer;
H
Hongze Cheng 已提交
60
} SCommitter2;
H
Hongze Cheng 已提交
61

62
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
63
  int32_t code = 0;
H
Hongze Cheng 已提交
64
  int32_t lino = 0;
H
Hongze Cheng 已提交
65

H
Hongze Cheng 已提交
66
  SFSetWriterConfig config = {
H
Hongze Cheng 已提交
67
      .tsdb = committer->tsdb,
H
Hongze Cheng 已提交
68 69 70
      .toSttOnly = true,
      .compactVersion = committer->compactVersion,
      .minRow = committer->minRow,
H
Hongze Cheng 已提交
71
      .maxRow = committer->maxRow,
H
Hongze Cheng 已提交
72
      .szPage = committer->szPage,
H
Hongze Cheng 已提交
73
      .cmprAlg = committer->cmprAlg,
H
Hongze Cheng 已提交
74 75
      .fid = committer->ctx->fid,
      .cid = committer->ctx->cid,
H
Hongze Cheng 已提交
76
      .did = committer->ctx->did,
H
Hongze Cheng 已提交
77
      .level = 0,
H
Hongze Cheng 已提交
78
  };
H
Hongze Cheng 已提交
79

H
Hongze Cheng 已提交
80
  if (committer->sttTrigger == 1) {
H
Hongze Cheng 已提交
81
    config.toSttOnly = false;
H
Hongze Cheng 已提交
82 83 84 85 86 87 88 89 90

    if (committer->ctx->fset) {
      for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ftype++) {
        if (committer->ctx->fset->farr[ftype] != NULL) {
          config.files[ftype].exist = true;
          config.files[ftype].file = committer->ctx->fset->farr[ftype]->f[0];
        }
      }
    }
H
Hongze Cheng 已提交
91
  }
H
Hongze Cheng 已提交
92

H
Hongze Cheng 已提交
93 94 95
  code = tsdbFSetWriterOpen(&config, &committer->writer);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
96 97 98
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
99 100
  }
  return code;
H
Hongze Cheng 已提交
101 102
}

103
static int32_t tsdbCommitCloseWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
104
  return tsdbFSetWriterClose(&committer->writer, 0, committer->fopArray);
H
Hongze Cheng 已提交
105 106 107
}

static int32_t tsdbCommitTSData(SCommitter2 *committer) {
H
Hongze Cheng 已提交
108 109 110
  int32_t   code = 0;
  int32_t   lino = 0;
  SMetaInfo info;
H
Hongze Cheng 已提交
111

H
Hongze Cheng 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
    if (row->uid != committer->ctx->tbid->uid) {
      // Ignore table of obsolescence
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
        code = tsdbIterMergerSkipTableData(committer->dataIterMerger, (TABLEID *)row);
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
      }

      committer->ctx->tbid->suid = row->suid;
      committer->ctx->tbid->uid = row->uid;
    }

    code = tsdbFSetWriteRow(committer->writer, row);
H
Hongze Cheng 已提交
126
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
127 128

    code = tsdbIterMergerNext(committer->dataIterMerger);
H
Hongze Cheng 已提交
129 130 131
    TSDB_CHECK_CODE(code, lino, _exit);
  }

132 133 134 135 136 137 138 139 140 141 142
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbCommitTombData(SCommitter2 *committer) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
143
  for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
H
Hongze Cheng 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if (record->ekey < committer->ctx->minKey) {
      continue;
    } else if (record->skey > committer->ctx->maxKey) {
      committer->ctx->maxKey = TMIN(record->skey, committer->ctx->maxKey);
      continue;
    }

    if (record->ekey > committer->ctx->maxKey) {
      committer->ctx->maxKey = committer->ctx->maxKey + 1;
    }

    record->skey = TMAX(record->skey, committer->ctx->minKey);
    record->ekey = TMIN(record->ekey, committer->ctx->maxKey);

H
Hongze Cheng 已提交
158 159
    code = tsdbFSetWriteTombRecord(committer->writer, record);
    TSDB_CHECK_CODE(code, lino, _exit);
160

H
Hongze Cheng 已提交
161 162
    code = tsdbIterMergerNext(committer->tombIterMerger);
    TSDB_CHECK_CODE(code, lino, _exit);
163
  }
H
Hongze Cheng 已提交
164 165 166 167

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
168 169 170 171
  }
  return code;
}

172
static int32_t tsdbCommitOpenReader(SCommitter2 *committer) {
H
Hongze Cheng 已提交
173 174 175
  int32_t code = 0;
  int32_t lino = 0;

176
  ASSERT(committer->sttReader == NULL);
H
Hongze Cheng 已提交
177

178 179 180 181 182 183
  if (committer->ctx->fset == NULL                        //
      || committer->sttTrigger > 1                        //
      || TARRAY2_SIZE(committer->ctx->fset->lvlArr) == 0  //
  ) {
    return 0;
  }
H
Hongze Cheng 已提交
184

185
  ASSERT(TARRAY2_SIZE(committer->ctx->fset->lvlArr) == 1);
H
Hongze Cheng 已提交
186

187
  SSttLvl *lvl = TARRAY2_FIRST(committer->ctx->fset->lvlArr);
H
Hongze Cheng 已提交
188

189
  ASSERT(lvl->level == 0);
H
Hongze Cheng 已提交
190

191 192
  if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
    return 0;
H
Hongze Cheng 已提交
193 194
  }

195
  ASSERT(TARRAY2_SIZE(lvl->fobjArr) == 1);
H
Hongze Cheng 已提交
196

197
  STFileObj *fobj = TARRAY2_FIRST(lvl->fobjArr);
H
Hongze Cheng 已提交
198

199 200 201 202 203 204
  SSttFileReaderConfig config = {
      .tsdb = committer->tsdb,
      .szPage = committer->szPage,
      .file = fobj->f[0],
  };
  code = tsdbSttFileReaderOpen(fobj->fname, &config, &committer->sttReader);
H
Hongze Cheng 已提交
205 206
  TSDB_CHECK_CODE(code, lino, _exit);

207 208 209 210 211 212 213
  STFileOp op = {
      .optype = TSDB_FOP_REMOVE,
      .fid = fobj->f->fid,
      .of = fobj->f[0],
  };

  code = TARRAY2_APPEND(committer->fopArray, op);
H
Hongze Cheng 已提交
214 215 216 217 218 219 220 221 222
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
  }
  return code;
}

223
static int32_t tsdbCommitCloseReader(SCommitter2 *committer) { return tsdbSttFileReaderClose(&committer->sttReader); }
H
Hongze Cheng 已提交
224

225
static int32_t tsdbCommitOpenIter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
226 227 228
  int32_t code = 0;
  int32_t lino = 0;

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  ASSERT(TARRAY2_SIZE(committer->dataIterArray) == 0);
  ASSERT(committer->dataIterMerger == NULL);
  ASSERT(TARRAY2_SIZE(committer->tombIterArray) == 0);
  ASSERT(committer->tombIterMerger == NULL);

  STsdbIter      *iter;
  STsdbIterConfig config = {0};

  // mem data iter
  config.type = TSDB_ITER_TYPE_MEMT;
  config.memt = committer->tsdb->imem;
  config.from->ts = committer->ctx->minKey;
  config.from->version = VERSION_MIN;

  code = tsdbIterOpen(&config, &iter);
H
Hongze Cheng 已提交
244 245
  TSDB_CHECK_CODE(code, lino, _exit);

246 247
  code = TARRAY2_APPEND(committer->dataIterArray, iter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
248

249 250 251
  // mem tomb iter
  config.type = TSDB_ITER_TYPE_MEMT_TOMB;
  config.memt = committer->tsdb->imem;
H
Hongze Cheng 已提交
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  code = tsdbIterOpen(&config, &iter);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = TARRAY2_APPEND(committer->tombIterArray, iter);
  TSDB_CHECK_CODE(code, lino, _exit);

  // STT
  if (committer->sttReader) {
    // data iter
    config.type = TSDB_ITER_TYPE_STT;
    config.sttReader = committer->sttReader;

    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(committer->dataIterArray, iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    // tomb iter
    config.type = TSDB_ITER_TYPE_STT_TOMB;
    config.sttReader = committer->sttReader;

    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(committer->tombIterArray, iter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
280 281
  }

282 283 284 285 286
  // open merger
  code = tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbIterMergerOpen(committer->tombIterArray, &committer->tombIterMerger, true);
H
Hongze Cheng 已提交
287 288
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
289 290
_exit:
  if (code) {
H
Hongze Cheng 已提交
291
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
292 293 294 295
  }
  return code;
}

296 297 298 299 300 301 302 303
static int32_t tsdbCommitCloseIter(SCommitter2 *committer) {
  tsdbIterMergerClose(&committer->tombIterMerger);
  tsdbIterMergerClose(&committer->dataIterMerger);
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
  return 0;
}

H
Hongze Cheng 已提交
304 305 306 307 308 309
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
  int32_t code = 0;
  int32_t lino = 0;
  STsdb  *tsdb = committer->tsdb;

  committer->ctx->fid = tsdbKeyFid(committer->ctx->nextKey, committer->minutes, committer->precision);
H
Hongze Cheng 已提交
310
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->fid, &tsdb->keepCfg, committer->ctx->now);
H
Hongze Cheng 已提交
311 312
  tsdbFidKeyRange(committer->ctx->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
                  &committer->ctx->maxKey);
H
Hongze Cheng 已提交
313 314
  code = tfsAllocDisk(committer->tsdb->pVnode->pTfs, committer->ctx->expLevel, &committer->ctx->did);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
315 316 317 318 319
  STFileSet fset = {.fid = committer->ctx->fid};
  committer->ctx->fset = &fset;
  committer->ctx->fset = TARRAY2_SEARCH_EX(committer->fsetArr, &committer->ctx->fset, tsdbTFileSetCmprFn, TD_EQ);
  committer->ctx->tbid->suid = 0;
  committer->ctx->tbid->uid = 0;
H
Hongze Cheng 已提交
320

321 322
  ASSERT(TARRAY2_SIZE(committer->dataIterArray) == 0);
  ASSERT(committer->dataIterMerger == NULL);
H
Hongze Cheng 已提交
323
  ASSERT(committer->writer == NULL);
H
Hongze Cheng 已提交
324

325 326 327 328 329 330
  code = tsdbCommitOpenReader(committer);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbCommitOpenIter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
331 332
  code = tsdbCommitOpenWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
333

H
Hongze Cheng 已提交
334 335 336
  // reset nextKey
  committer->ctx->nextKey = TSKEY_MAX;

H
Hongze Cheng 已提交
337 338
_exit:
  if (code) {
H
Hongze Cheng 已提交
339
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
340
  } else {
H
Hongze Cheng 已提交
341 342
    tsdbDebug("vgId:%d %s done, fid:%d minKey:%" PRId64 " maxKey:%" PRId64 " expLevel:%d", TD_VID(tsdb->pVnode),
              __func__, committer->ctx->fid, committer->ctx->minKey, committer->ctx->maxKey, committer->ctx->expLevel);
H
Hongze Cheng 已提交
343
  }
H
Hongze Cheng 已提交
344
  return 0;
H
Hongze Cheng 已提交
345 346
}

H
Hongze Cheng 已提交
347
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
H
Hongze Cheng 已提交
348
  int32_t code = 0;
H
Hongze Cheng 已提交
349
  int32_t lino = 0;
H
Hongze Cheng 已提交
350

351 352
  code = tsdbCommitCloseWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
353

354
  code = tsdbCommitCloseIter(committer);
H
Hongze Cheng 已提交
355
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
356

357 358
  code = tsdbCommitCloseReader(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
359

H
Hongze Cheng 已提交
360 361
_exit:
  if (code) {
H
Hongze Cheng 已提交
362
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
363
  } else {
H
Hongze Cheng 已提交
364
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
365 366 367 368
  }
  return code;
}

H
Hongze Cheng 已提交
369
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
H
Hongze Cheng 已提交
370 371 372
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
373
  // fset commit start
H
Hongze Cheng 已提交
374
  code = tsdbCommitFileSetBegin(committer);
H
Hongze Cheng 已提交
375
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
376

H
Hongze Cheng 已提交
377
  // commit fset
H
Hongze Cheng 已提交
378
  code = tsdbCommitTSData(committer);
H
Hongze Cheng 已提交
379 380
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
381
  code = tsdbCommitTombData(committer);
H
Hongze Cheng 已提交
382 383 384
  TSDB_CHECK_CODE(code, lino, _exit);

  // fset commit end
H
Hongze Cheng 已提交
385
  code = tsdbCommitFileSetEnd(committer);
H
Hongze Cheng 已提交
386
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
387 388 389

_exit:
  if (code) {
H
Hongze Cheng 已提交
390
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
391
  } else {
H
Hongze Cheng 已提交
392
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
393 394 395 396
  }
  return code;
}

H
Hongze Cheng 已提交
397
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
H
Hongze Cheng 已提交
398
  int32_t code = 0;
H
Hongze Cheng 已提交
399
  int32_t lino = 0;
H
Hongze Cheng 已提交
400

H
Hongze Cheng 已提交
401
  memset(committer, 0, sizeof(committer[0]));
H
Hongze Cheng 已提交
402

H
Hongze Cheng 已提交
403
  committer->tsdb = tsdb;
H
Hongze Cheng 已提交
404 405
  code = tsdbFSCreateCopySnapshot(tsdb->pFS, &committer->fsetArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
406 407 408 409 410 411
  committer->minutes = tsdb->keepCfg.days;
  committer->precision = tsdb->keepCfg.precision;
  committer->minRow = info->info.config.tsdbCfg.minRows;
  committer->maxRow = info->info.config.tsdbCfg.maxRows;
  committer->cmprAlg = info->info.config.tsdbCfg.compression;
  committer->sttTrigger = info->info.config.sttTrigger;
H
Hongze Cheng 已提交
412 413 414
  committer->szPage = info->info.config.tsdbPageSize;
  committer->compactVersion = INT64_MAX;
  committer->ctx->cid = tsdbFSAllocEid(tsdb->pFS);
H
Hongze Cheng 已提交
415
  committer->ctx->now = taosGetTimestampSec();
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417
  committer->ctx->nextKey = tsdb->imem->minKey;
H
Hongze Cheng 已提交
418 419 420
  if (tsdb->imem->nDel > 0) {
    SRBTreeIter iter[1] = {tRBTreeIterCreate(tsdb->imem->tbDataTree, 1)};

H
Hongze Cheng 已提交
421 422 423 424 425 426 427 428 429 430 431
    for (SRBTreeNode *node = tRBTreeIterNext(iter); node; node = tRBTreeIterNext(iter)) {
      STbData *tbData = TCONTAINER_OF(node, STbData, rbtn);

      for (SDelData *delData = tbData->pHead; delData; delData = delData->pNext) {
        if (delData->sKey < committer->ctx->nextKey) {
          committer->ctx->nextKey = delData->sKey;
        }
      }
    }
  }

H
Hongze Cheng 已提交
432 433
_exit:
  if (code) {
H
Hongze Cheng 已提交
434
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
435
  } else {
H
Hongze Cheng 已提交
436
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
437 438 439 440
  }
  return code;
}

H
Hongze Cheng 已提交
441
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
H
Hongze Cheng 已提交
442
  int32_t code = 0;
H
Hongze Cheng 已提交
443
  int32_t lino = 0;
H
Hongze Cheng 已提交
444

H
Hongze Cheng 已提交
445
  if (eno == 0) {
H
Hongze Cheng 已提交
446
    code = tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
447
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
448
  } else {
H
Hongze Cheng 已提交
449 450
    // TODO
    ASSERT(0);
H
Hongze Cheng 已提交
451
  }
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453
  ASSERT(committer->writer == NULL);
454 455 456 457
  ASSERT(committer->dataIterMerger == NULL);
  ASSERT(committer->tombIterMerger == NULL);
  TARRAY2_DESTROY(committer->dataIterArray, NULL);
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
H
Hongze Cheng 已提交
458
  TARRAY2_DESTROY(committer->fopArray, NULL);
H
Hongze Cheng 已提交
459
  tsdbFSDestroyCopySnapshot(&committer->fsetArr);
H
Hongze Cheng 已提交
460

H
Hongze Cheng 已提交
461
_exit:
H
Hongze Cheng 已提交
462
  if (code) {
H
Hongze Cheng 已提交
463 464
    tsdbError("vgId:%d %s failed at line %d since %s, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, lino,
              tstrerror(code), committer->ctx->cid);
H
Hongze Cheng 已提交
465
  } else {
H
Hongze Cheng 已提交
466
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->cid);
H
Hongze Cheng 已提交
467
  }
H
Hongze Cheng 已提交
468 469 470
  return code;
}

H
Hongze Cheng 已提交
471 472 473 474 475 476
int32_t tsdbPreCommit(STsdb *tsdb) {
  taosThreadRwlockWrlock(&tsdb->rwLock);
  ASSERT(tsdb->imem == NULL);
  tsdb->imem = tsdb->mem;
  tsdb->mem = NULL;
  taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
477 478 479
  return 0;
}

H
Hongze Cheng 已提交
480 481
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
  if (!tsdb) return 0;
H
Hongze Cheng 已提交
482

H
Hongze Cheng 已提交
483 484 485
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
486 487 488
  SMemTable *imem = tsdb->imem;
  int64_t    nRow = imem->nRow;
  int64_t    nDel = imem->nDel;
H
Hongze Cheng 已提交
489

H
Hongze Cheng 已提交
490
  if (nRow == 0 && nDel == 0) {
H
Hongze Cheng 已提交
491 492 493
    taosThreadRwlockWrlock(&tsdb->rwLock);
    tsdb->imem = NULL;
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
494
    tsdbUnrefMemTable(imem, NULL, true);
H
Hongze Cheng 已提交
495
  } else {
H
Hongze Cheng 已提交
496
    SCommitter2 committer[1];
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498
    code = tsdbOpenCommitter(tsdb, info, committer);
H
Hongze Cheng 已提交
499
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
500

H
Hongze Cheng 已提交
501 502
    while (committer->ctx->nextKey != TSKEY_MAX) {
      code = tsdbCommitFileSet(committer);
H
Hongze Cheng 已提交
503
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
504
    }
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
    code = tsdbCloseCommitter(committer, code);
H
Hongze Cheng 已提交
507 508
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
509 510 511

_exit:
  if (code) {
H
Hongze Cheng 已提交
512
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
513
  } else {
H
Hongze Cheng 已提交
514
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, TD_VID(tsdb->pVnode), __func__, nRow, nDel);
H
Hongze Cheng 已提交
515 516 517 518
  }
  return code;
}

H
Hongze Cheng 已提交
519
int32_t tsdbCommitCommit(STsdb *tsdb) {
H
Hongze Cheng 已提交
520 521
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
522

H
Hongze Cheng 已提交
523
  if (tsdb->imem == NULL) goto _exit;
H
Hongze Cheng 已提交
524

H
Hongze Cheng 已提交
525 526 527
  SMemTable *pMemTable = tsdb->imem;
  taosThreadRwlockWrlock(&tsdb->rwLock);
  code = tsdbFSEditCommit(tsdb->pFS);
H
Hongze Cheng 已提交
528
  if (code) {
H
Hongze Cheng 已提交
529
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
530 531
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
532 533
  tsdb->imem = NULL;
  taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
534
  tsdbUnrefMemTable(pMemTable, NULL, true);
H
Hongze Cheng 已提交
535 536 537

_exit:
  if (code) {
H
Hongze Cheng 已提交
538
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
539
  } else {
H
Hongze Cheng 已提交
540
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
541 542 543 544 545 546 547
  }
  return code;
}

int32_t tsdbCommitAbort(STsdb *pTsdb) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
548 549

  if (pTsdb->imem == NULL) goto _exit;
H
Hongze Cheng 已提交
550

H
Hongze Cheng 已提交
551
  code = tsdbFSEditAbort(pTsdb->pFS);
H
Hongze Cheng 已提交
552 553 554 555
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
556
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
557
  } else {
H
Hongze Cheng 已提交
558
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
559 560 561
  }
  return code;
}