tsdbCommit.c 14.7 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, false);
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 225 226 227
  if (mem->nDel == 0                        //
      || (committer->ctx->fset == NULL      //
          && committer->sttWriter == NULL)  //
  ) {
H
Hongze Cheng 已提交
228
    committer->ctx->nextKey = committer->ctx->maxKey + 1;
H
Hongze Cheng 已提交
229 230
    goto _exit;
  }
H
Hongze Cheng 已提交
231

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

H
Hongze Cheng 已提交
234
  for (SRBTreeNode *node = tRBTreeIterNext(iter); node; node = tRBTreeIterNext(iter)) {
H
Hongze Cheng 已提交
235 236
    STbData    *tbData = TCONTAINER_OF(node, STbData, rbtn);
    STombRecord record[1] = {{
H
Hongze Cheng 已提交
237 238 239
        .suid = tbData->suid,
        .uid = tbData->uid,
    }};
H
Hongze Cheng 已提交
240

H
Hongze Cheng 已提交
241 242 243 244
    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 已提交
245 246
        continue;
      }
H
Hongze Cheng 已提交
247

H
Hongze Cheng 已提交
248 249 250 251 252 253 254 255 256
      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;
      }

H
Hongze Cheng 已提交
257 258 259 260 261
      if (!committer->sttWriter) {
        code = tsdbCommitOpenWriter(committer);
        TSDB_CHECK_CODE(code, lino, _exit);
      }

H
Hongze Cheng 已提交
262
      code = tsdbSttFileWriteTombRecord(committer->sttWriter, record);
H
Hongze Cheng 已提交
263 264 265 266 267 268
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
269
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
270 271 272 273
  }
  return code;
}

H
Hongze Cheng 已提交
274 275 276 277 278 279 280
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 已提交
281
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->fid, &tsdb->keepCfg, committer->ctx->now);
H
Hongze Cheng 已提交
282 283
  tsdbFidKeyRange(committer->ctx->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
                  &committer->ctx->maxKey);
H
Hongze Cheng 已提交
284 285 286 287 288
  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 已提交
289

H
Hongze Cheng 已提交
290 291 292 293
  ASSERT(TARRAY2_SIZE(committer->iterArray) == 0);
  ASSERT(committer->iterMerger == NULL);
  ASSERT(committer->sttWriter == NULL);
  ASSERT(committer->dataWriter == NULL);
H
Hongze Cheng 已提交
294

H
Hongze Cheng 已提交
295 296
  code = tsdbCommitOpenWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
297

H
Hongze Cheng 已提交
298 299 300
  // reset nextKey
  committer->ctx->nextKey = TSKEY_MAX;

H
Hongze Cheng 已提交
301 302 303 304 305 306 307
_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 已提交
308
  return 0;
H
Hongze Cheng 已提交
309 310
}

H
Hongze Cheng 已提交
311
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
H
Hongze Cheng 已提交
312
  int32_t code = 0;
H
Hongze Cheng 已提交
313
  int32_t lino = 0;
H
Hongze Cheng 已提交
314

H
Hongze Cheng 已提交
315
  code = tsdbSttFileWriterClose(&committer->sttWriter, 0, committer->fopArray);
H
Hongze Cheng 已提交
316
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
317

H
Hongze Cheng 已提交
318 319 320
  tsdbIterMergerClose(&committer->iterMerger);
  TARRAY2_CLEAR(committer->iterArray, tsdbIterClose);

H
Hongze Cheng 已提交
321 322
_exit:
  if (code) {
H
Hongze Cheng 已提交
323
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
324
  } else {
H
Hongze Cheng 已提交
325
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
326 327 328 329
  }
  return code;
}

H
Hongze Cheng 已提交
330
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
H
Hongze Cheng 已提交
331 332
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
333
  int32_t vid = TD_VID(committer->tsdb->pVnode);
H
Hongze Cheng 已提交
334

H
Hongze Cheng 已提交
335
  // fset commit start
H
Hongze Cheng 已提交
336
  code = tsdbCommitFileSetBegin(committer);
H
Hongze Cheng 已提交
337
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
338

H
Hongze Cheng 已提交
339
  // commit fset
H
Hongze Cheng 已提交
340
  code = tsdbCommitTSData(committer);
H
Hongze Cheng 已提交
341 342
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
343
  code = tsdbCommitDelData(committer);
H
Hongze Cheng 已提交
344 345 346
  TSDB_CHECK_CODE(code, lino, _exit);

  // fset commit end
H
Hongze Cheng 已提交
347
  code = tsdbCommitFileSetEnd(committer);
H
Hongze Cheng 已提交
348
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
349 350 351

_exit:
  if (code) {
H
Hongze Cheng 已提交
352
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
353
  } else {
H
Hongze Cheng 已提交
354
    tsdbDebug("vgId:%d %s done, fid:%d", vid, __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
355 356 357 358
  }
  return code;
}

H
Hongze Cheng 已提交
359
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
H
Hongze Cheng 已提交
360
  int32_t code = 0;
H
Hongze Cheng 已提交
361
  int32_t lino = 0;
H
Hongze Cheng 已提交
362

H
Hongze Cheng 已提交
363
  SMemTable *mem = tsdb->imem;
H
Hongze Cheng 已提交
364

H
Hongze Cheng 已提交
365
  memset(committer, 0, sizeof(committer[0]));
H
Hongze Cheng 已提交
366
  committer->tsdb = tsdb;
H
Hongze Cheng 已提交
367 368
  code = tsdbFSCreateCopySnapshot(tsdb->pFS, &committer->fsetArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
369 370 371 372 373 374
  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 已提交
375 376 377
  committer->szPage = info->info.config.tsdbPageSize;
  committer->compactVersion = INT64_MAX;
  committer->ctx->cid = tsdbFSAllocEid(tsdb->pFS);
H
Hongze Cheng 已提交
378
  committer->ctx->now = taosGetTimestampSec();
H
Hongze Cheng 已提交
379 380
  TARRAY2_INIT(committer->fopArray);

H
Hongze Cheng 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394
  committer->ctx->nextKey = tsdb->imem->minKey;
  if (mem->nDel > 0) {
    SRBTreeIter iter[1] = {tRBTreeIterCreate(mem->tbDataTree, 1)};
    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 已提交
395 396
_exit:
  if (code) {
H
Hongze Cheng 已提交
397
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
398
  } else {
H
Hongze Cheng 已提交
399
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
400 401 402 403
  }
  return code;
}

H
Hongze Cheng 已提交
404
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
H
Hongze Cheng 已提交
405
  int32_t code = 0;
H
Hongze Cheng 已提交
406
  int32_t lino = 0;
H
Hongze Cheng 已提交
407
  int32_t vid = TD_VID(committer->tsdb->pVnode);
H
Hongze Cheng 已提交
408

H
Hongze Cheng 已提交
409
  if (eno == 0) {
H
Hongze Cheng 已提交
410
    code = tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
411
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
412
  } else {
H
Hongze Cheng 已提交
413 414
    // TODO
    ASSERT(0);
H
Hongze Cheng 已提交
415
  }
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417 418 419 420 421 422
  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 已提交
423

H
Hongze Cheng 已提交
424
_exit:
H
Hongze Cheng 已提交
425
  if (code) {
H
Hongze Cheng 已提交
426
    tsdbError("vgId:%d %s failed at line %d since %s, eid:%" PRId64, vid, __func__, lino, tstrerror(code),
H
Hongze Cheng 已提交
427
              committer->ctx->cid);
H
Hongze Cheng 已提交
428
  } else {
H
Hongze Cheng 已提交
429
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, vid, __func__, committer->ctx->cid);
H
Hongze Cheng 已提交
430
  }
H
Hongze Cheng 已提交
431 432 433
  return code;
}

H
Hongze Cheng 已提交
434 435 436 437 438 439
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 已提交
440 441 442
  return 0;
}

H
Hongze Cheng 已提交
443 444
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
  if (!tsdb) return 0;
H
Hongze Cheng 已提交
445 446 447

  int32_t    code = 0;
  int32_t    lino = 0;
H
Hongze Cheng 已提交
448
  int32_t    vid = TD_VID(tsdb->pVnode);
H
Hongze Cheng 已提交
449 450 451
  SMemTable *imem = tsdb->imem;
  int64_t    nRow = imem->nRow;
  int64_t    nDel = imem->nDel;
H
Hongze Cheng 已提交
452 453 454 455 456

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

H
Hongze Cheng 已提交
461
    code = tsdbOpenCommitter(tsdb, info, committer);
H
Hongze Cheng 已提交
462
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
463

H
Hongze Cheng 已提交
464 465
    while (committer->ctx->nextKey != TSKEY_MAX) {
      code = tsdbCommitFileSet(committer);
H
Hongze Cheng 已提交
466
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
467
    }
H
Hongze Cheng 已提交
468

H
Hongze Cheng 已提交
469
    code = tsdbCloseCommitter(committer, code);
H
Hongze Cheng 已提交
470 471
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
472 473 474

_exit:
  if (code) {
H
Hongze Cheng 已提交
475
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
476
  } else {
H
Hongze Cheng 已提交
477
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, vid, __func__, nRow, nDel);
H
Hongze Cheng 已提交
478 479 480 481
  }
  return code;
}

H
Hongze Cheng 已提交
482
int32_t tsdbCommitCommit(STsdb *tsdb) {
H
Hongze Cheng 已提交
483 484
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
485

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

H
Hongze Cheng 已提交
488 489 490
  SMemTable *pMemTable = tsdb->imem;
  taosThreadRwlockWrlock(&tsdb->rwLock);
  code = tsdbFSEditCommit(tsdb->pFS);
H
Hongze Cheng 已提交
491
  if (code) {
H
Hongze Cheng 已提交
492
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
493 494
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
495 496
  tsdb->imem = NULL;
  taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
497
  tsdbUnrefMemTable(pMemTable, NULL, true);
H
Hongze Cheng 已提交
498 499 500

_exit:
  if (code) {
H
Hongze Cheng 已提交
501
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
502
  } else {
H
Hongze Cheng 已提交
503
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
504 505 506 507 508 509 510
  }
  return code;
}

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

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

H
Hongze Cheng 已提交
515
  code = tsdbFSEditAbort(pTsdb->pFS);
H
Hongze Cheng 已提交
516 517 518 519
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
520
    tsdbError("vgId:%d, %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
521
  } else {
H
Hongze Cheng 已提交
522
    tsdbInfo("vgId:%d %s done", vid, __func__);
H
Hongze Cheng 已提交
523 524 525
  }
  return code;
}