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);
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 228 229
  if (mem->nDel == 0                        //
      || (committer->ctx->fset == NULL      //
          && committer->sttWriter == NULL)  //
  ) {
    goto _exit;
  }
H
Hongze Cheng 已提交
230

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

H
Hongze Cheng 已提交
233 234 235 236 237 238
  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 已提交
239

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

H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255
      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 已提交
256 257 258 259 260 261
      if (!committer->sttWriter) {
        code = tsdbCommitOpenWriter(committer);
        TSDB_CHECK_CODE(code, lino, _exit);
      }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393
  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 已提交
394 395
_exit:
  if (code) {
H
Hongze Cheng 已提交
396
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
397
  } else {
H
Hongze Cheng 已提交
398
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
399 400 401 402
  }
  return code;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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