dnodeMgmt.c 22.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
slguan 已提交
16
#define _DEFAULT_SOURCE
17
#include "os.h"
J
jtao1735 已提交
18
#include "cJSON.h"
S
slguan 已提交
19 20
#include "taoserror.h"
#include "taosmsg.h"
J
jtao1735 已提交
21 22
#include "ttime.h"
#include "ttimer.h"
S
slguan 已提交
23
#include "tsdb.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
24
#include "twal.h"
25
#include "tqueue.h"
J
jtao1735 已提交
26 27 28 29
#include "tsync.h"
#include "ttime.h"
#include "ttimer.h"
#include "tbalance.h"
S
slguan 已提交
30
#include "tglobal.h"
J
jtao1735 已提交
31 32 33
#include "dnode.h"
#include "vnode.h"
#include "mnode.h"
34
#include "dnodeInt.h"
35
#include "dnodeMgmt.h"
36 37
#include "dnodeVRead.h"
#include "dnodeVWrite.h"
J
jtao1735 已提交
38 39 40 41
#include "dnodeModule.h"

#define MPEER_CONTENT_LEN 2000

42 43 44 45
void *          tsDnodeTmr = NULL;
static void *   tsStatusTimer = NULL;
static uint32_t tsRebootTime;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
46
static SRpcIpSet     tsDMnodeIpSet = {0};
47 48
static SDMMnodeInfos tsDMnodeInfos = {0};
static SDMDnodeCfg   tsDnodeCfg = {0};
49 50 51
static taos_qset     tsMgmtQset = NULL;
static taos_queue    tsMgmtQueue = NULL;
static pthread_t     tsQthread;
52

J
jtao1735 已提交
53 54 55 56 57 58 59 60
static void   dnodeUpdateMnodeInfos(SDMMnodeInfos *pMnodes);
static bool   dnodeReadMnodeInfos();
static void   dnodeSaveMnodeInfos();
static void   dnodeUpdateDnodeCfg(SDMDnodeCfg *pCfg);
static bool   dnodeReadDnodeCfg();
static void   dnodeSaveDnodeCfg();
static void   dnodeProcessStatusRsp(SRpcMsg *pMsg);
static void   dnodeSendStatusMsg(void *handle, void *tmrId);
61
static void  *dnodeProcessMgmtQueue(void *param);
J
jtao1735 已提交
62

S
slguan 已提交
63
static int32_t  dnodeOpenVnodes();
64
static void     dnodeCloseVnodes();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65 66 67 68 69
static int32_t  dnodeProcessCreateVnodeMsg(SRpcMsg *pMsg);
static int32_t  dnodeProcessDropVnodeMsg(SRpcMsg *pMsg);
static int32_t  dnodeProcessAlterStreamMsg(SRpcMsg *pMsg);
static int32_t  dnodeProcessConfigDnodeMsg(SRpcMsg *pMsg);
static int32_t (*dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *pMsg);
S
slguan 已提交
70 71

int32_t dnodeInitMgmt() {
S
slguan 已提交
72 73
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = dnodeProcessCreateVnodeMsg;
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE]   = dnodeProcessDropVnodeMsg;
S
slguan 已提交
74 75
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = dnodeProcessAlterStreamMsg;
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = dnodeProcessConfigDnodeMsg;
S
slguan 已提交
76

J
jtao1735 已提交
77 78 79 80 81
  dnodeAddClientRspHandle(TSDB_MSG_TYPE_DM_STATUS_RSP,  dnodeProcessStatusRsp);
  dnodeReadDnodeCfg();
  tsRebootTime = taosGetTimestampSec();

  if (!dnodeReadMnodeInfos()) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
82
    memset(&tsDMnodeIpSet, 0, sizeof(SRpcIpSet));
83 84
    memset(&tsDMnodeInfos, 0, sizeof(SDMMnodeInfos));

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
85 86
    tsDMnodeIpSet.numOfIps = 1;
    taosGetFqdnPortFromEp(tsFirst, tsDMnodeIpSet.fqdn[0], &tsDMnodeIpSet.port[0]);
87
    
J
jtao1735 已提交
88
    if (strcmp(tsSecond, tsFirst) != 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
89 90
      tsDMnodeIpSet.numOfIps = 2;
      taosGetFqdnPortFromEp(tsSecond, tsDMnodeIpSet.fqdn[1], &tsDMnodeIpSet.port[1]);
J
jtao1735 已提交
91 92
    }
  } else {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
93 94
    tsDMnodeIpSet.inUse = tsDMnodeInfos.inUse;
    tsDMnodeIpSet.numOfIps = tsDMnodeInfos.nodeNum;
95
    for (int32_t i = 0; i < tsDMnodeInfos.nodeNum; i++) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
96
      taosGetFqdnPortFromEp(tsDMnodeInfos.nodeInfos[i].nodeEp, tsDMnodeIpSet.fqdn[i], &tsDMnodeIpSet.port[i]);
J
jtao1735 已提交
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 129
  // create the queue and thread to handle the message 
  tsMgmtQset = taosOpenQset();
  if (tsMgmtQset == NULL) {
    dError("failed to create the mgmt queue set");
    dnodeCleanupMgmt();
    return -1;
  }

  tsMgmtQueue = taosOpenQueue();
  if (tsMgmtQueue == NULL) {
    dError("failed to create the mgmt queue");
    dnodeCleanupMgmt();
    return -1;
  }

  taosAddIntoQset(tsMgmtQset, tsMgmtQueue, NULL);

  pthread_attr_t thAttr;
  pthread_attr_init(&thAttr);
  pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE);

  int32_t code = pthread_create(&tsQthread, &thAttr, dnodeProcessMgmtQueue, NULL);
  pthread_attr_destroy(&thAttr);
  if (code != 0) {
    dError("failed to create thread to process mgmt queue, reason:%s", strerror(errno));
    dnodeCleanupMgmt();
    return -1; 
  }

  code = dnodeOpenVnodes();
S
[TD-17]  
slguan 已提交
130
  if (code != TSDB_CODE_SUCCESS) {
131 132 133 134 135 136 137 138
    dnodeCleanupMgmt();
    return -1;
  }

  tsDnodeTmr = taosTmrInit(100, 200, 60000, "DND-DM");
  if (tsDnodeTmr == NULL) {
    dError("failed to init dnode timer");
    dnodeCleanupMgmt();
S
[TD-17]  
slguan 已提交
139 140
    return -1;
  }
S
slguan 已提交
141

J
jtao1735 已提交
142 143 144 145
  taosTmrReset(dnodeSendStatusMsg, 500, NULL, tsDnodeTmr, &tsStatusTimer);
  
  dPrint("dnode mgmt is initialized");
 
S
[TD-17]  
slguan 已提交
146
  return TSDB_CODE_SUCCESS;
S
#1177  
slguan 已提交
147 148
}

S
slguan 已提交
149
void dnodeCleanupMgmt() {
J
jtao1735 已提交
150 151 152 153 154 155 156 157 158 159
  if (tsStatusTimer != NULL) {
    taosTmrStopA(&tsStatusTimer);
    tsStatusTimer = NULL;
  }

  if (tsDnodeTmr != NULL) {
    taosTmrCleanUp(tsDnodeTmr);
    tsDnodeTmr = NULL;
  }

160
  dnodeCloseVnodes();
161 162 163 164 165 166 167 168 169

  if (tsMgmtQset) taosQsetThreadResume(tsMgmtQset);
  if (tsQthread) pthread_join(tsQthread, NULL);

  if (tsMgmtQueue) taosCloseQueue(tsMgmtQueue);
  if (tsMgmtQset) taosCloseQset(tsMgmtQset);
  tsMgmtQset = NULL;
  tsMgmtQueue = NULL;

S
#1177  
slguan 已提交
170 171
}

172 173
void dnodeDispatchToMgmtQueue(SRpcMsg *pMsg) {
  void *item;
S
slguan 已提交
174

175
  item = taosAllocateQitem(sizeof(SRpcMsg));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
176 177 178 179
  if (item) {
    memcpy(item, pMsg, sizeof(SRpcMsg));
    taosWriteQitem(tsMgmtQueue, 1, item);
  } else {
180 181 182 183 184 185
    SRpcMsg rsp = {
      .handle = pMsg->handle,
      .pCont  = NULL,
      .code   = TSDB_CODE_DND_OUT_OF_MEMORY
    };
    
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
186 187 188
    rpcSendResponse(&rsp);
    rpcFreeCont(pMsg->pCont);
  }
189 190 191 192
}

static void *dnodeProcessMgmtQueue(void *param) {
  SRpcMsg *pMsg;
193
  SRpcMsg  rsp = {0};
194
  int      type;
195
  void *   handle;
196 197 198 199 200 201 202 203 204 205 206

  while (1) {
    if (taosReadQitemFromQset(tsMgmtQset, &type, (void **) &pMsg, &handle) == 0) {
      dTrace("dnode mgmt got no message from qset, exit ...");
      break;
    }

    dTrace("%p, msg:%s will be processed", pMsg->ahandle, taosMsg[pMsg->msgType]);    
    if (dnodeProcessMgmtMsgFp[pMsg->msgType]) {
      rsp.code = (*dnodeProcessMgmtMsgFp[pMsg->msgType])(pMsg);
    } else {
207
      rsp.code = TSDB_CODE_DND_MSG_NOT_PROCESSED;
208 209 210 211 212 213 214 215 216
    }

    rsp.handle = pMsg->handle;
    rsp.pCont  = NULL;
    rpcSendResponse(&rsp);

    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
  }
S
slguan 已提交
217

218
  return NULL;
S
slguan 已提交
219
}
S
slguan 已提交
220

221
static int32_t dnodeGetVnodeList(int32_t vnodeList[], int32_t *numOfVnodes) {
S
slguan 已提交
222 223
  DIR *dir = opendir(tsVnodeDir);
  if (dir == NULL) {
224
    return TSDB_CODE_DND_NO_WRITE_ACCESS;
S
slguan 已提交
225 226
  }

227
  *numOfVnodes = 0;
S
slguan 已提交
228 229 230 231 232 233 234 235
  struct dirent *de = NULL;
  while ((de = readdir(dir)) != NULL) {
    if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
    if (de->d_type & DT_DIR) {
      if (strncmp("vnode", de->d_name, 5) != 0) continue;
      int32_t vnode = atoi(de->d_name + 5);
      if (vnode == 0) continue;

236 237
      vnodeList[*numOfVnodes] = vnode;
      (*numOfVnodes)++;
S
slguan 已提交
238 239 240 241
    }
  }
  closedir(dir);

242
  return TSDB_CODE_SUCCESS;
243 244 245
}

static int32_t dnodeOpenVnodes() {
S
slguan 已提交
246 247
  char vnodeDir[TSDB_FILENAME_LEN * 3];
  int32_t failed = 0;
248
  int32_t *vnodeList = (int32_t *)malloc(sizeof(int32_t) * TSDB_MAX_VNODES);
249 250 251 252 253 254 255
  int32_t numOfVnodes;
  int32_t status;

  status = dnodeGetVnodeList(vnodeList, &numOfVnodes);

  if (status != TSDB_CODE_SUCCESS) {
    dPrint("Get dnode list failed");
256
    free(vnodeList);
257 258
    return status;
  }
259

S
slguan 已提交
260 261 262 263
  for (int32_t i = 0; i < numOfVnodes; ++i) {
    snprintf(vnodeDir, TSDB_FILENAME_LEN * 3, "%s/vnode%d", tsVnodeDir, vnodeList[i]);
    if (vnodeOpen(vnodeList[i], vnodeDir) < 0) failed++;
  }
264

S
slguan 已提交
265
  free(vnodeList);
266
  dPrint("there are total vnodes:%d, openned:%d failed:%d", numOfVnodes, numOfVnodes-failed, failed);
S
slguan 已提交
267
  return TSDB_CODE_SUCCESS;
268 269
}

B
Bomin Zhang 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
void dnodeStartStream() {
  int32_t vnodeList[TSDB_MAX_VNODES];
  int32_t numOfVnodes = 0;
  int32_t status = dnodeGetVnodeList(vnodeList, &numOfVnodes);

  if (status != TSDB_CODE_SUCCESS) {
    dPrint("Get dnode list failed");
    return;
  }

  for (int32_t i = 0; i < numOfVnodes; ++i) {
    vnodeStartStream(vnodeList[i]);
  }

  dPrint("streams started");
}

287
static void dnodeCloseVnodes() {
288
  int32_t *vnodeList = (int32_t *)malloc(sizeof(int32_t) * TSDB_MAX_VNODES);
289 290 291 292 293 294 295
  int32_t numOfVnodes;
  int32_t status;

  status = dnodeGetVnodeList(vnodeList, &numOfVnodes);

  if (status != TSDB_CODE_SUCCESS) {
    dPrint("Get dnode list failed");
296
    free(vnodeList);
297 298
    return;
  }
S
slguan 已提交
299 300 301 302 303 304 305

  for (int32_t i = 0; i < numOfVnodes; ++i) {
    vnodeClose(vnodeList[i]);
  }

  free(vnodeList);
  dPrint("total vnodes:%d are all closed", numOfVnodes);
H
hzcheng 已提交
306 307
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
308
static int32_t dnodeProcessCreateVnodeMsg(SRpcMsg *rpcMsg) {
S
slguan 已提交
309
  SMDCreateVnodeMsg *pCreate = rpcMsg->pCont;
S
slguan 已提交
310
  pCreate->cfg.vgId                = htonl(pCreate->cfg.vgId);
S
slguan 已提交
311
  pCreate->cfg.cfgVersion          = htonl(pCreate->cfg.cfgVersion);
S
slguan 已提交
312
  pCreate->cfg.maxTables           = htonl(pCreate->cfg.maxTables);
S
slguan 已提交
313 314
  pCreate->cfg.cacheBlockSize      = htonl(pCreate->cfg.cacheBlockSize);
  pCreate->cfg.totalBlocks         = htonl(pCreate->cfg.totalBlocks);
S
slguan 已提交
315 316 317 318
  pCreate->cfg.daysPerFile         = htonl(pCreate->cfg.daysPerFile);
  pCreate->cfg.daysToKeep1         = htonl(pCreate->cfg.daysToKeep1);
  pCreate->cfg.daysToKeep2         = htonl(pCreate->cfg.daysToKeep2);
  pCreate->cfg.daysToKeep          = htonl(pCreate->cfg.daysToKeep);
S
slguan 已提交
319 320
  pCreate->cfg.minRowsPerFileBlock = htonl(pCreate->cfg.minRowsPerFileBlock);
  pCreate->cfg.maxRowsPerFileBlock = htonl(pCreate->cfg.maxRowsPerFileBlock);
S
slguan 已提交
321 322
  pCreate->cfg.commitTime          = htonl(pCreate->cfg.commitTime);

323
  for (int32_t j = 0; j < pCreate->cfg.replications; ++j) {
S
slguan 已提交
324
    pCreate->nodes[j].nodeId = htonl(pCreate->nodes[j].nodeId);
325
  }
326

S
slguan 已提交
327 328 329 330 331 332 333 334
  void *pVnode = vnodeAccquireVnode(pCreate->cfg.vgId);
  if (pVnode != NULL) {
    int32_t code = vnodeAlter(pVnode, pCreate);
    vnodeRelease(pVnode);
    return code;
  } else {
    return vnodeCreate(pCreate);
  }
S
slguan 已提交
335 336
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
337
static int32_t dnodeProcessDropVnodeMsg(SRpcMsg *rpcMsg) {
S
slguan 已提交
338
  SMDDropVnodeMsg *pDrop = rpcMsg->pCont;
S
slguan 已提交
339
  pDrop->vgId = htonl(pDrop->vgId);
S
slguan 已提交
340

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
341
  return vnodeDrop(pDrop->vgId);
H
hzcheng 已提交
342 343
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
344
static int32_t dnodeProcessAlterStreamMsg(SRpcMsg *pMsg) {
345
//  SMDAlterStreamMsg *pStream = pCont;
S
slguan 已提交
346 347 348 349 350 351 352
//  pStream->uid    = htobe64(pStream->uid);
//  pStream->stime  = htobe64(pStream->stime);
//  pStream->vnode  = htonl(pStream->vnode);
//  pStream->sid    = htonl(pStream->sid);
//  pStream->status = htonl(pStream->status);
//
//  int32_t code = dnodeCreateStream(pStream);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
353 354

  return 0;
S
slguan 已提交
355 356
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
357
static int32_t dnodeProcessConfigDnodeMsg(SRpcMsg *pMsg) {
S
slguan 已提交
358
  SMDCfgDnodeMsg *pCfg = (SMDCfgDnodeMsg *)pMsg->pCont;
S
slguan 已提交
359
  return taosCfgDynamicOptions(pCfg->config);
S
slguan 已提交
360
}
J
jtao1735 已提交
361

362
void dnodeUpdateMnodeIpSetForPeer(SRpcIpSet *pIpSet) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
363
  dPrint("mnode IP list for is changed, numOfIps:%d inUse:%d", pIpSet->numOfIps, pIpSet->inUse);
S
Shengliang Guan 已提交
364
  for (int i = 0; i < pIpSet->numOfIps; ++i) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
365
    pIpSet->port[i] -= TSDB_PORT_DNODEDNODE;
S
Shengliang Guan 已提交
366 367 368
    dPrint("mnode index:%d %s:%u", i, pIpSet->fqdn[i], pIpSet->port[i])
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
369
  tsDMnodeIpSet = *pIpSet;
J
jtao1735 已提交
370 371
}

372
void dnodeGetMnodeIpSetForPeer(void *ipSetRaw) {
J
jtao1735 已提交
373
  SRpcIpSet *ipSet = ipSetRaw;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
374 375 376 377
  *ipSet = tsDMnodeIpSet;

  for (int i=0; i<ipSet->numOfIps; ++i) 
    ipSet->port[i] += TSDB_PORT_DNODEDNODE;
378 379 380 381
}

void dnodeGetMnodeIpSetForShell(void *ipSetRaw) {
  SRpcIpSet *ipSet = ipSetRaw;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
382
  *ipSet = tsDMnodeIpSet;
J
jtao1735 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
}

static void dnodeProcessStatusRsp(SRpcMsg *pMsg) {
  if (pMsg->code != TSDB_CODE_SUCCESS) {
    dError("status rsp is received, error:%s", tstrerror(pMsg->code));
    taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer);
    return;
  }

  SDMStatusRsp *pStatusRsp = pMsg->pCont;
  SDMMnodeInfos *pMnodes = &pStatusRsp->mnodes;
  if (pMnodes->nodeNum <= 0) {
    dError("status msg is invalid, num of ips is %d", pMnodes->nodeNum);
    taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer);
    return;
  }

  SDMDnodeCfg *pCfg = &pStatusRsp->dnodeCfg;
  pCfg->numOfVnodes  = htonl(pCfg->numOfVnodes);
  pCfg->moduleStatus = htonl(pCfg->moduleStatus);
  pCfg->dnodeId      = htonl(pCfg->dnodeId);

  for (int32_t i = 0; i < pMnodes->nodeNum; ++i) {
    SDMMnodeInfo *pMnodeInfo = &pMnodes->nodeInfos[i];
    pMnodeInfo->nodeId   = htonl(pMnodeInfo->nodeId);
  }

  SDMVgroupAccess *pVgAcccess = pStatusRsp->vgAccess;
  for (int32_t i = 0; i < pCfg->numOfVnodes; ++i) {
    pVgAcccess[i].vgId = htonl(pVgAcccess[i].vgId);
  }
  
  dnodeProcessModuleStatus(pCfg->moduleStatus);
  dnodeUpdateDnodeCfg(pCfg);
417

J
jtao1735 已提交
418 419 420 421
  dnodeUpdateMnodeInfos(pMnodes);
  taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer);
}

422 423 424 425 426 427 428 429 430
static bool dnodeCheckMnodeInfos(SDMMnodeInfos *pMnodes) {
  if (pMnodes->nodeNum <= 0 || pMnodes->nodeNum > 3) {
    dError("invalid mnode infos, num:%d", pMnodes->nodeNum);
    return false;
  }

  for (int32_t i = 0; i < pMnodes->nodeNum; ++i) {
    SDMMnodeInfo *pMnodeInfo = &pMnodes->nodeInfos[i];
    if (pMnodeInfo->nodeId <= 0 || strlen(pMnodeInfo->nodeEp) <= 5) {
S
Shengliang Guan 已提交
431
      dError("invalid mnode info:%d, nodeId:%d nodeEp:%s", i, pMnodeInfo->nodeId, pMnodeInfo->nodeEp);
432 433 434 435 436 437 438
      return false;
    }
  }

  return true;
}

J
jtao1735 已提交
439
static void dnodeUpdateMnodeInfos(SDMMnodeInfos *pMnodes) {
440 441
  bool mnodesChanged = (memcmp(&tsDMnodeInfos, pMnodes, sizeof(SDMMnodeInfos)) != 0);
  bool mnodesNotInit = (tsDMnodeInfos.nodeNum == 0);
J
jtao1735 已提交
442 443
  if (!(mnodesChanged || mnodesNotInit)) return;

444 445
  if (!dnodeCheckMnodeInfos(pMnodes)) return;

446
  memcpy(&tsDMnodeInfos, pMnodes, sizeof(SDMMnodeInfos));
447 448 449 450
  dPrint("mnode infos is changed, nodeNum:%d inUse:%d", tsDMnodeInfos.nodeNum, tsDMnodeInfos.inUse);
  for (int32_t i = 0; i < tsDMnodeInfos.nodeNum; i++) {
    dPrint("mnode index:%d, %s", tsDMnodeInfos.nodeInfos[i].nodeId, tsDMnodeInfos.nodeInfos[i].nodeEp);
  }
J
jtao1735 已提交
451

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
452 453
  tsDMnodeIpSet.inUse = tsDMnodeInfos.inUse;
  tsDMnodeIpSet.numOfIps = tsDMnodeInfos.nodeNum;
454
  for (int32_t i = 0; i < tsDMnodeInfos.nodeNum; i++) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
455
    taosGetFqdnPortFromEp(tsDMnodeInfos.nodeInfos[i].nodeEp, tsDMnodeIpSet.fqdn[i], &tsDMnodeIpSet.port[i]);
J
jtao1735 已提交
456 457 458 459 460 461 462
  }

  dnodeSaveMnodeInfos();
  sdbUpdateSync();
}

static bool dnodeReadMnodeInfos() {
H
Hui Li 已提交
463 464
  char ipFile[TSDB_FILENAME_LEN*2] = {0};
  
S
Shengliang Guan 已提交
465
  sprintf(ipFile, "%s/mnodeIpList.json", tsDnodeDir);
J
jtao1735 已提交
466 467
  FILE *fp = fopen(ipFile, "r");
  if (!fp) {
S
Shengliang Guan 已提交
468
    dTrace("failed to read mnodeIpList.json, file not exist");
J
jtao1735 已提交
469 470 471 472 473 474 475 476 477 478
    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);
S
Shengliang Guan 已提交
479
    dError("failed to read mnodeIpList.json, content is null");
J
jtao1735 已提交
480 481 482
    return false;
  }

483
  content[len] = 0;
J
jtao1735 已提交
484 485
  cJSON* root = cJSON_Parse(content);
  if (root == NULL) {
S
Shengliang Guan 已提交
486
    dError("failed to read mnodeIpList.json, invalid json format");
J
jtao1735 已提交
487 488 489 490 491
    goto PARSE_OVER;
  }

  cJSON* inUse = cJSON_GetObjectItem(root, "inUse");
  if (!inUse || inUse->type != cJSON_Number) {
S
Shengliang Guan 已提交
492
    dError("failed to read mnodeIpList.json, inUse not found");
J
jtao1735 已提交
493 494
    goto PARSE_OVER;
  }
495
  tsDMnodeInfos.inUse = inUse->valueint;
J
jtao1735 已提交
496 497 498

  cJSON* nodeNum = cJSON_GetObjectItem(root, "nodeNum");
  if (!nodeNum || nodeNum->type != cJSON_Number) {
S
Shengliang Guan 已提交
499
    dError("failed to read mnodeIpList.json, nodeNum not found");
J
jtao1735 已提交
500 501
    goto PARSE_OVER;
  }
502
  tsDMnodeInfos.nodeNum = nodeNum->valueint;
J
jtao1735 已提交
503 504 505

  cJSON* nodeInfos = cJSON_GetObjectItem(root, "nodeInfos");
  if (!nodeInfos || nodeInfos->type != cJSON_Array) {
S
Shengliang Guan 已提交
506
    dError("failed to read mnodeIpList.json, nodeInfos not found");
J
jtao1735 已提交
507 508 509 510
    goto PARSE_OVER;
  }

  int size = cJSON_GetArraySize(nodeInfos);
511
  if (size != tsDMnodeInfos.nodeNum) {
S
Shengliang Guan 已提交
512
    dError("failed to read mnodeIpList.json, nodeInfos size not matched");
J
jtao1735 已提交
513 514 515 516 517 518 519 520 521
    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) {
S
Shengliang Guan 已提交
522
      dError("failed to read mnodeIpList.json, nodeId not found");
J
jtao1735 已提交
523 524
      goto PARSE_OVER;
    }
525
    tsDMnodeInfos.nodeInfos[i].nodeId = nodeId->valueint;
J
jtao1735 已提交
526 527 528

    cJSON *nodeEp = cJSON_GetObjectItem(nodeInfo, "nodeEp");
    if (!nodeEp || nodeEp->type != cJSON_String || nodeEp->valuestring == NULL) {
S
Shengliang Guan 已提交
529
      dError("failed to read mnodeIpList.json, nodeName not found");
J
jtao1735 已提交
530 531
      goto PARSE_OVER;
    }
532
    strncpy(tsDMnodeInfos.nodeInfos[i].nodeEp, nodeEp->valuestring, TSDB_EP_LEN);
J
jtao1735 已提交
533 534 535 536
 }

  ret = true;

537 538 539
  dPrint("read mnode iplist successed, numOfIps:%d inUse:%d", tsDMnodeInfos.nodeNum, tsDMnodeInfos.inUse);
  for (int32_t i = 0; i < tsDMnodeInfos.nodeNum; i++) {
    dPrint("mnode:%d, %s", tsDMnodeInfos.nodeInfos[i].nodeId, tsDMnodeInfos.nodeInfos[i].nodeEp);
J
jtao1735 已提交
540 541 542 543 544 545 546 547 548 549 550
  }

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

static void dnodeSaveMnodeInfos() {
  char ipFile[TSDB_FILENAME_LEN] = {0};
S
Shengliang Guan 已提交
551
  sprintf(ipFile, "%s/mnodeIpList.json", tsDnodeDir);
J
jtao1735 已提交
552 553 554 555 556 557 558 559
  FILE *fp = fopen(ipFile, "w");
  if (!fp) return;

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

  len += snprintf(content + len, maxLen - len, "{\n");
560 561
  len += snprintf(content + len, maxLen - len, "  \"inUse\": %d,\n", tsDMnodeInfos.inUse);
  len += snprintf(content + len, maxLen - len, "  \"nodeNum\": %d,\n", tsDMnodeInfos.nodeNum);
J
jtao1735 已提交
562
  len += snprintf(content + len, maxLen - len, "  \"nodeInfos\": [{\n");
563 564 565 566
  for (int32_t i = 0; i < tsDMnodeInfos.nodeNum; i++) {
    len += snprintf(content + len, maxLen - len, "    \"nodeId\": %d,\n", tsDMnodeInfos.nodeInfos[i].nodeId);
    len += snprintf(content + len, maxLen - len, "    \"nodeEp\": \"%s\"\n", tsDMnodeInfos.nodeInfos[i].nodeEp);
    if (i < tsDMnodeInfos.nodeNum -1) {
J
jtao1735 已提交
567 568 569 570 571 572 573 574
      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);
H
Hui Li 已提交
575
  fflush(fp);
J
jtao1735 已提交
576 577 578 579 580 581 582
  fclose(fp);
  free(content);
  
  dPrint("save mnode iplist successed");
}

char *dnodeGetMnodeMasterEp() {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
583
  return tsDMnodeInfos.nodeInfos[tsDMnodeIpSet.inUse].nodeEp;
J
jtao1735 已提交
584 585 586
}

void* dnodeGetMnodeInfos() {
587
  return &tsDMnodeInfos;
J
jtao1735 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
}

static void dnodeSendStatusMsg(void *handle, void *tmrId) {
  if (tsDnodeTmr == NULL) {
    dError("dnode timer is already released");
    return;
  }

  if (tsStatusTimer == NULL) {
    taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer);
    dError("failed to start status timer");
    return;
  }

  int32_t contLen = sizeof(SDMStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad);
  SDMStatusMsg *pStatus = rpcMallocCont(contLen);
  if (pStatus == NULL) {
    taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer);
    dError("failed to malloc status message");
    return;
  }

  //strcpy(pStatus->dnodeName, tsDnodeName);
  pStatus->version          = htonl(tsVersion);
  pStatus->dnodeId          = htonl(tsDnodeCfg.dnodeId);
  strcpy(pStatus->dnodeEp, tsLocalEp);
  pStatus->lastReboot       = htonl(tsRebootTime);
  pStatus->numOfTotalVnodes = htons((uint16_t) tsNumOfTotalVnodes);
  pStatus->numOfCores       = htons((uint16_t) tsNumOfCores);
  pStatus->diskAvailable    = tsAvailDataDirGB;
  pStatus->alternativeRole  = (uint8_t) tsAlternativeRole;
H
Hui Li 已提交
619 620

  // fill cluster cfg parameters
H
Hui Li 已提交
621 622 623 624 625 626 627 628
  pStatus->clusterCfg.numOfMnodes        = tsNumOfMnodes;
  pStatus->clusterCfg.mnodeEqualVnodeNum = tsMnodeEqualVnodeNum;
  pStatus->clusterCfg.offlineThreshold   = tsOfflineThreshold;
  pStatus->clusterCfg.statusInterval     = tsStatusInterval;
  strcpy(pStatus->clusterCfg.arbitrator, tsArbitrator);
  strcpy(pStatus->clusterCfg.timezone, tsTimezone);
  strcpy(pStatus->clusterCfg.locale, tsLocale);
  strcpy(pStatus->clusterCfg.charset, tsCharset);  
J
jtao1735 已提交
629 630 631 632 633 634 635 636 637 638 639
  
  vnodeBuildStatusMsg(pStatus);
  contLen = sizeof(SDMStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad);
  pStatus->openVnodes = htons(pStatus->openVnodes);
  
  SRpcMsg rpcMsg = {
    .pCont   = pStatus,
    .contLen = contLen,
    .msgType = TSDB_MSG_TYPE_DM_STATUS
  };

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
640 641 642
  SRpcIpSet ipSet;
  dnodeGetMnodeIpSetForPeer(&ipSet);
  dnodeSendMsgToDnode(&ipSet, &rpcMsg);
J
jtao1735 已提交
643 644 645
}

static bool dnodeReadDnodeCfg() {
H
Hui Li 已提交
646 647
  char dnodeCfgFile[TSDB_FILENAME_LEN*2] = {0};
  
J
jtao1735 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  sprintf(dnodeCfgFile, "%s/dnodeCfg.json", tsDnodeDir);

  FILE *fp = fopen(dnodeCfgFile, "r");
  if (!fp) {
    dTrace("failed to read dnodeCfg.json, file not exist");
    return false;
  }

  bool  ret = false;
  int   maxLen = 100;
  char *content = calloc(1, maxLen + 1);
  int   len = fread(content, 1, maxLen, fp);
  if (len <= 0) {
    free(content);
    fclose(fp);
    dError("failed to read dnodeCfg.json, content is null");
    return false;
  }

667
  content[len] = 0;
J
jtao1735 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  cJSON* root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read dnodeCfg.json, invalid json format");
    goto PARSE_CFG_OVER;
  }

  cJSON* dnodeId = cJSON_GetObjectItem(root, "dnodeId");
  if (!dnodeId || dnodeId->type != cJSON_Number) {
    dError("failed to read dnodeCfg.json, dnodeId not found");
    goto PARSE_CFG_OVER;
  }
  tsDnodeCfg.dnodeId = dnodeId->valueint;

  ret = true;

  dPrint("read numOfVnodes successed, dnodeId:%d", tsDnodeCfg.dnodeId);

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

static void dnodeSaveDnodeCfg() {
  char dnodeCfgFile[TSDB_FILENAME_LEN] = {0};
  sprintf(dnodeCfgFile, "%s/dnodeCfg.json", tsDnodeDir);

  FILE *fp = fopen(dnodeCfgFile, "w");
  if (!fp) return;

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

  len += snprintf(content + len, maxLen - len, "{\n");
  len += snprintf(content + len, maxLen - len, "  \"dnodeId\": %d\n", tsDnodeCfg.dnodeId);
  len += snprintf(content + len, maxLen - len, "}\n"); 

  fwrite(content, 1, len, fp);
H
Hui Li 已提交
708
  fflush(fp);
J
jtao1735 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  fclose(fp);
  free(content);
  
  dPrint("save dnodeId successed");
}

void dnodeUpdateDnodeCfg(SDMDnodeCfg *pCfg) {
  if (tsDnodeCfg.dnodeId == 0) {
    dPrint("dnodeId is set to %d", pCfg->dnodeId);  
    tsDnodeCfg.dnodeId = pCfg->dnodeId;
    dnodeSaveDnodeCfg();
  }
}

int32_t dnodeGetDnodeId() {
  return tsDnodeCfg.dnodeId;
}

727
void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) {
S
Shengliang Guan 已提交
728
  SRpcConnInfo connInfo;
729
  rpcGetConnInfo(rpcMsg->handle, &connInfo);
S
Shengliang Guan 已提交
730 731

  SRpcIpSet ipSet = {0};
732 733 734 735 736 737
  if (forShell) {
    dnodeGetMnodeIpSetForShell(&ipSet);
  } else {
    dnodeGetMnodeIpSetForPeer(&ipSet);
  }
  
738
  dTrace("msg:%s will be redirected, dnodeIp:%s user:%s, numOfIps:%d inUse:%d", taosMsg[rpcMsg->msgType],
S
Shengliang Guan 已提交
739 740 741 742 743 744 745
         taosIpStr(connInfo.clientIp), connInfo.user, ipSet.numOfIps, ipSet.inUse);

  for (int i = 0; i < ipSet.numOfIps; ++i) {
    dTrace("mnode index:%d %s:%d", i, ipSet.fqdn[i], ipSet.port[i]);
    ipSet.port[i] = htons(ipSet.port[i]);
  }

746
  rpcSendRedirectRsp(rpcMsg->handle, &ipSet);
747
}