tsdbCommit.c 23.8 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
  STsdb         *tsdb;
  TFileSetArray *fsetArr;
H
Hongze Cheng 已提交
22 23 24 25
  TFileOpArray   fopArray[1];

  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 42 43 44 45 46 47
    int64_t    now;
    TSKEY      nextKey;
    int32_t    fid;
    int32_t    expLevel;
    TSKEY      minKey;
    TSKEY      maxKey;
    STFileSet *fset;
    TABLEID    tbid[1];
  } ctx[1];

H
Hongze Cheng 已提交
48 49 50
  SSttFileReader *sttReader;
  TTsdbIterArray  iterArray[1];
  SIterMerger    *iterMerger;
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52
  // writer
H
Hongze Cheng 已提交
53 54
  SBlockData       blockData[2];
  int32_t          blockDataIdx;
H
Hongze Cheng 已提交
55
  SDataFileWriter *dataWriter;
H
Hongze Cheng 已提交
56
  SSttFileWriter  *sttWriter;
H
Hongze Cheng 已提交
57
} SCommitter2;
H
Hongze Cheng 已提交
58

H
Hongze Cheng 已提交
59
static int32_t tsdbCommitOpenNewSttWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
60
  int32_t code = 0;
H
Hongze Cheng 已提交
61
  int32_t lino = 0;
H
Hongze Cheng 已提交
62

H
Hongze Cheng 已提交
63 64
  SDiskID did[1];
  if (tfsAllocDisk(committer->tsdb->pVnode->pTfs, committer->ctx->expLevel, did) < 0) {
H
Hongze Cheng 已提交
65 66 67 68
    code = TSDB_CODE_FS_NO_VALID_DISK;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
69 70 71
  SSttFileWriterConfig config[1] = {{
      .tsdb = committer->tsdb,
      .maxRow = committer->maxRow,
H
Hongze Cheng 已提交
72
      .szPage = committer->szPage,
H
Hongze Cheng 已提交
73 74 75 76 77 78 79 80 81 82 83 84
      .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 已提交
85 86 87 88
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
89
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
90
  } else {
H
Hongze Cheng 已提交
91
    tsdbDebug("vgId:%d %s success", TD_VID(committer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
92 93 94
  }
  return code;
}
H
Hongze Cheng 已提交
95 96

static int32_t tsdbCommitOpenExistSttWriter(SCommitter2 *committer, const STFile *f) {
H
Hongze Cheng 已提交
97 98
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
99 100 101 102 103 104 105 106 107 108 109

  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 已提交
110 111 112 113
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
114
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
115
  } else {
H
Hongze Cheng 已提交
116
    tsdbDebug("vgId:%d %s success", TD_VID(committer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
117
  }
H
Hongze Cheng 已提交
118 119
  return code;
}
H
Hongze Cheng 已提交
120

H
Hongze Cheng 已提交
121
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
122 123 124
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
125 126 127 128 129 130 131 132 133 134
  // if (committer->sttTrigger == 1) {
  //   SDataFileWriterConfig config = {
  //       // TODO
  //   };

  //   code = tsdbDataFileWriterOpen(&config, &committer->dataWriter);
  //   TSDB_CHECK_CODE(code, lino, _exit);
  //   // TODO
  // }

H
Hongze Cheng 已提交
135
  // stt writer
H
Hongze Cheng 已提交
136
  if (!committer->ctx->fset) {
H
Hongze Cheng 已提交
137
    return tsdbCommitOpenNewSttWriter(committer);
H
Hongze Cheng 已提交
138 139
  }

H
Hongze Cheng 已提交
140 141
  const SSttLvl *lvl0 = tsdbTFileSetGetSttLvl(committer->ctx->fset, 0);
  if (lvl0 == NULL || TARRAY2_SIZE(lvl0->fobjArr) == 0) {
H
Hongze Cheng 已提交
142
    return tsdbCommitOpenNewSttWriter(committer);
H
Hongze Cheng 已提交
143 144
  }

H
Hongze Cheng 已提交
145
  STFileObj *fobj = TARRAY2_LAST(lvl0->fobjArr);
H
Hongze Cheng 已提交
146
  if (fobj->f->stt->nseg >= committer->sttTrigger) {
H
Hongze Cheng 已提交
147
    return tsdbCommitOpenNewSttWriter(committer);
H
Hongze Cheng 已提交
148
  } else {
H
Hongze Cheng 已提交
149
    return tsdbCommitOpenExistSttWriter(committer, fobj->f);
H
Hongze Cheng 已提交
150
  }
H
Hongze Cheng 已提交
151

H
Hongze Cheng 已提交
152 153 154
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
155 156
  }
  return code;
H
Hongze Cheng 已提交
157 158
}

H
Hongze Cheng 已提交
159
static int32_t tsdbCommitWriteDelData(SCommitter2 *committer, int64_t suid, int64_t uid, int64_t version, int64_t sKey,
H
Hongze Cheng 已提交
160 161 162 163 164 165
                                      int64_t eKey) {
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
166 167 168
static int32_t tsdbCommitTSDataOpenIterMerger(SCommitter2 *committer) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
169

H
Hongze Cheng 已提交
170 171
  ASSERT(TARRAY2_SIZE(committer->iterArray) == 0);
  ASSERT(committer->iterMerger == NULL);
H
Hongze Cheng 已提交
172 173

  STsdbIter      *iter;
H
Hongze Cheng 已提交
174 175 176 177 178 179 180
  STsdbIterConfig config[1];

  // memtable iter
  config->type = TSDB_ITER_TYPE_MEMT;
  config->memt = committer->tsdb->imem;
  config->from->ts = committer->ctx->minKey;
  config->from->version = VERSION_MIN;
H
Hongze Cheng 已提交
181 182 183

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

H
Hongze Cheng 已提交
185 186
  code = TARRAY2_APPEND(committer->iterArray, iter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
187

H
Hongze Cheng 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // stt file iter
  if (committer->sttReader) {
    const TSttSegReaderArray *readerArray;

    tsdbSttFileReaderGetSegReader(committer->sttReader, &readerArray);

    SSttSegReader *segReader;
    TARRAY2_FOREACH(readerArray, segReader) {
      config->type = TSDB_ITER_TYPE_STT;
      config->sttReader = segReader;
    }

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

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

  // open iter merger
H
Hongze Cheng 已提交
208
  code = tsdbIterMergerOpen(committer->iterArray, &committer->iterMerger, false);
H
Hongze Cheng 已提交
209
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
210

H
Hongze Cheng 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
  }
  return code;
}

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

  tsdbIterMergerClose(&committer->iterMerger);
  TARRAY2_CLEAR(committer->iterArray, tsdbIterClose);

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

static int32_t tsdbCommitTSDataToDataTableBegin(SCommitter2 *committer, const TABLEID *tbid) {
  int32_t code = 0;
  int32_t lino = 0;

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

  code = tsdbUpdateSkmTb(committer->tsdb, committer->ctx->tbid, committer->skmTb);
  TSDB_CHECK_CODE(code, lino, _exit);

  committer->blockDataIdx = 0;
  for (int32_t i = 0; i < ARRAY_SIZE(committer->blockData); i++) {
    code = tBlockDataInit(&committer->blockData[i], committer->ctx->tbid, committer->skmTb->pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

static int32_t tsdbCommitTSDataToDataTableEnd(SCommitter2 *committer) {
  if (committer->ctx->tbid->uid == 0) return 0;

  int32_t code = 0;
  int32_t lino = 0;

  int32_t cidx = committer->blockDataIdx;
  int32_t pidx = ((cidx + 1) & 1);
  int32_t numRow = (committer->blockData[cidx].nRow + committer->blockData[pidx].nRow) / 2;

  if (committer->blockData[pidx].nRow > 0 && numRow >= committer->minRow) {
    ASSERT(committer->blockData[pidx].nRow == committer->maxRow);

    SRowInfo row[1] = {{
        .suid = committer->ctx->tbid->suid,
        .uid = committer->ctx->tbid->uid,
        .row = tsdbRowFromBlockData(committer->blockData + pidx, 0),
    }};

    for (int32_t i = 0; i < numRow; i++) {
      row->row.iRow = i;

      code = tsdbDataFileWriteRow(committer->dataWriter, row);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tsdbDataFileFlush(committer->dataWriter);
    TSDB_CHECK_CODE(code, lino, _exit);

    for (int32_t i = numRow; i < committer->blockData[pidx].nRow; i++) {
      row->row.iRow = i;
      code = tsdbDataFileWriteRow(committer->dataWriter, row);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    row->row = tsdbRowFromBlockData(committer->blockData + cidx, 0);
    for (int32_t i = 0; i < committer->blockData[cidx].nRow; i++) {
      row->row.iRow = i;
      code = tsdbDataFileWriteRow(committer->dataWriter, row);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  } else {
    if (committer->blockData[pidx].nRow > 0) {
      code = tsdbDataFileWriteBlockData(committer->dataWriter, committer->blockData + cidx);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
    if (committer->blockData[cidx].nRow < committer->minRow) {
      code = tsdbSttFileWriteBlockData(committer->sttWriter, committer->blockData + cidx);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      code = tsdbDataFileWriteBlockData(committer->dataWriter, committer->blockData + cidx);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

  for (int32_t i = 0; i < ARRAY_SIZE(committer->blockData); i++) {
    tBlockDataReset(&committer->blockData[i]);
  }

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

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

  SMetaInfo info;
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->iterMerger)) != NULL;) {
    if (row->uid != committer->ctx->tbid->uid) {
      // end last table write
      code = tsdbCommitTSDataToDataTableEnd(committer);
      TSDB_CHECK_CODE(code, lino, _exit);

      // Ignore table of obsolescence
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
        code = tsdbIterMergerSkipTableData(committer->iterMerger, (TABLEID *)row);
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
      }

      code = tsdbCommitTSDataToDataTableBegin(committer, (TABLEID *)row);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    if (row->row.type == TSDBROW_ROW_FMT) {
      code = tsdbUpdateSkmRow(committer->tsdb, committer->ctx->tbid, TSDBROW_SVERSION(&row->row), committer->skmRow);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    TSDBKEY key = TSDBROW_KEY(&row->row);
    if (key.version <= committer->compactVersion                   //
        && committer->blockData[committer->blockDataIdx].nRow > 0  //
        && key.ts == committer->blockData[committer->blockDataIdx]
                         .aTSKEY[committer->blockData[committer->blockDataIdx].nRow - 1]) {
      code =
          tBlockDataUpdateRow(committer->blockData + committer->blockDataIdx, &row->row, committer->skmRow->pTSchema);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      if (committer->blockData[committer->blockDataIdx].nRow >= committer->maxRow) {
        int32_t idx = ((committer->blockDataIdx + 1) & 1);
        if (committer->blockData[idx].nRow >= committer->maxRow) {
          code = tsdbDataFileWriteBlockData(committer->dataWriter, committer->blockData + idx);
          TSDB_CHECK_CODE(code, lino, _exit);

          tBlockDataClear(committer->blockData + idx);
        }
        committer->blockDataIdx = idx;
      }

      code = tBlockDataAppendRow(&committer->blockData[committer->blockDataIdx], &row->row, committer->skmRow->pTSchema,
                                 row->uid);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tsdbIterMergerNext(committer->iterMerger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

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

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

  ASSERT(committer->sttReader == NULL);

  SMetaInfo info;
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->iterMerger)) != NULL;) {
H
Hongze Cheng 已提交
395 396 397 398
    if (row->uid != committer->ctx->tbid->uid) {
      committer->ctx->tbid->suid = row->suid;
      committer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
399
      // Ignore table of obsolescence
H
Hongze Cheng 已提交
400
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
H
Hongze Cheng 已提交
401 402 403
        code = tsdbIterMergerSkipTableData(committer->iterMerger, committer->ctx->tbid);
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
H
Hongze Cheng 已提交
404
      }
H
Hongze Cheng 已提交
405
    }
H
Hongze Cheng 已提交
406

H
Hongze Cheng 已提交
407 408 409 410 411 412
    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 已提交
413
      code = tsdbSttFileWriteRow(committer->sttWriter, row);
H
Hongze Cheng 已提交
414
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
415

H
Hongze Cheng 已提交
416 417
      code = tsdbIterMergerNext(committer->iterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
418 419 420 421 422
    }
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
  }
  return code;
}

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

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

  // open iter and iter merger
  code = tsdbCommitTSDataOpenIterMerger(committer);
  TSDB_CHECK_CODE(code, lino, _exit);

  // loop iter
  if (committer->sttTrigger == 1) {
    code = tsdbCommitTSDataToData(committer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
442
  } else {
H
Hongze Cheng 已提交
443 444 445 446 447 448 449 450 451 452 453
    code = tsdbCommitTSDataToStt(committer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // close iter and iter merger
  code = tsdbCommitTSDataCloseIterMerger(committer);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
454 455 456 457
  }
  return code;
}

H
Hongze Cheng 已提交
458
static int32_t tsdbCommitTombDataToStt(SCommitter2 *committer) {
H
Hongze Cheng 已提交
459
  int32_t code = 0;
H
Hongze Cheng 已提交
460
  int32_t lino = 0;
H
Hongze Cheng 已提交
461

H
Hongze Cheng 已提交
462 463 464
  for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->iterMerger));) {
    code = tsdbSttFileWriteTombRecord(committer->sttWriter, record);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
465

H
Hongze Cheng 已提交
466 467
    code = tsdbIterMergerNext(committer->iterMerger);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
468
  }
H
Hongze Cheng 已提交
469

H
Hongze Cheng 已提交
470 471 472 473 474 475
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
476

H
Hongze Cheng 已提交
477 478 479
static int32_t tsdbCommitTombDataToData(SCommitter2 *committer) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
480

H
Hongze Cheng 已提交
481 482 483 484
  if (committer->dataWriter == NULL || tsdbSttFileWriterIsOpened(committer->sttWriter)) {
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->iterMerger));) {
      code = tsdbSttFileWriteTombRecord(committer->sttWriter, record);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486 487 488 489 490 491 492
      code = tsdbIterMergerNext(committer->iterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  } else {
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->iterMerger));) {
      code = tsdbDataFileWriteTombRecord(committer->dataWriter, record);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
      code = tsdbIterMergerNext(committer->iterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

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

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

  STsdbIter      *iter;
  STsdbIterConfig config[1];

  if (committer->sttReader) {
    const TSttSegReaderArray *readerArray;

    tsdbSttFileReaderGetSegReader(committer->sttReader, &readerArray);

    SSttSegReader *segReader;
    TARRAY2_FOREACH(readerArray, segReader) {
      config->type = TSDB_ITER_TYPE_STT_TOMB;
      config->sttReader = segReader;

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

      code = TARRAY2_APPEND(committer->iterArray, iter);
H
Hongze Cheng 已提交
527 528 529 530
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

H
Hongze Cheng 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  config->type = TSDB_ITER_TYPE_MEMT_TOMB;
  config->memt = committer->tsdb->imem;

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

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

  // open iter
  code = tsdbIterMergerOpen(committer->iterArray, &committer->iterMerger, true);
  TSDB_CHECK_CODE(code, lino, _exit);

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

static int32_t tsdbCommitTombDataCloseIter(SCommitter2 *committer) {
  tsdbIterMergerClose(&committer->iterMerger);
  TARRAY2_CLEAR(committer->iterArray, tsdbIterClose);
  return 0;
}

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

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

  if (committer->sttTrigger > 1) {
    code = tsdbCommitTombDataToStt(committer);
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    code = tsdbCommitTombDataToData(committer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

H
Hongze Cheng 已提交
575 576
_exit:
  if (code) {
H
Hongze Cheng 已提交
577
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
578 579 580 581
  }
  return code;
}

H
Hongze Cheng 已提交
582 583 584 585 586 587
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 已提交
588
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->fid, &tsdb->keepCfg, committer->ctx->now);
H
Hongze Cheng 已提交
589 590
  tsdbFidKeyRange(committer->ctx->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
                  &committer->ctx->maxKey);
H
Hongze Cheng 已提交
591 592 593 594 595
  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 已提交
596

H
Hongze Cheng 已提交
597 598 599 600
  ASSERT(TARRAY2_SIZE(committer->iterArray) == 0);
  ASSERT(committer->iterMerger == NULL);
  ASSERT(committer->sttWriter == NULL);
  ASSERT(committer->dataWriter == NULL);
H
Hongze Cheng 已提交
601

H
Hongze Cheng 已提交
602 603
  code = tsdbCommitOpenWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
604

H
Hongze Cheng 已提交
605 606 607
  // reset nextKey
  committer->ctx->nextKey = TSKEY_MAX;

H
Hongze Cheng 已提交
608 609
_exit:
  if (code) {
H
Hongze Cheng 已提交
610
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
611
  } else {
H
Hongze Cheng 已提交
612 613
    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 已提交
614
  }
H
Hongze Cheng 已提交
615
  return 0;
H
Hongze Cheng 已提交
616 617
}

H
Hongze Cheng 已提交
618
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
H
Hongze Cheng 已提交
619
  int32_t code = 0;
H
Hongze Cheng 已提交
620
  int32_t lino = 0;
H
Hongze Cheng 已提交
621

H
Hongze Cheng 已提交
622 623 624 625 626
  if (committer->dataWriter) {
    code = tsdbDataFileWriterClose(&committer->dataWriter, 0, committer->fopArray);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
627
  code = tsdbSttFileWriterClose(&committer->sttWriter, 0, committer->fopArray);
H
Hongze Cheng 已提交
628
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
629

H
Hongze Cheng 已提交
630 631 632
  tsdbIterMergerClose(&committer->iterMerger);
  TARRAY2_CLEAR(committer->iterArray, tsdbIterClose);

H
Hongze Cheng 已提交
633 634
_exit:
  if (code) {
H
Hongze Cheng 已提交
635
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
636
  } else {
H
Hongze Cheng 已提交
637
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
638 639 640 641
  }
  return code;
}

H
Hongze Cheng 已提交
642
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
H
Hongze Cheng 已提交
643 644 645
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
646
  // fset commit start
H
Hongze Cheng 已提交
647
  code = tsdbCommitFileSetBegin(committer);
H
Hongze Cheng 已提交
648
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
649

H
Hongze Cheng 已提交
650
  // commit fset
H
Hongze Cheng 已提交
651
  code = tsdbCommitTSData(committer);
H
Hongze Cheng 已提交
652 653
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
654
  code = tsdbCommitTombData(committer);
H
Hongze Cheng 已提交
655 656 657
  TSDB_CHECK_CODE(code, lino, _exit);

  // fset commit end
H
Hongze Cheng 已提交
658
  code = tsdbCommitFileSetEnd(committer);
H
Hongze Cheng 已提交
659
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
660 661 662

_exit:
  if (code) {
H
Hongze Cheng 已提交
663
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
664
  } else {
H
Hongze Cheng 已提交
665
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
666 667 668 669
  }
  return code;
}

H
Hongze Cheng 已提交
670
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
H
Hongze Cheng 已提交
671
  int32_t code = 0;
H
Hongze Cheng 已提交
672
  int32_t lino = 0;
H
Hongze Cheng 已提交
673

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

H
Hongze Cheng 已提交
676
  committer->tsdb = tsdb;
H
Hongze Cheng 已提交
677 678
  code = tsdbFSCreateCopySnapshot(tsdb->pFS, &committer->fsetArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
679 680 681 682 683 684
  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 已提交
685 686 687
  committer->szPage = info->info.config.tsdbPageSize;
  committer->compactVersion = INT64_MAX;
  committer->ctx->cid = tsdbFSAllocEid(tsdb->pFS);
H
Hongze Cheng 已提交
688
  committer->ctx->now = taosGetTimestampSec();
H
Hongze Cheng 已提交
689

H
Hongze Cheng 已提交
690
  committer->ctx->nextKey = tsdb->imem->minKey;
H
Hongze Cheng 已提交
691 692 693
  if (tsdb->imem->nDel > 0) {
    SRBTreeIter iter[1] = {tRBTreeIterCreate(tsdb->imem->tbDataTree, 1)};

H
Hongze Cheng 已提交
694 695 696 697 698 699 700 701 702 703 704
    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 已提交
705 706
_exit:
  if (code) {
H
Hongze Cheng 已提交
707
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
708
  } else {
H
Hongze Cheng 已提交
709
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
710 711 712 713
  }
  return code;
}

H
Hongze Cheng 已提交
714
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
H
Hongze Cheng 已提交
715
  int32_t code = 0;
H
Hongze Cheng 已提交
716
  int32_t lino = 0;
H
Hongze Cheng 已提交
717

H
Hongze Cheng 已提交
718
  if (eno == 0) {
H
Hongze Cheng 已提交
719
    code = tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
720
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
721
  } else {
H
Hongze Cheng 已提交
722 723
    // TODO
    ASSERT(0);
H
Hongze Cheng 已提交
724
  }
H
Hongze Cheng 已提交
725

H
Hongze Cheng 已提交
726 727 728
  ASSERT(committer->dataWriter == NULL);
  ASSERT(committer->sttWriter == NULL);
  ASSERT(committer->iterMerger == NULL);
H
Hongze Cheng 已提交
729 730
  TARRAY2_DESTROY(committer->iterArray, NULL);
  TARRAY2_DESTROY(committer->fopArray, NULL);
H
Hongze Cheng 已提交
731
  tsdbFSDestroyCopySnapshot(&committer->fsetArr);
H
Hongze Cheng 已提交
732

H
Hongze Cheng 已提交
733
_exit:
H
Hongze Cheng 已提交
734
  if (code) {
H
Hongze Cheng 已提交
735 736
    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 已提交
737
  } else {
H
Hongze Cheng 已提交
738
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->cid);
H
Hongze Cheng 已提交
739
  }
H
Hongze Cheng 已提交
740 741 742
  return code;
}

H
Hongze Cheng 已提交
743 744 745 746 747 748
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 已提交
749 750 751
  return 0;
}

H
Hongze Cheng 已提交
752 753
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
  if (!tsdb) return 0;
H
Hongze Cheng 已提交
754

H
Hongze Cheng 已提交
755 756 757
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
758 759 760
  SMemTable *imem = tsdb->imem;
  int64_t    nRow = imem->nRow;
  int64_t    nDel = imem->nDel;
H
Hongze Cheng 已提交
761

H
Hongze Cheng 已提交
762
  if (nRow == 0 && nDel == 0) {
H
Hongze Cheng 已提交
763 764 765
    taosThreadRwlockWrlock(&tsdb->rwLock);
    tsdb->imem = NULL;
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
766
    tsdbUnrefMemTable(imem, NULL, true);
H
Hongze Cheng 已提交
767
  } else {
H
Hongze Cheng 已提交
768
    SCommitter2 committer[1];
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770
    code = tsdbOpenCommitter(tsdb, info, committer);
H
Hongze Cheng 已提交
771
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
772

H
Hongze Cheng 已提交
773 774
    while (committer->ctx->nextKey != TSKEY_MAX) {
      code = tsdbCommitFileSet(committer);
H
Hongze Cheng 已提交
775
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
776
    }
H
Hongze Cheng 已提交
777

H
Hongze Cheng 已提交
778
    code = tsdbCloseCommitter(committer, code);
H
Hongze Cheng 已提交
779 780
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
781 782 783

_exit:
  if (code) {
H
Hongze Cheng 已提交
784
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
785
  } else {
H
Hongze Cheng 已提交
786
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, TD_VID(tsdb->pVnode), __func__, nRow, nDel);
H
Hongze Cheng 已提交
787 788 789 790
  }
  return code;
}

H
Hongze Cheng 已提交
791
int32_t tsdbCommitCommit(STsdb *tsdb) {
H
Hongze Cheng 已提交
792 793
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
794

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

H
Hongze Cheng 已提交
797 798 799
  SMemTable *pMemTable = tsdb->imem;
  taosThreadRwlockWrlock(&tsdb->rwLock);
  code = tsdbFSEditCommit(tsdb->pFS);
H
Hongze Cheng 已提交
800
  if (code) {
H
Hongze Cheng 已提交
801
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
802 803
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
804 805
  tsdb->imem = NULL;
  taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
806
  tsdbUnrefMemTable(pMemTable, NULL, true);
H
Hongze Cheng 已提交
807 808 809

_exit:
  if (code) {
H
Hongze Cheng 已提交
810
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
811
  } else {
H
Hongze Cheng 已提交
812
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
813 814 815 816 817 818 819
  }
  return code;
}

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

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

H
Hongze Cheng 已提交
823
  code = tsdbFSEditAbort(pTsdb->pFS);
H
Hongze Cheng 已提交
824 825 826 827
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
828
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
829
  } else {
H
Hongze Cheng 已提交
830
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
831 832 833
  }
  return code;
}