Merge remote-tracking branch 'origin/3.0' into feature/qnode
This commit is contained in:
commit
c3102ecc07
|
@ -82,7 +82,9 @@ class FstReadMemory {
|
|||
bool init() {
|
||||
char* buf = (char*)calloc(1, sizeof(char) * _size);
|
||||
int nRead = fstCountingWriterRead(_w, (uint8_t*)buf, _size);
|
||||
if (nRead <= 0) { return false; }
|
||||
if (nRead <= 0) {
|
||||
return false;
|
||||
}
|
||||
_size = nRead;
|
||||
_s = fstSliceCreate((uint8_t*)buf, _size);
|
||||
_fst = fstCreate(&_s);
|
||||
|
@ -108,7 +110,9 @@ class FstReadMemory {
|
|||
StreamWithState* st = streamBuilderIntoStream(sb);
|
||||
StreamWithStateResult* rt = NULL;
|
||||
|
||||
while ((rt = streamWithStateNextWith(st, NULL)) != NULL) { result.push_back((uint64_t)(rt->out.out)); }
|
||||
while ((rt = streamWithStateNextWith(st, NULL)) != NULL) {
|
||||
result.push_back((uint64_t)(rt->out.out));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool SearchWithTimeCostUs(AutomationCtx* ctx, std::vector<uint64_t>& result) {
|
||||
|
@ -184,7 +188,9 @@ void checkFstPerf() {
|
|||
delete fw;
|
||||
|
||||
FstReadMemory* m = new FstReadMemory(1024 * 64);
|
||||
if (m->init()) { printf("success to init fst read"); }
|
||||
if (m->init()) {
|
||||
printf("success to init fst read");
|
||||
}
|
||||
Performance_fstReadRecords(m);
|
||||
delete m;
|
||||
}
|
||||
|
@ -348,7 +354,9 @@ class TFileObj {
|
|||
tfileReaderDestroy(reader_);
|
||||
reader_ = NULL;
|
||||
}
|
||||
if (writer_ == NULL) { InitWriter(); }
|
||||
if (writer_ == NULL) {
|
||||
InitWriter();
|
||||
}
|
||||
return tfileWriterPut(writer_, tv, false);
|
||||
}
|
||||
bool InitWriter() {
|
||||
|
@ -388,8 +396,12 @@ class TFileObj {
|
|||
return tfileReaderSearch(reader_, query, result);
|
||||
}
|
||||
~TFileObj() {
|
||||
if (writer_) { tfileWriterDestroy(writer_); }
|
||||
if (reader_) { tfileReaderDestroy(reader_); }
|
||||
if (writer_) {
|
||||
tfileWriterDestroy(writer_);
|
||||
}
|
||||
if (reader_) {
|
||||
tfileReaderDestroy(reader_);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -912,7 +924,8 @@ TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) {
|
|||
}
|
||||
TEST_F(IndexEnv2, testIndex_MultiWrite_and_MultiRead) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {}
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
|
||||
std::thread threads[NUM_OF_THREAD];
|
||||
for (int i = 0; i < NUM_OF_THREAD; i++) {
|
||||
|
@ -927,14 +940,24 @@ TEST_F(IndexEnv2, testIndex_MultiWrite_and_MultiRead) {
|
|||
|
||||
TEST_F(IndexEnv2, testIndex_restart) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {}
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->SearchOneTarget("tag1", "Hello", 10);
|
||||
index->SearchOneTarget("tag2", "Test", 10);
|
||||
}
|
||||
TEST_F(IndexEnv2, testIndex_restart1) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->ReadMultiMillonData("tag1", "coding");
|
||||
index->SearchOneTarget("tag1", "Hello", 10);
|
||||
index->SearchOneTarget("tag2", "Test", 10);
|
||||
}
|
||||
|
||||
TEST_F(IndexEnv2, testIndex_read_performance) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {}
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->PutOneTarge("tag1", "Hello", 12);
|
||||
index->PutOneTarge("tag1", "Hello", 15);
|
||||
index->ReadMultiMillonData("tag1", "Hello");
|
||||
|
@ -943,17 +966,84 @@ TEST_F(IndexEnv2, testIndex_read_performance) {
|
|||
}
|
||||
TEST_F(IndexEnv2, testIndexMultiTag) {
|
||||
std::string path = "/tmp/multi_tag";
|
||||
if (index->Init(path) != 0) {}
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
int64_t st = taosGetTimestampUs();
|
||||
int32_t num = 1000 * 10000;
|
||||
index->WriteMultiMillonData("tag1", "xxxxxxxxxxxxxxx", num);
|
||||
std::cout << "numOfRow: " << num << "\ttime cost:" << taosGetTimestampUs() - st << std::endl;
|
||||
// index->WriteMultiMillonData("tag2", "xxxxxxxxxxxxxxxxxxxxxxxxx", 100 * 10000);
|
||||
}
|
||||
TEST_F(IndexEnv2, testLongComVal) {
|
||||
TEST_F(IndexEnv2, testLongComVal1) {
|
||||
std::string path = "/tmp/long_colVal";
|
||||
if (index->Init(path) != 0) {}
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
// gen colVal by randstr
|
||||
std::string randstr = "xxxxxxxxxxxxxxxxx";
|
||||
index->WriteMultiMillonData("tag1", randstr, 100 * 10000);
|
||||
}
|
||||
|
||||
TEST_F(IndexEnv2, testLongComVal2) {
|
||||
std::string path = "/tmp/long_colVal";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
// gen colVal by randstr
|
||||
std::string randstr = "abcccc fdadfafdafda";
|
||||
index->WriteMultiMillonData("tag1", randstr, 100 * 10000);
|
||||
}
|
||||
TEST_F(IndexEnv2, testLongComVal3) {
|
||||
std::string path = "/tmp/long_colVal";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
// gen colVal by randstr
|
||||
std::string randstr = "Yes, coding and coding and coding";
|
||||
index->WriteMultiMillonData("tag1", randstr, 100 * 10000);
|
||||
}
|
||||
TEST_F(IndexEnv2, testLongComVal4) {
|
||||
std::string path = "/tmp/long_colVal";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
// gen colVal by randstr
|
||||
std::string randstr = "111111 bac fdadfa";
|
||||
index->WriteMultiMillonData("tag1", randstr, 100 * 10000);
|
||||
}
|
||||
TEST_F(IndexEnv2, testIndex_read_performance1) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->PutOneTarge("tag1", "Hello", 12);
|
||||
index->PutOneTarge("tag1", "Hello", 15);
|
||||
index->ReadMultiMillonData("tag1", "Hello", 1000);
|
||||
std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
|
||||
assert(3 == index->SearchOne("tag1", "Hello"));
|
||||
}
|
||||
TEST_F(IndexEnv2, testIndex_read_performance2) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->PutOneTarge("tag1", "Hello", 12);
|
||||
index->PutOneTarge("tag1", "Hello", 15);
|
||||
index->ReadMultiMillonData("tag1", "Hello", 1000 * 10);
|
||||
std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
|
||||
assert(3 == index->SearchOne("tag1", "Hello"));
|
||||
}
|
||||
TEST_F(IndexEnv2, testIndex_read_performance3) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->PutOneTarge("tag1", "Hello", 12);
|
||||
index->PutOneTarge("tag1", "Hello", 15);
|
||||
index->ReadMultiMillonData("tag1", "Hello", 1000 * 100);
|
||||
std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
|
||||
assert(3 == index->SearchOne("tag1", "Hello"));
|
||||
}
|
||||
TEST_F(IndexEnv2, testIndex_read_performance4) {
|
||||
std::string path = "/tmp/cache_and_tfile";
|
||||
if (index->Init(path) != 0) {
|
||||
}
|
||||
index->PutOneTarge("tag10", "Hello", 12);
|
||||
index->PutOneTarge("tag12", "Hello", 15);
|
||||
index->ReadMultiMillonData("tag10", "Hello", 1000 * 100);
|
||||
std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
|
||||
assert(3 == index->SearchOne("tag10", "Hello"));
|
||||
}
|
||||
|
|
|
@ -105,35 +105,107 @@ typedef struct {
|
|||
SRpcInfo* pRpc; // associated SRpcInfo
|
||||
SEpSet epSet; // ip list provided by app
|
||||
void* ahandle; // handle provided by app
|
||||
struct SRpcConn* pConn; // pConn allocated
|
||||
// struct SRpcConn* pConn; // pConn allocated
|
||||
tmsg_t msgType; // message type
|
||||
uint8_t* pCont; // content provided by app
|
||||
int32_t contLen; // content length
|
||||
int32_t code; // error code
|
||||
int16_t numOfTry; // number of try for different servers
|
||||
int8_t oldInUse; // server EP inUse passed by app
|
||||
int8_t redirect; // flag to indicate redirect
|
||||
// int32_t code; // error code
|
||||
// int16_t numOfTry; // number of try for different servers
|
||||
// int8_t oldInUse; // server EP inUse passed by app
|
||||
// int8_t redirect; // flag to indicate redirect
|
||||
int8_t connType; // connection type
|
||||
int64_t rid; // refId returned by taosAddRef
|
||||
SRpcMsg* pRsp; // for synchronous API
|
||||
tsem_t* pSem; // for synchronous API
|
||||
SEpSet* pSet; // for synchronous API
|
||||
char msg[0]; // RpcHead starts from here
|
||||
char* ip;
|
||||
uint32_t port;
|
||||
// SEpSet* pSet; // for synchronous API
|
||||
} SRpcReqContext;
|
||||
|
||||
#define container_of(ptr, type, member) ((type*)((char*)(ptr)-offsetof(type, member)))
|
||||
#define RPC_RESERVE_SIZE (sizeof(SRpcReqContext))
|
||||
typedef struct {
|
||||
SRpcInfo* pRpc; // associated SRpcInfo
|
||||
SEpSet epSet; // ip list provided by app
|
||||
void* ahandle; // handle provided by app
|
||||
// struct SRpcConn* pConn; // pConn allocated
|
||||
tmsg_t msgType; // message type
|
||||
uint8_t* pCont; // content provided by app
|
||||
int32_t contLen; // content length
|
||||
// int32_t code; // error code
|
||||
// int16_t numOfTry; // number of try for different servers
|
||||
// int8_t oldInUse; // server EP inUse passed by app
|
||||
// int8_t redirect; // flag to indicate redirect
|
||||
int8_t connType; // connection type
|
||||
int64_t rid; // refId returned by taosAddRef
|
||||
SRpcMsg* pRsp; // for synchronous API
|
||||
tsem_t* pSem; // for synchronous API
|
||||
char* ip;
|
||||
uint32_t port;
|
||||
// SEpSet* pSet; // for synchronous API
|
||||
} STransConnCtx;
|
||||
|
||||
#define RPC_MSG_OVERHEAD (sizeof(SRpcReqContext) + sizeof(SRpcHead) + sizeof(SRpcDigest))
|
||||
#pragma pack(push, 1)
|
||||
|
||||
typedef struct {
|
||||
char version : 4; // RPC version
|
||||
char comp : 4; // compression algorithm, 0:no compression 1:lz4
|
||||
char resflag : 2; // reserved bits
|
||||
char spi : 3; // security parameter index
|
||||
char encrypt : 3; // encrypt algorithm, 0: no encryption
|
||||
|
||||
uint32_t code; // del later
|
||||
uint32_t msgType;
|
||||
int32_t msgLen;
|
||||
uint8_t content[0]; // message body starts from here
|
||||
} STransMsgHead;
|
||||
|
||||
typedef struct {
|
||||
int32_t reserved;
|
||||
int32_t contLen;
|
||||
} STransCompMsg;
|
||||
|
||||
typedef struct {
|
||||
uint32_t timeStamp;
|
||||
uint8_t auth[TSDB_AUTH_LEN];
|
||||
} STransDigestMsg;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#define container_of(ptr, type, member) ((type*)((char*)(ptr)-offsetof(type, member)))
|
||||
#define RPC_RESERVE_SIZE (sizeof(STranConnCtx))
|
||||
|
||||
#define RPC_MSG_OVERHEAD (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)
|
||||
|
||||
#define TRANS_RESERVE_SIZE (sizeof(STranConnCtx))
|
||||
|
||||
#define TRANS_MSG_OVERHEAD (sizeof(STransMsgHead) + sizeof(STransDigestMsg))
|
||||
#define transHeadFromCont(cont) ((STransMsgHead*)((char*)cont - sizeof(STransMsgHead)))
|
||||
#define transContFromHead(msg) (msg + sizeof(STransMsgHead))
|
||||
#define transMsgLenFromCont(contLen) (contLen + sizeof(STransMsgHead))
|
||||
#define transContLenFromMsg(msgLen) (msgLen - sizeof(STransMsgHead));
|
||||
#define transIsReq(type) (type & 1U)
|
||||
|
||||
int rpcAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey);
|
||||
void rpcBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey);
|
||||
int32_t rpcCompressRpcMsg(char* pCont, int32_t contLen);
|
||||
SRpcHead* rpcDecompressRpcMsg(SRpcHead* pHead);
|
||||
|
||||
int transAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey);
|
||||
void transBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey);
|
||||
bool transCompressMsg(char* msg, int32_t len, int32_t* flen);
|
||||
bool transDecompressMsg(char* msg, int32_t len, int32_t* flen);
|
||||
|
||||
void transConnCtxDestroy(STransConnCtx* ctx);
|
||||
|
||||
typedef struct SConnBuffer {
|
||||
char* buf;
|
||||
int len;
|
||||
int cap;
|
||||
int left;
|
||||
} SConnBuffer;
|
||||
|
||||
#endif
|
||||
|
|
|
@ -45,6 +45,9 @@ extern "C" {
|
|||
void* taosInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle);
|
||||
void* taosInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle);
|
||||
|
||||
void taosCloseServer(void* arg);
|
||||
void taosCloseClient(void* arg);
|
||||
|
||||
typedef struct {
|
||||
int sessions; // number of sessions allowed
|
||||
int numOfThreads; // number of threads to process incoming messages
|
||||
|
|
|
@ -17,15 +17,9 @@
|
|||
|
||||
#include "transComm.h"
|
||||
|
||||
typedef struct SConnBuffer {
|
||||
char* buf;
|
||||
int len;
|
||||
int cap;
|
||||
int left;
|
||||
} SConnBuffer;
|
||||
|
||||
void* (*taosHandle[])(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) = {
|
||||
void* (*taosInitHandle[])(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) = {
|
||||
taosInitServer, taosInitClient};
|
||||
void (*taosCloseHandle[])(void* arg) = {taosCloseServer, taosCloseClient};
|
||||
|
||||
void* rpcOpen(const SRpcInit* pInit) {
|
||||
SRpcInfo* pRpc = calloc(1, sizeof(SRpcInfo));
|
||||
|
@ -38,13 +32,18 @@ void* rpcOpen(const SRpcInit* pInit) {
|
|||
pRpc->cfp = pInit->cfp;
|
||||
pRpc->numOfThreads = pInit->numOfThreads > TSDB_MAX_RPC_THREADS ? TSDB_MAX_RPC_THREADS : pInit->numOfThreads;
|
||||
pRpc->connType = pInit->connType;
|
||||
pRpc->tcphandle = (*taosHandle[pRpc->connType])(0, pInit->localPort, pRpc->label, pRpc->numOfThreads, NULL, pRpc);
|
||||
pRpc->tcphandle = (*taosInitHandle[pRpc->connType])(0, pInit->localPort, pRpc->label, pRpc->numOfThreads, NULL, pRpc);
|
||||
|
||||
return pRpc;
|
||||
}
|
||||
void rpcClose(void* arg) { return; }
|
||||
void rpcClose(void* arg) {
|
||||
SRpcInfo* pRpc = (SRpcInfo*)arg;
|
||||
(*taosCloseHandle[pRpc->connType])(pRpc->tcphandle);
|
||||
free(pRpc);
|
||||
return;
|
||||
}
|
||||
void* rpcMallocCont(int contLen) {
|
||||
int size = contLen + RPC_MSG_OVERHEAD;
|
||||
int size = contLen + TRANS_MSG_OVERHEAD;
|
||||
|
||||
char* start = (char*)calloc(1, (size_t)size);
|
||||
if (start == NULL) {
|
||||
|
@ -53,7 +52,7 @@ void* rpcMallocCont(int contLen) {
|
|||
} else {
|
||||
tTrace("malloc mem:%p size:%d", start, size);
|
||||
}
|
||||
return start + sizeof(SRpcReqContext) + sizeof(SRpcHead);
|
||||
return start + sizeof(STransMsgHead);
|
||||
}
|
||||
void rpcFreeCont(void* cont) { return; }
|
||||
void* rpcReallocCont(void* ptr, int contLen) { return NULL; }
|
||||
|
@ -71,6 +70,7 @@ int32_t rpcInit(void) {
|
|||
|
||||
void rpcCleanup(void) {
|
||||
// impl later
|
||||
//
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -21,13 +21,17 @@ typedef struct SCliConn {
|
|||
uv_connect_t connReq;
|
||||
uv_stream_t* stream;
|
||||
uv_write_t* writeReq;
|
||||
SConnBuffer readBuf;
|
||||
void* data;
|
||||
queue conn;
|
||||
char spi;
|
||||
char secured;
|
||||
uint64_t expireTime;
|
||||
} SCliConn;
|
||||
|
||||
typedef struct SCliMsg {
|
||||
SRpcReqContext* context;
|
||||
STransConnCtx* ctx;
|
||||
SRpcMsg msg;
|
||||
queue q;
|
||||
uint64_t st;
|
||||
} SCliMsg;
|
||||
|
@ -36,10 +40,13 @@ typedef struct SCliThrdObj {
|
|||
pthread_t thread;
|
||||
uv_loop_t* loop;
|
||||
uv_async_t* cliAsync; //
|
||||
uv_timer_t* pTimer;
|
||||
void* cache; // conn pool
|
||||
queue msg;
|
||||
pthread_mutex_t msgMtx;
|
||||
void* shandle;
|
||||
uint64_t nextTimeout; // next timeout
|
||||
void* shandle; //
|
||||
|
||||
} SCliThrdObj;
|
||||
|
||||
typedef struct SClientObj {
|
||||
|
@ -49,31 +56,188 @@ typedef struct SClientObj {
|
|||
SCliThrdObj** pThreadObj;
|
||||
} SClientObj;
|
||||
|
||||
typedef struct SConnList {
|
||||
queue conn;
|
||||
} SConnList;
|
||||
|
||||
// conn pool
|
||||
// add expire timeout and capacity limit
|
||||
static void* connCacheCreate(int size);
|
||||
static void* connCacheDestroy(void* cache);
|
||||
static SCliConn* getConnFromCache(void* cache, char* ip, uint32_t port);
|
||||
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn);
|
||||
|
||||
static void clientAllocrReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
||||
// register timer in each thread to clear expire conn
|
||||
static void clientTimeoutCb(uv_timer_t* handle);
|
||||
// process data read from server, auth/decompress etc
|
||||
static void clientProcessData(SCliConn* conn);
|
||||
// check whether already read complete packet from server
|
||||
static bool clientReadComplete(SConnBuffer* pBuf);
|
||||
// alloc buf for read
|
||||
static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
||||
// callback after read nbytes from socket
|
||||
static void clientReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf);
|
||||
// callback after write data to socket
|
||||
static void clientWriteCb(uv_write_t* req, int status);
|
||||
// callback after conn to server
|
||||
static void clientConnCb(uv_connect_t* req, int status);
|
||||
static void clientAsyncCb(uv_async_t* handle);
|
||||
static void clientDestroy(uv_handle_t* handle);
|
||||
static void clientConnDestroy(SCliConn* pConn);
|
||||
|
||||
static void clientMsgDestroy(SCliMsg* pMsg);
|
||||
|
||||
static void* clientThread(void* arg);
|
||||
|
||||
static void clientProcessData(SCliConn* conn) {
|
||||
STransConnCtx* pCtx = ((SCliMsg*)conn->data)->ctx;
|
||||
SRpcInfo* pRpc = pCtx->ahandle;
|
||||
SRpcMsg rpcMsg;
|
||||
|
||||
rpcMsg.pCont = conn->readBuf.buf;
|
||||
rpcMsg.contLen = conn->readBuf.len;
|
||||
rpcMsg.ahandle = pCtx->ahandle;
|
||||
(pRpc->cfp)(NULL, &rpcMsg, NULL);
|
||||
// impl
|
||||
}
|
||||
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd);
|
||||
|
||||
static void clientAllocrReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
||||
static void clientTimeoutCb(uv_timer_t* handle) {
|
||||
SCliThrdObj* pThrd = handle->data;
|
||||
SRpcInfo* pRpc = pThrd->shandle;
|
||||
int64_t currentTime = pThrd->nextTimeout;
|
||||
|
||||
SConnList* p = taosHashIterate((SHashObj*)pThrd->cache, NULL);
|
||||
while (p != NULL) {
|
||||
while (!QUEUE_IS_EMPTY(&p->conn)) {
|
||||
queue* h = QUEUE_HEAD(&p->conn);
|
||||
SCliConn* c = QUEUE_DATA(h, SCliConn, conn);
|
||||
if (c->expireTime < currentTime) {
|
||||
QUEUE_REMOVE(h);
|
||||
clientConnDestroy(c);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
p = taosHashIterate((SHashObj*)pThrd->cache, p);
|
||||
}
|
||||
|
||||
pThrd->nextTimeout = taosGetTimestampMs() + pRpc->idleTime * 1000 * 10;
|
||||
uv_timer_start(handle, clientTimeoutCb, pRpc->idleTime * 10, 0);
|
||||
}
|
||||
static void* connCacheCreate(int size) {
|
||||
SHashObj* cache = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
|
||||
return false;
|
||||
}
|
||||
static void* connCacheDestroy(void* cache) {
|
||||
SConnList* connList = taosHashIterate((SHashObj*)cache, NULL);
|
||||
while (connList != NULL) {
|
||||
while (!QUEUE_IS_EMPTY(&connList->conn)) {
|
||||
queue* h = QUEUE_HEAD(&connList->conn);
|
||||
QUEUE_REMOVE(h);
|
||||
SCliConn* c = QUEUE_DATA(h, SCliConn, conn);
|
||||
clientConnDestroy(c);
|
||||
}
|
||||
connList = taosHashIterate((SHashObj*)cache, connList);
|
||||
}
|
||||
taosHashClear(cache);
|
||||
}
|
||||
|
||||
static SCliConn* getConnFromCache(void* cache, char* ip, uint32_t port) {
|
||||
char key[128] = {0};
|
||||
tstrncpy(key, ip, strlen(ip));
|
||||
tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));
|
||||
|
||||
SHashObj* pCache = cache;
|
||||
SConnList* plist = taosHashGet(pCache, key, strlen(key));
|
||||
if (plist == NULL) {
|
||||
SConnList list;
|
||||
plist = &list;
|
||||
QUEUE_INIT(&plist->conn);
|
||||
taosHashPut(pCache, key, strlen(key), plist, sizeof(*plist));
|
||||
}
|
||||
|
||||
if (QUEUE_IS_EMPTY(&plist->conn)) {
|
||||
return NULL;
|
||||
}
|
||||
queue* h = QUEUE_HEAD(&plist->conn);
|
||||
QUEUE_REMOVE(h);
|
||||
return QUEUE_DATA(h, SCliConn, conn);
|
||||
}
|
||||
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn) {
|
||||
char key[128] = {0};
|
||||
tstrncpy(key, ip, strlen(ip));
|
||||
tstrncpy(key + strlen(key), (char*)(&port), sizeof(port));
|
||||
|
||||
STransConnCtx* ctx = ((SCliMsg*)conn->data)->ctx;
|
||||
SRpcInfo* pRpc = ctx->pRpc;
|
||||
conn->expireTime = taosGetTimestampMs() + pRpc->idleTime * 1000 * 10;
|
||||
SConnList* plist = taosHashGet((SHashObj*)cache, key, strlen(key));
|
||||
// list already create before
|
||||
assert(plist != NULL);
|
||||
QUEUE_PUSH(&plist->conn, &conn->conn);
|
||||
}
|
||||
static bool clientReadComplete(SConnBuffer* data) {
|
||||
STransMsgHead head;
|
||||
int32_t headLen = sizeof(head);
|
||||
if (data->len >= headLen) {
|
||||
memcpy((char*)&head, data->buf, headLen);
|
||||
int32_t msgLen = (int32_t)htonl((uint32_t)head.msgLen);
|
||||
if (msgLen > data->len) {
|
||||
data->left = msgLen - data->len;
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
static void clientAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
||||
// impl later
|
||||
static const int CAPACITY = 512;
|
||||
|
||||
SCliConn* conn = handle->data;
|
||||
SConnBuffer* pBuf = &conn->readBuf;
|
||||
if (pBuf->cap == 0) {
|
||||
pBuf->buf = (char*)calloc(CAPACITY, sizeof(char));
|
||||
pBuf->len = 0;
|
||||
pBuf->cap = CAPACITY;
|
||||
pBuf->left = -1;
|
||||
buf->base = pBuf->buf;
|
||||
buf->len = CAPACITY;
|
||||
} else {
|
||||
if (pBuf->len >= pBuf->cap) {
|
||||
if (pBuf->left == -1) {
|
||||
pBuf->cap *= 2;
|
||||
pBuf->buf = realloc(pBuf->buf, pBuf->cap);
|
||||
} else if (pBuf->len + pBuf->left > pBuf->cap) {
|
||||
pBuf->cap = pBuf->len + pBuf->left;
|
||||
pBuf->buf = realloc(pBuf->buf, pBuf->len + pBuf->left);
|
||||
}
|
||||
}
|
||||
buf->base = pBuf->buf + pBuf->len;
|
||||
buf->len = pBuf->cap - pBuf->len;
|
||||
}
|
||||
}
|
||||
static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
|
||||
// impl later
|
||||
SCliConn* conn = handle->data;
|
||||
SConnBuffer* pBuf = &conn->readBuf;
|
||||
if (nread > 0) {
|
||||
pBuf->len += nread;
|
||||
if (clientReadComplete(pBuf)) {
|
||||
tDebug("alread read complete pack");
|
||||
clientProcessData(conn);
|
||||
} else {
|
||||
tDebug("read halp packet, continue to read");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (nread != UV_EOF) {
|
||||
tDebug("Read error %s\n", uv_err_name(nread));
|
||||
}
|
||||
//
|
||||
uv_close((uv_handle_t*)handle, clientDestroy);
|
||||
}
|
||||
|
@ -84,6 +248,7 @@ static void clientConnDestroy(SCliConn* conn) {
|
|||
}
|
||||
static void clientDestroy(uv_handle_t* handle) {
|
||||
SCliConn* conn = handle->data;
|
||||
QUEUE_REMOVE(&conn->conn);
|
||||
clientConnDestroy(conn);
|
||||
}
|
||||
|
||||
|
@ -96,14 +261,16 @@ static void clientWriteCb(uv_write_t* req, int status) {
|
|||
return;
|
||||
}
|
||||
|
||||
uv_read_start((uv_stream_t*)pConn->stream, clientAllocrReadBufferCb, clientReadCb);
|
||||
uv_read_start((uv_stream_t*)pConn->stream, clientAllocReadBufferCb, clientReadCb);
|
||||
// impl later
|
||||
}
|
||||
|
||||
static void clientWrite(SCliConn* pConn) {
|
||||
SCliMsg* pMsg = pConn->data;
|
||||
SRpcHead* pHead = rpcHeadFromCont(pMsg->context->pCont);
|
||||
int msgLen = rpcMsgLenFromCont(pMsg->context->contLen);
|
||||
SCliMsg* pCliMsg = pConn->data;
|
||||
SRpcMsg* pMsg = (SRpcMsg*)(&pCliMsg->msg);
|
||||
STransMsgHead* pHead = transHeadFromCont(pMsg->pCont);
|
||||
|
||||
int msgLen = transMsgLenFromCont(pMsg->contLen);
|
||||
char* msg = (char*)(pHead);
|
||||
|
||||
uv_buf_t wb = uv_buf_init(msg, msgLen);
|
||||
|
@ -119,76 +286,51 @@ static void clientConnCb(uv_connect_t* req, int status) {
|
|||
}
|
||||
|
||||
SCliMsg* pMsg = pConn->data;
|
||||
SEpSet* pEpSet = &pMsg->context->epSet;
|
||||
SRpcMsg rpcMsg;
|
||||
// rpcMsg.ahandle = pMsg->context->ahandle;
|
||||
// rpcMsg.pCont = NULL;
|
||||
STransConnCtx* pCtx = ((SCliMsg*)(pConn->data))->ctx;
|
||||
|
||||
SRpcMsg rpcMsg;
|
||||
rpcMsg.ahandle = pCtx->ahandle;
|
||||
|
||||
char* fqdn = pEpSet->fqdn[pEpSet->inUse];
|
||||
uint32_t port = pEpSet->port[pEpSet->inUse];
|
||||
if (status != 0) {
|
||||
// call user fp later
|
||||
tError("failed to connect server(%s, %d), errmsg: %s", fqdn, port, uv_strerror(status));
|
||||
SRpcInfo* pRpc = pMsg->context->pRpc;
|
||||
(pRpc->cfp)(NULL, &rpcMsg, pEpSet);
|
||||
tError("failed to connect server(%s, %d), errmsg: %s", pCtx->ip, pCtx->port, uv_strerror(status));
|
||||
SRpcInfo* pRpc = pMsg->ctx->pRpc;
|
||||
(pRpc->cfp)(NULL, &rpcMsg, NULL);
|
||||
uv_close((uv_handle_t*)req->handle, clientDestroy);
|
||||
return;
|
||||
}
|
||||
assert(pConn->stream == req->handle);
|
||||
}
|
||||
|
||||
static SCliConn* getConnFromCache(void* cache, char* ip, uint32_t port) {
|
||||
// impl later
|
||||
|
||||
return NULL;
|
||||
}
|
||||
static void addConnToCache(void* cache, char* ip, uint32_t port, SCliConn* conn) {
|
||||
// impl later
|
||||
clientWrite(pConn);
|
||||
}
|
||||
|
||||
static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) {
|
||||
SEpSet* pEpSet = &pMsg->context->epSet;
|
||||
|
||||
char* fqdn = pEpSet->fqdn[pEpSet->inUse];
|
||||
uint32_t port = pEpSet->port[pEpSet->inUse];
|
||||
|
||||
uint64_t el = taosGetTimestampUs() - pMsg->st;
|
||||
uint64_t et = taosGetTimestampUs();
|
||||
uint64_t el = et - pMsg->st;
|
||||
tDebug("msg tran time cost: %" PRIu64 "", el);
|
||||
et = taosGetTimestampUs();
|
||||
|
||||
SCliConn* conn = getConnFromCache(pThrd->cache, fqdn, port);
|
||||
STransConnCtx* pCtx = pMsg->ctx;
|
||||
SCliConn* conn = getConnFromCache(pThrd->cache, pCtx->ip, pCtx->port);
|
||||
if (conn != NULL) {
|
||||
// impl later
|
||||
conn->data = pMsg;
|
||||
conn->writeReq->data = conn;
|
||||
clientWrite(conn);
|
||||
// uv_buf_t wb;
|
||||
// uv_write(conn->writeReq, (uv_stream_t*)conn->stream, &wb, 1, clientWriteCb);
|
||||
} else {
|
||||
SCliConn* conn = malloc(sizeof(SCliConn));
|
||||
SCliConn* conn = calloc(1, sizeof(SCliConn));
|
||||
|
||||
conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
|
||||
uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream));
|
||||
conn->writeReq = malloc(sizeof(uv_write_t));
|
||||
QUEUE_INIT(&conn->conn);
|
||||
|
||||
conn->connReq.data = conn;
|
||||
conn->data = pMsg;
|
||||
|
||||
struct sockaddr_in addr;
|
||||
uv_ip4_addr(fqdn, port, &addr);
|
||||
uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr);
|
||||
// handle error in callback if fail to connect
|
||||
uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb);
|
||||
|
||||
// SRpcMsg rpcMsg;
|
||||
// SEpSet* pEpSet = &pMsg->context->epSet;
|
||||
// SRpcInfo* pRpc = pMsg->context->pRpc;
|
||||
//// rpcMsg.ahandle = pMsg->context->ahandle;
|
||||
// rpcMsg.pCont = NULL;
|
||||
// rpcMsg.ahandle = pMsg->context->ahandle;
|
||||
// uint64_t el1 = taosGetTimestampUs() - et;
|
||||
// tError("msg tran back first: time cost: %" PRIu64 "", el1);
|
||||
// et = taosGetTimestampUs();
|
||||
//(pRpc->cfp)(NULL, &rpcMsg, pEpSet);
|
||||
// uint64_t el2 = taosGetTimestampUs() - et;
|
||||
// tError("msg tran back second: time cost: %" PRIu64 "", el2);
|
||||
}
|
||||
}
|
||||
static void clientAsyncCb(uv_async_t* handle) {
|
||||
|
@ -205,7 +347,8 @@ static void clientAsyncCb(uv_async_t* handle) {
|
|||
while (!QUEUE_IS_EMPTY(&wq)) {
|
||||
queue* h = QUEUE_HEAD(&wq);
|
||||
QUEUE_REMOVE(h);
|
||||
pMsg = QUEUE_DATA(h, SCliMsg, q);
|
||||
|
||||
SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q);
|
||||
clientHandleReq(pMsg, pThrd);
|
||||
count++;
|
||||
if (count >= 2) {
|
||||
|
@ -216,11 +359,15 @@ static void clientAsyncCb(uv_async_t* handle) {
|
|||
|
||||
static void* clientThread(void* arg) {
|
||||
SCliThrdObj* pThrd = (SCliThrdObj*)arg;
|
||||
SRpcInfo* pRpc = pThrd->shandle;
|
||||
pThrd->nextTimeout = taosGetTimestampMs() + pRpc->idleTime * 1000 * 10;
|
||||
uv_timer_start(pThrd->pTimer, clientTimeoutCb, pRpc->idleTime * 10, 0);
|
||||
uv_run(pThrd->loop, UV_RUN_DEFAULT);
|
||||
}
|
||||
|
||||
void* taosInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle) {
|
||||
SClientObj* cli = calloc(1, sizeof(SClientObj));
|
||||
|
||||
memcpy(cli->label, label, strlen(label));
|
||||
cli->numOfThreads = numOfThreads;
|
||||
cli->pThreadObj = (SCliThrdObj**)calloc(cli->numOfThreads, sizeof(SCliThrdObj*));
|
||||
|
@ -236,7 +383,11 @@ void* taosInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads,
|
|||
uv_async_init(pThrd->loop, pThrd->cliAsync, clientAsyncCb);
|
||||
pThrd->cliAsync->data = pThrd;
|
||||
|
||||
pThrd->pTimer = malloc(sizeof(uv_timer_t));
|
||||
uv_timer_init(pThrd->loop, pThrd->pTimer);
|
||||
|
||||
pThrd->shandle = shandle;
|
||||
|
||||
int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd));
|
||||
if (err == 0) {
|
||||
tDebug("sucess to create tranport-client thread %d", i);
|
||||
|
@ -245,22 +396,44 @@ void* taosInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads,
|
|||
}
|
||||
return cli;
|
||||
}
|
||||
static void clientMsgDestroy(SCliMsg* pMsg) {
|
||||
// impl later
|
||||
free(pMsg);
|
||||
}
|
||||
void taosCloseClient(void* arg) {
|
||||
// impl later
|
||||
SClientObj* cli = arg;
|
||||
for (int i = 0; i < cli->numOfThreads; i++) {
|
||||
SCliThrdObj* pThrd = cli->pThreadObj[i];
|
||||
pthread_join(pThrd->thread, NULL);
|
||||
pthread_mutex_destroy(&pThrd->msgMtx);
|
||||
free(pThrd->cliAsync);
|
||||
free(pThrd->loop);
|
||||
free(pThrd);
|
||||
}
|
||||
free(cli->pThreadObj);
|
||||
free(cli);
|
||||
}
|
||||
|
||||
void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) {
|
||||
// impl later
|
||||
char* ip = (char*)(pEpSet->fqdn[pEpSet->inUse]);
|
||||
uint32_t port = pEpSet->port[pEpSet->inUse];
|
||||
|
||||
SRpcInfo* pRpc = (SRpcInfo*)shandle;
|
||||
|
||||
int len = rpcCompressRpcMsg(pMsg->pCont, pMsg->contLen);
|
||||
int32_t flen = 0;
|
||||
if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) {
|
||||
// imp later
|
||||
}
|
||||
|
||||
SRpcReqContext* pContext;
|
||||
pContext = (SRpcReqContext*)((char*)pMsg->pCont - sizeof(SRpcHead) - sizeof(SRpcReqContext));
|
||||
pContext->ahandle = pMsg->ahandle;
|
||||
pContext->pRpc = (SRpcInfo*)shandle;
|
||||
pContext->epSet = *pEpSet;
|
||||
pContext->contLen = len;
|
||||
pContext->pCont = pMsg->pCont;
|
||||
pContext->msgType = pMsg->msgType;
|
||||
pContext->oldInUse = pEpSet->inUse;
|
||||
STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx));
|
||||
|
||||
pCtx->pRpc = (SRpcInfo*)shandle;
|
||||
pCtx->ahandle = pMsg->ahandle;
|
||||
pCtx->msgType = pMsg->msgType;
|
||||
pCtx->ip = strdup(ip);
|
||||
pCtx->port = port;
|
||||
|
||||
assert(pRpc->connType == TAOS_CONN_CLIENT);
|
||||
// atomic or not
|
||||
|
@ -268,14 +441,15 @@ void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t*
|
|||
if (pRpc->index++ >= pRpc->numOfThreads) {
|
||||
pRpc->index = 0;
|
||||
}
|
||||
SCliMsg* msg = malloc(sizeof(SCliMsg));
|
||||
msg->context = pContext;
|
||||
msg->st = taosGetTimestampUs();
|
||||
SCliMsg* cliMsg = malloc(sizeof(SCliMsg));
|
||||
cliMsg->ctx = pCtx;
|
||||
cliMsg->msg = *pMsg;
|
||||
cliMsg->st = taosGetTimestampUs();
|
||||
|
||||
SCliThrdObj* thrd = ((SClientObj*)pRpc->tcphandle)->pThreadObj[index % pRpc->numOfThreads];
|
||||
|
||||
pthread_mutex_lock(&thrd->msgMtx);
|
||||
QUEUE_PUSH(&thrd->msg, &msg->q);
|
||||
QUEUE_PUSH(&thrd->msg, &cliMsg->q);
|
||||
pthread_mutex_unlock(&thrd->msgMtx);
|
||||
|
||||
uv_async_send(thrd->cliAsync);
|
||||
|
|
|
@ -30,6 +30,20 @@ int rpcAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey) {
|
|||
|
||||
return ret;
|
||||
}
|
||||
int transAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey) {
|
||||
T_MD5_CTX context;
|
||||
int ret = -1;
|
||||
|
||||
tMD5Init(&context);
|
||||
tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
|
||||
tMD5Update(&context, (uint8_t*)pMsg, msgLen);
|
||||
tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
|
||||
tMD5Final(&context);
|
||||
|
||||
if (memcmp(context.digest, pAuth, sizeof(context.digest)) == 0) ret = 0;
|
||||
|
||||
return ret;
|
||||
}
|
||||
void rpcBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey) {
|
||||
T_MD5_CTX context;
|
||||
|
||||
|
@ -41,6 +55,17 @@ void rpcBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey) {
|
|||
|
||||
memcpy(pAuth, context.digest, sizeof(context.digest));
|
||||
}
|
||||
void transBuildAuthHead(void* pMsg, int msgLen, void* pAuth, void* pKey) {
|
||||
T_MD5_CTX context;
|
||||
|
||||
tMD5Init(&context);
|
||||
tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
|
||||
tMD5Update(&context, (uint8_t*)pMsg, msgLen);
|
||||
tMD5Update(&context, (uint8_t*)pKey, TSDB_PASSWORD_LEN);
|
||||
tMD5Final(&context);
|
||||
|
||||
memcpy(pAuth, context.digest, sizeof(context.digest));
|
||||
}
|
||||
|
||||
int32_t rpcCompressRpcMsg(char* pCont, int32_t contLen) {
|
||||
SRpcHead* pHead = rpcHeadFromCont(pCont);
|
||||
|
@ -81,6 +106,54 @@ int32_t rpcCompressRpcMsg(char* pCont, int32_t contLen) {
|
|||
return finalLen;
|
||||
}
|
||||
|
||||
bool transCompressMsg(char* msg, int32_t len, int32_t* flen) {
|
||||
return false;
|
||||
// SRpcHead* pHead = rpcHeadFromCont(pCont);
|
||||
bool succ = false;
|
||||
int overhead = sizeof(STransCompMsg);
|
||||
if (!NEEDTO_COMPRESSS_MSG(len)) {
|
||||
return succ;
|
||||
}
|
||||
|
||||
char* buf = malloc(len + overhead + 8); // 8 extra bytes
|
||||
if (buf == NULL) {
|
||||
tError("failed to allocate memory for rpc msg compression, contLen:%d", len);
|
||||
*flen = len;
|
||||
return succ;
|
||||
}
|
||||
|
||||
int32_t clen = LZ4_compress_default(msg, buf, len, len + overhead);
|
||||
tDebug("compress rpc msg, before:%d, after:%d, overhead:%d", len, clen, overhead);
|
||||
/*
|
||||
* only the compressed size is less than the value of contLen - overhead, the compression is applied
|
||||
* The first four bytes is set to 0, the second four bytes are utilized to keep the original length of message
|
||||
*/
|
||||
if (clen > 0 && clen < len - overhead) {
|
||||
STransCompMsg* pComp = (STransCompMsg*)msg;
|
||||
pComp->reserved = 0;
|
||||
pComp->contLen = htonl(len);
|
||||
memcpy(msg + overhead, buf, clen);
|
||||
|
||||
tDebug("compress rpc msg, before:%d, after:%d", len, clen);
|
||||
*flen = clen + overhead;
|
||||
succ = true;
|
||||
} else {
|
||||
*flen = len;
|
||||
succ = false;
|
||||
}
|
||||
free(buf);
|
||||
return succ;
|
||||
}
|
||||
bool transDecompressMsg(char* msg, int32_t len, int32_t* flen) {
|
||||
// impl later
|
||||
return false;
|
||||
STransCompMsg* pComp = (STransCompMsg*)msg;
|
||||
|
||||
int overhead = sizeof(STransCompMsg);
|
||||
int clen = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
SRpcHead* rpcDecompressRpcMsg(SRpcHead* pHead) {
|
||||
int overhead = sizeof(SRpcComp);
|
||||
SRpcHead* pNewHead = NULL;
|
||||
|
@ -114,4 +187,8 @@ SRpcHead* rpcDecompressRpcMsg(SRpcHead* pHead) {
|
|||
return pHead;
|
||||
}
|
||||
|
||||
void transConnCtxDestroy(STransConnCtx* ctx) {
|
||||
free(ctx->ip);
|
||||
free(ctx);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -16,13 +16,6 @@
|
|||
#ifdef USE_UV
|
||||
#include "transComm.h"
|
||||
|
||||
typedef struct SConnBuffer {
|
||||
char* buf;
|
||||
int len;
|
||||
int cap;
|
||||
int left;
|
||||
} SConnBuffer;
|
||||
|
||||
typedef struct SConn {
|
||||
uv_tcp_t* pTcp;
|
||||
uv_write_t* pWriter;
|
||||
|
@ -100,31 +93,32 @@ static void* acceptThread(void* arg);
|
|||
void uvAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
||||
/*
|
||||
* formate of data buffer:
|
||||
* |<-------SRpcReqContext------->|<------------data read from socket----------->|
|
||||
* |<--------------------------data from socket------------------------------->|
|
||||
* |<------STransMsgHead------->|<-------------------other data--------------->|
|
||||
*/
|
||||
static const int CAPACITY = 1024;
|
||||
|
||||
SConn* conn = handle->data;
|
||||
SConnBuffer* pBuf = &conn->connBuf;
|
||||
if (pBuf->cap == 0) {
|
||||
pBuf->buf = (char*)calloc(CAPACITY + RPC_RESERVE_SIZE, sizeof(char));
|
||||
pBuf->buf = (char*)calloc(CAPACITY, sizeof(char));
|
||||
pBuf->len = 0;
|
||||
pBuf->cap = CAPACITY;
|
||||
pBuf->left = -1;
|
||||
|
||||
buf->base = pBuf->buf + RPC_RESERVE_SIZE;
|
||||
buf->base = pBuf->buf;
|
||||
buf->len = CAPACITY;
|
||||
} else {
|
||||
if (pBuf->len >= pBuf->cap) {
|
||||
if (pBuf->left == -1) {
|
||||
pBuf->cap *= 2;
|
||||
pBuf->buf = realloc(pBuf->buf, pBuf->cap + RPC_RESERVE_SIZE);
|
||||
pBuf->buf = realloc(pBuf->buf, pBuf->cap);
|
||||
} else if (pBuf->len + pBuf->left > pBuf->cap) {
|
||||
pBuf->cap = pBuf->len + pBuf->left;
|
||||
pBuf->buf = realloc(pBuf->buf, pBuf->len + pBuf->left + RPC_RESERVE_SIZE);
|
||||
pBuf->buf = realloc(pBuf->buf, pBuf->len + pBuf->left);
|
||||
}
|
||||
}
|
||||
buf->base = pBuf->buf + pBuf->len + RPC_RESERVE_SIZE;
|
||||
buf->base = pBuf->buf + pBuf->len;
|
||||
buf->len = pBuf->cap - pBuf->len;
|
||||
}
|
||||
}
|
||||
|
@ -133,11 +127,11 @@ void uvAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* b
|
|||
//
|
||||
static bool readComplete(SConnBuffer* data) {
|
||||
// TODO(yihao): handle pipeline later
|
||||
SRpcHead rpcHead;
|
||||
int32_t headLen = sizeof(rpcHead);
|
||||
STransMsgHead head;
|
||||
int32_t headLen = sizeof(head);
|
||||
if (data->len >= headLen) {
|
||||
memcpy((char*)&rpcHead, data->buf + RPC_RESERVE_SIZE, headLen);
|
||||
int32_t msgLen = (int32_t)htonl((uint32_t)rpcHead.msgLen);
|
||||
memcpy((char*)&head, data->buf, headLen);
|
||||
int32_t msgLen = (int32_t)htonl((uint32_t)head.msgLen);
|
||||
if (msgLen > data->len) {
|
||||
data->left = msgLen - data->len;
|
||||
return false;
|
||||
|
@ -150,20 +144,20 @@ static bool readComplete(SConnBuffer* data) {
|
|||
}
|
||||
|
||||
static void uvDoProcess(SRecvInfo* pRecv) {
|
||||
SRpcHead* pHead = (SRpcHead*)pRecv->msg;
|
||||
// impl later
|
||||
STransMsgHead* pHead = (STransMsgHead*)pRecv->msg;
|
||||
SRpcInfo* pRpc = (SRpcInfo*)pRecv->shandle;
|
||||
SConn* pConn = pRecv->thandle;
|
||||
|
||||
tDump(pRecv->msg, pRecv->msgLen);
|
||||
|
||||
terrno = 0;
|
||||
SRpcReqContext* pContest;
|
||||
// SRpcReqContext* pContest;
|
||||
|
||||
// do auth and check
|
||||
}
|
||||
|
||||
static int uvAuthMsg(SConn* pConn, char* msg, int len) {
|
||||
SRpcHead* pHead = (SRpcHead*)msg;
|
||||
STransMsgHead* pHead = (STransMsgHead*)msg;
|
||||
|
||||
int code = 0;
|
||||
|
||||
if ((pConn->secured && pHead->spi == 0) || (pHead->spi == 0 && pConn->spi == 0)) {
|
||||
|
@ -216,15 +210,15 @@ static int uvAuthMsg(SConn* pConn, char* msg, int len) {
|
|||
|
||||
// refers specifically to query or insert timeout
|
||||
static void uvHandleActivityTimeout(uv_timer_t* handle) {
|
||||
// impl later
|
||||
SConn* conn = handle->data;
|
||||
tDebug("%p timeout since no activity", conn);
|
||||
}
|
||||
|
||||
static void uvProcessData(SConn* pConn) {
|
||||
SRecvInfo info;
|
||||
SRecvInfo* p = &info;
|
||||
SConnBuffer* pBuf = &pConn->connBuf;
|
||||
p->msg = pBuf->buf + RPC_RESERVE_SIZE;
|
||||
p->msg = pBuf->buf;
|
||||
p->msgLen = pBuf->len;
|
||||
p->ip = 0;
|
||||
p->port = 0;
|
||||
|
@ -233,28 +227,33 @@ static void uvProcessData(SConn* pConn) {
|
|||
p->chandle = NULL;
|
||||
|
||||
//
|
||||
SRpcHead* pHead = (SRpcHead*)p->msg;
|
||||
assert(rpcIsReq(pHead->msgType));
|
||||
STransMsgHead* pHead = (STransMsgHead*)p->msg;
|
||||
assert(transIsReq(pHead->msgType));
|
||||
|
||||
SRpcInfo* pRpc = (SRpcInfo*)p->shandle;
|
||||
pConn->ahandle = (void*)pHead->ahandle;
|
||||
// auth here
|
||||
// auth should not do in rpc thread
|
||||
|
||||
int8_t code = uvAuthMsg(pConn, (char*)pHead, p->msgLen);
|
||||
if (code != 0) {
|
||||
terrno = code;
|
||||
return;
|
||||
}
|
||||
// int8_t code = uvAuthMsg(pConn, (char*)pHead, p->msgLen);
|
||||
// if (code != 0) {
|
||||
// terrno = code;
|
||||
// return;
|
||||
//}
|
||||
pHead->code = htonl(pHead->code);
|
||||
|
||||
int32_t dlen = 0;
|
||||
SRpcMsg rpcMsg;
|
||||
|
||||
pHead = rpcDecompressRpcMsg(pHead);
|
||||
rpcMsg.contLen = rpcContLenFromMsg(pHead->msgLen);
|
||||
if (transDecompressMsg(NULL, 0, NULL)) {
|
||||
// add compress later
|
||||
// pHead = rpcDecompressRpcMsg(pHead);
|
||||
} else {
|
||||
// impl later
|
||||
}
|
||||
rpcMsg.contLen = transContLenFromMsg(pHead->msgLen);
|
||||
rpcMsg.pCont = pHead->content;
|
||||
rpcMsg.msgType = pHead->msgType;
|
||||
rpcMsg.code = pHead->code;
|
||||
rpcMsg.ahandle = pConn->ahandle;
|
||||
rpcMsg.ahandle = NULL;
|
||||
rpcMsg.handle = pConn;
|
||||
|
||||
(*(pRpc->cfp))(pRpc->parent, &rpcMsg, NULL);
|
||||
|
@ -265,13 +264,13 @@ static void uvProcessData(SConn* pConn) {
|
|||
|
||||
void uvOnReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) {
|
||||
// opt
|
||||
SConn* ctx = cli->data;
|
||||
SConnBuffer* pBuf = &ctx->connBuf;
|
||||
SConn* conn = cli->data;
|
||||
SConnBuffer* pBuf = &conn->connBuf;
|
||||
if (nread > 0) {
|
||||
pBuf->len += nread;
|
||||
if (readComplete(pBuf)) {
|
||||
tDebug("alread read complete packet");
|
||||
uvProcessData(ctx);
|
||||
uvProcessData(conn);
|
||||
} else {
|
||||
tDebug("read half packet, continue to read");
|
||||
}
|
||||
|
@ -320,6 +319,9 @@ void uvWorkerAsyncCb(uv_async_t* handle) {
|
|||
return;
|
||||
}
|
||||
uv_buf_t wb = uv_buf_init(conn->writeBuf.buf, conn->writeBuf.len);
|
||||
|
||||
uv_timer_stop(conn->pTimer);
|
||||
|
||||
uv_write(conn->pWriter, (uv_stream_t*)conn->pTcp, &wb, 1, uvOnWriteCb);
|
||||
}
|
||||
}
|
||||
|
@ -423,7 +425,7 @@ void* workerThread(void* arg) {
|
|||
uv_loop_init(pThrd->loop);
|
||||
|
||||
// SRpcInfo* pRpc = pThrd->shandle;
|
||||
uv_pipe_init(pThrd->loop, pThrd->pipe, 1);
|
||||
uv_pipe_init(pThrd->loop, pThrd->pipe, 0);
|
||||
uv_pipe_open(pThrd->pipe, pThrd->fd);
|
||||
|
||||
pThrd->pipe->data = pThrd;
|
||||
|
@ -491,6 +493,7 @@ void* taosInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads,
|
|||
|
||||
for (int i = 0; i < srv->numOfThreads; i++) {
|
||||
SWorkThrdObj* thrd = (SWorkThrdObj*)calloc(1, sizeof(SWorkThrdObj));
|
||||
|
||||
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) {
|
||||
|
@ -522,6 +525,22 @@ void* taosInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads,
|
|||
|
||||
return srv;
|
||||
}
|
||||
void taosCloseServer(void* arg) {
|
||||
// impl later
|
||||
SServerObj* srv = arg;
|
||||
for (int i = 0; i < srv->numOfThreads; i++) {
|
||||
SWorkThrdObj* pThrd = srv->pThreadObj[i];
|
||||
pthread_join(pThrd->thread, NULL);
|
||||
free(srv->pipe[i]);
|
||||
free(pThrd->loop);
|
||||
free(pThrd);
|
||||
}
|
||||
free(srv->loop);
|
||||
free(srv->pipe);
|
||||
free(srv->pThreadObj);
|
||||
pthread_join(srv->thread, NULL);
|
||||
free(srv);
|
||||
}
|
||||
|
||||
void rpcSendResponse(const SRpcMsg* pMsg) {
|
||||
SConn* pConn = pMsg->handle;
|
||||
|
|
|
@ -40,6 +40,7 @@ static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) {
|
|||
if (pEpSet) pInfo->epSet = *pEpSet;
|
||||
|
||||
rpcFreeCont(pMsg->pCont);
|
||||
// tsem_post(&pInfo->rspSem);
|
||||
tsem_post(&pInfo->rspSem);
|
||||
}
|
||||
|
||||
|
@ -60,6 +61,7 @@ static void *sendRequest(void *param) {
|
|||
// tDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num);
|
||||
rpcSendRequest(pInfo->pRpc, &pInfo->epSet, &rpcMsg, NULL);
|
||||
if (pInfo->num % 20000 == 0) tInfo("thread:%d, %d requests have been sent", pInfo->index, pInfo->num);
|
||||
// tsem_wait(&pInfo->rspSem);
|
||||
tsem_wait(&pInfo->rspSem);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,196 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "os.h"
|
||||
#include "tutil.h"
|
||||
#include "tglobal.h"
|
||||
#include "rpcLog.h"
|
||||
#include "trpc.h"
|
||||
#include "taoserror.h"
|
||||
|
||||
typedef struct {
|
||||
int index;
|
||||
SRpcEpSet epSet;
|
||||
int num;
|
||||
int numOfReqs;
|
||||
int msgSize;
|
||||
tsem_t rspSem;
|
||||
tsem_t *pOverSem;
|
||||
pthread_t thread;
|
||||
void *pRpc;
|
||||
} SInfo;
|
||||
|
||||
|
||||
static int tcount = 0;
|
||||
static int terror = 0;
|
||||
|
||||
static void *sendRequest(void *param) {
|
||||
SInfo *pInfo = (SInfo *)param;
|
||||
SRpcMsg rpcMsg, rspMsg;
|
||||
|
||||
tDebug("thread:%d, start to send request", pInfo->index);
|
||||
|
||||
while ( pInfo->numOfReqs == 0 || pInfo->num < pInfo->numOfReqs) {
|
||||
pInfo->num++;
|
||||
rpcMsg.pCont = rpcMallocCont(pInfo->msgSize);
|
||||
rpcMsg.contLen = pInfo->msgSize;
|
||||
rpcMsg.handle = pInfo;
|
||||
rpcMsg.msgType = 1;
|
||||
tDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num);
|
||||
|
||||
rpcSendRecv(pInfo->pRpc, &pInfo->epSet, &rpcMsg, &rspMsg);
|
||||
|
||||
// handle response
|
||||
if (rspMsg.code != 0) terror++;
|
||||
|
||||
tDebug("thread:%d, rspLen:%d code:%d", pInfo->index, rspMsg.contLen, rspMsg.code);
|
||||
|
||||
rpcFreeCont(rspMsg.pCont);
|
||||
|
||||
if ( pInfo->num % 20000 == 0 )
|
||||
tInfo("thread:%d, %d requests have been sent", pInfo->index, pInfo->num);
|
||||
}
|
||||
|
||||
tDebug("thread:%d, it is over", pInfo->index);
|
||||
tcount++;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
SRpcInit rpcInit;
|
||||
SRpcEpSet epSet;
|
||||
int msgSize = 128;
|
||||
int numOfReqs = 0;
|
||||
int appThreads = 1;
|
||||
char serverIp[40] = "127.0.0.1";
|
||||
char secret[TSDB_KEY_LEN] = "mypassword";
|
||||
struct timeval systemTime;
|
||||
int64_t startTime, endTime;
|
||||
pthread_attr_t thattr;
|
||||
|
||||
// server info
|
||||
epSet.numOfEps = 1;
|
||||
epSet.inUse = 0;
|
||||
epSet.port[0] = 7000;
|
||||
epSet.port[1] = 7000;
|
||||
strcpy(epSet.fqdn[0], serverIp);
|
||||
strcpy(epSet.fqdn[1], "192.168.0.1");
|
||||
|
||||
// client info
|
||||
memset(&rpcInit, 0, sizeof(rpcInit));
|
||||
//rpcInit.localIp = "0.0.0.0";
|
||||
rpcInit.localPort = 0;
|
||||
rpcInit.label = "APP";
|
||||
rpcInit.numOfThreads = 1;
|
||||
rpcInit.sessions = 100;
|
||||
rpcInit.idleTime = tsShellActivityTimer*1000;
|
||||
rpcInit.user = "michael";
|
||||
rpcInit.secret = secret;
|
||||
rpcInit.ckey = "key";
|
||||
rpcInit.spi = 1;
|
||||
rpcInit.connType = TAOS_CONN_CLIENT;
|
||||
|
||||
for (int i=1; i<argc; ++i) {
|
||||
if (strcmp(argv[i], "-p")==0 && i < argc-1) {
|
||||
epSet.port[0] = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-i") ==0 && i < argc-1) {
|
||||
tstrncpy(epSet.fqdn[0], argv[++i], sizeof(epSet.fqdn[0]));
|
||||
} else if (strcmp(argv[i], "-t")==0 && i < argc-1) {
|
||||
rpcInit.numOfThreads = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-m")==0 && i < argc-1) {
|
||||
msgSize = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-s")==0 && i < argc-1) {
|
||||
rpcInit.sessions = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-n")==0 && i < argc-1) {
|
||||
numOfReqs = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-a")==0 && i < argc-1) {
|
||||
appThreads = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-o")==0 && i < argc-1) {
|
||||
tsCompressMsgSize = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-u")==0 && i < argc-1) {
|
||||
rpcInit.user = argv[++i];
|
||||
} else if (strcmp(argv[i], "-k")==0 && i < argc-1) {
|
||||
rpcInit.secret = argv[++i];
|
||||
} else if (strcmp(argv[i], "-spi")==0 && i < argc-1) {
|
||||
rpcInit.spi = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-d")==0 && i < argc-1) {
|
||||
rpcDebugFlag = atoi(argv[++i]);
|
||||
} else {
|
||||
printf("\nusage: %s [options] \n", argv[0]);
|
||||
printf(" [-i ip]: first server IP address, default is:%s\n", serverIp);
|
||||
printf(" [-p port]: server port number, default is:%d\n", epSet.port[0]);
|
||||
printf(" [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads);
|
||||
printf(" [-s sessions]: number of rpc sessions, default is:%d\n", rpcInit.sessions);
|
||||
printf(" [-m msgSize]: message body size, default is:%d\n", msgSize);
|
||||
printf(" [-a threads]: number of app threads, default is:%d\n", appThreads);
|
||||
printf(" [-n requests]: number of requests per thread, default is:%d\n", numOfReqs);
|
||||
printf(" [-o compSize]: compression message size, default is:%d\n", tsCompressMsgSize);
|
||||
printf(" [-u user]: user name for the connection, default is:%s\n", rpcInit.user);
|
||||
printf(" [-k secret]: password for the connection, default is:%s\n", rpcInit.secret);
|
||||
printf(" [-spi SPI]: security parameter index, default is:%d\n", rpcInit.spi);
|
||||
printf(" [-d debugFlag]: debug flag, default:%d\n", rpcDebugFlag);
|
||||
printf(" [-h help]: print out this help\n\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
taosInitLog("client.log", 100000, 10);
|
||||
|
||||
void *pRpc = rpcOpen(&rpcInit);
|
||||
if (pRpc == NULL) {
|
||||
tError("failed to initialize RPC");
|
||||
return -1;
|
||||
}
|
||||
|
||||
tInfo("client is initialized");
|
||||
|
||||
gettimeofday(&systemTime, NULL);
|
||||
startTime = systemTime.tv_sec*1000000 + systemTime.tv_usec;
|
||||
|
||||
SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo)*appThreads);
|
||||
|
||||
pthread_attr_init(&thattr);
|
||||
pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
|
||||
|
||||
for (int i=0; i<appThreads; ++i) {
|
||||
pInfo->index = i;
|
||||
pInfo->epSet = epSet;
|
||||
pInfo->numOfReqs = numOfReqs;
|
||||
pInfo->msgSize = msgSize;
|
||||
tsem_init(&pInfo->rspSem, 0, 0);
|
||||
pInfo->pRpc = pRpc;
|
||||
pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo);
|
||||
pInfo++;
|
||||
}
|
||||
|
||||
do {
|
||||
usleep(1);
|
||||
} while ( tcount < appThreads);
|
||||
|
||||
gettimeofday(&systemTime, NULL);
|
||||
endTime = systemTime.tv_sec*1000000 + systemTime.tv_usec;
|
||||
float usedTime = (endTime - startTime)/1000.0; // mseconds
|
||||
|
||||
tInfo("it takes %.3f mseconds to send %d requests to server, error num:%d", usedTime, numOfReqs*appThreads, terror);
|
||||
tInfo("Performance: %.3f requests per second, msgSize:%d bytes", 1000.0*numOfReqs*appThreads/usedTime, msgSize);
|
||||
|
||||
taosCloseLog();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,207 @@
|
|||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "task.h"
|
||||
#include <uv.h>
|
||||
|
||||
#define NUM_OF_THREAD 1
|
||||
#define TIMEOUT 10000
|
||||
|
||||
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;
|
||||
|
||||
void echo_write(uv_write_t *req, int status) {
|
||||
if (status < 0) {
|
||||
fprintf(stderr, "Write error %s\n", uv_err_name(status));
|
||||
}
|
||||
printf("write data to client\n");
|
||||
free(req);
|
||||
}
|
||||
|
||||
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
|
||||
|
||||
SConnCtx *pConn = container_of(client, SConnCtx, pClient);
|
||||
pConn->ref += 1;
|
||||
printf("read data %d\n", nread, buf->base, buf->len);
|
||||
if (nread > 0) {
|
||||
uv_write_t *req = (uv_write_t *)malloc(sizeof(uv_write_t));
|
||||
// dispatch request to database other process thread
|
||||
// just write out
|
||||
uv_buf_t write_out;
|
||||
write_out.base = buf->base;
|
||||
write_out.len = nread;
|
||||
uv_write((uv_write_t *)req, client, &write_out, 1, echo_write);
|
||||
free(buf->base);
|
||||
return;
|
||||
}
|
||||
|
||||
if (nread < 0) {
|
||||
if (nread != UV_EOF)
|
||||
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
|
||||
uv_close((uv_handle_t *)client, NULL);
|
||||
}
|
||||
free(buf->base);
|
||||
}
|
||||
|
||||
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
|
||||
buf->base = malloc(suggested_size);
|
||||
buf->len = suggested_size;
|
||||
}
|
||||
|
||||
void on_new_connection(uv_stream_t *s, int status) {
|
||||
if (status == -1) {
|
||||
// error!
|
||||
return;
|
||||
}
|
||||
SServerObj *pObj = container_of(s, SServerObj, server);
|
||||
printf("new_connection from client\n");
|
||||
|
||||
uv_tcp_t *client = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
|
||||
uv_tcp_init(pObj->loop, client);
|
||||
if (uv_accept(s, (uv_stream_t *)client) == 0) {
|
||||
uv_write_t *write_req = (uv_write_t *)malloc(sizeof(uv_write_t));
|
||||
uv_buf_t dummy_buf = uv_buf_init("a", 1);
|
||||
// despatch to worker thread
|
||||
pObj->workerIdx = (pObj->workerIdx + 1) % pObj->numOfThread;
|
||||
uv_write2(write_req, (uv_stream_t *)&(pObj->pipe[pObj->workerIdx][0]),
|
||||
&dummy_buf, 1, (uv_stream_t *)client, echo_write);
|
||||
} else {
|
||||
uv_close((uv_handle_t *)client, NULL);
|
||||
}
|
||||
}
|
||||
void child_on_new_connection(uv_stream_t *q, ssize_t nread,
|
||||
const uv_buf_t *buf) {
|
||||
printf("x child_on_new_connection \n");
|
||||
if (nread < 0) {
|
||||
if (nread != UV_EOF)
|
||||
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
|
||||
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)) {
|
||||
fprintf(stderr, "No pending count\n");
|
||||
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);
|
||||
fprintf(stderr, "Worker Accepted fd %d\n", fd);
|
||||
uv_timer_start(pConn->pTimer, timeOutCallBack, TIMEOUT, 0);
|
||||
uv_read_start((uv_stream_t *)(pConn->pClient), alloc_buffer, echo_read);
|
||||
} else {
|
||||
uv_timer_stop(pConn->pTimer);
|
||||
free(pConn->pTimer);
|
||||
uv_close((uv_handle_t *)pConn->pClient, NULL);
|
||||
free(pConn->pClient);
|
||||
free(pConn);
|
||||
}
|
||||
}
|
||||
|
||||
static void workerAsyncCallback(uv_async_t *handle) {
|
||||
SThreadObj *pObj = container_of(handle, SThreadObj, workerAsync);
|
||||
// do nothing
|
||||
}
|
||||
void *worker_thread(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, workerAsyncCallback);
|
||||
uv_read_start((uv_stream_t *)pObj->pipe, alloc_buffer,
|
||||
child_on_new_connection);
|
||||
|
||||
uv_run(pObj->loop, UV_RUN_DEFAULT);
|
||||
}
|
||||
int main() {
|
||||
|
||||
SServerObj *server = calloc(1, sizeof(SServerObj));
|
||||
server->loop = (uv_loop_t *)malloc(sizeof(uv_loop_t));
|
||||
server->numOfThread = NUM_OF_THREAD;
|
||||
server->workerIdx = 0;
|
||||
server->pThreadObj =
|
||||
(SThreadObj **)calloc(server->numOfThread, sizeof(SThreadObj *));
|
||||
server->pipe = (uv_pipe_t **)calloc(server->numOfThread, sizeof(uv_pipe_t *));
|
||||
|
||||
uv_loop_init(server->loop);
|
||||
|
||||
for (int i = 0; i < server->numOfThread; i++) {
|
||||
server->pThreadObj[i] = (SThreadObj *)calloc(1, sizeof(SThreadObj));
|
||||
server->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 -1;
|
||||
}
|
||||
uv_pipe_init(server->loop, &(server->pipe[i][0]), 1);
|
||||
uv_pipe_open(&(server->pipe[i][0]), fds[1]); // init write
|
||||
|
||||
server->pThreadObj[i]->fd = fds[0];
|
||||
server->pThreadObj[i]->pipe = &(server->pipe[i][1]); // init read
|
||||
int err = pthread_create(&(server->pThreadObj[i]->thread), NULL,
|
||||
worker_thread, (void *)(server->pThreadObj[i]));
|
||||
if (err == 0) {
|
||||
printf("thread %d create\n", i);
|
||||
} else {
|
||||
printf("thread %d create failed", i);
|
||||
}
|
||||
|
||||
uv_tcp_init(server->loop, &server->server);
|
||||
struct sockaddr_in bind_addr;
|
||||
uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
|
||||
uv_tcp_bind(&server->server, (const struct sockaddr *)&bind_addr, 0);
|
||||
int err = 0;
|
||||
if ((err = uv_listen((uv_stream_t *)&server->server, 128,
|
||||
on_new_connection)) != 0) {
|
||||
fprintf(stderr, "Listen error %s\n", uv_err_name(err));
|
||||
return 2;
|
||||
}
|
||||
uv_run(server->loop, UV_RUN_DEFAULT);
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -182,8 +182,19 @@ void *threadFunc(void *param) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
// printf("thread:%d, table range: %"PRId64 " - %"PRId64 "\n", pInfo->threadIndex, pInfo->tableBeginIndex,
|
||||
// pInfo->tableEndIndex);
|
||||
pError("====before thread:%d, table range: %"PRId64 " - %"PRId64 "\n",
|
||||
pInfo->threadIndex,
|
||||
pInfo->tableBeginIndex,
|
||||
pInfo->tableEndIndex);
|
||||
|
||||
pInfo->tableBeginIndex += startOffset;
|
||||
pInfo->tableEndIndex += startOffset;
|
||||
|
||||
pError("====after thread:%d, table range: %"PRId64 " - %"PRId64 "\n",
|
||||
pInfo->threadIndex,
|
||||
pInfo->tableBeginIndex,
|
||||
pInfo->tableEndIndex);
|
||||
|
||||
sprintf(qstr, "use %s", pInfo->dbName);
|
||||
TAOS_RES *pRes = taos_query(con, qstr);
|
||||
taos_free_result(pRes);
|
||||
|
@ -211,7 +222,7 @@ void *threadFunc(void *param) {
|
|||
TAOS_RES *pRes = taos_query(con, qstr);
|
||||
code = taos_errno(pRes);
|
||||
if ((code != 0) && (code != TSDB_CODE_RPC_AUTH_REQUIRED)) {
|
||||
pError("failed to create table t%" PRId64 ", reason:%s", t, tstrerror(code));
|
||||
pError("failed to create table reason:%s, sql: %s", tstrerror(code), qstr);
|
||||
}
|
||||
taos_free_result(pRes);
|
||||
int64_t endTs = taosGetTimestampUs();
|
||||
|
@ -297,7 +308,7 @@ void printHelp() {
|
|||
printf("%s%s%s%d\n", indent, indent, "numOfThreads, default is ", numOfThreads);
|
||||
printf("%s%s\n", indent, "-n");
|
||||
printf("%s%s%s%" PRId64 "\n", indent, indent, "numOfTables, default is ", numOfTables);
|
||||
printf("%s%s\n", indent, "-o");
|
||||
printf("%s%s\n", indent, "-g");
|
||||
printf("%s%s%s%" PRId64 "\n", indent, indent, "startOffset, default is ", startOffset);
|
||||
printf("%s%s\n", indent, "-v");
|
||||
printf("%s%s%s%d\n", indent, indent, "numOfVgroups, default is ", numOfVgroups);
|
||||
|
@ -332,7 +343,7 @@ void parseArgument(int32_t argc, char *argv[]) {
|
|||
numOfThreads = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-n") == 0) {
|
||||
numOfTables = atoll(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-o") == 0) {
|
||||
} else if (strcmp(argv[i], "-g") == 0) {
|
||||
startOffset = atoll(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-v") == 0) {
|
||||
numOfVgroups = atoi(argv[++i]);
|
||||
|
@ -412,8 +423,8 @@ int32_t main(int32_t argc, char *argv[]) {
|
|||
|
||||
int64_t tableFrom = 0;
|
||||
for (int32_t i = 0; i < numOfThreads; ++i) {
|
||||
pInfo[i].tableBeginIndex = tableFrom + startOffset;
|
||||
pInfo[i].tableEndIndex = (i < b ? tableFrom + a : tableFrom + a - 1) + startOffset;
|
||||
pInfo[i].tableBeginIndex = tableFrom;
|
||||
pInfo[i].tableEndIndex = (i < b ? tableFrom + a : tableFrom + a - 1);
|
||||
tableFrom = pInfo[i].tableEndIndex + 1;
|
||||
pInfo[i].threadIndex = i;
|
||||
pInfo[i].minDelay = INT64_MAX;
|
||||
|
|
Loading…
Reference in New Issue