vnodeVersion.c 3.0 KB
Newer Older
S
TD-1762  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
#include "cJSON.h"
#include "tglobal.h"
#include "vnodeVersion.h"

22
int32_t vnodeReadVersion(SVnode *pVnode) {
S
TD-1762  
Shengliang Guan 已提交
23 24 25 26 27 28
  int32_t len = 0;
  int32_t maxLen = 100;
  char *  content = calloc(1, maxLen + 1);
  cJSON * root = NULL;
  FILE *  fp = NULL;

S
TD-1762  
Shengliang Guan 已提交
29
  terrno = TSDB_CODE_VND_INVALID_VRESION_FILE;
S
TD-1762  
Shengliang Guan 已提交
30 31 32 33 34 35 36
  char file[TSDB_FILENAME_LEN + 30] = {0};
  sprintf(file, "%s/vnode%d/version.json", tsVnodeDir, pVnode->vgId);

  fp = fopen(file, "r");
  if (!fp) {
    if (errno != ENOENT) {
      vError("vgId:%d, failed to read %s, error:%s", pVnode->vgId, file, strerror(errno));
S
TD-1762  
Shengliang Guan 已提交
37
      terrno = TAOS_SYSTEM_ERROR(errno);
S
TD-1762  
Shengliang Guan 已提交
38
    } else {
S
TD-1762  
Shengliang Guan 已提交
39
      terrno = TSDB_CODE_SUCCESS;
S
TD-1762  
Shengliang Guan 已提交
40 41 42 43
    }
    goto PARSE_VER_ERROR;
  }

S
TD-1207  
Shengliang Guan 已提交
44
  len = (int32_t)fread(content, 1, maxLen, fp);
S
TD-1762  
Shengliang Guan 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  if (len <= 0) {
    vError("vgId:%d, failed to read %s, content is null", pVnode->vgId, file);
    goto PARSE_VER_ERROR;
  }

  root = cJSON_Parse(content);
  if (root == NULL) {
    vError("vgId:%d, failed to read %s, invalid json format", pVnode->vgId, file);
    goto PARSE_VER_ERROR;
  }

  cJSON *ver = cJSON_GetObjectItem(root, "version");
  if (!ver || ver->type != cJSON_Number) {
    vError("vgId:%d, failed to read %s, version not found", pVnode->vgId, file);
    goto PARSE_VER_ERROR;
  }
S
Shengliang Guan 已提交
61
#if 0  
62
  pVnode->version = (uint64_t)ver->valueint;
S
TD-1762  
Shengliang Guan 已提交
63

S
TD-1762  
Shengliang Guan 已提交
64
  terrno = TSDB_CODE_SUCCESS;
S
Shengliang Guan 已提交
65
  vInfo("vgId:%d, read %s successfully, fver:%" PRIu64, pVnode->vgId, file, pVnode->version);
S
Shengliang Guan 已提交
66
#endif
S
TD-1762  
Shengliang Guan 已提交
67 68 69 70 71 72

PARSE_VER_ERROR:
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (fp != NULL) fclose(fp);

S
TD-1762  
Shengliang Guan 已提交
73
  return terrno;
S
TD-1762  
Shengliang Guan 已提交
74 75
}

76
int32_t vnodeSaveVersion(SVnode *pVnode) {
S
TD-1762  
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
  char file[TSDB_FILENAME_LEN + 30] = {0};
  sprintf(file, "%s/vnode%d/version.json", tsVnodeDir, pVnode->vgId);

  FILE *fp = fopen(file, "w");
  if (!fp) {
    vError("vgId:%d, failed to write %s, reason:%s", pVnode->vgId, file, strerror(errno));
    return -1;
  }

  int32_t len = 0;
  int32_t maxLen = 100;
  char *  content = calloc(1, maxLen + 1);

S
Shengliang Guan 已提交
90
#if 0
S
TD-1762  
Shengliang Guan 已提交
91
  len += snprintf(content + len, maxLen - len, "{\n");
92
  len += snprintf(content + len, maxLen - len, "  \"version\": %" PRIu64 "\n", pVnode->fversion);
S
TD-1762  
Shengliang Guan 已提交
93
  len += snprintf(content + len, maxLen - len, "}\n");
S
Shengliang Guan 已提交
94
#endif
S
TD-1762  
Shengliang Guan 已提交
95
  fwrite(content, 1, len, fp);
96
  taosFsyncFile(fileno(fp));
S
TD-1762  
Shengliang Guan 已提交
97 98
  fclose(fp);
  free(content);
S
TD-1762  
Shengliang Guan 已提交
99
  terrno = 0;
S
TD-1762  
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
  // vInfo("vgId:%d, successed to write %s, fver:%" PRIu64, pVnode->vgId, file, pVnode->fversion);
S
TD-1762  
Shengliang Guan 已提交
102 103
  return TSDB_CODE_SUCCESS;
}