dndQnode.c 10.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
#include "dndQnode.h"
S
Shengliang Guan 已提交
18
#include "dndMgmt.h"
S
Shengliang Guan 已提交
19
#include "dndTransport.h"
S
Shengliang Guan 已提交
20
#include "dndWorker.h"
S
Shengliang Guan 已提交
21

S
Shengliang Guan 已提交
22
static void dndProcessQnodeQueue(SDnode *pDnode, SRpcMsg *pMsg);
S
Shengliang Guan 已提交
23 24 25 26 27 28 29

static SQnode *dndAcquireQnode(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
  SQnode     *pQnode = NULL;
  int32_t     refCount = 0;

  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
30
  if (pMgmt->deployed && !pMgmt->dropped && pMgmt->pQnode != NULL) {
S
Shengliang Guan 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    refCount = atomic_add_fetch_32(&pMgmt->refCount, 1);
    pQnode = pMgmt->pQnode;
  } else {
    terrno = TSDB_CODE_DND_QNODE_NOT_DEPLOYED;
  }
  taosRUnLockLatch(&pMgmt->latch);

  if (pQnode != NULL) {
    dTrace("acquire qnode, refCount:%d", refCount);
  }
  return pQnode;
}

static void dndReleaseQnode(SDnode *pDnode, SQnode *pQnode) {
S
Shengliang Guan 已提交
45
  if (pQnode == NULL) return;
S
Shengliang Guan 已提交
46

S
Shengliang Guan 已提交
47
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
S
Shengliang Guan 已提交
48
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
49
  int32_t refCount = atomic_sub_fetch_32(&pMgmt->refCount, 1);
S
Shengliang Guan 已提交
50
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
51
  dTrace("release qnode, refCount:%d", refCount);
S
Shengliang Guan 已提交
52 53 54 55 56 57
}

static int32_t dndReadQnodeFile(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
  int32_t     code = TSDB_CODE_DND_QNODE_READ_FILE_ERROR;
  int32_t     len = 0;
S
Shengliang Guan 已提交
58
  int32_t     maxLen = 1024;
S
Shengliang Guan 已提交
59 60 61
  char       *content = calloc(1, maxLen + 1);
  cJSON      *root = NULL;

S
Shengliang Guan 已提交
62 63 64
  char file[PATH_MAX + 20];
  snprintf(file, PATH_MAX + 20, "%s/qnode.json", pDnode->dir.dnode);

65 66 67
  // FILE *fp = fopen(file, "r");
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
68
    dDebug("file %s not exist", file);
S
Shengliang Guan 已提交
69
    code = 0;
S
Shengliang Guan 已提交
70
    goto PRASE_QNODE_OVER;
S
Shengliang Guan 已提交
71 72
  }

73
  len = (int32_t)taosReadFile(pFile, content, maxLen);
S
Shengliang Guan 已提交
74
  if (len <= 0) {
S
Shengliang Guan 已提交
75 76
    dError("failed to read %s since content is null", file);
    goto PRASE_QNODE_OVER;
S
Shengliang Guan 已提交
77 78 79 80 81
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
S
Shengliang Guan 已提交
82 83
    dError("failed to read %s since invalid json format", file);
    goto PRASE_QNODE_OVER;
S
Shengliang Guan 已提交
84 85 86 87
  }

  cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
  if (!deployed || deployed->type != cJSON_Number) {
S
Shengliang Guan 已提交
88 89
    dError("failed to read %s since deployed not found", file);
    goto PRASE_QNODE_OVER;
S
Shengliang Guan 已提交
90 91 92 93 94
  }
  pMgmt->deployed = deployed->valueint;

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_Number) {
S
Shengliang Guan 已提交
95 96
    dError("failed to read %s since dropped not found", file);
    goto PRASE_QNODE_OVER;
S
Shengliang Guan 已提交
97 98 99 100
  }
  pMgmt->dropped = dropped->valueint;

  code = 0;
S
Shengliang Guan 已提交
101
  dDebug("succcessed to read file %s, deployed:%d dropped:%d", file, pMgmt->deployed, pMgmt->dropped);
S
Shengliang Guan 已提交
102

S
Shengliang Guan 已提交
103
PRASE_QNODE_OVER:
S
Shengliang Guan 已提交
104 105
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
106
  if (pFile != NULL) taosCloseFile(&pFile);
S
Shengliang Guan 已提交
107 108 109 110 111 112 113 114

  terrno = code;
  return code;
}

static int32_t dndWriteQnodeFile(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;

S
Shengliang Guan 已提交
115 116
  char file[PATH_MAX + 20];
  snprintf(file, PATH_MAX + 20, "%s/qnode.json", pDnode->dir.dnode);
S
Shengliang Guan 已提交
117

118 119 120
  // FILE *fp = fopen(file, "w");
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
121 122 123 124 125 126
    terrno = TSDB_CODE_DND_QNODE_WRITE_FILE_ERROR;
    dError("failed to write %s since %s", file, terrstr());
    return -1;
  }

  int32_t len = 0;
S
Shengliang Guan 已提交
127
  int32_t maxLen = 1024;
S
Shengliang Guan 已提交
128 129 130 131 132 133 134
  char   *content = calloc(1, maxLen + 1);

  len += snprintf(content + len, maxLen - len, "{\n");
  len += snprintf(content + len, maxLen - len, "  \"deployed\": %d,\n", pMgmt->deployed);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": %d\n", pMgmt->dropped);
  len += snprintf(content + len, maxLen - len, "}\n");

135 136 137
  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
S
Shengliang Guan 已提交
138 139
  free(content);

S
Shengliang Guan 已提交
140 141 142 143
  char realfile[PATH_MAX + 20];
  snprintf(realfile, PATH_MAX + 20, "%s/qnode.json", pDnode->dir.dnode);

  if (taosRenameFile(file, realfile) != 0) {
S
Shengliang Guan 已提交
144
    terrno = TSDB_CODE_DND_QNODE_WRITE_FILE_ERROR;
S
Shengliang Guan 已提交
145
    dError("failed to rename %s since %s", file, terrstr());
S
Shengliang Guan 已提交
146 147 148
    return -1;
  }

S
Shengliang Guan 已提交
149
  dInfo("successed to write %s, deployed:%d dropped:%d", realfile, pMgmt->deployed, pMgmt->dropped);
S
Shengliang Guan 已提交
150 151 152 153
  return 0;
}

static int32_t dndStartQnodeWorker(SDnode *pDnode) {
S
Shengliang Guan 已提交
154
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
S
Shengliang Guan 已提交
155
  if (dndInitWorker(pDnode, &pMgmt->queryWorker, DND_WORKER_SINGLE, "qnode-query", 0, 1, dndProcessQnodeQueue) != 0) {
S
Shengliang Guan 已提交
156 157 158 159
    dError("failed to start qnode query worker since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
160
  if (dndInitWorker(pDnode, &pMgmt->fetchWorker, DND_WORKER_SINGLE, "qnode-fetch", 0, 1, dndProcessQnodeQueue) != 0) {
S
Shengliang Guan 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    dError("failed to start qnode fetch worker since %s", terrstr());
    return -1;
  }

  return 0;
}

static void dndStopQnodeWorker(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;

  taosWLockLatch(&pMgmt->latch);
  pMgmt->deployed = 0;
  taosWUnLockLatch(&pMgmt->latch);

S
Shengliang Guan 已提交
175
  while (pMgmt->refCount > 0) {
S
Shengliang Guan 已提交
176 177
    taosMsleep(10);
  }
S
Shengliang Guan 已提交
178

S
Shengliang Guan 已提交
179 180
  dndCleanupWorker(&pMgmt->queryWorker);
  dndCleanupWorker(&pMgmt->fetchWorker);
S
Shengliang Guan 已提交
181 182 183 184
}

static void dndBuildQnodeOption(SDnode *pDnode, SQnodeOpt *pOption) {
  pOption->pDnode = pDnode;
S
Shengliang Guan 已提交
185 186 187
  pOption->sendReqToDnodeFp = dndSendReqToDnode;
  pOption->sendReqToMnodeFp = dndSendReqToMnode;
  pOption->sendRedirectRspFp = dndSendRedirectRsp;
S
Shengliang Guan 已提交
188 189
  pOption->dnodeId = dndGetDnodeId(pDnode);
  pOption->clusterId = dndGetClusterId(pDnode);
S
config  
Shengliang Guan 已提交
190
  pOption->sver = tsVersion;
S
Shengliang Guan 已提交
191 192 193 194
}

static int32_t dndOpenQnode(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
S
Shengliang Guan 已提交
195 196 197 198 199 200 201 202 203 204

  SQnode *pQnode = dndAcquireQnode(pDnode);
  if (pQnode != NULL) {
    dndReleaseQnode(pDnode, pQnode);
    terrno = TSDB_CODE_DND_QNODE_ALREADY_DEPLOYED;
    dError("failed to create qnode since %s", terrstr());
    return -1;
  }

  SQnodeOpt option = {0};
S
Shengliang Guan 已提交
205 206
  dndBuildQnodeOption(pDnode, &option);

S
Shengliang Guan 已提交
207
  pQnode = qndOpen(&option);
S
Shengliang Guan 已提交
208 209 210 211 212
  if (pQnode == NULL) {
    dError("failed to open qnode since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
213 214
  if (dndStartQnodeWorker(pDnode) != 0) {
    dError("failed to start qnode worker since %s", terrstr());
S
Shengliang Guan 已提交
215 216 217 218
    qndClose(pQnode);
    return -1;
  }

S
Shengliang Guan 已提交
219
  pMgmt->deployed = 1;
S
Shengliang Guan 已提交
220
  if (dndWriteQnodeFile(pDnode) != 0) {
S
Shengliang Guan 已提交
221
    pMgmt->deployed = 0;
S
Shengliang Guan 已提交
222
    dError("failed to write qnode file since %s", terrstr());
S
Shengliang Guan 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    dndStopQnodeWorker(pDnode);
    qndClose(pQnode);
    return -1;
  }

  taosWLockLatch(&pMgmt->latch);
  pMgmt->pQnode = pQnode;
  taosWUnLockLatch(&pMgmt->latch);

  dInfo("qnode open successfully");
  return 0;
}

static int32_t dndDropQnode(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;

  SQnode *pQnode = dndAcquireQnode(pDnode);
  if (pQnode == NULL) {
    dError("failed to drop qnode since %s", terrstr());
    return -1;
  }

  taosRLockLatch(&pMgmt->latch);
  pMgmt->dropped = 1;
  taosRUnLockLatch(&pMgmt->latch);

  if (dndWriteQnodeFile(pDnode) != 0) {
    taosRLockLatch(&pMgmt->latch);
    pMgmt->dropped = 0;
    taosRUnLockLatch(&pMgmt->latch);

    dndReleaseQnode(pDnode, pQnode);
    dError("failed to drop qnode since %s", terrstr());
    return -1;
  }

  dndReleaseQnode(pDnode, pQnode);
  dndStopQnodeWorker(pDnode);
S
Shengliang Guan 已提交
261 262
  pMgmt->deployed = 0;
  dndWriteQnodeFile(pDnode);
S
Shengliang Guan 已提交
263 264 265 266 267 268
  qndClose(pQnode);
  pMgmt->pQnode = NULL;

  return 0;
}

S
Shengliang Guan 已提交
269 270 271 272 273 274
int32_t dndProcessCreateQnodeReq(SDnode *pDnode, SRpcMsg *pReq) {
  SDCreateQnodeReq createReq = {0};
  if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
275

S
Shengliang Guan 已提交
276
  if (createReq.dnodeId != dndGetDnodeId(pDnode)) {
S
Shengliang Guan 已提交
277
    terrno = TSDB_CODE_DND_QNODE_INVALID_OPTION;
S
Shengliang Guan 已提交
278
    dError("failed to create qnode since %s", terrstr());
S
Shengliang Guan 已提交
279 280 281 282 283 284
    return -1;
  } else {
    return dndOpenQnode(pDnode);
  }
}

S
Shengliang Guan 已提交
285 286 287 288 289 290
int32_t dndProcessDropQnodeReq(SDnode *pDnode, SRpcMsg *pReq) {
  SDDropQnodeReq dropReq = {0};
  if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
291

S
Shengliang Guan 已提交
292
  if (dropReq.dnodeId != dndGetDnodeId(pDnode)) {
S
Shengliang Guan 已提交
293
    terrno = TSDB_CODE_DND_QNODE_INVALID_OPTION;
S
Shengliang Guan 已提交
294
    dError("failed to drop qnode since %s", terrstr());
S
Shengliang Guan 已提交
295 296 297 298 299 300 301 302 303
    return -1;
  } else {
    return dndDropQnode(pDnode);
  }
}

static void dndProcessQnodeQueue(SDnode *pDnode, SRpcMsg *pMsg) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
  SRpcMsg    *pRsp = NULL;
S
Shengliang Guan 已提交
304
  int32_t     code = TSDB_CODE_DND_QNODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
305 306

  SQnode *pQnode = dndAcquireQnode(pDnode);
S
Shengliang Guan 已提交
307 308
  if (pQnode != NULL) {
    code = qndProcessMsg(pQnode, pMsg, &pRsp);
S
Shengliang Guan 已提交
309
  }
S
Shengliang Guan 已提交
310
  dndReleaseQnode(pDnode, pQnode);
S
Shengliang Guan 已提交
311

312 313 314 315 316 317 318 319 320 321
  if (pMsg->msgType & 1u) {
    if (pRsp != NULL) {
      pRsp->ahandle = pMsg->ahandle;
      rpcSendResponse(pRsp);
      free(pRsp);
    } else {
      if (code != 0) code = terrno;
      SRpcMsg rpcRsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rpcRsp);
    }
S
Shengliang Guan 已提交
322 323 324 325 326 327
  }

  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
}

S
Shengliang Guan 已提交
328 329
static void dndWriteQnodeMsgToWorker(SDnode *pDnode, SDnodeWorker *pWorker, SRpcMsg *pMsg) {
  int32_t code = TSDB_CODE_DND_QNODE_NOT_DEPLOYED;
S
Shengliang Guan 已提交
330

S
Shengliang Guan 已提交
331 332 333
  SQnode *pQnode = dndAcquireQnode(pDnode);
  if (pQnode != NULL) {
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
S
Shengliang Guan 已提交
334
  }
S
Shengliang Guan 已提交
335
  dndReleaseQnode(pDnode, pQnode);
S
Shengliang Guan 已提交
336 337

  if (code != 0) {
S
Shengliang Guan 已提交
338 339
    if (pMsg->msgType & 1u) {
      SRpcMsg rsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
S
Shengliang Guan 已提交
340 341
      rpcSendResponse(&rsp);
    }
S
Shengliang Guan 已提交
342
    rpcFreeCont(pMsg->pCont);
S
Shengliang Guan 已提交
343 344 345 346
  }
}

void dndProcessQnodeQueryMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
347
  dndWriteQnodeMsgToWorker(pDnode, &pDnode->qmgmt.queryWorker, pMsg);
S
Shengliang Guan 已提交
348 349 350
}

void dndProcessQnodeFetchMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
351
  dndWriteQnodeMsgToWorker(pDnode, &pDnode->qmgmt.queryWorker, pMsg);
S
Shengliang Guan 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
}

int32_t dndInitQnode(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
  taosInitRWLatch(&pMgmt->latch);

  if (dndReadQnodeFile(pDnode) != 0) {
    return -1;
  }

  if (pMgmt->dropped) return 0;
  if (!pMgmt->deployed) return 0;

  return dndOpenQnode(pDnode);
}

void dndCleanupQnode(SDnode *pDnode) {
  SQnodeMgmt *pMgmt = &pDnode->qmgmt;
S
Shengliang Guan 已提交
370 371 372 373 374
  if (pMgmt->pQnode) {
    dndStopQnodeWorker(pDnode);
    qndClose(pMgmt->pQnode);
    pMgmt->pQnode = NULL;
  }
S
Shengliang Guan 已提交
375
}