dndDnode.c 17.8 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
int64_t dndGetClusterId(SDnode *pDnode) {
S
Shengliang Guan 已提交
30 31 32 33
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
  int64_t clusterId = pMgmt->clusterId;
  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 193
  }

  cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId");
  if (!dnodeId || dnodeId->type != cJSON_String) {
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 = atoi(dnodeId->valuestring);
S
Shengliang Guan 已提交
198

S
Shengliang Guan 已提交
199 200
  cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId");
  if (!clusterId || clusterId->type != cJSON_String) {
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 = atoll(clusterId->valuestring);
S
Shengliang Guan 已提交
205

S
Shengliang Guan 已提交
206 207
  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_String) {
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 = atoi(dropped->valuestring);
S
Shengliang Guan 已提交
212 213 214

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

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

S
Shengliang Guan 已提交
225 226
  pMgmt->dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps));
  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 = dnodeInfosSize;
S
Shengliang Guan 已提交
231 232 233 234 235

  for (int32_t i = 0; i < dnodeInfosSize; ++i) {
    cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i);
    if (dnodeInfo == NULL) break;

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

    cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId");
    if (!dnodeId || dnodeId->type != cJSON_String) {
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 = atoi(dnodeId->valuestring);
S
Shengliang Guan 已提交
244 245 246

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

    cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn");
    if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) {
S
Shengliang Guan 已提交
254
      dError("failed to read %s, dnodeFqdn not found", pMgmt->file);
S
Shengliang Guan 已提交
255
      goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
256
    }
S
Shengliang Guan 已提交
257
    tstrncpy(pDnodeEp->fqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN);
S
Shengliang Guan 已提交
258 259 260

    cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort");
    if (!dnodePort || dnodePort->type != cJSON_String) {
S
Shengliang Guan 已提交
261
      dError("failed to read %s, dnodePort not found", pMgmt->file);
S
Shengliang Guan 已提交
262
      goto PRASE_DNODE_OVER;
S
Shengliang Guan 已提交
263
    }
S
Shengliang Guan 已提交
264
    pDnodeEp->port = atoi(dnodePort->valuestring);
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;
284
    pMgmt->dnodeEps->eps[0].isMnode = 1;
S
Shengliang Guan 已提交
285 286
    pMgmt->dnodeEps->eps[0].port = pDnode->opt.serverPort;
    tstrncpy(pMgmt->dnodeEps->eps[0].fqdn, pDnode->opt.localFqdn, TSDB_FQDN_LEN);
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
  len += snprintf(content + len, maxLen - len, "  \"dnodeId\": \"%d\",\n", pMgmt->dnodeId);
  len += snprintf(content + len, maxLen - len, "  \"clusterId\": \"%" PRId64 "\",\n", pMgmt->clusterId);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": \"%d\",\n", pMgmt->dropped);
S
Shengliang Guan 已提交
313
  len += snprintf(content + len, maxLen - len, "  \"dnodeInfos\": [{\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, "    \"dnodeId\": \"%d\",\n", pDnodeEp->id);
    len += snprintf(content + len, maxLen - len, "    \"isMnode\": \"%d\",\n", pDnodeEp->isMnode);
    len += snprintf(content + len, maxLen - len, "    \"dnodeFqdn\": \"%s\",\n", pDnodeEp->fqdn);
    len += snprintf(content + len, maxLen - len, "    \"dnodePort\": \"%u\"\n", pDnodeEp->port);
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;
}

S
Shengliang Guan 已提交
338 339 340
static void dndSendStatusMsg(SDnode *pDnode) {
  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 349 350
  bool changed = false;

  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
351
  pStatus->sversion = htonl(pDnode->opt.sver);
S
Shengliang Guan 已提交
352 353 354
  pStatus->dnodeId = htonl(pMgmt->dnodeId);
  pStatus->clusterId = htobe64(pMgmt->clusterId);
  pStatus->rebootTime = htonl(pMgmt->rebootTime);
S
Shengliang Guan 已提交
355 356 357 358 359 360
  pStatus->numOfCores = htonl(pDnode->opt.numOfCores);
  tstrncpy(pStatus->dnodeEp, pDnode->opt.localEp, TSDB_EP_LEN);
  pStatus->clusterCfg.statusInterval = htonl(pDnode->opt.statusInterval);
  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 已提交
361
  pStatus->clusterCfg.checkTime = 0;
S
Shengliang Guan 已提交
362 363
  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 已提交
364
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
365

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

S
Shengliang Guan 已提交
369
  SRpcMsg rpcMsg = {.pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_STATUS};
S
Shengliang Guan 已提交
370
  dndSendMsgToMnode(pDnode, &rpcMsg);
S
Shengliang Guan 已提交
371 372
}

S
Shengliang Guan 已提交
373 374 375 376
static void dndUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  if (pMgmt->dnodeId == 0 || pMgmt->dropped != pCfg->dropped) {
    dInfo("set dnodeId:%d clusterId:%" PRId64 " dropped:%d", pCfg->dnodeId, pCfg->clusterId, pCfg->dropped);
S
Shengliang Guan 已提交
377

S
Shengliang Guan 已提交
378
    taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
379 380 381 382
    pMgmt->dnodeId = pCfg->dnodeId;
    pMgmt->clusterId = pCfg->clusterId;
    pMgmt->dropped = pCfg->dropped;
    (void)dndWriteDnodes(pDnode);
S
Shengliang Guan 已提交
383
    taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
384
  }
S
Shengliang Guan 已提交
385 386
}

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

S
Shengliang Guan 已提交
390 391
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
  taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
392

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

S
Shengliang Guan 已提交
404
  taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
405 406
}

S
Shengliang Guan 已提交
407
static void dndProcessStatusRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
408
  if (pEpSet && pEpSet->numOfEps > 0) {
S
Shengliang Guan 已提交
409
    dndUpdateMnodeEpSet(pDnode, pEpSet);
S
Shengliang Guan 已提交
410 411
  }

S
Shengliang Guan 已提交
412 413
  if (pMsg->code != TSDB_CODE_SUCCESS) return;

S
Shengliang Guan 已提交
414 415
  SStatusRsp *pRsp = pMsg->pCont;
  SDnodeCfg  *pCfg = &pRsp->dnodeCfg;
S
Shengliang Guan 已提交
416 417
  pCfg->dnodeId = htonl(pCfg->dnodeId);
  pCfg->clusterId = htobe64(pCfg->clusterId);
S
Shengliang Guan 已提交
418
  dndUpdateDnodeCfg(pDnode, pCfg);
S
Shengliang Guan 已提交
419

S
Shengliang Guan 已提交
420 421
  if (pCfg->dropped) return;

S
Shengliang Guan 已提交
422
  SDnodeEps *pDnodeEps = &pRsp->dnodeEps;
S
Shengliang Guan 已提交
423 424 425 426
  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 已提交
427 428
  }

S
Shengliang Guan 已提交
429
  dndUpdateDnodeEps(pDnode, pDnodeEps);
S
Shengliang Guan 已提交
430
}
S
Shengliang Guan 已提交
431

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

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

S
Shengliang Guan 已提交
436
static void dndProcessConfigDnodeReq(SDnode *pDnode, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
437
  SCfgDnodeMsg *pCfg = pMsg->pCont;
S
Shengliang Guan 已提交
438

S
Shengliang Guan 已提交
439
  int32_t code = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
440 441 442 443 444
  SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code};
  rpcSendResponse(&rspMsg);
  rpcFreeCont(pMsg->pCont);
}

S
Shengliang Guan 已提交
445
static void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
446
  dInfo("startup msg is received");
S
Shengliang Guan 已提交
447

S
Shengliang Guan 已提交
448
  SStartupMsg *pStartup = rpcMallocCont(sizeof(SStartupMsg));
S
Shengliang Guan 已提交
449
  dndGetStartup(pDnode, pStartup);
S
Shengliang Guan 已提交
450

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

S
Shengliang Guan 已提交
453
  SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStartup, .contLen = sizeof(SStartupMsg)};
S
Shengliang Guan 已提交
454 455
  rpcSendResponse(&rpcRsp);
  rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
456 457 458
}

static void *dnodeThreadRoutine(void *param) {
S
Shengliang Guan 已提交
459 460
  SDnode *pDnode = param;
  int32_t ms = pDnode->opt.statusInterval * 1000;
S
Shengliang Guan 已提交
461

S
Shengliang Guan 已提交
462 463 464
  while (true) {
    taosMsleep(ms);
    pthread_testcancel();
S
Shengliang Guan 已提交
465 466

    if (dndGetStat(pDnode) == DND_STAT_RUNNING) {
S
Shengliang Guan 已提交
467
      // dndSendStatusMsg(pDnode);
S
Shengliang Guan 已提交
468
    }
S
Shengliang Guan 已提交
469 470 471
  }
}

S
Shengliang Guan 已提交
472 473
int32_t dndInitDnode(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
474

S
Shengliang Guan 已提交
475 476 477 478
  pMgmt->dnodeId = 0;
  pMgmt->rebootTime = taosGetTimestampSec();
  pMgmt->dropped = 0;
  pMgmt->clusterId = 0;
S
Shengliang Guan 已提交
479 480

  char path[PATH_MAX];
S
Shengliang Guan 已提交
481 482 483
  snprintf(path, PATH_MAX, "%s/dnode.json", pDnode->dir.dnode);
  pMgmt->file = strdup(path);
  if (pMgmt->file == NULL) {
S
Shengliang Guan 已提交
484 485 486 487
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

488
  pMgmt->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
S
Shengliang Guan 已提交
489
  if (pMgmt->dnodeHash == NULL) {
S
Shengliang Guan 已提交
490
    dError("failed to init dnode hash");
S
Shengliang Guan 已提交
491 492
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
493 494
  }

S
Shengliang Guan 已提交
495 496
  if (dndReadDnodes(pDnode) != 0) {
    dError("failed to read file:%s since %s", pMgmt->file, terrstr());
S
Shengliang Guan 已提交
497
    return -1;
S
Shengliang Guan 已提交
498 499
  }

S
Shengliang Guan 已提交
500
  taosInitRWLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
501

S
Shengliang Guan 已提交
502 503
  pMgmt->threadId = taosCreateThread(dnodeThreadRoutine, pDnode);
  if (pMgmt->threadId == NULL) {
S
Shengliang Guan 已提交
504 505 506
    dError("failed to init dnode thread");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
507 508
  }

S
Shengliang Guan 已提交
509
  dInfo("dnode-dnode is initialized");
S
Shengliang Guan 已提交
510 511 512
  return 0;
}

S
Shengliang Guan 已提交
513 514
void dndCleanupDnode(SDnode *pDnode) {
  SDnodeMgmt *pMgmt = &pDnode->dmgmt;
S
Shengliang Guan 已提交
515

S
Shengliang Guan 已提交
516 517 518
  if (pMgmt->threadId != NULL) {
    taosDestoryThread(pMgmt->threadId);
    pMgmt->threadId = NULL;
S
Shengliang Guan 已提交
519 520
  }

S
Shengliang Guan 已提交
521
  taosWLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
522

S
Shengliang Guan 已提交
523 524 525
  if (pMgmt->dnodeEps != NULL) {
    free(pMgmt->dnodeEps);
    pMgmt->dnodeEps = NULL;
S
Shengliang Guan 已提交
526
  }
S
Shengliang Guan 已提交
527

S
Shengliang Guan 已提交
528 529 530
  if (pMgmt->dnodeHash != NULL) {
    taosHashCleanup(pMgmt->dnodeHash);
    pMgmt->dnodeHash = NULL;
S
Shengliang Guan 已提交
531
  }
S
Shengliang Guan 已提交
532

S
Shengliang Guan 已提交
533 534 535
  if (pMgmt->file != NULL) {
    free(pMgmt->file);
    pMgmt->file = NULL;
S
Shengliang Guan 已提交
536 537
  }

S
Shengliang Guan 已提交
538
  taosWUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
539
  dInfo("dnode-dnode is cleaned up");
S
Shengliang Guan 已提交
540 541
}

S
Shengliang Guan 已提交
542
void dndProcessDnodeReq(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
543
  switch (pMsg->msgType) {
S
Shengliang Guan 已提交
544
    case TSDB_MSG_TYPE_NETWORK_TEST:
S
Shengliang Guan 已提交
545
      dndProcessStartupReq(pDnode, pMsg);
S
Shengliang Guan 已提交
546 547
      break;
    case TSDB_MSG_TYPE_CONFIG_DNODE_IN:
S
Shengliang Guan 已提交
548
      dndProcessConfigDnodeReq(pDnode, pMsg);
S
Shengliang Guan 已提交
549 550
      break;
    default:
S
Shengliang Guan 已提交
551 552
      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 已提交
553 554 555
      rpcSendResponse(&rspMsg);
      rpcFreeCont(pMsg->pCont);
  }
S
Shengliang Guan 已提交
556
}
S
Shengliang Guan 已提交
557

S
Shengliang Guan 已提交
558
void dndProcessDnodeRsp(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
559 560
  switch (pMsg->msgType) {
    case TSDB_MSG_TYPE_STATUS_RSP:
S
Shengliang Guan 已提交
561
      dndProcessStatusRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
562 563
      break;
    case TSDB_MSG_TYPE_AUTH_RSP:
S
Shengliang Guan 已提交
564
      dndProcessAuthRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
565 566
      break;
    case TSDB_MSG_TYPE_GRANT_RSP:
S
Shengliang Guan 已提交
567
      dndProcessGrantRsp(pDnode, pMsg, pEpSet);
S
Shengliang Guan 已提交
568 569 570 571 572
      break;
    default:
      dError("RPC %p, dnode rsp:%s not processed", pMsg->handle, taosMsg[pMsg->msgType]);
  }
}