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

H
Hongze Cheng 已提交
18 19 20
extern int32_t tsdbUpdateTableSchema(SMeta *pMeta, int64_t suid, int64_t uid, SSkmInfo *pSkmInfo);
extern int32_t tsdbWriteDataBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SMapData *mDataBlk, int8_t cmprAlg);
extern int32_t tsdbWriteSttBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SArray *aSttBlk, int8_t cmprAlg);
H
Hongze Cheng 已提交
21 22

typedef struct {
H
Hongze Cheng 已提交
23 24 25 26 27
  STsdb  *pTsdb;
  int64_t commitID;
  int8_t  cmprAlg;
  int32_t maxRows;
  int32_t minRows;
H
Hongze Cheng 已提交
28

H
Hongze Cheng 已提交
29
  STsdbFS fs;
H
Hongze Cheng 已提交
30

H
Hongze Cheng 已提交
31 32 33
  int32_t  fid;
  TABLEID  tbid;
  SSkmInfo tbSkm;
H
Hongze Cheng 已提交
34

H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43
  // Tombstone
  SDelFReader *pDelFReader;
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
  SArray      *aSkyLine;  // SArray<TSDBKEY>
  int32_t      iDelIdx;
  int32_t      iSkyLine;
  TSDBKEY     *pDKey;
  TSDBKEY      dKey;
H
Hongze Cheng 已提交
44

H
Hongze Cheng 已提交
45 46 47 48 49
  // Reader
  SDataFReader   *pReader;
  STsdbDataIter2 *iterList;  // list of iterators
  STsdbDataIter2 *pIter;
  SRBTree         rbt;
H
Hongze Cheng 已提交
50

H
Hongze Cheng 已提交
51 52 53 54 55 56 57 58
  // Writer
  SDataFWriter *pWriter;
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
  SMapData      mDataBlk;   // SMapData<SDataBlk>
  SArray       *aSttBlk;    // SArray<SSttBlk>
  SBlockData    bData;
  SBlockData    sData;
} STsdbCompactor;
H
Hongze Cheng 已提交
59

H
Hongze Cheng 已提交
60
static int32_t tsdbCommitCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
61 62 63
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
64 65
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
66 67 68 69 70 71 72 73 74 75 76 77
  code = tsdbFSPrepareCommit(pTsdb, &pCompactor->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

  taosThreadRwlockWrlock(&pTsdb->rwLock);

  code = tsdbFSCommit(pTsdb);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
78 79 80 81 82 83 84 85

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
86
static int32_t tsdbAbortCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
87 88 89
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
90 91
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
92
  // TODO
H
Hongze Cheng 已提交
93
  ASSERT(0);
H
Hongze Cheng 已提交
94 95 96 97

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
98 99
  } else {
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
100 101 102 103
  }
  return code;
}

H
Hongze Cheng 已提交
104
static int32_t tsdbCompactWriteTableDataStart(STsdbCompactor *pCompactor, TABLEID *pId) {
H
Hongze Cheng 已提交
105 106 107
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
108 109
  pCompactor->tbid = *pId;

H
Hongze Cheng 已提交
110 111 112
  // tombstone
  for (;;) {
    if (pCompactor->iDelIdx >= taosArrayGetSize(pCompactor->aDelIdx)) {
H
Hongze Cheng 已提交
113
      pCompactor->pDKey = NULL;
H
Hongze Cheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      break;
    }

    SDelIdx *pDelIdx = (SDelIdx *)taosArrayGet(pCompactor->aDelIdx, pCompactor->iDelIdx);
    int32_t  c = tTABLEIDCmprFn(pDelIdx, &pCompactor->tbid);
    if (c < 0) {
      pCompactor->iDelIdx++;
    } else if (c == 0) {
      pCompactor->iDelIdx++;

      code = tsdbReadDelData(pCompactor->pDelFReader, pDelIdx, pCompactor->aDelData);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbBuildDeleteSkyline(pCompactor->aDelData, 0, taosArrayGetSize(pCompactor->aDelData) - 1,
                                    pCompactor->aSkyLine);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
130

H
Hongze Cheng 已提交
131
      pCompactor->iSkyLine = 0;
H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140
      if (pCompactor->iSkyLine < taosArrayGetSize(pCompactor->aSkyLine)) {
        TSDBKEY *pKey = (TSDBKEY *)taosArrayGet(pCompactor->aSkyLine, pCompactor->iSkyLine);

        pCompactor->dKey.version = 0;
        pCompactor->dKey.ts = pKey->ts;
        pCompactor->pDKey = &pCompactor->dKey;
      } else {
        pCompactor->pDKey = NULL;
      }
H
Hongze Cheng 已提交
141 142
      break;
    } else {
H
Hongze Cheng 已提交
143
      pCompactor->pDKey = NULL;
H
Hongze Cheng 已提交
144 145 146 147 148
      break;
    }
  }

  // reader and write (TODO)
H
Hongze Cheng 已提交
149
  code = tsdbUpdateTableSchema(pCompactor->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pCompactor->tbSkm);
H
Hongze Cheng 已提交
150
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
151

H
Hongze Cheng 已提交
152
  tMapDataReset(&pCompactor->mDataBlk);
H
Hongze Cheng 已提交
153

H
Hongze Cheng 已提交
154
  code = tBlockDataInit(&pCompactor->bData, pId, pCompactor->tbSkm.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
155 156
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
157 158 159 160 161 162 163
  if (!TABLE_SAME_SCHEMA(pCompactor->tbid.suid, pCompactor->tbid.uid, pId->suid, pId->uid)) {
    if (pCompactor->sData.nRow > 0) {
      code = tsdbWriteSttBlock(pCompactor->pWriter, &pCompactor->sData, pCompactor->aSttBlk, pCompactor->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tBlockDataInit(&pCompactor->sData, pId /* TODO */, pCompactor->tbSkm.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
164
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
165
  }
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167 168 169 170 171 172 173 174 175 176
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pCompactor->pTsdb->pVnode), __func__, pId->suid,
              pId->uid);
  }
  return code;
}
H
Hongze Cheng 已提交
177

H
Hongze Cheng 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static int32_t tsdbCompactWriteTableDataEnd(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pCompactor->bData.nRow > 0) {
    if (pCompactor->bData.nRow < pCompactor->minRows) {
      for (int32_t iRow = 0; iRow < pCompactor->bData.nRow; iRow++) {
        code = tBlockDataAppendRow(&pCompactor->sData, &tsdbRowFromBlockData(&pCompactor->bData, iRow), NULL,
                                   pCompactor->tbid.uid);
        TSDB_CHECK_CODE(code, lino, _exit);

        if (pCompactor->sData.nRow >= pCompactor->maxRows) {
          code = tsdbWriteSttBlock(pCompactor->pWriter, &pCompactor->sData, pCompactor->aSttBlk, pCompactor->cmprAlg);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      }
H
Hongze Cheng 已提交
194

H
Hongze Cheng 已提交
195 196 197
      tBlockDataClear(&pCompactor->bData);
    } else {
      code = tsdbWriteDataBlock(pCompactor->pWriter, &pCompactor->bData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
H
Hongze Cheng 已提交
198
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
199 200
    }
  }
H
Hongze Cheng 已提交
201

H
Hongze Cheng 已提交
202 203 204 205 206
  if (pCompactor->mDataBlk.nItem) {
    SBlockIdx *pBlockIdx = (SBlockIdx *)taosArrayReserve(pCompactor->aBlockIdx, 1);
    if (pBlockIdx == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
207 208
    }

H
Hongze Cheng 已提交
209 210
    pBlockIdx->suid = pCompactor->tbid.suid;
    pBlockIdx->uid = pCompactor->tbid.uid;
H
Hongze Cheng 已提交
211

H
Hongze Cheng 已提交
212 213 214
    code = tsdbWriteDataBlk(pCompactor->pWriter, &pCompactor->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
215

H
Hongze Cheng 已提交
216 217 218 219 220 221 222 223 224 225
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pCompactor->pTsdb->pVnode), __func__,
              pCompactor->tbid.suid, pCompactor->tbid.uid);
  }
  return code;
}
H
Hongze Cheng 已提交
226

H
Hongze Cheng 已提交
227
static bool tsdbCompactRowIsDeleted(STsdbCompactor *pCompactor, TSDBROW *pRow) {
H
Hongze Cheng 已提交
228 229 230
  TSDBKEY  tKey = TSDBROW_KEY(pRow);
  TSDBKEY *aKey = (TSDBKEY *)TARRAY_DATA(pCompactor->aSkyLine);
  int32_t  nKey = TARRAY_SIZE(pCompactor->aSkyLine);
H
Hongze Cheng 已提交
231 232

  if (tKey.ts > pCompactor->pDKey->ts) {
H
Hongze Cheng 已提交
233 234
    do {
      pCompactor->pDKey->version = aKey[pCompactor->iSkyLine].version;
H
Hongze Cheng 已提交
235
      pCompactor->iSkyLine++;
H
Hongze Cheng 已提交
236 237
      if (pCompactor->iSkyLine < nKey) {
        pCompactor->dKey.ts = aKey[pCompactor->iSkyLine].ts;
H
Hongze Cheng 已提交
238
      } else {
H
Hongze Cheng 已提交
239 240 241 242 243 244
        if (pCompactor->pDKey->version == 0) {
          pCompactor->pDKey = NULL;
          return false;
        } else {
          pCompactor->pDKey->ts = INT64_MAX;
        }
H
Hongze Cheng 已提交
245
      }
H
Hongze Cheng 已提交
246
    } while (tKey.ts > pCompactor->pDKey->ts);
H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255
  }

  if (tKey.ts < pCompactor->pDKey->ts) {
    if (tKey.version > pCompactor->pDKey->version) {
      return false;
    } else {
      return true;
    }
  } else if (tKey.ts == pCompactor->pDKey->ts) {
H
Hongze Cheng 已提交
256 257
    ASSERT(pCompactor->iSkyLine < nKey);
    if (tKey.version > TMAX(pCompactor->pDKey->version, aKey[pCompactor->iSkyLine].version)) {
H
Hongze Cheng 已提交
258 259 260 261 262
      return false;
    } else {
      return true;
    }
  }
H
Hongze Cheng 已提交
263 264 265 266

  return false;
}

H
Hongze Cheng 已提交
267 268 269
static int32_t tsdbCompactWriteTableData(STsdbCompactor *pCompactor, SRowInfo *pRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
270

H
Hongze Cheng 已提交
271 272 273 274 275 276 277 278
  SRowInfo rInfo;
  if (pRowInfo == NULL) {
    rInfo.suid = INT64_MAX;
    rInfo.uid = INT64_MAX;
    // rInfo.row = TSDBORW_V;
    pRowInfo = &rInfo;
  }

H
Hongze Cheng 已提交
279
  // start a new table data write if need
H
Hongze Cheng 已提交
280 281 282
  if (pRowInfo->uid != pCompactor->tbid.uid) {
    if (pCompactor->tbid.uid) {
      code = tsdbCompactWriteTableDataEnd(pCompactor);
H
Hongze Cheng 已提交
283
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
284
    }
H
Hongze Cheng 已提交
285

H
Hongze Cheng 已提交
286
    code = tsdbCompactWriteTableDataStart(pCompactor, (TABLEID *)pRowInfo);
H
Hongze Cheng 已提交
287
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
288
  }
H
Hongze Cheng 已提交
289

H
Hongze Cheng 已提交
290
  // check if row is deleted
H
Hongze Cheng 已提交
291
  if (pCompactor->pDKey && tsdbCompactRowIsDeleted(pCompactor, &pRowInfo->row)) goto _exit;
H
Hongze Cheng 已提交
292 293

  code = tBlockDataUpsertRow(&pCompactor->bData, &pRowInfo->row, NULL, pRowInfo->uid);
H
Hongze Cheng 已提交
294 295 296 297
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pCompactor->bData.nRow >= pCompactor->maxRows) {
    code = tsdbWriteDataBlock(pCompactor->pWriter, &pCompactor->bData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
H
Hongze Cheng 已提交
298
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  } else if (pRowInfo) {
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64 " ts:%" PRId64 " version:%" PRId64,
              TD_VID(pCompactor->pTsdb->pVnode), __func__, pRowInfo->suid, pRowInfo->uid, TSDBROW_TS(&pRowInfo->row),
              TSDBROW_VERSION(&pRowInfo->row));
  }
  return code;
}
H
Hongze Cheng 已提交
312

H
Hongze Cheng 已提交
313 314 315 316 317 318
static int32_t tsdbCompactNextRow(STsdbCompactor *pCompactor, SRowInfo **ppRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pCompactor->pIter) {
    code = tsdbDataIterNext2(pCompactor->pIter, NULL /* TODO */);
H
Hongze Cheng 已提交
319
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

    if (pCompactor->pIter->rowInfo.suid == 0 && pCompactor->pIter->rowInfo.uid == 0) {
      pCompactor->pIter = NULL;
    } else {
      SRBTreeNode *pNode = tRBTreeMin(&pCompactor->rbt);
      if (pNode) {
        int32_t c = tsdbDataIterCmprFn(&pCompactor->pIter->rbtn, pNode);
        if (c > 0) {
          tRBTreePut(&pCompactor->rbt, &pCompactor->pIter->rbtn);
          pCompactor->pIter = NULL;
        } else if (c == 0) {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
335
  }
H
Hongze Cheng 已提交
336

H
Hongze Cheng 已提交
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
  if (pCompactor->pIter == NULL) {
    SRBTreeNode *pNode = tRBTreeMin(&pCompactor->rbt);
    if (pNode) {
      tRBTreeDrop(&pCompactor->rbt, pNode);
      pCompactor->pIter = TSDB_RBTN_TO_DATA_ITER(pNode);
    }
  }

  if (ppRowInfo) {
    if (pCompactor->pIter) {
      *ppRowInfo = &pCompactor->pIter->rowInfo;
    } else {
      *ppRowInfo = NULL;
    }
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  }
  return code;
}

static int32_t tsdbCompactFileSetStart(STsdbCompactor *pCompactor, SDFileSet *pSet) {
  int32_t code = 0;
  int32_t lino = 0;

  pCompactor->fid = pSet->fid;
  pCompactor->tbid = (TABLEID){0};

H
Hongze Cheng 已提交
368 369 370 371
  /* tombstone */
  pCompactor->iDelIdx = 0;
  pCompactor->iSkyLine = 0;

H
Hongze Cheng 已提交
372 373
  /* reader */
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pCompactor->pTsdb, pSet);
H
Hongze Cheng 已提交
374
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
375

H
Hongze Cheng 已提交
376
  code = tsdbOpenDataFileDataIter(pCompactor->pReader, &pCompactor->pIter);
H
Hongze Cheng 已提交
377
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
378

H
Hongze Cheng 已提交
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
  if (pCompactor->pIter) {
    pCompactor->pIter->next = pCompactor->iterList;
    pCompactor->iterList = pCompactor->pIter;
  }

  for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    code = tsdbOpenSttFileDataIter(pCompactor->pReader, iStt, &pCompactor->pIter);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (pCompactor->pIter) {
      pCompactor->pIter->next = pCompactor->iterList;
      pCompactor->iterList = pCompactor->pIter;
    }
  }

  pCompactor->pIter = NULL;
  tRBTreeCreate(&pCompactor->rbt, tsdbDataIterCmprFn);

  /* writer */
  code = tsdbDataFWriterOpen(&pCompactor->pWriter, pCompactor->pTsdb,
                             &(SDFileSet){.fid = pCompactor->fid,
                                          .diskId = pSet->diskId,
                                          .pHeadF = &(SHeadFile){.commitID = pCompactor->commitID},
                                          .pDataF = &(SDataFile){.commitID = pCompactor->commitID},
                                          .pSmaF = &(SSmaFile){.commitID = pCompactor->commitID},
                                          .nSttF = 1,
                                          .aSttF = {&(SSttFile){.commitID = pCompactor->commitID}}});
H
Hongze Cheng 已提交
406
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
407

H
Hongze Cheng 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  if (pCompactor->aBlockIdx) {
    taosArrayClear(pCompactor->aBlockIdx);
  } else if ((pCompactor->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  tMapDataReset(&pCompactor->mDataBlk);

  if (pCompactor->aSttBlk) {
    taosArrayClear(pCompactor->aSttBlk);
  } else if ((pCompactor->aSttBlk = taosArrayInit(0, sizeof(SSttBlk))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  tBlockDataReset(&pCompactor->bData);
  tBlockDataReset(&pCompactor->sData);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, fid:%d", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code), pCompactor->fid);
  } else {
    tsdbInfo("vgId:%d %s done, fid:%d", TD_VID(pCompactor->pTsdb->pVnode), __func__, pCompactor->fid);
  }
  return code;
}

static int32_t tsdbCompactFileSetEnd(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  /* finish remaining data (TODO) */

  /* update files */
H
Hongze Cheng 已提交
444 445
  code = tsdbWriteSttBlk(pCompactor->pWriter, pCompactor->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
446

H
Hongze Cheng 已提交
447 448 449
  code = tsdbWriteBlockIdx(pCompactor->pWriter, pCompactor->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
450 451
  code = tsdbUpdateDFileSetHeader(pCompactor->pWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453 454
  code = tsdbFSUpsertFSet(&pCompactor->fs, &pCompactor->pWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
455

H
Hongze Cheng 已提交
456 457
  code = tsdbDataFWriterClose(&pCompactor->pWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
458

H
Hongze Cheng 已提交
459 460 461 462 463 464 465 466 467 468 469 470
  code = tsdbDataFReaderClose(&pCompactor->pReader);
  TSDB_CHECK_CODE(code, lino, _exit);

  /* do clear */
  while ((pCompactor->pIter = pCompactor->iterList) != NULL) {
    pCompactor->iterList = pCompactor->pIter->next;
    tsdbCloseDataIter2(pCompactor->pIter);
  }

  tBlockDataReset(&pCompactor->bData);
  tBlockDataReset(&pCompactor->sData);

H
Hongze Cheng 已提交
471
_exit:
H
Hongze Cheng 已提交
472 473 474 475 476 477
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, fid:%d", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code), pCompactor->fid);
  } else {
    tsdbInfo("vgId:%d %s done, fid:%d", TD_VID(pCompactor->pTsdb->pVnode), __func__, pCompactor->fid);
  }
H
Hongze Cheng 已提交
478 479
  return code;
}
H
Hongze Cheng 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

static int32_t tsdbCompactFileSet(STsdbCompactor *pCompactor, SDFileSet *pSet) {
  int32_t code = 0;
  int32_t lino = 0;

  // start compact
  code = tsdbCompactFileSetStart(pCompactor, pSet);
  TSDB_CHECK_CODE(code, lino, _exit);

  // do compact
  SRowInfo *pRowInfo;
  for (;;) {
    code = tsdbCompactNextRow(pCompactor, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbCompactWriteTableData(pCompactor, pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (pRowInfo == NULL) break;
  }

  // end compact
  code = tsdbCompactFileSetEnd(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  return code;
}

H
Hongze Cheng 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
static void tsdbEndCompact(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  // writer
  tBlockDataDestroy(&pCompactor->sData);
  tBlockDataDestroy(&pCompactor->bData);
  taosArrayDestroy(pCompactor->aSttBlk);
  tMapDataClear(&pCompactor->mDataBlk);
  taosArrayDestroy(pCompactor->aBlockIdx);

  // reader

  // tombstone
  taosArrayDestroy(pCompactor->aSkyLine);
  taosArrayDestroy(pCompactor->aDelData);
  taosArrayDestroy(pCompactor->aDelIdx);

  // others
  tDestroyTSchema(pCompactor->tbSkm.pTSchema);
  tsdbFSDestroy(&pCompactor->fs);

  tsdbInfo("vgId:%d %s done, commit ID:%" PRId64, TD_VID(pCompactor->pTsdb->pVnode), __func__, pCompactor->commitID);
}

H
Hongze Cheng 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547
static int32_t tsdbBeginCompact(STsdb *pTsdb, STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  pCompactor->pTsdb = pTsdb;
  pCompactor->commitID = 0;  // TODO
  pCompactor->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
  pCompactor->maxRows = pTsdb->pVnode->config.tsdbCfg.maxRows;
  pCompactor->minRows = pTsdb->pVnode->config.tsdbCfg.minRows;
  pCompactor->fid = INT32_MIN;

  code = tsdbFSCopy(pTsdb, &pCompactor->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
548
  /* tombstone */
H
Hongze Cheng 已提交
549 550 551 552
  if (pCompactor->fs.pDelFile) {
    code = tsdbDelFReaderOpen(&pCompactor->pDelFReader, pCompactor->fs.pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
553
    if ((pCompactor->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
554 555 556 557
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
558
    if ((pCompactor->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
H
Hongze Cheng 已提交
559 560 561 562
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
563
    if ((pCompactor->aSkyLine = taosArrayInit(0, sizeof(TSDBKEY))) == NULL) {
H
Hongze Cheng 已提交
564 565 566 567 568 569 570
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tsdbReadDelIdx(pCompactor->pDelFReader, pCompactor->aDelIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
571

H
Hongze Cheng 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584
  /* reader */

  /* writer */
  code = tBlockDataCreate(&pCompactor->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tBlockDataCreate(&pCompactor->sData);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, commit ID:%" PRId64, TD_VID(pTsdb->pVnode), __func__, lino,
              tstrerror(code), pCompactor->commitID);
H
Hongze Cheng 已提交
585 586 587 588 589 590 591 592 593
    tBlockDataDestroy(&pCompactor->sData);
    tBlockDataDestroy(&pCompactor->bData);
    if (pCompactor->fs.pDelFile) {
      taosArrayDestroy(pCompactor->aSkyLine);
      taosArrayDestroy(pCompactor->aDelData);
      taosArrayDestroy(pCompactor->aDelIdx);
      if (pCompactor->pDelFReader) tsdbDelFReaderClose(&pCompactor->pDelFReader);
    }
    tsdbFSDestroy(&pCompactor->fs);
H
Hongze Cheng 已提交
594 595 596 597 598 599
  } else {
    tsdbInfo("vgId:%d %s done, commit ID:%" PRId64, TD_VID(pTsdb->pVnode), __func__, pCompactor->commitID);
  }
  return code;
}

H
Hongze Cheng 已提交
600 601 602 603
int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;

  STsdbCompactor *pCompactor = &(STsdbCompactor){0};
H
Hongze Cheng 已提交
604

H
Hongze Cheng 已提交
605
  if ((code = tsdbBeginCompact(pTsdb, pCompactor))) return code;
H
Hongze Cheng 已提交
606

H
Hongze Cheng 已提交
607
  for (;;) {
H
Hongze Cheng 已提交
608 609 610 611 612 613
    SDFileSet *pSet = (SDFileSet *)taosArraySearch(pCompactor->fs.aDFileSet, &(SDFileSet){.fid = pCompactor->fid},
                                                   tDFileSetCmprFn, TD_GT);
    if (pSet == NULL) {
      pCompactor->fid = INT32_MAX;
      break;
    }
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615
    if ((code = tsdbCompactFileSet(pCompactor, pSet))) goto _exit;
H
Hongze Cheng 已提交
616 617
  }

H
Hongze Cheng 已提交
618
  if ((code = tsdbFSUpsertDelFile(&pCompactor->fs, NULL))) goto _exit;
H
Hongze Cheng 已提交
619

H
Hongze Cheng 已提交
620
_exit:
H
Hongze Cheng 已提交
621 622 623 624 625
  if (code) {
    tsdbAbortCompact(pCompactor);
  } else {
    tsdbCommitCompact(pCompactor);
  }
H
Hongze Cheng 已提交
626
  tsdbEndCompact(pCompactor);
H
Hongze Cheng 已提交
627 628
  return code;
}