tsdbCompact.c 21.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "tsdb.h"
H
Hongze Cheng 已提交
17

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

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

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

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

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

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

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

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

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

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

  taosThreadRwlockWrlock(&pTsdb->rwLock);

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

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

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

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

H
Hongze Cheng 已提交
90
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
91 92
  code = tsdbFSRollback(pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
93 94 95 96

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

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

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

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

H
Hongze Cheng 已提交
130
      pCompactor->iSkyLine = 0;
H
Hongze Cheng 已提交
131 132 133 134 135 136 137 138 139
      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 已提交
140 141
      break;
    } else {
H
Hongze Cheng 已提交
142
      pCompactor->pDKey = NULL;
H
Hongze Cheng 已提交
143 144 145 146
      break;
    }
  }

H
Hongze Cheng 已提交
147
  // writer
H
Hongze Cheng 已提交
148
  code = tsdbUpdateTableSchema(pCompactor->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pCompactor->tbSkm);
H
Hongze Cheng 已提交
149
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
150

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

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

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

H
Hongze Cheng 已提交
162 163
    TABLEID tbid = {.suid = pId->suid, .uid = pId->suid ? 0 : pId->uid};
    code = tBlockDataInit(&pCompactor->sData, &tbid, pCompactor->tbSkm.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
164
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
165
  }
H
Hongze Cheng 已提交
166

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

H
Hongze Cheng 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
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);
        }
      }
      tBlockDataClear(&pCompactor->bData);
    } else {
      code = tsdbWriteDataBlock(pCompactor->pWriter, &pCompactor->bData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
H
Hongze Cheng 已提交
197
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
198 199
    }
  }
H
Hongze Cheng 已提交
200

H
Hongze Cheng 已提交
201
  if (pCompactor->mDataBlk.nItem > 0) {
H
Hongze Cheng 已提交
202 203 204 205
    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 已提交
206 207
    }

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

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

H
Hongze Cheng 已提交
215 216 217 218 219 220 221 222 223 224
_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 已提交
225

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

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

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

  return false;
}

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

H
Hongze Cheng 已提交
270
  // start a new table data write if need
H
Hongze Cheng 已提交
271
  if (pRowInfo == NULL || pRowInfo->uid != pCompactor->tbid.uid) {
H
Hongze Cheng 已提交
272 273
    if (pCompactor->tbid.uid) {
      code = tsdbCompactWriteTableDataEnd(pCompactor);
H
Hongze Cheng 已提交
274
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
275
    }
H
Hongze Cheng 已提交
276

H
Hongze Cheng 已提交
277 278 279 280 281 282 283 284
    if (pRowInfo == NULL) {
      if (pCompactor->sData.nRow > 0) {
        code = tsdbWriteSttBlock(pCompactor->pWriter, &pCompactor->sData, pCompactor->aSttBlk, pCompactor->cmprAlg);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
      return code;
    }

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

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

H
Hongze Cheng 已提交
292
  if (tBlockDataTryUpsertRow(&pCompactor->bData, &pRowInfo->row, pRowInfo->uid) > pCompactor->maxRows) {
H
Hongze Cheng 已提交
293
    code = tsdbWriteDataBlock(pCompactor->pWriter, &pCompactor->bData, &pCompactor->mDataBlk, pCompactor->cmprAlg);
H
Hongze Cheng 已提交
294
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
295 296
  }

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

H
Hongze Cheng 已提交
300 301 302 303 304 305 306 307 308 309 310
_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 已提交
311

H
Hongze Cheng 已提交
312 313 314 315
static bool tsdbCompactTableIsDropped(STsdbCompactor *pCompactor) {
  SMetaInfo info;

  if (pCompactor->pIter->rowInfo.uid == pCompactor->tbid.uid) return false;
H
Hongze Cheng 已提交
316
  if (metaGetInfo(pCompactor->pTsdb->pVnode->pMeta, pCompactor->pIter->rowInfo.uid, &info, NULL)) {
H
Hongze Cheng 已提交
317 318 319 320
    return true;
  }
  return false;
}
H
Hongze Cheng 已提交
321 322 323 324
static int32_t tsdbCompactNextRow(STsdbCompactor *pCompactor, SRowInfo **ppRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
325 326 327 328
  for (;;) {
    if (pCompactor->pIter) {
      code = tsdbDataIterNext2(pCompactor->pIter, NULL);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
329

H
Hongze Cheng 已提交
330 331 332 333 334 335 336 337 338 339 340 341
      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 已提交
342 343 344
        }
      }
    }
H
Hongze Cheng 已提交
345

H
Hongze Cheng 已提交
346 347 348 349 350
    if (pCompactor->pIter == NULL) {
      SRBTreeNode *pNode = tRBTreeDropMin(&pCompactor->rbt);
      if (pNode) {
        pCompactor->pIter = TSDB_RBTN_TO_DATA_ITER(pNode);
      }
H
Hongze Cheng 已提交
351 352 353
    }

    if (pCompactor->pIter) {
H
Hongze Cheng 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
      if (tsdbCompactTableIsDropped(pCompactor)) {
        TABLEID tbid = {.suid = pCompactor->pIter->rowInfo.suid, .uid = pCompactor->pIter->rowInfo.uid};
        tRBTreeClear(&pCompactor->rbt);
        for (pCompactor->pIter = pCompactor->iterList; pCompactor->pIter; pCompactor->pIter = pCompactor->pIter->next) {
          code = tsdbDataIterNext2(pCompactor->pIter,
                                   &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_TABLEID, .tbid = tbid});
          TSDB_CHECK_CODE(code, lino, _exit);

          if (pCompactor->pIter->rowInfo.suid || pCompactor->pIter->rowInfo.uid) {
            tRBTreePut(&pCompactor->rbt, &pCompactor->pIter->rbtn);
          }
        }
      } else {
        *ppRowInfo = &pCompactor->pIter->rowInfo;
        break;
      }
H
Hongze Cheng 已提交
370 371
    } else {
      *ppRowInfo = NULL;
H
Hongze Cheng 已提交
372
      break;
H
Hongze Cheng 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    }
  }

_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 已提交
391 392 393
  /* tombstone */
  pCompactor->iDelIdx = 0;

H
Hongze Cheng 已提交
394 395
  /* reader */
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pCompactor->pTsdb, pSet);
H
Hongze Cheng 已提交
396
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398
  code = tsdbOpenDataFileDataIter(pCompactor->pReader, &pCompactor->pIter);
H
Hongze Cheng 已提交
399
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
400

H
Hongze Cheng 已提交
401
  tRBTreeCreate(&pCompactor->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
402 403 404
  if (pCompactor->pIter) {
    pCompactor->pIter->next = pCompactor->iterList;
    pCompactor->iterList = pCompactor->pIter;
H
Hongze Cheng 已提交
405 406 407 408 409 410

    code = tsdbDataIterNext2(pCompactor->pIter, NULL);
    TSDB_CHECK_CODE(code, lino, _exit);

    ASSERT(pCompactor->pIter->rowInfo.suid || pCompactor->pIter->rowInfo.uid);
    tRBTreePut(&pCompactor->rbt, &pCompactor->pIter->rbtn);
H
Hongze Cheng 已提交
411 412 413 414 415 416 417 418 419
  }

  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;
H
Hongze Cheng 已提交
420 421 422 423 424 425

      code = tsdbDataIterNext2(pCompactor->pIter, NULL);
      TSDB_CHECK_CODE(code, lino, _exit);

      ASSERT(pCompactor->pIter->rowInfo.suid || pCompactor->pIter->rowInfo.uid);
      tRBTreePut(&pCompactor->rbt, &pCompactor->pIter->rbtn);
H
Hongze Cheng 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438
    }
  }
  pCompactor->pIter = NULL;

  /* 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 已提交
439
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
440

H
Hongze Cheng 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  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;

H
Hongze Cheng 已提交
474 475
  ASSERT(pCompactor->bData.nRow == 0);
  ASSERT(pCompactor->sData.nRow == 0);
H
Hongze Cheng 已提交
476 477

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

H
Hongze Cheng 已提交
481 482 483
  code = tsdbWriteBlockIdx(pCompactor->pWriter, pCompactor->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
484 485
  code = tsdbUpdateDFileSetHeader(pCompactor->pWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487 488
  code = tsdbFSUpsertFSet(&pCompactor->fs, &pCompactor->pWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
489

H
Hongze Cheng 已提交
490 491
  code = tsdbDataFWriterClose(&pCompactor->pWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
492

H
Hongze Cheng 已提交
493 494 495 496 497 498 499 500 501 502 503 504
  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 已提交
505
_exit:
H
Hongze Cheng 已提交
506 507 508 509 510 511
  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 已提交
512 513
  return code;
}
H
Hongze Cheng 已提交
514 515 516 517 518 519 520 521 522

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

H
Hongze Cheng 已提交
523
  // do compact, end with a NULL row
H
Hongze Cheng 已提交
524
  SRowInfo *pRowInfo;
H
Hongze Cheng 已提交
525
  do {
H
Hongze Cheng 已提交
526 527 528 529 530
    code = tsdbCompactNextRow(pCompactor, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbCompactWriteTableData(pCompactor, pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
531
  } while (pRowInfo);
H
Hongze Cheng 已提交
532 533 534 535 536 537

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

_exit:
H
Hongze Cheng 已提交
538 539 540 541 542 543 544 545 546 547
  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);
    if (pCompactor->pWriter) tsdbDataFWriterClose(&pCompactor->pWriter, 0);
    while ((pCompactor->pIter = pCompactor->iterList)) {
      pCompactor->iterList = pCompactor->pIter->next;
      tsdbCloseDataIter2(pCompactor->pIter);
    }
    if (pCompactor->pReader) tsdbDataFReaderClose(&pCompactor->pReader);
  }
H
Hongze Cheng 已提交
548 549 550
  return code;
}

H
Hongze Cheng 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
static void tsdbEndCompact(STsdbCompactor *pCompactor) {
  // 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 已提交
573
static int32_t tsdbBeginCompact(STsdb *pTsdb, SCompactInfo *pInfo, STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
574 575 576 577
  int32_t code = 0;
  int32_t lino = 0;

  pCompactor->pTsdb = pTsdb;
H
Hongze Cheng 已提交
578
  pCompactor->commitID = pInfo->commitID;
H
Hongze Cheng 已提交
579 580 581 582 583 584 585 586
  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 已提交
587
  /* tombstone */
H
Hongze Cheng 已提交
588 589 590 591
  if (pCompactor->fs.pDelFile) {
    code = tsdbDelFReaderOpen(&pCompactor->pDelFReader, pCompactor->fs.pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
592
    if ((pCompactor->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
593 594 595 596
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
597
    if ((pCompactor->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
H
Hongze Cheng 已提交
598 599 600 601
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
602
    if ((pCompactor->aSkyLine = taosArrayInit(0, sizeof(TSDBKEY))) == NULL) {
H
Hongze Cheng 已提交
603 604 605 606 607 608 609
      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 已提交
610

H
Hongze Cheng 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623
  /* reader */

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

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

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, commit ID:%" PRId64, TD_VID(pTsdb->pVnode), __func__, lino,
              tstrerror(code), pCompactor->commitID);
H
Hongze Cheng 已提交
624 625 626 627 628 629 630 631 632
    tBlockDataDestroy(&pCompactor->sData);
    tBlockDataDestroy(&pCompactor->bData);
    if (pCompactor->fs.pDelFile) {
      taosArrayDestroy(pCompactor->aSkyLine);
      taosArrayDestroy(pCompactor->aDelData);
      taosArrayDestroy(pCompactor->aDelIdx);
      if (pCompactor->pDelFReader) tsdbDelFReaderClose(&pCompactor->pDelFReader);
    }
    tsdbFSDestroy(&pCompactor->fs);
H
Hongze Cheng 已提交
633 634 635 636 637 638
  } else {
    tsdbInfo("vgId:%d %s done, commit ID:%" PRId64, TD_VID(pTsdb->pVnode), __func__, pCompactor->commitID);
  }
  return code;
}

H
Hongze Cheng 已提交
639
int32_t tsdbCompact(STsdb *pTsdb, SCompactInfo *pInfo) {
H
Hongze Cheng 已提交
640 641 642
  int32_t code = 0;

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

H
Hongze Cheng 已提交
644
  if ((code = tsdbBeginCompact(pTsdb, pInfo, pCompactor))) return code;
H
Hongze Cheng 已提交
645

H
Hongze Cheng 已提交
646
  for (;;) {
H
Hongze Cheng 已提交
647 648 649 650 651 652
    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 已提交
653

H
Hongze Cheng 已提交
654
    if ((code = tsdbCompactFileSet(pCompactor, pSet))) goto _exit;
H
Hongze Cheng 已提交
655 656
  }

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

H
Hongze Cheng 已提交
659
_exit:
H
Hongze Cheng 已提交
660 661 662 663 664
  if (code) {
    tsdbAbortCompact(pCompactor);
  } else {
    tsdbCommitCompact(pCompactor);
  }
H
Hongze Cheng 已提交
665
  tsdbEndCompact(pCompactor);
H
Hongze Cheng 已提交
666 667
  return code;
}