dndDnode.c 18.1 KB
Newer Older
S
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 18 19
#include "dndDnode.h"
#include "dndTransport.h"
#include "dndVnodes.h"
20
#include "tep.h"
S
Shengliang Guan 已提交
21

S
Shengliang Guan 已提交
22
int32_t dndGetDnodeId(SDnode *pDnode) {
S
Shengliang Guan 已提交
23 24 25 26
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
  int32_t dnodeId = pMgmt->dnodeId;
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
27
  return dnodeId;
S
Shengliang Guan 已提交
28 29
}

S
Shengliang Guan 已提交
30
int32_t dndGetClusterId(SDnode *pDnode) {
S
Shengliang Guan 已提交
31 32
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
33
  int32_t clusterId = pMgmt->clusterId;
S
Shengliang Guan 已提交
34
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
35
  return clusterId;
S
Shengliang Guan 已提交
36 37
}

S
Shengliang Guan 已提交
38
void dndGetDnodeEp(SDnode *pDnode, int32_t dnodeId, char *pEp, char *pFqdn, uint16_t *pPort) {
S
Shengliang Guan 已提交
39 40
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
41

S
Shengliang Guan 已提交
42
  SDnodeEp *pDnodeEp = taosHashGet(pMgmt->dnodeHash, &dnodeId, sizeof(int32_t));
S
Shengliang Guan 已提交
43 44 45 46 47 48 49 50 51 52
  if (pDnodeEp != NULL) {
    if (pPort != NULL) {
      *pPort = pDnodeEp->port;
    }
    if (pFqdn != NULL) {
      tstrncpy(pFqdn, pDnodeEp->fqdn, TSDB_FQDN_LEN);
    }
    if (pEp != NULL) {
      snprintf(pEp, TSDB_EP_LEN, "%s:%u", pDnodeEp->fqdn, pDnodeEp->port);
    }
S
Shengliang Guan 已提交
53
  }
S
Shengliang Guan 已提交
54

S
Shengliang Guan 已提交
55
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
56
}
S
Shengliang Guan 已提交
57

S
Shengliang Guan 已提交
58
void dndGetMnodeEpSet(SDnode *pDnode, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
59 60 61 62
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
  *pEpSet = pMgmt->mnodeEpSet;
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
63 64
}

S
Shengliang Guan 已提交
65
void dndSendRedirectMsg(SDnode *pDnode, SRpcMsg *pMsg) {
66
  tmsg_t msgType = pMsg->msgType;
S
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68
  SEpSet epSet = {0};
S
Shengliang Guan 已提交
69
  dndGetMnodeEpSet(pDnode, &epSet);
S
Shengliang Guan 已提交
70

H
Hongze Cheng 已提交
71
  dDebug("RPC %p, msg:%s is redirected, num:%d inUse:%d", pMsg->handle, TMSG_INFO(msgType), epSet.numOfEps, epSet.inUse);
S
Shengliang Guan 已提交
72
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
S
Shengliang Guan 已提交
73
    dDebug("mnode index:%d %s:%u", i, epSet.fqdn[i], epSet.port[i]);
S
Shengliang Guan 已提交
74 75
    if (strcmp(epSet.fqdn[i], pDnode->opt.localFqdn) == 0 && epSet.port[i] == pDnode->opt.serverPort) {
      epSet.inUse = (i + 1) % epSet.numOfEps;
S
Shengliang Guan 已提交
76 77 78 79 80
    }

    epSet.port[i] = htons(epSet.port[i]);
  }

S
Shengliang Guan 已提交
81 82 83
  rpcSendRedirectRsp(pMsg->handle, &epSet);
}

S
Shengliang Guan 已提交
84
static void dndUpdateMnodeEpSet(SDnode *pDnode, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
85
  dInfo("mnode is changed, num:%d inUse:%d", pEpSet->numOfEps, pEpSet->inUse);
S
Shengliang Guan 已提交
86

S
Shengliang Guan 已提交
87 88
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90
  pMgmt->mnodeEpSet = *pEpSet;
S
Shengliang Guan 已提交
91 92 93 94
  for (int32_t i = 0; i < pEpSet->numOfEps; ++i) {
    dInfo("mnode index:%d %s:%u", i, pEpSet->fqdn[i], pEpSet->port[i]);
  }

S
Shengliang Guan 已提交
95
  taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
96 97
}

S
Shengliang Guan 已提交
98 99
static void dndPrintDnodes(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101 102 103
  dDebug("print dnode ep list, num:%d", pMgmt->dnodeEps->num);
  for (int32_t i = 0; i < pMgmt->dnodeEps->num; i++) {
    SDnodeEp *pEp = &pMgmt->dnodeEps->eps[i];
S
Shengliang Guan 已提交
104
    dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", pEp->id, pEp->fqdn, pEp->port, pEp->isMnode);
S
Shengliang Guan 已提交
105 106 107
  }
}

S
Shengliang Guan 已提交
108 109
static void dndResetDnodes(SDnode *pDnode, SDnodeEps *pDnodeEps) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
110

S
Shengliang Guan 已提交
111
  int32_t size = sizeof(SDnodeEps) + pDnodeEps->num * sizeof(SDnodeEp);
S
Shengliang Guan 已提交
112
  if (pDnodeEps->num > pMgmt->dnodeEps->num) {
S
Shengliang Guan 已提交
113 114 115
    SDnodeEps *tmp = calloc(1, size);
    if (tmp == NULL) return;

S
Shengliang Guan 已提交
116 117
    tfree(pMgmt->dnodeEps);
    pMgmt->dnodeEps = tmp;
S
Shengliang Guan 已提交
118 119
  }

S
Shengliang Guan 已提交
120 121
  if (pMgmt->dnodeEps != pDnodeEps) {
    memcpy(pMgmt->dnodeEps, pDnodeEps, size);
S
Shengliang Guan 已提交
122 123
  }

S
Shengliang Guan 已提交
124
  pMgmt->mnodeEpSet.inUse = 0;
125
  pMgmt->mnodeEpSet.numOfEps = 0;
S
Shengliang Guan 已提交
126 127

  int32_t mIndex = 0;
S
Shengliang Guan 已提交
128 129
  for (int32_t i = 0; i < pMgmt->dnodeEps->num; i++) {
    SDnodeEp *pDnodeEp = &pMgmt->dnodeEps->eps[i];
S
Shengliang Guan 已提交
130
    if (!pDnodeEp->isMnode) continue;
S
Shengliang Guan 已提交
131
    if (mIndex >= TSDB_MAX_REPLICA) continue;
132
    pMgmt->mnodeEpSet.numOfEps++;
S
Shengliang Guan 已提交
133 134
    strcpy(pMgmt->mnodeEpSet.fqdn[mIndex], pDnodeEp->fqdn);
    pMgmt->mnodeEpSet.port[mIndex] = pDnodeEp->port;
S
Shengliang Guan 已提交
135
    mIndex++;
S
Shengliang Guan 已提交
136 137
  }

S
Shengliang Guan 已提交
138 139 140
  for (int32_t i = 0; i < pMgmt->dnodeEps->num; ++i) {
    SDnodeEp *pDnodeEp = &pMgmt->dnodeEps->eps[i];
    taosHashPut(pMgmt->dnodeHash, &pDnodeEp->id, sizeof(int32_t), pDnodeEp, sizeof(SDnodeEp));
S
Shengliang Guan 已提交
141 142
  }

S
Shengliang Guan 已提交
143
  dndPrintDnodes(pDnode);
S
Shengliang Guan 已提交
144 145
}

S
Shengliang Guan 已提交
146
static bool dndIsEpChanged(SDnode *pDnode, int32_t dnodeId, char *pEp) {
S
Shengliang Guan 已提交
147 148
  bool changed = false;

S
Shengliang Guan 已提交
149 150 151 152
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);

  SDnodeEp *pDnodeEp = taosHashGet(pMgmt->dnodeHash, &dnodeId, sizeof(int32_t));
S
Shengliang Guan 已提交
153 154 155
  if (pDnodeEp != NULL) {
    char epstr[TSDB_EP_LEN + 1];
    snprintf(epstr, TSDB_EP_LEN, "%s:%u", pDnodeEp->fqdn, pDnodeEp->port);
S
Shengliang Guan 已提交
156
    changed = strcmp(pEp, epstr) != 0;
S
Shengliang Guan 已提交
157 158
  }

S
Shengliang Guan 已提交
159
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
160 161 162
  return changed;
}

S
Shengliang Guan 已提交
163 164
static int32_t dndReadDnodes(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
165

S
Shengliang Guan 已提交
166
  int32_t code = TSDB_CODE_DND_DNODE_READ_FILE_ERROR;
S
Shengliang Guan 已提交
167 168 169 170 171 172
  int32_t len = 0;
  int32_t maxLen = 30000;
  char   *content = calloc(1, maxLen + 1);
  cJSON  *root = NULL;
  FILE   *fp = NULL;

S
Shengliang Guan 已提交
173 174 175 176
  fp = fopen(pMgmt->file, "r");
  if (fp == NULL) {
    dDebug("file %s not exist", pMgmt->file);
    code = 0;
S
Shengliang Guan 已提交
177
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
178 179 180 181
  }

  len = (int32_t)fread(content, 1, maxLen, fp);
  if (len <= 0) {
S
Shengliang Guan 已提交
182
    dError("failed to read %s since content is null", pMgmt->file);
S
Shengliang Guan 已提交
183
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
184 185 186 187 188
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
S
Shengliang Guan 已提交
189
    dError("failed to read %s since invalid json format", pMgmt->file);
S
Shengliang Guan 已提交
190
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
191 192 193
  }

  cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId");
S
Shengliang Guan 已提交
194
  if (!dnodeId || dnodeId->type != cJSON_Number) {
S
Shengliang Guan 已提交
195
    dError("failed to read %s since dnodeId not found", pMgmt->file);
S
Shengliang Guan 已提交
196
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
197
  }
S
Shengliang Guan 已提交
198
  pMgmt->dnodeId = dnodeId->valueint;
S
Shengliang Guan 已提交
199

S
Shengliang Guan 已提交
200
  cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId");
S
Shengliang Guan 已提交
201
  if (!clusterId || clusterId->type != cJSON_Number) {
S
Shengliang Guan 已提交
202
    dError("failed to read %s since clusterId not found", pMgmt->file);
S
Shengliang Guan 已提交
203
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
204
  }
S
Shengliang Guan 已提交
205
  pMgmt->clusterId = clusterId->valueint;
S
Shengliang Guan 已提交
206

S
Shengliang Guan 已提交
207
  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
S
Shengliang Guan 已提交
208
  if (!dropped || dropped->type != cJSON_Number) {
S
Shengliang Guan 已提交
209
    dError("failed to read %s since dropped not found", pMgmt->file);
S
Shengliang Guan 已提交
210
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
211
  }
S
Shengliang Guan 已提交
212
  pMgmt->dropped = dropped->valueint;
S
Shengliang Guan 已提交
213

S
Shengliang Guan 已提交
214 215 216
  cJSON *dnodes = cJSON_GetObjectItem(root, "dnodes");
  if (!dnodes || dnodes->type != cJSON_Array) {
    dError("failed to read %s since dnodes not found", pMgmt->file);
S
Shengliang Guan 已提交
217
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
218 219
  }

S
Shengliang Guan 已提交
220 221 222
  int32_t numOfNodes = cJSON_GetArraySize(dnodes);
  if (numOfNodes <= 0) {
    dError("failed to read %s since numOfNodes:%d invalid", pMgmt->file, numOfNodes);
S
Shengliang Guan 已提交
223
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
224 225
  }

S
Shengliang Guan 已提交
226
  pMgmt->dnodeEps = calloc(1, numOfNodes * sizeof(SDnodeEp) + sizeof(SDnodeEps));
S
Shengliang Guan 已提交
227
  if (pMgmt->dnodeEps == NULL) {
S
Shengliang Guan 已提交
228
    dError("failed to calloc dnodeEpList since %s", strerror(errno));
S
Shengliang Guan 已提交
229
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
230
  }
S
Shengliang Guan 已提交
231
  pMgmt->dnodeEps->num = numOfNodes;
S
Shengliang Guan 已提交
232

S
Shengliang Guan 已提交
233 234 235
  for (int32_t i = 0; i < numOfNodes; ++i) {
    cJSON *node = cJSON_GetArrayItem(dnodes, i);
    if (node == NULL) break;
S
Shengliang Guan 已提交
236

S
Shengliang Guan 已提交
237
    SDnodeEp *pDnodeEp = &pMgmt->dnodeEps->eps[i];
S
Shengliang Guan 已提交
238

S
Shengliang Guan 已提交
239 240
    cJSON *dnodeId = cJSON_GetObjectItem(node, "id");
    if (!dnodeId || dnodeId->type != cJSON_Number) {
S
Shengliang Guan 已提交
241
      dError("failed to read %s, dnodeId not found", pMgmt->file);
S
Shengliang Guan 已提交
242
      goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
243
    }
S
Shengliang Guan 已提交
244
    pDnodeEp->id = dnodeId->valueint;
S
Shengliang Guan 已提交
245

S
Shengliang Guan 已提交
246
    cJSON *dnodeFqdn = cJSON_GetObjectItem(node, "fqdn");
S
Shengliang Guan 已提交
247
    if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) {
S
Shengliang Guan 已提交
248
      dError("failed to read %s, dnodeFqdn not found", pMgmt->file);
S
Shengliang Guan 已提交
249
      goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
250
    }
S
Shengliang Guan 已提交
251
    tstrncpy(pDnodeEp->fqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
252

S
Shengliang Guan 已提交
253 254
    cJSON *dnodePort = cJSON_GetObjectItem(node, "port");
    if (!dnodePort || dnodePort->type != cJSON_Number) {
S
Shengliang Guan 已提交
255
      dError("failed to read %s, dnodePort not found", pMgmt->file);
S
Shengliang Guan 已提交
256
      goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
257
    }
S
Shengliang Guan 已提交
258 259 260 261 262 263 264 265
    pDnodeEp->port = dnodePort->valueint;

    cJSON *isMnode = cJSON_GetObjectItem(node, "isMnode");
    if (!isMnode || isMnode->type != cJSON_Number) {
      dError("failed to read %s, isMnode not found", pMgmt->file);
      goto PRASE_DNODE_OVER;
    }
    pDnodeEp->isMnode = isMnode->valueint;
S
Shengliang Guan 已提交
266 267
  }

S
Shengliang Guan 已提交
268 269 270
  code = 0;
  dInfo("succcessed to read file %s", pMgmt->file);
  dndPrintDnodes(pDnode);
S
Shengliang Guan 已提交
271

S
Shengliang Guan 已提交
272
PRASE_DNODE_OVER:
S
Shengliang Guan 已提交
273 274 275 276
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (fp != NULL) fclose(fp);

S
Shengliang Guan 已提交
277 278
  if (dndIsEpChanged(pDnode, pMgmt->dnodeId, pDnode->opt.localEp)) {
    dError("localEp %s different with %s and need reconfigured", pDnode->opt.localEp, pMgmt->file);
S
Shengliang Guan 已提交
279 280 281
    return -1;
  }

S
Shengliang Guan 已提交
282 283 284
  if (pMgmt->dnodeEps == NULL) {
    pMgmt->dnodeEps = calloc(1, sizeof(SDnodeEps) + sizeof(SDnodeEp));
    pMgmt->dnodeEps->num = 1;
S
Shengliang Guan 已提交
285 286
    pMgmt->dnodeEps->eps[0].isMnode = 1;   
    taosGetFqdnPortFromEp(pDnode->opt.firstEp, pMgmt->dnodeEps->eps[0].fqdn, &pMgmt->dnodeEps->eps[0].port);
S
Shengliang Guan 已提交
287 288
  }

S
Shengliang Guan 已提交
289
  dndResetDnodes(pDnode, pMgmt->dnodeEps);
S
Shengliang Guan 已提交
290 291 292 293 294

  terrno = 0;
  return 0;
}

S
Shengliang Guan 已提交
295 296
static int32_t dndWriteDnodes(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
297

S
Shengliang Guan 已提交
298 299 300 301
  FILE *fp = fopen(pMgmt->file, "w");
  if (fp == NULL) {
    dError("failed to write %s since %s", pMgmt->file, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
302 303 304 305 306 307 308 309
    return -1;
  }

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

  len += snprintf(content + len, maxLen - len, "{\n");
S
Shengliang Guan 已提交
310 311 312 313
  len += snprintf(content + len, maxLen - len, "  \"dnodeId\": %d,\n", pMgmt->dnodeId);
  len += snprintf(content + len, maxLen - len, "  \"clusterId\": %d,\n", pMgmt->clusterId);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": %d,\n", pMgmt->dropped);
  len += snprintf(content + len, maxLen - len, "  \"dnodes\": [{\n");
S
Shengliang Guan 已提交
314 315
  for (int32_t i = 0; i < pMgmt->dnodeEps->num; ++i) {
    SDnodeEp *pDnodeEp = &pMgmt->dnodeEps->eps[i];
S
Shengliang Guan 已提交
316 317 318 319
    len += snprintf(content + len, maxLen - len, "    \"id\": %d,\n", pDnodeEp->id);
    len += snprintf(content + len, maxLen - len, "    \"fqdn\": \"%s\",\n", pDnodeEp->fqdn);
    len += snprintf(content + len, maxLen - len, "    \"port\": %u,\n", pDnodeEp->port);
    len += snprintf(content + len, maxLen - len, "    \"isMnode\": %d\n", pDnodeEp->isMnode);
S
Shengliang Guan 已提交
320
    if (i < pMgmt->dnodeEps->num - 1) {
S
Shengliang Guan 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333
      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);
  taosFsyncFile(fileno(fp));
  fclose(fp);
  free(content);
  terrno = 0;

S
Shengliang Guan 已提交
334
  dInfo("successed to write %s", pMgmt->file);
S
Shengliang Guan 已提交
335 336 337
  return 0;
}

338
void dndSendStatusMsg(SDnode *pDnode) {
S
Shengliang Guan 已提交
339 340
  int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad);

S
Shengliang Guan 已提交
341 342 343 344 345 346
  SStatusMsg *pStatus = rpcMallocCont(contLen);
  if (pStatus == NULL) {
    dError("failed to malloc status message");
    return;
  }

S
Shengliang Guan 已提交
347 348
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
349
  pStatus->sver = htonl(pDnode->opt.sver);
S
Shengliang Guan 已提交
350
  pStatus->dnodeId = htonl(pMgmt->dnodeId);
S
Shengliang Guan 已提交
351
  pStatus->clusterId = htonl(pMgmt->clusterId);
352
  pStatus->rebootTime = htobe64(pMgmt->rebootTime);
S
Shengliang Guan 已提交
353 354 355 356
  pStatus->numOfCores = htons(pDnode->opt.numOfCores);
  pStatus->numOfSupportMnodes = htons(pDnode->opt.numOfCores);
  pStatus->numOfSupportVnodes = htons(pDnode->opt.numOfCores);
  pStatus->numOfSupportQnodes = htons(pDnode->opt.numOfCores);
S
Shengliang Guan 已提交
357
  tstrncpy(pStatus->dnodeEp, pDnode->opt.localEp, TSDB_EP_LEN);
S
Shengliang Guan 已提交
358

S
Shengliang Guan 已提交
359
  pStatus->clusterCfg.statusInterval = htonl(pDnode->opt.statusInterval);
S
Shengliang Guan 已提交
360
  pStatus->clusterCfg.checkTime = 0;
S
Shengliang Guan 已提交
361 362
  char timestr[32] = "1970-01-01 00:00:00.00";
  (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0);
S
Shengliang Guan 已提交
363 364 365 366
  pStatus->clusterCfg.checkTime = htonl(pStatus->clusterCfg.checkTime);
  tstrncpy(pStatus->clusterCfg.timezone, pDnode->opt.timezone, TSDB_TIMEZONE_LEN);
  tstrncpy(pStatus->clusterCfg.locale, pDnode->opt.locale, TSDB_LOCALE_LEN);
  tstrncpy(pStatus->clusterCfg.charset, pDnode->opt.charset, TSDB_LOCALE_LEN);
S
Shengliang Guan 已提交
367
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
368

S
Shengliang Guan 已提交
369
  dndGetVnodeLoads(pDnode, &pStatus->vnodeLoads);
S
Shengliang Guan 已提交
370
  contLen = sizeof(SStatusMsg) + pStatus->vnodeLoads.num * sizeof(SVnodeLoad);
S
Shengliang Guan 已提交
371

372
  SRpcMsg rpcMsg = {.pCont = pStatus, .contLen = contLen, .msgType = TDMT_MND_STATUS, .ahandle = 9527};
373
  pMgmt->statusSent = 1;
S
Shengliang Guan 已提交
374 375

  dTrace("pDnode:%p, send status msg to mnode", pDnode);
S
Shengliang Guan 已提交
376
  dndSendMsgToMnode(pDnode, &rpcMsg);
S
Shengliang Guan 已提交
377 378
}

S
Shengliang Guan 已提交
379 380 381
static void dndUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  if (pMgmt->dnodeId == 0 || pMgmt->dropped != pCfg->dropped) {
S
Shengliang Guan 已提交
382
    dInfo("set dnodeId:%d clusterId:%d dropped:%d", pCfg->dnodeId, pCfg->clusterId, pCfg->dropped);
S
Shengliang Guan 已提交
383

S
Shengliang Guan 已提交
384
    taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
385 386 387
    pMgmt->dnodeId = pCfg->dnodeId;
    pMgmt->clusterId = pCfg->clusterId;
    pMgmt->dropped = pCfg->dropped;
388
    dndWriteDnodes(pDnode);
S
Shengliang Guan 已提交
389
    taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
390
  }
S
Shengliang Guan 已提交
391 392
}

S
Shengliang Guan 已提交
393
static void dndUpdateDnodeEps(SDnode *pDnode, SDnodeEps *pDnodeEps) {
S
Shengliang Guan 已提交
394
  if (pDnodeEps == NULL || pDnodeEps->num <= 0) return;
S
Shengliang Guan 已提交
395

S
Shengliang Guan 已提交
396 397
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
398

S
Shengliang Guan 已提交
399
  if (pDnodeEps->num != pMgmt->dnodeEps->num) {
S
Shengliang Guan 已提交
400 401
    dndResetDnodes(pDnode, pDnodeEps);
    dndWriteDnodes(pDnode);
S
Shengliang Guan 已提交
402
  } else {
S
Shengliang Guan 已提交
403
    int32_t size = pDnodeEps->num * sizeof(SDnodeEp) + sizeof(SDnodeEps);
S
Shengliang Guan 已提交
404
    if (memcmp(pMgmt->dnodeEps, pDnodeEps, size) != 0) {
S
Shengliang Guan 已提交
405 406
      dndResetDnodes(pDnode, pDnodeEps);
      dndWriteDnodes(pDnode);
S
Shengliang Guan 已提交
407 408 409
    }
  }

S
Shengliang Guan 已提交
410
  taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
411 412
}

S
Shengliang Guan 已提交
413
static void dndProcessStatusRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
414 415
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;

S
Shengliang Guan 已提交
416
  if (pEpSet && pEpSet->numOfEps > 0) {
S
Shengliang Guan 已提交
417
    dndUpdateMnodeEpSet(pDnode, pEpSet);
S
Shengliang Guan 已提交
418 419
  }

420 421 422 423
  if (pMsg->code != TSDB_CODE_SUCCESS) {
    pMgmt->statusSent = 0;
    return;
  }
S
Shengliang Guan 已提交
424

S
Shengliang Guan 已提交
425 426
  SStatusRsp *pRsp = pMsg->pCont;
  SDnodeCfg  *pCfg = &pRsp->dnodeCfg;
S
Shengliang Guan 已提交
427
  pCfg->dnodeId = htonl(pCfg->dnodeId);
S
Shengliang Guan 已提交
428
  pCfg->clusterId = htonl(pCfg->clusterId);
S
Shengliang Guan 已提交
429
  dndUpdateDnodeCfg(pDnode, pCfg);
S
Shengliang Guan 已提交
430

431 432 433 434
  if (pCfg->dropped) {
    pMgmt->statusSent = 0;
    return;
  }
S
Shengliang Guan 已提交
435

S
Shengliang Guan 已提交
436
  SDnodeEps *pDnodeEps = &pRsp->dnodeEps;
S
Shengliang Guan 已提交
437 438 439 440
  pDnodeEps->num = htonl(pDnodeEps->num);
  for (int32_t i = 0; i < pDnodeEps->num; ++i) {
    pDnodeEps->eps[i].id = htonl(pDnodeEps->eps[i].id);
    pDnodeEps->eps[i].port = htons(pDnodeEps->eps[i].port);
S
Shengliang Guan 已提交
441 442
  }

S
Shengliang Guan 已提交
443
  dndUpdateDnodeEps(pDnode, pDnodeEps);
444
  pMgmt->statusSent = 0;
S
Shengliang Guan 已提交
445
}
S
Shengliang Guan 已提交
446

S
Shengliang Guan 已提交
447
static void dndProcessAuthRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { assert(1); }
S
Shengliang Guan 已提交
448

S
Shengliang Guan 已提交
449
static void dndProcessGrantRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { assert(1); }
S
Shengliang Guan 已提交
450

S
Shengliang Guan 已提交
451
static void dndProcessConfigDnodeReq(SDnode *pDnode, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
452
  dError("config msg is received, but not supported yet");
S
Shengliang Guan 已提交
453
  SCfgDnodeMsg *pCfg = pMsg->pCont;
S
Shengliang Guan 已提交
454

S
Shengliang Guan 已提交
455
  int32_t code = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
456 457 458 459
  SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code};
  rpcSendResponse(&rspMsg);
}

S
Shengliang Guan 已提交
460
static void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
461
  dDebug("startup msg is received");
S
Shengliang Guan 已提交
462

S
Shengliang Guan 已提交
463
  SStartupMsg *pStartup = rpcMallocCont(sizeof(SStartupMsg));
S
Shengliang Guan 已提交
464
  dndGetStartup(pDnode, pStartup);
S
Shengliang Guan 已提交
465

S
Shengliang Guan 已提交
466
  dDebug("startup msg is sent, step:%s desc:%s finished:%d", pStartup->name, pStartup->desc, pStartup->finished);
S
Shengliang Guan 已提交
467

S
Shengliang Guan 已提交
468
  SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStartup, .contLen = sizeof(SStartupMsg)};
S
Shengliang Guan 已提交
469
  rpcSendResponse(&rpcRsp);
S
Shengliang Guan 已提交
470 471 472
}

static void *dnodeThreadRoutine(void *param) {
473 474 475
  SDnode     *pDnode = param;
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  int32_t     ms = pDnode->opt.statusInterval * 1000;
S
Shengliang Guan 已提交
476

S
Shengliang Guan 已提交
477 478
  while (true) {
    pthread_testcancel();
S
Shengliang Guan 已提交
479
    taosMsleep(ms);
S
Shengliang Guan 已提交
480

481
    if (dndGetStat(pDnode) == DND_STAT_RUNNING && !pMgmt->statusSent) {
S
Shengliang Guan 已提交
482
      dndSendStatusMsg(pDnode);
S
Shengliang Guan 已提交
483
    }
S
Shengliang Guan 已提交
484 485 486
  }
}

S
Shengliang Guan 已提交
487 488
int32_t dndInitDnode(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
489

S
Shengliang Guan 已提交
490
  pMgmt->dnodeId = 0;
491
  pMgmt->rebootTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
492 493
  pMgmt->dropped = 0;
  pMgmt->clusterId = 0;
S
Shengliang Guan 已提交
494 495

  char path[PATH_MAX];
S
Shengliang Guan 已提交
496 497 498
  snprintf(path, PATH_MAX, "%s/dnode.json", pDnode->dir.dnode);
  pMgmt->file = strdup(path);
  if (pMgmt->file == NULL) {
S
Shengliang Guan 已提交
499 500 501 502
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

503
  pMgmt->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
S
Shengliang Guan 已提交
504
  if (pMgmt->dnodeHash == NULL) {
S
Shengliang Guan 已提交
505
    dError("failed to init dnode hash");
S
Shengliang Guan 已提交
506 507
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
508 509
  }

S
Shengliang Guan 已提交
510 511
  if (dndReadDnodes(pDnode) != 0) {
    dError("failed to read file:%s since %s", pMgmt->file, terrstr());
S
Shengliang Guan 已提交
512
    return -1;
S
Shengliang Guan 已提交
513 514
  }

S
Shengliang Guan 已提交
515
  taosInitRWLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
516

S
Shengliang Guan 已提交
517 518
  pMgmt->threadId = taosCreateThread(dnodeThreadRoutine, pDnode);
  if (pMgmt->threadId == NULL) {
S
Shengliang Guan 已提交
519 520 521
    dError("failed to init dnode thread");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
522 523
  }

S
Shengliang Guan 已提交
524
  dInfo("dnode-dnode is initialized");
S
Shengliang Guan 已提交
525 526 527
  return 0;
}

S
Shengliang Guan 已提交
528 529
void dndCleanupDnode(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
530

S
Shengliang Guan 已提交
531 532 533
  if (pMgmt->threadId != NULL) {
    taosDestoryThread(pMgmt->threadId);
    pMgmt->threadId = NULL;
S
Shengliang Guan 已提交
534 535
  }

S
Shengliang Guan 已提交
536
  taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
537

S
Shengliang Guan 已提交
538 539 540
  if (pMgmt->dnodeEps != NULL) {
    free(pMgmt->dnodeEps);
    pMgmt->dnodeEps = NULL;
S
Shengliang Guan 已提交
541
  }
S
Shengliang Guan 已提交
542

S
Shengliang Guan 已提交
543 544 545
  if (pMgmt->dnodeHash != NULL) {
    taosHashCleanup(pMgmt->dnodeHash);
    pMgmt->dnodeHash = NULL;
S
Shengliang Guan 已提交
546
  }
S
Shengliang Guan 已提交
547

S
Shengliang Guan 已提交
548 549 550
  if (pMgmt->file != NULL) {
    free(pMgmt->file);
    pMgmt->file = NULL;
S
Shengliang Guan 已提交
551 552
  }

S
Shengliang Guan 已提交
553
  taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
554
  dInfo("dnode-dnode is cleaned up");
S
Shengliang Guan 已提交
555 556
}

S
Shengliang Guan 已提交
557
void dndProcessDnodeReq(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
558
  switch (pMsg->msgType) {
H
Hongze Cheng 已提交
559
    case TDMT_DND_NETWORK_TEST:
S
Shengliang Guan 已提交
560
      dndProcessStartupReq(pDnode, pMsg);
S
Shengliang Guan 已提交
561
      break;
H
Hongze Cheng 已提交
562
    case TDMT_DND_CONFIG_DNODE:
S
Shengliang Guan 已提交
563
      dndProcessConfigDnodeReq(pDnode, pMsg);
S
Shengliang Guan 已提交
564 565
      break;
    default:
H
Hongze Cheng 已提交
566
      dError("RPC %p, dnode req:%s not processed", pMsg->handle, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
567
      SRpcMsg rspMsg = {.handle = pMsg->handle, .code = TSDB_CODE_MSG_NOT_PROCESSED};
S
Shengliang Guan 已提交
568 569
      rpcSendResponse(&rspMsg);
  }
S
Shengliang Guan 已提交
570 571 572

  rpcFreeCont(pMsg->pCont);
  pMsg->pCont = NULL;
S
Shengliang Guan 已提交
573
}
S
Shengliang Guan 已提交
574

S
Shengliang Guan 已提交
575
void dndProcessDnodeRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
576
  switch (pMsg->msgType) {
H
Hongze Cheng 已提交
577
    case TDMT_MND_STATUS_RSP:
S
Shengliang Guan 已提交
578
      dndProcessStatusRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
579
      break;
H
Hongze Cheng 已提交
580
    case TDMT_MND_AUTH_RSP:
S
Shengliang Guan 已提交
581
      dndProcessAuthRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
582
      break;
H
Hongze Cheng 已提交
583
    case TDMT_MND_GRANT_RSP:
S
Shengliang Guan 已提交
584
      dndProcessGrantRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
585 586
      break;
    default:
H
Hongze Cheng 已提交
587
      dError("RPC %p, dnode rsp:%s not processed", pMsg->handle, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
588
  }
S
Shengliang Guan 已提交
589

S
Shengliang Guan 已提交
590
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
591
  pMsg->pCont = NULL;
S
Shengliang Guan 已提交
592
}