dmTransport.c 11.3 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "dmMgmt.h"
S
Shengliang Guan 已提交
18
#include "qworker.h"
19

20
static inline void dmSendRsp(SRpcMsg *pMsg) { rpcSendResponse(pMsg); }
S
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

static inline void dmBuildMnodeRedirectRsp(SDnode *pDnode, SRpcMsg *pMsg) {
  SEpSet epSet = {0};
  dmGetMnodeEpSetForRedirect(&pDnode->data, pMsg, &epSet);

  const int32_t contLen = tSerializeSEpSet(NULL, 0, &epSet);
  pMsg->pCont = rpcMallocCont(contLen);
  if (pMsg->pCont == NULL) {
    pMsg->code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    tSerializeSEpSet(pMsg->pCont, contLen, &epSet);
    pMsg->contLen = contLen;
  }
}

S
Shengliang Guan 已提交
36
int32_t dmProcessNodeMsg(SMgmtWrapper *pWrapper, SRpcMsg *pMsg) {
37 38
  const STraceId *trace = &pMsg->info.traceId;

S
Shengliang Guan 已提交
39
  NodeMsgFp msgFp = pWrapper->msgFps[TMSG_INDEX(pMsg->msgType)];
S
Shengliang Guan 已提交
40 41
  if (msgFp == NULL) {
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
42
    dGError("msg:%p, not processed since no handler, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
S
Shengliang Guan 已提交
43 44
    return -1;
  }
S
Shengliang Guan 已提交
45

S
Shengliang Guan 已提交
46
  dGTrace("msg:%p, will be processed by %s", pMsg, pWrapper->name);
S
Shengliang Guan 已提交
47
  pMsg->info.wrapper = pWrapper;
S
Shengliang Guan 已提交
48 49 50
  return (*msgFp)(pWrapper->pMgmt, pMsg);
}

dengyihao's avatar
dengyihao 已提交
51 52
static bool dmFailFastFp(tmsg_t msgType) {
  // add more msg type later
dengyihao's avatar
dengyihao 已提交
53
  return msgType == TDMT_SYNC_HEARTBEAT || msgType == TDMT_SYNC_APPEND_ENTRIES;
dengyihao's avatar
dengyihao 已提交
54 55
}

dengyihao's avatar
dengyihao 已提交
56 57 58 59 60 61 62 63 64
static void dmConvertErrCode(tmsg_t msgType) {
  if (terrno != TSDB_CODE_APP_IS_STOPPING) {
    return;
  }
  if ((msgType > TDMT_VND_MSG && msgType < TDMT_VND_MAX_MSG) ||
      (msgType > TDMT_SCH_MSG && msgType < TDMT_SCH_MAX_MSG)) {
    terrno = TSDB_CODE_VND_STOPPED;
  }
}
S
Shengliang Guan 已提交
65
static void dmProcessRpcMsg(SDnode *pDnode, SRpcMsg *pRpc, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
66
  SDnodeTrans  *pTrans = &pDnode->trans;
S
Shengliang Guan 已提交
67
  int32_t       code = -1;
S
Shengliang Guan 已提交
68
  SRpcMsg      *pMsg = NULL;
S
Shengliang Guan 已提交
69
  SMgmtWrapper *pWrapper = NULL;
S
Shengliang Guan 已提交
70
  SDnodeHandle *pHandle = &pTrans->msgHandles[TMSG_INDEX(pRpc->msgType)];
S
Shengliang Guan 已提交
71

S
Shengliang Guan 已提交
72
  const STraceId *trace = &pRpc->info.traceId;
dengyihao's avatar
dengyihao 已提交
73 74
  dGTrace("msg:%s is received, handle:%p len:%d code:0x%x app:%p refId:%" PRId64, TMSG_INFO(pRpc->msgType),
          pRpc->info.handle, pRpc->contLen, pRpc->code, pRpc->info.ahandle, pRpc->info.refId);
75

S
Shengliang Guan 已提交
76 77 78 79 80
  switch (pRpc->msgType) {
    case TDMT_DND_NET_TEST:
      dmProcessNetTestReq(pDnode, pRpc);
      return;
    case TDMT_MND_SYSTABLE_RETRIEVE_RSP:
D
dapan1121 已提交
81
    case TDMT_DND_SYSTABLE_RETRIEVE_RSP:
D
dapan1121 已提交
82
    case TDMT_SCH_FETCH_RSP:
D
dapan1121 已提交
83
    case TDMT_SCH_MERGE_FETCH_RSP:
D
dapan1121 已提交
84 85
    case TDMT_VND_SUBMIT_RSP:
      qWorkerProcessRspMsg(NULL, NULL, pRpc, 0);
S
Shengliang Guan 已提交
86 87 88 89 90 91 92 93
      return;
    case TDMT_MND_STATUS_RSP:
      if (pEpSet != NULL) {
        dmSetMnodeEpSet(&pDnode->data, pEpSet);
      }
      break;
    default:
      break;
S
Shengliang Guan 已提交
94 95
  }

C
cademfly 已提交
96 97 98 99 100 101 102 103 104 105 106 107
/*
pDnode is null, TD-22618
at trans.c line 91
before this line, dmProcessRpcMsg callback is set
after this line, parent is set
so when dmProcessRpcMsg is called, pDonde is still null.
*/
  if (pDnode != NULL){
    if(pDnode->status != DND_STAT_RUNNING) {
      if (pRpc->msgType == TDMT_DND_SERVER_STATUS) {
        dmProcessServerStartupStatus(pDnode, pRpc);
        return;
108
      } else {
C
cademfly 已提交
109 110 111 112 113 114
        if (pDnode->status == DND_STAT_INIT) {
          terrno = TSDB_CODE_APP_IS_STARTING;
        } else {
          terrno = TSDB_CODE_APP_IS_STOPPING;
        }
        goto _OVER;
115
      }
C
cademfly 已提交
116 117 118 119
    }   
  } else {
    terrno = TSDB_CODE_APP_IS_STARTING;
    goto _OVER;
S
Shengliang Guan 已提交
120 121
  }

122
  if (pRpc->pCont == NULL && (IsReq(pRpc) || pRpc->contLen != 0)) {
123
    dGError("msg:%p, type:%s pCont is NULL", pRpc, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
124
    terrno = TSDB_CODE_INVALID_MSG_LEN;
S
Shengliang Guan 已提交
125
    goto _OVER;
dengyihao's avatar
dengyihao 已提交
126 127 128 129 130 131
  } else if ((pRpc->code == TSDB_CODE_RPC_NETWORK_UNAVAIL || pRpc->code == TSDB_CODE_RPC_BROKEN_LINK) &&
             (!IsReq(pRpc)) && (pRpc->pCont == NULL)) {
    dGError("msg:%p, type:%s pCont is NULL, err: %s", pRpc, TMSG_INFO(pRpc->msgType), tstrerror(pRpc->code));
    terrno = pRpc->code;
    goto _OVER;
  }
S
Shengliang Guan 已提交
132 133

  if (pHandle->defaultNtype == NODE_END) {
134
    dGError("msg:%p, type:%s not processed since no handle", pRpc, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
135
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
136
    goto _OVER;
S
Shengliang Guan 已提交
137 138 139 140 141 142 143 144 145
  }

  pWrapper = &pDnode->wrappers[pHandle->defaultNtype];
  if (pHandle->needCheckVgId) {
    if (pRpc->contLen > 0) {
      const SMsgHead *pHead = pRpc->pCont;
      const int32_t   vgId = ntohl(pHead->vgId);
      switch (vgId) {
        case QNODE_HANDLE:
146
          pWrapper = &pDnode->wrappers[QNODE];
S
Shengliang Guan 已提交
147 148
          break;
        case SNODE_HANDLE:
149
          pWrapper = &pDnode->wrappers[SNODE];
S
Shengliang Guan 已提交
150 151
          break;
        case MNODE_HANDLE:
152
          pWrapper = &pDnode->wrappers[MNODE];
S
Shengliang Guan 已提交
153 154 155
          break;
        default:
          break;
S
Shengliang Guan 已提交
156
      }
S
Shengliang Guan 已提交
157
    } else {
158
      dGError("msg:%p, type:%s contLen is 0", pRpc, TMSG_INFO(pRpc->msgType));
S
Shengliang Guan 已提交
159 160
      terrno = TSDB_CODE_INVALID_MSG_LEN;
      goto _OVER;
S
Shengliang Guan 已提交
161 162
    }
  }
S
Shengliang Guan 已提交
163

S
Shengliang Guan 已提交
164
  if (dmMarkWrapper(pWrapper) != 0) {
S
Shengliang Guan 已提交
165 166
    pWrapper = NULL;
    goto _OVER;
S
Shengliang Guan 已提交
167
  }
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169
  pRpc->info.wrapper = pWrapper;
S
Shengliang Guan 已提交
170
  pMsg = taosAllocateQitem(sizeof(SRpcMsg), RPC_QITEM, pRpc->contLen);
171
  if (pMsg == NULL) goto _OVER;
S
Shengliang Guan 已提交
172

S
Shengliang Guan 已提交
173
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
dengyihao's avatar
dengyihao 已提交
174 175
  dGTrace("msg:%p, is created, type:%s handle:%p len:%d", pMsg, TMSG_INFO(pRpc->msgType), pMsg->info.handle,
          pRpc->contLen);
S
Shengliang Guan 已提交
176

177
  code = dmProcessNodeMsg(pWrapper, pMsg);
S
Shengliang Guan 已提交
178 179

_OVER:
180
  if (code != 0) {
dengyihao's avatar
dengyihao 已提交
181
    dmConvertErrCode(pRpc->msgType);
S
Shengliang Guan 已提交
182
    if (terrno != 0) code = terrno;
L
Liu Jicong 已提交
183 184 185 186 187
    if (pMsg) {
      dGTrace("msg:%p, failed to process %s since %s", pMsg, TMSG_INFO(pMsg->msgType), terrstr());
    } else {
      dGTrace("msg:%p, failed to process empty msg since %s", pMsg, terrstr());
    }
S
Shengliang Guan 已提交
188

S
Shengliang Guan 已提交
189
    if (IsReq(pRpc)) {
190
      SRpcMsg rsp = {.code = code, .info = pRpc->info};
191
      if (code == TSDB_CODE_MNODE_NOT_FOUND) {
192 193 194 195 196
        dmBuildMnodeRedirectRsp(pDnode, &rsp);
      }

      if (pWrapper != NULL) {
        dmSendRsp(&rsp);
S
Shengliang Guan 已提交
197
      } else {
198
        rpcSendResponse(&rsp);
S
Shengliang Guan 已提交
199
      }
S
Shengliang Guan 已提交
200
    }
S
Shengliang Guan 已提交
201

S
Shengliang Guan 已提交
202
    if (pMsg != NULL) {
S
Shengliang Guan 已提交
203
      dGTrace("msg:%p, is freed", pMsg);
S
Shengliang Guan 已提交
204 205
      taosFreeQitem(pMsg);
    }
S
Shengliang Guan 已提交
206
    rpcFreeCont(pRpc->pCont);
S
Shengliang Guan 已提交
207
    pRpc->pCont = NULL;
S
Shengliang Guan 已提交
208 209
  }

S
Shengliang Guan 已提交
210
  dmReleaseWrapper(pWrapper);
S
Shengliang Guan 已提交
211 212
}

S
Shengliang Guan 已提交
213
int32_t dmInitMsgHandle(SDnode *pDnode) {
S
Shengliang Guan 已提交
214
  SDnodeTrans *pTrans = &pDnode->trans;
S
shm  
Shengliang Guan 已提交
215

S
Shengliang 已提交
216 217
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
S
Shengliang Guan 已提交
218
    SArray       *pArray = (*pWrapper->func.getHandlesFp)();
S
Shengliang Guan 已提交
219 220 221
    if (pArray == NULL) return -1;

    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
S
Shengliang Guan 已提交
222
      SMgmtHandle  *pMgmt = taosArrayGet(pArray, i);
223
      SDnodeHandle *pHandle = &pTrans->msgHandles[TMSG_INDEX(pMgmt->msgType)];
S
Shengliang Guan 已提交
224 225 226 227
      if (pMgmt->needCheckVgId) {
        pHandle->needCheckVgId = pMgmt->needCheckVgId;
      }
      if (!pMgmt->needCheckVgId) {
S
Shengliang 已提交
228
        pHandle->defaultNtype = ntype;
S
shm  
Shengliang Guan 已提交
229
      }
S
Shengliang Guan 已提交
230
      pWrapper->msgFps[TMSG_INDEX(pMgmt->msgType)] = pMgmt->msgFp;
S
shm  
Shengliang Guan 已提交
231
    }
S
Shengliang Guan 已提交
232 233

    taosArrayDestroy(pArray);
S
shm  
Shengliang Guan 已提交
234 235 236 237 238
  }

  return 0;
}

S
Shengliang Guan 已提交
239
static inline int32_t dmSendReq(const SEpSet *pEpSet, SRpcMsg *pMsg) {
240
  SDnode *pDnode = dmInstance();
241
  if (pDnode->status != DND_STAT_RUNNING && pMsg->msgType < TDMT_SYNC_MSG) {
S
Shengliang Guan 已提交
242 243
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
244 245 246 247 248
    if (pDnode->status == DND_STAT_INIT) {
      terrno = TSDB_CODE_APP_IS_STARTING;
    } else {
      terrno = TSDB_CODE_APP_IS_STOPPING;
    }
249
    dError("failed to send rpc msg:%s since %s, handle:%p", TMSG_INFO(pMsg->msgType), terrstr(), pMsg->info.handle);
S
shm  
Shengliang Guan 已提交
250
    return -1;
251
  } else {
S
Shengliang Guan 已提交
252
    rpcSendRequest(pDnode->trans.clientRpc, pEpSet, pMsg, NULL);
253
    return 0;
S
shm  
Shengliang Guan 已提交
254 255 256
  }
}

257
static inline void dmRegisterBrokenLinkArg(SRpcMsg *pMsg) { rpcRegisterBrokenLinkArg(pMsg); }
S
shm  
Shengliang Guan 已提交
258

259
static inline void dmReleaseHandle(SRpcHandleInfo *pHandle, int8_t type) { rpcReleaseHandle(pHandle, type); }
S
shm  
Shengliang Guan 已提交
260

dengyihao's avatar
dengyihao 已提交
261
static bool rpcRfp(int32_t code, tmsg_t msgType) {
262
  if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_BROKEN_LINK || code == TSDB_CODE_MNODE_NOT_FOUND ||
dengyihao's avatar
dengyihao 已提交
263 264 265
      code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED || code == TSDB_CODE_SYN_NOT_LEADER ||
      code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_VND_STOPPED || code == TSDB_CODE_APP_IS_STARTING ||
      code == TSDB_CODE_APP_IS_STOPPING) {
L
Liu Jicong 已提交
266 267
    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY || msgType == TDMT_SCH_FETCH ||
        msgType == TDMT_SCH_MERGE_FETCH) {
dengyihao's avatar
dengyihao 已提交
268 269
      return false;
    }
dengyihao's avatar
dengyihao 已提交
270 271 272 273 274
    return true;
  } else {
    return false;
  }
}
M
Minghao Li 已提交
275

276
int32_t dmInitClient(SDnode *pDnode) {
S
Shengliang Guan 已提交
277 278 279
  SDnodeTrans *pTrans = &pDnode->trans;

  SRpcInit rpcInit = {0};
dengyihao's avatar
dengyihao 已提交
280
  rpcInit.label = "DND-C";
dengyihao's avatar
dengyihao 已提交
281
  rpcInit.numOfThreads = tsNumOfRpcThreads;
S
Shengliang Guan 已提交
282
  rpcInit.cfp = (RpcCfp)dmProcessRpcMsg;
S
Shengliang Guan 已提交
283 284 285 286
  rpcInit.sessions = 1024;
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.idleTime = tsShellActivityTimer * 1000;
  rpcInit.parent = pDnode;
M
Minghao Li 已提交
287
  rpcInit.rfp = rpcRfp;
dengyihao's avatar
dengyihao 已提交
288
  rpcInit.compressSize = tsCompressMsgSize;
dengyihao's avatar
dengyihao 已提交
289 290 291 292 293

  rpcInit.retryMinInterval = tsRedirectPeriod;
  rpcInit.retryStepFactor = tsRedirectFactor;
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
  rpcInit.retryMaxTimouet = tsMaxRetryWaitTime;
S
Shengliang Guan 已提交
294

dengyihao's avatar
dengyihao 已提交
295
  rpcInit.failFastInterval = 5000;  // interval threshold(ms)
dengyihao's avatar
dengyihao 已提交
296 297 298
  rpcInit.failFastThreshold = 3;    // failed threshold
  rpcInit.ffp = dmFailFastFp;

dengyihao's avatar
dengyihao 已提交
299
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
dengyihao's avatar
dengyihao 已提交
300
  connLimitNum = TMAX(connLimitNum, 10);
dengyihao's avatar
dengyihao 已提交
301
  connLimitNum = TMIN(connLimitNum, 500);
dengyihao's avatar
dengyihao 已提交
302 303

  rpcInit.connLimitNum = connLimitNum;
dengyihao's avatar
dengyihao 已提交
304
  rpcInit.connLimitLock = 1;
dengyihao's avatar
dengyihao 已提交
305
  rpcInit.supportBatch = 1;
dengyihao's avatar
dengyihao 已提交
306
  rpcInit.batchSize = 8 * 1024;
dengyihao's avatar
dengyihao 已提交
307
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
dengyihao's avatar
dengyihao 已提交
308

S
Shengliang Guan 已提交
309 310 311 312 313 314 315 316 317 318
  pTrans->clientRpc = rpcOpen(&rpcInit);
  if (pTrans->clientRpc == NULL) {
    dError("failed to init dnode rpc client");
    return -1;
  }

  dDebug("dnode rpc client is initialized");
  return 0;
}

319
void dmCleanupClient(SDnode *pDnode) {
S
Shengliang Guan 已提交
320 321 322 323 324 325 326 327
  SDnodeTrans *pTrans = &pDnode->trans;
  if (pTrans->clientRpc) {
    rpcClose(pTrans->clientRpc);
    pTrans->clientRpc = NULL;
    dDebug("dnode rpc client is closed");
  }
}

328
int32_t dmInitServer(SDnode *pDnode) {
S
Shengliang Guan 已提交
329 330 331
  SDnodeTrans *pTrans = &pDnode->trans;

  SRpcInit rpcInit = {0};
S
Shengliang Guan 已提交
332
  tstrncpy(rpcInit.localFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
333
  rpcInit.localPort = tsServerPort;
dengyihao's avatar
dengyihao 已提交
334
  rpcInit.label = "DND-S";
S
Shengliang Guan 已提交
335
  rpcInit.numOfThreads = tsNumOfRpcThreads;
S
Shengliang Guan 已提交
336
  rpcInit.cfp = (RpcCfp)dmProcessRpcMsg;
S
Shengliang Guan 已提交
337 338 339 340
  rpcInit.sessions = tsMaxShellConns;
  rpcInit.connType = TAOS_CONN_SERVER;
  rpcInit.idleTime = tsShellActivityTimer * 1000;
  rpcInit.parent = pDnode;
dengyihao's avatar
dengyihao 已提交
341
  rpcInit.compressSize = tsCompressMsgSize;
S
Shengliang Guan 已提交
342 343 344 345 346 347 348 349 350 351 352

  pTrans->serverRpc = rpcOpen(&rpcInit);
  if (pTrans->serverRpc == NULL) {
    dError("failed to init dnode rpc server");
    return -1;
  }

  dDebug("dnode rpc server is initialized");
  return 0;
}

353
void dmCleanupServer(SDnode *pDnode) {
S
Shengliang Guan 已提交
354 355 356 357 358 359 360 361
  SDnodeTrans *pTrans = &pDnode->trans;
  if (pTrans->serverRpc) {
    rpcClose(pTrans->serverRpc);
    pTrans->serverRpc = NULL;
    dDebug("dnode rpc server is closed");
  }
}

362 363 364 365 366 367 368 369
SMsgCb dmGetMsgcb(SDnode *pDnode) {
  SMsgCb msgCb = {
      .clientRpc = pDnode->trans.clientRpc,
      .sendReqFp = dmSendReq,
      .sendRspFp = dmSendRsp,
      .registerBrokenLinkArgFp = dmRegisterBrokenLinkArg,
      .releaseHandleFp = dmReleaseHandle,
      .reportStartupFp = dmReportStartup,
370 371
      .updateDnodeInfoFp = dmUpdateDnodeInfo,
      .data = &pDnode->data,
S
Shengliang Guan 已提交
372 373
  };
  return msgCb;
dengyihao's avatar
dengyihao 已提交
374
}