tsdbCompact.c 8.5 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 56
} STsdbCompactor;

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

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

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

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

H
Hongze Cheng 已提交
79
_exit:
H
Hongze Cheng 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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 已提交
98 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
  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 已提交
125 126 127 128
  // TODO

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

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

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

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

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

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

  pCompactor->fid = INT32_MIN;

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
291 292 293 294 295 296 297
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 已提交
298 299 300
  STsdbCompactor compactor = {0};

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

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

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

H
Hongze Cheng 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    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 已提交
329 330 331 332
    // loop to merge row by row
    TSDBROW *pRow = NULL;
    for (;;) {
      code = tsdbCompactNextRow(&compactor, &pRow);
H
Hongze Cheng 已提交
333
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
334 335

      if (pRow == NULL) break;
H
Hongze Cheng 已提交
336
    }
H
Hongze Cheng 已提交
337 338 339 340 341
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
342
    tsdbAbortCompact(&compactor);
H
Hongze Cheng 已提交
343
  } else {
H
Hongze Cheng 已提交
344
    tsdbCommitCompact(&compactor);
H
Hongze Cheng 已提交
345
  }
H
Hongze Cheng 已提交
346 347
  return code;
}