tsdbCommit.c 14.0 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 "inc/tsdbCommit.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
// extern dependencies
H
Hongze Cheng 已提交
19
typedef struct {
H
Hongze Cheng 已提交
20 21 22
  STsdb         *tsdb;
  TFileSetArray *fsetArr;

H
Hongze Cheng 已提交
23 24 25 26 27
  int32_t minutes;
  int8_t  precision;
  int32_t minRow;
  int32_t maxRow;
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
28 29
  int32_t sttTrigger;
  int32_t szPage;
H
Hongze Cheng 已提交
30 31 32
  int64_t compactVersion;

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

  TFileOpArray   fopArray[1];
  TTsdbIterArray iterArray[1];
  SIterMerger   *iterMerger;
H
Hongze Cheng 已提交
47

H
Hongze Cheng 已提交
48
  // writer
H
Hongze Cheng 已提交
49
  SSttFileWriter  *sttWriter;
H
Hongze Cheng 已提交
50
  SDataFileWriter *dataWriter;
H
Hongze Cheng 已提交
51
} SCommitter2;
H
Hongze Cheng 已提交
52

H
Hongze Cheng 已提交
53
static int32_t tsdbCommitOpenNewSttWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
54
  int32_t code = 0;
H
Hongze Cheng 已提交
55
  int32_t lino = 0;
H
Hongze Cheng 已提交
56

H
Hongze Cheng 已提交
57 58
  SDiskID did[1];
  if (tfsAllocDisk(committer->tsdb->pVnode->pTfs, committer->ctx->expLevel, did) < 0) {
H
Hongze Cheng 已提交
59 60 61 62
    code = TSDB_CODE_FS_NO_VALID_DISK;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  SSttFileWriterConfig config[1] = {{
      .tsdb = committer->tsdb,
      .maxRow = committer->maxRow,
      .szPage = committer->tsdb->pVnode->config.tsdbPageSize,
      .cmprAlg = committer->cmprAlg,
      .compactVersion = committer->compactVersion,
      .file =
          {
              .type = TSDB_FTYPE_STT,
              .did = did[0],
              .fid = committer->ctx->fid,
              .cid = committer->ctx->cid,
          },
  }};

  code = tsdbSttFileWriterOpen(config, &committer->sttWriter);
H
Hongze Cheng 已提交
79 80 81 82
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
83
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
84
  } else {
H
Hongze Cheng 已提交
85
    tsdbDebug("vgId:%d %s success", TD_VID(committer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
86 87 88
  }
  return code;
}
H
Hongze Cheng 已提交
89 90

static int32_t tsdbCommitOpenExistSttWriter(SCommitter2 *committer, const STFile *f) {
H
Hongze Cheng 已提交
91 92
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
93 94 95 96 97 98 99 100 101 102 103

  SSttFileWriterConfig config[1] = {{
      .tsdb = committer->tsdb,
      .maxRow = committer->maxRow,
      .szPage = committer->szPage,
      .cmprAlg = committer->cmprAlg,
      .compactVersion = committer->compactVersion,
      .file = f[0],
  }};

  code = tsdbSttFileWriterOpen(config, &committer->sttWriter);
H
Hongze Cheng 已提交
104 105 106 107
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
108
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
109
  } else {
H
Hongze Cheng 已提交
110
    tsdbDebug("vgId:%d %s success", TD_VID(committer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
111
  }
H
Hongze Cheng 已提交
112 113
  return code;
}
H
Hongze Cheng 已提交
114

H
Hongze Cheng 已提交
115
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
116 117 118 119
  int32_t code = 0;
  int32_t lino = 0;

  // stt writer
H
Hongze Cheng 已提交
120
  if (!committer->ctx->fset) {
H
Hongze Cheng 已提交
121
    return tsdbCommitOpenNewSttWriter(committer);
H
Hongze Cheng 已提交
122 123
  }

H
Hongze Cheng 已提交
124 125
  const SSttLvl *lvl0 = tsdbTFileSetGetSttLvl(committer->ctx->fset, 0);
  if (lvl0 == NULL || TARRAY2_SIZE(lvl0->fobjArr) == 0) {
H
Hongze Cheng 已提交
126
    return tsdbCommitOpenNewSttWriter(committer);
H
Hongze Cheng 已提交
127 128
  }

H
Hongze Cheng 已提交
129
  STFileObj *fobj = TARRAY2_LAST(lvl0->fobjArr);
H
Hongze Cheng 已提交
130
  if (fobj->f->stt->nseg >= committer->sttTrigger) {
H
Hongze Cheng 已提交
131
    return tsdbCommitOpenNewSttWriter(committer);
H
Hongze Cheng 已提交
132
  } else {
H
Hongze Cheng 已提交
133
    return tsdbCommitOpenExistSttWriter(committer, fobj->f);
H
Hongze Cheng 已提交
134
  }
H
Hongze Cheng 已提交
135

H
Hongze Cheng 已提交
136 137 138 139 140 141
  // data writer
  if (0) {
    // TODO
  }

  return code;
H
Hongze Cheng 已提交
142 143
}

H
Hongze Cheng 已提交
144
static int32_t tsdbCommitWriteDelData(SCommitter2 *committer, int64_t suid, int64_t uid, int64_t version, int64_t sKey,
H
Hongze Cheng 已提交
145 146 147 148 149 150
                                      int64_t eKey) {
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
  int32_t   code = 0;
  int32_t   lino = 0;
  int64_t   nRow = 0;
  int32_t   vid = TD_VID(committer->tsdb->pVnode);
  SRowInfo *row;

  if (committer->tsdb->imem->nRow == 0) goto _exit;

  // open iter and iter merger
  STsdbIter      *iter;
  STsdbIterConfig config[1] = {{
      .type = TSDB_ITER_TYPE_MEMT,
      .memt = committer->tsdb->imem,
      .from = {{
          .ts = committer->ctx->minKey,
          .version = VERSION_MIN,
      }},
  }};

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

H
Hongze Cheng 已提交
174 175
  code = TARRAY2_APPEND(committer->iterArray, iter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
176

H
Hongze Cheng 已提交
177
  code = tsdbIterMergerOpen(committer->iterArray, &committer->iterMerger);
H
Hongze Cheng 已提交
178
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
179

H
Hongze Cheng 已提交
180 181 182 183 184 185
  // loop iter
  while ((row = tsdbIterMergerGet(committer->iterMerger)) != NULL) {
    if (row->uid != committer->ctx->tbid->uid) {
      committer->ctx->tbid->suid = row->suid;
      committer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
186
      // Ignore table of obsolescence
H
Hongze Cheng 已提交
187 188 189 190 191
      SMetaInfo info[1];
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, info, NULL) != 0) {
        code = tsdbIterMergerSkipTableData(committer->iterMerger, committer->ctx->tbid);
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
H
Hongze Cheng 已提交
192
      }
H
Hongze Cheng 已提交
193
    }
H
Hongze Cheng 已提交
194

H
Hongze Cheng 已提交
195 196 197 198 199 200
    TSKEY ts = TSDBROW_TS(&row->row);
    if (ts > committer->ctx->maxKey) {
      committer->ctx->nextKey = TMIN(committer->ctx->nextKey, ts);
      code = tsdbIterMergerSkipTableData(committer->iterMerger, committer->ctx->tbid);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
H
Hongze Cheng 已提交
201
      code = tsdbSttFileWriteTSData(committer->sttWriter, row);
H
Hongze Cheng 已提交
202
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
203

H
Hongze Cheng 已提交
204 205
      code = tsdbIterMergerNext(committer->iterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
206 207 208 209 210
    }
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
211
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
212
  } else {
H
Hongze Cheng 已提交
213
    tsdbDebug("vgId:%d %s done, fid:%d nRow:%" PRId64, vid, __func__, committer->ctx->fid, nRow);
H
Hongze Cheng 已提交
214 215 216 217
  }
  return code;
}

H
Hongze Cheng 已提交
218
static int32_t tsdbCommitDelData(SCommitter2 *committer) {
H
Hongze Cheng 已提交
219
  int32_t code = 0;
H
Hongze Cheng 已提交
220
  int32_t lino = 0;
H
Hongze Cheng 已提交
221

H
Hongze Cheng 已提交
222
  SMemTable *mem = committer->tsdb->imem;
H
Hongze Cheng 已提交
223

H
Hongze Cheng 已提交
224
  if (mem->nDel == 0) goto _exit;
H
Hongze Cheng 已提交
225

H
Hongze Cheng 已提交
226
  SRBTreeIter iter[1] = {tRBTreeIterCreate(committer->tsdb->imem->tbDataTree, 1)};
H
Hongze Cheng 已提交
227

H
Hongze Cheng 已提交
228 229 230 231 232 233
  for (SRBTreeNode *node = tRBTreeIterNext(iter); node; node = tRBTreeIterNext(iter)) {
    STbData   *tbData = TCONTAINER_OF(node, STbData, rbtn);
    SDelRecord record[1] = {{
        .suid = tbData->suid,
        .uid = tbData->uid,
    }};
H
Hongze Cheng 已提交
234

H
Hongze Cheng 已提交
235 236 237 238
    for (SDelData *delData = tbData->pHead; delData; delData = delData->pNext) {
      if (delData->eKey < committer->ctx->minKey) continue;
      if (delData->sKey > committer->ctx->maxKey) {
        committer->ctx->nextKey = TMIN(committer->ctx->nextKey, delData->sKey);
H
Hongze Cheng 已提交
239 240
        continue;
      }
H
Hongze Cheng 已提交
241

H
Hongze Cheng 已提交
242 243 244 245 246 247 248 249 250 251
      record->version = delData->version;
      record->skey = TMAX(delData->sKey, committer->ctx->minKey);
      if (delData->eKey > committer->ctx->maxKey) {
        committer->ctx->nextKey = TMIN(committer->ctx->nextKey, committer->ctx->maxKey + 1);
        record->ekey = committer->ctx->maxKey;
      } else {
        record->ekey = delData->eKey;
      }

      code = tsdbSttFileWriteDelRecord(committer->sttWriter, record); // TODO
H
Hongze Cheng 已提交
252 253 254 255 256 257
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
258
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
259 260 261 262
  }
  return code;
}

H
Hongze Cheng 已提交
263 264 265 266 267 268 269
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
  int32_t code = 0;
  int32_t lino = 0;
  STsdb  *tsdb = committer->tsdb;
  int32_t vid = TD_VID(tsdb->pVnode);

  committer->ctx->fid = tsdbKeyFid(committer->ctx->nextKey, committer->minutes, committer->precision);
H
Hongze Cheng 已提交
270
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->fid, &tsdb->keepCfg, committer->ctx->now);
H
Hongze Cheng 已提交
271 272
  tsdbFidKeyRange(committer->ctx->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
                  &committer->ctx->maxKey);
H
Hongze Cheng 已提交
273 274 275 276 277
  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 已提交
278

H
Hongze Cheng 已提交
279 280 281 282
  ASSERT(TARRAY2_SIZE(committer->iterArray) == 0);
  ASSERT(committer->iterMerger == NULL);
  ASSERT(committer->sttWriter == NULL);
  ASSERT(committer->dataWriter == NULL);
H
Hongze Cheng 已提交
283

H
Hongze Cheng 已提交
284 285
  code = tsdbCommitOpenWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
286

H
Hongze Cheng 已提交
287 288 289
  // reset nextKey
  committer->ctx->nextKey = TSKEY_MAX;

H
Hongze Cheng 已提交
290 291 292 293 294 295 296
_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
  } else {
    tsdbDebug("vgId:%d %s done, fid:%d minKey:%" PRId64 " maxKey:%" PRId64 " expLevel:%d", vid, __func__,
              committer->ctx->fid, committer->ctx->minKey, committer->ctx->maxKey, committer->ctx->expLevel);
  }
H
Hongze Cheng 已提交
297
  return 0;
H
Hongze Cheng 已提交
298 299
}

H
Hongze Cheng 已提交
300
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
H
Hongze Cheng 已提交
301
  int32_t code = 0;
H
Hongze Cheng 已提交
302
  int32_t lino = 0;
H
Hongze Cheng 已提交
303

H
Hongze Cheng 已提交
304
  code = tsdbSttFileWriterClose(&committer->sttWriter, 0, committer->fopArray);
H
Hongze Cheng 已提交
305
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
306

H
Hongze Cheng 已提交
307 308 309
  tsdbIterMergerClose(&committer->iterMerger);
  TARRAY2_CLEAR(committer->iterArray, tsdbIterClose);

H
Hongze Cheng 已提交
310 311
_exit:
  if (code) {
H
Hongze Cheng 已提交
312
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
313
  } else {
H
Hongze Cheng 已提交
314
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
315 316 317 318
  }
  return code;
}

H
Hongze Cheng 已提交
319
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
H
Hongze Cheng 已提交
320 321
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
322
  int32_t vid = TD_VID(committer->tsdb->pVnode);
H
Hongze Cheng 已提交
323

H
Hongze Cheng 已提交
324
  // fset commit start
H
Hongze Cheng 已提交
325
  code = tsdbCommitFileSetBegin(committer);
H
Hongze Cheng 已提交
326
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
327

H
Hongze Cheng 已提交
328
  // commit fset
H
Hongze Cheng 已提交
329
  code = tsdbCommitTSData(committer);
H
Hongze Cheng 已提交
330 331
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
332
  code = tsdbCommitDelData(committer);
H
Hongze Cheng 已提交
333 334 335
  TSDB_CHECK_CODE(code, lino, _exit);

  // fset commit end
H
Hongze Cheng 已提交
336
  code = tsdbCommitFileSetEnd(committer);
H
Hongze Cheng 已提交
337
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
338 339 340

_exit:
  if (code) {
H
Hongze Cheng 已提交
341
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
342
  } else {
H
Hongze Cheng 已提交
343
    tsdbDebug("vgId:%d %s done, fid:%d", vid, __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
344 345 346 347
  }
  return code;
}

H
Hongze Cheng 已提交
348
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
H
Hongze Cheng 已提交
349
  int32_t code = 0;
H
Hongze Cheng 已提交
350 351
  int32_t lino = 0;
  int32_t vid = TD_VID(tsdb->pVnode);
H
Hongze Cheng 已提交
352

H
Hongze Cheng 已提交
353 354 355
  memset(committer, 0, sizeof(committer[0]));

  committer->tsdb = tsdb;
H
Hongze Cheng 已提交
356 357 358
  code = tsdbFSCreateCopySnapshot(tsdb->pFS, &committer->fsetArr);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
359 360 361 362 363 364
  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 已提交
365 366
  committer->szPage = info->info.config.tsdbPageSize;
  committer->compactVersion = INT64_MAX;
H
Hongze Cheng 已提交
367

H
Hongze Cheng 已提交
368
  committer->ctx->cid = tsdbFSAllocEid(tsdb->pFS);
H
Hongze Cheng 已提交
369 370
  committer->ctx->now = taosGetTimestampSec();
  committer->ctx->nextKey = tsdb->imem->minKey;  // TODO
H
Hongze Cheng 已提交
371

H
Hongze Cheng 已提交
372 373
  TARRAY2_INIT(committer->fopArray);

H
Hongze Cheng 已提交
374 375
_exit:
  if (code) {
H
Hongze Cheng 已提交
376
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
377
  } else {
H
Hongze Cheng 已提交
378
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
379 380 381 382
  }
  return code;
}

H
Hongze Cheng 已提交
383
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
H
Hongze Cheng 已提交
384
  int32_t code = 0;
H
Hongze Cheng 已提交
385
  int32_t lino = 0;
H
Hongze Cheng 已提交
386
  int32_t vid = TD_VID(committer->tsdb->pVnode);
H
Hongze Cheng 已提交
387

H
Hongze Cheng 已提交
388
  if (eno == 0) {
H
Hongze Cheng 已提交
389
    code = tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
390
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
391
  } else {
H
Hongze Cheng 已提交
392 393
    // TODO
    ASSERT(0);
H
Hongze Cheng 已提交
394
  }
H
Hongze Cheng 已提交
395

H
Hongze Cheng 已提交
396 397 398 399 400 401
  ASSERT(committer->dataWriter == NULL);
  ASSERT(committer->sttWriter == NULL);
  ASSERT(committer->iterMerger == NULL);
  TARRAY2_FREE(committer->iterArray);
  TARRAY2_FREE(committer->fopArray);
  tsdbFSDestroyCopySnapshot(&committer->fsetArr);
H
Hongze Cheng 已提交
402

H
Hongze Cheng 已提交
403
_exit:
H
Hongze Cheng 已提交
404
  if (code) {
H
Hongze Cheng 已提交
405
    tsdbError("vgId:%d %s failed at line %d since %s, eid:%" PRId64, vid, __func__, lino, tstrerror(code),
H
Hongze Cheng 已提交
406
              committer->ctx->cid);
H
Hongze Cheng 已提交
407
  } else {
H
Hongze Cheng 已提交
408
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, vid, __func__, committer->ctx->cid);
H
Hongze Cheng 已提交
409
  }
H
Hongze Cheng 已提交
410 411 412
  return code;
}

H
Hongze Cheng 已提交
413 414 415 416 417 418
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 已提交
419 420 421
  return 0;
}

H
Hongze Cheng 已提交
422 423
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
  if (!tsdb) return 0;
H
Hongze Cheng 已提交
424 425 426

  int32_t    code = 0;
  int32_t    lino = 0;
H
Hongze Cheng 已提交
427
  int32_t    vid = TD_VID(tsdb->pVnode);
H
Hongze Cheng 已提交
428 429 430
  SMemTable *imem = tsdb->imem;
  int64_t    nRow = imem->nRow;
  int64_t    nDel = imem->nDel;
H
Hongze Cheng 已提交
431 432 433 434 435

  if (!nRow && !nDel) {
    taosThreadRwlockWrlock(&tsdb->rwLock);
    tsdb->imem = NULL;
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
436
    tsdbUnrefMemTable(imem, NULL, true);
H
Hongze Cheng 已提交
437
  } else {
H
Hongze Cheng 已提交
438
    SCommitter2 committer[1];
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440
    code = tsdbOpenCommitter(tsdb, info, committer);
H
Hongze Cheng 已提交
441
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
442

H
Hongze Cheng 已提交
443 444
    while (committer->ctx->nextKey != TSKEY_MAX) {
      code = tsdbCommitFileSet(committer);
H
Hongze Cheng 已提交
445
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
446
    }
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448
    code = tsdbCloseCommitter(committer, code);
H
Hongze Cheng 已提交
449 450
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
451 452 453

_exit:
  if (code) {
H
Hongze Cheng 已提交
454
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
455
  } else {
H
Hongze Cheng 已提交
456
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, vid, __func__, nRow, nDel);
H
Hongze Cheng 已提交
457 458 459 460
  }
  return code;
}

H
Hongze Cheng 已提交
461
int32_t tsdbCommitCommit(STsdb *tsdb) {
H
Hongze Cheng 已提交
462 463
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
464

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

H
Hongze Cheng 已提交
467 468 469
  SMemTable *pMemTable = tsdb->imem;
  taosThreadRwlockWrlock(&tsdb->rwLock);
  code = tsdbFSEditCommit(tsdb->pFS);
H
Hongze Cheng 已提交
470
  if (code) {
H
Hongze Cheng 已提交
471
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
472 473
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
474 475
  tsdb->imem = NULL;
  taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
476
  tsdbUnrefMemTable(pMemTable, NULL, true);
H
Hongze Cheng 已提交
477 478 479

_exit:
  if (code) {
H
Hongze Cheng 已提交
480
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
481
  } else {
H
Hongze Cheng 已提交
482
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
483 484 485 486 487 488 489
  }
  return code;
}

int32_t tsdbCommitAbort(STsdb *pTsdb) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
490 491 492
  int32_t vid = TD_VID(pTsdb->pVnode);

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

H
Hongze Cheng 已提交
494
  code = tsdbFSEditAbort(pTsdb->pFS);
H
Hongze Cheng 已提交
495 496 497 498
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
499
    tsdbError("vgId:%d, %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
500
  } else {
H
Hongze Cheng 已提交
501
    tsdbInfo("vgId:%d %s done", vid, __func__);
H
Hongze Cheng 已提交
502 503 504
  }
  return code;
}