tsdbMerge.c 15.0 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

H
Hongze Cheng 已提交
18
typedef struct {
H
Hongze Cheng 已提交
19 20 21 22 23 24 25 26 27 28
  STsdb         *tsdb;
  TFileSetArray *fsetArr;
  int32_t        sttTrigger;
  int32_t        maxRow;
  int32_t        minRow;
  int32_t        szPage;
  int8_t         cmprAlg;
  int64_t        compactVersion;
  int64_t        cid;
  SSkmInfo       skmTb[1];
H
Hongze Cheng 已提交
29

H
Hongze Cheng 已提交
30
  // context
H
Hongze Cheng 已提交
31
  struct {
H
Hongze Cheng 已提交
32
    bool       opened;
H
Hongze Cheng 已提交
33
    STFileSet *fset;
H
Hongze Cheng 已提交
34 35
    bool       toData;
    int32_t    level;
H
Hongze Cheng 已提交
36 37
    SSttLvl   *lvl;
    STFileObj *fobj;
H
Hongze Cheng 已提交
38
    SRowInfo  *row;
H
Hongze Cheng 已提交
39
    SBlockData bData[1];
H
Hongze Cheng 已提交
40
  } ctx[1];
H
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42 43
  TFileOpArray fopArr[1];

H
Hongze Cheng 已提交
44
  // reader
H
Hongze Cheng 已提交
45
  TSttFileReaderArray sttReaderArr[1];
H
Hongze Cheng 已提交
46 47 48
  // iter
  TTsdbIterArray iterArr[1];
  SIterMerger   *iterMerger;
H
Hongze Cheng 已提交
49
  // writer
H
Hongze Cheng 已提交
50 51
  SSttFileWriter  *sttWriter;
  SDataFileWriter *dataWriter;
H
Hongze Cheng 已提交
52 53
} SMerger;

H
Hongze Cheng 已提交
54
static int32_t tsdbMergerOpen(SMerger *merger) {
H
Hongze Cheng 已提交
55 56 57 58 59 60
  merger->maxRow = merger->tsdb->pVnode->config.tsdbCfg.maxRows;
  merger->minRow = merger->tsdb->pVnode->config.tsdbCfg.minRows;
  merger->szPage = merger->tsdb->pVnode->config.tsdbPageSize;
  merger->cmprAlg = merger->tsdb->pVnode->config.tsdbCfg.compression;
  merger->compactVersion = INT64_MAX;
  tsdbFSAllocEid(merger->tsdb->pFS, &merger->cid);
H
Hongze Cheng 已提交
61
  merger->ctx->opened = true;
H
Hongze Cheng 已提交
62
  return 0;
H
Hongze Cheng 已提交
63 64
}

H
Hongze Cheng 已提交
65
static int32_t tsdbMergerClose(SMerger *merger) {
H
Hongze Cheng 已提交
66 67 68 69 70 71 72
  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
H
Hongze Cheng 已提交
73
  code = tsdbFSEditBegin(fs, merger->fopArr, TSDB_FEDIT_MERGE);
H
Hongze Cheng 已提交
74 75 76 77 78 79
  TSDB_CHECK_CODE(code, lino, _exit);

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

  // clear the merge
H
Hongze Cheng 已提交
80 81
  TARRAY2_FREE(merger->iterArr);
  TARRAY2_FREE(merger->sttReaderArr);
H
Hongze Cheng 已提交
82
  TARRAY2_FREE(merger->fopArr);
H
Hongze Cheng 已提交
83 84
  tBlockDataDestroy(merger->ctx->bData);
  tDestroyTSchema(merger->skmTb->pTSchema);
H
Hongze Cheng 已提交
85 86 87

_exit:
  if (code) {
H
Hongze Cheng 已提交
88
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
89
  }
H
Hongze Cheng 已提交
90 91
  return 0;
}
H
Hongze Cheng 已提交
92

H
Hongze Cheng 已提交
93 94
static int32_t tsdbMergeToDataTableEnd(SMerger *merger) {
  if (merger->ctx->bData->nRow == 0) return 0;
H
Hongze Cheng 已提交
95 96 97 98

  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);
H
Hongze Cheng 已提交
99 100 101 102

  if (merger->ctx->bData->nRow < merger->minRow) {
    code = tsdbSttFileWriteTSDataBlock(merger->sttWriter, merger->ctx->bData);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
103
  } else {
H
Hongze Cheng 已提交
104
    code = tsdbDataFileWriteTSDataBlock(merger->dataWriter, merger->ctx->bData);
H
Hongze Cheng 已提交
105 106
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
107
  tBlockDataClear(merger->ctx->bData);
H
Hongze Cheng 已提交
108 109 110

_exit:
  if (code) {
H
Hongze Cheng 已提交
111
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
112 113 114
  }
  return code;
}
H
Hongze Cheng 已提交
115 116

static int32_t tsdbMergeToDataTableBegin(SMerger *merger) {
H
Hongze Cheng 已提交
117 118 119 120
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);

H
Hongze Cheng 已提交
121 122
  code = tsdbUpdateSkmTb(merger->tsdb, (const TABLEID *)merger->ctx->row, merger->skmTb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
123

H
Hongze Cheng 已提交
124 125
  code = tBlockDataInit(merger->ctx->bData, (TABLEID *)merger->ctx->row, merger->skmTb->pTSchema, NULL, 0);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
126

H
Hongze Cheng 已提交
127 128 129 130 131 132 133 134 135 136 137
_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
  }
  return code;
}

static int32_t tsdbMergeToData(SMerger *merger) {
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);
H
Hongze Cheng 已提交
138

H
Hongze Cheng 已提交
139 140 141
  while ((merger->ctx->row = tsdbIterMergerGet(merger->iterMerger))) {
    if (merger->ctx->row->uid != merger->ctx->bData->uid) {
      code = tsdbMergeToDataTableEnd(merger);
H
Hongze Cheng 已提交
142 143
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
144
      code = tsdbMergeToDataTableBegin(merger);
H
Hongze Cheng 已提交
145 146
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
147

H
Hongze Cheng 已提交
148
    code = tBlockDataAppendRow(merger->ctx->bData, &merger->ctx->row->row, NULL, merger->ctx->row->uid);
H
Hongze Cheng 已提交
149 150
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
151 152
    if (merger->ctx->bData->nRow >= merger->maxRow) {
      code = tsdbDataFileWriteTSDataBlock(merger->dataWriter, merger->ctx->bData);
H
Hongze Cheng 已提交
153
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
154 155

      tBlockDataReset(merger->ctx->bData);
H
Hongze Cheng 已提交
156
    }
H
Hongze Cheng 已提交
157 158 159

    code = tsdbIterMergerNext(merger->iterMerger);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
160 161
  }

H
Hongze Cheng 已提交
162 163 164
  code = tsdbMergeToDataTableEnd(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
165 166
_exit:
  if (code) {
H
Hongze Cheng 已提交
167
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
168 169 170 171 172 173 174 175 176
  }
  return code;
}

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

H
Hongze Cheng 已提交
177 178 179
  SRowInfo *row;
  while ((row = tsdbIterMergerGet(merger->iterMerger))) {
    code = tsdbSttFileWriteTSData(merger->sttWriter, row);
H
Hongze Cheng 已提交
180 181
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
182
    code = tsdbIterMergerNext(merger->iterMerger);
H
Hongze Cheng 已提交
183 184 185 186 187 188 189 190 191 192
    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 已提交
193 194 195 196
static int32_t tsdbMergeFileSetBeginOpenReader(SMerger *merger) {
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);
H
Hongze Cheng 已提交
197

H
Hongze Cheng 已提交
198 199
  merger->ctx->toData = true;
  merger->ctx->level = 0;
H
Hongze Cheng 已提交
200 201 202 203
  TARRAY2_FOREACH(merger->ctx->fset->lvlArr, merger->ctx->lvl) {
    if (merger->ctx->lvl->level != merger->ctx->level || TARRAY2_SIZE(merger->ctx->lvl->fobjArr) == 0) {
      merger->ctx->toData = false;
      merger->ctx->lvl = NULL;
H
Hongze Cheng 已提交
204
      break;
H
Hongze Cheng 已提交
205
    }
H
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207 208 209 210
    ASSERT(merger->ctx->lvl->level == 0 || TARRAY2_SIZE(merger->ctx->lvl->fobjArr) == 1);

    merger->ctx->fobj = TARRAY2_FIRST(merger->ctx->lvl->fobjArr);
    if (merger->ctx->fobj->f->stt->nseg < merger->sttTrigger) {
H
Hongze Cheng 已提交
211
      merger->ctx->toData = false;
H
Hongze Cheng 已提交
212
      break;
H
Hongze Cheng 已提交
213
    } else {
H
Hongze Cheng 已提交
214
      merger->ctx->level++;
H
Hongze Cheng 已提交
215

H
Hongze Cheng 已提交
216 217 218 219 220 221 222 223 224
      // add the operation
      STFileOp op = {
          .optype = TSDB_FOP_REMOVE,
          .fid = merger->ctx->fset->fid,
          .of = merger->ctx->fobj->f[0],
      };
      code = TARRAY2_APPEND(merger->fopArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
225
      // open the reader
H
Hongze Cheng 已提交
226
      SSttFileReader      *reader;
H
Hongze Cheng 已提交
227
      SSttFileReaderConfig config[1] = {{
H
Hongze Cheng 已提交
228
          .tsdb = merger->tsdb,
H
Hongze Cheng 已提交
229 230 231 232
          .szPage = merger->szPage,
          .file[0] = merger->ctx->fobj->f[0],
      }};
      code = tsdbSttFReaderOpen(merger->ctx->fobj->fname, config, &reader);
H
Hongze Cheng 已提交
233
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
234

H
Hongze Cheng 已提交
235
      code = TARRAY2_APPEND(merger->sttReaderArr, reader);
H
Hongze Cheng 已提交
236
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
237 238
    }
  }
H
Hongze Cheng 已提交
239

H
Hongze Cheng 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
  }
  return code;
}

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

  SSttFileReader *sttReader;
  TARRAY2_FOREACH(merger->sttReaderArr, sttReader) {
    const TSttSegReaderArray *segReaderArr;

    code = tsdbSttFReaderGetSegReader(sttReader, &segReaderArr);
    TSDB_CHECK_CODE(code, lino, _exit);

    SSttSegReader *segReader;
    TARRAY2_FOREACH(segReaderArr, segReader) {
      STsdbIter *iter;

      STsdbIterConfig config[1] = {{
          .type = TSDB_ITER_TYPE_STT,
          .sttReader = segReader,
      }};

      code = tsdbIterOpen(config, &iter);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = TARRAY2_APPEND(merger->iterArr, iter);
H
Hongze Cheng 已提交
272
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
273
    }
H
Hongze Cheng 已提交
274
  }
H
Hongze Cheng 已提交
275

H
Hongze Cheng 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  code = tsdbIterMergerInit(merger->iterArr, &merger->iterMerger);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
  }
  return code;
}

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

  SDiskID did = {
      .level = 0,
      .id = 0,
  };  // TODO

  if (merger->ctx->lvl) {  // to existing level
    SSttFileWriterConfig config[1] = {{
H
Hongze Cheng 已提交
298
        .tsdb = merger->tsdb,
H
Hongze Cheng 已提交
299 300 301
        .maxRow = merger->maxRow,
        .szPage = merger->szPage,
        .cmprAlg = merger->cmprAlg,
H
Hongze Cheng 已提交
302 303 304 305
        .compactVersion = merger->compactVersion,
        .file = merger->ctx->fobj->f[0],
    }};
    code = tsdbSttFileWriterOpen(config, &merger->sttWriter);
H
Hongze Cheng 已提交
306
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
307 308
  } else {  // to new level
    SSttFileWriterConfig config[1] = {{
H
Hongze Cheng 已提交
309
        .tsdb = merger->tsdb,
H
Hongze Cheng 已提交
310 311 312
        .maxRow = merger->maxRow,
        .szPage = merger->szPage,
        .cmprAlg = merger->cmprAlg,
H
Hongze Cheng 已提交
313
        .compactVersion = merger->compactVersion,
H
Hongze Cheng 已提交
314
        .file =
H
Hongze Cheng 已提交
315
            {
H
Hongze Cheng 已提交
316
                .type = TSDB_FTYPE_STT,
H
Hongze Cheng 已提交
317 318
                .did = did,
                .fid = merger->ctx->fset->fid,
H
Hongze Cheng 已提交
319 320
                .cid = merger->cid,
                .size = 0,
H
Hongze Cheng 已提交
321 322 323 324
                .stt = {{
                    .level = merger->ctx->level,
                    .nseg = 0,
                }},
H
Hongze Cheng 已提交
325
            },
H
Hongze Cheng 已提交
326 327
    }};
    code = tsdbSttFileWriterOpen(config, &merger->sttWriter);
H
Hongze Cheng 已提交
328
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
329 330
  }

H
Hongze Cheng 已提交
331 332 333
  if (merger->ctx->toData) {  // TODO
    tBlockDataReset(merger->ctx->bData);

H
Hongze Cheng 已提交
334
    SDataFileWriterConfig config = {
H
Hongze Cheng 已提交
335
        .tsdb = merger->tsdb,
H
Hongze Cheng 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        .maxRow = merger->maxRow,
        .f =
            {
                [0] =
                    {
                        .type = TSDB_FTYPE_HEAD,
                        .did = did,
                        .fid = merger->ctx->fset->fid,
                        .cid = merger->cid,
                        .size = 0,
                    },
                [1] =
                    {
                        .type = TSDB_FTYPE_DATA,
                        .did = did,
                        .fid = merger->ctx->fset->fid,
                        .cid = merger->cid,
                        .size = 0,
                    },
                [2] =
                    {
                        .type = TSDB_FTYPE_SMA,
                        .did = did,
                        .fid = merger->ctx->fset->fid,
                        .cid = merger->cid,
                        .size = 0,
                    },
                [3] =
                    {
                        .type = TSDB_FTYPE_TOMB,
                        .did = did,
                        .fid = merger->ctx->fset->fid,
                        .cid = merger->cid,
                        .size = 0,
                    },
            },
H
Hongze Cheng 已提交
372 373 374
    };
    code = tsdbDataFileWriterOpen(&config, &merger->dataWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
375
  }
H
Hongze Cheng 已提交
376

H
Hongze Cheng 已提交
377 378
_exit:
  if (code) {
H
Hongze Cheng 已提交
379
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
380 381 382
  }
  return code;
}
H
Hongze Cheng 已提交
383 384

static int32_t tsdbMergeFileSetBegin(SMerger *merger) {
H
Hongze Cheng 已提交
385 386 387 388
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);

H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396
  ASSERT(TARRAY2_SIZE(merger->sttReaderArr) == 0);
  ASSERT(TARRAY2_SIZE(merger->iterArr) == 0);
  ASSERT(merger->iterMerger == NULL);
  ASSERT(merger->sttWriter == NULL);
  ASSERT(merger->dataWriter == NULL);

  // open reader
  code = tsdbMergeFileSetBeginOpenReader(merger);
H
Hongze Cheng 已提交
397 398
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
399 400 401 402 403 404 405 406 407 408 409
  // open iterator
  code = tsdbMergeFileSetBeginOpenIter(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

  // open writer
  code = tsdbMergeFileSetBeginOpenWriter(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
410
  }
H
Hongze Cheng 已提交
411 412 413 414 415 416 417 418 419
  return code;
}

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

  STFileOp op[1];
H
Hongze Cheng 已提交
420

H
Hongze Cheng 已提交
421
  if (merger->ctx->toData) {
H
Hongze Cheng 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    code = tsdbDataFileWriterClose(&merger->dataWriter, 0, op);
    TSDB_CHECK_CODE(code, lino, _exit);

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

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

  if (op->optype != TSDB_FOP_NONE) {
    code = TARRAY2_APPEND_PTR(merger->fopArr, op);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
437 438 439 440
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
441
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
442 443
  }
  return code;
H
Hongze Cheng 已提交
444
}
H
Hongze Cheng 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457

static int32_t tsdbMergeFileSetEndCloseIter(SMerger *merger) {
  tsdbIterMergerClear(&merger->iterMerger);
  TARRAY2_CLEAR(merger->iterArr, tsdbIterClose);
  return 0;
}

static int32_t tsdbMergeFileSetEndCloseReader(SMerger *merger) {
  TARRAY2_CLEAR(merger->sttReaderArr, tsdbSttFReaderClose);
  return 0;
}

static int32_t tsdbMergeFileSetEnd(SMerger *merger) {
H
Hongze Cheng 已提交
458 459
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
460
  int32_t vid = TD_VID(merger->tsdb->pVnode);
H
Hongze Cheng 已提交
461

H
Hongze Cheng 已提交
462 463 464 465 466 467 468 469 470 471 472 473
  code = tsdbMergeFileSetEndCloseWriter(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

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

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

_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
474
  }
H
Hongze Cheng 已提交
475 476
  return code;
}
H
Hongze Cheng 已提交
477

H
Hongze Cheng 已提交
478 479 480
static int32_t tsdbMergeFileSet(SMerger *merger, STFileSet *fset) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
481

H
Hongze Cheng 已提交
482
  merger->ctx->fset = fset;
H
Hongze Cheng 已提交
483 484 485
  code = tsdbMergeFileSetBegin(merger);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
486
  // do merge
H
Hongze Cheng 已提交
487
  if (merger->ctx->toData) {
H
Hongze Cheng 已提交
488 489 490 491 492 493
    code = tsdbMergeToData(merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    code = tsdbMergeToUpperLevel(merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
494 495 496 497

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

H
Hongze Cheng 已提交
498 499
_exit:
  if (code) {
H
Hongze Cheng 已提交
500
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(merger->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
501
  } else {
H
Hongze Cheng 已提交
502
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(merger->tsdb->pVnode), __func__, fset->fid);
H
Hongze Cheng 已提交
503
  }
H
Hongze Cheng 已提交
504
  return 0;
H
Hongze Cheng 已提交
505 506
}

H
Hongze Cheng 已提交
507
static int32_t tsdbDoMerge(SMerger *merger) {
H
Hongze Cheng 已提交
508
  int32_t code = 0;
H
Hongze Cheng 已提交
509 510
  int32_t lino = 0;
  int32_t vid = TD_VID(merger->tsdb->pVnode);
H
Hongze Cheng 已提交
511

H
Hongze Cheng 已提交
512 513 514 515
  STFileSet *fset;
  SSttLvl   *lvl;
  STFileObj *fobj;
  TARRAY2_FOREACH(merger->fsetArr, fset) {
H
Hongze Cheng 已提交
516
    lvl = TARRAY2_SIZE(fset->lvlArr) > 0 ? TARRAY2_FIRST(fset->lvlArr) : NULL;
H
Hongze Cheng 已提交
517
    if (!lvl || lvl->level != 0 || TARRAY2_SIZE(lvl->fobjArr) == 0) continue;
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519 520
    fobj = TARRAY2_FIRST(lvl->fobjArr);
    if (fobj->f->stt->nseg < merger->sttTrigger) continue;
H
Hongze Cheng 已提交
521

H
Hongze Cheng 已提交
522 523 524 525 526
    if (!merger->ctx->opened) {
      code = tsdbMergerOpen(merger);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
527 528
    code = tsdbMergeFileSet(merger, fset);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
529 530
  }

H
Hongze Cheng 已提交
531 532
  if (merger->ctx->opened) {
    code = tsdbMergerClose(merger);
H
Hongze Cheng 已提交
533 534
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
535

H
Hongze Cheng 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
  } else {
    tsdbDebug("vgId:%d %s done", vid, __func__);
  }
  return code;
}

int32_t tsdbMerge(STsdb *tsdb) {
  int32_t code = 0;
  int32_t lino = 0;
  int32_t vid = TD_VID(tsdb->pVnode);

  SMerger merger[1] = {{
      .tsdb = tsdb,
      .sttTrigger = tsdb->pVnode->config.sttTrigger,
  }};

H
Hongze Cheng 已提交
555
  code = tsdbFSCreateCopySnapshot(tsdb->pFS, &merger->fsetArr);
H
Hongze Cheng 已提交
556 557 558 559 560
  TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
561
  tsdbFSDestroyCopySnapshot(&merger->fsetArr);
H
Hongze Cheng 已提交
562

H
Hongze Cheng 已提交
563 564
_exit:
  if (code) {
H
Hongze Cheng 已提交
565 566
    TSDB_ERROR_LOG(vid, lino, code);
  } else if (merger->ctx->opened) {
H
Hongze Cheng 已提交
567
    tsdbDebug("vgId:%d %s done", vid, __func__);
H
Hongze Cheng 已提交
568 569 570
  }
  return 0;
}