dmEps.c 18.5 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static FORCE_INLINE void dmSwapEps(SEp *epLhs, SEp *epRhs) {
  SEp epTmp;

  epTmp.port = epLhs->port;
  tstrncpy(epTmp.fqdn, epLhs->fqdn, tListLen(epTmp.fqdn));

  epLhs->port = epRhs->port;
  tstrncpy(epLhs->fqdn, epRhs->fqdn, tListLen(epLhs->fqdn));

  epRhs->port = epTmp.port;
  tstrncpy(epRhs->fqdn, epTmp.fqdn, tListLen(epRhs->fqdn));
}

void dmRotateMnodeEpSet(SDnodeData *pData) {
  taosThreadRwlockRdlock(&pData->lock);
  SEpSet *pEpSet = &pData->mnodeEps;
  for (int i = 1; i < pEpSet->numOfEps; i++) {
    dmSwapEps(&pEpSet->eps[i - 1], &pEpSet->eps[i]);
  }
  taosThreadRwlockUnlock(&pData->lock);
}

350 351
void dmGetMnodeEpSetForRedirect(SDnodeData *pData, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dmGetMnodeEpSet(pData, pEpSet);
S
Shengliang Guan 已提交
352
  dTrace("msg is redirected, handle:%p num:%d use:%d", pMsg->info.handle, pEpSet->numOfEps, pEpSet->inUse);
353
  for (int32_t i = 0; i < pEpSet->numOfEps; ++i) {
S
Shengliang Guan 已提交
354
    dTrace("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port);
355 356 357 358 359 360
    if (strcmp(pEpSet->eps[i].fqdn, tsLocalFqdn) == 0 && pEpSet->eps[i].port == tsServerPort) {
      pEpSet->inUse = (i + 1) % pEpSet->numOfEps;
    }
  }
}

S
Shengliang Guan 已提交
361
void dmSetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet) {
362
  if (memcmp(pEpSet, &pData->mnodeEps, sizeof(SEpSet)) == 0) return;
363
  taosThreadRwlockWrlock(&pData->lock);
S
Shengliang Guan 已提交
364
  pData->mnodeEps = *pEpSet;
365 366 367
  taosThreadRwlockUnlock(&pData->lock);

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

373 374
bool dmUpdateDnodeInfo(void *data, int32_t *did, int64_t *clusterId, char *fqdn, uint16_t *port) {
  bool        updated = false;
375
  SDnodeData *pData = data;
376 377 378
  int32_t     dnodeId = -1;
  if (did != NULL) dnodeId = *did;

379
  taosThreadRwlockRdlock(&pData->lock);
380 381 382 383

  if (pData->oldDnodeEps != NULL) {
    int32_t size = (int32_t)taosArrayGetSize(pData->oldDnodeEps);
    for (int32_t i = 0; i < size; ++i) {
384 385 386 387 388
      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;
389
        updated = true;
390 391 392 393 394 395 396
      }
    }
  }

  if (did != NULL && dnodeId <= 0) {
    int32_t size = (int32_t)taosArrayGetSize(pData->dnodeEps);
    for (int32_t i = 0; i < size; ++i) {
397 398
      SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i);
      if (strcmp(pDnodeEp->ep.fqdn, fqdn) == 0 && pDnodeEp->ep.port == *port) {
399 400
        dInfo("dnode:%s:%u, update dnodeId to dnode:%d", fqdn, *port, pDnodeEp->id);
        *did = pDnodeEp->id;
401
        if (clusterId != NULL) *clusterId = pData->clusterId;
402 403
      }
    }
404 405 406 407
  }

  if (dnodeId > 0) {
    SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, &dnodeId, sizeof(int32_t));
408
    if (pDnodeEp) {
409 410
      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);
411 412
        tstrncpy(fqdn, pDnodeEp->ep.fqdn, TSDB_FQDN_LEN);
        *port = pDnodeEp->ep.port;
413
        updated = true;
414
      }
415
      if (clusterId != NULL) *clusterId = pData->clusterId;
416 417
    }
  }
418

419
  taosThreadRwlockUnlock(&pData->lock);
420
  return updated;
421 422 423 424 425 426
}

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

  SJson *dnodes = tjsonGetObjectItem(pJson, "dnodes");
427
  if (dnodes == NULL) return -1;
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  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;
}

452 453
void dmRemoveDnodePairs(SDnodeData *pData) {
  char file[PATH_MAX] = {0};
454
  char bak[PATH_MAX] = {0};
455
  snprintf(file, sizeof(file), "%s%sdnode%sep.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
456 457 458
  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);
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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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;
  }

509
  pData->oldDnodeEps = taosArrayInit(1, sizeof(SDnodeEpPair));
510 511 512 513 514
  if (pData->oldDnodeEps == NULL) {
    dError("failed to calloc dnodeEp array since %s", strerror(errno));
    goto _OVER;
  }

515
  if (dmDecodeEpPairs(pJson, pData) < 0) {
516 517
    taosArrayDestroy(pData->oldDnodeEps);
    pData->oldDnodeEps = NULL;
518 519 520 521 522
    terrno = TSDB_CODE_INVALID_JSON_FORMAT;
    goto _OVER;
  }

  code = 0;
523
  dInfo("succceed to read dnode file %s", file);
524 525 526 527 528 529 530 531

_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());
532 533 534 535 536 537 538 539 540
    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 已提交
541 542
        tstrncpy(pair->oldFqdn, pDnodeEp->ep.fqdn, TSDB_FQDN_LEN);
        pair->oldPort = pDnodeEp->ep.port;
543 544
      }
    }
545 546
  }

547
  // check new fqdn and port
548 549 550 551
  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);
552 553 554 555
      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);
556 557 558 559
        taosArrayDestroy(pData->oldDnodeEps);
        pData->oldDnodeEps = NULL;
        terrno = TSDB_CODE_INVALID_CFG;
        return -1;
560 561 562 563
      }
    }
  }

564 565 566 567 568 569 570 571 572 573 574
  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;
      }
    }
575 576
  }

577
  pData->dnodeVer = 0;
578
  return 0;
579
}