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

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

H
Hongze Cheng 已提交
18
// extern dependencies
H
Hongze Cheng 已提交
19
typedef struct {
H
Hongze Cheng 已提交
20 21
  STsdb         *tsdb;
  TFileSetArray *fsetArr;
H
Hongze Cheng 已提交
22 23 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
    int64_t    now;
    TSKEY      nextKey;
    int32_t    fid;
    int32_t    expLevel;
H
Hongze Cheng 已提交
42
    SDiskID    did;
H
Hongze Cheng 已提交
43 44 45 46 47 48
    TSKEY      minKey;
    TSKEY      maxKey;
    STFileSet *fset;
    TABLEID    tbid[1];
  } ctx[1];

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

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

H
Hongze Cheng 已提交
58
  // writer
H
Hongze Cheng 已提交
59 60
  SBlockData       blockData[2];
  int32_t          blockDataIdx;
H
Hongze Cheng 已提交
61
  SDataFileWriter *dataWriter;
H
Hongze Cheng 已提交
62
  SSttFileWriter  *sttWriter;
H
Hongze Cheng 已提交
63
} SCommitter2;
H
Hongze Cheng 已提交
64

65
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
66
  int32_t code = 0;
H
Hongze Cheng 已提交
67
  int32_t lino = 0;
H
Hongze Cheng 已提交
68

69
  // stt writer
H
Hongze Cheng 已提交
70 71 72
  SSttFileWriterConfig config[1] = {{
      .tsdb = committer->tsdb,
      .maxRow = committer->maxRow,
H
Hongze Cheng 已提交
73
      .szPage = committer->szPage,
H
Hongze Cheng 已提交
74 75
      .cmprAlg = committer->cmprAlg,
      .compactVersion = committer->compactVersion,
H
Hongze Cheng 已提交
76 77 78 79
      .did = committer->ctx->did,
      .fid = committer->ctx->fid,
      .cid = committer->ctx->cid,
      .level = 0,
H
Hongze Cheng 已提交
80 81 82
  }};

  code = tsdbSttFileWriterOpen(config, &committer->sttWriter);
H
Hongze Cheng 已提交
83 84
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  // data writer
  if (committer->sttTrigger == 1) {
    // data writer
    SDataFileWriterConfig config = {
        .tsdb = committer->tsdb,
        .cmprAlg = committer->cmprAlg,
        .maxRow = committer->maxRow,
        .szPage = committer->szPage,
        .fid = committer->ctx->fid,
        .cid = committer->ctx->cid,
        .did = committer->ctx->did,
        .compactVersion = committer->compactVersion,
    };

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

    code = tsdbDataFileWriterOpen(&config, &committer->dataWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
110
  }
H
Hongze Cheng 已提交
111

H
Hongze Cheng 已提交
112 113 114
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
115 116
  }
  return code;
H
Hongze Cheng 已提交
117 118
}

119
static int32_t tsdbCommitCloseWriter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
120 121
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
122

123
  code = tsdbSttFileWriterClose(&committer->sttWriter, 0, committer->fopArray);
H
Hongze Cheng 已提交
124
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
125

126
  code = tsdbDataFileWriterClose(&committer->dataWriter, 0, committer->fopArray);
H
Hongze Cheng 已提交
127
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
128

H
Hongze Cheng 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
_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;
230
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
H
Hongze Cheng 已提交
231 232 233 234 235 236 237
    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) {
238
        code = tsdbIterMergerSkipTableData(committer->dataIterMerger, (TABLEID *)row);
H
Hongze Cheng 已提交
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
        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);
    }

277
    code = tsdbIterMergerNext(committer->dataIterMerger);
H
Hongze Cheng 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    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;
298
  for (SRowInfo *row; (row = tsdbIterMergerGetData(committer->dataIterMerger)) != NULL;) {
H
Hongze Cheng 已提交
299 300 301 302
    if (row->uid != committer->ctx->tbid->uid) {
      committer->ctx->tbid->suid = row->suid;
      committer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
303
      // Ignore table of obsolescence
H
Hongze Cheng 已提交
304
      if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, &info, NULL) != 0) {
305
        code = tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid);
H
Hongze Cheng 已提交
306 307
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
H
Hongze Cheng 已提交
308
      }
H
Hongze Cheng 已提交
309
    }
H
Hongze Cheng 已提交
310

H
Hongze Cheng 已提交
311 312 313
    TSKEY ts = TSDBROW_TS(&row->row);
    if (ts > committer->ctx->maxKey) {
      committer->ctx->nextKey = TMIN(committer->ctx->nextKey, ts);
314
      code = tsdbIterMergerSkipTableData(committer->dataIterMerger, committer->ctx->tbid);
H
Hongze Cheng 已提交
315 316
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
H
Hongze Cheng 已提交
317
      code = tsdbSttFileWriteRow(committer->sttWriter, row);
H
Hongze Cheng 已提交
318
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
319

320
      code = tsdbIterMergerNext(committer->dataIterMerger);
H
Hongze Cheng 已提交
321
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
322 323 324 325 326
    }
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339
    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;

  // loop iter
  if (committer->sttTrigger == 1) {
    code = tsdbCommitTSDataToData(committer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
340
  } else {
H
Hongze Cheng 已提交
341 342 343 344
    code = tsdbCommitTSDataToStt(committer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
  }
  return code;
}

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

  if (committer->dataWriter == NULL || tsdbSttFileWriterIsOpened(committer->sttWriter)) {
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
      code = tsdbSttFileWriteTombRecord(committer->sttWriter, record);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbIterMergerNext(committer->tombIterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  } else {
    for (STombRecord *record; (record = tsdbIterMergerGetTombRecord(committer->tombIterMerger));) {
      code = tsdbDataFileWriteTombRecord(committer->dataWriter, record);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbIterMergerNext(committer->tombIterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
H
Hongze Cheng 已提交
373 374 375 376

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
377 378 379 380
  }
  return code;
}

381
static int32_t tsdbCommitOpenReader(SCommitter2 *committer) {
H
Hongze Cheng 已提交
382 383 384
  int32_t code = 0;
  int32_t lino = 0;

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

387 388 389 390 391 392
  if (committer->ctx->fset == NULL                        //
      || committer->sttTrigger > 1                        //
      || TARRAY2_SIZE(committer->ctx->fset->lvlArr) == 0  //
  ) {
    return 0;
  }
H
Hongze Cheng 已提交
393

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

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

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

400 401
  if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
    return 0;
H
Hongze Cheng 已提交
402 403
  }

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

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

408 409 410 411 412 413
  SSttFileReaderConfig config = {
      .tsdb = committer->tsdb,
      .szPage = committer->szPage,
      .file = fobj->f[0],
  };
  code = tsdbSttFileReaderOpen(fobj->fname, &config, &committer->sttReader);
H
Hongze Cheng 已提交
414 415
  TSDB_CHECK_CODE(code, lino, _exit);

416 417 418 419 420 421 422
  STFileOp op = {
      .optype = TSDB_FOP_REMOVE,
      .fid = fobj->f->fid,
      .of = fobj->f[0],
  };

  code = TARRAY2_APPEND(committer->fopArray, op);
H
Hongze Cheng 已提交
423 424 425 426 427 428 429 430 431
  TSDB_CHECK_CODE(code, lino, _exit);

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

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

434
static int32_t tsdbCommitOpenIter(SCommitter2 *committer) {
H
Hongze Cheng 已提交
435 436 437
  int32_t code = 0;
  int32_t lino = 0;

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  ASSERT(TARRAY2_SIZE(committer->dataIterArray) == 0);
  ASSERT(committer->dataIterMerger == NULL);
  ASSERT(TARRAY2_SIZE(committer->tombIterArray) == 0);
  ASSERT(committer->tombIterMerger == NULL);

  STsdbIter      *iter;
  STsdbIterConfig config = {0};

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

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

455 456
  code = TARRAY2_APPEND(committer->dataIterArray, iter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
457

458 459 460
  // mem tomb iter
  config.type = TSDB_ITER_TYPE_MEMT_TOMB;
  config.memt = committer->tsdb->imem;
H
Hongze Cheng 已提交
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
  code = tsdbIterOpen(&config, &iter);
  TSDB_CHECK_CODE(code, lino, _exit);

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

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

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

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

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

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

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

491 492 493 494 495
  // open merger
  code = tsdbIterMergerOpen(committer->dataIterArray, &committer->dataIterMerger, false);
  TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
498 499
_exit:
  if (code) {
H
Hongze Cheng 已提交
500
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
501 502 503 504
  }
  return code;
}

505 506 507 508 509 510 511 512
static int32_t tsdbCommitCloseIter(SCommitter2 *committer) {
  tsdbIterMergerClose(&committer->tombIterMerger);
  tsdbIterMergerClose(&committer->dataIterMerger);
  TARRAY2_CLEAR(committer->tombIterArray, tsdbIterClose);
  TARRAY2_CLEAR(committer->dataIterArray, tsdbIterClose);
  return 0;
}

H
Hongze Cheng 已提交
513 514 515 516 517 518
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 已提交
519
  committer->ctx->expLevel = tsdbFidLevel(committer->ctx->fid, &tsdb->keepCfg, committer->ctx->now);
H
Hongze Cheng 已提交
520 521
  tsdbFidKeyRange(committer->ctx->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
                  &committer->ctx->maxKey);
H
Hongze Cheng 已提交
522 523
  code = tfsAllocDisk(committer->tsdb->pVnode->pTfs, committer->ctx->expLevel, &committer->ctx->did);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
524 525 526 527 528
  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 已提交
529

530 531
  ASSERT(TARRAY2_SIZE(committer->dataIterArray) == 0);
  ASSERT(committer->dataIterMerger == NULL);
H
Hongze Cheng 已提交
532 533
  ASSERT(committer->sttWriter == NULL);
  ASSERT(committer->dataWriter == NULL);
H
Hongze Cheng 已提交
534

535 536 537 538 539 540
  code = tsdbCommitOpenReader(committer);
  TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
541 542
  code = tsdbCommitOpenWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
543

H
Hongze Cheng 已提交
544 545 546
  // reset nextKey
  committer->ctx->nextKey = TSKEY_MAX;

H
Hongze Cheng 已提交
547 548
_exit:
  if (code) {
H
Hongze Cheng 已提交
549
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
550
  } else {
H
Hongze Cheng 已提交
551 552
    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 已提交
553
  }
H
Hongze Cheng 已提交
554
  return 0;
H
Hongze Cheng 已提交
555 556
}

H
Hongze Cheng 已提交
557
static int32_t tsdbCommitFileSetEnd(SCommitter2 *committer) {
H
Hongze Cheng 已提交
558
  int32_t code = 0;
H
Hongze Cheng 已提交
559
  int32_t lino = 0;
H
Hongze Cheng 已提交
560

561 562
  code = tsdbCommitCloseWriter(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
563

564
  code = tsdbCommitCloseIter(committer);
H
Hongze Cheng 已提交
565
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
566

567 568
  code = tsdbCommitCloseReader(committer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
569

H
Hongze Cheng 已提交
570 571
_exit:
  if (code) {
H
Hongze Cheng 已提交
572
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
573
  } else {
H
Hongze Cheng 已提交
574
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
575 576 577 578
  }
  return code;
}

H
Hongze Cheng 已提交
579
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
H
Hongze Cheng 已提交
580 581 582
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
583
  // fset commit start
H
Hongze Cheng 已提交
584
  code = tsdbCommitFileSetBegin(committer);
H
Hongze Cheng 已提交
585
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
586

H
Hongze Cheng 已提交
587
  // commit fset
H
Hongze Cheng 已提交
588
  code = tsdbCommitTSData(committer);
H
Hongze Cheng 已提交
589 590
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
591
  code = tsdbCommitTombData(committer);
H
Hongze Cheng 已提交
592 593 594
  TSDB_CHECK_CODE(code, lino, _exit);

  // fset commit end
H
Hongze Cheng 已提交
595
  code = tsdbCommitFileSetEnd(committer);
H
Hongze Cheng 已提交
596
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
597 598 599

_exit:
  if (code) {
H
Hongze Cheng 已提交
600
    TSDB_ERROR_LOG(TD_VID(committer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
601
  } else {
H
Hongze Cheng 已提交
602
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->fid);
H
Hongze Cheng 已提交
603 604 605 606
  }
  return code;
}

H
Hongze Cheng 已提交
607
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
H
Hongze Cheng 已提交
608
  int32_t code = 0;
H
Hongze Cheng 已提交
609
  int32_t lino = 0;
H
Hongze Cheng 已提交
610

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

H
Hongze Cheng 已提交
613
  committer->tsdb = tsdb;
H
Hongze Cheng 已提交
614 615
  code = tsdbFSCreateCopySnapshot(tsdb->pFS, &committer->fsetArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
616 617 618 619 620 621
  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 已提交
622 623 624
  committer->szPage = info->info.config.tsdbPageSize;
  committer->compactVersion = INT64_MAX;
  committer->ctx->cid = tsdbFSAllocEid(tsdb->pFS);
H
Hongze Cheng 已提交
625
  committer->ctx->now = taosGetTimestampSec();
H
Hongze Cheng 已提交
626

H
Hongze Cheng 已提交
627
  committer->ctx->nextKey = tsdb->imem->minKey;
H
Hongze Cheng 已提交
628 629 630
  if (tsdb->imem->nDel > 0) {
    SRBTreeIter iter[1] = {tRBTreeIterCreate(tsdb->imem->tbDataTree, 1)};

H
Hongze Cheng 已提交
631 632 633 634 635 636 637 638 639 640 641
    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 已提交
642 643
_exit:
  if (code) {
H
Hongze Cheng 已提交
644
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
645
  } else {
H
Hongze Cheng 已提交
646
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
647 648 649 650
  }
  return code;
}

H
Hongze Cheng 已提交
651
static int32_t tsdbCloseCommitter(SCommitter2 *committer, int32_t eno) {
H
Hongze Cheng 已提交
652
  int32_t code = 0;
H
Hongze Cheng 已提交
653
  int32_t lino = 0;
H
Hongze Cheng 已提交
654

H
Hongze Cheng 已提交
655
  if (eno == 0) {
H
Hongze Cheng 已提交
656
    code = tsdbFSEditBegin(committer->tsdb->pFS, committer->fopArray, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
657
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
658
  } else {
H
Hongze Cheng 已提交
659 660
    // TODO
    ASSERT(0);
H
Hongze Cheng 已提交
661
  }
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663 664
  ASSERT(committer->dataWriter == NULL);
  ASSERT(committer->sttWriter == NULL);
665 666 667 668
  ASSERT(committer->dataIterMerger == NULL);
  ASSERT(committer->tombIterMerger == NULL);
  TARRAY2_DESTROY(committer->dataIterArray, NULL);
  TARRAY2_DESTROY(committer->tombIterArray, NULL);
H
Hongze Cheng 已提交
669
  TARRAY2_DESTROY(committer->fopArray, NULL);
H
Hongze Cheng 已提交
670
  tsdbFSDestroyCopySnapshot(&committer->fsetArr);
H
Hongze Cheng 已提交
671

H
Hongze Cheng 已提交
672
_exit:
H
Hongze Cheng 已提交
673
  if (code) {
H
Hongze Cheng 已提交
674 675
    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 已提交
676
  } else {
H
Hongze Cheng 已提交
677
    tsdbDebug("vgId:%d %s done, eid:%" PRId64, TD_VID(committer->tsdb->pVnode), __func__, committer->ctx->cid);
H
Hongze Cheng 已提交
678
  }
H
Hongze Cheng 已提交
679 680 681
  return code;
}

H
Hongze Cheng 已提交
682 683 684 685 686 687
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 已提交
688 689 690
  return 0;
}

H
Hongze Cheng 已提交
691 692
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
  if (!tsdb) return 0;
H
Hongze Cheng 已提交
693

H
Hongze Cheng 已提交
694 695 696
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
697 698 699
  SMemTable *imem = tsdb->imem;
  int64_t    nRow = imem->nRow;
  int64_t    nDel = imem->nDel;
H
Hongze Cheng 已提交
700

H
Hongze Cheng 已提交
701
  if (nRow == 0 && nDel == 0) {
H
Hongze Cheng 已提交
702 703 704
    taosThreadRwlockWrlock(&tsdb->rwLock);
    tsdb->imem = NULL;
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
705
    tsdbUnrefMemTable(imem, NULL, true);
H
Hongze Cheng 已提交
706
  } else {
H
Hongze Cheng 已提交
707
    SCommitter2 committer[1];
H
Hongze Cheng 已提交
708

H
Hongze Cheng 已提交
709
    code = tsdbOpenCommitter(tsdb, info, committer);
H
Hongze Cheng 已提交
710
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
711

H
Hongze Cheng 已提交
712 713
    while (committer->ctx->nextKey != TSKEY_MAX) {
      code = tsdbCommitFileSet(committer);
H
Hongze Cheng 已提交
714
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
715
    }
H
Hongze Cheng 已提交
716

H
Hongze Cheng 已提交
717
    code = tsdbCloseCommitter(committer, code);
H
Hongze Cheng 已提交
718 719
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
720 721 722

_exit:
  if (code) {
H
Hongze Cheng 已提交
723
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
724
  } else {
H
Hongze Cheng 已提交
725
    tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, TD_VID(tsdb->pVnode), __func__, nRow, nDel);
H
Hongze Cheng 已提交
726 727 728 729
  }
  return code;
}

H
Hongze Cheng 已提交
730
int32_t tsdbCommitCommit(STsdb *tsdb) {
H
Hongze Cheng 已提交
731 732
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
733

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

H
Hongze Cheng 已提交
736 737 738
  SMemTable *pMemTable = tsdb->imem;
  taosThreadRwlockWrlock(&tsdb->rwLock);
  code = tsdbFSEditCommit(tsdb->pFS);
H
Hongze Cheng 已提交
739
  if (code) {
H
Hongze Cheng 已提交
740
    taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
741 742
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
743 744
  tsdb->imem = NULL;
  taosThreadRwlockUnlock(&tsdb->rwLock);
H
Hongze Cheng 已提交
745
  tsdbUnrefMemTable(pMemTable, NULL, true);
H
Hongze Cheng 已提交
746 747 748

_exit:
  if (code) {
H
Hongze Cheng 已提交
749
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
750
  } else {
H
Hongze Cheng 已提交
751
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
752 753 754 755 756 757 758
  }
  return code;
}

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

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

H
Hongze Cheng 已提交
762
  code = tsdbFSEditAbort(pTsdb->pFS);
H
Hongze Cheng 已提交
763 764 765 766
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
767
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
768
  } else {
H
Hongze Cheng 已提交
769
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
770 771 772
  }
  return code;
}