smMgmt.c 13.3 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
shm  
Shengliang Guan 已提交
17
// #include "dndSnode.h"
S
shm  
Shengliang Guan 已提交
18
// #include "dm.h"
S
shm  
Shengliang Guan 已提交
19 20
// #include "dndTransport.h"
// #include "dndWorker.h"
S
Shengliang Guan 已提交
21

S
shm  
Shengliang Guan 已提交
22
#if 0
S
Shengliang Guan 已提交
23

L
Liu Jicong 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
typedef struct {
  int32_t     vgId;
  int32_t     refCount;
  int32_t     snVersion;
  int8_t      dropped;
  char       *path;
  SSnode     *pImpl;
  STaosQueue *pSharedQ;
  STaosQueue *pUniqueQ;
} SSnodeObj;

static void dndProcessSnodeSharedQueue(SDnode *pDnode, SRpcMsg *pMsg);

static void dndProcessSnodeUniqueQueue(SDnode *pDnode, STaosQall *qall, int32_t numOfMsgs);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44

static SSnode *dndAcquireSnode(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
  SSnode     *pSnode = NULL;
  int32_t     refCount = 0;

  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
45
  if (pMgmt->deployed && !pMgmt->dropped && pMgmt->pSnode != NULL) {
S
Shengliang Guan 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    refCount = atomic_add_fetch_32(&pMgmt->refCount, 1);
    pSnode = pMgmt->pSnode;
  } else {
    terrno = TSDB_CODE_DND_SNODE_NOT_DEPLOYED;
  }
  taosRUnLockLatch(&pMgmt->latch);

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

static void dndReleaseSnode(SDnode *pDnode, SSnode *pSnode) {
S
Shengliang Guan 已提交
60
  if (pSnode == NULL) return;
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
S
Shengliang Guan 已提交
63
  taosRLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
64
  int32_t refCount = atomic_sub_fetch_32(&pMgmt->refCount, 1);
S
Shengliang Guan 已提交
65
  taosRUnLockLatch(&pMgmt->latch);
S
Shengliang Guan 已提交
66
  dTrace("release snode, refCount:%d", refCount);
S
Shengliang Guan 已提交
67 68 69 70 71 72
}

static int32_t dndReadSnodeFile(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
  int32_t     code = TSDB_CODE_DND_SNODE_READ_FILE_ERROR;
  int32_t     len = 0;
S
Shengliang Guan 已提交
73
  int32_t     maxLen = 1024;
S
Shengliang Guan 已提交
74 75 76 77 78 79
  char       *content = calloc(1, maxLen + 1);
  cJSON      *root = NULL;

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

80 81 82
  // FILE *fp = fopen(file, "r");
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
83 84 85 86 87
    dDebug("file %s not exist", file);
    code = 0;
    goto PRASE_SNODE_OVER;
  }

88
  len = (int32_t)taosReadFile(pFile, content, maxLen);
S
Shengliang Guan 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
    goto PRASE_SNODE_OVER;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
    goto PRASE_SNODE_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_SNODE_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_SNODE_OVER;
  }
  pMgmt->dropped = dropped->valueint;

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

PRASE_SNODE_OVER:
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
121
  if (pFile != NULL) taosCloseFile(&pFile);
S
Shengliang Guan 已提交
122 123 124 125 126 127 128 129 130 131 132

  terrno = code;
  return code;
}

static int32_t dndWriteSnodeFile(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;

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

133 134 135
  // FILE *fp = fopen(file, "w");
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
S
Shengliang Guan 已提交
136 137 138 139 140 141
    terrno = TSDB_CODE_DND_SNODE_WRITE_FILE_ERROR;
    dError("failed to write %s since %s", file, terrstr());
    return -1;
  }

  int32_t len = 0;
S
Shengliang Guan 已提交
142
  int32_t maxLen = 1024;
S
Shengliang Guan 已提交
143 144 145 146 147 148 149
  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");

150 151 152
  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
S
Shengliang Guan 已提交
153 154
  free(content);

S
Shengliang Guan 已提交
155 156 157 158
  char realfile[PATH_MAX + 20];
  snprintf(realfile, PATH_MAX + 20, "%s/snode.json", pDnode->dir.dnode);

  if (taosRenameFile(file, realfile) != 0) {
S
Shengliang Guan 已提交
159 160 161 162 163
    terrno = TSDB_CODE_DND_SNODE_WRITE_FILE_ERROR;
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
164
  dInfo("successed to write %s, deployed:%d dropped:%d", realfile, pMgmt->deployed, pMgmt->dropped);
S
Shengliang Guan 已提交
165 166 167 168 169
  return 0;
}

static int32_t dndStartSnodeWorker(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
L
Liu Jicong 已提交
170
  pMgmt->uniqueWorkers = taosArrayInit(0, sizeof(void *));
L
Liu Jicong 已提交
171
  for (int32_t i = 0; i < SND_UNIQUE_THREAD_NUM; i++) {
L
Liu Jicong 已提交
172 173 174 175 176
    SDnodeWorker *pUniqueWorker = malloc(sizeof(SDnodeWorker));
    if (pUniqueWorker == NULL) {
      return -1;
    }
    if (dndInitWorker(pDnode, pUniqueWorker, DND_WORKER_MULTI, "snode-unique", 1, 1, dndProcessSnodeSharedQueue) != 0) {
L
Liu Jicong 已提交
177 178 179
      dError("failed to start snode unique worker since %s", terrstr());
      return -1;
    }
L
Liu Jicong 已提交
180
    taosArrayPush(pMgmt->uniqueWorkers, &pUniqueWorker);
L
Liu Jicong 已提交
181
  }
L
Liu Jicong 已提交
182 183
  if (dndInitWorker(pDnode, &pMgmt->sharedWorker, DND_WORKER_SINGLE, "snode-shared", SND_SHARED_THREAD_NUM,
                    SND_SHARED_THREAD_NUM, dndProcessSnodeSharedQueue)) {
L
Liu Jicong 已提交
184
    dError("failed to start snode shared worker since %s", terrstr());
S
Shengliang Guan 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
    return -1;
  }

  return 0;
}

static void dndStopSnodeWorker(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;

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

S
Shengliang Guan 已提交
198
  while (pMgmt->refCount > 0) {
S
Shengliang Guan 已提交
199
    taosMsleep(10);
L
Liu Jicong 已提交
200
  }
S
Shengliang Guan 已提交
201

L
Liu Jicong 已提交
202 203 204 205 206
  for (int32_t i = 0; i < taosArrayGetSize(pMgmt->uniqueWorkers); i++) {
    SDnodeWorker *worker = taosArrayGetP(pMgmt->uniqueWorkers, i);
    dndCleanupWorker(worker);
  }
  taosArrayDestroy(pMgmt->uniqueWorkers);
S
Shengliang Guan 已提交
207 208 209 210
}

static void dndBuildSnodeOption(SDnode *pDnode, SSnodeOpt *pOption) {
  pOption->pDnode = pDnode;
S
shm  
Shengliang Guan 已提交
211
  pOption->sendReqFp = dndSendReqToDnode;
S
shm  
Shengliang Guan 已提交
212
  pOption->sendMnodeReqFp = dndSendReqToMnode;
S
shm  
Shengliang Guan 已提交
213 214 215
  pOption->sendRedirectRspFp = dndSendRedirectRsp;
  pOption->dnodeId = pDnode->dnodeId;
  pOption->clusterId = pDnode->clusterId;
S
config  
Shengliang Guan 已提交
216
  pOption->sver = tsVersion;
S
Shengliang Guan 已提交
217 218 219 220
}

static int32_t dndOpenSnode(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
S
Shengliang Guan 已提交
221 222 223 224 225 226 227 228 229
  SSnode     *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
    dndReleaseSnode(pDnode, pSnode);
    terrno = TSDB_CODE_DND_SNODE_ALREADY_DEPLOYED;
    dError("failed to create snode since %s", terrstr());
    return -1;
  }

  SSnodeOpt option = {0};
S
Shengliang Guan 已提交
230 231
  dndBuildSnodeOption(pDnode, &option);

S
Shengliang Guan 已提交
232
  pSnode = sndOpen(pDnode->dir.snode, &option);
S
Shengliang Guan 已提交
233 234 235 236 237 238 239 240 241 242 243
  if (pSnode == NULL) {
    dError("failed to open snode since %s", terrstr());
    return -1;
  }

  if (dndStartSnodeWorker(pDnode) != 0) {
    dError("failed to start snode worker since %s", terrstr());
    sndClose(pSnode);
    return -1;
  }

S
Shengliang Guan 已提交
244
  pMgmt->deployed = 1;
S
Shengliang Guan 已提交
245
  if (dndWriteSnodeFile(pDnode) != 0) {
S
Shengliang Guan 已提交
246
    pMgmt->deployed = 0;
S
Shengliang Guan 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    dError("failed to write snode file since %s", terrstr());
    dndStopSnodeWorker(pDnode);
    sndClose(pSnode);
    return -1;
  }

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

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

static int32_t dndDropSnode(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode == NULL) {
    dError("failed to drop snode since %s", terrstr());
    return -1;
  }

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

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

    dndReleaseSnode(pDnode, pSnode);
    dError("failed to drop snode since %s", terrstr());
    return -1;
  }

  dndReleaseSnode(pDnode, pSnode);
  dndStopSnodeWorker(pDnode);
S
Shengliang Guan 已提交
286 287
  pMgmt->deployed = 0;
  dndWriteSnodeFile(pDnode);
S
Shengliang Guan 已提交
288 289 290 291 292 293 294
  sndClose(pSnode);
  pMgmt->pSnode = NULL;
  sndDestroy(pDnode->dir.snode);

  return 0;
}

S
shm  
Shengliang Guan 已提交
295
int32_t smProcessCreateReq(SDnode *pDnode, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
296 297 298 299 300
  SDCreateSnodeReq createReq = {0};
  if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
301

S
shm  
Shengliang Guan 已提交
302
  if (createReq.dnodeId != pDnode->dnodeId) {
S
Shengliang Guan 已提交
303
    terrno = TSDB_CODE_DND_SNODE_INVALID_OPTION;
S
Shengliang Guan 已提交
304
    dError("failed to create snode since %s", terrstr());
S
Shengliang Guan 已提交
305 306 307 308 309 310
    return -1;
  } else {
    return dndOpenSnode(pDnode);
  }
}

S
shm  
Shengliang Guan 已提交
311
int32_t smProcessDropReq(SDnode *pDnode, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
312 313 314 315 316
  SDDropSnodeReq dropReq = {0};
  if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
S
Shengliang Guan 已提交
317

S
shm  
Shengliang Guan 已提交
318
  if (dropReq.dnodeId != pDnode->dnodeId) {
S
Shengliang Guan 已提交
319
    terrno = TSDB_CODE_DND_SNODE_INVALID_OPTION;
S
Shengliang Guan 已提交
320
    dError("failed to drop snode since %s", terrstr());
S
Shengliang Guan 已提交
321 322 323 324 325 326
    return -1;
  } else {
    return dndDropSnode(pDnode);
  }
}

L
Liu Jicong 已提交
327
static void dndProcessSnodeUniqueQueue(SDnode *pDnode, STaosQall *qall, int32_t numOfMsgs) {
S
Shengliang Guan 已提交
328 329 330 331 332
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
  int32_t     code = TSDB_CODE_DND_SNODE_NOT_DEPLOYED;

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
L
Liu Jicong 已提交
333 334 335 336 337 338 339 340 341
    for (int32_t i = 0; i < numOfMsgs; i++) {
      SRpcMsg *pMsg = NULL;
      taosGetQitem(qall, (void **)&pMsg);

      sndProcessUMsg(pSnode, pMsg);

      rpcFreeCont(pMsg->pCont);
      taosFreeQitem(pMsg);
    }
S
Shengliang Guan 已提交
342
  }
S
Shengliang Guan 已提交
343
  dndReleaseSnode(pDnode, pSnode);
L
Liu Jicong 已提交
344
}
S
Shengliang Guan 已提交
345

L
Liu Jicong 已提交
346 347 348 349 350 351 352 353 354 355 356
static void dndProcessSnodeSharedQueue(SDnode *pDnode, SRpcMsg *pMsg) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
  int32_t     code = TSDB_CODE_DND_SNODE_NOT_DEPLOYED;

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
    code = sndProcessSMsg(pSnode, pMsg);
  }
  dndReleaseSnode(pDnode, pSnode);

#if 0
357 358 359 360 361 362 363 364 365 366
  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 已提交
367
  }
L
Liu Jicong 已提交
368
#endif
S
Shengliang Guan 已提交
369 370 371 372 373

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

L
Liu Jicong 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
static FORCE_INLINE int32_t dndGetSWIdFromMsg(SRpcMsg *pMsg) {
  SMsgHead *pHead = pMsg->pCont;
  pHead->streamTaskId = htonl(pHead->streamTaskId);
  return pHead->streamTaskId % SND_UNIQUE_THREAD_NUM;
}

static void dndWriteSnodeMsgToWorkerByMsg(SDnode *pDnode, SRpcMsg *pMsg) {
  int32_t code = TSDB_CODE_DND_SNODE_NOT_DEPLOYED;

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
    int32_t       index = dndGetSWIdFromMsg(pMsg);
    SDnodeWorker *pWorker = taosArrayGetP(pDnode->smgmt.uniqueWorkers, index);
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }

  dndReleaseSnode(pDnode, pSnode);

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

static void dndWriteSnodeMsgToMgmtWorker(SDnode *pDnode, SRpcMsg *pMsg) {
L
Liu Jicong 已提交
402 403 404 405
  int32_t code = TSDB_CODE_DND_SNODE_NOT_DEPLOYED;

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
L
Liu Jicong 已提交
406
    SDnodeWorker *pWorker = taosArrayGet(pDnode->smgmt.uniqueWorkers, 0);
L
Liu Jicong 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }
  dndReleaseSnode(pDnode, pSnode);

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

S
Shengliang Guan 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
static void dndWriteSnodeMsgToWorker(SDnode *pDnode, SDnodeWorker *pWorker, SRpcMsg *pMsg) {
  int32_t code = TSDB_CODE_DND_SNODE_NOT_DEPLOYED;

  SSnode *pSnode = dndAcquireSnode(pDnode);
  if (pSnode != NULL) {
    code = dndWriteMsgToWorker(pWorker, pMsg, sizeof(SRpcMsg));
  }
  dndReleaseSnode(pDnode, pSnode);

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

L
Liu Jicong 已提交
438 439 440 441
void dndProcessSnodeMgmtMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dndWriteSnodeMsgToMgmtWorker(pDnode, pMsg);
}

L
Liu Jicong 已提交
442
void dndProcessSnodeUniqueMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
L
Liu Jicong 已提交
443
  dndWriteSnodeMsgToWorkerByMsg(pDnode, pMsg);
L
Liu Jicong 已提交
444 445 446 447
}

void dndProcessSnodeSharedMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) {
  dndWriteSnodeMsgToWorker(pDnode, &pDnode->smgmt.sharedWorker, pMsg);
S
Shengliang Guan 已提交
448 449 450 451 452 453 454 455 456 457
}

int32_t dndInitSnode(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
  taosInitRWLatch(&pMgmt->latch);

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

S
Shengliang Guan 已提交
458 459 460 461 462 463
  if (pMgmt->dropped) {
    dInfo("snode has been deployed and needs to be deleted");
    sndDestroy(pDnode->dir.snode);
    return 0;
  }

S
Shengliang Guan 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476
  if (!pMgmt->deployed) return 0;

  return dndOpenSnode(pDnode);
}

void dndCleanupSnode(SDnode *pDnode) {
  SSnodeMgmt *pMgmt = &pDnode->smgmt;
  if (pMgmt->pSnode) {
    dndStopSnodeWorker(pDnode);
    sndClose(pMgmt->pSnode);
    pMgmt->pSnode = NULL;
  }
}
S
shm  
Shengliang Guan 已提交
477 478

#endif