提交 54cd817f 编写于 作者: 陶建辉(Jeff)'s avatar 陶建辉(Jeff)

fix many bugs

上级 e9470f0a
...@@ -20,10 +20,10 @@ ...@@ -20,10 +20,10 @@
extern "C" { extern "C" {
#endif #endif
void *taosOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl, int64_t keepTimer); void *rpcOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl, int64_t keepTimer);
void taosCloseConnCache(void *handle); void rpcCloseConnCache(void *handle);
void taosAddConnIntoCache(void *handle, void *data, uint32_t ip, uint16_t port, char *user); void rpcAddConnIntoCache(void *handle, void *data, uint32_t ip, uint16_t port, char *user);
void *taosGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user); void *rpcGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -40,13 +40,13 @@ typedef struct { ...@@ -40,13 +40,13 @@ typedef struct {
int * count; int * count;
int64_t keepTimer; int64_t keepTimer;
pthread_mutex_t mutex; pthread_mutex_t mutex;
void (*cleanFp)(void *); void (*cleanFp)(void *);
void *tmrCtrl; void *tmrCtrl;
void *pTimer; void *pTimer;
} SConnCache; } SConnCache;
int taosHashConn(void *handle, uint32_t ip, uint16_t port, char *user) { int rpcHashConn(void *handle, uint32_t ip, uint16_t port, char *user) {
SConnCache *pObj = (SConnCache *)handle; SConnCache *pCache = (SConnCache *)handle;
int hash = 0; int hash = 0;
// size_t user_len = strlen(user); // size_t user_len = strlen(user);
...@@ -58,109 +58,109 @@ int taosHashConn(void *handle, uint32_t ip, uint16_t port, char *user) { ...@@ -58,109 +58,109 @@ int taosHashConn(void *handle, uint32_t ip, uint16_t port, char *user) {
user++; user++;
} }
hash = hash % pObj->maxSessions; hash = hash % pCache->maxSessions;
return hash; return hash;
} }
void taosRemoveExpiredNodes(SConnCache *pObj, SConnHash *pNode, int hash, uint64_t time) { void rpcRemoveExpiredNodes(SConnCache *pCache, SConnHash *pNode, int hash, uint64_t time) {
if (time < pObj->keepTimer + pNode->time) return; if (pNode == NULL || (time < pCache->keepTimer + pNode->time) ) return;
SConnHash *pPrev = pNode->prev, *pNext; SConnHash *pPrev = pNode->prev, *pNext;
while (pNode) { while (pNode) {
(*pObj->cleanFp)(pNode->data); (*pCache->cleanFp)(pNode->data);
pNext = pNode->next; pNext = pNode->next;
pObj->total--; pCache->total--;
pObj->count[hash]--; pCache->count[hash]--;
tscTrace("%p ip:0x%x:%hu:%d:%p removed, connections in cache:%d", pNode->data, pNode->ip, pNode->port, hash, pNode, tTrace("%p ip:0x%x:%hu:%d:%p removed, connections in cache:%d", pNode->data, pNode->ip, pNode->port, hash, pNode,
pObj->count[hash]); pCache->count[hash]);
taosMemPoolFree(pObj->connHashMemPool, (char *)pNode); taosMemPoolFree(pCache->connHashMemPool, (char *)pNode);
pNode = pNext; pNode = pNext;
} }
if (pPrev) if (pPrev)
pPrev->next = NULL; pPrev->next = NULL;
else else
pObj->connHashList[hash] = NULL; pCache->connHashList[hash] = NULL;
} }
void taosAddConnIntoCache(void *handle, void *data, uint32_t ip, uint16_t port, char *user) { void rpcAddConnIntoCache(void *handle, void *data, uint32_t ip, uint16_t port, char *user) {
int hash; int hash;
SConnHash * pNode; SConnHash * pNode;
SConnCache *pObj; SConnCache *pCache;
uint64_t time = taosGetTimestampMs(); uint64_t time = taosGetTimestampMs();
pObj = (SConnCache *)handle; pCache = (SConnCache *)handle;
assert(pObj); assert(pCache);
assert(data); assert(data);
hash = taosHashConn(pObj, ip, port, user); hash = rpcHashConn(pCache, ip, port, user);
pNode = (SConnHash *)taosMemPoolMalloc(pObj->connHashMemPool); pNode = (SConnHash *)taosMemPoolMalloc(pCache->connHashMemPool);
pNode->ip = ip; pNode->ip = ip;
pNode->port = port; pNode->port = port;
pNode->data = data; pNode->data = data;
pNode->prev = NULL; pNode->prev = NULL;
pNode->time = time; pNode->time = time;
pthread_mutex_lock(&pObj->mutex); pthread_mutex_lock(&pCache->mutex);
pNode->next = pObj->connHashList[hash]; pNode->next = pCache->connHashList[hash];
if (pObj->connHashList[hash] != NULL) (pObj->connHashList[hash])->prev = pNode; if (pCache->connHashList[hash] != NULL) (pCache->connHashList[hash])->prev = pNode;
pObj->connHashList[hash] = pNode; pCache->connHashList[hash] = pNode;
pObj->total++; pCache->total++;
pObj->count[hash]++; pCache->count[hash]++;
taosRemoveExpiredNodes(pObj, pNode->next, hash, time); rpcRemoveExpiredNodes(pCache, pNode->next, hash, time);
pthread_mutex_unlock(&pObj->mutex); pthread_mutex_unlock(&pCache->mutex);
tscTrace("%p ip:0x%x:%hu:%d:%p added, connections in cache:%d", data, ip, port, hash, pNode, pObj->count[hash]); tTrace("%p ip:0x%x:%hu:%d:%p added, connections in cache:%d", data, ip, port, hash, pNode, pCache->count[hash]);
return; return;
} }
void taosCleanConnCache(void *handle, void *tmrId) { void rpcCleanConnCache(void *handle, void *tmrId) {
int hash; int hash;
SConnHash * pNode; SConnHash * pNode;
SConnCache *pObj; SConnCache *pCache;
pObj = (SConnCache *)handle; pCache = (SConnCache *)handle;
if (pObj == NULL || pObj->maxSessions == 0) return; if (pCache == NULL || pCache->maxSessions == 0) return;
if (pObj->pTimer != tmrId) return; if (pCache->pTimer != tmrId) return;
uint64_t time = taosGetTimestampMs(); uint64_t time = taosGetTimestampMs();
for (hash = 0; hash < pObj->maxSessions; ++hash) { for (hash = 0; hash < pCache->maxSessions; ++hash) {
pthread_mutex_lock(&pObj->mutex); pthread_mutex_lock(&pCache->mutex);
pNode = pObj->connHashList[hash]; pNode = pCache->connHashList[hash];
taosRemoveExpiredNodes(pObj, pNode, hash, time); rpcRemoveExpiredNodes(pCache, pNode, hash, time);
pthread_mutex_unlock(&pObj->mutex); pthread_mutex_unlock(&pCache->mutex);
} }
// tscTrace("timer, total connections in cache:%d", pObj->total); // tTrace("timer, total connections in cache:%d", pCache->total);
taosTmrReset(taosCleanConnCache, pObj->keepTimer * 2, pObj, pObj->tmrCtrl, &pObj->pTimer); taosTmrReset(rpcCleanConnCache, pCache->keepTimer * 2, pCache, pCache->tmrCtrl, &pCache->pTimer);
} }
void *taosGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user) { void *rpcGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user) {
int hash; int hash;
SConnHash * pNode; SConnHash * pNode;
SConnCache *pObj; SConnCache *pCache;
void * pData = NULL; void * pData = NULL;
pObj = (SConnCache *)handle; pCache = (SConnCache *)handle;
assert(pObj); assert(pCache);
uint64_t time = taosGetTimestampMs(); uint64_t time = taosGetTimestampMs();
hash = taosHashConn(pObj, ip, port, user); hash = rpcHashConn(pCache, ip, port, user);
pthread_mutex_lock(&pObj->mutex); pthread_mutex_lock(&pCache->mutex);
pNode = pObj->connHashList[hash]; pNode = pCache->connHashList[hash];
while (pNode) { while (pNode) {
if (time >= pObj->keepTimer + pNode->time) { if (time >= pCache->keepTimer + pNode->time) {
taosRemoveExpiredNodes(pObj, pNode, hash, time); rpcRemoveExpiredNodes(pCache, pNode, hash, time);
pNode = NULL; pNode = NULL;
break; break;
} }
...@@ -171,12 +171,12 @@ void *taosGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user) ...@@ -171,12 +171,12 @@ void *taosGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user)
} }
if (pNode) { if (pNode) {
taosRemoveExpiredNodes(pObj, pNode->next, hash, time); rpcRemoveExpiredNodes(pCache, pNode->next, hash, time);
if (pNode->prev) { if (pNode->prev) {
pNode->prev->next = pNode->next; pNode->prev->next = pNode->next;
} else { } else {
pObj->connHashList[hash] = pNode->next; pCache->connHashList[hash] = pNode->next;
} }
if (pNode->next) { if (pNode->next) {
...@@ -184,24 +184,24 @@ void *taosGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user) ...@@ -184,24 +184,24 @@ void *taosGetConnFromCache(void *handle, uint32_t ip, uint16_t port, char *user)
} }
pData = pNode->data; pData = pNode->data;
taosMemPoolFree(pObj->connHashMemPool, (char *)pNode); taosMemPoolFree(pCache->connHashMemPool, (char *)pNode);
pObj->total--; pCache->total--;
pObj->count[hash]--; pCache->count[hash]--;
} }
pthread_mutex_unlock(&pObj->mutex); pthread_mutex_unlock(&pCache->mutex);
if (pData) { if (pData) {
tscTrace("%p ip:0x%x:%hu:%d:%p retrieved, connections in cache:%d", pData, ip, port, hash, pNode, pObj->count[hash]); tTrace("%p ip:0x%x:%hu:%d:%p retrieved, connections in cache:%d", pData, ip, port, hash, pNode, pCache->count[hash]);
} }
return pData; return pData;
} }
void *taosOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl, int64_t keepTimer) { void *rpcOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl, int64_t keepTimer) {
SConnHash **connHashList; SConnHash **connHashList;
mpool_h connHashMemPool; mpool_h connHashMemPool;
SConnCache *pObj; SConnCache *pCache;
connHashMemPool = taosMemPoolInit(maxSessions, sizeof(SConnHash)); connHashMemPool = taosMemPoolInit(maxSessions, sizeof(SConnHash));
if (connHashMemPool == 0) return NULL; if (connHashMemPool == 0) return NULL;
...@@ -212,48 +212,48 @@ void *taosOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl, ...@@ -212,48 +212,48 @@ void *taosOpenConnCache(int maxSessions, void (*cleanFp)(void *), void *tmrCtrl,
return NULL; return NULL;
} }
pObj = malloc(sizeof(SConnCache)); pCache = malloc(sizeof(SConnCache));
if (pObj == NULL) { if (pCache == NULL) {
taosMemPoolCleanUp(connHashMemPool); taosMemPoolCleanUp(connHashMemPool);
free(connHashList); free(connHashList);
return NULL; return NULL;
} }
memset(pObj, 0, sizeof(SConnCache)); memset(pCache, 0, sizeof(SConnCache));
pObj->count = calloc(sizeof(int), maxSessions); pCache->count = calloc(sizeof(int), maxSessions);
pObj->total = 0; pCache->total = 0;
pObj->keepTimer = keepTimer; pCache->keepTimer = keepTimer;
pObj->maxSessions = maxSessions; pCache->maxSessions = maxSessions;
pObj->connHashMemPool = connHashMemPool; pCache->connHashMemPool = connHashMemPool;
pObj->connHashList = connHashList; pCache->connHashList = connHashList;
pObj->cleanFp = cleanFp; pCache->cleanFp = cleanFp;
pObj->tmrCtrl = tmrCtrl; pCache->tmrCtrl = tmrCtrl;
taosTmrReset(taosCleanConnCache, pObj->keepTimer * 2, pObj, pObj->tmrCtrl, &pObj->pTimer); taosTmrReset(rpcCleanConnCache, pCache->keepTimer * 2, pCache, pCache->tmrCtrl, &pCache->pTimer);
pthread_mutex_init(&pObj->mutex, NULL); pthread_mutex_init(&pCache->mutex, NULL);
return pObj; return pCache;
} }
void taosCloseConnCache(void *handle) { void rpcCloseConnCache(void *handle) {
SConnCache *pObj; SConnCache *pCache;
pObj = (SConnCache *)handle; pCache = (SConnCache *)handle;
if (pObj == NULL || pObj->maxSessions == 0) return; if (pCache == NULL || pCache->maxSessions == 0) return;
pthread_mutex_lock(&pObj->mutex); pthread_mutex_lock(&pCache->mutex);
taosTmrStopA(&(pObj->pTimer)); taosTmrStopA(&(pCache->pTimer));
if (pObj->connHashMemPool) taosMemPoolCleanUp(pObj->connHashMemPool); if (pCache->connHashMemPool) taosMemPoolCleanUp(pCache->connHashMemPool);
tfree(pObj->connHashList); tfree(pCache->connHashList);
tfree(pObj->count) tfree(pCache->count)
pthread_mutex_unlock(&pObj->mutex); pthread_mutex_unlock(&pCache->mutex);
pthread_mutex_destroy(&pObj->mutex); pthread_mutex_destroy(&pCache->mutex);
memset(pObj, 0, sizeof(SConnCache)); memset(pCache, 0, sizeof(SConnCache));
free(pObj); free(pCache);
} }
此差异已折叠。
...@@ -24,18 +24,23 @@ void processMsg(char type, void *pCont, int contLen, void *ahandle, int32_t code ...@@ -24,18 +24,23 @@ void processMsg(char type, void *pCont, int contLen, void *ahandle, int32_t code
} }
int32_t main(int32_t argc, char *argv[]) { int32_t main(int32_t argc, char *argv[]) {
taosInitLog("client.log", 100000, 10);
dPrint("unit test for rpc module"); dPrint("unit test for rpc module");
SRpcInit rpcInit; SRpcInit rpcInit;
memset(&rpcInit, 0, sizeof(rpcInit)); memset(&rpcInit, 0, sizeof(rpcInit));
rpcInit.localIp = "0.0.0.0"; rpcInit.localIp = "0.0.0.0";
rpcInit.localPort = 7000; rpcInit.localPort = 0;
rpcInit.label = "unittest"; rpcInit.label = "APP";
rpcInit.numOfThreads = 1; rpcInit.numOfThreads = 1;
rpcInit.cfp = processMsg; rpcInit.cfp = processMsg;
rpcInit.sessions = 1000; rpcInit.sessions = 1000;
rpcInit.connType = TAOS_CONN_UDPC; rpcInit.connType = TAOS_CONN_UDPC;
rpcInit.idleTime = 2000; rpcInit.idleTime = 2000;
rpcInit.meterId = "jefftao";
rpcInit.secret = "password";
rpcInit.ckey = "key";
void *pRpc = rpcOpen(&rpcInit); void *pRpc = rpcOpen(&rpcInit);
if (pRpc == NULL) { if (pRpc == NULL) {
...@@ -46,6 +51,7 @@ int32_t main(int32_t argc, char *argv[]) { ...@@ -46,6 +51,7 @@ int32_t main(int32_t argc, char *argv[]) {
SRpcIpSet ipSet; SRpcIpSet ipSet;
ipSet.numOfIps = 2; ipSet.numOfIps = 2;
ipSet.index = 0; ipSet.index = 0;
ipSet.port = 7000;
ipSet.ip[0] = inet_addr("127.0.0.1"); ipSet.ip[0] = inet_addr("127.0.0.1");
ipSet.ip[1] = inet_addr("192.168.0.1"); ipSet.ip[1] = inet_addr("192.168.0.1");
......
...@@ -27,18 +27,23 @@ void processMsg(char type, void *pCont, int contLen, void *ahandle, int32_t code ...@@ -27,18 +27,23 @@ void processMsg(char type, void *pCont, int contLen, void *ahandle, int32_t code
} }
int32_t main(int32_t argc, char *argv[]) { int32_t main(int32_t argc, char *argv[]) {
taosInitLog("server.log", 100000, 10);
dPrint("unit test for rpc module"); dPrint("unit test for rpc module");
SRpcInit rpcInit; SRpcInit rpcInit;
memset(&rpcInit, 0, sizeof(rpcInit)); memset(&rpcInit, 0, sizeof(rpcInit));
rpcInit.localIp = "0.0.0.0"; rpcInit.localIp = "0.0.0.0";
rpcInit.localPort = 7000; rpcInit.localPort = 7000;
rpcInit.label = "unittest"; rpcInit.label = "APP";
rpcInit.numOfThreads = 1; rpcInit.numOfThreads = 1;
rpcInit.cfp = processMsg; rpcInit.cfp = processMsg;
rpcInit.sessions = 1000; rpcInit.sessions = 1000;
rpcInit.connType = TAOS_CONN_UDPS; rpcInit.connType = TAOS_CONN_UDPS;
rpcInit.idleTime = 2000; rpcInit.idleTime = 2000;
rpcInit.meterId = "jefftao";
rpcInit.secret = "password";
rpcInit.ckey = "key";
void *pRpc = rpcOpen(&rpcInit); void *pRpc = rpcOpen(&rpcInit);
if (pRpc == NULL) { if (pRpc == NULL) {
......
...@@ -209,7 +209,7 @@ char tsLocale[TSDB_LOCALE_LEN] = {0}; ...@@ -209,7 +209,7 @@ char tsLocale[TSDB_LOCALE_LEN] = {0};
char tsCharset[TSDB_LOCALE_LEN] = {0}; // default encode string char tsCharset[TSDB_LOCALE_LEN] = {0}; // default encode string
int tsNumOfLogLines = 10000000; int tsNumOfLogLines = 10000000;
uint32_t rpcDebugFlag = 131; uint32_t rpcDebugFlag = 135;
uint32_t ddebugFlag = 131; uint32_t ddebugFlag = 131;
uint32_t mdebugFlag = 135; uint32_t mdebugFlag = 135;
uint32_t sdbDebugFlag = 135; uint32_t sdbDebugFlag = 135;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册