From fa3fce9c9deab7741071591a4c2cf2f9a734295e Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 4 Mar 2022 15:53:30 +0800 Subject: [PATCH 01/29] [td-13039] refactor. --- include/client/taos.h | 40 +++++----- source/libs/executor/inc/executorimpl.h | 10 +-- source/libs/executor/src/executorimpl.c | 102 +++++++++++++++++------- 3 files changed, 96 insertions(+), 56 deletions(-) diff --git a/include/client/taos.h b/include/client/taos.h index 8b1517c6ff..d3edd93c37 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -31,26 +31,26 @@ typedef void TAOS_SUB; typedef void **TAOS_ROW; // Data type definition -#define TSDB_DATA_TYPE_NULL 0 // 1 bytes -#define TSDB_DATA_TYPE_BOOL 1 // 1 bytes -#define TSDB_DATA_TYPE_TINYINT 2 // 1 byte -#define TSDB_DATA_TYPE_SMALLINT 3 // 2 bytes -#define TSDB_DATA_TYPE_INT 4 // 4 bytes -#define TSDB_DATA_TYPE_BIGINT 5 // 8 bytes -#define TSDB_DATA_TYPE_FLOAT 6 // 4 bytes -#define TSDB_DATA_TYPE_DOUBLE 7 // 8 bytes -#define TSDB_DATA_TYPE_BINARY 8 // string, alias for varchar -#define TSDB_DATA_TYPE_TIMESTAMP 9 // 8 bytes -#define TSDB_DATA_TYPE_NCHAR 10 // unicode string -#define TSDB_DATA_TYPE_UTINYINT 11 // 1 byte -#define TSDB_DATA_TYPE_USMALLINT 12 // 2 bytes -#define TSDB_DATA_TYPE_UINT 13 // 4 bytes -#define TSDB_DATA_TYPE_UBIGINT 14 // 8 bytes -#define TSDB_DATA_TYPE_VARCHAR 15 // string -#define TSDB_DATA_TYPE_VARBINARY 16 // binary -#define TSDB_DATA_TYPE_JSON 17 // json -#define TSDB_DATA_TYPE_DECIMAL 18 // decimal -#define TSDB_DATA_TYPE_BLOB 19 // binary +#define TSDB_DATA_TYPE_NULL 0 // 1 bytes +#define TSDB_DATA_TYPE_BOOL 1 // 1 bytes +#define TSDB_DATA_TYPE_TINYINT 2 // 1 byte +#define TSDB_DATA_TYPE_SMALLINT 3 // 2 bytes +#define TSDB_DATA_TYPE_INT 4 // 4 bytes +#define TSDB_DATA_TYPE_BIGINT 5 // 8 bytes +#define TSDB_DATA_TYPE_FLOAT 6 // 4 bytes +#define TSDB_DATA_TYPE_DOUBLE 7 // 8 bytes +#define TSDB_DATA_TYPE_BINARY 8 // string, alias for varchar +#define TSDB_DATA_TYPE_TIMESTAMP 9 // 8 bytes +#define TSDB_DATA_TYPE_NCHAR 10 // unicode string +#define TSDB_DATA_TYPE_UTINYINT 11 // 1 byte +#define TSDB_DATA_TYPE_USMALLINT 12 // 2 bytes +#define TSDB_DATA_TYPE_UINT 13 // 4 bytes +#define TSDB_DATA_TYPE_UBIGINT 14 // 8 bytes +#define TSDB_DATA_TYPE_JSON 15 // json string +#define TSDB_DATA_TYPE_VARCHAR 16 // string +#define TSDB_DATA_TYPE_VARBINARY 17 // binary +#define TSDB_DATA_TYPE_DECIMAL 18 // decimal +#define TSDB_DATA_TYPE_BLOB 19 // binary typedef enum { TSDB_OPTION_LOCALE, diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 991cd372c3..2280295c13 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -264,7 +264,8 @@ typedef struct SExecTaskInfo { uint64_t totalRows; // total number of rows STableGroupInfo tableqinfoGroupInfo; // this is a group array list, including SArray structure char* sql; // query sql string - jmp_buf env; // + jmp_buf env; // when error occurs, abort + int32_t bufSize; // available buffer size for all operator struct SOperatorInfo* pRoot; } SExecTaskInfo; @@ -322,6 +323,7 @@ typedef struct SOperatorInfo { SExprInfo* pExpr; STaskRuntimeEnv* pRuntimeEnv; // todo remove it SExecTaskInfo* pTaskInfo; + SOperatorCostInfo cost; struct SOperatorInfo** pDownstream; // downstram pointer list int32_t numOfDownstream; // number of downstream. The value is always ONE expect for join operator @@ -620,6 +622,8 @@ SOperatorInfo* createTableSeqScanOperatorInfo(void* pTsdbReadHandle, STaskRuntim SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SExecTaskInfo* pTaskInfo); +SOperatorInfo* createOrderOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SArray* pOrderVal, SExecTaskInfo* pTaskInfo); +SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t numOfDownstream, SArray* pExprInfo, SArray* pOrderVal, SArray* pGroupInfo, SExecTaskInfo* pTaskInfo); SOperatorInfo* createLimitOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream); SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SInterval* pInterval, SExecTaskInfo* pTaskInfo); @@ -655,10 +659,6 @@ SOperatorInfo* createFilterOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorI SOperatorInfo* createJoinOperatorInfo(SOperatorInfo** pdownstream, int32_t numOfDownstream, SSchema* pSchema, int32_t numOfOutput); -SOperatorInfo* createOrderOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SArray* pOrderVal, SExecTaskInfo* pTaskInfo); -SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t numOfDownstream, SArray* pExprInfo, SArray* pOrderVal, SArray* pGroupInfo, SExecTaskInfo* pTaskInfo); - -// SSDataBlock* doSLimit(void* param, bool* newgroup); // int32_t doCreateFilterInfo(SColumnInfo* pCols, int32_t numOfCols, int32_t numOfFilterCols, SSingleColumnFilterInfo** pFilterInfo, uint64_t qId); void doSetFilterColumnInfo(SSingleColumnFilterInfo* pFilterInfo, int32_t numOfFilterCols, SSDataBlock* pBlock); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 3ebad151fd..675fd3e39f 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -208,6 +208,9 @@ static void destroyOrderOperatorInfo(void* param, int32_t numOfOutput); static void destroySWindowOperatorInfo(void* param, int32_t numOfOutput); static void destroyStateWindowOperatorInfo(void* param, int32_t numOfOutput); static void destroyAggOperatorInfo(void* param, int32_t numOfOutput); +static void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput); +static void destroyConditionOperatorInfo(void* param, int32_t numOfOutput); + static void destroyOperatorInfo(SOperatorInfo* pOperator); static void doSetOperatorCompleted(SOperatorInfo* pOperator) { @@ -217,6 +220,10 @@ static void doSetOperatorCompleted(SOperatorInfo* pOperator) { } } +static void dummyOperatorOpenFn() { + return; +} + static int32_t doCopyToSDataBlock(SDiskbasedBuf *pBuf, SGroupResInfo* pGroupResInfo, int32_t orderType, SSDataBlock* pBlock, int32_t rowCapacity); static int32_t getGroupbyColumnIndex(SGroupbyExpr *pGroupbyExpr, SSDataBlock* pDataBlock); @@ -5236,28 +5243,10 @@ static SSDataBlock* doLoadRemoteData(void* param, bool* newgroup) { static SSDataBlock* createResultDataBlock(const SArray* pExprInfo); -SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* pExprInfo, SExecTaskInfo* pTaskInfo) { - SExchangeInfo* pInfo = calloc(1, sizeof(SExchangeInfo)); - SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); - - if (pInfo == NULL || pOperator == NULL) { - tfree(pInfo); - tfree(pOperator); - terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; - return NULL; - } - - size_t numOfSources = taosArrayGetSize(pSources); - - pInfo->pSources = taosArrayDup(pSources); +static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo) { pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo)); - if (pInfo->pSourceDataInfo == NULL || pInfo->pSources == NULL) { - tfree(pInfo); - tfree(pOperator); - taosArrayDestroy(pInfo->pSources); - taosArrayDestroy(pInfo->pSourceDataInfo); - terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; - return NULL; + if (pInfo->pSourceDataInfo == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; } for(int32_t i = 0; i < numOfSources; ++i) { @@ -5266,13 +5255,41 @@ SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* dataInfo.pEx = pInfo; dataInfo.index = i; - taosArrayPush(pInfo->pSourceDataInfo, &dataInfo); + void* ret = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo); + if (ret == NULL) { + taosArrayDestroy(pInfo->pSourceDataInfo); + return TSDB_CODE_OUT_OF_MEMORY; + } } - size_t size = taosArrayGetSize(pExprInfo); - pInfo->pResult = createResultDataBlock(pExprInfo); - pInfo->seqLoadData = true; + return TSDB_CODE_SUCCESS; +} +SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* pExprInfo, SExecTaskInfo* pTaskInfo) { + SExchangeInfo* pInfo = calloc(1, sizeof(SExchangeInfo)); + SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); + + if (pInfo == NULL || pOperator == NULL) { + goto _error; + } + + pInfo->pSources = taosArrayDup(pSources); + if (pInfo->pSources == NULL) { + goto _error; + } + + size_t numOfSources = taosArrayGetSize(pSources); + int32_t code = initDataSource(numOfSources, pInfo); + if (code != TSDB_CODE_SUCCESS) { + goto _error; + } + + pInfo->pResult = createResultDataBlock(pExprInfo); + if (pInfo->pResult == NULL) { + goto _error; + } + + pInfo->seqLoadData = true; // sequentially load data from the source node tsem_init(&pInfo->ready, 0, 0); pOperator->name = "ExchangeOperator"; @@ -5280,9 +5297,11 @@ SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* pOperator->blockingOptr = false; pOperator->status = OP_IN_EXECUTING; pOperator->info = pInfo; - pOperator->numOfOutput = size; - pOperator->nextDataFn = doLoadRemoteData; + pOperator->numOfOutput = taosArrayGetSize(pExprInfo); pOperator->pTaskInfo = pTaskInfo; + pOperator->openFn = NULL; // assign a dummy function. + pOperator->nextDataFn = doLoadRemoteData; + pOperator->closeFn = destroyExchangeOperatorInfo; #if 1 { // todo refactor @@ -5308,6 +5327,16 @@ SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* #endif return pOperator; + + _error: + if (pInfo != NULL) { + destroyExchangeOperatorInfo(pInfo, 0); + } + + tfree(pInfo); + tfree(pOperator); + terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; + return NULL; } SSDataBlock* createResultDataBlock(const SArray* pExprInfo) { @@ -7115,17 +7144,17 @@ static void destroyGroupbyOperatorInfo(void* param, int32_t numOfOutput) { tfree(pInfo->prevData); } -static void destroyProjectOperatorInfo(void* param, int32_t numOfOutput) { +void destroyProjectOperatorInfo(void* param, int32_t numOfOutput) { SProjectOperatorInfo* pInfo = (SProjectOperatorInfo*) param; doDestroyBasicInfo(&pInfo->binfo, numOfOutput); } -static void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput) { +void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput) { STagScanInfo* pInfo = (STagScanInfo*) param; pInfo->pRes = blockDataDestroy(pInfo->pRes); } -static void destroyOrderOperatorInfo(void* param, int32_t numOfOutput) { +void destroyOrderOperatorInfo(void* param, int32_t numOfOutput) { SOrderOperatorInfo* pInfo = (SOrderOperatorInfo*) param; pInfo->pDataBlock = blockDataDestroy(pInfo->pDataBlock); @@ -7145,6 +7174,17 @@ static void destroyDistinctOperatorInfo(void* param, int32_t numOfOutput) { pInfo->pRes = blockDataDestroy(pInfo->pRes); } +void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput) { + SExchangeInfo* pExInfo = (SExchangeInfo*) param; + taosArrayDestroy(pExInfo->pSources); + taosArrayDestroy(pExInfo->pSourceDataInfo); + if (pExInfo->pResult != NULL) { + blockDataDestroy(pExInfo->pResult); + } + + tsem_destroy(&pExInfo->ready); +} + SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo) { SAggOperatorInfo* pInfo = calloc(1, sizeof(SAggOperatorInfo)); @@ -7268,7 +7308,7 @@ SOperatorInfo* createLimitOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorIn pOperator->nextDataFn = doLimit; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - int32_t code = appendDownstream(pOperator, &downstream, 1); + int32_t code = appendDownstream(pOperator, &downstream, 1); return pOperator; } From 9c610369e27f7cb04bbdedf9d0db7efa9e3d2dc9 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 5 Mar 2022 15:58:28 +0800 Subject: [PATCH 02/29] [td-13039] refactor. --- include/libs/executor/dataSinkMgt.h | 1 - source/libs/executor/inc/executorimpl.h | 49 ++-- source/libs/executor/src/dataDispatcher.c | 19 +- source/libs/executor/src/executorMain.c | 2 +- source/libs/executor/src/executorimpl.c | 267 +++++++++----------- source/libs/executor/test/executorTests.cpp | 12 +- source/libs/qworker/src/qworker.c | 2 +- 7 files changed, 150 insertions(+), 202 deletions(-) diff --git a/include/libs/executor/dataSinkMgt.h b/include/libs/executor/dataSinkMgt.h index 19438b5dd4..08ae2212bc 100644 --- a/include/libs/executor/dataSinkMgt.h +++ b/include/libs/executor/dataSinkMgt.h @@ -40,7 +40,6 @@ int32_t dsDataSinkMgtInit(SDataSinkMgtCfg *cfg); typedef struct SInputData { const struct SSDataBlock* pData; - SHashObj* pTableRetrieveTsMap; } SInputData; typedef struct SOutputData { diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 2280295c13..53372cb2e3 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -253,6 +253,11 @@ typedef struct STaskIdInfo { char* str; } STaskIdInfo; +typedef struct STaskBufInfo { + int32_t bufSize; // total available buffer size in bytes + int32_t remainBuf; // remain buffer size +} STaskBufInfo; + typedef struct SExecTaskInfo { STaskIdInfo id; char* content; @@ -265,7 +270,7 @@ typedef struct SExecTaskInfo { STableGroupInfo tableqinfoGroupInfo; // this is a group array list, including SArray structure char* sql; // query sql string jmp_buf env; // when error occurs, abort - int32_t bufSize; // available buffer size for all operator + STaskBufInfo bufInfo; // available buffer info this task struct SOperatorInfo* pRoot; } SExecTaskInfo; @@ -308,9 +313,11 @@ typedef struct STaskRuntimeEnv { } STaskRuntimeEnv; enum { - OP_IN_EXECUTING = 1, - OP_RES_TO_RETURN = 2, - OP_EXEC_DONE = 3, + OP_NOT_OPENED = 0x0, + OP_OPENED = 0x1, + OP_IN_EXECUTING = 0x3, + OP_RES_TO_RETURN = 0x5, + OP_EXEC_DONE = 0x9, }; typedef struct SOperatorInfo { @@ -328,7 +335,7 @@ typedef struct SOperatorInfo { struct SOperatorInfo** pDownstream; // downstram pointer list int32_t numOfDownstream; // number of downstream. The value is always ONE expect for join operator __optr_open_fn_t openFn; - __optr_fn_t nextDataFn; + __optr_fn_t getNextFn; __optr_close_fn_t closeFn; } SOperatorInfo; @@ -380,9 +387,9 @@ typedef struct STaskParam { } STaskParam; enum { - DATA_NOT_READY = 0x1, - DATA_READY = 0x2, - DATA_EXHAUSTED = 0x3, + EX_SOURCE_DATA_NOT_READY = 0x1, + EX_SOURCE_DATA_READY = 0x2, + EX_SOURCE_DATA_EXHAUSTED = 0x3, }; typedef struct SSourceDataInfo { @@ -407,16 +414,16 @@ typedef struct SExchangeInfo { } SExchangeInfo; typedef struct STableScanInfo { - void* pTsdbReadHandle; - int32_t numOfBlocks; // extract basic running information. - int32_t numOfSkipped; - int32_t numOfBlockStatis; - int64_t numOfRows; + void* pTsdbReadHandle; + int32_t numOfBlocks; // extract basic running information. + int32_t numOfSkipped; + int32_t numOfBlockStatis; + int64_t numOfRows; - int32_t order; // scan order - int32_t times; // repeat counts - int32_t current; - int32_t reverseTimes; // 0 by default + int32_t order; // scan order + int32_t times; // repeat counts + int32_t current; + int32_t reverseTimes; // 0 by default SqlFunctionCtx* pCtx; // next operator query context SResultRowInfo* pResultRowInfo; @@ -427,7 +434,7 @@ typedef struct STableScanInfo { int64_t elapsedTime; int32_t prevGroupId; // previous table group id - int32_t scanFlag; // table scan flag to denote if it is a repeat/reverse/main scan + int32_t scanFlag; // table scan flag to denote if it is a repeat/reverse/main scan } STableScanInfo; typedef struct STagScanInfo { @@ -624,9 +631,9 @@ SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SArray SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SExecTaskInfo* pTaskInfo); SOperatorInfo* createOrderOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SArray* pOrderVal, SExecTaskInfo* pTaskInfo); SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t numOfDownstream, SArray* pExprInfo, SArray* pOrderVal, SArray* pGroupInfo, SExecTaskInfo* pTaskInfo); +SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SInterval* pInterval, SExecTaskInfo* pTaskInfo); SOperatorInfo* createLimitOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream); -SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SArray* pExprInfo, SInterval* pInterval, SExecTaskInfo* pTaskInfo); SOperatorInfo* createAllTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfOutput); @@ -672,13 +679,9 @@ void* doDestroyFilterInfo(SSingleColumnFilterInfo* pFilterInfo, int32_t numOfFil void setInputDataBlock(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order); void finalizeQueryResult(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset); -void updateOutputBuf(SOptrBasicInfo* pBInfo, int32_t* bufCapacity, int32_t numOfInputRows); void clearOutputBuf(SOptrBasicInfo* pBInfo, int32_t* bufCapacity); void copyTsColoum(SSDataBlock* pRes, SqlFunctionCtx* pCtx, int32_t numOfOutput); -int32_t createQueryFunc(SQueriedTableInfo* pTableInfo, int32_t numOfOutput, SExprInfo** pExprInfo, SSqlExpr** pExprMsg, - SColumnInfo* pTagCols, int32_t queryType, void* pMsg, struct SUdfInfo* pUdfInfo); - int32_t createIndirectQueryFuncExprFromMsg(SQueryTableReq* pQueryMsg, int32_t numOfOutput, SExprInfo** pExprInfo, SSqlExpr** pExpr, SExprInfo* prevExpr, struct SUdfInfo* pUdfInfo); diff --git a/source/libs/executor/src/dataDispatcher.c b/source/libs/executor/src/dataDispatcher.c index 3623f5947d..a9865e5dec 100644 --- a/source/libs/executor/src/dataDispatcher.c +++ b/source/libs/executor/src/dataDispatcher.c @@ -21,8 +21,6 @@ #include "tqueue.h" #include "executorimpl.h" -#define DATA_META_LENGTH(tables) (sizeof(int32_t) + sizeof(STableIdInfo) * taosHashGetSize(tables) + sizeof(SRetrieveTableRsp)) - typedef struct SDataDispatchBuf { int32_t useSize; int32_t allocSize; @@ -88,19 +86,6 @@ static void copyData(const SInputData* pInput, const SDataBlockSchema* pSchema, data += pColRes->info.bytes * pInput->pData->info.rows; } } - - int32_t numOfTables = (int32_t) taosHashGetSize(pInput->pTableRetrieveTsMap); - *(int32_t*)data = htonl(numOfTables); - data += sizeof(int32_t); - - STableIdInfo* item = taosHashIterate(pInput->pTableRetrieveTsMap, NULL); - while (item) { - STableIdInfo* pDst = (STableIdInfo*)data; - pDst->uid = htobe64(item->uid); - pDst->key = htobe64(item->key); - data += sizeof(STableIdInfo); - item = taosHashIterate(pInput->pTableRetrieveTsMap, item); - } } // data format with compress: SDataCacheEntry | cols_data_offset | col1_data col2_data ... | numOfTables | STableIdInfo STableIdInfo ... @@ -111,7 +96,7 @@ static void toDataCacheEntry(const SDataDispatchHandle* pHandle, const SInputDat pEntry->numOfRows = pInput->pData->info.rows; pEntry->dataLen = 0; - pBuf->useSize = DATA_META_LENGTH(pInput->pTableRetrieveTsMap); + pBuf->useSize = sizeof(SRetrieveTableRsp); copyData(pInput, &pHandle->schema, pEntry->data, pEntry->compressed, &pEntry->dataLen); if (0 == pEntry->compressed) { pEntry->dataLen = pHandle->schema.resultRowSize * pInput->pData->info.rows; @@ -128,7 +113,7 @@ static bool allocBuf(SDataDispatchHandle* pDispatcher, const SInputData* pInput, return false; } - pBuf->allocSize = DATA_META_LENGTH(pInput->pTableRetrieveTsMap) + pDispatcher->schema.resultRowSize * pInput->pData->info.rows; + pBuf->allocSize = sizeof(SRetrieveTableRsp) + pDispatcher->schema.resultRowSize * pInput->pData->info.rows; pBuf->pData = malloc(pBuf->allocSize); if (pBuf->pData == NULL) { qError("SinkNode failed to malloc memory, size:%d, code:%d", pBuf->allocSize, TAOS_SYSTEM_ERROR(errno)); diff --git a/source/libs/executor/src/executorMain.c b/source/libs/executor/src/executorMain.c index fabaa2d31d..7e55a4b3e1 100644 --- a/source/libs/executor/src/executorMain.c +++ b/source/libs/executor/src/executorMain.c @@ -158,7 +158,7 @@ int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t *useconds) { int64_t st = 0; st = taosGetTimestampUs(); - *pRes = pTaskInfo->pRoot->nextDataFn(pTaskInfo->pRoot, &newgroup); + *pRes = pTaskInfo->pRoot->getNextFn(pTaskInfo->pRoot, &newgroup); uint64_t el = (taosGetTimestampUs() - st); pTaskInfo->cost.elapsedTime += el; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 675fd3e39f..197bb97d3a 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -220,10 +220,14 @@ static void doSetOperatorCompleted(SOperatorInfo* pOperator) { } } -static void dummyOperatorOpenFn() { - return; +static int32_t operatorDummyOpenFn(void* param) { + SOperatorInfo* pOperator = (SOperatorInfo*) param; + pOperator->status = OP_OPENED; + return TSDB_CODE_SUCCESS; } +static void operatorDummyCloseFn(void* param, int32_t numOfCols) {} + static int32_t doCopyToSDataBlock(SDiskbasedBuf *pBuf, SGroupResInfo* pGroupResInfo, int32_t orderType, SSDataBlock* pBlock, int32_t rowCapacity); static int32_t getGroupbyColumnIndex(SGroupbyExpr *pGroupbyExpr, SSDataBlock* pDataBlock); @@ -4940,7 +4944,7 @@ int32_t loadRemoteDataCallback(void* param, const SDataBuf* pMsg, int32_t code) pRsp->useconds = htobe64(pRsp->useconds); pRsp->compLen = htonl(pRsp->compLen); - pSourceDataInfo->status = DATA_READY; + pSourceDataInfo->status = EX_SOURCE_DATA_READY; tsem_post(&pSourceDataInfo->pEx->ready); } @@ -5068,12 +5072,12 @@ static SSDataBlock* concurrentlyLoadRemoteDataImpl(SOperatorInfo *pOperator, SEx for (int32_t i = 0; i < totalSources; ++i) { SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i); - if (pDataInfo->status == DATA_EXHAUSTED) { + if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) { completed += 1; continue; } - if (pDataInfo->status != DATA_READY) { + if (pDataInfo->status != EX_SOURCE_DATA_READY) { continue; } @@ -5086,7 +5090,7 @@ static SSDataBlock* concurrentlyLoadRemoteDataImpl(SOperatorInfo *pOperator, SEx qDebug("%s vgId:%d, taskID:0x%" PRIx64 " index:%d completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 " try next", GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, i + 1, pDataInfo->totalRows, pExchangeInfo->totalRows); - pDataInfo->status = DATA_EXHAUSTED; + pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED; completed += 1; continue; } @@ -5102,15 +5106,15 @@ static SSDataBlock* concurrentlyLoadRemoteDataImpl(SOperatorInfo *pOperator, SEx GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pRes->info.rows, pDataInfo->totalRows, pExchangeInfo->totalRows, pExchangeInfo->totalSize, i + 1, totalSources); - pDataInfo->status = DATA_EXHAUSTED; + pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED; } else { qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " numOfRows:%d, totalRows:%" PRIu64 ", totalBytes:%" PRIu64, GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pRes->info.rows, pExchangeInfo->totalRows, pExchangeInfo->totalSize); } - if (pDataInfo->status != DATA_EXHAUSTED) { - pDataInfo->status = DATA_NOT_READY; + if (pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED) { + pDataInfo->status = EX_SOURCE_DATA_NOT_READY; code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i); if (code != TSDB_CODE_SUCCESS) { goto _error; @@ -5130,14 +5134,10 @@ _error: return NULL; } -static SSDataBlock* concurrentlyLoadRemoteData(SOperatorInfo *pOperator) { +static int32_t prepareConcurrentlyLoad(SOperatorInfo *pOperator) { SExchangeInfo *pExchangeInfo = pOperator->info; SExecTaskInfo *pTaskInfo = pOperator->pTaskInfo; - if (pOperator->status == OP_RES_TO_RETURN) { - return concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo); - } - size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources); int64_t startTs = taosGetTimestampUs(); @@ -5145,7 +5145,8 @@ static SSDataBlock* concurrentlyLoadRemoteData(SOperatorInfo *pOperator) { for(int32_t i = 0; i < totalSources; ++i) { int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i); if (code != TSDB_CODE_SUCCESS) { - return NULL; + pTaskInfo->code = code; + return code; } } @@ -5153,9 +5154,9 @@ static SSDataBlock* concurrentlyLoadRemoteData(SOperatorInfo *pOperator) { qDebug("%s send all fetch request to %"PRIzu" sources completed, elapsed:%"PRId64, GET_TASKID(pTaskInfo), totalSources, endTs - startTs); tsem_wait(&pExchangeInfo->ready); + pOperator->cost.openCost = taosGetTimestampUs() - startTs; - pOperator->status = OP_RES_TO_RETURN; - return concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo); + return TSDB_CODE_SUCCESS; } static SSDataBlock* seqLoadRemoteData(SOperatorInfo *pOperator) { @@ -5183,7 +5184,7 @@ static SSDataBlock* seqLoadRemoteData(SOperatorInfo *pOperator) { GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pExchangeInfo->current + 1, pDataInfo->totalRows, pExchangeInfo->totalRows); - pDataInfo->status = DATA_EXHAUSTED; + pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED; pExchangeInfo->current += 1; continue; } @@ -5198,7 +5199,7 @@ static SSDataBlock* seqLoadRemoteData(SOperatorInfo *pOperator) { pDataInfo->totalRows, pExchangeInfo->totalRows, pExchangeInfo->totalSize, pExchangeInfo->current + 1, totalSources); - pDataInfo->status = DATA_EXHAUSTED; + pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED; pExchangeInfo->current += 1; } else { qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " numOfRows:%d, totalRows:%" PRIu64 ", totalBytes:%" PRIu64, @@ -5209,6 +5210,26 @@ static SSDataBlock* seqLoadRemoteData(SOperatorInfo *pOperator) { } } +static int32_t prepareLoadRemoteData(void* param) { + SOperatorInfo *pOperator = (SOperatorInfo*) param; + if ((pOperator->status & OP_OPENED) == OP_OPENED) { + return TSDB_CODE_SUCCESS; + } + + SExchangeInfo *pExchangeInfo = pOperator->info; + if (pExchangeInfo->seqLoadData) { + // do nothing for sequentially load data + } else { + int32_t code = prepareConcurrentlyLoad(pOperator); + if (code != TSDB_CODE_SUCCESS) { + return code; + } + } + + pOperator->status = OP_OPENED; + return TSDB_CODE_SUCCESS; +} + static SSDataBlock* doLoadRemoteData(void* param, bool* newgroup) { SOperatorInfo *pOperator = (SOperatorInfo*) param; @@ -5216,6 +5237,10 @@ static SSDataBlock* doLoadRemoteData(void* param, bool* newgroup) { SExecTaskInfo *pTaskInfo = pOperator->pTaskInfo; size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources); + if ((pOperator->status & OP_OPENED) != OP_OPENED) { + pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC; // TODO add a new error code + return NULL; + } if (pOperator->status == OP_EXEC_DONE) { qDebug("%s all %"PRIzu" source(s) are exhausted, total rows:%"PRIu64" bytes:%"PRIu64", elapsed:%.2f ms", GET_TASKID(pTaskInfo), totalSources, @@ -5224,11 +5249,10 @@ static SSDataBlock* doLoadRemoteData(void* param, bool* newgroup) { } *newgroup = false; - if (pExchangeInfo->seqLoadData) { return seqLoadRemoteData(pOperator); } else { - return concurrentlyLoadRemoteData(pOperator); + return concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo); } #if 0 @@ -5251,7 +5275,7 @@ static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo) { for(int32_t i = 0; i < numOfSources; ++i) { SSourceDataInfo dataInfo = {0}; - dataInfo.status = DATA_NOT_READY; + dataInfo.status = EX_SOURCE_DATA_NOT_READY; dataInfo.pEx = pInfo; dataInfo.index = i; @@ -5295,12 +5319,12 @@ SOperatorInfo* createExchangeOperatorInfo(const SArray* pSources, const SArray* pOperator->name = "ExchangeOperator"; pOperator->operatorType = OP_Exchange; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = taosArrayGetSize(pExprInfo); pOperator->pTaskInfo = pTaskInfo; - pOperator->openFn = NULL; // assign a dummy function. - pOperator->nextDataFn = doLoadRemoteData; + pOperator->openFn = prepareLoadRemoteData; // assign a dummy function. + pOperator->getNextFn = doLoadRemoteData; pOperator->closeFn = destroyExchangeOperatorInfo; #if 1 @@ -5387,10 +5411,12 @@ SOperatorInfo* createTableScanOperatorInfo(void* pTsdbReadHandle, int32_t order, pOperator->name = "TableScanOperator"; pOperator->operatorType = OP_TableScan; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = numOfOutput; - pOperator->nextDataFn = doTableScan; + pOperator->openFn = operatorDummyOpenFn; + pOperator->getNextFn = doTableScan; + pOperator->closeFn = operatorDummyCloseFn; pOperator->pTaskInfo = pTaskInfo; return pOperator; @@ -5411,11 +5437,11 @@ SOperatorInfo* createTableSeqScanOperatorInfo(void* pTsdbReadHandle, STaskRuntim pOperator->name = "TableSeqScanOperator"; pOperator->operatorType = OP_TableSeqScan; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = pRuntimeEnv->pQueryAttr->numOfCols; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doTableScanImpl; + pOperator->getNextFn = doTableScanImpl; return pOperator; } @@ -5436,10 +5462,10 @@ SOperatorInfo* createTableBlockInfoScanOperator(void* pTsdbReadHandle, STaskRunt pOperator->name = "TableBlockInfoScanOperator"; // pOperator->operatorType = OP_TableBlockInfoScan; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; // pOperator->numOfOutput = pRuntimeEnv->pQueryAttr->numOfCols; - pOperator->nextDataFn = doBlockInfoScan; + pOperator->getNextFn = doBlockInfoScan; return pOperator; } @@ -5478,10 +5504,13 @@ SOperatorInfo* createStreamScanOperatorInfo(void *streamReadHandle, SArray* pExp pOperator->name = "StreamBlockScanOperator"; pOperator->operatorType = OP_StreamScan; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = numOfOutput; - pOperator->nextDataFn = doStreamBlockScan; + pOperator->openFn = operatorDummyOpenFn; + pOperator->getNextFn = doStreamBlockScan; + pOperator->closeFn = operatorDummyCloseFn; + pOperator->pTaskInfo = pTaskInfo; return pOperator; } @@ -5692,7 +5721,7 @@ SSDataBlock* loadNextDataBlock(void* param) { SOperatorInfo* pOperator = (SOperatorInfo*) param; bool newgroup = false; - return pOperator->nextDataFn(pOperator, &newgroup); + return pOperator->getNextFn(pOperator, &newgroup); } static bool needToMerge(SSDataBlock* pBlock, SArray* groupInfo, char **buf, int32_t rowIndex) { @@ -6006,13 +6035,13 @@ SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t pOperator->name = "SortedMerge"; pOperator->operatorType = OP_SortedMerge; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = numOfOutput; pOperator->pExpr = exprArrayDup(pExprInfo); pOperator->pTaskInfo = pTaskInfo; - pOperator->nextDataFn = doSortedMerge; + pOperator->getNextFn = doSortedMerge; pOperator->closeFn = destroySortedMergeOperatorInfo; code = appendDownstream(pOperator, downstream, numOfDownstream); @@ -6104,11 +6133,11 @@ SOperatorInfo *createOrderOperatorInfo(SOperatorInfo* downstream, SArray* pExprI pOperator->name = "Order"; pOperator->operatorType = OP_Order; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->pTaskInfo = pTaskInfo; - pOperator->nextDataFn = doSort; + pOperator->getNextFn = doSort; pOperator->closeFn = destroyOrderOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -6134,7 +6163,7 @@ static SSDataBlock* doAggregate(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6184,7 +6213,7 @@ static SSDataBlock* doMultiTableAggregate(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6270,7 +6299,7 @@ static SSDataBlock* doProjectOperation(void* param, bool* newgroup) { // The downstream exec may change the value of the newgroup, so use a local variable instead. publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = pOperator->pDownstream[0]->nextDataFn(pOperator->pDownstream[0], newgroup); + SSDataBlock* pBlock = pOperator->pDownstream[0]->getNextFn(pOperator->pDownstream[0], newgroup); publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6328,7 +6357,7 @@ static SSDataBlock* doLimit(void* param, bool* newgroup) { SSDataBlock* pBlock = NULL; while (1) { publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_BEFORE_OPERATOR_EXEC); - pBlock = pOperator->pDownstream[0]->nextDataFn(pOperator->pDownstream[0], newgroup); + pBlock = pOperator->pDownstream[0]->getNextFn(pOperator->pDownstream[0], newgroup); publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6379,7 +6408,7 @@ static SSDataBlock* doFilter(void* param, bool* newgroup) { while (1) { publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock *pBlock = pOperator->pDownstream[0]->nextDataFn(pOperator->pDownstream[0], newgroup); + SSDataBlock *pBlock = pOperator->pDownstream[0]->getNextFn(pOperator->pDownstream[0], newgroup); publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6422,7 +6451,7 @@ static SSDataBlock* doIntervalAgg(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6482,7 +6511,7 @@ static SSDataBlock* doAllIntervalAgg(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6545,7 +6574,7 @@ static SSDataBlock* doSTableIntervalAgg(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6600,7 +6629,7 @@ static SSDataBlock* doAllSTableIntervalAgg(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6735,7 +6764,7 @@ static SSDataBlock* doStateWindowAgg(void *param, bool* newgroup) { SOperatorInfo* downstream = pOperator->pDownstream[0]; while (1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -6797,7 +6826,7 @@ static SSDataBlock* doSessionWindowAgg(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { break; @@ -6850,7 +6879,7 @@ static SSDataBlock* hashGroupbyAggregate(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(downstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = downstream->nextDataFn(downstream, newgroup); + SSDataBlock* pBlock = downstream->getNextFn(downstream, newgroup); publishOperatorProfEvent(downstream, QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { break; @@ -6935,7 +6964,7 @@ static SSDataBlock* doFill(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_BEFORE_OPERATOR_EXEC); - SSDataBlock* pBlock = pOperator->pDownstream[0]->nextDataFn(pOperator->pDownstream[0], newgroup); + SSDataBlock* pBlock = pOperator->pDownstream[0]->getNextFn(pOperator->pDownstream[0], newgroup); publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_AFTER_OPERATOR_EXEC); if (*newgroup) { @@ -7090,14 +7119,15 @@ SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SArray* pE pOperator->name = "TableAggregate"; pOperator->operatorType = OP_Aggregate; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->pExpr = exprArrayDup(pExprInfo); pOperator->numOfOutput = taosArrayGetSize(pExprInfo); pOperator->pTaskInfo = pTaskInfo; - pOperator->nextDataFn = doAggregate; - pOperator->closeFn = destroyAggOperatorInfo; + pOperator->openFn = operatorDummyOpenFn; + pOperator->getNextFn = doAggregate; + pOperator->closeFn = destroyAggOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); return pOperator; @@ -7199,12 +7229,12 @@ SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SArray pOperator->name = "MultiTableAggregate"; pOperator->operatorType = OP_MultiTableAggregate; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->pExpr = exprArrayDup(pExprInfo); pOperator->numOfOutput = numOfOutput; - pOperator->nextDataFn = doMultiTableAggregate; + pOperator->getNextFn = doMultiTableAggregate; pOperator->closeFn = destroyAggOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7225,12 +7255,12 @@ SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SArray* pExp pOperator->name = "ProjectOperator"; pOperator->operatorType = OP_Project; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->pExpr = exprArrayDup(pExprInfo); pOperator->numOfOutput = taosArrayGetSize(pExprInfo); - pOperator->nextDataFn = doProjectOperation; + pOperator->getNextFn = doProjectOperation; pOperator->closeFn = destroyProjectOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7283,10 +7313,10 @@ SOperatorInfo* createFilterOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorI pOperator->name = "FilterOperator"; // pOperator->operatorType = OP_Filter; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->numOfOutput = numOfOutput; pOperator->pExpr = pExpr; - pOperator->nextDataFn = doFilter; + pOperator->getNextFn = doFilter; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; pOperator->closeFn = destroyConditionOperatorInfo; @@ -7304,8 +7334,8 @@ SOperatorInfo* createLimitOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorIn pOperator->name = "LimitOperator"; // pOperator->operatorType = OP_Limit; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; - pOperator->nextDataFn = doLimit; + pOperator->status = OP_NOT_OPENED; + pOperator->getNextFn = doLimit; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7335,13 +7365,13 @@ SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SArray* pEx pOperator->name = "TimeIntervalAggOperator"; pOperator->operatorType = OP_TimeWindow; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->pExpr = exprArrayDup(pExprInfo); pOperator->pTaskInfo = pTaskInfo; pOperator->numOfOutput = taosArrayGetSize(pExprInfo); pOperator->info = pInfo; - pOperator->nextDataFn = doIntervalAgg; + pOperator->getNextFn = doIntervalAgg; pOperator->closeFn = destroyBasicOperatorInfo; code = appendDownstream(pOperator, &downstream, 1); @@ -7360,12 +7390,12 @@ SOperatorInfo* createAllTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, S pOperator->name = "AllTimeIntervalAggOperator"; // pOperator->operatorType = OP_AllTimeWindow; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doAllIntervalAgg; + pOperator->getNextFn = doAllIntervalAgg; pOperator->closeFn = destroyBasicOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7384,12 +7414,12 @@ SOperatorInfo* createStatewindowOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOper pOperator->name = "StateWindowOperator"; // pOperator->operatorType = OP_StateWindow; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doStateWindowAgg; + pOperator->getNextFn = doStateWindowAgg; pOperator->closeFn = destroyStateWindowOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7409,12 +7439,12 @@ SOperatorInfo* createSWindowOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperator pOperator->name = "SessionWindowAggOperator"; // pOperator->operatorType = OP_SessionWindow; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doSessionWindowAgg; + pOperator->getNextFn = doSessionWindowAgg; pOperator->closeFn = destroySWindowOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7432,13 +7462,13 @@ SOperatorInfo* createMultiTableTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntim pOperator->name = "MultiTableTimeIntervalOperator"; // pOperator->operatorType = OP_MultiTableTimeInterval; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doSTableIntervalAgg; + pOperator->getNextFn = doSTableIntervalAgg; pOperator->closeFn = destroyBasicOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7456,13 +7486,13 @@ SOperatorInfo* createAllMultiTableTimeIntervalOperatorInfo(STaskRuntimeEnv* pRun pOperator->name = "AllMultiTableTimeIntervalOperator"; // pOperator->operatorType = OP_AllMultiTableTimeInterval; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doAllSTableIntervalAgg; + pOperator->getNextFn = doAllSTableIntervalAgg; pOperator->closeFn = destroyBasicOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7488,13 +7518,13 @@ SOperatorInfo* createGroupbyOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperator SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "GroupbyAggOperator"; pOperator->blockingOptr = true; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; // pOperator->operatorType = OP_Groupby; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = hashGroupbyAggregate; + pOperator->getNextFn = hashGroupbyAggregate; pOperator->closeFn = destroyGroupbyOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7527,13 +7557,13 @@ SOperatorInfo* createFillOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInf pOperator->name = "FillOperator"; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; // pOperator->operatorType = OP_Fill; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = doFill; + pOperator->getNextFn = doFill; pOperator->closeFn = destroySFillOperatorInfo; int32_t code = appendDownstream(pOperator, &downstream, 1); @@ -7578,7 +7608,7 @@ SOperatorInfo* createSLimitOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorI pOperator->name = "SLimitOperator"; pOperator->operatorType = OP_SLimit; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; // pOperator->exec = doSLimit; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; @@ -7734,9 +7764,9 @@ SOperatorInfo* createTagScanOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo pOperator->name = "SeqTableTagScan"; pOperator->operatorType = OP_TagScan; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; - pOperator->nextDataFn = doTagScan; + pOperator->getNextFn = doTagScan; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->pRuntimeEnv = pRuntimeEnv; @@ -7806,7 +7836,7 @@ static SSDataBlock* hashDistinct(void* param, bool* newgroup) { while(1) { publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_BEFORE_OPERATOR_EXEC); - pBlock = pOperator->pDownstream[0]->nextDataFn(pOperator->pDownstream[0], newgroup); + pBlock = pOperator->pDownstream[0]->getNextFn(pOperator->pDownstream[0], newgroup); publishOperatorProfEvent(pOperator->pDownstream[0], QUERY_PROF_AFTER_OPERATOR_EXEC); if (pBlock == NULL) { @@ -7872,13 +7902,13 @@ SOperatorInfo* createDistinctOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperato SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "DistinctOperator"; pOperator->blockingOptr = false; - pOperator->status = OP_IN_EXECUTING; + pOperator->status = OP_NOT_OPENED; // pOperator->operatorType = OP_Distinct; pOperator->pExpr = pExpr; pOperator->numOfOutput = numOfOutput; pOperator->info = pInfo; pOperator->pRuntimeEnv = pRuntimeEnv; - pOperator->nextDataFn = hashDistinct; + pOperator->getNextFn = hashDistinct; pOperator->pExpr = pExpr; pOperator->closeFn = destroyDistinctOperatorInfo; @@ -8589,75 +8619,6 @@ void setResultBufSize(STaskAttr* pQueryAttr, SRspResultInfo* pResultInfo) { pResultInfo->total = 0; } -FORCE_INLINE bool checkQIdEqual(void *qHandle, uint64_t qId) { - return ((SQInfo *)qHandle)->qId == qId; -} - -int32_t initQInfo(STsBufInfo* pTsBufInfo, void* tsdb, void* sourceOptr, SQInfo* pQInfo, STaskParam* param, char* start, - int32_t prevResultLen, void* merger) { - int32_t code = TSDB_CODE_SUCCESS; - - STaskRuntimeEnv* pRuntimeEnv = &pQInfo->runtimeEnv; - pRuntimeEnv->qinfo = pQInfo; - - STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr; - - STSBuf *pTsBuf = NULL; - - if (pTsBufInfo->tsLen > 0) { // open new file to save the result - char* tsBlock = start + pTsBufInfo->tsOffset; - pTsBuf = tsBufCreateFromCompBlocks(tsBlock, pTsBufInfo->tsNumOfBlocks, pTsBufInfo->tsLen, pTsBufInfo->tsOrder, - pQueryAttr->vgId); - - if (pTsBuf == NULL) { - code = TSDB_CODE_QRY_NO_DISKSPACE; - goto _error; - } - tsBufResetPos(pTsBuf); - bool ret = tsBufNextPos(pTsBuf); - UNUSED(ret); - } - - SArray* prevResult = NULL; - if (prevResultLen > 0) { - prevResult = interResFromBinary(param->prevResult, prevResultLen); - pRuntimeEnv->prevResult = prevResult; - } - - pRuntimeEnv->currentOffset = pQueryAttr->limit.offset; - if (tsdb != NULL) { -// pQueryAttr->precision = tsdbGetCfg(tsdb)->precision; - } - - if ((QUERY_IS_ASC_QUERY(pQueryAttr) && (pQueryAttr->window.skey > pQueryAttr->window.ekey)) || - (!QUERY_IS_ASC_QUERY(pQueryAttr) && (pQueryAttr->window.ekey > pQueryAttr->window.skey))) { - //qDebug("QInfo:0x%"PRIx64" no result in time range %" PRId64 "-%" PRId64 ", order %d", pQInfo->qId, pQueryAttr->window.skey, -// pQueryAttr->window.ekey, pQueryAttr->order.order); -// setTaskStatus(pOperator->pTaskInfo, QUERY_COMPLETED); - pRuntimeEnv->tableqinfoGroupInfo.numOfTables = 0; - // todo free memory - return TSDB_CODE_SUCCESS; - } - - if (pRuntimeEnv->tableqinfoGroupInfo.numOfTables == 0) { - //qDebug("QInfo:0x%"PRIx64" no table qualified for tag filter, abort query", pQInfo->qId); -// setTaskStatus(pOperator->pTaskInfo, QUERY_COMPLETED); - return TSDB_CODE_SUCCESS; - } - - // filter the qualified - if ((code = doInitQInfo(pQInfo, pTsBuf, tsdb, sourceOptr, param->tableScanOperator, param->pOperator, merger)) != TSDB_CODE_SUCCESS) { - goto _error; - } - - return code; - -_error: - // table query ref will be decrease during error handling -// doDestroyTask(pQInfo); - return code; -} - //TODO refactor void freeColumnFilterInfo(SColumnFilterInfo* pFilter, int32_t numOfFilters) { if (pFilter == NULL || numOfFilters == 0) { diff --git a/source/libs/executor/test/executorTests.cpp b/source/libs/executor/test/executorTests.cpp index ff29d5f355..607a70437f 100644 --- a/source/libs/executor/test/executorTests.cpp +++ b/source/libs/executor/test/executorTests.cpp @@ -201,9 +201,9 @@ SOperatorInfo* createDummyOperator(int32_t startVal, int32_t numOfBlocks, int32_ pOperator->name = "dummyInputOpertor4Test"; if (numOfCols == 1) { - pOperator->nextDataFn = getDummyBlock; + pOperator->getNextFn = getDummyBlock; } else { - pOperator->nextDataFn = get2ColsDummyBlock; + pOperator->getNextFn = get2ColsDummyBlock; } SDummyInputInfo *pInfo = (SDummyInputInfo*) calloc(1, sizeof(SDummyInputInfo)); @@ -837,7 +837,7 @@ TEST(testCase, inMem_sort_Test) { SOperatorInfo* pOperator = createOrderOperatorInfo(createDummyOperator(10000, 5, 1000, data_asc, 1), pExprInfo, pOrderVal, NULL); bool newgroup = false; - SSDataBlock* pRes = pOperator->nextDataFn(pOperator, &newgroup); + SSDataBlock* pRes = pOperator->getNextFn(pOperator, &newgroup); SColumnInfoData* pCol1 = static_cast(taosArrayGet(pRes->pDataBlock, 0)); SColumnInfoData* pCol2 = static_cast(taosArrayGet(pRes->pDataBlock, 1)); @@ -912,7 +912,7 @@ TEST(testCase, external_sort_Test) { while(1) { int64_t s = taosGetTimestampUs(); - pRes = pOperator->nextDataFn(pOperator, &newgroup); + pRes = pOperator->getNextFn(pOperator, &newgroup); int64_t e = taosGetTimestampUs(); if (t++ == 1) { @@ -984,7 +984,7 @@ TEST(testCase, sorted_merge_Test) { while(1) { int64_t s = taosGetTimestampUs(); - pRes = pOperator->nextDataFn(pOperator, &newgroup); + pRes = pOperator->getNextFn(pOperator, &newgroup); int64_t e = taosGetTimestampUs(); if (t++ == 1) { @@ -1062,7 +1062,7 @@ TEST(testCase, time_interval_Operator_Test) { while(1) { int64_t s = taosGetTimestampUs(); - pRes = pOperator->nextDataFn(pOperator, &newgroup); + pRes = pOperator->getNextFn(pOperator, &newgroup); int64_t e = taosGetTimestampUs(); if (t++ == 1) { diff --git a/source/libs/qworker/src/qworker.c b/source/libs/qworker/src/qworker.c index 7a6fa4cfd7..c7ca0a6132 100644 --- a/source/libs/qworker/src/qworker.c +++ b/source/libs/qworker/src/qworker.c @@ -494,7 +494,7 @@ int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryEnd) { ASSERT(pRes->info.rows > 0); - SInputData inputData = {.pData = pRes, .pTableRetrieveTsMap = NULL}; + SInputData inputData = {.pData = pRes}; code = dsPutDataBlock(sinkHandle, &inputData, &qcontinue); if (code) { QW_TASK_ELOG("dsPutDataBlock failed, code:%s", tstrerror(code)); From a12bb2dbf03dd58691047cecd59d5cdd1282ff22 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 7 Mar 2022 19:42:10 +0800 Subject: [PATCH 03/29] add UT --- source/libs/index/src/index.c | 8 ++++++++ source/libs/index/src/index_cache.c | 9 ++++++++- source/libs/index/src/index_tfile.c | 24 +++++++++++++++++------- source/libs/index/test/indexTests.cc | 4 +++- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index ae0a6c775e..744f6ca70b 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -341,6 +341,8 @@ static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result // TODO: iterator mem and tidex STermValueType s = kTypeValue; + int64_t st = taosGetTimestampUs(); + SIdxTempResult* tr = sIdxTempResultCreate(); if (0 == indexCacheSearch(cache, query, tr, &s)) { if (s == kTypeDeletion) { @@ -348,17 +350,23 @@ static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result // coloum already drop by other oper, no need to query tindex return 0; } else { + st = taosGetTimestampUs(); if (0 != indexTFileSearch(sIdx->tindex, query, tr)) { indexError("corrupt at index(TFile) col:%s val: %s", term->colName, term->colVal); goto END; } + int64_t tfCost = taosGetTimestampUs() - st; + indexInfo("tfile search cost: %" PRIu64 "us", tfCost); } } else { indexError("corrupt at index(cache) col:%s val: %s", term->colName, term->colVal); goto END; } + int64_t cost = taosGetTimestampUs() - st; + indexInfo("search cost: %" PRIu64 "us", cost); sIdxTempResultMergeTo(*result, tr); + sIdxTempResultDestroy(tr); return 0; END: diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index d3b25afdbc..e601740e35 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -276,14 +276,19 @@ static int indexQueryMem(MemTable* mem, CacheTerm* ct, EIndexQueryType qtype, SI } else if (c->operaType == DEL_VALUE) { INDEX_MERGE_ADD_DEL(tr->added, tr->deled, c->uid) } + } else { + break; } - } + } else if (qtype == QUERY_PREFIX) { + } else if (qtype == QUERY_SUFFIX) { + } else if (qtype == QUERY_RANGE) } } tSkipListDestroyIter(iter); return 0; } int indexCacheSearch(void* cache, SIndexTermQuery* query, SIdxTempResult* result, STermValueType* s) { + int64_t st = taosGetTimestampUs(); if (cache == NULL) { return 0; } @@ -312,12 +317,14 @@ int indexCacheSearch(void* cache, SIndexTermQuery* query, SIdxTempResult* result // continue search in imm ret = indexQueryMem(imm, &ct, qtype, result, s); } + if (hasJson) { tfree(p); } indexMemUnRef(mem); indexMemUnRef(imm); + indexInfo("cache search, time cost %" PRIu64 "us", taosGetTimestampUs() - st); return ret; } diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index fd267fbf03..2396b6af5b 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -189,8 +189,8 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SIdxTempResul bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON); EIndexQueryType qtype = query->qType; - SArray* result = taosArrayInit(16, sizeof(uint64_t)); - int ret = -1; + // SArray* result = taosArrayInit(16, sizeof(uint64_t)); + int ret = -1; // refactor to callback later if (qtype == QUERY_TERM) { uint64_t offset; @@ -200,11 +200,18 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SIdxTempResul p = indexPackJsonData(term); sz = strlen(p); } + int64_t st = taosGetTimestampUs(); FstSlice key = fstSliceCreate(p, sz); if (fstGet(reader->fst, &key, &offset)) { - indexInfo("index: %" PRIu64 ", col: %s, colVal: %s, found table info in tindex", term->suid, term->colName, - term->colVal); - ret = tfileReaderLoadTableIds(reader, offset, result); + int64_t et = taosGetTimestampUs(); + int64_t cost = et - st; + indexInfo("index: %" PRIu64 ", col: %s, colVal: %s, found table info in tindex, time cost: %" PRIu64 "us", + term->suid, term->colName, term->colVal, cost); + + ret = tfileReaderLoadTableIds(reader, offset, tr->total); + cost = taosGetTimestampUs() - et; + indexInfo("index: %" PRIu64 ", col: %s, colVal: %s, load all table info, time cost: %" PRIu64 "us", term->suid, + term->colName, term->colVal, cost); } else { indexInfo("index: %" PRIu64 ", col: %s, colVal: %s, not found table info in tindex", term->suid, term->colName, term->colVal); @@ -225,8 +232,8 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SIdxTempResul } tfileReaderUnRef(reader); - taosArrayAddAll(tr->total, result); - taosArrayDestroy(result); + // taosArrayAddAll(tr->total, result); + // taosArrayDestroy(result); return ret; } @@ -391,6 +398,7 @@ int indexTFileSearch(void* tfile, SIndexTermQuery* query, SIdxTempResult* result return ret; } + int64_t st = taosGetTimestampUs(); IndexTFile* pTfile = tfile; SIndexTerm* term = query->term; @@ -399,6 +407,8 @@ int indexTFileSearch(void* tfile, SIndexTermQuery* query, SIdxTempResult* result if (reader == NULL) { return 0; } + int64_t cost = taosGetTimestampUs() - st; + indexInfo("index tfile stage 1 cost: %" PRId64 "", cost); return tfileReaderSearch(reader, query, result); } diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index ce3f7fe25e..90ed1b26b8 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -768,7 +768,7 @@ class IndexObj { int64_t s = taosGetTimestampUs(); if (Search(mq, result) == 0) { int64_t e = taosGetTimestampUs(); - std::cout << "search one successfully and time cost:" << e - s << "\tquery col:" << colName + std::cout << "search one successfully and time cost:" << e - s << "us\tquery col:" << colName << "\t val: " << colVal << "\t size:" << taosArrayGetSize(result) << std::endl; } else { } @@ -1106,8 +1106,10 @@ TEST_F(IndexEnv2, testIndex_del) { } index->Del("tag10", "Hello", 12); index->Del("tag10", "Hello", 11); + EXPECT_EQ(98, index->SearchOne("tag10", "Hello")); index->WriteMultiMillonData("tag10", "xxxxxxxxxxxxxx", 100 * 10000); index->Del("tag10", "Hello", 17); + EXPECT_EQ(97, index->SearchOne("tag10", "Hello")); } From db2f17e6e298abe5f953fc5f926d0bb89c78d618 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 7 Mar 2022 19:43:39 +0800 Subject: [PATCH 04/29] add UT --- source/libs/index/src/index_cache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index e601740e35..b40ded9e9a 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -281,7 +281,8 @@ static int indexQueryMem(MemTable* mem, CacheTerm* ct, EIndexQueryType qtype, SI } } else if (qtype == QUERY_PREFIX) { } else if (qtype == QUERY_SUFFIX) { - } else if (qtype == QUERY_RANGE) + } else if (qtype == QUERY_RANGE) { + } } } tSkipListDestroyIter(iter); From 914b5387c38f8592dade679c6da5d3240cbec6a4 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 8 Mar 2022 15:07:35 +0800 Subject: [PATCH 05/29] make CI/CD happy --- source/libs/index/test/indexTests.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index 90ed1b26b8..8ab855fdc2 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -1110,6 +1110,5 @@ TEST_F(IndexEnv2, testIndex_del) { index->WriteMultiMillonData("tag10", "xxxxxxxxxxxxxx", 100 * 10000); index->Del("tag10", "Hello", 17); - EXPECT_EQ(97, index->SearchOne("tag10", "Hello")); } From 19686d9ab1cb5b1a6f73e6c4d5dd21b1ea09f1ce Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 9 Mar 2022 22:16:59 +0800 Subject: [PATCH 06/29] update transport --- source/libs/index/test/fstTest.cc | 12 +- source/libs/transport/inc/transComm.h | 6 +- source/libs/transport/src/transCli.c | 200 ++++++++++++-------------- source/libs/transport/src/transComm.c | 6 + source/libs/transport/src/transSrv.c | 24 ++-- source/libs/transport/test/rclient.c | 1 + source/libs/transport/test/rserver.c | 2 + 7 files changed, 122 insertions(+), 129 deletions(-) diff --git a/source/libs/index/test/fstTest.cc b/source/libs/index/test/fstTest.cc index cb3206a611..32f11b8af6 100644 --- a/source/libs/index/test/fstTest.cc +++ b/source/libs/index/test/fstTest.cc @@ -258,7 +258,7 @@ void checkFstCheckIterator() { // prefix search std::vector result; - AutomationCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_ALWAYS); + AutomationCtx* ctx = automCtxCreate((void*)"H", AUTOMATION_PREFIX); m->Search(ctx, result); std::cout << "size: " << result.size() << std::endl; // assert(result.size() == count); @@ -328,11 +328,11 @@ void iterTFileReader(char* path, char* uid, char* colName, char* ver) { int main(int argc, char* argv[]) { // tool to check all kind of fst test // if (argc > 1) { validateTFile(argv[1]); } - if (argc > 4) { - // path suid colName ver - iterTFileReader(argv[1], argv[2], argv[3], argv[4]); - } - // checkFstCheckIterator(); + // if (argc > 4) { + // path suid colName ver + // iterTFileReader(argv[1], argv[2], argv[3], argv[4]); + //} + checkFstCheckIterator(); // checkFstLongTerm(); // checkFstPrefixSearch(); diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index d4d9bff56c..f3e9e88583 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -123,9 +123,8 @@ typedef struct { } SRpcReqContext; typedef struct { - SRpcInfo* pTransInst; // associated SRpcInfo - SEpSet epSet; // ip list provided by app - void* ahandle; // handle provided by app + 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 @@ -244,6 +243,7 @@ int transDestroyBuffer(SConnBuffer* buf); int transAllocBuffer(SConnBuffer* connBuf, uv_buf_t* uvBuf); bool transReadComplete(SConnBuffer* connBuf); +int transSetConnOption(uv_tcp_t* stream); // int transPackMsg(SRpcMsg *rpcMsg, bool sercured, bool auth, char **msg, int32_t *msgLen); // int transUnpackMsg(char *msg, SRpcMsg *pMsg, bool ); diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index fb76f38fe5..821b51c935 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -20,7 +20,10 @@ #define CONN_HOST_THREAD_INDEX(conn) (conn ? ((SCliConn*)conn)->hThrdIdx : -1) #define CONN_PERSIST_TIME(para) (para * 1000 * 10) +#define CONN_GET_INST_LABEL(conn) (((SRpcInfo*)(((SCliThrdObj*)conn->hostThrd)->pTransInst))->label) + typedef struct SCliConn { + T_REF_DECLARE() uv_connect_t connReq; uv_stream_t* stream; uv_write_t* writeReq; @@ -32,8 +35,7 @@ typedef struct SCliConn { int8_t ctnRdCnt; // continue read count int hThrdIdx; - SRpcPush* push; - int persist; // + int persist; // // spi configure char spi; char secured; @@ -41,6 +43,7 @@ typedef struct SCliConn { // debug and log info struct sockaddr_in addr; struct sockaddr_in locaddr; + } SCliConn; typedef struct SCliMsg { @@ -54,14 +57,17 @@ typedef struct SCliThrdObj { pthread_t thread; uv_loop_t* loop; // uv_async_t* cliAsync; // - SAsyncPool* asyncPool; - uv_timer_t* timer; - void* pool; // conn pool + SAsyncPool* asyncPool; + uv_timer_t* timer; + void* pool; // conn pool + + // msg queue queue msg; pthread_mutex_t msgMtx; - uint64_t nextTimeout; // next timeout - void* pTransInst; // - bool quit; + + uint64_t nextTimeout; // next timeout + void* pTransInst; // + bool quit; } SCliThrdObj; typedef struct SClientObj { @@ -96,7 +102,7 @@ static void clientAsyncCb(uv_async_t* handle); static void clientDestroy(uv_handle_t* handle); static void clientConnDestroy(SCliConn* pConn, bool clear /*clear tcp handle or not*/); -// process data read from server, auth/decompress etc later +// process data read from server, add decompress etc later static void clientHandleResp(SCliConn* conn); // handle except about conn static void clientHandleExcept(SCliConn* conn); @@ -104,9 +110,10 @@ static void clientHandleExcept(SCliConn* conn); static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd); static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd); static void clientSendQuit(SCliThrdObj* thrd); - static void destroyUserdata(SRpcMsg* userdata); +static int clientRBChoseIdx(SRpcInfo* pTransInst); + static void destroyCmsg(SCliMsg* cmsg); static void transDestroyConnCtx(STransConnCtx* ctx); // thread obj @@ -115,10 +122,13 @@ static void destroyThrdObj(SCliThrdObj* pThrd); // thread static void* clientThread(void* arg); -static void clientHandleResp(SCliConn* conn) { +static void* clientNotifyApp() {} +static void clientHandleResp(SCliConn* conn) { SCliMsg* pMsg = conn->data; STransConnCtx* pCtx = pMsg->ctx; - SRpcInfo* pRpc = pCtx->pTransInst; + + SCliThrdObj* pThrd = conn->hostThrd; + SRpcInfo* pTransInst = pThrd->pTransInst; STransMsgHead* pHead = (STransMsgHead*)(conn->readBuf.buf); pHead->code = htonl(pHead->code); @@ -134,26 +144,24 @@ static void clientHandleResp(SCliConn* conn) { rpcMsg.msgType = pHead->msgType; rpcMsg.ahandle = pCtx->ahandle; - if (pRpc->pfp != NULL && (pRpc->pfp)(pRpc->parent, rpcMsg.msgType)) { + if (pTransInst->pfp != NULL && (pTransInst->pfp)(pTransInst->parent, rpcMsg.msgType)) { rpcMsg.handle = conn; conn->persist = 1; tDebug("client conn %p persist by app", conn); } - tDebug("%s client conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pRpc->label, conn, + tDebug("%s client conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pTransInst->label, conn, TMSG_INFO(pHead->msgType), inet_ntoa(conn->addr.sin_addr), ntohs(conn->addr.sin_port), inet_ntoa(conn->locaddr.sin_addr), ntohs(conn->locaddr.sin_port), rpcMsg.contLen); conn->secured = pHead->secured; - if (conn->push != NULL && conn->ctnRdCnt != 0) { - (*conn->push->callback)(conn->push->arg, &rpcMsg); - conn->push = NULL; - } else { + + if (pCtx->pSem == NULL) { if (pCtx->pSem == NULL) { - tTrace("%s client conn %p handle resp", pRpc->label, conn); - (pRpc->cfp)(pRpc->parent, &rpcMsg, NULL); + tTrace("%s client conn %p handle resp", pTransInst->label, conn); + (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); } else { - tTrace("%s client conn(sync) %p handle resp", pRpc->label, conn); + tTrace("%s client conn(sync) %p handle resp", pTransInst->label, conn); memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg)); tsem_post(pCtx->pSem); } @@ -162,28 +170,33 @@ static void clientHandleResp(SCliConn* conn) { uv_read_start((uv_stream_t*)conn->stream, clientAllocBufferCb, clientReadCb); - SCliThrdObj* pThrd = conn->hostThrd; - // user owns conn->persist = 1 - if (conn->push == NULL && conn->persist == 0) { - if (pRpc->noPool == true) { + if (conn->persist == 0) { + if (pTransInst->noPool == true) { + destroyCmsg(conn->data); + clientConnDestroy(conn, true); } else { addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn); + destroyCmsg(conn->data); + conn->data = NULL; } + } else { + // app decide to free or not } - destroyCmsg(conn->data); - conn->data = NULL; // start thread's timer of conn pool if not active - if (!uv_is_active((uv_handle_t*)pThrd->timer) && pRpc->idleTime > 0) { + if (!uv_is_active((uv_handle_t*)pThrd->timer) && pTransInst->idleTime > 0) { // uv_timer_start((uv_timer_t*)pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); } } static void clientHandleExcept(SCliConn* pConn) { - if (pConn->data == NULL && pConn->push == NULL) { + if (pConn->data == NULL) { // handle conn except in conn pool clientConnDestroy(pConn, true); return; } + SCliThrdObj* pThrd = pConn->hostThrd; + SRpcInfo* pTransInst = pThrd->pTransInst; + SCliMsg* pMsg = pConn->data; STransConnCtx* pCtx = pMsg->ctx; @@ -192,29 +205,14 @@ static void clientHandleExcept(SCliConn* pConn) { rpcMsg.code = TSDB_CODE_RPC_NETWORK_UNAVAIL; rpcMsg.msgType = pMsg->msg.msgType + 1; - if (pConn->push != NULL && pConn->ctnRdCnt != 0) { - (*pConn->push->callback)(pConn->push->arg, &rpcMsg); - pConn->push = NULL; + if (pCtx->pSem == NULL) { + (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); } else { - if (pCtx->pSem == NULL) { - (pCtx->pTransInst->cfp)(pCtx->pTransInst->parent, &rpcMsg, NULL); - } else { - memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg)); - tsem_post(pCtx->pSem); - } - if (pConn->push != NULL) { - (*pConn->push->callback)(pConn->push->arg, &rpcMsg); - } - pConn->push = NULL; + memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg)); + tsem_post(pCtx->pSem); } - tTrace("%s client conn %p start to destroy", pCtx->pTransInst->label, pConn); - if (pConn->push == NULL) { - destroyCmsg(pConn->data); - pConn->data = NULL; - } - // transDestroyConnCtx(pCtx); + tTrace("%s client conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn); clientConnDestroy(pConn, true); - pConn->ctnRdCnt += 1; } static void clientTimeoutCb(uv_timer_t* handle) { @@ -316,17 +314,14 @@ static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf if (nread > 0) { pBuf->len += nread; if (transReadComplete(pBuf)) { - tTrace("client conn %p read complete", conn); + tTrace("%s client conn %p read complete", CONN_GET_INST_LABEL(conn), conn); clientHandleResp(conn); } else { - tTrace("client conn %p read partial packet, continue to read", conn); + tTrace("%s client conn %p read partial packet, continue to read", CONN_GET_INST_LABEL(conn), conn); } return; } - if (nread == UV_EOF) { - tError("client conn %p read error: %s", conn, uv_err_name(nread)); - clientHandleExcept(conn); - } + assert(nread <= 0); if (nread == 0) { // ref http://docs.libuv.org/en/v1.x/stream.html?highlight=uv_read_start#c.uv_read_cb @@ -335,18 +330,16 @@ static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf return; } if (nread < 0) { - tError("client conn %p read error: %s", conn, uv_err_name(nread)); + tError("%s client conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread)); clientHandleExcept(conn); } - // tDebug("Read error %s\n", uv_err_name(nread)); - // uv_close((uv_handle_t*)handle, clientDestroy); } static void clientConnDestroy(SCliConn* conn, bool clear) { // conn->ref--; if (conn->ref == 0) { - tTrace("client conn %p remove from conn pool", conn); + tTrace("%s client conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn); QUEUE_REMOVE(&conn->conn); if (clear) { uv_close((uv_handle_t*)conn->stream, clientDestroy); @@ -367,8 +360,9 @@ static void clientDestroy(uv_handle_t* handle) { static void clientWriteCb(uv_write_t* req, int status) { SCliConn* pConn = req->data; + if (status == 0) { - tTrace("client conn %p data already was written out", pConn); + tTrace("%s client conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn); SCliMsg* pMsg = pConn->data; if (pMsg == NULL) { // handle @@ -376,18 +370,19 @@ static void clientWriteCb(uv_write_t* req, int status) { } destroyUserdata(&pMsg->msg); } else { - tError("client conn %p failed to write: %s", pConn, uv_err_name(status)); + tError("%s client conn %p failed to write: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status)); clientHandleExcept(pConn); return; } - SCliThrdObj* pThrd = pConn->hostThrd; uv_read_start((uv_stream_t*)pConn->stream, clientAllocBufferCb, clientReadCb); } static void clientWrite(SCliConn* pConn) { SCliMsg* pCliMsg = pConn->data; STransConnCtx* pCtx = pCliMsg->ctx; - SRpcInfo* pTransInst = pCtx->pTransInst; + + SCliThrdObj* pThrd = pConn->hostThrd; + SRpcInfo* pTransInst = pThrd->pTransInst; SRpcMsg* pMsg = (SRpcMsg*)(&pCliMsg->msg); @@ -416,20 +411,18 @@ static void clientWrite(SCliConn* pConn) { pHead->msgType = pMsg->msgType; pHead->msgLen = (int32_t)htonl((uint32_t)msgLen); - // if (pHead->msgType == TDMT_VND_QUERY || pHead->msgType == TDMT_VND_) - uv_buf_t wb = uv_buf_init((char*)pHead, msgLen); - tDebug("client conn %p %s is send to %s:%d, local info %s:%d", pConn, TMSG_INFO(pHead->msgType), - inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), - ntohs(pConn->locaddr.sin_port)); + tDebug("%s client conn %p %s is send to %s:%d, local info %s:%d", CONN_GET_INST_LABEL(pConn), pConn, + TMSG_INFO(pHead->msgType), inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), + inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port)); + uv_write(pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb); } static void clientConnCb(uv_connect_t* req, int status) { // impl later SCliConn* pConn = req->data; if (status != 0) { - // tError("failed to connect server(%s, %d), errmsg: %s", pCtx->ip, pCtx->port, uv_strerror(status)); - tError("client conn %p failed to connect server: %s", pConn, uv_strerror(status)); + tError("%s client conn %p failed to connect server: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status)); clientHandleExcept(pConn); return; } @@ -439,7 +432,7 @@ static void clientConnCb(uv_connect_t* req, int status) { addrlen = sizeof(pConn->locaddr); uv_tcp_getsockname((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->locaddr, &addrlen); - tTrace("client conn %p connect to server successfully", pConn); + tTrace("%s client conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn); assert(pConn->stream == req->handle); clientWrite(pConn); @@ -462,20 +455,18 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { et = taosGetTimestampUs(); STransConnCtx* pCtx = pMsg->ctx; + SRpcInfo* pTransInst = pThrd->pTransInst; + SCliConn* conn = NULL; - SCliConn* conn = NULL; - if (pMsg->msg.handle == NULL) { - if (pCtx->pTransInst->noPool == true) { - } else { - conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); - } - if (conn != NULL) { - tTrace("client conn %p get from conn pool", conn); - } - } else { + if (pMsg->msg.handle != NULL) { conn = (SCliConn*)(pMsg->msg.handle); if (conn != NULL) { - tTrace("client conn %p reused", conn); + tTrace("%s client conn %p reused", CONN_GET_INST_LABEL(conn), conn); + } + } else { + if (pTransInst->noPool == false) { + conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); + if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); } } @@ -489,7 +480,6 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { return; } clientWrite(conn); - } else { conn = calloc(1, sizeof(SCliConn)); conn->ref++; @@ -497,12 +487,7 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t)); uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream)); conn->stream->data = conn; - uv_tcp_nodelay((uv_tcp_t*)conn->stream, 1); - int ret = uv_tcp_keepalive((uv_tcp_t*)conn->stream, 1, 1); - if (ret) { - tTrace("client conn %p failed to set keepalive, %s", conn, uv_err_name(ret)); - } - // write req handle + conn->writeReq = malloc(sizeof(uv_write_t)); conn->writeReq->data = conn; @@ -512,17 +497,17 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { conn->data = pMsg; conn->hostThrd = pThrd; - // conn->push = pMsg->msg.push; - // conn->ctnRdCnt = 0; - + int ret = transSetConnOption((uv_tcp_t*)conn->stream); + if (ret) { + tError("%s client conn %p failed to set conn option, errmsg %s", pTransInst->label, conn, uv_err_name(ret)); + } struct sockaddr_in addr; uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr); // handle error in callback if fail to connect - tTrace("client conn %p try to connect to %s:%d", conn, pMsg->ctx->ip, pMsg->ctx->port); + tTrace("%s client conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port); uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb); } - conn->push = pMsg->msg.push; conn->ctnRdCnt = 0; conn->hThrdIdx = pCtx->hThrdIdx; } @@ -548,7 +533,6 @@ static void clientAsyncCb(uv_async_t* handle) { } else { clientHandleReq(pMsg, pThrd); } - // clientHandleReq(pMsg, pThrd); count++; } if (count >= 2) { @@ -656,37 +640,36 @@ void taosCloseClient(void* arg) { free(cli->pThreadObj); free(cli); } -static int clientRBChoseIdx(SRpcInfo* pRpc) { - int64_t index = pRpc->index; - if (pRpc->index++ >= pRpc->numOfThreads) { - pRpc->index = 0; +static int clientRBChoseIdx(SRpcInfo* pTransInst) { + int64_t index = pTransInst->index; + if (pTransInst->index++ >= pTransInst->numOfThreads) { + pTransInst->index = 0; } - return index % pRpc->numOfThreads; + return index % pTransInst->numOfThreads; } void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) { // impl later char* ip = (char*)(pEpSet->eps[pEpSet->inUse].fqdn); uint32_t port = pEpSet->eps[pEpSet->inUse].port; - SRpcInfo* pRpc = (SRpcInfo*)shandle; + SRpcInfo* pTransInst = (SRpcInfo*)shandle; int index = CONN_HOST_THREAD_INDEX(pMsg->handle); if (index == -1) { - index = clientRBChoseIdx(pRpc); + index = clientRBChoseIdx(pTransInst); } int32_t flen = 0; if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) { // imp later } STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx)); - pCtx->pTransInst = (SRpcInfo*)shandle; pCtx->ahandle = pMsg->ahandle; pCtx->msgType = pMsg->msgType; pCtx->ip = strdup(ip); pCtx->port = port; pCtx->hThrdIdx = index; - assert(pRpc->connType == TAOS_CONN_CLIENT); + assert(pTransInst->connType == TAOS_CONN_CLIENT); // atomic or not SCliMsg* cliMsg = malloc(sizeof(SCliMsg)); @@ -694,7 +677,7 @@ void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* cliMsg->msg = *pMsg; cliMsg->st = taosGetTimestampUs(); - SCliThrdObj* thrd = ((SClientObj*)pRpc->tcphandle)->pThreadObj[index]; + SCliThrdObj* thrd = ((SClientObj*)pTransInst->tcphandle)->pThreadObj[index]; transSendAsync(thrd->asyncPool, &(cliMsg->q)); } @@ -702,15 +685,14 @@ void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pReq, SRpcMsg* pRsp) { char* ip = (char*)(pEpSet->eps[pEpSet->inUse].fqdn); uint32_t port = pEpSet->eps[pEpSet->inUse].port; - SRpcInfo* pRpc = (SRpcInfo*)shandle; + SRpcInfo* pTransInst = (SRpcInfo*)shandle; int index = CONN_HOST_THREAD_INDEX(pReq->handle); if (index == -1) { - index = clientRBChoseIdx(pRpc); + index = clientRBChoseIdx(pTransInst); } STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx)); - pCtx->pTransInst = (SRpcInfo*)shandle; pCtx->ahandle = pReq->ahandle; pCtx->msgType = pReq->msgType; pCtx->ip = strdup(ip); @@ -725,7 +707,7 @@ void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pReq, SRpcMsg* pRsp) { cliMsg->msg = *pReq; cliMsg->st = taosGetTimestampUs(); - SCliThrdObj* thrd = ((SClientObj*)pRpc->tcphandle)->pThreadObj[index]; + SCliThrdObj* thrd = ((SClientObj*)pTransInst->tcphandle)->pThreadObj[index]; transSendAsync(thrd->asyncPool, &(cliMsg->q)); tsem_t* pSem = pCtx->pSem; tsem_wait(pSem); diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index 9a8607b0ed..92e42cb380 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -257,6 +257,12 @@ int transDestroyBuffer(SConnBuffer* buf) { transClearBuffer(buf); } +int transSetConnOption(uv_tcp_t* stream) { + uv_tcp_nodelay(stream, 1); + int ret = uv_tcp_keepalive(stream, 5, 5); + return ret; +} + SAsyncPool* transCreateAsyncPool(uv_loop_t* loop, int sz, void* arg, AsyncCB cb) { SAsyncPool* pool = calloc(1, sizeof(SAsyncPool)); pool->index = 0; diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index c7b6ca2a2c..3d0da29e2d 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -18,6 +18,7 @@ #include "transComm.h" typedef struct SSrvConn { + T_REF_DECLARE() uv_tcp_t* pTcp; uv_write_t* pWriter; uv_timer_t* pTimer; @@ -67,16 +68,19 @@ typedef struct SWorkThrdObj { } SWorkThrdObj; typedef struct SServerObj { - pthread_t thread; - uv_tcp_t server; - uv_loop_t* loop; + pthread_t thread; + uv_tcp_t server; + uv_loop_t* loop; + + // work thread info int workerIdx; int numOfThreads; SWorkThrdObj** pThreadObj; - uv_pipe_t** pipe; - uint32_t ip; - uint32_t port; - uv_async_t* pAcceptAsync; // just to quit from from accept thread + + uv_pipe_t** pipe; + uint32_t ip; + uint32_t port; + uv_async_t* pAcceptAsync; // just to quit from from accept thread } SServerObj; static const char* notify = "a"; @@ -493,13 +497,11 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { uv_tcp_init(pThrd->loop, pConn->pTcp); pConn->pTcp->data = pConn; - // uv_tcp_nodelay(pConn->pTcp, 1); - // uv_tcp_keepalive(pConn->pTcp, 1, 1); - - // init write request, just pConn->pWriter = calloc(1, sizeof(uv_write_t)); pConn->pWriter->data = pConn; + transSetConnOption((uv_tcp_t*)pConn->pTcp); + if (uv_accept(q, (uv_stream_t*)(pConn->pTcp)) == 0) { uv_os_fd_t fd; uv_fileno((const uv_handle_t*)pConn->pTcp, &fd); diff --git a/source/libs/transport/test/rclient.c b/source/libs/transport/test/rclient.c index bcdf32bf6a..62ae0101ef 100644 --- a/source/libs/transport/test/rclient.c +++ b/source/libs/transport/test/rclient.c @@ -124,6 +124,7 @@ int main(int argc, char *argv[]) { rpcInit.ckey = "key"; rpcInit.spi = 1; rpcInit.connType = TAOS_CONN_CLIENT; + rpcDebugFlag = 143; for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-p") == 0 && i < argc - 1) { diff --git a/source/libs/transport/test/rserver.c b/source/libs/transport/test/rserver.c index 5432a07649..ece3c7a500 100644 --- a/source/libs/transport/test/rserver.c +++ b/source/libs/transport/test/rserver.c @@ -125,6 +125,8 @@ int main(int argc, char *argv[]) { rpcInit.idleTime = 2 * 1500; rpcInit.afp = retrieveAuthInfo; + rpcDebugFlag = 143; + for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-p") == 0 && i < argc - 1) { rpcInit.localPort = atoi(argv[++i]); From fdb64e7c5d68877e0eed492d0c7e62769ec94fb6 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Thu, 10 Mar 2022 13:50:40 +0800 Subject: [PATCH 07/29] test --- source/client/src/clientImpl.c | 6 +++--- source/libs/transport/src/transCli.c | 27 +++++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index e9febef7e2..c281d93c69 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -225,7 +225,7 @@ void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t } int32_t scheduleQuery(SRequestObj* pRequest, SQueryDag* pDag, SArray* pNodeList) { - void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter; + void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter; SQueryResult res = {.code = 0, .numOfRows = 0, .msgSize = ERROR_MSG_BUF_DEFAULT_SIZE, .msg = pRequest->msgBuf}; int32_t code = schedulerExecJob(pTransporter, pNodeList, pDag, &pRequest->body.queryJob, pRequest->sqlstr, &res); if (code != TSDB_CODE_SUCCESS) { @@ -239,12 +239,12 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryDag* pDag, SArray* pNodeList) if (TSDB_SQL_INSERT == pRequest->type || TSDB_SQL_CREATE_TABLE == pRequest->type) { pRequest->body.resInfo.numOfRows = res.numOfRows; - + if (pRequest->body.queryJob != 0) { schedulerFreeJob(pRequest->body.queryJob); } } - + pRequest->code = res.code; return pRequest->code; } diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 821b51c935..a417a57436 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -157,14 +157,12 @@ static void clientHandleResp(SCliConn* conn) { conn->secured = pHead->secured; if (pCtx->pSem == NULL) { - if (pCtx->pSem == NULL) { - tTrace("%s client conn %p handle resp", pTransInst->label, conn); - (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); - } else { - tTrace("%s client conn(sync) %p handle resp", pTransInst->label, conn); - memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg)); - tsem_post(pCtx->pSem); - } + tTrace("%s client conn %p handle resp", pTransInst->label, conn); + (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); + } else { + tTrace("%s client conn(sync) %p handle resp", pTransInst->label, conn); + memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg)); + tsem_post(pCtx->pSem); } conn->ctnRdCnt += 1; @@ -175,14 +173,14 @@ static void clientHandleResp(SCliConn* conn) { if (pTransInst->noPool == true) { destroyCmsg(conn->data); clientConnDestroy(conn, true); + return; } else { addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn); - destroyCmsg(conn->data); - conn->data = NULL; } - } else { - // app decide to free or not } + destroyCmsg(conn->data); + conn->data = NULL; + // start thread's timer of conn pool if not active if (!uv_is_active((uv_handle_t*)pThrd->timer) && pTransInst->idleTime > 0) { // uv_timer_start((uv_timer_t*)pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); @@ -206,11 +204,16 @@ static void clientHandleExcept(SCliConn* pConn) { rpcMsg.msgType = pMsg->msg.msgType + 1; if (pCtx->pSem == NULL) { + tTrace("%s client conn %p handle resp", pTransInst->label, pConn); (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); } else { + tTrace("%s client conn(sync) %p handle resp", pTransInst->label, pConn); memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg)); tsem_post(pCtx->pSem); } + destroyCmsg(pConn->data); + pConn->data = NULL; + tTrace("%s client conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn); clientConnDestroy(pConn, true); } From 73d768d273cf05840c252d57007a74cf65a2a77e Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 10 Mar 2022 16:54:26 +0800 Subject: [PATCH 08/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 2 +- source/libs/sync/inc/syncRaftEntry.h | 8 ++++-- source/libs/sync/inc/syncRaftLog.h | 41 +++++++++------------------- source/libs/sync/src/syncMessage.c | 6 ++-- source/libs/sync/src/syncRaftEntry.c | 27 ++++++++++++++---- source/libs/sync/src/syncRaftLog.c | 35 ++++++++++++++++-------- 6 files changed, 67 insertions(+), 52 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 6231cb8399..cbe45e07d4 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -83,7 +83,7 @@ typedef struct SyncPing { char data[]; } SyncPing; -#define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) +//#define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) SyncPing* syncPingBuild(uint32_t dataLen); void syncPingDestroy(SyncPing* pMsg); diff --git a/source/libs/sync/inc/syncRaftEntry.h b/source/libs/sync/inc/syncRaftEntry.h index be25675db4..e471488f5c 100644 --- a/source/libs/sync/inc/syncRaftEntry.h +++ b/source/libs/sync/inc/syncRaftEntry.h @@ -46,8 +46,12 @@ char* syncEntrySerialize(const SSyncRaftEntry* pEntry, uint32_t* len); SSyncRaftEntry* syncEntryDeserialize(const char* buf, uint32_t len); cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry); char* syncEntry2Str(const SSyncRaftEntry* pEntry); -void syncEntryPrint(const SSyncRaftEntry* pEntry); -void syncEntryPrint2(char *s, const SSyncRaftEntry* pEntry); + +// for debug +void syncEntryPrint(const SSyncRaftEntry* pEntry); +void syncEntryPrint2(char* s, const SSyncRaftEntry* pEntry); +void syncEntryLog(const SSyncRaftEntry* pEntry); +void syncEntryLog2(char* s, const SSyncRaftEntry* pEntry); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncRaftLog.h b/source/libs/sync/inc/syncRaftLog.h index d59b3206b5..59b5fa94db 100644 --- a/source/libs/sync/inc/syncRaftLog.h +++ b/source/libs/sync/inc/syncRaftLog.h @@ -32,39 +32,24 @@ typedef struct SSyncLogStoreData { SWal* pWal; } SSyncLogStoreData; -SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode); - -void logStoreDestory(SSyncLogStore* pLogStore); - -// append one log entry -int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry); - -// get one log entry, user need to free pEntry->pCont +SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode); +void logStoreDestory(SSyncLogStore* pLogStore); +int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry); SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index); - -// truncate log with index, entries after the given index (>=index) will be deleted -int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex); - -// return index of last entry -SyncIndex logStoreLastIndex(SSyncLogStore* pLogStore); - -// return term of last entry -SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore); - -// update log store commit index with "index" -int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index); - -// return commit index of log -SyncIndex logStoreGetCommitIndex(SSyncLogStore* pLogStore); - +int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex); +SyncIndex logStoreLastIndex(SSyncLogStore* pLogStore); +SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore); +int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index); +SyncIndex logStoreGetCommitIndex(SSyncLogStore* pLogStore); SSyncRaftEntry* logStoreGetLastEntry(SSyncLogStore* pLogStore); - -cJSON* logStore2Json(SSyncLogStore* pLogStore); - -char* logStore2Str(SSyncLogStore* pLogStore); +cJSON* logStore2Json(SSyncLogStore* pLogStore); +char* logStore2Str(SSyncLogStore* pLogStore); // for debug void logStorePrint(SSyncLogStore* pLogStore); +void logStorePrint2(char* s, SSyncLogStore* pLogStore); +void logStoreLog(SSyncLogStore* pLogStore); +void logStoreLog2(char* s, SSyncLogStore* pLogStore); #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index baef49d748..4afe14b475 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -150,7 +150,7 @@ SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock // ---- message process SyncPing---- SyncPing* syncPingBuild(uint32_t dataLen) { - uint32_t bytes = SYNC_PING_FIX_LEN + dataLen; + uint32_t bytes = sizeof(SyncPing) + dataLen; SyncPing* pMsg = malloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; @@ -173,7 +173,7 @@ void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen) { void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) { memcpy(pMsg, buf, len); assert(len == pMsg->bytes); - assert(pMsg->bytes == SYNC_PING_FIX_LEN + pMsg->dataLen); + assert(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen); } void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) { @@ -272,7 +272,7 @@ void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLe void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg) { memcpy(pMsg, buf, len); assert(len == pMsg->bytes); - assert(pMsg->bytes == SYNC_PING_FIX_LEN + pMsg->dataLen); + assert(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen); } void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) { diff --git a/source/libs/sync/src/syncRaftEntry.c b/source/libs/sync/src/syncRaftEntry.c index 959bf49ee7..d2feb98c90 100644 --- a/source/libs/sync/src/syncRaftEntry.c +++ b/source/libs/sync/src/syncRaftEntry.c @@ -104,14 +104,29 @@ char* syncEntry2Str(const SSyncRaftEntry* pEntry) { return serialized; } +// for debug ----------- void syncEntryPrint(const SSyncRaftEntry* pEntry) { - char* s = syncEntry2Str(pEntry); - sTrace("%s", s); - free(s); + char* serialized = syncEntry2Str(pEntry); + printf("syncEntryPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); } void syncEntryPrint2(char* s, const SSyncRaftEntry* pEntry) { - char* ss = syncEntry2Str(pEntry); - sTrace("%s | %s", s, ss); - free(ss); + char* serialized = syncEntry2Str(pEntry); + printf("syncEntryPrint | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncEntryLog(const SSyncRaftEntry* pEntry) { + char* serialized = syncEntry2Str(pEntry); + sTrace("syncEntryPrint | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncEntryLog2(char* s, const SSyncRaftEntry* pEntry) { + char* serialized = syncEntry2Str(pEntry); + sTrace("syncEntryPrint | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } \ No newline at end of file diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index d177d3ac9b..27c8a26154 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -43,7 +43,6 @@ void logStoreDestory(SSyncLogStore* pLogStore) { } } -// append one log entry int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; @@ -61,7 +60,6 @@ int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) { free(serialized); } -// get one log entry, user need to free pEntry->pCont SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; @@ -77,14 +75,12 @@ SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { return pEntry; } -// truncate log with index, entries after the given index (>=index) will be deleted int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; walRollback(pWal, fromIndex); } -// return index of last entry SyncIndex logStoreLastIndex(SSyncLogStore* pLogStore) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; @@ -92,7 +88,6 @@ SyncIndex logStoreLastIndex(SSyncLogStore* pLogStore) { return lastIndex; } -// return term of last entry SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore) { SyncTerm lastTerm = 0; SSyncRaftEntry* pLastEntry = logStoreGetLastEntry(pLogStore); @@ -103,14 +98,12 @@ SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore) { return lastTerm; } -// update log store commit index with "index" int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; walCommit(pWal, index); } -// return commit index of log SyncIndex logStoreGetCommitIndex(SSyncLogStore* pLogStore) { SSyncLogStoreData* pData = pLogStore->data; return pData->pSyncNode->commitIndex; @@ -163,11 +156,29 @@ char* logStore2Str(SSyncLogStore* pLogStore) { return serialized; } -// for debug +// for debug ----------------- void logStorePrint(SSyncLogStore* pLogStore) { - char* s = logStore2Str(pLogStore); - // sTrace("%s", s); - fprintf(stderr, "logStorePrint: [len:%lu]| %s \n", strlen(s), s); + char* serialized = logStore2Str(pLogStore); + printf("logStorePrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} - free(s); +void logStorePrint2(char* s, SSyncLogStore* pLogStore) { + char* serialized = logStore2Str(pLogStore); + printf("logStorePrint | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void logStoreLog(SSyncLogStore* pLogStore) { + char* serialized = logStore2Str(pLogStore); + sTrace("logStorePrint | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void logStoreLog2(char* s, SSyncLogStore* pLogStore) { + char* serialized = logStore2Str(pLogStore); + sTrace("logStorePrint | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } \ No newline at end of file From 88aef2d1ab9a069ac17fb80f7539668bcf2a9cc2 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 10 Mar 2022 19:21:02 +0800 Subject: [PATCH 09/29] sync refactor --- source/libs/sync/inc/syncIndexMgr.h | 6 +++ source/libs/sync/inc/syncInt.h | 11 +++-- source/libs/sync/inc/syncRaftEntry.h | 8 +-- source/libs/sync/inc/syncRaftStore.h | 7 ++- source/libs/sync/inc/syncVoteMgr.h | 12 +++++ source/libs/sync/src/syncIndexMgr.c | 27 +++++++++++ source/libs/sync/src/syncMain.c | 30 ++++++++++-- source/libs/sync/src/syncRaftEntry.c | 22 ++++----- source/libs/sync/src/syncRaftStore.c | 30 ++++++++++-- source/libs/sync/src/syncVoteMgr.c | 54 +++++++++++++++++++++ source/libs/sync/test/syncInitTest.cpp | 2 +- source/libs/sync/test/syncLogStoreTest.cpp | 8 +-- source/libs/sync/test/syncPingTest.cpp | 10 ++-- source/libs/sync/test/syncRaftStoreTest.cpp | 12 +++-- 14 files changed, 197 insertions(+), 42 deletions(-) diff --git a/source/libs/sync/inc/syncIndexMgr.h b/source/libs/sync/inc/syncIndexMgr.h index 7116ae9d46..63f24b104f 100644 --- a/source/libs/sync/inc/syncIndexMgr.h +++ b/source/libs/sync/inc/syncIndexMgr.h @@ -42,6 +42,12 @@ SyncIndex syncIndexMgrGetIndex(SSyncIndexMgr *pSyncIndexMgr, const SRaftId cJSON * syncIndexMgr2Json(SSyncIndexMgr *pSyncIndexMgr); char * syncIndexMgr2Str(SSyncIndexMgr *pSyncIndexMgr); +// for debug ------------------- +void syncIndexMgrPrint(SSyncIndexMgr *pObj); +void syncIndexMgrPrint2(char *s, SSyncIndexMgr *pObj); +void syncIndexMgrLog(SSyncIndexMgr *pObj); +void syncIndexMgrLog2(char *s, SSyncIndexMgr *pObj); + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index 2932240ec1..df277e2d7e 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -211,11 +211,14 @@ int32_t syncNodeStopElectTimer(SSyncNode* pSyncNode); int32_t syncNodeRestartElectTimer(SSyncNode* pSyncNode, int32_t ms); int32_t syncNodeStartHeartbeatTimer(SSyncNode* pSyncNode); int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode); +cJSON* syncNode2Json(const SSyncNode* pSyncNode); +char* syncNode2Str(const SSyncNode* pSyncNode); -// for debug -cJSON* syncNode2Json(const SSyncNode* pSyncNode); -char* syncNode2Str(const SSyncNode* pSyncNode); -void syncNodePrint(char* s, const SSyncNode* pSyncNode); +// for debug -------------- +void syncNodePrint(SSyncNode* pObj); +void syncNodePrint2(char* s, SSyncNode* pObj); +void syncNodeLog(SSyncNode* pObj); +void syncNodeLog2(char* s, SSyncNode* pObj); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncRaftEntry.h b/source/libs/sync/inc/syncRaftEntry.h index e471488f5c..335d23ea8e 100644 --- a/source/libs/sync/inc/syncRaftEntry.h +++ b/source/libs/sync/inc/syncRaftEntry.h @@ -48,10 +48,10 @@ cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry); char* syncEntry2Str(const SSyncRaftEntry* pEntry); // for debug -void syncEntryPrint(const SSyncRaftEntry* pEntry); -void syncEntryPrint2(char* s, const SSyncRaftEntry* pEntry); -void syncEntryLog(const SSyncRaftEntry* pEntry); -void syncEntryLog2(char* s, const SSyncRaftEntry* pEntry); +void syncEntryPrint(const SSyncRaftEntry* pObj); +void syncEntryPrint2(char* s, const SSyncRaftEntry* pObj); +void syncEntryLog(const SSyncRaftEntry* pObj); +void syncEntryLog2(char* s, const SSyncRaftEntry* pObj); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncRaftStore.h b/source/libs/sync/inc/syncRaftStore.h index 1c25b799b4..4058d3bd1c 100644 --- a/source/libs/sync/inc/syncRaftStore.h +++ b/source/libs/sync/inc/syncRaftStore.h @@ -42,7 +42,12 @@ int32_t raftStoreClose(SRaftStore *pRaftStore); int32_t raftStorePersist(SRaftStore *pRaftStore); int32_t raftStoreSerialize(SRaftStore *pRaftStore, char *buf, size_t len); int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len); -void raftStorePrint(SRaftStore *pRaftStore); + +// for debug ------------------- +void raftStorePrint(SRaftStore *pObj); +void raftStorePrint2(char *s, SRaftStore *pObj); +void raftStoreLog(SRaftStore *pObj); +void raftStoreLog2(char *s, SRaftStore *pObj); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncVoteMgr.h b/source/libs/sync/inc/syncVoteMgr.h index ae9cfe8d01..5bc240e921 100644 --- a/source/libs/sync/inc/syncVoteMgr.h +++ b/source/libs/sync/inc/syncVoteMgr.h @@ -48,6 +48,12 @@ void voteGrantedReset(SVotesGranted *pVotesGranted, SyncTerm term); cJSON * voteGranted2Json(SVotesGranted *pVotesGranted); char * voteGranted2Str(SVotesGranted *pVotesGranted); +// for debug ------------------- +void voteGrantedPrint(SVotesGranted *pObj); +void voteGrantedPrint2(char *s, SVotesGranted *pObj); +void voteGrantedLog(SVotesGranted *pObj); +void voteGrantedLog2(char *s, SVotesGranted *pObj); + // SVotesRespond ----------------------------- typedef struct SVotesRespond { SRaftId (*replicas)[TSDB_MAX_REPLICA]; @@ -65,6 +71,12 @@ void votesRespondReset(SVotesRespond *pVotesRespond, SyncTerm term); cJSON * votesRespond2Json(SVotesRespond *pVotesRespond); char * votesRespond2Str(SVotesRespond *pVotesRespond); +// for debug ------------------- +void votesRespondPrint(SVotesRespond *pObj); +void votesRespondPrint2(char *s, SVotesRespond *pObj); +void votesRespondLog(SVotesRespond *pObj); +void votesRespondLog2(char *s, SVotesRespond *pObj); + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncIndexMgr.c b/source/libs/sync/src/syncIndexMgr.c index fff54638e2..9567938197 100644 --- a/source/libs/sync/src/syncIndexMgr.c +++ b/source/libs/sync/src/syncIndexMgr.c @@ -97,4 +97,31 @@ char *syncIndexMgr2Str(SSyncIndexMgr *pSyncIndexMgr) { char * serialized = cJSON_Print(pJson); cJSON_Delete(pJson); return serialized; +} + +// for debug ------------------- +void syncIndexMgrPrint(SSyncIndexMgr *pObj) { + char *serialized = syncIndexMgr2Str(pObj); + printf("syncIndexMgrPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncIndexMgrPrint2(char *s, SSyncIndexMgr *pObj) { + char *serialized = syncIndexMgr2Str(pObj); + printf("syncIndexMgrPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncIndexMgrLog(SSyncIndexMgr *pObj) { + char *serialized = syncIndexMgr2Str(pObj); + sTrace("syncIndexMgrLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncIndexMgrLog2(char *s, SSyncIndexMgr *pObj) { + char *serialized = syncIndexMgr2Str(pObj); + sTrace("syncIndexMgrLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } \ No newline at end of file diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 5cd7149b72..860dd96cdf 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -327,11 +327,31 @@ char* syncNode2Str(const SSyncNode* pSyncNode) { return serialized; } -void syncNodePrint(char* s, const SSyncNode* pSyncNode) { - char* ss = syncNode2Str(pSyncNode); - // sTrace("syncNodePrint: %s [len:%lu]| %s", s, strlen(ss), ss); - fprintf(stderr, "syncNodePrint: %s [len:%lu]| %s", s, strlen(ss), ss); - free(ss); +// for debug -------------- +void syncNodePrint(SSyncNode* pObj) { + char* serialized = syncNode2Str(pObj); + printf("syncNodePrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncNodePrint2(char* s, SSyncNode* pObj) { + char* serialized = syncNode2Str(pObj); + printf("syncNodePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncNodeLog(SSyncNode* pObj) { + char* serialized = syncNode2Str(pObj); + sTrace("syncNodeLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncNodeLog2(char* s, SSyncNode* pObj) { + char* serialized = syncNode2Str(pObj); + sTrace("syncNodeLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg) { diff --git a/source/libs/sync/src/syncRaftEntry.c b/source/libs/sync/src/syncRaftEntry.c index d2feb98c90..1a5d9c9455 100644 --- a/source/libs/sync/src/syncRaftEntry.c +++ b/source/libs/sync/src/syncRaftEntry.c @@ -105,28 +105,28 @@ char* syncEntry2Str(const SSyncRaftEntry* pEntry) { } // for debug ----------- -void syncEntryPrint(const SSyncRaftEntry* pEntry) { - char* serialized = syncEntry2Str(pEntry); +void syncEntryPrint(const SSyncRaftEntry* pObj) { + char* serialized = syncEntry2Str(pObj); printf("syncEntryPrint | len:%lu | %s \n", strlen(serialized), serialized); fflush(NULL); free(serialized); } -void syncEntryPrint2(char* s, const SSyncRaftEntry* pEntry) { - char* serialized = syncEntry2Str(pEntry); - printf("syncEntryPrint | len:%lu | %s | %s \n", strlen(serialized), s, serialized); +void syncEntryPrint2(char* s, const SSyncRaftEntry* pObj) { + char* serialized = syncEntry2Str(pObj); + printf("syncEntryPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); fflush(NULL); free(serialized); } -void syncEntryLog(const SSyncRaftEntry* pEntry) { - char* serialized = syncEntry2Str(pEntry); - sTrace("syncEntryPrint | len:%lu | %s", strlen(serialized), serialized); +void syncEntryLog(const SSyncRaftEntry* pObj) { + char* serialized = syncEntry2Str(pObj); + sTrace("syncEntryLog | len:%lu | %s", strlen(serialized), serialized); free(serialized); } -void syncEntryLog2(char* s, const SSyncRaftEntry* pEntry) { - char* serialized = syncEntry2Str(pEntry); - sTrace("syncEntryPrint | len:%lu | %s | %s", strlen(serialized), s, serialized); +void syncEntryLog2(char* s, const SSyncRaftEntry* pObj) { + char* serialized = syncEntry2Str(pObj); + sTrace("syncEntryLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); free(serialized); } \ No newline at end of file diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index 7154a21bd1..3a26caa161 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -135,8 +135,30 @@ int32_t raftStoreDeserialize(SRaftStore *pRaftStore, char *buf, size_t len) { return 0; } -void raftStorePrint(SRaftStore *pRaftStore) { - char storeBuf[RAFT_STORE_BLOCK_SIZE]; - raftStoreSerialize(pRaftStore, storeBuf, sizeof(storeBuf)); - printf("%s\n", storeBuf); +// for debug ------------------- +void raftStorePrint(SRaftStore *pObj) { + char serialized[RAFT_STORE_BLOCK_SIZE]; + raftStoreSerialize(pObj, serialized, sizeof(serialized)); + printf("raftStorePrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); +} + +void raftStorePrint2(char *s, SRaftStore *pObj) { + char serialized[RAFT_STORE_BLOCK_SIZE]; + raftStoreSerialize(pObj, serialized, sizeof(serialized)); + printf("raftStorePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); +} +void raftStoreLog(SRaftStore *pObj) { + char serialized[RAFT_STORE_BLOCK_SIZE]; + raftStoreSerialize(pObj, serialized, sizeof(serialized)); + sTrace("raftStoreLog | len:%lu | %s", strlen(serialized), serialized); + fflush(NULL); +} + +void raftStoreLog2(char *s, SRaftStore *pObj) { + char serialized[RAFT_STORE_BLOCK_SIZE]; + raftStoreSerialize(pObj, serialized, sizeof(serialized)); + sTrace("raftStoreLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + fflush(NULL); } diff --git a/source/libs/sync/src/syncVoteMgr.c b/source/libs/sync/src/syncVoteMgr.c index a2f10ce339..5c8e70979c 100644 --- a/source/libs/sync/src/syncVoteMgr.c +++ b/source/libs/sync/src/syncVoteMgr.c @@ -119,6 +119,33 @@ char *voteGranted2Str(SVotesGranted *pVotesGranted) { return serialized; } +// for debug ------------------- +void voteGrantedPrint(SVotesGranted *pObj) { + char *serialized = voteGranted2Str(pObj); + printf("voteGrantedPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void voteGrantedPrint2(char *s, SVotesGranted *pObj) { + char *serialized = voteGranted2Str(pObj); + printf("voteGrantedPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void voteGrantedLog(SVotesGranted *pObj) { + char *serialized = voteGranted2Str(pObj); + sTrace("voteGrantedLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void voteGrantedLog2(char *s, SVotesGranted *pObj) { + char *serialized = voteGranted2Str(pObj); + sTrace("voteGrantedLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); +} + // SVotesRespond ----------------------------- SVotesRespond *votesRespondCreate(SSyncNode *pSyncNode) { SVotesRespond *pVotesRespond = malloc(sizeof(SVotesRespond)); @@ -210,4 +237,31 @@ char *votesRespond2Str(SVotesRespond *pVotesRespond) { char * serialized = cJSON_Print(pJson); cJSON_Delete(pJson); return serialized; +} + +// for debug ------------------- +void votesRespondPrint(SVotesRespond *pObj) { + char *serialized = votesRespond2Str(pObj); + printf("votesRespondPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void votesRespondPrint2(char *s, SVotesRespond *pObj) { + char *serialized = votesRespond2Str(pObj); + printf("votesRespondPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void votesRespondLog(SVotesRespond *pObj) { + char *serialized = votesRespond2Str(pObj); + sTrace("votesRespondLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void votesRespondLog2(char *s, SVotesRespond *pObj) { + char *serialized = votesRespond2Str(pObj); + sTrace("votesRespondLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } \ No newline at end of file diff --git a/source/libs/sync/test/syncInitTest.cpp b/source/libs/sync/test/syncInitTest.cpp index b6794544eb..7898fda8c0 100644 --- a/source/libs/sync/test/syncInitTest.cpp +++ b/source/libs/sync/test/syncInitTest.cpp @@ -89,7 +89,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint((char*)"syncInitTest", pSyncNode); + syncNodePrint2((char*)"syncInitTest", pSyncNode); initRaftId(pSyncNode); diff --git a/source/libs/sync/test/syncLogStoreTest.cpp b/source/libs/sync/test/syncLogStoreTest.cpp index a5eb748de6..602fdee8c2 100644 --- a/source/libs/sync/test/syncLogStoreTest.cpp +++ b/source/libs/sync/test/syncLogStoreTest.cpp @@ -83,7 +83,7 @@ SSyncNode* syncInitTest() { return syncNodeInit(); } void logStoreTest() { logStorePrint(pSyncNode->pLogStore); for (int i = 0; i < 5; ++i) { - int32_t dataLen = 10; + int32_t dataLen = 10; SSyncRaftEntry* pEntry = syncEntryBuild(dataLen); assert(pEntry != NULL); pEntry->msgType = 1; @@ -94,7 +94,7 @@ void logStoreTest() { pEntry->index = pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore) + 1; snprintf(pEntry->data, dataLen, "value%d", i); - //syncEntryPrint2((char*)"write entry:", pEntry); + // syncEntryPrint2((char*)"write entry:", pEntry); pSyncNode->pLogStore->appendEntry(pSyncNode->pLogStore, pEntry); syncEntryDestory(pEntry); } @@ -132,8 +132,8 @@ int main(int argc, char** argv) { pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - //syncNodePrint((char*)"syncLogStoreTest", pSyncNode); - //initRaftId(pSyncNode); + // syncNodePrint((char*)"syncLogStoreTest", pSyncNode); + // initRaftId(pSyncNode); logStoreTest(); diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 83f1f67eb1..e69878632f 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -86,7 +86,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint((char*)"----1", pSyncNode); + syncNodePrint2((char*)"----1", pSyncNode); initRaftId(pSyncNode); @@ -95,7 +95,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStartPingTimer ..."); ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint((char*)"----2", pSyncNode); + syncNodePrint2((char*)"----2", pSyncNode); sTrace("sleep ..."); taosMsleep(10000); @@ -103,7 +103,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStopPingTimer ..."); ret = syncNodeStopPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint((char*)"----3", pSyncNode); + syncNodePrint2((char*)"----3", pSyncNode); sTrace("sleep ..."); taosMsleep(5000); @@ -111,7 +111,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStartPingTimer ..."); ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint((char*)"----4", pSyncNode); + syncNodePrint2((char*)"----4", pSyncNode); sTrace("sleep ..."); taosMsleep(10000); @@ -119,7 +119,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStopPingTimer ..."); ret = syncNodeStopPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint((char*)"----5", pSyncNode); + syncNodePrint2((char*)"----5", pSyncNode); while (1) { sTrace("while 1 sleep ..."); diff --git a/source/libs/sync/test/syncRaftStoreTest.cpp b/source/libs/sync/test/syncRaftStoreTest.cpp index 71c0138c8d..0c1c9b881e 100644 --- a/source/libs/sync/test/syncRaftStoreTest.cpp +++ b/source/libs/sync/test/syncRaftStoreTest.cpp @@ -22,15 +22,21 @@ int main() { SRaftStore *pRaftStore = raftStoreOpen("./raft_store.json"); assert(pRaftStore != NULL); - raftStorePrint(pRaftStore); +#if 0 pRaftStore->currentTerm = 100; pRaftStore->voteFor.addr = 200; pRaftStore->voteFor.vgId = 300; - - raftStorePrint(pRaftStore); raftStorePersist(pRaftStore); + raftStorePrint(pRaftStore); +#endif + + ++(pRaftStore->currentTerm); + ++(pRaftStore->voteFor.addr); + ++(pRaftStore->voteFor.vgId); + raftStorePersist(pRaftStore); + raftStorePrint(pRaftStore); return 0; } From b2c24a0338cf0935c1c85b68a7d9bda9b6a945ca Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Thu, 10 Mar 2022 19:32:26 +0800 Subject: [PATCH 10/29] modify transport --- include/libs/transport/trpc.h | 5 +- source/libs/transport/inc/transComm.h | 7 +- source/libs/transport/src/trans.c | 13 ++++ source/libs/transport/src/transCli.c | 27 +++++--- source/libs/transport/src/transSrv.c | 92 ++++++++++++++++----------- 5 files changed, 95 insertions(+), 49 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 5e3860822e..101092f13e 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -86,7 +86,7 @@ typedef struct SRpcInit { int32_t rpcInit(); void rpcCleanup(); -void *rpcOpen(const SRpcInit *pRpc); +void * rpcOpen(const SRpcInit *pRpc); void rpcClose(void *); void * rpcMallocCont(int contLen); void rpcFreeCont(void *pCont); @@ -99,6 +99,9 @@ void rpcSendRecv(void *shandle, SEpSet *pEpSet, SRpcMsg *pReq, SRpcMsg *pRsp) int rpcReportProgress(void *pConn, char *pCont, int contLen); void rpcCancelRequest(int64_t rid); +void rpcRefHandle(void *handle, int8_t type); +void rpcUnrefHandle(void *handle, int8_t type); + #ifdef __cplusplus } #endif diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index cd6c78cbdc..f2ac77fe61 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -242,8 +242,11 @@ int transAllocBuffer(SConnBuffer* connBuf, uv_buf_t* uvBuf); bool transReadComplete(SConnBuffer* connBuf); int transSetConnOption(uv_tcp_t* stream); -// int transPackMsg(SRpcMsg *rpcMsg, bool sercured, bool auth, char **msg, int32_t *msgLen); -// int transUnpackMsg(char *msg, SRpcMsg *pMsg, bool ); +void transRefSrvHandle(void* handle); +void transUnrefSrvHandle(void* handle); + +void transRefCliHandle(void* handle); +void transUnrefCliHandle(void* handle); #endif diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index b45683617f..f3e0417397 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -122,4 +122,17 @@ void rpcCleanup(void) { // return; } + +void (*taosRefHandle[])(void* handle) = {transRefSrvHandle, transRefCliHandle}; +void (*taosUnRefHandle[])(void* handle) = {transUnrefSrvHandle, transUnrefCliHandle}; + +void rpcRefHandle(void* handle, int8_t type) { + assert(type == TAOS_CONN_SERVER || type == TAOS_CONN_CLIENT); + (*taosRefHandle[type])(handle); +} +void rpcUnrefHandle(void* handle, int8_t type) { + assert(type == TAOS_CONN_SERVER || type == TAOS_CONN_CLIENT); + (*taosUnRefHandle[type])(handle); +} + #endif diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index a417a57436..0d3946d967 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -351,14 +351,11 @@ static void clientConnDestroy(SCliConn* conn, bool clear) { } static void clientDestroy(uv_handle_t* handle) { SCliConn* conn = handle->data; - // transDestroyBuffer(&conn->readBuf); free(conn->stream); free(conn->writeReq); - tTrace("client conn %p destroy successfully", conn); + tTrace("%s client conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn); free(conn); - - // clientConnDestroy(conn, false); } static void clientWriteCb(uv_write_t* req, int status) { @@ -454,8 +451,7 @@ static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { uint64_t et = taosGetTimestampUs(); uint64_t el = et - pMsg->st; - tTrace("client msg tran time cost: %" PRIu64 "us", el); - et = taosGetTimestampUs(); + tTrace("%s client msg tran time cost: %" PRIu64 "us", ((SRpcInfo*)pThrd->pTransInst)->label, el); STransConnCtx* pCtx = pMsg->ctx; SRpcInfo* pTransInst = pThrd->pTransInst; @@ -630,8 +626,6 @@ static void transDestroyConnCtx(STransConnCtx* ctx) { static void clientSendQuit(SCliThrdObj* thrd) { // cli can stop gracefully SCliMsg* msg = calloc(1, sizeof(SCliMsg)); - msg->ctx = NULL; // - transSendAsync(thrd->asyncPool, &msg->q); } void taosCloseClient(void* arg) { @@ -650,6 +644,23 @@ static int clientRBChoseIdx(SRpcInfo* pTransInst) { } return index % pTransInst->numOfThreads; } +void transRefCliHandle(void* handle) { + if (handle == NULL) { + return; + } + int ref = T_REF_INC((SCliConn*)handle); + UNUSED(ref); +} +void transUnrefCliHandle(void* handle) { + if (handle == NULL) { + return; + } + int ref = T_REF_DEC((SCliConn*)handle); + if (ref == 0) { + } + + // unref cli handle +} void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* pRid) { // impl later char* ip = (char*)(pEpSet->eps[pEpSet->inUse].fqdn); diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index cd02d3bd77..432ec472fb 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -33,11 +33,11 @@ typedef struct SSrvConn { void* ahandle; // void* hostThrd; SArray* srvMsgs; - // void* pSrvMsg; + + bool broken; // conn broken; struct sockaddr_in addr; struct sockaddr_in locaddr; - // SRpcMsg sendMsg; // del later char secured; @@ -206,7 +206,6 @@ static void uvHandleReq(SSrvConn* pConn) { } pConn->inType = pHead->msgType; - // assert(transIsReq(pHead->msgType)); SRpcInfo* pRpc = (SRpcInfo*)p->shandle; pHead->code = htonl(pHead->code); @@ -230,7 +229,8 @@ static void uvHandleReq(SSrvConn* pConn) { rpcMsg.handle = pConn; transClearBuffer(&pConn->readBuf); - pConn->ref++; + + transRefSrvHandle(pConn); tDebug("server conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pConn, TMSG_INFO(rpcMsg.msgType), inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port), rpcMsg.contLen); @@ -255,23 +255,20 @@ void uvOnReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) { } return; } - if (nread == UV_EOF) { - tError("server conn %p read error: %s", conn, uv_err_name(nread)); - if (conn->ref > 1) { - conn->ref++; // ref > 1 signed that write is in progress - } - destroyConn(conn, true); - return; - } if (nread == 0) { return; } - if (nread < 0 || nread != UV_EOF) { - if (conn->ref > 1) { - conn->ref++; // ref > 1 signed that write is in progress - } - tError("server conn %p read error: %s", conn, uv_err_name(nread)); - destroyConn(conn, true); + + tError("server conn %p read error: %s", conn, uv_err_name(nread)); + if (nread < 0 || nread == UV_EOF) { + conn->broken = true; + transUnrefSrvHandle(conn); + + // if (conn->ref > 1) { + // conn->ref++; // ref > 1 signed that write is in progress + //} + // tError("server conn %p read error: %s", conn, uv_err_name(nread)); + // destroyConn(conn, true); } } void uvAllocConnBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { @@ -304,10 +301,9 @@ void uvOnWriteCb(uv_write_t* req, int status) { } } else { tError("server conn %p failed to write data, %s", conn, uv_err_name(status)); - // - destroyConn(conn, true); + conn->broken = false; + transUnrefSrvHandle(conn); } - // opt } static void uvOnPipeWriteCb(uv_write_t* req, int status) { if (status == 0) { @@ -353,15 +349,18 @@ static void uvStartSendRespInternal(SSrvMsg* smsg) { SSrvConn* pConn = smsg->pConn; uv_timer_stop(pConn->pTimer); - - // pConn->pSrvMsg = smsg; - // conn->pWriter->data = smsg; uv_write(pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnWriteCb); } static void uvStartSendResp(SSrvMsg* smsg) { // impl SSrvConn* pConn = smsg->pConn; - pConn->ref--; // + + if (pConn->broken == true) { + transUnrefSrvHandle(pConn); + return; + } + transUnrefSrvHandle(pConn); + if (taosArrayGetSize(pConn->srvMsgs) > 0) { tDebug("server conn %p push data to client %s:%d, local info: %s:%d", pConn, inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port)); @@ -386,7 +385,8 @@ static void destroyAllConn(SWorkThrdObj* pThrd) { QUEUE_INIT(h); SSrvConn* c = QUEUE_DATA(h, SSrvConn, queue); - destroyConn(c, true); + transUnrefSrvHandle(c); + // destroyConn(c, true); } } void uvWorkerAsyncCb(uv_async_t* handle) { @@ -394,11 +394,11 @@ void uvWorkerAsyncCb(uv_async_t* handle) { SWorkThrdObj* pThrd = item->pThrd; SSrvConn* conn = NULL; queue wq; + // batch process to avoid to lock/unlock frequently pthread_mutex_lock(&item->mtx); QUEUE_MOVE(&item->qmsg, &wq); pthread_mutex_unlock(&item->mtx); - // pthread_mutex_unlock(&mtx); while (!QUEUE_IS_EMPTY(&wq)) { queue* head = QUEUE_HEAD(&wq); @@ -411,7 +411,6 @@ void uvWorkerAsyncCb(uv_async_t* handle) { } if (msg->pConn == NULL) { free(msg); - destroyAllConn(pThrd); uv_loop_close(pThrd->loop); @@ -601,7 +600,9 @@ static SSrvConn* createConn(void* hThrd) { QUEUE_PUSH(&pThrd->conn, &pConn->queue); pConn->srvMsgs = taosArrayInit(2, sizeof(void*)); // tTrace("conn %p created", pConn); - ++pConn->ref; + + pConn->broken = false; + transRefSrvHandle(pConn); return pConn; } @@ -609,10 +610,6 @@ static void destroyConn(SSrvConn* conn, bool clear) { if (conn == NULL) { return; } - tTrace("server conn %p try to destroy, ref: %d", conn, conn->ref); - if (--conn->ref > 0) { - return; - } transDestroyBuffer(&conn->readBuf); for (int i = 0; i < taosArrayGetSize(conn->srvMsgs); i++) { @@ -624,9 +621,9 @@ static void destroyConn(SSrvConn* conn, bool clear) { if (clear) { tTrace("try to destroy conn %p", conn); - uv_tcp_close_reset(conn->pTcp, uvDestroyConn); - // uv_shutdown_t* req = malloc(sizeof(uv_shutdown_t)); - // uv_shutdown(req, (uv_stream_t*)conn->pTcp, uvShutDownCb); + // uv_tcp_close_reset(conn->pTcp, uvDestroyConn); + uv_shutdown_t* req = malloc(sizeof(uv_shutdown_t)); + uv_shutdown(req, (uv_stream_t*)conn->pTcp, uvShutDownCb); // uv_unref((uv_handle_t*)conn->pTcp); // uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn); } @@ -722,8 +719,6 @@ void destroyWorkThrd(SWorkThrdObj* pThrd) { pthread_join(pThrd->thread, NULL); free(pThrd->loop); transDestroyAsyncPool(pThrd->asyncPool); - - // free(pThrd->workerAsync); free(pThrd); } void sendQuitToWorkThrd(SWorkThrdObj* pThrd) { @@ -757,6 +752,27 @@ void taosCloseServer(void* arg) { free(srv); } +void transRefSrvHandle(void* handle) { + if (handle == NULL) { + return; + } + SSrvConn* conn = handle; + + int ref = T_REF_INC((SSrvConn*)handle); + UNUSED(ref); +} + +void transUnrefSrvHandle(void* handle) { + if (handle == NULL) { + return; + } + int ref = T_REF_DEC((SSrvConn*)handle); + + if (ref == 0) { + destroyConn((SSrvConn*)handle, true); + } + // unref srv handle +} void rpcSendResponse(const SRpcMsg* pMsg) { if (pMsg->handle == NULL) { return; From 6a3869a3bc509b6dbd6b9c225f0a7ac2cea85320 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 10:50:50 +0800 Subject: [PATCH 11/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 12 ++- source/libs/sync/inc/syncRaftEntry.h | 2 +- source/libs/sync/src/syncMessage.c | 57 ++++++++++++++ source/libs/sync/src/syncRaftEntry.c | 2 +- source/libs/sync/test/CMakeLists.txt | 14 ++++ source/libs/sync/test/syncEncodeTest.cpp | 6 ++ source/libs/sync/test/syncRequestVote.cpp | 96 +++++++++++++++++++++++ 7 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 source/libs/sync/test/syncRequestVote.cpp diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index cbe45e07d4..8578d242dd 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -83,8 +83,6 @@ typedef struct SyncPing { char data[]; } SyncPing; -//#define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) - SyncPing* syncPingBuild(uint32_t dataLen); void syncPingDestroy(SyncPing* pMsg); void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); @@ -163,9 +161,19 @@ SyncRequestVote* syncRequestVoteBuild(); void syncRequestVoteDestroy(SyncRequestVote* pMsg); void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen); void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg); +char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len); +SyncRequestVote* syncRequestVoteDeserialize2(const char* buf, uint32_t len); void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg); void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg); +SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg); cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg); +char* syncRequestVote2Str(const SyncRequestVote* pMsg); + +// for debug ---------------------- +void syncRequestVotePrint(const SyncRequestVote* pMsg); +void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg); +void syncRequestVoteLog(const SyncRequestVote* pMsg); +void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg); // --------------------------------------------- typedef struct SyncRequestVoteReply { diff --git a/source/libs/sync/inc/syncRaftEntry.h b/source/libs/sync/inc/syncRaftEntry.h index 335d23ea8e..6ba27b0d8a 100644 --- a/source/libs/sync/inc/syncRaftEntry.h +++ b/source/libs/sync/inc/syncRaftEntry.h @@ -47,7 +47,7 @@ SSyncRaftEntry* syncEntryDeserialize(const char* buf, uint32_t len); cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry); char* syncEntry2Str(const SSyncRaftEntry* pEntry); -// for debug +// for debug ---------------------- void syncEntryPrint(const SSyncRaftEntry* pObj); void syncEntryPrint2(char* s, const SSyncRaftEntry* pObj); void syncEntryLog(const SSyncRaftEntry* pObj); diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 4afe14b475..3b95835e07 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -439,6 +439,25 @@ void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* assert(len == pMsg->bytes); } +char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncRequestVoteSerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncRequestVote* syncRequestVoteDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncRequestVote* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncRequestVoteDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -451,6 +470,10 @@ void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) { syncRequestVoteDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncRequestVote* pMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); +} + cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) { char u64buf[128]; @@ -499,6 +522,40 @@ cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) { return pJson; } +char* syncRequestVote2Str(const SyncRequestVote* pMsg) { + cJSON* pJson = syncRequestVote2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncRequestVotePrint(const SyncRequestVote* pMsg) { + char* serialized = syncRequestVote2Str(pMsg); + printf("syncRequestVotePrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg) { + char* serialized = syncRequestVote2Str(pMsg); + printf("syncRequestVotePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncRequestVoteLog(const SyncRequestVote* pMsg) { + char* serialized = syncRequestVote2Str(pMsg); + sTrace("syncRequestVoteLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) { + char* serialized = syncRequestVote2Str(pMsg); + sTrace("syncRequestVoteLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); +} + // ---- message process SyncRequestVoteReply---- SyncRequestVoteReply* SyncRequestVoteReplyBuild() { uint32_t bytes = sizeof(SyncRequestVoteReply); diff --git a/source/libs/sync/src/syncRaftEntry.c b/source/libs/sync/src/syncRaftEntry.c index 1a5d9c9455..f29b3022d8 100644 --- a/source/libs/sync/src/syncRaftEntry.c +++ b/source/libs/sync/src/syncRaftEntry.c @@ -104,7 +104,7 @@ char* syncEntry2Str(const SSyncRaftEntry* pEntry) { return serialized; } -// for debug ----------- +// for debug ---------------------- void syncEntryPrint(const SSyncRaftEntry* pObj) { char* serialized = syncEntry2Str(pObj); printf("syncEntryPrint | len:%lu | %s \n", strlen(serialized), serialized); diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index bfde08ffac..e442dec890 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(syncVotesRespondTest "") add_executable(syncIndexMgrTest "") add_executable(syncLogStoreTest "") add_executable(syncEntryTest "") +add_executable(syncRequestVote "") target_sources(syncTest @@ -95,6 +96,10 @@ target_sources(syncEntryTest PRIVATE "syncEntryTest.cpp" ) +target_sources(syncRequestVote + PRIVATE + "syncRequestVote.cpp" +) target_include_directories(syncTest @@ -192,6 +197,11 @@ target_include_directories(syncEntryTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncRequestVote + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -270,6 +280,10 @@ target_link_libraries(syncEntryTest sync gtest_main ) +target_link_libraries(syncRequestVote + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index 6197621051..4be239fde4 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -17,6 +17,8 @@ void logTest() { #define PING_MSG_LEN 20 #define APPEND_ENTRIES_VALUE_LEN 32 +#if 0 + void test1() { sTrace("test1: ---- syncPingSerialize, syncPingDeserialize"); @@ -487,11 +489,14 @@ void test12() { syncAppendEntriesReplyDestroy(pMsg2); } +#endif + int main() { // taosInitLog((char*)"syncPingTest.log", 100000, 10); tsAsyncLog = 0; sDebugFlag = 143 + 64; +#if 0 test1(); test2(); test3(); @@ -504,6 +509,7 @@ int main() { test10(); test11(); test12(); +#endif return 0; } diff --git a/source/libs/sync/test/syncRequestVote.cpp b/source/libs/sync/test/syncRequestVote.cpp new file mode 100644 index 0000000000..31fb5a5307 --- /dev/null +++ b/source/libs/sync/test/syncRequestVote.cpp @@ -0,0 +1,96 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +SyncRequestVote *createMsg() { + SyncRequestVote *pMsg = syncRequestVoteBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->currentTerm = 11; + pMsg->lastLogIndex = 22; + pMsg->lastLogTerm = 33; +} + +void test1() { + SyncRequestVote *pMsg = createMsg(); + syncRequestVotePrint2((char *)"test1:", pMsg); + syncRequestVoteDestroy(pMsg); +} + +void test2() { + SyncRequestVote *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncRequestVoteSerialize(pMsg, serialized, len); + SyncRequestVote *pMsg2 = syncRequestVoteBuild(); + syncRequestVoteDeserialize(serialized, len, pMsg2); + syncRequestVotePrint2((char *)"test2: syncRequestVoteSerialize -> syncRequestVoteDeserialize ", pMsg); + + free(serialized); + syncRequestVoteDestroy(pMsg); + syncRequestVoteDestroy(pMsg2); +} + +void test3() { + SyncRequestVote *pMsg = createMsg(); + uint32_t len; + char * serialized = syncRequestVoteSerialize2(pMsg, &len); + SyncRequestVote *pMsg2 = syncRequestVoteDeserialize2(serialized, len); + syncRequestVotePrint2((char *)"test3: syncRequestVoteSerialize3 -> syncRequestVoteDeserialize2 ", pMsg); + + free(serialized); + syncRequestVoteDestroy(pMsg); + syncRequestVoteDestroy(pMsg2); +} + +void test4() { + SyncRequestVote *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncRequestVote2RpcMsg(pMsg, &rpcMsg); + SyncRequestVote *pMsg2 = syncRequestVoteBuild(); + syncRequestVoteFromRpcMsg(&rpcMsg, pMsg2); + syncRequestVotePrint2((char *)"test4: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg ", pMsg); + + syncRequestVoteDestroy(pMsg); + syncRequestVoteDestroy(pMsg2); +} + +void test5() { + SyncRequestVote *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncRequestVote2RpcMsg(pMsg, &rpcMsg); + SyncRequestVote *pMsg2 = syncRequestVoteFromRpcMsg2(&rpcMsg); + syncRequestVotePrint2((char *)"test5: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg2 ", pMsg); + + syncRequestVoteDestroy(pMsg); + syncRequestVoteDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} From 5f51fb3a3267f7be77646f58c25c19fcf7ec8a22 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 11 Mar 2022 11:18:05 +0800 Subject: [PATCH 12/29] modify transport --- source/libs/transport/src/transSrv.c | 44 ++++++++++++++++++---------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index 432ec472fb..9e68b0bf7b 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -65,6 +65,7 @@ typedef struct SWorkThrdObj { queue conn; pthread_mutex_t msgMtx; void* pTransInst; + bool stop; } SWorkThrdObj; typedef struct SServerObj { @@ -386,7 +387,6 @@ static void destroyAllConn(SWorkThrdObj* pThrd) { SSrvConn* c = QUEUE_DATA(h, SSrvConn, queue); transUnrefSrvHandle(c); - // destroyConn(c, true); } } void uvWorkerAsyncCb(uv_async_t* handle) { @@ -411,10 +411,15 @@ void uvWorkerAsyncCb(uv_async_t* handle) { } if (msg->pConn == NULL) { free(msg); - destroyAllConn(pThrd); - - uv_loop_close(pThrd->loop); - uv_stop(pThrd->loop); + bool noConn = QUEUE_IS_EMPTY(&pThrd->conn); + if (noConn == true) { + uv_loop_close(pThrd->loop); + uv_stop(pThrd->loop); + } else { + destroyAllConn(pThrd); + uv_loop_close(pThrd->loop); + pThrd->stop = true; + } } else { uvStartSendResp(msg); } @@ -422,12 +427,15 @@ void uvWorkerAsyncCb(uv_async_t* handle) { } static void uvAcceptAsyncCb(uv_async_t* async) { SServerObj* srv = async->data; + tDebug("close server port %d", srv->port); uv_close((uv_handle_t*)&srv->server, NULL); uv_stop(srv->loop); } static void uvShutDownCb(uv_shutdown_t* req, int status) { - tDebug("conn failed to shut down: %s", uv_err_name(status)); + if (status != 0) { + tDebug("conn failed to shut down: %s", uv_err_name(status)); + } uv_close((uv_handle_t*)req->handle, uvDestroyConn); free(req); } @@ -509,14 +517,14 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { int addrlen = sizeof(pConn->addr); if (0 != uv_tcp_getpeername(pConn->pTcp, (struct sockaddr*)&pConn->addr, &addrlen)) { tError("server conn %p failed to get peer info", pConn); - destroyConn(pConn, true); + transUnrefSrvHandle(pConn); return; } addrlen = sizeof(pConn->locaddr); if (0 != uv_tcp_getsockname(pConn->pTcp, (struct sockaddr*)&pConn->locaddr, &addrlen)) { tError("server conn %p failed to get local info", pConn); - destroyConn(pConn, true); + transUnrefSrvHandle(pConn); return; } @@ -524,7 +532,7 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { } else { tDebug("failed to create new connection"); - destroyConn(pConn, true); + transUnrefSrvHandle(pConn); } } @@ -602,6 +610,7 @@ static SSrvConn* createConn(void* hThrd) { tTrace("conn %p created", pConn); pConn->broken = false; + transRefSrvHandle(pConn); return pConn; } @@ -617,25 +626,26 @@ static void destroyConn(SSrvConn* conn, bool clear) { destroySmsg(msg); } conn->srvMsgs = taosArrayDestroy(conn->srvMsgs); - QUEUE_REMOVE(&conn->queue); - if (clear) { tTrace("try to destroy conn %p", conn); - // uv_tcp_close_reset(conn->pTcp, uvDestroyConn); uv_shutdown_t* req = malloc(sizeof(uv_shutdown_t)); uv_shutdown(req, (uv_stream_t*)conn->pTcp, uvShutDownCb); - // uv_unref((uv_handle_t*)conn->pTcp); - // uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn); } } static void uvDestroyConn(uv_handle_t* handle) { - SSrvConn* conn = handle->data; + SSrvConn* conn = handle->data; + SWorkThrdObj* thrd = conn->hostThrd; + tDebug("server conn %p destroy", conn); uv_timer_stop(conn->pTimer); - // free(conn->pTimer); + QUEUE_REMOVE(&conn->queue); free(conn->pTcp); free(conn->pWriter); free(conn); + + if (thrd->stop && QUEUE_IS_EMPTY(&thrd->conn)) { + uv_stop(thrd->loop); + } } static int transAddAuthPart(SSrvConn* pConn, char* msg, int msgLen) { STransMsgHead* pHead = (STransMsgHead*)msg; @@ -670,6 +680,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)); + thrd->stop = false; srv->pThreadObj[i] = thrd; srv->pipe[i] = (uv_pipe_t*)calloc(2, sizeof(uv_pipe_t)); @@ -767,6 +778,7 @@ void transUnrefSrvHandle(void* handle) { return; } int ref = T_REF_DEC((SSrvConn*)handle); + tDebug("handle %p ref count: %d", handle, ref); if (ref == 0) { destroyConn((SSrvConn*)handle, true); From 5262b4ad3b78c0f5cce8ff4a98f1e7f880588386 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 11 Mar 2022 11:46:05 +0800 Subject: [PATCH 13/29] modify transport --- include/libs/transport/trpc.h | 1 - source/client/src/clientEnv.c | 3 +-- source/dnode/mgmt/impl/src/dndTransport.c | 11 +++++------ source/libs/transport/inc/transportInt.h | 1 - source/libs/transport/src/rpcMain.c | 1 - source/libs/transport/src/trans.c | 1 - source/libs/transport/src/transCli.c | 14 +++----------- 7 files changed, 9 insertions(+), 23 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 101092f13e..6ccb6c0dc4 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -64,7 +64,6 @@ typedef struct SRpcInit { int8_t connType; // TAOS_CONN_UDP, TAOS_CONN_TCPC, TAOS_CONN_TCPS int idleTime; // milliseconds, 0 means idle timer is disabled - bool noPool; // create conn pool or not // the following is for client app ecurity only char *user; // user name char spi; // security parameter index diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 08285c9d26..edb3bf4f11 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -72,8 +72,6 @@ static void deregisterRequest(SRequestObj *pRequest) { taosReleaseRef(clientConnRefPool, pTscObj->id); } - - // todo close the transporter properly void closeTransporter(STscObj *pTscObj) { if (pTscObj == NULL || pTscObj->pAppInfo->pTransporter == NULL) { @@ -241,6 +239,7 @@ void taos_init_imp(void) { clientConnRefPool = taosOpenRef(200, destroyTscObj); clientReqRefPool = taosOpenRef(40960, doDestroyRequest); + // transDestroyBuffer(&conn->readBuf); taosGetAppName(appInfo.appName, NULL); pthread_mutex_init(&appInfo.mutex, NULL); diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index 157bad26a6..db1b15dd58 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -25,8 +25,8 @@ #include "dndMnode.h" #include "dndVnodes.h" -#define INTERNAL_USER "_dnd" -#define INTERNAL_CKEY "_key" +#define INTERNAL_USER "_dnd" +#define INTERNAL_CKEY "_key" #define INTERNAL_SECRET "_pwd" static void dndInitMsgFp(STransMgmt *pMgmt) { @@ -155,7 +155,7 @@ static void dndInitMsgFp(STransMgmt *pMgmt) { } static void dndProcessResponse(void *parent, SRpcMsg *pRsp, SEpSet *pEpSet) { - SDnode *pDnode = parent; + SDnode * pDnode = parent; STransMgmt *pMgmt = &pDnode->tmgmt; tmsg_t msgType = pRsp->msgType; @@ -193,7 +193,6 @@ static int32_t dndInitClient(SDnode *pDnode) { rpcInit.ckey = INTERNAL_CKEY; rpcInit.spi = 1; rpcInit.parent = pDnode; - rpcInit.noPool = true; char pass[TSDB_PASSWORD_LEN + 1] = {0}; taosEncryptPass_c((uint8_t *)(INTERNAL_SECRET), strlen(INTERNAL_SECRET), pass); @@ -219,7 +218,7 @@ static void dndCleanupClient(SDnode *pDnode) { } static void dndProcessRequest(void *param, SRpcMsg *pReq, SEpSet *pEpSet) { - SDnode *pDnode = param; + SDnode * pDnode = param; STransMgmt *pMgmt = &pDnode->tmgmt; tmsg_t msgType = pReq->msgType; @@ -313,7 +312,7 @@ static int32_t dndRetrieveUserAuthInfo(void *parent, char *user, char *spi, char SAuthReq authReq = {0}; tstrncpy(authReq.user, user, TSDB_USER_LEN); int32_t contLen = tSerializeSAuthReq(NULL, 0, &authReq); - void *pReq = rpcMallocCont(contLen); + void * pReq = rpcMallocCont(contLen); tSerializeSAuthReq(pReq, contLen, &authReq); SRpcMsg rpcMsg = {.pCont = pReq, .contLen = contLen, .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528}; diff --git a/source/libs/transport/inc/transportInt.h b/source/libs/transport/inc/transportInt.h index 73137487eb..4e4dcf7aa4 100644 --- a/source/libs/transport/inc/transportInt.h +++ b/source/libs/transport/inc/transportInt.h @@ -54,7 +54,6 @@ typedef struct { int8_t connType; int64_t index; char label[TSDB_LABEL_LEN]; - bool noPool; // pool or not char user[TSDB_UNI_LEN]; // meter ID char spi; // security parameter index diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index e1319da162..7056d998f9 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -64,7 +64,6 @@ typedef struct { void (*cfp)(void *parent, SRpcMsg *, SEpSet *); int (*afp)(void *parent, char *user, char *spi, char *encrypt, char *secret, char *ckey); - bool noPool; int32_t refCount; void * parent; void * idPool; // handle to ID pool diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index f3e0417397..4d244665c7 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -41,7 +41,6 @@ void* rpcOpen(const SRpcInit* pInit) { pRpc->numOfThreads = pInit->numOfThreads; } - pRpc->noPool = pInit->noPool; pRpc->connType = pInit->connType; pRpc->idleTime = pInit->idleTime; pRpc->tcphandle = (*taosInitHandle[pRpc->connType])(0, pInit->localPort, pRpc->label, pRpc->numOfThreads, NULL, pRpc); diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 0d3946d967..13a5d57dfe 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -170,13 +170,7 @@ static void clientHandleResp(SCliConn* conn) { // user owns conn->persist = 1 if (conn->persist == 0) { - if (pTransInst->noPool == true) { - destroyCmsg(conn->data); - clientConnDestroy(conn, true); - return; - } else { - addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn); - } + addConnToPool(pThrd->pool, pCtx->ip, pCtx->port, conn); } destroyCmsg(conn->data); conn->data = NULL; @@ -463,10 +457,8 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { tTrace("%s client conn %p reused", CONN_GET_INST_LABEL(conn), conn); } } else { - if (pTransInst->noPool == false) { - conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); - if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); - } + conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); + if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); } if (conn != NULL) { From a0a62227c8988525fc591448833f68225f640efd Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 11:47:56 +0800 Subject: [PATCH 14/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 12 ++- source/libs/sync/src/syncIO.c | 2 +- source/libs/sync/src/syncMessage.c | 61 +++++++++++- source/libs/sync/test/CMakeLists.txt | 14 +++ source/libs/sync/test/syncRequestVote.cpp | 8 +- .../libs/sync/test/syncRequestVoteReply.cpp | 95 +++++++++++++++++++ .../libs/sync/test/syncVotesGrantedTest.cpp | 2 +- .../libs/sync/test/syncVotesRespondTest.cpp | 2 +- 8 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 source/libs/sync/test/syncRequestVoteReply.cpp diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 8578d242dd..cc4b8d39d4 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -186,13 +186,23 @@ typedef struct SyncRequestVoteReply { bool voteGranted; } SyncRequestVoteReply; -SyncRequestVoteReply* SyncRequestVoteReplyBuild(); +SyncRequestVoteReply* syncRequestVoteReplyBuild(); void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg); void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen); void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg); +char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len); +SyncRequestVoteReply* syncRequestVoteReplyDeserialize2(const char* buf, uint32_t len); void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg); void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg); +SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg); +char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg); + +// for debug ---------------------- +void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg); +void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg); +void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg); +void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg); // --------------------------------------------- typedef struct SyncAppendEntries { diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 9d2afb03ca..18ce13a585 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -263,7 +263,7 @@ static void *syncIOConsumerFunc(void *param) { } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE_REPLY) { if (io->FpOnSyncRequestVoteReply != NULL) { SyncRequestVoteReply *pSyncMsg; - pSyncMsg = SyncRequestVoteReplyBuild(); + pSyncMsg = syncRequestVoteReplyBuild(); syncRequestVoteReplyFromRpcMsg(pRpcMsg, pSyncMsg); io->FpOnSyncRequestVoteReply(io->pSyncNode, pSyncMsg); syncRequestVoteReplyDestroy(pSyncMsg); diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 3b95835e07..5bd5321ec6 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -472,6 +472,7 @@ void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg) { SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg) { SyncRequestVote* pMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; } cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) { @@ -557,7 +558,7 @@ void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) { } // ---- message process SyncRequestVoteReply---- -SyncRequestVoteReply* SyncRequestVoteReplyBuild() { +SyncRequestVoteReply* syncRequestVoteReplyBuild() { uint32_t bytes = sizeof(SyncRequestVoteReply); SyncRequestVoteReply* pMsg = malloc(bytes); memset(pMsg, 0, bytes); @@ -582,6 +583,25 @@ void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestV assert(len == pMsg->bytes); } +char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncRequestVoteReplySerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncRequestVoteReply* syncRequestVoteReplyDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncRequestVoteReply* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncRequestVoteReplyDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -594,6 +614,11 @@ void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply syncRequestVoteReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncRequestVoteReply* pMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) { char u64buf[128]; @@ -639,6 +664,40 @@ cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) { return pJson; } +char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg) { + cJSON* pJson = syncRequestVoteReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg) { + char* serialized = syncRequestVoteReply2Str(pMsg); + printf("syncRequestVoteReplyPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg) { + char* serialized = syncRequestVoteReply2Str(pMsg); + printf("syncRequestVoteReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg) { + char* serialized = syncRequestVoteReply2Str(pMsg); + sTrace("syncRequestVoteReplyLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) { + char* serialized = syncRequestVoteReply2Str(pMsg); + sTrace("syncRequestVoteReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); +} + // ---- message process SyncAppendEntries---- SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen) { uint32_t bytes = SYNC_APPEND_ENTRIES_FIX_LEN + dataLen; diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index e442dec890..71d2c5c3c9 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(syncIndexMgrTest "") add_executable(syncLogStoreTest "") add_executable(syncEntryTest "") add_executable(syncRequestVote "") +add_executable(syncRequestVoteReply "") target_sources(syncTest @@ -100,6 +101,10 @@ target_sources(syncRequestVote PRIVATE "syncRequestVote.cpp" ) +target_sources(syncRequestVoteReply + PRIVATE + "syncRequestVoteReply.cpp" +) target_include_directories(syncTest @@ -202,6 +207,11 @@ target_include_directories(syncRequestVote "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncRequestVoteReply + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -284,6 +294,10 @@ target_link_libraries(syncRequestVote sync gtest_main ) +target_link_libraries(syncRequestVoteReply + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncRequestVote.cpp b/source/libs/sync/test/syncRequestVote.cpp index 31fb5a5307..e46eb87fe9 100644 --- a/source/libs/sync/test/syncRequestVote.cpp +++ b/source/libs/sync/test/syncRequestVote.cpp @@ -38,7 +38,7 @@ void test2() { syncRequestVoteSerialize(pMsg, serialized, len); SyncRequestVote *pMsg2 = syncRequestVoteBuild(); syncRequestVoteDeserialize(serialized, len, pMsg2); - syncRequestVotePrint2((char *)"test2: syncRequestVoteSerialize -> syncRequestVoteDeserialize ", pMsg); + syncRequestVotePrint2((char *)"test2: syncRequestVoteSerialize -> syncRequestVoteDeserialize ", pMsg2); free(serialized); syncRequestVoteDestroy(pMsg); @@ -50,7 +50,7 @@ void test3() { uint32_t len; char * serialized = syncRequestVoteSerialize2(pMsg, &len); SyncRequestVote *pMsg2 = syncRequestVoteDeserialize2(serialized, len); - syncRequestVotePrint2((char *)"test3: syncRequestVoteSerialize3 -> syncRequestVoteDeserialize2 ", pMsg); + syncRequestVotePrint2((char *)"test3: syncRequestVoteSerialize3 -> syncRequestVoteDeserialize2 ", pMsg2); free(serialized); syncRequestVoteDestroy(pMsg); @@ -63,7 +63,7 @@ void test4() { syncRequestVote2RpcMsg(pMsg, &rpcMsg); SyncRequestVote *pMsg2 = syncRequestVoteBuild(); syncRequestVoteFromRpcMsg(&rpcMsg, pMsg2); - syncRequestVotePrint2((char *)"test4: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg ", pMsg); + syncRequestVotePrint2((char *)"test4: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg ", pMsg2); syncRequestVoteDestroy(pMsg); syncRequestVoteDestroy(pMsg2); @@ -74,7 +74,7 @@ void test5() { SRpcMsg rpcMsg; syncRequestVote2RpcMsg(pMsg, &rpcMsg); SyncRequestVote *pMsg2 = syncRequestVoteFromRpcMsg2(&rpcMsg); - syncRequestVotePrint2((char *)"test5: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg2 ", pMsg); + syncRequestVotePrint2((char *)"test5: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg2 ", pMsg2); syncRequestVoteDestroy(pMsg); syncRequestVoteDestroy(pMsg2); diff --git a/source/libs/sync/test/syncRequestVoteReply.cpp b/source/libs/sync/test/syncRequestVoteReply.cpp new file mode 100644 index 0000000000..37c00fac81 --- /dev/null +++ b/source/libs/sync/test/syncRequestVoteReply.cpp @@ -0,0 +1,95 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +SyncRequestVoteReply *createMsg() { + SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->term = 77; + pMsg->voteGranted = true; +} + +void test1() { + SyncRequestVoteReply *pMsg = createMsg(); + syncRequestVoteReplyPrint2((char *)"test1:", pMsg); + syncRequestVoteReplyDestroy(pMsg); +} + +void test2() { + SyncRequestVoteReply *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncRequestVoteReplySerialize(pMsg, serialized, len); + SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); + syncRequestVoteReplyDeserialize(serialized, len, pMsg2); + syncRequestVoteReplyPrint2((char *)"test2: syncRequestVoteReplySerialize -> syncRequestVoteReplyDeserialize ", pMsg2); + + free(serialized); + syncRequestVoteReplyDestroy(pMsg); + syncRequestVoteReplyDestroy(pMsg2); +} + +void test3() { + SyncRequestVoteReply *pMsg = createMsg(); + uint32_t len; + char * serialized = syncRequestVoteReplySerialize2(pMsg, &len); + SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyDeserialize2(serialized, len); + syncRequestVoteReplyPrint2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ", pMsg2); + + free(serialized); + syncRequestVoteReplyDestroy(pMsg); + syncRequestVoteReplyDestroy(pMsg2); +} + +void test4() { + SyncRequestVoteReply *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); + SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); + syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2); + syncRequestVoteReplyPrint2((char *)"test4: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg ", pMsg2); + + syncRequestVoteReplyDestroy(pMsg); + syncRequestVoteReplyDestroy(pMsg2); +} + +void test5() { + SyncRequestVoteReply *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); + SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyFromRpcMsg2(&rpcMsg); + syncRequestVoteReplyPrint2((char *)"test5: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg2 ", pMsg2); + + syncRequestVoteReplyDestroy(pMsg); + syncRequestVoteReplyDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} diff --git a/source/libs/sync/test/syncVotesGrantedTest.cpp b/source/libs/sync/test/syncVotesGrantedTest.cpp index 504fa3034a..588eb32ffd 100644 --- a/source/libs/sync/test/syncVotesGrantedTest.cpp +++ b/source/libs/sync/test/syncVotesGrantedTest.cpp @@ -118,7 +118,7 @@ int main(int argc, char** argv) { } for (int i = 0; i < replicaNum; ++i) { - SyncRequestVoteReply* reply = SyncRequestVoteReplyBuild(); + SyncRequestVoteReply* reply = syncRequestVoteReplyBuild(); reply->destId = pSyncNode->myRaftId; reply->srcId = ids[i]; reply->term = term; diff --git a/source/libs/sync/test/syncVotesRespondTest.cpp b/source/libs/sync/test/syncVotesRespondTest.cpp index 0b6abef212..76fd6fab4e 100644 --- a/source/libs/sync/test/syncVotesRespondTest.cpp +++ b/source/libs/sync/test/syncVotesRespondTest.cpp @@ -118,7 +118,7 @@ int main(int argc, char** argv) { } for (int i = 0; i < replicaNum; ++i) { - SyncRequestVoteReply* reply = SyncRequestVoteReplyBuild(); + SyncRequestVoteReply* reply = syncRequestVoteReplyBuild(); reply->destId = pSyncNode->myRaftId; reply->srcId = ids[i]; reply->term = term; From 7469d62fd7d59c8a7d2c9bf8ca0859bbeab01192 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 15:05:10 +0800 Subject: [PATCH 15/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 24 +++- source/libs/sync/src/syncMessage.c | 128 +++++++++++++++++- source/libs/sync/test/CMakeLists.txt | 48 +++++-- .../sync/test/syncAppendEntriesReplyTest.cpp | 96 +++++++++++++ .../libs/sync/test/syncAppendEntriesTest.cpp | 98 ++++++++++++++ ...Reply.cpp => syncRequestVoteReplyTest.cpp} | 15 +- ...equestVote.cpp => syncRequestVoteTest.cpp} | 0 7 files changed, 385 insertions(+), 24 deletions(-) create mode 100644 source/libs/sync/test/syncAppendEntriesReplyTest.cpp create mode 100644 source/libs/sync/test/syncAppendEntriesTest.cpp rename source/libs/sync/test/{syncRequestVoteReply.cpp => syncRequestVoteReplyTest.cpp} (87%) rename source/libs/sync/test/{syncRequestVote.cpp => syncRequestVoteTest.cpp} (100%) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index cc4b8d39d4..902a6cbe1d 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -218,17 +218,23 @@ typedef struct SyncAppendEntries { char data[]; } SyncAppendEntries; -#define SYNC_APPEND_ENTRIES_FIX_LEN \ - (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(SyncIndex) + sizeof(SyncTerm) + \ - sizeof(SyncIndex) + sizeof(uint32_t)) - SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen); void syncAppendEntriesDestroy(SyncAppendEntries* pMsg); void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen); void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg); +char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len); +SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len); void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg); void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg); +SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg); cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg); +char* syncAppendEntries2Str(const SyncAppendEntries* pMsg); + +// for debug ---------------------- +void syncAppendEntriesPrint(const SyncAppendEntries* pMsg); +void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg); +void syncAppendEntriesLog(const SyncAppendEntries* pMsg); +void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg); // --------------------------------------------- typedef struct SyncAppendEntriesReply { @@ -245,9 +251,19 @@ SyncAppendEntriesReply* syncAppendEntriesReplyBuild(); void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg); void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen); void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg); +char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len); +SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len); void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg); void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg); +SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg); +char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg); + +// for debug ---------------------- +void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg); #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 5bd5321ec6..49b3c020ab 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -700,7 +700,7 @@ void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) { // ---- message process SyncAppendEntries---- SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen) { - uint32_t bytes = SYNC_APPEND_ENTRIES_FIX_LEN + dataLen; + uint32_t bytes = sizeof(SyncAppendEntries) + dataLen; SyncAppendEntries* pMsg = malloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; @@ -723,7 +723,26 @@ void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32 void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) { memcpy(pMsg, buf, len); assert(len == pMsg->bytes); - assert(pMsg->bytes == SYNC_APPEND_ENTRIES_FIX_LEN + pMsg->dataLen); + assert(pMsg->bytes == sizeof(SyncAppendEntries) + pMsg->dataLen); +} + +char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncAppendEntriesSerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncAppendEntries* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncAppendEntriesDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; } void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg) { @@ -738,6 +757,11 @@ void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg syncAppendEntriesDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) { char u64buf[128]; @@ -785,13 +809,53 @@ cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) { cJSON_AddStringToObject(pRoot, "commit_index", u64buf); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); - cJSON_AddStringToObject(pRoot, "data", pMsg->data); + char* s; + s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", s); + free(s); + s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data2", s); + free(s); cJSON* pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "SyncAppendEntries", pRoot); return pJson; } +char* syncAppendEntries2Str(const SyncAppendEntries* pMsg) { + cJSON* pJson = syncAppendEntries2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncAppendEntriesPrint(const SyncAppendEntries* pMsg) { + char* serialized = syncAppendEntries2Str(pMsg); + printf("syncAppendEntriesPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg) { + char* serialized = syncAppendEntries2Str(pMsg); + printf("syncAppendEntriesPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncAppendEntriesLog(const SyncAppendEntries* pMsg) { + char* serialized = syncAppendEntries2Str(pMsg); + sTrace("syncAppendEntriesLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) { + char* serialized = syncAppendEntries2Str(pMsg); + sTrace("syncAppendEntriesLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); +} + // ---- message process SyncAppendEntriesReply---- SyncAppendEntriesReply* syncAppendEntriesReplyBuild() { uint32_t bytes = sizeof(SyncAppendEntriesReply); @@ -818,6 +882,25 @@ void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppend assert(len == pMsg->bytes); } +char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncAppendEntriesReplySerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncAppendEntriesReply* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncAppendEntriesReplyDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -830,6 +913,11 @@ void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesR syncAppendEntriesReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) { char u64buf[128]; @@ -874,4 +962,38 @@ cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) { cJSON* pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot); return pJson; +} + +char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg) { + cJSON* pJson = syncAppendEntriesReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg) { + char* serialized = syncAppendEntriesReply2Str(pMsg); + printf("syncAppendEntriesReplyPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg) { + char* serialized = syncAppendEntriesReply2Str(pMsg); + printf("syncAppendEntriesReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg) { + char* serialized = syncAppendEntriesReply2Str(pMsg); + sTrace("syncAppendEntriesReplyLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) { + char* serialized = syncAppendEntriesReply2Str(pMsg); + sTrace("syncAppendEntriesReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } \ No newline at end of file diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 71d2c5c3c9..8d25c357a3 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -17,8 +17,10 @@ add_executable(syncVotesRespondTest "") add_executable(syncIndexMgrTest "") add_executable(syncLogStoreTest "") add_executable(syncEntryTest "") -add_executable(syncRequestVote "") -add_executable(syncRequestVoteReply "") +add_executable(syncRequestVoteTest "") +add_executable(syncRequestVoteReplyTest "") +add_executable(syncAppendEntriesTest "") +add_executable(syncAppendEntriesReplyTest "") target_sources(syncTest @@ -97,13 +99,21 @@ target_sources(syncEntryTest PRIVATE "syncEntryTest.cpp" ) -target_sources(syncRequestVote +target_sources(syncRequestVoteTest PRIVATE - "syncRequestVote.cpp" + "syncRequestVoteTest.cpp" ) -target_sources(syncRequestVoteReply +target_sources(syncRequestVoteReplyTest PRIVATE - "syncRequestVoteReply.cpp" + "syncRequestVoteReplyTest.cpp" +) +target_sources(syncAppendEntriesTest + PRIVATE + "syncAppendEntriesTest.cpp" +) +target_sources(syncAppendEntriesReplyTest + PRIVATE + "syncAppendEntriesReplyTest.cpp" ) @@ -202,12 +212,22 @@ target_include_directories(syncEntryTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncRequestVote +target_include_directories(syncRequestVoteTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncRequestVoteReply +target_include_directories(syncRequestVoteReplyTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncAppendEntriesTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncAppendEntriesReplyTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" @@ -290,11 +310,19 @@ target_link_libraries(syncEntryTest sync gtest_main ) -target_link_libraries(syncRequestVote +target_link_libraries(syncRequestVoteTest sync gtest_main ) -target_link_libraries(syncRequestVoteReply +target_link_libraries(syncRequestVoteReplyTest + sync + gtest_main +) +target_link_libraries(syncAppendEntriesTest + sync + gtest_main +) +target_link_libraries(syncAppendEntriesReplyTest sync gtest_main ) diff --git a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp new file mode 100644 index 0000000000..c802350ae5 --- /dev/null +++ b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp @@ -0,0 +1,96 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +SyncAppendEntriesReply *createMsg() { + SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->success = true; + pMsg->matchIndex = 77; +} + +void test1() { + SyncAppendEntriesReply *pMsg = createMsg(); + syncAppendEntriesReplyPrint2((char *)"test1:", pMsg); + syncAppendEntriesReplyDestroy(pMsg); +} + +void test2() { + SyncAppendEntriesReply *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncAppendEntriesReplySerialize(pMsg, serialized, len); + SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(); + syncAppendEntriesReplyDeserialize(serialized, len, pMsg2); + syncAppendEntriesReplyPrint2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ", pMsg2); + + free(serialized); + syncAppendEntriesReplyDestroy(pMsg); + syncAppendEntriesReplyDestroy(pMsg2); +} + +void test3() { + SyncAppendEntriesReply *pMsg = createMsg(); + uint32_t len; + char * serialized = syncAppendEntriesReplySerialize2(pMsg, &len); + SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyDeserialize2(serialized, len); + syncAppendEntriesReplyPrint2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ", + pMsg2); + + free(serialized); + syncAppendEntriesReplyDestroy(pMsg); + syncAppendEntriesReplyDestroy(pMsg2); +} + +void test4() { + SyncAppendEntriesReply *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); + SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(); + syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2); + syncAppendEntriesReplyPrint2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ", pMsg2); + + syncAppendEntriesReplyDestroy(pMsg); + syncAppendEntriesReplyDestroy(pMsg2); +} + +void test5() { + SyncAppendEntriesReply *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); + SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyFromRpcMsg2(&rpcMsg); + syncAppendEntriesReplyPrint2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ", pMsg2); + + syncAppendEntriesReplyDestroy(pMsg); + syncAppendEntriesReplyDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} diff --git a/source/libs/sync/test/syncAppendEntriesTest.cpp b/source/libs/sync/test/syncAppendEntriesTest.cpp new file mode 100644 index 0000000000..6fa3041919 --- /dev/null +++ b/source/libs/sync/test/syncAppendEntriesTest.cpp @@ -0,0 +1,98 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +SyncAppendEntries *createMsg() { + SyncAppendEntries *pMsg = syncAppendEntriesBuild(20); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->prevLogIndex = 11; + pMsg->prevLogTerm = 22; + pMsg->commitIndex = 33; + strcpy(pMsg->data, "hello world"); + return pMsg; +} + +void test1() { + SyncAppendEntries *pMsg = createMsg(); + syncAppendEntriesPrint2((char *)"test1:", pMsg); + syncAppendEntriesDestroy(pMsg); +} + +void test2() { + SyncAppendEntries *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncAppendEntriesSerialize(pMsg, serialized, len); + SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg2->dataLen); + syncAppendEntriesDeserialize(serialized, len, pMsg2); + syncAppendEntriesPrint2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2); + + free(serialized); + syncAppendEntriesDestroy(pMsg); + syncAppendEntriesDestroy(pMsg2); +} + +void test3() { + SyncAppendEntries *pMsg = createMsg(); + uint32_t len; + char * serialized = syncAppendEntriesSerialize2(pMsg, &len); + SyncAppendEntries *pMsg2 = syncAppendEntriesDeserialize2(serialized, len); + syncAppendEntriesPrint2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2); + + free(serialized); + syncAppendEntriesDestroy(pMsg); + syncAppendEntriesDestroy(pMsg2); +} + +void test4() { + SyncAppendEntries *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncAppendEntries2RpcMsg(pMsg, &rpcMsg); + SyncAppendEntries *pMsg2 = (SyncAppendEntries *)malloc(rpcMsg.contLen); + syncAppendEntriesFromRpcMsg(&rpcMsg, pMsg2); + syncAppendEntriesPrint2((char *)"test4: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg ", pMsg2); + + syncAppendEntriesDestroy(pMsg); + syncAppendEntriesDestroy(pMsg2); +} + +void test5() { + SyncAppendEntries *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncAppendEntries2RpcMsg(pMsg, &rpcMsg); + SyncAppendEntries *pMsg2 = syncAppendEntriesFromRpcMsg2(&rpcMsg); + syncAppendEntriesPrint2((char *)"test5: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg2 ", pMsg2); + + syncAppendEntriesDestroy(pMsg); + syncAppendEntriesDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} diff --git a/source/libs/sync/test/syncRequestVoteReply.cpp b/source/libs/sync/test/syncRequestVoteReplyTest.cpp similarity index 87% rename from source/libs/sync/test/syncRequestVoteReply.cpp rename to source/libs/sync/test/syncRequestVoteReplyTest.cpp index 37c00fac81..52f54cbaaf 100644 --- a/source/libs/sync/test/syncRequestVoteReply.cpp +++ b/source/libs/sync/test/syncRequestVoteReplyTest.cpp @@ -32,8 +32,8 @@ void test1() { void test2() { SyncRequestVoteReply *pMsg = createMsg(); - uint32_t len = pMsg->bytes; - char * serialized = (char *)malloc(len); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); syncRequestVoteReplySerialize(pMsg, serialized, len); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); syncRequestVoteReplyDeserialize(serialized, len, pMsg2); @@ -46,10 +46,11 @@ void test2() { void test3() { SyncRequestVoteReply *pMsg = createMsg(); - uint32_t len; - char * serialized = syncRequestVoteReplySerialize2(pMsg, &len); + uint32_t len; + char * serialized = syncRequestVoteReplySerialize2(pMsg, &len); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyDeserialize2(serialized, len); - syncRequestVoteReplyPrint2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ", pMsg2); + syncRequestVoteReplyPrint2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ", + pMsg2); free(serialized); syncRequestVoteReplyDestroy(pMsg); @@ -58,7 +59,7 @@ void test3() { void test4() { SyncRequestVoteReply *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2); @@ -70,7 +71,7 @@ void test4() { void test5() { SyncRequestVoteReply *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyFromRpcMsg2(&rpcMsg); syncRequestVoteReplyPrint2((char *)"test5: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg2 ", pMsg2); diff --git a/source/libs/sync/test/syncRequestVote.cpp b/source/libs/sync/test/syncRequestVoteTest.cpp similarity index 100% rename from source/libs/sync/test/syncRequestVote.cpp rename to source/libs/sync/test/syncRequestVoteTest.cpp From e04ef027abbbacfed387b2e85f7fb12e9936d847 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Fri, 11 Mar 2022 16:49:07 +0800 Subject: [PATCH 16/29] [TD-13768]: redefine time api. --- include/os/osDef.h | 2 - include/os/osTime.h | 12 + source/common/src/tname.c | 2 +- source/common/src/ttime.c | 24 +- source/libs/executor/src/executorimpl.c | 2 +- source/libs/executor/test/executorTests.cpp | 6 +- source/libs/executor/test/lhashTests.cpp | 2 +- source/libs/qworker/test/qworkerTests.cpp | 14 +- .../libs/scalar/test/filter/filterTests.cpp | 2 +- .../libs/scalar/test/scalar/scalarTests.cpp | 2 +- source/libs/scheduler/test/schedulerTests.cpp | 4 +- source/libs/sync/src/syncEnv.c | 2 +- source/libs/sync/src/syncIO.c | 2 +- source/libs/transport/test/pushClient.c | 4 +- source/libs/transport/test/rclient.c | 4 +- source/libs/transport/test/rsclient.c | 4 +- source/libs/transport/test/syncClient.c | 4 +- source/os/src/osRand.c | 4 +- source/os/src/osStrptime.c | 692 +++++++++--------- source/os/src/osTime.c | 392 +++++++++- source/os/src/osTimezone.c | 8 +- source/util/src/tdes.c | 2 +- source/util/src/tlog.c | 2 +- source/util/src/tskiplist.c | 2 +- source/util/test/codingTests.cpp | 4 +- source/util/test/pageBufferTest.cpp | 2 +- source/util/test/skiplistTest.cpp | 4 +- tests/script/api/batchprepare.c | 2 +- tests/script/api/stmtBatchTest.c | 2 +- tests/test/c/tmqDemo.c | 2 +- 30 files changed, 786 insertions(+), 424 deletions(-) diff --git a/include/os/osDef.h b/include/os/osDef.h index 339bf13343..7df9ad39bb 100644 --- a/include/os/osDef.h +++ b/include/os/osDef.h @@ -65,8 +65,6 @@ extern "C" { #define in_addr_t unsigned long #define socklen_t int - struct tm *localtime_r(const time_t *timep, struct tm *result); - char * strptime(const char *buf, const char *fmt, struct tm *tm); char * strsep(char **stringp, const char *delim); char * getpass(const char *prefix); char * strndup(const char *s, size_t n); diff --git a/include/os/osTime.h b/include/os/osTime.h index 62ecd5477f..4718082ad1 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -20,6 +20,15 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define strptime STRPTIME_FUNC_TAOS_FORBID + #define gettimeofday GETTIMEOFDAY_FUNC_TAOS_FORBID + #define localtime_s LOCALTIMES_FUNC_TAOS_FORBID + #define localtime_r LOCALTIMER_FUNC_TAOS_FORBID + #define time TIME_FUNC_TAOS_FORBID +#endif + #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) #ifdef _TD_GO_DLL_ #define MILLISECOND_PER_SECOND (1000LL) @@ -61,6 +70,9 @@ static FORCE_INLINE int64_t taosGetTimestampNs() { return (int64_t)systemTime.tv_sec * 1000000000L + (int64_t)systemTime.tv_nsec; } +char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm); +struct tm *taosLocalTime(const time_t *timep, struct tm *result); + #ifdef __cplusplus } #endif diff --git a/source/common/src/tname.c b/source/common/src/tname.c index fb77417cac..a97792de66 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -61,7 +61,7 @@ int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, in } struct tm tm; time_t t = (time_t)start; - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index 7a318c2eb8..22c10f62b4 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -69,7 +69,7 @@ static int64_t m_deltaUtc = 0; void deltaToUtcInitOnce() { struct tm tm = {0}; - (void)strptime("1970-01-01 00:00:00", (const char*)("%Y-%m-%d %H:%M:%S"), &tm); + (void)taosStrpTime("1970-01-01 00:00:00", (const char*)("%Y-%m-%d %H:%M:%S"), &tm); m_deltaUtc = (int64_t)mktime(&tm); // printf("====delta:%lld\n\n", seconds); } @@ -236,9 +236,9 @@ int32_t parseTimeWithTz(const char* timestr, int64_t* time, int32_t timePrec, ch char* str; if (delim == 'T') { - str = strptime(timestr, "%Y-%m-%dT%H:%M:%S", &tm); + str = taosStrpTime(timestr, "%Y-%m-%dT%H:%M:%S", &tm); } else if (delim == 0) { - str = strptime(timestr, "%Y-%m-%d %H:%M:%S", &tm); + str = taosStrpTime(timestr, "%Y-%m-%d %H:%M:%S", &tm); } else { str = NULL; } @@ -303,7 +303,7 @@ int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec) { *time = 0; struct tm tm = {0}; - char* str = strptime(timestr, "%Y-%m-%d %H:%M:%S", &tm); + char* str = taosStrpTime(timestr, "%Y-%m-%d %H:%M:%S", &tm); if (str == NULL) { return -1; } @@ -338,7 +338,7 @@ int32_t parseLocaltimeDst(char* timestr, int64_t* time, int32_t timePrec) { struct tm tm = {0}; tm.tm_isdst = -1; - char* str = strptime(timestr, "%Y-%m-%d %H:%M:%S", &tm); + char* str = taosStrpTime(timestr, "%Y-%m-%d %H:%M:%S", &tm); if (str == NULL) { return -1; } @@ -466,7 +466,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { struct tm tm; time_t tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision)); - localtime_r(&tt, &tm); + taosLocalTime(&tt, &tm); int32_t mon = tm.tm_year * 12 + tm.tm_mon + (int32_t)duration; tm.tm_year = mon / 12; tm.tm_mon = mon % 12; @@ -489,11 +489,11 @@ int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char struct tm tm; time_t t = (time_t)skey; - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); int32_t smon = tm.tm_year * 12 + tm.tm_mon; t = (time_t)ekey; - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); int32_t emon = tm.tm_year * 12 + tm.tm_mon; if (unit == 'y') { @@ -514,7 +514,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio start /= (int64_t)(TSDB_TICK_PER_SECOND(precision)); struct tm tm; time_t tt = (time_t)start; - localtime_r(&tt, &tm); + taosLocalTime(&tt, &tm); tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; @@ -597,13 +597,13 @@ const char* fmtts(int64_t ts) { if (ts > -62135625943 && ts < 32503651200) { time_t t = (time_t)ts; - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); pos += strftime(buf + pos, sizeof(buf), "s=%Y-%m-%d %H:%M:%S", &tm); } if (ts > -62135625943000 && ts < 32503651200000) { time_t t = (time_t)(ts / 1000); - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); if (pos > 0) { buf[pos++] = ' '; buf[pos++] = '|'; @@ -615,7 +615,7 @@ const char* fmtts(int64_t ts) { { time_t t = (time_t)(ts / 1000000); - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); if (pos > 0) { buf[pos++] = ' '; buf[pos++] = '|'; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index b1b190c816..9b519f9004 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -163,7 +163,7 @@ static void getNextTimeWindow(SInterval* pInterval, int32_t precision, int32_t o struct tm tm; time_t t = (time_t)key; - localtime_r(&t, &tm); + taosLocalTime(&t, &tm); int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor); tm.tm_year = mon / 12; diff --git a/source/libs/executor/test/executorTests.cpp b/source/libs/executor/test/executorTests.cpp index 624c04da2f..28e2c9ef75 100644 --- a/source/libs/executor/test/executorTests.cpp +++ b/source/libs/executor/test/executorTests.cpp @@ -1019,7 +1019,7 @@ TEST(testCase, external_sort_Test) { return; #endif - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {0}; @@ -1080,7 +1080,7 @@ TEST(testCase, external_sort_Test) { } TEST(testCase, sorted_merge_Test) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {0}; @@ -1152,7 +1152,7 @@ TEST(testCase, sorted_merge_Test) { } TEST(testCase, time_interval_Operator_Test) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {0}; diff --git a/source/libs/executor/test/lhashTests.cpp b/source/libs/executor/test/lhashTests.cpp index 88cf713727..695552faa0 100644 --- a/source/libs/executor/test/lhashTests.cpp +++ b/source/libs/executor/test/lhashTests.cpp @@ -25,7 +25,7 @@ #pragma GCC diagnostic ignored "-Wsign-compare" TEST(testCase, linear_hash_Tests) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT); #if 0 diff --git a/source/libs/qworker/test/qworkerTests.cpp b/source/libs/qworker/test/qworkerTests.cpp index 2e262abcd0..d7814a5dfb 100644 --- a/source/libs/qworker/test/qworkerTests.cpp +++ b/source/libs/qworker/test/qworkerTests.cpp @@ -963,7 +963,7 @@ TEST(seqTest, randCase) { stubSetRpcSendResponse(); stubSetCreateExecTask(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); @@ -1025,7 +1025,7 @@ TEST(seqTest, multithreadRand) { stubSetStringToPlan(); stubSetRpcSendResponse(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); @@ -1076,7 +1076,7 @@ TEST(rcTest, shortExecshortDelay) { stubSetPutDataBlock(); stubSetGetDataBlock(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); qwtTestStop = false; qwtTestQuitThreadNum = 0; @@ -1157,7 +1157,7 @@ TEST(rcTest, longExecshortDelay) { stubSetPutDataBlock(); stubSetGetDataBlock(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); qwtTestStop = false; qwtTestQuitThreadNum = 0; @@ -1240,7 +1240,7 @@ TEST(rcTest, shortExeclongDelay) { stubSetPutDataBlock(); stubSetGetDataBlock(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); qwtTestStop = false; qwtTestQuitThreadNum = 0; @@ -1324,7 +1324,7 @@ TEST(rcTest, dropTest) { stubSetPutDataBlock(); stubSetGetDataBlock(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue); ASSERT_EQ(code, 0); @@ -1358,7 +1358,7 @@ TEST(rcTest, dropTest) { int main(int argc, char** argv) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/scalar/test/filter/filterTests.cpp b/source/libs/scalar/test/filter/filterTests.cpp index d435159b2e..328028a1d4 100644 --- a/source/libs/scalar/test/filter/filterTests.cpp +++ b/source/libs/scalar/test/filter/filterTests.cpp @@ -1286,7 +1286,7 @@ TEST(scalarModelogicTest, diff_columns_or_and_or) { int main(int argc, char** argv) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index a631511867..d6a73d99bb 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -1435,7 +1435,7 @@ TEST(columnTest, greater_and_lower) { int main(int argc, char** argv) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index e1bb9a0533..376ed1e2bc 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -724,7 +724,7 @@ TEST(queryTest, flowCtrlCase) { schtInitLogFile(); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); SArray *qnodeList = taosArrayInit(1, sizeof(SEp)); @@ -873,7 +873,7 @@ TEST(multiThread, forceFree) { } int main(int argc, char** argv) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index fa58bda76f..cb38b3f6f8 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -28,7 +28,7 @@ static void doSyncEnvStopTimer(SSyncEnv *pSyncEnv, tmr_h *pTimer); int32_t syncEnvStart() { int32_t ret; - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); gSyncEnv = (SSyncEnv *)malloc(sizeof(SSyncEnv)); assert(gSyncEnv != NULL); ret = doSyncEnvStart(gSyncEnv); diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 9d2afb03ca..d2de22b66c 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -44,7 +44,7 @@ int32_t syncIOStart(char *host, uint16_t port) { gSyncIO = syncIOCreate(host, port); assert(gSyncIO != NULL); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); int32_t ret = syncIOStartInternal(gSyncIO); assert(ret == 0); diff --git a/source/libs/transport/test/pushClient.c b/source/libs/transport/test/pushClient.c index bdb754ea14..2a569cbe30 100644 --- a/source/libs/transport/test/pushClient.c +++ b/source/libs/transport/test/pushClient.c @@ -203,7 +203,7 @@ int main(int argc, char *argv[]) { tInfo("client is initialized"); tInfo("threads:%d msgSize:%d requests:%d", appThreads, msgSize, numOfReqs); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); startTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads); @@ -226,7 +226,7 @@ int main(int argc, char *argv[]) { taosUsleep(1); } while (tcount < appThreads); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); endTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; float usedTime = (endTime - startTime) / 1000.0f; // mseconds diff --git a/source/libs/transport/test/rclient.c b/source/libs/transport/test/rclient.c index f30d725f4c..c6ff2480ef 100644 --- a/source/libs/transport/test/rclient.c +++ b/source/libs/transport/test/rclient.c @@ -181,7 +181,7 @@ int main(int argc, char *argv[]) { tInfo("client is initialized"); tInfo("threads:%d msgSize:%d requests:%d", appThreads, msgSize, numOfReqs); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); startTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads); @@ -204,7 +204,7 @@ int main(int argc, char *argv[]) { taosUsleep(1); } while (tcount < appThreads); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); endTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; float usedTime = (endTime - startTime) / 1000.0f; // mseconds diff --git a/source/libs/transport/test/rsclient.c b/source/libs/transport/test/rsclient.c index f2a963b83f..1fe13a35b1 100644 --- a/source/libs/transport/test/rsclient.c +++ b/source/libs/transport/test/rsclient.c @@ -158,7 +158,7 @@ int main(int argc, char *argv[]) { tInfo("client is initialized"); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); startTime = systemTime.tv_sec*1000000 + systemTime.tv_usec; SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo)*appThreads); @@ -181,7 +181,7 @@ int main(int argc, char *argv[]) { taosUsleep(1); } while ( tcount < appThreads); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); endTime = systemTime.tv_sec*1000000 + systemTime.tv_usec; float usedTime = (endTime - startTime)/1000.0; // mseconds diff --git a/source/libs/transport/test/syncClient.c b/source/libs/transport/test/syncClient.c index f84ced5374..b3c2f6ebdc 100644 --- a/source/libs/transport/test/syncClient.c +++ b/source/libs/transport/test/syncClient.c @@ -181,7 +181,7 @@ int main(int argc, char *argv[]) { tInfo("client is initialized"); tInfo("threads:%d msgSize:%d requests:%d", appThreads, msgSize, numOfReqs); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); startTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads); @@ -204,7 +204,7 @@ int main(int argc, char *argv[]) { taosUsleep(1); } while (tcount < appThreads); - gettimeofday(&systemTime, NULL); + taosGetTimeOfDay(&systemTime); endTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; float usedTime = (endTime - startTime) / 1000.0f; // mseconds diff --git a/source/os/src/osRand.c b/source/os/src/osRand.c index f3dd9b74c5..847f3a52a6 100644 --- a/source/os/src/osRand.c +++ b/source/os/src/osRand.c @@ -33,11 +33,11 @@ uint32_t taosSafeRand(void) { pFile = taosOpenFile("/dev/urandom", TD_FILE_READ); if (pFile == NULL) { - seed = (int)time(0); + seed = (int)taosGetTimestampSec(); } else { int len = taosReadFile(pFile, &seed, sizeof(seed)); if (len < 0) { - seed = (int)time(0); + seed = (int)taosGetTimestampSec(); } taosCloseFile(&pFile); } diff --git a/source/os/src/osStrptime.c b/source/os/src/osStrptime.c index e8c873bc49..99549f02cb 100644 --- a/source/os/src/osStrptime.c +++ b/source/os/src/osStrptime.c @@ -38,366 +38,368 @@ // //#include "lukemftp.h" -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +// #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) -#include -#include -#include -#include -//#define TM_YEAR_BASE 1970 //origin -#define TM_YEAR_BASE 1900 //slguan -/* -* We do not implement alternate representations. However, we always -* check whether a given modifier is allowed for a certain conversion. -*/ -#define ALT_E 0x01 -#define ALT_O 0x02 -#define LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); } +// #include +// #include +// #include +// #include +// //#define TM_YEAR_BASE 1970 //origin +// #define TM_YEAR_BASE 1900 //slguan +// /* +// * We do not implement alternate representations. However, we always +// * check whether a given modifier is allowed for a certain conversion. +// */ +// #define ALT_E 0x01 +// #define ALT_O 0x02 +// #define LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); } -static int conv_num(const char **, int *, int, int); +// static int conv_num(const char **buf, int *dest, int llim, int ulim) +// { +// int result = 0; -static const char *day[7] = { - "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", - "Friday", "Saturday" -}; -static const char *abday[7] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" -}; -static const char *mon[12] = { - "January", "February", "March", "April", "May", "June", "July", - "August", "September", "October", "November", "December" -}; -static const char *abmon[12] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; -static const char *am_pm[2] = { - "AM", "PM" -}; +// /* The limit also determines the number of valid digits. */ +// int rulim = ulim; + +// if (**buf < '0' || **buf > '9') +// return (0); + +// do { +// result *= 10; +// result += *(*buf)++ - '0'; +// rulim /= 10; +// } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9'); + +// if (result < llim || result > ulim) +// return (0); + +// *dest = result; +// return (1); +// } + +// static const char *day[7] = { +// "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", +// "Friday", "Saturday" +// }; +// static const char *abday[7] = { +// "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" +// }; +// static const char *mon[12] = { +// "January", "February", "March", "April", "May", "June", "July", +// "August", "September", "October", "November", "December" +// }; +// static const char *abmon[12] = { +// "Jan", "Feb", "Mar", "Apr", "May", "Jun", +// "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +// }; +// static const char *am_pm[2] = { +// "AM", "PM" +// }; + +// #endif + +// char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) { +// #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +// char c; +// const char *bp; +// size_t len = 0; +// int alt_format, i, split_year = 0; + +// bp = buf; + +// while ((c = *fmt) != '\0') { +// /* Clear `alternate' modifier prior to new conversion. */ +// alt_format = 0; + +// /* Eat up white-space. */ +// if (isspace(c)) { +// while (isspace(*bp)) +// bp++; + +// fmt++; +// continue; +// } + +// if ((c = *fmt++) != '%') +// goto literal; -char * -strptime(const char *buf, const char *fmt, struct tm *tm) -{ - char c; - const char *bp; - size_t len = 0; - int alt_format, i, split_year = 0; +// again: switch (c = *fmt++) { +// case '%': /* "%%" is converted to "%". */ +// literal : +// if (c != *bp++) +// return (0); +// break; - bp = buf; +// /* +// * "Alternative" modifiers. Just set the appropriate flag +// * and start over again. +// */ +// case 'E': /* "%E?" alternative conversion modifier. */ +// LEGAL_ALT(0); +// alt_format |= ALT_E; +// goto again; - while ((c = *fmt) != '\0') { - /* Clear `alternate' modifier prior to new conversion. */ - alt_format = 0; +// case 'O': /* "%O?" alternative conversion modifier. */ +// LEGAL_ALT(0); +// alt_format |= ALT_O; +// goto again; - /* Eat up white-space. */ - if (isspace(c)) { - while (isspace(*bp)) - bp++; +// /* +// * "Complex" conversion rules, implemented through recursion. +// */ +// case 'c': /* Date and time, using the locale's format. */ +// LEGAL_ALT(ALT_E); +// if (!(bp = taosStrpTime(bp, "%x %X", tm))) +// return (0); +// break; - fmt++; - continue; - } +// case 'D': /* The date as "%m/%d/%y". */ +// LEGAL_ALT(0); +// if (!(bp = taosStrpTime(bp, "%m/%d/%y", tm))) +// return (0); +// break; - if ((c = *fmt++) != '%') - goto literal; +// case 'R': /* The time as "%H:%M". */ +// LEGAL_ALT(0); +// if (!(bp = taosStrpTime(bp, "%H:%M", tm))) +// return (0); +// break; + +// case 'r': /* The time in 12-hour clock representation. */ +// LEGAL_ALT(0); +// if (!(bp = taosStrpTime(bp, "%I:%M:%S %p", tm))) +// return (0); +// break; + +// case 'T': /* The time as "%H:%M:%S". */ +// LEGAL_ALT(0); +// if (!(bp = taosStrpTime(bp, "%H:%M:%S", tm))) +// return (0); +// break; + +// case 'X': /* The time, using the locale's format. */ +// LEGAL_ALT(ALT_E); +// if (!(bp = taosStrpTime(bp, "%H:%M:%S", tm))) +// return (0); +// break; + +// case 'x': /* The date, using the locale's format. */ +// LEGAL_ALT(ALT_E); +// if (!(bp = taosStrpTime(bp, "%m/%d/%y", tm))) +// return (0); +// break; + +// /* +// * "Elementary" conversion rules. +// */ +// case 'A': /* The day of week, using the locale's form. */ +// case 'a': +// LEGAL_ALT(0); +// for (i = 0; i < 7; i++) { +// /* Full name. */ +// len = strlen(day[i]); +// if (strncmp(day[i], bp, len) == 0) +// break; + +// /* Abbreviated name. */ +// len = strlen(abday[i]); +// if (strncmp(abday[i], bp, len) == 0) +// break; +// } + +// /* Nothing matched. */ +// if (i == 7) +// return (0); + +// tm->tm_wday = i; +// bp += len; +// break; + +// case 'B': /* The month, using the locale's form. */ +// case 'b': +// case 'h': +// LEGAL_ALT(0); +// for (i = 0; i < 12; i++) { +// /* Full name. */ +// len = strlen(mon[i]); +// if (strncmp(mon[i], bp, len) == 0) +// break; + +// /* Abbreviated name. */ +// len = strlen(abmon[i]); +// if (strncmp(abmon[i], bp, len) == 0) +// break; +// } + +// /* Nothing matched. */ +// if (i == 12) +// return (0); + +// tm->tm_mon = i; +// bp += len; +// break; + +// case 'C': /* The century number. */ +// LEGAL_ALT(ALT_E); +// if (!(conv_num(&bp, &i, 0, 99))) +// return (0); + +// if (split_year) { +// tm->tm_year = (tm->tm_year % 100) + (i * 100); +// } +// else { +// tm->tm_year = i * 100; +// split_year = 1; +// } +// break; + +// case 'd': /* The day of month. */ +// case 'e': +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &tm->tm_mday, 1, 31))) +// return (0); +// break; + +// case 'k': /* The hour (24-hour clock representation). */ +// LEGAL_ALT(0); +// /* FALLTHROUGH */ +// case 'H': +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &tm->tm_hour, 0, 23))) +// return (0); +// break; + +// case 'l': /* The hour (12-hour clock representation). */ +// LEGAL_ALT(0); +// /* FALLTHROUGH */ +// case 'I': +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &tm->tm_hour, 1, 12))) +// return (0); +// if (tm->tm_hour == 12) +// tm->tm_hour = 0; +// break; + +// case 'j': /* The day of year. */ +// LEGAL_ALT(0); +// if (!(conv_num(&bp, &i, 1, 366))) +// return (0); +// tm->tm_yday = i - 1; +// break; + +// case 'M': /* The minute. */ +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &tm->tm_min, 0, 59))) +// return (0); +// break; + +// case 'm': /* The month. */ +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &i, 1, 12))) +// return (0); +// tm->tm_mon = i - 1; +// break; + +// case 'p': /* The locale's equivalent of AM/PM. */ +// LEGAL_ALT(0); +// /* AM? */ +// if (strcmp(am_pm[0], bp) == 0) { +// if (tm->tm_hour > 11) +// return (0); + +// bp += strlen(am_pm[0]); +// break; +// } +// /* PM? */ +// else if (strcmp(am_pm[1], bp) == 0) { +// if (tm->tm_hour > 11) +// return (0); + +// tm->tm_hour += 12; +// bp += strlen(am_pm[1]); +// break; +// } + +// /* Nothing matched. */ +// return (0); + +// case 'S': /* The seconds. */ +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &tm->tm_sec, 0, 61))) +// return (0); +// break; + +// case 'U': /* The week of year, beginning on sunday. */ +// case 'W': /* The week of year, beginning on monday. */ +// LEGAL_ALT(ALT_O); +// /* +// * XXX This is bogus, as we can not assume any valid +// * information present in the tm structure at this +// * point to calculate a real value, so just check the +// * range for now. +// */ +// if (!(conv_num(&bp, &i, 0, 53))) +// return (0); +// break; + +// case 'w': /* The day of week, beginning on sunday. */ +// LEGAL_ALT(ALT_O); +// if (!(conv_num(&bp, &tm->tm_wday, 0, 6))) +// return (0); +// break; + +// case 'Y': /* The year. */ +// LEGAL_ALT(ALT_E); +// if (!(conv_num(&bp, &i, 0, 9999))) +// return (0); + +// tm->tm_year = i - TM_YEAR_BASE; +// break; + +// case 'y': /* The year within 100 years of the epoch. */ +// LEGAL_ALT(ALT_E | ALT_O); +// if (!(conv_num(&bp, &i, 0, 99))) +// return (0); + +// if (split_year) { +// tm->tm_year = ((tm->tm_year / 100) * 100) + i; +// break; +// } +// split_year = 1; +// if (i <= 68) +// tm->tm_year = i + 2000 - TM_YEAR_BASE; +// else +// tm->tm_year = i + 1900 - TM_YEAR_BASE; +// break; + +// /* +// * Miscellaneous conversions. +// */ +// case 'n': /* Any kind of white-space. */ +// case 't': +// LEGAL_ALT(0); +// while (isspace(*bp)) +// bp++; +// break; - again: switch (c = *fmt++) { - case '%': /* "%%" is converted to "%". */ - literal : - if (c != *bp++) - return (0); - break; - - /* - * "Alternative" modifiers. Just set the appropriate flag - * and start over again. - */ - case 'E': /* "%E?" alternative conversion modifier. */ - LEGAL_ALT(0); - alt_format |= ALT_E; - goto again; - - case 'O': /* "%O?" alternative conversion modifier. */ - LEGAL_ALT(0); - alt_format |= ALT_O; - goto again; - - /* - * "Complex" conversion rules, implemented through recursion. - */ - case 'c': /* Date and time, using the locale's format. */ - LEGAL_ALT(ALT_E); - if (!(bp = strptime(bp, "%x %X", tm))) - return (0); - break; - - case 'D': /* The date as "%m/%d/%y". */ - LEGAL_ALT(0); - if (!(bp = strptime(bp, "%m/%d/%y", tm))) - return (0); - break; - - case 'R': /* The time as "%H:%M". */ - LEGAL_ALT(0); - if (!(bp = strptime(bp, "%H:%M", tm))) - return (0); - break; - - case 'r': /* The time in 12-hour clock representation. */ - LEGAL_ALT(0); - if (!(bp = strptime(bp, "%I:%M:%S %p", tm))) - return (0); - break; - - case 'T': /* The time as "%H:%M:%S". */ - LEGAL_ALT(0); - if (!(bp = strptime(bp, "%H:%M:%S", tm))) - return (0); - break; - - case 'X': /* The time, using the locale's format. */ - LEGAL_ALT(ALT_E); - if (!(bp = strptime(bp, "%H:%M:%S", tm))) - return (0); - break; - - case 'x': /* The date, using the locale's format. */ - LEGAL_ALT(ALT_E); - if (!(bp = strptime(bp, "%m/%d/%y", tm))) - return (0); - break; - - /* - * "Elementary" conversion rules. - */ - case 'A': /* The day of week, using the locale's form. */ - case 'a': - LEGAL_ALT(0); - for (i = 0; i < 7; i++) { - /* Full name. */ - len = strlen(day[i]); - if (strncmp(day[i], bp, len) == 0) - break; - - /* Abbreviated name. */ - len = strlen(abday[i]); - if (strncmp(abday[i], bp, len) == 0) - break; - } - - /* Nothing matched. */ - if (i == 7) - return (0); - - tm->tm_wday = i; - bp += len; - break; - - case 'B': /* The month, using the locale's form. */ - case 'b': - case 'h': - LEGAL_ALT(0); - for (i = 0; i < 12; i++) { - /* Full name. */ - len = strlen(mon[i]); - if (strncmp(mon[i], bp, len) == 0) - break; - - /* Abbreviated name. */ - len = strlen(abmon[i]); - if (strncmp(abmon[i], bp, len) == 0) - break; - } - - /* Nothing matched. */ - if (i == 12) - return (0); - - tm->tm_mon = i; - bp += len; - break; - - case 'C': /* The century number. */ - LEGAL_ALT(ALT_E); - if (!(conv_num(&bp, &i, 0, 99))) - return (0); - - if (split_year) { - tm->tm_year = (tm->tm_year % 100) + (i * 100); - } - else { - tm->tm_year = i * 100; - split_year = 1; - } - break; - - case 'd': /* The day of month. */ - case 'e': - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &tm->tm_mday, 1, 31))) - return (0); - break; - - case 'k': /* The hour (24-hour clock representation). */ - LEGAL_ALT(0); - /* FALLTHROUGH */ - case 'H': - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &tm->tm_hour, 0, 23))) - return (0); - break; - - case 'l': /* The hour (12-hour clock representation). */ - LEGAL_ALT(0); - /* FALLTHROUGH */ - case 'I': - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &tm->tm_hour, 1, 12))) - return (0); - if (tm->tm_hour == 12) - tm->tm_hour = 0; - break; - - case 'j': /* The day of year. */ - LEGAL_ALT(0); - if (!(conv_num(&bp, &i, 1, 366))) - return (0); - tm->tm_yday = i - 1; - break; - - case 'M': /* The minute. */ - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &tm->tm_min, 0, 59))) - return (0); - break; - - case 'm': /* The month. */ - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &i, 1, 12))) - return (0); - tm->tm_mon = i - 1; - break; - - case 'p': /* The locale's equivalent of AM/PM. */ - LEGAL_ALT(0); - /* AM? */ - if (strcmp(am_pm[0], bp) == 0) { - if (tm->tm_hour > 11) - return (0); - - bp += strlen(am_pm[0]); - break; - } - /* PM? */ - else if (strcmp(am_pm[1], bp) == 0) { - if (tm->tm_hour > 11) - return (0); - - tm->tm_hour += 12; - bp += strlen(am_pm[1]); - break; - } - - /* Nothing matched. */ - return (0); - - case 'S': /* The seconds. */ - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &tm->tm_sec, 0, 61))) - return (0); - break; - - case 'U': /* The week of year, beginning on sunday. */ - case 'W': /* The week of year, beginning on monday. */ - LEGAL_ALT(ALT_O); - /* - * XXX This is bogus, as we can not assume any valid - * information present in the tm structure at this - * point to calculate a real value, so just check the - * range for now. - */ - if (!(conv_num(&bp, &i, 0, 53))) - return (0); - break; - - case 'w': /* The day of week, beginning on sunday. */ - LEGAL_ALT(ALT_O); - if (!(conv_num(&bp, &tm->tm_wday, 0, 6))) - return (0); - break; - - case 'Y': /* The year. */ - LEGAL_ALT(ALT_E); - if (!(conv_num(&bp, &i, 0, 9999))) - return (0); - - tm->tm_year = i - TM_YEAR_BASE; - break; - - case 'y': /* The year within 100 years of the epoch. */ - LEGAL_ALT(ALT_E | ALT_O); - if (!(conv_num(&bp, &i, 0, 99))) - return (0); - - if (split_year) { - tm->tm_year = ((tm->tm_year / 100) * 100) + i; - break; - } - split_year = 1; - if (i <= 68) - tm->tm_year = i + 2000 - TM_YEAR_BASE; - else - tm->tm_year = i + 1900 - TM_YEAR_BASE; - break; - - /* - * Miscellaneous conversions. - */ - case 'n': /* Any kind of white-space. */ - case 't': - LEGAL_ALT(0); - while (isspace(*bp)) - bp++; - break; +// default: /* Unknown/unsupported conversion. */ +// return (0); +// } - default: /* Unknown/unsupported conversion. */ - return (0); - } +// } + +// /* LINTED functional specification */ +// return ((char *)bp); +// #elif defined(_TD_DARWIN_64) +// return strptime(buf, fmt, tm); +// #else +// return strptime(buf, fmt, tm); +// #endif +// } - } - /* LINTED functional specification */ - return ((char *)bp); -} - - -static int -conv_num(const char **buf, int *dest, int llim, int ulim) -{ - int result = 0; - - /* The limit also determines the number of valid digits. */ - int rulim = ulim; - - if (**buf < '0' || **buf > '9') - return (0); - - do { - result *= 10; - result += *(*buf)++ - '0'; - rulim /= 10; - } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9'); - - if (result < llim || result > ulim) - return (0); - - *dest = result; - return (1); -} - -#endif \ No newline at end of file diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 7c655c0251..2b0de94880 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#define ALLOW_FORBID_FUNC #define _BSD_SOURCE #ifdef DARWIN @@ -26,16 +27,373 @@ #include "os.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) -/* - * windows implementation - */ #include +#include +#include #include +//#define TM_YEAR_BASE 1970 //origin +#define TM_YEAR_BASE 1900 //slguan +/* +* We do not implement alternate representations. However, we always +* check whether a given modifier is allowed for a certain conversion. +*/ +#define ALT_E 0x01 +#define ALT_O 0x02 +#define LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); } -int taosGetTimeOfDay(struct timeval *tv, struct timezone *tz) { + +static int conv_num(const char **buf, int *dest, int llim, int ulim) +{ + int result = 0; + + /* The limit also determines the number of valid digits. */ + int rulim = ulim; + + if (**buf < '0' || **buf > '9') + return (0); + + do { + result *= 10; + result += *(*buf)++ - '0'; + rulim /= 10; + } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9'); + + if (result < llim || result > ulim) + return (0); + + *dest = result; + return (1); +} + +static const char *day[7] = { + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", + "Friday", "Saturday" +}; +static const char *abday[7] = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" +}; +static const char *mon[12] = { + "January", "February", "March", "April", "May", "June", "July", + "August", "September", "October", "November", "December" +}; +static const char *abmon[12] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +}; +static const char *am_pm[2] = { + "AM", "PM" +}; + + +#else +#include +#endif + +char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + char c; + const char *bp; + size_t len = 0; + int alt_format, i, split_year = 0; + + bp = buf; + + while ((c = *fmt) != '\0') { + /* Clear `alternate' modifier prior to new conversion. */ + alt_format = 0; + + /* Eat up white-space. */ + if (isspace(c)) { + while (isspace(*bp)) + bp++; + + fmt++; + continue; + } + + if ((c = *fmt++) != '%') + goto literal; + + + again: switch (c = *fmt++) { + case '%': /* "%%" is converted to "%". */ + literal : + if (c != *bp++) + return (0); + break; + + /* + * "Alternative" modifiers. Just set the appropriate flag + * and start over again. + */ + case 'E': /* "%E?" alternative conversion modifier. */ + LEGAL_ALT(0); + alt_format |= ALT_E; + goto again; + + case 'O': /* "%O?" alternative conversion modifier. */ + LEGAL_ALT(0); + alt_format |= ALT_O; + goto again; + + /* + * "Complex" conversion rules, implemented through recursion. + */ + case 'c': /* Date and time, using the locale's format. */ + LEGAL_ALT(ALT_E); + if (!(bp = taosStrpTime(bp, "%x %X", tm))) + return (0); + break; + + case 'D': /* The date as "%m/%d/%y". */ + LEGAL_ALT(0); + if (!(bp = taosStrpTime(bp, "%m/%d/%y", tm))) + return (0); + break; + + case 'R': /* The time as "%H:%M". */ + LEGAL_ALT(0); + if (!(bp = taosStrpTime(bp, "%H:%M", tm))) + return (0); + break; + + case 'r': /* The time in 12-hour clock representation. */ + LEGAL_ALT(0); + if (!(bp = taosStrpTime(bp, "%I:%M:%S %p", tm))) + return (0); + break; + + case 'T': /* The time as "%H:%M:%S". */ + LEGAL_ALT(0); + if (!(bp = taosStrpTime(bp, "%H:%M:%S", tm))) + return (0); + break; + + case 'X': /* The time, using the locale's format. */ + LEGAL_ALT(ALT_E); + if (!(bp = taosStrpTime(bp, "%H:%M:%S", tm))) + return (0); + break; + + case 'x': /* The date, using the locale's format. */ + LEGAL_ALT(ALT_E); + if (!(bp = taosStrpTime(bp, "%m/%d/%y", tm))) + return (0); + break; + + /* + * "Elementary" conversion rules. + */ + case 'A': /* The day of week, using the locale's form. */ + case 'a': + LEGAL_ALT(0); + for (i = 0; i < 7; i++) { + /* Full name. */ + len = strlen(day[i]); + if (strncmp(day[i], bp, len) == 0) + break; + + /* Abbreviated name. */ + len = strlen(abday[i]); + if (strncmp(abday[i], bp, len) == 0) + break; + } + + /* Nothing matched. */ + if (i == 7) + return (0); + + tm->tm_wday = i; + bp += len; + break; + + case 'B': /* The month, using the locale's form. */ + case 'b': + case 'h': + LEGAL_ALT(0); + for (i = 0; i < 12; i++) { + /* Full name. */ + len = strlen(mon[i]); + if (strncmp(mon[i], bp, len) == 0) + break; + + /* Abbreviated name. */ + len = strlen(abmon[i]); + if (strncmp(abmon[i], bp, len) == 0) + break; + } + + /* Nothing matched. */ + if (i == 12) + return (0); + + tm->tm_mon = i; + bp += len; + break; + + case 'C': /* The century number. */ + LEGAL_ALT(ALT_E); + if (!(conv_num(&bp, &i, 0, 99))) + return (0); + + if (split_year) { + tm->tm_year = (tm->tm_year % 100) + (i * 100); + } + else { + tm->tm_year = i * 100; + split_year = 1; + } + break; + + case 'd': /* The day of month. */ + case 'e': + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_mday, 1, 31))) + return (0); + break; + + case 'k': /* The hour (24-hour clock representation). */ + LEGAL_ALT(0); + /* FALLTHROUGH */ + case 'H': + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_hour, 0, 23))) + return (0); + break; + + case 'l': /* The hour (12-hour clock representation). */ + LEGAL_ALT(0); + /* FALLTHROUGH */ + case 'I': + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_hour, 1, 12))) + return (0); + if (tm->tm_hour == 12) + tm->tm_hour = 0; + break; + + case 'j': /* The day of year. */ + LEGAL_ALT(0); + if (!(conv_num(&bp, &i, 1, 366))) + return (0); + tm->tm_yday = i - 1; + break; + + case 'M': /* The minute. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_min, 0, 59))) + return (0); + break; + + case 'm': /* The month. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &i, 1, 12))) + return (0); + tm->tm_mon = i - 1; + break; + + case 'p': /* The locale's equivalent of AM/PM. */ + LEGAL_ALT(0); + /* AM? */ + if (strcmp(am_pm[0], bp) == 0) { + if (tm->tm_hour > 11) + return (0); + + bp += strlen(am_pm[0]); + break; + } + /* PM? */ + else if (strcmp(am_pm[1], bp) == 0) { + if (tm->tm_hour > 11) + return (0); + + tm->tm_hour += 12; + bp += strlen(am_pm[1]); + break; + } + + /* Nothing matched. */ + return (0); + + case 'S': /* The seconds. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_sec, 0, 61))) + return (0); + break; + + case 'U': /* The week of year, beginning on sunday. */ + case 'W': /* The week of year, beginning on monday. */ + LEGAL_ALT(ALT_O); + /* + * XXX This is bogus, as we can not assume any valid + * information present in the tm structure at this + * point to calculate a real value, so just check the + * range for now. + */ + if (!(conv_num(&bp, &i, 0, 53))) + return (0); + break; + + case 'w': /* The day of week, beginning on sunday. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_wday, 0, 6))) + return (0); + break; + + case 'Y': /* The year. */ + LEGAL_ALT(ALT_E); + if (!(conv_num(&bp, &i, 0, 9999))) + return (0); + + tm->tm_year = i - TM_YEAR_BASE; + break; + + case 'y': /* The year within 100 years of the epoch. */ + LEGAL_ALT(ALT_E | ALT_O); + if (!(conv_num(&bp, &i, 0, 99))) + return (0); + + if (split_year) { + tm->tm_year = ((tm->tm_year / 100) * 100) + i; + break; + } + split_year = 1; + if (i <= 68) + tm->tm_year = i + 2000 - TM_YEAR_BASE; + else + tm->tm_year = i + 1900 - TM_YEAR_BASE; + break; + + /* + * Miscellaneous conversions. + */ + case 'n': /* Any kind of white-space. */ + case 't': + LEGAL_ALT(0); + while (isspace(*bp)) + bp++; + break; + + + default: /* Unknown/unsupported conversion. */ + return (0); + } + + + } + + /* LINTED functional specification */ + return ((char *)bp); +#else + return strptime(buf, fmt, tm); +#endif +} + +FORCE_INLINE int32_t taosGetTimeOfDay(struct timeval *tv) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) time_t t; - t = time(NULL); + t = taosGetTimestampSec(); SYSTEMTIME st; GetLocalTime(&st); @@ -43,26 +401,18 @@ int taosGetTimeOfDay(struct timeval *tv, struct timezone *tz) { tv->tv_usec = st.wMilliseconds * 1000; return 0; +#else + return gettimeofday(tv, NULL); +#endif } -struct tm *localtime_r(const time_t *timep, struct tm *result) { +struct tm *taosLocalTime(const time_t *timep, struct tm *result) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) localtime_s(result, timep); +#else + localtime_r(timep, result); +#endif return result; } -#else - -/* - * linux and darwin implementation - */ - -#include -// #include "monotonic.h" - -FORCE_INLINE int32_t taosGetTimeOfDay(struct timeval *tv) { - return gettimeofday(tv, NULL); -} - int32_t taosGetTimestampSec() { return (int32_t)time(NULL); } - -#endif diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index 68da2ce25e..864a60b706 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -128,9 +128,9 @@ void taosGetSystemTimezone(char *outTimezone) { * Enforce set the correct daylight saving time(DST) flag according * to current time */ - time_t tx1 = time(NULL); + time_t tx1 = taosGetTimestampSec(); struct tm tm1; - localtime_r(&tx1, &tm1); + taosLocalTime(&tx1, &tm1); /* * format example: @@ -147,9 +147,9 @@ void taosGetSystemTimezone(char *outTimezone) { * Enforce set the correct daylight saving time(DST) flag according * to current time */ - time_t tx1 = time(NULL); + time_t tx1 = taosGetTimestampSec(); struct tm tm1; - localtime_r(&tx1, &tm1); + taosLocalTime(&tx1, &tm1); /* load time zone string from /etc/timezone */ // FILE *f = fopen("/etc/timezone", "r"); diff --git a/source/util/src/tdes.c b/source/util/src/tdes.c index d12b47efe8..a7f5131c26 100644 --- a/source/util/src/tdes.c +++ b/source/util/src/tdes.c @@ -31,7 +31,7 @@ void process_message(uint8_t* message_piece, uint8_t* processed_piece, key_set* #if 0 int64_t taosDesGenKey() { - uint32_t iseed = (uint32_t)time(NULL); + uint32_t iseed = (uint32_t)taosGetTimestampSec(); taosSeedRand(iseed); uint8_t key[8] = {0}; diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 7f5664b9cd..6dedc3f740 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -401,7 +401,7 @@ static inline int32_t taosBuildLogHead(char *buffer, const char *flags) { taosGetTimeOfDay(&timeSecs); time_t curTime = timeSecs.tv_sec; - ptm = localtime_r(&curTime, &Tm); + ptm = taosLocalTime(&curTime, &Tm); return sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " %s", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId(), flags); diff --git a/source/util/src/tskiplist.c b/source/util/src/tskiplist.c index d9d6e4e3da..b5e54d12d3 100644 --- a/source/util/src/tskiplist.c +++ b/source/util/src/tskiplist.c @@ -82,7 +82,7 @@ SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, _ } } - taosSeedRand((uint32_t)time(NULL)); + taosSeedRand((uint32_t)taosGetTimestampSec()); #if SKIP_LIST_RECORD_PERFORMANCE pSkipList->state.nTotalMemSize += sizeof(SSkipList); diff --git a/source/util/test/codingTests.cpp b/source/util/test/codingTests.cpp index b991411047..f88520b44a 100644 --- a/source/util/test/codingTests.cpp +++ b/source/util/test/codingTests.cpp @@ -150,7 +150,7 @@ static bool test_variant_int64(int64_t value) { } TEST(codingTest, fixed_encode_decode) { - taosSeedRand(time(0)); + taosSeedRand(taosGetTimestampSec()); // uint16_t for (uint16_t value = 0; value <= UINT16_MAX; value++) { @@ -204,7 +204,7 @@ TEST(codingTest, fixed_encode_decode) { } TEST(codingTest, variant_encode_decode) { - taosSeedRand(time(0)); + taosSeedRand(taosGetTimestampSec()); // uint16_t for (uint16_t value = 0; value <= UINT16_MAX; value++) { diff --git a/source/util/test/pageBufferTest.cpp b/source/util/test/pageBufferTest.cpp index e63e6f04a1..5ad3cb42aa 100644 --- a/source/util/test/pageBufferTest.cpp +++ b/source/util/test/pageBufferTest.cpp @@ -161,7 +161,7 @@ void recyclePageTest() { TEST(testCase, resultBufferTest) { - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); simpleTest(); writeDownTest(); recyclePageTest(); diff --git a/source/util/test/skiplistTest.cpp b/source/util/test/skiplistTest.cpp index f61ebfd890..0b629f64aa 100644 --- a/source/util/test/skiplistTest.cpp +++ b/source/util/test/skiplistTest.cpp @@ -74,7 +74,7 @@ void randKeyTest() { false, getkey); int32_t size = 200000; - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); printf("generated %d keys is: \n", size); @@ -337,7 +337,7 @@ void duplicatedKeyTest() { TEST(testCase, skiplist_test) { assert(sizeof(SSkipListKey) == 8); - taosSeedRand(time(NULL)); + taosSeedRand(taosGetTimestampSec()); stringKeySkiplistTest(); doubleSkipListTest(); diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 5336a557e3..2db4761b7a 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -18,7 +18,7 @@ void taosMsleep(int mseconds); unsigned long long getCurrentTime(){ struct timeval tv; - if (gettimeofday(&tv, NULL) != 0) { + if (taosGetTimeOfDay(&tv) != 0) { perror("Failed to get current time in ms"); exit(EXIT_FAILURE); } diff --git a/tests/script/api/stmtBatchTest.c b/tests/script/api/stmtBatchTest.c index eb7845714e..57cbdf1090 100644 --- a/tests/script/api/stmtBatchTest.c +++ b/tests/script/api/stmtBatchTest.c @@ -42,7 +42,7 @@ int g_runTimes = 5; unsigned long long getCurrentTime(){ struct timeval tv; - if (gettimeofday(&tv, NULL) != 0) { + if (taosGetTimeOfDay(&tv) != 0) { perror("Failed to get current time in ms"); exit(EXIT_FAILURE); } diff --git a/tests/test/c/tmqDemo.c b/tests/test/c/tmqDemo.c index cb7d7c67ce..609f8d6b69 100644 --- a/tests/test/c/tmqDemo.c +++ b/tests/test/c/tmqDemo.c @@ -609,7 +609,7 @@ void printParaIntoFile() { }; g_fp = pFile; - time_t tTime = time(NULL); + time_t tTime = taosGetTimestampSec(); struct tm tm = *localtime(&tTime); taosFprintfFile(pFile, "###################################################################\n"); From 78b5da0e0f74a54f5c913aa86669e7853f1513d3 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 17:08:27 +0800 Subject: [PATCH 17/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 26 ++- source/libs/sync/src/syncMessage.c | 152 ++++++++++++++++-- source/libs/sync/test/CMakeLists.txt | 28 ++++ .../sync/test/syncAppendEntriesReplyTest.cpp | 23 +-- .../libs/sync/test/syncClientRequestTest.cpp | 96 +++++++++++ source/libs/sync/test/syncTimeoutTest.cpp | 92 +++++++++++ 6 files changed, 390 insertions(+), 27 deletions(-) create mode 100644 source/libs/sync/test/syncClientRequestTest.cpp create mode 100644 source/libs/sync/test/syncTimeoutTest.cpp diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 902a6cbe1d..a3b7517bdc 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -60,17 +60,27 @@ typedef struct SyncTimeout { ESyncTimeoutType timeoutType; uint64_t logicClock; int32_t timerMS; - void* data; + void* data; // need optimized } SyncTimeout; SyncTimeout* syncTimeoutBuild(); +SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, void* data); void syncTimeoutDestroy(SyncTimeout* pMsg); void syncTimeoutSerialize(const SyncTimeout* pMsg, char* buf, uint32_t bufLen); void syncTimeoutDeserialize(const char* buf, uint32_t len, SyncTimeout* pMsg); +char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len); // +SyncTimeout* syncTimeoutDeserialize2(const char* buf, uint32_t len); // void syncTimeout2RpcMsg(const SyncTimeout* pMsg, SRpcMsg* pRpcMsg); void syncTimeoutFromRpcMsg(const SRpcMsg* pRpcMsg, SyncTimeout* pMsg); +SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg); // cJSON* syncTimeout2Json(const SyncTimeout* pMsg); -SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, void* data); +char* syncTimeout2Str(const SyncTimeout* pMsg); // + +// for debug ---------------------- +void syncTimeoutPrint(const SyncTimeout* pMsg); +void syncTimeoutPrint2(char* s, const SyncTimeout* pMsg); +void syncTimeoutLog(const SyncTimeout* pMsg); +void syncTimeoutLog2(char* s, const SyncTimeout* pMsg); // --------------------------------------------- typedef struct SyncPing { @@ -129,13 +139,23 @@ typedef struct SyncClientRequest { } SyncClientRequest; SyncClientRequest* syncClientRequestBuild(uint32_t dataLen); +SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak); void syncClientRequestDestroy(SyncClientRequest* pMsg); void syncClientRequestSerialize(const SyncClientRequest* pMsg, char* buf, uint32_t bufLen); void syncClientRequestDeserialize(const char* buf, uint32_t len, SyncClientRequest* pMsg); +char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len); +SyncClientRequest* syncClientRequestDeserialize2(const char* buf, uint32_t len); void syncClientRequest2RpcMsg(const SyncClientRequest* pMsg, SRpcMsg* pRpcMsg); void syncClientRequestFromRpcMsg(const SRpcMsg* pRpcMsg, SyncClientRequest* pMsg); +SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg); cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg); -SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak); +char* syncClientRequest2Str(const SyncClientRequest* pMsg); + +// for debug ---------------------- +void syncClientRequestPrint(const SyncClientRequest* pMsg); +void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg); +void syncClientRequestLog(const SyncClientRequest* pMsg); +void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg); // --------------------------------------------- typedef struct SyncClientRequestReply { diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 49b3c020ab..3d5d302559 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -93,6 +93,15 @@ SyncTimeout* syncTimeoutBuild() { return pMsg; } +SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, void* data) { + SyncTimeout* pMsg = syncTimeoutBuild(); + pMsg->timeoutType = timeoutType; + pMsg->logicClock = logicClock; + pMsg->timerMS = timerMS; + pMsg->data = data; + return pMsg; +} + void syncTimeoutDestroy(SyncTimeout* pMsg) { if (pMsg != NULL) { free(pMsg); @@ -109,6 +118,25 @@ void syncTimeoutDeserialize(const char* buf, uint32_t len, SyncTimeout* pMsg) { assert(len == pMsg->bytes); } +char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncTimeoutSerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncTimeout* syncTimeoutDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncTimeout* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncTimeoutDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncTimeout2RpcMsg(const SyncTimeout* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -121,6 +149,11 @@ void syncTimeoutFromRpcMsg(const SRpcMsg* pRpcMsg, SyncTimeout* pMsg) { syncTimeoutDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncTimeout* pMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncTimeout2Json(const SyncTimeout* pMsg) { char u64buf[128]; @@ -139,13 +172,38 @@ cJSON* syncTimeout2Json(const SyncTimeout* pMsg) { return pJson; } -SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, void* data) { - SyncTimeout* pMsg = syncTimeoutBuild(); - pMsg->timeoutType = timeoutType; - pMsg->logicClock = logicClock; - pMsg->timerMS = timerMS; - pMsg->data = data; - return pMsg; +char* syncTimeout2Str(const SyncTimeout* pMsg) { + cJSON* pJson = syncTimeout2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncTimeoutPrint(const SyncTimeout* pMsg) { + char* serialized = syncTimeout2Str(pMsg); + printf("syncTimeoutPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncTimeoutPrint2(char* s, const SyncTimeout* pMsg) { + char* serialized = syncTimeout2Str(pMsg); + printf("syncTimeoutPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncTimeoutLog(const SyncTimeout* pMsg) { + char* serialized = syncTimeout2Str(pMsg); + sTrace("syncTimeoutLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncTimeoutLog2(char* s, const SyncTimeout* pMsg) { + char* serialized = syncTimeout2Str(pMsg); + sTrace("syncTimeoutLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } // ---- message process SyncPing---- @@ -359,6 +417,15 @@ SyncClientRequest* syncClientRequestBuild(uint32_t dataLen) { return pMsg; } +SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak) { + SyncClientRequest* pMsg = syncClientRequestBuild(pOriginalRpcMsg->contLen); + pMsg->originalRpcType = pOriginalRpcMsg->msgType; + pMsg->seqNum = seqNum; + pMsg->isWeak = isWeak; + memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen); + return pMsg; +} + void syncClientRequestDestroy(SyncClientRequest* pMsg) { if (pMsg != NULL) { free(pMsg); @@ -375,6 +442,25 @@ void syncClientRequestDeserialize(const char* buf, uint32_t len, SyncClientReque assert(len == pMsg->bytes); } +char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncClientRequestSerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncClientRequest* syncClientRequestDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncClientRequest* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncClientRequestDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncClientRequest2RpcMsg(const SyncClientRequest* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -387,6 +473,11 @@ void syncClientRequestFromRpcMsg(const SRpcMsg* pRpcMsg, SyncClientRequest* pMsg syncClientRequestDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncClientRequest* pMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg) { char u64buf[128]; @@ -399,18 +490,51 @@ cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg) { cJSON_AddNumberToObject(pRoot, "isWeak", pMsg->isWeak); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); + char* s; + s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", s); + free(s); + s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data2", s); + free(s); + cJSON* pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "SyncClientRequest", pRoot); return pJson; } -SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak) { - SyncClientRequest* pMsg = syncClientRequestBuild(pOriginalRpcMsg->contLen); - pMsg->originalRpcType = pOriginalRpcMsg->msgType; - pMsg->seqNum = seqNum; - pMsg->isWeak = isWeak; - memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen); - return pMsg; +char* syncClientRequest2Str(const SyncClientRequest* pMsg) { + cJSON* pJson = syncClientRequest2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncClientRequestPrint(const SyncClientRequest* pMsg) { + char* serialized = syncClientRequest2Str(pMsg); + printf("syncClientRequestPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg) { + char* serialized = syncClientRequest2Str(pMsg); + printf("syncClientRequestPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncClientRequestLog(const SyncClientRequest* pMsg) { + char* serialized = syncClientRequest2Str(pMsg); + sTrace("syncClientRequestLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg) { + char* serialized = syncClientRequest2Str(pMsg); + sTrace("syncClientRequestLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } // ---- message process SyncRequestVote---- diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 8d25c357a3..2ed4799467 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -21,6 +21,8 @@ add_executable(syncRequestVoteTest "") add_executable(syncRequestVoteReplyTest "") add_executable(syncAppendEntriesTest "") add_executable(syncAppendEntriesReplyTest "") +add_executable(syncClientRequestTest "") +add_executable(syncTimeoutTest "") target_sources(syncTest @@ -115,6 +117,14 @@ target_sources(syncAppendEntriesReplyTest PRIVATE "syncAppendEntriesReplyTest.cpp" ) +target_sources(syncClientRequestTest + PRIVATE + "syncClientRequestTest.cpp" +) +target_sources(syncTimeoutTest + PRIVATE + "syncTimeoutTest.cpp" +) target_include_directories(syncTest @@ -232,6 +242,16 @@ target_include_directories(syncAppendEntriesReplyTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncClientRequestTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncTimeoutTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -326,6 +346,14 @@ target_link_libraries(syncAppendEntriesReplyTest sync gtest_main ) +target_link_libraries(syncClientRequestTest + sync + gtest_main +) +target_link_libraries(syncTimeoutTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp index c802350ae5..c883ab941a 100644 --- a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp +++ b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp @@ -32,12 +32,13 @@ void test1() { void test2() { SyncAppendEntriesReply *pMsg = createMsg(); - uint32_t len = pMsg->bytes; - char * serialized = (char *)malloc(len); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); syncAppendEntriesReplySerialize(pMsg, serialized, len); SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(); syncAppendEntriesReplyDeserialize(serialized, len, pMsg2); - syncAppendEntriesReplyPrint2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ", pMsg2); + syncAppendEntriesReplyPrint2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ", + pMsg2); free(serialized); syncAppendEntriesReplyDestroy(pMsg); @@ -46,11 +47,11 @@ void test2() { void test3() { SyncAppendEntriesReply *pMsg = createMsg(); - uint32_t len; - char * serialized = syncAppendEntriesReplySerialize2(pMsg, &len); + uint32_t len; + char * serialized = syncAppendEntriesReplySerialize2(pMsg, &len); SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyDeserialize2(serialized, len); syncAppendEntriesReplyPrint2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ", - pMsg2); + pMsg2); free(serialized); syncAppendEntriesReplyDestroy(pMsg); @@ -59,11 +60,12 @@ void test3() { void test4() { SyncAppendEntriesReply *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(); syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2); - syncAppendEntriesReplyPrint2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ", pMsg2); + syncAppendEntriesReplyPrint2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ", + pMsg2); syncAppendEntriesReplyDestroy(pMsg); syncAppendEntriesReplyDestroy(pMsg2); @@ -71,10 +73,11 @@ void test4() { void test5() { SyncAppendEntriesReply *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyFromRpcMsg2(&rpcMsg); - syncAppendEntriesReplyPrint2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ", pMsg2); + syncAppendEntriesReplyPrint2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ", + pMsg2); syncAppendEntriesReplyDestroy(pMsg); syncAppendEntriesReplyDestroy(pMsg2); diff --git a/source/libs/sync/test/syncClientRequestTest.cpp b/source/libs/sync/test/syncClientRequestTest.cpp new file mode 100644 index 0000000000..311a7e5fa3 --- /dev/null +++ b/source/libs/sync/test/syncClientRequestTest.cpp @@ -0,0 +1,96 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +SyncClientRequest *createMsg() { + SRpcMsg rpcMsg; + memset(&rpcMsg, 0, sizeof(rpcMsg)); + rpcMsg.msgType = 12345; + rpcMsg.contLen = 20; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + strcpy((char*)rpcMsg.pCont, "hello rpc"); + SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true); + return pMsg; +} + +void test1() { + SyncClientRequest *pMsg = createMsg(); + syncClientRequestPrint2((char *)"test1:", pMsg); + syncClientRequestDestroy(pMsg); +} + +void test2() { + SyncClientRequest *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncClientRequestSerialize(pMsg, serialized, len); + SyncClientRequest *pMsg2 = syncClientRequestBuild(pMsg2->dataLen); + syncClientRequestDeserialize(serialized, len, pMsg2); + syncClientRequestPrint2((char *)"test2: syncClientRequestSerialize -> syncClientRequestDeserialize ", pMsg2); + + free(serialized); + syncClientRequestDestroy(pMsg); + syncClientRequestDestroy(pMsg2); +} + +void test3() { + SyncClientRequest *pMsg = createMsg(); + uint32_t len; + char * serialized = syncClientRequestSerialize2(pMsg, &len); + SyncClientRequest *pMsg2 = syncClientRequestDeserialize2(serialized, len); + syncClientRequestPrint2((char *)"test3: syncClientRequestSerialize3 -> syncClientRequestDeserialize2 ", pMsg2); + + free(serialized); + syncClientRequestDestroy(pMsg); + syncClientRequestDestroy(pMsg2); +} + +void test4() { + SyncClientRequest *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncClientRequest2RpcMsg(pMsg, &rpcMsg); + SyncClientRequest *pMsg2 = (SyncClientRequest *)malloc(rpcMsg.contLen); + syncClientRequestFromRpcMsg(&rpcMsg, pMsg2); + syncClientRequestPrint2((char *)"test4: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg ", pMsg2); + + syncClientRequestDestroy(pMsg); + syncClientRequestDestroy(pMsg2); +} + +void test5() { + SyncClientRequest *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncClientRequest2RpcMsg(pMsg, &rpcMsg); + SyncClientRequest *pMsg2 = syncClientRequestFromRpcMsg2(&rpcMsg); + syncClientRequestPrint2((char *)"test5: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg2 ", pMsg2); + + syncClientRequestDestroy(pMsg); + syncClientRequestDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} diff --git a/source/libs/sync/test/syncTimeoutTest.cpp b/source/libs/sync/test/syncTimeoutTest.cpp new file mode 100644 index 0000000000..4d2da97a00 --- /dev/null +++ b/source/libs/sync/test/syncTimeoutTest.cpp @@ -0,0 +1,92 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int gg = 0; + +SyncTimeout *createMsg() { + SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, &gg); + return pMsg; +} + +void test1() { + SyncTimeout *pMsg = createMsg(); + syncTimeoutPrint2((char *)"test1:", pMsg); + syncTimeoutDestroy(pMsg); +} + +void test2() { + SyncTimeout *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncTimeoutSerialize(pMsg, serialized, len); + SyncTimeout *pMsg2 = syncTimeoutBuild(); + syncTimeoutDeserialize(serialized, len, pMsg2); + syncTimeoutPrint2((char *)"test2: syncTimeoutSerialize -> syncTimeoutDeserialize ", pMsg2); + + free(serialized); + syncTimeoutDestroy(pMsg); + syncTimeoutDestroy(pMsg2); +} + +void test3() { + SyncTimeout *pMsg = createMsg(); + uint32_t len; + char * serialized = syncTimeoutSerialize2(pMsg, &len); + SyncTimeout *pMsg2 = syncTimeoutDeserialize2(serialized, len); + syncTimeoutPrint2((char *)"test3: syncTimeoutSerialize3 -> syncTimeoutDeserialize2 ", pMsg2); + + free(serialized); + syncTimeoutDestroy(pMsg); + syncTimeoutDestroy(pMsg2); +} + +void test4() { + SyncTimeout *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncTimeout2RpcMsg(pMsg, &rpcMsg); + SyncTimeout *pMsg2 = (SyncTimeout *)malloc(rpcMsg.contLen); + syncTimeoutFromRpcMsg(&rpcMsg, pMsg2); + syncTimeoutPrint2((char *)"test4: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg ", pMsg2); + + syncTimeoutDestroy(pMsg); + syncTimeoutDestroy(pMsg2); +} + +void test5() { + SyncTimeout *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncTimeout2RpcMsg(pMsg, &rpcMsg); + SyncTimeout *pMsg2 = syncTimeoutFromRpcMsg2(&rpcMsg); + syncTimeoutPrint2((char *)"test5: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg2 ", pMsg2); + + syncTimeoutDestroy(pMsg); + syncTimeoutDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} From aa2e9ada55a402b9298c528672b4d919a586f764 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Fri, 11 Mar 2022 18:03:59 +0800 Subject: [PATCH 18/29] [TD-13771]: redefine timer api. --- include/os/osTimer.h | 9 ++++ source/os/src/osTimer.c | 111 +++++++++++++++++----------------------- 2 files changed, 57 insertions(+), 63 deletions(-) diff --git a/include/os/osTimer.h b/include/os/osTimer.h index 4b5db895a2..9040113b23 100644 --- a/include/os/osTimer.h +++ b/include/os/osTimer.h @@ -20,6 +20,15 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define timer_create TIMER_CREATE_FUNC_TAOS_FORBID + #define timer_settime TIMER_SETTIME_FUNC_TAOS_FORBID + #define timer_delete TIMER_DELETE_FUNC_TAOS_FORBID + #define timeSetEvent TIMESETEVENT_SETTIME_FUNC_TAOS_FORBID + #define timeKillEvent TIMEKILLEVENT_SETTIME_FUNC_TAOS_FORBID +#endif + #define MSECONDS_PER_TICK 5 int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); diff --git a/source/os/src/osTimer.c b/source/os/src/osTimer.c index 6b60923189..c95ca72bd5 100644 --- a/source/os/src/osTimer.c +++ b/source/os/src/osTimer.c @@ -13,15 +13,11 @@ * along with this program. If not, see . */ +#define ALLOW_FORBID_FUNC #define _DEFAULT_SOURCE #include "os.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - -/* - * windows implementation - */ - #include #include #include @@ -39,24 +35,9 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR } static MMRESULT timerId; -int taosInitTimer(win_timer_f callback, int ms) { - DWORD_PTR param = *((int64_t *)&callback); - - timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); - if (timerId == 0) { - return -1; - } - return 0; -} - -void taosUninitTimer() { timeKillEvent(timerId); } #elif defined(_TD_DARWIN_64) -/* - * darwin implementation - */ - #include #include #include @@ -88,53 +69,12 @@ static void* timer_routine(void* arg) { return NULL; } -int taosInitTimer(void (*callback)(int), int ms) { - int r = 0; - timer_kq = -1; - timer_stop = 0; - timer_ms = ms; - timer_callback = callback; - - timer_kq = kqueue(); - if (timer_kq == -1) { - fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - - r = pthread_create(&timer_thread, NULL, timer_routine, NULL); - if (r) { - fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - return 0; -} - -void taosUninitTimer() { - int r = 0; - timer_stop = 1; - r = pthread_join(timer_thread, NULL); - if (r) { - fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - close(timer_kq); - timer_kq = -1; -} - void taos_block_sigalrm(void) { // we don't know if there's any specific API for SIGALRM to deliver to specific thread // this implementation relies on kqueue rather than SIGALRM } #else - -/* - * linux implementation - */ - #include #include @@ -200,8 +140,39 @@ static void * taosProcessAlarmSignal(void *tharg) { return NULL; } +#endif int taosInitTimer(void (*callback)(int), int ms) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + DWORD_PTR param = *((int64_t *)&callback); + + timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); + if (timerId == 0) { + return -1; + } + return 0; +#elif defined(_TD_DARWIN_64) + int r = 0; + timer_kq = -1; + timer_stop = 0; + timer_ms = ms; + timer_callback = callback; + + timer_kq = kqueue(); + if (timer_kq == -1) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + + r = pthread_create(&timer_thread, NULL, timer_routine, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + return 0; +#else stopTimer = false; pthread_attr_t tattr; pthread_attr_init(&tattr); @@ -215,13 +186,29 @@ int taosInitTimer(void (*callback)(int), int ms) { } return 0; +#endif } void taosUninitTimer() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + timeKillEvent(timerId); +#elif defined(_TD_DARWIN_64) + int r = 0; + timer_stop = 1; + r = pthread_join(timer_thread, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + close(timer_kq); + timer_kq = -1; +#else stopTimer = true; // printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); pthread_join(timerThread, NULL); +#endif } int64_t taosGetMonotonicMs() { @@ -239,5 +226,3 @@ const char *taosMonotonicInit() { return NULL; #endif } - -#endif From 9913d0c72ad20e61a0686ba4011a1c8fbe05fc4e Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 11 Mar 2022 18:27:52 +0800 Subject: [PATCH 19/29] fix except --- source/libs/transport/src/transCli.c | 107 +++++++++++++------------- source/libs/transport/src/transComm.c | 4 + source/libs/transport/src/transSrv.c | 43 ++++++----- 3 files changed, 78 insertions(+), 76 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 13a5d57dfe..1323677071 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -26,7 +26,7 @@ typedef struct SCliConn { T_REF_DECLARE() uv_connect_t connReq; uv_stream_t* stream; - uv_write_t* writeReq; + uv_write_t writeReq; void* hostThrd; SConnBuffer readBuf; void* data; @@ -34,12 +34,12 @@ typedef struct SCliConn { uint64_t expireTime; int8_t ctnRdCnt; // continue read count int hThrdIdx; + bool broken; // link broken or not int persist; // // spi configure - char spi; - char secured; - int32_t ref; + char spi; + char secured; // debug and log info struct sockaddr_in addr; struct sockaddr_in locaddr; @@ -54,11 +54,10 @@ typedef struct SCliMsg { } SCliMsg; typedef struct SCliThrdObj { - pthread_t thread; - uv_loop_t* loop; - // uv_async_t* cliAsync; // + pthread_t thread; + uv_loop_t* loop; SAsyncPool* asyncPool; - uv_timer_t* timer; + uv_timer_t timer; void* pool; // conn pool // msg queue @@ -83,7 +82,7 @@ typedef struct SConnList { // conn pool // add expire timeout and capacity limit -static void* creatConnPool(int size); +static void* createConnPool(int size); static void* destroyConnPool(void* pool); static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port); static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn); @@ -99,8 +98,10 @@ 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, bool clear /*clear tcp handle or not*/); + +static SCliConn* clientConnCreate(SCliThrdObj* thrd); +static void clientConnDestroy(SCliConn* pConn, bool clear /*clear tcp handle or not*/); +static void clientDestroy(uv_handle_t* handle); // process data read from server, add decompress etc later static void clientHandleResp(SCliConn* conn); @@ -176,14 +177,14 @@ static void clientHandleResp(SCliConn* conn) { conn->data = NULL; // start thread's timer of conn pool if not active - if (!uv_is_active((uv_handle_t*)pThrd->timer) && pTransInst->idleTime > 0) { - // uv_timer_start((uv_timer_t*)pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); + if (!uv_is_active((uv_handle_t*)&pThrd->timer) && pTransInst->idleTime > 0) { + // uv_timer_start((uv_timer_t*)&pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); } } static void clientHandleExcept(SCliConn* pConn) { if (pConn->data == NULL) { // handle conn except in conn pool - clientConnDestroy(pConn, true); + transUnrefCliHandle(pConn); return; } SCliThrdObj* pThrd = pConn->hostThrd; @@ -209,7 +210,7 @@ static void clientHandleExcept(SCliConn* pConn) { pConn->data = NULL; tTrace("%s client conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn); - clientConnDestroy(pConn, true); + transUnrefCliHandle(pConn); } static void clientTimeoutCb(uv_timer_t* handle) { @@ -225,9 +226,7 @@ static void clientTimeoutCb(uv_timer_t* handle) { SCliConn* c = QUEUE_DATA(h, SCliConn, conn); if (c->expireTime < currentTime) { QUEUE_REMOVE(h); - // uv_stream_t stm = *(c->stream); - // uv_close((uv_handle_t*)&stm, clientDestroy); - clientConnDestroy(c, true); + transUnrefCliHandle(c); } else { break; } @@ -238,7 +237,7 @@ static void clientTimeoutCb(uv_timer_t* handle) { pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime); uv_timer_start(handle, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); } -static void* creatConnPool(int size) { +static void* createConnPool(int size) { // thread local, no lock return taosHashInit(size, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); } @@ -253,7 +252,7 @@ static void* destroyConnPool(void* pool) { } connList = taosHashIterate((SHashObj*)pool, connList); } - taosHashClear(pool); + taosHashCleanup(pool); } static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) { @@ -328,26 +327,38 @@ static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf } if (nread < 0) { tError("%s client conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread)); + conn->broken = true; clientHandleExcept(conn); } } +static SCliConn* clientConnCreate(SCliThrdObj* pThrd) { + SCliConn* conn = calloc(1, sizeof(SCliConn)); + // read/write stream handle + conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t)); + uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream)); + conn->stream->data = conn; + + conn->writeReq.data = conn; + conn->connReq.data = conn; + + QUEUE_INIT(&conn->conn); + conn->hostThrd = pThrd; + conn->broken = false; + transRefCliHandle(conn); + return conn; +} static void clientConnDestroy(SCliConn* conn, bool clear) { - // - conn->ref--; - if (conn->ref == 0) { - tTrace("%s client conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn); - QUEUE_REMOVE(&conn->conn); - if (clear) { - uv_close((uv_handle_t*)conn->stream, clientDestroy); - } + tTrace("%s client conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn); + QUEUE_REMOVE(&conn->conn); + if (clear) { + uv_close((uv_handle_t*)conn->stream, clientDestroy); } } static void clientDestroy(uv_handle_t* handle) { SCliConn* conn = handle->data; free(conn->stream); - free(conn->writeReq); tTrace("%s client conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn); free(conn); } @@ -359,7 +370,6 @@ static void clientWriteCb(uv_write_t* req, int status) { tTrace("%s client conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn); SCliMsg* pMsg = pConn->data; if (pMsg == NULL) { - // handle return; } destroyUserdata(&pMsg->msg); @@ -410,7 +420,7 @@ static void clientWrite(SCliConn* pConn) { TMSG_INFO(pHead->msgType), inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port)); - uv_write(pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb); + uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb); } static void clientConnCb(uv_connect_t* req, int status) { // impl later @@ -436,10 +446,10 @@ static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { tDebug("client work thread %p start to quit", pThrd); destroyCmsg(pMsg); destroyConnPool(pThrd->pool); - // transDestroyAsyncPool(pThr) uv_close((uv_handle_t*)pThrd->cliAsync, NULL); - uv_timer_stop(pThrd->timer); + + uv_timer_stop(&pThrd->timer); + pThrd->quit = true; - // uv__async_stop(pThrd->cliAsync); uv_stop(pThrd->loop); } static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { @@ -463,7 +473,7 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { if (conn != NULL) { conn->data = pMsg; - conn->writeReq->data = conn; + conn->writeReq.data = conn; transDestroyBuffer(&conn->readBuf); if (pThrd->quit) { @@ -472,21 +482,8 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { } clientWrite(conn); } else { - conn = calloc(1, sizeof(SCliConn)); - conn->ref++; - // read/write stream handle - conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t)); - uv_tcp_init(pThrd->loop, (uv_tcp_t*)(conn->stream)); - conn->stream->data = conn; - - conn->writeReq = malloc(sizeof(uv_write_t)); - conn->writeReq->data = conn; - - QUEUE_INIT(&conn->conn); - - conn->connReq.data = conn; + conn = clientConnCreate(pThrd); conn->data = pMsg; - conn->hostThrd = pThrd; int ret = transSetConnOption((uv_tcp_t*)conn->stream); if (ret) { @@ -585,11 +582,10 @@ static SCliThrdObj* createThrdObj() { pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, clientAsyncCb); - pThrd->timer = malloc(sizeof(uv_timer_t)); - uv_timer_init(pThrd->loop, pThrd->timer); - pThrd->timer->data = pThrd; + uv_timer_init(pThrd->loop, &pThrd->timer); + pThrd->timer.data = pThrd; - pThrd->pool = creatConnPool(4); + pThrd->pool = createConnPool(4); pThrd->quit = false; return pThrd; @@ -602,8 +598,8 @@ static void destroyThrdObj(SCliThrdObj* pThrd) { pthread_join(pThrd->thread, NULL); pthread_mutex_destroy(&pThrd->msgMtx); transDestroyAsyncPool(pThrd->asyncPool); - // free(pThrd->cliAsync); - free(pThrd->timer); + + uv_timer_stop(&pThrd->timer); free(pThrd->loop); free(pThrd); } @@ -649,6 +645,7 @@ void transUnrefCliHandle(void* handle) { } int ref = T_REF_DEC((SCliConn*)handle); if (ref == 0) { + clientConnDestroy((SCliConn*)handle, true); } // unref cli handle diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index 92e42cb380..c83f76c2ec 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -226,9 +226,13 @@ int transAllocBuffer(SConnBuffer* connBuf, uv_buf_t* uvBuf) { uvBuf->base = p->buf; uvBuf->len = CAPACITY; + } else if (p->total == -1 && p->len < CAPACITY) { + uvBuf->base = p->buf + p->len; + uvBuf->len = CAPACITY - p->len; } else { p->cap = p->total; p->buf = realloc(p->buf, p->cap); + uvBuf->base = p->buf + p->len; uvBuf->len = p->cap - p->len; } diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index 9e68b0bf7b..593d0acfc5 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -19,11 +19,10 @@ typedef struct SSrvConn { T_REF_DECLARE() - uv_tcp_t* pTcp; - uv_write_t* pWriter; - uv_timer_t* pTimer; + uv_tcp_t* pTcp; + uv_write_t pWriter; + uv_timer_t pTimer; - // uv_async_t* pWorkerAsync; queue queue; int ref; int persist; // persist connection or not @@ -65,7 +64,7 @@ typedef struct SWorkThrdObj { queue conn; pthread_mutex_t msgMtx; void* pTransInst; - bool stop; + bool quit; } SWorkThrdObj; typedef struct SServerObj { @@ -236,7 +235,7 @@ static void uvHandleReq(SSrvConn* pConn) { inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port), rpcMsg.contLen); (*(pRpc->cfp))(pRpc->parent, &rpcMsg, NULL); - // uv_timer_start(pConn->pTimer, uvHandleActivityTimeout, pRpc->idleTime * 10000, 0); + // uv_timer_start(&pConn->pTimer, uvHandleActivityTimeout, pRpc->idleTime * 10000, 0); // auth // validate msg type } @@ -312,6 +311,7 @@ static void uvOnPipeWriteCb(uv_write_t* req, int status) { } else { tError("fail to dispatch conn to work thread"); } + free(req); } static void uvPrepareSendData(SSrvMsg* smsg, uv_buf_t* wb) { @@ -349,8 +349,8 @@ static void uvStartSendRespInternal(SSrvMsg* smsg) { uvPrepareSendData(smsg, &wb); SSrvConn* pConn = smsg->pConn; - uv_timer_stop(pConn->pTimer); - uv_write(pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnWriteCb); + uv_timer_stop(&pConn->pTimer); + uv_write(&pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnWriteCb); } static void uvStartSendResp(SSrvMsg* smsg) { // impl @@ -417,8 +417,8 @@ void uvWorkerAsyncCb(uv_async_t* handle) { uv_stop(pThrd->loop); } else { destroyAllConn(pThrd); - uv_loop_close(pThrd->loop); - pThrd->stop = true; + // uv_loop_close(pThrd->loop); + pThrd->quit = true; } } else { uvStartSendResp(msg); @@ -493,9 +493,8 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { pConn->pTransInst = pThrd->pTransInst; /* init conn timer*/ - pConn->pTimer = malloc(sizeof(uv_timer_t)); - uv_timer_init(pThrd->loop, pConn->pTimer); - pConn->pTimer->data = pConn; + uv_timer_init(pThrd->loop, &pConn->pTimer); + pConn->pTimer.data = pConn; pConn->hostThrd = pThrd; @@ -504,8 +503,7 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { uv_tcp_init(pThrd->loop, pConn->pTcp); pConn->pTcp->data = pConn; - pConn->pWriter = calloc(1, sizeof(uv_write_t)); - pConn->pWriter->data = pConn; + pConn->pWriter.data = pConn; transSetConnOption((uv_tcp_t*)pConn->pTcp); @@ -633,17 +631,20 @@ static void destroyConn(SSrvConn* conn, bool clear) { } } static void uvDestroyConn(uv_handle_t* handle) { - SSrvConn* conn = handle->data; + SSrvConn* conn = handle->data; + if (conn == NULL) { + return; + } SWorkThrdObj* thrd = conn->hostThrd; tDebug("server conn %p destroy", conn); - uv_timer_stop(conn->pTimer); + uv_timer_stop(&conn->pTimer); QUEUE_REMOVE(&conn->queue); free(conn->pTcp); - free(conn->pWriter); - free(conn); + // free(conn); - if (thrd->stop && QUEUE_IS_EMPTY(&thrd->conn)) { + if (thrd->quit && QUEUE_IS_EMPTY(&thrd->conn)) { + uv_loop_close(thrd->loop); uv_stop(thrd->loop); } } @@ -680,7 +681,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)); - thrd->stop = false; + thrd->quit = false; srv->pThreadObj[i] = thrd; srv->pipe[i] = (uv_pipe_t*)calloc(2, sizeof(uv_pipe_t)); From 690dabd61795b178eec16dddb9a7a6a02a6827df Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 19:11:38 +0800 Subject: [PATCH 20/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 31 ++- source/libs/sync/src/syncMessage.c | 184 +++++++++++++++--- source/libs/sync/test/CMakeLists.txt | 38 +++- .../libs/sync/test/syncAppendEntriesTest.cpp | 2 +- .../libs/sync/test/syncClientRequestTest.cpp | 4 +- source/libs/sync/test/syncPingReplyTest.cpp | 95 +++++++++ source/libs/sync/test/syncPingTest.cpp | 173 +++++++--------- source/libs/sync/test/syncPingTimerTest.cpp | 130 +++++++++++++ source/libs/sync/test/syncTimeoutTest.cpp | 12 +- 9 files changed, 516 insertions(+), 153 deletions(-) create mode 100644 source/libs/sync/test/syncPingReplyTest.cpp create mode 100644 source/libs/sync/test/syncPingTimerTest.cpp diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index a3b7517bdc..3f19593e50 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -94,14 +94,24 @@ typedef struct SyncPing { } SyncPing; SyncPing* syncPingBuild(uint32_t dataLen); +SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); +SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId); void syncPingDestroy(SyncPing* pMsg); void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg); +char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len); +SyncPing* syncPingDeserialize2(const char* buf, uint32_t len); void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); +SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg); cJSON* syncPing2Json(const SyncPing* pMsg); -SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); -SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId); +char* syncPing2Str(const SyncPing* pMsg); + +// for debug ---------------------- +void syncPingPrint(const SyncPing* pMsg); +void syncPingPrint2(char* s, const SyncPing* pMsg); +void syncPingLog(const SyncPing* pMsg); +void syncPingLog2(char* s, const SyncPing* pMsg); // --------------------------------------------- typedef struct SyncPingReply { @@ -114,18 +124,25 @@ typedef struct SyncPingReply { char data[]; } SyncPingReply; -#define SYNC_PING_REPLY_FIX_LEN \ - (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) - SyncPingReply* syncPingReplyBuild(uint32_t dataLen); +SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); +SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId); void syncPingReplyDestroy(SyncPingReply* pMsg); void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen); void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg); +char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len); // +SyncPingReply* syncPingReplyDeserialize2(const char* buf, uint32_t len); // void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg); void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); +SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); // cJSON* syncPingReply2Json(const SyncPingReply* pMsg); -SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); -SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId); +char* syncPingReply2Str(const SyncPingReply* pMsg); // + +// for debug ---------------------- +void syncPingReplyPrint(const SyncPingReply* pMsg); +void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg); +void syncPingReplyLog(const SyncPingReply* pMsg); +void syncPingReplyLog2(char* s, const SyncPingReply* pMsg); // --------------------------------------------- typedef struct SyncClientRequest { diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 3d5d302559..477ba1929e 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -217,6 +217,20 @@ SyncPing* syncPingBuild(uint32_t dataLen) { return pMsg; } +SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { + uint32_t dataLen = strlen(str) + 1; + SyncPing* pMsg = syncPingBuild(dataLen); + pMsg->srcId = *srcId; + pMsg->destId = *destId; + snprintf(pMsg->data, pMsg->dataLen, "%s", str); + return pMsg; +} + +SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId) { + SyncPing* pMsg = syncPingBuild2(srcId, destId, "ping"); + return pMsg; +} + void syncPingDestroy(SyncPing* pMsg) { if (pMsg != NULL) { free(pMsg); @@ -234,6 +248,25 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg) { assert(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen); } +char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncPingSerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncPing* syncPingDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncPing* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncPingDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -246,6 +279,11 @@ void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg) { syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncPing* pMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncPing2Json(const SyncPing* pMsg) { char u64buf[128]; @@ -284,35 +322,75 @@ cJSON* syncPing2Json(const SyncPing* pMsg) { cJSON_AddItemToObject(pRoot, "destId", pDestId); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); - cJSON_AddStringToObject(pRoot, "data", pMsg->data); + char* s; + s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", s); + free(s); + s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data2", s); + free(s); cJSON* pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "SyncPing", pRoot); return pJson; } -SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { - uint32_t dataLen = strlen(str) + 1; - SyncPing* pMsg = syncPingBuild(dataLen); +char* syncPing2Str(const SyncPing* pMsg) { + cJSON* pJson = syncPing2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +// for debug ---------------------- +void syncPingPrint(const SyncPing* pMsg) { + char* serialized = syncPing2Str(pMsg); + printf("syncPingPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncPingPrint2(char* s, const SyncPing* pMsg) { + char* serialized = syncPing2Str(pMsg); + printf("syncPingPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncPingLog(const SyncPing* pMsg) { + char* serialized = syncPing2Str(pMsg); + sTrace("syncPingLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncPingLog2(char* s, const SyncPing* pMsg) { + char* serialized = syncPing2Str(pMsg); + sTrace("syncPingLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); +} + +// ---- message process SyncPingReply---- +SyncPingReply* syncPingReplyBuild(uint32_t dataLen) { + uint32_t bytes = sizeof(SyncPingReply) + dataLen; + SyncPingReply* pMsg = malloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = SYNC_PING_REPLY; + pMsg->dataLen = dataLen; + return pMsg; +} + +SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { + uint32_t dataLen = strlen(str) + 1; + SyncPingReply* pMsg = syncPingReplyBuild(dataLen); pMsg->srcId = *srcId; pMsg->destId = *destId; snprintf(pMsg->data, pMsg->dataLen, "%s", str); return pMsg; } -SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId) { - SyncPing* pMsg = syncPingBuild2(srcId, destId, "ping"); - return pMsg; -} - -// ---- message process SyncPingReply---- -SyncPingReply* syncPingReplyBuild(uint32_t dataLen) { - uint32_t bytes = SYNC_PING_REPLY_FIX_LEN + dataLen; - SyncPingReply* pMsg = malloc(bytes); - memset(pMsg, 0, bytes); - pMsg->bytes = bytes; - pMsg->msgType = SYNC_PING_REPLY; - pMsg->dataLen = dataLen; +SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId) { + SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, "pang"); return pMsg; } @@ -333,6 +411,25 @@ void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg assert(pMsg->bytes == sizeof(SyncPing) + pMsg->dataLen); } +char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len) { + char* buf = malloc(pMsg->bytes); + assert(buf != NULL); + syncPingReplySerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncPingReply* syncPingReplyDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncPingReply* pMsg = malloc(bytes); + assert(pMsg != NULL); + syncPingReplyDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} + void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -345,6 +442,11 @@ void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg) { syncPingReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); } +SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncPingReply* pMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} + cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { char u64buf[128]; @@ -383,25 +485,51 @@ cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { cJSON_AddItemToObject(pRoot, "destId", pDestId); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); - cJSON_AddStringToObject(pRoot, "data", pMsg->data); + char* s; + s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", s); + free(s); + s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data2", s); + free(s); cJSON* pJson = cJSON_CreateObject(); cJSON_AddItemToObject(pJson, "SyncPingReply", pRoot); return pJson; } -SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { - uint32_t dataLen = strlen(str) + 1; - SyncPingReply* pMsg = syncPingReplyBuild(dataLen); - pMsg->srcId = *srcId; - pMsg->destId = *destId; - snprintf(pMsg->data, pMsg->dataLen, "%s", str); - return pMsg; +char* syncPingReply2Str(const SyncPingReply* pMsg) { + cJSON* pJson = syncPingReply2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; } -SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId) { - SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, "pang"); - return pMsg; +// for debug ---------------------- +void syncPingReplyPrint(const SyncPingReply* pMsg) { + char* serialized = syncPingReply2Str(pMsg); + printf("syncPingReplyPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg) { + char* serialized = syncPingReply2Str(pMsg); + printf("syncPingReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncPingReplyLog(const SyncPingReply* pMsg) { + char* serialized = syncPingReply2Str(pMsg); + sTrace("syncPingReplyLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncPingReplyLog2(char* s, const SyncPingReply* pMsg) { + char* serialized = syncPingReply2Str(pMsg); + sTrace("syncPingReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); } // ---- message process SyncClientRequest---- diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 2ed4799467..3a87d3f2e2 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -1,6 +1,6 @@ add_executable(syncTest "") add_executable(syncEnvTest "") -add_executable(syncPingTest "") +add_executable(syncPingTimerTest "") add_executable(syncEncodeTest "") add_executable(syncIOTickQTest "") add_executable(syncIOTickPingTest "") @@ -23,6 +23,8 @@ add_executable(syncAppendEntriesTest "") add_executable(syncAppendEntriesReplyTest "") add_executable(syncClientRequestTest "") add_executable(syncTimeoutTest "") +add_executable(syncPingTest "") +add_executable(syncPingReplyTest "") target_sources(syncTest @@ -33,9 +35,9 @@ target_sources(syncEnvTest PRIVATE "syncEnvTest.cpp" ) -target_sources(syncPingTest +target_sources(syncPingTimerTest PRIVATE - "syncPingTest.cpp" + "syncPingTimerTest.cpp" ) target_sources(syncEncodeTest PRIVATE @@ -125,6 +127,14 @@ target_sources(syncTimeoutTest PRIVATE "syncTimeoutTest.cpp" ) +target_sources(syncPingTest + PRIVATE + "syncPingTest.cpp" +) +target_sources(syncPingReplyTest + PRIVATE + "syncPingReplyTest.cpp" +) target_include_directories(syncTest @@ -137,7 +147,7 @@ target_include_directories(syncEnvTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncPingTest +target_include_directories(syncPingTimerTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" @@ -252,6 +262,16 @@ target_include_directories(syncTimeoutTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncPingTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncPingReplyTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -262,7 +282,7 @@ target_link_libraries(syncEnvTest sync gtest_main ) -target_link_libraries(syncPingTest +target_link_libraries(syncPingTimerTest sync gtest_main ) @@ -354,6 +374,14 @@ target_link_libraries(syncTimeoutTest sync gtest_main ) +target_link_libraries(syncPingTest + sync + gtest_main +) +target_link_libraries(syncPingReplyTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncAppendEntriesTest.cpp b/source/libs/sync/test/syncAppendEntriesTest.cpp index 6fa3041919..687d9bcb94 100644 --- a/source/libs/sync/test/syncAppendEntriesTest.cpp +++ b/source/libs/sync/test/syncAppendEntriesTest.cpp @@ -38,7 +38,7 @@ void test2() { uint32_t len = pMsg->bytes; char * serialized = (char *)malloc(len); syncAppendEntriesSerialize(pMsg, serialized, len); - SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg2->dataLen); + SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg->dataLen); syncAppendEntriesDeserialize(serialized, len, pMsg2); syncAppendEntriesPrint2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2); diff --git a/source/libs/sync/test/syncClientRequestTest.cpp b/source/libs/sync/test/syncClientRequestTest.cpp index 311a7e5fa3..6323f53a03 100644 --- a/source/libs/sync/test/syncClientRequestTest.cpp +++ b/source/libs/sync/test/syncClientRequestTest.cpp @@ -20,7 +20,7 @@ SyncClientRequest *createMsg() { rpcMsg.msgType = 12345; rpcMsg.contLen = 20; rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); - strcpy((char*)rpcMsg.pCont, "hello rpc"); + strcpy((char *)rpcMsg.pCont, "hello rpc"); SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true); return pMsg; } @@ -36,7 +36,7 @@ void test2() { uint32_t len = pMsg->bytes; char * serialized = (char *)malloc(len); syncClientRequestSerialize(pMsg, serialized, len); - SyncClientRequest *pMsg2 = syncClientRequestBuild(pMsg2->dataLen); + SyncClientRequest *pMsg2 = syncClientRequestBuild(pMsg->dataLen); syncClientRequestDeserialize(serialized, len, pMsg2); syncClientRequestPrint2((char *)"test2: syncClientRequestSerialize -> syncClientRequestDeserialize ", pMsg2); diff --git a/source/libs/sync/test/syncPingReplyTest.cpp b/source/libs/sync/test/syncPingReplyTest.cpp new file mode 100644 index 0000000000..27f88b698c --- /dev/null +++ b/source/libs/sync/test/syncPingReplyTest.cpp @@ -0,0 +1,95 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +SyncPingReply *createMsg() { + SRaftId srcId, destId; + srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + srcId.vgId = 100; + destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + destId.vgId = 100; + SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId); + return pMsg; +} + +void test1() { + SyncPingReply *pMsg = createMsg(); + syncPingReplyPrint2((char *)"test1:", pMsg); + syncPingReplyDestroy(pMsg); +} + +void test2() { + SyncPingReply *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncPingReplySerialize(pMsg, serialized, len); + SyncPingReply *pMsg2 = syncPingReplyBuild(pMsg->dataLen); + syncPingReplyDeserialize(serialized, len, pMsg2); + syncPingReplyPrint2((char *)"test2: syncPingReplySerialize -> syncPingReplyDeserialize ", pMsg2); + + free(serialized); + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); +} + +void test3() { + SyncPingReply *pMsg = createMsg(); + uint32_t len; + char * serialized = syncPingReplySerialize2(pMsg, &len); + SyncPingReply *pMsg2 = syncPingReplyDeserialize2(serialized, len); + syncPingReplyPrint2((char *)"test3: syncPingReplySerialize3 -> syncPingReplyDeserialize2 ", pMsg2); + + free(serialized); + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); +} + +void test4() { + SyncPingReply *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncPingReply2RpcMsg(pMsg, &rpcMsg); + SyncPingReply *pMsg2 = (SyncPingReply *)malloc(rpcMsg.contLen); + syncPingReplyFromRpcMsg(&rpcMsg, pMsg2); + syncPingReplyPrint2((char *)"test4: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg ", pMsg2); + + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); +} + +void test5() { + SyncPingReply *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncPingReply2RpcMsg(pMsg, &rpcMsg); + SyncPingReply *pMsg2 = syncPingReplyFromRpcMsg2(&rpcMsg); + syncPingReplyPrint2((char *)"test5: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg2 ", pMsg2); + + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index e69878632f..8776d2de52 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -1,9 +1,8 @@ #include #include -#include "syncEnv.h" #include "syncIO.h" #include "syncInt.h" -#include "syncRaftStore.h" +#include "syncMessage.h" #include "syncUtil.h" void logTest() { @@ -15,116 +14,82 @@ void logTest() { sFatal("--- sync log test: fatal"); } -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; - -SSyncNode* syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./"); - - SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->replicaNum = replicaNum; - - for (int i = 0; i < replicaNum; ++i) { - pCfg->nodeInfo[i].nodePort = ports[i]; - snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1"); - // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); - } - - SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); - assert(pSyncNode != NULL); - - gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; - gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; - gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote; - gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply; - gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries; - gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply; - gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->pSyncNode = pSyncNode; - - return pSyncNode; +SyncPing *createMsg() { + SRaftId srcId, destId; + srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + srcId.vgId = 100; + destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + destId.vgId = 100; + SyncPing *pMsg = syncPingBuild3(&srcId, &destId); + return pMsg; } -SSyncNode* syncInitTest() { return syncNodeInit(); } - -void initRaftId(SSyncNode* pSyncNode) { - for (int i = 0; i < replicaNum; ++i) { - ids[i] = pSyncNode->replicasId[i]; - char* s = syncUtilRaftId2Str(&ids[i]); - printf("raftId[%d] : %s\n", i, s); - free(s); - } +void test1() { + SyncPing *pMsg = createMsg(); + syncPingPrint2((char *)"test1:", pMsg); + syncPingDestroy(pMsg); } -int main(int argc, char** argv) { +void test2() { + SyncPing *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); + syncPingSerialize(pMsg, serialized, len); + SyncPing *pMsg2 = syncPingBuild(pMsg->dataLen); + syncPingDeserialize(serialized, len, pMsg2); + syncPingPrint2((char *)"test2: syncPingSerialize -> syncPingDeserialize ", pMsg2); + + free(serialized); + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); +} + +void test3() { + SyncPing *pMsg = createMsg(); + uint32_t len; + char * serialized = syncPingSerialize2(pMsg, &len); + SyncPing *pMsg2 = syncPingDeserialize2(serialized, len); + syncPingPrint2((char *)"test3: syncPingSerialize3 -> syncPingDeserialize2 ", pMsg2); + + free(serialized); + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); +} + +void test4() { + SyncPing *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncPing2RpcMsg(pMsg, &rpcMsg); + SyncPing *pMsg2 = (SyncPing *)malloc(rpcMsg.contLen); + syncPingFromRpcMsg(&rpcMsg, pMsg2); + syncPingPrint2((char *)"test4: syncPing2RpcMsg -> syncPingFromRpcMsg ", pMsg2); + + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); +} + +void test5() { + SyncPing *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncPing2RpcMsg(pMsg, &rpcMsg); + SyncPing *pMsg2 = syncPingFromRpcMsg2(&rpcMsg); + syncPingPrint2((char *)"test5: syncPing2RpcMsg -> syncPingFromRpcMsg2 ", pMsg2); + + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); +} + +int main() { // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; sDebugFlag = 143 + 64; + logTest(); - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - ret = syncEnvStart(); - assert(ret == 0); - - SSyncNode* pSyncNode = syncInitTest(); - assert(pSyncNode != NULL); - syncNodePrint2((char*)"----1", pSyncNode); - - initRaftId(pSyncNode); - - //--------------------------- - - sTrace("syncNodeStartPingTimer ..."); - ret = syncNodeStartPingTimer(pSyncNode); - assert(ret == 0); - syncNodePrint2((char*)"----2", pSyncNode); - - sTrace("sleep ..."); - taosMsleep(10000); - - sTrace("syncNodeStopPingTimer ..."); - ret = syncNodeStopPingTimer(pSyncNode); - assert(ret == 0); - syncNodePrint2((char*)"----3", pSyncNode); - - sTrace("sleep ..."); - taosMsleep(5000); - - sTrace("syncNodeStartPingTimer ..."); - ret = syncNodeStartPingTimer(pSyncNode); - assert(ret == 0); - syncNodePrint2((char*)"----4", pSyncNode); - - sTrace("sleep ..."); - taosMsleep(10000); - - sTrace("syncNodeStopPingTimer ..."); - ret = syncNodeStopPingTimer(pSyncNode); - assert(ret == 0); - syncNodePrint2((char*)"----5", pSyncNode); - - while (1) { - sTrace("while 1 sleep ..."); - taosMsleep(1000); - } + test1(); + test2(); + test3(); + test4(); + test5(); return 0; } diff --git a/source/libs/sync/test/syncPingTimerTest.cpp b/source/libs/sync/test/syncPingTimerTest.cpp new file mode 100644 index 0000000000..e69878632f --- /dev/null +++ b/source/libs/sync/test/syncPingTimerTest.cpp @@ -0,0 +1,130 @@ +#include +#include +#include "syncEnv.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftStore.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; +int32_t replicaNum = 3; +int32_t myIndex = 0; + +SRaftId ids[TSDB_MAX_REPLICA]; +SSyncInfo syncInfo; +SSyncFSM* pFsm; + +SSyncNode* syncNodeInit() { + syncInfo.vgId = 1234; + syncInfo.rpcClient = gSyncIO->clientRpc; + syncInfo.FpSendMsg = syncIOSendMsg; + syncInfo.queue = gSyncIO->pMsgQ; + syncInfo.FpEqMsg = syncIOEqMsg; + syncInfo.pFsm = pFsm; + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./"); + + SSyncCfg* pCfg = &syncInfo.syncCfg; + pCfg->myIndex = myIndex; + pCfg->replicaNum = replicaNum; + + for (int i = 0; i < replicaNum; ++i) { + pCfg->nodeInfo[i].nodePort = ports[i]; + snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1"); + // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); + } + + SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); + assert(pSyncNode != NULL); + + gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; + gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; + gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote; + gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply; + gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries; + gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply; + gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; + gSyncIO->pSyncNode = pSyncNode; + + return pSyncNode; +} + +SSyncNode* syncInitTest() { return syncNodeInit(); } + +void initRaftId(SSyncNode* pSyncNode) { + for (int i = 0; i < replicaNum; ++i) { + ids[i] = pSyncNode->replicasId[i]; + char* s = syncUtilRaftId2Str(&ids[i]); + printf("raftId[%d] : %s\n", i, s); + free(s); + } +} + +int main(int argc, char** argv) { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + myIndex = 0; + if (argc >= 2) { + myIndex = atoi(argv[1]); + } + + int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); + assert(ret == 0); + + ret = syncEnvStart(); + assert(ret == 0); + + SSyncNode* pSyncNode = syncInitTest(); + assert(pSyncNode != NULL); + syncNodePrint2((char*)"----1", pSyncNode); + + initRaftId(pSyncNode); + + //--------------------------- + + sTrace("syncNodeStartPingTimer ..."); + ret = syncNodeStartPingTimer(pSyncNode); + assert(ret == 0); + syncNodePrint2((char*)"----2", pSyncNode); + + sTrace("sleep ..."); + taosMsleep(10000); + + sTrace("syncNodeStopPingTimer ..."); + ret = syncNodeStopPingTimer(pSyncNode); + assert(ret == 0); + syncNodePrint2((char*)"----3", pSyncNode); + + sTrace("sleep ..."); + taosMsleep(5000); + + sTrace("syncNodeStartPingTimer ..."); + ret = syncNodeStartPingTimer(pSyncNode); + assert(ret == 0); + syncNodePrint2((char*)"----4", pSyncNode); + + sTrace("sleep ..."); + taosMsleep(10000); + + sTrace("syncNodeStopPingTimer ..."); + ret = syncNodeStopPingTimer(pSyncNode); + assert(ret == 0); + syncNodePrint2((char*)"----5", pSyncNode); + + while (1) { + sTrace("while 1 sleep ..."); + taosMsleep(1000); + } + + return 0; +} diff --git a/source/libs/sync/test/syncTimeoutTest.cpp b/source/libs/sync/test/syncTimeoutTest.cpp index 4d2da97a00..3f46ab5c7c 100644 --- a/source/libs/sync/test/syncTimeoutTest.cpp +++ b/source/libs/sync/test/syncTimeoutTest.cpp @@ -29,8 +29,8 @@ void test1() { void test2() { SyncTimeout *pMsg = createMsg(); - uint32_t len = pMsg->bytes; - char * serialized = (char *)malloc(len); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); syncTimeoutSerialize(pMsg, serialized, len); SyncTimeout *pMsg2 = syncTimeoutBuild(); syncTimeoutDeserialize(serialized, len, pMsg2); @@ -43,8 +43,8 @@ void test2() { void test3() { SyncTimeout *pMsg = createMsg(); - uint32_t len; - char * serialized = syncTimeoutSerialize2(pMsg, &len); + uint32_t len; + char * serialized = syncTimeoutSerialize2(pMsg, &len); SyncTimeout *pMsg2 = syncTimeoutDeserialize2(serialized, len); syncTimeoutPrint2((char *)"test3: syncTimeoutSerialize3 -> syncTimeoutDeserialize2 ", pMsg2); @@ -55,7 +55,7 @@ void test3() { void test4() { SyncTimeout *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncTimeout2RpcMsg(pMsg, &rpcMsg); SyncTimeout *pMsg2 = (SyncTimeout *)malloc(rpcMsg.contLen); syncTimeoutFromRpcMsg(&rpcMsg, pMsg2); @@ -67,7 +67,7 @@ void test4() { void test5() { SyncTimeout *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncTimeout2RpcMsg(pMsg, &rpcMsg); SyncTimeout *pMsg2 = syncTimeoutFromRpcMsg2(&rpcMsg); syncTimeoutPrint2((char *)"test5: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg2 ", pMsg2); From 0c307568e456c897f77f81e3106e287d848284e6 Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Fri, 11 Mar 2022 19:38:13 +0800 Subject: [PATCH 21/29] code refactor --- include/common/tmsg.h | 138 ++++------ source/dnode/vnode/inc/meta.h | 4 +- source/dnode/vnode/inc/tsdb.h | 23 +- source/dnode/vnode/src/inc/tsdbFS.h | 13 +- source/dnode/vnode/src/inc/tsdbFile.h | 11 + source/dnode/vnode/src/inc/tsdbSma.h | 14 +- source/dnode/vnode/src/meta/metaBDBImpl.c | 35 ++- source/dnode/vnode/src/tsdb/tsdbSma.c | 309 +++++++++------------- source/dnode/vnode/src/tsdb/tsdbWrite.c | 12 +- source/dnode/vnode/test/tsdbSmaTest.cpp | 158 +++++------ source/os/src/osFile.c | 2 +- 11 files changed, 334 insertions(+), 385 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 0dcf554433..ec74b6e8b9 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1895,24 +1895,19 @@ typedef enum { } ETDTimeUnit; typedef struct { - uint16_t funcId; - uint16_t nColIds; - col_id_t* colIds; // sorted colIds -} SFuncColIds; - -typedef struct { - uint8_t version; // for compatibility - uint8_t intervalUnit; - uint8_t slidingUnit; - char indexName[TSDB_INDEX_NAME_LEN]; - char timezone[TD_TIMEZONE_LEN]; - uint16_t nFuncColIds; - uint16_t tagsFilterLen; - tb_uid_t tableUid; // super/common table uid - int64_t interval; - int64_t sliding; - SFuncColIds* funcColIds; // sorted funcIds - char* tagsFilter; + int8_t version; // for compatibility(default 0) + int8_t intervalUnit; + int8_t slidingUnit; + char indexName[TSDB_INDEX_NAME_LEN]; + char timezone[TD_TIMEZONE_LEN]; // sma data is invalid if timezone change. + uint16_t exprLen; + uint16_t tagsFilterLen; + int64_t indexUid; + tb_uid_t tableUid; // super/child/common table uid + int64_t interval; + int64_t sliding; + char* expr; // sma expression + char* tagsFilter; } STSma; // Time-range-wise SMA typedef struct { @@ -1939,24 +1934,30 @@ int32_t tSerializeSVDropTSmaReq(void** buf, SVDropTSmaReq* pReq); void* tDeserializeSVDropTSmaReq(void* buf, SVDropTSmaReq* pReq); typedef struct { - STimeWindow tsWindow; // [skey, ekey] - uint64_t tableUid; // sub/common table uid - int32_t numOfBlocks; // number of sma blocks for each column, total number is numOfBlocks*numOfColId - int32_t dataLen; // total data length - col_id_t* colIds; // e.g. 2,4,9,10 - col_id_t numOfColIds; // e.g. 4 - char data[]; // the sma blocks -} STSmaData; + col_id_t colId; + uint16_t blockSize; // sma data block size + char data[]; +} STSmaColData; -// TODO: move to the final location afte schema of STSma/STSmaData defined -static FORCE_INLINE void tdDestroySmaData(STSmaData* pSmaData) { - if (pSmaData) { - if (pSmaData->colIds) { - tfree(pSmaData->colIds); - } - tfree(pSmaData); - } -} +typedef struct { + tb_uid_t tableUid; // super/child/normal table uid + int32_t dataLen; // not including head + char data[]; +} STSmaTbData; + +typedef struct { + int64_t indexUid; + TSKEY skey; // startTS of one interval/sliding + int64_t interval; + int32_t dataLen; // not including head + int8_t intervalUnit; + char data[]; +} STSmaDataWrapper; // sma data for a interval/sliding window + +// interval/sliding => window + +// => window->table->colId +// => 当一个window下所有的表均计算完成时,流计算告知tsdb清除window的过期标记 // RSma: Rollup SMA typedef struct { @@ -1979,13 +1980,7 @@ typedef struct { static FORCE_INLINE void tdDestroyTSma(STSma* pSma) { if (pSma) { - if (pSma->funcColIds != NULL) { - for (uint16_t i = 0; i < pSma->nFuncColIds; ++i) { - tfree((pSma->funcColIds + i)->colIds); - } - tfree(pSma->funcColIds); - } - + tfree(pSma->expr); tfree(pSma->tagsFilter); } } @@ -2004,24 +1999,20 @@ static FORCE_INLINE void tdDestroyTSmaWrapper(STSmaWrapper* pSW) { static FORCE_INLINE int32_t tEncodeTSma(void** buf, const STSma* pSma) { int32_t tlen = 0; - tlen += taosEncodeFixedU8(buf, pSma->version); - tlen += taosEncodeFixedU8(buf, pSma->intervalUnit); - tlen += taosEncodeFixedU8(buf, pSma->slidingUnit); + tlen += taosEncodeFixedI8(buf, pSma->version); + tlen += taosEncodeFixedI8(buf, pSma->intervalUnit); + tlen += taosEncodeFixedI8(buf, pSma->slidingUnit); tlen += taosEncodeString(buf, pSma->indexName); tlen += taosEncodeString(buf, pSma->timezone); - tlen += taosEncodeFixedU16(buf, pSma->nFuncColIds); + tlen += taosEncodeFixedU16(buf, pSma->exprLen); tlen += taosEncodeFixedU16(buf, pSma->tagsFilterLen); + tlen += taosEncodeFixedI64(buf, pSma->indexUid); tlen += taosEncodeFixedI64(buf, pSma->tableUid); tlen += taosEncodeFixedI64(buf, pSma->interval); tlen += taosEncodeFixedI64(buf, pSma->sliding); - - for (uint16_t i = 0; i < pSma->nFuncColIds; ++i) { - SFuncColIds* funcColIds = pSma->funcColIds + i; - tlen += taosEncodeFixedU16(buf, funcColIds->funcId); - tlen += taosEncodeFixedU16(buf, funcColIds->nColIds); - for (uint16_t j = 0; j < funcColIds->nColIds; ++j) { - tlen += taosEncodeFixedU16(buf, *(funcColIds->colIds + j)); - } + + if (pSma->exprLen > 0) { + tlen += taosEncodeString(buf, pSma->expr); } if (pSma->tagsFilterLen > 0) { @@ -2042,43 +2033,30 @@ static FORCE_INLINE int32_t tEncodeTSmaWrapper(void** buf, const STSmaWrapper* p } static FORCE_INLINE void* tDecodeTSma(void* buf, STSma* pSma) { - buf = taosDecodeFixedU8(buf, &pSma->version); - buf = taosDecodeFixedU8(buf, &pSma->intervalUnit); - buf = taosDecodeFixedU8(buf, &pSma->slidingUnit); + buf = taosDecodeFixedI8(buf, &pSma->version); + buf = taosDecodeFixedI8(buf, &pSma->intervalUnit); + buf = taosDecodeFixedI8(buf, &pSma->slidingUnit); buf = taosDecodeStringTo(buf, pSma->indexName); buf = taosDecodeStringTo(buf, pSma->timezone); - buf = taosDecodeFixedU16(buf, &pSma->nFuncColIds); + buf = taosDecodeFixedU16(buf, &pSma->exprLen); buf = taosDecodeFixedU16(buf, &pSma->tagsFilterLen); + buf = taosDecodeFixedI64(buf, &pSma->indexUid); buf = taosDecodeFixedI64(buf, &pSma->tableUid); buf = taosDecodeFixedI64(buf, &pSma->interval); buf = taosDecodeFixedI64(buf, &pSma->sliding); - if (pSma->nFuncColIds > 0) { - pSma->funcColIds = (SFuncColIds*)calloc(pSma->nFuncColIds, sizeof(SFuncColIds)); - if (pSma->funcColIds == NULL) { + + if (pSma->exprLen > 0) { + pSma->expr = (char*)calloc(pSma->exprLen, 1); + if (pSma->expr != NULL) { + buf = taosDecodeStringTo(buf, pSma->expr); + } else { tdDestroyTSma(pSma); return NULL; } - for (uint16_t i = 0; i < pSma->nFuncColIds; ++i) { - SFuncColIds* funcColIds = pSma->funcColIds + i; - buf = taosDecodeFixedU16(buf, &funcColIds->funcId); - buf = taosDecodeFixedU16(buf, &funcColIds->nColIds); - if (funcColIds->nColIds > 0) { - funcColIds->colIds = (col_id_t*)calloc(funcColIds->nColIds, sizeof(col_id_t)); - if (funcColIds->colIds != NULL) { - for (uint16_t j = 0; j < funcColIds->nColIds; ++j) { - buf = taosDecodeFixedU16(buf, funcColIds->colIds + j); - } - } else { - tdDestroyTSma(pSma); - return NULL; - } - } else { - funcColIds->colIds = NULL; - } - } + } else { - pSma->funcColIds = NULL; + pSma->expr = NULL; } if (pSma->tagsFilterLen > 0) { diff --git a/source/dnode/vnode/inc/meta.h b/source/dnode/vnode/inc/meta.h index 2d747d0e80..05749884d3 100644 --- a/source/dnode/vnode/inc/meta.h +++ b/source/dnode/vnode/inc/meta.h @@ -58,8 +58,8 @@ STbCfg * metaGetTbInfoByUid(SMeta *pMeta, tb_uid_t uid); STbCfg * metaGetTbInfoByName(SMeta *pMeta, char *tbname, tb_uid_t *uid); SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline); STSchema * metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver); -STSma * metaGetSmaInfoByName(SMeta *pMeta, const char *indexName); -STSmaWrapper * metaGetSmaInfoByUid(SMeta *pMeta, tb_uid_t uid); +STSma * metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid); +STSmaWrapper * metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid); SArray * metaGetSmaTbUids(SMeta *pMeta, bool isDup); SMTbCursor *metaOpenTbCursor(SMeta *pMeta); diff --git a/source/dnode/vnode/inc/tsdb.h b/source/dnode/vnode/inc/tsdb.h index 5513742c73..e67e0cae4b 100644 --- a/source/dnode/vnode/inc/tsdb.h +++ b/source/dnode/vnode/inc/tsdb.h @@ -89,24 +89,21 @@ int tsdbCommit(STsdb *pTsdb); /** * @brief Insert tSma(Time-range-wise SMA) data from stream computing engine - * - * @param pTsdb - * @param param - * @param pData - * @return int32_t + * + * @param pTsdb + * @param msg + * @return int32_t */ -int32_t tsdbInsertTSmaData(STsdb *pTsdb, STSma *param, STSmaData *pData); +int32_t tsdbInsertTSmaData(STsdb *pTsdb, char *msg); /** * @brief Insert RSma(Time-range-wise Rollup SMA) data. - * - * @param pTsdb - * @param param - * @param pData - * @return int32_t + * + * @param pTsdb + * @param msg + * @return int32_t */ -int32_t tsdbInsertRSmaData(STsdb *pTsdb, SRSma *param, STSmaData *pData); - +int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg); // STsdbCfg int tsdbOptionsInit(STsdbCfg *); diff --git a/source/dnode/vnode/src/inc/tsdbFS.h b/source/dnode/vnode/src/inc/tsdbFS.h index a7275f57fd..96c5f8468f 100644 --- a/source/dnode/vnode/src/inc/tsdbFS.h +++ b/source/dnode/vnode/src/inc/tsdbFS.h @@ -42,17 +42,14 @@ typedef struct { typedef struct { STsdbFSMeta meta; // FS meta SArray * df; // data file array - - // SArray * v2t100.index_name - - SArray * smaf; // sma data file array v2t1900.index_name + SArray * sf; // sma data file array v2(t|r)1900.index_name_1 } SFSStatus; /** * @brief Directory structure of .tsma data files. - * - * root@cary /vnode2/tsdb $ tree .tsma/ - * .tsma/ + * + * /vnode2/tsdb $ tree .sma/ + * .sma/ * ├── v2t100.index_name_1 * ├── v2t101.index_name_1 * ├── v2t102.index_name_1 @@ -66,7 +63,7 @@ typedef struct { * 0 directories, 9 files */ - typedef struct { +typedef struct { pthread_rwlock_t lock; SFSStatus *cstatus; // current status diff --git a/source/dnode/vnode/src/inc/tsdbFile.h b/source/dnode/vnode/src/inc/tsdbFile.h index d6ce33c389..5cc8cc045e 100644 --- a/source/dnode/vnode/src/inc/tsdbFile.h +++ b/source/dnode/vnode/src/inc/tsdbFile.h @@ -335,6 +335,17 @@ typedef struct { SDFile files[TSDB_FILE_MAX]; } SDFileSet; +typedef struct { + int fid; + int8_t state; + uint8_t ver; +#if 0 + SDFInfo info; +#endif + STfsFile f; + TdFilePtr pFile; +} SSFile; // files split by days with fid + #define TSDB_LATEST_FSET_VER 0 #define TSDB_FSET_FID(s) ((s)->fid) diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index 1a5ccbdc21..7fceb580d6 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -19,26 +19,28 @@ typedef struct SSmaStat SSmaStat; // insert/update interface -int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData); -int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, SRSma *param, STSmaData *pData); +int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg); +int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, char *msg); // query interface // TODO: This is the basic params, and should wrap the params to a queryHandle. -int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeWindow *queryWin, int32_t nMaxResult); +int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSmaDataWrapper *pData, STimeWindow *queryWin, int32_t nMaxResult); // management interface int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg); +int32_t tsdbDestroySmaState(SSmaStat *pSmaStat); +#if 0 int32_t tsdbGetTSmaStatus(STsdb *pTsdb, STSma *param, void *result); int32_t tsdbRemoveTSmaData(STsdb *pTsdb, STSma *param, STimeWindow *pWin); -int32_t tsdbDestroySmaState(SSmaStat *pSmaStat); +#endif // internal func -static FORCE_INLINE int32_t tsdbEncodeTSmaKey(uint64_t tableUid, col_id_t colId, TSKEY tsKey, void **pData) { +static FORCE_INLINE int32_t tsdbEncodeTSmaKey(tb_uid_t tableUid, col_id_t colId, TSKEY tsKey, void **pData) { int32_t len = 0; - len += taosEncodeFixedU64(pData, tableUid); + len += taosEncodeFixedI64(pData, tableUid); len += taosEncodeFixedU16(pData, colId); len += taosEncodeFixedI64(pData, tsKey); return len; diff --git a/source/dnode/vnode/src/meta/metaBDBImpl.c b/source/dnode/vnode/src/meta/metaBDBImpl.c index efdb3e0fe4..d9af526c2a 100644 --- a/source/dnode/vnode/src/meta/metaBDBImpl.c +++ b/source/dnode/vnode/src/meta/metaBDBImpl.c @@ -227,21 +227,27 @@ int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) { } int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) { - char buf[512] = {0}; // TODO: may overflow - void *pBuf = NULL; + // char buf[512] = {0}; // TODO: may overflow + void *pBuf = NULL, *qBuf = NULL; DBT key1 = {0}, value1 = {0}; { // save sma info - pBuf = buf; + int32_t len = tEncodeTSma(NULL, pSmaCfg); + pBuf = calloc(len, 1); + if (pBuf == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } - key1.data = pSmaCfg->indexName; - key1.size = strlen(key1.data); + key1.data = (void *)&pSmaCfg->indexUid; + key1.size = sizeof(pSmaCfg->indexUid); - tEncodeTSma(&pBuf, pSmaCfg); + qBuf = pBuf; + tEncodeTSma(&qBuf, pSmaCfg); - value1.data = buf; - value1.size = POINTER_DISTANCE(pBuf, buf); + value1.data = pBuf; + value1.size = POINTER_DISTANCE(qBuf, pBuf); value1.app_data = pSmaCfg; } @@ -609,7 +615,7 @@ STbCfg *metaGetTbInfoByName(SMeta *pMeta, char *tbname, tb_uid_t *uid) { return pTbCfg; } -STSma *metaGetSmaInfoByName(SMeta *pMeta, const char *indexName) { +STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) { STSma * pCfg = NULL; SMetaDB *pDB = pMeta->pDB; DBT key = {0}; @@ -617,8 +623,8 @@ STSma *metaGetSmaInfoByName(SMeta *pMeta, const char *indexName) { int ret; // Set key/value - key.data = (void *)indexName; - key.size = strlen(indexName); + key.data = (void *)&indexUid; + key.size = sizeof(indexUid); // Query metaDBRLock(pDB); @@ -634,7 +640,10 @@ STSma *metaGetSmaInfoByName(SMeta *pMeta, const char *indexName) { return NULL; } - tDecodeTSma(value.data, pCfg); + if (tDecodeTSma(value.data, pCfg) == NULL) { + tfree(pCfg); + return NULL; + } return pCfg; } @@ -871,7 +880,7 @@ const char *metaSmaCursorNext(SMSmaCursor *pCur) { } } -STSmaWrapper *metaGetSmaInfoByUid(SMeta *pMeta, tb_uid_t uid) { +STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) { STSmaWrapper *pSW = NULL; pSW = calloc(sizeof(*pSW), 1); diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 51abcbd9dd..dc0d262725 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -19,22 +19,20 @@ #define SMA_STORAGE_SPLIT_HOURS 24 #define SMA_KEY_LEN 18 // tableUid_colId_TSKEY 8+2+8 -#define SMA_STORE_SINGLE_BLOCKS // store SMA data by single block or multiple blocks - #define SMA_STATE_HASH_SLOT 4 #define SMA_STATE_ITEM_HASH_SLOT 32 #define SMA_TEST_INDEX_NAME "smaTestIndexName" // TODO: just for test +#define SMA_TEST_INDEX_UID 123456 // TODO: just for test typedef enum { - SMA_STORAGE_LEVEL_TSDB = 0, // store TSma in dir e.g. vnode${N}/tsdb/.tsma - SMA_STORAGE_LEVEL_DFILESET = 1 // store TSma in file e.g. vnode${N}/tsdb/v2f1900.tsma.${sma_index_name} + SMA_STORAGE_LEVEL_TSDB = 0, // use days of self-defined e.g. vnode${N}/tsdb/tsma/sma_index_uid/v2t200.dat + SMA_STORAGE_LEVEL_DFILESET = 1 // use days of TS data e.g. vnode${N}/tsdb/rsma/sma_index_uid/v2r200.dat } ESmaStorageLevel; typedef struct { STsdb * pTsdb; char * pDFile; // TODO: use the real DFile type, not char* int32_t interval; // interval with the precision of DB - int32_t blockSize; // size of SMA block item // TODO } STSmaWriteH; @@ -62,6 +60,7 @@ typedef struct { */ int8_t state; // ETsdbSmaStat SHashObj *expiredWindows; // key: skey of time window, value: N/A + STSma * pSma; } SSmaStatItem; struct SSmaStat { @@ -69,20 +68,18 @@ struct SSmaStat { }; // declaration of static functions -static int32_t tsdbInitTSmaWriteH(STSmaWriteH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData); -static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData); +static int32_t tsdbInitTSmaWriteH(STSmaWriteH *pSmaH, STsdb *pTsdb, STSmaDataWrapper *pData); +static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSmaDataWrapper *pData); static int32_t tsdbJudgeStorageLevel(int64_t interval, int8_t intervalUnit); -static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaData *pData, int32_t sectionDataLen, int32_t nBlocks); +static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaDataWrapper *pData); static int32_t tsdbInsertTSmaBlocks(void *bTree, const char *smaKey, const char *pData, int32_t dataLen); -static int32_t tsdbTSmaDataSplit(STSmaWriteH *pSmaH, STSma *param, STSmaData *pData, int32_t days, int32_t nOffset, - int32_t fid, int32_t *nSmaBlocks); -static int64_t tsdbGetIntervalByPrecision(int64_t interval, uint8_t intervalUnit, int8_t precision); -static int32_t tsdbSetTSmaDataFile(STSmaWriteH *pSmaH, STSma *param, STSmaData *pData, int32_t storageLevel, - int32_t fid); -static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData); -static int32_t tsdbInitTSmaFile(STSmaReadH *pReadH, STSma *param, STimeWindow *queryWin); -static bool tsdbSetAndOpenTSmaFile(STSmaReadH *pReadH, STSma *param, STimeWindow *queryWin); +static int64_t tsdbGetIntervalByPrecision(int64_t interval, uint8_t intervalUnit, int8_t precision); +static int32_t tsdbSetTSmaDataFile(STSmaWriteH *pSmaH, STSmaDataWrapper *pData, int32_t storageLevel, int32_t fid); + +static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSmaDataWrapper *pData); +static int32_t tsdbInitTSmaFile(STSmaReadH *pReadH, STimeWindow *queryWin); +static bool tsdbSetAndOpenTSmaFile(STSmaReadH *pReadH, STimeWindow *queryWin); static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat) { ASSERT(pSmaStat != NULL); @@ -133,10 +130,10 @@ int32_t tsdbDestroySmaState(SSmaStat *pSmaStat) { // TODO: use taosHashSetFreeFp when taosHashSetFreeFp is ready. SSmaStatItem *item = taosHashIterate(pSmaStat->smaStatItems, NULL); while (item != NULL) { + tfree(item->pSma); taosHashCleanup(item->expiredWindows); item = taosHashIterate(pSmaStat->smaStatItems, item); } - taosHashCleanup(pSmaStat->smaStatItems); free(pSmaStat); } @@ -154,9 +151,13 @@ int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg) { return TSDB_CODE_FAILED; } - tsdbInitSmaStat(&pTsdb->pSmaStat); // lazy mode + // lazy mode + if (tsdbInitSmaStat(&pTsdb->pSmaStat) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_FAILED; + } // TODO: decode the msg => start + int64_t indexUid = SMA_TEST_INDEX_UID; const char * indexName = SMA_TEST_INDEX_NAME; const int32_t SMA_TEST_EXPIRED_WINDOW_SIZE = 10; TSKEY expiredWindows[SMA_TEST_EXPIRED_WINDOW_SIZE]; @@ -169,14 +170,24 @@ int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg) { SHashObj *pItemsHash = pTsdb->pSmaStat->smaStatItems; SSmaStatItem *pItem = (SSmaStatItem *)taosHashGet(pItemsHash, indexName, strlen(indexName)); - if (!pItem) { + if (pItem == NULL) { pItem = tsdbNewSmaStatItem(TSDB_SMA_STAT_EXPIRED); // TODO use the real state - if (!pItem) { + if (pItem == NULL) { // Response to stream computing: OOM // For query, if the indexName not found, the TSDB should tell query module to query raw TS data. return TSDB_CODE_FAILED; } + // cache smaMeta + STSma *pSma = metaGetSmaInfoByIndex(pTsdb->pMeta, indexUid); + if (pSma == NULL) { + taosHashCleanup(pItem->expiredWindows); + free(pItem); + return TSDB_CODE_FAILED; + } + pItem->pSma = pSma; + + // TODO: change indexName to indexUid if (taosHashPut(pItemsHash, indexName, strnlen(indexName, TSDB_INDEX_NAME_LEN), &pItem, sizeof(pItem)) != 0) { // If error occurs during put smaStatItem, free the resources of pItem taosHashCleanup(pItem->expiredWindows); @@ -195,6 +206,7 @@ int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg) { // 2) This would solve the inconsistency to some extent, but not completely, unless we record all expired // windows failed to put into hash table. taosHashCleanup(pItem->expiredWindows); + tfree(pItem->pSma); taosHashRemove(pItemsHash, indexName, sizeof(indexName)); return TSDB_CODE_FAILED; } @@ -203,19 +215,21 @@ int32_t tsdbUpdateExpiredWindow(STsdb *pTsdb, char *msg) { return TSDB_CODE_SUCCESS; } -static int32_t tsdbResetExpiredWindow(STsdb *pTsdb, const char *indexName, void *timeWindow) { +static int32_t tsdbResetExpiredWindow(STsdb *pTsdb, int64_t indexUid, TSKEY skey) { SSmaStatItem *pItem = NULL; if (pTsdb->pSmaStat && pTsdb->pSmaStat->smaStatItems) { - pItem = (SSmaStatItem *)taosHashGet(pTsdb->pSmaStat->smaStatItems, indexName, strlen(indexName)); + pItem = (SSmaStatItem *)taosHashGet(pTsdb->pSmaStat->smaStatItems, &indexUid, sizeof(indexUid)); } if (pItem != NULL) { - // TODO: reset time windows for the sma data blocks - while (true) { - TSKEY thisWindow = 0; - taosHashRemove(pItem->expiredWindows, &thisWindow, sizeof(thisWindow)); + // TODO: reset time window for the sma data blocks + if (taosHashRemove(pItem->expiredWindows, &skey, sizeof(TSKEY)) != 0) { + // error handling } + + } else { + // error handling } return TSDB_CODE_SUCCESS; } @@ -277,7 +291,7 @@ static int32_t tsdbJudgeStorageLevel(int64_t interval, int8_t intervalUnit) { */ static int32_t tsdbInsertTSmaBlocks(void *bTree, const char *smaKey, const char *pData, int32_t dataLen) { // TODO: insert sma data blocks into B+Tree - printf("insert sma data blocks into B+Tree: smaKey %" PRIx64 "-%" PRIu16 "-%" PRIx64 ", dataLen %d\n", + tsdbDebug("insert sma data blocks into B+Tree: smaKey %" PRIx64 "-%" PRIu16 "-%" PRIx64 ", dataLen %d", *(uint64_t *)smaKey, *(uint16_t *)POINTER_SHIFT(smaKey, 8), *(int64_t *)POINTER_SHIFT(smaKey, 10), dataLen); return TSDB_CODE_SUCCESS; } @@ -360,85 +374,60 @@ static int64_t tsdbGetIntervalByPrecision(int64_t interval, uint8_t intervalUnit * @param nBlocks The nBlocks with the same fid since nOffset. * @return int32_t */ -static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaData *pData, int32_t nOffset, int32_t nBlocks) { +static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaDataWrapper *pData) { STsdb *pTsdb = pSmaH->pTsdb; - TASSERT(pData->colIds != NULL); + tsdbDebug("tsdbInsertTSmaDataSection: index %" PRIi64 ", skey %" PRIi64, pData->indexUid, pData->skey); - tsdbDebug("tsdbInsertTSmaDataSection: nOffset %d, nBlocks %d", nOffset, nBlocks); - printf("tsdbInsertTSmaDataSection: nOffset %d, nBlocks %d\n", nOffset, nBlocks); + // TODO: check the data integrity - int32_t colDataLen = pData->dataLen / pData->numOfColIds; - int32_t sectionDataLen = pSmaH->blockSize * nBlocks; + void *bTree = pSmaH->pDFile; - for (col_id_t i = 0; i < pData->numOfColIds; ++i) { - // param: pointer of B+Tree, key, value, dataLen - void *bTree = pSmaH->pDFile; -#ifndef SMA_STORE_SINGLE_BLOCKS - // save tSma data blocks as a whole - char smaKey[SMA_KEY_LEN] = {0}; - void *pSmaKey = &smaKey; - tsdbEncodeTSmaKey(pData->tableUid, *(pData->colIds + i), pData->tsWindow.skey + nOffset * pSmaH->interval, - (void **)&pSmaKey); - if (tsdbInsertTSmaBlocks(bTree, smaKey, pData->data + i * colDataLen + nOffset * pSmaH->blockSize, sectionDataLen) < - 0) { - tsdbWarn("vgId:%d insert tSma blocks failed since %s", REPO_ID(pTsdb), tstrerror(terrno)); + int32_t len = 0; + while (true) { + if (len >= pData->dataLen) { + break; } -#else - // save tSma data blocks separately - for (int32_t n = 0; n < nBlocks; ++n) { - char smaKey[SMA_KEY_LEN] = {0}; - void *pSmaKey = &smaKey; - tsdbEncodeTSmaKey(pData->tableUid, *(pData->colIds + i), pData->tsWindow.skey + (nOffset + n) * pSmaH->interval, - (void **)&pSmaKey); - if (tsdbInsertTSmaBlocks(bTree, smaKey, pData->data + i * colDataLen + (nOffset + n) * pSmaH->blockSize, - pSmaH->blockSize) < 0) { + assert(pData->dataLen > 0); + STSmaTbData *pTbData = (STSmaTbData *)POINTER_SHIFT(pData->data, len); + + int32_t tbLen = 0; + while (true) { + if (tbLen >= pTbData->dataLen) { + break; + } + assert(pTbData->dataLen > 0); + STSmaColData *pColData = (STSmaColData *)POINTER_SHIFT(pTbData->data, tbLen); + char smaKey[SMA_KEY_LEN] = {0}; + void * pSmaKey = &smaKey; +#if 0 + printf("tsdbInsertTSmaDataSection: index %" PRIi64 ", skey %" PRIi64 " table[%" PRIi64 "]col[%" PRIu16 "]\n", + pData->indexUid, pData->skey, pTbData->tableUid, pColData->colId); +#endif + tsdbEncodeTSmaKey(pTbData->tableUid, pColData->colId, pData->skey, (void **)&pSmaKey); + if (tsdbInsertTSmaBlocks(bTree, smaKey, pColData->data, pColData->blockSize) < 0) { tsdbWarn("vgId:%d insert tSma blocks failed since %s", REPO_ID(pTsdb), tstrerror(terrno)); } + tbLen += (sizeof(STSmaColData) + pColData->blockSize); } -#endif + len += (sizeof(STSmaTbData) + pTbData->dataLen); } + return TSDB_CODE_SUCCESS; } -static int32_t tsdbInitTSmaWriteH(STSmaWriteH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData) { +static int32_t tsdbInitTSmaWriteH(STSmaWriteH *pSmaH, STsdb *pTsdb, STSmaDataWrapper *pData) { pSmaH->pTsdb = pTsdb; - pSmaH->interval = tsdbGetIntervalByPrecision(param->interval, param->intervalUnit, REPO_CFG(pTsdb)->precision); - // pSmaH->blockSize = param->numOfFuncIds * sizeof(int64_t); + pSmaH->interval = tsdbGetIntervalByPrecision(pData->interval, pData->intervalUnit, REPO_CFG(pTsdb)->precision); } -static int32_t tsdbSetTSmaDataFile(STSmaWriteH *pSmaH, STSma *param, STSmaData *pData, int32_t storageLevel, - int32_t fid) { +static int32_t tsdbSetTSmaDataFile(STSmaWriteH *pSmaH, STSmaDataWrapper *pData, int32_t storageLevel, int32_t fid) { // TODO pSmaH->pDFile = "tSma_interval_file_name"; return TSDB_CODE_SUCCESS; -} /** - * @brief Split the sma data blocks by fid. - * - * @param pSmaH - * @param param - * @param pData - * @param nOffset - * @param fid - * @param nSmaBlocks - * @return int32_t - */ -static int32_t tsdbTSmaDataSplit(STSmaWriteH *pSmaH, STSma *param, STSmaData *pData, int32_t days, int32_t nOffset, - int32_t fid, int32_t *nSmaBlocks) { - STsdbCfg *pCfg = REPO_CFG(pSmaH->pTsdb); +} - // TODO: use binary search - for (int32_t n = nOffset + 1; n < pData->numOfBlocks; ++n) { - // TODO: The tsWindow.skey should use the precision of DB. - int32_t tFid = (int32_t)(TSDB_KEY_FID(pData->tsWindow.skey + pSmaH->interval * n, days, pCfg->precision)); - if (tFid > fid) { - *nSmaBlocks = n - nOffset; - break; - } - } - return TSDB_CODE_SUCCESS; -} /** * @brief Insert/Update Time-range-wise SMA data. @@ -449,124 +438,81 @@ static int32_t tsdbTSmaDataSplit(STSmaWriteH *pSmaH, STSma *param, STSmaData *pD * - The destination file of one data block for some interval is determined by its start TS key. * * @param pTsdb - * @param param - * @param pData + * @param msg * @return int32_t */ -int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData) { +int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) { STsdbCfg * pCfg = REPO_CFG(pTsdb); - STSmaData * curData = pData; + STSmaDataWrapper *pData = (STSmaDataWrapper *)msg; STSmaWriteH tSmaH = {0}; - tsdbInitTSmaWriteH(&tSmaH, pTsdb, param, pData); + tsdbInitTSmaWriteH(&tSmaH, pTsdb, pData); - if (pData->numOfBlocks <= 0 || pData->numOfColIds <= 0 || pData->dataLen <= 0) { + if (pData->dataLen <= 0) { TASSERT(0); terrno = TSDB_CODE_INVALID_PARA; return terrno; } // Step 1: Judge the storage level - int32_t storageLevel = tsdbJudgeStorageLevel(param->interval, param->intervalUnit); + int32_t storageLevel = tsdbJudgeStorageLevel(pData->interval, pData->intervalUnit); int32_t daysPerFile = storageLevel == SMA_STORAGE_LEVEL_TSDB ? SMA_STORAGE_TSDB_DAYS : pCfg->daysPerFile; // Step 2: Set the DFile for storage of SMA index, and iterate/split the TSma data and store to B+Tree index file // - Set and open the DFile or the B+Tree file - int32_t minFid = (int32_t)(TSDB_KEY_FID(pData->tsWindow.skey, daysPerFile, pCfg->precision)); - int32_t maxFid = (int32_t)(TSDB_KEY_FID(pData->tsWindow.ekey, daysPerFile, pCfg->precision)); + int32_t fid = (int32_t)(TSDB_KEY_FID(pData->skey, daysPerFile, pCfg->precision)); - if (minFid == maxFid) { - // Save all the TSma data to one file - // TODO: tsdbStartTSmaCommit(); - tsdbSetTSmaDataFile(&tSmaH, param, pData, storageLevel, minFid); - tsdbInsertTSmaDataSection(&tSmaH, pData, 0, pData->numOfBlocks); - // TODO:tsdbEndTSmaCommit(); - } else if (minFid < maxFid) { - // Split the TSma data and save to multiple files. As there is limit for the span, it can't span more than 2 files - // actually. - // TODO: tsdbStartTSmaCommit(); - int32_t tFid = minFid; - int32_t nOffset = 0; - int32_t nSmaBlocks = 0; - do { - tsdbTSmaDataSplit(&tSmaH, param, pData, daysPerFile, nOffset, tFid, &nSmaBlocks); - tsdbSetTSmaDataFile(&tSmaH, param, pData, storageLevel, tFid); - if (tsdbInsertTSmaDataSection(&tSmaH, pData, nOffset, nSmaBlocks) < 0) { - return terrno; - } - - ++tFid; - nOffset += nSmaBlocks; - - if (tFid == maxFid) { - tsdbSetTSmaDataFile(&tSmaH, param, pData, storageLevel, tFid); - tsdbInsertTSmaDataSection(&tSmaH, pData, nOffset, pData->numOfBlocks - nOffset); - break; - } - } while (true); - - // TODO:tsdbEndTSmaCommit(); - } else { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; - } + // Save all the TSma data to one file + // TODO: tsdbStartTSmaCommit(); + tsdbSetTSmaDataFile(&tSmaH, pData, storageLevel, fid); + tsdbInsertTSmaDataSection(&tSmaH, pData); + // TODO:tsdbEndTSmaCommit(); // reset the SSmaStat - tsdbResetExpiredWindow(pTsdb, param->indexName, &pData->tsWindow); + tsdbResetExpiredWindow(pTsdb, pData->indexUid, pData->skey); return TSDB_CODE_SUCCESS; } -static int32_t tsdbSetRSmaDataFile(STSmaWriteH *pSmaH, SRSma *param, STSmaData *pData, int32_t fid) { +static int32_t tsdbSetRSmaDataFile(STSmaWriteH *pSmaH, STSmaDataWrapper *pData, int32_t fid) { // TODO pSmaH->pDFile = "rSma_interval_file_name"; return TSDB_CODE_SUCCESS; } -int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, SRSma *param, STSmaData *pData) { +int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, char *msg) { STsdbCfg * pCfg = REPO_CFG(pTsdb); - STSma * tParam = ¶m->tsma; - STSmaData * curData = pData; + STSmaDataWrapper *pData = (STSmaDataWrapper *)msg; STSmaWriteH tSmaH = {0}; - tsdbInitTSmaWriteH(&tSmaH, pTsdb, tParam, pData); + tsdbInitTSmaWriteH(&tSmaH, pTsdb, pData); - int32_t nSmaBlocks = pData->numOfBlocks; - int32_t colDataLen = pData->dataLen / nSmaBlocks; - - // Step 2.2: Storage of SMA_STORAGE_LEVEL_DFILESET - // TODO: Use the daysPerFile for rSma data, not for TS data. - // TODO: The lifecycle of rSma data should be processed like the TS data files. - int32_t minFid = (int32_t)(TSDB_KEY_FID(pData->tsWindow.skey, pCfg->daysPerFile, pCfg->precision)); - int32_t maxFid = (int32_t)(TSDB_KEY_FID(pData->tsWindow.ekey, pCfg->daysPerFile, pCfg->precision)); - - if (minFid == maxFid) { - // Save all the TSma data to one file - tsdbSetRSmaDataFile(&tSmaH, param, pData, minFid); - // TODO: tsdbStartTSmaCommit(); - tsdbInsertTSmaDataSection(&tSmaH, pData, colDataLen, nSmaBlocks); - // TODO:tsdbEndTSmaCommit(); - } else if (minFid < maxFid) { - // Split the TSma data and save to multiple files. As there is limit for the span, it can't span more than 2 files - // actually. - // TODO: tsdbStartTSmaCommit(); - int32_t tmpFid = 0; - int32_t step = 0; - for (int32_t n = 0; n < pData->numOfBlocks; ++n) { - } - tsdbInsertTSmaDataSection(&tSmaH, pData, colDataLen, nSmaBlocks); - // TODO:tsdbEndTSmaCommit(); - } else { + if (pData->dataLen <= 0) { TASSERT(0); - return TSDB_CODE_INVALID_PARA; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } - // reset the SSmaStat - tsdbResetExpiredWindow(pTsdb, param->tsma.indexName, &pData->tsWindow); + // Step 1: Judge the storage level + int32_t storageLevel = tsdbJudgeStorageLevel(pData->interval, pData->intervalUnit); + int32_t daysPerFile = storageLevel == SMA_STORAGE_LEVEL_TSDB ? SMA_STORAGE_TSDB_DAYS : pCfg->daysPerFile; + + // Step 2: Set the DFile for storage of SMA index, and iterate/split the TSma data and store to B+Tree index file + // - Set and open the DFile or the B+Tree file + + int32_t fid = (int32_t)(TSDB_KEY_FID(pData->skey, daysPerFile, pCfg->precision)); + + // Save all the TSma data to one file + // TODO: tsdbStartTSmaCommit(); + tsdbSetTSmaDataFile(&tSmaH, pData, storageLevel, fid); + tsdbInsertTSmaDataSection(&tSmaH, pData); + // TODO:tsdbEndTSmaCommit(); + + // reset the SSmaStat + tsdbResetExpiredWindow(pTsdb, pData->indexUid, pData->skey); - // Step 4: finish return TSDB_CODE_SUCCESS; } @@ -579,9 +525,9 @@ int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, SRSma *param, STSmaData *pData) { * @param pData * @return int32_t */ -static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSma *param, STSmaData *pData) { +static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSmaDataWrapper *pData) { pSmaH->pTsdb = pTsdb; - pSmaH->interval = tsdbGetIntervalByPrecision(param->interval, param->intervalUnit, REPO_CFG(pTsdb)->precision); + pSmaH->interval = tsdbGetIntervalByPrecision(pData->interval, pData->intervalUnit, REPO_CFG(pTsdb)->precision); // pSmaH->blockSize = param->numOfFuncIds * sizeof(int64_t); } @@ -593,8 +539,8 @@ static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, STSma *param, * @param queryWin * @return int32_t */ -static int32_t tsdbInitTSmaFile(STSmaReadH *pReadH, STSma *param, STimeWindow *queryWin) { - int32_t storageLevel = tsdbJudgeStorageLevel(param->interval, param->intervalUnit); +static int32_t tsdbInitTSmaFile(STSmaReadH *pReadH, STimeWindow *queryWin) { + int32_t storageLevel = 0; //tsdbJudgeStorageLevel(param->interval, param->intervalUnit); int32_t daysPerFile = storageLevel == SMA_STORAGE_LEVEL_TSDB ? SMA_STORAGE_TSDB_DAYS : REPO_CFG(pReadH->pTsdb)->daysPerFile; pReadH->storageLevel = storageLevel; @@ -611,8 +557,8 @@ static int32_t tsdbInitTSmaFile(STSmaReadH *pReadH, STSma *param, STimeWindow *q * @return true * @return false */ -static bool tsdbSetAndOpenTSmaFile(STSmaReadH *pReadH, STSma *param, STimeWindow *queryWin) { - SArray *smaFs = pReadH->pTsdb->fs->cstatus->smaf; +static bool tsdbSetAndOpenTSmaFile(STSmaReadH *pReadH, STimeWindow *queryWin) { + SArray *smaFs = pReadH->pTsdb->fs->cstatus->sf; int32_t nSmaFs = taosArrayGetSize(smaFs); pReadH->pDFile = NULL; @@ -646,10 +592,9 @@ static bool tsdbSetAndOpenTSmaFile(STSmaReadH *pReadH, STSma *param, STimeWindow * @param nMaxResult The query invoker should control the nMaxResult need to return to avoid OOM. * @return int32_t */ -int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeWindow *queryWin, int32_t nMaxResult) { - const char *indexName = param->indexName; - - SSmaStatItem *pItem = (SSmaStatItem *)taosHashGet(pTsdb->pSmaStat->smaStatItems, indexName, strlen(indexName)); +int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSmaDataWrapper *pData, STimeWindow *queryWin, int32_t nMaxResult) { + SSmaStatItem *pItem = + (SSmaStatItem *)taosHashGet(pTsdb->pSmaStat->smaStatItems, &pData->indexUid, sizeof(pData->indexUid)); if (pItem == NULL) { // mark all window as expired and notify query module to query raw TS data. return TSDB_CODE_SUCCESS; @@ -664,9 +609,9 @@ int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeW } STSmaReadH tReadH = {0}; - tsdbInitTSmaReadH(&tReadH, pTsdb, param, pData); + tsdbInitTSmaReadH(&tReadH, pTsdb, pData); - tsdbInitTSmaFile(&tReadH, param, queryWin); + tsdbInitTSmaFile(&tReadH, queryWin); int32_t nResult = 0; int64_t lastKey = 0; @@ -677,7 +622,7 @@ int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeW } // set and open the file according to the STSma param - if (tsdbSetAndOpenTSmaFile(&tReadH, param, queryWin)) { + if (tsdbSetAndOpenTSmaFile(&tReadH, queryWin)) { char bTree[100] = "\0"; while (strncmp(bTree, "has more nodes", 100) == 0) { if (nResult >= nMaxResult) { @@ -694,6 +639,7 @@ int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeW return TSDB_CODE_SUCCESS; } +#if 0 /** * @brief Get the start TS key of the last data block of one interval/sliding. * @@ -704,7 +650,7 @@ int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSma *param, STSmaData *pData, STimeW * 1) Return 0 and fill the result if the check procedure is normal; * 2) Return -1 if error occurs during the check procedure. */ -int32_t tsdbGetTSmaStatus(STsdb *pTsdb, STSma *param, void *result) { +int32_t tsdbGetTSmaStatus(STsdb *pTsdb, void *smaIndex, void *result) { const char *procedure = ""; if (strncmp(procedure, "get the start TS key of the last data block", 100) != 0) { return -1; @@ -721,9 +667,10 @@ int32_t tsdbGetTSmaStatus(STsdb *pTsdb, STSma *param, void *result) { * @param pWin * @return int32_t */ -int32_t tsdbRemoveTSmaData(STsdb *pTsdb, STSma *param, STimeWindow *pWin) { +int32_t tsdbRemoveTSmaData(STsdb *pTsdb, void *smaIndex, STimeWindow *pWin) { // for ("tSmaFiles of param-interval-sliding between pWin") { // // remove the tSmaFile // } return TSDB_CODE_SUCCESS; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/source/dnode/vnode/src/tsdb/tsdbWrite.c b/source/dnode/vnode/src/tsdb/tsdbWrite.c index ba8eea809e..26d31af4f3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbWrite.c +++ b/source/dnode/vnode/src/tsdb/tsdbWrite.c @@ -39,13 +39,13 @@ int tsdbInsertData(STsdb *pTsdb, SSubmitReq *pMsg, SSubmitRsp *pRsp) { * * @param pTsdb * @param param - * @param pData + * @param msg * @return int32_t * TODO: Who is responsible for resource allocate and release? */ -int32_t tsdbInsertTSmaData(STsdb *pTsdb, STSma *param, STSmaData *pData) { +int32_t tsdbInsertTSmaData(STsdb *pTsdb, char *msg) { int32_t code = TSDB_CODE_SUCCESS; - if ((code = tsdbInsertTSmaDataImpl(pTsdb, param, pData)) < 0) { + if ((code = tsdbInsertTSmaDataImpl(pTsdb, msg)) < 0) { tsdbWarn("vgId:%d insert tSma data failed since %s", REPO_ID(pTsdb), tstrerror(terrno)); } return code; @@ -56,12 +56,12 @@ int32_t tsdbInsertTSmaData(STsdb *pTsdb, STSma *param, STSmaData *pData) { * * @param pTsdb * @param param - * @param pData + * @param msg * @return int32_t */ -int32_t tsdbInsertRSmaData(STsdb *pTsdb, SRSma *param, STSmaData *pData) { +int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg) { int32_t code = TSDB_CODE_SUCCESS; - if ((code = tsdbInsertRSmaDataImpl(pTsdb, param, pData)) < 0) { + if ((code = tsdbInsertRSmaDataImpl(pTsdb, msg)) < 0) { tsdbWarn("vgId:%d insert rSma data failed since %s", REPO_ID(pTsdb), tstrerror(terrno)); } return code; diff --git a/source/dnode/vnode/test/tsdbSmaTest.cpp b/source/dnode/vnode/test/tsdbSmaTest.cpp index 5a5c5e1530..ac9a8fd3d0 100644 --- a/source/dnode/vnode/test/tsdbSmaTest.cpp +++ b/source/dnode/vnode/test/tsdbSmaTest.cpp @@ -43,20 +43,8 @@ TEST(testCase, tSmaEncodeDecodeTest) { tSma.sliding = 0; tstrncpy(tSma.indexName, "sma_index_test", TSDB_INDEX_NAME_LEN); tstrncpy(tSma.timezone, "Asia/Shanghai", TD_TIMEZONE_LEN); + tSma.indexUid = 2345678910; tSma.tableUid = 1234567890; - tSma.nFuncColIds = 5; - tSma.funcColIds = (SFuncColIds *)calloc(tSma.nFuncColIds, sizeof(SFuncColIds)); - ASSERT(tSma.funcColIds != NULL); - for (int32_t n = 0; n < tSma.nFuncColIds; ++n) { - SFuncColIds *funcColIds = tSma.funcColIds + n; - funcColIds->funcId = n; - funcColIds->nColIds = 10; - funcColIds->colIds = (col_id_t *)calloc(funcColIds->nColIds, sizeof(col_id_t)); - ASSERT(funcColIds->colIds != NULL); - for (int32_t i = 0; i < funcColIds->nColIds; ++i) { - *(funcColIds->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); - } - } STSmaWrapper tSmaWrapper = {.number = 1, .tSma = &tSma}; uint32_t bufLen = tEncodeTSmaWrapper(NULL, &tSmaWrapper); @@ -85,35 +73,31 @@ TEST(testCase, tSmaEncodeDecodeTest) { EXPECT_EQ(pSma->slidingUnit, qSma->slidingUnit); EXPECT_STRCASEEQ(pSma->indexName, qSma->indexName); EXPECT_STRCASEEQ(pSma->timezone, qSma->timezone); - EXPECT_EQ(pSma->nFuncColIds, qSma->nFuncColIds); + EXPECT_EQ(pSma->indexUid, qSma->indexUid); EXPECT_EQ(pSma->tableUid, qSma->tableUid); EXPECT_EQ(pSma->interval, qSma->interval); EXPECT_EQ(pSma->sliding, qSma->sliding); + EXPECT_EQ(pSma->exprLen, qSma->exprLen); + EXPECT_STRCASEEQ(pSma->expr, qSma->expr); EXPECT_EQ(pSma->tagsFilterLen, qSma->tagsFilterLen); EXPECT_STRCASEEQ(pSma->tagsFilter, qSma->tagsFilter); - for (uint32_t j = 0; j < pSma->nFuncColIds; ++j) { - SFuncColIds *pFuncColIds = pSma->funcColIds + j; - SFuncColIds *qFuncColIds = qSma->funcColIds + j; - EXPECT_EQ(pFuncColIds->funcId, qFuncColIds->funcId); - EXPECT_EQ(pFuncColIds->nColIds, qFuncColIds->nColIds); - for (uint32_t k = 0; k < pFuncColIds->nColIds; ++k) { - EXPECT_EQ(*(pFuncColIds->colIds + k), *(qFuncColIds->colIds + k)); - } - } } // resource release tdDestroyTSma(&tSma); tdDestroyTSmaWrapper(&dstTSmaWrapper); } - +#if 1 TEST(testCase, tSma_DB_Put_Get_Del_Test) { const char * smaIndexName1 = "sma_index_test_1"; const char * smaIndexName2 = "sma_index_test_2"; - const char * timeZone = "Asia/Shanghai"; + const char * timezone = "Asia/Shanghai"; + const char * expr = "select count(a,b, top 20), from table interval 1d, sliding 1h;"; const char * tagsFilter = "I'm tags filter"; const char * smaTestDir = "./smaTest"; - const uint64_t tbUid = 1234567890; + const tb_uid_t tbUid = 1234567890; + const int64_t indexUid1 = 2000000001; + const int64_t indexUid2 = 2000000002; const uint32_t nCntTSma = 2; // encode STSma tSma = {0}; @@ -122,22 +106,15 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { tSma.interval = 1; tSma.slidingUnit = TD_TIME_UNIT_HOUR; tSma.sliding = 0; + tSma.indexUid = indexUid1; tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN); - tstrncpy(tSma.timezone, timeZone, TD_TIMEZONE_LEN); + tstrncpy(tSma.timezone, timezone, TD_TIMEZONE_LEN); tSma.tableUid = tbUid; - tSma.nFuncColIds = 5; - tSma.funcColIds = (SFuncColIds *)calloc(tSma.nFuncColIds, sizeof(SFuncColIds)); - ASSERT(tSma.funcColIds != NULL); - for (int32_t n = 0; n < tSma.nFuncColIds; ++n) { - SFuncColIds *funcColIds = tSma.funcColIds + n; - funcColIds->funcId = n; - funcColIds->nColIds = 10; - funcColIds->colIds = (col_id_t *)calloc(funcColIds->nColIds, sizeof(col_id_t)); - ASSERT(funcColIds->colIds != NULL); - for (int32_t i = 0; i < funcColIds->nColIds; ++i) { - *(funcColIds->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); - } - } + + tSma.exprLen = strlen(expr); + tSma.expr = (char *)calloc(tSma.exprLen + 1, 1); + tstrncpy(tSma.expr, expr, tSma.exprLen + 1); + tSma.tagsFilterLen = strlen(tagsFilter); tSma.tagsFilter = (char *)calloc(tSma.tagsFilterLen + 1, 1); tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1); @@ -151,8 +128,9 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { pMeta = metaOpen(smaTestDir, pMetaCfg, NULL); assert(pMeta != NULL); // save index 1 - metaSaveSmaToDB(pMeta, pSmaCfg); + EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0); + pSmaCfg->indexUid = indexUid2; tstrncpy(pSmaCfg->indexName, smaIndexName2, TSDB_INDEX_NAME_LEN); pSmaCfg->version = 1; pSmaCfg->intervalUnit = TD_TIME_UNIT_HOUR; @@ -161,24 +139,26 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { pSmaCfg->sliding = 5; // save index 2 - metaSaveSmaToDB(pMeta, pSmaCfg); + EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0); // get value by indexName STSma *qSmaCfg = NULL; - qSmaCfg = metaGetSmaInfoByName(pMeta, smaIndexName1); + qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid1); assert(qSmaCfg != NULL); printf("name1 = %s\n", qSmaCfg->indexName); printf("timezone1 = %s\n", qSmaCfg->timezone); + printf("expr1 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : ""); printf("tagsFilter1 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : ""); EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName1); EXPECT_EQ(qSmaCfg->tableUid, tSma.tableUid); tdDestroyTSma(qSmaCfg); tfree(qSmaCfg); - qSmaCfg = metaGetSmaInfoByName(pMeta, smaIndexName2); + qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid2); assert(qSmaCfg != NULL); printf("name2 = %s\n", qSmaCfg->indexName); printf("timezone2 = %s\n", qSmaCfg->timezone); + printf("expr2 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : ""); printf("tagsFilter2 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : ""); EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName2); EXPECT_EQ(qSmaCfg->interval, tSma.interval); @@ -201,17 +181,21 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { metaCloseSmaCurosr(pSmaCur); // get wrapper by table uid - STSmaWrapper *pSW = metaGetSmaInfoByUid(pMeta, tbUid); + STSmaWrapper *pSW = metaGetSmaInfoByTable(pMeta, tbUid); assert(pSW != NULL); EXPECT_EQ(pSW->number, nCntTSma); EXPECT_STRCASEEQ(pSW->tSma->indexName, smaIndexName1); - EXPECT_STRCASEEQ(pSW->tSma->timezone, timeZone); + EXPECT_STRCASEEQ(pSW->tSma->timezone, timezone); + EXPECT_STRCASEEQ(pSW->tSma->expr, expr); EXPECT_STRCASEEQ(pSW->tSma->tagsFilter, tagsFilter); - EXPECT_EQ(pSW->tSma->tableUid, tSma.tableUid); + EXPECT_EQ(pSW->tSma->indexUid, indexUid1); + EXPECT_EQ(pSW->tSma->tableUid, tbUid); EXPECT_STRCASEEQ((pSW->tSma + 1)->indexName, smaIndexName2); - EXPECT_STRCASEEQ((pSW->tSma + 1)->timezone, timeZone); + EXPECT_STRCASEEQ((pSW->tSma + 1)->timezone, timezone); + EXPECT_STRCASEEQ((pSW->tSma + 1)->expr, expr); EXPECT_STRCASEEQ((pSW->tSma + 1)->tagsFilter, tagsFilter); - EXPECT_EQ((pSW->tSma + 1)->tableUid, tSma.tableUid); + EXPECT_EQ((pSW->tSma + 1)->indexUid, indexUid2); + EXPECT_EQ((pSW->tSma + 1)->tableUid, tbUid); tdDestroyTSmaWrapper(pSW); tfree(pSW); @@ -233,44 +217,68 @@ TEST(testCase, tSma_DB_Put_Get_Del_Test) { tdDestroyTSma(&tSma); metaClose(pMeta); } +#endif -#if 0 +#if 1 TEST(testCase, tSmaInsertTest) { - STSma tSma = {0}; - STSmaData *pSmaData = NULL; - STsdb tsdb = {0}; + const int64_t indexUid = 2000000002; + STSmaDataWrapper *pSmaData = NULL; + STsdb tsdb = {0}; + STsdbCfg * pCfg = &tsdb.config; + + pCfg->daysPerFile = 1; // init - tSma.intervalUnit = TD_TIME_UNIT_DAY; - tSma.interval = 1; - tSma.numOfFuncIds = 5; // sum/min/max/avg/last + int32_t allocCnt = 0; + int32_t allocStep = 40960; + int32_t buffer = 4096; + void * buf = NULL; + EXPECT_EQ(tsdbMakeRoom(&buf, allocStep), 0); + int32_t bufSize = taosTSizeof(buf); + int32_t numOfTables = 25; + col_id_t numOfCols = 4096; + EXPECT_GT(numOfCols, 0); - int32_t blockSize = tSma.numOfFuncIds * sizeof(int64_t); - int32_t numOfColIds = 3; - int32_t numOfBlocks = 10; + pSmaData = (STSmaDataWrapper *)buf; + printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData); + pSmaData->skey = 1646987196; + pSmaData->interval = 10; + pSmaData->intervalUnit = TD_TIME_UNIT_MINUTE; + pSmaData->indexUid = indexUid; - int32_t dataLen = numOfColIds * numOfBlocks * blockSize; + int32_t len = sizeof(STSmaDataWrapper); + for (int32_t t = 0; t < numOfTables; ++t) { + STSmaTbData *pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len); + pTbData->tableUid = t; - pSmaData = (STSmaData *)malloc(sizeof(STSmaData) + dataLen); - ASSERT_EQ(pSmaData != NULL, true); - pSmaData->tableUid = 3232329230; - pSmaData->numOfColIds = numOfColIds; - pSmaData->numOfBlocks = numOfBlocks; - pSmaData->dataLen = dataLen; - pSmaData->tsWindow.skey = 1640000000; - pSmaData->tsWindow.ekey = 1645788649; - pSmaData->colIds = (col_id_t *)malloc(sizeof(col_id_t) * numOfColIds); - ASSERT_EQ(pSmaData->colIds != NULL, true); - - for (int32_t i = 0; i < numOfColIds; ++i) { - *(pSmaData->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID); + int32_t tableDataLen = sizeof(STSmaTbData); + for (col_id_t c = 0; c < numOfCols; ++c) { + if (bufSize - len - tableDataLen < buffer) { + EXPECT_EQ(tsdbMakeRoom(&buf, bufSize + allocStep), 0); + pSmaData = (STSmaDataWrapper *)buf; + pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len); + bufSize = taosTSizeof(buf); + printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData); + } + STSmaColData *pColData = (STSmaColData *)POINTER_SHIFT(pSmaData, len + tableDataLen); + pColData->colId = c + PRIMARYKEY_TIMESTAMP_COL_ID; + pColData->blockSize = ((c & 1) == 0) ? 8 : 16; + // TODO: fill col data + tableDataLen += (sizeof(STSmaColData) + pColData->blockSize); + } + pTbData->dataLen = (tableDataLen - sizeof(STSmaTbData)); + len += tableDataLen; + // printf("bufSize=%d, len=%d, len of table[%d]=%d\n", bufSize, len, t, tableDataLen); } + pSmaData->dataLen = (len - sizeof(STSmaDataWrapper)); + + EXPECT_GE(bufSize, pSmaData->dataLen); // execute - EXPECT_EQ(tsdbInsertTSmaData(&tsdb, &tSma, pSmaData), TSDB_CODE_SUCCESS); + EXPECT_EQ(tsdbInsertTSmaData(&tsdb, (char *)pSmaData), TSDB_CODE_SUCCESS); // release - tdDestroySmaData(pSmaData); + taosTZfree(buf); } #endif diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index fb926610bd..b565a512f3 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -265,7 +265,7 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { return NULL; } #if FILE_WITH_LOCK - pthread_rwlock_init(&(pFile->rwlock),NULL); + pthread_rwlock_init(&(pFile->rwlock), NULL); #endif pFile->fd = fd; pFile->fp = fp; From ba8d9a798e971811fe17a5ca029250e2ee358967 Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Fri, 11 Mar 2022 19:39:20 +0800 Subject: [PATCH 22/29] Feature/sangshuduo/td 13063 3.0 windows (#10708) * [TD-13063]: 3.0 on Windows * add pthread in contrib * fix linux compile * fix osSemaphore * add gnu regex for Windows * fix compile error for Windows --- CMakeLists.txt | 33 +++------------------ cmake/cmake.define | 42 ++++++++++++++++++++++++++ cmake/cmake.options | 42 ++++++++++++++++++++++---- cmake/cmake.platform | 51 ++++++++++++++++++++++++++++++++ cmake/gnuregex_CMakeLists.txt.in | 13 ++++++++ cmake/pthread_CMakeLists.txt.in | 13 ++++++++ contrib/CMakeLists.txt | 16 +++++++++- include/common/tmsg.h | 5 ++++ include/common/trow.h | 3 +- include/os/os.h | 24 +++++++++------ include/os/osDef.h | 47 ++++++++++++++++++++++++----- include/os/osFile.h | 15 ++++++++++ include/os/osSemaphore.h | 5 ++-- include/os/osSocket.h | 2 +- include/os/osSysinfo.h | 7 +++++ include/os/osTime.h | 3 ++ source/common/CMakeLists.txt | 4 +++ source/os/CMakeLists.txt | 4 ++- source/os/src/osFile.c | 10 ++++++- source/os/src/osString.c | 3 +- source/os/src/osSysinfo.c | 15 +++++----- source/os/src/osSystem.c | 13 ++++---- source/util/CMakeLists.txt | 4 +++ source/util/src/tpagedbuf.c | 2 +- source/util/src/ttimer.c | 2 ++ 25 files changed, 306 insertions(+), 72 deletions(-) create mode 100644 cmake/cmake.define create mode 100644 cmake/cmake.platform create mode 100644 cmake/gnuregex_CMakeLists.txt.in create mode 100644 cmake/pthread_CMakeLists.txt.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d64966861..2e37a14143 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,40 +6,14 @@ project( DESCRIPTION "An open-source big data platform designed and optimized for the Internet of Things(IOT)" ) -IF ("${BUILD_TOOLS}" STREQUAL "") - IF (TD_LINUX) - IF (TD_ARM_32) - SET(BUILD_TOOLS "false") - ELSEIF (TD_ARM_64) - SET(BUILD_TOOLS "false") - ELSE () - SET(BUILD_TOOLS "false") - ENDIF () - ELSEIF (TD_DARWIN) - SET(BUILD_TOOLS "false") - ELSE () - SET(BUILD_TOOLS "false") - ENDIF () -ENDIF () - -IF ("${BUILD_TOOLS}" MATCHES "false") - MESSAGE("${Yellow} Will _not_ build taos_tools! ${ColourReset}") - SET(TD_TAOS_TOOLS FALSE) -ELSE () - MESSAGE("") - MESSAGE("${Green} Will build taos_tools! ${ColourReset}") - MESSAGE("") - SET(TD_TAOS_TOOLS TRUE) -ENDIF () - set(CMAKE_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/cmake") set(CMAKE_CONTRIB_DIR "${CMAKE_SOURCE_DIR}/contrib") + +include(${CMAKE_SUPPORT_DIR}/cmake.platform) +include(${CMAKE_SUPPORT_DIR}/cmake.define) include(${CMAKE_SUPPORT_DIR}/cmake.options) include(${CMAKE_SUPPORT_DIR}/cmake.version) -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -fPIC -gdwarf-2 -msse4.2 -mfma -g3") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -fPIC -gdwarf-2 -msse4.2 -mfma -g3") - # contrib add_subdirectory(contrib) @@ -52,6 +26,7 @@ if(${BUILD_TEST}) include(CTest) enable_testing() endif(${BUILD_TEST}) + add_subdirectory(source) add_subdirectory(tools) add_subdirectory(tests) diff --git a/cmake/cmake.define b/cmake/cmake.define new file mode 100644 index 0000000000..b97c10a4b7 --- /dev/null +++ b/cmake/cmake.define @@ -0,0 +1,42 @@ +cmake_minimum_required(VERSION 3.16) + +IF ("${BUILD_TOOLS}" STREQUAL "") + IF (TD_LINUX) + IF (TD_ARM_32) + SET(BUILD_TOOLS "false") + ELSEIF (TD_ARM_64) + SET(BUILD_TOOLS "false") + ELSE () + SET(BUILD_TOOLS "false") + ENDIF () + ELSEIF (TD_DARWIN) + SET(BUILD_TOOLS "false") + ELSE () + SET(BUILD_TOOLS "false") + ENDIF () +ENDIF () + +IF ("${BUILD_TOOLS}" MATCHES "false") + MESSAGE("${Yellow} Will _not_ build taos_tools! ${ColourReset}") + SET(TD_TAOS_TOOLS FALSE) +ELSE () + MESSAGE("") + MESSAGE("${Green} Will build taos_tools! ${ColourReset}") + MESSAGE("") + SET(TD_TAOS_TOOLS TRUE) +ENDIF () + +IF (TD_WINDOWS) + MESSAGE("${Yellow} set compiler flag for Windows! ${ColourReset}") + SET(CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE) + SET(COMMON_FLAGS "/nologo /WX /wd4018 /wd4999 /Oi /Oy- /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Gd /errorReport:prompt /analyze-") + + IF (MSVC AND (MSVC_VERSION GREATER_EQUAL 1900)) + SET(COMMON_FLAGS "${COMMON_FLAGS} /Wv:18") + ENDIF () + +ELSE () + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -fPIC -gdwarf-2 -msse4.2 -mfma -g3") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -fPIC -gdwarf-2 -msse4.2 -mfma -g3") + +ENDIF () diff --git a/cmake/cmake.options b/cmake/cmake.options index e19c10f6b2..1a1a5b5d78 100644 --- a/cmake/cmake.options +++ b/cmake/cmake.options @@ -1,12 +1,35 @@ # ========================================================= # Deps options # ========================================================= + +IF(${TD_WINDOWS}) + + MESSAGE("build pthread Win32") + option( + BUILD_PTHREAD + "If build pthread on Windows" + ON + ) + + MESSAGE("build gnu regex for Windows") + option( + BUILD_GNUREGEX + "If build gnu regex on Windows" + ON + ) + +ENDIF () + +IF(${TD_LINUX} MATCHES TRUE) + option( BUILD_TEST "If build unit tests using googletest" ON ) +ENDIF () + option( BUILD_WITH_LEVELDB "If build with leveldb" @@ -25,11 +48,16 @@ option( OFF ) -option( - BUILD_WITH_BDB - "If build with BerkleyDB" - ON -) +IF(${TD_WINDOWS}) + MESSAGE("Not build BDB on Windows") +ELSE () + option( + BUILD_WITH_BDB + "If build with BerkleyDB" + ON + ) + +ENDIF () option( BUILD_WITH_LUCENE @@ -68,12 +96,16 @@ option( OFF ) +IF(${TD_LINUX} MATCHES TRUE) + option( BUILD_DEPENDENCY_TESTS "If build dependency tests" ON ) +ENDIF () + option( BUILD_DOCS "If use doxygen build documents" diff --git a/cmake/cmake.platform b/cmake/cmake.platform new file mode 100644 index 0000000000..7ef259ba54 --- /dev/null +++ b/cmake/cmake.platform @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.16) + +MESSAGE("Current system is ${CMAKE_SYSTEM_NAME}") + +# init +SET(TD_LINUX FALSE) +SET(TD_WINDOWS FALSE) +SET(TD_DARWIN FALSE) + +IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + + SET(TD_LINUX TRUE) + SET(OSTYPE "Linux") + ADD_DEFINITIONS("-DLINUX") + + IF (${CMAKE_SIZEOF_VOID_P} MATCHES 8) + SET(TD_LINUX_64 TRUE) + ELSE () + SET(TD_LINUX_32 TRUE) + ENDIF () + +ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + + SET(TD_DARWIN TRUE) + SET(OSTYPE "macOS") + ADD_DEFINITIONS("-DDARWIN") + IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64") + MESSAGE("Current system arch is arm64") + SET(TD_DARWIN_64 TRUE) + ADD_DEFINITIONS("-D_TD_DARWIN_64") + ENDIF () + + ADD_DEFINITIONS("-DHAVE_UNISTD_H") + +ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Windows") + + SET(TD_WINDOWS TRUE) + SET(OSTYPE "Windows") + ADD_DEFINITIONS("-DWINDOWS") + + IF (${CMAKE_SIZEOF_VOID_P} MATCHES 8) + SET(TD_WINDOWS_64 TRUE) + ADD_DEFINITIONS("-D_TD_WINDOWS_64") + ELSE () + SET(TD_WINDOWS_32 TRUE) + ADD_DEFINITIONS("-D_TD_WINDOWS_32") + ENDIF () +ENDIF() + +MESSAGE("C Compiler ID: ${CMAKE_C_COMPILER_ID}") +MESSAGE("CXX Compiler ID: ${CMAKE_CXX_COMPILER_ID}") diff --git a/cmake/gnuregex_CMakeLists.txt.in b/cmake/gnuregex_CMakeLists.txt.in new file mode 100644 index 0000000000..e0c07fe3ba --- /dev/null +++ b/cmake/gnuregex_CMakeLists.txt.in @@ -0,0 +1,13 @@ + +# gnuregex +ExternalProject_Add(gnuregex + URL https://launchpad.net/gnuregex/trunk/2.9/+download/libgnurx-src-2.9.zip + DOWNLOAD_NAME libgnurx-src.zip + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/gnuregex" + BINARY_DIR "" + #BUILD_IN_SOURCE TRUE + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/cmake/pthread_CMakeLists.txt.in b/cmake/pthread_CMakeLists.txt.in new file mode 100644 index 0000000000..ee6d069155 --- /dev/null +++ b/cmake/pthread_CMakeLists.txt.in @@ -0,0 +1,13 @@ + +# pthread +ExternalProject_Add(pthread + GIT_REPOSITORY https://github.com/GerHobbelt/pthread-win32 + GIT_TAG v3.0.3.1 + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/pthread-win32" + BINARY_DIR "" + #BUILD_IN_SOURCE TRUE + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index 21b8b661df..5e2bfc52e1 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -9,6 +9,16 @@ endfunction(cat IN_FILE OUT_FILE) set(CONTRIB_TMP_FILE "${CMAKE_BINARY_DIR}/deps_tmp_CMakeLists.txt.in") configure_file("${CMAKE_SUPPORT_DIR}/deps_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +# pthread +if(${BUILD_PTHREAD}) + cat("${CMAKE_SUPPORT_DIR}/pthread_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif() + +# gnu regex +if(${BUILD_GNUREGEX}) + cat("${CMAKE_SUPPORT_DIR}/gnuregex_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif() + # googletest if(${BUILD_TEST}) cat("${CMAKE_SUPPORT_DIR}/gtest_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) @@ -193,7 +203,10 @@ endif(${BUILD_WITH_TRAFT}) # LIBUV if(${BUILD_WITH_UV}) - add_compile_options(-Wno-sign-compare) + if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows") + MESSAGE("Windows need set no-sign-compare") + add_compile_options(-Wno-sign-compare) + endif () add_subdirectory(libuv) endif(${BUILD_WITH_UV}) @@ -224,6 +237,7 @@ if(${BUILD_WITH_SQLITE}) ) endif(${BUILD_WITH_SQLITE}) +# pthread # ================================================================================================ diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 0dcf554433..85ce0d0cd3 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1356,6 +1356,7 @@ typedef struct SVCreateTbReq { } SVCreateTbReq, SVUpdateTbReq; typedef struct { + int tmp; // TODO: to avoid compile error } SVCreateTbRsp, SVUpdateTbRsp; int32_t tSerializeSVCreateTbReq(void** buf, SVCreateTbReq* pReq); @@ -1367,6 +1368,7 @@ typedef struct { } SVCreateTbBatchReq; typedef struct { + int tmp; // TODO: to avoid compile error } SVCreateTbBatchRsp; int32_t tSerializeSVCreateTbBatchReq(void** buf, SVCreateTbBatchReq* pReq); @@ -1380,6 +1382,7 @@ typedef struct { } SVDropTbReq; typedef struct { + int tmp; // TODO: to avoid compile error } SVDropTbRsp; int32_t tSerializeSVDropTbReq(void** buf, SVDropTbReq* pReq); @@ -1930,7 +1933,9 @@ typedef struct { int64_t ver; // use a general definition char indexName[TSDB_INDEX_NAME_LEN]; } SVDropTSmaReq; + typedef struct { + int tmp; // TODO: to avoid compile error } SVCreateTSmaRsp, SVDropTSmaRsp; int32_t tSerializeSVCreateTSmaReq(void** buf, SVCreateTSmaReq* pReq); diff --git a/include/common/trow.h b/include/common/trow.h index ef30796d78..4dd3daba4d 100644 --- a/include/common/trow.h +++ b/include/common/trow.h @@ -103,6 +103,7 @@ typedef struct { typedef struct { // TODO + int tmp; // TODO: to avoid compile error } STpRow; // tuple #pragma pack(push, 1) @@ -1098,4 +1099,4 @@ const STSRow *tRowBatchIterNext(STSRowBatchIter *pRowBatchIter); } #endif -#endif /*_TD_COMMON_ROW_H_*/ \ No newline at end of file +#endif /*_TD_COMMON_ROW_H_*/ diff --git a/include/os/os.h b/include/os/os.h index a58e798d38..fd272b15a3 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -22,7 +22,22 @@ extern "C" { #include #include + +#if !defined(WINDOWS) +#include #include +#include +#include +#include +#include + +#include +#include +#include +#include + +#endif + #include #include #include @@ -30,8 +45,6 @@ extern "C" { #include #include #include -#include -#include #include #include #include @@ -43,16 +56,9 @@ extern "C" { #include #include #include -#include -#include -#include #include #include -#include -#include -#include -#include #include "osAtomic.h" #include "osDef.h" diff --git a/include/os/osDef.h b/include/os/osDef.h index 7df9ad39bb..fcc96c19b4 100644 --- a/include/os/osDef.h +++ b/include/os/osDef.h @@ -49,7 +49,7 @@ extern "C" { #endif -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) char *stpcpy (char *dest, const char *src); char *stpncpy (char *dest, const char *src, size_t n); @@ -63,14 +63,46 @@ extern "C" { #define strtok_r strtok_s #define snprintf _snprintf #define in_addr_t unsigned long - #define socklen_t int +// #define socklen_t int char * strsep(char **stringp, const char *delim); char * getpass(const char *prefix); char * strndup(const char *s, size_t n); - -#endif + int gettimeofday(struct timeval *ptv, void *pTimeZone); + // for send function in tsocket.c + #define MSG_NOSIGNAL 0 + #define SO_NO_CHECK 0x1234 + #define SOL_TCP 0x1234 + + #define SHUT_RDWR SD_BOTH + #define SHUT_RD SD_RECEIVE + #define SHUT_WR SD_SEND + + #define LOCK_EX 1 + #define LOCK_NB 2 + #define LOCK_UN 3 + + #ifndef PATH_MAX + #define PATH_MAX 256 + #endif + + typedef struct { + int we_wordc; + char *we_wordv[1]; + int we_offs; + char wordPos[1025]; + } wordexp_t; + int wordexp(char *words, wordexp_t *pwordexp, int flags); + void wordfree(wordexp_t *pwordexp); + + #define openlog(a, b, c) + #define closelog() + #define LOG_ERR 0 + #define LOG_INFO 1 + void syslog(int unused, const char *format, ...); +#endif // WINDOWS + #ifndef WINDOWS #ifndef O_BINARY #define O_BINARY 0 @@ -164,7 +196,7 @@ extern "C" { #define PRIzu "zu" #endif -#if defined(_TD_LINUX_64) || defined(_TD_LINUX_32) || defined(_TD_MIPS_64) || defined(_TD_ARM_32) || defined(_TD_ARM_64) || defined(_TD_DARWIN_64) +#if !defined(WINDOWS) #if defined(_TD_DARWIN_64) // MacOS #if !defined(_GNU_SOURCE) @@ -179,8 +211,7 @@ extern "C" { #endif #else // Windows -// #define setThreadName(name) -#define setThreadName(name) do { prctl(PR_SET_NAME, (name)); } while (0) + #define setThreadName(name) #endif #if defined(_WIN32) @@ -197,4 +228,4 @@ extern "C" { } #endif -#endif /*_TD_OS_DEF_H_*/ \ No newline at end of file +#endif /*_TD_OS_DEF_H_*/ diff --git a/include/os/osFile.h b/include/os/osFile.h index 703ba196ef..b0e97e7b54 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -22,6 +22,15 @@ extern "C" { #include "osSocket.h" +#if defined(WINDOWS) +typedef int32_t FileFd; +typedef SOCKET SocketFd; +#else +typedef int32_t FileFd; +typedef int32_t SocketFd; +#endif + +int64_t taosRead(FileFd fd, void *buf, int64_t count); // If the error is in a third-party library, place this header file under the third-party library header file. #ifndef ALLOW_FORBID_FUNC #define open OPEN_FUNC_TAOS_FORBID @@ -76,7 +85,13 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count); int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset); int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count); void taosFprintfFile(TdFilePtr pFile, const char *format, ...); + +#if defined(WINDOWS) +#define __restrict__ +#endif // WINDOWS + int64_t taosGetLineFile(TdFilePtr pFile, char ** __restrict__ ptrBuf); + int32_t taosEOFFile(TdFilePtr pFile); int64_t taosCloseFile(TdFilePtr *ppFile); diff --git a/include/os/osSemaphore.h b/include/os/osSemaphore.h index 7fb9a2202d..9904170d61 100644 --- a/include/os/osSemaphore.h +++ b/include/os/osSemaphore.h @@ -16,12 +16,13 @@ #ifndef _TD_OS_SEMPHONE_H_ #define _TD_OS_SEMPHONE_H_ -#include - #ifdef __cplusplus extern "C" { #endif +#include +#include + #if defined (_TD_DARWIN_64) typedef struct tsem_s *tsem_t; int tsem_init(tsem_t *sem, int pshared, unsigned int value); diff --git a/include/os/osSocket.h b/include/os/osSocket.h index 6544c89548..3faed855ba 100644 --- a/include/os/osSocket.h +++ b/include/os/osSocket.h @@ -27,7 +27,7 @@ #define epoll_wait EPOLL_WAIT_FUNC_TAOS_FORBID #endif -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) #include "winsock2.h" #include #include diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index aec63f3322..d7cf05a594 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -52,6 +52,13 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen); char *taosGetCmdlineByPID(int32_t pid); void taosSetCoreDump(bool enable); +#if defined(WINDOWS) + +#define _UTSNAME_LENGTH 65 +#define _UTSNAME_MACHINE_LENGTH _UTSNAME_LENGTH + +#endif // WINDOWS + typedef struct { char sysname[_UTSNAME_MACHINE_LENGTH]; char nodename[_UTSNAME_MACHINE_LENGTH]; diff --git a/include/os/osTime.h b/include/os/osTime.h index 4718082ad1..4904eb9b51 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -30,6 +30,9 @@ extern "C" { #endif #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + + #define CLOCK_REALTIME 0 + #ifdef _TD_GO_DLL_ #define MILLISECOND_PER_SECOND (1000LL) #else diff --git a/source/common/CMakeLists.txt b/source/common/CMakeLists.txt index 1e4ad95504..1344b5b58c 100644 --- a/source/common/CMakeLists.txt +++ b/source/common/CMakeLists.txt @@ -4,6 +4,10 @@ target_include_directories( common PUBLIC "${CMAKE_SOURCE_DIR}/include/common" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +IF(${TD_WINDOWS}) + PRIVATE "${CMAKE_SOURCE_DIR}/contrib/pthread-win32" + PRIVATE "${CMAKE_SOURCE_DIR}/contrib/gnuregex" +ENDIF () ) target_link_libraries( common diff --git a/source/os/CMakeLists.txt b/source/os/CMakeLists.txt index e52f349338..27906c2627 100644 --- a/source/os/CMakeLists.txt +++ b/source/os/CMakeLists.txt @@ -3,7 +3,9 @@ add_library(os STATIC ${OS_SRC}) target_include_directories( os PUBLIC "${CMAKE_SOURCE_DIR}/include/os" - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" + PRIVATE "${CMAKE_SOURCE_DIR}/include" + PRIVATE "${CMAKE_SOURCE_DIR}/include/util" + PRIVATE "${CMAKE_SOURCE_DIR}/contrib/pthread-win32" ) target_link_libraries( os pthread dl rt m diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index fb926610bd..e506210cd2 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -735,6 +735,7 @@ void taosFprintfFile(TdFilePtr pFile, const char *format, ...) { fflush(pFile->fp); } +#if !defined(WINDOWS) void *taosMmapReadOnlyFile(TdFilePtr pFile, int64_t length) { if (pFile == NULL) { return NULL; @@ -744,6 +745,7 @@ void *taosMmapReadOnlyFile(TdFilePtr pFile, int64_t length) { void *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, pFile->fd, 0); return ptr; } +#endif bool taosValidFile(TdFilePtr pFile) { return pFile != NULL; } @@ -773,6 +775,9 @@ int32_t taosEOFFile(TdFilePtr pFile) { return feof(pFile->fp); } + +#if !defined(WINDOWS) + bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) { int flags = 0; @@ -790,4 +795,7 @@ bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) { return access(pathname, flags) == 0; } -bool taosCheckExistFile(const char *pathname) { return taosCheckAccessFile(pathname, TD_FILE_ACCESS_EXIST_OK); }; \ No newline at end of file + +bool taosCheckExistFile(const char *pathname) { return taosCheckAccessFile(pathname, TD_FILE_ACCESS_EXIST_OK); }; + +#endif // WINDOWS diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 0fab7a941e..1052d108af 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "tdef.h" #include #include @@ -360,4 +361,4 @@ int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes, int8_t ncha return wcsncmp((wchar_t *)f1_ucs4, (wchar_t *)f2_ucs4, bytes / ncharSize); } -#endif \ No newline at end of file +#endif diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 25c748a8d8..9bff760509 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "taoserror.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) @@ -374,12 +375,12 @@ int32_t taosGetCpuCores(float *numOfCores) { int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine) { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - *sysCpuUsage = 0; - *procCpuUsage = 0; + *cpu_system = 0; + *cpu_engine = 0; return 0; #elif defined(_TD_DARWIN_64) - *sysCpuUsage = 0; - *procCpuUsage = 0; + *cpu_system = 0; + *cpu_engine = 0; return 0; #else static uint64_t lastSysUsed = 0; @@ -514,7 +515,7 @@ int32_t taosGetSysMemory(int64_t *usedKB) { } int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) unsigned _int64 i64FreeBytesToCaller; unsigned _int64 i64TotalBytes; unsigned _int64 i64FreeBytes; @@ -522,7 +523,7 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { BOOL fResult = GetDiskFreeSpaceExA(dataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes); if (fResult) { - diskSize->tsize = (int64_t)(i64TotalBytes); + diskSize->total = (int64_t)(i64TotalBytes); diskSize->avail = (int64_t)(i64FreeBytesToCaller); diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes); return 0; @@ -538,7 +539,7 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { terrno = TAOS_SYSTEM_ERROR(errno); return -1; } else { - diskSize->tsize = info.f_blocks * info.f_frsize; + diskSize->total = info.f_blocks * info.f_frsize; diskSize->avail = info.f_bavail * info.f_frsize; diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize; return 0; diff --git a/source/os/src/osSystem.c b/source/os/src/osSystem.c index 5c98db33d1..8ccbe9d780 100644 --- a/source/os/src/osSystem.c +++ b/source/os/src/osSystem.c @@ -17,7 +17,7 @@ #define _DEFAULT_SOURCE #include "os.h" -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) #elif defined(_TD_DARWIN_64) #else #include @@ -25,10 +25,12 @@ #include #endif +#if !defined(WINDOWS) struct termios oldtio; +#endif int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize) { -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) FILE *fp; if (cmd == NULL) { // printf("taosSystem cmd is NULL!"); @@ -51,6 +53,7 @@ int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize) { } return 0; + } #elif defined(_TD_DARWIN_64) printf("no support funtion"); return -1; @@ -82,7 +85,7 @@ int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize) { } void* taosLoadDll(const char* filename) { -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) return NULL; #elif defined(_TD_DARWIN_64) return NULL; @@ -252,7 +255,7 @@ void setTerminalMode() { } int32_t getOldTerminalMode() { -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if defined(WINDOWS) #elif defined(_TD_DARWIN_64) /* Make sure stdin is a terminal. */ @@ -295,4 +298,4 @@ void resetTerminalMode() { exit(EXIT_FAILURE); } #endif -} \ No newline at end of file +} diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index 7a47639e75..08fa0a2409 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -5,6 +5,10 @@ target_include_directories( util PUBLIC "${CMAKE_SOURCE_DIR}/include/util" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +IF(${TD_WINDOWS}) + PRIVATE "${CMAKE_SOURCE_DIR}/contrib/pthread-win32" + PRIVATE "${CMAKE_SOURCE_DIR}/contrib/gnuregex" +ENDIF () ) target_link_libraries( util diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index 8c2fd7809d..a9d925ac07 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -351,7 +351,7 @@ static void lruListMoveToFront(SList* pList, SPageInfo* pi) { static SPageInfo* getPageInfoFromPayload(void* page) { int32_t offset = offsetof(SPageInfo, pData); - char* p = page - offset; + char* p = (char *)page - offset; SPageInfo* ppi = ((SPageInfo**)p)[0]; return ppi; diff --git a/source/util/src/ttimer.c b/source/util/src/ttimer.c index 72c18518e3..7bdcf3cc64 100644 --- a/source/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -628,6 +628,8 @@ void taosTmrCleanUp(void* handle) { tmrCtrls = NULL; unusedTmrCtrl = NULL; +#if !defined(WINDOWS) tmrModuleInit = PTHREAD_ONCE_INIT; // to support restart +#endif } } From 317f7669d7aa9ed4beedb45ff127c2209840c9d6 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 19:52:59 +0800 Subject: [PATCH 23/29] sync refactor --- source/libs/sync/inc/syncMessage.h | 6 + source/libs/sync/src/syncMessage.c | 53 ++++- source/libs/sync/test/CMakeLists.txt | 14 ++ .../sync/test/syncAppendEntriesReplyTest.cpp | 1 + source/libs/sync/test/syncPingReplyTest.cpp | 12 +- source/libs/sync/test/syncPingTest.cpp | 12 +- .../sync/test/syncRequestVoteReplyTest.cpp | 1 + source/libs/sync/test/syncRequestVoteTest.cpp | 1 + source/libs/sync/test/syncRpcMsgTest.cpp | 181 ++++++++++++++++++ 9 files changed, 260 insertions(+), 21 deletions(-) create mode 100644 source/libs/sync/test/syncRpcMsgTest.cpp diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 3f19593e50..2876577410 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -47,6 +47,12 @@ cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg); cJSON* syncRpcUnknownMsg2Json(); char* syncRpcMsg2Str(SRpcMsg* pRpcMsg); +// for debug ---------------------- +void syncRpcMsgPrint(SRpcMsg* pMsg); +void syncRpcMsgPrint2(char* s, SRpcMsg* pMsg); +void syncRpcMsgLog(SRpcMsg* pMsg); +void syncRpcMsgLog2(char* s, SRpcMsg* pMsg); + // --------------------------------------------- typedef enum ESyncTimeoutType { SYNC_TIMEOUT_PING = 100, diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 477ba1929e..5a55bbc11f 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -23,39 +23,47 @@ cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg) { // in compiler optimization, switch case = if else constants if (pRpcMsg->msgType == SYNC_TIMEOUT) { - SyncTimeout* pSyncMsg = (SyncTimeout*)pRpcMsg->pCont; + SyncTimeout* pSyncMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncTimeout2Json(pSyncMsg); + syncTimeoutDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_PING) { - SyncPing* pSyncMsg = (SyncPing*)pRpcMsg->pCont; + SyncPing* pSyncMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncPing2Json(pSyncMsg); + syncPingDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { - SyncPingReply* pSyncMsg = (SyncPingReply*)pRpcMsg->pCont; + SyncPingReply* pSyncMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncPingReply2Json(pSyncMsg); + syncPingReplyDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_CLIENT_REQUEST) { - SyncClientRequest* pSyncMsg = (SyncClientRequest*)pRpcMsg->pCont; + SyncClientRequest* pSyncMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncClientRequest2Json(pSyncMsg); + syncClientRequestDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_CLIENT_REQUEST_REPLY) { pRoot = syncRpcUnknownMsg2Json(); } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE) { - SyncRequestVote* pSyncMsg = (SyncRequestVote*)pRpcMsg->pCont; + SyncRequestVote* pSyncMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncRequestVote2Json(pSyncMsg); + syncRequestVoteDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE_REPLY) { - SyncRequestVoteReply* pSyncMsg = (SyncRequestVoteReply*)pRpcMsg->pCont; + SyncRequestVoteReply* pSyncMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncRequestVoteReply2Json(pSyncMsg); + syncRequestVoteReplyDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES) { - SyncAppendEntries* pSyncMsg = (SyncAppendEntries*)pRpcMsg->pCont; + SyncAppendEntries* pSyncMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncAppendEntries2Json(pSyncMsg); + syncAppendEntriesDestroy(pSyncMsg); } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES_REPLY) { - SyncAppendEntriesReply* pSyncMsg = (SyncAppendEntriesReply*)pRpcMsg->pCont; + SyncAppendEntriesReply* pSyncMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncAppendEntriesReply2Json(pSyncMsg); + syncAppendEntriesReplyDestroy(pSyncMsg); } else { pRoot = syncRpcUnknownMsg2Json(); @@ -72,7 +80,7 @@ cJSON* syncRpcUnknownMsg2Json() { cJSON_AddStringToObject(pRoot, "data", "known message"); cJSON* pJson = cJSON_CreateObject(); - cJSON_AddItemToObject(pJson, "SyncPing", pRoot); + cJSON_AddItemToObject(pJson, "SyncUnknown", pRoot); return pJson; } @@ -83,6 +91,33 @@ char* syncRpcMsg2Str(SRpcMsg* pRpcMsg) { return serialized; } +// for debug ---------------------- +void syncRpcMsgPrint(SRpcMsg* pMsg) { + char* serialized = syncRpcMsg2Str(pMsg); + printf("syncRpcMsgPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + free(serialized); +} + +void syncRpcMsgPrint2(char* s, SRpcMsg* pMsg) { + char* serialized = syncRpcMsg2Str(pMsg); + printf("syncRpcMsgPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + free(serialized); +} + +void syncRpcMsgLog(SRpcMsg* pMsg) { + char* serialized = syncRpcMsg2Str(pMsg); + sTrace("syncRpcMsgLog | len:%lu | %s", strlen(serialized), serialized); + free(serialized); +} + +void syncRpcMsgLog2(char* s, SRpcMsg* pMsg) { + char* serialized = syncRpcMsg2Str(pMsg); + sTrace("syncRpcMsgLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + free(serialized); +} + // ---- message process SyncTimeout---- SyncTimeout* syncTimeoutBuild() { uint32_t bytes = sizeof(SyncTimeout); diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 3a87d3f2e2..6648ae3580 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable(syncClientRequestTest "") add_executable(syncTimeoutTest "") add_executable(syncPingTest "") add_executable(syncPingReplyTest "") +add_executable(syncRpcMsgTest "") target_sources(syncTest @@ -135,6 +136,10 @@ target_sources(syncPingReplyTest PRIVATE "syncPingReplyTest.cpp" ) +target_sources(syncRpcMsgTest + PRIVATE + "syncRpcMsgTest.cpp" +) target_include_directories(syncTest @@ -272,6 +277,11 @@ target_include_directories(syncPingReplyTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncRpcMsgTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -382,6 +392,10 @@ target_link_libraries(syncPingReplyTest sync gtest_main ) +target_link_libraries(syncRpcMsgTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp index c883ab941a..362da67c66 100644 --- a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp +++ b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp @@ -22,6 +22,7 @@ SyncAppendEntriesReply *createMsg() { pMsg->destId.vgId = 100; pMsg->success = true; pMsg->matchIndex = 77; + return pMsg; } void test1() { diff --git a/source/libs/sync/test/syncPingReplyTest.cpp b/source/libs/sync/test/syncPingReplyTest.cpp index 27f88b698c..8e1448e781 100644 --- a/source/libs/sync/test/syncPingReplyTest.cpp +++ b/source/libs/sync/test/syncPingReplyTest.cpp @@ -32,8 +32,8 @@ void test1() { void test2() { SyncPingReply *pMsg = createMsg(); - uint32_t len = pMsg->bytes; - char * serialized = (char *)malloc(len); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); syncPingReplySerialize(pMsg, serialized, len); SyncPingReply *pMsg2 = syncPingReplyBuild(pMsg->dataLen); syncPingReplyDeserialize(serialized, len, pMsg2); @@ -46,8 +46,8 @@ void test2() { void test3() { SyncPingReply *pMsg = createMsg(); - uint32_t len; - char * serialized = syncPingReplySerialize2(pMsg, &len); + uint32_t len; + char * serialized = syncPingReplySerialize2(pMsg, &len); SyncPingReply *pMsg2 = syncPingReplyDeserialize2(serialized, len); syncPingReplyPrint2((char *)"test3: syncPingReplySerialize3 -> syncPingReplyDeserialize2 ", pMsg2); @@ -58,7 +58,7 @@ void test3() { void test4() { SyncPingReply *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncPingReply2RpcMsg(pMsg, &rpcMsg); SyncPingReply *pMsg2 = (SyncPingReply *)malloc(rpcMsg.contLen); syncPingReplyFromRpcMsg(&rpcMsg, pMsg2); @@ -70,7 +70,7 @@ void test4() { void test5() { SyncPingReply *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncPingReply2RpcMsg(pMsg, &rpcMsg); SyncPingReply *pMsg2 = syncPingReplyFromRpcMsg2(&rpcMsg); syncPingReplyPrint2((char *)"test5: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg2 ", pMsg2); diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index 8776d2de52..83394b0e77 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -32,8 +32,8 @@ void test1() { void test2() { SyncPing *pMsg = createMsg(); - uint32_t len = pMsg->bytes; - char * serialized = (char *)malloc(len); + uint32_t len = pMsg->bytes; + char * serialized = (char *)malloc(len); syncPingSerialize(pMsg, serialized, len); SyncPing *pMsg2 = syncPingBuild(pMsg->dataLen); syncPingDeserialize(serialized, len, pMsg2); @@ -46,8 +46,8 @@ void test2() { void test3() { SyncPing *pMsg = createMsg(); - uint32_t len; - char * serialized = syncPingSerialize2(pMsg, &len); + uint32_t len; + char * serialized = syncPingSerialize2(pMsg, &len); SyncPing *pMsg2 = syncPingDeserialize2(serialized, len); syncPingPrint2((char *)"test3: syncPingSerialize3 -> syncPingDeserialize2 ", pMsg2); @@ -58,7 +58,7 @@ void test3() { void test4() { SyncPing *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); SyncPing *pMsg2 = (SyncPing *)malloc(rpcMsg.contLen); syncPingFromRpcMsg(&rpcMsg, pMsg2); @@ -70,7 +70,7 @@ void test4() { void test5() { SyncPing *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); SyncPing *pMsg2 = syncPingFromRpcMsg2(&rpcMsg); syncPingPrint2((char *)"test5: syncPing2RpcMsg -> syncPingFromRpcMsg2 ", pMsg2); diff --git a/source/libs/sync/test/syncRequestVoteReplyTest.cpp b/source/libs/sync/test/syncRequestVoteReplyTest.cpp index 52f54cbaaf..2bce3e4cd6 100644 --- a/source/libs/sync/test/syncRequestVoteReplyTest.cpp +++ b/source/libs/sync/test/syncRequestVoteReplyTest.cpp @@ -22,6 +22,7 @@ SyncRequestVoteReply *createMsg() { pMsg->destId.vgId = 100; pMsg->term = 77; pMsg->voteGranted = true; + return pMsg; } void test1() { diff --git a/source/libs/sync/test/syncRequestVoteTest.cpp b/source/libs/sync/test/syncRequestVoteTest.cpp index e46eb87fe9..7f75ee937b 100644 --- a/source/libs/sync/test/syncRequestVoteTest.cpp +++ b/source/libs/sync/test/syncRequestVoteTest.cpp @@ -23,6 +23,7 @@ SyncRequestVote *createMsg() { pMsg->currentTerm = 11; pMsg->lastLogIndex = 22; pMsg->lastLogTerm = 33; + return pMsg; } void test1() { diff --git a/source/libs/sync/test/syncRpcMsgTest.cpp b/source/libs/sync/test/syncRpcMsgTest.cpp new file mode 100644 index 0000000000..0331a29f22 --- /dev/null +++ b/source/libs/sync/test/syncRpcMsgTest.cpp @@ -0,0 +1,181 @@ +#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncUtil.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +int gg = 0; +SyncTimeout *createSyncTimeout() { + SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, &gg); + return pMsg; +} + +SyncPing *createSyncPing() { + SRaftId srcId, destId; + srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + srcId.vgId = 100; + destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + destId.vgId = 100; + SyncPing *pMsg = syncPingBuild3(&srcId, &destId); + return pMsg; +} + +SyncPingReply *createSyncPingReply() { + SRaftId srcId, destId; + srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + srcId.vgId = 100; + destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + destId.vgId = 100; + SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId); + return pMsg; +} + +SyncClientRequest *createSyncClientRequest() { + SRpcMsg rpcMsg; + memset(&rpcMsg, 0, sizeof(rpcMsg)); + rpcMsg.msgType = 12345; + rpcMsg.contLen = 20; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + strcpy((char *)rpcMsg.pCont, "hello rpc"); + SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true); + return pMsg; +} + +SyncRequestVote *createSyncRequestVote() { + SyncRequestVote *pMsg = syncRequestVoteBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->currentTerm = 11; + pMsg->lastLogIndex = 22; + pMsg->lastLogTerm = 33; + return pMsg; +} + +SyncRequestVoteReply *createSyncRequestVoteReply() { + SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->term = 77; + pMsg->voteGranted = true; + return pMsg; +} + +SyncAppendEntries *createSyncAppendEntries() { + SyncAppendEntries *pMsg = syncAppendEntriesBuild(20); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->prevLogIndex = 11; + pMsg->prevLogTerm = 22; + pMsg->commitIndex = 33; + strcpy(pMsg->data, "hello world"); + return pMsg; +} + +SyncAppendEntriesReply *createSyncAppendEntriesReply() { + SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(); + pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); + pMsg->srcId.vgId = 100; + pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); + pMsg->destId.vgId = 100; + pMsg->success = true; + pMsg->matchIndex = 77; + return pMsg; +} + +void test1() { + SyncTimeout *pMsg = createSyncTimeout(); + SRpcMsg rpcMsg; + syncTimeout2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test1", &rpcMsg); + syncTimeoutDestroy(pMsg); +} + +void test2() { + SyncPing *pMsg = createSyncPing(); + SRpcMsg rpcMsg; + syncPing2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test2", &rpcMsg); + syncPingDestroy(pMsg); +} + +void test3() { + SyncPingReply *pMsg = createSyncPingReply(); + SRpcMsg rpcMsg; + syncPingReply2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test3", &rpcMsg); + syncPingReplyDestroy(pMsg); +} + +void test4() { + SyncRequestVote *pMsg = createSyncRequestVote(); + SRpcMsg rpcMsg; + syncRequestVote2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test4", &rpcMsg); + syncRequestVoteDestroy(pMsg); +} + +void test5() { + SyncRequestVoteReply *pMsg = createSyncRequestVoteReply(); + SRpcMsg rpcMsg; + syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test5", &rpcMsg); + syncRequestVoteReplyDestroy(pMsg); +} + +void test6() { + SyncAppendEntries *pMsg = createSyncAppendEntries(); + SRpcMsg rpcMsg; + syncAppendEntries2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test6", &rpcMsg); + syncAppendEntriesDestroy(pMsg); +} + +void test7() { + SyncAppendEntriesReply *pMsg = createSyncAppendEntriesReply(); + SRpcMsg rpcMsg; + syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test7", &rpcMsg); + syncAppendEntriesReplyDestroy(pMsg); +} + +void test8() { + SyncClientRequest *pMsg = createSyncClientRequest(); + SRpcMsg rpcMsg; + syncClientRequest2RpcMsg(pMsg, &rpcMsg); + syncRpcMsgPrint2((char *)"test8", &rpcMsg); + syncClientRequestDestroy(pMsg); +} + +int main() { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + test8(); + + return 0; +} From f1624582ccfe639f5267ecd13f1d1f550c72df38 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 11 Mar 2022 19:56:17 +0800 Subject: [PATCH 24/29] sync refactor --- source/libs/sync/test/CMakeLists.txt | 14 - source/libs/sync/test/syncEncodeTest.cpp | 515 ----------------------- 2 files changed, 529 deletions(-) delete mode 100644 source/libs/sync/test/syncEncodeTest.cpp diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 6648ae3580..2a47b53945 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -1,7 +1,6 @@ add_executable(syncTest "") add_executable(syncEnvTest "") add_executable(syncPingTimerTest "") -add_executable(syncEncodeTest "") add_executable(syncIOTickQTest "") add_executable(syncIOTickPingTest "") add_executable(syncIOSendMsgTest "") @@ -40,10 +39,6 @@ target_sources(syncPingTimerTest PRIVATE "syncPingTimerTest.cpp" ) -target_sources(syncEncodeTest - PRIVATE - "syncEncodeTest.cpp" -) target_sources(syncIOTickQTest PRIVATE "syncIOTickQTest.cpp" @@ -157,11 +152,6 @@ target_include_directories(syncPingTimerTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncEncodeTest - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) target_include_directories(syncIOTickQTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" @@ -296,10 +286,6 @@ target_link_libraries(syncPingTimerTest sync gtest_main ) -target_link_libraries(syncEncodeTest - sync - gtest_main -) target_link_libraries(syncIOTickQTest sync gtest_main diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp deleted file mode 100644 index 4be239fde4..0000000000 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ /dev/null @@ -1,515 +0,0 @@ -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncMessage.h" -#include "syncUtil.h" - -void logTest() { - sTrace("--- sync log test: trace"); - sDebug("--- sync log test: debug"); - sInfo("--- sync log test: info"); - sWarn("--- sync log test: warn"); - sError("--- sync log test: error"); - sFatal("--- sync log test: fatal"); -} - -#define PING_MSG_LEN 20 -#define APPEND_ENTRIES_VALUE_LEN 32 - -#if 0 - -void test1() { - sTrace("test1: ---- syncPingSerialize, syncPingDeserialize"); - - char msg[PING_MSG_LEN]; - snprintf(msg, sizeof(msg), "%s", "test ping"); - SyncPing* pMsg = syncPingBuild(PING_MSG_LEN); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); - pMsg->destId.vgId = 100; - memcpy(pMsg->data, msg, PING_MSG_LEN); - - { - cJSON* pJson = syncPing2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - uint32_t bufLen = pMsg->bytes; - char* buf = (char*)malloc(bufLen); - syncPingSerialize(pMsg, buf, bufLen); - - SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes); - syncPingDeserialize(buf, bufLen, pMsg2); - - { - cJSON* pJson = syncPing2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncPingDestroy(pMsg); - syncPingDestroy(pMsg2); - free(buf); -} - -void test2() { - sTrace("test2: ---- syncPing2RpcMsg, syncPingFromRpcMsg"); - - char msg[PING_MSG_LEN]; - snprintf(msg, sizeof(msg), "%s", "hello raft"); - SyncPing* pMsg = syncPingBuild(PING_MSG_LEN); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 3333); - pMsg->srcId.vgId = 200; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 4444); - pMsg->destId.vgId = 200; - memcpy(pMsg->data, msg, PING_MSG_LEN); - - { - cJSON* pJson = syncPing2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - SRpcMsg rpcMsg; - syncPing2RpcMsg(pMsg, &rpcMsg); - SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes); - syncPingFromRpcMsg(&rpcMsg, pMsg2); - rpcFreeCont(rpcMsg.pCont); - - { - cJSON* pJson = syncPing2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncPingDestroy(pMsg); - syncPingDestroy(pMsg2); -} - -void test3() { - sTrace("test3: ---- syncPingReplySerialize, syncPingReplyDeserialize"); - - char msg[PING_MSG_LEN]; - snprintf(msg, sizeof(msg), "%s", "test ping"); - SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 5555); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 6666); - pMsg->destId.vgId = 100; - memcpy(pMsg->data, msg, PING_MSG_LEN); - - { - cJSON* pJson = syncPingReply2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - uint32_t bufLen = pMsg->bytes; - char* buf = (char*)malloc(bufLen); - syncPingReplySerialize(pMsg, buf, bufLen); - - SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes); - syncPingReplyDeserialize(buf, bufLen, pMsg2); - - { - cJSON* pJson = syncPingReply2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncPingReplyDestroy(pMsg); - syncPingReplyDestroy(pMsg2); - free(buf); -} - -void test4() { - sTrace("test4: ---- syncPingReply2RpcMsg, syncPingReplyFromRpcMsg"); - - char msg[PING_MSG_LEN]; - snprintf(msg, sizeof(msg), "%s", "hello raft"); - SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 7777); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 8888); - pMsg->destId.vgId = 100; - memcpy(pMsg->data, msg, PING_MSG_LEN); - - { - cJSON* pJson = syncPingReply2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - SRpcMsg rpcMsg; - syncPingReply2RpcMsg(pMsg, &rpcMsg); - SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes); - syncPingReplyFromRpcMsg(&rpcMsg, pMsg2); - rpcFreeCont(rpcMsg.pCont); - - { - cJSON* pJson = syncPingReply2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncPingReplyDestroy(pMsg); - syncPingReplyDestroy(pMsg2); -} - -void test5() { - sTrace("test5: ---- syncRequestVoteSerialize, syncRequestVoteDeserialize"); - - SyncRequestVote* pMsg = syncRequestVoteBuild(); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); - pMsg->destId.vgId = 100; - pMsg->currentTerm = 20; - pMsg->lastLogIndex = 21; - pMsg->lastLogTerm = 22; - - { - cJSON* pJson = syncRequestVote2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - uint32_t bufLen = pMsg->bytes; - char* buf = (char*)malloc(bufLen); - syncRequestVoteSerialize(pMsg, buf, bufLen); - - SyncRequestVote* pMsg2 = (SyncRequestVote*)malloc(pMsg->bytes); - syncRequestVoteDeserialize(buf, bufLen, pMsg2); - - { - cJSON* pJson = syncRequestVote2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncRequestVoteDestroy(pMsg); - syncRequestVoteDestroy(pMsg2); - free(buf); -} - -void test6() { - sTrace("test6: ---- syncRequestVote2RpcMsg, syncRequestVoteFromRpcMsg"); - - SyncRequestVote* pMsg = syncRequestVoteBuild(); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); - pMsg->destId.vgId = 100; - pMsg->currentTerm = 20; - pMsg->lastLogIndex = 21; - pMsg->lastLogTerm = 22; - - { - cJSON* pJson = syncRequestVote2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - SRpcMsg rpcMsg; - syncRequestVote2RpcMsg(pMsg, &rpcMsg); - SyncRequestVote* pMsg2 = (SyncRequestVote*)malloc(pMsg->bytes); - syncRequestVoteFromRpcMsg(&rpcMsg, pMsg2); - rpcFreeCont(rpcMsg.pCont); - - { - cJSON* pJson = syncRequestVote2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncRequestVoteDestroy(pMsg); - syncRequestVoteDestroy(pMsg2); -} - -void test7() { - sTrace("test7: ---- syncRequestVoteReplySerialize, syncRequestVoteReplyDeserialize"); - - SyncRequestVoteReply* pMsg = SyncRequestVoteReplyBuild(); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); - pMsg->destId.vgId = 100; - pMsg->term = 20; - pMsg->voteGranted = 1; - - { - cJSON* pJson = syncRequestVoteReply2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - uint32_t bufLen = pMsg->bytes; - char* buf = (char*)malloc(bufLen); - syncRequestVoteReplySerialize(pMsg, buf, bufLen); - - SyncRequestVoteReply* pMsg2 = (SyncRequestVoteReply*)malloc(pMsg->bytes); - syncRequestVoteReplyDeserialize(buf, bufLen, pMsg2); - - { - cJSON* pJson = syncRequestVoteReply2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncRequestVoteReplyDestroy(pMsg); - syncRequestVoteReplyDestroy(pMsg2); - free(buf); -} - -void test8() { - sTrace("test8: ---- syncRequestVoteReply2RpcMsg, syncRequestVoteReplyFromRpcMsg"); - - SyncRequestVoteReply* pMsg = SyncRequestVoteReplyBuild(); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678); - pMsg->destId.vgId = 100; - pMsg->term = 20; - pMsg->voteGranted = 1; - - { - cJSON* pJson = syncRequestVoteReply2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - SRpcMsg rpcMsg; - syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); - SyncRequestVoteReply* pMsg2 = (SyncRequestVoteReply*)malloc(pMsg->bytes); - syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2); - rpcFreeCont(rpcMsg.pCont); - - { - cJSON* pJson = syncRequestVoteReply2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncRequestVoteReplyDestroy(pMsg); - syncRequestVoteReplyDestroy(pMsg2); -} - -void test9() { - sTrace("test9: ---- syncAppendEntriesSerialize, syncAppendEntriesDeserialize"); - - char msg[APPEND_ENTRIES_VALUE_LEN]; - snprintf(msg, sizeof(msg), "%s", "test value"); - SyncAppendEntries* pMsg = syncAppendEntriesBuild(APPEND_ENTRIES_VALUE_LEN); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); - pMsg->destId.vgId = 100; - pMsg->prevLogIndex = 55; - pMsg->prevLogTerm = 66; - pMsg->commitIndex = 77; - memcpy(pMsg->data, msg, APPEND_ENTRIES_VALUE_LEN); - - { - cJSON* pJson = syncAppendEntries2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - uint32_t bufLen = pMsg->bytes; - char* buf = (char*)malloc(bufLen); - syncAppendEntriesSerialize(pMsg, buf, bufLen); - - SyncAppendEntries* pMsg2 = (SyncAppendEntries*)malloc(pMsg->bytes); - syncAppendEntriesDeserialize(buf, bufLen, pMsg2); - - { - cJSON* pJson = syncAppendEntries2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncAppendEntriesDestroy(pMsg); - syncAppendEntriesDestroy(pMsg2); - free(buf); -} - -void test10() { - sTrace("test10: ---- syncAppendEntries2RpcMsg, syncAppendEntriesFromRpcMsg"); - - char msg[APPEND_ENTRIES_VALUE_LEN]; - snprintf(msg, sizeof(msg), "%s", "test value"); - SyncAppendEntries* pMsg = syncAppendEntriesBuild(APPEND_ENTRIES_VALUE_LEN); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); - pMsg->destId.vgId = 100; - pMsg->prevLogIndex = 55; - pMsg->prevLogTerm = 66; - pMsg->commitIndex = 77; - memcpy(pMsg->data, msg, APPEND_ENTRIES_VALUE_LEN); - - { - cJSON* pJson = syncAppendEntries2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - SRpcMsg rpcMsg; - syncAppendEntries2RpcMsg(pMsg, &rpcMsg); - SyncAppendEntries* pMsg2 = (SyncAppendEntries*)malloc(pMsg->bytes); - syncAppendEntriesFromRpcMsg(&rpcMsg, pMsg2); - rpcFreeCont(rpcMsg.pCont); - - { - cJSON* pJson = syncAppendEntries2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncAppendEntriesDestroy(pMsg); - syncAppendEntriesDestroy(pMsg2); -} - -void test11() { - sTrace("test11: ---- syncAppendEntriesReplySerialize, syncAppendEntriesReplyDeserialize"); - - SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyBuild(); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); - pMsg->destId.vgId = 100; - pMsg->success = 1; - pMsg->matchIndex = 23; - - { - cJSON* pJson = syncAppendEntriesReply2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - uint32_t bufLen = pMsg->bytes; - char* buf = (char*)malloc(bufLen); - syncAppendEntriesReplySerialize(pMsg, buf, bufLen); - - SyncAppendEntriesReply* pMsg2 = (SyncAppendEntriesReply*)malloc(pMsg->bytes); - syncAppendEntriesReplyDeserialize(buf, bufLen, pMsg2); - - { - cJSON* pJson = syncAppendEntriesReply2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncAppendEntriesReplyDestroy(pMsg); - syncAppendEntriesReplyDestroy(pMsg2); - free(buf); -} - -void test12() { - sTrace("test12: ---- syncAppendEntriesReply2RpcMsg, syncAppendEntriesReplyFromRpcMsg"); - - SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyBuild(); - pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111); - pMsg->srcId.vgId = 100; - pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222); - pMsg->destId.vgId = 100; - pMsg->success = 1; - pMsg->matchIndex = 23; - - { - cJSON* pJson = syncAppendEntriesReply2Json(pMsg); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - SRpcMsg rpcMsg; - syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); - SyncAppendEntriesReply* pMsg2 = (SyncAppendEntriesReply*)malloc(pMsg->bytes); - syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2); - rpcFreeCont(rpcMsg.pCont); - - { - cJSON* pJson = syncAppendEntriesReply2Json(pMsg2); - char* serialized = cJSON_Print(pJson); - printf("\n%s\n\n", serialized); - free(serialized); - cJSON_Delete(pJson); - } - - syncAppendEntriesReplyDestroy(pMsg); - syncAppendEntriesReplyDestroy(pMsg2); -} - -#endif - -int main() { - // taosInitLog((char*)"syncPingTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - -#if 0 - test1(); - test2(); - test3(); - test4(); - test5(); - test6(); - test7(); - test8(); - test9(); - test10(); - test11(); - test12(); -#endif - - return 0; -} From 62700c1f1307c94f0c0b7a0d34ee6d21620c2446 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 11 Mar 2022 21:39:16 +0800 Subject: [PATCH 25/29] handle persist conn --- include/libs/transport/trpc.h | 2 - source/libs/transport/src/transCli.c | 93 +++++++++++++++---------- source/libs/transport/src/transSrv.c | 14 ++-- source/libs/transport/test/pushClient.c | 2 +- 4 files changed, 63 insertions(+), 48 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 6ccb6c0dc4..feb493b50f 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -47,8 +47,6 @@ typedef struct SRpcMsg { void * ahandle; // app handle set by client int persist; // keep handle or not, default 0 - SRpcPush *push; - } SRpcMsg; typedef struct SRpcPush { diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 1323677071..c2d5feccaf 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -17,11 +17,6 @@ #include "transComm.h" -#define CONN_HOST_THREAD_INDEX(conn) (conn ? ((SCliConn*)conn)->hThrdIdx : -1) -#define CONN_PERSIST_TIME(para) (para * 1000 * 10) - -#define CONN_GET_INST_LABEL(conn) (((SRpcInfo*)(((SCliThrdObj*)conn->hostThrd)->pTransInst))->label) - typedef struct SCliConn { T_REF_DECLARE() uv_connect_t connReq; @@ -32,7 +27,6 @@ typedef struct SCliConn { void* data; queue conn; uint64_t expireTime; - int8_t ctnRdCnt; // continue read count int hThrdIdx; bool broken; // link broken or not @@ -92,9 +86,9 @@ static void clientTimeoutCb(uv_timer_t* handle); // 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); +static void clientRecvCb(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); +static void clientSendDataCb(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); @@ -120,7 +114,27 @@ static void transDestroyConnCtx(STransConnCtx* ctx); // thread obj static SCliThrdObj* createThrdObj(); static void destroyThrdObj(SCliThrdObj* pThrd); -// thread + +#define CONN_HOST_THREAD_INDEX(conn) (conn ? ((SCliConn*)conn)->hThrdIdx : -1) +#define CONN_PERSIST_TIME(para) (para * 1000 * 10) + +#define CONN_GET_INST_LABEL(conn) (((SRpcInfo*)(((SCliThrdObj*)conn->hostThrd)->pTransInst))->label) +#define CONN_HANDLE_THREAD_QUIT(conn, thrd) \ + do { \ + if (thrd->quit) { \ + clientHandleExcept(conn); \ + } \ + goto _RETURE; \ + } while (0) + +#define CONN_HANDLE_BROKEN(conn) \ + do { \ + if (conn->broken) { \ + clientHandleExcept(conn); \ + } \ + goto _RETURE; \ + } while (0); + static void* clientThread(void* arg); static void* clientNotifyApp() {} @@ -147,6 +161,8 @@ static void clientHandleResp(SCliConn* conn) { if (pTransInst->pfp != NULL && (pTransInst->pfp)(pTransInst->parent, rpcMsg.msgType)) { rpcMsg.handle = conn; + transRefCliHandle(conn); + conn->persist = 1; tDebug("client conn %p persist by app", conn); } @@ -165,9 +181,8 @@ static void clientHandleResp(SCliConn* conn) { memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg)); tsem_post(pCtx->pSem); } - conn->ctnRdCnt += 1; - uv_read_start((uv_stream_t*)conn->stream, clientAllocBufferCb, clientReadCb); + uv_read_start((uv_stream_t*)conn->stream, clientAllocBufferCb, clientRecvCb); // user owns conn->persist = 1 if (conn->persist == 0) { @@ -290,7 +305,6 @@ static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn) { conn->expireTime = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime); SConnList* plist = taosHashGet((SHashObj*)pool, key, strlen(key)); - conn->ctnRdCnt = 0; // list already create before assert(plist != NULL); QUEUE_PUSH(&plist->conn, &conn->conn); @@ -300,7 +314,7 @@ static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_b SConnBuffer* pBuf = &conn->readBuf; transAllocBuffer(pBuf, buf); } -static void clientReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { +static void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { // impl later if (handle->data == NULL) { return; @@ -363,7 +377,7 @@ static void clientDestroy(uv_handle_t* handle) { free(conn); } -static void clientWriteCb(uv_write_t* req, int status) { +static void clientSendDataCb(uv_write_t* req, int status) { SCliConn* pConn = req->data; if (status == 0) { @@ -378,10 +392,12 @@ static void clientWriteCb(uv_write_t* req, int status) { clientHandleExcept(pConn); return; } - uv_read_start((uv_stream_t*)pConn->stream, clientAllocBufferCb, clientReadCb); + uv_read_start((uv_stream_t*)pConn->stream, clientAllocBufferCb, clientRecvCb); } -static void clientWrite(SCliConn* pConn) { +static void clientSendData(SCliConn* pConn) { + CONN_HANDLE_BROKEN(pConn); + SCliMsg* pCliMsg = pConn->data; STransConnCtx* pCtx = pCliMsg->ctx; @@ -420,7 +436,11 @@ static void clientWrite(SCliConn* pConn) { TMSG_INFO(pHead->msgType), inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port)); - uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientWriteCb); + uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientSendDataCb); + + return; +_RETURE: + return; } static void clientConnCb(uv_connect_t* req, int status) { // impl later @@ -439,7 +459,7 @@ static void clientConnCb(uv_connect_t* req, int status) { tTrace("%s client conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn); assert(pConn->stream == req->handle); - clientWrite(pConn); + clientSendData(pConn); } static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { @@ -452,6 +472,21 @@ static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { pThrd->quit = true; uv_stop(pThrd->loop); } +static SCliConn* clientGetConn(SCliMsg* pMsg, SCliThrdObj* pThrd) { + SCliConn* conn = NULL; + if (pMsg->msg.handle != NULL) { + conn = (SCliConn*)(pMsg->msg.handle); + transUnrefCliHandle(conn); + if (conn != NULL) { + tTrace("%s client conn %p reused", CONN_GET_INST_LABEL(conn), conn); + } + } else { + STransConnCtx* pCtx = pMsg->ctx; + conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); + if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); + } + return conn; +} static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { uint64_t et = taosGetTimestampUs(); uint64_t el = et - pMsg->st; @@ -459,28 +494,12 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { STransConnCtx* pCtx = pMsg->ctx; SRpcInfo* pTransInst = pThrd->pTransInst; - SCliConn* conn = NULL; - - if (pMsg->msg.handle != NULL) { - conn = (SCliConn*)(pMsg->msg.handle); - if (conn != NULL) { - tTrace("%s client conn %p reused", CONN_GET_INST_LABEL(conn), conn); - } - } else { - conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); - if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); - } + SCliConn* conn = clientGetConn(pMsg, pThrd); if (conn != NULL) { conn->data = pMsg; - conn->writeReq.data = conn; transDestroyBuffer(&conn->readBuf); - - if (pThrd->quit) { - clientHandleExcept(conn); - return; - } - clientWrite(conn); + clientSendData(conn); } else { conn = clientConnCreate(pThrd); conn->data = pMsg; @@ -495,8 +514,6 @@ static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { tTrace("%s client conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port); uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb); } - - conn->ctnRdCnt = 0; conn->hThrdIdx = pCtx->hThrdIdx; } static void clientAsyncCb(uv_async_t* handle) { diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index 593d0acfc5..1abca9ad97 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -92,9 +92,9 @@ static int uvAuthMsg(SSrvConn* pConn, char* msg, int msgLen); static void uvAllocConnBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf); static void uvAllocReadBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf); -static void uvOnReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf); +static void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf); static void uvOnTimeoutCb(uv_timer_t* handle); -static void uvOnWriteCb(uv_write_t* req, int status); +static void uvOnSendCb(uv_write_t* req, int status); static void uvOnPipeWriteCb(uv_write_t* req, int status); static void uvOnAcceptCb(uv_stream_t* stream, int status); static void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf); @@ -240,7 +240,7 @@ static void uvHandleReq(SSrvConn* pConn) { // validate msg type } -void uvOnReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) { +void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) { // opt SSrvConn* conn = cli->data; SConnBuffer* pBuf = &conn->readBuf; @@ -282,7 +282,7 @@ void uvOnTimeoutCb(uv_timer_t* handle) { tError("server conn %p time out", pConn); } -void uvOnWriteCb(uv_write_t* req, int status) { +void uvOnSendCb(uv_write_t* req, int status) { SSrvConn* conn = req->data; transClearBuffer(&conn->readBuf); if (status == 0) { @@ -350,7 +350,7 @@ static void uvStartSendRespInternal(SSrvMsg* smsg) { SSrvConn* pConn = smsg->pConn; uv_timer_stop(&pConn->pTimer); - uv_write(&pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnWriteCb); + uv_write(&pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnSendCb); } static void uvStartSendResp(SSrvMsg* smsg) { // impl @@ -526,7 +526,7 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { return; } - uv_read_start((uv_stream_t*)(pConn->pTcp), uvAllocReadBufferCb, uvOnReadCb); + uv_read_start((uv_stream_t*)(pConn->pTcp), uvAllocReadBufferCb, uvOnRecvCb); } else { tDebug("failed to create new connection"); @@ -641,7 +641,7 @@ static void uvDestroyConn(uv_handle_t* handle) { uv_timer_stop(&conn->pTimer); QUEUE_REMOVE(&conn->queue); free(conn->pTcp); - // free(conn); + free(conn); if (thrd->quit && QUEUE_IS_EMPTY(&thrd->conn)) { uv_loop_close(thrd->loop); diff --git a/source/libs/transport/test/pushClient.c b/source/libs/transport/test/pushClient.c index f4babc9980..dc9914be35 100644 --- a/source/libs/transport/test/pushClient.c +++ b/source/libs/transport/test/pushClient.c @@ -82,7 +82,7 @@ static void *sendRequest(void *param) { rpcMsg.contLen = pInfo->msgSize; rpcMsg.ahandle = pInfo; rpcMsg.msgType = 1; - rpcMsg.push = push; + // rpcMsg.push = push; // tDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num); int64_t start = taosGetTimestampUs(); rpcSendRequest(pInfo->pRpc, &pInfo->epSet, &rpcMsg, NULL); From 31bf003dd24482e3e5169419e83a0daca7b66c8d Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 11 Mar 2022 22:00:56 +0800 Subject: [PATCH 26/29] handle except --- source/libs/transport/src/transCli.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index c2d5feccaf..834337d0ff 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -123,16 +123,16 @@ static void destroyThrdObj(SCliThrdObj* pThrd); do { \ if (thrd->quit) { \ clientHandleExcept(conn); \ + goto _RETURE; \ } \ - goto _RETURE; \ } while (0) #define CONN_HANDLE_BROKEN(conn) \ do { \ if (conn->broken) { \ clientHandleExcept(conn); \ + goto _RETURE; \ } \ - goto _RETURE; \ } while (0); static void* clientThread(void* arg); From baf0ac2d3150048899aaab1ff4b330602353adfc Mon Sep 17 00:00:00 2001 From: ubuntu Date: Sat, 12 Mar 2022 11:31:07 +0800 Subject: [PATCH 27/29] update UT test --- include/libs/transport/trpc.h | 7 - source/libs/transport/test/CMakeLists.txt | 18 -- source/libs/transport/test/pushClient.c | 242 ---------------------- 3 files changed, 267 deletions(-) delete mode 100644 source/libs/transport/test/pushClient.c diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index feb493b50f..fdc9368b76 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -29,7 +29,6 @@ extern "C" { extern int tsRpcHeadSize; -typedef struct SRpcPush SRpcPush; typedef struct SRpcConnInfo { uint32_t clientIp; @@ -45,14 +44,8 @@ typedef struct SRpcMsg { int32_t code; void * handle; // rpc handle returned to app void * ahandle; // app handle set by client - int persist; // keep handle or not, default 0 - } SRpcMsg; -typedef struct SRpcPush { - void *arg; - int (*callback)(void *arg, SRpcMsg *rpcMsg); -} SRpcPush; typedef struct SRpcInit { uint16_t localPort; // local port diff --git a/source/libs/transport/test/CMakeLists.txt b/source/libs/transport/test/CMakeLists.txt index b4f50219ff..b29bad07f0 100644 --- a/source/libs/transport/test/CMakeLists.txt +++ b/source/libs/transport/test/CMakeLists.txt @@ -3,7 +3,6 @@ add_executable(client "") add_executable(server "") add_executable(transUT "") add_executable(syncClient "") -add_executable(pushClient "") add_executable(pushServer "") target_sources(transUT @@ -27,10 +26,6 @@ target_sources (syncClient "syncClient.c" ) -target_sources(pushClient - PRIVATE - "pushClient.c" -) target_sources(pushServer PRIVATE "pushServer.c" @@ -102,19 +97,6 @@ target_link_libraries (syncClient transport ) -target_include_directories(pushClient - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/transport" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) -target_link_libraries (pushClient - os - util - common - gtest_main - transport -) - target_include_directories(pushServer PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/transport" diff --git a/source/libs/transport/test/pushClient.c b/source/libs/transport/test/pushClient.c deleted file mode 100644 index dc9914be35..0000000000 --- a/source/libs/transport/test/pushClient.c +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ -#include - -#include -#include "os.h" -#include "rpcLog.h" -#include "taoserror.h" -#include "tglobal.h" -#include "trpc.h" -#include "tutil.h" - -typedef struct { - int index; - SEpSet epSet; - int num; - int numOfReqs; - int msgSize; - tsem_t rspSem; - tsem_t * pOverSem; - pthread_t thread; - void * pRpc; -} SInfo; -static void processResponse(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - SInfo *pInfo = (SInfo *)pMsg->ahandle; - tDebug("thread:%d, response is received, type:%d contLen:%d code:0x%x", pInfo->index, pMsg->msgType, pMsg->contLen, - pMsg->code); - - if (pEpSet) pInfo->epSet = *pEpSet; - - rpcFreeCont(pMsg->pCont); - // tsem_post(&pInfo->rspSem); - tsem_post(&pInfo->rspSem); -} - -static int tcount = 0; - -typedef struct SPushArg { - tsem_t sem; -} SPushArg; -// ping -int pushCallback(void *arg, SRpcMsg *msg) { - SPushArg *push = arg; - tsem_post(&push->sem); -} -SRpcPush *createPushArg() { - SRpcPush *push = calloc(1, sizeof(SRpcPush)); - push->arg = calloc(1, sizeof(SPushArg)); - - tsem_init(&(((SPushArg *)push->arg)->sem), 0, 0); - push->callback = pushCallback; - return push; -} -static void *sendRequest(void *param) { - SInfo * pInfo = (SInfo *)param; - SRpcMsg rpcMsg = {0}; - - tDebug("thread:%d, start to send request", pInfo->index); - - tDebug("thread:%d, reqs: %d", pInfo->index, pInfo->numOfReqs); - int u100 = 0; - int u500 = 0; - int u1000 = 0; - int u10000 = 0; - - while (pInfo->numOfReqs == 0 || pInfo->num < pInfo->numOfReqs) { - SRpcPush *push = createPushArg(); - pInfo->num++; - rpcMsg.pCont = rpcMallocCont(pInfo->msgSize); - rpcMsg.contLen = pInfo->msgSize; - rpcMsg.ahandle = pInfo; - rpcMsg.msgType = 1; - // rpcMsg.push = push; - // tDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num); - int64_t start = taosGetTimestampUs(); - 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); // ping->pong - // tsem_wait(&pInfo->rspSem); - SPushArg *arg = push->arg; - /// e - tsem_wait(&arg->sem); // push callback - - // query_fetch(client->h) - int64_t end = taosGetTimestampUs() - start; - if (end <= 100) { - u100++; - } else if (end > 100 && end <= 500) { - u500++; - } else if (end > 500 && end < 1000) { - u1000++; - } else { - u10000++; - } - - tDebug("recv response succefully"); - - // usleep(100000000); - } - - tError("send and recv sum: %d, %d, %d, %d", u100, u500, u1000, u10000); - tDebug("thread:%d, it is over", pInfo->index); - tcount++; - - return NULL; -} - -int main(int argc, char *argv[]) { - SRpcInit rpcInit; - SEpSet epSet; - int msgSize = 128; - int numOfReqs = 0; - int appThreads = 1; - char serverIp[40] = "127.0.0.1"; - char secret[20] = "mypassword"; - struct timeval systemTime; - int64_t startTime, endTime; - pthread_attr_t thattr; - - // server info - epSet.inUse = 0; - addEpIntoEpSet(&epSet, serverIp, 7000); - addEpIntoEpSet(&epSet, "192.168.0.1", 7000); - - // client info - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 0; - rpcInit.label = "APP"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = processResponse; - rpcInit.sessions = 100; - rpcInit.idleTime = 100; - 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.eps[0].port = atoi(argv[++i]); - } else if (strcmp(argv[i], "-i") == 0 && i < argc - 1) { - tstrncpy(epSet.eps[0].fqdn, argv[++i], sizeof(epSet.eps[0].fqdn)); - } 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.eps[0].port); - 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", 10); - - void *pRpc = rpcOpen(&rpcInit); - if (pRpc == NULL) { - tError("failed to initialize RPC"); - return -1; - } - - tInfo("client is initialized"); - tInfo("threads:%d msgSize:%d requests:%d", appThreads, msgSize, numOfReqs); - - 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.0f; // mseconds - - tInfo("it takes %.3f mseconds to send %d requests to server", usedTime, numOfReqs * appThreads); - tInfo("Performance: %.3f requests per second, msgSize:%d bytes", 1000.0 * numOfReqs * appThreads / usedTime, msgSize); - - int ch = getchar(); - UNUSED(ch); - - taosCloseLog(); - - return 0; -} From 30f602fae0f406f76950ecea5ec2ec5ad7b90bc8 Mon Sep 17 00:00:00 2001 From: ubuntu Date: Sat, 12 Mar 2022 13:59:21 +0800 Subject: [PATCH 28/29] update UT test --- source/libs/transport/src/transCli.c | 197 ++++++++++++++------------- 1 file changed, 99 insertions(+), 98 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 834337d0ff..727845b7a9 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -63,12 +63,12 @@ typedef struct SCliThrdObj { bool quit; } SCliThrdObj; -typedef struct SClientObj { +typedef struct SCliObj { char label[TSDB_LABEL_LEN]; int32_t index; int numOfThreads; SCliThrdObj** pThreadObj; -} SClientObj; +} SCliObj; typedef struct SConnList { queue conn; @@ -82,32 +82,32 @@ static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port); static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn); // register timer in each thread to clear expire conn -static void clientTimeoutCb(uv_timer_t* handle); -// 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 clientRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf); +static void cliTimeoutCb(uv_timer_t* handle); +// alloc buf for recv +static void cliAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf); +// callback after read nbytes from socket +static void cliRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf); // callback after write data to socket -static void clientSendDataCb(uv_write_t* req, int status); +static void cliSendCb(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 cliConnCb(uv_connect_t* req, int status); +static void cliAsyncCb(uv_async_t* handle); -static SCliConn* clientConnCreate(SCliThrdObj* thrd); -static void clientConnDestroy(SCliConn* pConn, bool clear /*clear tcp handle or not*/); -static void clientDestroy(uv_handle_t* handle); +static SCliConn* cliCreateConn(SCliThrdObj* thrd); +static void cliDestroyConn(SCliConn* pConn, bool clear /*clear tcp handle or not*/); +static void cliDestroy(uv_handle_t* handle); // process data read from server, add decompress etc later -static void clientHandleResp(SCliConn* conn); +static void cliHandleResp(SCliConn* conn); // handle except about conn -static void clientHandleExcept(SCliConn* conn); +static void cliHandleExcept(SCliConn* conn); // handle req from app -static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd); -static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd); -static void clientSendQuit(SCliThrdObj* thrd); +static void cliHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd); +static void cliHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd); +static void cliSendQuit(SCliThrdObj* thrd); static void destroyUserdata(SRpcMsg* userdata); -static int clientRBChoseIdx(SRpcInfo* pTransInst); +static int cliRBChoseIdx(SRpcInfo* pTransInst); static void destroyCmsg(SCliMsg* cmsg); static void transDestroyConnCtx(STransConnCtx* ctx); @@ -122,7 +122,7 @@ static void destroyThrdObj(SCliThrdObj* pThrd); #define CONN_HANDLE_THREAD_QUIT(conn, thrd) \ do { \ if (thrd->quit) { \ - clientHandleExcept(conn); \ + cliHandleExcept(conn); \ goto _RETURE; \ } \ } while (0) @@ -130,15 +130,15 @@ static void destroyThrdObj(SCliThrdObj* pThrd); #define CONN_HANDLE_BROKEN(conn) \ do { \ if (conn->broken) { \ - clientHandleExcept(conn); \ + cliHandleExcept(conn); \ goto _RETURE; \ } \ } while (0); -static void* clientThread(void* arg); +static void* cliWorkThread(void* arg); -static void* clientNotifyApp() {} -static void clientHandleResp(SCliConn* conn) { +static void* cliNotifyApp() {} +static void cliHandleResp(SCliConn* conn) { SCliMsg* pMsg = conn->data; STransConnCtx* pCtx = pMsg->ctx; @@ -164,25 +164,25 @@ static void clientHandleResp(SCliConn* conn) { transRefCliHandle(conn); conn->persist = 1; - tDebug("client conn %p persist by app", conn); + tDebug("cli conn %p persist by app", conn); } - tDebug("%s client conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pTransInst->label, conn, + tDebug("%s cli conn %p %s received from %s:%d, local info: %s:%d, msg size: %d", pTransInst->label, conn, TMSG_INFO(pHead->msgType), inet_ntoa(conn->addr.sin_addr), ntohs(conn->addr.sin_port), inet_ntoa(conn->locaddr.sin_addr), ntohs(conn->locaddr.sin_port), rpcMsg.contLen); conn->secured = pHead->secured; if (pCtx->pSem == NULL) { - tTrace("%s client conn %p handle resp", pTransInst->label, conn); + tTrace("%s cli conn %p handle resp", pTransInst->label, conn); (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); } else { - tTrace("%s client conn(sync) %p handle resp", pTransInst->label, conn); + tTrace("%s cli conn(sync) %p handle resp", pTransInst->label, conn); memcpy((char*)pCtx->pRsp, (char*)&rpcMsg, sizeof(rpcMsg)); tsem_post(pCtx->pSem); } - uv_read_start((uv_stream_t*)conn->stream, clientAllocBufferCb, clientRecvCb); + uv_read_start((uv_stream_t*)conn->stream, cliAllocBufferCb, cliRecvCb); // user owns conn->persist = 1 if (conn->persist == 0) { @@ -193,10 +193,10 @@ static void clientHandleResp(SCliConn* conn) { // start thread's timer of conn pool if not active if (!uv_is_active((uv_handle_t*)&pThrd->timer) && pTransInst->idleTime > 0) { - // uv_timer_start((uv_timer_t*)&pThrd->timer, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); + // uv_timer_start((uv_timer_t*)&pThrd->timer, cliTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); } } -static void clientHandleExcept(SCliConn* pConn) { +static void cliHandleExcept(SCliConn* pConn) { if (pConn->data == NULL) { // handle conn except in conn pool transUnrefCliHandle(pConn); @@ -214,25 +214,25 @@ static void clientHandleExcept(SCliConn* pConn) { rpcMsg.msgType = pMsg->msg.msgType + 1; if (pCtx->pSem == NULL) { - tTrace("%s client conn %p handle resp", pTransInst->label, pConn); + tTrace("%s cli conn %p handle resp", pTransInst->label, pConn); (pTransInst->cfp)(pTransInst->parent, &rpcMsg, NULL); } else { - tTrace("%s client conn(sync) %p handle resp", pTransInst->label, pConn); + tTrace("%s cli conn(sync) %p handle resp", pTransInst->label, pConn); memcpy((char*)(pCtx->pRsp), (char*)(&rpcMsg), sizeof(rpcMsg)); tsem_post(pCtx->pSem); } destroyCmsg(pConn->data); pConn->data = NULL; - tTrace("%s client conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn); + tTrace("%s cli conn %p start to destroy", CONN_GET_INST_LABEL(pConn), pConn); transUnrefCliHandle(pConn); } -static void clientTimeoutCb(uv_timer_t* handle) { +static void cliTimeoutCb(uv_timer_t* handle) { SCliThrdObj* pThrd = handle->data; SRpcInfo* pRpc = pThrd->pTransInst; int64_t currentTime = pThrd->nextTimeout; - tTrace("%s, client conn timeout, try to remove expire conn from conn pool", pRpc->label); + tTrace("%s, cli conn timeout, try to remove expire conn from conn pool", pRpc->label); SConnList* p = taosHashIterate((SHashObj*)pThrd->pool, NULL); while (p != NULL) { @@ -250,7 +250,7 @@ static void clientTimeoutCb(uv_timer_t* handle) { } pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime); - uv_timer_start(handle, clientTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); + uv_timer_start(handle, cliTimeoutCb, CONN_PERSIST_TIME(pRpc->idleTime) / 2, 0); } static void* createConnPool(int size) { // thread local, no lock @@ -263,7 +263,7 @@ static void* destroyConnPool(void* pool) { queue* h = QUEUE_HEAD(&connList->conn); QUEUE_REMOVE(h); SCliConn* c = QUEUE_DATA(h, SCliConn, conn); - clientConnDestroy(c, true); + cliDestroyConn(c, true); } connList = taosHashIterate((SHashObj*)pool, connList); } @@ -299,7 +299,7 @@ static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn) { tstrncpy(key, ip, strlen(ip)); tstrncpy(key + strlen(key), (char*)(&port), sizeof(port)); - tTrace("client conn %p added to conn pool, read buf cap: %d", conn, conn->readBuf.cap); + tTrace("cli conn %p added to conn pool, read buf cap: %d", conn, conn->readBuf.cap); SRpcInfo* pRpc = ((SCliThrdObj*)conn->hostThrd)->pTransInst; @@ -309,12 +309,12 @@ static void addConnToPool(void* pool, char* ip, uint32_t port, SCliConn* conn) { assert(plist != NULL); QUEUE_PUSH(&plist->conn, &conn->conn); } -static void clientAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { +static void cliAllocBufferCb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { SCliConn* conn = handle->data; SConnBuffer* pBuf = &conn->readBuf; transAllocBuffer(pBuf, buf); } -static void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { +static void cliRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { // impl later if (handle->data == NULL) { return; @@ -324,10 +324,10 @@ static void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf if (nread > 0) { pBuf->len += nread; if (transReadComplete(pBuf)) { - tTrace("%s client conn %p read complete", CONN_GET_INST_LABEL(conn), conn); - clientHandleResp(conn); + tTrace("%s cli conn %p read complete", CONN_GET_INST_LABEL(conn), conn); + cliHandleResp(conn); } else { - tTrace("%s client conn %p read partial packet, continue to read", CONN_GET_INST_LABEL(conn), conn); + tTrace("%s cli conn %p read partial packet, continue to read", CONN_GET_INST_LABEL(conn), conn); } return; } @@ -340,13 +340,13 @@ static void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf return; } if (nread < 0) { - tError("%s client conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread)); + tError("%s cli conn %p read error: %s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(nread)); conn->broken = true; - clientHandleExcept(conn); + cliHandleExcept(conn); } } -static SCliConn* clientConnCreate(SCliThrdObj* pThrd) { +static SCliConn* cliCreateConn(SCliThrdObj* pThrd) { SCliConn* conn = calloc(1, sizeof(SCliConn)); // read/write stream handle conn->stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t)); @@ -362,40 +362,40 @@ static SCliConn* clientConnCreate(SCliThrdObj* pThrd) { transRefCliHandle(conn); return conn; } -static void clientConnDestroy(SCliConn* conn, bool clear) { - tTrace("%s client conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn); +static void cliDestroyConn(SCliConn* conn, bool clear) { + tTrace("%s cli conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn); QUEUE_REMOVE(&conn->conn); if (clear) { - uv_close((uv_handle_t*)conn->stream, clientDestroy); + uv_close((uv_handle_t*)conn->stream, cliDestroy); } } -static void clientDestroy(uv_handle_t* handle) { +static void cliDestroy(uv_handle_t* handle) { SCliConn* conn = handle->data; free(conn->stream); - tTrace("%s client conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn); + tTrace("%s cli conn %p destroy successfully", CONN_GET_INST_LABEL(conn), conn); free(conn); } -static void clientSendDataCb(uv_write_t* req, int status) { +static void cliSendCb(uv_write_t* req, int status) { SCliConn* pConn = req->data; if (status == 0) { - tTrace("%s client conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn); + tTrace("%s cli conn %p data already was written out", CONN_GET_INST_LABEL(pConn), pConn); SCliMsg* pMsg = pConn->data; if (pMsg == NULL) { return; } destroyUserdata(&pMsg->msg); } else { - tError("%s client conn %p failed to write: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status)); - clientHandleExcept(pConn); + tError("%s cli conn %p failed to write: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_err_name(status)); + cliHandleExcept(pConn); return; } - uv_read_start((uv_stream_t*)pConn->stream, clientAllocBufferCb, clientRecvCb); + uv_read_start((uv_stream_t*)pConn->stream, cliAllocBufferCb, cliRecvCb); } -static void clientSendData(SCliConn* pConn) { +static void cliSend(SCliConn* pConn) { CONN_HANDLE_BROKEN(pConn); SCliMsg* pCliMsg = pConn->data; @@ -432,22 +432,22 @@ static void clientSendData(SCliConn* pConn) { pHead->msgLen = (int32_t)htonl((uint32_t)msgLen); uv_buf_t wb = uv_buf_init((char*)pHead, msgLen); - tDebug("%s client conn %p %s is send to %s:%d, local info %s:%d", CONN_GET_INST_LABEL(pConn), pConn, + tDebug("%s cli conn %p %s is send to %s:%d, local info %s:%d", CONN_GET_INST_LABEL(pConn), pConn, TMSG_INFO(pHead->msgType), inet_ntoa(pConn->addr.sin_addr), ntohs(pConn->addr.sin_port), inet_ntoa(pConn->locaddr.sin_addr), ntohs(pConn->locaddr.sin_port)); - uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, clientSendDataCb); + uv_write(&pConn->writeReq, (uv_stream_t*)pConn->stream, &wb, 1, cliSendCb); return; _RETURE: return; } -static void clientConnCb(uv_connect_t* req, int status) { +static void cliConnCb(uv_connect_t* req, int status) { // impl later SCliConn* pConn = req->data; if (status != 0) { - tError("%s client conn %p failed to connect server: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status)); - clientHandleExcept(pConn); + tError("%s cli conn %p failed to connect server: %s", CONN_GET_INST_LABEL(pConn), pConn, uv_strerror(status)); + cliHandleExcept(pConn); return; } int addrlen = sizeof(pConn->addr); @@ -456,14 +456,14 @@ static void clientConnCb(uv_connect_t* req, int status) { addrlen = sizeof(pConn->locaddr); uv_tcp_getsockname((uv_tcp_t*)pConn->stream, (struct sockaddr*)&pConn->locaddr, &addrlen); - tTrace("%s client conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn); + tTrace("%s cli conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn); assert(pConn->stream == req->handle); - clientSendData(pConn); + cliSend(pConn); } -static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { - tDebug("client work thread %p start to quit", pThrd); +static void cliHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { + tDebug("cli work thread %p start to quit", pThrd); destroyCmsg(pMsg); destroyConnPool(pThrd->pool); @@ -472,57 +472,57 @@ static void clientHandleQuit(SCliMsg* pMsg, SCliThrdObj* pThrd) { pThrd->quit = true; uv_stop(pThrd->loop); } -static SCliConn* clientGetConn(SCliMsg* pMsg, SCliThrdObj* pThrd) { +static SCliConn* cliGetConn(SCliMsg* pMsg, SCliThrdObj* pThrd) { SCliConn* conn = NULL; if (pMsg->msg.handle != NULL) { conn = (SCliConn*)(pMsg->msg.handle); transUnrefCliHandle(conn); if (conn != NULL) { - tTrace("%s client conn %p reused", CONN_GET_INST_LABEL(conn), conn); + tTrace("%s cli conn %p reused", CONN_GET_INST_LABEL(conn), conn); } } else { STransConnCtx* pCtx = pMsg->ctx; conn = getConnFromPool(pThrd->pool, pCtx->ip, pCtx->port); - if (conn != NULL) tTrace("%s client conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); + if (conn != NULL) tTrace("%s cli conn %p get from conn pool", CONN_GET_INST_LABEL(conn), conn); } return conn; } -static void clientHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { +static void cliHandleReq(SCliMsg* pMsg, SCliThrdObj* pThrd) { uint64_t et = taosGetTimestampUs(); uint64_t el = et - pMsg->st; - tTrace("%s client msg tran time cost: %" PRIu64 "us", ((SRpcInfo*)pThrd->pTransInst)->label, el); + tTrace("%s cli msg tran time cost: %" PRIu64 "us", ((SRpcInfo*)pThrd->pTransInst)->label, el); STransConnCtx* pCtx = pMsg->ctx; SRpcInfo* pTransInst = pThrd->pTransInst; - SCliConn* conn = clientGetConn(pMsg, pThrd); + SCliConn* conn = cliGetConn(pMsg, pThrd); if (conn != NULL) { conn->data = pMsg; transDestroyBuffer(&conn->readBuf); - clientSendData(conn); + cliSend(conn); } else { - conn = clientConnCreate(pThrd); + conn = cliCreateConn(pThrd); conn->data = pMsg; int ret = transSetConnOption((uv_tcp_t*)conn->stream); if (ret) { - tError("%s client conn %p failed to set conn option, errmsg %s", pTransInst->label, conn, uv_err_name(ret)); + tError("%s cli conn %p failed to set conn option, errmsg %s", pTransInst->label, conn, uv_err_name(ret)); } struct sockaddr_in addr; uv_ip4_addr(pMsg->ctx->ip, pMsg->ctx->port, &addr); // handle error in callback if fail to connect - tTrace("%s client conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port); - uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, clientConnCb); + tTrace("%s cli conn %p try to connect to %s:%d", pTransInst->label, conn, pMsg->ctx->ip, pMsg->ctx->port); + uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb); } conn->hThrdIdx = pCtx->hThrdIdx; } -static void clientAsyncCb(uv_async_t* handle) { +static void cliAsyncCb(uv_async_t* handle) { SAsyncItem* item = handle->data; SCliThrdObj* pThrd = item->pThrd; SCliMsg* pMsg = NULL; - queue wq; // batch process to avoid to lock/unlock frequently + queue wq; pthread_mutex_lock(&item->mtx); QUEUE_MOVE(&item->qmsg, &wq); pthread_mutex_unlock(&item->mtx); @@ -534,25 +534,25 @@ static void clientAsyncCb(uv_async_t* handle) { SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q); if (pMsg->ctx == NULL) { - clientHandleQuit(pMsg, pThrd); + cliHandleQuit(pMsg, pThrd); } else { - clientHandleReq(pMsg, pThrd); + cliHandleReq(pMsg, pThrd); } count++; } if (count >= 2) { - tTrace("client process batch size: %d", count); + tTrace("cli process batch size: %d", count); } } -static void* clientThread(void* arg) { +static void* cliWorkThread(void* arg) { SCliThrdObj* pThrd = (SCliThrdObj*)arg; - setThreadName("trans-client-work"); + setThreadName("trans-cli-work"); 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)); + SCliObj* cli = calloc(1, sizeof(SCliObj)); SRpcInfo* pRpc = shandle; memcpy(cli->label, label, strlen(label)); @@ -564,9 +564,9 @@ void* taosInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, pThrd->nextTimeout = taosGetTimestampMs() + CONN_PERSIST_TIME(pRpc->idleTime); pThrd->pTransInst = shandle; - int err = pthread_create(&pThrd->thread, NULL, clientThread, (void*)(pThrd)); + int err = pthread_create(&pThrd->thread, NULL, cliWorkThread, (void*)(pThrd)); if (err == 0) { - tDebug("success to create tranport-client thread %d", i); + tDebug("success to create tranport-cli thread %d", i); } cli->pThreadObj[i] = pThrd; } @@ -591,13 +591,14 @@ static void destroyCmsg(SCliMsg* pMsg) { static SCliThrdObj* createThrdObj() { SCliThrdObj* pThrd = (SCliThrdObj*)calloc(1, sizeof(SCliThrdObj)); + QUEUE_INIT(&pThrd->msg); pthread_mutex_init(&pThrd->msgMtx, NULL); pThrd->loop = (uv_loop_t*)malloc(sizeof(uv_loop_t)); uv_loop_init(pThrd->loop); - pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, clientAsyncCb); + pThrd->asyncPool = transCreateAsyncPool(pThrd->loop, 5, pThrd, cliAsyncCb); uv_timer_init(pThrd->loop, &pThrd->timer); pThrd->timer.data = pThrd; @@ -628,21 +629,21 @@ static void transDestroyConnCtx(STransConnCtx* ctx) { free(ctx); } // -static void clientSendQuit(SCliThrdObj* thrd) { +static void cliSendQuit(SCliThrdObj* thrd) { // cli can stop gracefully SCliMsg* msg = calloc(1, sizeof(SCliMsg)); transSendAsync(thrd->asyncPool, &msg->q); } void taosCloseClient(void* arg) { - SClientObj* cli = arg; + SCliObj* cli = arg; for (int i = 0; i < cli->numOfThreads; i++) { - clientSendQuit(cli->pThreadObj[i]); + cliSendQuit(cli->pThreadObj[i]); destroyThrdObj(cli->pThreadObj[i]); } free(cli->pThreadObj); free(cli); } -static int clientRBChoseIdx(SRpcInfo* pTransInst) { +static int cliRBChoseIdx(SRpcInfo* pTransInst) { int64_t index = pTransInst->index; if (pTransInst->index++ >= pTransInst->numOfThreads) { pTransInst->index = 0; @@ -662,7 +663,7 @@ void transUnrefCliHandle(void* handle) { } int ref = T_REF_DEC((SCliConn*)handle); if (ref == 0) { - clientConnDestroy((SCliConn*)handle, true); + cliDestroyConn((SCliConn*)handle, true); } // unref cli handle @@ -676,7 +677,7 @@ void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* int index = CONN_HOST_THREAD_INDEX(pMsg->handle); if (index == -1) { - index = clientRBChoseIdx(pTransInst); + index = cliRBChoseIdx(pTransInst); } int32_t flen = 0; if (transCompressMsg(pMsg->pCont, pMsg->contLen, &flen)) { @@ -697,7 +698,7 @@ void rpcSendRequest(void* shandle, const SEpSet* pEpSet, SRpcMsg* pMsg, int64_t* cliMsg->msg = *pMsg; cliMsg->st = taosGetTimestampUs(); - SCliThrdObj* thrd = ((SClientObj*)pTransInst->tcphandle)->pThreadObj[index]; + SCliThrdObj* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[index]; transSendAsync(thrd->asyncPool, &(cliMsg->q)); } @@ -709,7 +710,7 @@ void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pReq, SRpcMsg* pRsp) { int index = CONN_HOST_THREAD_INDEX(pReq->handle); if (index == -1) { - index = clientRBChoseIdx(pTransInst); + index = cliRBChoseIdx(pTransInst); } STransConnCtx* pCtx = calloc(1, sizeof(STransConnCtx)); @@ -727,7 +728,7 @@ void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pReq, SRpcMsg* pRsp) { cliMsg->msg = *pReq; cliMsg->st = taosGetTimestampUs(); - SCliThrdObj* thrd = ((SClientObj*)pTransInst->tcphandle)->pThreadObj[index]; + SCliThrdObj* thrd = ((SCliObj*)pTransInst->tcphandle)->pThreadObj[index]; transSendAsync(thrd->asyncPool, &(cliMsg->q)); tsem_t* pSem = pCtx->pSem; tsem_wait(pSem); From 5f376274be94397fd9644601923ff88c56292a02 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 12 Mar 2022 15:28:15 +0800 Subject: [PATCH 29/29] [td-13039] fix bug in project query. --- source/libs/executor/inc/executorimpl.h | 3 ++- source/libs/executor/src/executorimpl.c | 32 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index d0c1a3f0b5..ee841e3ce9 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -334,9 +334,10 @@ typedef struct SOperatorInfo { struct SOperatorInfo** pDownstream; // downstram pointer list int32_t numOfDownstream; // number of downstream. The value is always ONE expect for join operator - __optr_open_fn_t openFn; __optr_fn_t getNextFn; + __optr_fn_t cleanupFn; __optr_close_fn_t closeFn; + __optr_open_fn_t _openFn; // DO NOT invoke this function directly } SOperatorInfo; typedef struct { diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index ec220098ac..420d70233c 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -224,9 +224,12 @@ static void doSetOperatorCompleted(SOperatorInfo* pOperator) { } } +#define OPTR_IS_OPENED(_optr) (((_optr)->status & OP_OPENED) == OP_OPENED) +#define OPTR_SET_OPENED(_optr) ((_optr)->status |= OP_OPENED) + static int32_t operatorDummyOpenFn(void* param) { SOperatorInfo* pOperator = (SOperatorInfo*) param; - pOperator->status = OP_OPENED; + OPTR_SET_OPENED(pOperator); return TSDB_CODE_SUCCESS; } @@ -4734,6 +4737,11 @@ static SSDataBlock* doTableScan(void* param, bool *newgroup) { STableScanInfo *pTableScanInfo = pOperator->info; SExecTaskInfo *pTaskInfo = pOperator->pTaskInfo; + pTaskInfo->code = pOperator->_openFn(pOperator); + if (pTaskInfo->code != TSDB_CODE_SUCCESS) { + return NULL; + } + // The read handle is not initialized yet, since no qualified tables exists if (pTableScanInfo->pTsdbReadHandle == NULL) { return NULL; @@ -4851,6 +4859,11 @@ static SSDataBlock* doStreamBlockScan(void* param, bool* newgroup) { SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo; SStreamBlockScanInfo* pInfo = pOperator->info; + pTaskInfo->code = pOperator->_openFn(pOperator); + if (pTaskInfo->code != TSDB_CODE_SUCCESS) { + return NULL; + } + SDataBlockInfo* pBlockInfo = &pInfo->pRes->info; pBlockInfo->rows = 0; @@ -5159,7 +5172,7 @@ static SSDataBlock* seqLoadRemoteData(SOperatorInfo *pOperator) { static int32_t prepareLoadRemoteData(void* param) { SOperatorInfo *pOperator = (SOperatorInfo*) param; - if ((pOperator->status & OP_OPENED) == OP_OPENED) { + if (OPTR_IS_OPENED(pOperator)) { return TSDB_CODE_SUCCESS; } @@ -5173,7 +5186,7 @@ static int32_t prepareLoadRemoteData(void* param) { } } - pOperator->status = OP_OPENED; + OPTR_SET_OPENED(pOperator); return TSDB_CODE_SUCCESS; } @@ -5183,12 +5196,13 @@ static SSDataBlock* doLoadRemoteData(void* param, bool* newgroup) { SExchangeInfo *pExchangeInfo = pOperator->info; SExecTaskInfo *pTaskInfo = pOperator->pTaskInfo; - size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources); - if ((pOperator->status & OP_OPENED) != OP_OPENED) { - pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC; // TODO add a new error code + int32_t code = pOperator->_openFn(pOperator); + if (code != TSDB_CODE_SUCCESS) { + pTaskInfo->code = code; return NULL; } + size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources); if (pOperator->status == OP_EXEC_DONE) { qDebug("%s all %"PRIzu" source(s) are exhausted, total rows:%"PRIu64" bytes:%"PRIu64", elapsed:%.2f ms", GET_TASKID(pTaskInfo), totalSources, pExchangeInfo->totalRows, pExchangeInfo->totalSize, pExchangeInfo->totalElapsed/1000.0); @@ -5286,7 +5300,7 @@ SOperatorInfo* createExchangeOperatorInfo(const SNodeList* pSources, SSDataBlock pOperator->info = pInfo; pOperator->numOfOutput = size; pOperator->pTaskInfo = pTaskInfo; - pOperator->openFn = prepareLoadRemoteData; // assign a dummy function. + pOperator->_openFn = prepareLoadRemoteData; // assign a dummy function. pOperator->getNextFn = doLoadRemoteData; pOperator->closeFn = destroyExchangeOperatorInfo; @@ -5377,7 +5391,7 @@ SOperatorInfo* createTableScanOperatorInfo(void* pTsdbReadHandle, int32_t order, pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = numOfOutput; - pOperator->openFn = operatorDummyOpenFn; + pOperator->_openFn = operatorDummyOpenFn; pOperator->getNextFn = doTableScan; pOperator->closeFn = operatorDummyCloseFn; pOperator->pTaskInfo = pTaskInfo; @@ -5461,7 +5475,7 @@ SOperatorInfo* createStreamScanOperatorInfo(void *streamReadHandle, SSDataBlock* pOperator->status = OP_NOT_OPENED; pOperator->info = pInfo; pOperator->numOfOutput = pResBlock->info.numOfCols; - pOperator->openFn = operatorDummyOpenFn; + pOperator->_openFn = operatorDummyOpenFn; pOperator->getNextFn = doStreamBlockScan; pOperator->closeFn = operatorDummyCloseFn;