dndTransport.c 10.1 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 "dndTransport.h"
S
shm  
Shengliang Guan 已提交
18
#include "dmMgmt.h"
S
shm  
Shengliang Guan 已提交
19
#include "mmInt.h"
S
Shengliang Guan 已提交
20

S
shm  
Shengliang Guan 已提交
21 22
#define INTERNAL_USER   "_dnd"
#define INTERNAL_CKEY   "_key"
S
Shengliang 已提交
23
#define INTERNAL_SECRET "_pwd"
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25
static void dndProcessResponse(void *parent, SRpcMsg *pRsp, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
26
  SDnode     *pDnode = parent;
S
Shengliang Guan 已提交
27
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29
  tmsg_t msgType = pRsp->msgType;
S
Shengliang Guan 已提交
30

S
shm  
Shengliang Guan 已提交
31
  if (dndGetStatus(pDnode) == DND_STAT_STOPPED) {
S
Shengliang Guan 已提交
32
    if (pRsp == NULL || pRsp->pCont == NULL) return;
S
Shengliang 已提交
33
    dTrace("RPC %p, rsp:%s ignored since dnode exiting, app:%p", pRsp->handle, TMSG_INFO(msgType), pRsp->ahandle);
S
Shengliang Guan 已提交
34
    rpcFreeCont(pRsp->pCont);
S
Shengliang Guan 已提交
35 36 37
    return;
  }

S
shm  
Shengliang Guan 已提交
38 39 40 41 42
  SMsgHandle *pHandle = &pMgmt->msgHandles[TMSG_INDEX(msgType)];
  if (pHandle->rpcMsgFp != NULL) {
    dTrace("RPC %p, rsp:%s will be processed by %s, code:0x%x app:%p", pRsp->handle, TMSG_INFO(msgType),
           pHandle->pWrapper->name, pRsp->code & 0XFFFF, pRsp->ahandle);
    (*pHandle->rpcMsgFp)(pDnode, pHandle->pWrapper, pRsp, pEpSet);
S
Shengliang Guan 已提交
43
  } else {
S
Shengliang 已提交
44
    dError("RPC %p, rsp:%s not processed, app:%p", pRsp->handle, TMSG_INFO(msgType), pRsp->ahandle);
S
Shengliang Guan 已提交
45
    rpcFreeCont(pRsp->pCont);
S
Shengliang Guan 已提交
46 47 48
  }
}

S
shm  
Shengliang Guan 已提交
49
 int32_t dndInitClient(SDnode *pDnode) {
S
Shengliang Guan 已提交
50
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
51 52 53

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
S
shm  
Shengliang Guan 已提交
54
  rpcInit.label = "CLI";
S
Shengliang Guan 已提交
55 56
  rpcInit.numOfThreads = 1;
  rpcInit.cfp = dndProcessResponse;
S
Shengliang Guan 已提交
57
  rpcInit.sessions = 1024;
S
Shengliang Guan 已提交
58
  rpcInit.connType = TAOS_CONN_CLIENT;
S
Shengliang Guan 已提交
59
  rpcInit.idleTime = tsShellActivityTimer * 1000;
S
Shengliang Guan 已提交
60 61
  rpcInit.user = INTERNAL_USER;
  rpcInit.ckey = INTERNAL_CKEY;
S
Shengliang Guan 已提交
62
  rpcInit.spi = 1;
63
  rpcInit.parent = pDnode;
S
Shengliang Guan 已提交
64

S
Shengliang Guan 已提交
65 66
  char pass[TSDB_PASSWORD_LEN + 1] = {0};
  taosEncryptPass_c((uint8_t *)(INTERNAL_SECRET), strlen(INTERNAL_SECRET), pass);
S
Shengliang Guan 已提交
67 68
  rpcInit.secret = pass;

S
Shengliang Guan 已提交
69 70
  pMgmt->clientRpc = rpcOpen(&rpcInit);
  if (pMgmt->clientRpc == NULL) {
S
Shengliang 已提交
71
    dError("failed to init dnode rpc client");
S
Shengliang Guan 已提交
72 73 74
    return -1;
  }

75
  dDebug("dnode rpc client is initialized");
S
Shengliang Guan 已提交
76 77 78
  return 0;
}

S
shm  
Shengliang Guan 已提交
79
void dndCleanupClient(SDnode *pDnode) {
S
Shengliang Guan 已提交
80
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
81 82 83
  if (pMgmt->clientRpc) {
    rpcClose(pMgmt->clientRpc);
    pMgmt->clientRpc = NULL;
84
    dDebug("dnode rpc client is closed");
S
Shengliang Guan 已提交
85 86 87
  }
}

S
Shengliang Guan 已提交
88
static void dndProcessRequest(void *param, SRpcMsg *pReq, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
89
  SDnode     *pDnode = param;
S
Shengliang Guan 已提交
90
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
91

S
Shengliang Guan 已提交
92
  tmsg_t msgType = pReq->msgType;
H
Hongze Cheng 已提交
93
  if (msgType == TDMT_DND_NETWORK_TEST) {
S
Shengliang 已提交
94
    dTrace("RPC %p, network test req will be processed, app:%p", pReq->handle, pReq->ahandle);
S
Shengliang Guan 已提交
95
    dndProcessStartupReq(pDnode, pReq);
S
Shengliang Guan 已提交
96 97 98
    return;
  }

S
shm  
Shengliang Guan 已提交
99
  if (dndGetStatus(pDnode) == DND_STAT_STOPPED) {
S
Shengliang 已提交
100 101
    dError("RPC %p, req:%s ignored since dnode exiting, app:%p", pReq->handle, TMSG_INFO(msgType), pReq->ahandle);
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_DND_OFFLINE, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
102
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
103
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
104
    return;
S
shm  
Shengliang Guan 已提交
105
  } else if (dndGetStatus(pDnode) != DND_STAT_RUNNING) {
S
Shengliang 已提交
106 107
    dError("RPC %p, req:%s ignored since dnode not running, app:%p", pReq->handle, TMSG_INFO(msgType), pReq->ahandle);
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_APP_NOT_READY, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
108
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
109
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
110 111 112
    return;
  }

S
Shengliang Guan 已提交
113
  if (pReq->pCont == NULL) {
S
Shengliang 已提交
114 115
    dTrace("RPC %p, req:%s not processed since its empty, app:%p", pReq->handle, TMSG_INFO(msgType), pReq->ahandle);
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_DND_INVALID_MSG_LEN, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
116 117 118 119
    rpcSendResponse(&rspMsg);
    return;
  }

S
shm  
Shengliang Guan 已提交
120 121 122 123 124
  SMsgHandle *pHandle = &pMgmt->msgHandles[TMSG_INDEX(msgType)];
  if (pHandle->rpcMsgFp != NULL) {
    dTrace("RPC %p, req:%s will be processed by %s, app:%p", pReq->handle, TMSG_INFO(msgType), pHandle->pWrapper->name,
           pReq->ahandle);
    (*pHandle->rpcMsgFp)(pDnode, pHandle->pWrapper, pReq, pEpSet);
S
Shengliang Guan 已提交
125
  } else {
S
Shengliang 已提交
126 127
    dError("RPC %p, req:%s not processed since no handle, app:%p", pReq->handle, TMSG_INFO(msgType), pReq->ahandle);
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_MSG_NOT_PROCESSED, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
128
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
129
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
130 131 132 133
  }
}

static void dndSendMsgToMnodeRecv(SDnode *pDnode, SRpcMsg *pRpcMsg, SRpcMsg *pRpcRsp) {
S
Shengliang Guan 已提交
134
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
135 136 137 138 139 140

  SEpSet epSet = {0};
  dndGetMnodeEpSet(pDnode, &epSet);
  rpcSendRecv(pMgmt->clientRpc, &epSet, pRpcMsg, pRpcRsp);
}

S
shm  
Shengliang Guan 已提交
141 142 143 144
static int32_t dndGetHideUserAuth(SDnode *pDnode, char *user, char *spi, char *encrypt, char *secret, char *ckey) {
  int32_t code = 0;
  char    pass[TSDB_PASSWORD_LEN + 1] = {0};

S
Shengliang Guan 已提交
145
  if (strcmp(user, INTERNAL_USER) == 0) {
S
Shengliang Guan 已提交
146
    taosEncryptPass_c((uint8_t *)(INTERNAL_SECRET), strlen(INTERNAL_SECRET), pass);
S
Shengliang Guan 已提交
147
  } else if (strcmp(user, TSDB_NETTEST_USER) == 0) {
S
Shengliang Guan 已提交
148
    taosEncryptPass_c((uint8_t *)(TSDB_NETTEST_USER), strlen(TSDB_NETTEST_USER), pass);
S
shm  
Shengliang Guan 已提交
149 150 151 152 153
  } else {
    code = -1;
  }

  if (code == 0) {
154
    memcpy(secret, pass, TSDB_PASSWORD_LEN);
S
Shengliang Guan 已提交
155
    *spi = 1;
S
Shengliang Guan 已提交
156 157 158
    *encrypt = 0;
    *ckey = 0;
  }
S
shm  
Shengliang Guan 已提交
159 160

  return code;
S
Shengliang Guan 已提交
161 162
}

S
Shengliang Guan 已提交
163 164 165
static int32_t dndRetrieveUserAuthInfo(void *parent, char *user, char *spi, char *encrypt, char *secret, char *ckey) {
  SDnode *pDnode = parent;

S
shm  
Shengliang Guan 已提交
166
  if (dndGetHideUserAuth(parent, user, spi, encrypt, secret, ckey) == 0) {
S
Shengliang 已提交
167
    dTrace("user:%s, get auth from mnode, spi:%d encrypt:%d", user, *spi, *encrypt);
S
Shengliang Guan 已提交
168 169 170
    return 0;
  }

S
shm  
Shengliang Guan 已提交
171
  if (mmGetUserAuth(dndGetWrapper(pDnode, MNODE), user, spi, encrypt, secret, ckey) == 0) {
S
Shengliang 已提交
172
    dTrace("user:%s, get auth from mnode, spi:%d encrypt:%d", user, *spi, *encrypt);
S
Shengliang Guan 已提交
173 174 175 176
    return 0;
  }

  if (terrno != TSDB_CODE_APP_NOT_READY) {
S
Shengliang 已提交
177
    dTrace("failed to get user auth from mnode since %s", terrstr());
S
Shengliang Guan 已提交
178
    return -1;
S
Shengliang Guan 已提交
179 180
  }

S
Shengliang Guan 已提交
181 182 183
  SAuthReq authReq = {0};
  tstrncpy(authReq.user, user, TSDB_USER_LEN);
  int32_t contLen = tSerializeSAuthReq(NULL, 0, &authReq);
S
shm  
Shengliang Guan 已提交
184
  void   *pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
185
  tSerializeSAuthReq(pReq, contLen, &authReq);
S
Shengliang Guan 已提交
186

S
Shengliang Guan 已提交
187
  SRpcMsg rpcMsg = {.pCont = pReq, .contLen = contLen, .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528};
S
Shengliang Guan 已提交
188
  SRpcMsg rpcRsp = {0};
S
Shengliang Guan 已提交
189
  dTrace("user:%s, send user auth req to other mnodes, spi:%d encrypt:%d", user, authReq.spi, authReq.encrypt);
S
Shengliang Guan 已提交
190 191 192 193
  dndSendMsgToMnodeRecv(pDnode, &rpcMsg, &rpcRsp);

  if (rpcRsp.code != 0) {
    terrno = rpcRsp.code;
S
Shengliang Guan 已提交
194
    dError("user:%s, failed to get user auth from other mnodes since %s", user, terrstr());
S
Shengliang Guan 已提交
195
  } else {
S
Shengliang Guan 已提交
196 197 198 199 200 201 202 203
    SAuthRsp authRsp = {0};
    tDeserializeSAuthReq(rpcRsp.pCont, rpcRsp.contLen, &authRsp);
    memcpy(secret, authRsp.secret, TSDB_PASSWORD_LEN);
    memcpy(ckey, authRsp.ckey, TSDB_PASSWORD_LEN);
    *spi = authRsp.spi;
    *encrypt = authRsp.encrypt;
    dTrace("user:%s, success to get user auth from other mnodes, spi:%d encrypt:%d", user, authRsp.spi,
           authRsp.encrypt);
S
Shengliang Guan 已提交
204 205 206 207 208 209
  }

  rpcFreeCont(rpcRsp.pCont);
  return rpcRsp.code;
}

S
shm  
Shengliang Guan 已提交
210
 int32_t dndInitServer(SDnode *pDnode) {
S
Shengliang Guan 已提交
211
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
212

S
config  
Shengliang Guan 已提交
213
  int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0);
S
Shengliang Guan 已提交
214 215 216 217 218 219
  if (numOfThreads < 1) {
    numOfThreads = 1;
  }

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
S
Shengliang Guan 已提交
220
  rpcInit.localPort = pDnode->cfg.serverPort;
S
shm  
Shengliang Guan 已提交
221
  rpcInit.label = "SRV";
S
Shengliang Guan 已提交
222 223
  rpcInit.numOfThreads = numOfThreads;
  rpcInit.cfp = dndProcessRequest;
S
Shengliang Guan 已提交
224
  rpcInit.sessions = tsMaxShellConns;
S
Shengliang Guan 已提交
225
  rpcInit.connType = TAOS_CONN_SERVER;
S
Shengliang Guan 已提交
226
  rpcInit.idleTime = tsShellActivityTimer * 1000;
S
Shengliang Guan 已提交
227
  rpcInit.afp = dndRetrieveUserAuthInfo;
228
  rpcInit.parent = pDnode;
S
Shengliang Guan 已提交
229 230 231

  pMgmt->serverRpc = rpcOpen(&rpcInit);
  if (pMgmt->serverRpc == NULL) {
S
Shengliang 已提交
232
    dError("failed to init dnode rpc server");
S
Shengliang Guan 已提交
233 234 235
    return -1;
  }

236
  dDebug("dnode rpc server is initialized");
S
Shengliang Guan 已提交
237 238 239 240
  return 0;
}

static void dndCleanupServer(SDnode *pDnode) {
S
Shengliang Guan 已提交
241
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
242 243 244
  if (pMgmt->serverRpc) {
    rpcClose(pMgmt->serverRpc);
    pMgmt->serverRpc = NULL;
245
    dDebug("dnode rpc server is closed");
S
Shengliang Guan 已提交
246 247 248
  }
}

S
shm  
Shengliang Guan 已提交
249 250 251 252
static int32_t dndSetMsgHandle(SDnode *pDnode) {
  STransMgmt *pMgmt = &pDnode->tmgmt;

  for (ENodeType nodeType = 0; nodeType < NODE_MAX; ++nodeType) {
S
shm  
Shengliang Guan 已提交
253
    SMgmtWrapper  *pWrapper = &pDnode->wrappers[nodeType];
S
shm  
Shengliang Guan 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    GetMsgHandleFp getMsgHandleFp = pDnode->fps[nodeType].getMsgHandleFp;
    if (getMsgHandleFp == NULL) continue;

    for (int32_t msgIndex = 0; msgIndex < TDMT_MAX; ++msgIndex) {
      SMsgHandle msgHandle = (*getMsgHandleFp)(pWrapper, msgIndex);
      if (msgHandle.rpcMsgFp == NULL) continue;

      SMsgHandle *pHandle = &pMgmt->msgHandles[msgIndex];
      if (pHandle->rpcMsgFp != NULL) {
        dError("msg:%s, has multiple process nodes, prev node:%s, curr node:%s", tMsgInfo[msgIndex],
               pHandle->pWrapper->name, pWrapper->name);
        return -1;
      } else {
        dDebug("msg:%s, will be processed by node:%s", tMsgInfo[msgIndex], pWrapper->name);
        *pHandle = msgHandle;
      }
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
276
int32_t dndInitTrans(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
277 278 279 280 281 282
  dInfo("dnode-transport start to init");

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

S
Shengliang Guan 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
  if (dndInitClient(pDnode) != 0) {
    return -1;
  }

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

  dInfo("dnode-transport is initialized");
  return 0;
}

void dndCleanupTrans(SDnode *pDnode) {
296
  dInfo("dnode-transport start to clean up");
S
Shengliang Guan 已提交
297 298 299 300 301
  dndCleanupServer(pDnode);
  dndCleanupClient(pDnode);
  dInfo("dnode-transport is cleaned up");
}

S
Shengliang Guan 已提交
302
int32_t dndSendReqToDnode(SDnode *pDnode, SEpSet *pEpSet, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
303
  STransMgmt *pMgmt = &pDnode->tmgmt;
S
Shengliang Guan 已提交
304 305 306 307 308
  if (pMgmt->clientRpc == NULL) {
    terrno = TSDB_CODE_DND_OFFLINE;
    return -1;
  }

S
Shengliang Guan 已提交
309
  rpcSendRequest(pMgmt->clientRpc, pEpSet, pReq, NULL);
S
Shengliang Guan 已提交
310
  return 0;
S
Shengliang Guan 已提交
311 312
}

S
Shengliang Guan 已提交
313
int32_t dndSendReqToMnode(SDnode *pDnode, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
314 315
  SEpSet epSet = {0};
  dndGetMnodeEpSet(pDnode, &epSet);
S
Shengliang Guan 已提交
316
  return dndSendReqToDnode(pDnode, &epSet, pReq);
D
dapan1121 已提交
317
}