vnodeCompact.c 3.1 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 "vnd.h"

H
Hongze Cheng 已提交
18 19
extern int32_t tsdbCommitCompact(STsdb *pTsdb);

H
Hongze Cheng 已提交
20
static int32_t vnodeCompactTask(void *param) {
H
Hongze Cheng 已提交
21
  int32_t code = 0;
H
Hongze Cheng 已提交
22
  int32_t lino = 0;
H
Hongze Cheng 已提交
23

H
Hongze Cheng 已提交
24 25
  SCompactInfo *pInfo = (SCompactInfo *)param;
  SVnode       *pVnode = pInfo->pVnode;
H
Hongze Cheng 已提交
26

H
Hongze Cheng 已提交
27 28
  // do compact
  code = tsdbCompact(pInfo->pVnode->pTsdb, pInfo);
H
Hongze Cheng 已提交
29
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
30

H
Hongze Cheng 已提交
31 32 33 34
  // end compact
  char dir[TSDB_FILENAME_LEN] = {0};
  if (pVnode->pTfs) {
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path);
H
Hongze Cheng 已提交
35
  } else {
H
Hongze Cheng 已提交
36
    snprintf(dir, TSDB_FILENAME_LEN, "%s", pVnode->path);
H
Hongze Cheng 已提交
37
  }
H
Hongze Cheng 已提交
38

H
Hongze Cheng 已提交
39 40
  vnodeCommitInfo(dir);

H
Hongze Cheng 已提交
41 42
  tsdbCommitCompact(pVnode->pTsdb);

H
Hongze Cheng 已提交
43 44
_exit:
  tsem_post(&pInfo->pVnode->canCommit);
H
Hongze Cheng 已提交
45
  taosMemoryFree(pInfo);
H
Hongze Cheng 已提交
46 47
  return code;
}
H
Hongze Cheng 已提交
48
static int32_t vnodePrepareCompact(SVnode *pVnode, SCompactInfo *pInfo) {
H
Hongze Cheng 已提交
49
  int32_t code = 0;
H
Hongze Cheng 已提交
50
  int32_t lino = 0;
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52
  tsem_wait(&pVnode->canCommit);
H
Hongze Cheng 已提交
53

H
Hongze Cheng 已提交
54 55 56
  pInfo->pVnode = pVnode;
  pInfo->flag = 0;
  pInfo->commitID = ++pVnode->state.commitID;
H
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58 59 60 61 62 63 64 65 66
  char       dir[TSDB_FILENAME_LEN] = {0};
  SVnodeInfo info = {0};

  if (pVnode->pTfs) {
    snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path);
  } else {
    snprintf(dir, TSDB_FILENAME_LEN, "%s", pVnode->path);
  }

H
Hongze Cheng 已提交
67 68 69 70 71
  if (vnodeLoadInfo(dir, &info) < 0) {
    code = terrno;
    goto _exit;
  }

H
Hongze Cheng 已提交
72
  info.state.commitID = pInfo->commitID;
H
Hongze Cheng 已提交
73 74 75 76 77

  if (vnodeSaveInfo(dir, &info) < 0) {
    code = terrno;
    goto _exit;
  }
H
Hongze Cheng 已提交
78 79

_exit:
H
Hongze Cheng 已提交
80
  if (code) {
H
Hongze Cheng 已提交
81
    vError("vgId:%d %s failed at line %d since %s, commit ID:%" PRId64, TD_VID(pVnode), __func__, lino, tstrerror(code),
H
Hongze Cheng 已提交
82 83
           pVnode->state.commitID);
  } else {
H
Hongze Cheng 已提交
84
    vDebug("vgId:%d %s done, commit ID:%" PRId64, TD_VID(pVnode), __func__, pVnode->state.commitID);
H
Hongze Cheng 已提交
85
  }
H
Hongze Cheng 已提交
86 87 88 89
  return code;
}
int32_t vnodeAsyncCompact(SVnode *pVnode) {
  int32_t code = 0;
H
Hongze Cheng 已提交
90
  int32_t lino = 0;
H
Hongze Cheng 已提交
91

H
Hongze Cheng 已提交
92 93
  SCompactInfo *pInfo = taosMemoryCalloc(1, sizeof(*pInfo));
  if (pInfo == NULL) {
H
Hongze Cheng 已提交
94
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
95
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
96
  }
H
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100 101 102
  vnodeAsyncCommit(pVnode);

  code = vnodePrepareCompact(pVnode, pInfo);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
103 104 105 106
  vnodeScheduleTask(vnodeCompactTask, pInfo);

_exit:
  if (code) {
H
Hongze Cheng 已提交
107 108 109 110
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
    if (pInfo) taosMemoryFree(pInfo);
  } else {
    vInfo("vgId:%d %s done", TD_VID(pVnode), __func__);
H
Hongze Cheng 已提交
111 112 113 114 115 116 117 118 119 120
  }
  return code;
}

int32_t vnodeSyncCompact(SVnode *pVnode) {
  vnodeAsyncCompact(pVnode);
  tsem_wait(&pVnode->canCommit);
  tsem_post(&pVnode->canCommit);
  return 0;
}