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 20
#include "dndDnode.h"
#include "dndTransport.h"
#include "dndVnodes.h"

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

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

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

S
Shengliang Guan 已提交
41
  SDnodeEp *pDnodeEp = taosHashGet(pMgmt->dnodeHash, &dnodeId, sizeof(int32_t));
S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50 51
  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 已提交
52
  }
S
Shengliang Guan 已提交
53

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
89
  pMgmt->mnodeEpSet = *pEpSet;
S
Shengliang Guan 已提交
90 91 92 93
  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 已提交
94
  taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
95 96
}

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

S
Shengliang Guan 已提交
100 101 102
  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 已提交
103
    dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", pEp->id, pEp->fqdn, pEp->port, pEp->isMnode);
S
Shengliang Guan 已提交
104 105 106
  }
}

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

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

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

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

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

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

S
Shengliang Guan 已提交
137 138 139
  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 已提交
140 141
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
213 214 215
  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 已提交
216
    goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
217 218
  }

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
276 277
  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 已提交
278 279 280
    return -1;
  }

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

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

  terrno = 0;
  return 0;
}

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

S
Shengliang Guan 已提交
297 298 299 300
  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 已提交
301 302 303 304 305 306 307 308
    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 已提交
309 310 311 312
  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 已提交
313 314
  for (int32_t i = 0; i < pMgmt->dnodeEps->num; ++i) {
    SDnodeEp *pDnodeEp = &pMgmt->dnodeEps->eps[i];
S
Shengliang Guan 已提交
315 316 317 318
    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 已提交
319
    if (i < pMgmt->dnodeEps->num - 1) {
S
Shengliang Guan 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332
      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 已提交
333
  dInfo("successed to write %s", pMgmt->file);
S
Shengliang Guan 已提交
334 335 336
  return 0;
}

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

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

S
Shengliang Guan 已提交
346 347
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
348
  pStatus->sver = htonl(pDnode->opt.sver);
S
Shengliang Guan 已提交
349
  pStatus->dnodeId = htonl(pMgmt->dnodeId);
S
Shengliang Guan 已提交
350
  pStatus->clusterId = htonl(pMgmt->clusterId);
351
  pStatus->rebootTime = htobe64(pMgmt->rebootTime);
S
Shengliang Guan 已提交
352 353 354 355
  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 已提交
356
  tstrncpy(pStatus->dnodeEp, pDnode->opt.localEp, TSDB_EP_LEN);
S
Shengliang Guan 已提交
357

S
Shengliang Guan 已提交
358
  pStatus->clusterCfg.statusInterval = htonl(pDnode->opt.statusInterval);
S
Shengliang Guan 已提交
359
  pStatus->clusterCfg.checkTime = 0;
S
Shengliang Guan 已提交
360 361
  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 已提交
362 363 364 365
  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 已提交
366
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
367

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
435
  SDnodeEps *pDnodeEps = &pRsp->dnodeEps;
S
Shengliang Guan 已提交
436 437 438 439
  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 已提交
440 441
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
574
void dndProcessDnodeRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
575 576
  switch (pMsg->msgType) {
    case TSDB_MSG_TYPE_STATUS_RSP:
S
Shengliang Guan 已提交
577
      dndProcessStatusRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
578 579
      break;
    case TSDB_MSG_TYPE_AUTH_RSP:
S
Shengliang Guan 已提交
580
      dndProcessAuthRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
581 582
      break;
    case TSDB_MSG_TYPE_GRANT_RSP:
S
Shengliang Guan 已提交
583
      dndProcessGrantRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
584 585 586 587
      break;
    default:
      dError("RPC %p, dnode rsp:%s not processed", pMsg->handle, taosMsg[pMsg->msgType]);
  }
S
Shengliang Guan 已提交
588

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