|
|
|
@ -13,31 +13,27 @@
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef USE_UV
|
|
|
|
|
#include <uv.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include "lz4.h"
|
|
|
|
|
#include "os.h"
|
|
|
|
|
#include "rpcCache.h"
|
|
|
|
|
#include "rpcHead.h"
|
|
|
|
|
#include "rpcLog.h"
|
|
|
|
|
#include "rpcTcp.h"
|
|
|
|
|
#include "rpcUdp.h"
|
|
|
|
|
#include "taoserror.h"
|
|
|
|
|
#include "tglobal.h"
|
|
|
|
|
#include "thash.h"
|
|
|
|
|
#include "tidpool.h"
|
|
|
|
|
#include "tmd5.h"
|
|
|
|
|
#include "tmempool.h"
|
|
|
|
|
#include "tmsg.h"
|
|
|
|
|
#include "tref.h"
|
|
|
|
|
#include "trpc.h"
|
|
|
|
|
#include "ttimer.h"
|
|
|
|
|
#include "tutil.h"
|
|
|
|
|
#include "lz4.h"
|
|
|
|
|
#include "tref.h"
|
|
|
|
|
#include "taoserror.h"
|
|
|
|
|
#include "tglobal.h"
|
|
|
|
|
#include "tmsg.h"
|
|
|
|
|
#include "trpc.h"
|
|
|
|
|
#include "thash.h"
|
|
|
|
|
#include "rpcLog.h"
|
|
|
|
|
#include "rpcUdp.h"
|
|
|
|
|
#include "rpcCache.h"
|
|
|
|
|
#include "rpcTcp.h"
|
|
|
|
|
#include "rpcHead.h"
|
|
|
|
|
|
|
|
|
|
#define RPC_MSG_OVERHEAD (sizeof(SRpcReqContext) + sizeof(SRpcHead) + sizeof(SRpcDigest))
|
|
|
|
|
#define rpcHeadFromCont(cont) ((SRpcHead *) ((char*)cont - sizeof(SRpcHead)))
|
|
|
|
|
#define rpcContFromHead(msg) (msg + sizeof(SRpcHead))
|
|
|
|
|
#define rpcMsgLenFromCont(contLen) (contLen + sizeof(SRpcHead))
|
|
|
|
|
#define rpcContLenFromMsg(msgLen) (msgLen - sizeof(SRpcHead))
|
|
|
|
|
#define rpcIsReq(type) (type & 1U)
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
int sessions; // number of sessions allowed
|
|
|
|
@ -69,6 +65,220 @@ typedef struct {
|
|
|
|
|
struct SRpcConn* connList; // connection list
|
|
|
|
|
} SRpcInfo;
|
|
|
|
|
|
|
|
|
|
#ifdef USE_UV
|
|
|
|
|
|
|
|
|
|
#define container_of(ptr, type, member) ((type*)((char*)(ptr)-offsetof(type, member)))
|
|
|
|
|
|
|
|
|
|
typedef struct SThreadObj {
|
|
|
|
|
pthread_t thread;
|
|
|
|
|
uv_pipe_t* pipe;
|
|
|
|
|
uv_loop_t* loop;
|
|
|
|
|
uv_async_t* workerAsync; //
|
|
|
|
|
int fd;
|
|
|
|
|
} SThreadObj;
|
|
|
|
|
|
|
|
|
|
typedef struct SServerObj {
|
|
|
|
|
uv_tcp_t server;
|
|
|
|
|
uv_loop_t* loop;
|
|
|
|
|
int workerIdx;
|
|
|
|
|
int numOfThread;
|
|
|
|
|
SThreadObj** pThreadObj;
|
|
|
|
|
uv_pipe_t** pipe;
|
|
|
|
|
} SServerObj;
|
|
|
|
|
|
|
|
|
|
typedef struct SConnCtx {
|
|
|
|
|
uv_tcp_t* pClient;
|
|
|
|
|
uv_timer_t* pTimer;
|
|
|
|
|
uv_async_t* pWorkerAsync;
|
|
|
|
|
int ref;
|
|
|
|
|
} SConnCtx;
|
|
|
|
|
|
|
|
|
|
static void allocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
|
|
|
|
static void onTimeout(uv_timer_t* handle);
|
|
|
|
|
static void onRead(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
|
|
|
|
|
static void onWrite(uv_write_t* req, int status);
|
|
|
|
|
static void onAccept(uv_stream_t* stream, int status);
|
|
|
|
|
void onConnection(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf);
|
|
|
|
|
static void workerAsyncCB(uv_async_t* handle);
|
|
|
|
|
static void* workerThread(void* arg);
|
|
|
|
|
|
|
|
|
|
int32_t rpcInit() { return -1; }
|
|
|
|
|
void rpcCleanup() { return; };
|
|
|
|
|
void* rpcOpen(const SRpcInit* pInit) {
|
|
|
|
|
SRpcInfo* pRpc = calloc(1, sizeof(SRpcInfo));
|
|
|
|
|
if (pRpc == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (pInit->label) {
|
|
|
|
|
tstrncpy(pRpc->label, pInit->label, sizeof(pRpc->label));
|
|
|
|
|
}
|
|
|
|
|
pRpc->numOfThreads = pInit->numOfThreads > TSDB_MAX_RPC_THREADS ? TSDB_MAX_RPC_THREADS : pInit->numOfThreads;
|
|
|
|
|
|
|
|
|
|
SServerObj* srv = calloc(1, sizeof(SServerObj));
|
|
|
|
|
srv->loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
|
|
|
|
|
srv->numOfThread = pRpc->numOfThreads;
|
|
|
|
|
srv->workerIdx = 0;
|
|
|
|
|
srv->pThreadObj = (SThreadObj**)calloc(srv->numOfThread, sizeof(SThreadObj*));
|
|
|
|
|
srv->pipe = (uv_pipe_t**)calloc(srv->numOfThread, sizeof(uv_pipe_t*));
|
|
|
|
|
uv_loop_init(srv->loop);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < srv->numOfThread; i++) {
|
|
|
|
|
srv->pThreadObj[i] = (SThreadObj*)calloc(1, sizeof(SThreadObj));
|
|
|
|
|
srv->pipe[i] = (uv_pipe_t*)calloc(2, sizeof(uv_pipe_t));
|
|
|
|
|
int fds[2];
|
|
|
|
|
if (uv_socketpair(AF_UNIX, SOCK_STREAM, fds, UV_NONBLOCK_PIPE, UV_NONBLOCK_PIPE) != 0) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
uv_pipe_init(srv->loop, &(srv->pipe[i][0]), 1);
|
|
|
|
|
uv_pipe_open(&(srv->pipe[i][0]), fds[1]); // init write
|
|
|
|
|
|
|
|
|
|
srv->pThreadObj[i]->fd = fds[0];
|
|
|
|
|
srv->pThreadObj[i]->pipe = &(srv->pipe[i][1]); // init read
|
|
|
|
|
int err = pthread_create(&(srv->pThreadObj[i]->thread), NULL, workerThread, (void*)(srv->pThreadObj[i]));
|
|
|
|
|
if (err == 0) {
|
|
|
|
|
tError("sucess to create worker thread %d", i);
|
|
|
|
|
// printf("thread %d create\n", i);
|
|
|
|
|
} else {
|
|
|
|
|
tError("failed to create worker thread %d", i);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uv_tcp_init(srv->loop, &srv->server);
|
|
|
|
|
struct sockaddr_in bind_addr;
|
|
|
|
|
uv_ip4_addr("0.0.0.0", pInit->localPort, &bind_addr);
|
|
|
|
|
uv_tcp_bind(&srv->server, (const struct sockaddr*)&bind_addr, 0);
|
|
|
|
|
int err = 0;
|
|
|
|
|
if ((err = uv_listen((uv_stream_t*)&srv->server, 128, onAccept)) != 0) {
|
|
|
|
|
tError("Listen error %s\n", uv_err_name(err));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
uv_run(srv->loop, UV_RUN_DEFAULT);
|
|
|
|
|
|
|
|
|
|
return pRpc;
|
|
|
|
|
}
|
|
|
|
|
void rpcClose(void* arg) { return; }
|
|
|
|
|
void* rpcMallocCont(int contLen) { return NULL; }
|
|
|
|
|
void rpcFreeCont(void* cont) { return; }
|
|
|
|
|
void* rpcReallocCont(void* ptr, int contLen) { return NULL; }
|
|
|
|
|
|
|
|
|
|
void rpcSendRequest(void* thandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* rid) { return; }
|
|
|
|
|
|
|
|
|
|
void rpcSendResponse(const SRpcMsg* pMsg) {}
|
|
|
|
|
|
|
|
|
|
void rpcSendRedirectRsp(void* pConn, const SEpSet* pEpSet) {}
|
|
|
|
|
int rpcGetConnInfo(void* thandle, SRpcConnInfo* pInfo) { return -1; }
|
|
|
|
|
void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pReq, SRpcMsg* pRsp) { return; }
|
|
|
|
|
int rpcReportProgress(void* pConn, char* pCont, int contLen) { return -1; }
|
|
|
|
|
void rpcCancelRequest(int64_t rid) { return; }
|
|
|
|
|
|
|
|
|
|
void allocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
|
|
|
|
buf->base = malloc(suggested_size);
|
|
|
|
|
buf->len = suggested_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onTimeout(uv_timer_t* handle) {
|
|
|
|
|
// opt
|
|
|
|
|
tDebug("time out");
|
|
|
|
|
}
|
|
|
|
|
void onRead(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) {
|
|
|
|
|
// opt
|
|
|
|
|
tDebug("data already was read on a stream");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onWrite(uv_write_t* req, int status) {
|
|
|
|
|
// opt
|
|
|
|
|
if (req) tDebug("data already was written on stream");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void workerAsyncCB(uv_async_t* handle) {
|
|
|
|
|
// opt
|
|
|
|
|
SThreadObj* pObj = container_of(handle, SThreadObj, workerAsync);
|
|
|
|
|
}
|
|
|
|
|
void onAccept(uv_stream_t* stream, int status) {
|
|
|
|
|
if (status == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SServerObj* pObj = container_of(stream, SServerObj, server);
|
|
|
|
|
tDebug("new conntion accepted by main server, dispatch to one worker thread");
|
|
|
|
|
|
|
|
|
|
uv_tcp_t* cli = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
|
|
|
|
|
uv_tcp_init(pObj->loop, cli);
|
|
|
|
|
if (uv_accept(stream, (uv_stream_t*)cli) == 0) {
|
|
|
|
|
uv_write_t* wr = (uv_write_t*)malloc(sizeof(uv_write_t));
|
|
|
|
|
|
|
|
|
|
uv_buf_t buf = uv_buf_init("a", 1);
|
|
|
|
|
// despatch to worker thread
|
|
|
|
|
pObj->workerIdx = (pObj->workerIdx + 1) % pObj->numOfThread;
|
|
|
|
|
uv_write2(wr, (uv_stream_t*)&(pObj->pipe[pObj->workerIdx][0]), &buf, 1, (uv_stream_t*)cli, onWrite);
|
|
|
|
|
} else {
|
|
|
|
|
uv_close((uv_handle_t*)cli, NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void onConnection(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) {
|
|
|
|
|
if (nread < 0) {
|
|
|
|
|
if (nread != UV_EOF) {
|
|
|
|
|
tError("read error %s", uv_err_name(nread));
|
|
|
|
|
}
|
|
|
|
|
// TODO(log other failure reason)
|
|
|
|
|
uv_close((uv_handle_t*)q, NULL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SThreadObj* pObj = (SThreadObj*)container_of(q, struct SThreadObj, pipe);
|
|
|
|
|
|
|
|
|
|
uv_pipe_t* pipe = (uv_pipe_t*)q;
|
|
|
|
|
if (!uv_pipe_pending_count(pipe)) {
|
|
|
|
|
tError("No pending count");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
uv_handle_type pending = uv_pipe_pending_type(pipe);
|
|
|
|
|
assert(pending == UV_TCP);
|
|
|
|
|
|
|
|
|
|
SConnCtx* pConn = malloc(sizeof(SConnCtx));
|
|
|
|
|
/* init conn timer*/
|
|
|
|
|
pConn->pTimer = malloc(sizeof(uv_timer_t));
|
|
|
|
|
uv_timer_init(pObj->loop, pConn->pTimer);
|
|
|
|
|
|
|
|
|
|
pConn->pClient = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
|
|
|
|
|
pConn->pWorkerAsync = pObj->workerAsync; // thread safty
|
|
|
|
|
uv_tcp_init(pObj->loop, pConn->pClient);
|
|
|
|
|
|
|
|
|
|
if (uv_accept(q, (uv_stream_t*)(pConn->pClient)) == 0) {
|
|
|
|
|
uv_os_fd_t fd;
|
|
|
|
|
uv_fileno((const uv_handle_t*)pConn->pClient, &fd);
|
|
|
|
|
tDebug("new connection created: %d", fd);
|
|
|
|
|
uv_timer_start(pConn->pTimer, onTimeout, 10, 0);
|
|
|
|
|
uv_read_start((uv_stream_t*)(pConn->pClient), allocBuffer, onRead);
|
|
|
|
|
} else {
|
|
|
|
|
uv_timer_stop(pConn->pTimer);
|
|
|
|
|
free(pConn->pTimer);
|
|
|
|
|
uv_close((uv_handle_t*)pConn->pClient, NULL);
|
|
|
|
|
free(pConn->pClient);
|
|
|
|
|
free(pConn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* workerThread(void* arg) {
|
|
|
|
|
SThreadObj* pObj = (SThreadObj*)arg;
|
|
|
|
|
int fd = pObj->fd;
|
|
|
|
|
pObj->loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
|
|
|
|
|
uv_loop_init(pObj->loop);
|
|
|
|
|
|
|
|
|
|
uv_pipe_init(pObj->loop, pObj->pipe, 1);
|
|
|
|
|
uv_pipe_open(pObj->pipe, fd);
|
|
|
|
|
|
|
|
|
|
pObj->workerAsync = malloc(sizeof(uv_async_t));
|
|
|
|
|
uv_async_init(pObj->loop, pObj->workerAsync, workerAsyncCB);
|
|
|
|
|
uv_read_start((uv_stream_t*)pObj->pipe, allocBuffer, onConnection);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#define RPC_MSG_OVERHEAD (sizeof(SRpcReqContext) + sizeof(SRpcHead) + sizeof(SRpcDigest))
|
|
|
|
|
#define rpcHeadFromCont(cont) ((SRpcHead*)((char*)cont - sizeof(SRpcHead)))
|
|
|
|
|
#define rpcContFromHead(msg) (msg + sizeof(SRpcHead))
|
|
|
|
|
#define rpcMsgLenFromCont(contLen) (contLen + sizeof(SRpcHead))
|
|
|
|
|
#define rpcContLenFromMsg(msgLen) (msgLen - sizeof(SRpcHead))
|
|
|
|
|
#define rpcIsReq(type) (type & 1U)
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
SRpcInfo * pRpc; // associated SRpcInfo
|
|
|
|
|
SEpSet epSet; // ip list provided by app
|
|
|
|
@ -146,18 +356,10 @@ static int32_t tsRpcNum = 0;
|
|
|
|
|
#define RPC_CONN_TCPC 3
|
|
|
|
|
|
|
|
|
|
void *(*taosInitConn[])(uint32_t ip, uint16_t port, char *label, int threads, void *fp, void *shandle) = {
|
|
|
|
|
taosInitUdpConnection,
|
|
|
|
|
taosInitUdpConnection,
|
|
|
|
|
taosInitTcpServer,
|
|
|
|
|
taosInitTcpClient
|
|
|
|
|
};
|
|
|
|
|
taosInitUdpConnection, taosInitUdpConnection, taosInitTcpServer, taosInitTcpClient};
|
|
|
|
|
|
|
|
|
|
void (*taosCleanUpConn[])(void *thandle) = {
|
|
|
|
|
taosCleanUpUdpConnection,
|
|
|
|
|
taosCleanUpUdpConnection,
|
|
|
|
|
taosCleanUpTcpServer,
|
|
|
|
|
taosCleanUpTcpClient
|
|
|
|
|
};
|
|
|
|
|
void (*taosCleanUpConn[])(void *thandle) = {taosCleanUpUdpConnection, taosCleanUpUdpConnection, taosCleanUpTcpServer,
|
|
|
|
|
taosCleanUpTcpClient};
|
|
|
|
|
|
|
|
|
|
void (*taosStopConn[])(void *thandle) = {
|
|
|
|
|
taosStopUdpConnection,
|
|
|
|
@ -167,11 +369,7 @@ void (*taosStopConn[])(void *thandle) = {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int (*taosSendData[])(uint32_t ip, uint16_t port, void *data, int len, void *chandle) = {
|
|
|
|
|
taosSendUdpData,
|
|
|
|
|
taosSendUdpData,
|
|
|
|
|
taosSendTcpData,
|
|
|
|
|
taosSendTcpData
|
|
|
|
|
};
|
|
|
|
|
taosSendUdpData, taosSendUdpData, taosSendTcpData, taosSendTcpData};
|
|
|
|
|
|
|
|
|
|
void *(*taosOpenConn[])(void *shandle, void *thandle, uint32_t ip, uint16_t port) = {
|
|
|
|
|
taosOpenUdpConnection,
|
|
|
|
@ -180,12 +378,7 @@ void *(*taosOpenConn[])(void *shandle, void *thandle, uint32_t ip, uint16_t port
|
|
|
|
|
taosOpenTcpClientConnection,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void (*taosCloseConn[])(void *chandle) = {
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
taosCloseTcpConnection,
|
|
|
|
|
taosCloseTcpConnection
|
|
|
|
|
};
|
|
|
|
|
void (*taosCloseConn[])(void *chandle) = {NULL, NULL, taosCloseTcpConnection, taosCloseTcpConnection};
|
|
|
|
|
|
|
|
|
|
static SRpcConn *rpcOpenConn(SRpcInfo *pRpc, char *peerFqdn, uint16_t peerPort, int8_t connType);
|
|
|
|
|
static void rpcCloseConn(void *thandle);
|
|
|
|
@ -311,10 +504,10 @@ void *rpcOpen(const SRpcInit *pInit) {
|
|
|
|
|
|
|
|
|
|
pthread_mutex_init(&pRpc->mutex, NULL);
|
|
|
|
|
|
|
|
|
|
pRpc->tcphandle = (*taosInitConn[pRpc->connType|RPC_CONN_TCP])(0, pRpc->localPort, pRpc->label,
|
|
|
|
|
pRpc->numOfThreads, rpcProcessMsgFromPeer, pRpc);
|
|
|
|
|
pRpc->udphandle = (*taosInitConn[pRpc->connType])(0, pRpc->localPort, pRpc->label,
|
|
|
|
|
pRpc->numOfThreads, rpcProcessMsgFromPeer, pRpc);
|
|
|
|
|
pRpc->tcphandle = (*taosInitConn[pRpc->connType | RPC_CONN_TCP])(0, pRpc->localPort, pRpc->label, pRpc->numOfThreads,
|
|
|
|
|
rpcProcessMsgFromPeer, pRpc);
|
|
|
|
|
pRpc->udphandle =
|
|
|
|
|
(*taosInitConn[pRpc->connType])(0, pRpc->localPort, pRpc->label, pRpc->numOfThreads, rpcProcessMsgFromPeer, pRpc);
|
|
|
|
|
|
|
|
|
|
if (pRpc->tcphandle == NULL || pRpc->udphandle == NULL) {
|
|
|
|
|
tError("%s failed to init network, port:%d", pRpc->label, pRpc->localPort);
|
|
|
|
@ -410,10 +603,9 @@ void rpcSendRequest(void *shandle, const SEpSet *pEpSet, SRpcMsg *pMsg, int64_t
|
|
|
|
|
// connection type is application specific.
|
|
|
|
|
// for TDengine, all the query, show commands shall have TCP connection
|
|
|
|
|
tmsg_t type = pMsg->msgType;
|
|
|
|
|
if (type == TDMT_VND_QUERY || type == TDMT_MND_SHOW_RETRIEVE
|
|
|
|
|
|| type == TDMT_VND_FETCH || type == TDMT_MND_VGROUP_LIST
|
|
|
|
|
|| type == TDMT_VND_TABLES_META || type == TDMT_VND_TABLE_META
|
|
|
|
|
|| type == TDMT_MND_SHOW || type == TDMT_MND_STATUS || type == TDMT_VND_ALTER_TABLE)
|
|
|
|
|
if (type == TDMT_VND_QUERY || type == TDMT_MND_SHOW_RETRIEVE || type == TDMT_VND_FETCH ||
|
|
|
|
|
type == TDMT_MND_VGROUP_LIST || type == TDMT_VND_TABLES_META || type == TDMT_VND_TABLE_META ||
|
|
|
|
|
type == TDMT_MND_SHOW || type == TDMT_MND_STATUS || type == TDMT_VND_ALTER_TABLE)
|
|
|
|
|
pContext->connType = RPC_CONN_TCPC;
|
|
|
|
|
|
|
|
|
|
pContext->rid = taosAddRef(tsRpcRefId, pContext);
|
|
|
|
@ -482,8 +674,7 @@ void rpcSendResponse(const SRpcMsg *pRsp) {
|
|
|
|
|
rpcSendMsgToPeer(pConn, msg, msgLen);
|
|
|
|
|
|
|
|
|
|
// if not set to secured, set it expcet NOT_READY case, since client wont treat it as secured
|
|
|
|
|
if (pConn->secured == 0 && pMsg->code != TSDB_CODE_RPC_NOT_READY)
|
|
|
|
|
pConn->secured = 1; // connection shall be secured
|
|
|
|
|
if (pConn->secured == 0 && pMsg->code != TSDB_CODE_RPC_NOT_READY) pConn->secured = 1; // connection shall be secured
|
|
|
|
|
|
|
|
|
|
if (pConn->pReqMsg) rpcFreeCont(pConn->pReqMsg);
|
|
|
|
|
pConn->pReqMsg = NULL;
|
|
|
|
@ -567,7 +758,6 @@ int rpcReportProgress(void *handle, char *pCont, int contLen) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rpcCancelRequest(int64_t rid) {
|
|
|
|
|
|
|
|
|
|
SRpcReqContext *pContext = taosAcquireRef(tsRpcRefId, rid);
|
|
|
|
|
if (pContext == NULL) return;
|
|
|
|
|
|
|
|
|
@ -631,7 +821,8 @@ static void rpcReleaseConn(SRpcConn *pConn) {
|
|
|
|
|
|
|
|
|
|
if (pRpc->connType == TAOS_CONN_SERVER) {
|
|
|
|
|
char hashstr[40] = {0};
|
|
|
|
|
size_t size = snprintf(hashstr, sizeof(hashstr), "%x:%x:%x:%d", pConn->peerIp, pConn->linkUid, pConn->peerId, pConn->connType);
|
|
|
|
|
size_t size = snprintf(hashstr, sizeof(hashstr), "%x:%x:%x:%d", pConn->peerIp, pConn->linkUid, pConn->peerId,
|
|
|
|
|
pConn->connType);
|
|
|
|
|
taosHashRemove(pRpc->hash, hashstr, size);
|
|
|
|
|
rpcFreeMsg(pConn->pRspMsg); // it may have a response msg saved, but not request msg
|
|
|
|
|
pConn->pRspMsg = NULL;
|
|
|
|
@ -682,8 +873,7 @@ static void rpcCloseConn(void *thandle) {
|
|
|
|
|
|
|
|
|
|
rpcLockConn(pConn);
|
|
|
|
|
|
|
|
|
|
if (pConn->user[0])
|
|
|
|
|
rpcReleaseConn(pConn);
|
|
|
|
|
if (pConn->user[0]) rpcReleaseConn(pConn);
|
|
|
|
|
|
|
|
|
|
rpcUnlockConn(pConn);
|
|
|
|
|
}
|
|
|
|
@ -717,7 +907,8 @@ static SRpcConn *rpcAllocateServerConn(SRpcInfo *pRpc, SRecvInfo *pRecv) {
|
|
|
|
|
char hashstr[40] = {0};
|
|
|
|
|
SRpcHead *pHead = (SRpcHead *)pRecv->msg;
|
|
|
|
|
|
|
|
|
|
size_t size = snprintf(hashstr, sizeof(hashstr), "%x:%x:%x:%d", pRecv->ip, pHead->linkUid, pHead->sourceId, pRecv->connType);
|
|
|
|
|
size_t size =
|
|
|
|
|
snprintf(hashstr, sizeof(hashstr), "%x:%x:%x:%d", pRecv->ip, pHead->linkUid, pHead->sourceId, pRecv->connType);
|
|
|
|
|
|
|
|
|
|
// check if it is already allocated
|
|
|
|
|
SRpcConn **ppConn = (SRpcConn **)(taosHashGet(pRpc->hash, hashstr, size));
|
|
|
|
@ -767,7 +958,8 @@ static SRpcConn *rpcAllocateServerConn(SRpcInfo *pRpc, SRecvInfo *pRecv) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taosHashPut(pRpc->hash, hashstr, size, (char *)&pConn, POINTER_BYTES);
|
|
|
|
|
tDebug("%s %p server connection is allocated, uid:0x%x sid:%d key:%s", pRpc->label, pConn, pConn->linkUid, sid, hashstr);
|
|
|
|
|
tDebug("%s %p server connection is allocated, uid:0x%x sid:%d key:%s", pRpc->label, pConn, pConn->linkUid, sid,
|
|
|
|
|
hashstr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pConn;
|
|
|
|
@ -807,7 +999,8 @@ static SRpcConn *rpcSetupConnToServer(SRpcReqContext *pContext) {
|
|
|
|
|
SRpcInfo *pRpc = pContext->pRpc;
|
|
|
|
|
SEpSet * pEpSet = &pContext->epSet;
|
|
|
|
|
|
|
|
|
|
pConn = rpcGetConnFromCache(pRpc->pCache, pEpSet->fqdn[pEpSet->inUse], pEpSet->port[pEpSet->inUse], pContext->connType);
|
|
|
|
|
pConn =
|
|
|
|
|
rpcGetConnFromCache(pRpc->pCache, pEpSet->fqdn[pEpSet->inUse], pEpSet->port[pEpSet->inUse], pContext->connType);
|
|
|
|
|
if (pConn == NULL || pConn->user[0] == 0) {
|
|
|
|
|
pConn = rpcOpenConn(pRpc, pEpSet->fqdn[pEpSet->inUse], pEpSet->port[pEpSet->inUse], pContext->connType);
|
|
|
|
|
}
|
|
|
|
@ -825,13 +1018,11 @@ static SRpcConn *rpcSetupConnToServer(SRpcReqContext *pContext) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rpcProcessReqHead(SRpcConn *pConn, SRpcHead *pHead) {
|
|
|
|
|
|
|
|
|
|
if (pConn->peerId == 0) {
|
|
|
|
|
pConn->peerId = pHead->sourceId;
|
|
|
|
|
} else {
|
|
|
|
|
if (pConn->peerId != pHead->sourceId) {
|
|
|
|
|
tDebug("%s, source Id is changed, old:0x%08x new:0x%08x", pConn->info,
|
|
|
|
|
pConn->peerId, pHead->sourceId);
|
|
|
|
|
tDebug("%s, source Id is changed, old:0x%08x new:0x%08x", pConn->info, pConn->peerId, pHead->sourceId);
|
|
|
|
|
return TSDB_CODE_RPC_INVALID_VALUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -856,8 +1047,7 @@ static int rpcProcessReqHead(SRpcConn *pConn, SRpcHead *pHead) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pConn->inType != 0) {
|
|
|
|
|
tDebug("%s, last session is not finished, inTranId:%d tranId:%d", pConn->info,
|
|
|
|
|
pConn->inTranId, pHead->tranId);
|
|
|
|
|
tDebug("%s, last session is not finished, inTranId:%d tranId:%d", pConn->info, pConn->inTranId, pHead->tranId);
|
|
|
|
|
return TSDB_CODE_RPC_LAST_SESSION_NOT_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -961,18 +1151,22 @@ static SRpcConn *rpcProcessMsgHead(SRpcInfo *pRpc, SRecvInfo *pRecv, SRpcReqCont
|
|
|
|
|
|
|
|
|
|
if (TMSG_INDEX(pHead->msgType) >= TDMT_MAX || TMSG_INDEX(pHead->msgType) <= 0) {
|
|
|
|
|
tDebug("%s sid:%d, invalid message type:%d", pRpc->label, sid, pHead->msgType);
|
|
|
|
|
terrno = TSDB_CODE_RPC_INVALID_MSG_TYPE; return NULL;
|
|
|
|
|
terrno = TSDB_CODE_RPC_INVALID_MSG_TYPE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sid < 0 || sid >= pRpc->sessions) {
|
|
|
|
|
tDebug("%s sid:%d, sid is out of range, max sid:%d, %s discarded", pRpc->label, sid,
|
|
|
|
|
pRpc->sessions, TMSG_INFO(pHead->msgType));
|
|
|
|
|
terrno = TSDB_CODE_RPC_INVALID_SESSION_ID; return NULL;
|
|
|
|
|
tDebug("%s sid:%d, sid is out of range, max sid:%d, %s discarded", pRpc->label, sid, pRpc->sessions,
|
|
|
|
|
TMSG_INFO(pHead->msgType));
|
|
|
|
|
terrno = TSDB_CODE_RPC_INVALID_SESSION_ID;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rpcIsReq(pHead->msgType) && htonl(pHead->msgVer) != tsVersion >> 8) {
|
|
|
|
|
tDebug("%s sid:%d, invalid client version:%x/%x %s", pRpc->label, sid, htonl(pHead->msgVer), tsVersion, TMSG_INFO(pHead->msgType));
|
|
|
|
|
terrno = TSDB_CODE_RPC_INVALID_VERSION; return NULL;
|
|
|
|
|
tDebug("%s sid:%d, invalid client version:%x/%x %s", pRpc->label, sid, htonl(pHead->msgVer), tsVersion,
|
|
|
|
|
TMSG_INFO(pHead->msgType));
|
|
|
|
|
terrno = TSDB_CODE_RPC_INVALID_VERSION;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pConn = rpcGetConnObj(pRpc, sid, pRecv);
|
|
|
|
@ -1100,13 +1294,13 @@ static void *rpcProcessMsgFromPeer(SRecvInfo *pRecv) {
|
|
|
|
|
taosIpPort2String(pRecv->ip, pRecv->port, ipstr);
|
|
|
|
|
|
|
|
|
|
if (TMSG_INDEX(pHead->msgType) >= 1 && TMSG_INDEX(pHead->msgType) < TDMT_MAX) {
|
|
|
|
|
tDebug("%s %p %p, %s received from %s, parse code:0x%x len:%d sig:0x%08x:0x%08x:%d code:0x%x", pRpc->label,
|
|
|
|
|
pConn, (void *)pHead->ahandle, TMSG_INFO(pHead->msgType), ipstr, terrno, pRecv->msgLen,
|
|
|
|
|
pHead->sourceId, pHead->destId, pHead->tranId, pHead->code);
|
|
|
|
|
tDebug("%s %p %p, %s received from %s, parse code:0x%x len:%d sig:0x%08x:0x%08x:%d code:0x%x", pRpc->label, pConn,
|
|
|
|
|
(void *)pHead->ahandle, TMSG_INFO(pHead->msgType), ipstr, terrno, pRecv->msgLen, pHead->sourceId,
|
|
|
|
|
pHead->destId, pHead->tranId, pHead->code);
|
|
|
|
|
} else {
|
|
|
|
|
tDebug("%s %p %p, %d received from %s, parse code:0x%x len:%d sig:0x%08x:0x%08x:%d code:0x%x", pRpc->label,
|
|
|
|
|
pConn, (void *)pHead->ahandle, pHead->msgType, ipstr, terrno, pRecv->msgLen,
|
|
|
|
|
pHead->sourceId, pHead->destId, pHead->tranId, pHead->code);
|
|
|
|
|
tDebug("%s %p %p, %d received from %s, parse code:0x%x len:%d sig:0x%08x:0x%08x:%d code:0x%x", pRpc->label, pConn,
|
|
|
|
|
(void *)pHead->ahandle, pHead->msgType, ipstr, terrno, pRecv->msgLen, pHead->sourceId, pHead->destId,
|
|
|
|
|
pHead->tranId, pHead->code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t code = terrno;
|
|
|
|
@ -1118,9 +1312,11 @@ static void *rpcProcessMsgFromPeer(SRecvInfo *pRecv) {
|
|
|
|
|
rpcCloseConn(pConn);
|
|
|
|
|
}
|
|
|
|
|
if (TMSG_INDEX(pHead->msgType) + 1 > 1 && TMSG_INDEX(pHead->msgType) + 1 < TDMT_MAX) {
|
|
|
|
|
tDebug("%s %p %p, %s is sent with error code:0x%x", pRpc->label, pConn, (void *)pHead->ahandle, TMSG_INFO(pHead->msgType+1), code);
|
|
|
|
|
tDebug("%s %p %p, %s is sent with error code:0x%x", pRpc->label, pConn, (void *)pHead->ahandle,
|
|
|
|
|
TMSG_INFO(pHead->msgType + 1), code);
|
|
|
|
|
} else {
|
|
|
|
|
tError("%s %p %p, %s is sent with error code:0x%x", pRpc->label, pConn, (void *)pHead->ahandle, TMSG_INFO(pHead->msgType), code);
|
|
|
|
|
tError("%s %p %p, %s is sent with error code:0x%x", pRpc->label, pConn, (void *)pHead->ahandle,
|
|
|
|
|
TMSG_INFO(pHead->msgType), code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { // msg is passed to app only parsing is ok
|
|
|
|
@ -1144,8 +1340,7 @@ static void rpcNotifyClient(SRpcReqContext *pContext, SRpcMsg *pMsg) {
|
|
|
|
|
} else {
|
|
|
|
|
// for asynchronous API
|
|
|
|
|
SEpSet *pEpSet = NULL;
|
|
|
|
|
if (pContext->epSet.inUse != pContext->oldInUse || pContext->redirect)
|
|
|
|
|
pEpSet = &pContext->epSet;
|
|
|
|
|
if (pContext->epSet.inUse != pContext->oldInUse || pContext->redirect) pEpSet = &pContext->epSet;
|
|
|
|
|
|
|
|
|
|
(*pRpc->cfp)(pRpc->parent, pMsg, pEpSet);
|
|
|
|
|
}
|
|
|
|
@ -1155,7 +1350,6 @@ static void rpcNotifyClient(SRpcReqContext *pContext, SRpcMsg *pMsg) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rpcProcessIncomingMsg(SRpcConn *pConn, SRpcHead *pHead, SRpcReqContext *pContext) {
|
|
|
|
|
|
|
|
|
|
SRpcInfo *pRpc = pConn->pRpc;
|
|
|
|
|
SRpcMsg rpcMsg;
|
|
|
|
|
|
|
|
|
@ -1180,7 +1374,8 @@ static void rpcProcessIncomingMsg(SRpcConn *pConn, SRpcHead *pHead, SRpcReqConte
|
|
|
|
|
|
|
|
|
|
// for UDP, port may be changed by server, the port in epSet shall be used for cache
|
|
|
|
|
if (pHead->code != TSDB_CODE_RPC_TOO_SLOW) {
|
|
|
|
|
rpcAddConnIntoCache(pRpc->pCache, pConn, pConn->peerFqdn, pContext->epSet.port[pContext->epSet.inUse], pConn->connType);
|
|
|
|
|
rpcAddConnIntoCache(pRpc->pCache, pConn, pConn->peerFqdn, pContext->epSet.port[pContext->epSet.inUse],
|
|
|
|
|
pConn->connType);
|
|
|
|
|
} else {
|
|
|
|
|
rpcCloseConn(pConn);
|
|
|
|
|
}
|
|
|
|
@ -1200,7 +1395,8 @@ static void rpcProcessIncomingMsg(SRpcConn *pConn, SRpcHead *pHead, SRpcReqConte
|
|
|
|
|
}
|
|
|
|
|
rpcSendReqToServer(pRpc, pContext);
|
|
|
|
|
rpcFreeCont(rpcMsg.pCont);
|
|
|
|
|
} else if (pHead->code == TSDB_CODE_RPC_NOT_READY || pHead->code == TSDB_CODE_APP_NOT_READY || pHead->code == TSDB_CODE_DND_OFFLINE) {
|
|
|
|
|
} else if (pHead->code == TSDB_CODE_RPC_NOT_READY || pHead->code == TSDB_CODE_APP_NOT_READY ||
|
|
|
|
|
pHead->code == TSDB_CODE_DND_OFFLINE) {
|
|
|
|
|
pContext->code = pHead->code;
|
|
|
|
|
rpcProcessConnError(pContext, NULL);
|
|
|
|
|
rpcFreeCont(rpcMsg.pCont);
|
|
|
|
@ -1347,14 +1543,12 @@ static void rpcSendMsgToPeer(SRpcConn *pConn, void *msg, int msgLen) {
|
|
|
|
|
msgLen = rpcAddAuthPart(pConn, msg, msgLen);
|
|
|
|
|
|
|
|
|
|
if (rpcIsReq(pHead->msgType)) {
|
|
|
|
|
tDebug("%s, %s is sent to %s:%hu, len:%d sig:0x%08x:0x%08x:%d",
|
|
|
|
|
pConn->info, TMSG_INFO(pHead->msgType), pConn->peerFqdn, pConn->peerPort,
|
|
|
|
|
msgLen, pHead->sourceId, pHead->destId, pHead->tranId);
|
|
|
|
|
tDebug("%s, %s is sent to %s:%hu, len:%d sig:0x%08x:0x%08x:%d", pConn->info, TMSG_INFO(pHead->msgType),
|
|
|
|
|
pConn->peerFqdn, pConn->peerPort, msgLen, pHead->sourceId, pHead->destId, pHead->tranId);
|
|
|
|
|
} else {
|
|
|
|
|
if (pHead->code == 0) pConn->secured = 1; // for success response, set link as secured
|
|
|
|
|
tDebug("%s, %s is sent to 0x%x:%hu, code:0x%x len:%d sig:0x%08x:0x%08x:%d",
|
|
|
|
|
pConn->info, TMSG_INFO(pHead->msgType), pConn->peerIp, pConn->peerPort,
|
|
|
|
|
htonl(pHead->code), msgLen, pHead->sourceId, pHead->destId, pHead->tranId);
|
|
|
|
|
tDebug("%s, %s is sent to 0x%x:%hu, code:0x%x len:%d sig:0x%08x:0x%08x:%d", pConn->info, TMSG_INFO(pHead->msgType),
|
|
|
|
|
pConn->peerIp, pConn->peerPort, htonl(pHead->code), msgLen, pHead->sourceId, pHead->destId, pHead->tranId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// tTrace("connection type is: %d", pConn->connType);
|
|
|
|
@ -1411,7 +1605,8 @@ static void rpcProcessRetryTimer(void *param, void *tmrId) {
|
|
|
|
|
pConn->pTimer = taosTmrStart(rpcProcessRetryTimer, tsRpcTimer, pConn, pRpc->tmrCtrl);
|
|
|
|
|
} else {
|
|
|
|
|
// close the connection
|
|
|
|
|
tDebug("%s, failed to send msg:%s to %s:%hu", pConn->info, TMSG_INFO(pConn->outType), pConn->peerFqdn, pConn->peerPort);
|
|
|
|
|
tDebug("%s, failed to send msg:%s to %s:%hu", pConn->info, TMSG_INFO(pConn->outType), pConn->peerFqdn,
|
|
|
|
|
pConn->peerPort);
|
|
|
|
|
if (pConn->pContext) {
|
|
|
|
|
pConn->pContext->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
|
|
|
|
|
pConn->pContext->pConn = NULL;
|
|
|
|
@ -1593,8 +1788,8 @@ static int rpcCheckAuthentication(SRpcConn *pConn, char *msg, int msgLen) {
|
|
|
|
|
// for response, if code is auth failure, it shall bypass the auth process
|
|
|
|
|
code = htonl(pHead->code);
|
|
|
|
|
if (code == TSDB_CODE_RPC_INVALID_TIME_STAMP || code == TSDB_CODE_RPC_AUTH_FAILURE ||
|
|
|
|
|
code == TSDB_CODE_RPC_INVALID_VERSION ||
|
|
|
|
|
code == TSDB_CODE_RPC_AUTH_REQUIRED || code == TSDB_CODE_MND_USER_NOT_EXIST || code == TSDB_CODE_RPC_NOT_READY) {
|
|
|
|
|
code == TSDB_CODE_RPC_INVALID_VERSION || code == TSDB_CODE_RPC_AUTH_REQUIRED ||
|
|
|
|
|
code == TSDB_CODE_MND_USER_NOT_EXIST || code == TSDB_CODE_RPC_NOT_READY) {
|
|
|
|
|
pHead->msgLen = (int32_t)htonl((uint32_t)pHead->msgLen);
|
|
|
|
|
// tTrace("%s, dont check authentication since code is:0x%x", pConn->info, code);
|
|
|
|
|
return 0;
|
|
|
|
@ -1647,13 +1842,9 @@ static void rpcUnlockConn(SRpcConn *pConn) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rpcAddRef(SRpcInfo *pRpc)
|
|
|
|
|
{
|
|
|
|
|
atomic_add_fetch_32(&pRpc->refCount, 1);
|
|
|
|
|
}
|
|
|
|
|
static void rpcAddRef(SRpcInfo *pRpc) { atomic_add_fetch_32(&pRpc->refCount, 1); }
|
|
|
|
|
|
|
|
|
|
static void rpcDecRef(SRpcInfo *pRpc)
|
|
|
|
|
{
|
|
|
|
|
static void rpcDecRef(SRpcInfo *pRpc) {
|
|
|
|
|
if (atomic_sub_fetch_32(&pRpc->refCount, 1) == 0) {
|
|
|
|
|
rpcCloseConnCache(pRpc->pCache);
|
|
|
|
|
taosHashCleanup(pRpc->hash);
|
|
|
|
@ -1668,4 +1859,4 @@ static void rpcDecRef(SRpcInfo *pRpc)
|
|
|
|
|
atomic_sub_fetch_32(&tsRpcNum, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|