taosmigrateDnodeCfg.c 4.8 KB
Newer Older
H
Hui Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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/>.
 */

#include "taosmigrate.h"

//#include "dnodeInt.h"
//#include "dnodeMgmt.h"
//#include "dnodeVRead.h"
//#include "dnodeVWrite.h"
//#include "dnodeModule.h"

static SDMMnodeInfos tsDnodeIpInfos = {0};

26 27
static bool dnodeReadMnodeInfos(char* dnodeEpSet) {
  FILE *fp = fopen(dnodeEpSet, "r");
H
Hui Li 已提交
28
  if (!fp) {
29
    printf("failed to read mnodeEpSet.json, file not exist\n");
H
Hui Li 已提交
30 31 32 33 34 35 36 37 38 39
    return false;
  }

  bool  ret = false;
  int   maxLen = 2000;
  char *content = calloc(1, maxLen + 1);
  int   len = fread(content, 1, maxLen, fp);
  if (len <= 0) {
    free(content);
    fclose(fp);
40
    printf("failed to read mnodeEpSet.json, content is null\n");
H
Hui Li 已提交
41 42 43 44 45 46
    return false;
  }

  content[len] = 0;
  cJSON* root = cJSON_Parse(content);
  if (root == NULL) {
47
    printf("failed to read mnodeEpSet.json, invalid json format\n");
H
Hui Li 已提交
48 49 50 51 52
    goto PARSE_OVER;
  }

  cJSON* inUse = cJSON_GetObjectItem(root, "inUse");
  if (!inUse || inUse->type != cJSON_Number) {
53
    printf("failed to read mnodeEpSet.json, inUse not found\n");
H
Hui Li 已提交
54 55 56 57 58 59
    goto PARSE_OVER;
  }
  tsDnodeIpInfos.inUse = inUse->valueint;

  cJSON* nodeNum = cJSON_GetObjectItem(root, "nodeNum");
  if (!nodeNum || nodeNum->type != cJSON_Number) {
60
    printf("failed to read mnodeEpSet.json, nodeNum not found\n");
H
Hui Li 已提交
61 62 63 64 65 66
    goto PARSE_OVER;
  }
  tsDnodeIpInfos.nodeNum = nodeNum->valueint;

  cJSON* nodeInfos = cJSON_GetObjectItem(root, "nodeInfos");
  if (!nodeInfos || nodeInfos->type != cJSON_Array) {
67
    printf("failed to read mnodeEpSet.json, nodeInfos not found\n");
H
Hui Li 已提交
68 69 70 71 72
    goto PARSE_OVER;
  }

  int size = cJSON_GetArraySize(nodeInfos);
  if (size != tsDnodeIpInfos.nodeNum) {
73
    printf("failed to read mnodeEpSet.json, nodeInfos size not matched\n");
H
Hui Li 已提交
74 75 76 77 78 79 80 81 82
    goto PARSE_OVER;
  }

  for (int i = 0; i < size; ++i) {
    cJSON* nodeInfo = cJSON_GetArrayItem(nodeInfos, i);
    if (nodeInfo == NULL) continue;

    cJSON *nodeId = cJSON_GetObjectItem(nodeInfo, "nodeId");
    if (!nodeId || nodeId->type != cJSON_Number) {
83
      printf("failed to read mnodeEpSet.json, nodeId not found\n");
H
Hui Li 已提交
84 85 86 87 88 89
      goto PARSE_OVER;
    }
    tsDnodeIpInfos.nodeInfos[i].nodeId = nodeId->valueint;

    cJSON *nodeEp = cJSON_GetObjectItem(nodeInfo, "nodeEp");
    if (!nodeEp || nodeEp->type != cJSON_String || nodeEp->valuestring == NULL) {
90
      printf("failed to read mnodeEpSet.json, nodeName not found\n");
H
Hui Li 已提交
91 92 93
      goto PARSE_OVER;
    }
    strncpy(tsDnodeIpInfos.nodeInfos[i].nodeEp, nodeEp->valuestring, TSDB_EP_LEN);
H
Hui Li 已提交
94 95 96 97 98 99 100

    SdnodeIfo* pDnodeInfo = getDnodeInfo(tsDnodeIpInfos.nodeInfos[i].nodeId);
    if (NULL == pDnodeInfo) {
      continue;
    }

    tstrncpy(tsDnodeIpInfos.nodeInfos[i].nodeEp, pDnodeInfo->ep, TSDB_EP_LEN);    
H
Hui Li 已提交
101 102 103 104
 }

  ret = true;

105
  //printf("read mnode epSet successed, numOfEps:%d inUse:%d\n", tsDnodeIpInfos.nodeNum, tsDnodeIpInfos.inUse);
H
Hui Li 已提交
106 107 108 109 110 111 112 113 114 115 116 117
  //for (int32_t i = 0; i < tsDnodeIpInfos.nodeNum; i++) {
  //  printf("mnode:%d, %s\n", tsDnodeIpInfos.nodeInfos[i].nodeId, tsDnodeIpInfos.nodeInfos[i].nodeEp);
  //}

PARSE_OVER:
  free(content);
  cJSON_Delete(root);
  fclose(fp);
  return ret;
}


118 119
static void dnodeSaveMnodeInfos(char* dnodeEpSet) {
  FILE *fp = fopen(dnodeEpSet, "w");
H
Hui Li 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  if (!fp) return;

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

  len += snprintf(content + len, maxLen - len, "{\n");
  len += snprintf(content + len, maxLen - len, "  \"inUse\": %d,\n", tsDnodeIpInfos.inUse);
  len += snprintf(content + len, maxLen - len, "  \"nodeNum\": %d,\n", tsDnodeIpInfos.nodeNum);
  len += snprintf(content + len, maxLen - len, "  \"nodeInfos\": [{\n");
  for (int32_t i = 0; i < tsDnodeIpInfos.nodeNum; i++) {
    len += snprintf(content + len, maxLen - len, "    \"nodeId\": %d,\n", tsDnodeIpInfos.nodeInfos[i].nodeId);
    len += snprintf(content + len, maxLen - len, "    \"nodeEp\": \"%s\"\n", tsDnodeIpInfos.nodeInfos[i].nodeEp);
    if (i < tsDnodeIpInfos.nodeNum -1) {
      len += snprintf(content + len, maxLen - len, "  },{\n");  
    } else {
      len += snprintf(content + len, maxLen - len, "  }]\n");  
    }
  }
  len += snprintf(content + len, maxLen - len, "}\n"); 

  fwrite(content, 1, len, fp);
  fflush(fp);
  fclose(fp);
  free(content);
  
146
  printf("mod mnode epSet successed\n");
H
Hui Li 已提交
147 148
}

149
void modDnodeEpSet(char* dnodeEpSet)
H
Hui Li 已提交
150
{
151 152
  (void)dnodeReadMnodeInfos(dnodeEpSet);
  dnodeSaveMnodeInfos(dnodeEpSet);
H
Hui Li 已提交
153 154 155 156
  return;
}