From 07e6caa32c7146b0d2d6fb074fc668ffbecb2b93 Mon Sep 17 00:00:00 2001 From: slguan Date: Sat, 22 Feb 2020 18:58:43 +0800 Subject: [PATCH] shell can connect to server --- src/client/inc/tsclient.h | 2 + src/client/src/tscServer.c | 2 - src/client/src/tscSql.c | 10 ++-- src/client/src/tscSystem.c | 92 ++++++++++++++++++++------------- src/kit/shell/src/shellEngine.c | 2 + src/kit/shell/src/shellMain.c | 6 +-- src/mnode/src/mgmtShell.c | 16 +++--- 7 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 5e58ebe574..7d3bcd9adb 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -371,6 +371,8 @@ typedef struct _sstream { struct _sstream *prev, *next; } SSqlStream; +int32_t tscInitRpc(const char *user, const char *secret); + // tscSql API int tsParseSql(SSqlObj *pSql, bool multiVnodeInsertion); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index b44750050c..704842506b 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -166,8 +166,6 @@ void tscProcessActivityTimer(void *handle, void *tmrId) { if (tscShouldFreeHeatBeat(pObj->pHb)) { tscTrace("%p free HB object and release connection", pObj); - //taosCloseRpcConn(pObj->pHb->thandle); - tscFreeSqlObj(pObj->pHb); tscCloseTscObj(pObj); return; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index c432fb177c..467a95a941 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -36,11 +36,6 @@ TAOS *taos_connect_imp(const char *ip, const char *user, const char *pass, const taos_init(); - if (pTscMgmtConn == NULL || pVnodeConn == NULL) { - globalCode = TSDB_CODE_APP_ERROR; - return NULL; - } - if (user == NULL) { globalCode = TSDB_CODE_INVALID_ACCT; return NULL; @@ -63,6 +58,11 @@ TAOS *taos_connect_imp(const char *ip, const char *user, const char *pass, const } } + if (tscInitRpc(user, pass) != 0) { + globalCode = TSDB_CODE_NETWORK_UNAVAIL; + return NULL; + } + if (ip && ip[0]) { tscMgmtIpList.index = 0; tscMgmtIpList.port = tsMgmtShellPort; diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index fadcc426fe..f805bfa4a8 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -45,6 +45,7 @@ int tsInsertHeadSize; extern int tscEmbedded; int tscNumOfThreads; static pthread_once_t tscinit = PTHREAD_ONCE_INIT; +static pthread_mutex_t tscMutex; extern int tsTscEnableRecordSql; extern int tsNumOfLogLines; @@ -56,11 +57,64 @@ void tscCheckDiskUsage(void *para, void *unused) { taosTmrReset(tscCheckDiskUsage, 1000, NULL, tscTmr, &tscCheckDiskUsageTmr); } +int32_t tscInitRpc(const char *user, const char *secret) { + SRpcInit rpcInit; + char secretEncrypt[32] = {0}; + taosEncryptPass((uint8_t *)secret, strlen(secret), secretEncrypt); + + pthread_mutex_lock(&tscMutex); + if (pVnodeConn == NULL) { + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.localIp = tsLocalIp; + rpcInit.localPort = 0; + rpcInit.label = "TSC-vnode"; + rpcInit.numOfThreads = tscNumOfThreads; + rpcInit.cfp = tscProcessMsgFromServer; + rpcInit.sessions = tsMaxVnodeConnections; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.user = user; + rpcInit.ckey = "key"; + rpcInit.secret = secretEncrypt; + + pVnodeConn = rpcOpen(&rpcInit); + if (pVnodeConn == NULL) { + tscError("failed to init connection to vnode"); + pthread_mutex_unlock(&tscMutex); + return -1; + } + } + + if (pTscMgmtConn == NULL) { + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.localIp = tsLocalIp; + rpcInit.localPort = 0; + rpcInit.label = "TSC-mgmt"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = tscProcessMsgFromServer; + rpcInit.sessions = tsMaxMgmtConnections; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.idleTime = 2000; + rpcInit.user = "root"; + rpcInit.ckey = "key"; + rpcInit.secret = secretEncrypt; + + pTscMgmtConn = rpcOpen(&rpcInit); + if (pTscMgmtConn == NULL) { + tscError("failed to init connection to mgmt"); + pthread_mutex_unlock(&tscMutex); + return -1; + } + } + + pthread_mutex_unlock(&tscMutex); + return 0; +} + void taos_init_imp() { char temp[128]; struct stat dirstat; - SRpcInit rpcInit; + pthread_mutex_init(&tscMutex, NULL); srand(taosGetTimestampSec()); deltaToUtcInitOnce(); @@ -124,42 +178,6 @@ void taos_init_imp() { return; } - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localIp = "0.0.0.0";//tsLocalIp; - rpcInit.localPort = 0; - rpcInit.label = "TSC-vnode"; - rpcInit.numOfThreads = tscNumOfThreads; - rpcInit.cfp = tscProcessMsgFromServer; - rpcInit.sessions = tsMaxVnodeConnections; - rpcInit.connType = TAOS_CONN_CLIENT; - pVnodeConn = rpcOpen(&rpcInit); - if (pVnodeConn == NULL) { - tscError("failed to init connection to vnode"); - return; - } - - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localIp = "0.0.0.0";//tsLocalIp; - rpcInit.localPort = 0; - rpcInit.label = "TSC-mgmt"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = tscProcessMsgFromServer; - rpcInit.sessions = tsMaxMgmtConnections; - rpcInit.connType = TAOS_CONN_CLIENT; - rpcInit.idleTime = 2000; - rpcInit.user = "root"; - rpcInit.ckey = "key"; - - char secret[32] = {0}; - taosEncryptPass((uint8_t *)"taosdata", strlen("taosdata"), secret); - rpcInit.secret = secret; - - pTscMgmtConn = rpcOpen(&rpcInit); - if (pTscMgmtConn == NULL) { - tscError("failed to init connection to mgmt"); - return; - } - tscTmr = taosTmrInit(tsMaxMgmtConnections * 2, 200, 60000, "TSC"); if(0 == tscEmbedded){ taosTmrReset(tscCheckDiskUsage, 10, NULL, tscTmr, &tscCheckDiskUsageTmr); diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index 27a4aaaa0c..22af4ca5c3 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -800,6 +800,8 @@ void source_file(TAOS *con, char *fptr) { } void shellGetGrantInfo(void *con) { + return; + char sql[] = "show grants"; int code = taos_query(con, sql); diff --git a/src/kit/shell/src/shellMain.c b/src/kit/shell/src/shellMain.c index 5083eb8ee1..8c060295e1 100644 --- a/src/kit/shell/src/shellMain.c +++ b/src/kit/shell/src/shellMain.c @@ -81,11 +81,7 @@ struct arguments args = { */ int main(int argc, char* argv[]) { /*setlocale(LC_ALL, "en_US.UTF-8"); */ - // - void *taos = taos_connect(NULL, "root", "taosdata", NULL, 0); - printf("ok\n"); - // - + if (!checkVersion()) { exit(EXIT_FAILURE); } diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 6b80421ceb..1cc7892f38 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -1230,20 +1230,18 @@ int32_t mgmtProcessHeartBeatMsg(void *pCont, int32_t contLen, void *ahandle) { } int32_t mgmtRetriveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { + *spi = 0; + *encrypt = 0; + *ckey = 0; + SUserObj *pUser = mgmtGetUser(user); if (pUser == NULL) { - *spi = 0; - *encrypt = 0; - *ckey = 0; *secret = 0; return TSDB_CODE_INVALID_USER; + } else { + memcpy(secret, pUser->pass, TSDB_KEY_LEN); + return TSDB_CODE_SUCCESS; } - - *spi = 0; - *encrypt = 0; - *ckey = 0; - memcpy(secret, pUser->pass, TSDB_KEY_LEN); - return TSDB_CODE_SUCCESS; } static int32_t mgmtProcessConnectMsg(void *pCont, int32_t contLen, void *thandle) { -- GitLab