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
/*
 * 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 18
#include "dndInt.h"

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

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

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

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

S
shm  
Shengliang Guan 已提交
46
int32_t dndInitClient(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
47
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
48 49 50

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

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

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

72
  dDebug("dnode rpc client is initialized");
S
Shengliang Guan 已提交
73 74 75
  return 0;
}

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

S
Shengliang Guan 已提交
85
static void dndProcessRequest(void *param, SRpcMsg *pReq, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
86
  SDnode     *pDnode = param;
S
shm  
Shengliang Guan 已提交
87
  STransMgmt *pMgmt = &pDnode->trans;
S
shm  
Shengliang Guan 已提交
88
  tmsg_t      msgType = pReq->msgType;
S
Shengliang Guan 已提交
89

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

S
shm  
Shengliang Guan 已提交
96
  if (dndGetStatus(pDnode) != DND_STAT_RUNNING) {
S
Shengliang 已提交
97 98
    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 已提交
99
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
100
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
101 102 103
    return;
  }

S
Shengliang Guan 已提交
104
  if (pReq->pCont == NULL) {
S
Shengliang 已提交
105 106
    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 已提交
107 108 109 110
    rpcSendResponse(&rspMsg);
    return;
  }

S
shm  
Shengliang Guan 已提交
111
  SMsgHandle *pHandle = &pMgmt->msgHandles[TMSG_INDEX(msgType)];
S
shm  
Shengliang Guan 已提交
112
  if (pHandle->msgFp != NULL) {
S
shm  
Shengliang Guan 已提交
113 114
    dTrace("RPC %p, req:%s will be processed by %s, app:%p", pReq->handle, TMSG_INFO(msgType), pHandle->pWrapper->name,
           pReq->ahandle);
S
shm  
Shengliang Guan 已提交
115
    dndProcessRpcMsg(pHandle->pWrapper, pReq, pEpSet);
S
Shengliang Guan 已提交
116
  } else {
S
Shengliang 已提交
117 118
    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 已提交
119
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
120
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
121 122 123 124
  }
}

static void dndSendMsgToMnodeRecv(SDnode *pDnode, SRpcMsg *pRpcMsg, SRpcMsg *pRpcRsp) {
S
shm  
Shengliang Guan 已提交
125
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
126 127

  SEpSet epSet = {0};
S
shm  
Shengliang Guan 已提交
128
  dmGetMnodeEpSet(dndAcquireWrapper(pDnode, DNODE)->pMgmt, &epSet);
S
Shengliang Guan 已提交
129 130 131
  rpcSendRecv(pMgmt->clientRpc, &epSet, pRpcMsg, pRpcRsp);
}

S
shm  
Shengliang Guan 已提交
132 133 134 135
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 已提交
136
  if (strcmp(user, INTERNAL_USER) == 0) {
S
Shengliang Guan 已提交
137
    taosEncryptPass_c((uint8_t *)(INTERNAL_SECRET), strlen(INTERNAL_SECRET), pass);
S
Shengliang Guan 已提交
138
  } else if (strcmp(user, TSDB_NETTEST_USER) == 0) {
S
Shengliang Guan 已提交
139
    taosEncryptPass_c((uint8_t *)(TSDB_NETTEST_USER), strlen(TSDB_NETTEST_USER), pass);
S
shm  
Shengliang Guan 已提交
140 141 142 143 144
  } else {
    code = -1;
  }

  if (code == 0) {
145
    memcpy(secret, pass, TSDB_PASSWORD_LEN);
S
Shengliang Guan 已提交
146
    *spi = 1;
S
Shengliang Guan 已提交
147 148 149
    *encrypt = 0;
    *ckey = 0;
  }
S
shm  
Shengliang Guan 已提交
150 151

  return code;
S
Shengliang Guan 已提交
152 153
}

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

S
shm  
Shengliang Guan 已提交
157
  if (dndGetHideUserAuth(parent, user, spi, encrypt, secret, ckey) == 0) {
S
Shengliang 已提交
158
    dTrace("user:%s, get auth from mnode, spi:%d encrypt:%d", user, *spi, *encrypt);
S
Shengliang Guan 已提交
159 160 161
    return 0;
  }

S
shm  
Shengliang Guan 已提交
162
  if (mmGetUserAuth(dndAcquireWrapper(pDnode, MNODE), user, spi, encrypt, secret, ckey) == 0) {
S
Shengliang 已提交
163
    dTrace("user:%s, get auth from mnode, spi:%d encrypt:%d", user, *spi, *encrypt);
S
Shengliang Guan 已提交
164 165 166 167
    return 0;
  }

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

S
Shengliang Guan 已提交
172 173 174
  SAuthReq authReq = {0};
  tstrncpy(authReq.user, user, TSDB_USER_LEN);
  int32_t contLen = tSerializeSAuthReq(NULL, 0, &authReq);
S
shm  
Shengliang Guan 已提交
175
  void   *pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
176
  tSerializeSAuthReq(pReq, contLen, &authReq);
S
Shengliang Guan 已提交
177

S
Shengliang Guan 已提交
178
  SRpcMsg rpcMsg = {.pCont = pReq, .contLen = contLen, .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528};
S
Shengliang Guan 已提交
179
  SRpcMsg rpcRsp = {0};
S
Shengliang Guan 已提交
180
  dTrace("user:%s, send user auth req to other mnodes, spi:%d encrypt:%d", user, authReq.spi, authReq.encrypt);
S
Shengliang Guan 已提交
181 182 183 184
  dndSendMsgToMnodeRecv(pDnode, &rpcMsg, &rpcRsp);

  if (rpcRsp.code != 0) {
    terrno = rpcRsp.code;
S
Shengliang Guan 已提交
185
    dError("user:%s, failed to get user auth from other mnodes since %s", user, terrstr());
S
Shengliang Guan 已提交
186
  } else {
S
Shengliang Guan 已提交
187 188 189 190 191 192 193 194
    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 已提交
195 196 197 198 199 200
  }

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

S
shm  
Shengliang Guan 已提交
201
int32_t dndInitServer(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
202
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
203

S
config  
Shengliang Guan 已提交
204
  int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0);
S
Shengliang Guan 已提交
205 206 207 208 209 210
  if (numOfThreads < 1) {
    numOfThreads = 1;
  }

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
S
shm  
Shengliang Guan 已提交
211
  rpcInit.localPort = pDnode->serverPort;
S
shm  
Shengliang Guan 已提交
212
  rpcInit.label = "DND";
S
Shengliang Guan 已提交
213 214
  rpcInit.numOfThreads = numOfThreads;
  rpcInit.cfp = dndProcessRequest;
S
Shengliang Guan 已提交
215
  rpcInit.sessions = tsMaxShellConns;
S
Shengliang Guan 已提交
216
  rpcInit.connType = TAOS_CONN_SERVER;
S
Shengliang Guan 已提交
217
  rpcInit.idleTime = tsShellActivityTimer * 1000;
S
Shengliang Guan 已提交
218
  rpcInit.afp = dndRetrieveUserAuthInfo;
219
  rpcInit.parent = pDnode;
S
Shengliang Guan 已提交
220 221 222

  pMgmt->serverRpc = rpcOpen(&rpcInit);
  if (pMgmt->serverRpc == NULL) {
S
Shengliang 已提交
223
    dError("failed to init dnode rpc server");
S
Shengliang Guan 已提交
224 225 226
    return -1;
  }

227
  dDebug("dnode rpc server is initialized");
S
Shengliang Guan 已提交
228 229 230
  return 0;
}

S
shm  
Shengliang Guan 已提交
231
void dndCleanupServer(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
232
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
233 234 235
  if (pMgmt->serverRpc) {
    rpcClose(pMgmt->serverRpc);
    pMgmt->serverRpc = NULL;
236
    dDebug("dnode rpc server is closed");
S
Shengliang Guan 已提交
237 238 239
  }
}

S
shm  
Shengliang Guan 已提交
240
int32_t dndInitMsgHandle(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
241
  STransMgmt *pMgmt = &pDnode->trans;
S
shm  
Shengliang Guan 已提交
242

S
shm  
Shengliang Guan 已提交
243 244
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
shm  
Shengliang Guan 已提交
245 246

    for (int32_t msgIndex = 0; msgIndex < TDMT_MAX; ++msgIndex) {
S
shm  
Shengliang Guan 已提交
247 248
      NodeMsgFp msgFp = pWrapper->msgFps[msgIndex];
      if (msgFp == NULL) continue;
S
shm  
Shengliang Guan 已提交
249 250

      SMsgHandle *pHandle = &pMgmt->msgHandles[msgIndex];
S
shm  
Shengliang Guan 已提交
251
      if (pHandle->msgFp != NULL) {
S
shm  
Shengliang Guan 已提交
252
        dError("msg:%s has multiple process nodes, prev node:%s, curr node:%s", tMsgInfo[msgIndex],
S
shm  
Shengliang Guan 已提交
253 254 255
               pHandle->pWrapper->name, pWrapper->name);
        return -1;
      } else {
S
shm  
Shengliang Guan 已提交
256
        dTrace("msg:%s will be processed by %s", tMsgInfo[msgIndex], pWrapper->name);
S
shm  
Shengliang Guan 已提交
257
        pHandle->msgFp = msgFp;
S
shm  
Shengliang Guan 已提交
258
        pHandle->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
259 260 261 262 263 264 265
      }
    }
  }

  return 0;
}

S
shm  
Shengliang Guan 已提交
266
static int32_t dndSendRpcReq(STransMgmt *pMgmt, SEpSet *pEpSet, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
267 268 269 270 271
  if (pMgmt->clientRpc == NULL) {
    terrno = TSDB_CODE_DND_OFFLINE;
    return -1;
  }

S
Shengliang Guan 已提交
272
  rpcSendRequest(pMgmt->clientRpc, pEpSet, pReq, NULL);
S
Shengliang Guan 已提交
273
  return 0;
S
Shengliang Guan 已提交
274 275
}

S
shm  
Shengliang Guan 已提交
276
int32_t dndSendReqToDnode(SMgmtWrapper *pWrapper, SEpSet *pEpSet, SRpcMsg *pReq) {
S
shm  
Shengliang Guan 已提交
277 278 279 280 281
  if (pWrapper->procType == PROC_CHILD) {
  } else {
    STransMgmt *pTrans = &pWrapper->pDnode->trans;
    return dndSendRpcReq(pTrans, pEpSet, pReq);
  }
S
shm  
Shengliang Guan 已提交
282 283
}

S
shm  
Shengliang Guan 已提交
284
int32_t dndSendReqToMnode(SMgmtWrapper *pWrapper, SRpcMsg *pReq) {
S
shm  
Shengliang Guan 已提交
285 286 287 288 289
  if (pWrapper->procType == PROC_CHILD) {
  } else {
    SDnode     *pDnode = pWrapper->pDnode;
    STransMgmt *pTrans = &pDnode->trans;
    SEpSet      epSet = {0};
S
shm  
Shengliang Guan 已提交
290
    dmGetMnodeEpSet(dndAcquireWrapper(pDnode, DNODE)->pMgmt, &epSet);
S
shm  
Shengliang Guan 已提交
291 292 293 294 295
    return dndSendRpcReq(pTrans, &epSet, pReq);
  }
}

void dndSendRpcRsp(SMgmtWrapper *pWrapper, SRpcMsg *pRsp) {
S
shm  
Shengliang Guan 已提交
296
  if (pRsp->code == TSDB_CODE_NODE_NOT_DEPLOYED || pRsp->code == TSDB_CODE_APP_NOT_READY) {
S
shm  
Shengliang Guan 已提交
297
    SMgmtWrapper *pDnodeWrapper = dndAcquireWrapper(pWrapper->pDnode, DNODE);
S
shm  
Shengliang Guan 已提交
298
    dmSendRedirectRsp(pDnodeWrapper->pMgmt, pRsp);
S
shm  
Shengliang Guan 已提交
299 300 301 302 303
  } else {
    rpcSendResponse(pRsp);
  }
}

S
shm  
Shengliang Guan 已提交
304
void dndSendRsp(SMgmtWrapper *pWrapper, SRpcMsg *pRsp) {
S
shm  
Shengliang Guan 已提交
305 306 307 308 309 310 311 312 313 314 315
  if (pWrapper->procType == PROC_CHILD) {
    int32_t code = -1;
    do {
      code = taosProcPutToParentQueue(pWrapper->pProc, pRsp, sizeof(SRpcMsg), pRsp->pCont, pRsp->contLen);
      if (code != 0) {
        taosMsleep(10);
      }
    } while (code != 0);
  } else {
    dndSendRpcRsp(pWrapper, pRsp);
  }
D
dapan1121 已提交
316
}