dndMsg.c 4.4 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 "dndInt.h"

S
shm  
Shengliang Guan 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
static int32_t dndBuildMsg(SNodeMsg *pMsg, SRpcMsg *pRpc, SEpSet *pEpSet) {
  SRpcConnInfo connInfo = {0};
  if ((pRpc->msgType & 1U) && rpcGetConnInfo(pRpc->handle, &connInfo) != 0) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    dError("failed to build msg since %s, app:%p RPC:%p", terrstr(), pRpc->ahandle, pRpc->handle);
    return -1;
  }

  memcpy(pMsg->user, connInfo.user, TSDB_USER_LEN);
  pMsg->rpcMsg = *pRpc;

  return 0;
}

void dndProcessRpcMsg(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, SEpSet *pEpSet) {
  if (pEpSet && pEpSet->numOfEps > 0 && pRpc->msgType == TDMT_MND_STATUS_RSP) {
S
shm  
Shengliang Guan 已提交
35
    dmUpdateMnodeEpSet(dndAcquireWrapper(pWrapper->pDnode, DNODE)->pMgmt, pEpSet);
S
shm  
Shengliang Guan 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  }

  int32_t   code = -1;
  SNodeMsg *pMsg = NULL;

  NodeMsgFp msgFp = pWrapper->msgFps[TMSG_INDEX(pRpc->msgType)];
  if (msgFp == NULL) {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
    goto _OVER;
  }

  pMsg = taosAllocateQitem(sizeof(SNodeMsg));
  if (pMsg == NULL) {
    goto _OVER;
  }

  if (dndBuildMsg(pMsg, pRpc, pEpSet) != 0) {
    goto _OVER;
  }

  dTrace("msg:%p, is created, app:%p user:%s", pMsg, pRpc->ahandle, pMsg->user);

  if (pWrapper->procType == PROC_SINGLE) {
    code = (*msgFp)(pWrapper->pMgmt, pMsg);
  } else if (pWrapper->procType == PROC_PARENT) {
    code = taosProcPutToChildQueue(pWrapper->pProc, pMsg, sizeof(SNodeMsg), pRpc->pCont, pRpc->contLen);
  } else {
    terrno = TSDB_CODE_MEMORY_CORRUPTED;
    dError("msg:%p, won't be processed for it is child process", pMsg);
  }

_OVER:

  if (code == 0) {
    if (pWrapper->procType == PROC_PARENT) {
      dTrace("msg:%p, is freed", pMsg);
      taosFreeQitem(pMsg);
      rpcFreeCont(pRpc->pCont);
    }
  } else {
    dError("msg:%p, failed to process since %s", pMsg, terrstr());
    bool isReq = (pRpc->msgType & 1U);
    if (isReq) {
      SRpcMsg rsp = {.handle = pRpc->handle, .ahandle = pRpc->ahandle, .code = terrno};
      dndSendRsp(pWrapper, &rsp);
    }
    dTrace("msg:%p, is freed", pMsg);
    taosFreeQitem(pMsg);
    rpcFreeCont(pRpc->pCont);
  }
}

S
shm  
Shengliang Guan 已提交
88 89 90 91
static SMgmtWrapper *dndGetWrapperFromMsg(SDnode *pDnode, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = NULL;
  switch (pMsg->rpcMsg.msgType) {
    case TDMT_DND_CREATE_MNODE:
S
shm  
Shengliang Guan 已提交
92
      return dndAcquireWrapper(pDnode, MNODE);
S
shm  
Shengliang Guan 已提交
93
    case TDMT_DND_CREATE_QNODE:
S
shm  
Shengliang Guan 已提交
94
      return dndAcquireWrapper(pDnode, QNODE);
S
shm  
Shengliang Guan 已提交
95
    case TDMT_DND_CREATE_SNODE:
S
shm  
Shengliang Guan 已提交
96
      return dndAcquireWrapper(pDnode, SNODE);
S
shm  
Shengliang Guan 已提交
97
    case TDMT_DND_CREATE_BNODE:
S
shm  
Shengliang Guan 已提交
98
      return dndAcquireWrapper(pDnode, BNODE);
S
shm  
Shengliang Guan 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    default:
      return NULL;
  }
}

int32_t dndProcessCreateNodeMsg(SDnode *pDnode, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = dndGetWrapperFromMsg(pDnode, pMsg);
  if (pWrapper->procType == PROC_SINGLE) {
    switch (pMsg->rpcMsg.msgType) {
      case TDMT_DND_CREATE_MNODE:
        return mmProcessCreateReq(pWrapper->pMgmt, pMsg);
      case TDMT_DND_CREATE_QNODE:
        return qmProcessCreateReq(pWrapper->pMgmt, pMsg);
      case TDMT_DND_CREATE_SNODE:
        return smProcessCreateReq(pWrapper->pMgmt, pMsg);
      case TDMT_DND_CREATE_BNODE:
        return bmProcessCreateReq(pWrapper->pMgmt, pMsg);
      default:
        terrno = TSDB_CODE_MSG_NOT_PROCESSED;
        return -1;
    }
  } else {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
    return -1;
  }
}

int32_t dndProcessDropNodeMsg(SDnode *pDnode, SNodeMsg *pMsg) {
  SMgmtWrapper *pWrapper = dndGetWrapperFromMsg(pDnode, pMsg);
  switch (pMsg->rpcMsg.msgType) {
    case TDMT_DND_DROP_MNODE:
      return mmProcessDropReq(pWrapper->pMgmt, pMsg);
    case TDMT_DND_DROP_QNODE:
      return qmProcessDropReq(pWrapper->pMgmt, pMsg);
    case TDMT_DND_DROP_SNODE:
      return smProcessDropReq(pWrapper->pMgmt, pMsg);
    case TDMT_DND_DROP_BNODE:
      return bmProcessDropReq(pWrapper->pMgmt, pMsg);
    default:
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
      return -1;
  }
}