tsdbMerge.c 3.7 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
  bool launched;
} SMergeCtx;
H
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22 23 24 25
typedef struct {
  STsdb       *tsdb;
  SMergeCtx    ctx;
  TFileOpArray fopArr;
H
Hongze Cheng 已提交
26 27
} SMerger;

H
Hongze Cheng 已提交
28 29 30
static int32_t tsdbMergerOpen(SMerger *merger) {
  merger->ctx.launched = true;
  TARRAY2_INIT(&merger->fopArr);
H
Hongze Cheng 已提交
31
  return 0;
H
Hongze Cheng 已提交
32 33
}

H
Hongze Cheng 已提交
34
static int32_t tsdbMergerClose(SMerger *merger) {
H
Hongze Cheng 已提交
35
  // TODO
H
Hongze Cheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  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 已提交
56 57
  return 0;
}
H
Hongze Cheng 已提交
58

H
Hongze Cheng 已提交
59
static int32_t tsdbMergeFileSet(SMerger *merger, STFileSet *fset) {
H
Hongze Cheng 已提交
60
  int32_t code = 0;
H
Hongze Cheng 已提交
61
  int32_t lino = 0;
H
Hongze Cheng 已提交
62

H
Hongze Cheng 已提交
63 64 65 66
  if (merger->ctx.launched == false) {
    code = tsdbMergerOpen(merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  {  // prepare the merger file set
    SSttLvl   *lvl;
    STFileObj *fobj;
    TARRAY2_FOREACH(&fset->lvlArr, lvl) {
      TARRAY2_FOREACH(&lvl->farr, fobj) {
        if (fobj->f.stt.nseg >= merger->tsdb->pVnode->config.sttTrigger) {
          STFileOp op = {
              .fid = fset->fid,
              .optype = TSDB_FOP_REMOVE,
              .of = fobj->f,
          };

          code = TARRAY2_APPEND(&merger->fopArr, op);
          TSDB_CHECK_CODE(code, lino, _exit);
        } else {
          if (lvl->level == 0) {
            continue;
          } else {
            // TODO
          }
        }
      }
    }
  }
H
Hongze Cheng 已提交
92

H
Hongze Cheng 已提交
93 94 95 96 97 98
  {
      // do merge the file set
  }

  {  // end merge the file set
  }
H
Hongze Cheng 已提交
99 100 101

_exit:
  if (code) {
H
Hongze Cheng 已提交
102
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(merger->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
103
  } else {
H
Hongze Cheng 已提交
104
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(merger->tsdb->pVnode), __func__, fset->fid);
H
Hongze Cheng 已提交
105
  }
H
Hongze Cheng 已提交
106
  return 0;
H
Hongze Cheng 已提交
107 108
}

H
Hongze Cheng 已提交
109
int32_t tsdbMerge(STsdb *tsdb) {
H
Hongze Cheng 已提交
110
  int32_t code = 0;
H
Hongze Cheng 已提交
111 112
  int32_t lino;

H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  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 已提交
143 144
  }

H
Hongze Cheng 已提交
145 146 147 148 149
  // end the merge
  if (merger.ctx.launched) {
    code = tsdbMergerClose(&merger);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
150 151 152

_exit:
  if (code) {
H
Hongze Cheng 已提交
153
    tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
154
  } else {
H
Hongze Cheng 已提交
155
    tsdbDebug("vgId:%d %s done, do merge: %d", vid, __func__, merger.ctx.launched);
H
Hongze Cheng 已提交
156 157 158
  }
  return 0;
}