diff --git a/include/libs/nodes/cmdnodes.h b/include/libs/nodes/cmdnodes.h index ded12620d61cdde001ed0b2e37c32866fd4215a0..48a3a9bda8b200cab6c46520be64b017fcd98c00 100644 --- a/include/libs/nodes/cmdnodes.h +++ b/include/libs/nodes/cmdnodes.h @@ -199,6 +199,7 @@ typedef struct SIndexOptions { typedef struct SCreateIndexStmt { ENodeType type; EIndexType indexType; + bool ignoreExists; char indexName[TSDB_INDEX_NAME_LEN]; char tableName[TSDB_TABLE_NAME_LEN]; SNodeList* pCols; @@ -207,6 +208,7 @@ typedef struct SCreateIndexStmt { typedef struct SDropIndexStmt { ENodeType type; + bool ignoreNotExists; char indexName[TSDB_INDEX_NAME_LEN]; char tableName[TSDB_TABLE_NAME_LEN]; } SDropIndexStmt; diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 860c77de15ffdaa9c1678c2b1185c618cae52f0a..2254298e5c4d8dbe73f42760fac34f8df82a56a7 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -60,6 +60,8 @@ int32_t qParseQuerySql(SParseContext* pCxt, SQuery** pQuery); void qDestroyQuery(SQuery* pQueryNode); +int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema); + #ifdef __cplusplus } #endif diff --git a/source/client/test/clientTests.cpp b/source/client/test/clientTests.cpp index 87a3e651fe227c2c1bd071f4f3a2538046371921..629dd90da3b531f373ff353ea6bd4123215f65aa 100644 --- a/source/client/test/clientTests.cpp +++ b/source/client/test/clientTests.cpp @@ -661,7 +661,7 @@ TEST(testCase, agg_query_tables) { TAOS_RES* pRes = taos_query(pConn, "use abc1"); taos_free_result(pRes); - pRes = taos_query(pConn, "select k from tm0"); + pRes = taos_query(pConn, "select count(*) from tu"); if (taos_errno(pRes) != 0) { printf("failed to select from table, reason:%s\n", taos_errstr(pRes)); taos_free_result(pRes); diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index da1adf0c59187bf3cfdf0af9236ca84133399118..510a2809e74b92acecc510b928d9fe288ba9f7db 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -562,8 +562,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio // not enough time range if (start < 0 || INT64_MAX - start > pInterval->interval - 1) { - end = start + pInterval->interval - 1; - + end = taosTimeAdd(start, pInterval->interval, pInterval->intervalUnit, precision) - 1; while (end < t && ((start + pInterval->sliding) <= INT64_MAX)) { // move forward to the correct time window start += pInterval->sliding; @@ -587,7 +586,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio start = taosTimeAdd(start, -pInterval->interval, pInterval->intervalUnit, precision); } else { // try to move current window to the left-hande-side, due to the offset effect. - int64_t end = start + pInterval->interval - 1; + int64_t end = taosTimeAdd(start, pInterval->interval, pInterval->intervalUnit, precision) - 1; ASSERT(end >= t); end = taosTimeAdd(end, -pInterval->sliding, pInterval->slidingUnit, precision); if (end >= t) { diff --git a/source/dnode/mnode/impl/CMakeLists.txt b/source/dnode/mnode/impl/CMakeLists.txt index 514bba19f48cf1625c9d4b74c8aabb6a296ace15..dd2caf1f7f86efc1d5fd8dadb21b5f82715b853e 100644 --- a/source/dnode/mnode/impl/CMakeLists.txt +++ b/source/dnode/mnode/impl/CMakeLists.txt @@ -6,7 +6,7 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( - mnode scheduler sdb wal transport cjson sync monitor + mnode scheduler sdb wal transport cjson sync monitor parser ) if(${BUILD_TEST}) diff --git a/source/dnode/mnode/impl/src/mndSma.c b/source/dnode/mnode/impl/src/mndSma.c index ebd34fb2a526b915c09ffdb58660fd16a493ccbd..4e3ab8eae49af393252ab533e949e6b9cf6b096d 100644 --- a/source/dnode/mnode/impl/src/mndSma.c +++ b/source/dnode/mnode/impl/src/mndSma.c @@ -69,7 +69,7 @@ void mndCleanupSma(SMnode *pMnode) {} static SSdbRaw *mndSmaActionEncode(SSmaObj *pSma) { terrno = TSDB_CODE_OUT_OF_MEMORY; - int32_t size = sizeof(SSmaObj) + pSma->exprLen + pSma->tagsFilterLen + TSDB_SMA_RESERVE_SIZE; + int32_t size = sizeof(SSmaObj) + pSma->exprLen + pSma->tagsFilterLen + pSma->sqlLen + pSma->astLen + TSDB_SMA_RESERVE_SIZE; SSdbRaw *pRaw = sdbAllocRaw(SDB_SMA, TSDB_SMA_VER_NUMBER, size); if (pRaw == NULL) goto _OVER; diff --git a/source/dnode/mnode/impl/src/mndStream.c b/source/dnode/mnode/impl/src/mndStream.c index bb7891ad2d052bb744230035d606523abe29a74f..e3d77ced0de12f3f7c43ee293cb6ad01ed2be8ab 100644 --- a/source/dnode/mnode/impl/src/mndStream.c +++ b/source/dnode/mnode/impl/src/mndStream.c @@ -14,6 +14,7 @@ */ #include "mndStream.h" +#include "parser.h" #include "mndAuth.h" #include "mndDb.h" #include "mndDnode.h" @@ -218,28 +219,6 @@ static int32_t mndCheckCreateStreamReq(SCMCreateStreamReq *pCreate) { return 0; } -static SArray *mndExtractNamesFromAst(const SNode *pAst) { - if (pAst->type != QUERY_NODE_SELECT_STMT) return NULL; - - SArray *names = taosArrayInit(0, sizeof(void *)); - if (names == NULL) { - return NULL; - } - SSelectStmt *pSelect = (SSelectStmt *)pAst; - SNodeList *pNodes = pSelect->pProjectionList; - SListCell *pCell = pNodes->pHead; - while (pCell != NULL) { - if (pCell->pNode->type != QUERY_NODE_FUNCTION) { - continue; - } - SFunctionNode *pFunction = (SFunctionNode *)pCell->pNode; - char *name = strdup(pFunction->node.aliasName); - taosArrayPush(names, &name); - pCell = pCell->pNext; - } - return names; -} - static int32_t mndStreamGetPlanString(const char *ast, char **pStr) { if (NULL == ast) { return TSDB_CODE_SUCCESS; @@ -269,18 +248,23 @@ static int32_t mndStreamGetPlanString(const char *ast, char **pStr) { int32_t mndAddStreamToTrans(SMnode *pMnode, SStreamObj *pStream, const char *ast, STrans *pTrans) { SNode *pAst = NULL; +#if 1 // TODO: remove debug info later + printf("ast = %s\n", ast); +#endif if (nodesStringToNode(ast, &pAst) < 0) { return -1; } #if 1 - SArray *names = mndExtractNamesFromAst(pAst); + SSchemaWrapper sw = {0}; + qExtractResultSchema(pAst, (int32_t*)&sw.nCols, &sw.pSchema); + printf("|"); - for (int i = 0; i < taosArrayGetSize(names); i++) { - printf(" %15s |", (char *)taosArrayGetP(names, i)); + for (int i = 0; i < sw.nCols; i++) { + printf(" %15s |", (char *)sw.pSchema[i].name); } printf("\n=======================================================\n"); - pStream->ColAlias = names; + pStream->ColAlias = NULL; #endif if (TSDB_CODE_SUCCESS != mndStreamGetPlanString(ast, &pStream->physicalPlan)) { diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 2d7e2eedc671c269a3177c6e9a1a116a551d06ba..c582873315d38efb14e904e436bd0ef3b055270f 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -665,6 +665,9 @@ SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SExprInfo* pEx SArray* pGroupColList, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createFillOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfCols, SInterval* pInterval, SSDataBlock* pResBlock, int32_t fillType, char* fillVal, bool multigroupResult, SExecTaskInfo* pTaskInfo); +SOperatorInfo* createDistinctOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, SExprInfo* pExpr, + int32_t numOfOutput); +SOperatorInfo* createTableBlockInfoScanOperator(void* pTsdbReadHandle, STaskRuntimeEnv* pRuntimeEnv); SOperatorInfo* createTableSeqScanOperatorInfo(void* pTsdbReadHandle, STaskRuntimeEnv* pRuntimeEnv); SOperatorInfo* createAllTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, @@ -675,9 +678,7 @@ SOperatorInfo* createMultiTableTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntim SOperatorInfo* createAllMultiTableTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfOutput); SOperatorInfo* createTagScanOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput); -SOperatorInfo* createDistinctOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, SExprInfo* pExpr, - int32_t numOfOutput); -SOperatorInfo* createTableBlockInfoScanOperator(void* pTsdbReadHandle, STaskRuntimeEnv* pRuntimeEnv); + SOperatorInfo* createMultiwaySortOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput, int32_t numOfRows, void* merger); SOperatorInfo* createGlobalAggregateOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 177d09be764b046ae7683bbba4232e3603d67058..cea9b3ce115ee1a7ebbf7326e84c94e72d1be8a2 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -674,12 +674,7 @@ static void getInitialStartTimeWindow(SInterval* pInterval, int32_t precision, T int64_t key = w->skey; while(key < ts) { // moving towards end - if (pInterval->intervalUnit == 'n' || pInterval->intervalUnit == 'y') { - key = taosTimeAdd(key, pInterval->sliding, pInterval->slidingUnit, precision); - } else { - key += pInterval->sliding; - } - + key = taosTimeAdd(key, pInterval->sliding, pInterval->slidingUnit, precision); if (key >= ts) { break; } @@ -695,12 +690,7 @@ static STimeWindow getActiveTimeWindow(SResultRowInfo * pResultRowInfo, int64_t if (pResultRowInfo->curPos == -1) { // the first window, from the previous stored value getInitialStartTimeWindow(pInterval, precision, ts, &w, win->ekey, true); - - if (pInterval->intervalUnit == 'n' || pInterval->intervalUnit == 'y') { - w.ekey = taosTimeAdd(w.skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; - } else { - w.ekey = w.skey + pInterval->interval - 1; - } + w.ekey = taosTimeAdd(w.skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; } else { w = getResultRow(pResultRowInfo, pResultRowInfo->curPos)->win; } @@ -722,7 +712,7 @@ static STimeWindow getActiveTimeWindow(SResultRowInfo * pResultRowInfo, int64_t } w.skey = st; - w.ekey = w.skey + pInterval->interval - 1; + w.ekey = taosTimeAdd(w.skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; } } return w; @@ -2208,7 +2198,7 @@ static SqlFunctionCtx* createSqlFunctionCtx_rv(SExprInfo* pExprInfo, int32_t num } for(int32_t i = 1; i < numOfOutput; ++i) { - (*rowCellInfoOffset)[i] = (int32_t)((*rowCellInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) + pFuncCtx[i].resDataInfo.interBufSize); + (*rowCellInfoOffset)[i] = (int32_t)((*rowCellInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) + pFuncCtx[i - 1].resDataInfo.interBufSize); } setCtxTagColumnInfo(pFuncCtx, numOfOutput); @@ -2407,7 +2397,7 @@ static bool isCachedLastQuery(STaskAttr *pQueryAttr) { ///////////////////////////////////////////////////////////////////////////////////////////// //todo refactor : return window void getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key, int64_t keyFirst, int64_t keyLast, STimeWindow *win) { - assert(key >= keyFirst && key <= keyLast && pInterval->sliding <= pInterval->interval); + ASSERT(key >= keyFirst && key <= keyLast); win->skey = taosTimeTruncate(key, pInterval, precision); /* @@ -2417,10 +2407,8 @@ void getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t ke if (keyFirst > (INT64_MAX - pInterval->interval)) { assert(keyLast - keyFirst < pInterval->interval); win->ekey = INT64_MAX; - } else if (pInterval->intervalUnit == 'n' || pInterval->intervalUnit == 'y') { - win->ekey = taosTimeAdd(win->skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; } else { - win->ekey = win->skey + pInterval->interval - 1; + win->ekey = taosTimeAdd(win->skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; } } diff --git a/source/libs/function/CMakeLists.txt b/source/libs/function/CMakeLists.txt index a10a542b6b2e094166202c6cb299801576d15234..6305d242acc49833001db5875cb731b762170079 100644 --- a/source/libs/function/CMakeLists.txt +++ b/source/libs/function/CMakeLists.txt @@ -1,12 +1,55 @@ aux_source_directory(src FUNCTION_SRC) +list(REMOVE_ITEM FUNCTION_SRC src/udfd.c) add_library(function STATIC ${FUNCTION_SRC}) target_include_directories( function - PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/function" + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/function" + "${CMAKE_SOURCE_DIR}/contrib/libuv/include" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( function + PUBLIC uv_a PRIVATE os util common nodes -) \ No newline at end of file +) + +add_executable(runUdf test/runUdf.c) +target_include_directories( + runUdf + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/function" + "${CMAKE_SOURCE_DIR}/contrib/libuv/include" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + runUdf + PUBLIC uv_a + PRIVATE os util common nodes function +) + +add_library(udf1 MODULE test/udf1.c) +target_include_directories( + udf1 + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/function" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +#SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/build/bin) +add_executable(udfd src/udfd.c) +target_include_directories( + udfd + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/function" + "${CMAKE_SOURCE_DIR}/contrib/libuv/include" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) + +target_link_libraries( + udfd + PUBLIC uv_a + PRIVATE os util common nodes function +) + diff --git a/source/libs/function/inc/tudf.h b/source/libs/function/inc/tudf.h index 163fbdf4bb204be1c00022505ea13cf32854ff99..72875239d2c7f335d08ccf1c3af73108e4b04ef9 100644 --- a/source/libs/function/inc/tudf.h +++ b/source/libs/function/inc/tudf.h @@ -20,68 +20,116 @@ extern "C" { #endif -#include "os.h" -#include "taoserror.h" +//====================================================================================== +//begin API to taosd and qworker +/** + * start udf dameon service + * @return error code + */ +int32_t startUdfService(); + +/** + * stop udf dameon service + * @return error code + */ +int32_t stopUdfService(); enum { - TSDB_UDF_FUNC_NORMAL = 0, - TSDB_UDF_FUNC_INIT, - TSDB_UDF_FUNC_FINALIZE, - TSDB_UDF_FUNC_MERGE, - TSDB_UDF_FUNC_DESTROY, - TSDB_UDF_FUNC_MAX_NUM + TSDB_UDF_TYPE_SCALAR = 0, + TSDB_UDF_TYPE_AGGREGATE = 1 }; -typedef struct SUdfInit { - int32_t maybe_null; /* 1 if function can return NULL */ - uint32_t decimals; /* for real functions */ - uint64_t length; /* For string functions */ - char* ptr; /* free pointer for function data */ - int32_t const_item; /* 0 if result is independent of arguments */ - - // script like lua/javascript - void* script_ctx; - void (*destroyCtxFunc)(void* script_ctx); -} SUdfInit; +enum { + TSDB_UDF_SCRIPT_BIN_LIB = 0, + TSDB_UDF_SCRIPT_LUA = 1, +}; typedef struct SUdfInfo { - int32_t functionId; // system assigned function id - int32_t funcType; // scalar function or aggregate function - int8_t resType; // result type - int16_t resBytes; // result byte - int32_t contLen; // content length - int32_t bufSize; // interbuf size - char* name; // function name - void* handle; // handle loaded in mem - void* funcs[TSDB_UDF_FUNC_MAX_NUM]; // function ptr - - // for script like lua/javascript only - int isScript; - void* pScriptCtx; - - SUdfInit init; - char* content; - char* path; + char *udfName; // function name + int32_t udfType; // scalar function or aggregate function + int8_t scriptType; + char *path; + + int8_t resType; // result type + int16_t resBytes; // result byte + int32_t bufSize; //interbuf size + } SUdfInfo; +typedef void *UdfHandle; + +/** + * setup udf + * @param udf, in + * @param handle, out + * @return error code + */ +int32_t setupUdf(SUdfInfo* udf, UdfHandle *handle); + + +enum { + TSDB_UDF_STEP_NORMAL = 0, + TSDB_UDF_STEP_MERGE, + TSDb_UDF_STEP_FINALIZE, + TSDB_UDF_STEP_MAX_NUM +}; +/** + * call udf + * @param handle udf handle + * @param step + * @param state + * @param stateSize + * @param input + * @param newstate + * @param newStateSize + * @param output + * @return error code + */ + +//TODO: must change the following after metadata flow and data flow between qworker and udfd is well defined +typedef struct SUdfDataBlock { + char* data; + int32_t size; +} SUdfDataBlock; + +int32_t callUdf(UdfHandle handle, int8_t step, char *state, int32_t stateSize, SUdfDataBlock input, char **newstate, + int32_t *newStateSize, SUdfDataBlock *output); + +/** + * tearn down udf + * @param handle + * @return + */ +int32_t teardownUdf(UdfHandle handle); + +// end API to taosd and qworker +//============================================================================================================================= +// TODO: Must change +// begin API to UDF writer. + // script -typedef int32_t (*scriptInitFunc)(void* pCtx); -typedef void (*scriptNormalFunc)(void* pCtx, char* data, int16_t iType, int16_t iBytes, int32_t numOfRows, - int64_t* ptList, int64_t key, char* dataOutput, char* tsOutput, int32_t* numOfOutput, - int16_t oType, int16_t oBytes); -typedef void (*scriptFinalizeFunc)(void* pCtx, int64_t key, char* dataOutput, int32_t* numOfOutput); -typedef void (*scriptMergeFunc)(void* pCtx, char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput); -typedef void (*scriptDestroyFunc)(void* pCtx); +//typedef int32_t (*scriptInitFunc)(void* pCtx); +//typedef void (*scriptNormalFunc)(void* pCtx, char* data, int16_t iType, int16_t iBytes, int32_t numOfRows, +// int64_t* ptList, int64_t key, char* dataOutput, char* tsOutput, int32_t* numOfOutput, +// int16_t oType, int16_t oBytes); +//typedef void (*scriptFinalizeFunc)(void* pCtx, int64_t key, char* dataOutput, int32_t* numOfOutput); +//typedef void (*scriptMergeFunc)(void* pCtx, char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput); +//typedef void (*scriptDestroyFunc)(void* pCtx); // dynamic lib -typedef void (*udfNormalFunc)(char* data, int16_t itype, int16_t iBytes, int32_t numOfRows, int64_t* ts, - char* dataOutput, char* interBuf, char* tsOutput, int32_t* numOfOutput, int16_t oType, - int16_t oBytes, SUdfInit* buf); -typedef int32_t (*udfInitFunc)(SUdfInit* data); -typedef void (*udfFinalizeFunc)(char* dataOutput, char* interBuf, int32_t* numOfOutput, SUdfInit* buf); -typedef void (*udfMergeFunc)(char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput, SUdfInit* buf); -typedef void (*udfDestroyFunc)(SUdfInit* buf); +typedef int32_t (*TUdfInitFunc)(); +typedef void (*TUdfDestroyFunc)(); + +typedef void (*TUdfFunc)(int8_t step, + char *state, int32_t stateSize, SUdfDataBlock input, + char **newstate, int32_t *newStateSize, SUdfDataBlock *output); + +//typedef void (*udfMergeFunc)(char *data, int32_t numOfRows, char *dataOutput, int32_t* numOfOutput); +//typedef void (*udfFinalizeFunc)(char* state, int32_t stateSize, SUdfDataBlock *output); + +// end API to UDF writer +//======================================================================================================================= #ifdef __cplusplus } diff --git a/source/libs/function/inc/tudfInt.h b/source/libs/function/inc/tudfInt.h new file mode 100644 index 0000000000000000000000000000000000000000..5f757c1ef0a36d47d52a1b74670d3f1b888b676f --- /dev/null +++ b/source/libs/function/inc/tudfInt.h @@ -0,0 +1,100 @@ +/* + * 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 . + */ + +#ifndef TDENGINE_TUDF_INT_H +#define TDENGINE_TUDF_INT_H + +#ifdef __cplusplus +extern "C" { +#endif + +//TODO replaces them with fnDebug +//#define debugPrint(...) taosPrintLog("Function", DEBUG_INFO, 135, __VA_ARGS__) +#define debugPrint(...) {fprintf(stderr, __VA_ARGS__);fprintf(stderr, "\n");} +enum { + UDF_TASK_SETUP = 0, + UDF_TASK_CALL = 1, + UDF_TASK_TEARDOWN = 2 + +}; + +typedef struct SUdfSetupRequest { + char udfName[16]; // + int8_t scriptType; // 0:c, 1: lua, 2:js + int8_t udfType; //udaf, udf + int16_t pathSize; + char *path; +} SUdfSetupRequest; + +typedef struct SUdfSetupResponse { + int64_t udfHandle; +} SUdfSetupResponse; + + +typedef struct SUdfCallRequest { + int64_t udfHandle; + int8_t step; + + int32_t inputBytes; + char *input; + + int32_t stateBytes; + char *state; +} SUdfCallRequest; + + +typedef struct SUdfCallResponse { + int32_t outputBytes; + char *output; + int32_t newStateBytes; + char *newState; +} SUdfCallResponse; + + +typedef struct SUdfTeardownRequest { + int64_t udfHandle; +} SUdfTeardownRequest; + + +typedef struct SUdfTeardownResponse { +} SUdfTeardownResponse; + +typedef struct SUdfRequest { + int32_t msgLen; + int64_t seqNum; + + int8_t type; + void *subReq; +} SUdfRequest; + +typedef struct SUdfResponse { + int32_t msgLen; + int64_t seqNum; + + int8_t type; + int32_t code; + void *subRsp; +} SUdfResponse; + +int32_t decodeRequest(char *buf, int32_t bufLen, SUdfRequest **pRequest); +int32_t encodeResponse(char **buf, int32_t *bufLen, SUdfResponse *response); +int32_t encodeRequest(char **buf, int32_t *bufLen, SUdfRequest *request); +int32_t decodeResponse(char *buf, int32_t bufLen, SUdfResponse **pResponse); + +#ifdef __cplusplus +} +#endif + +#endif // TDENGINE_TUDF_INT_H diff --git a/source/libs/function/inc/udfc.h b/source/libs/function/inc/udfc.h new file mode 100644 index 0000000000000000000000000000000000000000..4d2aeb7049c21a07d1875c2ad9169ee3b5c9212b --- /dev/null +++ b/source/libs/function/inc/udfc.h @@ -0,0 +1,110 @@ +// +// Created by shenglian on 28/02/22. +// + +#ifndef UDF_UDF_H +#define UDF_UDF_H + +#include +#define DEBUG +#ifdef DEBUG +#define debugPrint(...) fprintf(__VA_ARGS__) +#else +#define debugPrint(...) /**/ +#endif + +enum { + UDF_TASK_SETUP = 0, + UDF_TASK_CALL = 1, + UDF_TASK_TEARDOWN = 2 + +}; + +typedef struct SSDataBlock{ + char *data; + int32_t size; +} SSDataBlock; + +typedef struct SUdfInfo { + char *udfName; + char *path; +} SUdfInfo; + +typedef void *UdfHandle; + +int32_t startUdfService(); + +int32_t stopUdfService(); + +//int32_t setupUdf(SUdfInfo *udf, int32_t numOfUdfs, UdfHandle *handles); + +int32_t setupUdf(SUdfInfo* udf, UdfHandle* handle); + +int32_t callUdf(UdfHandle handle, int8_t step, char *state, int32_t stateSize, SSDataBlock input, char **newstate, + int32_t *newStateSize, SSDataBlock *output); + +int32_t teardownUdf(UdfHandle handle); + +typedef struct SUdfSetupRequest { + char udfName[16]; // + int8_t scriptType; // 0:c, 1: lua, 2:js + int8_t udfType; //udaf, udf, udtf + int16_t pathSize; + char *path; +} SUdfSetupRequest; + +typedef struct SUdfSetupResponse { + int64_t udfHandle; +} SUdfSetupResponse; + + +typedef struct SUdfCallRequest { + int64_t udfHandle; + int8_t step; + + int32_t inputBytes; + char *input; + + int32_t stateBytes; + char *state; +} SUdfCallRequest; + + +typedef struct SUdfCallResponse { + int32_t outputBytes; + char *output; + int32_t newStateBytes; + char *newState; +} SUdfCallResponse; + + +typedef struct SUdfTeardownRequest { + int64_t udfHandle; +} SUdfTeardownRequest; + + +typedef struct SUdfTeardownResponse { +} SUdfTeardownResponse; + +typedef struct SUdfRequest { + int32_t msgLen; + int64_t seqNum; + + int8_t type; + void *subReq; +} SUdfRequest; + +typedef struct SUdfResponse { + int32_t msgLen; + int64_t seqNum; + + int8_t type; + int32_t code; + void *subRsp; +} SUdfResponse; + +int32_t decodeRequest(char *buf, int32_t bufLen, SUdfRequest **pRequest); +int32_t encodeResponse(char **buf, int32_t *bufLen, SUdfResponse *response); +int32_t encodeRequest(char **buf, int32_t *bufLen, SUdfRequest *request); +int32_t decodeResponse(char *buf, int32_t bufLen, SUdfResponse **pResponse); +#endif //UDF_UDF_H diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 98170208178d58244a8d662690689b431d1be916..610e5a0bb231a3f1693f4d18869fa53972ecfcdd 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -528,11 +528,10 @@ void firstFunction(SqlFunctionCtx *pCtx) { char* buf = GET_ROWCELL_INTERBUF(pResInfo); SInputColumnInfoData* pInput = &pCtx->input; - SColumnInfoData* pInputCol = pInput->pData[0]; // All null data column, return directly. - if (pInput->pColumnDataAgg[0]->numOfNull == pInput->totalRows) { + if (pInput->colDataAggIsSet && (pInput->pColumnDataAgg[0]->numOfNull == pInput->totalRows)) { ASSERT(pInputCol->hasNull == true); return; } diff --git a/source/libs/function/src/tudf.c b/source/libs/function/src/tudf.c index b65e637a57496486919f0114dd633077f337d8d1..6fcdb34529d933a95fc9260979f7593b675003d2 100644 --- a/source/libs/function/src/tudf.c +++ b/source/libs/function/src/tudf.c @@ -1,195 +1,925 @@ +/* + * 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 "uv.h" +#include "os.h" +#include "tlog.h" #include "tudf.h" +#include "tudfInt.h" -static char* getUdfFuncName(char* funcname, char* name, int type) { - switch (type) { - case TSDB_UDF_FUNC_NORMAL: - strcpy(funcname, name); +//TODO: when startup, set thread poll size. add it to cfg +//TODO: udfd restart when exist or aborts +//TODO: network error processing. +//TODO: add unit test +//TODO: add lua support +void onUdfcRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf); + +enum { + UV_TASK_CONNECT = 0, + UV_TASK_REQ_RSP = 1, + UV_TASK_DISCONNECT = 2 +}; + +typedef struct SUdfUvSession { + int64_t severHandle; + uv_pipe_t *udfSvcPipe; +} SUdfUvSession; + +typedef struct SClientUvTaskNode { + int8_t type; + int errCode; + + uv_pipe_t *pipe; + + int64_t seqNum; + uv_buf_t reqBuf; + + uv_sem_t taskSem; + uv_buf_t rspBuf; + + struct SClientUvTaskNode *prev; + struct SClientUvTaskNode *next; +} SClientUvTaskNode; + +typedef struct SClientUdfTask { + int8_t type; + + SUdfUvSession *session; + + int32_t errCode; + + union { + struct { + SUdfSetupRequest req; + SUdfSetupResponse rsp; + } _setup; + struct { + SUdfCallRequest req; + SUdfCallResponse rsp; + } _call; + struct { + SUdfTeardownRequest req; + SUdfTeardownResponse rsp; + } _teardown; + }; + + +} SClientUdfTask; + +typedef struct SClientConnBuf { + char *buf; + int32_t len; + int32_t cap; + int32_t total; +} SClientConnBuf; + +typedef struct SClientUvConn { + uv_pipe_t *pipe; + SClientUvTaskNode taskQueue; + SClientConnBuf readBuf; +} SClientUvConn; + +uv_process_t gUdfdProcess; + +uv_barrier_t gUdfInitBarrier; + +uv_loop_t gUdfdLoop; +uv_thread_t gUdfLoopThread; +uv_async_t gUdfLoopTaskAync; + +uv_async_t gUdfLoopStopAsync; + +uv_mutex_t gUdfTaskQueueMutex; +int64_t gUdfTaskSeqNum = 0; + +//double circular linked list +typedef SClientUvTaskNode *SClientUvTaskQueue; +SClientUvTaskNode gUdfQueueNode; +SClientUvTaskQueue gUdfTaskQueue = &gUdfQueueNode; + +//add SClientUvTaskNode task that close conn + + + +void udfTaskQueueInit(SClientUvTaskQueue q) { + q->next = q; + q->prev = q; +} + +bool udfTaskQueueIsEmpty(SClientUvTaskQueue q) { + return q == q->next; +} + +void udfTaskQueueInsertTail(SClientUvTaskQueue q, SClientUvTaskNode *e) { + e->next = q; + e->prev = q->prev; + e->prev->next = e; + q->prev = e; +} + +void udfTaskQueueInsertTaskAtHead(SClientUvTaskQueue q, SClientUvTaskNode *e) { + e->next = q->next; + e->prev = q; + q->next->prev = e; + q->next = e; +} + +void udfTaskQueueRemoveTask(SClientUvTaskNode *e) { + e->prev->next = e->next; + e->next->prev = e->prev; +} + +void udfTaskQueueSplit(SClientUvTaskQueue q, SClientUvTaskNode *from, SClientUvTaskQueue n) { + n->prev = q->prev; + n->prev->next = n; + n->next = from; + q->prev = from->prev; + q->prev->next = q; + from->prev = n; +} + +SClientUvTaskNode *udfTaskQueueHeadTask(SClientUvTaskQueue q) { + return q->next; +} + +SClientUvTaskNode *udfTaskQueueTailTask(SClientUvTaskQueue q) { + return q->prev; +} + +SClientUvTaskNode *udfTaskQueueNext(SClientUvTaskNode *e) { + return e->next; +} + +void udfTaskQueueMove(SClientUvTaskQueue q, SClientUvTaskQueue n) { + if (udfTaskQueueIsEmpty(q)) { + udfTaskQueueInit(n); + } else { + SClientUvTaskNode *h = udfTaskQueueHeadTask(q); + udfTaskQueueSplit(q, h, n); + } +} + + +int32_t encodeRequest(char **pBuf, int32_t *pBufLen, SUdfRequest *request) { + debugPrint("%s", "encoding request"); + + int len = sizeof(SUdfRequest) - sizeof(void *); + switch (request->type) { + case UDF_TASK_SETUP: { + SUdfSetupRequest *setup = (SUdfSetupRequest *) (request->subReq); + len += sizeof(SUdfSetupRequest) - 1 * sizeof(char *) + setup->pathSize; break; - case TSDB_UDF_FUNC_INIT: - sprintf(funcname, "%s_init", name); + } + case UDF_TASK_CALL: { + SUdfCallRequest *call = (SUdfCallRequest *) (request->subReq); + len += sizeof(SUdfCallRequest) - 2 * sizeof(char *) + call->inputBytes + call->stateBytes; break; - case TSDB_UDF_FUNC_FINALIZE: - sprintf(funcname, "%s_finalize", name); + } + case UDF_TASK_TEARDOWN: { + SUdfTeardownRequest *teardown = (SUdfTeardownRequest *) (request->subReq); + len += sizeof(SUdfTeardownRequest); break; - case TSDB_UDF_FUNC_MERGE: - sprintf(funcname, "%s_merge", name); + } + default: break; - case TSDB_UDF_FUNC_DESTROY: - sprintf(funcname, "%s_destroy", name); + } + + char *bufBegin = malloc(len); + char *buf = bufBegin; + + //skip msgLen first + buf += sizeof(int32_t); + + *(int64_t *) buf = request->seqNum; + buf += sizeof(int64_t); + *(int8_t *) buf = request->type; + buf += sizeof(int8_t); + + switch (request->type) { + case UDF_TASK_SETUP: { + SUdfSetupRequest *setup = (SUdfSetupRequest *) (request->subReq); + memcpy(buf, setup->udfName, 16); + buf += 16; + *(int8_t *) buf = setup->scriptType; + buf += sizeof(int8_t); + *(int8_t *) buf = setup->udfType; + buf += sizeof(int8_t); + *(int16_t *) buf = setup->pathSize; + buf += sizeof(int16_t); + memcpy(buf, setup->path, setup->pathSize); + buf += setup->pathSize; + break; + } + + case UDF_TASK_CALL: { + SUdfCallRequest *call = (SUdfCallRequest *) (request->subReq); + *(int64_t *) buf = call->udfHandle; + buf += sizeof(int64_t); + *(int8_t *) buf = call->step; + buf += sizeof(int8_t); + *(int32_t *) buf = call->inputBytes; + buf += sizeof(int32_t); + memcpy(buf, call->input, call->inputBytes); + buf += call->inputBytes; + *(int32_t *) buf = call->stateBytes; + buf += sizeof(int32_t); + memcpy(buf, call->state, call->stateBytes); + buf += call->stateBytes; break; + } + + case UDF_TASK_TEARDOWN: { + SUdfTeardownRequest *teardown = (SUdfTeardownRequest *) (request->subReq); + *(int64_t *) buf = teardown->udfHandle; + buf += sizeof(int64_t); + break; + } default: - assert(0); break; } - return funcname; + request->msgLen = buf - bufBegin; + *(int32_t *) bufBegin = request->msgLen; + *pBuf = bufBegin; + *pBufLen = request->msgLen; + return 0; } -#if 0 -int32_t initUdfInfo(SUdfInfo* pUdfInfo) { - if (pUdfInfo == NULL) { - return TSDB_CODE_SUCCESS; +int32_t decodeRequest(char *bufMsg, int32_t bufLen, SUdfRequest **pRequest) { + debugPrint("%s", "decoding request"); + if (*(int32_t *) bufMsg != bufLen) { + debugPrint("%s", "decoding request error"); + return -1; } - ////qError("script len: %d", pUdfInfo->contLen); - if (isValidScript(pUdfInfo->content, pUdfInfo->contLen)) { - pUdfInfo->isScript = 1; - pUdfInfo->pScriptCtx = createScriptCtx(pUdfInfo->content, pUdfInfo->resType, pUdfInfo->resBytes); - if (pUdfInfo->pScriptCtx == NULL) { - return TSDB_CODE_QRY_SYS_ERROR; + char *buf = bufMsg; + SUdfRequest *request = malloc(sizeof(SUdfRequest)); + request->subReq = NULL; + request->msgLen = *(int32_t *) (buf); + buf += sizeof(int32_t); + request->seqNum = *(int64_t *) (buf); + buf += sizeof(int64_t); + request->type = *(int8_t *) (buf); + buf += sizeof(int8_t); + + switch (request->type) { + case UDF_TASK_SETUP: { + SUdfSetupRequest *setup = malloc(sizeof(SUdfSetupRequest)); + + memcpy(setup->udfName, buf, 16); + buf += 16; + setup->scriptType = *(int8_t *) buf; + buf += sizeof(int8_t); + setup->udfType = *(int8_t *) buf; + buf += sizeof(int8_t); + setup->pathSize = *(int16_t *) buf; + buf += sizeof(int16_t); + setup->path = buf; + buf += setup->pathSize; + + request->subReq = setup; + break; } - tfree(pUdfInfo->content); - - pUdfInfo->funcs[TSDB_UDF_FUNC_INIT] = taosLoadScriptInit; - if (pUdfInfo->funcs[TSDB_UDF_FUNC_INIT] == NULL - || (*(scriptInitFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_INIT])(pUdfInfo->pScriptCtx) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_QRY_SYS_ERROR; + case UDF_TASK_CALL: { + SUdfCallRequest *call = malloc(sizeof(SUdfCallRequest)); + + call->udfHandle = *(int64_t *) buf; + buf += sizeof(int64_t); + call->step = *(int8_t *) buf; + buf += sizeof(int8_t); + call->inputBytes = *(int32_t *) buf; + buf += sizeof(int32_t); + call->input = buf; + buf += call->inputBytes; + call->stateBytes = *(int32_t *) buf; + buf += sizeof(int32_t); + call->state = buf; + buf += call->stateBytes; + + request->subReq = call; + break; } - pUdfInfo->funcs[TSDB_UDF_FUNC_NORMAL] = taosLoadScriptNormal; + case UDF_TASK_TEARDOWN: { + SUdfTeardownRequest *teardown = malloc(sizeof(SUdfTeardownRequest)); + + teardown->udfHandle = *(int64_t *) buf; + buf += sizeof(int64_t); - if (pUdfInfo->funcType == FUNCTION_TYPE_AGG) { - pUdfInfo->funcs[TSDB_UDF_FUNC_FINALIZE] = taosLoadScriptFinalize; - pUdfInfo->funcs[TSDB_UDF_FUNC_MERGE] = taosLoadScriptMerge; + request->subReq = teardown; } - pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY] = taosLoadScriptDestroy; - } else { - char path[PATH_MAX] = {0}; - taosGetTmpfilePath("script", path, tsTempDir); + } + if (buf - bufMsg != bufLen) { + debugPrint("%s", "decode request error"); + free(request->subReq); + free(request); + return -1; + } + *pRequest = request; + return 0; +} + +int32_t encodeResponse(char **pBuf, int32_t *pBufLen, SUdfResponse *response) { + debugPrint("%s", "encoding response"); + + int32_t len = sizeof(SUdfResponse) - sizeof(void *); + + switch (response->type) { + case UDF_TASK_SETUP: { + len += sizeof(SUdfSetupResponse); + break; + } + case UDF_TASK_CALL: { + SUdfCallResponse *callResp = (SUdfCallResponse *) (response->subRsp); + len += sizeof(SUdfCallResponse) - 2 * sizeof(char *) + + callResp->outputBytes + callResp->newStateBytes; + break; + } + case UDF_TASK_TEARDOWN: { + len += sizeof(SUdfTeardownResponse); + break; + } + } - FILE* file = fopen(path, "w+"); + char *bufBegin = malloc(len); + char *buf = bufBegin; - // TODO check for failure of flush to disk - /*size_t t = */ fwrite(pUdfInfo->content, pUdfInfo->contLen, 1, file); - fclose(file); - tfree(pUdfInfo->content); + //skip msgLen + buf += sizeof(int32_t); - pUdfInfo->path = strdup(path); + *(int64_t *) buf = response->seqNum; + buf += sizeof(int64_t); + *(int8_t *) buf = response->type; + buf += sizeof(int8_t); + *(int32_t *) buf = response->code; + buf += sizeof(int32_t); - pUdfInfo->handle = taosLoadDll(path); - if (NULL == pUdfInfo->handle) { - return TSDB_CODE_QRY_SYS_ERROR; + switch (response->type) { + case UDF_TASK_SETUP: { + SUdfSetupResponse *setupResp = (SUdfSetupResponse *) (response->subRsp); + *(int64_t *) buf = setupResp->udfHandle; + buf += sizeof(int64_t); + break; + } + case UDF_TASK_CALL: { + SUdfCallResponse *callResp = (SUdfCallResponse *) (response->subRsp); + *(int32_t *) buf = callResp->outputBytes; + buf += sizeof(int32_t); + memcpy(buf, callResp->output, callResp->outputBytes); + buf += callResp->outputBytes; + + *(int32_t *) buf = callResp->newStateBytes; + buf += sizeof(int32_t); + memcpy(buf, callResp->newState, callResp->newStateBytes); + buf += callResp->newStateBytes; + break; } + case UDF_TASK_TEARDOWN: { + SUdfTeardownResponse *teardownResp = (SUdfTeardownResponse *) (response->subRsp); + break; + } + default: + break; + } + response->msgLen = buf - bufBegin; + *(int32_t *) bufBegin = response->msgLen; + *pBuf = bufBegin; + *pBufLen = response->msgLen; + return 0; +} - char funcname[FUNCTIONS_NAME_MAX_LENGTH + 10] = {0}; - pUdfInfo->funcs[TSDB_UDF_FUNC_NORMAL] = taosLoadSym(pUdfInfo->handle, getUdfFuncName(funcname, pUdfInfo->name, TSDB_UDF_FUNC_NORMAL)); - if (NULL == pUdfInfo->funcs[TSDB_UDF_FUNC_NORMAL]) { - return TSDB_CODE_QRY_SYS_ERROR; +int32_t decodeResponse(char *bufMsg, int32_t bufLen, SUdfResponse **pResponse) { + debugPrint("%s", "decoding response"); + + if (*(int32_t *) bufMsg != bufLen) { + debugPrint("%s", "can not decode response"); + return -1; + } + char *buf = bufMsg; + SUdfResponse *rsp = malloc(sizeof(SUdfResponse)); + rsp->msgLen = *(int32_t *) buf; + buf += sizeof(int32_t); + rsp->seqNum = *(int64_t *) buf; + buf += sizeof(int64_t); + rsp->type = *(int8_t *) buf; + buf += sizeof(int8_t); + rsp->code = *(int32_t *) buf; + buf += sizeof(int32_t); + + switch (rsp->type) { + case UDF_TASK_SETUP: { + SUdfSetupResponse *setupRsp = (SUdfSetupResponse *) malloc(sizeof(SUdfSetupResponse)); + setupRsp->udfHandle = *(int64_t *) buf; + buf += sizeof(int64_t); + rsp->subRsp = (char *) setupRsp; + break; } + case UDF_TASK_CALL: { + SUdfCallResponse *callRsp = (SUdfCallResponse *) malloc(sizeof(SUdfCallResponse)); + callRsp->outputBytes = *(int32_t *) buf; + buf += sizeof(int32_t); - pUdfInfo->funcs[TSDB_UDF_FUNC_INIT] = taosLoadSym(pUdfInfo->handle, getUdfFuncName(funcname, pUdfInfo->name, TSDB_UDF_FUNC_INIT)); + callRsp->output = buf; + buf += callRsp->outputBytes; - if (pUdfInfo->funcType == FUNCTION_TYPE_AGG) { - pUdfInfo->funcs[TSDB_UDF_FUNC_FINALIZE] = taosLoadSym(pUdfInfo->handle, getUdfFuncName(funcname, pUdfInfo->name, TSDB_UDF_FUNC_FINALIZE)); - pUdfInfo->funcs[TSDB_UDF_FUNC_MERGE] = taosLoadSym(pUdfInfo->handle, getUdfFuncName(funcname, pUdfInfo->name, TSDB_UDF_FUNC_MERGE)); + callRsp->newStateBytes = *(int32_t *) buf; + buf += sizeof(int32_t); + + callRsp->newState = buf; + buf += callRsp->newStateBytes; + + rsp->subRsp = callRsp; + break; } + case UDF_TASK_TEARDOWN: { + SUdfTeardownResponse *teardownRsp = (SUdfTeardownResponse *) malloc(sizeof(SUdfTeardownResponse)); + rsp->subRsp = teardownRsp; + break; + } + default: + break; + } + if (buf - bufMsg != bufLen) { + debugPrint("%s", "can not decode response"); + free(rsp->subRsp); + free(rsp); + return -1; + } + *pResponse = rsp; + return 0; +} + +void onUdfdExit(uv_process_t *req, int64_t exit_status, int term_signal) { + debugPrint("Process exited with status %" PRId64 ", signal %d", exit_status, term_signal); + uv_close((uv_handle_t *) req, NULL); + //TODO: restart the udfd process +} - pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY] = taosLoadSym(pUdfInfo->handle, getUdfFuncName(funcname, pUdfInfo->name, TSDB_UDF_FUNC_DESTROY)); +void onUdfcPipeClose(uv_handle_t *handle) { + SClientUvConn *conn = handle->data; + if (!udfTaskQueueIsEmpty(&conn->taskQueue)) { + SClientUvTaskNode *task = udfTaskQueueHeadTask(&conn->taskQueue); + task->errCode = 0; + uv_sem_post(&task->taskSem); + } + + free(conn->readBuf.buf); + free(conn); + free((uv_pipe_t *) handle); + +} + +int32_t udfcGetUvTaskResponseResult(SClientUdfTask *task, SClientUvTaskNode *uvTask) { + debugPrint("%s", "get uv task result"); + if (uvTask->type == UV_TASK_REQ_RSP) { + if (uvTask->rspBuf.base != NULL) { + SUdfResponse *rsp; + decodeResponse(uvTask->rspBuf.base, uvTask->rspBuf.len, &rsp); + task->errCode = rsp->code; + + switch (task->type) { + case UDF_TASK_SETUP: { + //TODO: copy or not + task->_setup.rsp = *(SUdfSetupResponse *) (rsp->subRsp); + break; + } + case UDF_TASK_CALL: { + task->_call.rsp = *(SUdfCallResponse *) (rsp->subRsp); + //TODO: copy or not + break; + } + case UDF_TASK_TEARDOWN: { + task->_teardown.rsp = *(SUdfTeardownResponse *) (rsp->subRsp); + //TODO: copy or not? + break; + } + default: { + break; + } + } + + // TODO: the call buffer is setup and freed by udf invocation + free(uvTask->rspBuf.base); + free(rsp->subRsp); + free(rsp); + } else { + task->errCode = uvTask->errCode; + } + } else if (uvTask->type == UV_TASK_CONNECT) { + task->errCode = uvTask->errCode; + } else if (uvTask->type == UV_TASK_DISCONNECT) { + task->errCode = uvTask->errCode; + } + return 0; +} - if (pUdfInfo->funcs[TSDB_UDF_FUNC_INIT]) { - return (*(udfInitFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_INIT])(&pUdfInfo->init); +void udfcAllocateBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf) { + debugPrint("%s", "client allocate buffer to receive from pipe"); + SClientUvConn *conn = handle->data; + SClientConnBuf *connBuf = &conn->readBuf; + + int32_t msgHeadSize = sizeof(int32_t) + sizeof(int64_t); + if (connBuf->cap == 0) { + connBuf->buf = malloc(msgHeadSize); + if (connBuf->buf) { + connBuf->len = 0; + connBuf->cap = msgHeadSize; + connBuf->total = -1; + + buf->base = connBuf->buf; + buf->len = connBuf->cap; + } else { + //TODO: log error + buf->base = NULL; + buf->len = 0; + } + } else { + connBuf->cap = connBuf->total > connBuf->cap ? connBuf->total : connBuf->cap; + void *resultBuf = realloc(connBuf->buf, connBuf->cap); + if (resultBuf) { + connBuf->buf = resultBuf; + buf->base = connBuf->buf + connBuf->len; + buf->len = connBuf->cap - connBuf->len; + } else { + //TODO: log error free connBuf->buf + buf->base = NULL; + buf->len = 0; } } - return TSDB_CODE_SUCCESS; + debugPrint("\tconn buf cap - len - total : %d - %d - %d", connBuf->cap, connBuf->len, connBuf->total); + } -void destroyUdfInfo(SUdfInfo* pUdfInfo) { - if (pUdfInfo == NULL) { - return; +bool isUdfcUvMsgComplete(SClientConnBuf *connBuf) { + if (connBuf->total == -1 && connBuf->len >= sizeof(int32_t)) { + connBuf->total = *(int32_t *) (connBuf->buf); } + if (connBuf->len == connBuf->cap && connBuf->total == connBuf->cap) { + return true; + } + return false; +} + +void udfcUvHandleRsp(SClientUvConn *conn) { + SClientConnBuf *connBuf = &conn->readBuf; + int64_t seqNum = *(int64_t *) (connBuf->buf + sizeof(int32_t)); // msglen int32_t then seqnum - if (pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY]) { - if (pUdfInfo->isScript) { - (*(scriptDestroyFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY])(pUdfInfo->pScriptCtx); - tfree(pUdfInfo->content); - }else{ - (*(udfDestroyFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_DESTROY])(&pUdfInfo->init); + if (udfTaskQueueIsEmpty(&conn->taskQueue)) { + //LOG error + return; + } + bool found = false; + SClientUvTaskNode *taskFound = NULL; + SClientUvTaskNode *task = udfTaskQueueNext(&conn->taskQueue); + while (task != &conn->taskQueue) { + if (task->seqNum == seqNum) { + if (found == false) { + found = true; + taskFound = task; + } else { + //LOG error; + continue; + } } + task = udfTaskQueueNext(task); } - tfree(pUdfInfo->name); + if (taskFound) { + taskFound->rspBuf = uv_buf_init(connBuf->buf, connBuf->len); + udfTaskQueueRemoveTask(taskFound); + uv_sem_post(&taskFound->taskSem); + } else { + //LOG error + } + connBuf->buf = NULL; + connBuf->total = -1; + connBuf->len = 0; + connBuf->cap = 0; +} + +void udfcUvHandleError(SClientUvConn *conn) { + uv_close((uv_handle_t *) conn->pipe, onUdfcPipeClose); +} + +void onUdfcRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { + debugPrint("%s, nread: %zd", "client read from pipe", nread); + if (nread == 0) return; + + SClientUvConn *conn = client->data; + SClientConnBuf *connBuf = &conn->readBuf; + if (nread > 0) { + connBuf->len += nread; + if (isUdfcUvMsgComplete(connBuf)) { + udfcUvHandleRsp(conn); + } - if (pUdfInfo->path) { - unlink(pUdfInfo->path); + } + if (nread < 0) { + debugPrint("\tclient read error: %s", uv_strerror(nread)); + if (nread == UV_EOF) { + //TODO: + } + udfcUvHandleError(conn); } - tfree(pUdfInfo->path); - tfree(pUdfInfo->content); - taosCloseDll(pUdfInfo->handle); - tfree(pUdfInfo); } -void doInvokeUdf(struct SUdfInfo* pUdfInfo, SqlFunctionCtx *pCtx, int32_t idx, int32_t type) { - int32_t output = 0; +void onUdfClientWrite(uv_write_t *write, int status) { + debugPrint("%s", "after writing to pipe"); + SClientUvTaskNode *uvTask = write->data; + if (status == 0) { + uv_pipe_t *pipe = uvTask->pipe; + SClientUvConn *conn = pipe->data; + udfTaskQueueInsertTail(&conn->taskQueue, uvTask); + } else { + //TODO Log error; + } + debugPrint("\tlength:%zu", uvTask->reqBuf.len); + free(write); + free(uvTask->reqBuf.base); +} - if (pUdfInfo == NULL || pUdfInfo->funcs[type] == NULL) { - //qError("empty udf function, type:%d", type); - return; +void onUdfClientConnect(uv_connect_t *connect, int status) { + SClientUvTaskNode *uvTask = connect->data; + uvTask->errCode = status; + if (status != 0) { + //TODO: LOG error } + uv_read_start((uv_stream_t *) uvTask->pipe, udfcAllocateBuffer, onUdfcRead); + free(connect); + uv_sem_post(&uvTask->taskSem); +} -// //qDebug("invoke udf function:%s,%p", pUdfInfo->name, pUdfInfo->funcs[type]); +int32_t createUdfcUvTask(SClientUdfTask *task, int8_t uvTaskType, SClientUvTaskNode **pUvTask) { + SClientUvTaskNode *uvTask = calloc(1, sizeof(SClientUvTaskNode)); + uvTask->type = uvTaskType; + + if (uvTaskType == UV_TASK_CONNECT) { + } else if (uvTaskType == UV_TASK_REQ_RSP) { + uvTask->pipe = task->session->udfSvcPipe; + SUdfRequest request; + request.type = task->type; + request.seqNum = gUdfTaskSeqNum++; + + if (task->type == UDF_TASK_SETUP) { + request.subReq = &task->_setup.req; + request.type = UDF_TASK_SETUP; + } else if (task->type == UDF_TASK_CALL) { + request.subReq = &task->_call.req; + request.type = UDF_TASK_CALL; + } else if (task->type == UDF_TASK_TEARDOWN) { + request.subReq = &task->_teardown.req; + request.type = UDF_TASK_TEARDOWN; + } else { + //TODO log and return error + } + char *buf = NULL; + int32_t bufLen = 0; + encodeRequest(&buf, &bufLen, &request); + uvTask->reqBuf = uv_buf_init(buf, bufLen); + uvTask->seqNum = request.seqNum; + } else if (uvTaskType == UV_TASK_DISCONNECT) { + uvTask->pipe = task->session->udfSvcPipe; + } + uv_sem_init(&uvTask->taskSem, 0); - switch (type) { - case TSDB_UDF_FUNC_NORMAL: - if (pUdfInfo->isScript) { - (*(scriptNormalFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_NORMAL])(pUdfInfo->pScriptCtx, - (char *)pCtx->pInput + idx * pCtx->inputType, pCtx->inputType, pCtx->inputBytes, pCtx->size, pCtx->ptsList, pCtx->startTs, pCtx->pOutput, - (char *)pCtx->ptsOutputBuf, &output, pCtx->resDataInfo.type, pCtx->resDataInfo.bytes); - } else { - SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx); + *pUvTask = uvTask; + return 0; +} - void *interBuf = (void *)GET_ROWCELL_INTERBUF(pResInfo); +int32_t queueUvUdfTask(SClientUvTaskNode *uvTask) { + debugPrint("%s, %d", "queue uv task", uvTask->type); - (*(udfNormalFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_NORMAL])((char *)pCtx->pInput + idx * pCtx->inputType, pCtx->inputType, pCtx->inputBytes, pCtx->size, pCtx->ptsList, - pCtx->pOutput, interBuf, (char *)pCtx->ptsOutputBuf, &output, pCtx->resDataInfo.type, pCtx->resDataInfo.bytes, &pUdfInfo->init); - } + uv_mutex_lock(&gUdfTaskQueueMutex); + udfTaskQueueInsertTail(gUdfTaskQueue, uvTask); + uv_mutex_unlock(&gUdfTaskQueueMutex); + uv_async_send(&gUdfLoopTaskAync); - if (pUdfInfo->funcType == TSDB_FUNC_TYPE_AGGREGATE) { - pCtx->resultInfo->numOfRes = output; - } else { - pCtx->resultInfo->numOfRes += output; - } + uv_sem_wait(&uvTask->taskSem); + uv_sem_destroy(&uvTask->taskSem); - if (pCtx->resultInfo->numOfRes > 0) { - pCtx->resultInfo->hasResult = DATA_SET_FLAG; - } + return 0; +} - break; +int32_t startUvUdfTask(SClientUvTaskNode *uvTask) { + debugPrint("%s, type %d", "start uv task ", uvTask->type); + switch (uvTask->type) { + case UV_TASK_CONNECT: { + uv_pipe_t *pipe = malloc(sizeof(uv_pipe_t)); + uv_pipe_init(&gUdfdLoop, pipe, 0); + uvTask->pipe = pipe; - case TSDB_UDF_FUNC_MERGE: - if (pUdfInfo->isScript) { - (*(scriptMergeFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_MERGE])(pUdfInfo->pScriptCtx, pCtx->pInput, pCtx->size, pCtx->pOutput, &output); - } else { - (*(udfMergeFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_MERGE])(pCtx->pInput, pCtx->size, pCtx->pOutput, &output, &pUdfInfo->init); - } + SClientUvConn *conn = malloc(sizeof(SClientUvConn)); + conn->pipe = pipe; + conn->readBuf.len = 0; + conn->readBuf.cap = 0; + conn->readBuf.buf = 0; + conn->readBuf.total = -1; + udfTaskQueueInit(&conn->taskQueue); - // set the output value exist - pCtx->resultInfo->numOfRes = output; - if (output > 0) { - pCtx->resultInfo->hasResult = DATA_SET_FLAG; - } + pipe->data = conn; + + uv_connect_t *connReq = malloc(sizeof(uv_connect_t)); + connReq->data = uvTask; + uv_pipe_connect(connReq, pipe, "udf.sock", onUdfClientConnect); + break; + } + case UV_TASK_REQ_RSP: { + uv_pipe_t *pipe = uvTask->pipe; + uv_write_t *write = malloc(sizeof(uv_write_t)); + write->data = uvTask; + uv_write(write, (uv_stream_t *) pipe, &uvTask->reqBuf, 1, onUdfClientWrite); break; + } + case UV_TASK_DISCONNECT: { + SClientUvConn *conn = uvTask->pipe->data; + udfTaskQueueInsertTail(&conn->taskQueue, uvTask); + uv_close((uv_handle_t *) uvTask->pipe, onUdfcPipeClose); + break; + } + default: { + break; + } + } - case TSDB_UDF_FUNC_FINALIZE: { - SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx); - void *interBuf = (void *)GET_ROWCELL_INTERBUF(pResInfo); - if (pUdfInfo->isScript) { - (*(scriptFinalizeFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_FINALIZE])(pUdfInfo->pScriptCtx, pCtx->startTs, pCtx->pOutput, &output); - } else { - (*(udfFinalizeFunc)pUdfInfo->funcs[TSDB_UDF_FUNC_FINALIZE])(pCtx->pOutput, interBuf, &output, &pUdfInfo->init); - } - // set the output value exist - pCtx->resultInfo->numOfRes = output; - if (output > 0) { - pCtx->resultInfo->hasResult = DATA_SET_FLAG; - } + return 0; +} - break; - } +void udfClientAsyncCb(uv_async_t *async) { + SClientUvTaskNode node; + SClientUvTaskQueue q = &node; + udfTaskQueueInit(q); + + uv_mutex_lock(&gUdfTaskQueueMutex); + udfTaskQueueMove(gUdfTaskQueue, q); + uv_mutex_unlock(&gUdfTaskQueueMutex); + + while (!udfTaskQueueIsEmpty(q)) { + SClientUvTaskNode *task = udfTaskQueueHeadTask(q); + udfTaskQueueRemoveTask(task); + startUvUdfTask(task); + } + +} + +void udfStopAsyncCb(uv_async_t *async) { + uv_stop(&gUdfdLoop); + uv_loop_close(&gUdfdLoop); +} + +void startUdfd(void *argsThread) { + uv_loop_init(&gUdfdLoop); + + //TODO: path + uv_process_options_t options; + static char path[256] = {0}; + size_t cwdSize; + uv_cwd(path, &cwdSize); + strcat(path, "./udfd"); + char* args[2] = {path, NULL}; + options.args = args; + options.file = path; + options.exit_cb = onUdfdExit; + + int err = uv_spawn(&gUdfdLoop, &gUdfdProcess, &options); + if (err != 0) { + debugPrint("can not spawn udfd. path: %s, error: %s", path, uv_strerror(err)); + } + + uv_async_init(&gUdfdLoop, &gUdfLoopTaskAync, udfClientAsyncCb); + uv_async_init(&gUdfdLoop, &gUdfLoopStopAsync, udfStopAsyncCb); + uv_mutex_init(&gUdfTaskQueueMutex); + udfTaskQueueInit(gUdfTaskQueue); + uv_barrier_wait(&gUdfInitBarrier); + uv_run(&gUdfdLoop, UV_RUN_DEFAULT); +} + +int32_t startUdfService() { + uv_barrier_init(&gUdfInitBarrier, 2); + uv_thread_create(&gUdfLoopThread, startUdfd, 0); + uv_barrier_wait(&gUdfInitBarrier); + return 0; +} + +int32_t stopUdfService() { + uv_barrier_destroy(&gUdfInitBarrier); + uv_process_kill(&gUdfdProcess, SIGINT); + uv_async_send(&gUdfLoopStopAsync); + uv_mutex_destroy(&gUdfTaskQueueMutex); + uv_thread_join(&gUdfLoopThread); + return 0; +} + +int32_t udfcRunUvTask(SClientUdfTask *task, int8_t uvTaskType) { + SClientUvTaskNode *uvTask = NULL; + + createUdfcUvTask(task, uvTaskType, &uvTask); + queueUvUdfTask(uvTask); + udfcGetUvTaskResponseResult(task, uvTask); + if (uvTaskType == UV_TASK_CONNECT) { + task->session->udfSvcPipe = uvTask->pipe; + } + free(uvTask); + uvTask = NULL; + return task->errCode; +} + +int32_t setupUdf(SUdfInfo *udfInfo, UdfHandle *handle) { + debugPrint("%s", "client setup udf"); + SClientUdfTask *task = malloc(sizeof(SClientUdfTask)); + task->errCode = 0; + task->session = malloc(sizeof(SUdfUvSession)); + task->type = UDF_TASK_SETUP; + + SUdfSetupRequest *req = &task->_setup.req; + memcpy(req->udfName, udfInfo->udfName, 16); + req->path = udfInfo->path; + req->pathSize = strlen(req->path) + 1; + req->udfType = udfInfo->udfType; + req->scriptType = udfInfo->scriptType; + + int32_t errCode = udfcRunUvTask(task, UV_TASK_CONNECT); + if (errCode != 0) { + //TODO: log error + return -1; } + + udfcRunUvTask(task, UV_TASK_REQ_RSP); + + SUdfSetupResponse *rsp = &task->_setup.rsp; + task->session->severHandle = rsp->udfHandle; + *handle = task->session; + int32_t err = task->errCode; + free(task); + return err; } -#endif \ No newline at end of file +int32_t callUdf(UdfHandle handle, int8_t step, char *state, int32_t stateSize, SUdfDataBlock input, char **newState, + int32_t *newStateSize, SUdfDataBlock *output) { + debugPrint("%s", "client call udf"); + + SClientUdfTask *task = malloc(sizeof(SClientUdfTask)); + task->errCode = 0; + task->session = (SUdfUvSession *) handle; + task->type = UDF_TASK_CALL; + + SUdfCallRequest *req = &task->_call.req; + + req->state = state; + req->stateBytes = stateSize; + req->inputBytes = input.size; + req->input = input.data; + req->udfHandle = task->session->severHandle; + req->step = step; + + udfcRunUvTask(task, UV_TASK_REQ_RSP); + + SUdfCallResponse *rsp = &task->_call.rsp; + *newState = rsp->newState; + *newStateSize = rsp->newStateBytes; + output->size = rsp->outputBytes; + output->data = rsp->output; + int32_t err = task->errCode; + free(task); + return err; +} + +int32_t teardownUdf(UdfHandle handle) { + debugPrint("%s", "client teardown udf"); + + SClientUdfTask *task = malloc(sizeof(SClientUdfTask)); + task->errCode = 0; + task->session = (SUdfUvSession *) handle; + task->type = UDF_TASK_TEARDOWN; + + SUdfTeardownRequest *req = &task->_teardown.req; + req->udfHandle = task->session->severHandle; + + udfcRunUvTask(task, UV_TASK_REQ_RSP); + + + SUdfTeardownResponse *rsp = &task->_teardown.rsp; + + int32_t err = task->errCode; + + udfcRunUvTask(task, UV_TASK_DISCONNECT); + + free(task->session); + free(task); + + return err; +} diff --git a/source/libs/function/src/udfd.c b/source/libs/function/src/udfd.c new file mode 100644 index 0000000000000000000000000000000000000000..27385325f57aa171942c133f5b0333363e55e10d --- /dev/null +++ b/source/libs/function/src/udfd.c @@ -0,0 +1,356 @@ +/* + * 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 "uv.h" +#include "os.h" +#include "tlog.h" + +#include "tudf.h" +#include "tudfInt.h" + + +static uv_loop_t *loop; + +typedef struct SUdfdUvConn { + uv_stream_t *client; + char *inputBuf; + int32_t inputLen; + int32_t inputCap; + int32_t inputTotal; +} SUdfdUvConn; + +typedef struct SUvUdfWork { + uv_stream_t *client; + uv_buf_t input; + uv_buf_t output; +} SUvUdfWork; + +typedef struct SUdf { + int32_t refCount; + + char name[16]; + int8_t type; + + uv_lib_t lib; + TUdfFunc normalFunc; +} SUdf; + +//TODO: low priority: change name onxxx to xxxCb, and udfc or udfd as prefix +//TODO: add private udf structure. +typedef struct SUdfHandle { + SUdf *udf; +} SUdfHandle; + + +void udfdProcessRequest(uv_work_t *req) { + SUvUdfWork *uvUdf = (SUvUdfWork *) (req->data); + SUdfRequest *request = NULL; + decodeRequest(uvUdf->input.base, uvUdf->input.len, &request); + + switch (request->type) { + case UDF_TASK_SETUP: { + debugPrint("%s", "process setup request"); + SUdf *udf = malloc(sizeof(SUdf)); + udf->refCount = 0; + SUdfSetupRequest *setup = request->subReq; + strcpy(udf->name, setup->udfName); + int err = uv_dlopen(setup->path, &udf->lib); + if (err != 0) { + debugPrint("can not load library %s. error: %s", setup->path, uv_strerror(err)); + //TODO set error + } + + char normalFuncName[32] = {0}; + strcpy(normalFuncName, setup->udfName); + //TODO error, + //TODO find all functions normal, init, destroy, normal, merge, finalize + uv_dlsym(&udf->lib, normalFuncName, (void **) (&udf->normalFunc)); + + SUdfHandle *handle = malloc(sizeof(SUdfHandle)); + handle->udf = udf; + udf->refCount++; + //TODO: allocate private structure and call init function and set it to handle + SUdfResponse *rsp = malloc(sizeof(SUdfResponse)); + rsp->seqNum = request->seqNum; + rsp->type = request->type; + rsp->code = 0; + SUdfSetupResponse *subRsp = malloc(sizeof(SUdfSetupResponse)); + subRsp->udfHandle = (int64_t) (handle); + rsp->subRsp = subRsp; + char *buf; + int32_t len; + encodeResponse(&buf, &len, rsp); + + uvUdf->output = uv_buf_init(buf, len); + + free(rsp->subRsp); + free(rsp); + free(request->subReq); + free(request); + free(uvUdf->input.base); + break; + } + + case UDF_TASK_CALL: { + debugPrint("%s", "process call request"); + SUdfCallRequest *call = request->subReq; + SUdfHandle *handle = (SUdfHandle *) (call->udfHandle); + SUdf *udf = handle->udf; + char *newState; + int32_t newStateSize; + SUdfDataBlock input = {.data = call->input, .size= call->inputBytes}; + SUdfDataBlock output; + //TODO: call different functions according to the step + udf->normalFunc(call->step, call->state, call->stateBytes, input, &newState, &newStateSize, &output); + + SUdfResponse *rsp = malloc(sizeof(SUdfResponse)); + rsp->seqNum = request->seqNum; + rsp->type = request->type; + rsp->code = 0; + SUdfCallResponse *subRsp = malloc(sizeof(SUdfCallResponse)); + subRsp->outputBytes = output.size; + subRsp->output = output.data; + subRsp->newStateBytes = newStateSize; + subRsp->newState = newState; + rsp->subRsp = subRsp; + + char *buf; + int32_t len; + encodeResponse(&buf, &len, rsp); + uvUdf->output = uv_buf_init(buf, len); + + free(rsp->subRsp); + free(rsp); + free(newState); + free(output.data); + free(request->subReq); + free(request); + free(uvUdf->input.base); + break; + } + case UDF_TASK_TEARDOWN: { + debugPrint("%s", "process teardown request"); + + SUdfTeardownRequest *teardown = request->subReq; + SUdfHandle *handle = (SUdfHandle *) (teardown->udfHandle); + SUdf *udf = handle->udf; + udf->refCount--; + if (udf->refCount == 0) { + uv_dlclose(&udf->lib); + } + free(udf); + //TODO: call destroy and free udf private + free(handle); + + SUdfResponse *rsp = malloc(sizeof(SUdfResponse)); + rsp->seqNum = request->seqNum; + rsp->type = request->type; + rsp->code = 0; + SUdfTeardownResponse *subRsp = malloc(sizeof(SUdfTeardownResponse)); + rsp->subRsp = subRsp; + char *buf; + int32_t len; + encodeResponse(&buf, &len, rsp); + uvUdf->output = uv_buf_init(buf, len); + + free(rsp->subRsp); + free(rsp); + free(request->subReq); + free(request); + free(uvUdf->input.base); + break; + } + default: { + break; + } + + } + +} + +void udfdOnWrite(uv_write_t *req, int status) { + debugPrint("%s", "after writing to pipe"); + if (status < 0) { + debugPrint("Write error %s", uv_err_name(status)); + } + SUvUdfWork *work = (SUvUdfWork *) req->data; + debugPrint("\tlength: %zu", work->output.len); + free(work->output.base); + free(work); + free(req); +} + + +void udfdSendResponse(uv_work_t *work, int status) { + debugPrint("%s", "send response"); + SUvUdfWork *udfWork = (SUvUdfWork *) (work->data); + + uv_write_t *write_req = malloc(sizeof(uv_write_t)); + write_req->data = udfWork; + uv_write(write_req, udfWork->client, &udfWork->output, 1, udfdOnWrite); + + free(work); +} + +void udfdAllocBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf) { + debugPrint("%s", "allocate buffer for read"); + SUdfdUvConn *ctx = handle->data; + int32_t msgHeadSize = sizeof(int32_t) + sizeof(int64_t); + if (ctx->inputCap == 0) { + ctx->inputBuf = malloc(msgHeadSize); + if (ctx->inputBuf) { + ctx->inputLen = 0; + ctx->inputCap = msgHeadSize; + ctx->inputTotal = -1; + + buf->base = ctx->inputBuf; + buf->len = ctx->inputCap; + } else { + //TODO: log error + buf->base = NULL; + buf->len = 0; + } + } else { + ctx->inputCap = ctx->inputTotal > ctx->inputCap ? ctx->inputTotal : ctx->inputCap; + void *inputBuf = realloc(ctx->inputBuf, ctx->inputCap); + if (inputBuf) { + ctx->inputBuf = inputBuf; + buf->base = ctx->inputBuf + ctx->inputLen; + buf->len = ctx->inputCap - ctx->inputLen; + } else { + //TODO: log error + buf->base = NULL; + buf->len = 0; + } + } + debugPrint("\tinput buf cap - len - total : %d - %d - %d", ctx->inputCap, ctx->inputLen, ctx->inputTotal); + +} + +bool isUdfdUvMsgComplete(SUdfdUvConn *pipe) { + if (pipe->inputTotal == -1 && pipe->inputLen >= sizeof(int32_t)) { + pipe->inputTotal = *(int32_t *) (pipe->inputBuf); + } + if (pipe->inputLen == pipe->inputCap && pipe->inputTotal == pipe->inputCap) { + return true; + } + return false; +} + +void udfdHandleRequest(SUdfdUvConn *conn) { + uv_work_t *work = malloc(sizeof(uv_work_t)); + SUvUdfWork *udfWork = malloc(sizeof(SUvUdfWork)); + udfWork->client = conn->client; + udfWork->input = uv_buf_init(conn->inputBuf, conn->inputLen); + conn->inputBuf = NULL; + conn->inputLen = 0; + conn->inputCap = 0; + conn->inputTotal = -1; + work->data = udfWork; + uv_queue_work(loop, work, udfdProcessRequest, udfdSendResponse); +} + +void udfdPipeCloseCb(uv_handle_t *pipe) { + SUdfdUvConn *conn = pipe->data; + free(conn->client); + free(conn->inputBuf); + free(conn); +} + +void udfdUvHandleError(SUdfdUvConn *conn) { + uv_close((uv_handle_t *) conn->client, udfdPipeCloseCb); +} + +void udfdPipeRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { + debugPrint("%s, nread: %zd", "read from pipe", nread); + + if (nread == 0) return; + + SUdfdUvConn *conn = client->data; + + if (nread > 0) { + conn->inputLen += nread; + if (isUdfdUvMsgComplete(conn)) { + udfdHandleRequest(conn); + } else { + //log error or continue; + } + return; + } + + if (nread < 0) { + debugPrint("Read error %s", uv_err_name(nread)); + if (nread == UV_EOF) { + //TODO check more when close + } else { + } + udfdUvHandleError(conn); + } +} + +void udfdOnNewConnection(uv_stream_t *server, int status) { + debugPrint("%s", "on new connection"); + if (status < 0) { + // TODO + return; + } + + uv_pipe_t *client = (uv_pipe_t *) malloc(sizeof(uv_pipe_t)); + uv_pipe_init(loop, client, 0); + if (uv_accept(server, (uv_stream_t *) client) == 0) { + SUdfdUvConn *ctx = malloc(sizeof(SUdfdUvConn)); + ctx->client = (uv_stream_t *) client; + ctx->inputBuf = 0; + ctx->inputLen = 0; + ctx->inputCap = 0; + client->data = ctx; + ctx->client = (uv_stream_t *) client; + uv_read_start((uv_stream_t *) client, udfdAllocBuffer, udfdPipeRead); + } else { + uv_close((uv_handle_t *) client, NULL); + } +} + +void removeListeningPipe(int sig) { + uv_fs_t req; + uv_fs_unlink(loop, &req, "udf.sock", NULL); + exit(0); +} + +int main() { + debugPrint("libuv version: %x", UV_VERSION_HEX); + + loop = uv_default_loop(); + uv_fs_t req; + uv_fs_unlink(loop, &req, "udf.sock", NULL); + + uv_pipe_t server; + uv_pipe_init(loop, &server, 0); + + signal(SIGINT, removeListeningPipe); + + int r; + if ((r = uv_pipe_bind(&server, "udf.sock"))) { + debugPrint("Bind error %s\n", uv_err_name(r)); + removeListeningPipe(0); + return 1; + } + if ((r = uv_listen((uv_stream_t *) &server, 128, udfdOnNewConnection))) { + debugPrint("Listen error %s", uv_err_name(r)); + return 2; + } + uv_run(loop, UV_RUN_DEFAULT); + uv_loop_close(loop); +} diff --git a/source/libs/function/test/runUdf.c b/source/libs/function/test/runUdf.c new file mode 100644 index 0000000000000000000000000000000000000000..b7d651e55e03bcc9a4f89a64d061396fbec6f2d6 --- /dev/null +++ b/source/libs/function/test/runUdf.c @@ -0,0 +1,45 @@ +#include +#include +#include + +#include "uv.h" +#include "tudf.h" + +int main(int argc, char *argv[]) { + startUdfService(); + uv_sleep(1000); + char path[256] = {0}; + size_t cwdSize = 256; + int err = uv_cwd(path, &cwdSize); + if (err != 0) { + fprintf(stderr, "err cwd: %s\n", uv_strerror(err)); + return err; + } + fprintf(stdout, "current working directory:%s\n", path); + strcat(path, "/libudf1.so"); + SUdfInfo udfInfo = {.udfName="udf1", .path=path}; + + UdfHandle handle; + setupUdf(&udfInfo, &handle); + + //char state[5000000] = "state"; + //char input[5000000] = "input"; + int dataSize = 500; + int callCount = 2; + if (argc > 1) dataSize = atoi(argv[1]); + if (argc > 2) callCount = atoi(argv[2]); + char *state = malloc(dataSize); + char *input = malloc(dataSize); + SUdfDataBlock blockInput = {.data = input, .size = dataSize}; + SUdfDataBlock blockOutput; + char* newState; + int32_t newStateSize; + for (int l = 0; l < callCount; ++l) { + callUdf(handle, 0, state, dataSize, blockInput, &newState, &newStateSize, &blockOutput); + } + free(state); + free(input); + teardownUdf(handle); + + stopUdfService(); +} diff --git a/source/libs/function/test/udf1.c b/source/libs/function/test/udf1.c new file mode 100644 index 0000000000000000000000000000000000000000..15e96e3bd1019831c75d652e87b5f0df9f9cd6c7 --- /dev/null +++ b/source/libs/function/test/udf1.c @@ -0,0 +1,20 @@ +#include +#include +#include + +#include "tudf.h" + +void udf1(int8_t step, char *state, int32_t stateSize, SUdfDataBlock input, + char **newState, int32_t *newStateSize, SUdfDataBlock *output) { + fprintf(stdout, "%s, step:%d\n", "udf function called", step); + char *newStateBuf = malloc(stateSize); + memcpy(newStateBuf, state, stateSize); + *newState = newStateBuf; + *newStateSize = stateSize; + + char *outputBuf = malloc(input.size); + memcpy(outputBuf, input.data, input.size); + output->data = outputBuf; + output->size = input.size; + return; +} diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index 9859d4a9b9119595d25fe3c066c5afdc28c72d01..b648e1880945528eb4af4b531b9bc9cccfc551e6 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -1480,7 +1480,7 @@ static int32_t jsonToDatum(const SJson* pJson, void* pObj) { case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_VARCHAR: case TSDB_DATA_TYPE_VARBINARY: { - pNode->datum.p = calloc(1, pNode->node.resType.bytes); + pNode->datum.p = calloc(1, pNode->node.resType.bytes + VARSTR_HEADER_SIZE + 1); if (NULL == pNode->datum.p) { code = TSDB_CODE_OUT_OF_MEMORY; break; diff --git a/source/libs/parser/inc/parAst.h b/source/libs/parser/inc/parAst.h index f417356209ce489469c0c6ea94bea4b58a50c38e..358edcb279f0ff7f7f328e7c86b6c5186a412885 100644 --- a/source/libs/parser/inc/parAst.h +++ b/source/libs/parser/inc/parAst.h @@ -150,9 +150,9 @@ SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName); SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort); SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode); SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig, const SToken* pValue); -SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, SToken* pIndexName, SToken* pTableName, SNodeList* pCols, SNode* pOptions); +SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName, SToken* pTableName, SNodeList* pCols, SNode* pOptions); SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding); -SNode* createDropIndexStmt(SAstCreateContext* pCxt, SToken* pIndexName, SToken* pTableName); +SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName, SToken* pTableName); SNode* createCreateQnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId); SNode* createDropQnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId); SNode* createCreateTopicStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName, SNode* pQuery, const SToken* pSubscribeDbName); diff --git a/source/libs/parser/inc/sql.y b/source/libs/parser/inc/sql.y index 30b03158e2880776b41e57092274af66d937f889..529fbb55c8e65521d8d3c20d26c963628750009f 100644 --- a/source/libs/parser/inc/sql.y +++ b/source/libs/parser/inc/sql.y @@ -313,10 +313,11 @@ func_name_list(A) ::= func_name_list(B) NK_COMMA col_name(C). func_name(A) ::= function_name(B). { A = createFunctionNode(pCxt, &B, NULL); } /************************************************ create index ********************************************************/ -cmd ::= CREATE SMA INDEX index_name(A) ON table_name(B) index_options(C). { pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_SMA, &A, &B, NULL, C); } -cmd ::= CREATE FULLTEXT INDEX - index_name(A) ON table_name(B) NK_LP col_name_list(C) NK_RP. { pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_FULLTEXT, &A, &B, C, NULL); } -cmd ::= DROP INDEX index_name(A) ON table_name(B). { pCxt->pRootNode = createDropIndexStmt(pCxt, &A, &B); } +cmd ::= CREATE SMA INDEX not_exists_opt(D) + index_name(A) ON table_name(B) index_options(C). { pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_SMA, D, &A, &B, NULL, C); } +cmd ::= CREATE FULLTEXT INDEX not_exists_opt(D) + index_name(A) ON table_name(B) NK_LP col_name_list(C) NK_RP. { pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_FULLTEXT, D, &A, &B, C, NULL); } +cmd ::= DROP INDEX exists_opt(C) index_name(A) ON table_name(B). { pCxt->pRootNode = createDropIndexStmt(pCxt, C, &A, &B); } index_options(A) ::= . { A = NULL; } index_options(A) ::= FUNCTION NK_LP func_list(B) NK_RP INTERVAL diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index a6d6e06113a83265d9cc27e7721121e0ab3395e9..4f27743b2ba9afe733f9c4a36c59ba778c1b42a6 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -1160,13 +1160,14 @@ SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const return (SNode*)pStmt; } -SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, SToken* pIndexName, SToken* pTableName, SNodeList* pCols, SNode* pOptions) { +SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName, SToken* pTableName, SNodeList* pCols, SNode* pOptions) { if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName)) { return NULL; } SCreateIndexStmt* pStmt = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT); CHECK_OUT_OF_MEM(pStmt); pStmt->indexType = type; + pStmt->ignoreExists = ignoreExists; strncpy(pStmt->indexName, pIndexName->z, pIndexName->n); strncpy(pStmt->tableName, pTableName->z, pTableName->n); pStmt->pCols = pCols; @@ -1184,12 +1185,13 @@ SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInt return (SNode*)pOptions; } -SNode* createDropIndexStmt(SAstCreateContext* pCxt, SToken* pIndexName, SToken* pTableName) { +SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName, SToken* pTableName) { if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName)) { return NULL; } SDropIndexStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT); CHECK_OUT_OF_MEM(pStmt); + pStmt->ignoreNotExists = ignoreNotExists; strncpy(pStmt->indexName, pIndexName->z, pIndexName->n); strncpy(pStmt->tableName, pTableName->z, pTableName->n); return (SNode*)pStmt; diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index f82ce2c1b4814b5d77142ffd0f08e09810f8ca79..8c842db16113da67dc0ec54f50b24311083d8e76 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -383,7 +383,7 @@ static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) { case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_VARCHAR: case TSDB_DATA_TYPE_VARBINARY: { - pVal->datum.p = calloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE); + pVal->datum.p = calloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1); if (NULL == pVal->datum.p) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY); } @@ -1406,34 +1406,110 @@ static int32_t translateShowTables(STranslateContext* pCxt) { return TSDB_CODE_SUCCESS; } -static int32_t translateCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) { - SVCreateTSmaReq createSmaReq = {0}; +static int32_t getSmaIndexDstVgId(STranslateContext* pCxt, char* pTableName, int32_t* pVgId) { + SVgroupInfo vg = {0}; + int32_t code = getTableHashVgroup(pCxt, pCxt->pParseCxt->db, pTableName, &vg); + if (TSDB_CODE_SUCCESS == code) { + *pVgId = vg.vgId; + } + return code; +} - if (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pInterval) || - (NULL != pStmt->pOptions->pOffset && DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pOffset)) || - (NULL != pStmt->pOptions->pSliding && DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pSliding))) { - return pCxt->errCode; +static int32_t getSmaIndexSql(STranslateContext* pCxt, char** pSql, int32_t* pLen) { + *pSql = strdup(pCxt->pParseCxt->pSql); + if (NULL == *pSql) { + return TSDB_CODE_OUT_OF_MEMORY; } + *pLen = pCxt->pParseCxt->sqlLen + 1; + return TSDB_CODE_SUCCESS; +} - createSmaReq.tSma.intervalUnit = ((SValueNode*)pStmt->pOptions->pInterval)->unit; - createSmaReq.tSma.slidingUnit = (NULL != pStmt->pOptions->pSliding ? ((SValueNode*)pStmt->pOptions->pSliding)->unit : 0); - strcpy(createSmaReq.tSma.indexName, pStmt->indexName); +static int32_t getSmaIndexExpr(STranslateContext* pCxt, SCreateIndexStmt* pStmt, char** pExpr, int32_t* pLen) { + return nodesListToString(pStmt->pOptions->pFuncs, false, pExpr, pLen); +} - SName name; - name.type = TSDB_TABLE_NAME_T; - name.acctId = pCxt->pParseCxt->acctId; +static int32_t getSmaIndexBuildAst(STranslateContext* pCxt, SCreateIndexStmt* pStmt, char** pAst, int32_t* pLen) { + SSelectStmt* pSelect = nodesMakeNode(QUERY_NODE_SELECT_STMT); + if (NULL == pSelect) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SRealTableNode* pTable = nodesMakeNode(QUERY_NODE_REAL_TABLE); + if (NULL == pTable) { + nodesDestroyNode(pSelect); + return TSDB_CODE_OUT_OF_MEMORY; + } + strcpy(pTable->table.dbName, pCxt->pParseCxt->db); + strcpy(pTable->table.tableName, pStmt->tableName); + pSelect->pFromTable = (SNode*)pTable; + + pSelect->pProjectionList = nodesCloneList(pStmt->pOptions->pFuncs); + if (NULL == pTable) { + nodesDestroyNode(pSelect); + return TSDB_CODE_OUT_OF_MEMORY; + } + + SIntervalWindowNode* pInterval = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW); + if (NULL == pInterval) { + nodesDestroyNode(pSelect); + return TSDB_CODE_OUT_OF_MEMORY; + } + pSelect->pWindow = (SNode*)pInterval; + pInterval->pInterval = nodesCloneNode(pStmt->pOptions->pInterval); + pInterval->pOffset = nodesCloneNode(pStmt->pOptions->pOffset); + pInterval->pSliding = nodesCloneNode(pStmt->pOptions->pSliding); + if (NULL == pInterval->pInterval || (NULL != pStmt->pOptions->pOffset && NULL == pInterval->pOffset) || + (NULL != pStmt->pOptions->pSliding && NULL == pInterval->pSliding)) { + nodesDestroyNode(pSelect); + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = translateQuery(pCxt, (SNode*)pSelect); + if (TSDB_CODE_SUCCESS == code) { + code = nodesNodeToString(pSelect, false, pAst, pLen); + } + nodesDestroyNode(pSelect); + return code; +} + +static int32_t buildCreateSmaReq(STranslateContext* pCxt, SCreateIndexStmt* pStmt, SMCreateSmaReq* pReq) { + SName name = { .type = TSDB_TABLE_NAME_T, .acctId = pCxt->pParseCxt->acctId }; strcpy(name.dbname, pCxt->pParseCxt->db); + strcpy(name.tname, pStmt->indexName); + tNameExtractFullName(&name, pReq->name); strcpy(name.tname, pStmt->tableName); - STableMeta* pMeta = NULL; - int32_t code = catalogGetTableMeta(pCxt->pParseCxt->pCatalog, pCxt->pParseCxt->pTransporter, &pCxt->pParseCxt->mgmtEpSet, &name, &pMeta); - if (TSDB_CODE_SUCCESS != code) { - return code; + name.tname[strlen(pStmt->tableName)] = '\0'; + tNameExtractFullName(&name, pReq->stb); + pReq->igExists = pStmt->ignoreExists; + pReq->interval = ((SValueNode*)pStmt->pOptions->pInterval)->datum.i; + pReq->intervalUnit = ((SValueNode*)pStmt->pOptions->pInterval)->unit; + pReq->offset = (NULL != pStmt->pOptions->pOffset ? ((SValueNode*)pStmt->pOptions->pOffset)->datum.i : 0); + pReq->sliding = (NULL != pStmt->pOptions->pSliding ? ((SValueNode*)pStmt->pOptions->pSliding)->datum.i : pReq->interval); + pReq->slidingUnit = (NULL != pStmt->pOptions->pSliding ? ((SValueNode*)pStmt->pOptions->pSliding)->unit : pReq->intervalUnit); + + int32_t code = getSmaIndexDstVgId(pCxt, pStmt->tableName, &pReq->dstVgId); + if (TSDB_CODE_SUCCESS == code) { + code = getSmaIndexSql(pCxt, &pReq->sql, &pReq->sqlLen); + } + if (TSDB_CODE_SUCCESS == code) { + code = getSmaIndexExpr(pCxt, pStmt, &pReq->expr, &pReq->exprLen); + } + if (TSDB_CODE_SUCCESS == code) { + code = getSmaIndexBuildAst(pCxt, pStmt, &pReq->ast, &pReq->astLen); + } + + return code; +} + +static int32_t translateCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) { + if (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pInterval) || + (NULL != pStmt->pOptions->pOffset && DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pOffset)) || + (NULL != pStmt->pOptions->pSliding && DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pSliding))) { + return pCxt->errCode; } - createSmaReq.tSma.tableUid = pMeta->uid; - createSmaReq.tSma.interval = ((SValueNode*)pStmt->pOptions->pInterval)->datum.i; - createSmaReq.tSma.sliding = (NULL != pStmt->pOptions->pSliding ? ((SValueNode*)pStmt->pOptions->pSliding)->datum.i : 0); - code = nodesListToString(pStmt->pOptions->pFuncs, false, &createSmaReq.tSma.expr, &createSmaReq.tSma.exprLen); + SMCreateSmaReq createSmaReq = {0}; + int32_t code = buildCreateSmaReq(pCxt, pStmt, &createSmaReq); if (TSDB_CODE_SUCCESS != code) { return code; } @@ -1443,15 +1519,14 @@ static int32_t translateCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt return TSDB_CODE_OUT_OF_MEMORY; } pCxt->pCmdMsg->epSet = pCxt->pParseCxt->mgmtEpSet; - pCxt->pCmdMsg->msgType = TDMT_VND_CREATE_SMA; - pCxt->pCmdMsg->msgLen = tSerializeSVCreateTSmaReq(NULL, &createSmaReq); + pCxt->pCmdMsg->msgType = TDMT_MND_CREATE_SMA; + pCxt->pCmdMsg->msgLen = tSerializeSMCreateSmaReq(NULL, 0, &createSmaReq); pCxt->pCmdMsg->pMsg = malloc(pCxt->pCmdMsg->msgLen); if (NULL == pCxt->pCmdMsg->pMsg) { return TSDB_CODE_OUT_OF_MEMORY; } - void* pBuf = pCxt->pCmdMsg->pMsg; - tSerializeSVCreateTSmaReq(&pBuf, &createSmaReq); - tdDestroyTSma(&createSmaReq.tSma); + tSerializeSMCreateSmaReq(pCxt->pCmdMsg->pMsg, pCxt->pCmdMsg->msgLen, &createSmaReq); + tFreeSMCreateSmaReq(&createSmaReq); return TSDB_CODE_SUCCESS; } @@ -1696,24 +1771,27 @@ static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode) { return code; } -static int32_t setReslutSchema(STranslateContext* pCxt, SQuery* pQuery) { - if (QUERY_NODE_SELECT_STMT == nodeType(pQuery->pRoot)) { - SSelectStmt* pSelect = (SSelectStmt*)pQuery->pRoot; - pQuery->numOfResCols = LIST_LENGTH(pSelect->pProjectionList); - pQuery->pResSchema = calloc(pQuery->numOfResCols, sizeof(SSchema)); - if (NULL == pQuery->pResSchema) { - return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY); +int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) { + if (QUERY_NODE_SELECT_STMT == nodeType(pRoot)) { + SSelectStmt* pSelect = (SSelectStmt*) pRoot; + *numOfCols = LIST_LENGTH(pSelect->pProjectionList); + *pSchema = calloc((*numOfCols), sizeof(SSchema)); + if (NULL == (*pSchema)) { + return TSDB_CODE_OUT_OF_MEMORY; } + SNode* pNode; int32_t index = 0; FOREACH(pNode, pSelect->pProjectionList) { SExprNode* pExpr = (SExprNode*)pNode; - pQuery->pResSchema[index].type = pExpr->resType.type; - pQuery->pResSchema[index].bytes = pExpr->resType.bytes; - strcpy(pQuery->pResSchema[index].name, pExpr->aliasName); + (*pSchema)[index].type = pExpr->resType.type; + (*pSchema)[index].bytes = pExpr->resType.bytes; + (*pSchema)[index].colId = index + 1; + strcpy((*pSchema)[index].name, pExpr->aliasName); index +=1; } } + return TSDB_CODE_SUCCESS; } @@ -2297,7 +2375,7 @@ static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) { pQuery->haveResultSet = true; pQuery->directRpc = false; pQuery->msgType = TDMT_VND_QUERY; - code = setReslutSchema(pCxt, pQuery); + code = qExtractResultSchema(pQuery->pRoot, &pQuery->numOfResCols, &pQuery->pResSchema); break; case QUERY_NODE_VNODE_MODIF_STMT: pQuery->haveResultSet = false; diff --git a/source/libs/parser/src/sql.c b/source/libs/parser/src/sql.c index 9c2edc4e74165e37fc0d9bac749db80635324ac7..8f9443d4ece4f358d4110c5c7c3cd58a8cebb2ba 100644 --- a/source/libs/parser/src/sql.c +++ b/source/libs/parser/src/sql.c @@ -131,17 +131,17 @@ typedef union { #define ParseCTX_PARAM #define ParseCTX_FETCH #define ParseCTX_STORE -#define YYNSTATE 427 +#define YYNSTATE 430 #define YYNRULE 337 #define YYNTOKEN 163 -#define YY_MAX_SHIFT 426 -#define YY_MIN_SHIFTREDUCE 659 -#define YY_MAX_SHIFTREDUCE 995 -#define YY_ERROR_ACTION 996 -#define YY_ACCEPT_ACTION 997 -#define YY_NO_ACTION 998 -#define YY_MIN_REDUCE 999 -#define YY_MAX_REDUCE 1335 +#define YY_MAX_SHIFT 429 +#define YY_MIN_SHIFTREDUCE 662 +#define YY_MAX_SHIFTREDUCE 998 +#define YY_ERROR_ACTION 999 +#define YY_ACCEPT_ACTION 1000 +#define YY_NO_ACTION 1001 +#define YY_MIN_REDUCE 1002 +#define YY_MAX_REDUCE 1338 /************* End control #defines *******************************************/ #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) @@ -208,135 +208,135 @@ typedef union { ** yy_default[] Default action for each state. ** *********** Begin parsing tables **********************************************/ -#define YY_ACTTAB_COUNT (1266) +#define YY_ACTTAB_COUNT (1263) static const YYACTIONTYPE yy_action[] = { - /* 0 */ 1042, 1201, 222, 43, 24, 168, 359, 1197, 1203, 1092, - /* 10 */ 247, 266, 89, 31, 29, 27, 26, 25, 20, 1201, - /* 20 */ 1098, 27, 26, 25, 1081, 1197, 1202, 1103, 31, 29, - /* 30 */ 27, 26, 25, 358, 997, 78, 206, 866, 77, 76, - /* 40 */ 75, 74, 73, 72, 71, 70, 69, 1088, 208, 411, - /* 50 */ 410, 409, 408, 407, 406, 405, 404, 403, 402, 401, - /* 60 */ 400, 399, 398, 397, 396, 395, 394, 393, 1000, 105, - /* 70 */ 267, 1011, 878, 31, 29, 27, 26, 25, 1228, 131, - /* 80 */ 901, 346, 111, 246, 234, 343, 1213, 1177, 272, 78, - /* 90 */ 130, 22, 77, 76, 75, 74, 73, 72, 71, 70, - /* 100 */ 69, 31, 29, 27, 26, 25, 1228, 1314, 208, 288, - /* 110 */ 317, 283, 267, 343, 287, 44, 902, 286, 128, 284, - /* 120 */ 117, 342, 285, 345, 1312, 23, 229, 1189, 896, 897, - /* 130 */ 898, 899, 900, 904, 905, 906, 1079, 201, 1214, 1217, - /* 140 */ 901, 769, 382, 381, 380, 773, 379, 775, 776, 378, - /* 150 */ 778, 375, 109, 784, 372, 786, 787, 369, 366, 1148, - /* 160 */ 1213, 846, 127, 1141, 302, 221, 125, 43, 323, 186, - /* 170 */ 1146, 358, 1133, 30, 28, 938, 902, 844, 255, 239, - /* 180 */ 1228, 231, 392, 846, 1099, 23, 229, 343, 896, 897, - /* 190 */ 898, 899, 900, 904, 905, 906, 1201, 345, 358, 844, - /* 200 */ 893, 1189, 1197, 1202, 303, 359, 331, 845, 12, 118, - /* 210 */ 66, 61, 1214, 1217, 1253, 1213, 962, 276, 207, 1249, - /* 220 */ 1165, 10, 122, 121, 30, 28, 1103, 120, 1314, 845, - /* 230 */ 1314, 1, 231, 423, 846, 1228, 313, 960, 961, 963, - /* 240 */ 964, 117, 330, 117, 1213, 1312, 242, 1312, 10, 321, - /* 250 */ 844, 334, 345, 1168, 1170, 423, 1189, 346, 696, 12, - /* 260 */ 695, 847, 850, 1178, 1228, 359, 62, 1214, 1217, 1253, - /* 270 */ 66, 330, 280, 224, 1249, 112, 279, 282, 697, 327, - /* 280 */ 845, 345, 1, 847, 850, 1189, 1103, 164, 118, 9, - /* 290 */ 8, 59, 1213, 309, 1280, 62, 1214, 1217, 1253, 281, - /* 300 */ 92, 93, 224, 1249, 112, 1314, 423, 327, 1095, 106, - /* 310 */ 1070, 903, 1228, 31, 29, 27, 26, 25, 1313, 343, - /* 320 */ 21, 1213, 1312, 1281, 392, 1267, 90, 868, 92, 345, - /* 330 */ 907, 419, 418, 1189, 847, 850, 114, 1260, 1261, 867, - /* 340 */ 1265, 1228, 1264, 62, 1214, 1217, 1253, 331, 343, 118, - /* 350 */ 224, 1249, 1326, 695, 90, 1094, 1213, 864, 345, 914, - /* 360 */ 1228, 1287, 1189, 865, 161, 1260, 326, 343, 325, 274, - /* 370 */ 235, 1314, 62, 1214, 1217, 1253, 1228, 359, 104, 224, - /* 380 */ 1249, 1326, 1100, 343, 117, 1213, 1105, 385, 1312, 1022, - /* 390 */ 1310, 1090, 320, 345, 30, 28, 1189, 1189, 1103, 1021, - /* 400 */ 1267, 333, 231, 104, 846, 1228, 1080, 62, 1214, 1217, - /* 410 */ 1253, 1106, 343, 1148, 224, 1249, 1326, 1263, 359, 236, - /* 420 */ 844, 1148, 345, 356, 1146, 1271, 1189, 243, 163, 12, - /* 430 */ 1189, 331, 1146, 30, 28, 1012, 196, 1214, 1217, 1103, - /* 440 */ 1189, 231, 1213, 846, 191, 1020, 1019, 1018, 136, 193, - /* 450 */ 845, 134, 1, 1038, 316, 1314, 30, 28, 344, 844, - /* 460 */ 6, 192, 1228, 389, 231, 1213, 846, 388, 117, 343, - /* 470 */ 1017, 123, 1312, 1148, 118, 289, 423, 322, 318, 345, - /* 480 */ 1016, 1015, 844, 1189, 1169, 1228, 1189, 1189, 1189, 845, - /* 490 */ 390, 7, 343, 63, 1214, 1217, 1253, 1148, 869, 1086, - /* 500 */ 1252, 1249, 345, 1045, 847, 850, 1189, 945, 1147, 387, - /* 510 */ 386, 1189, 845, 866, 7, 423, 63, 1214, 1217, 1253, - /* 520 */ 241, 1189, 1189, 341, 1249, 30, 28, 300, 104, 30, - /* 530 */ 28, 64, 384, 231, 338, 846, 1105, 231, 423, 846, - /* 540 */ 298, 1213, 1267, 847, 850, 999, 31, 29, 27, 26, - /* 550 */ 25, 844, 288, 933, 283, 844, 1014, 287, 118, 1262, - /* 560 */ 286, 1228, 284, 118, 1213, 285, 847, 850, 343, 87, - /* 570 */ 86, 85, 84, 83, 82, 81, 80, 79, 345, 864, - /* 580 */ 1033, 845, 1189, 7, 1228, 845, 248, 1, 1071, 260, - /* 590 */ 1013, 343, 107, 1214, 1217, 937, 426, 1189, 261, 149, - /* 600 */ 1213, 345, 291, 165, 359, 1189, 244, 423, 339, 357, - /* 610 */ 184, 423, 305, 88, 104, 63, 1214, 1217, 1253, 415, - /* 620 */ 1228, 183, 1105, 1250, 98, 1103, 1010, 343, 314, 332, - /* 630 */ 1327, 1189, 1031, 1009, 1008, 847, 850, 345, 359, 847, - /* 640 */ 850, 1189, 1142, 181, 230, 359, 60, 1007, 1006, 179, - /* 650 */ 245, 202, 1214, 1217, 294, 1005, 158, 1004, 1213, 1103, - /* 660 */ 1272, 933, 1229, 1213, 273, 259, 1103, 1189, 254, 253, - /* 670 */ 252, 251, 250, 335, 1189, 1189, 1003, 853, 1228, 138, - /* 680 */ 355, 1213, 137, 1228, 140, 343, 1213, 139, 1189, 1189, - /* 690 */ 343, 852, 991, 992, 308, 345, 1189, 146, 1189, 1189, - /* 700 */ 345, 1228, 310, 327, 1189, 328, 1228, 856, 343, 202, - /* 710 */ 1214, 1217, 1002, 343, 107, 1214, 1217, 1189, 345, 9, - /* 720 */ 8, 855, 1189, 345, 92, 228, 864, 1189, 1213, 936, - /* 730 */ 232, 1213, 202, 1214, 1217, 1283, 1213, 202, 1214, 1217, - /* 740 */ 31, 29, 27, 26, 25, 336, 327, 1167, 1228, 52, - /* 750 */ 90, 1228, 1328, 1189, 167, 343, 1228, 1078, 343, 329, - /* 760 */ 113, 1260, 1261, 343, 1265, 345, 1096, 92, 345, 1189, - /* 770 */ 2, 119, 1189, 345, 1213, 142, 257, 1189, 141, 200, - /* 780 */ 1214, 1217, 203, 1214, 1217, 959, 154, 194, 1214, 1217, - /* 790 */ 249, 256, 258, 90, 1228, 262, 1213, 41, 152, 878, - /* 800 */ 908, 343, 1213, 115, 1260, 1261, 872, 1265, 994, 995, - /* 810 */ 263, 345, 32, 264, 389, 1189, 1228, 124, 388, 871, - /* 820 */ 875, 58, 1228, 343, 42, 204, 1214, 1217, 1207, 343, - /* 830 */ 1213, 54, 32, 345, 839, 265, 1213, 1189, 268, 345, - /* 840 */ 1205, 390, 129, 1189, 275, 870, 32, 195, 1214, 1217, - /* 850 */ 1228, 277, 68, 205, 1214, 1217, 1228, 343, 173, 1093, - /* 860 */ 387, 386, 220, 343, 1213, 133, 351, 345, 178, 1089, - /* 870 */ 171, 1189, 135, 345, 762, 100, 101, 1189, 95, 1091, - /* 880 */ 96, 1225, 1214, 1217, 1228, 1087, 98, 1224, 1214, 1217, - /* 890 */ 757, 343, 1213, 102, 790, 794, 103, 800, 1213, 799, - /* 900 */ 307, 345, 41, 304, 145, 1189, 364, 96, 99, 97, - /* 910 */ 306, 98, 1228, 869, 315, 1223, 1214, 1217, 1228, 343, - /* 920 */ 96, 1284, 349, 1294, 150, 343, 312, 850, 1293, 345, - /* 930 */ 153, 223, 1213, 1189, 319, 345, 5, 110, 1274, 1189, - /* 940 */ 1213, 324, 157, 211, 1214, 1217, 238, 237, 311, 210, - /* 950 */ 1214, 1217, 1228, 4, 933, 91, 858, 868, 1268, 343, - /* 960 */ 1228, 33, 159, 160, 1311, 225, 1329, 343, 1213, 345, - /* 970 */ 337, 340, 851, 1189, 293, 215, 166, 345, 17, 1235, - /* 980 */ 1176, 1189, 347, 212, 1214, 1217, 348, 233, 1228, 301, - /* 990 */ 1175, 209, 1214, 1217, 352, 343, 353, 280, 354, 175, - /* 1000 */ 51, 279, 854, 144, 185, 345, 296, 187, 53, 1189, - /* 1010 */ 1104, 290, 182, 216, 143, 214, 213, 422, 278, 199, - /* 1020 */ 1214, 1217, 362, 197, 281, 198, 189, 190, 360, 1183, - /* 1030 */ 822, 1160, 1159, 94, 1158, 1157, 1156, 1155, 1154, 40, - /* 1040 */ 1153, 824, 39, 1152, 1151, 1150, 1149, 1044, 1182, 1173, - /* 1050 */ 126, 1082, 708, 1043, 1041, 269, 859, 850, 270, 271, - /* 1060 */ 1030, 1029, 1026, 1084, 67, 132, 805, 1083, 804, 1039, - /* 1070 */ 1034, 737, 803, 1032, 1025, 1024, 1181, 736, 217, 735, - /* 1080 */ 734, 218, 1180, 36, 1172, 45, 219, 148, 297, 3, - /* 1090 */ 733, 65, 32, 732, 299, 14, 292, 151, 958, 15, - /* 1100 */ 1205, 147, 162, 295, 37, 11, 156, 19, 34, 48, - /* 1110 */ 8, 350, 894, 1171, 177, 998, 998, 998, 998, 998, - /* 1120 */ 998, 783, 998, 998, 998, 998, 998, 998, 980, 979, - /* 1130 */ 226, 984, 983, 227, 860, 998, 998, 998, 108, 998, - /* 1140 */ 998, 176, 998, 998, 998, 998, 998, 155, 998, 952, - /* 1150 */ 46, 998, 951, 47, 998, 768, 930, 929, 998, 985, - /* 1160 */ 998, 998, 35, 998, 998, 998, 998, 998, 876, 116, - /* 1170 */ 13, 18, 16, 170, 956, 998, 172, 174, 169, 49, - /* 1180 */ 50, 1204, 998, 998, 54, 38, 363, 1040, 240, 367, - /* 1190 */ 1028, 370, 373, 180, 998, 361, 998, 796, 376, 998, - /* 1200 */ 791, 365, 728, 788, 368, 1027, 785, 998, 798, 371, - /* 1210 */ 420, 720, 797, 779, 998, 998, 998, 727, 782, 998, - /* 1220 */ 726, 998, 998, 725, 724, 706, 781, 374, 391, 777, - /* 1230 */ 723, 722, 721, 377, 719, 1023, 718, 717, 413, 716, - /* 1240 */ 383, 55, 56, 57, 425, 715, 729, 714, 713, 421, - /* 1250 */ 712, 711, 412, 848, 188, 424, 998, 416, 417, 998, - /* 1260 */ 998, 414, 998, 998, 998, 780, + /* 0 */ 1045, 1204, 225, 43, 24, 170, 362, 1200, 1206, 1095, + /* 10 */ 250, 269, 89, 31, 29, 27, 26, 25, 20, 1204, + /* 20 */ 1101, 27, 26, 25, 1084, 1200, 1205, 1106, 31, 29, + /* 30 */ 27, 26, 25, 361, 1000, 78, 209, 869, 77, 76, + /* 40 */ 75, 74, 73, 72, 71, 70, 69, 1091, 211, 414, + /* 50 */ 413, 412, 411, 410, 409, 408, 407, 406, 405, 404, + /* 60 */ 403, 402, 401, 400, 399, 398, 397, 396, 1003, 105, + /* 70 */ 270, 1014, 881, 31, 29, 27, 26, 25, 1231, 132, + /* 80 */ 904, 349, 111, 249, 237, 346, 1216, 1180, 275, 78, + /* 90 */ 131, 22, 77, 76, 75, 74, 73, 72, 71, 70, + /* 100 */ 69, 31, 29, 27, 26, 25, 1231, 1317, 211, 291, + /* 110 */ 320, 286, 270, 346, 290, 44, 905, 289, 129, 287, + /* 120 */ 117, 345, 288, 348, 1315, 23, 232, 1192, 899, 900, + /* 130 */ 901, 902, 903, 907, 908, 909, 1082, 204, 1217, 1220, + /* 140 */ 904, 772, 385, 384, 383, 776, 382, 778, 779, 381, + /* 150 */ 781, 378, 109, 787, 375, 789, 790, 372, 369, 1151, + /* 160 */ 1216, 849, 128, 1144, 305, 224, 125, 43, 326, 189, + /* 170 */ 1149, 361, 1136, 30, 28, 941, 905, 847, 258, 242, + /* 180 */ 1231, 234, 395, 849, 1102, 23, 232, 346, 899, 900, + /* 190 */ 901, 902, 903, 907, 908, 909, 1204, 348, 361, 847, + /* 200 */ 896, 1192, 1200, 1205, 306, 362, 334, 848, 12, 118, + /* 210 */ 66, 61, 1217, 1220, 1256, 1216, 965, 279, 210, 1252, + /* 220 */ 1168, 10, 122, 121, 30, 28, 1106, 120, 1317, 848, + /* 230 */ 1317, 1, 234, 426, 849, 1231, 316, 963, 964, 966, + /* 240 */ 967, 117, 333, 117, 1216, 1315, 245, 1315, 10, 324, + /* 250 */ 847, 337, 348, 1171, 1173, 426, 1192, 349, 699, 12, + /* 260 */ 698, 850, 853, 1181, 1231, 362, 62, 1217, 1220, 1256, + /* 270 */ 66, 333, 283, 227, 1252, 112, 282, 285, 700, 330, + /* 280 */ 848, 348, 1, 850, 853, 1192, 1106, 166, 118, 9, + /* 290 */ 8, 59, 1216, 312, 1283, 62, 1217, 1220, 1256, 284, + /* 300 */ 92, 93, 227, 1252, 112, 1317, 426, 330, 1098, 106, + /* 310 */ 1073, 906, 1231, 31, 29, 27, 26, 25, 1316, 346, + /* 320 */ 21, 1216, 1315, 1284, 395, 1270, 90, 871, 92, 348, + /* 330 */ 910, 422, 421, 1192, 850, 853, 114, 1263, 1264, 870, + /* 340 */ 1268, 1231, 1267, 62, 1217, 1220, 1256, 334, 346, 118, + /* 350 */ 227, 1252, 1329, 698, 90, 1097, 1216, 867, 348, 917, + /* 360 */ 1231, 1290, 1192, 868, 163, 1263, 329, 346, 328, 277, + /* 370 */ 238, 1317, 62, 1217, 1220, 1256, 1231, 362, 104, 227, + /* 380 */ 1252, 1329, 1103, 346, 117, 1216, 1108, 388, 1315, 1025, + /* 390 */ 1313, 1093, 323, 348, 30, 28, 1192, 1192, 1106, 1024, + /* 400 */ 1270, 336, 234, 104, 849, 1231, 1083, 62, 1217, 1220, + /* 410 */ 1256, 1109, 346, 1151, 227, 1252, 1329, 1266, 362, 239, + /* 420 */ 847, 1151, 348, 359, 1149, 1274, 1192, 246, 165, 12, + /* 430 */ 1192, 334, 1149, 30, 28, 1015, 199, 1217, 1220, 1106, + /* 440 */ 1192, 234, 1216, 849, 194, 1023, 1022, 1021, 137, 196, + /* 450 */ 848, 135, 1, 1041, 319, 1317, 30, 28, 347, 847, + /* 460 */ 6, 195, 1231, 392, 234, 1216, 849, 391, 117, 346, + /* 470 */ 1020, 123, 1315, 1151, 118, 292, 426, 325, 321, 348, + /* 480 */ 1019, 1018, 847, 1192, 1172, 1231, 1192, 1192, 1192, 848, + /* 490 */ 393, 7, 346, 63, 1217, 1220, 1256, 1151, 872, 1089, + /* 500 */ 1255, 1252, 348, 1048, 850, 853, 1192, 948, 1150, 390, + /* 510 */ 389, 1192, 848, 869, 7, 426, 63, 1217, 1220, 1256, + /* 520 */ 244, 1192, 1192, 344, 1252, 30, 28, 303, 104, 30, + /* 530 */ 28, 64, 387, 234, 341, 849, 1108, 234, 426, 849, + /* 540 */ 301, 1216, 1270, 850, 853, 1002, 31, 29, 27, 26, + /* 550 */ 25, 847, 291, 936, 286, 847, 1017, 290, 118, 1265, + /* 560 */ 289, 1231, 287, 118, 1216, 288, 850, 853, 346, 87, + /* 570 */ 86, 85, 84, 83, 82, 81, 80, 79, 348, 867, + /* 580 */ 1036, 848, 1192, 7, 1231, 848, 251, 1, 1074, 263, + /* 590 */ 1016, 346, 107, 1217, 1220, 940, 429, 1192, 264, 151, + /* 600 */ 1216, 348, 294, 167, 362, 1192, 247, 426, 342, 360, + /* 610 */ 187, 426, 308, 88, 104, 63, 1217, 1220, 1256, 418, + /* 620 */ 1231, 186, 1108, 1253, 98, 1106, 1013, 346, 317, 335, + /* 630 */ 1330, 1192, 1034, 1012, 1011, 850, 853, 348, 362, 850, + /* 640 */ 853, 1192, 1145, 184, 233, 362, 60, 1010, 1009, 182, + /* 650 */ 248, 205, 1217, 1220, 297, 1008, 160, 1007, 1216, 1106, + /* 660 */ 1275, 936, 1232, 1216, 276, 262, 1106, 1192, 257, 256, + /* 670 */ 255, 254, 253, 338, 1192, 1192, 1006, 856, 1231, 139, + /* 680 */ 358, 1216, 138, 1231, 141, 346, 1216, 140, 1192, 1192, + /* 690 */ 346, 855, 994, 995, 311, 348, 1192, 147, 1192, 1192, + /* 700 */ 348, 1231, 313, 330, 1192, 331, 1231, 859, 346, 205, + /* 710 */ 1217, 1220, 1005, 346, 107, 1217, 1220, 1192, 348, 9, + /* 720 */ 8, 858, 1192, 348, 92, 231, 867, 1192, 1216, 939, + /* 730 */ 235, 1216, 205, 1217, 1220, 1286, 1216, 205, 1217, 1220, + /* 740 */ 31, 29, 27, 26, 25, 339, 330, 1170, 1231, 52, + /* 750 */ 90, 1231, 1331, 1192, 169, 346, 1231, 1081, 346, 332, + /* 760 */ 113, 1263, 1264, 346, 1268, 348, 1099, 92, 348, 1192, + /* 770 */ 2, 119, 1192, 348, 1216, 143, 260, 1192, 142, 203, + /* 780 */ 1217, 1220, 206, 1217, 1220, 962, 156, 197, 1217, 1220, + /* 790 */ 252, 259, 261, 90, 1231, 265, 1216, 41, 154, 881, + /* 800 */ 911, 346, 1216, 115, 1263, 1264, 875, 1268, 997, 998, + /* 810 */ 266, 348, 32, 267, 392, 1192, 1231, 124, 391, 874, + /* 820 */ 878, 58, 1231, 346, 127, 207, 1217, 1220, 1210, 346, + /* 830 */ 1216, 54, 32, 348, 842, 268, 1216, 1192, 271, 348, + /* 840 */ 1208, 393, 42, 1192, 278, 130, 32, 198, 1217, 1220, + /* 850 */ 1231, 873, 68, 208, 1217, 1220, 1231, 346, 175, 280, + /* 860 */ 390, 389, 223, 346, 1216, 1096, 354, 348, 181, 134, + /* 870 */ 173, 1192, 1092, 348, 765, 136, 100, 1192, 95, 101, + /* 880 */ 96, 1228, 1217, 1220, 1231, 1094, 98, 1227, 1217, 1220, + /* 890 */ 760, 346, 1216, 1090, 793, 797, 102, 803, 1216, 802, + /* 900 */ 310, 348, 41, 103, 146, 1192, 367, 96, 99, 97, + /* 910 */ 149, 98, 1231, 307, 309, 1226, 1217, 1220, 1231, 346, + /* 920 */ 96, 872, 318, 1297, 1287, 346, 352, 152, 853, 348, + /* 930 */ 315, 1296, 1216, 1192, 226, 348, 5, 159, 155, 1192, + /* 940 */ 1216, 322, 327, 214, 1217, 1220, 241, 240, 1277, 213, + /* 950 */ 1217, 1220, 1231, 314, 4, 161, 861, 936, 91, 346, + /* 960 */ 1231, 871, 110, 1271, 33, 162, 228, 346, 1216, 348, + /* 970 */ 1332, 340, 854, 1192, 296, 218, 343, 348, 17, 1238, + /* 980 */ 1179, 1192, 168, 215, 1217, 1220, 350, 1314, 1231, 304, + /* 990 */ 351, 212, 1217, 1220, 355, 346, 1178, 283, 356, 236, + /* 1000 */ 177, 282, 857, 145, 357, 348, 299, 179, 53, 1192, + /* 1010 */ 51, 293, 188, 219, 144, 217, 216, 190, 281, 202, + /* 1020 */ 1217, 1220, 1107, 365, 284, 185, 425, 200, 363, 201, + /* 1030 */ 192, 193, 1186, 825, 1163, 1162, 94, 1161, 1160, 40, + /* 1040 */ 1159, 1158, 39, 1157, 1156, 827, 1155, 1154, 1153, 1152, + /* 1050 */ 1047, 1185, 1176, 126, 1085, 711, 862, 853, 1046, 1044, + /* 1060 */ 272, 273, 274, 1033, 1032, 1029, 1087, 67, 133, 808, + /* 1070 */ 1086, 807, 1042, 1037, 740, 806, 1035, 739, 1028, 220, + /* 1080 */ 738, 1027, 221, 737, 222, 1184, 1183, 36, 1175, 150, + /* 1090 */ 300, 302, 3, 736, 735, 65, 45, 32, 14, 153, + /* 1100 */ 295, 37, 15, 158, 19, 298, 1208, 34, 11, 164, + /* 1110 */ 48, 8, 35, 16, 148, 897, 353, 1174, 180, 1001, + /* 1120 */ 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, + /* 1130 */ 983, 982, 229, 987, 986, 230, 863, 1001, 1001, 1001, + /* 1140 */ 1001, 1001, 1001, 178, 1001, 961, 1001, 108, 1001, 157, + /* 1150 */ 1001, 955, 46, 1001, 954, 933, 1001, 47, 1001, 1001, + /* 1160 */ 932, 1001, 988, 1001, 1001, 1001, 1001, 1001, 1001, 366, + /* 1170 */ 879, 13, 116, 18, 243, 172, 959, 174, 176, 1001, + /* 1180 */ 171, 49, 50, 1001, 1207, 38, 370, 1001, 373, 794, + /* 1190 */ 54, 376, 771, 1001, 379, 183, 1001, 364, 799, 1001, + /* 1200 */ 1001, 1001, 368, 731, 791, 371, 1043, 1001, 788, 723, + /* 1210 */ 1001, 428, 423, 801, 374, 786, 800, 782, 1001, 1001, + /* 1220 */ 1001, 730, 377, 729, 728, 709, 780, 727, 380, 394, + /* 1230 */ 785, 726, 1031, 725, 724, 417, 722, 721, 1030, 1026, + /* 1240 */ 416, 720, 55, 56, 57, 415, 420, 732, 719, 424, + /* 1250 */ 1001, 851, 718, 717, 716, 386, 715, 784, 191, 783, + /* 1260 */ 714, 419, 427, }; static const YYCODETYPE yy_lookahead[] = { /* 0 */ 0, 207, 190, 174, 221, 222, 172, 213, 214, 187, @@ -423,49 +423,49 @@ static const YYCODETYPE yy_lookahead[] = { /* 810 */ 211, 203, 83, 193, 57, 207, 186, 174, 61, 20, /* 820 */ 71, 70, 186, 193, 174, 217, 218, 219, 70, 193, /* 830 */ 166, 80, 83, 203, 71, 204, 166, 207, 172, 203, - /* 840 */ 82, 84, 174, 207, 168, 20, 83, 217, 218, 219, - /* 850 */ 186, 186, 172, 217, 218, 219, 186, 193, 71, 186, + /* 840 */ 82, 84, 174, 207, 168, 174, 83, 217, 218, 219, + /* 850 */ 186, 20, 172, 217, 218, 219, 186, 193, 71, 186, /* 860 */ 103, 104, 168, 193, 166, 186, 71, 203, 71, 186, /* 870 */ 83, 207, 186, 203, 71, 186, 186, 207, 83, 186, /* 880 */ 83, 217, 218, 219, 186, 186, 83, 217, 218, 219, /* 890 */ 71, 193, 166, 186, 71, 71, 186, 71, 166, 71, - /* 900 */ 204, 203, 83, 211, 171, 207, 83, 83, 71, 83, - /* 910 */ 193, 83, 186, 20, 145, 217, 218, 219, 186, 193, - /* 920 */ 83, 216, 144, 246, 208, 193, 207, 123, 246, 203, - /* 930 */ 208, 207, 166, 207, 207, 203, 152, 240, 243, 207, - /* 940 */ 166, 151, 242, 217, 218, 219, 12, 13, 140, 217, - /* 950 */ 218, 219, 186, 139, 136, 193, 22, 20, 215, 193, - /* 960 */ 186, 114, 239, 227, 252, 160, 257, 193, 166, 203, - /* 970 */ 154, 156, 38, 207, 4, 35, 251, 203, 70, 223, - /* 980 */ 208, 207, 207, 217, 218, 219, 207, 207, 186, 19, - /* 990 */ 208, 217, 218, 219, 118, 193, 205, 57, 204, 193, - /* 1000 */ 171, 61, 68, 33, 182, 203, 36, 172, 70, 207, - /* 1010 */ 193, 41, 171, 73, 44, 75, 76, 168, 78, 217, - /* 1020 */ 218, 219, 189, 180, 84, 180, 173, 164, 94, 0, - /* 1030 */ 82, 0, 0, 114, 0, 0, 0, 0, 0, 69, - /* 1040 */ 0, 22, 72, 0, 0, 0, 0, 0, 0, 0, - /* 1050 */ 43, 0, 48, 0, 0, 38, 122, 123, 36, 43, - /* 1060 */ 0, 0, 0, 0, 79, 77, 38, 0, 38, 0, - /* 1070 */ 0, 38, 22, 0, 0, 0, 0, 38, 22, 38, - /* 1080 */ 38, 22, 0, 121, 0, 70, 22, 116, 22, 83, - /* 1090 */ 38, 20, 83, 38, 22, 141, 39, 71, 71, 141, - /* 1100 */ 82, 43, 82, 38, 83, 141, 83, 83, 135, 4, - /* 1110 */ 2, 119, 126, 0, 116, 258, 258, 258, 258, 258, - /* 1120 */ 258, 96, 258, 258, 258, 258, 258, 258, 38, 38, - /* 1130 */ 38, 38, 38, 38, 22, 258, 258, 258, 70, 258, - /* 1140 */ 258, 43, 258, 258, 258, 258, 258, 70, 258, 71, - /* 1150 */ 70, 258, 71, 70, 258, 22, 71, 71, 258, 71, - /* 1160 */ 258, 258, 83, 258, 258, 258, 258, 258, 71, 82, - /* 1170 */ 70, 70, 83, 71, 71, 258, 70, 70, 82, 70, - /* 1180 */ 70, 82, 258, 258, 80, 70, 38, 0, 38, 38, - /* 1190 */ 0, 38, 38, 82, 258, 81, 258, 22, 38, 258, - /* 1200 */ 71, 70, 22, 71, 70, 0, 71, 258, 38, 70, - /* 1210 */ 22, 22, 38, 71, 258, 258, 258, 38, 96, 258, - /* 1220 */ 38, 258, 258, 38, 38, 48, 96, 70, 47, 71, - /* 1230 */ 38, 38, 38, 70, 38, 0, 38, 38, 36, 38, - /* 1240 */ 84, 70, 70, 70, 20, 38, 68, 38, 38, 21, - /* 1250 */ 38, 38, 38, 22, 22, 21, 258, 38, 37, 258, - /* 1260 */ 258, 43, 258, 258, 258, 96, 258, 258, 258, 258, + /* 900 */ 204, 203, 83, 186, 171, 207, 83, 83, 71, 83, + /* 910 */ 171, 83, 186, 211, 193, 217, 218, 219, 186, 193, + /* 920 */ 83, 20, 145, 246, 216, 193, 144, 208, 123, 203, + /* 930 */ 207, 246, 166, 207, 207, 203, 152, 242, 208, 207, + /* 940 */ 166, 207, 151, 217, 218, 219, 12, 13, 243, 217, + /* 950 */ 218, 219, 186, 140, 139, 239, 22, 136, 193, 193, + /* 960 */ 186, 20, 240, 215, 114, 227, 160, 193, 166, 203, + /* 970 */ 257, 154, 38, 207, 4, 35, 156, 203, 70, 223, + /* 980 */ 208, 207, 251, 217, 218, 219, 207, 252, 186, 19, + /* 990 */ 207, 217, 218, 219, 118, 193, 208, 57, 205, 207, + /* 1000 */ 193, 61, 68, 33, 204, 203, 36, 171, 70, 207, + /* 1010 */ 171, 41, 182, 73, 44, 75, 76, 172, 78, 217, + /* 1020 */ 218, 219, 193, 189, 84, 171, 168, 180, 94, 180, + /* 1030 */ 173, 164, 0, 82, 0, 0, 114, 0, 0, 69, + /* 1040 */ 0, 0, 72, 0, 0, 22, 0, 0, 0, 0, + /* 1050 */ 0, 0, 0, 43, 0, 48, 122, 123, 0, 0, + /* 1060 */ 38, 36, 43, 0, 0, 0, 0, 79, 77, 38, + /* 1070 */ 0, 38, 0, 0, 38, 22, 0, 38, 0, 22, + /* 1080 */ 38, 0, 22, 38, 22, 0, 0, 121, 0, 116, + /* 1090 */ 22, 22, 83, 38, 38, 20, 70, 83, 141, 71, + /* 1100 */ 39, 83, 141, 83, 83, 38, 82, 135, 141, 82, + /* 1110 */ 4, 2, 83, 83, 43, 126, 119, 0, 116, 258, + /* 1120 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, + /* 1130 */ 38, 38, 38, 38, 38, 38, 22, 258, 258, 258, + /* 1140 */ 258, 258, 258, 43, 258, 71, 258, 70, 258, 70, + /* 1150 */ 258, 71, 70, 258, 71, 71, 258, 70, 258, 258, + /* 1160 */ 71, 258, 71, 258, 258, 258, 258, 258, 258, 38, + /* 1170 */ 71, 70, 82, 70, 38, 71, 71, 70, 70, 258, + /* 1180 */ 82, 70, 70, 258, 82, 70, 38, 258, 38, 71, + /* 1190 */ 80, 38, 22, 258, 38, 82, 258, 81, 22, 258, + /* 1200 */ 258, 258, 70, 22, 71, 70, 0, 258, 71, 22, + /* 1210 */ 258, 20, 22, 38, 70, 96, 38, 71, 258, 258, + /* 1220 */ 258, 38, 70, 38, 38, 48, 71, 38, 70, 47, + /* 1230 */ 96, 38, 0, 38, 38, 43, 38, 38, 0, 0, + /* 1240 */ 36, 38, 70, 70, 70, 38, 37, 68, 38, 21, + /* 1250 */ 258, 22, 38, 38, 38, 84, 38, 96, 22, 96, + /* 1260 */ 38, 38, 21, 258, 258, 258, 258, 258, 258, 258, /* 1270 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1280 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1290 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, @@ -481,11 +481,11 @@ static const YYCODETYPE yy_lookahead[] = { /* 1390 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1400 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1410 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, - /* 1420 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, + /* 1420 */ 258, 258, 258, 258, 258, 258, }; -#define YY_SHIFT_COUNT (426) +#define YY_SHIFT_COUNT (429) #define YY_SHIFT_MIN (0) -#define YY_SHIFT_MAX (1235) +#define YY_SHIFT_MAX (1241) static const unsigned short int yy_shift_ofst[] = { /* 0 */ 426, 212, 161, 382, 382, 382, 382, 421, 382, 382, /* 10 */ 151, 513, 517, 444, 513, 513, 513, 513, 513, 513, @@ -499,41 +499,41 @@ static const unsigned short int yy_shift_ofst[] = { /* 90 */ 307, 307, 307, 136, 343, 319, 17, 17, 17, 306, /* 100 */ 56, 56, 56, 56, 56, 68, 503, 534, 90, 215, /* 110 */ 335, 331, 478, 525, 417, 525, 493, 248, 591, 706, - /* 120 */ 657, 661, 661, 706, 786, 66, 343, 799, 66, 706, - /* 130 */ 66, 825, 17, 17, 17, 17, 17, 17, 17, 17, - /* 140 */ 17, 17, 17, 706, 825, 786, 278, 343, 799, 893, - /* 150 */ 769, 778, 804, 769, 778, 804, 804, 784, 790, 808, - /* 160 */ 814, 818, 343, 937, 847, 805, 815, 816, 908, 17, - /* 170 */ 778, 804, 804, 778, 804, 876, 343, 799, 306, 278, - /* 180 */ 343, 938, 706, 278, 825, 1266, 1266, 1266, 1266, 0, - /* 190 */ 545, 577, 46, 970, 16, 89, 728, 406, 757, 301, - /* 200 */ 301, 301, 301, 301, 301, 301, 115, 288, 196, 7, - /* 210 */ 7, 7, 7, 374, 605, 610, 701, 453, 580, 632, - /* 220 */ 506, 541, 714, 715, 718, 647, 590, 451, 729, 74, - /* 230 */ 749, 758, 763, 787, 795, 797, 803, 639, 653, 819, - /* 240 */ 823, 824, 826, 828, 837, 751, 1029, 948, 1031, 1032, - /* 250 */ 919, 1034, 1035, 1036, 1037, 1038, 1040, 1019, 1043, 1044, - /* 260 */ 1045, 1046, 1047, 1048, 1049, 1007, 1051, 1004, 1053, 1054, - /* 270 */ 1017, 1022, 1016, 1060, 1061, 1062, 1063, 985, 988, 1028, - /* 280 */ 1030, 1050, 1067, 1033, 1039, 1041, 1042, 1052, 1055, 1069, - /* 290 */ 1056, 1070, 1059, 1057, 1073, 1064, 1065, 1074, 1066, 1075, - /* 300 */ 1072, 1071, 1076, 1082, 962, 1084, 1015, 1058, 971, 1006, - /* 310 */ 1009, 954, 1026, 1021, 1027, 1068, 1077, 1078, 1080, 1081, - /* 320 */ 1023, 1018, 1083, 1024, 958, 1085, 1086, 1020, 973, 1079, - /* 330 */ 1087, 1088, 1089, 964, 1105, 1090, 1091, 1092, 1093, 1094, - /* 340 */ 1095, 1108, 986, 1096, 1097, 1100, 1101, 1102, 1103, 1106, - /* 350 */ 1107, 992, 1109, 1113, 1098, 998, 1110, 1104, 1099, 1111, - /* 360 */ 1112, 1115, 1114, 1129, 1148, 1150, 1131, 1132, 1151, 1134, - /* 370 */ 1135, 1153, 1139, 1142, 1154, 1157, 1158, 1160, 1163, 1025, - /* 380 */ 1122, 1130, 1169, 1133, 1156, 1171, 1172, 1173, 1170, 1174, - /* 390 */ 1175, 1177, 1181, 1178, 1180, 1179, 1182, 1185, 1186, 1192, - /* 400 */ 1193, 1194, 1189, 1196, 1198, 1199, 1201, 1207, 1209, 1210, - /* 410 */ 1212, 1213, 1187, 1214, 1202, 1218, 1190, 1219, 1221, 1205, - /* 420 */ 1235, 1188, 1228, 1231, 1232, 1234, 1224, + /* 120 */ 657, 661, 661, 706, 786, 66, 343, 799, 66, 66, + /* 130 */ 706, 66, 831, 17, 17, 17, 17, 17, 17, 17, + /* 140 */ 17, 17, 17, 17, 706, 831, 786, 278, 343, 799, + /* 150 */ 278, 901, 777, 782, 805, 777, 782, 805, 805, 784, + /* 160 */ 791, 813, 815, 821, 343, 941, 850, 806, 820, 817, + /* 170 */ 908, 17, 782, 805, 805, 782, 805, 876, 343, 799, + /* 180 */ 278, 306, 278, 343, 938, 706, 278, 831, 1263, 1263, + /* 190 */ 1263, 1263, 0, 545, 577, 46, 970, 16, 89, 728, + /* 200 */ 406, 757, 301, 301, 301, 301, 301, 301, 301, 115, + /* 210 */ 288, 196, 7, 7, 7, 7, 374, 605, 610, 701, + /* 220 */ 453, 580, 632, 506, 541, 714, 715, 718, 647, 590, + /* 230 */ 451, 729, 74, 749, 758, 763, 787, 795, 797, 803, + /* 240 */ 639, 653, 819, 823, 824, 826, 828, 837, 751, 1032, + /* 250 */ 951, 1034, 1035, 922, 1037, 1038, 1040, 1041, 1043, 1044, + /* 260 */ 1023, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1010, 1054, + /* 270 */ 1007, 1058, 1059, 1022, 1025, 1019, 1063, 1064, 1065, 1066, + /* 280 */ 988, 991, 1031, 1033, 1053, 1070, 1036, 1039, 1042, 1045, + /* 290 */ 1055, 1056, 1072, 1057, 1073, 1060, 1061, 1076, 1062, 1067, + /* 300 */ 1078, 1068, 1081, 1069, 1075, 1085, 1086, 966, 1088, 1026, + /* 310 */ 1071, 973, 1009, 1014, 957, 1028, 1018, 1074, 1077, 1079, + /* 320 */ 1080, 1082, 1083, 1020, 1024, 1087, 1021, 961, 1084, 1089, + /* 330 */ 1027, 972, 1029, 1090, 1091, 1030, 967, 1106, 1092, 1093, + /* 340 */ 1094, 1095, 1096, 1097, 1109, 989, 1098, 1099, 1101, 1103, + /* 350 */ 1104, 1105, 1107, 1108, 997, 1111, 1117, 1100, 1002, 1112, + /* 360 */ 1110, 1102, 1113, 1114, 1115, 1116, 1118, 1131, 1136, 1132, + /* 370 */ 1133, 1148, 1135, 1137, 1150, 1144, 1146, 1153, 1152, 1155, + /* 380 */ 1156, 1158, 1119, 1134, 1161, 1163, 1170, 1171, 1172, 1173, + /* 390 */ 1174, 1175, 1178, 1176, 1177, 1182, 1179, 1181, 1183, 1185, + /* 400 */ 1186, 1189, 1193, 1195, 1196, 1187, 1198, 1199, 1203, 1210, + /* 410 */ 1214, 1215, 1216, 1218, 1222, 1206, 1207, 1204, 1192, 1232, + /* 420 */ 1223, 1209, 1238, 1239, 1190, 1228, 1229, 1236, 1241, 1191, }; -#define YY_REDUCE_COUNT (188) +#define YY_REDUCE_COUNT (191) #define YY_REDUCE_MIN (-217) -#define YY_REDUCE_MAX (863) +#define YY_REDUCE_MAX (867) static const short yy_reduce_ofst[] = { /* 0 */ -129, -6, 49, 78, 126, 155, 190, 219, 276, 299, /* 10 */ 135, 375, 398, 434, 492, -80, 497, 515, 520, 562, @@ -547,58 +547,59 @@ static const short yy_reduce_ofst[] = { /* 90 */ 110, 185, 327, 578, 27, 54, 217, 287, 311, -10, /* 100 */ -178, -140, 204, 312, 345, 268, 412, 347, 381, 446, /* 110 */ 415, 495, 519, 471, 471, 471, 476, 504, 533, 575, - /* 120 */ 589, 592, 593, 623, 599, 643, 620, 631, 650, 666, - /* 130 */ 668, 676, 665, 673, 679, 683, 686, 689, 690, 693, - /* 140 */ 699, 707, 710, 680, 694, 692, 733, 717, 696, 705, - /* 150 */ 677, 716, 719, 682, 722, 724, 727, 695, 700, 697, - /* 160 */ 723, 471, 762, 743, 736, 709, 712, 725, 756, 476, - /* 170 */ 772, 775, 779, 782, 780, 791, 806, 794, 822, 829, - /* 180 */ 817, 833, 835, 841, 849, 843, 845, 853, 863, + /* 120 */ 589, 592, 593, 623, 599, 643, 620, 631, 650, 668, + /* 130 */ 666, 671, 676, 673, 679, 683, 686, 689, 690, 693, + /* 140 */ 699, 707, 710, 717, 680, 694, 702, 733, 721, 696, + /* 150 */ 739, 708, 677, 719, 723, 685, 730, 727, 734, 705, + /* 160 */ 695, 722, 716, 471, 765, 748, 738, 713, 735, 731, + /* 170 */ 756, 476, 772, 779, 783, 788, 792, 793, 807, 800, + /* 180 */ 836, 830, 839, 829, 834, 845, 854, 858, 847, 849, + /* 190 */ 857, 867, }; static const YYACTIONTYPE yy_default[] = { - /* 0 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 10 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 20 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 30 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 40 */ 996, 996, 996, 996, 1049, 996, 996, 996, 996, 996, - /* 50 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 60 */ 1047, 996, 1255, 996, 1161, 996, 996, 996, 996, 996, - /* 70 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 80 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 1049, - /* 90 */ 1266, 1266, 1266, 1047, 996, 996, 996, 996, 996, 1132, - /* 100 */ 996, 996, 996, 996, 996, 996, 996, 1330, 996, 1085, - /* 110 */ 1290, 996, 1282, 1258, 1272, 1259, 996, 1315, 1275, 996, - /* 120 */ 1166, 1163, 1163, 996, 996, 1049, 996, 996, 1049, 996, - /* 130 */ 1049, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 140 */ 996, 996, 996, 996, 996, 996, 1047, 996, 996, 996, - /* 150 */ 1297, 1295, 996, 1297, 1295, 996, 996, 1309, 1305, 1288, - /* 160 */ 1286, 1272, 996, 996, 996, 1333, 1321, 1317, 996, 996, - /* 170 */ 1295, 996, 996, 1295, 996, 1174, 996, 996, 996, 1047, - /* 180 */ 996, 1101, 996, 1047, 996, 1135, 1135, 1050, 1001, 996, - /* 190 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 1227, - /* 200 */ 1308, 1307, 1226, 1232, 1231, 1230, 996, 996, 996, 1221, - /* 210 */ 1222, 1220, 1219, 996, 996, 996, 996, 996, 996, 996, - /* 220 */ 996, 996, 996, 996, 1256, 996, 1318, 1322, 996, 996, - /* 230 */ 996, 1206, 996, 996, 996, 996, 996, 996, 996, 996, - /* 240 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 250 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 260 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 270 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 280 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 290 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 300 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 1279, - /* 310 */ 1289, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 320 */ 996, 1206, 996, 1306, 996, 1265, 1261, 996, 996, 1257, - /* 330 */ 996, 996, 1316, 996, 996, 996, 996, 996, 996, 996, - /* 340 */ 996, 1251, 996, 996, 996, 996, 996, 996, 996, 996, - /* 350 */ 996, 996, 996, 996, 996, 996, 996, 996, 1205, 996, - /* 360 */ 996, 996, 996, 996, 996, 996, 1129, 996, 996, 996, - /* 370 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 1114, - /* 380 */ 1112, 1111, 1110, 996, 1107, 996, 996, 996, 996, 996, - /* 390 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 400 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 410 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 420 */ 996, 996, 996, 996, 996, 996, 996, + /* 0 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 10 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 20 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 30 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 40 */ 999, 999, 999, 999, 1052, 999, 999, 999, 999, 999, + /* 50 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 60 */ 1050, 999, 1258, 999, 1164, 999, 999, 999, 999, 999, + /* 70 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 80 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 1052, + /* 90 */ 1269, 1269, 1269, 1050, 999, 999, 999, 999, 999, 1135, + /* 100 */ 999, 999, 999, 999, 999, 999, 999, 1333, 999, 1088, + /* 110 */ 1293, 999, 1285, 1261, 1275, 1262, 999, 1318, 1278, 999, + /* 120 */ 1169, 1166, 1166, 999, 999, 1052, 999, 999, 1052, 1052, + /* 130 */ 999, 1052, 999, 999, 999, 999, 999, 999, 999, 999, + /* 140 */ 999, 999, 999, 999, 999, 999, 999, 1050, 999, 999, + /* 150 */ 1050, 999, 1300, 1298, 999, 1300, 1298, 999, 999, 1312, + /* 160 */ 1308, 1291, 1289, 1275, 999, 999, 999, 1336, 1324, 1320, + /* 170 */ 999, 999, 1298, 999, 999, 1298, 999, 1177, 999, 999, + /* 180 */ 1050, 999, 1050, 999, 1104, 999, 1050, 999, 1138, 1138, + /* 190 */ 1053, 1004, 999, 999, 999, 999, 999, 999, 999, 999, + /* 200 */ 999, 999, 1230, 1311, 1310, 1229, 1235, 1234, 1233, 999, + /* 210 */ 999, 999, 1224, 1225, 1223, 1222, 999, 999, 999, 999, + /* 220 */ 999, 999, 999, 999, 999, 999, 999, 1259, 999, 1321, + /* 230 */ 1325, 999, 999, 999, 1209, 999, 999, 999, 999, 999, + /* 240 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 250 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 260 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 270 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 280 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 290 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 300 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 310 */ 999, 999, 1282, 1292, 999, 999, 999, 999, 999, 999, + /* 320 */ 999, 999, 999, 999, 1209, 999, 1309, 999, 1268, 1264, + /* 330 */ 999, 999, 1260, 999, 999, 1319, 999, 999, 999, 999, + /* 340 */ 999, 999, 999, 999, 1254, 999, 999, 999, 999, 999, + /* 350 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 360 */ 999, 1208, 999, 999, 999, 999, 999, 999, 999, 1132, + /* 370 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 380 */ 999, 999, 1117, 1115, 1114, 1113, 999, 1110, 999, 999, + /* 390 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 400 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 410 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, + /* 420 */ 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, }; /********** End of lemon-generated parsing tables *****************************/ @@ -1142,9 +1143,9 @@ static const char *const yyRuleName[] = { /* 169 */ "func_name_list ::= func_name", /* 170 */ "func_name_list ::= func_name_list NK_COMMA col_name", /* 171 */ "func_name ::= function_name", - /* 172 */ "cmd ::= CREATE SMA INDEX index_name ON table_name index_options", - /* 173 */ "cmd ::= CREATE FULLTEXT INDEX index_name ON table_name NK_LP col_name_list NK_RP", - /* 174 */ "cmd ::= DROP INDEX index_name ON table_name", + /* 172 */ "cmd ::= CREATE SMA INDEX not_exists_opt index_name ON table_name index_options", + /* 173 */ "cmd ::= CREATE FULLTEXT INDEX not_exists_opt index_name ON table_name NK_LP col_name_list NK_RP", + /* 174 */ "cmd ::= DROP INDEX exists_opt index_name ON table_name", /* 175 */ "index_options ::=", /* 176 */ "index_options ::= FUNCTION NK_LP func_list NK_RP INTERVAL NK_LP duration_literal NK_RP sliding_opt", /* 177 */ "index_options ::= FUNCTION NK_LP func_list NK_RP INTERVAL NK_LP duration_literal NK_COMMA duration_literal NK_RP sliding_opt", @@ -2042,9 +2043,9 @@ static const struct { { 195, -1 }, /* (169) func_name_list ::= func_name */ { 195, -3 }, /* (170) func_name_list ::= func_name_list NK_COMMA col_name */ { 202, -1 }, /* (171) func_name ::= function_name */ - { 163, -7 }, /* (172) cmd ::= CREATE SMA INDEX index_name ON table_name index_options */ - { 163, -9 }, /* (173) cmd ::= CREATE FULLTEXT INDEX index_name ON table_name NK_LP col_name_list NK_RP */ - { 163, -5 }, /* (174) cmd ::= DROP INDEX index_name ON table_name */ + { 163, -8 }, /* (172) cmd ::= CREATE SMA INDEX not_exists_opt index_name ON table_name index_options */ + { 163, -10 }, /* (173) cmd ::= CREATE FULLTEXT INDEX not_exists_opt index_name ON table_name NK_LP col_name_list NK_RP */ + { 163, -6 }, /* (174) cmd ::= DROP INDEX exists_opt index_name ON table_name */ { 205, 0 }, /* (175) index_options ::= */ { 205, -9 }, /* (176) index_options ::= FUNCTION NK_LP func_list NK_RP INTERVAL NK_LP duration_literal NK_RP sliding_opt */ { 205, -11 }, /* (177) index_options ::= FUNCTION NK_LP func_list NK_RP INTERVAL NK_LP duration_literal NK_COMMA duration_literal NK_RP sliding_opt */ @@ -2840,14 +2841,14 @@ static YYACTIONTYPE yy_reduce( { yylhsminor.yy140 = createFunctionNode(pCxt, &yymsp[0].minor.yy149, NULL); } yymsp[0].minor.yy140 = yylhsminor.yy140; break; - case 172: /* cmd ::= CREATE SMA INDEX index_name ON table_name index_options */ -{ pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_SMA, &yymsp[-3].minor.yy149, &yymsp[-1].minor.yy149, NULL, yymsp[0].minor.yy140); } + case 172: /* cmd ::= CREATE SMA INDEX not_exists_opt index_name ON table_name index_options */ +{ pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_SMA, yymsp[-4].minor.yy497, &yymsp[-3].minor.yy149, &yymsp[-1].minor.yy149, NULL, yymsp[0].minor.yy140); } break; - case 173: /* cmd ::= CREATE FULLTEXT INDEX index_name ON table_name NK_LP col_name_list NK_RP */ -{ pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_FULLTEXT, &yymsp[-5].minor.yy149, &yymsp[-3].minor.yy149, yymsp[-1].minor.yy136, NULL); } + case 173: /* cmd ::= CREATE FULLTEXT INDEX not_exists_opt index_name ON table_name NK_LP col_name_list NK_RP */ +{ pCxt->pRootNode = createCreateIndexStmt(pCxt, INDEX_TYPE_FULLTEXT, yymsp[-6].minor.yy497, &yymsp[-5].minor.yy149, &yymsp[-3].minor.yy149, yymsp[-1].minor.yy136, NULL); } break; - case 174: /* cmd ::= DROP INDEX index_name ON table_name */ -{ pCxt->pRootNode = createDropIndexStmt(pCxt, &yymsp[-2].minor.yy149, &yymsp[0].minor.yy149); } + case 174: /* cmd ::= DROP INDEX exists_opt index_name ON table_name */ +{ pCxt->pRootNode = createDropIndexStmt(pCxt, yymsp[-3].minor.yy497, &yymsp[-2].minor.yy149, &yymsp[0].minor.yy149); } break; case 176: /* index_options ::= FUNCTION NK_LP func_list NK_RP INTERVAL NK_LP duration_literal NK_RP sliding_opt */ { yymsp[-8].minor.yy140 = createIndexOption(pCxt, yymsp[-6].minor.yy136, releaseRawExprNode(pCxt, yymsp[-2].minor.yy140), NULL, yymsp[0].minor.yy140); } diff --git a/source/libs/sync/inc/syncRaftEntry.h b/source/libs/sync/inc/syncRaftEntry.h index 037a49f45c5e7a43f77d2dfff13edf7be56398fd..d4e63e9676d519c0ae92a0d93a7199f780c58eee 100644 --- a/source/libs/sync/inc/syncRaftEntry.h +++ b/source/libs/sync/inc/syncRaftEntry.h @@ -27,6 +27,12 @@ extern "C" { #include "syncMessage.h" #include "taosdef.h" +typedef enum EntryType { + SYNC_RAFT_ENTRY_NOOP = 0, + SYNC_RAFT_ENTRY_DATA = 1, + SYNC_RAFT_ENTRY_CONFIG = 2, +} EntryType; + typedef struct SSyncRaftEntry { uint32_t bytes; uint32_t msgType; @@ -35,12 +41,15 @@ typedef struct SSyncRaftEntry { bool isWeak; SyncTerm term; SyncIndex index; + EntryType entryType; uint32_t dataLen; char data[]; } SSyncRaftEntry; SSyncRaftEntry* syncEntryBuild(uint32_t dataLen); SSyncRaftEntry* syncEntryBuild2(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index); // step 4 +SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index, EntryType entryType); +SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index); void syncEntryDestory(SSyncRaftEntry* pEntry); char* syncEntrySerialize(const SSyncRaftEntry* pEntry, uint32_t* len); // step 5 SSyncRaftEntry* syncEntryDeserialize(const char* buf, uint32_t len); // step 6 diff --git a/source/libs/sync/src/syncAppendEntries.c b/source/libs/sync/src/syncAppendEntries.c index 1116c1a9050753f1d3e8d47e4b0b343081b3f2d5..2e9cd63de6aa0f80daefa3f385c9b4463e4f5adc 100644 --- a/source/libs/sync/src/syncAppendEntries.c +++ b/source/libs/sync/src/syncAppendEntries.c @@ -199,6 +199,9 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SSyncRaftEntry* pRollBackEntry = logStoreGetEntry(ths->pLogStore, index); assert(pRollBackEntry != NULL); + // maybe is a NOOP ENTRY + // assert(pRollBackEntry->entryType == SYNC_RAFT_ENTRY_DATA); + SRpcMsg rpcMsg; syncEntry2OriginalRpc(pRollBackEntry, &rpcMsg); ths->pFsm->FpRollBackCb(ths->pFsm, &rpcMsg, pRollBackEntry->index, pRollBackEntry->isWeak, 0, ths->state); @@ -217,7 +220,7 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pAppendEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL) { + if (ths->pFsm->FpPreCommitCb != NULL && pAppendEntry->entryType == SYNC_RAFT_ENTRY_DATA) { ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pAppendEntry->index, pAppendEntry->isWeak, 2, ths->state); } } @@ -242,7 +245,7 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pAppendEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL) { + if (ths->pFsm->FpPreCommitCb != NULL && pAppendEntry->entryType == SYNC_RAFT_ENTRY_DATA) { ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pAppendEntry->index, pAppendEntry->isWeak, 3, ths->state); } } @@ -298,7 +301,7 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pEntry, &rpcMsg); - if (ths->pFsm->FpCommitCb != NULL) { + if (ths->pFsm->FpCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { ths->pFsm->FpCommitCb(ths->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 0, ths->state); } diff --git a/source/libs/sync/src/syncCommit.c b/source/libs/sync/src/syncCommit.c index f581b31c4b34dbd733a207d40fda67ede825d1f5..6023c00afa183716c06828470a1e91e0fdec087b 100644 --- a/source/libs/sync/src/syncCommit.c +++ b/source/libs/sync/src/syncCommit.c @@ -97,7 +97,7 @@ void syncMaybeAdvanceCommitIndex(SSyncNode* pSyncNode) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pEntry, &rpcMsg); - if (pSyncNode->pFsm->FpCommitCb != NULL) { + if (pSyncNode->pFsm->FpCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 0, pSyncNode->state); } diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index b05d2e397dfb6b22607e26dd5dec7833aa960c40..da54aa9668d0b693a6efcdce28826dd65f7061a3 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -29,7 +29,7 @@ static int32_t syncIODestroy(SSyncIO *io); static int32_t syncIOStartInternal(SSyncIO *io); static int32_t syncIOStopInternal(SSyncIO *io); -static void * syncIOConsumerFunc(void *param); +static void *syncIOConsumerFunc(void *param); static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static void syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static int32_t syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); @@ -75,6 +75,7 @@ int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) { syncRpcMsgPrint2(logBuf, pMsg); pMsg->handle = NULL; + pMsg->noResp = 1; rpcSendRequest(clientRpc, pEpSet, pMsg, NULL); return ret; } @@ -234,9 +235,9 @@ static int32_t syncIOStopInternal(SSyncIO *io) { } static void *syncIOConsumerFunc(void *param) { - SSyncIO * io = param; + SSyncIO *io = param; STaosQall *qall; - SRpcMsg * pRpcMsg, rpcMsg; + SRpcMsg *pRpcMsg, rpcMsg; qall = taosAllocateQall(); while (1) { @@ -324,19 +325,21 @@ static void *syncIOConsumerFunc(void *param) { taosGetQitem(qall, (void **)&pRpcMsg); rpcFreeCont(pRpcMsg->pCont); - if (pRpcMsg->handle != NULL) { - int msgSize = 32; - memset(&rpcMsg, 0, sizeof(rpcMsg)); - rpcMsg.msgType = SYNC_RESPONSE; - rpcMsg.pCont = rpcMallocCont(msgSize); - rpcMsg.contLen = msgSize; - snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "give a reply"); - rpcMsg.handle = pRpcMsg->handle; - rpcMsg.code = 0; - - syncRpcMsgPrint2((char *)"syncIOConsumerFunc rpcSendResponse --> ", &rpcMsg); - rpcSendResponse(&rpcMsg); - } + /* + if (pRpcMsg->handle != NULL) { + int msgSize = 32; + memset(&rpcMsg, 0, sizeof(rpcMsg)); + rpcMsg.msgType = SYNC_RESPONSE; + rpcMsg.pCont = rpcMallocCont(msgSize); + rpcMsg.contLen = msgSize; + snprintf(rpcMsg.pCont, rpcMsg.contLen, "%s", "give a reply"); + rpcMsg.handle = pRpcMsg->handle; + rpcMsg.code = 0; + + syncRpcMsgPrint2((char *)"syncIOConsumerFunc rpcSendResponse --> ", &rpcMsg); + rpcSendResponse(&rpcMsg); + } + */ taosFreeQitem(pRpcMsg); } diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index f2341ef521f8516c4c56186a1fa9a70f136929b2..58e02b3866b4ef0d3e6a85c14fd1069b494e28bb 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -37,11 +37,13 @@ static int32_t tsNodeRefId = -1; // ------ local funciton --------- // enqueue message ---- -static void syncNodeEqPingTimer(void* param, void* tmrId); -static void syncNodeEqElectTimer(void* param, void* tmrId); -static void syncNodeEqHeartbeatTimer(void* param, void* tmrId); +static void syncNodeEqPingTimer(void* param, void* tmrId); +static void syncNodeEqElectTimer(void* param, void* tmrId); +static void syncNodeEqHeartbeatTimer(void* param, void* tmrId); +static int32_t syncNodeEqNoop(SSyncNode* ths); +static int32_t syncNodeAppendNoop(SSyncNode* ths); -// on message ---- +// process message ---- static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg); static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg); static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg); @@ -669,6 +671,10 @@ void syncNodeCandidate2Leader(SSyncNode* pSyncNode) { assert(pSyncNode->state == TAOS_SYNC_STATE_CANDIDATE); assert(voteGrantedMajority(pSyncNode->pVotesGranted)); syncNodeBecomeLeader(pSyncNode); + + // Raft 3.6.2 Committing entries from previous terms + syncNodeAppendNoop(pSyncNode); + // syncNodeEqNoop(pSyncNode); } void syncNodeFollower2Candidate(SSyncNode* pSyncNode) { @@ -803,6 +809,47 @@ static void syncNodeEqHeartbeatTimer(void* param, void* tmrId) { } } +static int32_t syncNodeEqNoop(SSyncNode* ths) { + int32_t ret = 0; + assert(ths->state == TAOS_SYNC_STATE_LEADER); + + SyncIndex index = ths->pLogStore->getLastIndex(ths->pLogStore) + 1; + SyncTerm term = ths->pRaftStore->currentTerm; + SSyncRaftEntry* pEntry = syncEntryBuildNoop(term, index); + assert(pEntry != NULL); + + uint32_t entryLen; + char* serialized = syncEntrySerialize(pEntry, &entryLen); + SyncClientRequest* pSyncMsg = syncClientRequestBuild(entryLen); + assert(pSyncMsg->dataLen == entryLen); + memcpy(pSyncMsg->data, serialized, entryLen); + + SRpcMsg rpcMsg; + syncClientRequest2RpcMsg(pSyncMsg, &rpcMsg); + ths->FpEqMsg(ths->queue, &rpcMsg); + + free(serialized); + syncClientRequestDestroy(pSyncMsg); + + return ret; +} + +static int32_t syncNodeAppendNoop(SSyncNode* ths) { + int32_t ret = 0; + + SyncIndex index = ths->pLogStore->getLastIndex(ths->pLogStore) + 1; + SyncTerm term = ths->pRaftStore->currentTerm; + SSyncRaftEntry* pEntry = syncEntryBuildNoop(term, index); + assert(pEntry != NULL); + + if (ths->state == TAOS_SYNC_STATE_LEADER) { + ths->pLogStore->appendEntry(ths->pLogStore, pEntry); + syncNodeReplicate(ths); + } + + return ret; +} + // on message ---- static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { int32_t ret = 0; @@ -851,7 +898,7 @@ static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg syncEntry2OriginalRpc(pEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL) { + if (ths->pFsm->FpPreCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 0, ths->state); } } @@ -866,7 +913,7 @@ static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg syncEntry2OriginalRpc(pEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL) { + if (ths->pFsm->FpPreCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 1, ths->state); } } diff --git a/source/libs/sync/src/syncRaftEntry.c b/source/libs/sync/src/syncRaftEntry.c index e4c5882e9896d423709e2fcc4c8de6245ad908b4..9b9842b81d5c8cc9dd2a64dc0ba3ff2634907fa4 100644 --- a/source/libs/sync/src/syncRaftEntry.c +++ b/source/libs/sync/src/syncRaftEntry.c @@ -28,6 +28,13 @@ SSyncRaftEntry* syncEntryBuild(uint32_t dataLen) { // step 4. SyncClientRequest => SSyncRaftEntry, add term, index SSyncRaftEntry* syncEntryBuild2(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index) { + SSyncRaftEntry* pEntry = syncEntryBuild3(pMsg, term, index, SYNC_RAFT_ENTRY_DATA); + assert(pEntry != NULL); + + return pEntry; +} + +SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index, EntryType entryType) { SSyncRaftEntry* pEntry = syncEntryBuild(pMsg->dataLen); assert(pEntry != NULL); @@ -37,12 +44,23 @@ SSyncRaftEntry* syncEntryBuild2(SyncClientRequest* pMsg, SyncTerm term, SyncInde pEntry->isWeak = pMsg->isWeak; pEntry->term = term; pEntry->index = index; + pEntry->entryType = entryType; pEntry->dataLen = pMsg->dataLen; memcpy(pEntry->data, pMsg->data, pMsg->dataLen); return pEntry; } +SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index) { + SSyncRaftEntry* pEntry = syncEntryBuild(0); + assert(pEntry != NULL); + pEntry->term = term; + pEntry->index = index; + pEntry->entryType = SYNC_RAFT_ENTRY_NOOP; + + return pEntry; +} + void syncEntryDestory(SSyncRaftEntry* pEntry) { if (pEntry != NULL) { free(pEntry); @@ -85,6 +103,7 @@ cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry) { cJSON_AddStringToObject(pRoot, "term", u64buf); snprintf(u64buf, sizeof(u64buf), "%lu", pEntry->index); cJSON_AddStringToObject(pRoot, "index", u64buf); + cJSON_AddNumberToObject(pRoot, "entryType", pEntry->entryType); cJSON_AddNumberToObject(pRoot, "dataLen", pEntry->dataLen); char* s; diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index f569c5d6216a3606ce3a9218246b35bb7da0739b..b28f899aa9c333cfd0718cdfedcfd921f6faf5be 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -34,7 +34,7 @@ SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode) { pLogStore->getLastTerm = logStoreLastTerm; pLogStore->updateCommitIndex = logStoreUpdateCommitIndex; pLogStore->getCommitIndex = logStoreGetCommitIndex; - return pLogStore; // to avoid compiler error + return pLogStore; } void logStoreDestory(SSyncLogStore* pLogStore) { @@ -48,18 +48,22 @@ int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; - assert(pEntry->index == logStoreLastIndex(pLogStore) + 1); + SyncIndex lastIndex = logStoreLastIndex(pLogStore); + assert(pEntry->index == lastIndex + 1); uint32_t len; char* serialized = syncEntrySerialize(pEntry, &len); assert(serialized != NULL); - int code; - code = walWrite(pWal, pEntry->index, pEntry->msgType, serialized, len); - assert(code == 0); + int code = 0; + /* + code = walWrite(pWal, pEntry->index, pEntry->entryType, serialized, len); + assert(code == 0); + */ + assert(walWrite(pWal, pEntry->index, pEntry->entryType, serialized, len) == 0); walFsync(pWal, true); free(serialized); - return code; // to avoid compiler error + return code; } SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { @@ -69,7 +73,7 @@ SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { if (index >= SYNC_INDEX_BEGIN && index <= logStoreLastIndex(pLogStore)) { SWalReadHandle* pWalHandle = walOpenReadHandle(pWal); - walReadWithHandle(pWalHandle, index); + assert(walReadWithHandle(pWalHandle, index) == 0); pEntry = syncEntryDeserialize(pWalHandle->pHead->head.body, pWalHandle->pHead->head.len); assert(pEntry != NULL); @@ -83,7 +87,7 @@ SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; - walRollback(pWal, fromIndex); + assert(walRollback(pWal, fromIndex) == 0); return 0; // to avoid compiler error } @@ -107,7 +111,7 @@ SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore) { int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; - walCommit(pWal, index); + assert(walCommit(pWal, index) == 0); return 0; // to avoid compiler error } diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index dcf380e7e4248dfd2d8e6f613d2482ec818d3b41..7341565b1d526239e2b15aae2d463b294991c3da 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -34,6 +34,7 @@ add_executable(syncEncodeTest "") add_executable(syncWriteTest "") add_executable(syncReplicateTest "") add_executable(syncReplicateTest2 "") +add_executable(syncReplicateTest3 "") add_executable(syncReplicateLoadTest "") add_executable(syncRefTest "") add_executable(syncLogStoreCheck "") @@ -183,6 +184,10 @@ target_sources(syncReplicateTest2 PRIVATE "syncReplicateTest2.cpp" ) +target_sources(syncReplicateTest3 + PRIVATE + "syncReplicateTest3.cpp" +) target_sources(syncReplicateLoadTest PRIVATE "syncReplicateLoadTest.cpp" @@ -377,6 +382,11 @@ target_include_directories(syncReplicateTest2 "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncReplicateTest3 + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_include_directories(syncReplicateLoadTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" @@ -538,6 +548,10 @@ target_link_libraries(syncReplicateTest2 sync gtest_main ) +target_link_libraries(syncReplicateTest3 + sync + gtest_main +) target_link_libraries(syncReplicateLoadTest sync gtest_main diff --git a/source/libs/sync/test/syncEntryTest.cpp b/source/libs/sync/test/syncEntryTest.cpp index e54daaa79a052504fd8cb8bbbb7f59baec9e6385..2c8433426a32010234db8a0b0f18e2f915be6399 100644 --- a/source/libs/sync/test/syncEntryTest.cpp +++ b/source/libs/sync/test/syncEntryTest.cpp @@ -46,6 +46,20 @@ void test2() { } void test3() { + SyncClientRequest* pSyncMsg = syncClientRequestBuild(10); + pSyncMsg->originalRpcType = 33; + pSyncMsg->seqNum = 11; + pSyncMsg->isWeak = 1; + strcpy(pSyncMsg->data, "test3"); + + SSyncRaftEntry* pEntry = syncEntryBuild3(pSyncMsg, 100, 200, SYNC_RAFT_ENTRY_NOOP); + syncEntryPrint(pEntry); + + syncClientRequestDestroy(pSyncMsg); + syncEntryDestory(pEntry); +} + +void test4() { SSyncRaftEntry* pEntry = syncEntryBuild(10); assert(pEntry != NULL); pEntry->msgType = 11; @@ -54,7 +68,8 @@ void test3() { pEntry->isWeak = true; pEntry->term = 44; pEntry->index = 55; - strcpy(pEntry->data, "test3"); + pEntry->entryType = SYNC_RAFT_ENTRY_CONFIG; + strcpy(pEntry->data, "test4"); syncEntryPrint(pEntry); uint32_t len; @@ -76,6 +91,7 @@ int main(int argc, char** argv) { test1(); test2(); test3(); + test4(); return 0; } diff --git a/source/libs/sync/test/syncReplicateLoadTest.cpp b/source/libs/sync/test/syncReplicateLoadTest.cpp index d53ceca473fa5cffed99379cf600a7a51cad7bc4..b2d3f1a98d7c06e4fd236afaf1bab1445cdb827a 100644 --- a/source/libs/sync/test/syncReplicateLoadTest.cpp +++ b/source/libs/sync/test/syncReplicateLoadTest.cpp @@ -178,9 +178,10 @@ int main(int argc, char **argv) { while (1) { sTrace( "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d", + "electTimerMS:%d, commitIndex:%ld", pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS); + pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, + pSyncNode->commitIndex); taosMsleep(1000); } diff --git a/source/libs/sync/test/syncReplicateTest.cpp b/source/libs/sync/test/syncReplicateTest.cpp index 47399eeb3c741ddc139e592d74f35483871be14a..09aec6534fe96085a94a895112e2dafc31e72ed5 100644 --- a/source/libs/sync/test/syncReplicateTest.cpp +++ b/source/libs/sync/test/syncReplicateTest.cpp @@ -183,18 +183,20 @@ int main(int argc, char **argv) { taosMsleep(1000); sTrace( - "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d", + "syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " + "electTimerMS:%d, commitIndex:%ld", gSyncNode->state, syncUtilState2String(gSyncNode->state), gSyncNode->pRaftStore->currentTerm, - gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS); + gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS, + gSyncNode->commitIndex); } while (1) { sTrace( "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d", + "electTimerMS:%d, commitIndex:%ld", gSyncNode->state, syncUtilState2String(gSyncNode->state), gSyncNode->pRaftStore->currentTerm, - gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS); + gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS, + gSyncNode->commitIndex); taosMsleep(1000); } diff --git a/source/libs/sync/test/syncReplicateTest2.cpp b/source/libs/sync/test/syncReplicateTest2.cpp index 09dbc0e2ed5d746b6b8a2ef95bb12910c6959840..8739a8d12fdc5d52c9f3512d7ffdb0702753fec2 100644 --- a/source/libs/sync/test/syncReplicateTest2.cpp +++ b/source/libs/sync/test/syncReplicateTest2.cpp @@ -185,17 +185,19 @@ int main(int argc, char **argv) { sTrace( "syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d", + "electTimerMS:%d, commitIndex:%ld", pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS); + pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, + pSyncNode->commitIndex); } while (1) { sTrace( "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d", + "electTimerMS:%d, commitIndex:%ld", pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS); + pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, + pSyncNode->commitIndex); taosMsleep(1000); } diff --git a/source/libs/sync/test/syncReplicateTest3.cpp b/source/libs/sync/test/syncReplicateTest3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad2172ccd2187c30d3a00f6039b7d2b2ad177306 --- /dev/null +++ b/source/libs/sync/test/syncReplicateTest3.cpp @@ -0,0 +1,217 @@ +#define ALLOW_FORBID_FUNC + +#include +#include +#include "syncEnv.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncRaftEntry.h" +#include "syncRaftLog.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; +SWal * pWal; + +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, + ESyncState state) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", + pFsm, index, isWeak, code, state, syncUtilState2String(state)); + syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); +} + +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, + ESyncState state) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, index, isWeak, + code, state, syncUtilState2String(state)); + syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); +} + +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, + ESyncState state) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", + pFsm, index, isWeak, code, state, syncUtilState2String(state)); + syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); +} + +void initFsm() { + pFsm = (SSyncFSM *)malloc(sizeof(SSyncFSM)); + pFsm->FpCommitCb = CommitCb; + pFsm->FpPreCommitCb = PreCommitCb; + pFsm->FpRollBackCb = RollBackCb; +} + +int64_t 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), "./replicate2_test_%d", myIndex); + + int code = walInit(); + assert(code == 0); + SWalCfg walCfg; + memset(&walCfg, 0, sizeof(SWalCfg)); + walCfg.vgId = syncInfo.vgId; + walCfg.fsyncPeriod = 1000; + walCfg.retentionPeriod = 1000; + walCfg.rollPeriod = 1000; + walCfg.retentionSize = 1000; + walCfg.segSize = 1000; + walCfg.level = TAOS_WAL_FSYNC; + + char tmpdir[128]; + snprintf(tmpdir, sizeof(tmpdir), "./replicate2_test_wal_%d", myIndex); + pWal = walOpen(tmpdir, &walCfg); + assert(pWal != NULL); + + syncInfo.pWal = pWal; + + 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); + } + + int64_t rid = syncStart(&syncInfo); + assert(rid > 0); + + SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); + assert(pSyncNode != NULL); + + // pSyncNode->hbBaseLine = 500; + // pSyncNode->electBaseLine = 1500; + + 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->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; + gSyncIO->pSyncNode = pSyncNode; + + syncNodeRelease(pSyncNode); + + return rid; +} + +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); + } +} + +SRpcMsg *step0(int i) { + SRpcMsg *pMsg = (SRpcMsg *)malloc(sizeof(SRpcMsg)); + memset(pMsg, 0, sizeof(SRpcMsg)); + pMsg->msgType = 9999; + pMsg->contLen = 128; + pMsg->pCont = malloc(pMsg->contLen); + snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-%u-%d", ports[myIndex], i); + return pMsg; +} + +SyncClientRequest *step1(const SRpcMsg *pMsg) { + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + return pRetMsg; +} + +int main(int argc, char **argv) { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + void logTest(); + + myIndex = 0; + if (argc >= 2) { + myIndex = atoi(argv[1]); + } + + int recordCount = 100; + if (argc >= 3) { + recordCount = atoi(argv[2]); + } + + int sleepMS = 10; + if (argc >= 4) { + sleepMS = atoi(argv[3]); + } + + int32_t ret = syncIOStart((char *)"127.0.0.1", ports[myIndex]); + assert(ret == 0); + + initFsm(); + + ret = syncInit(); + assert(ret == 0); + + int64_t rid = syncNodeInit(); + assert(rid > 0); + + SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); + assert(pSyncNode != NULL); + + syncNodePrint2((char *)"", pSyncNode); + initRaftId(pSyncNode); + + for (int i = 0; i < recordCount; ++i) { + // step0 + SRpcMsg *pMsg0 = step0(i); + syncRpcMsgPrint2((char *)"==step0==", pMsg0); + + syncPropose(rid, pMsg0, true); + taosMsleep(sleepMS); + + free(pMsg0); + + sTrace( + "syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " + "electTimerMS:%d, commitIndex:%ld", + pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, + pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, + pSyncNode->commitIndex); + } + + while (1) { + sTrace( + "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " + "electTimerMS:%d, commitIndex:%ld", + pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, + pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, + pSyncNode->commitIndex); + taosMsleep(1000); + } + + return 0; +} diff --git a/tests/script/jenkins/basic.txt b/tests/script/jenkins/basic.txt index ee25b202854b0c48ce4aae27729bbe985fcfb491..dd44baed27734b3574091b1f29d7d1c63e68203b 100644 --- a/tests/script/jenkins/basic.txt +++ b/tests/script/jenkins/basic.txt @@ -21,7 +21,7 @@ # ---- query ./test.sh -f tsim/query/interval.sim -#./test.sh -f tsim/query/interval-offset.sim +./test.sh -f tsim/query/interval-offset.sim # ---- show ./test.sh -f tsim/show/basic.sim diff --git a/tests/script/tsim/insert/basic0.sim b/tests/script/tsim/insert/basic0.sim index 46aa99127bc5ef2be8d3ce9c12a01578f054456f..da74eb95e81f95ea0dd7fcffab3706632a37ff88 100644 --- a/tests/script/tsim/insert/basic0.sim +++ b/tests/script/tsim/insert/basic0.sim @@ -109,7 +109,26 @@ endi #print =============== select first(*)/first(column) from child table #sql select first(*) from ct1 -#sql select first(ts), first(c1), first(c2), first(c3) from ct1 +#print ====> select first(*) from ct1 +#print rows: $rows +#print $data00 $data01 $data02 $data03 + +sql select first(ts), first(c1), first(c2), first(c3) from ct1 +print ====> select first(ts), first(c1), first(c2), first(c3) from ct1 +print rows: $rows +print $data00 $data01 $data02 $data03 +if $rows != 1 then + return -1 +endi +if $data01 != 10 then + return -1 +endi +if $data02 != 2.00000 then + return -1 +endi +if $data03 != 3.000000000 then + return -1 +endi print =============== select min(column) from child table sql select min(c1), min(c2), min(c3) from ct1 @@ -216,10 +235,42 @@ endi # return -1 #endi +print =============== select count(column) from supter table +sql select ts, c1, c2, c3 from stb +print rows: $rows +print $data00 $data01 $data02 $data03 +print $data10 $data11 $data12 $data13 +print $data20 $data21 $data22 $data23 +print $data30 $data31 $data32 $data33 +print $data40 $data41 $data42 $data43 +print $data50 $data51 $data52 $data53 +print $data60 $data61 $data62 $data63 +print $data70 $data71 $data72 $data73 +print $data80 $data81 $data82 $data83 +if $rows != 9 then + return -1 +endi + +# The order of data from different sub tables in the super table is random, +# so this detection may fail randomly +if $data01 != 10 then + return -1 +endi +if $data02 != 2.00000 then + return -1 +endi +if $data03 != 3.000000000 then + return -1 +endi + #print =============== select count(column) from supter table #sql select count(ts), count(c1), count(c2), count(c3) from stb +#print rows: $rows #print $data00 $data01 $data02 $data03 -#if $data00 != 8 then +#print $data10 $data11 $data12 $data13 +#print $data20 $data21 $data22 $data23 +#print $data30 $data31 $data32 $data33 +#if $data00 != 9 then # return -1 #endi #if $data01 != 8 then @@ -232,7 +283,6 @@ endi # return -1 #endi - #=================================================================== #=================================================================== @@ -317,7 +367,26 @@ endi #print =============== select first(*)/first(column) from child table #sql select first(*) from ct1 -#sql select first(ts), first(c1), first(c2), first(c3) from ct1 +#print ====> select first(*) from ct1 +#print rows: $rows +#print $data00 $data01 $data02 $data03 + +sql select first(ts), first(c1), first(c2), first(c3) from ct1 +print ====> select first(ts), first(c1), first(c2), first(c3) from ct1 +print rows: $rows +print $data00 $data01 $data02 $data03 +if $rows != 1 then + return -1 +endi +if $data01 != 10 then + return -1 +endi +if $data02 != 2.00000 then + return -1 +endi +if $data03 != 3.000000000 then + return -1 +endi print =============== select min(column) from child table sql select min(c1), min(c2), min(c3) from ct1 @@ -424,6 +493,33 @@ endi # return -1 #endi +print =============== select count(column) from supter table +sql select ts, c1, c2, c3 from stb +print rows: $rows +print $data00 $data01 $data02 $data03 +print $data10 $data11 $data12 $data13 +print $data20 $data21 $data22 $data23 +print $data30 $data31 $data32 $data33 +print $data40 $data41 $data42 $data43 +print $data50 $data51 $data52 $data53 +print $data60 $data61 $data62 $data63 +print $data70 $data71 $data72 $data73 +print $data80 $data81 $data82 $data83 +if $rows != 9 then + return -1 +endi +# The order of data from different sub tables in the super table is random, +# so this detection may fail randomly +if $data01 != 10 then + return -1 +endi +if $data02 != 2.00000 then + return -1 +endi +if $data03 != 3.000000000 then + return -1 +endi + #print =============== select count(column) from supter table #sql select count(ts), count(c1), count(c2), count(c3) from stb #print $data00 $data01 $data02 $data03 diff --git a/tests/script/tsim/query/interval-offset.sim b/tests/script/tsim/query/interval-offset.sim index b2415be7d1222fa91e89dc4ff782d425bad9ca17..f188dff1ba91cb0311a58f992d00f3a4ef4430ae 100644 --- a/tests/script/tsim/query/interval-offset.sim +++ b/tests/script/tsim/query/interval-offset.sim @@ -88,14 +88,17 @@ print ===> rows4: $data40 $data41 $data42 $data43 $data44 print ===> rows5: $data50 $data51 $data52 $data53 $data54 print ===> rows6: $data60 $data61 $data62 $data63 $data64 print ===> rows7: $data70 $data71 $data72 $data73 $data74 -if $rows != 8 then - print expect 8, actual $rows +print ===> rows8: $data80 $data81 $data82 $data83 $data84 +if $rows != 9 then return -1 endi -if $data00 != 2 then +if $data00 != 1 then + return -1 +endi +if $data70 != 2 then return -1 endi -if $data70 != 1 then +if $data80 != 1 then return -1 endi @@ -166,15 +169,15 @@ print ===> rows7: $data70 $data71 $data72 $data73 $data74 $data75 #endi print =============== insert data into child table ct3 (n) -sql insert into ct3 values ( '2021-12-21 01:01:01.000', NULL ) -sql insert into ct3 values ( '2021-12-31 01:01:01.000', 1 ) -sql insert into ct3 values ( '2022-01-01 01:01:06.000', 2 ) -sql insert into ct3 values ( '2022-01-07 01:01:10.000', 3 ) -sql insert into ct3 values ( '2022-01-31 01:01:16.000', 4 ) -sql insert into ct3 values ( '2022-02-01 01:01:20.000', 5 ) -sql insert into ct3 values ( '2022-02-28 01:01:26.000', 6 ) -sql insert into ct3 values ( '2022-03-01 01:01:30.000', 7 ) -sql insert into ct3 values ( '2022-03-08 01:01:36.000', 8 ) +sql insert into ct3 values ( '2021-12-21 01:01:01.000', NULL ); +sql insert into ct3 values ( '2021-12-31 01:01:01.000', 1 ); +sql insert into ct3 values ( '2022-01-01 01:01:06.000', 2 ); +sql insert into ct3 values ( '2022-01-07 01:01:10.000', 3 ); +sql insert into ct3 values ( '2022-01-31 01:01:16.000', 4 ); +sql insert into ct3 values ( '2022-02-01 01:01:20.000', 5 ); +sql insert into ct3 values ( '2022-02-28 01:01:26.000', 6 ); +sql insert into ct3 values ( '2022-03-01 01:01:30.000', 7 ); +sql insert into ct3 values ( '2022-03-08 01:01:36.000', 8 ); sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct3 interval(1n, 1w) print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct3 interval(1n, 1w) diff --git a/tools/shell/src/shellMain.c b/tools/shell/src/shellMain.c index ea91736aadc26d25a8fca0f135ffe9a17957df31..cc87c2c54b26a1ab8a40e21d54dc06d0f3ad2813 100644 --- a/tools/shell/src/shellMain.c +++ b/tools/shell/src/shellMain.c @@ -17,6 +17,7 @@ #include "os.h" #include "shell.h" #include "tglobal.h" +#include "tconfig.h" #include "shellCommand.h" #include "tbase64.h" #include "tlog.h" @@ -619,20 +620,17 @@ int main(int argc, char *argv[]) { shellParseArgument(argc, argv, &args); -#if 0 if (args.dump_config) { - taosInitGlobalCfg(); - taosReadGlobalLogCfg(); + taosInitCfg(configDir, NULL, NULL, NULL, 1); - if (taosReadGlobalCfg() ! =0) { - printf("TDengine read global config failed"); + SConfig *pCfg = taosGetCfg(); + if (NULL == pCfg) { + printf("TDengine read global config failed!\n"); exit(EXIT_FAILURE); } - - taosDumpGlobalCfg(); + cfgDumpCfg(pCfg, 0, 1); exit(0); } -#endif if (args.netTestRole && args.netTestRole[0] != 0) { TAOS *con = NULL;