tsdbUpgrade.c 18.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#include "tsdbUpgrade.h"

// old
H
Hongze Cheng 已提交
19 20
extern void    tsdbGetCurrentFName(STsdb *pTsdb, char *current, char *current_t);
extern int32_t tsdbReadDataBlockEx(SDataFReader *pReader, SDataBlk *pDataBlk, SBlockData *pBlockData);
H
Hongze Cheng 已提交
21 22 23 24

// new
extern int32_t save_fs(const TFileSetArray *arr, const char *fname);
extern int32_t current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype);
H
Hongze Cheng 已提交
25 26 27 28
extern int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, int8_t cmprAlg, int64_t *fileSize,
                                      TBrinBlkArray *brinBlkArray, uint8_t **bufArr);
extern int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize);
extern int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer);
H
Hongze Cheng 已提交
29 30 31
extern int32_t tsdbSttLvlInit(int32_t level, SSttLvl **lvl);
extern int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize);
extern int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize);
H
Hongze Cheng 已提交
32 33 34 35
extern int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
                                      TTombBlkArray *tombBlkArray, uint8_t **bufArr);
extern int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize);
extern int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize);
H
Hongze Cheng 已提交
36

H
Hongze Cheng 已提交
37
static int32_t tsdbUpgradeHead(STsdb *tsdb, SDFileSet *pDFileSet, SDataFReader *reader, STFileSet *fset) {
H
Hongze Cheng 已提交
38 39 40
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  struct {
    // config
    int32_t  maxRow;
    int8_t   cmprAlg;
    int32_t  szPage;
    uint8_t *bufArr[8];
    // reader
    SArray    *aBlockIdx;
    SMapData   mDataBlk[1];
    SBlockData blockData[1];
    // writer
    STsdbFD      *fd;
    SBrinBlock    brinBlock[1];
    TBrinBlkArray brinBlkArray[1];
    SHeadFooter   footer[1];
  } ctx[1] = {{
      .maxRow = tsdb->pVnode->config.tsdbCfg.maxRows,
      .cmprAlg = tsdb->pVnode->config.tsdbCfg.compression,
      .szPage = tsdb->pVnode->config.tsdbPageSize,
  }};

  if ((ctx->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
66

H
Hongze Cheng 已提交
67
  code = tsdbReadBlockIdx(reader, ctx->aBlockIdx);
H
Hongze Cheng 已提交
68 69
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
  if (taosArrayGetSize(ctx->aBlockIdx) == 0) {
    goto _exit;
  } else {
    STFile file = {
        .type = TSDB_FTYPE_HEAD,
        .did = pDFileSet->diskId,
        .fid = fset->fid,
        .cid = pDFileSet->pHeadF->commitID,
        .size = pDFileSet->pHeadF->size,
    };

    code = tsdbTFileObjInit(tsdb, &file, &fset->farr[TSDB_FTYPE_HEAD]);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
83

H
Hongze Cheng 已提交
84 85 86 87 88
    // open fd
    char fname[TSDB_FILENAME_LEN];
    tsdbTFileName(tsdb, &file, fname);

    code = tsdbOpenFile(fname, ctx->szPage, TD_FILE_READ | TD_FILE_WRITE, &ctx->fd);
H
Hongze Cheng 已提交
89
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
90
  }
H
Hongze Cheng 已提交
91

H
Hongze Cheng 已提交
92 93
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(ctx->aBlockIdx); ++iBlockIdx) {
    SBlockIdx *pBlockIdx = taosArrayGet(ctx->aBlockIdx, iBlockIdx);
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95 96
    code = tsdbReadDataBlk(reader, pBlockIdx, ctx->mDataBlk);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    for (int32_t iDataBlk = 0; iDataBlk < ctx->mDataBlk->nItem; ++iDataBlk) {
      SDataBlk dataBlk[1];
      tMapDataGetItemByIdx(ctx->mDataBlk, iDataBlk, dataBlk, tGetDataBlk);

      SBrinRecord record = {
          .suid = pBlockIdx->suid,
          .uid = pBlockIdx->uid,
          .firstKey = dataBlk->minKey.ts,
          .firstKeyVer = dataBlk->minKey.version,
          .lastKey = dataBlk->maxKey.ts,
          .lastKeyVer = dataBlk->maxKey.version,
          .minVer = dataBlk->minVer,
          .maxVer = dataBlk->maxVer,
          .blockOffset = dataBlk->aSubBlock->offset,
          .smaOffset = dataBlk->smaInfo.offset,
          .blockSize = dataBlk->aSubBlock->szBlock,
          .blockKeySize = dataBlk->aSubBlock->szKey,
          .smaSize = dataBlk->smaInfo.size,
          .numRow = dataBlk->nRow,
          .count = dataBlk->nRow,
      };
H
Hongze Cheng 已提交
119

H
Hongze Cheng 已提交
120 121
      if (dataBlk->hasDup) {
        code = tsdbReadDataBlockEx(reader, dataBlk, ctx->blockData);
H
Hongze Cheng 已提交
122 123
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
124 125 126 127 128
        record.count = 1;
        for (int32_t i = 1; i < ctx->blockData->nRow; ++i) {
          if (ctx->blockData->aTSKEY[i] != ctx->blockData->aTSKEY[i - 1]) {
            record.count++;
          }
H
Hongze Cheng 已提交
129 130 131
        }
      }

H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139
      code = tBrinBlockPut(ctx->brinBlock, &record);
      TSDB_CHECK_CODE(code, lino, _exit);

      if (BRIN_BLOCK_SIZE(ctx->brinBlock) >= ctx->maxRow) {
        code = tsdbFileWriteBrinBlock(ctx->fd, ctx->brinBlock, ctx->cmprAlg, &fset->farr[TSDB_FTYPE_HEAD]->f->size,
                                      ctx->brinBlkArray, ctx->bufArr);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
140
    }
H
Hongze Cheng 已提交
141
  }
H
Hongze Cheng 已提交
142

H
Hongze Cheng 已提交
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
  if (BRIN_BLOCK_SIZE(ctx->brinBlock) > 0) {
    code = tsdbFileWriteBrinBlock(ctx->fd, ctx->brinBlock, ctx->cmprAlg, &fset->farr[TSDB_FTYPE_HEAD]->f->size,
                                  ctx->brinBlkArray, ctx->bufArr);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code =
      tsdbFileWriteBrinBlk(ctx->fd, ctx->brinBlkArray, ctx->footer->brinBlkPtr, &fset->farr[TSDB_FTYPE_HEAD]->f->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbFileWriteHeadFooter(ctx->fd, &fset->farr[TSDB_FTYPE_HEAD]->f->size, ctx->footer);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbFsyncFile(ctx->fd);
  TSDB_CHECK_CODE(code, lino, _exit);

  tsdbCloseFile(&ctx->fd);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
  TARRAY2_DESTROY(ctx->brinBlkArray, NULL);
  tBrinBlockDestroy(ctx->brinBlock);
  tBlockDataDestroy(ctx->blockData);
  tMapDataClear(ctx->mDataBlk);
  taosArrayDestroy(ctx->aBlockIdx);
  for (int32_t i = 0; i < ARRAY_SIZE(ctx->bufArr); ++i) {
    tFree(ctx->bufArr[i]);
  }
  return code;
}

static int32_t tsdbUpgradeData(STsdb *tsdb, SDFileSet *pDFileSet, SDataFReader *reader, STFileSet *fset) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193
  if (fset->farr[TSDB_FTYPE_HEAD] == NULL) {
    return 0;
  }

  STFile file = {
      .type = TSDB_FTYPE_DATA,
      .did = pDFileSet->diskId,
      .fid = fset->fid,
      .cid = pDFileSet->pDataF->commitID,
      .size = pDFileSet->pDataF->size,
  };

  code = tsdbTFileObjInit(tsdb, &file, &fset->farr[TSDB_FTYPE_DATA]);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
194 195 196 197 198 199 200 201 202 203 204 205

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

static int32_t tsdbUpgradeSma(STsdb *tsdb, SDFileSet *pDFileSet, SDataFReader *reader, STFileSet *fset) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
206 207 208 209 210 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
  if (fset->farr[TSDB_FTYPE_HEAD] == NULL) {
    return 0;
  }

  STFile file = {
      .type = TSDB_FTYPE_SMA,
      .did = pDFileSet->diskId,
      .fid = fset->fid,
      .cid = pDFileSet->pSmaF->commitID,
      .size = pDFileSet->pSmaF->size,
  };

  code = tsdbTFileObjInit(tsdb, &file, &fset->farr[TSDB_FTYPE_SMA]);
  TSDB_CHECK_CODE(code, lino, _exit);

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

static int32_t tsdbUpgradeSttFile(STsdb *tsdb, SDFileSet *pDFileSet, SDataFReader *reader, STFileSet *fset,
                                  int32_t iStt, SSttLvl *lvl) {
  int32_t code = 0;
  int32_t lino = 0;

  SArray *aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (aSttBlk == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = tsdbReadSttBlk(reader, iStt, aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(aSttBlk) > 0) {
    SSttFile  *pSttF = pDFileSet->aSttF[iStt];
    STFileObj *fobj;
    struct {
      int32_t szPage;
      // writer
      STsdbFD     *fd;
      TSttBlkArray sttBlkArray[1];
      SSttFooter   footer[1];
    } ctx[1] = {{
        .szPage = tsdb->pVnode->config.tsdbPageSize,
    }};

    STFile file = {
        .type = TSDB_FTYPE_STT,
        .did = pDFileSet->diskId,
        .fid = fset->fid,
        .cid = pSttF->commitID,
        .size = pSttF->size,
    };
    code = tsdbTFileObjInit(tsdb, &file, &fobj);
    TSDB_CHECK_CODE(code, lino, _exit1);

    code = tsdbOpenFile(fobj->fname, ctx->szPage, TD_FILE_READ | TD_FILE_WRITE, &ctx->fd);
    TSDB_CHECK_CODE(code, lino, _exit1);

    for (int32_t iSttBlk = 0; iSttBlk < taosArrayGetSize(aSttBlk); iSttBlk++) {
      code = TARRAY2_APPEND_PTR(ctx->sttBlkArray, (SSttBlk *)taosArrayGet(aSttBlk, iSttBlk));
      TSDB_CHECK_CODE(code, lino, _exit1);
    }

    code = tsdbFileWriteSttBlk(ctx->fd, ctx->sttBlkArray, ctx->footer->sttBlkPtr, &fobj->f->size);
    TSDB_CHECK_CODE(code, lino, _exit1);

    code = tsdbFileWriteSttFooter(ctx->fd, ctx->footer, &fobj->f->size);
    TSDB_CHECK_CODE(code, lino, _exit1);

    code = tsdbFsyncFile(ctx->fd);
    TSDB_CHECK_CODE(code, lino, _exit1);

    tsdbCloseFile(&ctx->fd);

    code = TARRAY2_APPEND(lvl->fobjArr, fobj);
    TSDB_CHECK_CODE(code, lino, _exit1);

  _exit1:
    TARRAY2_DESTROY(ctx->sttBlkArray, NULL);
  }
H
Hongze Cheng 已提交
290 291 292 293 294

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
295
  taosArrayDestroy(aSttBlk);
H
Hongze Cheng 已提交
296 297 298 299 300 301 302
  return code;
}

static int32_t tsdbUpgradeStt(STsdb *tsdb, SDFileSet *pDFileSet, SDataFReader *reader, STFileSet *fset) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  if (pDFileSet->nSttF == 0) {
    return 0;
  }

  SSttLvl *lvl;
  code = tsdbSttLvlInit(0, &lvl);
  TSDB_CHECK_CODE(code, lino, _exit);

  for (int32_t iStt = 0; iStt < pDFileSet->nSttF; ++iStt) {
    code = tsdbUpgradeSttFile(tsdb, pDFileSet, reader, fset, iStt, lvl);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = TARRAY2_APPEND(fset->lvlArr, lvl);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
318

H
Hongze Cheng 已提交
319 320 321
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
322
  }
H
Hongze Cheng 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  return code;
}

static int32_t tsdbUpgradeFileSet(STsdb *tsdb, SDFileSet *pDFileSet, TFileSetArray *fileSetArray) {
  int32_t code = 0;
  int32_t lino = 0;

  SDataFReader *reader;
  STFileSet    *fset;

  code = tsdbTFileSetInit(pDFileSet->fid, &fset);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbDataFReaderOpen(&reader, tsdb, pDFileSet);
  TSDB_CHECK_CODE(code, lino, _exit);

  // .head
  code = tsdbUpgradeHead(tsdb, pDFileSet, reader, fset);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
342 343

  // .data
H
Hongze Cheng 已提交
344 345
  code = tsdbUpgradeData(tsdb, pDFileSet, reader, fset);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
346 347

  // .sma
H
Hongze Cheng 已提交
348 349
  code = tsdbUpgradeSma(tsdb, pDFileSet, reader, fset);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
350 351

  // .stt
H
Hongze Cheng 已提交
352 353 354
  if (pDFileSet->nSttF > 0) {
    code = tsdbUpgradeStt(tsdb, pDFileSet, reader, fset);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
355 356 357 358
  }

  tsdbDataFReaderClose(&reader);

H
Hongze Cheng 已提交
359 360 361
  code = TARRAY2_APPEND(fileSetArray, fset);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
362 363 364 365 366 367 368
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static int32_t tsdbUpgradeOpenTombFile(STsdb *tsdb, STFileSet *fset, STsdbFD **fd, STFileObj **fobj, bool *toStt) {
  int32_t code = 0;
  int32_t lino = 0;

  if (TARRAY2_SIZE(fset->lvlArr) == 0) {  // to .tomb file
    *toStt = false;

    STFile file = {
        .type = TSDB_FTYPE_TOMB,
        .did = fset->farr[TSDB_FTYPE_HEAD]->f->did,
        .fid = fset->fid,
        .cid = 0,
        .size = 0,
    };

    code = tsdbTFileObjInit(tsdb, &file, fobj);
    TSDB_CHECK_CODE(code, lino, _exit);

    fset->farr[TSDB_FTYPE_TOMB] = *fobj;
  } else {  // to .stt file
    *toStt = true;
    SSttLvl *lvl = TARRAY2_GET(fset->lvlArr, 0);

    STFile file = {
        .type = TSDB_FTYPE_STT,
        .did = TARRAY2_GET(lvl->fobjArr, 0)->f->did,
        .fid = fset->fid,
        .cid = 0,
        .size = 0,
    };

    code = tsdbTFileObjInit(tsdb, &file, fobj);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(lvl->fobjArr, fobj[0]);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  char fname[TSDB_FILENAME_LEN] = {0};
  code = tsdbOpenFile(fobj[0]->fname, tsdb->pVnode->config.tsdbPageSize,
                      TD_FILE_READ | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_CREATE, fd);
  TSDB_CHECK_CODE(code, lino, _exit);

  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
  code = tsdbWriteFile(fd[0], 0, hdr, TSDB_FHDR_SIZE);
  TSDB_CHECK_CODE(code, lino, _exit);
  fobj[0]->f->size += TSDB_FHDR_SIZE;

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

H
Hongze Cheng 已提交
424 425 426 427
static int32_t tsdbDumpTombDataToFSet(STsdb *tsdb, SDelFReader *reader, SArray *aDelIdx, STFileSet *fset) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  struct {
    // context
    bool     toStt;
    int8_t   cmprAlg;
    int32_t  maxRow;
    int64_t  minKey;
    int64_t  maxKey;
    uint8_t *bufArr[8];
    // reader
    SArray *aDelData;
    // writer
    STsdbFD      *fd;
    STFileObj    *fobj;
    STombBlock    tombBlock[1];
    TTombBlkArray tombBlkArray[1];
    STombFooter   tombFooter[1];
    SSttFooter    sttFooter[1];
  } ctx[1] = {{
      .maxRow = tsdb->pVnode->config.tsdbCfg.maxRows,
      .cmprAlg = tsdb->pVnode->config.tsdbCfg.compression,
  }};
H
Hongze Cheng 已提交
449

H
Hongze Cheng 已提交
450
  tsdbFidKeyRange(fset->fid, tsdb->keepCfg.days, tsdb->keepCfg.precision, &ctx->minKey, &ctx->maxKey);
H
Hongze Cheng 已提交
451

H
Hongze Cheng 已提交
452
  if ((ctx->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
H
Hongze Cheng 已提交
453 454 455 456
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
457 458
  for (int32_t iDelIdx = 0; iDelIdx < taosArrayGetSize(aDelIdx); iDelIdx++) {
    SDelIdx *pDelIdx = (SDelIdx *)taosArrayGet(aDelIdx, iDelIdx);
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460
    code = tsdbReadDelData(reader, pDelIdx, ctx->aDelData);
H
Hongze Cheng 已提交
461 462
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
463 464
    for (int32_t iDelData = 0; iDelData < taosArrayGetSize(ctx->aDelData); iDelData++) {
      SDelData *pDelData = (SDelData *)taosArrayGet(ctx->aDelData, iDelData);
H
Hongze Cheng 已提交
465 466 467 468 469 470 471 472 473

      STombRecord record = {
          .suid = pDelIdx->suid,
          .uid = pDelIdx->uid,
          .version = pDelData->version,
          .skey = pDelData->sKey,
          .ekey = pDelData->eKey,
      };

H
Hongze Cheng 已提交
474
      code = tTombBlockPut(ctx->tombBlock, &record);
H
Hongze Cheng 已提交
475 476
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
477 478 479
      if (TOMB_BLOCK_SIZE(ctx->tombBlock) > ctx->maxRow) {
        if (ctx->fd == NULL) {
          code = tsdbUpgradeOpenTombFile(tsdb, fset, &ctx->fd, &ctx->fobj, &ctx->toStt);
H
Hongze Cheng 已提交
480 481
          TSDB_CHECK_CODE(code, lino, _exit);
        }
H
Hongze Cheng 已提交
482 483 484
        code = tsdbFileWriteTombBlock(ctx->fd, ctx->tombBlock, ctx->cmprAlg, &ctx->fobj->f->size, ctx->tombBlkArray,
                                      ctx->bufArr);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
485 486 487 488
      }
    }
  }

H
Hongze Cheng 已提交
489 490 491 492 493 494 495 496
  if (TOMB_BLOCK_SIZE(ctx->tombBlock) > 0) {
    if (ctx->fd == NULL) {
      code = tsdbUpgradeOpenTombFile(tsdb, fset, &ctx->fd, &ctx->fobj, &ctx->toStt);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
    code = tsdbFileWriteTombBlock(ctx->fd, ctx->tombBlock, ctx->cmprAlg, &ctx->fobj->f->size, ctx->tombBlkArray,
                                  ctx->bufArr);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
497 498
  }

H
Hongze Cheng 已提交
499 500 501 502
  if (ctx->fd != NULL) {
    if (ctx->toStt) {
      code = tsdbFileWriteTombBlk(ctx->fd, ctx->tombBlkArray, ctx->sttFooter->tombBlkPtr, &ctx->fobj->f->size);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
503

H
Hongze Cheng 已提交
504 505 506 507 508
      code = tsdbFileWriteSttFooter(ctx->fd, ctx->sttFooter, &ctx->fobj->f->size);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      code = tsdbFileWriteTombBlk(ctx->fd, ctx->tombBlkArray, ctx->tombFooter->tombBlkPtr, &ctx->fobj->f->size);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
509

H
Hongze Cheng 已提交
510 511 512 513 514
      code = tsdbFileWriteTombFooter(ctx->fd, ctx->tombFooter, &ctx->fobj->f->size);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tsdbFsyncFile(ctx->fd);
H
Hongze Cheng 已提交
515
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
516

H
Hongze Cheng 已提交
517 518
    tsdbCloseFile(&ctx->fd);
  }
H
Hongze Cheng 已提交
519 520 521 522 523

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
524 525 526 527 528 529
  for (int32_t i = 0; i < ARRAY_SIZE(ctx->bufArr); i++) {
    tFree(ctx->bufArr[i]);
  }
  TARRAY2_DESTROY(ctx->tombBlkArray, NULL);
  tTombBlockDestroy(ctx->tombBlock);
  taosArrayDestroy(ctx->aDelData);
H
Hongze Cheng 已提交
530 531 532
  return code;
}

H
Hongze Cheng 已提交
533 534 535 536
static int32_t tsdbUpgradeTombFile(STsdb *tsdb, SDelFile *pDelFile, TFileSetArray *fileSetArray) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  SDelFReader *reader = NULL;
  SArray      *aDelIdx = NULL;

  if ((aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = tsdbDelFReaderOpen(&reader, pDelFile, tsdb);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbReadDelIdx(reader, aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(aDelIdx) > 0) {
    STFileSet *fset;
    TARRAY2_FOREACH(fileSetArray, fset) {
      code = tsdbDumpTombDataToFSet(tsdb, reader, aDelIdx, fset);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

H
Hongze Cheng 已提交
559 560 561 562
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
563 564
  tsdbDelFReaderClose(&reader);
  taosArrayDestroy(aDelIdx);
H
Hongze Cheng 已提交
565 566 567
  return code;
}

H
Hongze Cheng 已提交
568
static int32_t tsdbDoUpgradeFileSystem(STsdb *tsdb, TFileSetArray *fileSetArray) {
H
Hongze Cheng 已提交
569 570 571
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
572
  // upgrade each file set
H
Hongze Cheng 已提交
573
  for (int32_t i = 0; i < taosArrayGetSize(tsdb->fs.aDFileSet); i++) {
H
Hongze Cheng 已提交
574
    code = tsdbUpgradeFileSet(tsdb, taosArrayGet(tsdb->fs.aDFileSet, i), fileSetArray);
H
Hongze Cheng 已提交
575 576 577
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
578
  // upgrade tomb file
H
Hongze Cheng 已提交
579 580 581 582 583
  if (tsdb->fs.pDelFile != NULL) {
    code = tsdbUpgradeTombFile(tsdb, tsdb->fs.pDelFile, fileSetArray);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbUpgradeFileSystem(STsdb *tsdb, int8_t rollback) {
  int32_t code = 0;
  int32_t lino = 0;

  TFileSetArray fileSetArray[1] = {0};

  // open old file system
  code = tsdbFSOpen(tsdb, rollback);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbDoUpgradeFileSystem(tsdb, fileSetArray);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
604
  // close file system
H
Hongze Cheng 已提交
605 606 607 608 609 610
  code = tsdbFSClose(tsdb);
  TSDB_CHECK_CODE(code, lino, _exit);

  // save new file system
  char fname[TSDB_FILENAME_LEN];
  current_fname(tsdb, fname, TSDB_FCURRENT);
H
Hongze Cheng 已提交
611
  code = save_fs(fileSetArray, fname);
H
Hongze Cheng 已提交
612 613 614 615 616 617
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
618
  TARRAY2_DESTROY(fileSetArray, tsdbTFileSetClear);
H
Hongze Cheng 已提交
619 620 621 622 623 624 625 626 627
  return code;
}

int32_t tsdbCheckAndUpgradeFileSystem(STsdb *tsdb, int8_t rollback) {
  char fname[TSDB_FILENAME_LEN];

  tsdbGetCurrentFName(tsdb, fname, NULL);
  if (!taosCheckExistFile(fname)) return 0;

H
Hongze Cheng 已提交
628
  int32_t code = tsdbUpgradeFileSystem(tsdb, rollback);
H
Hongze Cheng 已提交
629 630 631 632 633
  if (code) return code;

  taosRemoveFile(fname);
  return 0;
}