mmFile.c 5.2 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

S
shm  
Shengliang Guan 已提交
19
int32_t mmReadFile(SMnodeMgmt *pMgmt, bool *pDeployed) {
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 28 29
  TdFilePtr pFile = NULL;

  snprintf(file, sizeof(file), "%s%smnode.json", pMgmt->path, TD_DIRSEP);
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
30
  if (pFile == NULL) {
S
Shengliang Guan 已提交
31
    // dDebug("file %s not exist", file);
S
shm  
Shengliang Guan 已提交
32
    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
  }
S
shm  
Shengliang Guan 已提交
54
  *pDeployed = deployed->valueint;
S
shm  
Shengliang Guan 已提交
55 56 57 58

  cJSON *mnodes = cJSON_GetObjectItem(root, "mnodes");
  if (!mnodes || mnodes->type != cJSON_Array) {
    dError("failed to read %s since nodes not found", file);
S
Shengliang Guan 已提交
59
    goto _OVER;
S
shm  
Shengliang Guan 已提交
60 61 62 63 64
  }

  pMgmt->replica = cJSON_GetArraySize(mnodes);
  if (pMgmt->replica <= 0 || pMgmt->replica > TSDB_MAX_REPLICA) {
    dError("failed to read %s since mnodes size %d invalid", file, pMgmt->replica);
S
Shengliang Guan 已提交
65
    goto _OVER;
S
shm  
Shengliang Guan 已提交
66 67 68 69 70 71 72 73 74 75 76
  }

  for (int32_t i = 0; i < pMgmt->replica; ++i) {
    cJSON *node = cJSON_GetArrayItem(mnodes, i);
    if (node == NULL) break;

    SReplica *pReplica = &pMgmt->replicas[i];

    cJSON *id = cJSON_GetObjectItem(node, "id");
    if (!id || id->type != cJSON_Number) {
      dError("failed to read %s since id not found", file);
S
Shengliang Guan 已提交
77
      goto _OVER;
S
shm  
Shengliang Guan 已提交
78 79 80 81 82 83
    }
    pReplica->id = id->valueint;

    cJSON *fqdn = cJSON_GetObjectItem(node, "fqdn");
    if (!fqdn || fqdn->type != cJSON_String || fqdn->valuestring == NULL) {
      dError("failed to read %s since fqdn not found", file);
S
Shengliang Guan 已提交
84
      goto _OVER;
S
shm  
Shengliang Guan 已提交
85 86 87 88 89 90
    }
    tstrncpy(pReplica->fqdn, fqdn->valuestring, TSDB_FQDN_LEN);

    cJSON *port = cJSON_GetObjectItem(node, "port");
    if (!port || port->type != cJSON_Number) {
      dError("failed to read %s since port not found", file);
S
Shengliang Guan 已提交
91
      goto _OVER;
S
shm  
Shengliang Guan 已提交
92 93 94 95 96
    }
    pReplica->port = port->valueint;
  }

  code = 0;
S
shm  
Shengliang Guan 已提交
97
  dDebug("succcessed to read file %s, deployed:%d", file, *pDeployed);
S
shm  
Shengliang Guan 已提交
98

S
Shengliang Guan 已提交
99
_OVER:
wafwerar's avatar
wafwerar 已提交
100
  if (content != NULL) taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
101 102 103 104 105 106 107
  if (root != NULL) cJSON_Delete(root);
  if (pFile != NULL) taosCloseFile(&pFile);

  terrno = code;
  return code;
}

S
Shengliang Guan 已提交
108 109 110 111 112
int32_t mmWriteFile(SMgmtWrapper *pWrapper, SDCreateMnodeReq *pReq, bool deployed) {
  char file[PATH_MAX] = {0};
  char realfile[PATH_MAX] = {0};
  snprintf(file, sizeof(file), "%s%smnode.json.bak", pWrapper->path, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%smnode.json", pWrapper->path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
113

114
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
115
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
116
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
117 118 119 120 121 122
    dError("failed to write %s since %s", file, terrstr());
    return -1;
  }

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

S
shm  
Shengliang Guan 已提交
125
  len += snprintf(content + len, maxLen - len, "{\n");
S
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134 135
  len += snprintf(content + len, maxLen - len, "  \"mnodes\": [{\n");

  SMnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pReq != NULL || pMgmt != NULL) {
    int8_t replica = (pReq != NULL ? pReq->replica : pMgmt->replica);
    for (int32_t i = 0; i < replica; ++i) {
      SReplica *pReplica = &pMgmt->replicas[i];
      if (pReq != NULL) {
        pReplica = &pReq->replicas[i];
      }
S
Shengliang Guan 已提交
136 137 138
      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);
S
Shengliang Guan 已提交
139
      if (i < replica - 1) {
S
Shengliang Guan 已提交
140 141 142 143
        len += snprintf(content + len, maxLen - len, "  },{\n");
      } else {
        len += snprintf(content + len, maxLen - len, "  }],\n");
      }
S
shm  
Shengliang Guan 已提交
144 145
    }
  }
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147
  len += snprintf(content + len, maxLen - len, "  \"deployed\": %d\n", deployed);
S
shm  
Shengliang Guan 已提交
148 149 150 151 152
  len += snprintf(content + len, maxLen - len, "}\n");

  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
153
  taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
154 155

  if (taosRenameFile(file, realfile) != 0) {
S
shm  
Shengliang Guan 已提交
156
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
157 158 159 160
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
161
  dInfo("successed to write %s, deployed:%d", realfile, deployed);
S
shm  
Shengliang Guan 已提交
162 163
  return 0;
}