vnodeCommit.c 8.1 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 "vnd.h"
H
refact  
Hongze Cheng 已提交
17

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

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

H
Hongze Cheng 已提交
28
int vnodeBegin(SVnode *pVnode) {
H
Hongze Cheng 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
  // alloc buffer pool
  /* pthread_mutex_lock(); */

  while (pVnode->pPool == NULL) {
    /* pthread_cond_wait(); */
  }

  pVnode->inUse = pVnode->pPool;
  pVnode->pPool = pVnode->inUse->next;
  pVnode->inUse->next = NULL;
  /* ref pVnode->inUse buffer pool */

  /* pthread_mutex_unlock(); */
H
Hongze Cheng 已提交
42 43 44

  // begin meta
  if (metaBegin(pVnode->pMeta) < 0) {
H
Hongze Cheng 已提交
45
    vError("vgId:%d failed to begin meta since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
46 47 48 49
    return -1;
  }

  // begin tsdb
C
Cary Xu 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  if (vnodeIsRollup(pVnode)) {
    if (tsdbBegin(VND_RSMA0(pVnode)) < 0) {
      vError("vgId:%d failed to begin rsma0 since %s", TD_VID(pVnode), tstrerror(terrno));
      return -1;
    }

    if (tsdbBegin(VND_RSMA1(pVnode)) < 0) {
      vError("vgId:%d failed to begin rsma1 since %s", TD_VID(pVnode), tstrerror(terrno));
      return -1;
    }

    if (tsdbBegin(VND_RSMA2(pVnode)) < 0) {
      vError("vgId:%d failed to begin rsma2 since %s", TD_VID(pVnode), tstrerror(terrno));
      return -1;
    }
  } else {
    if (tsdbBegin(pVnode->pTsdb) < 0) {
      vError("vgId:%d failed to begin tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
      return -1;
    }
H
Hongze Cheng 已提交
70 71 72 73 74
  }

  return 0;
}

H
Hongze Cheng 已提交
75
int vnodeShouldCommit(SVnode *pVnode) { return pVnode->inUse->size > pVnode->config.szBuf / 3; }
H
Hongze Cheng 已提交
76

H
Hongze Cheng 已提交
77 78 79
int vnodeSaveInfo(const char *dir, const SVnodeInfo *pInfo) {
  char      fname[TSDB_FILENAME_LEN];
  TdFilePtr pFile;
H
Hongze Cheng 已提交
80
  char     *data;
H
Hongze Cheng 已提交
81 82 83 84 85 86

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

  // encode info
  data = NULL;

H
Hongze Cheng 已提交
87
  if (vnodeEncodeInfo(pInfo, &data) < 0) {
H
Hongze Cheng 已提交
88 89 90 91 92 93 94 95 96 97
    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;
  }

H
Hongze Cheng 已提交
98
  if (taosWriteFile(pFile, data, strlen(data)) < 0) {
H
Hongze Cheng 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    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);

H
Hongze Cheng 已提交
113
  vInfo("vgId:%d vnode info is saved, fname: %s", pInfo->config.vgId, fname);
H
Hongze Cheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  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;
  }

H
Hongze Cheng 已提交
135
  vInfo("vgId:%d vnode info is committed", pInfo->config.vgId);
H
Hongze Cheng 已提交
136 137 138 139

  return 0;
}

H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
int vnodeLoadInfo(const char *dir, SVnodeInfo *pInfo) {
  char      fname[TSDB_FILENAME_LEN];
  TdFilePtr pFile = NULL;
  char     *pData = NULL;
  int64_t   size;

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

  // read info
  pFile = taosOpenFile(fname, TD_FILE_READ);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (taosFStatFile(pFile, &size, NULL) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
160
  pData = taosMemoryMalloc(size + 1);
H
Hongze Cheng 已提交
161 162 163 164 165 166 167 168 169 170
  if (pData == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  if (taosReadFile(pFile, pData, size) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
171 172
  pData[size] = '\0';

H
Hongze Cheng 已提交
173 174 175 176 177 178 179 180 181 182
  taosCloseFile(&pFile);

  // decode info
  if (vnodeDecodeInfo(pData, pInfo) < 0) {
    taosMemoryFree(pData);
    return -1;
  }

  taosMemoryFree(pData);

H
Hongze Cheng 已提交
183
  return 0;
H
Hongze Cheng 已提交
184 185 186 187 188

_err:
  taosCloseFile(&pFile);
  taosMemoryFree(pData);
  return -1;
H
Hongze Cheng 已提交
189 190
}

H
refact  
Hongze Cheng 已提交
191
int vnodeAsyncCommit(SVnode *pVnode) {
H
Hongze Cheng 已提交
192 193
  vnodeWaitCommit(pVnode);

H
Hongze Cheng 已提交
194
  // vnodeBufPoolSwitch(pVnode);
H
Hongze Cheng 已提交
195
  // tsdbPrepareCommit(pVnode->pTsdb);
H
more  
Hongze Cheng 已提交
196

H
Hongze Cheng 已提交
197
  vnodeScheduleTask(vnodeCommitImpl, pVnode);
H
Hongze Cheng 已提交
198

H
Hongze Cheng 已提交
199 200
  return 0;
}
H
refact  
Hongze Cheng 已提交
201

H
Hongze Cheng 已提交
202 203 204
int vnodeSyncCommit(SVnode *pVnode) {
  vnodeAsyncCommit(pVnode);
  vnodeWaitCommit(pVnode);
H
Hongze Cheng 已提交
205
  tsem_post(&(pVnode->canCommit));
H
Hongze Cheng 已提交
206 207 208
  return 0;
}

H
Hongze Cheng 已提交
209
int vnodeCommit(SVnode *pVnode) {
210
  SVnodeInfo info = {0};
H
Hongze Cheng 已提交
211 212
  char       dir[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
213 214
  vInfo("vgId:%d start to commit, version: %" PRId64, TD_VID(pVnode), pVnode->state.applied);

H
Hongze Cheng 已提交
215 216 217 218 219
  pVnode->onCommit = pVnode->inUse;
  pVnode->inUse = NULL;

  // save info
  info.config = pVnode->config;
H
Hongze Cheng 已提交
220
  info.state.committed = pVnode->state.applied;
H
Hongze Cheng 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path);
  if (vnodeSaveInfo(dir, &info) < 0) {
    ASSERT(0);
    return -1;
  }

  // commit each sub-system
  if (metaCommit(pVnode->pMeta) < 0) {
    ASSERT(0);
    return -1;
  }
  if (tsdbCommit(pVnode->pTsdb) < 0) {
    ASSERT(0);
    return -1;
  }
  if (tqCommit(pVnode->pTq) < 0) {
    ASSERT(0);
    return -1;
  }
  // walCommit (TODO)

  // commit info
  if (vnodeCommitInfo(dir, &info) < 0) {
    ASSERT(0);
    return -1;
  }

  // apply the commit (TODO)
  vnodeBufPoolReset(pVnode->onCommit);
  pVnode->onCommit->next = pVnode->pPool;
  pVnode->pPool = pVnode->onCommit;
  pVnode->onCommit = NULL;

H
Hongze Cheng 已提交
254 255
  vInfo("vgId:%d commit over", TD_VID(pVnode));

H
Hongze Cheng 已提交
256 257 258 259
  return 0;
}

static int vnodeCommitImpl(void *arg) {
H
Hongze Cheng 已提交
260
  SVnode *pVnode = (SVnode *)arg;
H
refact  
Hongze Cheng 已提交
261

H
Hongze Cheng 已提交
262
  // metaCommit(pVnode->pMeta);
H
more  
Hongze Cheng 已提交
263
  tqCommit(pVnode->pTq);
H
more  
Hongze Cheng 已提交
264
  tsdbCommit(pVnode->pTsdb);
H
more  
Hongze Cheng 已提交
265

H
Hongze Cheng 已提交
266
  // vnodeBufPoolRecycle(pVnode);
H
more  
Hongze Cheng 已提交
267
  tsem_post(&(pVnode->canCommit));
H
refact  
Hongze Cheng 已提交
268 269 270 271 272 273 274 275 276 277 278
  return 0;
}

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

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

H
Hongze Cheng 已提交
281 282
static FORCE_INLINE void vnodeWaitCommit(SVnode *pVnode) { tsem_wait(&pVnode->canCommit); }

H
Hongze Cheng 已提交
283 284 285 286
static int vnodeEncodeState(const void *pObj, SJson *pJson) {
  const SVState *pState = (SVState *)pObj;

  if (tjsonAddIntegerToObject(pJson, "commit version", pState->committed) < 0) return -1;
H
Hongze Cheng 已提交
287
  if (tjsonAddIntegerToObject(pJson, "applied version", pState->applied) < 0) return -1;
H
Hongze Cheng 已提交
288 289 290 291 292 293 294 295

  return 0;
}

static int vnodeDecodeState(const SJson *pJson, void *pObj) {
  SVState *pState = (SVState *)pObj;

  if (tjsonGetNumberValue(pJson, "commit version", pState->committed) < 0) return -1;
H
Hongze Cheng 已提交
296
  if (tjsonGetNumberValue(pJson, "applied version", pState->applied) < 0) return -1;
H
Hongze Cheng 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

  return 0;
}

static int vnodeEncodeInfo(const SVnodeInfo *pInfo, char **ppData) {
  SJson *pJson;
  char  *pData;

  *ppData = NULL;

  pJson = tjsonCreateObject();
  if (pJson == NULL) {
    return -1;
  }

  if (tjsonAddObject(pJson, "config", vnodeEncodeConfig, (void *)&pInfo->config) < 0) {
    goto _err;
  }

  if (tjsonAddObject(pJson, "state", vnodeEncodeState, (void *)&pInfo->state) < 0) {
    goto _err;
  }

  pData = tjsonToString(pJson);
  if (pData == NULL) {
    goto _err;
  }

  tjsonDelete(pJson);

  *ppData = pData;
H
Hongze Cheng 已提交
328
  return 0;
H
Hongze Cheng 已提交
329 330 331 332

_err:
  tjsonDelete(pJson);
  return -1;
H
Hongze Cheng 已提交
333 334
}

H
Hongze Cheng 已提交
335
static int vnodeDecodeInfo(uint8_t *pData, SVnodeInfo *pInfo) {
H
Hongze Cheng 已提交
336 337
  SJson *pJson = NULL;

H
fix bug  
Hongze Cheng 已提交
338
  pJson = tjsonParse(pData);
H
Hongze Cheng 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352
  if (pJson == NULL) {
    return -1;
  }

  if (tjsonToObject(pJson, "config", vnodeDecodeConfig, (void *)&pInfo->config) < 0) {
    goto _err;
  }

  if (tjsonToObject(pJson, "state", vnodeDecodeState, (void *)&pInfo->state) < 0) {
    goto _err;
  }

  tjsonDelete(pJson);

H
Hongze Cheng 已提交
353
  return 0;
H
Hongze Cheng 已提交
354 355 356 357

_err:
  tjsonDelete(pJson);
  return -1;
H
Hongze Cheng 已提交
358
}