mmFile.c 5.8 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"
S
shm  
Shengliang Guan 已提交
18

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

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

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

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

  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 已提交
51
    goto _OVER;
S
shm  
Shengliang Guan 已提交
52
  }
53
  pOption->deploy = deployed->valueint;
S
shm  
Shengliang Guan 已提交
54

55 56 57 58
  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 已提交
59
      goto _OVER;
S
shm  
Shengliang Guan 已提交
60
    }
61
    pOption->selfIndex = selfIndex->valueint;
S
Shengliang Guan 已提交
62
  }
S
shm  
Shengliang Guan 已提交
63

64 65 66 67
  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 已提交
68
      goto _OVER;
S
shm  
Shengliang Guan 已提交
69
    }
S
Shengliang Guan 已提交
70

71 72 73
    int32_t numOfReplicas = cJSON_GetArraySize(replicas);
    if (numOfReplicas <= 0) {
      dError("failed to read %s since numOfReplicas:%d invalid", file, numOfReplicas);
S
Shengliang Guan 已提交
74 75
      goto _OVER;
    }
76 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
    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 已提交
116 117 118 119 120
    }
  }

  code = 0;

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

  terrno = code;
  return code;
}

133
int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) {
S
Shengliang Guan 已提交
134 135
  char file[PATH_MAX] = {0};
  char realfile[PATH_MAX] = {0};
136 137
  snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
138

139
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
140
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
141
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
142 143 144 145 146 147
    dError("failed to write %s since %s", file, terrstr());
    return -1;
  }

  int32_t len = 0;
  int32_t maxLen = 4096;
wafwerar's avatar
wafwerar 已提交
148
  char   *content = taosMemoryCalloc(1, maxLen + 1);
S
Shengliang Guan 已提交
149

S
shm  
Shengliang Guan 已提交
150
  len += snprintf(content + len, maxLen - len, "{\n");
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  if (pOption->deploy && pOption->numOfReplicas > 0) {
    len += snprintf(content + len, maxLen - len, "  \"selfIndex\": %d,\n", pOption->selfIndex);
    len += snprintf(content + len, maxLen - len, "  \"replicas\": [{\n");

    for (int32_t i = 0; i < pOption->numOfReplicas; ++i) {
      const SReplica *pReplica = pOption->replicas + i;
      if (pReplica != NULL && pReplica->id > 0) {
        len += snprintf(content + len, maxLen - len, "    \"id\": %d,\n", pReplica->id);
        len += snprintf(content + len, maxLen - len, "    \"fqdn\": \"%s\",\n", pReplica->fqdn);
        len += snprintf(content + len, maxLen - len, "    \"port\": %u\n", pReplica->port);
      }
      if (i < pOption->numOfReplicas - 1) {
        len += snprintf(content + len, maxLen - len, "  },{\n");
      } else {
        len += snprintf(content + len, maxLen - len, "  }],\n");
      }
    }
S
shm  
Shengliang Guan 已提交
168
  }
169
  len += snprintf(content + len, maxLen - len, "  \"deployed\": %d\n", pOption->deploy);
S
shm  
Shengliang Guan 已提交
170 171 172 173 174
  len += snprintf(content + len, maxLen - len, "}\n");

  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
175
  taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
176 177

  if (taosRenameFile(file, realfile) != 0) {
S
shm  
Shengliang Guan 已提交
178
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
179 180 181 182
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

183
  dDebug("successed to write %s, deployed:%d", realfile, pOption->deploy);
S
shm  
Shengliang Guan 已提交
184 185
  return 0;
}