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

20
int32_t mmReadFile(const char *path, SMnodeOpt *pOption) {
21
  int32_t   code = TSDB_CODE_INVALID_JSON_FORMAT;
S
shm  
Shengliang Guan 已提交
22 23
  int32_t   len = 0;
  int32_t   maxLen = 4096;
wafwerar's avatar
wafwerar 已提交
24
  char     *content = taosMemoryCalloc(1, maxLen + 1);
S
shm  
Shengliang Guan 已提交
25
  cJSON    *root = NULL;
S
Shengliang Guan 已提交
26
  char      file[PATH_MAX] = {0};
S
shm  
Shengliang Guan 已提交
27 28
  TdFilePtr pFile = NULL;

29
  snprintf(file, sizeof(file), "%s%smnode.json", path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
30
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
31 32
  if (pFile == NULL) {
    code = 0;
S
Shengliang Guan 已提交
33
    goto _OVER;
S
shm  
Shengliang Guan 已提交
34 35 36 37 38
  }

  len = (int32_t)taosReadFile(pFile, content, maxLen);
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
S
Shengliang Guan 已提交
39
    goto _OVER;
S
shm  
Shengliang Guan 已提交
40 41 42 43 44 45
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
S
Shengliang Guan 已提交
46
    goto _OVER;
S
shm  
Shengliang Guan 已提交
47 48 49 50 51
  }

  cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
  if (!deployed || deployed->type != cJSON_Number) {
    dError("failed to read %s since deployed not found", file);
S
Shengliang Guan 已提交
52
    goto _OVER;
S
shm  
Shengliang Guan 已提交
53
  }
54
  pOption->deploy = deployed->valueint;
S
shm  
Shengliang Guan 已提交
55

56 57 58 59
  cJSON *selfIndex = cJSON_GetObjectItem(root, "selfIndex");
  if (selfIndex) {
    if (selfIndex->type != cJSON_Number) {
      dError("failed to read %s since selfIndex not found", file);
S
Shengliang Guan 已提交
60
      goto _OVER;
S
shm  
Shengliang Guan 已提交
61
    }
62
    pOption->selfIndex = selfIndex->valueint;
S
Shengliang Guan 已提交
63
  }
S
shm  
Shengliang Guan 已提交
64

65 66 67 68
  cJSON *replicas = cJSON_GetObjectItem(root, "replicas");
  if (replicas) {
    if (replicas->type != cJSON_Array) {
      dError("failed to read %s since replicas not found", file);
S
Shengliang Guan 已提交
69
      goto _OVER;
S
shm  
Shengliang Guan 已提交
70
    }
S
Shengliang Guan 已提交
71

72 73 74
    int32_t numOfReplicas = cJSON_GetArraySize(replicas);
    if (numOfReplicas <= 0) {
      dError("failed to read %s since numOfReplicas:%d invalid", file, numOfReplicas);
S
Shengliang Guan 已提交
75 76
      goto _OVER;
    }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    pOption->numOfReplicas = numOfReplicas;

    for (int32_t i = 0; i < numOfReplicas; ++i) {
      SReplica *pReplica = pOption->replicas + i;

      cJSON *replica = cJSON_GetArrayItem(replicas, i);
      if (replica == NULL) break;

      cJSON *id = cJSON_GetObjectItem(replica, "id");
      if (id) {
        if (id->type != cJSON_Number) {
          dError("failed to read %s since id not found", file);
          goto _OVER;
        }
        if (pReplica) {
          pReplica->id = id->valueint;
        }
      }

      cJSON *fqdn = cJSON_GetObjectItem(replica, "fqdn");
      if (fqdn) {
        if (fqdn->type != cJSON_String || fqdn->valuestring == NULL) {
          dError("failed to read %s since fqdn not found", file);
          goto _OVER;
        }
        if (pReplica) {
          tstrncpy(pReplica->fqdn, fqdn->valuestring, TSDB_FQDN_LEN);
        }
      }

      cJSON *port = cJSON_GetObjectItem(replica, "port");
      if (port) {
        if (port->type != cJSON_Number) {
          dError("failed to read %s since port not found", file);
          goto _OVER;
        }
        if (pReplica) {
          pReplica->port = (uint16_t)port->valueint;
        }
      }
S
shm  
Shengliang Guan 已提交
117 118 119 120 121
    }
  }

  code = 0;

S
Shengliang Guan 已提交
122
_OVER:
wafwerar's avatar
wafwerar 已提交
123
  if (content != NULL) taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
124 125
  if (root != NULL) cJSON_Delete(root);
  if (pFile != NULL) taosCloseFile(&pFile);
S
Shengliang Guan 已提交
126
  if (code == 0) {
127
    dDebug("succcessed to read file %s, deployed:%d", file, pOption->deploy);
S
Shengliang Guan 已提交
128
  }
S
shm  
Shengliang Guan 已提交
129 130 131 132 133

  terrno = code;
  return code;
}

134
static int32_t mmEncodeOption(SJson *pJson, const SMnodeOpt *pOption) {
135
  if (pOption->deploy && pOption->numOfReplicas > 0) {
136 137 138 139 140
    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;
141 142

    for (int32_t i = 0; i < pOption->numOfReplicas; ++i) {
143 144 145
      SJson *replica = tjsonCreateObject();
      if (replica == NULL) return -1;

146
      const SReplica *pReplica = pOption->replicas + i;
147 148 149 150
      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;
      if (tjsonAddItemToArray(replicas, replica) < 0) return -1;
151
    }
S
shm  
Shengliang Guan 已提交
152 153
  }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  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;
175 176 177 178
  terrno = 0;

  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) goto _OVER;
179 180 181 182

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

184
  taosCloseFile(&pFile);
185
  if (taosRenameFile(file, realfile) != 0) goto _OVER;
S
shm  
Shengliang Guan 已提交
186

187 188 189 190 191 192 193 194 195
  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) {
196
    if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
197 198 199
    dError("failed to write mnode file:%s since %s, deloyed:%d", realfile, terrstr(), pOption->deploy);
  }
  return code;
S
shm  
Shengliang Guan 已提交
200
}