dndBnode.c 10.9 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 "dndBnode.h"
S
Shengliang Guan 已提交
18
#include "dndMgmt.h"
S
Shengliang Guan 已提交
19 20 21
#include "dndTransport.h"
#include "dndWorker.h"

S
shm  
Shengliang Guan 已提交
22
#if 0
23
static void dndProcessBnodeQueue(SDnode *pDnode, STaosQall *qall, int32_t numOfMsgs);
S
Shengliang Guan 已提交
24 25 26 27 28 29 30

static SBnode *dndAcquireBnode(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;
  SBnode     *pBnode = NULL;
  int32_t     refCount = 0;

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

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

static void dndReleaseBnode(SDnode *pDnode, SBnode *pBnode) {
S
Shengliang Guan 已提交
46
  if (pBnode == NULL) return;
S
Shengliang Guan 已提交
47

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

static int32_t dndReadBnodeFile(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;
  int32_t     code = TSDB_CODE_DND_BNODE_READ_FILE_ERROR;
  int32_t     len = 0;
S
Shengliang Guan 已提交
59
  int32_t     maxLen = 1024;
S
Shengliang Guan 已提交
60 61 62 63 64 65
  char       *content = calloc(1, maxLen + 1);
  cJSON      *root = NULL;

  char file[PATH_MAX + 20];
  snprintf(file, PATH_MAX + 20, "%s/bnode.json", pDnode->dir.dnode);

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

74
  len = (int32_t)taosReadFile(pFile, content, maxLen);
S
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
    goto PRASE_BNODE_OVER;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
    goto PRASE_BNODE_OVER;
  }

  cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
  if (!deployed || deployed->type != cJSON_Number) {
    dError("failed to read %s since deployed not found", file);
    goto PRASE_BNODE_OVER;
  }
  pMgmt->deployed = deployed->valueint;

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_Number) {
    dError("failed to read %s since dropped not found", file);
    goto PRASE_BNODE_OVER;
  }
  pMgmt->dropped = dropped->valueint;

  code = 0;
  dDebug("succcessed to read file %s, deployed:%d dropped:%d", file, pMgmt->deployed, pMgmt->dropped);

PRASE_BNODE_OVER:
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
107
  if (pFile != NULL) taosCloseFile(&pFile);
S
Shengliang Guan 已提交
108 109 110 111 112 113 114 115 116 117 118

  terrno = code;
  return code;
}

static int32_t dndWriteBnodeFile(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;

  char file[PATH_MAX + 20];
  snprintf(file, PATH_MAX + 20, "%s/bnode.json", pDnode->dir.dnode);

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

  int32_t len = 0;
S
Shengliang Guan 已提交
128
  int32_t maxLen = 1024;
S
Shengliang Guan 已提交
129 130 131 132 133 134 135
  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");

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

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

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

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

static int32_t dndStartBnodeWorker(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;
S
Shengliang Guan 已提交
156
  if (dndInitWorker(pDnode, &pMgmt->writeWorker, DND_WORKER_MULTI, "bnode-write", 0, 1, dndProcessBnodeQueue) != 0) {
S
Shengliang Guan 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
    dError("failed to start bnode write worker since %s", terrstr());
    return -1;
  }

  return 0;
}

static void dndStopBnodeWorker(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;

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

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

  dndCleanupWorker(&pMgmt->writeWorker);
}

static void dndBuildBnodeOption(SDnode *pDnode, SBnodeOpt *pOption) {
  pOption->pDnode = pDnode;
S
Shengliang Guan 已提交
180 181 182
  pOption->sendReqToDnodeFp = dndSendReqToDnode;
  pOption->sendReqToMnodeFp = dndSendReqToMnode;
  pOption->sendRedirectRspFp = dndSendRedirectRsp;
S
Shengliang Guan 已提交
183 184
  pOption->dnodeId = dndGetDnodeId(pDnode);
  pOption->clusterId = dndGetClusterId(pDnode);
S
config  
Shengliang Guan 已提交
185
  pOption->sver = tsVersion;
S
Shengliang Guan 已提交
186 187 188 189
}

static int32_t dndOpenBnode(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;
S
Shengliang Guan 已提交
190 191 192 193 194 195 196 197 198
  SBnode     *pBnode = dndAcquireBnode(pDnode);
  if (pBnode != NULL) {
    dndReleaseBnode(pDnode, pBnode);
    terrno = TSDB_CODE_DND_BNODE_ALREADY_DEPLOYED;
    dError("failed to create bnode since %s", terrstr());
    return -1;
  }

  SBnodeOpt option = {0};
S
Shengliang Guan 已提交
199 200
  dndBuildBnodeOption(pDnode, &option);

S
Shengliang Guan 已提交
201
  pBnode = bndOpen(pDnode->dir.bnode, &option);
S
Shengliang Guan 已提交
202 203 204 205 206 207 208 209 210 211 212
  if (pBnode == NULL) {
    dError("failed to open bnode since %s", terrstr());
    return -1;
  }

  if (dndStartBnodeWorker(pDnode) != 0) {
    dError("failed to start bnode worker since %s", terrstr());
    bndClose(pBnode);
    return -1;
  }

S
Shengliang Guan 已提交
213
  pMgmt->deployed = 1;
S
Shengliang Guan 已提交
214
  if (dndWriteBnodeFile(pDnode) != 0) {
S
Shengliang Guan 已提交
215
    pMgmt->deployed = 0;
S
Shengliang Guan 已提交
216 217 218 219 220 221 222 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
    dError("failed to write bnode file since %s", terrstr());
    dndStopBnodeWorker(pDnode);
    bndClose(pBnode);
    return -1;
  }

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

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

static int32_t dndDropBnode(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;

  SBnode *pBnode = dndAcquireBnode(pDnode);
  if (pBnode == NULL) {
    dError("failed to drop bnode since %s", terrstr());
    return -1;
  }

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

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

    dndReleaseBnode(pDnode, pBnode);
    dError("failed to drop bnode since %s", terrstr());
    return -1;
  }

  dndReleaseBnode(pDnode, pBnode);
  dndStopBnodeWorker(pDnode);
S
Shengliang Guan 已提交
255 256
  pMgmt->deployed = 0;
  dndWriteBnodeFile(pDnode);
S
Shengliang Guan 已提交
257 258 259 260 261 262 263
  bndClose(pBnode);
  pMgmt->pBnode = NULL;
  bndDestroy(pDnode->dir.bnode);

  return 0;
}

S
Shengliang Guan 已提交
264 265 266 267 268 269
int32_t dndProcessCreateBnodeReq(SDnode *pDnode, SRpcMsg *pReq) {
  SDCreateBnodeReq createReq = {0};
  if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271
  if (createReq.dnodeId != dndGetDnodeId(pDnode)) {
S
Shengliang Guan 已提交
272
    terrno = TSDB_CODE_DND_BNODE_INVALID_OPTION;
S
Shengliang Guan 已提交
273
    dError("failed to create bnode since %s", terrstr());
S
Shengliang Guan 已提交
274 275 276 277 278 279
    return -1;
  } else {
    return dndOpenBnode(pDnode);
  }
}

S
Shengliang Guan 已提交
280 281 282 283 284 285
int32_t dndProcessDropBnodeReq(SDnode *pDnode, SRpcMsg *pReq) {
  SDDropBnodeReq dropReq = {0};
  if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
286

S
Shengliang Guan 已提交
287
  if (dropReq.dnodeId != dndGetDnodeId(pDnode)) {
S
Shengliang Guan 已提交
288
    terrno = TSDB_CODE_DND_BNODE_INVALID_OPTION;
S
Shengliang Guan 已提交
289
    dError("failed to drop bnode since %s", terrstr());
S
Shengliang Guan 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302
    return -1;
  } else {
    return dndDropBnode(pDnode);
  }
}

static void dndSendBnodeErrorRsp(SRpcMsg *pMsg, int32_t code) {
  SRpcMsg rpcRsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
  rpcSendResponse(&rpcRsp);
  rpcFreeCont(pMsg->pCont);
  taosFreeQitem(pMsg);
}

303
static void dndSendBnodeErrorRsps(STaosQall *qall, int32_t numOfMsgs, int32_t code) {
S
Shengliang Guan 已提交
304 305 306 307 308 309 310
  for (int32_t i = 0; i < numOfMsgs; ++i) {
    SRpcMsg *pMsg = NULL;
    taosGetQitem(qall, (void **)&pMsg);
    dndSendBnodeErrorRsp(pMsg, code);
  }
}

311
static void dndProcessBnodeQueue(SDnode *pDnode, STaosQall *qall, int32_t numOfMsgs) {
S
Shengliang Guan 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  SBnode *pBnode = dndAcquireBnode(pDnode);
  if (pBnode == NULL) {
    dndSendBnodeErrorRsps(qall, numOfMsgs, TSDB_CODE_OUT_OF_MEMORY);
    return;
  }

  SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SRpcMsg *));
  if (pArray == NULL) {
    dndReleaseBnode(pDnode, pBnode);
    dndSendBnodeErrorRsps(qall, numOfMsgs, TSDB_CODE_OUT_OF_MEMORY);
    return;
  }

  for (int32_t i = 0; i < numOfMsgs; ++i) {
    SRpcMsg *pMsg = NULL;
    taosGetQitem(qall, (void **)&pMsg);
    void *ptr = taosArrayPush(pArray, &pMsg);
    if (ptr == NULL) {
      dndSendBnodeErrorRsp(pMsg, TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  bndProcessWMsgs(pBnode, pArray);

  for (size_t i = 0; i < numOfMsgs; i++) {
    SRpcMsg *pMsg = *(SRpcMsg **)taosArrayGet(pArray, i);
    rpcFreeCont(pMsg->pCont);
    taosFreeQitem(pMsg);
  }
  taosArrayDestroy(pArray);
  dndReleaseBnode(pDnode, pBnode);
}

static void dndWriteBnodeMsgToWorker(SDnode *pDnode, SDnodeWorker *pWorker, SRpcMsg *pMsg) {
  int32_t code = TSDB_CODE_DND_BNODE_NOT_DEPLOYED;

  SBnode *pBnode = dndAcquireBnode(pDnode);
  if (pBnode != NULL) {
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }
  dndReleaseBnode(pDnode, pBnode);

  if (code != 0) {
    if (pMsg->msgType & 1u) {
      SRpcMsg rsp = {.handle = pMsg->handle, .ahandle = pMsg->ahandle, .code = code};
      rpcSendResponse(&rsp);
    }
    rpcFreeCont(pMsg->pCont);
  }
}

void dndProcessBnodeWriteMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dndWriteBnodeMsgToWorker(pDnode, &pDnode->bmgmt.writeWorker, pMsg);
}

int32_t dndInitBnode(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;
  taosInitRWLatch(&pMgmt->latch);

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

S
Shengliang Guan 已提交
375 376 377 378 379 380
  if (pMgmt->dropped) {
    dInfo("bnode has been deployed and needs to be deleted");
    bndDestroy(pDnode->dir.bnode);
    return 0;
  }

S
Shengliang Guan 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393
  if (!pMgmt->deployed) return 0;

  return dndOpenBnode(pDnode);
}

void dndCleanupBnode(SDnode *pDnode) {
  SBnodeMgmt *pMgmt = &pDnode->bmgmt;
  if (pMgmt->pBnode) {
    dndStopBnodeWorker(pDnode);
    bndClose(pMgmt->pBnode);
    pMgmt->pBnode = NULL;
  }
}
S
shm  
Shengliang Guan 已提交
394 395

#endif