tsdbMerge.c 9.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "inc/tsdbMerge.h"
H
Hongze Cheng 已提交
17 18

typedef struct {
H
Hongze Cheng 已提交
19 20 21 22
  bool       launched;
  bool       toData;
  int32_t    level;
  STFileSet *fset;
H
Hongze Cheng 已提交
23 24
  SRowInfo  *pRowInfo;
  SBlockData bData;
H
Hongze Cheng 已提交
25
} SMergeCtx;
H
Hongze Cheng 已提交
26

H
Hongze Cheng 已提交
27
typedef struct {
H
Hongze Cheng 已提交
28
  STsdb *tsdb;
H
Hongze Cheng 已提交
29 30
  // context
  SMergeCtx ctx;
H
Hongze Cheng 已提交
31 32
  // config
  int32_t  maxRow;
H
Hongze Cheng 已提交
33
  int32_t  minRow;
H
Hongze Cheng 已提交
34 35 36 37 38 39 40 41 42 43
  int32_t  szPage;
  int8_t   cmprAlg;
  int64_t  cid;
  SSkmInfo skmTb;
  SSkmInfo skmRow;
  uint8_t *aBuf[5];
  // reader
  TARRAY2(SSttFileReader *) sttReaderArr;
  SDataFileReader *dataReader;
  // writer
H
Hongze Cheng 已提交
44 45
  SSttFileWriter  *sttWriter;
  SDataFileWriter *dataWriter;
H
Hongze Cheng 已提交
46 47
  // operations
  TFileOpArray fopArr;
H
Hongze Cheng 已提交
48 49
} SMerger;

H
Hongze Cheng 已提交
50 51 52
static int32_t tsdbMergerOpen(SMerger *merger) {
  merger->ctx.launched = true;
  TARRAY2_INIT(&merger->fopArr);
H
Hongze Cheng 已提交
53
  return 0;
H
Hongze Cheng 已提交
54 55
}

H
Hongze Cheng 已提交
56
static int32_t tsdbMergerClose(SMerger *merger) {
H
Hongze Cheng 已提交
57
  // TODO
H
Hongze Cheng 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  int32_t       code = 0;
  int32_t       lino = 0;
  SVnode       *pVnode = merger->tsdb->pVnode;
  int32_t       vid = TD_VID(pVnode);
  STFileSystem *fs = merger->tsdb->pFS;

  // edit file system
  code = tsdbFSEditBegin(fs, &merger->fopArr, TSDB_FEDIT_MERGE);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbFSEditCommit(fs);
  TSDB_CHECK_CODE(code, lino, _exit);

  // clear the merge
  TARRAY2_FREE(&merger->fopArr);

_exit:
  if (code) {
  } else {
  }
H
Hongze Cheng 已提交
78 79
  return 0;
}
H
Hongze Cheng 已提交
80

H
Hongze Cheng 已提交
81
static int32_t tsdbMergeNextRow(SMerger *merger) {
H
Hongze Cheng 已提交
82 83 84
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
85

H
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static int32_t tsdbMergeToDataWriteTSDataBlock(SMerger *merger) {
  if (merger->ctx.bData.nRow == 0) return 0;

  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);
  if (merger->ctx.bData.nRow >= merger->minRow) {
    // code = tsdbDataFWriteTSDataBlock(merger->dataWriter, &merger->ctx.bData);
    // TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    code = tsdbSttFWriteTSDataBlock(merger->sttWriter, &merger->ctx.bData);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  tBlockDataReset(&merger->ctx.bData);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
  }
  return code;
}
H
Hongze Cheng 已提交
108 109 110 111 112 113 114 115 116
static int32_t tsdbMergeToData(SMerger *merger) {
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);

  for (;;) {
    code = tsdbMergeNextRow(merger);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    if (!merger->ctx.pRowInfo) {
      code = tsdbMergeToDataWriteTSDataBlock(merger);
      TSDB_CHECK_CODE(code, lino, _exit);
      break;
    }

    if (!TABLE_SAME_SCHEMA(merger->ctx.bData.suid, merger->ctx.bData.suid, merger->ctx.pRowInfo->suid,
                           merger->ctx.pRowInfo->uid)) {
      code = tsdbMergeToDataWriteTSDataBlock(merger);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbUpdateSkmTb(merger->tsdb, (TABLEID *)merger->ctx.pRowInfo, &merger->skmTb);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tBlockDataInit(&merger->ctx.bData, (TABLEID *)merger->ctx.pRowInfo, merger->skmTb.pTSchema, NULL, 0);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
134 135 136 137 138

    code = tBlockDataAppendRow(&merger->ctx.bData, &merger->ctx.pRowInfo->row, NULL, merger->ctx.pRowInfo->uid);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (merger->ctx.bData.nRow >= merger->maxRow) {
H
Hongze Cheng 已提交
139 140
      code = tsdbMergeToDataWriteTSDataBlock(merger);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    }
  }

_exit:
  if (code) {
    tsdbError("vid:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
  }
  return code;
}

static int32_t tsdbMergeToUpperLevel(SMerger *merger) {
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);

  for (;;) {
    code = tsdbMergeNextRow(merger);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (!merger->ctx.pRowInfo) break;

    code = tsdbSttFWriteTSData(merger->sttWriter, merger->ctx.pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    tsdbError("vid:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static int32_t tsdbMergeFileSetBegin(SMerger *merger) {
  int32_t    code = 0;
  int32_t    lino = 0;
  int32_t    vid = TD_VID(merger->tsdb->pVnode);
  STFileSet *fset = merger->ctx.fset;

  // prepare the merger file set
  SSttLvl   *lvl;
  STFileObj *fobj;
  merger->ctx.toData = true;
  merger->ctx.level = 0;

  TARRAY2_FOREACH(&fset->lvlArr, lvl) {
    if (lvl->level != merger->ctx.level) {
      lvl = NULL;
      break;
H
Hongze Cheng 已提交
189
    }
H
Hongze Cheng 已提交
190

H
Hongze Cheng 已提交
191 192 193 194
    fobj = TARRAY2_GET(&lvl->farr, 0);
    if (fobj->f.stt.nseg < merger->tsdb->pVnode->config.sttTrigger) {
      merger->ctx.toData = false;
      break;
H
Hongze Cheng 已提交
195
    } else {
H
Hongze Cheng 已提交
196 197
      ASSERT(lvl->level == 0 || TARRAY2_SIZE(&lvl->farr) == 1);
      merger->ctx.level++;
H
Hongze Cheng 已提交
198

H
Hongze Cheng 已提交
199 200 201
      // open the reader
      SSttFileReader *reader;
      // code = tsdbSttFileReaderOpen(&fobj->f.stt, &reader);
H
Hongze Cheng 已提交
202
      // TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
203 204 205 206 207 208 209 210 211 212 213 214

      code = TARRAY2_APPEND(&merger->sttReaderArr, reader);
      TSDB_CHECK_CODE(code, lino, _exit);

      // add the operation
      STFileOp op = {
          .fid = fobj->f.fid,
          .optype = TSDB_FOP_REMOVE,
          .of = fobj->f,
      };
      code = TARRAY2_APPEND(&merger->fopArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
215
    }
H
Hongze Cheng 已提交
216
  }
H
Hongze Cheng 已提交
217

H
Hongze Cheng 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  // open stt file writer
  SSttFileWriterConfig config = {
      .pTsdb = merger->tsdb,
      .maxRow = merger->maxRow,
      .szPage = merger->szPage,
      .cmprAlg = merger->cmprAlg,
      .pSkmTb = &merger->skmTb,
      .pSkmRow = &merger->skmRow,
      .aBuf = merger->aBuf,
  };
  if (lvl) {
    config.file = fobj->f;
  } else {
    config.file = (STFile){
        .type = TSDB_FTYPE_STT,
        .did = {.level = 0, .id = 0},
        .fid = fset->fid,
        .cid = merger->cid,
        .size = 0,
        .stt = {.level = merger->ctx.level, .nseg = 0},
    };
H
Hongze Cheng 已提交
239
  }
H
Hongze Cheng 已提交
240 241
  code = tsdbSttFWriterOpen(&config, &merger->sttWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
242

H
Hongze Cheng 已提交
243 244 245 246
  // open data file writer
  if (merger->ctx.toData) {
    // code = tsdbDataFWriterOpen();
    // TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
247
  }
H
Hongze Cheng 已提交
248

H
Hongze Cheng 已提交
249 250 251 252 253 254 255
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
  }
  return code;
}
static int32_t tsdbMergeFileSetEnd(SMerger *merger) {
H
Hongze Cheng 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);

  STFileOp op;
  code = tsdbSttFWriterClose(&merger->sttWriter, 0, &op);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (op.optype != TSDB_FOP_NONE) {
    code = TARRAY2_APPEND(&merger->fopArr, op);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (merger->ctx.toData) {
    // code = tsdbDataFWriterClose();
    // TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
  }
  return code;
H
Hongze Cheng 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}
static int32_t tsdbMergeFileSet(SMerger *merger, STFileSet *fset) {
  int32_t code = 0;
  int32_t lino = 0;

  if (merger->ctx.launched == false) {
    code = tsdbMergerOpen(merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  merger->ctx.fset = fset;

  code = tsdbMergeFileSetBegin(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
294 295 296 297 298 299 300 301
  // do merge
  if (merger->ctx.toData) {
    code = tsdbMergeToData(merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    code = tsdbMergeToUpperLevel(merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
302 303 304 305

  code = tsdbMergeFileSetEnd(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
306 307
_exit:
  if (code) {
H
Hongze Cheng 已提交
308
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(merger->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
309
  } else {
H
Hongze Cheng 已提交
310
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(merger->tsdb->pVnode), __func__, fset->fid);
H
Hongze Cheng 已提交
311
  }
H
Hongze Cheng 已提交
312
  return 0;
H
Hongze Cheng 已提交
313 314
}

H
Hongze Cheng 已提交
315
int32_t tsdbMerge(STsdb *tsdb) {
H
Hongze Cheng 已提交
316
  int32_t code = 0;
H
Hongze Cheng 已提交
317 318
  int32_t lino;

H
Hongze Cheng 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  SVnode       *vnode = tsdb->pVnode;
  int32_t       vid = TD_VID(vnode);
  STFileSystem *fs = tsdb->pFS;
  STFileSet    *fset;
  STFileObj    *fobj;
  int32_t       sttTrigger = vnode->config.sttTrigger;

  SMerger merger = {
      .tsdb = tsdb,
      .ctx =
          {
              .launched = false,
          },
  };

  // loop to merge each file set
  TARRAY2_FOREACH(&fs->cstate, fset) {
    SSttLvl *lvl0 = tsdbTFileSetGetLvl(fset, 0);
    if (lvl0 == NULL) {
      continue;
    }

    ASSERT(TARRAY2_SIZE(&lvl0->farr) > 0);

    fobj = TARRAY2_GET(&lvl0->farr, 0);

    if (fobj->f.stt.nseg >= sttTrigger) {
      code = tsdbMergeFileSet(&merger, fset);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
349 350
  }

H
Hongze Cheng 已提交
351 352 353 354 355
  // end the merge
  if (merger.ctx.launched) {
    code = tsdbMergerClose(&merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
356 357 358

_exit:
  if (code) {
H
Hongze Cheng 已提交
359
    tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
360 361
  } else if (merger.ctx.launched) {
    tsdbDebug("vgId:%d %s done", vid, __func__);
H
Hongze Cheng 已提交
362 363 364
  }
  return 0;
}