tsdbCompact.c 8.8 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
typedef struct {
} SMemDIter;
H
Hongze Cheng 已提交
20 21

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

typedef struct {
H
Hongze Cheng 已提交
32 33 34 35 36 37
  SDataFReader *pReader;
  int32_t       iStt;
  SArray       *aSttBlk;  // SArray<SSttBlk>
  SBlockData    bData;
  int32_t       iSttBlk;
  int32_t       iRow;
H
Hongze Cheng 已提交
38 39
} SSttDIter;

H
Hongze Cheng 已提交
40
typedef struct {
H
Hongze Cheng 已提交
41 42 43 44
  int32_t     flag;
  SRowInfo    rowInfo;
  SRBTreeNode n;
  char        handle[];
H
Hongze Cheng 已提交
45 46
} STsdbDataIter;

H
Hongze Cheng 已提交
47
typedef struct {
H
Hongze Cheng 已提交
48 49 50 51 52 53 54
  STsdb        *pTsdb;
  STsdbFS       fs;
  int64_t       cid;
  int32_t       fid;
  SDataFReader *pReader;
  SDFileSet    *pDFileSet;
  SRBTree       rtree;
H
Hongze Cheng 已提交
55
  SBlockData    bData;
H
Hongze Cheng 已提交
56 57
} STsdbCompactor;

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

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

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

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

H
Hongze Cheng 已提交
80
_exit:
H
Hongze Cheng 已提交
81 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  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;

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

  // read first data block
  pDataDIter->iBlockIdx = 0;
  code = tsdbReadDataBlk(pReader, taosArrayGet(pDataDIter->aBlockIdx, pDataDIter->iBlockIdx), &pDataDIter->mDataBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  pDataDIter->iDataBlk = 0;
  // code = tsdbReadDataBlock(pReader, tMapDat);
  // TSDB_CHECK_CODE(code, lino, _exit);

  pDataDIter->iRow = 0;

H
Hongze Cheng 已提交
126 127 128 129
  // TODO

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

H
Hongze Cheng 已提交
144
static int32_t tsdbSttDIterOpen(SDataFReader *pReader, int32_t iStt, STsdbDataIter **ppIter) {
H
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153
  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 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  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);

  pSttDIter->iSttBlk = 0;
  // code = tsdbReadSttBlock(pReader, taosArrayGet(pSttDIter->aSttBlk, pSttDIter->iSttBlk), &pSttDIter->bData);
  // TSDB_CHECK_CODE(code, lino, _exit);

  pSttDIter->iRow = 0;

H
Hongze Cheng 已提交
177 178 179 180
  // TODO

_exit:
  if (code) {
H
Hongze Cheng 已提交
181
  _clear_exit:
H
Hongze Cheng 已提交
182
    *ppIter = NULL;
H
Hongze Cheng 已提交
183 184 185 186 187
    if (pIter) {
      tBlockDataDestroy(&pSttDIter->bData, 1);
      taosArrayDestroy(pSttDIter->aSttBlk);
      taosMemoryFree(pIter);
    }
H
Hongze Cheng 已提交
188 189 190
  } else {
    *ppIter = pIter;
  }
H
Hongze Cheng 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204
  return code;
}

static void tsdbDataIterClose(STsdbDataIter *pIter) {
  // TODO
}

static int32_t tsdbDataIterNext(STsdbDataIter *pIter) {
  int32_t code = 0;
  int32_t lino = 0;
  // TODO
_exit:
  return code;
}
H
Hongze Cheng 已提交
205 206

// COMPACT =========================
H
Hongze Cheng 已提交
207
static int32_t tsdbBeginCompact(STsdb *pTsdb, STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
208 209 210
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
211 212 213 214 215 216 217
  pCompactor->pTsdb = pTsdb;

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

  pCompactor->fid = INT32_MIN;

H
Hongze Cheng 已提交
218 219 220 221 222 223 224
_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 已提交
225
static int32_t tsdbCommitCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
226 227 228
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
229 230
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
231 232 233 234 235 236 237 238 239
  // 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 已提交
240
static int32_t tsdbAbortCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
241 242 243
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
244 245
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
246 247 248 249 250 251 252 253 254
  // 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 已提交
255
static int32_t tsdbDeepCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
256 257 258
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
259
  STsdb *pTsdb = pCompactor->pTsdb;
H
Hongze Cheng 已提交
260

H
Hongze Cheng 已提交
261 262 263
  code = tsdbDataFReaderOpen(&pCompactor->pReader, pTsdb, pCompactor->pDFileSet);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
264 265 266 267 268 269 270
_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 已提交
271
static int32_t tsdbShallowCompact(STsdbCompactor *pCompactor) {
H
Hongze Cheng 已提交
272
  int32_t code = 0;
H
Hongze Cheng 已提交
273 274
  int32_t lino = 0;

H
Hongze Cheng 已提交
275 276
  STsdb *pTsdb = pCompactor->pTsdb;

H
Hongze Cheng 已提交
277 278 279 280 281 282 283
_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 已提交
284 285 286 287 288 289 290 291
static int32_t tsdbCompactNextRow(STsdbCompactor *pCompactor, TSDBROW **ppRow) {
  int32_t code = 0;
  int32_t lino = 0;
  // TODO
_exit:
  return code;
}

H
Hongze Cheng 已提交
292 293 294 295 296 297 298
int32_t tsdbCompact(STsdb *pTsdb, int32_t flag) {
  int32_t code = 0;
  int32_t lino = 0;

  // Check if can do compact (TODO)

  // Do compact
H
Hongze Cheng 已提交
299 300 301
  STsdbCompactor compactor = {0};

  code = tsdbBeginCompact(pTsdb, &compactor);
H
Hongze Cheng 已提交
302 303
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
304 305 306 307
  while (true) {
    compactor.pDFileSet = (SDFileSet *)taosArraySearch(compactor.fs.aDFileSet, &compactor.fid, tDFileSetCmprFn, TD_GT);
    if (compactor.pDFileSet == NULL) break;

H
Hongze Cheng 已提交
308 309
    compactor.fid = compactor.pDFileSet->fid;

H
Hongze Cheng 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    code = tsdbDataFReaderOpen(&compactor.pReader, pTsdb, compactor.pDFileSet);
    TSDB_CHECK_CODE(code, lino, _exit);

    // open those iterators
    tRBTreeCreate(&compactor.rtree, tsdbDataIterCmprFn);

    STsdbDataIter *pIter;

    code = tsdbDataDIterOpen(compactor.pReader, &pIter);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (pIter) tRBTreePut(&compactor.rtree, &pIter->n);

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

      if (pIter) tRBTreePut(&compactor.rtree, &pIter->n);
    }

H
Hongze Cheng 已提交
330 331 332 333
    // loop to merge row by row
    TSDBROW *pRow = NULL;
    for (;;) {
      code = tsdbCompactNextRow(&compactor, &pRow);
H
Hongze Cheng 已提交
334
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
335 336

      if (pRow == NULL) break;
H
Hongze Cheng 已提交
337 338 339 340 341 342 343 344

      // code = tBlockDataAppendRow(&compactor.bData, pRow, pRow, NULL, 0);
      // TSDB_CHECK_CODE(code, lino, _exit);

      // if (compactor.bData.nRows >= TSDB_MAX_ROWS_PER_BLOCK) {
      //   code = tsdbFlushBlock(&compactor);
      //   TSDB_CHECK_CODE(code, lino, _exit);
      // }
H
Hongze Cheng 已提交
345
    }
H
Hongze Cheng 已提交
346 347 348 349 350
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
351
    tsdbAbortCompact(&compactor);
H
Hongze Cheng 已提交
352
  } else {
H
Hongze Cheng 已提交
353
    tsdbCommitCompact(&compactor);
H
Hongze Cheng 已提交
354
  }
H
Hongze Cheng 已提交
355 356
  return code;
}