提交 db061c3d 编写于 作者: dengyihao's avatar dengyihao

test

上级 332cdd34
...@@ -307,6 +307,7 @@ typedef struct STscObj { ...@@ -307,6 +307,7 @@ typedef struct STscObj {
struct SSqlStream *streamList; struct SSqlStream *streamList;
SRpcCorEpSet *tscCorMgmtEpSet; SRpcCorEpSet *tscCorMgmtEpSet;
void* pDnodeConn; void* pDnodeConn;
void* pRpcObj;
pthread_mutex_t mutex; pthread_mutex_t mutex;
int32_t numOfObj; // number of sqlObj from this tscObj int32_t numOfObj; // number of sqlObj from this tscObj
} STscObj; } STscObj;
...@@ -377,7 +378,15 @@ typedef struct SSqlStream { ...@@ -377,7 +378,15 @@ typedef struct SSqlStream {
void tscSetStreamDestTable(SSqlStream* pStream, const char* dstTable); void tscSetStreamDestTable(SSqlStream* pStream, const char* dstTable);
int32_t tscInitRpc(const char *user, const char *secret, void** pDnodeConn); typedef struct {
char key[512];
void *pDnodeConn;
} SRpcObj;
void *tscAcquireRpc(const char *insKey);
void tscReleaseRpc(void *param);
int32_t tscGetRpcIns(const char *insKey, const char *user, const char *secretEncrypt, void **ppRpcObj, void **pDnodeConn);
//int32_t tscInitRpc(const char *user, const char *secret, void** pDnodeConn);
void tscInitMsgsFp(); void tscInitMsgsFp();
int tsParseSql(SSqlObj *pSql, bool initial); int tsParseSql(SSqlObj *pSql, bool initial);
......
...@@ -91,8 +91,12 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa ...@@ -91,8 +91,12 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa
if (tscSetMgmtEpSetFromCfg(tsFirst, tsSecond, &corMgmtEpSet) < 0) return NULL; if (tscSetMgmtEpSetFromCfg(tsFirst, tsSecond, &corMgmtEpSet) < 0) return NULL;
} }
char rpcInskey[512] = {0};
sprintf(rpcInskey, "%s:%s:%s:%d", user, pass, ip, port);
void *pRpcObj = NULL;
void *pDnodeConn = NULL; void *pDnodeConn = NULL;
if (tscInitRpc(user, secretEncrypt, &pDnodeConn) != 0) { if (tscGetRpcIns(rpcInskey, user, secretEncrypt, &pRpcObj, &pDnodeConn) != 0) {
terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL; terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
return NULL; return NULL;
} }
...@@ -107,14 +111,15 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa ...@@ -107,14 +111,15 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa
pObj->tscCorMgmtEpSet = (SRpcCorEpSet *)malloc(sizeof(SRpcCorEpSet)); pObj->tscCorMgmtEpSet = (SRpcCorEpSet *)malloc(sizeof(SRpcCorEpSet));
if (NULL == pObj->tscCorMgmtEpSet) { if (NULL == pObj->tscCorMgmtEpSet) {
terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
rpcClose(pDnodeConn); tscReleaseRpc(pRpcObj);
free(pObj->tscCorMgmtEpSet); free(pObj->tscCorMgmtEpSet);
free(pObj); free(pObj);
} }
memcpy(pObj->tscCorMgmtEpSet, &corMgmtEpSet, sizeof(SRpcCorEpSet)); memcpy(pObj->tscCorMgmtEpSet, &corMgmtEpSet, sizeof(SRpcCorEpSet));
pObj->signature = pObj; pObj->signature = pObj;
pObj->pDnodeConn = pDnodeConn; pObj->pRpcObj = pRpcObj;
pObj->pDnodeConn = pDnodeConn;
tstrncpy(pObj->user, user, sizeof(pObj->user)); tstrncpy(pObj->user, user, sizeof(pObj->user));
secretEncryptLen = MIN(secretEncryptLen, sizeof(pObj->pass)); secretEncryptLen = MIN(secretEncryptLen, sizeof(pObj->pass));
...@@ -125,7 +130,7 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa ...@@ -125,7 +130,7 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa
/* db name is too long */ /* db name is too long */
if (len >= TSDB_DB_NAME_LEN) { if (len >= TSDB_DB_NAME_LEN) {
terrno = TSDB_CODE_TSC_INVALID_DB_LENGTH; terrno = TSDB_CODE_TSC_INVALID_DB_LENGTH;
rpcClose(pDnodeConn); tscReleaseRpc(pRpcObj);
free(pObj->tscCorMgmtEpSet); free(pObj->tscCorMgmtEpSet);
free(pObj); free(pObj);
return NULL; return NULL;
...@@ -143,7 +148,7 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa ...@@ -143,7 +148,7 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa
SSqlObj *pSql = (SSqlObj *)calloc(1, sizeof(SSqlObj)); SSqlObj *pSql = (SSqlObj *)calloc(1, sizeof(SSqlObj));
if (NULL == pSql) { if (NULL == pSql) {
terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
rpcClose(pDnodeConn); tscReleaseRpc(pRpcObj);
free(pObj->tscCorMgmtEpSet); free(pObj->tscCorMgmtEpSet);
free(pObj); free(pObj);
return NULL; return NULL;
...@@ -160,7 +165,7 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa ...@@ -160,7 +165,7 @@ static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pa
if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) {
terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
rpcClose(pDnodeConn); tscReleaseRpc(pRpcObj);
free(pSql); free(pSql);
free(pObj->tscCorMgmtEpSet); free(pObj->tscCorMgmtEpSet);
free(pObj); free(pObj);
......
...@@ -39,7 +39,8 @@ void *tscQhandle; ...@@ -39,7 +39,8 @@ void *tscQhandle;
void *tscCheckDiskUsageTmr; void *tscCheckDiskUsageTmr;
int tscRefId = -1; int tscRefId = -1;
int tscNumOfObj = 0; // number of sqlObj in current process. int tscNumOfObj = 0; // number of sqlObj in current process.
void *tscRpcCache = NULL;
pthread_mutex_t rpcObjMutex;
static pthread_once_t tscinit = PTHREAD_ONCE_INIT; static pthread_once_t tscinit = PTHREAD_ONCE_INIT;
void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) { void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) {
...@@ -47,32 +48,72 @@ void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) { ...@@ -47,32 +48,72 @@ void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) {
taosTmrReset(tscCheckDiskUsage, 1000, NULL, tscTmr, &tscCheckDiskUsageTmr); taosTmrReset(tscCheckDiskUsage, 1000, NULL, tscTmr, &tscCheckDiskUsageTmr);
} }
int32_t tscInitRpc(const char *user, const char *secretEncrypt, void **pDnodeConn) { void tscFreeRpcObj(void *param) {
SRpcInit rpcInit; assert(param);
SRpcObj *pRpcObj = (SRpcObj *)(param);
rpcClose(pRpcObj->pDnodeConn);
}
if (*pDnodeConn == NULL) { void *tscAcquireRpc(const char *insKey) {
memset(&rpcInit, 0, sizeof(rpcInit)); SRpcObj *pRpcObj = taosCacheAcquireByKey(tscRpcCache, insKey, strlen(insKey));
rpcInit.localPort = 0; if (pRpcObj == NULL) {
rpcInit.label = "TSC"; return NULL;
rpcInit.numOfThreads = 1; // every DB connection has only one thread }
rpcInit.cfp = tscProcessMsgFromServer; return pRpcObj;
rpcInit.sessions = tsMaxConnections; }
rpcInit.connType = TAOS_CONN_CLIENT;
rpcInit.user = (char *)user; void tscReleaseRpc(void *param) {
rpcInit.idleTime = 2000; if (param == NULL) {
rpcInit.ckey = "key"; return;
rpcInit.spi = 1;
rpcInit.secret = (char *)secretEncrypt;
*pDnodeConn = rpcOpen(&rpcInit);
if (*pDnodeConn == NULL) {
tscError("failed to init connection to TDengine");
return -1;
} else {
tscDebug("dnodeConn:%p is created, user:%s", *pDnodeConn, user);
}
} }
pthread_mutex_lock(&rpcObjMutex);
taosCacheRelease(tscRpcCache, (void **)&param, false);
pthread_mutex_unlock(&rpcObjMutex);
}
int32_t tscGetRpcIns(const char *insKey, const char *user, const char *secretEncrypt, void **ppRpcObj, void **pDnodeConn) {
pthread_mutex_lock(&rpcObjMutex);
SRpcObj *pRpcObj = (SRpcObj *)tscAcquireRpc(insKey);
if (pRpcObj != NULL) {
*ppRpcObj = pRpcObj;
*pDnodeConn = pRpcObj->pDnodeConn;
pthread_mutex_unlock(&rpcObjMutex);
return 0;
}
SRpcInit rpcInit;
memset(&rpcInit, 0, sizeof(rpcInit));
rpcInit.localPort = 0;
rpcInit.label = "TSC";
rpcInit.numOfThreads = 1;
rpcInit.cfp = tscProcessMsgFromServer;
rpcInit.sessions = tsMaxConnections;
rpcInit.connType = TAOS_CONN_CLIENT;
rpcInit.user = (char *)user;
rpcInit.idleTime = 2000;
rpcInit.ckey = "key";
rpcInit.spi = 1;
rpcInit.secret = (char *)secretEncrypt;
SRpcObj rpcObj;
memset(&rpcObj, 0, sizeof(rpcObj));
strncpy(rpcObj.key, insKey, strlen(insKey));
rpcObj.pDnodeConn = rpcOpen(&rpcInit);
if (rpcObj.pDnodeConn == NULL) {
pthread_mutex_unlock(&rpcObjMutex);
tscError("failed to init connection to TDengine");
return -1;
}
pRpcObj = taosCachePut(tscRpcCache, rpcObj.key, strlen(rpcObj.key), &rpcObj, sizeof(rpcObj), 1000*10);
if (pRpcObj == NULL) {
rpcClose(rpcObj.pDnodeConn);
pthread_mutex_unlock(&rpcObjMutex);
return -1;
}
*ppRpcObj = pRpcObj;
*pDnodeConn = pRpcObj->pDnodeConn;
pthread_mutex_unlock(&rpcObjMutex);
return 0; return 0;
} }
...@@ -135,7 +176,8 @@ void taos_init_imp(void) { ...@@ -135,7 +176,8 @@ void taos_init_imp(void) {
tscObjRef = taosOpenRef(40960, tscFreeRegisteredSqlObj); tscObjRef = taosOpenRef(40960, tscFreeRegisteredSqlObj);
tscHashMap = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); tscHashMap = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
} }
tscRpcCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, refreshTime, true, tscFreeRpcObj, "rpcObj");
pthread_mutex_init(&rpcObjMutex, NULL);
tscRefId = taosOpenRef(200, tscCloseTscObj); tscRefId = taosOpenRef(200, tscCloseTscObj);
// in other language APIs, taos_cleanup is not available yet. // in other language APIs, taos_cleanup is not available yet.
...@@ -169,7 +211,21 @@ void taos_cleanup(void) { ...@@ -169,7 +211,21 @@ void taos_cleanup(void) {
taosCloseRef(tscRefId); taosCloseRef(tscRefId);
taosCleanupKeywordsTable(); taosCleanupKeywordsTable();
taosCloseLog(); taosCloseLog();
m = tscRpcCache;
if (m != NULL && atomic_val_compare_exchange_ptr(&tscRpcCache, m, 0) == m) {
pthread_mutex_lock(&rpcObjMutex);
taosCacheCleanup(tscRpcCache);
tscRpcCache = NULL;
pthread_mutex_unlock(&rpcObjMutex);
pthread_mutex_destroy(&rpcObjMutex);
}
if (tscEmbedded == 0) rpcCleanup(); if (tscEmbedded == 0) rpcCleanup();
m = tscTmr;
if (m != NULL && atomic_val_compare_exchange_ptr(&tscTmr, m, 0) == m) {
taosTmrCleanUp(m);
}
m = tscTmr; m = tscTmr;
if (m != NULL && atomic_val_compare_exchange_ptr(&tscTmr, m, 0) == m) { if (m != NULL && atomic_val_compare_exchange_ptr(&tscTmr, m, 0) == m) {
......
...@@ -895,15 +895,10 @@ void tscCloseTscObj(void *param) { ...@@ -895,15 +895,10 @@ void tscCloseTscObj(void *param) {
pObj->signature = NULL; pObj->signature = NULL;
taosTmrStopA(&(pObj->pTimer)); taosTmrStopA(&(pObj->pTimer));
void* p = pObj->pDnodeConn; tscReleaseRpc(pObj->pRpcObj);
if (pObj->pDnodeConn != NULL) {
rpcClose(pObj->pDnodeConn);
pObj->pDnodeConn = NULL;
}
tfree(pObj->tscCorMgmtEpSet); tfree(pObj->tscCorMgmtEpSet);
pthread_mutex_destroy(&pObj->mutex); pthread_mutex_destroy(&pObj->mutex);
tscDebug("%p DB connection is closed, dnodeConn:%p", pObj, p);
tfree(pObj); tfree(pObj);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册