tsdbCompact.c 37.1 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
#if 0
H
Hongze Cheng 已提交
19

H
Hongze Cheng 已提交
20 21 22 23
#define TSDB_ITER_TYPE_MEM 0x0
#define TSDB_ITER_TYPE_DAT 0x1
#define TSDB_ITER_TYPE_STT 0x2

H
Hongze Cheng 已提交
24 25
typedef struct {
} SMemDIter;
H
Hongze Cheng 已提交
26 27

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

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

H
Hongze Cheng 已提交
46 47 48
typedef struct STsdbDataIter {
  struct STsdbDataIter *next;

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

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

H
Hongze Cheng 已提交
57 58
#define TSDB_FLG_DEEP_COMPACT 0x1

H
Hongze Cheng 已提交
59
// ITER =========================
H
Hongze Cheng 已提交
60
static int32_t tsdbDataIterNext(STsdbDataIter *pIter, TABLEID *pExcludeTableId);
H
Hongze Cheng 已提交
61

H
Hongze Cheng 已提交
62 63 64 65 66 67 68
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 已提交
69
static int32_t tsdbMemDIterOpen(STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
70 71
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
72 73 74 75 76 77 78

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

H
Hongze Cheng 已提交
79
  // TODO
H
Hongze Cheng 已提交
80

H
Hongze Cheng 已提交
81
_exit:
H
Hongze Cheng 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  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 已提交
99
  pIter->flag = TSDB_ITER_TYPE_DAT;
H
Hongze Cheng 已提交
100

H
Hongze Cheng 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
  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 已提交
114
  // TODO
H
Hongze Cheng 已提交
115 116 117
  code = tBlockDataCreate(&pDataDIter->bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
118
  pDataDIter->iBlockIdx = -1;
H
Hongze Cheng 已提交
119 120 121
  pDataDIter->iDataBlk = 0;
  pDataDIter->iRow = 0;

H
Hongze Cheng 已提交
122
  code = tsdbDataIterNext(pIter, NULL);
H
Hongze Cheng 已提交
123
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
124 125 126

_exit:
  if (code) {
H
Hongze Cheng 已提交
127
  _clear_exit:
H
Hongze Cheng 已提交
128
    *ppIter = NULL;
H
Hongze Cheng 已提交
129
    if (pIter) {
H
Hongze Cheng 已提交
130
      tBlockDataDestroy(&pDataDIter->bData);
H
Hongze Cheng 已提交
131 132 133 134
      tMapDataClear(&pDataDIter->mDataBlk);
      taosArrayDestroy(pDataDIter->aBlockIdx);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
135 136 137 138 139 140
  } else {
    *ppIter = pIter;
  }
  return code;
}

H
Hongze Cheng 已提交
141
static int32_t tsdbSttDIterOpen(SDataFReader *pReader, int32_t iStt, STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
142 143 144 145 146 147 148 149
  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 已提交
150
  pIter->flag = TSDB_ITER_TYPE_STT;
H
Hongze Cheng 已提交
151

H
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  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 已提交
169 170
  pSttDIter->iSttBlk = -1;
  pSttDIter->iRow = -1;
H
Hongze Cheng 已提交
171

H
Hongze Cheng 已提交
172
  code = tsdbDataIterNext(pIter, NULL);
H
Hongze Cheng 已提交
173
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
174 175 176

_exit:
  if (code) {
H
Hongze Cheng 已提交
177
  _clear_exit:
H
Hongze Cheng 已提交
178
    *ppIter = NULL;
H
Hongze Cheng 已提交
179
    if (pIter) {
H
Hongze Cheng 已提交
180
      tBlockDataDestroy(&pSttDIter->bData);
H
Hongze Cheng 已提交
181 182 183
      taosArrayDestroy(pSttDIter->aSttBlk);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
184 185 186
  } else {
    *ppIter = pIter;
  }
H
Hongze Cheng 已提交
187 188 189 190
  return code;
}

static void tsdbDataIterClose(STsdbDataIter *pIter) {
H
Hongze Cheng 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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 已提交
207 208
}

H
Hongze Cheng 已提交
209
static int32_t tsdbDataIterNext(STsdbDataIter *pIter, TABLEID *pExcludeTableId) {
H
Hongze Cheng 已提交
210 211
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
212 213 214 215 216 217 218 219 220 221

  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 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    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 已提交
252 253
      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 已提交
254 255 256 257 258 259 260 261

      // 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 已提交
262
      }
H
Hongze Cheng 已提交
263 264

      break;
H
Hongze Cheng 已提交
265 266 267 268 269
    }
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
270 271 272
_exit:
  return code;
}
H
Hongze Cheng 已提交
273 274

// COMPACT =========================
H
Hongze Cheng 已提交
275

H
Hongze Cheng 已提交
276
static int32_t tsdbCommitCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
277 278 279
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
280 281
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
282 283 284 285 286 287 288 289 290 291 292 293
  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 已提交
294 295 296 297 298 299 300 301

_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 已提交
302
static int32_t tsdbAbortCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
303 304 305
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
306 307
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
308 309 310 311 312 313 314 315 316
  // 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 已提交
317
static int32_t tsdbDeepCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
318 319 320
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
321
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
322

H
Hongze Cheng 已提交
323 324 325
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
326 327 328 329 330 331 332
_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 已提交
333
static int32_t tsdbShallowCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
334
  int32_t code = 0;
H
Hongze Cheng 已提交
335 336
  int32_t lino = 0;

H
Hongze Cheng 已提交
337 338
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
339 340 341 342 343 344 345
_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 已提交
346
static int32_t tsdbCompactNextRowImpl(STsdbCompactor *pCompactor, TABLEID *pExcludeTableId) {
H
Hongze Cheng 已提交
347 348
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350 351 352 353
  for (;;) {
    if (pCompactor->pIter) {
      code = tsdbDataIterNext(pCompactor->pIter, pExcludeTableId);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
354

H
Hongze Cheng 已提交
355 356 357 358 359 360
      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 已提交
361

H
Hongze Cheng 已提交
362 363
          int32_t c = tRowInfoCmprFn(&pCompactor->pIter->rowInfo, &pIter->rowInfo);
          ASSERT(c);
H
Hongze Cheng 已提交
364

H
Hongze Cheng 已提交
365 366 367 368
          if (c > 0) {
            tRBTreePut(&pCompactor->rtree, &pCompactor->pIter->n);
            pCompactor->pIter = NULL;
          }
H
Hongze Cheng 已提交
369 370 371
        }
      }
    }
H
Hongze Cheng 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

    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 已提交
390 391
  }

H
Hongze Cheng 已提交
392 393 394 395
_exit:
  return code;
}

H
Hongze Cheng 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
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 已提交
411

H
Hongze Cheng 已提交
412 413 414 415
static int32_t tsdbCompactNextRow(STsdbCompactor *pCompactor) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
416 417 418
  TABLEID  excludeTableId;
  TABLEID *pExcludeTableId = NULL;

H
Hongze Cheng 已提交
419
  for (;;) {
H
Hongze Cheng 已提交
420
    code = tsdbCompactNextRowImpl(pCompactor, pExcludeTableId);
H
Hongze Cheng 已提交
421 422 423 424
    TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
      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 已提交
450

H
Hongze Cheng 已提交
451 452 453 454 455 456 457
          // 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 已提交
458

H
Hongze Cheng 已提交
459 460 461 462
          // table exists
          pCompactor->tbSkm.uid = pRowInfo->uid;
        } else {  // normal table
          // check if table exists
H
Hongze Cheng 已提交
463 464
          if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->uid, &info, NULL) != TSDB_CODE_SUCCESS) {
            excludeTableId.suid = pRowInfo->suid;
H
Hongze Cheng 已提交
465
            excludeTableId.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
466 467 468 469
            pExcludeTableId = &excludeTableId;
            continue;
          }

H
Hongze Cheng 已提交
470
          // table exists
H
Hongze Cheng 已提交
471
          pCompactor->tbSkm.suid = pRowInfo->suid;
H
Hongze Cheng 已提交
472
          pCompactor->tbSkm.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
473
          tDestroyTSchema(pCompactor->tbSkm.pTSchema);
H
Hongze Cheng 已提交
474

H
Hongze Cheng 已提交
475 476
          pCompactor->tbSkm.pTSchema = metaGetTbTSchema(pCompactor->pTsdb->pVnode->pMeta, pRowInfo->suid, -1, 1);
          if (pCompactor->tbSkm.pTSchema == NULL) {
H
Hongze Cheng 已提交
477 478 479
            code = TSDB_CODE_OUT_OF_MEMORY;
            TSDB_CHECK_CODE(code, lino, _exit);
          }
H
Hongze Cheng 已提交
480
        }
H
Hongze Cheng 已提交
481

H
Hongze Cheng 已提交
482 483 484 485 486 487 488 489
        // 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 已提交
490

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

H
Hongze Cheng 已提交
495
            pCompactor->aTSDBKEY = (TSDBKEY *)TARRAY_DATA(pCompactor->aSkyLine);
H
Hongze Cheng 已提交
496 497 498
            pCompactor->iKey = 0;
            pCompactor->sKey.version = 0;
            pCompactor->sKey.ts = pCompactor->aTSDBKEY[0].ts;
H
Hongze Cheng 已提交
499 500 501
          } else {
            pCompactor->aTSDBKEY = NULL;
          }
H
Hongze Cheng 已提交
502 503
        }
      }
H
Hongze Cheng 已提交
504

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

H
Hongze Cheng 已提交
507 508
      // check if the row is deleted
      if (pCompactor->aTSDBKEY && tsdbCompactRowIsDeleted(pCompactor, &pRowInfo->row)) {
H
Hongze Cheng 已提交
509 510 511 512
        continue;
      } else {
        break;
      }
H
Hongze Cheng 已提交
513 514 515
    } else {
      // iter end, just break out
      break;
H
Hongze Cheng 已提交
516 517 518 519
    }
  }

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

H
Hongze Cheng 已提交
527
static int32_t tsdbCompactGetRow(STsdbCompactor *pCompactor, SRowInfo **ppRowInfo, STSchema **ppTSchema) {
H
Hongze Cheng 已提交
528 529 530 531 532 533 534 535 536
  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 已提交
537 538
    ASSERT(pCompactor->pIter->rowInfo.suid == pCompactor->tbSkm.suid);
    ASSERT(pCompactor->pIter->rowInfo.uid == pCompactor->tbSkm.uid);
H
Hongze Cheng 已提交
539
    *ppRowInfo = &pCompactor->pIter->rowInfo;
H
Hongze Cheng 已提交
540
    *ppTSchema = pCompactor->tbSkm.pTSchema;
H
Hongze Cheng 已提交
541
  } else {
H
Hongze Cheng 已提交
542 543
    *ppRowInfo = NULL;
    *ppTSchema = NULL;
H
Hongze Cheng 已提交
544 545
  }

H
Hongze Cheng 已提交
546 547 548 549
_exit:
  return code;
}

H
Hongze Cheng 已提交
550
static int32_t tsdbOpenCompactor(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
551 552 553
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
554
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
555

H
Hongze Cheng 已提交
556
  // reader
H
Hongze Cheng 已提交
557
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
H
Hongze Cheng 已提交
558 559
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
560 561
  // open iters
  STsdbDataIter *pIter;
H
Hongze Cheng 已提交
562

H
Hongze Cheng 已提交
563 564
  pCompactor->iterList = NULL;
  tRBTreeCreate(&pCompactor->rtree, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
565

H
Hongze Cheng 已提交
566 567 568 569 570 571 572 573 574 575 576
  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 已提交
577 578
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
579 580 581 582 583 584
    if (pIter) {
      pIter->next = pCompactor->iterList;
      pCompactor->iterList = pIter;
      tRBTreePut(&pCompactor->rtree, &pIter->n);
    }
  }
H
Hongze Cheng 已提交
585
  pCompactor->pIter = NULL;
H
Hongze Cheng 已提交
586
  tBlockDataReset(&pCompactor->bData);
H
Hongze Cheng 已提交
587

H
Hongze Cheng 已提交
588 589 590 591 592 593 594 595 596
  // 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 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  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 已提交
621 622 623 624 625 626 627 628
_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 已提交
629

H
Hongze Cheng 已提交
630 631 632 633 634 635
static void tsdbCloseCompactor(STsdbCompactor *pCompactor) {
  for (STsdbDataIter *pIter = pCompactor->iterList; pIter;) {
    STsdbDataIter *pIterNext = pIter->next;
    tsdbDataIterClose(pIter);
    pIter = pIterNext;
  }
H
Hongze Cheng 已提交
636

H
Hongze Cheng 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  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 已提交
665

H
Hongze Cheng 已提交
666
_exit:
H
Hongze Cheng 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  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 已提交
700
}
H
Hongze Cheng 已提交
701

H
Hongze Cheng 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
#endif

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);

typedef struct {
  STsdb  *pTsdb;
  int64_t commitID;
  int8_t  cmprAlg;
  int32_t maxRows;
  int32_t minRows;

  STsdbFS fs;

  int32_t  fid;
  TABLEID  tbid;
  SSkmInfo tbSkm;

  // Tombstone
  SDelFReader *pDelFReader;
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
  SArray      *aSkyLine;  // SArray<TSDBKEY>
H
Hongze Cheng 已提交
726 727
  int32_t      iDelIdx;
  int32_t      iSkyLine;
H
Hongze Cheng 已提交
728 729
  TSDBKEY     *pDKey;
  TSDBKEY      dKey;
H
Hongze Cheng 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

  // Reader
  SDataFReader   *pReader;
  STsdbDataIter2 *iterList;  // list of iterators
  STsdbDataIter2 *pIter;
  SRBTree         rbt;

  // Writer
  SDataFWriter *pWriter;
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
  SMapData      mDataBlk;   // SMapData<SDataBlk>
  SArray       *aSttBlk;    // SArray<SSttBlk>
  SBlockData    bData;
  SBlockData    sData;
} STsdbCompactor;

static int32_t tsdbCompactWriteTableDataStart(STsdbCompactor *pCompactor, TABLEID *pId) {
H
Hongze Cheng 已提交
747 748 749
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
750 751
  pCompactor->tbid = *pId;

H
Hongze Cheng 已提交
752 753 754
  // tombstone
  for (;;) {
    if (pCompactor->iDelIdx >= taosArrayGetSize(pCompactor->aDelIdx)) {
H
Hongze Cheng 已提交
755
      pCompactor->pDKey = NULL;
H
Hongze Cheng 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
      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 已提交
772

H
Hongze Cheng 已提交
773
      pCompactor->iSkyLine = 0;
H
Hongze Cheng 已提交
774 775 776 777 778 779 780 781 782
      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 已提交
783 784
      break;
    } else {
H
Hongze Cheng 已提交
785
      pCompactor->pDKey = NULL;
H
Hongze Cheng 已提交
786 787 788 789 790
      break;
    }
  }

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

H
Hongze Cheng 已提交
794
  tMapDataReset(&pCompactor->mDataBlk);
H
Hongze Cheng 已提交
795

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

H
Hongze Cheng 已提交
799 800 801 802 803 804 805
  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 已提交
806
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
807
  }
H
Hongze Cheng 已提交
808

H
Hongze Cheng 已提交
809 810 811 812 813 814 815 816 817 818
_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 已提交
819

H
Hongze Cheng 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
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 已提交
836

H
Hongze Cheng 已提交
837 838 839
      tBlockDataClear(&pCompactor->bData);
    } else {
      code = tsdbWriteDataBlock(pCompactor->pWriter, &pCompactor->bData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
H
Hongze Cheng 已提交
840
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
841 842
    }
  }
H
Hongze Cheng 已提交
843

H
Hongze Cheng 已提交
844 845 846 847 848
  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 已提交
849 850
    }

H
Hongze Cheng 已提交
851 852
    pBlockIdx->suid = pCompactor->tbid.suid;
    pBlockIdx->uid = pCompactor->tbid.uid;
H
Hongze Cheng 已提交
853

H
Hongze Cheng 已提交
854 855 856
    code = tsdbWriteDataBlk(pCompactor->pWriter, &pCompactor->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
857

H
Hongze Cheng 已提交
858 859 860 861 862 863 864 865 866 867
_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 已提交
868

H
Hongze Cheng 已提交
869
static bool tsdbCompactRowIsDeleted(STsdbCompactor *pCompactor, TSDBROW *pRow) {
H
Hongze Cheng 已提交
870 871 872
  TSDBKEY  tKey = TSDBROW_KEY(pRow);
  TSDBKEY *aKey = (TSDBKEY *)TARRAY_DATA(pCompactor->aSkyLine);
  int32_t  nKey = TARRAY_SIZE(pCompactor->aSkyLine);
H
Hongze Cheng 已提交
873 874

  if (tKey.ts > pCompactor->pDKey->ts) {
H
Hongze Cheng 已提交
875 876
    do {
      pCompactor->pDKey->version = aKey[pCompactor->iSkyLine].version;
H
Hongze Cheng 已提交
877
      pCompactor->iSkyLine++;
H
Hongze Cheng 已提交
878 879
      if (pCompactor->iSkyLine < nKey) {
        pCompactor->dKey.ts = aKey[pCompactor->iSkyLine].ts;
H
Hongze Cheng 已提交
880
      } else {
H
Hongze Cheng 已提交
881 882 883 884 885 886
        if (pCompactor->pDKey->version == 0) {
          pCompactor->pDKey = NULL;
          return false;
        } else {
          pCompactor->pDKey->ts = INT64_MAX;
        }
H
Hongze Cheng 已提交
887
      }
H
Hongze Cheng 已提交
888
    } while (tKey.ts > pCompactor->pDKey->ts);
H
Hongze Cheng 已提交
889 890 891 892 893 894 895 896 897
  }

  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 已提交
898 899
    ASSERT(pCompactor->iSkyLine < nKey);
    if (tKey.version > TMAX(pCompactor->pDKey->version, aKey[pCompactor->iSkyLine].version)) {
H
Hongze Cheng 已提交
900 901 902 903 904
      return false;
    } else {
      return true;
    }
  }
H
Hongze Cheng 已提交
905 906 907 908

  return false;
}

H
Hongze Cheng 已提交
909 910 911
static int32_t tsdbCompactWriteTableData(STsdbCompactor *pCompactor, SRowInfo *pRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
912

H
Hongze Cheng 已提交
913 914 915 916 917 918 919 920
  SRowInfo rInfo;
  if (pRowInfo == NULL) {
    rInfo.suid = INT64_MAX;
    rInfo.uid = INT64_MAX;
    // rInfo.row = TSDBORW_V;
    pRowInfo = &rInfo;
  }

H
Hongze Cheng 已提交
921
  // start a new table data write if need
H
Hongze Cheng 已提交
922 923 924
  if (pRowInfo->uid != pCompactor->tbid.uid) {
    if (pCompactor->tbid.uid) {
      code = tsdbCompactWriteTableDataEnd(pCompactor);
H
Hongze Cheng 已提交
925
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
926
    }
H
Hongze Cheng 已提交
927

H
Hongze Cheng 已提交
928
    code = tsdbCompactWriteTableDataStart(pCompactor, (TABLEID *)pRowInfo);
H
Hongze Cheng 已提交
929
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
930
  }
H
Hongze Cheng 已提交
931

H
Hongze Cheng 已提交
932
  // check if row is deleted
H
Hongze Cheng 已提交
933
  if (pCompactor->pDKey && tsdbCompactRowIsDeleted(pCompactor, &pRowInfo->row)) goto _exit;
H
Hongze Cheng 已提交
934 935

  code = tBlockDataUpsertRow(&pCompactor->bData, &pRowInfo->row, NULL, pRowInfo->uid);
H
Hongze Cheng 已提交
936 937 938 939
  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 已提交
940
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953
  }

_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 已提交
954

H
Hongze Cheng 已提交
955 956 957 958 959 960
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 已提交
961
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976

    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 已提交
977
  }
H
Hongze Cheng 已提交
978

H
Hongze Cheng 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
  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 已提交
1010 1011 1012 1013
  /* tombstone */
  pCompactor->iDelIdx = 0;
  pCompactor->iSkyLine = 0;

H
Hongze Cheng 已提交
1014 1015
  /* reader */
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pCompactor->pTsdb, pSet);
H
Hongze Cheng 已提交
1016
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1017

H
Hongze Cheng 已提交
1018
  code = tsdbOpenDataFileDataIter(pCompactor->pReader, &pCompactor->pIter);
H
Hongze Cheng 已提交
1019
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1020

H
Hongze Cheng 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  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 已提交
1048
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1049

H
Hongze Cheng 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  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 已提交
1086 1087
  code = tsdbWriteSttBlk(pCompactor->pWriter, pCompactor->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1088

H
Hongze Cheng 已提交
1089 1090 1091
  code = tsdbWriteBlockIdx(pCompactor->pWriter, pCompactor->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1092 1093
  code = tsdbUpdateDFileSetHeader(pCompactor->pWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1094

H
Hongze Cheng 已提交
1095 1096
  code = tsdbFSUpsertFSet(&pCompactor->fs, &pCompactor->pWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1097

H
Hongze Cheng 已提交
1098 1099
  code = tsdbDataFWriterClose(&pCompactor->pWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1100

H
Hongze Cheng 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  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 已提交
1113
_exit:
H
Hongze Cheng 已提交
1114 1115 1116 1117 1118 1119
  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 已提交
1120 1121
  return code;
}
H
Hongze Cheng 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

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 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
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 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
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 已提交
1190
  /* tombstone */
H
Hongze Cheng 已提交
1191 1192 1193 1194
  if (pCompactor->fs.pDelFile) {
    code = tsdbDelFReaderOpen(&pCompactor->pDelFReader, pCompactor->fs.pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1195
    if ((pCompactor->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1196 1197 1198 1199
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1200
    if ((pCompactor->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
H
Hongze Cheng 已提交
1201 1202 1203 1204
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1205
    if ((pCompactor->aSkyLine = taosArrayInit(0, sizeof(TSDBKEY))) == NULL) {
H
Hongze Cheng 已提交
1206 1207 1208 1209 1210 1211 1212
      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 已提交
1213

H
Hongze Cheng 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
  /* 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);
  } else {
    tsdbInfo("vgId:%d %s done, commit ID:%" PRId64, TD_VID(pTsdb->pVnode), __func__, pCompactor->commitID);
  }
  return code;
}

H
Hongze Cheng 已提交
1233 1234 1235 1236 1237
int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
1239 1240 1241 1242
  // begin compact
  code = tsdbBeginCompact(pTsdb, pCompactor);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1243
  // loop to compact each file set
H
Hongze Cheng 已提交
1244
  while (true) {
H
Hongze Cheng 已提交
1245 1246 1247 1248 1249 1250
    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 已提交
1251

H
Hongze Cheng 已提交
1252
    code = tsdbCompactFileSet(pCompactor, pSet);
H
Hongze Cheng 已提交
1253
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1254 1255
  }

H
Hongze Cheng 已提交
1256 1257
#if 0 

H
Hongze Cheng 已提交
1258 1259 1260
  code = tsdbFSUpsertDelFile(&pCompactor->fs, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1261
#endif
H
Hongze Cheng 已提交
1262 1263 1264 1265 1266 1267 1268 1269
_exit:
  // // commit/abort compact
  // if (code) {
  //   tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  //   tsdbAbortCompact(pCompactor);
  // } else {
  //   tsdbCommitCompact(pCompactor);
  // }
H
Hongze Cheng 已提交
1270
  tsdbEndCompact(pCompactor);
H
Hongze Cheng 已提交
1271 1272
  return code;
}