tsdbCompact.c 27.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 已提交
18 19 20 21
#define TSDB_ITER_TYPE_MEM 0x0
#define TSDB_ITER_TYPE_DAT 0x1
#define TSDB_ITER_TYPE_STT 0x2

H
Hongze Cheng 已提交
22 23
typedef struct {
} SMemDIter;
H
Hongze Cheng 已提交
24 25

typedef struct {
H
Hongze Cheng 已提交
26 27 28 29 30 31 32
  SDataFReader *pReader;
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
  SMapData      mDataBlk;   // SMapData<SDataBlk>
  SBlockData    bData;
  int32_t       iBlockIdx;
  int32_t       iDataBlk;
  int32_t       iRow;
H
Hongze Cheng 已提交
33 34 35
} SDataDIter;

typedef struct {
H
Hongze Cheng 已提交
36 37 38 39 40 41
  SDataFReader *pReader;
  int32_t       iStt;
  SArray       *aSttBlk;  // SArray<SSttBlk>
  SBlockData    bData;
  int32_t       iSttBlk;
  int32_t       iRow;
H
Hongze Cheng 已提交
42 43
} SSttDIter;

H
Hongze Cheng 已提交
44 45 46
typedef struct STsdbDataIter {
  struct STsdbDataIter *next;

H
Hongze Cheng 已提交
47 48 49 50
  int32_t     flag;
  SRowInfo    rowInfo;
  SRBTreeNode n;
  char        handle[];
H
Hongze Cheng 已提交
51 52
} STsdbDataIter;

H
Hongze Cheng 已提交
53 54
#define TSDB_DATA_ITER_FROM_RBTN(N) ((STsdbDataIter *)((char *)N - offsetof(STsdbDataIter, n)))

H
Hongze Cheng 已提交
55
typedef struct {
H
Hongze Cheng 已提交
56 57
  STsdb     *pTsdb;
  int64_t    cid;
H
Hongze Cheng 已提交
58
  int8_t     cmprAlg;
H
Hongze Cheng 已提交
59 60 61 62 63 64
  int32_t    maxRows;
  int32_t    minRows;
  STsdbFS    fs;
  int32_t    fid;
  SDFileSet *pDFileSet;

H
Hongze Cheng 已提交
65 66 67 68 69 70
  // Tombstone
  SDelFReader *pDelFReader;
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
  SArray      *aSkyLine;  // SArray<TSDBKEY>
  TSDBKEY     *aTSDBKEY;
H
Hongze Cheng 已提交
71 72
  int32_t      iKey;
  TSDBKEY      sKey;
H
Hongze Cheng 已提交
73

H
Hongze Cheng 已提交
74
  // Reader
H
Hongze Cheng 已提交
75 76 77
  SDataFReader  *pReader;
  STsdbDataIter *iterList;  // list of iterators
  SRBTree        rtree;
H
Hongze Cheng 已提交
78
  STsdbDataIter *pIter;
H
Hongze Cheng 已提交
79
  SBlockData     bData;
H
Hongze Cheng 已提交
80
  SSkmInfo       tbSkm;
H
Hongze Cheng 已提交
81 82 83 84

  // Writer
  SDataFWriter *pWriter;
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
85 86 87
  TABLEID       tableId;
  SMapData      mDataBlk;  // SMapData<SDataBlk>
  SArray       *aSttBlk;   // SArray<SSttBlk>
H
Hongze Cheng 已提交
88 89
} STsdbCompactor;

H
Hongze Cheng 已提交
90 91
#define TSDB_FLG_DEEP_COMPACT 0x1

H
Hongze Cheng 已提交
92
// ITER =========================
H
Hongze Cheng 已提交
93
static int32_t tsdbDataIterNext(STsdbDataIter *pIter, TABLEID *pExcludeTableId);
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95 96 97 98 99 100 101
static int32_t tsdbDataIterCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
  const STsdbDataIter *pIter1 = (STsdbDataIter *)((char *)n1 - offsetof(STsdbDataIter, n));
  const STsdbDataIter *pIter2 = (STsdbDataIter *)((char *)n2 - offsetof(STsdbDataIter, n));

  return tRowInfoCmprFn(&pIter1->rowInfo, &pIter2->rowInfo);
}

H
Hongze Cheng 已提交
102
static int32_t tsdbMemDIterOpen(STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
103 104
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
105 106 107 108 109 110 111

  STsdbDataIter *pIter = (STsdbDataIter *)taosMemoryCalloc(1, sizeof(*pIter) + sizeof(SMemDIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
112
  // TODO
H
Hongze Cheng 已提交
113

H
Hongze Cheng 已提交
114
_exit:
H
Hongze Cheng 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  if (code) {
    *ppIter = NULL;
  } else {
    *ppIter = pIter;
  }
  return code;
}

static int32_t tsdbDataDIterOpen(SDataFReader *pReader, STsdbDataIter **ppIter) {
  int32_t code = 0;
  int32_t lino = 0;

  STsdbDataIter *pIter = (STsdbDataIter *)taosMemoryCalloc(1, sizeof(*pIter) + sizeof(SDataDIter));
  if (NULL == pIter) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
132
  pIter->flag = TSDB_ITER_TYPE_DAT;
H
Hongze Cheng 已提交
133

H
Hongze Cheng 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146
  SDataDIter *pDataDIter = (SDataDIter *)pIter->handle;
  pDataDIter->pReader = pReader;
  pDataDIter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pDataDIter->aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  code = tsdbReadBlockIdx(pReader, pDataDIter->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pDataDIter->aBlockIdx) == 0) goto _clear_exit;

H
Hongze Cheng 已提交
147
  // TODO
H
Hongze Cheng 已提交
148 149 150
  code = tBlockDataCreate(&pDataDIter->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
151
  pDataDIter->iBlockIdx = -1;
H
Hongze Cheng 已提交
152 153 154
  pDataDIter->iDataBlk = 0;
  pDataDIter->iRow = 0;

H
Hongze Cheng 已提交
155
  code = tsdbDataIterNext(pIter, NULL);
H
Hongze Cheng 已提交
156
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
157 158 159

_exit:
  if (code) {
H
Hongze Cheng 已提交
160
  _clear_exit:
H
Hongze Cheng 已提交
161
    *ppIter = NULL;
H
Hongze Cheng 已提交
162
    if (pIter) {
H
Hongze Cheng 已提交
163
      tBlockDataDestroy(&pDataDIter->bData);
H
Hongze Cheng 已提交
164 165 166 167
      tMapDataClear(&pDataDIter->mDataBlk);
      taosArrayDestroy(pDataDIter->aBlockIdx);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
168 169 170 171 172 173
  } else {
    *ppIter = pIter;
  }
  return code;
}

H
Hongze Cheng 已提交
174
static int32_t tsdbSttDIterOpen(SDataFReader *pReader, int32_t iStt, STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
175 176 177 178 179 180 181 182
  int32_t code = 0;
  int32_t lino = 0;

  STsdbDataIter *pIter = (STsdbDataIter *)taosMemoryCalloc(1, sizeof(*pIter) + sizeof(SSttDIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
183
  pIter->flag = TSDB_ITER_TYPE_STT;
H
Hongze Cheng 已提交
184

H
Hongze Cheng 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  SSttDIter *pSttDIter = (SSttDIter *)pIter->handle;
  pSttDIter->pReader = pReader;
  pSttDIter->iStt = iStt;
  pSttDIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pSttDIter->aSttBlk == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  code = tsdbReadSttBlk(pReader, pSttDIter->iStt, pSttDIter->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pSttDIter->aSttBlk) == 0) goto _clear_exit;

  code = tBlockDataCreate(&pSttDIter->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
202 203
  pSttDIter->iSttBlk = -1;
  pSttDIter->iRow = -1;
H
Hongze Cheng 已提交
204

H
Hongze Cheng 已提交
205
  code = tsdbDataIterNext(pIter, NULL);
H
Hongze Cheng 已提交
206
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
207 208 209

_exit:
  if (code) {
H
Hongze Cheng 已提交
210
  _clear_exit:
H
Hongze Cheng 已提交
211
    *ppIter = NULL;
H
Hongze Cheng 已提交
212
    if (pIter) {
H
Hongze Cheng 已提交
213
      tBlockDataDestroy(&pSttDIter->bData);
H
Hongze Cheng 已提交
214 215 216
      taosArrayDestroy(pSttDIter->aSttBlk);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
217 218 219
  } else {
    *ppIter = pIter;
  }
H
Hongze Cheng 已提交
220 221 222 223
  return code;
}

static void tsdbDataIterClose(STsdbDataIter *pIter) {
H
Hongze Cheng 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  if (pIter == NULL) return;

  if (pIter->flag & TSDB_ITER_TYPE_MEM) {
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_DAT) {
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_STT) {
    SSttDIter *pSttDIter = (SSttDIter *)pIter->handle;

    tBlockDataDestroy(&pSttDIter->bData);
    taosArrayDestroy(pSttDIter->aSttBlk);
  } else {
    ASSERT(0);
  }

  taosMemoryFree(pIter);
H
Hongze Cheng 已提交
240 241
}

H
Hongze Cheng 已提交
242
static int32_t tsdbDataIterNext(STsdbDataIter *pIter, TABLEID *pExcludeTableId) {
H
Hongze Cheng 已提交
243 244
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
245 246 247 248 249 250 251 252 253 254

  if (pIter->flag & TSDB_ITER_TYPE_MEM) {
    // TODO
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_DAT) {
    // TODO
    ASSERT(0);
  } else if (pIter->flag & TSDB_ITER_TYPE_STT) {
    SSttDIter *pSttDIter = (SSttDIter *)pIter->handle;

H
Hongze Cheng 已提交
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
    for (;;) {
      if (++pSttDIter->iRow >= pSttDIter->bData.nRow) {
        for (;;) {
          if (++pSttDIter->iSttBlk < taosArrayGetSize(pSttDIter->aSttBlk)) {
            SSttBlk *pSttBlk = (SSttBlk *)taosArrayGet(pSttDIter->aSttBlk, pSttDIter->iSttBlk);

            // check exclusion
            if (pExcludeTableId) {
              if (pExcludeTableId->uid) {  // exclude (suid, uid)
                if (pSttBlk->minUid == pExcludeTableId->uid && pSttBlk->maxUid == pExcludeTableId->uid) continue;
              } else {  // exclude (suid, *)
                if (pSttBlk->suid == pExcludeTableId->suid) continue;
              }
            }

            code = tsdbReadSttBlockEx(pSttDIter->pReader, pSttDIter->iStt, pSttBlk, &pSttDIter->bData);
            TSDB_CHECK_CODE(code, lino, _exit);

            pIter->rowInfo.suid = pSttBlk->suid;
            pSttDIter->iRow = 0;
            break;
          } else {
            // iter end, all set 0 and exit
            pIter->rowInfo.suid = 0;
            pIter->rowInfo.uid = 0;
            goto _exit;
          }
        }
      }

H
Hongze Cheng 已提交
285 286
      pIter->rowInfo.uid = pSttDIter->bData.uid ? pSttDIter->bData.uid : pSttDIter->bData.aUid[pSttDIter->iRow];
      pIter->rowInfo.row = tsdbRowFromBlockData(&pSttDIter->bData, pSttDIter->iRow);
H
Hongze Cheng 已提交
287 288 289 290 291 292 293 294

      // check exclusion
      if (pExcludeTableId) {
        if (pExcludeTableId->uid) {  // exclude (suid, uid)
          if (pIter->rowInfo.uid == pExcludeTableId->uid) continue;
        } else {  // exclude (suid, *)
          if (pIter->rowInfo.suid == pExcludeTableId->suid) continue;
        }
H
Hongze Cheng 已提交
295
      }
H
Hongze Cheng 已提交
296 297

      break;
H
Hongze Cheng 已提交
298 299 300 301 302
    }
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
303 304 305
_exit:
  return code;
}
H
Hongze Cheng 已提交
306 307

// COMPACT =========================
H
Hongze Cheng 已提交
308
static int32_t tsdbBeginCompact(STsdb *pTsdb, STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
309 310 311
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
312
  pCompactor->pTsdb = pTsdb;
H
Hongze Cheng 已提交
313 314
  pCompactor->cid = 0;  // TODO
  pCompactor->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
315 316
  pCompactor->maxRows = pTsdb->pVnode->config.tsdbCfg.maxRows;
  pCompactor->minRows = pTsdb->pVnode->config.tsdbCfg.minRows;
H
Hongze Cheng 已提交
317 318 319 320 321 322

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

  pCompactor->fid = INT32_MIN;

H
Hongze Cheng 已提交
323 324 325
  code = tBlockDataCreate(&pCompactor->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
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
  // tombstone
  if (pCompactor->fs.pDelFile) {
    code = tsdbDelFReaderOpen(&pCompactor->pDelFReader, pCompactor->fs.pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

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

    pCompactor->aDelData = taosArrayInit(0, sizeof(SDelData));
    if (pCompactor->aDelData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    pCompactor->aSkyLine = taosArrayInit(0, sizeof(TSDBKEY));
    if (pCompactor->aSkyLine == NULL) {
      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 已提交
353 354 355 356 357 358 359
_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 已提交
360
static void tsdbEndCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
361 362 363 364
  taosArrayDestroy(pCompactor->aSkyLine);
  taosArrayDestroy(pCompactor->aDelData);
  taosArrayDestroy(pCompactor->aDelIdx);
  tsdbDelFReaderClose(&pCompactor->pDelFReader);
H
Hongze Cheng 已提交
365 366 367 368 369 370 371 372 373
  tsdbFSDestroy(&pCompactor->fs);
  tBlockDataDestroy(&pCompactor->bData);
  tDestroyTSchema(pCompactor->tbSkm.pTSchema);
  pCompactor->tbSkm.pTSchema = NULL;
  taosArrayDestroy(pCompactor->aBlockIdx);
  tMapDataClear(&pCompactor->mDataBlk);
  taosArrayDestroy(pCompactor->aSttBlk);
}

H
Hongze Cheng 已提交
374
static int32_t tsdbCommitCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
375 376 377
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
378 379
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
380 381 382 383 384 385 386 387 388 389 390 391
  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 已提交
392 393 394 395 396 397 398 399

_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 已提交
400
static int32_t tsdbAbortCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
401 402 403
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
404 405
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
406 407 408 409 410 411 412 413 414
  // TODO

_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 已提交
415
static int32_t tsdbDeepCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
416 417 418
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
419
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
420

H
Hongze Cheng 已提交
421 422 423
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
424 425 426 427 428 429 430
_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 已提交
431
static int32_t tsdbShallowCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
432
  int32_t code = 0;
H
Hongze Cheng 已提交
433 434
  int32_t lino = 0;

H
Hongze Cheng 已提交
435 436
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
437 438 439 440 441 442 443
_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 已提交
444
static int32_t tsdbCompactNextRowImpl(STsdbCompactor *pCompactor, TABLEID *pExcludeTableId) {
H
Hongze Cheng 已提交
445 446
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448 449 450 451
  for (;;) {
    if (pCompactor->pIter) {
      code = tsdbDataIterNext(pCompactor->pIter, pExcludeTableId);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453 454 455 456 457 458
      if (pCompactor->pIter->rowInfo.suid == 0 && pCompactor->pIter->rowInfo.uid == 0) {
        pCompactor->pIter = NULL;
      } else {
        SRBTreeNode *pNode = tRBTreeMin(&pCompactor->rtree);
        if (pNode) {
          STsdbDataIter *pIter = TSDB_DATA_ITER_FROM_RBTN(pNode);
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460 461
          int32_t c = tRowInfoCmprFn(&pCompactor->pIter->rowInfo, &pIter->rowInfo);
          ASSERT(c);
H
Hongze Cheng 已提交
462

H
Hongze Cheng 已提交
463 464 465 466
          if (c > 0) {
            tRBTreePut(&pCompactor->rtree, &pCompactor->pIter->n);
            pCompactor->pIter = NULL;
          }
H
Hongze Cheng 已提交
467 468 469
        }
      }
    }
H
Hongze Cheng 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

    if (pCompactor->pIter == NULL) {
      SRBTreeNode *pNode = tRBTreeMin(&pCompactor->rtree);
      if (pNode) {
        pCompactor->pIter = TSDB_DATA_ITER_FROM_RBTN(pNode);
        tRBTreeDrop(&pCompactor->rtree, pNode);

        if (pExcludeTableId) {
          if (pExcludeTableId->uid) {
            if (pCompactor->pIter->rowInfo.uid == pExcludeTableId->uid) continue;
          } else {
            if (pCompactor->pIter->rowInfo.suid == pExcludeTableId->suid) continue;
          }
        }
      }
    }

    break;
H
Hongze Cheng 已提交
488 489
  }

H
Hongze Cheng 已提交
490 491 492 493
_exit:
  return code;
}

H
Hongze Cheng 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
static int32_t tDelIdxCmprFn(const SDelIdx *pDelIdx1, const SDelIdx *pDelIdx2) {
  if (pDelIdx1->suid < pDelIdx2->suid) {
    return -1;
  } else if (pDelIdx1->suid > pDelIdx2->suid) {
    return 1;
  }

  if (pDelIdx1->uid < pDelIdx2->uid) {
    return -1;
  } else if (pDelIdx1->uid > pDelIdx2->uid) {
    return 1;
  }

  return 0;
}
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 534 535 536 537 538 539 540 541 542 543 544

static bool tsdbCompactRowIsDeleted(STsdbCompactor *pCompactor, TSDBROW *pRow) {
  TSDBKEY tKey = TSDBROW_KEY(pRow);

  while (tKey.ts > pCompactor->sKey.ts) {
    pCompactor->sKey.version = pCompactor->aTSDBKEY[pCompactor->iKey].version;
    pCompactor->iKey++;
    if (pCompactor->iKey < taosArrayGetSize(pCompactor->aSkyLine)) {
      pCompactor->sKey.ts = pCompactor->aTSDBKEY[pCompactor->iKey].ts;
    } else {
      pCompactor->sKey.ts = TSKEY_MAX;
    }
  }

  if (tKey.ts < pCompactor->sKey.ts) {
    if (tKey.version > pCompactor->sKey.version) {
      return false;
    } else {
      return true;
    }
  } else if (tKey.ts == pCompactor->sKey.ts) {
    int64_t version;
    if (pCompactor->iKey < taosArrayGetSize(pCompactor->aSkyLine)) {
      version = TMAX(pCompactor->sKey.version, pCompactor->aTSDBKEY[pCompactor->iKey].version);
    }

    if (tKey.version > version) {
      return false;
    } else {
      return true;
    }
  }

  return false;
}

H
Hongze Cheng 已提交
545 546 547 548
static int32_t tsdbCompactNextRow(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
549 550 551
  TABLEID  excludeTableId;
  TABLEID *pExcludeTableId = NULL;

H
Hongze Cheng 已提交
552
  for (;;) {
H
Hongze Cheng 已提交
553
    code = tsdbCompactNextRowImpl(pCompactor, pExcludeTableId);
H
Hongze Cheng 已提交
554 555 556 557
    TSDB_CHECK_CODE(code, lino, _exit);

    // check if the table of the row exists
    if (pCompactor->pIter) {
H
Hongze Cheng 已提交
558 559
      SRowInfo *pRowInfo = &pCompactor->pIter->rowInfo;

H
Hongze Cheng 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
      if (pRowInfo->uid != pCompactor->tbSkm.uid) {
        SMetaInfo info;
        if (pRowInfo->suid) {  // child table

          // check if super table exists
          if (pRowInfo->suid != pCompactor->tbSkm.suid) {
            if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
              excludeTableId.suid = pRowInfo->suid;
              excludeTableId.uid = 0;
              pExcludeTableId = &excludeTableId;
              continue;
            }

            // super table exists
            pCompactor->tbSkm.suid = pRowInfo->suid;
            pCompactor->tbSkm.uid = 0;
            tDestroyTSchema(pCompactor->tbSkm.pTSchema);
            pCompactor->tbSkm.pTSchema = metaGetTbTSchema(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->suid, -1, 1);
            if (pCompactor->tbSkm.pTSchema == NULL) {
              code = TSDB_CODE_OUT_OF_MEMORY;
              TSDB_CHECK_CODE(code, lino, _exit);
            }
          }
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584 585 586 587 588 589 590
          // check if table exists
          if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
            excludeTableId.suid = pRowInfo->suid;
            excludeTableId.uid = pRowInfo->uid;
            pExcludeTableId = &excludeTableId;
            continue;
          }
H
Hongze Cheng 已提交
591

H
Hongze Cheng 已提交
592 593 594 595
          // table exists
          pCompactor->tbSkm.uid = pRowInfo->uid;
        } else {  // normal table
          // check if table exists
H
Hongze Cheng 已提交
596 597
          if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
            excludeTableId.suid = pRowInfo->suid;
H
Hongze Cheng 已提交
598
            excludeTableId.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
599 600 601 602
            pExcludeTableId = &excludeTableId;
            continue;
          }

H
Hongze Cheng 已提交
603
          // table exists
H
Hongze Cheng 已提交
604
          pCompactor->tbSkm.suid = pRowInfo->suid;
H
Hongze Cheng 已提交
605
          pCompactor->tbSkm.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
606
          tDestroyTSchema(pCompactor->tbSkm.pTSchema);
H
Hongze Cheng 已提交
607

H
Hongze Cheng 已提交
608 609
          pCompactor->tbSkm.pTSchema = metaGetTbTSchema(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->suid, -1, 1);
          if (pCompactor->tbSkm.pTSchema == NULL) {
H
Hongze Cheng 已提交
610 611 612
            code = TSDB_CODE_OUT_OF_MEMORY;
            TSDB_CHECK_CODE(code, lino, _exit);
          }
H
Hongze Cheng 已提交
613
        }
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615 616 617 618 619 620 621 622
        // load delData and build the skyline
        if (pCompactor->pDelFReader) {
          SDelIdx *pDelIdx =
              taosArraySearch(pCompactor->aDelIdx, &(SDelIdx){.suid = pRowInfo->suid, .uid = pRowInfo->uid},
                              (__compar_fn_t)tDelIdxCmprFn, TD_EQ);
          if (pDelIdx) {
            code = tsdbReadDelData(pCompactor->pDelFReader, pDelIdx, pCompactor->aDelData);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
623

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

H
Hongze Cheng 已提交
628
            pCompactor->aTSDBKEY = (TSDBKEY *)TARRAY_DATA(pCompactor->aSkyLine);
H
Hongze Cheng 已提交
629 630 631
            pCompactor->iKey = 0;
            pCompactor->sKey.version = 0;
            pCompactor->sKey.ts = pCompactor->aTSDBKEY[0].ts;
H
Hongze Cheng 已提交
632 633 634
          } else {
            pCompactor->aTSDBKEY = NULL;
          }
H
Hongze Cheng 已提交
635 636
        }
      }
H
Hongze Cheng 已提交
637

H
Hongze Cheng 已提交
638
      ASSERT(pRowInfo->uid == pCompactor->tbSkm.uid);
H
Hongze Cheng 已提交
639

H
Hongze Cheng 已提交
640 641
      // check if the row is deleted
      if (pCompactor->aTSDBKEY && tsdbCompactRowIsDeleted(pCompactor, &pRowInfo->row)) {
H
Hongze Cheng 已提交
642 643 644 645
        continue;
      } else {
        break;
      }
H
Hongze Cheng 已提交
646 647 648
    } else {
      // iter end, just break out
      break;
H
Hongze Cheng 已提交
649 650 651 652
    }
  }

_exit:
H
Hongze Cheng 已提交
653 654 655 656
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  }
H
Hongze Cheng 已提交
657 658 659
  return code;
}

H
Hongze Cheng 已提交
660
static int32_t tsdbCompactGetRow(STsdbCompactor *pCompactor, SRowInfo **ppRowInfo, STSchema **ppTSchema) {
H
Hongze Cheng 已提交
661 662 663 664 665 666 667 668 669
  int32_t code = 0;
  int32_t lino = 0;

  if (pCompactor->pIter == NULL) {
    code = tsdbCompactNextRow(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (pCompactor->pIter) {
H
Hongze Cheng 已提交
670 671
    ASSERT(pCompactor->pIter->rowInfo.suid == pCompactor->tbSkm.suid);
    ASSERT(pCompactor->pIter->rowInfo.uid == pCompactor->tbSkm.uid);
H
Hongze Cheng 已提交
672
    *ppRowInfo = &pCompactor->pIter->rowInfo;
H
Hongze Cheng 已提交
673
    *ppTSchema = pCompactor->tbSkm.pTSchema;
H
Hongze Cheng 已提交
674
  } else {
H
Hongze Cheng 已提交
675 676
    *ppRowInfo = NULL;
    *ppTSchema = NULL;
H
Hongze Cheng 已提交
677 678
  }

H
Hongze Cheng 已提交
679 680 681 682
_exit:
  return code;
}

H
Hongze Cheng 已提交
683
static int32_t tsdbOpenCompactor(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
684 685 686
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
687
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
688

H
Hongze Cheng 已提交
689 690 691 692
  // next compact file
  pCompactor->pDFileSet = (SDFileSet *)taosArraySearch(pCompactor->fs.aDFileSet, &(SDFileSet){.fid = pCompactor->fid},
                                                       tDFileSetCmprFn, TD_GT);
  if (pCompactor->pDFileSet == NULL) goto _exit;
H
Hongze Cheng 已提交
693

H
Hongze Cheng 已提交
694 695
  pCompactor->fid = pCompactor->pDFileSet->fid;

H
Hongze Cheng 已提交
696
  // reader
H
Hongze Cheng 已提交
697
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
H
Hongze Cheng 已提交
698 699
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
700 701
  // open iters
  STsdbDataIter *pIter;
H
Hongze Cheng 已提交
702

H
Hongze Cheng 已提交
703 704
  pCompactor->iterList = NULL;
  tRBTreeCreate(&pCompactor->rtree, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
705

H
Hongze Cheng 已提交
706 707 708 709 710 711 712 713 714 715 716
  code = tsdbDataDIterOpen(pCompactor->pReader, &pIter);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pIter) {
    pIter->next = pCompactor->iterList;
    pCompactor->iterList = pIter;
    tRBTreePut(&pCompactor->rtree, &pIter->n);
  }

  for (int32_t iStt = 0; iStt < pCompactor->pReader->pSet->nSttF; iStt++) {
    code = tsdbSttDIterOpen(pCompactor->pReader, iStt, &pIter);
H
Hongze Cheng 已提交
717 718
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
719 720 721 722 723 724
    if (pIter) {
      pIter->next = pCompactor->iterList;
      pCompactor->iterList = pIter;
      tRBTreePut(&pCompactor->rtree, &pIter->n);
    }
  }
H
Hongze Cheng 已提交
725
  pCompactor->pIter = NULL;
H
Hongze Cheng 已提交
726
  tBlockDataReset(&pCompactor->bData);
H
Hongze Cheng 已提交
727

H
Hongze Cheng 已提交
728 729 730 731 732 733 734 735 736
  // writer
  SDFileSet wSet = {.diskId = (SDiskID){0},  // TODO
                    .fid = pCompactor->pDFileSet->fid,
                    .pHeadF = &(SHeadFile){.commitID = pCompactor->cid},
                    .pDataF = &(SDataFile){.commitID = pCompactor->cid},
                    .pSmaF = &(SSmaFile){.commitID = pCompactor->cid},
                    .nSttF = 1,
                    .aSttF = {&(SSttFile){.commitID = pCompactor->cid}}};
  code = tsdbDataFWriterOpen(&pCompactor->pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
  TSDB_CHECK_CODE(code, lino, _exit);

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

  tMapDataReset(&pCompactor->mDataBlk);

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

H
Hongze Cheng 已提交
761 762 763 764 765 766 767 768
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
  return code;
}
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770 771 772 773 774 775
static void tsdbCloseCompactor(STsdbCompactor *pCompactor) {
  for (STsdbDataIter *pIter = pCompactor->iterList; pIter;) {
    STsdbDataIter *pIterNext = pIter->next;
    tsdbDataIterClose(pIter);
    pIter = pIterNext;
  }
H
Hongze Cheng 已提交
776

H
Hongze Cheng 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
  tDestroyTSchema(pCompactor->tbSkm.pTSchema);
  pCompactor->tbSkm.pTSchema = NULL;

  tsdbDataFReaderClose(&pCompactor->pReader);
}

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);
static int32_t tsdbCompactWriteBlockData(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  SBlockData *pBData = &pCompactor->bData;

  if (pBData->nRow == 0) goto _exit;

  if (pBData->uid && pBData->nRow >= pCompactor->minRows) {  // write to .data file
    code = tsdbWriteDataBlock(pCompactor->pWriter, pBData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);

    pCompactor->tableId.suid = pBData->suid;
    pCompactor->tableId.uid = pBData->uid;
  } else {  // write to .stt file
    code = tsdbWriteSttBlock(pCompactor->pWriter, pBData, pCompactor->aSttBlk, pCompactor->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  tBlockDataClear(&pCompactor->bData);
H
Hongze Cheng 已提交
805

H
Hongze Cheng 已提交
806
_exit:
H
Hongze Cheng 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  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 tsdbCompactWriteDataBlk(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pCompactor->mDataBlk.nItem == 0) goto _exit;

  SBlockIdx *pBlockIdx = (SBlockIdx *)taosArrayReserve(pCompactor->aBlockIdx, 1);
  if (pBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pBlockIdx->suid = pCompactor->tableId.suid;
  pBlockIdx->uid = pCompactor->tableId.uid;

  code = tsdbWriteDataBlk(pCompactor->pWriter, &pCompactor->mDataBlk, pBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  tMapDataReset(&pCompactor->mDataBlk);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCompactor->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
  }
  return code;
H
Hongze Cheng 已提交
840
}
H
Hongze Cheng 已提交
841

H
Hongze Cheng 已提交
842 843 844 845 846 847 848
int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;
  int32_t lino = 0;

  // Check if can do compact (TODO)

  // Do compact
H
Hongze Cheng 已提交
849
  STsdbCompactor *pCompactor = &(STsdbCompactor){0};
H
Hongze Cheng 已提交
850

H
Hongze Cheng 已提交
851
  code = tsdbBeginCompact(pTsdb, pCompactor);
H
Hongze Cheng 已提交
852 853 854
  TSDB_CHECK_CODE(code, lino, _exit);

  while (true) {
H
Hongze Cheng 已提交
855
    code = tsdbOpenCompactor(pCompactor);
H
Hongze Cheng 已提交
856 857
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
858
    if (pCompactor->pDFileSet == NULL) break;
H
Hongze Cheng 已提交
859

H
Hongze Cheng 已提交
860
    // loop to merge row by row
H
Hongze Cheng 已提交
861 862 863
    SRowInfo *pRowInfo = NULL;
    STSchema *pTSchema = NULL;
    int64_t   nRow = 0;
H
Hongze Cheng 已提交
864
    for (;;) {
H
Hongze Cheng 已提交
865
      code = tsdbCompactGetRow(pCompactor, &pRowInfo, &pTSchema);
H
Hongze Cheng 已提交
866
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
867

H
Hongze Cheng 已提交
868
      if (pRowInfo == NULL) break;
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870 871
      nRow++;

H
Hongze Cheng 已提交
872 873 874 875 876
      if (pCompactor->bData.suid == 0 && pCompactor->bData.uid == 0) {  // init the block data if not initialized yet
        code = tBlockDataInit(&pCompactor->bData, &(TABLEID){.suid = pRowInfo->suid, .uid = pRowInfo->uid}, pTSchema,
                              NULL, 0);
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
H
Hongze Cheng 已提交
877 878 879 880 881
        if ((pCompactor->tableId.suid != pRowInfo->suid) ||  // different super table
            (pCompactor->tableId.uid != pRowInfo->uid &&
             (pRowInfo->suid == 0 ||
              pCompactor->bData.uid && pCompactor->bData.nRow >= pCompactor->minRows))  // different table
        ) {
H
Hongze Cheng 已提交
882 883 884 885 886
          code = tsdbCompactWriteBlockData(pCompactor);
          TSDB_CHECK_CODE(code, lino, _exit);

          code = tsdbCompactWriteDataBlk(pCompactor);
          TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
887 888 889 890 891 892 893 894

          code = tBlockDataInit(&pCompactor->bData, &(TABLEID){.suid = pRowInfo->suid, .uid = pRowInfo->uid}, pTSchema,
                                NULL, 0);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      }

      // append row to block data
H
Hongze Cheng 已提交
895
      code = tBlockDataAppendRowEx(&pCompactor->bData, &pRowInfo->row, pTSchema, pRowInfo->uid);
H
Hongze Cheng 已提交
896 897
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
898 899 900
      pCompactor->tableId.suid = pRowInfo->suid;
      pCompactor->tableId.uid = pRowInfo->uid;

H
Hongze Cheng 已提交
901 902
      // check if block data is full
      if (pCompactor->bData.nRow >= pCompactor->maxRows) {
H
Hongze Cheng 已提交
903 904
        code = tsdbCompactWriteBlockData(pCompactor);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
905
      }
H
Hongze Cheng 已提交
906

H
Hongze Cheng 已提交
907 908 909
      // iterate to next row
      code = tsdbCompactNextRow(pCompactor);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
910
    }
H
Hongze Cheng 已提交
911

H
Hongze Cheng 已提交
912 913 914 915 916 917 918 919
    code = tsdbCompactWriteBlockData(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbCompactWriteDataBlk(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbWriteBlockIdx(pCompactor->pWriter, pCompactor->aBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
920 921 922 923

    code = tsdbWriteSttBlk(pCompactor->pWriter, pCompactor->aSttBlk);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
924 925
    code = tsdbUpdateDFileSetHeader(pCompactor->pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
926

H
Hongze Cheng 已提交
927 928
    code = tsdbFSUpsertFSet(&pCompactor->fs, &pCompactor->pWriter->wSet);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
929

H
Hongze Cheng 已提交
930
    code = tsdbDataFWriterClose(&pCompactor->pWriter, 1);
H
Hongze Cheng 已提交
931 932
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
933
    tsdbCloseCompactor(pCompactor);
H
Hongze Cheng 已提交
934 935
  }

H
Hongze Cheng 已提交
936 937 938
  code = tsdbFSUpsertDelFile(&pCompactor->fs, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
939 940 941
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
942
    tsdbAbortCompact(pCompactor);
H
Hongze Cheng 已提交
943
  } else {
H
Hongze Cheng 已提交
944
    tsdbCommitCompact(pCompactor);
H
Hongze Cheng 已提交
945
  }
H
Hongze Cheng 已提交
946
  tsdbEndCompact(pCompactor);
H
Hongze Cheng 已提交
947 948
  return code;
}