tsdbCompact.c 27.3 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
  SMapData      mDataBlk;   // SMapData<SDataBlk>
  SArray       *aSttBlk;    // SArray<SSttBlk>
H
Hongze Cheng 已提交
87
  TABLEID       tableId;
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

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);
H
Hongze Cheng 已提交
533 534
    } else {
      version = pCompactor->sKey.version;
H
Hongze Cheng 已提交
535 536 537 538 539 540 541 542 543 544 545 546
    }

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

  return false;
}

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

H
Hongze Cheng 已提交
551 552 553
  TABLEID  excludeTableId;
  TABLEID *pExcludeTableId = NULL;

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

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

H
Hongze Cheng 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
      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 已提交
585

H
Hongze Cheng 已提交
586 587 588 589 590 591 592
          // 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 已提交
593

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

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

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

H
Hongze Cheng 已提交
617 618 619 620 621 622 623 624
        // 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 已提交
625

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

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

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

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

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

H
Hongze Cheng 已提交
662
static int32_t tsdbCompactGetRow(STsdbCompactor *pCompactor, SRowInfo **ppRowInfo, STSchema **ppTSchema) {
H
Hongze Cheng 已提交
663 664 665 666 667 668 669 670 671
  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 已提交
672 673
    ASSERT(pCompactor->pIter->rowInfo.suid == pCompactor->tbSkm.suid);
    ASSERT(pCompactor->pIter->rowInfo.uid == pCompactor->tbSkm.uid);
H
Hongze Cheng 已提交
674
    *ppRowInfo = &pCompactor->pIter->rowInfo;
H
Hongze Cheng 已提交
675
    *ppTSchema = pCompactor->tbSkm.pTSchema;
H
Hongze Cheng 已提交
676
  } else {
H
Hongze Cheng 已提交
677 678
    *ppRowInfo = NULL;
    *ppTSchema = NULL;
H
Hongze Cheng 已提交
679 680
  }

H
Hongze Cheng 已提交
681 682 683 684
_exit:
  return code;
}

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

H
Hongze Cheng 已提交
689
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
690

H
Hongze Cheng 已提交
691
  // reader
H
Hongze Cheng 已提交
692
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
H
Hongze Cheng 已提交
693 694
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
695 696
  // open iters
  STsdbDataIter *pIter;
H
Hongze Cheng 已提交
697

H
Hongze Cheng 已提交
698 699
  pCompactor->iterList = NULL;
  tRBTreeCreate(&pCompactor->rtree, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
700

H
Hongze Cheng 已提交
701 702 703 704 705 706 707 708 709 710 711
  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 已提交
712 713
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
714 715 716 717 718 719
    if (pIter) {
      pIter->next = pCompactor->iterList;
      pCompactor->iterList = pIter;
      tRBTreePut(&pCompactor->rtree, &pIter->n);
    }
  }
H
Hongze Cheng 已提交
720
  pCompactor->pIter = NULL;
H
Hongze Cheng 已提交
721
  tBlockDataReset(&pCompactor->bData);
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723 724 725 726 727 728 729 730 731
  // 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 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  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 已提交
756 757 758 759 760 761 762 763
_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 已提交
764

H
Hongze Cheng 已提交
765 766 767 768 769 770
static void tsdbCloseCompactor(STsdbCompactor *pCompactor) {
  for (STsdbDataIter *pIter = pCompactor->iterList; pIter;) {
    STsdbDataIter *pIterNext = pIter->next;
    tsdbDataIterClose(pIter);
    pIter = pIterNext;
  }
H
Hongze Cheng 已提交
771

H
Hongze Cheng 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  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 已提交
800

H
Hongze Cheng 已提交
801
_exit:
H
Hongze Cheng 已提交
802 803 804 805 806 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
  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 已提交
835
}
H
Hongze Cheng 已提交
836

H
Hongze Cheng 已提交
837
static int32_t tsdbCompactFileSet(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
838 839 840
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
841 842 843
  // open compactor
  code = tsdbOpenCompactor(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
844

H
Hongze Cheng 已提交
845 846 847 848
  // do compact
  SRowInfo *pRowInfo;
  STSchema *pTSchema;
  int64_t   nRow = 0;
H
Hongze Cheng 已提交
849

H
Hongze Cheng 已提交
850
  code = tsdbCompactGetRow(pCompactor, &pRowInfo, &pTSchema);
H
Hongze Cheng 已提交
851 852
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
853
  if (pRowInfo && (code = tBlockDataInit(&pCompactor->bData, (TABLEID *)pRowInfo, pTSchema, NULL, 0))) {
H
Hongze Cheng 已提交
854
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
855
  }
H
Hongze Cheng 已提交
856

H
Hongze Cheng 已提交
857
  while (pRowInfo) {
H
Hongze Cheng 已提交
858
    // write block data according to table id if necessary (TODO)
H
Hongze Cheng 已提交
859
    if ((pCompactor->tableId.suid != pRowInfo->suid) ||
H
Hongze Cheng 已提交
860
        (pCompactor->tableId.uid != pRowInfo->uid &&
H
Hongze Cheng 已提交
861
         (pRowInfo->suid == 0 || (pCompactor->bData.uid && pCompactor->bData.nRow >= pCompactor->minRows)))) {
H
Hongze Cheng 已提交
862 863
      code = tsdbCompactWriteBlockData(pCompactor);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
864

H
Hongze Cheng 已提交
865 866
      code = tsdbCompactWriteDataBlk(pCompactor);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
867

H
Hongze Cheng 已提交
868
      code = tBlockDataInit(&pCompactor->bData, (TABLEID *)pRowInfo, pTSchema, NULL, 0);
H
Hongze Cheng 已提交
869 870 871
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
872
    // check if append/merge the row causes nRow exceed maxRows (TODO)
H
Hongze Cheng 已提交
873
    if (0 /* add the row causes row exceed maxRows */) {
H
Hongze Cheng 已提交
874
      code = tsdbCompactWriteBlockData(pCompactor);
H
Hongze Cheng 已提交
875
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
876
    }
H
Hongze Cheng 已提交
877

H
Hongze Cheng 已提交
878 879 880
    // append/merge the row
    pCompactor->tableId.suid = pRowInfo->suid;
    pCompactor->tableId.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
881 882
    code = tBlockDataUpsertRow(&pCompactor->bData, &pRowInfo->row, pTSchema, pRowInfo->uid);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
883 884

    // iter to the next row
H
Hongze Cheng 已提交
885 886
    code = tsdbCompactNextRow(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888 889 890
    code = tsdbCompactGetRow(pCompactor, &pRowInfo, &pTSchema);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
891

H
Hongze Cheng 已提交
892 893 894
  // handle remain data
  code = tsdbCompactWriteBlockData(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
895

H
Hongze Cheng 已提交
896 897
  code = tsdbCompactWriteDataBlk(pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
898

H
Hongze Cheng 已提交
899 900
  code = tsdbWriteBlockIdx(pCompactor->pWriter, pCompactor->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
901

H
Hongze Cheng 已提交
902 903
  code = tsdbWriteSttBlk(pCompactor->pWriter, pCompactor->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
904

H
Hongze Cheng 已提交
905 906
  code = tsdbUpdateDFileSetHeader(pCompactor->pWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
907

H
Hongze Cheng 已提交
908 909
  code = tsdbFSUpsertFSet(&pCompactor->fs, &pCompactor->pWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
910

H
Hongze Cheng 已提交
911 912
  code = tsdbDataFWriterClose(&pCompactor->pWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
913

H
Hongze Cheng 已提交
914 915 916 917 918
_exit:
  // close compactor
  tsdbCloseCompactor(pCompactor);
  return code;
}
H
Hongze Cheng 已提交
919

H
Hongze Cheng 已提交
920 921 922 923 924
int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938
  // begin compact
  code = tsdbBeginCompact(pTsdb, pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);

  // do compact each file set
  while (true) {
    pCompactor->pDFileSet = (SDFileSet *)taosArraySearch(pCompactor->fs.aDFileSet, &(SDFileSet){.fid = pCompactor->fid},
                                                         tDFileSetCmprFn, TD_GT);
    if (pCompactor->pDFileSet == NULL) break;

    pCompactor->fid = pCompactor->pDFileSet->fid;
    code = tsdbCompactFileSet(pCompactor);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
939 940
  }

H
Hongze Cheng 已提交
941 942 943
  code = tsdbFSUpsertDelFile(&pCompactor->fs, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
944
_exit:
H
Hongze Cheng 已提交
945
  // commit/abort compact
H
Hongze Cheng 已提交
946 947
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
948
    tsdbAbortCompact(pCompactor);
H
Hongze Cheng 已提交
949
  } else {
H
Hongze Cheng 已提交
950
    tsdbCommitCompact(pCompactor);
H
Hongze Cheng 已提交
951
  }
H
Hongze Cheng 已提交
952
  tsdbEndCompact(pCompactor);
H
Hongze Cheng 已提交
953 954
  return code;
}