dndTransport.c 11.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 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

23 24 25 26 27 28 29 30 31 32 33 34 35 36
static inline void dndProcessQVnodeRpcMsg(SMsgHandle *pHandle, SRpcMsg *pMsg, SEpSet *pEpSet) {
  SMsgHead *pHead = pMsg->pCont;
  int32_t   vgId = htonl(pHead->vgId);

  SMgmtWrapper *pWrapper = pHandle->pWrapper;
  if (vgId == pHandle->vgId && pHandle->pVgIdWrapper != NULL) {
    pWrapper = pHandle->pVgIdWrapper;
  }

  dTrace("msg:%s will be processed by %s, handle:%p app:%p vgId:%d", TMSG_INFO(pMsg->msgType), pWrapper->name,
         pMsg->handle, pMsg->ahandle, vgId);
  dndProcessRpcMsg(pWrapper, pMsg, pEpSet);
}

S
Shengliang Guan 已提交
37
static void dndProcessResponse(void *parent, SRpcMsg *pRsp, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
38
  SDnode     *pDnode = parent;
S
shm  
Shengliang Guan 已提交
39
  STransMgmt *pMgmt = &pDnode->trans;
S
shm  
Shengliang Guan 已提交
40
  tmsg_t      msgType = pRsp->msgType;
S
Shengliang Guan 已提交
41

S
shm  
Shengliang Guan 已提交
42
  if (dndGetStatus(pDnode) != DND_STAT_RUNNING) {
S
shm  
Shengliang Guan 已提交
43
    dTrace("rsp:%s ignored since dnode not running, handle:%p app:%p", TMSG_INFO(msgType), pRsp->handle, pRsp->ahandle);
S
Shengliang Guan 已提交
44
    rpcFreeCont(pRsp->pCont);
S
Shengliang Guan 已提交
45 46 47
    return;
  }

S
shm  
Shengliang Guan 已提交
48
  SMsgHandle *pHandle = &pMgmt->msgHandles[TMSG_INDEX(msgType)];
S
shm  
Shengliang Guan 已提交
49
  if (pHandle->msgFp != NULL) {
50 51 52 53 54 55 56
    if (pHandle->vgId == 0) {
      dTrace("rsp:%s will be processed by %s, handle:%p app:%p code:0x%04x:%s", TMSG_INFO(msgType),
             pHandle->pWrapper->name, pRsp->handle, pRsp->ahandle, pRsp->code & 0XFFFF, tstrerror(pRsp->code));
      dndProcessRpcMsg(pHandle->pWrapper, pRsp, pEpSet);
    } else {
      dndProcessQVnodeRpcMsg(pHandle, pRsp, pEpSet);
    }
S
Shengliang Guan 已提交
57
  } else {
S
shm  
Shengliang Guan 已提交
58
    dError("rsp:%s not processed since no handle, handle:%p app:%p", TMSG_INFO(msgType), pRsp->handle, pRsp->ahandle);
S
Shengliang Guan 已提交
59
    rpcFreeCont(pRsp->pCont);
S
Shengliang Guan 已提交
60 61 62
  }
}

S
shm  
Shengliang Guan 已提交
63
int32_t dndInitClient(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
64
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
65 66 67

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
S
shm  
Shengliang Guan 已提交
68
  rpcInit.label = "DND";
S
Shengliang Guan 已提交
69 70
  rpcInit.numOfThreads = 1;
  rpcInit.cfp = dndProcessResponse;
S
Shengliang Guan 已提交
71
  rpcInit.sessions = 1024;
S
Shengliang Guan 已提交
72
  rpcInit.connType = TAOS_CONN_CLIENT;
S
Shengliang Guan 已提交
73
  rpcInit.idleTime = tsShellActivityTimer * 1000;
S
Shengliang Guan 已提交
74 75
  rpcInit.user = INTERNAL_USER;
  rpcInit.ckey = INTERNAL_CKEY;
S
Shengliang Guan 已提交
76
  rpcInit.spi = 1;
77
  rpcInit.parent = pDnode;
S
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79 80
  char pass[TSDB_PASSWORD_LEN + 1] = {0};
  taosEncryptPass_c((uint8_t *)(INTERNAL_SECRET), strlen(INTERNAL_SECRET), pass);
S
Shengliang Guan 已提交
81 82
  rpcInit.secret = pass;

S
Shengliang Guan 已提交
83 84
  pMgmt->clientRpc = rpcOpen(&rpcInit);
  if (pMgmt->clientRpc == NULL) {
S
Shengliang 已提交
85
    dError("failed to init dnode rpc client");
S
Shengliang Guan 已提交
86 87 88
    return -1;
  }

89
  dDebug("dnode rpc client is initialized");
S
Shengliang Guan 已提交
90 91 92
  return 0;
}

S
shm  
Shengliang Guan 已提交
93
void dndCleanupClient(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
94
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
95 96 97
  if (pMgmt->clientRpc) {
    rpcClose(pMgmt->clientRpc);
    pMgmt->clientRpc = NULL;
98
    dDebug("dnode rpc client is closed");
S
Shengliang Guan 已提交
99 100 101
  }
}

S
Shengliang Guan 已提交
102
static void dndProcessRequest(void *param, SRpcMsg *pReq, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
103
  SDnode     *pDnode = param;
S
shm  
Shengliang Guan 已提交
104
  STransMgmt *pMgmt = &pDnode->trans;
S
shm  
Shengliang Guan 已提交
105
  tmsg_t      msgType = pReq->msgType;
S
Shengliang Guan 已提交
106

H
Hongze Cheng 已提交
107
  if (msgType == TDMT_DND_NETWORK_TEST) {
S
shm  
Shengliang Guan 已提交
108
    dTrace("network test req will be processed, handle:%p, app:%p", pReq->handle, pReq->ahandle);
S
shm  
Shengliang Guan 已提交
109
    dndProcessStartupReq(pDnode, pReq);
S
Shengliang Guan 已提交
110 111 112
    return;
  }

S
shm  
Shengliang Guan 已提交
113
  if (dndGetStatus(pDnode) != DND_STAT_RUNNING) {
S
shm  
Shengliang Guan 已提交
114
    dError("req:%s ignored since dnode not running, handle:%p app:%p", TMSG_INFO(msgType), pReq->handle, pReq->ahandle);
S
Shengliang 已提交
115
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_APP_NOT_READY, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
116
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
117
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
118 119 120
    return;
  }

S
Shengliang Guan 已提交
121
  if (pReq->pCont == NULL) {
S
shm  
Shengliang Guan 已提交
122
    dTrace("req:%s not processed since its empty, handle:%p app:%p", TMSG_INFO(msgType), pReq->handle, pReq->ahandle);
S
Shengliang 已提交
123
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_DND_INVALID_MSG_LEN, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
124 125 126 127
    rpcSendResponse(&rspMsg);
    return;
  }

S
shm  
Shengliang Guan 已提交
128
  SMsgHandle *pHandle = &pMgmt->msgHandles[TMSG_INDEX(msgType)];
S
shm  
Shengliang Guan 已提交
129
  if (pHandle->msgFp != NULL) {
130 131 132 133 134 135 136
    if (pHandle->vgId == 0) {
      dTrace("req:%s will be processed by %s, handle:%p app:%p", TMSG_INFO(msgType), pHandle->pWrapper->name,
             pReq->handle, pReq->ahandle);
      dndProcessRpcMsg(pHandle->pWrapper, pReq, pEpSet);
    } else {
      dndProcessQVnodeRpcMsg(pHandle, pReq, pEpSet);
    }
S
Shengliang Guan 已提交
137
  } else {
S
shm  
Shengliang Guan 已提交
138
    dError("req:%s not processed since no handle, handle:%p app:%p", TMSG_INFO(msgType), pReq->handle, pReq->ahandle);
S
Shengliang 已提交
139
    SRpcMsg rspMsg = {.handle = pReq->handle, .code = TSDB_CODE_MSG_NOT_PROCESSED, .ahandle = pReq->ahandle};
S
Shengliang Guan 已提交
140
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
141
    rpcFreeCont(pReq->pCont);
S
Shengliang Guan 已提交
142 143 144 145
  }
}

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

  SEpSet epSet = {0};
S
shm  
Shengliang Guan 已提交
149
  dmGetMnodeEpSet(dndAcquireWrapper(pDnode, DNODE)->pMgmt, &epSet);
S
Shengliang Guan 已提交
150 151 152
  rpcSendRecv(pMgmt->clientRpc, &epSet, pRpcMsg, pRpcRsp);
}

S
shm  
Shengliang Guan 已提交
153 154 155 156
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 已提交
157
  if (strcmp(user, INTERNAL_USER) == 0) {
S
Shengliang Guan 已提交
158
    taosEncryptPass_c((uint8_t *)(INTERNAL_SECRET), strlen(INTERNAL_SECRET), pass);
S
Shengliang Guan 已提交
159
  } else if (strcmp(user, TSDB_NETTEST_USER) == 0) {
S
Shengliang Guan 已提交
160
    taosEncryptPass_c((uint8_t *)(TSDB_NETTEST_USER), strlen(TSDB_NETTEST_USER), pass);
S
shm  
Shengliang Guan 已提交
161 162 163 164 165
  } else {
    code = -1;
  }

  if (code == 0) {
166
    memcpy(secret, pass, TSDB_PASSWORD_LEN);
S
Shengliang Guan 已提交
167
    *spi = 1;
S
Shengliang Guan 已提交
168 169 170
    *encrypt = 0;
    *ckey = 0;
  }
S
shm  
Shengliang Guan 已提交
171 172

  return code;
S
Shengliang Guan 已提交
173 174
}

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

S
shm  
Shengliang Guan 已提交
178
  if (dndGetHideUserAuth(parent, user, spi, encrypt, secret, ckey) == 0) {
S
Shengliang 已提交
179
    dTrace("user:%s, get auth from mnode, spi:%d encrypt:%d", user, *spi, *encrypt);
S
Shengliang Guan 已提交
180 181 182
    return 0;
  }

S
shm  
Shengliang Guan 已提交
183
  if (mmGetUserAuth(dndAcquireWrapper(pDnode, MNODE), user, spi, encrypt, secret, ckey) == 0) {
S
Shengliang 已提交
184
    dTrace("user:%s, get auth from mnode, spi:%d encrypt:%d", user, *spi, *encrypt);
S
Shengliang Guan 已提交
185 186 187 188
    return 0;
  }

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

S
Shengliang Guan 已提交
193 194 195
  SAuthReq authReq = {0};
  tstrncpy(authReq.user, user, TSDB_USER_LEN);
  int32_t contLen = tSerializeSAuthReq(NULL, 0, &authReq);
S
shm  
Shengliang Guan 已提交
196
  void   *pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
197
  tSerializeSAuthReq(pReq, contLen, &authReq);
S
Shengliang Guan 已提交
198

S
Shengliang Guan 已提交
199
  SRpcMsg rpcMsg = {.pCont = pReq, .contLen = contLen, .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528};
S
Shengliang Guan 已提交
200
  SRpcMsg rpcRsp = {0};
S
Shengliang Guan 已提交
201
  dTrace("user:%s, send user auth req to other mnodes, spi:%d encrypt:%d", user, authReq.spi, authReq.encrypt);
S
Shengliang Guan 已提交
202 203 204 205
  dndSendMsgToMnodeRecv(pDnode, &rpcMsg, &rpcRsp);

  if (rpcRsp.code != 0) {
    terrno = rpcRsp.code;
S
Shengliang Guan 已提交
206
    dError("user:%s, failed to get user auth from other mnodes since %s", user, terrstr());
S
Shengliang Guan 已提交
207
  } else {
S
Shengliang Guan 已提交
208 209 210 211 212 213 214 215
    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 已提交
216 217 218 219 220 221
  }

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

S
shm  
Shengliang Guan 已提交
222
int32_t dndInitServer(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
223
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
224

S
config  
Shengliang Guan 已提交
225
  int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0);
S
Shengliang Guan 已提交
226 227 228 229 230 231
  if (numOfThreads < 1) {
    numOfThreads = 1;
  }

  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
S
shm  
Shengliang Guan 已提交
232
  rpcInit.localPort = pDnode->serverPort;
S
shm  
Shengliang Guan 已提交
233
  rpcInit.label = "DND";
S
Shengliang Guan 已提交
234 235
  rpcInit.numOfThreads = numOfThreads;
  rpcInit.cfp = dndProcessRequest;
S
Shengliang Guan 已提交
236
  rpcInit.sessions = tsMaxShellConns;
S
Shengliang Guan 已提交
237
  rpcInit.connType = TAOS_CONN_SERVER;
S
Shengliang Guan 已提交
238
  rpcInit.idleTime = tsShellActivityTimer * 1000;
S
Shengliang Guan 已提交
239
  rpcInit.afp = dndRetrieveUserAuthInfo;
240
  rpcInit.parent = pDnode;
S
Shengliang Guan 已提交
241 242 243

  pMgmt->serverRpc = rpcOpen(&rpcInit);
  if (pMgmt->serverRpc == NULL) {
S
Shengliang 已提交
244
    dError("failed to init dnode rpc server");
S
Shengliang Guan 已提交
245 246 247
    return -1;
  }

248
  dDebug("dnode rpc server is initialized");
S
Shengliang Guan 已提交
249 250 251
  return 0;
}

S
shm  
Shengliang Guan 已提交
252
void dndCleanupServer(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
253
  STransMgmt *pMgmt = &pDnode->trans;
S
Shengliang Guan 已提交
254 255 256
  if (pMgmt->serverRpc) {
    rpcClose(pMgmt->serverRpc);
    pMgmt->serverRpc = NULL;
257
    dDebug("dnode rpc server is closed");
S
Shengliang Guan 已提交
258 259 260
  }
}

S
shm  
Shengliang Guan 已提交
261
int32_t dndInitMsgHandle(SDnode *pDnode) {
S
shm  
Shengliang Guan 已提交
262
  STransMgmt *pMgmt = &pDnode->trans;
S
shm  
Shengliang Guan 已提交
263

S
shm  
Shengliang Guan 已提交
264 265
  for (ENodeType n = 0; n < NODE_MAX; ++n) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[n];
S
shm  
Shengliang Guan 已提交
266 267

    for (int32_t msgIndex = 0; msgIndex < TDMT_MAX; ++msgIndex) {
S
shm  
Shengliang Guan 已提交
268
      NodeMsgFp msgFp = pWrapper->msgFps[msgIndex];
269
      int32_t   vgId = pWrapper->msgVgIds[msgIndex];
S
shm  
Shengliang Guan 已提交
270
      if (msgFp == NULL) continue;
S
shm  
Shengliang Guan 已提交
271 272

      SMsgHandle *pHandle = &pMgmt->msgHandles[msgIndex];
273 274 275
      if (pHandle->msgFp != NULL && pHandle->vgId == vgId) {
        dError("msg:%s has multiple process nodes, prev node:%s:%d, curr node:%s:%d", tMsgInfo[msgIndex],
               pHandle->pWrapper->name, pHandle->pWrapper->msgVgIds[msgIndex], pWrapper->name, vgId);
S
shm  
Shengliang Guan 已提交
276 277
        return -1;
      } else {
278 279 280 281 282 283 284 285 286
        dTrace("msg:%s will be processed by %s, vgId:%d", tMsgInfo[msgIndex], pWrapper->name, vgId);
        if (vgId == 0) {
          pHandle->msgFp = msgFp;
          pHandle->pWrapper = pWrapper;
        } else {
          pHandle->vgId = vgId;
          pHandle->vgIdMsgFp = msgFp;
          pHandle->pVgIdWrapper = pWrapper;
        }
S
shm  
Shengliang Guan 已提交
287 288 289 290 291 292 293
      }
    }
  }

  return 0;
}

S
shm  
Shengliang Guan 已提交
294
static int32_t dndSendRpcReq(STransMgmt *pMgmt, SEpSet *pEpSet, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
295 296 297 298 299
  if (pMgmt->clientRpc == NULL) {
    terrno = TSDB_CODE_DND_OFFLINE;
    return -1;
  }

S
Shengliang Guan 已提交
300
  rpcSendRequest(pMgmt->clientRpc, pEpSet, pReq, NULL);
S
Shengliang Guan 已提交
301
  return 0;
S
Shengliang Guan 已提交
302 303
}

S
shm  
Shengliang Guan 已提交
304
int32_t dndSendReqToDnode(SMgmtWrapper *pWrapper, SEpSet *pEpSet, SRpcMsg *pReq) {
S
shm  
Shengliang Guan 已提交
305 306
  if (pWrapper->procType == PROC_CHILD) {
  } else {
S
shm  
Shengliang Guan 已提交
307 308 309 310 311 312 313
    SDnode *pDnode = pWrapper->pDnode;
    if (dndGetStatus(pDnode) != DND_STAT_RUNNING) {
      terrno = TSDB_CODE_DND_OFFLINE;
      dError("failed to send rpc msg since %s, handle:%p", terrstr(), pReq->handle);
      return -1;
    }
    return dndSendRpcReq(&pDnode->trans, pEpSet, pReq);
S
shm  
Shengliang Guan 已提交
314
  }
S
shm  
Shengliang Guan 已提交
315 316
}

S
shm  
Shengliang Guan 已提交
317
int32_t dndSendReqToMnode(SMgmtWrapper *pWrapper, SRpcMsg *pReq) {
S
shm  
Shengliang Guan 已提交
318 319 320 321 322
  if (pWrapper->procType == PROC_CHILD) {
  } else {
    SDnode     *pDnode = pWrapper->pDnode;
    STransMgmt *pTrans = &pDnode->trans;
    SEpSet      epSet = {0};
S
shm  
Shengliang Guan 已提交
323
    dmGetMnodeEpSet(dndAcquireWrapper(pDnode, DNODE)->pMgmt, &epSet);
S
shm  
Shengliang Guan 已提交
324 325 326 327 328
    return dndSendRpcReq(pTrans, &epSet, pReq);
  }
}

void dndSendRpcRsp(SMgmtWrapper *pWrapper, SRpcMsg *pRsp) {
S
shm  
Shengliang Guan 已提交
329
  if (pRsp->code == TSDB_CODE_APP_NOT_READY) {
S
shm  
Shengliang Guan 已提交
330
    SMgmtWrapper *pDnodeWrapper = dndAcquireWrapper(pWrapper->pDnode, DNODE);
S
shm  
Shengliang Guan 已提交
331
    dmSendRedirectRsp(pDnodeWrapper->pMgmt, pRsp);
S
shm  
Shengliang Guan 已提交
332 333 334 335 336
  } else {
    rpcSendResponse(pRsp);
  }
}

S
shm  
Shengliang Guan 已提交
337
void dndSendRsp(SMgmtWrapper *pWrapper, SRpcMsg *pRsp) {
S
shm  
Shengliang Guan 已提交
338 339 340 341 342 343 344 345 346 347 348
  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 已提交
349
}