dmEps.c 17.9 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
Shengliang Guan 已提交
17
#include "dmUtil.h"
18
#include "tjson.h"
H
Haojun Liao 已提交
19
#include "tmisce.h"
S
shm  
Shengliang Guan 已提交
20

21 22 23 24 25 26 27 28 29 30 31 32
typedef struct {
  int32_t  id;
  uint16_t oldPort;
  uint16_t newPort;
  char     oldFqdn[TSDB_FQDN_LEN];
  char     newFqdn[TSDB_FQDN_LEN];
} SDnodeEpPair;

static void    dmPrintEps(SDnodeData *pData);
static bool    dmIsEpChanged(SDnodeData *pData, int32_t dnodeId, const char *ep);
static void    dmResetEps(SDnodeData *pData, SArray *dnodeEps);
static int32_t dmReadDnodePairs(SDnodeData *pData);
S
shm  
Shengliang Guan 已提交
33

S
Shengliang Guan 已提交
34
static void dmGetDnodeEp(SDnodeData *pData, int32_t dnodeId, char *pEp, char *pFqdn, uint16_t *pPort) {
35
  taosThreadRwlockRdlock(&pData->lock);
S
Shengliang Guan 已提交
36

S
Shengliang Guan 已提交
37
  SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, &dnodeId, sizeof(int32_t));
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48 49
  if (pDnodeEp != NULL) {
    if (pPort != NULL) {
      *pPort = pDnodeEp->ep.port;
    }
    if (pFqdn != NULL) {
      tstrncpy(pFqdn, pDnodeEp->ep.fqdn, TSDB_FQDN_LEN);
    }
    if (pEp != NULL) {
      snprintf(pEp, TSDB_EP_LEN, "%s:%u", pDnodeEp->ep.fqdn, pDnodeEp->ep.port);
    }
  }

50
  taosThreadRwlockUnlock(&pData->lock);
S
Shengliang Guan 已提交
51 52
}

S
Shengliang Guan 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static int32_t dmDecodeEps(SJson *pJson, SDnodeData *pData) {
  int32_t code = 0;

  tjsonGetInt32ValueFromDouble(pJson, "dnodeId", pData->dnodeId, code);
  if (code < 0) return -1;
  tjsonGetNumberValue(pJson, "dnodeVer", pData->dnodeVer, code);
  if (code < 0) return -1;
  tjsonGetNumberValue(pJson, "clusterId", pData->clusterId, code);
  if (code < 0) return -1;
  tjsonGetInt32ValueFromDouble(pJson, "dropped", pData->dropped, code);
  if (code < 0) return -1;

  SJson *dnodes = tjsonGetObjectItem(pJson, "dnodes");
  if (dnodes == NULL) return 0;
  int32_t numOfDnodes = tjsonGetArraySize(dnodes);

  for (int32_t i = 0; i < numOfDnodes; ++i) {
    SJson *dnode = tjsonGetArrayItem(dnodes, i);
    if (dnode == NULL) return -1;

    SDnodeEp dnodeEp = {0};
    tjsonGetInt32ValueFromDouble(dnode, "id", dnodeEp.id, code);
    if (code < 0) return -1;
    code = tjsonGetStringValue(dnode, "fqdn", dnodeEp.ep.fqdn);
    if (code < 0) return -1;
    tjsonGetUInt16ValueFromDouble(dnode, "port", dnodeEp.ep.port, code);
    if (code < 0) return -1;
    tjsonGetInt8ValueFromDouble(dnode, "isMnode", dnodeEp.isMnode, code);
    if (code < 0) return -1;

    if (taosArrayPush(pData->dnodeEps, &dnodeEp) == NULL) return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
89
int32_t dmReadEps(SDnodeData *pData) {
S
Shengliang Guan 已提交
90
  int32_t   code = -1;
S
shm  
Shengliang Guan 已提交
91
  TdFilePtr pFile = NULL;
S
Shengliang Guan 已提交
92 93 94 95
  char     *content = NULL;
  SJson    *pJson = NULL;
  char      file[PATH_MAX] = {0};
  snprintf(file, sizeof(file), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97 98
  pData->dnodeEps = taosArrayInit(1, sizeof(SDnodeEp));
  if (pData->dnodeEps == NULL) {
S
shm  
Shengliang Guan 已提交
99
    dError("failed to calloc dnodeEp array since %s", strerror(errno));
S
Shengliang Guan 已提交
100
    goto _OVER;
S
shm  
Shengliang Guan 已提交
101 102
  }

S
Shengliang Guan 已提交
103 104
  if (taosStatFile(file, NULL, NULL) < 0) {
    dInfo("dnode file:%s not exist", file);
S
shm  
Shengliang Guan 已提交
105
    code = 0;
S
Shengliang Guan 已提交
106
    goto _OVER;
S
shm  
Shengliang Guan 已提交
107 108
  }

S
Shengliang Guan 已提交
109 110 111 112
  pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to open dnode file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
113
    goto _OVER;
S
shm  
Shengliang Guan 已提交
114 115
  }

S
Shengliang Guan 已提交
116 117 118 119
  int64_t size = 0;
  if (taosFStatFile(pFile, &size, NULL) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to fstat dnode file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
120
    goto _OVER;
S
shm  
Shengliang Guan 已提交
121 122
  }

S
Shengliang Guan 已提交
123 124 125
  content = taosMemoryMalloc(size + 1);
  if (content == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
126 127 128
    goto _OVER;
  }

S
Shengliang Guan 已提交
129 130 131
  if (taosReadFile(pFile, content, size) != size) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to read dnode file:%s since %s", file, terrstr());
S
Shengliang Guan 已提交
132
    goto _OVER;
S
shm  
Shengliang Guan 已提交
133 134
  }

S
Shengliang Guan 已提交
135
  content[size] = '\0';
S
shm  
Shengliang Guan 已提交
136

S
Shengliang Guan 已提交
137 138 139
  pJson = tjsonParse(content);
  if (pJson == NULL) {
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
S
Shengliang Guan 已提交
140
    goto _OVER;
S
shm  
Shengliang Guan 已提交
141 142
  }

S
Shengliang Guan 已提交
143 144
  if (dmDecodeEps(pJson, pData) < 0) {
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
S
Shengliang Guan 已提交
145
    goto _OVER;
S
shm  
Shengliang Guan 已提交
146 147 148
  }

  code = 0;
149
  dInfo("succceed to read dnode file %s", file);
S
shm  
Shengliang Guan 已提交
150

S
Shengliang Guan 已提交
151
_OVER:
wafwerar's avatar
wafwerar 已提交
152
  if (content != NULL) taosMemoryFree(content);
S
Shengliang Guan 已提交
153
  if (pJson != NULL) cJSON_Delete(pJson);
S
shm  
Shengliang Guan 已提交
154 155
  if (pFile != NULL) taosCloseFile(&pFile);

S
Shengliang Guan 已提交
156 157
  if (code != 0) {
    dError("failed to read dnode file:%s since %s", file, terrstr());
158
    return code;
S
shm  
Shengliang Guan 已提交
159
  }
S
Shengliang Guan 已提交
160 161 162 163 164 165 166 167

  if (taosArrayGetSize(pData->dnodeEps) == 0) {
    SDnodeEp dnodeEp = {0};
    dnodeEp.isMnode = 1;
    taosGetFqdnPortFromEp(tsFirst, &dnodeEp.ep);
    taosArrayPush(pData->dnodeEps, &dnodeEp);
  }

168 169 170 171
  if (dmReadDnodePairs(pData) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
172 173 174
  dDebug("reset dnode list on startup");
  dmResetEps(pData, pData->dnodeEps);

175
  if (pData->oldDnodeEps == NULL && dmIsEpChanged(pData, pData->dnodeId, tsLocalEp)) {
S
Shengliang Guan 已提交
176
    dError("localEp %s different with %s and need reconfigured", tsLocalEp, file);
177
    terrno = TSDB_CODE_INVALID_CFG;
S
Shengliang Guan 已提交
178 179 180
    return -1;
  }

S
shm  
Shengliang Guan 已提交
181
  return code;
S
shm  
Shengliang Guan 已提交
182 183
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
static int32_t dmEncodeEps(SJson *pJson, SDnodeData *pData) {
  if (tjsonAddDoubleToObject(pJson, "dnodeId", pData->dnodeId) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "dnodeVer", pData->dnodeVer) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "clusterId", pData->clusterId) < 0) return -1;
  if (tjsonAddDoubleToObject(pJson, "dropped", pData->dropped) < 0) return -1;

  SJson *dnodes = tjsonCreateArray();
  if (dnodes == NULL) return -1;
  if (tjsonAddItemToObject(pJson, "dnodes", dnodes) < 0) return -1;

  int32_t numOfEps = (int32_t)taosArrayGetSize(pData->dnodeEps);
  for (int32_t i = 0; i < numOfEps; ++i) {
    SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i);
    SJson    *dnode = tjsonCreateObject();
    if (dnode == NULL) return -1;

    if (tjsonAddDoubleToObject(dnode, "id", pDnodeEp->id) < 0) return -1;
    if (tjsonAddStringToObject(dnode, "fqdn", pDnodeEp->ep.fqdn) < 0) return -1;
    if (tjsonAddDoubleToObject(dnode, "port", pDnodeEp->ep.port) < 0) return -1;
    if (tjsonAddDoubleToObject(dnode, "isMnode", pDnodeEp->isMnode) < 0) return -1;
    if (tjsonAddItemToArray(dnodes, dnode) < 0) return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
210
int32_t dmWriteEps(SDnodeData *pData) {
211
  int32_t   code = -1;
212 213
  char     *buffer = NULL;
  SJson    *pJson = NULL;
214
  TdFilePtr pFile = NULL;
215 216
  char      file[PATH_MAX] = {0};
  char      realfile[PATH_MAX] = {0};
217 218
  snprintf(file, sizeof(file), "%s%sdnode%sdnode.json.bak", tsDataDir, TD_DIRSEP, TD_DIRSEP);
  snprintf(realfile, sizeof(realfile), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
S
shm  
Shengliang Guan 已提交
219

220 221 222 223 224 225
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  pJson = tjsonCreateObject();
  if (pJson == NULL) goto _OVER;
  if (dmEncodeEps(pJson, pData) != 0) goto _OVER;
  buffer = tjsonToString(pJson);
  if (buffer == NULL) goto _OVER;
226
  terrno = 0;
227

S
Shengliang Guan 已提交
228 229 230
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) goto _OVER;

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

235
  taosCloseFile(&pFile);
236
  if (taosRenameFile(file, realfile) != 0) goto _OVER;
S
shm  
Shengliang Guan 已提交
237

238
  code = 0;
S
Shengliang Guan 已提交
239
  pData->updateTime = taosGetTimestampMs();
240 241
  dInfo("succeed to write dnode file:%s, num:%d ver:%" PRId64, realfile, (int32_t)taosArrayGetSize(pData->dnodeEps),
        pData->dnodeVer);
242 243

_OVER:
244 245
  if (pJson != NULL) tjsonDelete(pJson);
  if (buffer != NULL) taosMemoryFree(buffer);
246
  if (pFile != NULL) taosCloseFile(&pFile);
247

248
  if (code != 0) {
249
    if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
250
    dError("failed to write dnode file:%s since %s, dnodeVer:%" PRId64, realfile, terrstr(), pData->dnodeVer);
251 252
  }
  return code;
S
shm  
Shengliang Guan 已提交
253 254
}

S
Shengliang Guan 已提交
255
void dmUpdateEps(SDnodeData *pData, SArray *eps) {
256
  taosThreadRwlockWrlock(&pData->lock);
257 258 259
  dDebug("new dnode list get from mnode, dnodeVer:%" PRId64, pData->dnodeVer);
  dmResetEps(pData, eps);
  dmWriteEps(pData);
260
  taosThreadRwlockUnlock(&pData->lock);
S
shm  
Shengliang Guan 已提交
261 262
}

S
Shengliang Guan 已提交
263 264 265
static void dmResetEps(SDnodeData *pData, SArray *dnodeEps) {
  if (pData->dnodeEps != dnodeEps) {
    SArray *tmp = pData->dnodeEps;
H
Haojun Liao 已提交
266
    pData->dnodeEps = taosArrayDup(dnodeEps, NULL);
S
shm  
Shengliang Guan 已提交
267 268 269
    taosArrayDestroy(tmp);
  }

S
Shengliang Guan 已提交
270 271
  pData->mnodeEps.inUse = 0;
  pData->mnodeEps.numOfEps = 0;
S
shm  
Shengliang Guan 已提交
272 273

  int32_t mIndex = 0;
S
shm  
Shengliang Guan 已提交
274
  int32_t numOfEps = (int32_t)taosArrayGetSize(dnodeEps);
S
shm  
Shengliang Guan 已提交
275 276

  for (int32_t i = 0; i < numOfEps; i++) {
S
shm  
Shengliang Guan 已提交
277
    SDnodeEp *pDnodeEp = taosArrayGet(dnodeEps, i);
S
shm  
Shengliang Guan 已提交
278 279
    if (!pDnodeEp->isMnode) continue;
    if (mIndex >= TSDB_MAX_REPLICA) continue;
S
Shengliang Guan 已提交
280
    pData->mnodeEps.numOfEps++;
S
shm  
Shengliang Guan 已提交
281

S
Shengliang Guan 已提交
282
    pData->mnodeEps.eps[mIndex] = pDnodeEp->ep;
S
shm  
Shengliang Guan 已提交
283 284 285 286
    mIndex++;
  }

  for (int32_t i = 0; i < numOfEps; i++) {
S
shm  
Shengliang Guan 已提交
287
    SDnodeEp *pDnodeEp = taosArrayGet(dnodeEps, i);
S
Shengliang Guan 已提交
288
    taosHashPut(pData->dnodeHash, &pDnodeEp->id, sizeof(int32_t), pDnodeEp, sizeof(SDnodeEp));
S
shm  
Shengliang Guan 已提交
289 290
  }

S
Shengliang Guan 已提交
291
  dmPrintEps(pData);
S
shm  
Shengliang Guan 已提交
292 293
}

S
Shengliang Guan 已提交
294 295
static void dmPrintEps(SDnodeData *pData) {
  int32_t numOfEps = (int32_t)taosArrayGetSize(pData->dnodeEps);
S
Shengliang Guan 已提交
296
  dDebug("print dnode list, num:%d", numOfEps);
S
shm  
Shengliang Guan 已提交
297
  for (int32_t i = 0; i < numOfEps; i++) {
S
Shengliang Guan 已提交
298
    SDnodeEp *pEp = taosArrayGet(pData->dnodeEps, i);
299
    dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", pEp->id, pEp->ep.fqdn, pEp->ep.port, pEp->isMnode);
S
shm  
Shengliang Guan 已提交
300 301 302
  }
}

S
Shengliang Guan 已提交
303
static bool dmIsEpChanged(SDnodeData *pData, int32_t dnodeId, const char *ep) {
S
shm  
Shengliang Guan 已提交
304
  bool changed = false;
305
  if (dnodeId == 0) return changed;
306
  taosThreadRwlockRdlock(&pData->lock);
S
shm  
Shengliang Guan 已提交
307

S
Shengliang Guan 已提交
308
  SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, &dnodeId, sizeof(int32_t));
S
shm  
Shengliang Guan 已提交
309
  if (pDnodeEp != NULL) {
310
    char epstr[TSDB_EP_LEN + 1] = {0};
S
shm  
Shengliang Guan 已提交
311
    snprintf(epstr, TSDB_EP_LEN, "%s:%u", pDnodeEp->ep.fqdn, pDnodeEp->ep.port);
312 313
    changed = (strcmp(ep, epstr) != 0);
    if (changed) {
314
      dError("dnode:%d, localEp %s different from %s", dnodeId, ep, epstr);
315
    }
S
shm  
Shengliang Guan 已提交
316 317
  }

318
  taosThreadRwlockUnlock(&pData->lock);
S
shm  
Shengliang Guan 已提交
319 320
  return changed;
}
S
Shengliang Guan 已提交
321 322

void dmGetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet) {
323
  taosThreadRwlockRdlock(&pData->lock);
S
Shengliang Guan 已提交
324
  *pEpSet = pData->mnodeEps;
325
  taosThreadRwlockUnlock(&pData->lock);
S
Shengliang Guan 已提交
326 327
}

328 329
void dmGetMnodeEpSetForRedirect(SDnodeData *pData, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dmGetMnodeEpSet(pData, pEpSet);
S
Shengliang Guan 已提交
330
  dTrace("msg is redirected, handle:%p num:%d use:%d", pMsg->info.handle, pEpSet->numOfEps, pEpSet->inUse);
331
  for (int32_t i = 0; i < pEpSet->numOfEps; ++i) {
S
Shengliang Guan 已提交
332
    dTrace("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port);
333 334 335 336 337 338
    if (strcmp(pEpSet->eps[i].fqdn, tsLocalFqdn) == 0 && pEpSet->eps[i].port == tsServerPort) {
      pEpSet->inUse = (i + 1) % pEpSet->numOfEps;
    }
  }
}

S
Shengliang Guan 已提交
339
void dmSetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet) {
340
  if (memcmp(pEpSet, &pData->mnodeEps, sizeof(SEpSet)) == 0) return;
341
  taosThreadRwlockWrlock(&pData->lock);
S
Shengliang Guan 已提交
342
  pData->mnodeEps = *pEpSet;
343 344 345
  taosThreadRwlockUnlock(&pData->lock);

  dInfo("mnode is changed, num:%d use:%d", pEpSet->numOfEps, pEpSet->inUse);
S
Shengliang Guan 已提交
346 347 348 349
  for (int32_t i = 0; i < pEpSet->numOfEps; ++i) {
    dInfo("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port);
  }
}
350

351 352
bool dmUpdateDnodeInfo(void *data, int32_t *did, int64_t *clusterId, char *fqdn, uint16_t *port) {
  bool        updated = false;
353
  SDnodeData *pData = data;
354 355 356
  int32_t     dnodeId = -1;
  if (did != NULL) dnodeId = *did;

357
  taosThreadRwlockRdlock(&pData->lock);
358 359 360 361

  if (pData->oldDnodeEps != NULL) {
    int32_t size = (int32_t)taosArrayGetSize(pData->oldDnodeEps);
    for (int32_t i = 0; i < size; ++i) {
362 363 364 365 366
      SDnodeEpPair *pair = taosArrayGet(pData->oldDnodeEps, i);
      if (strcmp(pair->oldFqdn, fqdn) == 0 && pair->oldPort == *port) {
        dInfo("dnode:%d, update ep:%s:%u to %s:%u", dnodeId, fqdn, *port, pair->newFqdn, pair->newPort);
        tstrncpy(fqdn, pair->newFqdn, TSDB_FQDN_LEN);
        *port = pair->newPort;
367
        updated = true;
368 369 370 371 372 373 374
      }
    }
  }

  if (did != NULL && dnodeId <= 0) {
    int32_t size = (int32_t)taosArrayGetSize(pData->dnodeEps);
    for (int32_t i = 0; i < size; ++i) {
375 376
      SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i);
      if (strcmp(pDnodeEp->ep.fqdn, fqdn) == 0 && pDnodeEp->ep.port == *port) {
377 378
        dInfo("dnode:%s:%u, update dnodeId to dnode:%d", fqdn, *port, pDnodeEp->id);
        *did = pDnodeEp->id;
379
        if (clusterId != NULL) *clusterId = pData->clusterId;
380 381
      }
    }
382 383 384 385
  }

  if (dnodeId > 0) {
    SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, &dnodeId, sizeof(int32_t));
386
    if (pDnodeEp) {
387 388
      if (strcmp(pDnodeEp->ep.fqdn, fqdn) != 0 || pDnodeEp->ep.port != *port) {
        dInfo("dnode:%d, update ep:%s:%u to %s:%u", dnodeId, fqdn, *port, pDnodeEp->ep.fqdn, pDnodeEp->ep.port);
389 390
        tstrncpy(fqdn, pDnodeEp->ep.fqdn, TSDB_FQDN_LEN);
        *port = pDnodeEp->ep.port;
391
        updated = true;
392
      }
393
      if (clusterId != NULL) *clusterId = pData->clusterId;
394 395
    }
  }
396

397
  taosThreadRwlockUnlock(&pData->lock);
398
  return updated;
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
}

static int32_t dmDecodeEpPairs(SJson *pJson, SDnodeData *pData) {
  int32_t code = 0;

  SJson *dnodes = tjsonGetObjectItem(pJson, "dnodes");
  if (dnodes == NULL) return 0;
  int32_t numOfDnodes = tjsonGetArraySize(dnodes);

  for (int32_t i = 0; i < numOfDnodes; ++i) {
    SJson *dnode = tjsonGetArrayItem(dnodes, i);
    if (dnode == NULL) return -1;

    SDnodeEpPair pair = {0};
    tjsonGetInt32ValueFromDouble(dnode, "id", pair.id, code);
    if (code < 0) return -1;
    code = tjsonGetStringValue(dnode, "fqdn", pair.oldFqdn);
    if (code < 0) return -1;
    tjsonGetUInt16ValueFromDouble(dnode, "port", pair.oldPort, code);
    if (code < 0) return -1;
    code = tjsonGetStringValue(dnode, "new_fqdn", pair.newFqdn);
    if (code < 0) return -1;
    tjsonGetUInt16ValueFromDouble(dnode, "new_port", pair.newPort, code);
    if (code < 0) return -1;

    if (taosArrayPush(pData->oldDnodeEps, &pair) == NULL) return -1;
  }

  return code;
}

430 431
void dmRemoveDnodePairs(SDnodeData *pData) {
  char file[PATH_MAX] = {0};
432
  char bak[PATH_MAX] = {0};
433
  snprintf(file, sizeof(file), "%s%sdnode%sep.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
434 435 436
  snprintf(bak, sizeof(bak), "%s%sdnode%sep.json.bak", tsDataDir, TD_DIRSEP, TD_DIRSEP);
  dInfo("dnode file:%s is rename to bak file", file);
  (void)taosRenameFile(file, bak);
437 438
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
static int32_t dmReadDnodePairs(SDnodeData *pData) {
  int32_t   code = -1;
  TdFilePtr pFile = NULL;
  char     *content = NULL;
  SJson    *pJson = NULL;
  char      file[PATH_MAX] = {0};
  snprintf(file, sizeof(file), "%s%sdnode%sep.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);

  if (taosStatFile(file, NULL, NULL) < 0) {
    dDebug("dnode file:%s not exist", file);
    code = 0;
    goto _OVER;
  }

  pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to open dnode file:%s since %s", file, terrstr());
    goto _OVER;
  }

  int64_t size = 0;
  if (taosFStatFile(pFile, &size, NULL) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to fstat dnode file:%s since %s", file, terrstr());
    goto _OVER;
  }

  content = taosMemoryMalloc(size + 1);
  if (content == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  if (taosReadFile(pFile, content, size) != size) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to read dnode file:%s since %s", file, terrstr());
    goto _OVER;
  }

  content[size] = '\0';

  pJson = tjsonParse(content);
  if (pJson == NULL) {
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
    goto _OVER;
  }

487
  pData->oldDnodeEps = taosArrayInit(1, sizeof(SDnodeEpPair));
488 489 490 491 492
  if (pData->oldDnodeEps == NULL) {
    dError("failed to calloc dnodeEp array since %s", strerror(errno));
    goto _OVER;
  }

493
  if (dmDecodeEpPairs(pJson, pData) < 0) {
494 495
    taosArrayDestroy(pData->oldDnodeEps);
    pData->oldDnodeEps = NULL;
496 497 498 499 500
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
    goto _OVER;
  }

  code = 0;
501
  dInfo("succceed to read dnode file %s", file);
502 503 504 505 506 507 508 509

_OVER:
  if (content != NULL) taosMemoryFree(content);
  if (pJson != NULL) cJSON_Delete(pJson);
  if (pFile != NULL) taosCloseFile(&pFile);

  if (code != 0) {
    dError("failed to read dnode file:%s since %s", file, terrstr());
510 511 512 513 514 515 516 517 518
    return code;
  }

  // update old fqdn and port
  for (int32_t i = 0; i < (int32_t)taosArrayGetSize(pData->oldDnodeEps); ++i) {
    SDnodeEpPair *pair = taosArrayGet(pData->oldDnodeEps, i);
    for (int32_t j = 0; j < (int32_t)taosArrayGetSize(pData->dnodeEps); ++j) {
      SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, j);
      if (pDnodeEp->id == pair->id) {
S
Shengliang Guan 已提交
519
        tstrncpy(pair->oldFqdn, pair->newFqdn, TSDB_FQDN_LEN);
520 521 522
        pair->oldPort = pair->newPort;
      }
    }
523 524
  }

525
  // check new fqdn and port
526 527 528 529
  for (int32_t i = 0; i < (int32_t)taosArrayGetSize(pData->oldDnodeEps); ++i) {
    SDnodeEpPair *pair = taosArrayGet(pData->oldDnodeEps, i);
    for (int32_t j = 0; j < (int32_t)taosArrayGetSize(pData->dnodeEps); ++j) {
      SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, j);
530 531 532 533
      if (pDnodeEp->id != pair->id &&
          (strcmp(pDnodeEp->ep.fqdn, pair->newFqdn) == 0 && pDnodeEp->ep.port == pair->newPort)) {
        dError("dnode:%d, can't update ep:%s:%u to %s:%u since already exists as dnode:%d", pair->id, pair->oldFqdn,
               pair->oldPort, pair->newFqdn, pair->newPort, pDnodeEp->id);
534 535 536 537
        taosArrayDestroy(pData->oldDnodeEps);
        pData->oldDnodeEps = NULL;
        terrno = TSDB_CODE_INVALID_CFG;
        return -1;
538 539 540 541
      }
    }
  }

542 543 544 545 546 547 548 549 550 551 552
  for (int32_t i = 0; i < (int32_t)taosArrayGetSize(pData->oldDnodeEps); ++i) {
    SDnodeEpPair *pair = taosArrayGet(pData->oldDnodeEps, i);
    for (int32_t j = 0; j < (int32_t)taosArrayGetSize(pData->dnodeEps); ++j) {
      SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, j);
      if (strcmp(pDnodeEp->ep.fqdn, pair->oldFqdn) == 0 && pDnodeEp->ep.port == pair->oldPort) {
        dInfo("dnode:%d, will update ep:%s:%u to %s:%u", pDnodeEp->id, pDnodeEp->ep.fqdn, pDnodeEp->ep.port,
              pair->newFqdn, pair->newPort);
        tstrncpy(pDnodeEp->ep.fqdn, pair->newFqdn, TSDB_FQDN_LEN);
        pDnodeEp->ep.port = pair->newPort;
      }
    }
553 554
  }

555
  pData->dnodeVer = 0;
556
  return 0;
557
}