mmFile.c 6.1 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
S
shm  
Shengliang Guan 已提交
17
#include "mmInt.h"
18
#include "tjson.h"
S
shm  
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20 21 22 23 24 25 26
static int32_t mmDecodeOption(SJson *pJson, SMnodeOpt *pOption) {
  int32_t code = 0;

  tjsonGetInt32ValueFromDouble(pJson, "deployed", pOption->deploy, code);
  if (code < 0) return -1;
  tjsonGetInt32ValueFromDouble(pJson, "selfIndex", pOption->selfIndex, code);
  if (code < 0) return 0;
C
cadem 已提交
27 28
  tjsonGetInt32ValueFromDouble(pJson, "lastIndex", pOption->lastIndex, code);
  if (code < 0) return 0;
S
Shengliang Guan 已提交
29 30 31

  SJson *replicas = tjsonGetObjectItem(pJson, "replicas");
  if (replicas == NULL) return 0;
C
cadem 已提交
32 33 34
  pOption->numOfTotalReplicas = tjsonGetArraySize(replicas);

  pOption->numOfReplicas = 0;
S
Shengliang Guan 已提交
35

C
cadem 已提交
36
  for (int32_t i = 0; i < pOption->numOfTotalReplicas; ++i) {
S
Shengliang Guan 已提交
37 38 39 40 41 42 43 44 45 46
    SJson *replica = tjsonGetArrayItem(replicas, i);
    if (replica == NULL) return -1;

    SReplica *pReplica = pOption->replicas + i;
    tjsonGetInt32ValueFromDouble(replica, "id", pReplica->id, code);
    if (code < 0) return -1;
    code = tjsonGetStringValue(replica, "fqdn", pReplica->fqdn);
    if (code < 0) return -1;
    tjsonGetUInt16ValueFromDouble(replica, "port", pReplica->port, code);
    if (code < 0) return -1;
C
cadem 已提交
47 48 49 50 51 52 53 54
    tjsonGetInt32ValueFromDouble(replica, "role", pOption->nodeRoles[i], code);
    if (code < 0) return -1;
    if(pOption->nodeRoles[i] == TAOS_SYNC_ROLE_VOTER){
      pOption->numOfReplicas++;
    }
  }

  for (int32_t i = 0; i < pOption->numOfTotalReplicas; ++i) {
S
Shengliang Guan 已提交
55 56 57 58 59
  }

  return 0;
}

60
int32_t mmReadFile(const char *path, SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
61
  int32_t   code = -1;
S
shm  
Shengliang Guan 已提交
62
  TdFilePtr pFile = NULL;
S
Shengliang Guan 已提交
63 64 65
  char     *pData = NULL;
  SJson    *pJson = NULL;
  char      file[PATH_MAX] = {0};
66
  snprintf(file, sizeof(file), "%s%smnode.json", path, TD_DIRSEP);
S
Shengliang Guan 已提交
67 68 69 70 71 72

  if (taosStatFile(file, NULL, NULL) < 0) {
    dInfo("mnode file:%s not exist", file);
    return 0;
  }

S
shm  
Shengliang Guan 已提交
73
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
74
  if (pFile == NULL) {
S
Shengliang Guan 已提交
75 76
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to open mnode file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
77
    goto _OVER;
S
shm  
Shengliang Guan 已提交
78 79
  }

S
Shengliang Guan 已提交
80 81 82 83
  int64_t size = 0;
  if (taosFStatFile(pFile, &size, NULL) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to fstat mnode file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
84
    goto _OVER;
S
shm  
Shengliang Guan 已提交
85 86
  }

S
Shengliang Guan 已提交
87 88 89
  pData = taosMemoryMalloc(size + 1);
  if (pData == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
90
    goto _OVER;
S
shm  
Shengliang Guan 已提交
91 92
  }

S
Shengliang Guan 已提交
93 94 95
  if (taosReadFile(pFile, pData, size) != size) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to read mnode file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
96
    goto _OVER;
S
shm  
Shengliang Guan 已提交
97
  }
S
Shengliang Guan 已提交
98 99 100 101 102 103 104

  pData[size] = '\0';

  pJson = tjsonParse(pData);
  if (pJson == NULL) {
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
    goto _OVER;
S
Shengliang Guan 已提交
105
  }
S
shm  
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107 108 109
  if (mmDecodeOption(pJson, pOption) < 0) {
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
    goto _OVER;
S
shm  
Shengliang Guan 已提交
110 111 112
  }

  code = 0;
S
Shengliang Guan 已提交
113
  dInfo("succceed to read mnode file %s", file);
S
shm  
Shengliang Guan 已提交
114

S
Shengliang Guan 已提交
115
_OVER:
S
Shengliang Guan 已提交
116 117
  if (pData != NULL) taosMemoryFree(pData);
  if (pJson != NULL) cJSON_Delete(pJson);
S
shm  
Shengliang Guan 已提交
118 119
  if (pFile != NULL) taosCloseFile(&pFile);

S
Shengliang Guan 已提交
120 121 122
  if (code != 0) {
    dError("failed to read mnode file:%s since %s", file, terrstr());
  }
S
shm  
Shengliang Guan 已提交
123 124 125
  return code;
}

126
static int32_t mmEncodeOption(SJson *pJson, const SMnodeOpt *pOption) {
C
cadem 已提交
127
  if (pOption->deploy && pOption->numOfTotalReplicas > 0) {
128 129 130 131 132
    if (tjsonAddDoubleToObject(pJson, "selfIndex", pOption->selfIndex) < 0) return -1;

    SJson *replicas = tjsonCreateArray();
    if (replicas == NULL) return -1;
    if (tjsonAddItemToObject(pJson, "replicas", replicas) < 0) return -1;
133

C
cadem 已提交
134
    for (int32_t i = 0; i < pOption->numOfTotalReplicas; ++i) {
135 136 137
      SJson *replica = tjsonCreateObject();
      if (replica == NULL) return -1;

138
      const SReplica *pReplica = pOption->replicas + i;
139 140 141
      if (tjsonAddDoubleToObject(replica, "id", pReplica->id) < 0) return -1;
      if (tjsonAddStringToObject(replica, "fqdn", pReplica->fqdn) < 0) return -1;
      if (tjsonAddDoubleToObject(replica, "port", pReplica->port) < 0) return -1;
C
cadem 已提交
142
      if (tjsonAddDoubleToObject(replica, "role", pOption->nodeRoles[i]) < 0) return -1;
143
      if (tjsonAddItemToArray(replicas, replica) < 0) return -1;
144
    }
S
shm  
Shengliang Guan 已提交
145 146
  }

C
cadem 已提交
147 148
  if (tjsonAddDoubleToObject(pJson, "lastIndex", pOption->lastIndex) < 0) return -1;

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  if (tjsonAddDoubleToObject(pJson, "deployed", pOption->deploy) < 0) return -1;

  return 0;
}

int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) {
  int32_t   code = -1;
  char     *buffer = NULL;
  SJson    *pJson = NULL;
  TdFilePtr pFile = NULL;
  char      file[PATH_MAX] = {0};
  char      realfile[PATH_MAX] = {0};
  snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP);

  terrno = TSDB_CODE_OUT_OF_MEMORY;
  pJson = tjsonCreateObject();
  if (pJson == NULL) goto _OVER;
  if (mmEncodeOption(pJson, pOption) != 0) goto _OVER;
  buffer = tjsonToString(pJson);
  if (buffer == NULL) goto _OVER;
170 171 172 173
  terrno = 0;

  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) goto _OVER;
174 175 176 177

  int32_t len = strlen(buffer);
  if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
  if (taosFsyncFile(pFile) < 0) goto _OVER;
S
shm  
Shengliang Guan 已提交
178

179
  taosCloseFile(&pFile);
180
  if (taosRenameFile(file, realfile) != 0) goto _OVER;
S
shm  
Shengliang Guan 已提交
181

182 183 184 185 186 187 188 189 190
  code = 0;
  dInfo("succeed to write mnode file:%s, deloyed:%d", realfile, pOption->deploy);

_OVER:
  if (pJson != NULL) tjsonDelete(pJson);
  if (buffer != NULL) taosMemoryFree(buffer);
  if (pFile != NULL) taosCloseFile(&pFile);

  if (code != 0) {
191
    if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
192 193 194
    dError("failed to write mnode file:%s since %s, deloyed:%d", realfile, terrstr(), pOption->deploy);
  }
  return code;
S
shm  
Shengliang Guan 已提交
195
}