dmFile.c 9.3 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 "dmInt.h"
S
shm  
Shengliang Guan 已提交
18

S
shm  
Shengliang Guan 已提交
19
static void dmPrintDnodes(SDnodeMgmt *pMgmt);
S
shm  
Shengliang Guan 已提交
20 21
static bool dmIsEpChanged(SDnodeMgmt *pMgmt, int32_t dnodeId, const char *ep);
static void dmResetDnodes(SDnodeMgmt *pMgmt, SArray *dnodeEps);
S
shm  
Shengliang Guan 已提交
22 23

int32_t dmReadFile(SDnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
24
  int32_t   code = TSDB_CODE_NODE_PARSE_FILE_ERROR;
S
shm  
Shengliang Guan 已提交
25 26
  int32_t   len = 0;
  int32_t   maxLen = 256 * 1024;
wafwerar's avatar
wafwerar 已提交
27
  char     *content = taosMemoryCalloc(1, maxLen + 1);
S
shm  
Shengliang Guan 已提交
28 29 30
  cJSON    *root = NULL;
  char      file[PATH_MAX];
  TdFilePtr pFile = NULL;
S
shm  
Shengliang Guan 已提交
31
  SDnode   *pDnode = pMgmt->pDnode;
S
shm  
Shengliang Guan 已提交
32

S
shm  
Shengliang Guan 已提交
33 34
  pMgmt->dnodeEps = taosArrayInit(1, sizeof(SDnodeEp));
  if (pMgmt->dnodeEps == NULL) {
S
shm  
Shengliang Guan 已提交
35 36 37 38
    dError("failed to calloc dnodeEp array since %s", strerror(errno));
    goto PRASE_DNODE_OVER;
  }

S
shm  
Shengliang Guan 已提交
39 40
  snprintf(file, sizeof(file), "%s%sdnode.json", pMgmt->path, TD_DIRSEP);
  pFile = taosOpenFile(file, TD_FILE_READ);
S
shm  
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  if (pFile == NULL) {
    dDebug("file %s not exist", file);
    code = 0;
    goto PRASE_DNODE_OVER;
  }

  len = (int32_t)taosReadFile(pFile, content, maxLen);
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
    goto PRASE_DNODE_OVER;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
    goto PRASE_DNODE_OVER;
  }

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

  cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId");
  if (!clusterId || clusterId->type != cJSON_String) {
    dError("failed to read %s since clusterId not found", file);
    goto PRASE_DNODE_OVER;
  }
S
shm  
Shengliang Guan 已提交
72
  pDnode->clusterId = atoll(clusterId->valuestring);
S
shm  
Shengliang Guan 已提交
73 74 75 76 77 78

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_Number) {
    dError("failed to read %s since dropped not found", file);
    goto PRASE_DNODE_OVER;
  }
S
shm  
Shengliang Guan 已提交
79
  pDnode->dropped = dropped->valueint;
S
shm  
Shengliang Guan 已提交
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 117 118 119 120 121 122 123 124 125 126 127 128

  cJSON *dnodes = cJSON_GetObjectItem(root, "dnodes");
  if (!dnodes || dnodes->type != cJSON_Array) {
    dError("failed to read %s since dnodes not found", file);
    goto PRASE_DNODE_OVER;
  }

  int32_t numOfDnodes = cJSON_GetArraySize(dnodes);
  if (numOfDnodes <= 0) {
    dError("failed to read %s since numOfDnodes:%d invalid", file, numOfDnodes);
    goto PRASE_DNODE_OVER;
  }

  for (int32_t i = 0; i < numOfDnodes; ++i) {
    cJSON *node = cJSON_GetArrayItem(dnodes, i);
    if (node == NULL) break;

    SDnodeEp dnodeEp = {0};

    cJSON *did = cJSON_GetObjectItem(node, "id");
    if (!did || did->type != cJSON_Number) {
      dError("failed to read %s since dnodeId not found", file);
      goto PRASE_DNODE_OVER;
    }

    dnodeEp.id = dnodeId->valueint;

    cJSON *dnodeFqdn = cJSON_GetObjectItem(node, "fqdn");
    if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) {
      dError("failed to read %s since dnodeFqdn not found", file);
      goto PRASE_DNODE_OVER;
    }
    tstrncpy(dnodeEp.ep.fqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN);

    cJSON *dnodePort = cJSON_GetObjectItem(node, "port");
    if (!dnodePort || dnodePort->type != cJSON_Number) {
      dError("failed to read %s since dnodePort not found", file);
      goto PRASE_DNODE_OVER;
    }

    dnodeEp.ep.port = dnodePort->valueint;

    cJSON *isMnode = cJSON_GetObjectItem(node, "isMnode");
    if (!isMnode || isMnode->type != cJSON_Number) {
      dError("failed to read %s since isMnode not found", file);
      goto PRASE_DNODE_OVER;
    }
    dnodeEp.isMnode = isMnode->valueint;

S
shm  
Shengliang Guan 已提交
129
    taosArrayPush(pMgmt->dnodeEps, &dnodeEp);
S
shm  
Shengliang Guan 已提交
130 131 132 133
  }

  code = 0;
  dInfo("succcessed to read file %s", file);
S
shm  
Shengliang Guan 已提交
134
  dmPrintDnodes(pMgmt);
S
shm  
Shengliang Guan 已提交
135 136

PRASE_DNODE_OVER:
wafwerar's avatar
wafwerar 已提交
137
  if (content != NULL) taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
138 139 140
  if (root != NULL) cJSON_Delete(root);
  if (pFile != NULL) taosCloseFile(&pFile);

S
shm  
Shengliang Guan 已提交
141 142
  if (dmIsEpChanged(pMgmt, pDnode->dnodeId, pDnode->localEp)) {
    dError("localEp %s different with %s and need reconfigured", pDnode->localEp, file);
S
shm  
Shengliang Guan 已提交
143 144 145
    return -1;
  }

S
shm  
Shengliang Guan 已提交
146
  if (taosArrayGetSize(pMgmt->dnodeEps) == 0) {
S
shm  
Shengliang Guan 已提交
147 148
    SDnodeEp dnodeEp = {0};
    dnodeEp.isMnode = 1;
S
shm  
Shengliang Guan 已提交
149 150
    taosGetFqdnPortFromEp(pDnode->firstEp, &dnodeEp.ep);
    taosArrayPush(pMgmt->dnodeEps, &dnodeEp);
S
shm  
Shengliang Guan 已提交
151 152
  }

S
shm  
Shengliang Guan 已提交
153
  dmResetDnodes(pMgmt, pMgmt->dnodeEps);
S
shm  
Shengliang Guan 已提交
154

S
shm  
Shengliang Guan 已提交
155 156
  terrno = code;
  return code;
S
shm  
Shengliang Guan 已提交
157 158
}

S
shm  
Shengliang Guan 已提交
159
int32_t dmWriteFile(SDnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
160 161
  SDnode *pDnode = pMgmt->pDnode;

S
shm  
Shengliang Guan 已提交
162
  char file[PATH_MAX];
S
shm  
Shengliang Guan 已提交
163
  snprintf(file, sizeof(file), "%s%sdnode.json.bak", pMgmt->path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
164 165 166 167 168 169 170 171 172 173

  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
    dError("failed to write %s since %s", file, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  int32_t len = 0;
  int32_t maxLen = 256 * 1024;
wafwerar's avatar
wafwerar 已提交
174
  char   *content = taosMemoryCalloc(1, maxLen + 1);
S
shm  
Shengliang Guan 已提交
175 176

  len += snprintf(content + len, maxLen - len, "{\n");
S
shm  
Shengliang Guan 已提交
177 178 179
  len += snprintf(content + len, maxLen - len, "  \"dnodeId\": %d,\n", pDnode->dnodeId);
  len += snprintf(content + len, maxLen - len, "  \"clusterId\": \"%" PRId64 "\",\n", pDnode->clusterId);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": %d,\n", pDnode->dropped);
S
shm  
Shengliang Guan 已提交
180 181
  len += snprintf(content + len, maxLen - len, "  \"dnodes\": [{\n");

S
shm  
Shengliang Guan 已提交
182
  int32_t numOfEps = (int32_t)taosArrayGetSize(pMgmt->dnodeEps);
S
shm  
Shengliang Guan 已提交
183
  for (int32_t i = 0; i < numOfEps; ++i) {
S
shm  
Shengliang Guan 已提交
184
    SDnodeEp *pDnodeEp = taosArrayGet(pMgmt->dnodeEps, i);
S
shm  
Shengliang Guan 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    len += snprintf(content + len, maxLen - len, "    \"id\": %d,\n", pDnodeEp->id);
    len += snprintf(content + len, maxLen - len, "    \"fqdn\": \"%s\",\n", pDnodeEp->ep.fqdn);
    len += snprintf(content + len, maxLen - len, "    \"port\": %u,\n", pDnodeEp->ep.port);
    len += snprintf(content + len, maxLen - len, "    \"isMnode\": %d\n", pDnodeEp->isMnode);
    if (i < numOfEps - 1) {
      len += snprintf(content + len, maxLen - len, "  },{\n");
    } else {
      len += snprintf(content + len, maxLen - len, "  }]\n");
    }
  }
  len += snprintf(content + len, maxLen - len, "}\n");

  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
200
  taosMemoryFree(content);
S
shm  
Shengliang Guan 已提交
201 202

  char realfile[PATH_MAX];
S
shm  
Shengliang Guan 已提交
203
  snprintf(realfile, sizeof(realfile), "%s%smnode.json", pMgmt->path, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
204 205

  if (taosRenameFile(file, realfile) != 0) {
S
shm  
Shengliang Guan 已提交
206
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
207 208 209 210 211
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

  pMgmt->updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
212
  dDebug("successed to write %s", realfile);
S
shm  
Shengliang Guan 已提交
213 214 215
  return 0;
}

S
shm  
Shengliang Guan 已提交
216 217
void dmUpdateDnodeEps(SDnodeMgmt *pMgmt, SArray *dnodeEps) {
  int32_t numOfEps = taosArrayGetSize(dnodeEps);
S
shm  
Shengliang Guan 已提交
218 219 220 221
  if (numOfEps <= 0) return;

  taosWLockLatch(&pMgmt->latch);

S
shm  
Shengliang Guan 已提交
222
  int32_t numOfEpsOld = (int32_t)taosArrayGetSize(pMgmt->dnodeEps);
S
shm  
Shengliang Guan 已提交
223
  if (numOfEps != numOfEpsOld) {
S
shm  
Shengliang Guan 已提交
224
    dmResetDnodes(pMgmt, dnodeEps);
S
shm  
Shengliang Guan 已提交
225
    dmWriteFile(pMgmt);
S
shm  
Shengliang Guan 已提交
226 227
  } else {
    int32_t size = numOfEps * sizeof(SDnodeEp);
S
shm  
Shengliang Guan 已提交
228 229
    if (memcmp(pMgmt->dnodeEps->pData, dnodeEps->pData, size) != 0) {
      dmResetDnodes(pMgmt, dnodeEps);
S
shm  
Shengliang Guan 已提交
230
      dmWriteFile(pMgmt);
S
shm  
Shengliang Guan 已提交
231 232 233 234 235 236
    }
  }

  taosWUnLockLatch(&pMgmt->latch);
}

S
shm  
Shengliang Guan 已提交
237 238 239 240
static void dmResetDnodes(SDnodeMgmt *pMgmt, SArray *dnodeEps) {
  if (pMgmt->dnodeEps != dnodeEps) {
    SArray *tmp = pMgmt->dnodeEps;
    pMgmt->dnodeEps = taosArrayDup(dnodeEps);
S
shm  
Shengliang Guan 已提交
241 242 243 244 245 246 247
    taosArrayDestroy(tmp);
  }

  pMgmt->mnodeEpSet.inUse = 0;
  pMgmt->mnodeEpSet.numOfEps = 0;

  int32_t mIndex = 0;
S
shm  
Shengliang Guan 已提交
248
  int32_t numOfEps = (int32_t)taosArrayGetSize(dnodeEps);
S
shm  
Shengliang Guan 已提交
249 250

  for (int32_t i = 0; i < numOfEps; i++) {
S
shm  
Shengliang Guan 已提交
251
    SDnodeEp *pDnodeEp = taosArrayGet(dnodeEps, i);
S
shm  
Shengliang Guan 已提交
252 253 254 255 256 257 258 259 260
    if (!pDnodeEp->isMnode) continue;
    if (mIndex >= TSDB_MAX_REPLICA) continue;
    pMgmt->mnodeEpSet.numOfEps++;

    pMgmt->mnodeEpSet.eps[mIndex] = pDnodeEp->ep;
    mIndex++;
  }

  for (int32_t i = 0; i < numOfEps; i++) {
S
shm  
Shengliang Guan 已提交
261
    SDnodeEp *pDnodeEp = taosArrayGet(dnodeEps, i);
S
shm  
Shengliang Guan 已提交
262 263 264
    taosHashPut(pMgmt->dnodeHash, &pDnodeEp->id, sizeof(int32_t), pDnodeEp, sizeof(SDnodeEp));
  }

S
shm  
Shengliang Guan 已提交
265
  dmPrintDnodes(pMgmt);
S
shm  
Shengliang Guan 已提交
266 267
}

S
shm  
Shengliang Guan 已提交
268
static void dmPrintDnodes(SDnodeMgmt *pMgmt) {
S
shm  
Shengliang Guan 已提交
269
  int32_t numOfEps = (int32_t)taosArrayGetSize(pMgmt->dnodeEps);
S
shm  
Shengliang Guan 已提交
270 271
  dDebug("print dnode ep list, num:%d", numOfEps);
  for (int32_t i = 0; i < numOfEps; i++) {
S
shm  
Shengliang Guan 已提交
272
    SDnodeEp *pEp = taosArrayGet(pMgmt->dnodeEps, i);
S
shm  
Shengliang Guan 已提交
273 274 275 276
    dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", pEp->id, pEp->ep.fqdn, pEp->ep.port, pEp->isMnode);
  }
}

S
shm  
Shengliang Guan 已提交
277
static bool dmIsEpChanged(SDnodeMgmt *pMgmt, int32_t dnodeId, const char *ep) {
S
shm  
Shengliang Guan 已提交
278 279 280
  bool changed = false;
  taosRLockLatch(&pMgmt->latch);

S
shm  
Shengliang Guan 已提交
281
  SDnodeEp *pDnodeEp = taosHashGet(pMgmt->dnodeHash, &dnodeId, sizeof(int32_t));
S
shm  
Shengliang Guan 已提交
282 283 284
  if (pDnodeEp != NULL) {
    char epstr[TSDB_EP_LEN + 1];
    snprintf(epstr, TSDB_EP_LEN, "%s:%u", pDnodeEp->ep.fqdn, pDnodeEp->ep.port);
S
shm  
Shengliang Guan 已提交
285
    changed = strcmp(ep, epstr) != 0;
S
shm  
Shengliang Guan 已提交
286 287 288 289 290
  }

  taosRUnLockLatch(&pMgmt->latch);
  return changed;
}