vnodeCommit.c 3.5 KB
Newer Older
H
refact  
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 "vnodeInt.h"
H
refact  
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19 20 21 22
#define VND_INFO_FNAME     "vnode.json"
#define VND_INFO_FNAME_TMP "vnode_tmp.json"

static int  vnodeEncodeInfo(const SVnodeInfo *pInfo, uint8_t **ppData, int *len);
static int  vnodeDecodeInfo(uint8_t *pData, int len, SVnodeInfo *pInfo);
H
Hongze Cheng 已提交
23 24 25 26
static int  vnodeStartCommit(SVnode *pVnode);
static int  vnodeEndCommit(SVnode *pVnode);
static int  vnodeCommit(void *arg);
static void vnodeWaitCommit(SVnode *pVnode);
H
refact  
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
int vnodeSaveInfo(const char *dir, const SVnodeInfo *pInfo) {
  char      fname[TSDB_FILENAME_LEN];
  TdFilePtr pFile;
  uint8_t  *data;
  int       len;

  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);

  // encode info
  data = NULL;
  len = 0;

  if (vnodeEncodeInfo(pInfo, &data, &len) < 0) {
    return -1;
  }

  // save info to a vnode_tmp.json
  pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (taosWriteFile(pFile, data, len) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (taosFsyncFile(pFile) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  taosCloseFile(&pFile);

  // free info binary
  taosMemoryFree(data);

  vInfo("vgId: %d vnode info is saved, fname: %s", pInfo->config.vgId, fname);

  return 0;

_err:
  taosCloseFile(&pFile);
  taosMemoryFree(data);
  return -1;
}

int vnodeCommitInfo(const char *dir, const SVnodeInfo *pInfo) {
  char fname[TSDB_FILENAME_LEN];
  char tfname[TSDB_FILENAME_LEN];

  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME);
  snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP);

  if (taosRenameFile(tfname, fname) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  vInfo("vgId: %d vnode info is committed", pInfo->config.vgId);

  return 0;
}

int vnodeLoadInfo(const char *dir) {
  // TODO
  return 0;
}

H
refact  
Hongze Cheng 已提交
98
int vnodeAsyncCommit(SVnode *pVnode) {
H
Hongze Cheng 已提交
99 100
  vnodeWaitCommit(pVnode);

H
Hongze Cheng 已提交
101
  vnodeBufPoolSwitch(pVnode);
H
more  
Hongze Cheng 已提交
102 103
  tsdbPrepareCommit(pVnode->pTsdb);

H
Hongze Cheng 已提交
104 105
  vnodeScheduleTask(vnodeCommit, pVnode);

H
Hongze Cheng 已提交
106 107
  return 0;
}
H
refact  
Hongze Cheng 已提交
108

H
Hongze Cheng 已提交
109 110 111
int vnodeSyncCommit(SVnode *pVnode) {
  vnodeAsyncCommit(pVnode);
  vnodeWaitCommit(pVnode);
H
Hongze Cheng 已提交
112
  tsem_post(&(pVnode->canCommit));
H
Hongze Cheng 已提交
113 114 115 116
  return 0;
}

static int vnodeCommit(void *arg) {
H
Hongze Cheng 已提交
117
  SVnode *pVnode = (SVnode *)arg;
H
refact  
Hongze Cheng 已提交
118

H
Hongze Cheng 已提交
119
  // metaCommit(pVnode->pMeta);
H
more  
Hongze Cheng 已提交
120
  tqCommit(pVnode->pTq);
H
more  
Hongze Cheng 已提交
121
  tsdbCommit(pVnode->pTsdb);
H
more  
Hongze Cheng 已提交
122

H
Hongze Cheng 已提交
123
  vnodeBufPoolRecycle(pVnode);
H
more  
Hongze Cheng 已提交
124
  tsem_post(&(pVnode->canCommit));
H
refact  
Hongze Cheng 已提交
125 126 127 128 129 130 131 132 133 134 135
  return 0;
}

static int vnodeStartCommit(SVnode *pVnode) {
  // TODO
  return 0;
}

static int vnodeEndCommit(SVnode *pVnode) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
136 137
}

H
Hongze Cheng 已提交
138 139 140 141 142 143 144 145 146 147 148
static FORCE_INLINE void vnodeWaitCommit(SVnode *pVnode) { tsem_wait(&pVnode->canCommit); }

static int vnodeEncodeInfo(const SVnodeInfo *pInfo, uint8_t **ppData, int *len) {
  // TODO
  return 0;
}

static int vnodeDecodeInfo(uint8_t *pData, int len, SVnodeInfo *pInfo) {
  // TODO
  return 0;
}