diff --git a/Jenkinsfile2 b/Jenkinsfile2 index a3006c4f7d0dbbb1282ab6eaba4c66862f7723b0..cbf663cdcfeb1ed1f31adbf152dc2e3ff4fc8b66 100644 --- a/Jenkinsfile2 +++ b/Jenkinsfile2 @@ -33,7 +33,18 @@ def abort_previous(){ milestone(buildNumber) } def pre_test(){ - sh 'hostname' + sh ''' + hostname + date + ''' + sh ''' + cd ${WK} + git reset --hard + git fetch || git fetch + cd ${WKC} + git reset --hard + git fetch || git fetch + ''' script { if (env.CHANGE_TARGET == 'master') { sh ''' @@ -222,7 +233,7 @@ pipeline { changeRequest() } steps { - timeout(time: 20, unit: 'MINUTES'){ + timeout(time: 40, unit: 'MINUTES'){ pre_test() script { sh ''' @@ -234,8 +245,9 @@ pipeline { ''' sh ''' cd ${WKC}/tests/parallel_test + export DEFAULT_RETRY_TIME=2 date - time ./run.sh -e -m /home/m.json -t /tmp/cases.task -b ${CHANGE_TARGET} -l ${WKDIR}/log + timeout 2100 time ./run.sh -e -m /home/m.json -t /tmp/cases.task -b ${BRANCH_NAME} -l ${WKDIR}/log -o 480 ''' } } diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index 926fbc8957590511b476ed163ac3df3430cceb6d..14a85ee4f6f028b3af52f8e78c0029fc34a1223a 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -365,7 +365,7 @@ if(${BUILD_ADDR2LINE}) if(HAVE_LIBELF_H OR HAVE_LIBELF_LIBELF_H) target_link_libraries(libdwarf PUBLIC libelf) endif() - target_include_directories(libdwarf SYSTEM PUBLIC "libdwarf/src/lib/libdwarf" ${CMAKE_BINARY_DIR}/contrib) + target_include_directories(libdwarf SYSTEM PUBLIC "libdwarf/src/lib/libdwarf" ${CMAKE_CURRENT_BINARY_DIR}) file(READ "addr2line/addr2line.c" ADDR2LINE_CONTENT) string(REPLACE "static int" "int" ADDR2LINE_CONTENT "${ADDR2LINE_CONTENT}") string(REPLACE "static void" "void" ADDR2LINE_CONTENT "${ADDR2LINE_CONTENT}") diff --git a/include/common/tcommon.h b/include/common/tcommon.h index 80125d67880043759e86fddb70483480edd7cecf..9e3ad42a82fe779bc507417d84718b342a98a34e 100644 --- a/include/common/tcommon.h +++ b/include/common/tcommon.h @@ -201,18 +201,17 @@ typedef struct SExprInfo { } SExprInfo; typedef struct { - const char* key; - int32_t keyLen; - uint8_t type; - int16_t length; + const char* key; + int32_t keyLen; + uint8_t type; union{ const char* value; - int64_t i; - uint64_t u; - double d; - float f; + int64_t i; + uint64_t u; + double d; + float f; }; - int32_t valueLen; + int32_t length; } SSmlKv; #define QUERY_ASC_FORWARD_STEP 1 diff --git a/include/common/tglobal.h b/include/common/tglobal.h index 84aae46347292fa5b4ea66cf0c435e99494beba2..fd8cea449f0a8cd1b34ae91ed8072cc2b2f4ac1a 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -34,7 +34,6 @@ extern int32_t tsVersion; extern int32_t tsStatusInterval; // common -extern int32_t tsMaxConnections; extern int32_t tsMaxShellConns; extern int32_t tsShellActivityTimer; extern int32_t tsCompressMsgSize; diff --git a/include/common/tmsgcb.h b/include/common/tmsgcb.h index b6c96bb2d16e7b3b4bd644aa30a6e825f8c98250..a484c2acc98c969aed77f3cfd9151dd93148d4ea 100644 --- a/include/common/tmsgcb.h +++ b/include/common/tmsgcb.h @@ -37,11 +37,12 @@ typedef enum { QUEUE_MAX, } EQueueType; -typedef int32_t (*PutToQueueFp)(SMgmtWrapper* pWrapper, SRpcMsg* pReq); -typedef int32_t (*GetQueueSizeFp)(SMgmtWrapper* pWrapper, int32_t vgId, EQueueType qtype); +typedef int32_t (*PutToQueueFp)(void *pMgmt, SRpcMsg* pReq); +typedef int32_t (*GetQueueSizeFp)(void *pMgmt, int32_t vgId, EQueueType qtype); typedef int32_t (*SendReqFp)(SMgmtWrapper* pWrapper, const SEpSet* epSet, SRpcMsg* pReq); typedef int32_t (*SendMnodeReqFp)(SMgmtWrapper* pWrapper, SRpcMsg* pReq); typedef void (*SendRspFp)(SMgmtWrapper* pWrapper, const SRpcMsg* pRsp); +typedef void (*SendMnodeRecvFp)(SMgmtWrapper* pWrapper, SRpcMsg* pReq, SRpcMsg* pRsp); typedef void (*SendRedirectRspFp)(SMgmtWrapper* pWrapper, const SRpcMsg* pRsp, const SEpSet* pNewEpSet); typedef void (*RegisterBrokenLinkArgFp)(SMgmtWrapper* pWrapper, SRpcMsg* pMsg); typedef void (*ReleaseHandleFp)(SMgmtWrapper* pWrapper, void* handle, int8_t type); @@ -49,23 +50,26 @@ typedef void (*ReportStartup)(SMgmtWrapper* pWrapper, const char* name, const ch typedef struct { SMgmtWrapper* pWrapper; + void* pMgmt; + void* clientRpc; PutToQueueFp queueFps[QUEUE_MAX]; GetQueueSizeFp qsizeFp; SendReqFp sendReqFp; SendRspFp sendRspFp; + SendMnodeRecvFp sendMnodeRecvFp; SendRedirectRspFp sendRedirectRspFp; RegisterBrokenLinkArgFp registerBrokenLinkArgFp; ReleaseHandleFp releaseHandleFp; ReportStartup reportStartupFp; - void* clientRpc; } SMsgCb; void tmsgSetDefaultMsgCb(const SMsgCb* pMsgCb); int32_t tmsgPutToQueue(const SMsgCb* pMsgCb, EQueueType qtype, SRpcMsg* pReq); int32_t tmsgGetQueueSize(const SMsgCb* pMsgCb, int32_t vgId, EQueueType qtype); int32_t tmsgSendReq(const SMsgCb* pMsgCb, const SEpSet* epSet, SRpcMsg* pReq); -void tmsgSendRsp(const SRpcMsg* pRsp); -void tmsgSendRedirectRsp(const SRpcMsg* pRsp, const SEpSet* pNewEpSet); +void tmsgSendRsp(SRpcMsg* pRsp); +void tmsgSendMnodeRecv(SRpcMsg* pReq, SRpcMsg* pRsp); +void tmsgSendRedirectRsp(SRpcMsg* pRsp, const SEpSet* pNewEpSet); void tmsgRegisterBrokenLinkArg(const SMsgCb* pMsgCb, SRpcMsg* pMsg); void tmsgReleaseHandle(void* handle, int8_t type); void tmsgReportStartup(const char* name, const char* desc); diff --git a/include/util/tdef.h b/include/util/tdef.h index 022fd8ba8e517da21a93eccd6b0e1b0e0c617982..f95d96be56d40a24cb227820058807eac9e7f051 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -425,9 +425,12 @@ enum { SND_WORKER_TYPE__UNIQUE, }; -#define MNODE_HANDLE -1 -#define QNODE_HANDLE 1 #define DEFAULT_HANDLE 0 +#define MNODE_HANDLE -1 +#define QNODE_HANDLE -2 +#define SNODE_HANDLE -3 +#define VNODE_HANDLE -4 +#define BNODE_HANDLE -5 #define TSDB_CONFIG_OPTION_LEN 16 #define TSDB_CONIIG_VALUE_LEN 48 diff --git a/include/util/tjson.h b/include/util/tjson.h index d23f7b402ea142ea9118f57f6274983d78c5a72a..a95efe56e7d777be60f51f4424c3619967c14c5a 100644 --- a/include/util/tjson.h +++ b/include/util/tjson.h @@ -22,17 +22,12 @@ extern "C" { #endif -#ifdef WINDOWS -#define tjsonGetNumberValue(pJson, pName, val) -1 -#else -#define tjsonGetNumberValue(pJson, pName, val) \ - ({ \ - uint64_t _tmp = 0; \ - int32_t _code = tjsonGetUBigIntValue(pJson, pName, &_tmp); \ - val = _tmp; \ - _code; \ - }) -#endif +#define tjsonGetNumberValue(pJson, pName, val, code) \ + do { \ + uint64_t _tmp = 0; \ + code = tjsonGetUBigIntValue(pJson, pName, &_tmp); \ + val = _tmp; \ + } while (0) typedef void SJson; diff --git a/source/client/inc/clientStmt.h b/source/client/inc/clientStmt.h index ae27e611cb4fada8b6b3c8255f6595f80e50f5ce..32625b4c51f49398c67f6c3ab2824cf286e0ce16 100644 --- a/source/client/inc/clientStmt.h +++ b/source/client/inc/clientStmt.h @@ -63,7 +63,7 @@ typedef struct SStmtBindInfo { int8_t tbType; bool tagsCached; void* boundTags; - char tbName[TSDB_TABLE_FNAME_LEN];; + char tbName[TSDB_TABLE_FNAME_LEN]; char tbFName[TSDB_TABLE_FNAME_LEN]; char stbFName[TSDB_TABLE_FNAME_LEN]; SName sname; diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 819c50275b737238f11b74b19c3fb441a5d38aaf..c82cce9af3916aa13439949df2c62831ad16fc59 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -91,7 +91,7 @@ void *openTransporter(const char *user, const char *auth, int32_t numOfThread) { rpcInit.label = "TSC"; rpcInit.numOfThreads = numOfThread; rpcInit.cfp = processMsgFromServer; - rpcInit.sessions = tsMaxConnections; + rpcInit.sessions = 1024; rpcInit.connType = TAOS_CONN_CLIENT; rpcInit.user = (char *)user; rpcInit.idleTime = tsShellActivityTimer * 1000; diff --git a/source/client/src/clientSml.c b/source/client/src/clientSml.c index 3e71714f21cdad052a1608b19f91f002ba02189c..605d71ffedddbda4b8dcb66f9495df281fc6dd92 100644 --- a/source/client/src/clientSml.c +++ b/source/client/src/clientSml.c @@ -54,29 +54,29 @@ typedef enum { } ESchemaAction; typedef struct { - char sTableName[TSDB_TABLE_NAME_LEN]; - SArray *tags; - SArray *fields; + char sTableName[TSDB_TABLE_NAME_LEN]; + SArray *tags; + SArray *fields; } SCreateSTableActionInfo; typedef struct { - char sTableName[TSDB_TABLE_NAME_LEN]; - SSmlKv * field; + char sTableName[TSDB_TABLE_NAME_LEN]; + SSmlKv *field; } SAlterSTableActionInfo; typedef struct { - ESchemaAction action; + ESchemaAction action; union { SCreateSTableActionInfo createSTable; - SAlterSTableActionInfo alterSTable; + SAlterSTableActionInfo alterSTable; }; } SSchemaAction; typedef struct { - const char* measure; - const char* tags; - const char* cols; - const char* timestamp; + const char *measure; + const char *tags; + const char *cols; + const char *timestamp; int32_t measureLen; int32_t measureTagsLen; @@ -103,7 +103,7 @@ typedef struct { SHashObj *tagHash; // elements are SArray *cols; - SHashObj *fieldHash; + SHashObj *colHash; STableMeta *tableMeta; } SSmlSTableMeta; @@ -180,6 +180,7 @@ static inline bool smlCheckDuplicateKey(const char *key, int32_t keyLen, SHashOb } static int32_t smlBuildInvalidDataMsg(SSmlMsgBuf* pBuf, const char *msg1, const char *msg2) { + memset(pBuf->buf, 0 , pBuf->len); if(msg1) strncat(pBuf->buf, msg1, pBuf->len); int32_t left = pBuf->len - strlen(pBuf->buf); if(left > 2 && msg2) { @@ -189,47 +190,39 @@ static int32_t smlBuildInvalidDataMsg(SSmlMsgBuf* pBuf, const char *msg1, const return TSDB_CODE_SML_INVALID_DATA; } -static int32_t smlGenerateSchemaAction(SSchema* pointColField, SHashObj* dbAttrHash, SArray* dbAttrArray, bool isTag, char sTableName[], +static int32_t smlGenerateSchemaAction(SSchema* colField, SHashObj* colHash, SSmlKv* kv, bool isTag, SSchemaAction* action, bool* actionNeeded, SSmlHandle* info) { -// char fieldName[TSDB_COL_NAME_LEN] = {0}; -// strcpy(fieldName, pointColField->name); -// -// size_t* pDbIndex = taosHashGet(dbAttrHash, fieldName, strlen(fieldName)); -// if (pDbIndex) { -// SSchema* dbAttr = taosArrayGet(dbAttrArray, *pDbIndex); -// assert(strcasecmp(dbAttr->name, pointColField->name) == 0); -// if (pointColField->type != dbAttr->type) { -// uError("SML:0x%"PRIx64" point type and db type mismatch. key: %s. point type: %d, db type: %d", info->id, pointColField->name, -// pointColField->type, dbAttr->type); -// return TSDB_CODE_TSC_INVALID_VALUE; -// } -// -// if (IS_VAR_DATA_TYPE(pointColField->type) && (pointColField->bytes > dbAttr->bytes)) { -// if (isTag) { -// action->action = SCHEMA_ACTION_CHANGE_TAG_SIZE; -// } else { -// action->action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE; -// } -// memset(&action->alterSTable, 0, sizeof(SAlterSTableActionInfo)); -// memcpy(action->alterSTable.sTableName, sTableName, TSDB_TABLE_NAME_LEN); -// action->alterSTable.field = pointColField; -// *actionNeeded = true; -// } -// } else { -// if (isTag) { -// action->action = SCHEMA_ACTION_ADD_TAG; -// } else { -// action->action = SCHEMA_ACTION_ADD_COLUMN; -// } -// memset(&action->alterSTable, 0, sizeof(SAlterSTableActionInfo)); -// memcpy(action->alterSTable.sTableName, sTableName, TSDB_TABLE_NAME_LEN); -// action->alterSTable.field = pointColField; -// *actionNeeded = true; -// } -// if (*actionNeeded) { -// uDebug("SML:0x%" PRIx64 " generate schema action. column name: %s, action: %d", info->id, fieldName, -// action->action); -// } + uint16_t *index = (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen); + if (index) { + if (colField[*index].type != kv->type) { + uError("SML:0x%"PRIx64" point type and db type mismatch. key: %s. point type: %d, db type: %d", info->id, kv->key, + colField[*index].type, kv->type); + return TSDB_CODE_TSC_INVALID_VALUE; + } + + if ((colField[*index].type == TSDB_DATA_TYPE_VARCHAR && (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) || + (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) { + if (isTag) { + action->action = SCHEMA_ACTION_CHANGE_TAG_SIZE; + } else { + action->action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE; + } + action->alterSTable.field = kv; + *actionNeeded = true; + } + } else { + if (isTag) { + action->action = SCHEMA_ACTION_ADD_TAG; + } else { + action->action = SCHEMA_ACTION_ADD_COLUMN; + } + action->alterSTable.field = kv; + *actionNeeded = true; + } + if (*actionNeeded) { + uDebug("SML:0x%" PRIx64 " generate schema action. column name: %s, action: %d", info->id, colField->name, + action->action); + } return 0; } @@ -238,9 +231,9 @@ static int32_t smlBuildColumnDescription(SSmlKv* field, char* buf, int32_t bufSi char tname[TSDB_TABLE_NAME_LEN] = {0}; memcpy(tname, field->key, field->keyLen); if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { - int32_t bytes = field->valueLen; // todo + int32_t bytes = field->length; // todo int out = snprintf(buf, bufSize,"`%s` %s(%d)", - tname,tDataTypes[field->type].name, bytes); + tname, tDataTypes[field->type].name, bytes); *outBytes = out; } else { int out = snprintf(buf, bufSize, "`%s` %s", tname, tDataTypes[type].name); @@ -259,7 +252,7 @@ static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) { uDebug("SML:0x%"PRIx64" apply schema action. action: %d", info->id, action->action); switch (action->action) { case SCHEMA_ACTION_ADD_COLUMN: { - int n = sprintf(result, "alter stable %s add column ", action->alterSTable.sTableName); + int n = sprintf(result, "alter stable `%s` add column ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes); TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery code = taos_errno(res); @@ -282,7 +275,7 @@ static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) { break; } case SCHEMA_ACTION_ADD_TAG: { - int n = sprintf(result, "alter stable %s add tag ", action->alterSTable.sTableName); + int n = sprintf(result, "alter stable `%s` add tag ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes); TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery @@ -306,7 +299,7 @@ static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) { break; } case SCHEMA_ACTION_CHANGE_COLUMN_SIZE: { - int n = sprintf(result, "alter stable %s modify column ", action->alterSTable.sTableName); + int n = sprintf(result, "alter stable `%s` modify column ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes); TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery @@ -329,7 +322,7 @@ static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) { break; } case SCHEMA_ACTION_CHANGE_TAG_SIZE: { - int n = sprintf(result, "alter stable %s modify tag ", action->alterSTable.sTableName); + int n = sprintf(result, "alter stable `%s` modify tag ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result+n, capacity-n, &outBytes); TAOS_RES* res = taos_query(info->taos, result); //TODO async doAsyncQuery @@ -408,6 +401,25 @@ static int32_t smlApplySchemaAction(SSmlHandle* info, SSchemaAction* action) { return code; } +static int32_t smlProcessSchemaAction(SSmlHandle* info, SSchema* schemaField, SHashObj* schemaHash, SArray *cols, SSchemaAction* action, bool isTag){ + int32_t code = TSDB_CODE_SUCCESS; + for (int j = 0; j < taosArrayGetSize(cols); ++j) { + SSmlKv* kv = (SSmlKv*)taosArrayGetP(cols, j); + bool actionNeeded = false; + code = smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, &actionNeeded, info); + if(code != TSDB_CODE_SUCCESS){ + return code; + } + if (actionNeeded) { + code = smlApplySchemaAction(info, action); + if (code != TSDB_CODE_SUCCESS) { + return code; + } + } + } + return TSDB_CODE_SUCCESS; +} + static int32_t smlModifyDBSchemas(SSmlHandle* info) { int32_t code = 0; @@ -427,27 +439,50 @@ static int32_t smlModifyDBSchemas(SSmlHandle* info) { code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta); if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_INVALID_STB) { - SSchemaAction schemaAction = { SCHEMA_ACTION_CREATE_STABLE, 0}; + SSchemaAction schemaAction = { .action = SCHEMA_ACTION_CREATE_STABLE, .createSTable = {0}}; memcpy(schemaAction.createSTable.sTableName, superTable, superTableLen); schemaAction.createSTable.tags = sTableData->tags; schemaAction.createSTable.fields = sTableData->cols; code = smlApplySchemaAction(info, &schemaAction); - if (code != 0) { + if (code != TSDB_CODE_SUCCESS) { uError("SML:0x%"PRIx64" smlApplySchemaAction failed. can not create %s", info->id, schemaAction.createSTable.sTableName); return code; } + info->cost.numOfCreateSTables++; + }else if (code == TSDB_CODE_SUCCESS) { + SHashObj *hashTmp = taosHashInit(pTableMeta->tableInfo.numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + for(uint16_t i = pTableMeta->tableInfo.numOfColumns; i < pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; i++){ + taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES); + } - code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta); - if (code != 0) { - uError("SML:0x%"PRIx64" catalogGetSTableMeta failed. super table name %s", info->id, schemaAction.createSTable.sTableName); + SSchemaAction schemaAction = {.alterSTable = {0}}; + memcpy(schemaAction.createSTable.sTableName, superTable, superTableLen); + code = smlProcessSchemaAction(info, pTableMeta->schema, hashTmp, sTableData->tags, &schemaAction, true); + if (code != TSDB_CODE_SUCCESS) { + taosHashCleanup(hashTmp); + return code; + } + + taosHashClear(hashTmp); + for(uint16_t i = 0; i < pTableMeta->tableInfo.numOfColumns; i++){ + taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES); + } + code = smlProcessSchemaAction(info, pTableMeta->schema, hashTmp, sTableData->cols, &schemaAction, false); + taosHashCleanup(hashTmp); + if (code != TSDB_CODE_SUCCESS) { return code; } - info->cost.numOfCreateSTables++; - }else if (code == TSDB_CODE_SUCCESS) { } else { uError("SML:0x%"PRIx64" load table meta error: %s", info->id, tstrerror(code)); return code; } + if(pTableMeta) taosMemoryFree(pTableMeta); + + code = catalogGetSTableMeta(info->pCatalog, info->taos->pAppInfo->pTransporter, &ep, &pName, &pTableMeta); + if (code != TSDB_CODE_SUCCESS) { + uError("SML:0x%"PRIx64" catalogGetSTableMeta failed. super table name %s", info->id, (char*)superTable); + return code; + } sTableData->tableMeta = pTableMeta; tableMetaSml = (SSmlSTableMeta**)taosHashIterate(info->superTables, tableMetaSml); @@ -507,7 +542,7 @@ static int32_t smlModifyDBSchemas(SSmlHandle* info) { static bool smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg){ const char *pVal = kvVal->value; - int32_t len = kvVal->valueLen; + int32_t len = kvVal->length; char *endptr = NULL; double result = strtod(pVal, &endptr); if(pVal == endptr){ @@ -591,7 +626,7 @@ static bool smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg){ static bool smlParseBool(SSmlKv *kvVal) { const char *pVal = kvVal->value; - int32_t len = kvVal->valueLen; + int32_t len = kvVal->length; if ((len == 1) && pVal[0] == 't') { kvVal->i = true; return true; @@ -690,7 +725,7 @@ static int8_t smlGetTsTypeByLen(int32_t len) { if (len == TSDB_TIME_PRECISION_SEC_DIGITS) { return TSDB_TIME_PRECISION_SECONDS; } else if (len == TSDB_TIME_PRECISION_MILLI_DIGITS) { - return TSDB_TIME_PRECISION_MILLI_DIGITS; + return TSDB_TIME_PRECISION_MILLI; } else { return -1; } @@ -756,9 +791,12 @@ static int32_t smlParseTS(SSmlHandle* info, const char* data, int32_t len, SArra int64_t ts = 0; if(info->protocol == TSDB_SML_LINE_PROTOCOL){ ts = smlParseInfluxTime(info, data, len); - }else{ + }else if(info->protocol == TSDB_SML_TELNET_PROTOCOL){ ts = smlParseOpenTsdbTime(info, data, len); + }else{ + ASSERT(0); } + if(ts == -1) return TSDB_CODE_TSC_INVALID_TIME_STAMP; // add ts to @@ -778,18 +816,16 @@ static int32_t smlParseTS(SSmlHandle* info, const char* data, int32_t len, SArra static bool smlParseValue(SSmlKv *pVal, SSmlMsgBuf *msg) { //binary - if (smlIsBinary(pVal->value, pVal->valueLen)) { + if (smlIsBinary(pVal->value, pVal->length)) { pVal->type = TSDB_DATA_TYPE_BINARY; - pVal->valueLen -= BINARY_ADD_LEN; - pVal->length = pVal->valueLen; + pVal->length -= BINARY_ADD_LEN; pVal->value += (BINARY_ADD_LEN - 1); return true; } //nchar - if (smlIsNchar(pVal->value, pVal->valueLen)) { + if (smlIsNchar(pVal->value, pVal->length)) { pVal->type = TSDB_DATA_TYPE_NCHAR; - pVal->valueLen -= NCHAR_ADD_LEN; - pVal->length = pVal->valueLen; + pVal->length -= NCHAR_ADD_LEN; pVal->value += (NCHAR_ADD_LEN - 1); return true; } @@ -953,8 +989,8 @@ static int32_t smlParseTelnetTags(const char* data, int32_t len, SArray *cols, S kv->key = key; kv->keyLen = keyLen; kv->value = value; - kv->valueLen = valueLen; - kv->type = TSDB_DATA_TYPE_NCHAR; + kv->length = valueLen; + kv->type = TSDB_DATA_TYPE_NCHAR; //todo if(cols) taosArrayPush(cols, &kv); } @@ -1002,7 +1038,7 @@ static int32_t smlParseTelnetString(SSmlHandle *info, const char* sql, SSmlTable kv->key = VALUE; kv->keyLen = VALUE_LEN; kv->value = value; - kv->valueLen = valueLen; + kv->length = valueLen; if(!smlParseValue(kv, &info->msgBuf) || kv->type == TSDB_DATA_TYPE_BINARY || kv->type == TSDB_DATA_TYPE_NCHAR || kv->type == TSDB_DATA_TYPE_BOOL){ return TSDB_CODE_SML_INVALID_DATA; @@ -1028,7 +1064,7 @@ static int32_t smlParseCols(const char* data, int32_t len, SArray *cols, bool is kv->key = TAG; kv->keyLen = TAG_LEN; kv->value = TAG; - kv->valueLen = TAG_LEN; + kv->length = TAG_LEN; kv->type = TSDB_DATA_TYPE_NCHAR; if(cols) taosArrayPush(cols, &kv); return TSDB_CODE_SUCCESS; @@ -1086,7 +1122,7 @@ static int32_t smlParseCols(const char* data, int32_t len, SArray *cols, bool is kv->key = key; kv->keyLen = keyLen; kv->value = value; - kv->valueLen = valueLen; + kv->length = valueLen; if(isTag){ kv->type = TSDB_DATA_TYPE_NCHAR; }else{ @@ -1142,73 +1178,40 @@ static int32_t smlParseCols(const char* data, int32_t len, SArray *cols, bool is // return TSDB_CODE_SUCCESS; //} -static bool smlUpdateMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols, SSmlMsgBuf *msg){ - if(tags){ - for (int i = 0; i < taosArrayGetSize(tags); ++i) { - SSmlKv *kv = (SSmlKv *)taosArrayGetP(tags, i); - ASSERT(kv->type == TSDB_DATA_TYPE_NCHAR); - - uint8_t *index = (uint8_t *)taosHashGet(tableMeta->tagHash, kv->key, kv->keyLen); - if(index){ - SSmlKv **value = (SSmlKv **)taosArrayGet(tableMeta->tags, *index); - ASSERT((*value)->type == TSDB_DATA_TYPE_NCHAR); - if(kv->valueLen > (*value)->valueLen){ // tags type is nchar - *value = kv; - } - }else{ - size_t tmp = taosArrayGetSize(tableMeta->tags); - ASSERT(tmp <= UINT8_MAX); - uint8_t size = tmp; - taosArrayPush(tableMeta->tags, &kv); - taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &size, CHAR_BYTES); - } - } - } - - if(cols){ - for (int i = 1; i < taosArrayGetSize(cols); ++i) { //jump timestamp - SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i); +static bool smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SSmlMsgBuf *msg){ + for (int i = 0; i < taosArrayGetSize(cols); ++i) { //jump timestamp + SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i); - int16_t *index = (int16_t *)taosHashGet(tableMeta->fieldHash, kv->key, kv->keyLen); - if(index){ - SSmlKv **value = (SSmlKv **)taosArrayGet(tableMeta->cols, *index); - if(kv->type != (*value)->type){ - smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key); - return false; - }else{ - if(IS_VAR_DATA_TYPE(kv->type)){ // update string len, if bigger - if(kv->valueLen > (*value)->valueLen){ - *value = kv; - } + int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen); + if(index){ + SSmlKv **value = (SSmlKv **)taosArrayGet(metaArray, *index); + if(kv->type != (*value)->type){ + smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key); + return false; + }else{ + if(IS_VAR_DATA_TYPE(kv->type)){ // update string len, if bigger + if(kv->length > (*value)->length){ + *value = kv; } } - }else{ - size_t tmp = taosArrayGetSize(tableMeta->cols); - ASSERT(tmp <= INT16_MAX); - int16_t size = tmp; - taosArrayPush(tableMeta->cols, &kv); - taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &size, SHORT_BYTES); } + }else{ + size_t tmp = taosArrayGetSize(metaArray); + ASSERT(tmp <= INT16_MAX); + int16_t size = tmp; + taosArrayPush(metaArray, &kv); + taosHashPut(metaHash, kv->key, kv->keyLen, &size, SHORT_BYTES); } } + return true; } -static void smlInsertMeta(SSmlSTableMeta* tableMeta, SArray *tags, SArray *cols){ - if(tags){ - for (uint8_t i = 0; i < taosArrayGetSize(tags); ++i) { - SSmlKv *kv = (SSmlKv *)taosArrayGetP(tags, i); - taosArrayPush(tableMeta->tags, &kv); - taosHashPut(tableMeta->tagHash, kv->key, kv->keyLen, &i, CHAR_BYTES); - } - } - - if(cols){ - for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) { - SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i); - taosArrayPush(tableMeta->cols, &kv); - taosHashPut(tableMeta->fieldHash, kv->key, kv->keyLen, &i, SHORT_BYTES); - } +static void smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols){ + for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) { + SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, i); + taosArrayPush(metaArray, &kv); + taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES); } } @@ -1236,12 +1239,16 @@ cleanup: return NULL; } -static void smlDestroyTableInfo(SSmlTableInfo *tag, bool format){ - if(format){ +static void smlDestroyTableInfo(SSmlHandle* info, SSmlTableInfo *tag){ + if(info->dataFormat){ for(size_t i = 0; i < taosArrayGetSize(tag->cols); i++){ SArray *kvArray = (SArray *)taosArrayGetP(tag->cols, i); for (int j = 0; j < taosArrayGetSize(kvArray); ++j) { - void *p = taosArrayGetP(kvArray, j); + SSmlKv *p = (SSmlKv *)taosArrayGetP(kvArray, j); + if(info->protocol == TSDB_SML_JSON_PROTOCOL && + (p->type == TSDB_DATA_TYPE_NCHAR || p->type == TSDB_DATA_TYPE_BINARY)){ + taosMemoryFree((void*)p->value); + } taosMemoryFree(p); } taosArrayDestroy(kvArray); @@ -1257,6 +1264,19 @@ static void smlDestroyTableInfo(SSmlTableInfo *tag, bool format){ taosHashCleanup(kvHash); } } + for(size_t i = 0; i < taosArrayGetSize(tag->tags); i++){ + SSmlKv *p = (SSmlKv *)taosArrayGetP(tag->tags, i); + if(info->protocol == TSDB_SML_JSON_PROTOCOL){ + taosMemoryFree((void*)p->key); + if(p->type == TSDB_DATA_TYPE_NCHAR || p->type == TSDB_DATA_TYPE_BINARY){ + taosMemoryFree((void*)p->value); + } + } + taosMemoryFree(p); + } + if(info->protocol == TSDB_SML_JSON_PROTOCOL && tag->sTableName){ + taosMemoryFree((void*)tag->sTableName); + } taosArrayDestroy(tag->cols); taosArrayDestroy(tag->tags); taosMemoryFree(tag); @@ -1293,8 +1313,8 @@ static SSmlSTableMeta* smlBuildSTableMeta(){ goto cleanup; } - meta->fieldHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); - if (meta->fieldHash == NULL) { + meta->colHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); + if (meta->colHash == NULL) { uError("SML:smlBuildSTableMeta failed to allocate memory"); goto cleanup; } @@ -1319,7 +1339,7 @@ cleanup: static void smlDestroySTableMeta(SSmlSTableMeta *meta){ taosHashCleanup(meta->tagHash); - taosHashCleanup(meta->fieldHash); + taosHashCleanup(meta->colHash); taosArrayDestroy(meta->tags); taosArrayDestroy(meta->cols); taosMemoryFree(meta->tableMeta); @@ -1341,7 +1361,7 @@ static void smlDestroyInfo(SSmlHandle* info){ // destroy info->childTables void** p1 = (void**)taosHashIterate(info->childTables, NULL); while (p1) { - smlDestroyTableInfo((SSmlTableInfo*)(*p1), info->dataFormat); + smlDestroyTableInfo(info, (SSmlTableInfo*)(*p1)); p1 = (void**)taosHashIterate(info->childTables, p1); } taosHashCleanup(info->childTables); @@ -1357,7 +1377,9 @@ static void smlDestroyInfo(SSmlHandle* info){ // destroy info->pVgHash taosHashCleanup(info->pVgHash); taosHashCleanup(info->dumplicateKey); - + if(!info->dataFormat){ + taosArrayDestroy(info->colsContainer); + } taosMemoryFreeClear(info); } @@ -1682,8 +1704,7 @@ static int32_t smlConvertJSONString(SSmlKv *pVal, char* typeStr, cJSON *value) { return TSDB_CODE_TSC_INVALID_JSON_TYPE; } pVal->length = (int16_t)strlen(value->valuestring); - pVal->valueLen = pVal->length; - return smlJsonCreateSring(&pVal->value, value->valuestring, pVal->valueLen); + return smlJsonCreateSring(&pVal->value, value->valuestring, pVal->length); } static int32_t smlParseValueFromJSONObj(cJSON *root, SSmlKv *kv) { @@ -1754,7 +1775,7 @@ static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) { * user configured parameter tsDefaultJSONStrType */ - char *tsDefaultJSONStrType = "binary"; //todo + char *tsDefaultJSONStrType = "nchar"; //todo smlConvertJSONString(kv, tsDefaultJSONStrType, root); break; } @@ -1985,8 +2006,8 @@ static int32_t smlParseInfluxLine(SSmlHandle* info, const char* sql) { (*oneTable)->sTableName = elements.measure; (*oneTable)->sTableNameLen = elements.measureLen; - RandTableName rName = {.tags=(*oneTable)->tags, .sTableName=(*oneTable)->sTableName, .sTableNameLen=(uint8_t)(*oneTable)->sTableNameLen, - .childTableName=(*oneTable)->childTableName}; + RandTableName rName = { (*oneTable)->tags, (*oneTable)->sTableName, (uint8_t)(*oneTable)->sTableNameLen, + (*oneTable)->childTableName, 0 }; buildChildTableName(&rName); (*oneTable)->uid = rName.uid; @@ -1994,14 +2015,18 @@ static int32_t smlParseInfluxLine(SSmlHandle* info, const char* sql) { SSmlSTableMeta** tableMeta = (SSmlSTableMeta**)taosHashGet(info->superTables, elements.measure, elements.measureLen); if(tableMeta){ // update meta - ret = smlUpdateMeta(*tableMeta, hasTable ? NULL : (*oneTable)->tags, cols, &info->msgBuf); + ret = smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, cols, &info->msgBuf); + if(!hasTable && ret){ + ret = smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, (*oneTable)->tags, &info->msgBuf); + } if(!ret){ uError("SML:0x%"PRIx64" smlUpdateMeta failed", info->id); return TSDB_CODE_SML_INVALID_DATA; } }else{ SSmlSTableMeta *meta = smlBuildSTableMeta(); - smlInsertMeta(meta, (*oneTable)->tags, cols); + smlInsertMeta(meta->tagHash, meta->tags, (*oneTable)->tags); + smlInsertMeta(meta->colHash, meta->cols, cols); taosHashPut(info->superTables, elements.measure, elements.measureLen, &meta, POINTER_BYTES); } @@ -2026,15 +2051,15 @@ static int32_t smlParseTelnetLine(SSmlHandle* info, void *data) { } if(info->protocol == TSDB_SML_TELNET_PROTOCOL){ - smlParseTelnetString(info, (const char*)data, tinfo, cols); + ret = smlParseTelnetString(info, (const char*)data, tinfo, cols); }else if(info->protocol == TSDB_SML_JSON_PROTOCOL){ - smlParseJSONString(info, (cJSON *)data, tinfo, cols); + ret = smlParseJSONString(info, (cJSON *)data, tinfo, cols); }else{ ASSERT(0); } if(ret != TSDB_CODE_SUCCESS){ uError("SML:0x%"PRIx64" smlParseTelnetLine failed", info->id); - smlDestroyTableInfo(tinfo, true); + smlDestroyTableInfo(info, tinfo); taosArrayDestroy(cols); return ret; } @@ -2045,8 +2070,8 @@ static int32_t smlParseTelnetLine(SSmlHandle* info, void *data) { } taosHashClear(info->dumplicateKey); - RandTableName rName = {.tags=tinfo->tags, .sTableName=tinfo->sTableName, .sTableNameLen=(uint8_t)tinfo->sTableNameLen, - .childTableName=tinfo->childTableName}; + RandTableName rName = { tinfo->tags, tinfo->sTableName, (uint8_t)tinfo->sTableNameLen, + tinfo->childTableName, 0 }; buildChildTableName(&rName); tinfo->uid = rName.uid; @@ -2057,20 +2082,24 @@ static int32_t smlParseTelnetLine(SSmlHandle* info, void *data) { oneTable = &tinfo; hasTable = false; }else{ - smlDestroyTableInfo(tinfo, true); + smlDestroyTableInfo(info, tinfo); } taosArrayPush((*oneTable)->cols, &cols); SSmlSTableMeta** tableMeta = (SSmlSTableMeta** )taosHashGet(info->superTables, (*oneTable)->sTableName, (*oneTable)->sTableNameLen); if(tableMeta){ // update meta - ret = smlUpdateMeta(*tableMeta, hasTable ? NULL : (*oneTable)->tags, cols, &info->msgBuf); + ret = smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, cols, &info->msgBuf); + if(!hasTable && ret){ + ret = smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, (*oneTable)->tags, &info->msgBuf); + } if(!ret){ uError("SML:0x%"PRIx64" smlUpdateMeta failed", info->id); return TSDB_CODE_SML_INVALID_DATA; } }else{ SSmlSTableMeta *meta = smlBuildSTableMeta(); - smlInsertMeta(meta, (*oneTable)->tags, cols); + smlInsertMeta(meta->tagHash, meta->tags, (*oneTable)->tags); + smlInsertMeta(meta->colHash, meta->cols, cols); taosHashPut(info->superTables, (*oneTable)->sTableName, (*oneTable)->sTableNameLen, &meta, POINTER_BYTES); } diff --git a/source/client/test/smlTest.cpp b/source/client/test/smlTest.cpp index f579489e62480c1247873c09b954debe63db67d2..b9870633dbb7cbae63c4f5a74555831d442d6f64 100644 --- a/source/client/test/smlTest.cpp +++ b/source/client/test/smlTest.cpp @@ -228,7 +228,7 @@ TEST(testCase, smlParseCols_tag_Test) { ASSERT_EQ(strncasecmp(kv->key, "cbin", 4), 0); ASSERT_EQ(kv->keyLen, 4); ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR); - ASSERT_EQ(kv->valueLen, 17); + ASSERT_EQ(kv->length, 17); ASSERT_EQ(strncasecmp(kv->value, "\"passit", 7), 0); taosMemoryFree(kv); @@ -237,7 +237,7 @@ TEST(testCase, smlParseCols_tag_Test) { ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0); ASSERT_EQ(kv->keyLen, 4); ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR); - ASSERT_EQ(kv->valueLen, 7); + ASSERT_EQ(kv->length, 7); ASSERT_EQ(strncasecmp(kv->value, "4.31f64", 7), 0); taosMemoryFree(kv); @@ -259,7 +259,7 @@ TEST(testCase, smlParseCols_tag_Test) { ASSERT_EQ(strncasecmp(kv->key, TAG, strlen(TAG)), 0); ASSERT_EQ(kv->keyLen, strlen(TAG)); ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR); - ASSERT_EQ(kv->valueLen, strlen(TAG)); + ASSERT_EQ(kv->length, strlen(TAG)); ASSERT_EQ(strncasecmp(kv->value, TAG, strlen(TAG)), 0); taosMemoryFree(kv); @@ -499,12 +499,14 @@ TEST(testCase, smlProcess_influx_Test) { TAOS_RES *res = taos_query(taos, "select * from t_6885c584b98481584ee13dac399e173d"); ASSERT_NE(res, nullptr); int fieldNum = taos_field_count(res); - ASSERT_EQ(fieldNum, 11); + ASSERT_EQ(fieldNum, 5); int rowNum = taos_affected_rows(res); ASSERT_EQ(rowNum, 2); for (int i = 0; i < rowNum; ++i) { TAOS_ROW rows = taos_fetch_row(res); } + taos_free_result(res); + smlDestroyInfo(info); } // different types @@ -530,245 +532,542 @@ TEST(testCase, smlParseLine_error_Test) { }; int ret = smlProcess(info, (char **)sql, sizeof(sql)/sizeof(sql[0])); ASSERT_NE(ret, 0); + destroyRequest(request); + smlDestroyInfo(info); +} + +TEST(testCase, smlGetTimestampLen_Test) { + uint8_t len = smlGetTimestampLen(0); + ASSERT_EQ(len, 1); + + len = smlGetTimestampLen(1); + ASSERT_EQ(len, 1); + + len = smlGetTimestampLen(10); + ASSERT_EQ(len, 2); + + len = smlGetTimestampLen(390); + ASSERT_EQ(len, 3); + + len = smlGetTimestampLen(-1); + ASSERT_EQ(len, 1); + + len = smlGetTimestampLen(-10); + ASSERT_EQ(len, 2); + + len = smlGetTimestampLen(-390); + ASSERT_EQ(len, 3); +} + +TEST(testCase, smlProcess_telnet_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + const char *sql[5] = { + "sys.if.bytes.out 1479496100 1.3E0 host=web01 interface=eth0", + "sys.if.bytes.out 1479496101 1.3E1 interface=eth0 host=web01", + "sys.if.bytes.out 1479496102 1.3E3 network=tcp", + "sys.procs.running 1479496100 42 host=web01 ", + " sys.procs.running 1479496200 42 host=web01=4" + }; + int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0])); + ASSERT_EQ(ret, 0); + + TAOS_RES *res = taos_query(taos, "select * from t_8c30283b3c4131a071d1e16cf6d7094a"); + ASSERT_NE(res, nullptr); + int fieldNum = taos_field_count(res); + ASSERT_EQ(fieldNum, 2); + int rowNum = taos_affected_rows(res); + ASSERT_EQ(rowNum, 1); + for (int i = 0; i < rowNum; ++i) { + TAOS_ROW rows = taos_fetch_row(res); + } + taos_free_result(pRes); + + res = taos_query(taos, "select * from t_6931529054e5637ca92c78a1ad441961"); + ASSERT_NE(res, nullptr); + fieldNum = taos_field_count(res); + ASSERT_EQ(fieldNum, 2); + rowNum = taos_affected_rows(res); + ASSERT_EQ(rowNum, 2); + for (int i = 0; i < rowNum; ++i) { + TAOS_ROW rows = taos_fetch_row(res); + } + taos_free_result(pRes); + smlDestroyInfo(info); +} + +TEST(testCase, smlProcess_json1_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + const char *sql = + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"host\": \"web01\",\n" + " \"dc\": \"lga\"\n" + " }\n" + " },\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": 9,\n" + " \"tags\": {\n" + " \"host\": \"web02\",\n" + " \"dc\": \"lga\"\n" + " }\n" + " }\n" + "]"; + int ret = smlProcess(info, (char **)(&sql), -1); + ASSERT_EQ(ret, 0); + + TAOS_RES *res = taos_query(taos, "select * from t_cb27a7198d637b4f1c6464bd73f756a7"); + ASSERT_NE(res, nullptr); + int fieldNum = taos_field_count(res); + ASSERT_EQ(fieldNum, 2); + // int rowNum = taos_affected_rows(res); + // ASSERT_EQ(rowNum, 1); + // for (int i = 0; i < rowNum; ++i) { + // TAOS_ROW rows = taos_fetch_row(res); + // } + taos_free_result(pRes); + smlDestroyInfo(info); +} + +TEST(testCase, smlProcess_json2_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + const char *sql = + "{\n" + " \"metric\": \"meter_current0\",\n" + " \"timestamp\": {\n" + " \"value\" : 1346846400,\n" + " \"type\" : \"s\"\n" + " },\n" + " \"value\": {\n" + " \"value\" : 10.3,\n" + " \"type\" : \"i64\"\n" + " },\n" + " \"tags\": {\n" + " \"groupid\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"bigint\"\n" + " },\n" + " \"location\": { \n" + " \"value\" : \"北京\",\n" + " \"type\" : \"binary\"\n" + " },\n" + " \"id\": \"d1001\"\n" + " }\n" + "}"; + int32_t ret = smlProcess(info, (char **)(&sql), -1); + ASSERT_EQ(ret, 0); + taos_free_result(pRes); + smlDestroyInfo(info); } - TEST(testCase, smlGetTimestampLen_Test) { - uint8_t len = smlGetTimestampLen(0); - ASSERT_EQ(len, 1); - - len = smlGetTimestampLen(1); - ASSERT_EQ(len, 1); - - len = smlGetTimestampLen(10); - ASSERT_EQ(len, 2); - - len = smlGetTimestampLen(390); - ASSERT_EQ(len, 3); - - len = smlGetTimestampLen(-1); - ASSERT_EQ(len, 1); - - len = smlGetTimestampLen(-10); - ASSERT_EQ(len, 2); - - len = smlGetTimestampLen(-390); - ASSERT_EQ(len, 3); - } - - TEST(testCase, smlProcess_telnet_Test) { - TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); - ASSERT_NE(taos, nullptr); - - TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); - taos_free_result(pRes); - - pRes = taos_query(taos, "use sml_db"); - taos_free_result(pRes); - - SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); - ASSERT_NE(request, nullptr); - - SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); - ASSERT_NE(info, nullptr); - - const char *sql[4] = { - "sys.if.bytes.out 1479496100 1.3E0 host=web01 interface=eth0", - "sys.if.bytes.out 1479496101 1.3E1 interface=eth0 host=web01 ", - "sys.if.bytes.out 1479496102 1.3E3 network=tcp", - "sys.procs.running 1479496100 42 host=web01" - }; - int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0])); - ASSERT_EQ(ret, 0); - - TAOS_RES *res = taos_query(taos, "select * from t_8c30283b3c4131a071d1e16cf6d7094a"); - ASSERT_NE(res, nullptr); - int fieldNum = taos_field_count(res); - ASSERT_EQ(fieldNum, 2); - int rowNum = taos_affected_rows(res); - ASSERT_EQ(rowNum, 1); - for (int i = 0; i < rowNum; ++i) { - TAOS_ROW rows = taos_fetch_row(res); - } - - res = taos_query(taos, "select * from t_6931529054e5637ca92c78a1ad441961"); - ASSERT_NE(res, nullptr); - fieldNum = taos_field_count(res); - ASSERT_EQ(fieldNum, 2); - rowNum = taos_affected_rows(res); - ASSERT_EQ(rowNum, 2); - for (int i = 0; i < rowNum; ++i) { - TAOS_ROW rows = taos_fetch_row(res); - } - } - - TEST(testCase, smlProcess_json_Test) { - TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); - ASSERT_NE(taos, nullptr); - - TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); - taos_free_result(pRes); - - pRes = taos_query(taos, "use sml_db"); - taos_free_result(pRes); - - SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); - ASSERT_NE(request, nullptr); - - SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); - ASSERT_NE(info, nullptr); - - const char *sql = "[\n" - " {\n" - " \"metric\": \"sys.cpu.nice\",\n" - " \"timestamp\": 1346846400,\n" - " \"value\": 18,\n" - " \"tags\": {\n" - " \"host\": \"web01\",\n" - " \"dc\": \"lga\"\n" - " }\n" - " },\n" - " {\n" - " \"metric\": \"sys.cpu.nice\",\n" - " \"timestamp\": 1346846400,\n" - " \"value\": 9,\n" - " \"tags\": {\n" - " \"host\": \"web02\",\n" - " \"dc\": \"lga\"\n" - " }\n" - " }\n" - "]"; - int ret = smlProcess(info, (char**)(&sql), -1); - ASSERT_EQ(ret, 0); - - TAOS_RES *res = taos_query(taos, "select * from t_cb27a7198d637b4f1c6464bd73f756a7"); - ASSERT_NE(res, nullptr); - int fieldNum = taos_field_count(res); - ASSERT_EQ(fieldNum, 2); -// int rowNum = taos_affected_rows(res); -// ASSERT_EQ(rowNum, 1); -// for (int i = 0; i < rowNum; ++i) { -// TAOS_ROW rows = taos_fetch_row(res); -// } - - sql = "{\n" - " \"metric\": \"meter_current\",\n" - " \"timestamp\": {\n" - " \"value\" : 1346846400,\n" - " \"type\" : \"s\"\n" - " },\n" - " \"value\": {\n" - " \"value\" : 10.3,\n" - " \"type\" : \"i64\"\n" - " },\n" - " \"tags\": {\n" - " \"groupid\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"bigint\"\n" - " },\n" - " \"location\": { \n" - " \"value\" : \"北京\",\n" - " \"type\" : \"binary\"\n" - " },\n" - " \"id\": \"d1001\"\n" - " }\n" - "}"; - ret = smlProcess(info, (char**)(&sql), -1); - ASSERT_EQ(ret, 0); - - sql = "{\n" - " \"metric\": \"meter_current\",\n" - " \"timestamp\": {\n" - " \"value\" : 1346846400,\n" - " \"type\" : \"s\"\n" - " },\n" - " \"value\": {\n" - " \"value\" : 10.3,\n" - " \"type\" : \"i64\"\n" - " },\n" - " \"tags\": {\n" - " \"t1\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"bigint\"\n" - " },\n" - " \"t2\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"int\"\n" - " },\n" - " \"t3\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"i16\"\n" - " },\n" - " \"t4\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"i8\"\n" - " },\n" - " \"t5\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"f32\"\n" - " },\n" - " \"t6\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"double\"\n" - " },\n" - " \"t7\": { \n" - " \"value\" : \"8323\",\n" - " \"type\" : \"binary\"\n" - " },\n" - " \"t8\": { \n" - " \"value\" : \"北京\",\n" - " \"type\" : \"binary\"\n" - " },\n" - " \"t9\": { \n" - " \"value\" : true,\n" - " \"type\" : \"bool\"\n" - " },\n" - " \"id\": \"d1001\"\n" - " }\n" - "}"; - ret = smlProcess(info, (char**)(&sql), -1); - ASSERT_EQ(ret, 0); - - sql = "{\n" - " \"metric\": \"meter_current\",\n" - " \"timestamp\": {\n" - " \"value\" : 1346846400000,\n" - " \"type\" : \"ms\"\n" - " },\n" - " \"value\": \"ni\",\n" - " \"tags\": {\n" - " \"t1\": { \n" - " \"value\" : 20,\n" - " \"type\" : \"i64\"\n" - " },\n" - " \"t2\": { \n" - " \"value\" : 25,\n" - " \"type\" : \"i32\"\n" - " },\n" - " \"t3\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"smallint\"\n" - " },\n" - " \"t4\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"tinyint\"\n" - " },\n" - " \"t5\": { \n" - " \"value\" : 2,\n" - " \"type\" : \"float\"\n" - " },\n" - " \"t6\": { \n" - " \"value\" : 0.2,\n" - " \"type\" : \"f64\"\n" - " },\n" - " \"t7\": \"nsj\",\n" - " \"t8\": { \n" - " \"value\" : \"北京\",\n" - " \"type\" : \"binary\"\n" - " },\n" - " \"t9\": false,\n" - " \"id\": \"d1001\"\n" - " }\n" - "}"; - ret = smlProcess(info, (char**)(&sql), -1); - ASSERT_EQ(ret, 0); - } +TEST(testCase, smlProcess_json3_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + const char *sql = + "{\n" + " \"metric\": \"meter_current1\",\n" + " \"timestamp\": {\n" + " \"value\" : 1346846400,\n" + " \"type\" : \"s\"\n" + " },\n" + " \"value\": {\n" + " \"value\" : 10.3,\n" + " \"type\" : \"i64\"\n" + " },\n" + " \"tags\": {\n" + " \"t1\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"bigint\"\n" + " },\n" + " \"t2\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"int\"\n" + " },\n" + " \"t3\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"i16\"\n" + " },\n" + " \"t4\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"i8\"\n" + " },\n" + " \"t5\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"f32\"\n" + " },\n" + " \"t6\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"double\"\n" + " },\n" + " \"t7\": { \n" + " \"value\" : \"8323\",\n" + " \"type\" : \"binary\"\n" + " },\n" + " \"t8\": { \n" + " \"value\" : \"北京\",\n" + " \"type\" : \"nchar\"\n" + " },\n" + " \"t9\": { \n" + " \"value\" : true,\n" + " \"type\" : \"bool\"\n" + " },\n" + " \"id\": \"d1001\"\n" + " }\n" + "}"; + int32_t ret = smlProcess(info, (char **)(&sql), -1); + ASSERT_EQ(ret, 0); + taos_free_result(pRes); + smlDestroyInfo(info); +} + +TEST(testCase, smlProcess_json4_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + const char *sql = "{\n" + " \"metric\": \"meter_current2\",\n" + " \"timestamp\": {\n" + " \"value\" : 1346846500000,\n" + " \"type\" : \"ms\"\n" + " },\n" + " \"value\": \"ni\",\n" + " \"tags\": {\n" + " \"t1\": { \n" + " \"value\" : 20,\n" + " \"type\" : \"i64\"\n" + " },\n" + " \"t2\": { \n" + " \"value\" : 25,\n" + " \"type\" : \"i32\"\n" + " },\n" + " \"t3\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"smallint\"\n" + " },\n" + " \"t4\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"tinyint\"\n" + " },\n" + " \"t5\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"float\"\n" + " },\n" + " \"t6\": { \n" + " \"value\" : 0.2,\n" + " \"type\" : \"f64\"\n" + " },\n" + " \"t7\": \"nsj\",\n" + " \"t8\": { \n" + " \"value\" : \"北京\",\n" + " \"type\" : \"nchar\"\n" + " },\n" + " \"t9\": false,\n" + " \"id\": \"d1001\"\n" + " }\n" + "}"; + int32_t ret = smlProcess(info, (char**)(&sql), -1); + ASSERT_EQ(ret, 0); + taos_free_result(pRes); + smlDestroyInfo(info); +} + +TEST(testCase, smlParseTelnetLine_error_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + int32_t ret = 0; + const char *sql[19] = { + "sys.procs.running 14794961040 42 host=web01", + "sys.procs.running 14791040 42 host=web01", + "sys.procs.running erere 42 host=web01", + "sys.procs.running 1.6e10 42 host=web01", + "sys.procs.running 1.47949610 42 host=web01", + "sys.procs.running 147949610i 42 host=web01", + "sys.procs.running -147949610 42 host=web01", + "", + " ", + "sys ", + "sys.procs.running 1479496100 42 ", + "sys.procs.running 1479496100 42 host= ", + "sys.procs.running 1479496100 42or host=web01", + "sys.procs.running 1479496100 true host=web01", + "sys.procs.running 1479496100 \"binary\" host=web01", + "sys.procs.running 1479496100 L\"rfr\" host=web01", + "sys.procs.running 1479496100 42 host=web01 cpu= ", + "sys.procs.running 1479496100 42 host=web01 host=w2", + "sys.procs.running 1479496100 42 host=web01 host", + }; + for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){ + ret = smlParseTelnetLine(info, (void*)sql[i]); + ASSERT_NE(ret, 0); + } + + destroyRequest(request); + smlDestroyInfo(info); +} + +TEST(testCase, smlParseTelnetLine_diff_type_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + const char *sql[2] = { + "sys.procs.running 1479496104000 42 host=web01", + "sys.procs.running 1479496104000 42u8 host=web01" + }; + int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0])); + ASSERT_NE(ret, 0); + + destroyRequest(request); + smlDestroyInfo(info); +} + +TEST(testCase, smlParseTelnetLine_json_error_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + int32_t ret = 0; + const char *sql[] = { + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 13468464009999333322222223,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"host\": \"web01\",\n" + " \"dc\": \"lga\"\n" + " }\n" + " },\n" + "]", + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400i,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"host\": \"web01\",\n" + " \"dc\": \"lga\"\n" + " }\n" + " },\n" + "]", + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"groupid\": { \n" + " \"value\" : 2,\n" + " \"type\" : \"nchar\"\n" + " },\n" + " \"location\": { \n" + " \"value\" : \"北京\",\n" + " \"type\" : \"binary\"\n" + " },\n" + " \"id\": \"d1001\"\n" + " }\n" + " },\n" + "]", + }; + for(int i = 0; i < sizeof(sql)/sizeof(sql[0]); i++){ + ret = smlParseTelnetLine(info, (void*)sql[i]); + ASSERT_NE(ret, 0); + } + + destroyRequest(request); + smlDestroyInfo(info); +} + +TEST(testCase, smlParseTelnetLine_diff_json_type1_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + const char *sql[2] = { + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"host\": \"lga\"\n" + " }\n" + " },\n" + "]", + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"host\": 8\n" + " }\n" + " },\n" + "]", + }; + int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0])); + ASSERT_NE(ret, 0); + + destroyRequest(request); + smlDestroyInfo(info); +} + +TEST(testCase, smlParseTelnetLine_diff_json_type2_Test) { + TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); + ASSERT_NE(taos, nullptr); + + TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db"); + taos_free_result(pRes); + + pRes = taos_query(taos, "use sml_db"); + taos_free_result(pRes); + + SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT); + ASSERT_NE(request, nullptr); + + SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true); + ASSERT_NE(info, nullptr); + + const char *sql[2] = { + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": 18,\n" + " \"tags\": {\n" + " \"host\": \"lga\"\n" + " }\n" + " },\n" + "]", + "[\n" + " {\n" + " \"metric\": \"sys.cpu.nice\",\n" + " \"timestamp\": 1346846400,\n" + " \"value\": \"18\",\n" + " \"tags\": {\n" + " \"host\": \"fff\"\n" + " }\n" + " },\n" + "]", + }; + int32_t ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0])); + ASSERT_NE(ret, 0); + + destroyRequest(request); + smlDestroyInfo(info); +} diff --git a/source/common/src/systable.c b/source/common/src/systable.c index 81682bb734252d85ff8af19eda3edb506d17f8a2..e4e5abe148130b031036ed2101b7d8075ecc687e 100644 --- a/source/common/src/systable.c +++ b/source/common/src/systable.c @@ -26,7 +26,7 @@ static const SSysDbTableSchema dnodesSchema[] = { {.name = "id", .bytes = 2, .type = TSDB_DATA_TYPE_SMALLINT}, {.name = "endpoint", .bytes = TSDB_EP_LEN + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_VARCHAR}, {.name = "vnodes", .bytes = 2, .type = TSDB_DATA_TYPE_SMALLINT}, - {.name = "max_vnodes", .bytes = 2, .type = TSDB_DATA_TYPE_SMALLINT}, + {.name = "support_vnodes", .bytes = 2, .type = TSDB_DATA_TYPE_SMALLINT}, {.name = "status", .bytes = 10 + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_VARCHAR}, {.name = "create_time", .bytes = 8, .type = TSDB_DATA_TYPE_TIMESTAMP}, {.name = "note", .bytes = 256 + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_VARCHAR}, diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 0999cb4d2c4be55ac5b151b28b8037b74d585736..d82378c3157c3f5648f864329047e2d117b29d33 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -33,18 +33,17 @@ int32_t tsStatusInterval = 1; // second // common int32_t tsMaxShellConns = 50000; -int32_t tsMaxConnections = 50000; int32_t tsShellActivityTimer = 3; // second bool tsEnableSlaveQuery = true; bool tsPrintAuth = false; // multi process bool tsMultiProcess = false; -int32_t tsMnodeShmSize = TSDB_MAX_WAL_SIZE * 2; -int32_t tsVnodeShmSize = TSDB_MAX_WAL_SIZE * 10; -int32_t tsQnodeShmSize = TSDB_MAX_WAL_SIZE * 4; -int32_t tsSnodeShmSize = TSDB_MAX_WAL_SIZE * 4; -int32_t tsBnodeShmSize = TSDB_MAX_WAL_SIZE * 4; +int32_t tsMnodeShmSize = TSDB_MAX_WAL_SIZE * 2 + 128; +int32_t tsVnodeShmSize = TSDB_MAX_WAL_SIZE * 10 + 128; +int32_t tsQnodeShmSize = TSDB_MAX_WAL_SIZE * 4 + 128; +int32_t tsSnodeShmSize = TSDB_MAX_WAL_SIZE * 4 + 128; +int32_t tsBnodeShmSize = TSDB_MAX_WAL_SIZE * 4 + 128; // queue & threads int32_t tsNumOfRpcThreads = 1; @@ -351,15 +350,14 @@ static int32_t taosAddSystemCfg(SConfig *pCfg) { } static int32_t taosAddServerCfg(SConfig *pCfg) { - if (cfgAddInt32(pCfg, "supportVnodes", 256, 0, 4096, 0) != 0) return -1; if (cfgAddDir(pCfg, "dataDir", tsDataDir, 0) != 0) return -1; if (cfgAddFloat(pCfg, "minimalDataDirGB", 2.0f, 0.001f, 10000000, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "maxNumOfDistinctRes", tsMaxNumOfDistinctResults, 10 * 10000, 10000 * 10000, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "maxConnections", tsMaxConnections, 1, 100000, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "supportVnodes", 256, 0, 4096, 0) != 0) return -1; if (cfgAddInt32(pCfg, "maxShellConns", tsMaxShellConns, 10, 50000000, 0) != 0) return -1; if (cfgAddInt32(pCfg, "statusInterval", tsStatusInterval, 1, 30, 0) != 0) return -1; if (cfgAddInt32(pCfg, "minSlidingTime", tsMinSlidingTime, 10, 1000000, 0) != 0) return -1; if (cfgAddInt32(pCfg, "minIntervalTime", tsMinIntervalTime, 1, 1000000, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "maxNumOfDistinctRes", tsMaxNumOfDistinctResults, 10 * 10000, 10000 * 10000, 0) != 0) return -1; if (cfgAddInt32(pCfg, "maxStreamCompDelay", tsMaxStreamComputDelay, 10, 1000000000, 0) != 0) return -1; if (cfgAddInt32(pCfg, "maxFirstStreamCompDelay", tsStreamCompStartDelay, 1000, 1000000000, 0) != 0) return -1; if (cfgAddInt32(pCfg, "retryStreamCompDelay", tsRetryStreamCompDelay, 10, 1000000000, 0) != 0) return -1; @@ -371,11 +369,11 @@ static int32_t taosAddServerCfg(SConfig *pCfg) { if (cfgAddBool(pCfg, "deadLockKillQuery", tsDeadLockKillQuery, 0) != 0) return -1; if (cfgAddBool(pCfg, "multiProcess", tsMultiProcess, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "mnodeShmSize", tsMnodeShmSize, 4096, INT32_MAX, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "vnodeShmSize", tsVnodeShmSize, 4096, INT32_MAX, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "qnodeShmSize", tsQnodeShmSize, 4096, INT32_MAX, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "snodeShmSize", tsSnodeShmSize, 4096, INT32_MAX, 0) != 0) return -1; - if (cfgAddInt32(pCfg, "bnodeShmSize", tsBnodeShmSize, 4096, INT32_MAX, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "mnodeShmSize", tsMnodeShmSize, TSDB_MAX_WAL_SIZE + 128, INT32_MAX, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "vnodeShmSize", tsVnodeShmSize, TSDB_MAX_WAL_SIZE + 128, INT32_MAX, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "qnodeShmSize", tsQnodeShmSize, TSDB_MAX_WAL_SIZE + 128, INT32_MAX, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "snodeShmSize", tsSnodeShmSize, TSDB_MAX_WAL_SIZE + 128, INT32_MAX, 0) != 0) return -1; + if (cfgAddInt32(pCfg, "bnodeShmSize", tsBnodeShmSize, TSDB_MAX_WAL_SIZE + 128, INT32_MAX, 0) != 0) return -1; tsNumOfRpcThreads = tsNumOfCores / 2; tsNumOfRpcThreads = TRANGE(tsNumOfRpcThreads, 1, 4); @@ -533,12 +531,11 @@ static void taosSetSystemCfg(SConfig *pCfg) { static int32_t taosSetServerCfg(SConfig *pCfg) { tsDataSpace.reserved = cfgGetItem(pCfg, "minimalDataDirGB")->fval; - tsMaxNumOfDistinctResults = cfgGetItem(pCfg, "maxNumOfDistinctRes")->i32; - tsMaxConnections = cfgGetItem(pCfg, "maxConnections")->i32; tsMaxShellConns = cfgGetItem(pCfg, "maxShellConns")->i32; tsStatusInterval = cfgGetItem(pCfg, "statusInterval")->i32; tsMinSlidingTime = cfgGetItem(pCfg, "minSlidingTime")->i32; tsMinIntervalTime = cfgGetItem(pCfg, "minIntervalTime")->i32; + tsMaxNumOfDistinctResults = cfgGetItem(pCfg, "maxNumOfDistinctRes")->i32; tsMaxStreamComputDelay = cfgGetItem(pCfg, "maxStreamCompDelay")->i32; tsStreamCompStartDelay = cfgGetItem(pCfg, "maxFirstStreamCompDelay")->i32; tsRetryStreamCompDelay = cfgGetItem(pCfg, "retryStreamCompDelay")->i32; diff --git a/source/common/src/tmsgcb.c b/source/common/src/tmsgcb.c index 42612cecb9531a7d29e92963f7fa7bf582982986..78b70c92882331b697a5dd09abcd1a6991771dd8 100644 --- a/source/common/src/tmsgcb.c +++ b/source/common/src/tmsgcb.c @@ -19,12 +19,16 @@ static SMsgCb tsDefaultMsgCb; -void tmsgSetDefaultMsgCb(const SMsgCb* pMsgCb) { tsDefaultMsgCb = *pMsgCb; } +void tmsgSetDefaultMsgCb(const SMsgCb* pMsgCb) { + // if (tsDefaultMsgCb.pWrapper == NULL) { + tsDefaultMsgCb = *pMsgCb; + //} +} int32_t tmsgPutToQueue(const SMsgCb* pMsgCb, EQueueType qtype, SRpcMsg* pReq) { PutToQueueFp fp = pMsgCb->queueFps[qtype]; if (fp != NULL) { - return (*fp)(pMsgCb->pWrapper, pReq); + return (*fp)(pMsgCb->pMgmt, pReq); } else { terrno = TSDB_CODE_INVALID_PTR; return -1; @@ -34,7 +38,7 @@ int32_t tmsgPutToQueue(const SMsgCb* pMsgCb, EQueueType qtype, SRpcMsg* pReq) { int32_t tmsgGetQueueSize(const SMsgCb* pMsgCb, int32_t vgId, EQueueType qtype) { GetQueueSizeFp fp = pMsgCb->qsizeFp; if (fp != NULL) { - return (*fp)(pMsgCb->pWrapper, vgId, qtype); + return (*fp)(pMsgCb->pMgmt, vgId, qtype); } else { terrno = TSDB_CODE_INVALID_PTR; return -1; @@ -51,7 +55,7 @@ int32_t tmsgSendReq(const SMsgCb* pMsgCb, const SEpSet* epSet, SRpcMsg* pReq) { } } -void tmsgSendRsp(const SRpcMsg* pRsp) { +void tmsgSendRsp(SRpcMsg* pRsp) { SendRspFp fp = tsDefaultMsgCb.sendRspFp; if (fp != NULL) { return (*fp)(tsDefaultMsgCb.pWrapper, pRsp); @@ -60,7 +64,7 @@ void tmsgSendRsp(const SRpcMsg* pRsp) { } } -void tmsgSendRedirectRsp(const SRpcMsg* pRsp, const SEpSet* pNewEpSet) { +void tmsgSendRedirectRsp(SRpcMsg* pRsp, const SEpSet* pNewEpSet) { SendRedirectRspFp fp = tsDefaultMsgCb.sendRedirectRspFp; if (fp != NULL) { (*fp)(tsDefaultMsgCb.pWrapper, pRsp, pNewEpSet); @@ -69,6 +73,15 @@ void tmsgSendRedirectRsp(const SRpcMsg* pRsp, const SEpSet* pNewEpSet) { } } +void tmsgSendMnodeRecv(SRpcMsg* pReq, SRpcMsg* pRsp) { + SendMnodeRecvFp fp = tsDefaultMsgCb.sendMnodeRecvFp; + if (fp != NULL) { + (*fp)(tsDefaultMsgCb.pWrapper, pReq, pRsp); + } else { + terrno = TSDB_CODE_INVALID_PTR; + } +} + void tmsgRegisterBrokenLinkArg(const SMsgCb* pMsgCb, SRpcMsg* pMsg) { RegisterBrokenLinkArgFp fp = pMsgCb->registerBrokenLinkArgFp; if (fp != NULL) { diff --git a/source/common/src/tname.c b/source/common/src/tname.c index 56fbfed8fff294ac482f5ffe25a00192f4bb9880..fa9b6e1e63628593aca1fb27ead10ec6a7bff7d1 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -317,7 +317,11 @@ void buildChildTableName(RandTableName* rName) { for (int j = 0; j < size; ++j) { SSmlKv* tagKv = taosArrayGetP(rName->tags, j); taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen); - taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->valueLen); + if(IS_VAR_DATA_TYPE(tagKv->type)){ + taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->length); + }else{ + taosStringBuilderAppendStringLen(&sb, (char*)(&(tagKv->value)), tagKv->length); + } } size_t len = 0; char* keyJoined = taosStringBuilderGetResult(&sb, &len); diff --git a/source/dnode/mgmt/CMakeLists.txt b/source/dnode/mgmt/CMakeLists.txt index 49ea54e92835f1173e80fef46f6184f258ebdb5e..581686ba904001b80638efd9ab225c3f68e91e09 100644 --- a/source/dnode/mgmt/CMakeLists.txt +++ b/source/dnode/mgmt/CMakeLists.txt @@ -1,16 +1,17 @@ -add_subdirectory(interface) -add_subdirectory(implement) +add_subdirectory(node_mgmt) +add_subdirectory(node_util) add_subdirectory(mgmt_bnode) add_subdirectory(mgmt_mnode) add_subdirectory(mgmt_qnode) add_subdirectory(mgmt_snode) add_subdirectory(mgmt_vnode) +add_subdirectory(mgmt_dnode) add_subdirectory(test) aux_source_directory(exe EXEC_SRC) add_executable(taosd ${EXEC_SRC}) target_include_directories( taosd - PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/implement/inc" + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/node_mgmt/inc" ) target_link_libraries(taosd dnode) diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index 4a2d02d25d08b8b1f141e688ac1ee61015db3c73..81df34ef4a827ae16b64d5d422e129a0c42df165 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include "dmImp.h" +#include "dmMgmt.h" #include "tconfig.h" #define DM_APOLLO_URL "The apollo string to use when configuring the server, such as: -a 'jsonFile:./tests/cfg.json', cfg.json text can be '{\"fqdn\":\"td1\"}'." @@ -163,14 +163,14 @@ static SDnodeOpt dmGetOpt() { static int32_t dmInitLog() { char logName[12] = {0}; - snprintf(logName, sizeof(logName), "%slog", dmLogName(global.ntype)); + snprintf(logName, sizeof(logName), "%slog", dmNodeLogName(global.ntype)); return taosCreateLog(logName, 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0); } static void dmSetProcInfo(int32_t argc, char **argv) { taosSetProcPath(argc, argv); if (global.ntype != DNODE && global.ntype != NODE_END) { - const char *name = dmProcName(global.ntype); + const char *name = dmNodeProcName(global.ntype); taosSetProcName(argc, argv, name); } } diff --git a/source/dnode/mgmt/implement/inc/dmImp.h b/source/dnode/mgmt/implement/inc/dmImp.h deleted file mode 100644 index 8a1a116ab3ab2693921e0fe6a61132bb9b44bb29..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/implement/inc/dmImp.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DND_IMP_H_ -#define _TD_DND_IMP_H_ - -#include "dmInt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int32_t dmOpenNode(SMgmtWrapper *pWrapper); -void dmCloseNode(SMgmtWrapper *pWrapper); - -// dmTransport.c -int32_t dmInitServer(SDnode *pDnode); -void dmCleanupServer(SDnode *pDnode); -int32_t dmInitClient(SDnode *pDnode); -void dmCleanupClient(SDnode *pDnode); -SProcCfg dmGenProcCfg(SMgmtWrapper *pWrapper); -SMsgCb dmGetMsgcb(SMgmtWrapper *pWrapper); -int32_t dmInitMsgHandle(SDnode *pDnode); -void dmSendRecv(SDnode *pDnode, SEpSet *pEpSet, SRpcMsg *pReq, SRpcMsg *pRsp); -void dmSendToMnodeRecv(SDnode *pDnode, SRpcMsg *pReq, SRpcMsg *pRsp); - -// dmEps.c -int32_t dmReadEps(SDnode *pDnode); -int32_t dmWriteEps(SDnode *pDnode); -void dmUpdateEps(SDnode *pDnode, SArray *pDnodeEps); - -// dmHandle.c -void dmSendStatusReq(SDnode *pDnode); -int32_t dmProcessConfigReq(SDnode *pDnode, SNodeMsg *pMsg); -int32_t dmProcessAuthRsp(SDnode *pDnode, SNodeMsg *pMsg); -int32_t dmProcessGrantRsp(SDnode *pDnode, SNodeMsg *pMsg); -int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg); -int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg); - -// dmMonitor.c -void dmGetVnodeLoads(SDnode *pDnode, SMonVloadInfo *pInfo); -void dmGetMnodeLoads(SDnode *pDnode, SMonMloadInfo *pInfo); -void dmSendMonitorReport(SDnode *pDnode); - -// dmWorker.c -int32_t dmStartStatusThread(SDnode *pDnode); -void dmStopStatusThread(SDnode *pDnode); -int32_t dmStartMonitorThread(SDnode *pDnode); -void dmStopMonitorThread(SDnode *pDnode); -int32_t dmStartWorker(SDnode *pDnode); -void dmStopWorker(SDnode *pDnode); -int32_t dmProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t dmProcessStatusMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); - -// mgmt nodes -void dmSetMgmtFp(SMgmtWrapper *pWrapper); -void bmSetMgmtFp(SMgmtWrapper *pWrapper); -void qmSetMgmtFp(SMgmtWrapper *pWrapper); -void smSetMgmtFp(SMgmtWrapper *pWrapper); -void vmSetMgmtFp(SMgmtWrapper *pWrapper); -void mmSetMgmtFp(SMgmtWrapper *pWrapper); - -void vmGetVnodeLoads(SMgmtWrapper *pWrapper, SMonVloadInfo *pInfo); -void mmGetMnodeLoads(SMgmtWrapper *pWrapper, SMonMloadInfo *pInfo); -void mmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonMmInfo *mmInfo); -void vmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonVmInfo *vmInfo); -void qmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonQmInfo *qmInfo); -void smGetMonitorInfo(SMgmtWrapper *pWrapper, SMonSmInfo *smInfo); -void bmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonBmInfo *bmInfo); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DND_IMP_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/implement/src/dmHandle.c b/source/dnode/mgmt/implement/src/dmHandle.c deleted file mode 100644 index 097d18679b0a3bd99a6e61ad2d2d935b2ac1c3cb..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/implement/src/dmHandle.c +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "dmImp.h" - -static void dmUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) { - if (pDnode->data.dnodeId == 0 || pDnode->data.clusterId == 0) { - dInfo("set dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId); - taosWLockLatch(&pDnode->data.latch); - pDnode->data.dnodeId = pCfg->dnodeId; - pDnode->data.clusterId = pCfg->clusterId; - dmWriteEps(pDnode); - taosWUnLockLatch(&pDnode->data.latch); - } -} - -static int32_t dmProcessStatusRsp(SDnode *pDnode, SRpcMsg *pRsp) { - if (pRsp->code != TSDB_CODE_SUCCESS) { - if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pDnode->data.dropped && pDnode->data.dnodeId > 0) { - dInfo("dnode:%d, set to dropped since not exist in mnode", pDnode->data.dnodeId); - pDnode->data.dropped = 1; - dmWriteEps(pDnode); - } - } else { - SStatusRsp statusRsp = {0}; - if (pRsp->pCont != NULL && pRsp->contLen > 0 && - tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) { - pDnode->data.dnodeVer = statusRsp.dnodeVer; - dmUpdateDnodeCfg(pDnode, &statusRsp.dnodeCfg); - dmUpdateEps(pDnode, statusRsp.pDnodeEps); - } - rpcFreeCont(pRsp->pCont); - tFreeSStatusRsp(&statusRsp); - } - - return TSDB_CODE_SUCCESS; -} - -void dmSendStatusReq(SDnode *pDnode) { - SStatusReq req = {0}; - - taosRLockLatch(&pDnode->data.latch); - req.sver = tsVersion; - req.dnodeVer = pDnode->data.dnodeVer; - req.dnodeId = pDnode->data.dnodeId; - req.clusterId = pDnode->data.clusterId; - if (req.clusterId == 0) req.dnodeId = 0; - req.rebootTime = pDnode->data.rebootTime; - req.updateTime = pDnode->data.updateTime; - req.numOfCores = tsNumOfCores; - req.numOfSupportVnodes = pDnode->data.supportVnodes; - tstrncpy(req.dnodeEp, pDnode->data.localEp, TSDB_EP_LEN); - - req.clusterCfg.statusInterval = tsStatusInterval; - req.clusterCfg.checkTime = 0; - char timestr[32] = "1970-01-01 00:00:00.00"; - (void)taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); - memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN); - memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN); - memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN); - taosRUnLockLatch(&pDnode->data.latch); - - SMonVloadInfo vinfo = {0}; - dmGetVnodeLoads(pDnode, &vinfo); - req.pVloads = vinfo.pVloads; - pDnode->data.unsyncedVgId = 0; - pDnode->data.vndState = TAOS_SYNC_STATE_LEADER; - for (int32_t i = 0; i < taosArrayGetSize(req.pVloads); ++i) { - SVnodeLoad *pLoad = taosArrayGet(req.pVloads, i); - if (pLoad->syncState != TAOS_SYNC_STATE_LEADER && pLoad->syncState != TAOS_SYNC_STATE_FOLLOWER) { - pDnode->data.unsyncedVgId = pLoad->vgId; - pDnode->data.vndState = pLoad->syncState; - } - } - - SMonMloadInfo minfo = {0}; - dmGetMnodeLoads(pDnode, &minfo); - pDnode->data.isMnode = minfo.isMnode; - pDnode->data.mndState = minfo.load.syncState; - - int32_t contLen = tSerializeSStatusReq(NULL, 0, &req); - void *pHead = rpcMallocCont(contLen); - tSerializeSStatusReq(pHead, contLen, &req); - tFreeSStatusReq(&req); - - SRpcMsg rpcMsg = {.pCont = pHead, .contLen = contLen, .msgType = TDMT_MND_STATUS, .ahandle = (void *)0x9527}; - SRpcMsg rpcRsp = {0}; - - dTrace("send req:%s to mnode, app:%p", TMSG_INFO(rpcMsg.msgType), rpcMsg.ahandle); - dmSendToMnodeRecv(pDnode, &rpcMsg, &rpcRsp); - dmProcessStatusRsp(pDnode, &rpcRsp); -} - -int32_t dmProcessAuthRsp(SDnode *pDnode, SNodeMsg *pMsg) { - SRpcMsg *pRsp = &pMsg->rpcMsg; - dError("auth rsp is received, but not supported yet"); - return 0; -} - -int32_t dmProcessGrantRsp(SDnode *pDnode, SNodeMsg *pMsg) { - SRpcMsg *pRsp = &pMsg->rpcMsg; - dError("grant rsp is received, but not supported yet"); - return 0; -} - -int32_t dmProcessConfigReq(SDnode *pDnode, SNodeMsg *pMsg) { - SRpcMsg *pReq = &pMsg->rpcMsg; - SDCfgDnodeReq *pCfg = pReq->pCont; - dError("config req is received, but not supported yet"); - return TSDB_CODE_OPS_NOT_SUPPORT; -} - -int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) { - SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype); - if (pWrapper != NULL) { - dmReleaseWrapper(pWrapper); - terrno = TSDB_CODE_NODE_ALREADY_DEPLOYED; - dError("failed to create node since %s", terrstr()); - return -1; - } - - taosThreadMutexLock(&pDnode->mutex); - pWrapper = &pDnode->wrappers[ntype]; - - if (taosMkDir(pWrapper->path) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - dError("failed to create dir:%s since %s", pWrapper->path, terrstr()); - return -1; - } - - int32_t code = (*pWrapper->fp.createFp)(pWrapper, pMsg); - if (code != 0) { - dError("node:%s, failed to create since %s", pWrapper->name, terrstr()); - } else { - dDebug("node:%s, has been created", pWrapper->name); - (void)dmOpenNode(pWrapper); - pWrapper->required = true; - pWrapper->deployed = true; - pWrapper->procType = pDnode->ptype; - } - - taosThreadMutexUnlock(&pDnode->mutex); - return code; -} - -int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) { - SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype); - if (pWrapper == NULL) { - terrno = TSDB_CODE_NODE_NOT_DEPLOYED; - dError("failed to drop node since %s", terrstr()); - return -1; - } - - taosThreadMutexLock(&pDnode->mutex); - - int32_t code = (*pWrapper->fp.dropFp)(pWrapper, pMsg); - if (code != 0) { - dError("node:%s, failed to drop since %s", pWrapper->name, terrstr()); - } else { - dDebug("node:%s, has been dropped", pWrapper->name); - pWrapper->required = false; - pWrapper->deployed = false; - } - - dmReleaseWrapper(pWrapper); - - if (code == 0) { - dmCloseNode(pWrapper); - taosRemoveDir(pWrapper->path); - } - taosThreadMutexUnlock(&pDnode->mutex); - return code; -} - -static void dmSetMgmtMsgHandle(SMgmtWrapper *pWrapper) { - // Requests handled by DNODE - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_MNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_MNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_QNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_QNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_SNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_SNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_BNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_BNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CONFIG_DNODE, dmProcessMgmtMsg, DEFAULT_HANDLE); - - // Requests handled by MNODE - dmSetMsgHandle(pWrapper, TDMT_MND_GRANT_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_AUTH_RSP, dmProcessMgmtMsg, DEFAULT_HANDLE); -} - -static int32_t dmStartMgmt(SMgmtWrapper *pWrapper) { - if (dmStartStatusThread(pWrapper->pDnode) != 0) { - return -1; - } - if (dmStartMonitorThread(pWrapper->pDnode) != 0) { - return -1; - } - return 0; -} - -static void dmStopMgmt(SMgmtWrapper *pWrapper) { - dmStopMonitorThread(pWrapper->pDnode); - dmStopStatusThread(pWrapper->pDnode); -} - -static int32_t dmInitMgmt(SMgmtWrapper *pWrapper) { - dInfo("dnode-mgmt start to init"); - SDnode *pDnode = pWrapper->pDnode; - - pDnode->data.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); - if (pDnode->data.dnodeHash == NULL) { - dError("failed to init dnode hash"); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } - - if (dmReadEps(pDnode) != 0) { - dError("failed to read file since %s", terrstr()); - return -1; - } - - if (pDnode->data.dropped) { - dError("dnode will not start since its already dropped"); - return -1; - } - - if (dmStartWorker(pDnode) != 0) { - return -1; - } - - if (dmInitServer(pDnode) != 0) { - dError("failed to init transport since %s", terrstr()); - return -1; - } - dmReportStartup(pDnode, "dnode-transport", "initialized"); - - if (udfStartUdfd(pDnode->data.dnodeId) != 0) { - dError("failed to start udfd"); - } - - dInfo("dnode-mgmt is initialized"); - return 0; -} - -static void dmCleanupMgmt(SMgmtWrapper *pWrapper) { - dInfo("dnode-mgmt start to clean up"); - SDnode *pDnode = pWrapper->pDnode; - - udfStopUdfd(); - - dmStopWorker(pDnode); - - taosWLockLatch(&pDnode->data.latch); - if (pDnode->data.dnodeEps != NULL) { - taosArrayDestroy(pDnode->data.dnodeEps); - pDnode->data.dnodeEps = NULL; - } - if (pDnode->data.dnodeHash != NULL) { - taosHashCleanup(pDnode->data.dnodeHash); - pDnode->data.dnodeHash = NULL; - } - taosWUnLockLatch(&pDnode->data.latch); - - dmCleanupClient(pDnode); - dmCleanupServer(pDnode); - dInfo("dnode-mgmt is cleaned up"); -} - -static int32_t dmRequireMgmt(SMgmtWrapper *pWrapper, bool *required) { - *required = true; - return 0; -} - -void dmSetMgmtFp(SMgmtWrapper *pWrapper) { - SMgmtFp mgmtFp = {0}; - mgmtFp.openFp = dmInitMgmt; - mgmtFp.closeFp = dmCleanupMgmt; - mgmtFp.startFp = dmStartMgmt; - mgmtFp.stopFp = dmStopMgmt; - mgmtFp.requiredFp = dmRequireMgmt; - - dmSetMgmtMsgHandle(pWrapper); - pWrapper->name = "dnode"; - pWrapper->fp = mgmtFp; -} diff --git a/source/dnode/mgmt/implement/src/dmMonitor.c b/source/dnode/mgmt/implement/src/dmMonitor.c deleted file mode 100644 index 8543310eb54479fe4edd331b8d38b840892cac20..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/implement/src/dmMonitor.c +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "dmImp.h" - -static void dmGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { - pInfo->protocol = 1; - pInfo->dnode_id = pDnode->data.dnodeId; - pInfo->cluster_id = pDnode->data.clusterId; - tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); -} - -static void dmGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { - pInfo->uptime = (taosGetTimestampMs() - pDnode->data.rebootTime) / (86400000.0f); - pInfo->has_mnode = pDnode->wrappers[MNODE].required; - pInfo->has_qnode = pDnode->wrappers[QNODE].required; - pInfo->has_snode = pDnode->wrappers[SNODE].required; - pInfo->has_bnode = pDnode->wrappers[BNODE].required; - tstrncpy(pInfo->logdir.name, tsLogDir, sizeof(pInfo->logdir.name)); - pInfo->logdir.size = tsLogSpace.size; - tstrncpy(pInfo->tempdir.name, tsTempDir, sizeof(pInfo->tempdir.name)); - pInfo->tempdir.size = tsTempSpace.size; -} - -static void dmGetMonitorInfo(SDnode *pDnode, SMonDmInfo *pInfo) { - dmGetMonitorBasicInfo(pDnode, &pInfo->basic); - dmGetMonitorSysInfo(&pInfo->sys); - dmGetMonitorDnodeInfo(pDnode, &pInfo->dnode); -} - -void dmSendMonitorReport(SDnode *pDnode) { - if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; - dTrace("send monitor report to %s:%u", tsMonitorFqdn, tsMonitorPort); - - SMonDmInfo dmInfo = {0}; - SMonMmInfo mmInfo = {0}; - SMonVmInfo vmInfo = {0}; - SMonQmInfo qmInfo = {0}; - SMonSmInfo smInfo = {0}; - SMonBmInfo bmInfo = {0}; - - SRpcMsg req = {0}; - SRpcMsg rsp; - SEpSet epset = {.inUse = 0, .numOfEps = 1}; - tstrncpy(epset.eps[0].fqdn, pDnode->data.localFqdn, TSDB_FQDN_LEN); - epset.eps[0].port = tsServerPort; - - SMgmtWrapper *pWrapper = NULL; - dmGetMonitorInfo(pDnode, &dmInfo); - - bool getFromAPI = !tsMultiProcess; - pWrapper = &pDnode->wrappers[MNODE]; - if (getFromAPI) { - if (dmMarkWrapper(pWrapper) == 0) { - mmGetMonitorInfo(pWrapper, &mmInfo); - dmReleaseWrapper(pWrapper); - } - } else { - if (pWrapper->required) { - req.msgType = TDMT_MON_MM_INFO; - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonMmInfo(rsp.pCont, rsp.contLen, &mmInfo); - } - rpcFreeCont(rsp.pCont); - } - } - - pWrapper = &pDnode->wrappers[VNODE]; - if (getFromAPI) { - if (dmMarkWrapper(pWrapper) == 0) { - vmGetMonitorInfo(pWrapper, &vmInfo); - dmReleaseWrapper(pWrapper); - } - } else { - if (pWrapper->required) { - req.msgType = TDMT_MON_VM_INFO; - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonVmInfo(rsp.pCont, rsp.contLen, &vmInfo); - } - rpcFreeCont(rsp.pCont); - } - } - - pWrapper = &pDnode->wrappers[QNODE]; - if (getFromAPI) { - if (dmMarkWrapper(pWrapper) == 0) { - qmGetMonitorInfo(pWrapper, &qmInfo); - dmReleaseWrapper(pWrapper); - } - } else { - if (pWrapper->required) { - req.msgType = TDMT_MON_QM_INFO; - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonQmInfo(rsp.pCont, rsp.contLen, &qmInfo); - } - rpcFreeCont(rsp.pCont); - } - } - - pWrapper = &pDnode->wrappers[SNODE]; - if (getFromAPI) { - if (dmMarkWrapper(pWrapper) == 0) { - smGetMonitorInfo(pWrapper, &smInfo); - dmReleaseWrapper(pWrapper); - } - } else { - if (pWrapper->required) { - req.msgType = TDMT_MON_SM_INFO; - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonSmInfo(rsp.pCont, rsp.contLen, &smInfo); - } - rpcFreeCont(rsp.pCont); - } - } - - pWrapper = &pDnode->wrappers[BNODE]; - if (getFromAPI) { - if (dmMarkWrapper(pWrapper) == 0) { - bmGetMonitorInfo(pWrapper, &bmInfo); - dmReleaseWrapper(pWrapper); - } - } else { - if (pWrapper->required) { - req.msgType = TDMT_MON_BM_INFO; - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonBmInfo(rsp.pCont, rsp.contLen, &bmInfo); - } - rpcFreeCont(rsp.pCont); - } - } - - monSetDmInfo(&dmInfo); - monSetMmInfo(&mmInfo); - monSetVmInfo(&vmInfo); - monSetQmInfo(&qmInfo); - monSetSmInfo(&smInfo); - monSetBmInfo(&bmInfo); - tFreeSMonMmInfo(&mmInfo); - tFreeSMonVmInfo(&vmInfo); - tFreeSMonQmInfo(&qmInfo); - tFreeSMonSmInfo(&smInfo); - tFreeSMonBmInfo(&bmInfo); - monSendReport(); -} - -void dmGetVnodeLoads(SDnode *pDnode, SMonVloadInfo *pInfo) { - SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE); - if (pWrapper == NULL) return; - - bool getFromAPI = !tsMultiProcess; - if (getFromAPI) { - vmGetVnodeLoads(pWrapper, pInfo); - } else { - SRpcMsg req = {.msgType = TDMT_MON_VM_LOAD}; - SRpcMsg rsp = {0}; - SEpSet epset = {.inUse = 0, .numOfEps = 1}; - tstrncpy(epset.eps[0].fqdn, pDnode->data.localFqdn, TSDB_FQDN_LEN); - epset.eps[0].port = tsServerPort; - - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonVloadInfo(rsp.pCont, rsp.contLen, pInfo); - } - rpcFreeCont(rsp.pCont); - } - dmReleaseWrapper(pWrapper); -} - -void dmGetMnodeLoads(SDnode *pDnode, SMonMloadInfo *pInfo) { - SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, MNODE); - if (pWrapper == NULL) { - pInfo->isMnode = 0; - return; - } - - bool getFromAPI = !tsMultiProcess; - if (getFromAPI) { - mmGetMnodeLoads(pWrapper, pInfo); - } else { - SRpcMsg req = {.msgType = TDMT_MON_MM_LOAD}; - SRpcMsg rsp = {0}; - SEpSet epset = {.inUse = 0, .numOfEps = 1}; - tstrncpy(epset.eps[0].fqdn, pDnode->data.localFqdn, TSDB_FQDN_LEN); - epset.eps[0].port = tsServerPort; - - dmSendRecv(pDnode, &epset, &req, &rsp); - if (rsp.code == 0 && rsp.contLen > 0) { - tDeserializeSMonMloadInfo(rsp.pCont, rsp.contLen, pInfo); - } - rpcFreeCont(rsp.pCont); - } - dmReleaseWrapper(pWrapper); -} diff --git a/source/dnode/mgmt/implement/src/dmObj.c b/source/dnode/mgmt/implement/src/dmObj.c deleted file mode 100644 index a43439d4658beda0465e5a3c2179d7e71e755395..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/implement/src/dmObj.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "dmImp.h" - -static int32_t dmInitVars(SDnode *pDnode, const SDnodeOpt *pOption) { - pDnode->data.dnodeId = 0; - pDnode->data.clusterId = 0; - pDnode->data.dnodeVer = 0; - pDnode->data.updateTime = 0; - pDnode->data.rebootTime = taosGetTimestampMs(); - pDnode->data.dropped = 0; - pDnode->data.localEp = strdup(pOption->localEp); - pDnode->data.localFqdn = strdup(pOption->localFqdn); - pDnode->data.firstEp = strdup(pOption->firstEp); - pDnode->data.secondEp = strdup(pOption->secondEp); - pDnode->data.dataDir = strdup(pOption->dataDir); - pDnode->data.disks = pOption->disks; - pDnode->data.numOfDisks = pOption->numOfDisks; - pDnode->data.supportVnodes = pOption->numOfSupportVnodes; - pDnode->data.serverPort = pOption->serverPort; - pDnode->ntype = pOption->ntype; - - if (pDnode->data.dataDir == NULL || pDnode->data.localEp == NULL || pDnode->data.localFqdn == NULL || - pDnode->data.firstEp == NULL || pDnode->data.secondEp == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } - - if (!tsMultiProcess || pDnode->ntype == DNODE || pDnode->ntype == NODE_END) { - pDnode->data.lockfile = dmCheckRunning(pDnode->data.dataDir); - if (pDnode->data.lockfile == NULL) { - return -1; - } - } - - taosInitRWLatch(&pDnode->data.latch); - taosThreadMutexInit(&pDnode->mutex, NULL); - return 0; -} - -static void dmClearVars(SDnode *pDnode) { - for (EDndNodeType n = DNODE; n < NODE_END; ++n) { - SMgmtWrapper *pMgmt = &pDnode->wrappers[n]; - taosMemoryFreeClear(pMgmt->path); - } - if (pDnode->data.lockfile != NULL) { - taosUnLockFile(pDnode->data.lockfile); - taosCloseFile(&pDnode->data.lockfile); - pDnode->data.lockfile = NULL; - } - taosMemoryFreeClear(pDnode->data.localEp); - taosMemoryFreeClear(pDnode->data.localFqdn); - taosMemoryFreeClear(pDnode->data.firstEp); - taosMemoryFreeClear(pDnode->data.secondEp); - taosMemoryFreeClear(pDnode->data.dataDir); - taosThreadMutexDestroy(&pDnode->mutex); - memset(&pDnode->mutex, 0, sizeof(pDnode->mutex)); - taosMemoryFree(pDnode); - dDebug("dnode memory is cleared, data:%p", pDnode); -} - -SDnode *dmCreate(const SDnodeOpt *pOption) { - dDebug("start to create dnode"); - int32_t code = -1; - char path[PATH_MAX] = {0}; - SDnode *pDnode = NULL; - - pDnode = taosMemoryCalloc(1, sizeof(SDnode)); - if (pDnode == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; - } - - if (dmInitVars(pDnode, pOption) != 0) { - dError("failed to init variables since %s", terrstr()); - goto _OVER; - } - - dmSetStatus(pDnode, DND_STAT_INIT); - dmSetMgmtFp(&pDnode->wrappers[DNODE]); - mmSetMgmtFp(&pDnode->wrappers[MNODE]); - vmSetMgmtFp(&pDnode->wrappers[VNODE]); - qmSetMgmtFp(&pDnode->wrappers[QNODE]); - smSetMgmtFp(&pDnode->wrappers[SNODE]); - bmSetMgmtFp(&pDnode->wrappers[BNODE]); - - for (EDndNodeType n = DNODE; n < NODE_END; ++n) { - SMgmtWrapper *pWrapper = &pDnode->wrappers[n]; - snprintf(path, sizeof(path), "%s%s%s", pDnode->data.dataDir, TD_DIRSEP, pWrapper->name); - pWrapper->path = strdup(path); - pWrapper->procShm.id = -1; - pWrapper->pDnode = pDnode; - pWrapper->ntype = n; - pWrapper->procType = DND_PROC_SINGLE; - taosInitRWLatch(&pWrapper->latch); - - if (pWrapper->path == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; - } - - if (n != DNODE && dmReadShmFile(pWrapper) != 0) { - dError("node:%s, failed to read shm file since %s", pWrapper->name, terrstr()); - goto _OVER; - } - } - - if (dmInitMsgHandle(pDnode) != 0) { - dError("failed to init msg handles since %s", terrstr()); - goto _OVER; - } - - dInfo("dnode is created, data:%p", pDnode); - code = 0; - -_OVER: - if (code != 0 && pDnode) { - dmClearVars(pDnode); - pDnode = NULL; - dError("failed to create dnode since %s", terrstr()); - } - - return pDnode; -} - -void dmClose(SDnode *pDnode) { - if (pDnode == NULL) return; - dmClearVars(pDnode); - dInfo("dnode is closed, data:%p", pDnode); -} diff --git a/source/dnode/mgmt/implement/src/dmWorker.c b/source/dnode/mgmt/implement/src/dmWorker.c deleted file mode 100644 index 72b21115918641a7962619118ff7487f22cad126..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/implement/src/dmWorker.c +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "dmImp.h" - -static void *dmStatusThreadFp(void *param) { - SDnode *pDnode = param; - int64_t lastTime = taosGetTimestampMs(); - - setThreadName("dnode-status"); - - while (1) { - taosThreadTestCancel(); - taosMsleep(200); - - if (pDnode->status != DND_STAT_RUNNING || pDnode->data.dropped) { - continue; - } - - int64_t curTime = taosGetTimestampMs(); - float interval = (curTime - lastTime) / 1000.0f; - if (interval >= tsStatusInterval) { - dmSendStatusReq(pDnode); - lastTime = curTime; - } - } - - return NULL; -} - -static void *dmMonitorThreadFp(void *param) { - SDnode *pDnode = param; - int64_t lastTime = taosGetTimestampMs(); - - setThreadName("dnode-monitor"); - - while (1) { - taosThreadTestCancel(); - taosMsleep(200); - - if (pDnode->status != DND_STAT_RUNNING || pDnode->data.dropped) { - continue; - } - - int64_t curTime = taosGetTimestampMs(); - float interval = (curTime - lastTime) / 1000.0f; - if (interval >= tsMonitorInterval) { - dmSendMonitorReport(pDnode); - lastTime = curTime; - } - } - - return NULL; -} - -int32_t dmStartStatusThread(SDnode *pDnode) { - pDnode->data.statusThreadId = taosCreateThread(dmStatusThreadFp, pDnode); - if (pDnode->data.statusThreadId == NULL) { - dError("failed to init dnode status thread"); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } - - dmReportStartup(pDnode, "dnode-status", "initialized"); - return 0; -} - -void dmStopStatusThread(SDnode *pDnode) { - if (pDnode->data.statusThreadId != NULL) { - taosDestoryThread(pDnode->data.statusThreadId); - pDnode->data.statusThreadId = NULL; - } -} - -int32_t dmStartMonitorThread(SDnode *pDnode) { - pDnode->data.monitorThreadId = taosCreateThread(dmMonitorThreadFp, pDnode); - if (pDnode->data.monitorThreadId == NULL) { - dError("failed to init dnode monitor thread"); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } - - dmReportStartup(pDnode, "dnode-monitor", "initialized"); - return 0; -} - -void dmStopMonitorThread(SDnode *pDnode) { - if (pDnode->data.monitorThreadId != NULL) { - taosDestoryThread(pDnode->data.monitorThreadId); - pDnode->data.monitorThreadId = NULL; - } -} - -static void dmProcessMgmtQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { - SDnode *pDnode = pInfo->ahandle; - - int32_t code = -1; - tmsg_t msgType = pMsg->rpcMsg.msgType; - dTrace("msg:%p, will be processed in dnode-mgmt queue", pMsg); - - switch (msgType) { - case TDMT_DND_CONFIG_DNODE: - code = dmProcessConfigReq(pDnode, pMsg); - break; - case TDMT_MND_AUTH_RSP: - code = dmProcessAuthRsp(pDnode, pMsg); - break; - case TDMT_MND_GRANT_RSP: - code = dmProcessGrantRsp(pDnode, pMsg); - break; - case TDMT_DND_CREATE_MNODE: - code = dmProcessCreateNodeReq(pDnode, MNODE, pMsg); - break; - case TDMT_DND_DROP_MNODE: - code = dmProcessDropNodeReq(pDnode, MNODE, pMsg); - break; - case TDMT_DND_CREATE_QNODE: - code = dmProcessCreateNodeReq(pDnode, QNODE, pMsg); - break; - case TDMT_DND_DROP_QNODE: - code = dmProcessDropNodeReq(pDnode, QNODE, pMsg); - break; - case TDMT_DND_CREATE_SNODE: - code = dmProcessCreateNodeReq(pDnode, SNODE, pMsg); - break; - case TDMT_DND_DROP_SNODE: - code = dmProcessDropNodeReq(pDnode, SNODE, pMsg); - break; - case TDMT_DND_CREATE_BNODE: - code = dmProcessCreateNodeReq(pDnode, BNODE, pMsg); - break; - case TDMT_DND_DROP_BNODE: - code = dmProcessDropNodeReq(pDnode, BNODE, pMsg); - break; - default: - break; - } - - if (msgType & 1u) { - if (code != 0 && terrno != 0) code = terrno; - SRpcMsg rsp = { - .handle = pMsg->rpcMsg.handle, - .ahandle = pMsg->rpcMsg.ahandle, - .code = code, - .refId = pMsg->rpcMsg.refId, - }; - rpcSendResponse(&rsp); - } - - dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code)); - rpcFreeCont(pMsg->rpcMsg.pCont); - taosFreeQitem(pMsg); -} - -int32_t dmStartWorker(SDnode *pDnode) { - SSingleWorkerCfg cfg = { - .min = 1, - .max = 1, - .name = "dnode-mgmt", - .fp = (FItem)dmProcessMgmtQueue, - .param = pDnode, - }; - if (tSingleWorkerInit(&pDnode->data.mgmtWorker, &cfg) != 0) { - dError("failed to start dnode-mgmt worker since %s", terrstr()); - return -1; - } - - dDebug("dnode workers are initialized"); - return 0; -} - -void dmStopWorker(SDnode *pDnode) { - tSingleWorkerCleanup(&pDnode->data.mgmtWorker); - dDebug("dnode workers are closed"); -} - -int32_t dmProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SSingleWorker *pWorker = &pWrapper->pDnode->data.mgmtWorker; - dTrace("msg:%p, put into worker %s", pMsg, pWorker->name); - taosWriteQitem(pWorker->queue, pMsg); - return 0; -} diff --git a/source/dnode/mgmt/interface/CMakeLists.txt b/source/dnode/mgmt/interface/CMakeLists.txt deleted file mode 100644 index a99fc2703d468197149d95fbad23fdcfd5066650..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/interface/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -aux_source_directory(src DNODE_INTERFACE) -add_library(dnode_interface STATIC ${DNODE_INTERFACE}) -target_include_directories( - dnode_interface - PUBLIC "${TD_SOURCE_DIR}/include/dnode/mgmt" - PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) -target_link_libraries( - dnode_interface cjson mnode vnode qnode snode bnode wal sync taos_static tfs monitor -) \ No newline at end of file diff --git a/source/dnode/mgmt/interface/inc/dmDef.h b/source/dnode/mgmt/interface/inc/dmDef.h deleted file mode 100644 index 445e1d42f5d6bd96d2fe93aac84d6e24683313f5..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/interface/inc/dmDef.h +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DM_DEF_H_ -#define _TD_DM_DEF_H_ - -#include "uv.h" -#include "dmLog.h" - -#include "cJSON.h" -#include "tcache.h" -#include "tcrc32c.h" -#include "tdatablock.h" -#include "tglobal.h" -#include "thash.h" -#include "tlockfree.h" -#include "tlog.h" -#include "tmsg.h" -#include "tmsgcb.h" -#include "tprocess.h" -#include "tqueue.h" -#include "trpc.h" -#include "tthread.h" -#include "ttime.h" -#include "tworker.h" - -#include "dnode.h" -#include "mnode.h" -#include "monitor.h" -#include "sync.h" - -#include "libs/function/function.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { DNODE, VNODE, QNODE, SNODE, MNODE, BNODE, NODE_END } EDndNodeType; -typedef enum { DND_STAT_INIT, DND_STAT_RUNNING, DND_STAT_STOPPED } EDndRunStatus; -typedef enum { DND_ENV_INIT, DND_ENV_READY, DND_ENV_CLEANUP } EDndEnvStatus; -typedef enum { DND_PROC_SINGLE, DND_PROC_CHILD, DND_PROC_PARENT } EDndProcType; - -typedef int32_t (*NodeMsgFp)(struct SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -typedef int32_t (*OpenNodeFp)(struct SMgmtWrapper *pWrapper); -typedef void (*CloseNodeFp)(struct SMgmtWrapper *pWrapper); -typedef int32_t (*StartNodeFp)(struct SMgmtWrapper *pWrapper); -typedef void (*StopNodeFp)(struct SMgmtWrapper *pWrapper); -typedef int32_t (*CreateNodeFp)(struct SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -typedef int32_t (*DropNodeFp)(struct SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -typedef int32_t (*RequireNodeFp)(struct SMgmtWrapper *pWrapper, bool *required); - -typedef struct { - SMgmtWrapper *pQndWrapper; - SMgmtWrapper *pMndWrapper; - SMgmtWrapper *pNdWrapper; -} SMsgHandle; - -typedef struct { - OpenNodeFp openFp; - CloseNodeFp closeFp; - StartNodeFp startFp; - StopNodeFp stopFp; - CreateNodeFp createFp; - DropNodeFp dropFp; - RequireNodeFp requiredFp; -} SMgmtFp; - -typedef struct SMgmtWrapper { - SDnode *pDnode; - struct { - const char *name; - char *path; - int32_t refCount; - SRWLatch latch; - EDndNodeType ntype; - bool deployed; - bool required; - SMgmtFp fp; - void *pMgmt; - }; - struct { - EDndProcType procType; - int32_t procId; - SProcObj *procObj; - SShm procShm; - }; - struct { - int8_t msgVgIds[TDMT_MAX]; // Handle the case where the same message type is distributed to qnode or vnode - NodeMsgFp msgFps[TDMT_MAX]; - }; -} SMgmtWrapper; - -typedef struct { - void *serverRpc; - void *clientRpc; - SMsgHandle msgHandles[TDMT_MAX]; -} SDnodeTrans; - -typedef struct { - int32_t dnodeId; - int64_t clusterId; - int64_t dnodeVer; - int64_t updateTime; - int64_t rebootTime; - int32_t unsyncedVgId; - ESyncState vndState; - ESyncState mndState; - bool isMnode; - bool dropped; - SEpSet mnodeEps; - SArray *dnodeEps; - SHashObj *dnodeHash; - TdThread *statusThreadId; - TdThread *monitorThreadId; - SRWLatch latch; - SSingleWorker mgmtWorker; - SMsgCb msgCb; - SDnode *pDnode; - TdFilePtr lockfile; - char *localEp; - char *localFqdn; - char *firstEp; - char *secondEp; - char *dataDir; - SDiskCfg *disks; - int32_t numOfDisks; - int32_t supportVnodes; - uint16_t serverPort; -} SDnodeData; - -typedef struct { - char name[TSDB_STEP_NAME_LEN]; - char desc[TSDB_STEP_DESC_LEN]; -} SStartupInfo; - -typedef struct SUdfdData { - bool startCalled; - bool needCleanUp; - uv_loop_t loop; - uv_thread_t thread; - uv_barrier_t barrier; - uv_process_t process; - int spawnErr; - uv_pipe_t ctrlPipe; - uv_async_t stopAsync; - int32_t stopCalled; - - int32_t dnodeId; -} SUdfdData; - -typedef struct SDnode { - EDndProcType ptype; - EDndNodeType ntype; - EDndRunStatus status; - EDndEvent event; - SStartupInfo startup; - SDnodeTrans trans; - SDnodeData data; - SUdfdData udfdData; - TdThreadMutex mutex; - SMgmtWrapper wrappers[NODE_END]; -} SDnode; - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DM_DEF_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/interface/inc/dmInt.h b/source/dnode/mgmt/interface/inc/dmInt.h deleted file mode 100644 index b56edd26300f19f39551a7f6911d8c7260c34171..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/interface/inc/dmInt.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DM_INT_H_ -#define _TD_DM_INT_H_ - -#include "dmDef.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// dmInt.c -SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType nType); -int32_t dmMarkWrapper(SMgmtWrapper *pWrapper); -void dmReleaseWrapper(SMgmtWrapper *pWrapper); -const char *dmStatName(EDndRunStatus stat); -const char *dmLogName(EDndNodeType ntype); -const char *dmProcName(EDndNodeType ntype); -const char *dmEventName(EDndEvent ev); - -void dmSetStatus(SDnode *pDnode, EDndRunStatus stat); -void dmSetEvent(SDnode *pDnode, EDndEvent event); -void dmSetMsgHandle(SMgmtWrapper *pWrapper, tmsg_t msgType, NodeMsgFp nodeMsgFp, int8_t vgId); -void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc); -void dmReportStartupByWrapper(SMgmtWrapper *pWrapper, const char *pName, const char *pDesc); -void dmProcessServerStatusReq(SDnode *pDnode, SRpcMsg *pMsg); -void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pMsg); -void dmGetMonitorSysInfo(SMonSysInfo *pInfo); - -// dmFile.c -int32_t dmReadFile(SMgmtWrapper *pWrapper, bool *pDeployed); -int32_t dmWriteFile(SMgmtWrapper *pWrapper, bool deployed); -TdFilePtr dmCheckRunning(const char *dataDir); -int32_t dmReadShmFile(SMgmtWrapper *pWrapper); -int32_t dmWriteShmFile(SMgmtWrapper *pWrapper); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DM_INT_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/interface/inc/dmLog.h b/source/dnode/mgmt/interface/inc/dmLog.h deleted file mode 100644 index c21933fc01fd19624f1b763e4191ce4456411494..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/interface/inc/dmLog.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DM_LOG_H_ -#define _TD_DM_LOG_H_ - -#include "tlog.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define dFatal(...) { if (dDebugFlag & DEBUG_FATAL) { taosPrintLog("DND FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} -#define dError(...) { if (dDebugFlag & DEBUG_ERROR) { taosPrintLog("DND ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} -#define dWarn(...) { if (dDebugFlag & DEBUG_WARN) { taosPrintLog("DND WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} -#define dInfo(...) { if (dDebugFlag & DEBUG_INFO) { taosPrintLog("DND ", DEBUG_INFO, 255, __VA_ARGS__); }} -#define dDebug(...) { if (dDebugFlag & DEBUG_DEBUG) { taosPrintLog("DND ", DEBUG_DEBUG, dDebugFlag, __VA_ARGS__); }} -#define dTrace(...) { if (dDebugFlag & DEBUG_TRACE) { taosPrintLog("DND ", DEBUG_TRACE, dDebugFlag, __VA_ARGS__); }} - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DM_LOG_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/interface/src/dmInt.c b/source/dnode/mgmt/interface/src/dmInt.c deleted file mode 100644 index f8e23ad26212c093ab11b1c83fea085094156ea5..0000000000000000000000000000000000000000 --- a/source/dnode/mgmt/interface/src/dmInt.c +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "dmInt.h" - -const char *dmStatName(EDndRunStatus status) { - switch (status) { - case DND_STAT_INIT: - return "init"; - case DND_STAT_RUNNING: - return "running"; - case DND_STAT_STOPPED: - return "stopped"; - default: - return "UNKNOWN"; - } -} - -const char *dmLogName(EDndNodeType ntype) { - switch (ntype) { - case VNODE: - return "vnode"; - case QNODE: - return "qnode"; - case SNODE: - return "snode"; - case MNODE: - return "mnode"; - case BNODE: - return "bnode"; - default: - return "taosd"; - } -} - -const char *dmProcName(EDndNodeType ntype) { - switch (ntype) { - case VNODE: - return "taosv"; - case QNODE: - return "taosq"; - case SNODE: - return "taoss"; - case MNODE: - return "taosm"; - case BNODE: - return "taosb"; - default: - return "taosd"; - } -} - -const char *dmEventName(EDndEvent ev) { - switch (ev) { - case DND_EVENT_START: - return "start"; - case DND_EVENT_STOP: - return "stop"; - case DND_EVENT_CHILD: - return "child"; - default: - return "UNKNOWN"; - } -} - -void dmSetStatus(SDnode *pDnode, EDndRunStatus status) { - if (pDnode->status != status) { - dDebug("dnode status set from %s to %s", dmStatName(pDnode->status), dmStatName(status)); - pDnode->status = status; - } -} - -void dmSetEvent(SDnode *pDnode, EDndEvent event) { - if (event == DND_EVENT_STOP) { - pDnode->event = event; - } -} - -void dmSetMsgHandle(SMgmtWrapper *pWrapper, tmsg_t msgType, NodeMsgFp nodeMsgFp, int8_t vgId) { - pWrapper->msgFps[TMSG_INDEX(msgType)] = nodeMsgFp; - pWrapper->msgVgIds[TMSG_INDEX(msgType)] = vgId; -} - -SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) { - SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; - SMgmtWrapper *pRetWrapper = pWrapper; - - taosRLockLatch(&pWrapper->latch); - if (pWrapper->deployed) { - int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1); - dTrace("node:%s, is acquired, refCount:%d", pWrapper->name, refCount); - } else { - terrno = TSDB_CODE_NODE_NOT_DEPLOYED; - pRetWrapper = NULL; - } - taosRUnLockLatch(&pWrapper->latch); - - return pRetWrapper; -} - -int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) { - int32_t code = 0; - - taosRLockLatch(&pWrapper->latch); - if (pWrapper->deployed || (pWrapper->procType == DND_PROC_PARENT && pWrapper->required)) { - int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1); - dTrace("node:%s, is marked, refCount:%d", pWrapper->name, refCount); - } else { - terrno = TSDB_CODE_NODE_NOT_DEPLOYED; - code = -1; - } - taosRUnLockLatch(&pWrapper->latch); - - return code; -} - -void dmReleaseWrapper(SMgmtWrapper *pWrapper) { - if (pWrapper == NULL) return; - - taosRLockLatch(&pWrapper->latch); - int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1); - taosRUnLockLatch(&pWrapper->latch); - dTrace("node:%s, is released, refCount:%d", pWrapper->name, refCount); -} - -void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) { - SStartupInfo *pStartup = &pDnode->startup; - tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN); - tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN); - dInfo("step:%s, %s", pStartup->name, pStartup->desc); -} - -void dmReportStartupByWrapper(SMgmtWrapper *pWrapper, const char *pName, const char *pDesc) { - dmReportStartup(pWrapper->pDnode, pName, pDesc); -} - -static void dmGetServerStatus(SDnode *pDnode, SServerStatusRsp *pStatus) { - pStatus->details[0] = 0; - - if (pDnode->status == DND_STAT_INIT) { - pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK; - snprintf(pStatus->details, sizeof(pStatus->details), "%s: %s", pDnode->startup.name, pDnode->startup.desc); - } else if (pDnode->status == DND_STAT_STOPPED) { - pStatus->statusCode = TSDB_SRV_STATUS_EXTING; - } else { - SDnodeData *pData = &pDnode->data; - if (pData->isMnode && pData->mndState != TAOS_SYNC_STATE_LEADER && pData->mndState == TAOS_SYNC_STATE_FOLLOWER) { - pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED; - snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(pData->mndState)); - } else if (pData->unsyncedVgId != 0 && pData->vndState != TAOS_SYNC_STATE_LEADER && - pData->vndState != TAOS_SYNC_STATE_FOLLOWER) { - pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED; - snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pData->unsyncedVgId, - syncStr(pData->vndState)); - } else { - pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK; - } - } -} - -void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pReq) { - dDebug("net test req is received"); - SRpcMsg rsp = {.handle = pReq->handle, .refId = pReq->refId, .ahandle = pReq->ahandle, .code = 0}; - rsp.pCont = rpcMallocCont(pReq->contLen); - if (rsp.pCont == NULL) { - rsp.code = TSDB_CODE_OUT_OF_MEMORY; - } else { - rsp.contLen = pReq->contLen; - } - rpcSendResponse(&rsp); - rpcFreeCont(pReq->pCont); -} - -void dmProcessServerStatusReq(SDnode *pDnode, SRpcMsg *pReq) { - dDebug("server status req is received"); - - SServerStatusRsp statusRsp = {0}; - dmGetServerStatus(pDnode, &statusRsp); - - SRpcMsg rspMsg = {.handle = pReq->handle, .ahandle = pReq->ahandle, .refId = pReq->refId}; - int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp); - if (rspLen < 0) { - rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; - } - - void *pRsp = rpcMallocCont(rspLen); - if (pRsp == NULL) { - rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; - } - - tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp); - rspMsg.pCont = pRsp; - rspMsg.contLen = rspLen; - -_OVER: - rpcSendResponse(&rspMsg); - rpcFreeCont(pReq->pCont); -} - -void dmGetMonitorSysInfo(SMonSysInfo *pInfo) { - taosGetCpuUsage(&pInfo->cpu_engine, &pInfo->cpu_system); - taosGetCpuCores(&pInfo->cpu_cores); - taosGetProcMemory(&pInfo->mem_engine); - taosGetSysMemory(&pInfo->mem_system); - pInfo->mem_total = tsTotalMemoryKB; - pInfo->disk_engine = 0; - pInfo->disk_used = tsDataSpace.size.used; - pInfo->disk_total = tsDataSpace.size.total; - taosGetCardInfoDelta(&pInfo->net_in, &pInfo->net_out); - taosGetProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk); -} diff --git a/source/dnode/mgmt/mgmt_bnode/CMakeLists.txt b/source/dnode/mgmt/mgmt_bnode/CMakeLists.txt index 6a447dccf89b9cca83b2cd3df8309c9a3cec9fe0..0a6cf52fb86dfd7ac24332631f2b9a1ee6c2c572 100644 --- a/source/dnode/mgmt/mgmt_bnode/CMakeLists.txt +++ b/source/dnode/mgmt/mgmt_bnode/CMakeLists.txt @@ -5,5 +5,5 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( - mgmt_bnode dnode_interface + mgmt_bnode node_util ) \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_bnode/inc/bmInt.h b/source/dnode/mgmt/mgmt_bnode/inc/bmInt.h index 3adcc1206bf7f8f9e5360102e49b90771344178e..1923d049a581742c5e44e93d7bf137283cdf99e8 100644 --- a/source/dnode/mgmt/mgmt_bnode/inc/bmInt.h +++ b/source/dnode/mgmt/mgmt_bnode/inc/bmInt.h @@ -16,7 +16,7 @@ #ifndef _TD_DND_BNODE_INT_H_ #define _TD_DND_BNODE_INT_H_ -#include "dmInt.h" +#include "dmUtil.h" #include "bnode.h" @@ -26,24 +26,25 @@ extern "C" { typedef struct SBnodeMgmt { SBnode *pBnode; - SDnode *pDnode; - SMgmtWrapper *pWrapper; + SMsgCb msgCb; const char *path; + const char *name; + int32_t dnodeId; SMultiWorker writeWorker; SSingleWorker monitorWorker; } SBnodeMgmt; // bmHandle.c -void bmInitMsgHandle(SMgmtWrapper *pWrapper); -int32_t bmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t bmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t bmProcessGetMonBmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); +SArray *bmGetMsgHandles(); +int32_t bmProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg); +int32_t bmProcessDropReq(SBnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t bmProcessGetMonBmInfoReq(SBnodeMgmt *pMgmt, SNodeMsg *pReq); // bmWorker.c int32_t bmStartWorker(SBnodeMgmt *pMgmt); void bmStopWorker(SBnodeMgmt *pMgmt); -int32_t bmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t bmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); +int32_t bmPutNodeMsgToWriteQueue(SBnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t bmPutNodeMsgToMonitorQueue(SBnodeMgmt *pMgmt, SNodeMsg *pMsg); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/mgmt_bnode/src/bmHandle.c b/source/dnode/mgmt/mgmt_bnode/src/bmHandle.c index 3b314b1d2b796664c893ebad40837db40da3b311..d87f832262374ec7d8c49dc5fe08b87cd2b2e0db 100644 --- a/source/dnode/mgmt/mgmt_bnode/src/bmHandle.c +++ b/source/dnode/mgmt/mgmt_bnode/src/bmHandle.c @@ -16,12 +16,12 @@ #define _DEFAULT_SOURCE #include "bmInt.h" -void bmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonBmInfo *bmInfo) {} +static void bmGetMonitorInfo(SBnodeMgmt *pMgmt, SMonBmInfo *bmInfo) {} -int32_t bmProcessGetMonBmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t bmProcessGetMonBmInfoReq(SBnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonBmInfo bmInfo = {0}; - bmGetMonitorInfo(pWrapper, &bmInfo); - dmGetMonitorSysInfo(&bmInfo.sys); + bmGetMonitorInfo(pMgmt, &bmInfo); + dmGetMonitorSystemInfo(&bmInfo.sys); monGetLogs(&bmInfo.log); int32_t rspLen = tSerializeSMonBmInfo(NULL, 0, &bmInfo); @@ -43,8 +43,7 @@ int32_t bmProcessGetMonBmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { return 0; } -int32_t bmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t bmProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDCreateBnodeReq createReq = {0}; @@ -53,14 +52,14 @@ int32_t bmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (pDnode->data.dnodeId != 0 && createReq.dnodeId != pDnode->data.dnodeId) { + if (pInput->dnodeId != 0 && createReq.dnodeId != pInput->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; - dError("failed to create bnode since %s, input:%d cur:%d", terrstr(), createReq.dnodeId, pDnode->data.dnodeId); + dError("failed to create bnode since %s, input:%d cur:%d", terrstr(), createReq.dnodeId, pInput->dnodeId); return -1; } bool deployed = true; - if (dmWriteFile(pWrapper, deployed) != 0) { + if (dmWriteFile(pInput->path, pInput->name, deployed) != 0) { dError("failed to write bnode file since %s", terrstr()); return -1; } @@ -68,8 +67,7 @@ int32_t bmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t bmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t bmProcessDropReq(SBnodeMgmt *pMgmt, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDDropBnodeReq dropReq = {0}; @@ -78,14 +76,14 @@ int32_t bmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (dropReq.dnodeId != pDnode->data.dnodeId) { + if (pMgmt->dnodeId != 0 && dropReq.dnodeId != pMgmt->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to drop bnode since %s", terrstr()); return -1; } bool deployed = false; - if (dmWriteFile(pWrapper, deployed) != 0) { + if (dmWriteFile(pMgmt->path, pMgmt->name, deployed) != 0) { dError("failed to write bnode file since %s", terrstr()); return -1; } @@ -93,6 +91,19 @@ int32_t bmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -void bmInitMsgHandle(SMgmtWrapper *pWrapper) { - dmSetMsgHandle(pWrapper, TDMT_MON_BM_INFO, bmProcessMonitorMsg, DEFAULT_HANDLE); +SArray *bmGetMsgHandles() { + int32_t code = -1; + SArray *pArray = taosArrayInit(2, sizeof(SMgmtHandle)); + if (pArray == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_MON_BM_INFO, bmPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; + + code = 0; +_OVER: + if (code != 0) { + taosArrayDestroy(pArray); + return NULL; + } else { + return pArray; + } } diff --git a/source/dnode/mgmt/mgmt_bnode/src/bmInt.c b/source/dnode/mgmt/mgmt_bnode/src/bmInt.c index 2920d12eb4a4cf4c680f49f96eecdfe0a1f23302..45e0c3219330458420504354c94760f506e5e991 100644 --- a/source/dnode/mgmt/mgmt_bnode/src/bmInt.c +++ b/source/dnode/mgmt/mgmt_bnode/src/bmInt.c @@ -16,18 +16,13 @@ #define _DEFAULT_SOURCE #include "bmInt.h" -static int32_t bmRequire(SMgmtWrapper *pWrapper, bool *required) { return dmReadFile(pWrapper, required); } - -static void bmInitOption(SBnodeMgmt *pMgmt, SBnodeOpt *pOption) { - SMsgCb msgCb = pMgmt->pDnode->data.msgCb; - msgCb.pWrapper = pMgmt->pWrapper; - pOption->msgCb = msgCb; +static int32_t bmRequire(const SMgmtInputOpt *pInput, bool *required) { + return dmReadFile(pInput->path, pInput->name, required); } -static void bmClose(SMgmtWrapper *pWrapper) { - SBnodeMgmt *pMgmt = pWrapper->pMgmt; - if (pMgmt == NULL) return; +static void bmInitOption(SBnodeMgmt *pMgmt, SBnodeOpt *pOption) { pOption->msgCb = pMgmt->msgCb; } +static void bmClose(SBnodeMgmt *pMgmt) { dInfo("bnode-mgmt start to cleanup"); if (pMgmt->pBnode != NULL) { bmStopWorker(pMgmt); @@ -35,12 +30,11 @@ static void bmClose(SMgmtWrapper *pWrapper) { pMgmt->pBnode = NULL; } - pWrapper->pMgmt = NULL; taosMemoryFree(pMgmt); dInfo("bnode-mgmt is cleaned up"); } -int32_t bmOpen(SMgmtWrapper *pWrapper) { +int32_t bmOpen(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { dInfo("bnode-mgmt start to init"); SBnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SBnodeMgmt)); if (pMgmt == NULL) { @@ -48,40 +42,42 @@ int32_t bmOpen(SMgmtWrapper *pWrapper) { return -1; } - pMgmt->path = pWrapper->path; - pMgmt->pDnode = pWrapper->pDnode; - pMgmt->pWrapper = pWrapper; - pWrapper->pMgmt = pMgmt; + pMgmt->path = pInput->path; + pMgmt->name = pInput->name; + pMgmt->dnodeId = pInput->dnodeId; + pMgmt->msgCb = pInput->msgCb; + pMgmt->msgCb.pMgmt = pMgmt; SBnodeOpt option = {0}; bmInitOption(pMgmt, &option); pMgmt->pBnode = bndOpen(pMgmt->path, &option); if (pMgmt->pBnode == NULL) { dError("failed to open bnode since %s", terrstr()); - bmClose(pWrapper); + bmClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "bnode-impl", "initialized"); + tmsgReportStartup("bnode-impl", "initialized"); if (bmStartWorker(pMgmt) != 0) { dError("failed to start bnode worker since %s", terrstr()); - bmClose(pWrapper); + bmClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "bnode-worker", "initialized"); + tmsgReportStartup("bnode-worker", "initialized"); + pOutput->pMgmt = pMgmt; + dInfo("bnode-mgmt is initialized"); return 0; } -void bmSetMgmtFp(SMgmtWrapper *pWrapper) { - SMgmtFp mgmtFp = {0}; - mgmtFp.openFp = bmOpen; - mgmtFp.closeFp = bmClose; - mgmtFp.createFp = bmProcessCreateReq; - mgmtFp.dropFp = bmProcessDropReq; - mgmtFp.requiredFp = bmRequire; +SMgmtFunc bmGetMgmtFunc() { + SMgmtFunc mgmtFunc = {0}; + mgmtFunc.openFp = bmOpen; + mgmtFunc.closeFp = (NodeCloseFp)bmClose; + mgmtFunc.createFp = (NodeCreateFp)bmProcessCreateReq; + mgmtFunc.dropFp = (NodeDropFp)bmProcessDropReq; + mgmtFunc.requiredFp = bmRequire; + mgmtFunc.getHandlesFp = bmGetMsgHandles; - bmInitMsgHandle(pWrapper); - pWrapper->name = "bnode"; - pWrapper->fp = mgmtFp; + return mgmtFunc; } diff --git a/source/dnode/mgmt/mgmt_bnode/src/bmWorker.c b/source/dnode/mgmt/mgmt_bnode/src/bmWorker.c index d3204039e6769e62d28586da96aa9b1e3adf095a..1ab8b5ef3454928dc0d63024708c7de947cd54e4 100644 --- a/source/dnode/mgmt/mgmt_bnode/src/bmWorker.c +++ b/source/dnode/mgmt/mgmt_bnode/src/bmWorker.c @@ -58,7 +58,7 @@ static void bmProcessMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { int32_t code = -1; if (pMsg->rpcMsg.msgType == TDMT_MON_BM_INFO) { - code = bmProcessGetMonBmInfoReq(pMgmt->pWrapper, pMsg); + code = bmProcessGetMonBmInfoReq(pMgmt, pMsg); } else { terrno = TSDB_CODE_MSG_NOT_PROCESSED; } @@ -106,8 +106,7 @@ static void bmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO taosArrayDestroy(pArray); } -int32_t bmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SBnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t bmPutNodeMsgToWriteQueue(SBnodeMgmt *pMgmt, SNodeMsg *pMsg) { SMultiWorker *pWorker = &pMgmt->writeWorker; dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name); @@ -115,8 +114,7 @@ int32_t bmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t bmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SBnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t bmPutNodeMsgToMonitorQueue(SBnodeMgmt *pMgmt, SNodeMsg *pMsg) { SSingleWorker *pWorker = &pMgmt->monitorWorker; dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name); @@ -136,18 +134,16 @@ int32_t bmStartWorker(SBnodeMgmt *pMgmt) { return -1; } - if (tsMultiProcess) { - SSingleWorkerCfg mCfg = { - .min = 1, - .max = 1, - .name = "bnode-monitor", - .fp = (FItem)bmProcessMonitorQueue, - .param = pMgmt, - }; - if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { - dError("failed to start bnode-monitor worker since %s", terrstr()); - return -1; - } + SSingleWorkerCfg mCfg = { + .min = 1, + .max = 1, + .name = "bnode-monitor", + .fp = (FItem)bmProcessMonitorQueue, + .param = pMgmt, + }; + if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { + dError("failed to start bnode-monitor worker since %s", terrstr()); + return -1; } dDebug("bnode workers are initialized"); diff --git a/source/dnode/mgmt/mgmt_dnode/CMakeLists.txt b/source/dnode/mgmt/mgmt_dnode/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a4268fc9f0d501ae377934d9df321984010c665e --- /dev/null +++ b/source/dnode/mgmt/mgmt_dnode/CMakeLists.txt @@ -0,0 +1,9 @@ +aux_source_directory(src MGMT_DNODE) +add_library(mgmt_dnode STATIC ${MGMT_DNODE}) +target_include_directories( + mgmt_dnode + PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + mgmt_dnode node_util +) \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_dnode/inc/dmInt.h b/source/dnode/mgmt/mgmt_dnode/inc/dmInt.h new file mode 100644 index 0000000000000000000000000000000000000000..5ef2706f1e9e94e282083b8483abc234be6a57a5 --- /dev/null +++ b/source/dnode/mgmt/mgmt_dnode/inc/dmInt.h @@ -0,0 +1,70 @@ +/* + * 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 _TD_DND_QNODE_INT_H_ +#define _TD_DND_QNODE_INT_H_ + +#include "dmUtil.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SDnodeMgmt { + struct SDnode *pDnode; + SMsgCb msgCb; + const char *path; + const char *name; + TdThread statusThread; + TdThread monitorThread; + SSingleWorker mgmtWorker; + ProcessCreateNodeFp processCreateNodeFp; + ProcessDropNodeFp processDropNodeFp; + IsNodeDeployedFp isNodeDeployedFp; + SDnodeData data; +} SDnodeMgmt; + +// dmEps.c +int32_t dmReadEps(SDnodeMgmt *pMgmt); +int32_t dmWriteEps(SDnodeMgmt *pMgmt); +void dmUpdateEps(SDnodeMgmt *pMgmt, SArray *pDnodeEps); + +// dmHandle.c +SArray *dmGetMsgHandles(); +void dmSendStatusReq(SDnodeMgmt *pMgmt); +int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SNodeMsg *pMsg); + +// dmMonitor.c +void dmGetVnodeLoads(SDnodeMgmt *pMgmt, SMonVloadInfo *pInfo); +void dmGetMnodeLoads(SDnodeMgmt *pMgmt, SMonMloadInfo *pInfo); +void dmSendMonitorReport(SDnodeMgmt *pMgmt); + +// dmWorker.c +int32_t dmPutNodeMsgToMgmtQueue(SDnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t dmStartStatusThread(SDnodeMgmt *pMgmt); +void dmStopStatusThread(SDnodeMgmt *pMgmt); +int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt); +void dmStopMonitorThread(SDnodeMgmt *pMgmt); +int32_t dmStartWorker(SDnodeMgmt *pMgmt); +void dmStopWorker(SDnodeMgmt *pMgmt); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DND_QNODE_INT_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/implement/src/dmEps.c b/source/dnode/mgmt/mgmt_dnode/src/dmEps.c similarity index 69% rename from source/dnode/mgmt/implement/src/dmEps.c rename to source/dnode/mgmt/mgmt_dnode/src/dmEps.c index f5c9a1d91b2fc7f2169386980ec627ac2f5473fc..9ebb02b9646c13fe8997f2899431e079d3fce128 100644 --- a/source/dnode/mgmt/implement/src/dmEps.c +++ b/source/dnode/mgmt/mgmt_dnode/src/dmEps.c @@ -14,16 +14,16 @@ */ #define _DEFAULT_SOURCE -#include "dmImp.h" +#include "dmInt.h" -static void dmPrintEps(SDnode *pDnode); -static bool dmIsEpChanged(SDnode *pDnode, int32_t dnodeId, const char *ep); -static void dmResetEps(SDnode *pDnode, SArray *dnodeEps); +static void dmPrintEps(SDnodeMgmt *pMgmt); +static bool dmIsEpChanged(SDnodeMgmt *pMgmt, int32_t dnodeId, const char *ep); +static void dmResetEps(SDnodeMgmt *pMgmt, SArray *dnodeEps); -static void dmGetDnodeEp(SDnode *pDnode, int32_t dnodeId, char *pEp, char *pFqdn, uint16_t *pPort) { - taosRLockLatch(&pDnode->data.latch); +static void dmGetDnodeEp(SDnodeMgmt *pMgmt, int32_t dnodeId, char *pEp, char *pFqdn, uint16_t *pPort) { + taosRLockLatch(&pMgmt->data.latch); - SDnodeEp *pDnodeEp = taosHashGet(pDnode->data.dnodeHash, &dnodeId, sizeof(int32_t)); + SDnodeEp *pDnodeEp = taosHashGet(pMgmt->data.dnodeHash, &dnodeId, sizeof(int32_t)); if (pDnodeEp != NULL) { if (pPort != NULL) { *pPort = pDnodeEp->ep.port; @@ -36,10 +36,10 @@ static void dmGetDnodeEp(SDnode *pDnode, int32_t dnodeId, char *pEp, char *pFqdn } } - taosRUnLockLatch(&pDnode->data.latch); + taosRUnLockLatch(&pMgmt->data.latch); } -int32_t dmReadEps(SDnode *pDnode) { +int32_t dmReadEps(SDnodeMgmt *pMgmt) { int32_t code = TSDB_CODE_INVALID_JSON_FORMAT; int32_t len = 0; int32_t maxLen = 256 * 1024; @@ -48,16 +48,15 @@ int32_t dmReadEps(SDnode *pDnode) { char file[PATH_MAX] = {0}; TdFilePtr pFile = NULL; - pDnode->data.dnodeEps = taosArrayInit(1, sizeof(SDnodeEp)); - if (pDnode->data.dnodeEps == NULL) { + pMgmt->data.dnodeEps = taosArrayInit(1, sizeof(SDnodeEp)); + if (pMgmt->data.dnodeEps == NULL) { dError("failed to calloc dnodeEp array since %s", strerror(errno)); goto _OVER; } - snprintf(file, sizeof(file), "%s%sdnode.json", pDnode->wrappers[DNODE].path, TD_DIRSEP); + snprintf(file, sizeof(file), "%s%sdnode.json", pMgmt->path, TD_DIRSEP); pFile = taosOpenFile(file, TD_FILE_READ); if (pFile == NULL) { - // dDebug("file %s not exist", file); code = 0; goto _OVER; } @@ -80,21 +79,21 @@ int32_t dmReadEps(SDnode *pDnode) { dError("failed to read %s since dnodeId not found", file); goto _OVER; } - pDnode->data.dnodeId = dnodeId->valueint; + pMgmt->data.dnodeId = dnodeId->valueint; cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); if (!clusterId || clusterId->type != cJSON_String) { dError("failed to read %s since clusterId not found", file); goto _OVER; } - pDnode->data.clusterId = atoll(clusterId->valuestring); + pMgmt->data.clusterId = atoll(clusterId->valuestring); cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); if (!dropped || dropped->type != cJSON_Number) { dError("failed to read %s since dropped not found", file); goto _OVER; } - pDnode->data.dropped = dropped->valueint; + pMgmt->data.dropped = dropped->valueint; cJSON *dnodes = cJSON_GetObjectItem(root, "dnodes"); if (!dnodes || dnodes->type != cJSON_Array) { @@ -144,29 +143,29 @@ int32_t dmReadEps(SDnode *pDnode) { } dnodeEp.isMnode = isMnode->valueint; - taosArrayPush(pDnode->data.dnodeEps, &dnodeEp); + taosArrayPush(pMgmt->data.dnodeEps, &dnodeEp); } code = 0; dDebug("succcessed to read file %s", file); - dmPrintEps(pDnode); + dmPrintEps(pMgmt); _OVER: if (content != NULL) taosMemoryFree(content); if (root != NULL) cJSON_Delete(root); if (pFile != NULL) taosCloseFile(&pFile); - if (taosArrayGetSize(pDnode->data.dnodeEps) == 0) { + if (taosArrayGetSize(pMgmt->data.dnodeEps) == 0) { SDnodeEp dnodeEp = {0}; dnodeEp.isMnode = 1; - taosGetFqdnPortFromEp(pDnode->data.firstEp, &dnodeEp.ep); - taosArrayPush(pDnode->data.dnodeEps, &dnodeEp); + taosGetFqdnPortFromEp(pMgmt->data.firstEp, &dnodeEp.ep); + taosArrayPush(pMgmt->data.dnodeEps, &dnodeEp); } - dmResetEps(pDnode, pDnode->data.dnodeEps); + dmResetEps(pMgmt, pMgmt->data.dnodeEps); - if (dmIsEpChanged(pDnode, pDnode->data.dnodeId, pDnode->data.localEp)) { - dError("localEp %s different with %s and need reconfigured", pDnode->data.localEp, file); + if (dmIsEpChanged(pMgmt, pMgmt->data.dnodeId, pMgmt->data.localEp)) { + dError("localEp %s different with %s and need reconfigured", pMgmt->data.localEp, file); return -1; } @@ -174,11 +173,11 @@ _OVER: return code; } -int32_t dmWriteEps(SDnode *pDnode) { +int32_t dmWriteEps(SDnodeMgmt *pMgmt) { char file[PATH_MAX] = {0}; char realfile[PATH_MAX] = {0}; - snprintf(file, sizeof(file), "%s%sdnode.json.bak", pDnode->wrappers[DNODE].path, TD_DIRSEP); - snprintf(realfile, sizeof(realfile), "%s%sdnode.json", pDnode->wrappers[DNODE].path, TD_DIRSEP); + snprintf(file, sizeof(file), "%s%sdnode.json.bak", pMgmt->path, TD_DIRSEP); + snprintf(realfile, sizeof(realfile), "%s%sdnode.json", pMgmt->path, TD_DIRSEP); TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { @@ -192,14 +191,14 @@ int32_t dmWriteEps(SDnode *pDnode) { char *content = taosMemoryCalloc(1, maxLen + 1); len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", pDnode->data.dnodeId); - len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", pDnode->data.clusterId); - len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", pDnode->data.dropped); + len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", pMgmt->data.dnodeId); + len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", pMgmt->data.clusterId); + len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", pMgmt->data.dropped); len += snprintf(content + len, maxLen - len, " \"dnodes\": [{\n"); - int32_t numOfEps = (int32_t)taosArrayGetSize(pDnode->data.dnodeEps); + int32_t numOfEps = (int32_t)taosArrayGetSize(pMgmt->data.dnodeEps); for (int32_t i = 0; i < numOfEps; ++i) { - SDnodeEp *pDnodeEp = taosArrayGet(pDnode->data.dnodeEps, i); + SDnodeEp *pDnodeEp = taosArrayGet(pMgmt->data.dnodeEps, i); len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pDnodeEp->id); len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pDnodeEp->ep.fqdn); len += snprintf(content + len, maxLen - len, " \"port\": %u,\n", pDnodeEp->ep.port); @@ -223,41 +222,41 @@ int32_t dmWriteEps(SDnode *pDnode) { return -1; } - pDnode->data.updateTime = taosGetTimestampMs(); + pMgmt->data.updateTime = taosGetTimestampMs(); dDebug("successed to write %s", realfile); return 0; } -void dmUpdateEps(SDnode *pDnode, SArray *eps) { +void dmUpdateEps(SDnodeMgmt *pMgmt, SArray *eps) { int32_t numOfEps = taosArrayGetSize(eps); if (numOfEps <= 0) return; - taosWLockLatch(&pDnode->data.latch); + taosWLockLatch(&pMgmt->data.latch); - int32_t numOfEpsOld = (int32_t)taosArrayGetSize(pDnode->data.dnodeEps); + int32_t numOfEpsOld = (int32_t)taosArrayGetSize(pMgmt->data.dnodeEps); if (numOfEps != numOfEpsOld) { - dmResetEps(pDnode, eps); - dmWriteEps(pDnode); + dmResetEps(pMgmt, eps); + dmWriteEps(pMgmt); } else { int32_t size = numOfEps * sizeof(SDnodeEp); - if (memcmp(pDnode->data.dnodeEps->pData, eps->pData, size) != 0) { - dmResetEps(pDnode, eps); - dmWriteEps(pDnode); + if (memcmp(pMgmt->data.dnodeEps->pData, eps->pData, size) != 0) { + dmResetEps(pMgmt, eps); + dmWriteEps(pMgmt); } } - taosWUnLockLatch(&pDnode->data.latch); + taosWUnLockLatch(&pMgmt->data.latch); } -static void dmResetEps(SDnode *pDnode, SArray *dnodeEps) { - if (pDnode->data.dnodeEps != dnodeEps) { - SArray *tmp = pDnode->data.dnodeEps; - pDnode->data.dnodeEps = taosArrayDup(dnodeEps); +static void dmResetEps(SDnodeMgmt *pMgmt, SArray *dnodeEps) { + if (pMgmt->data.dnodeEps != dnodeEps) { + SArray *tmp = pMgmt->data.dnodeEps; + pMgmt->data.dnodeEps = taosArrayDup(dnodeEps); taosArrayDestroy(tmp); } - pDnode->data.mnodeEps.inUse = 0; - pDnode->data.mnodeEps.numOfEps = 0; + pMgmt->data.mnodeEps.inUse = 0; + pMgmt->data.mnodeEps.numOfEps = 0; int32_t mIndex = 0; int32_t numOfEps = (int32_t)taosArrayGetSize(dnodeEps); @@ -266,35 +265,35 @@ static void dmResetEps(SDnode *pDnode, SArray *dnodeEps) { SDnodeEp *pDnodeEp = taosArrayGet(dnodeEps, i); if (!pDnodeEp->isMnode) continue; if (mIndex >= TSDB_MAX_REPLICA) continue; - pDnode->data.mnodeEps.numOfEps++; + pMgmt->data.mnodeEps.numOfEps++; - pDnode->data.mnodeEps.eps[mIndex] = pDnodeEp->ep; + pMgmt->data.mnodeEps.eps[mIndex] = pDnodeEp->ep; mIndex++; } for (int32_t i = 0; i < numOfEps; i++) { SDnodeEp *pDnodeEp = taosArrayGet(dnodeEps, i); - taosHashPut(pDnode->data.dnodeHash, &pDnodeEp->id, sizeof(int32_t), pDnodeEp, sizeof(SDnodeEp)); + taosHashPut(pMgmt->data.dnodeHash, &pDnodeEp->id, sizeof(int32_t), pDnodeEp, sizeof(SDnodeEp)); } - dmPrintEps(pDnode); + dmPrintEps(pMgmt); } -static void dmPrintEps(SDnode *pDnode) { - int32_t numOfEps = (int32_t)taosArrayGetSize(pDnode->data.dnodeEps); +static void dmPrintEps(SDnodeMgmt *pMgmt) { + int32_t numOfEps = (int32_t)taosArrayGetSize(pMgmt->data.dnodeEps); dDebug("print dnode ep list, num:%d", numOfEps); for (int32_t i = 0; i < numOfEps; i++) { - SDnodeEp *pEp = taosArrayGet(pDnode->data.dnodeEps, i); + SDnodeEp *pEp = taosArrayGet(pMgmt->data.dnodeEps, i); dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", pEp->id, pEp->ep.fqdn, pEp->ep.port, pEp->isMnode); } } -static bool dmIsEpChanged(SDnode *pDnode, int32_t dnodeId, const char *ep) { +static bool dmIsEpChanged(SDnodeMgmt *pMgmt, int32_t dnodeId, const char *ep) { bool changed = false; if (dnodeId == 0) return changed; - taosRLockLatch(&pDnode->data.latch); + taosRLockLatch(&pMgmt->data.latch); - SDnodeEp *pDnodeEp = taosHashGet(pDnode->data.dnodeHash, &dnodeId, sizeof(int32_t)); + SDnodeEp *pDnodeEp = taosHashGet(pMgmt->data.dnodeHash, &dnodeId, sizeof(int32_t)); if (pDnodeEp != NULL) { char epstr[TSDB_EP_LEN + 1] = {0}; snprintf(epstr, TSDB_EP_LEN, "%s:%u", pDnodeEp->ep.fqdn, pDnodeEp->ep.port); @@ -304,6 +303,6 @@ static bool dmIsEpChanged(SDnode *pDnode, int32_t dnodeId, const char *ep) { } } - taosRUnLockLatch(&pDnode->data.latch); + taosRUnLockLatch(&pMgmt->data.latch); return changed; } diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c b/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c new file mode 100644 index 0000000000000000000000000000000000000000..38f97d36006bf0d773e0d76a22fdf3a2902a6902 --- /dev/null +++ b/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c @@ -0,0 +1,206 @@ +/* + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "dmInt.h" + +static void dmUpdateDnodeCfg(SDnodeMgmt *pMgmt, SDnodeCfg *pCfg) { + if (pMgmt->data.dnodeId == 0 || pMgmt->data.clusterId == 0) { + dInfo("set dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId); + taosWLockLatch(&pMgmt->data.latch); + pMgmt->data.dnodeId = pCfg->dnodeId; + pMgmt->data.clusterId = pCfg->clusterId; + dmWriteEps(pMgmt); + taosWUnLockLatch(&pMgmt->data.latch); + } +} + +static void dmProcessStatusRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) { + if (pRsp->code != 0) { + if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->data.dropped && pMgmt->data.dnodeId > 0) { + dInfo("dnode:%d, set to dropped since not exist in mnode", pMgmt->data.dnodeId); + pMgmt->data.dropped = 1; + dmWriteEps(pMgmt); + } + } else { + SStatusRsp statusRsp = {0}; + if (pRsp->pCont != NULL && pRsp->contLen > 0 && + tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) { + pMgmt->data.dnodeVer = statusRsp.dnodeVer; + dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg); + dmUpdateEps(pMgmt, statusRsp.pDnodeEps); + } + rpcFreeCont(pRsp->pCont); + tFreeSStatusRsp(&statusRsp); + } +} + +void dmSendStatusReq(SDnodeMgmt *pMgmt) { + SStatusReq req = {0}; + + taosRLockLatch(&pMgmt->data.latch); + req.sver = tsVersion; + req.dnodeVer = pMgmt->data.dnodeVer; + req.dnodeId = pMgmt->data.dnodeId; + req.clusterId = pMgmt->data.clusterId; + if (req.clusterId == 0) req.dnodeId = 0; + req.rebootTime = pMgmt->data.rebootTime; + req.updateTime = pMgmt->data.updateTime; + req.numOfCores = tsNumOfCores; + req.numOfSupportVnodes = pMgmt->data.supportVnodes; + tstrncpy(req.dnodeEp, pMgmt->data.localEp, TSDB_EP_LEN); + + req.clusterCfg.statusInterval = tsStatusInterval; + req.clusterCfg.checkTime = 0; + char timestr[32] = "1970-01-01 00:00:00.00"; + (void)taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); + memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN); + memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN); + memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN); + taosRUnLockLatch(&pMgmt->data.latch); + + SMonVloadInfo vinfo = {0}; + dmGetVnodeLoads(pMgmt, &vinfo); + req.pVloads = vinfo.pVloads; + pMgmt->data.unsyncedVgId = 0; + pMgmt->data.vndState = TAOS_SYNC_STATE_LEADER; + for (int32_t i = 0; i < taosArrayGetSize(req.pVloads); ++i) { + SVnodeLoad *pLoad = taosArrayGet(req.pVloads, i); + if (pLoad->syncState != TAOS_SYNC_STATE_LEADER && pLoad->syncState != TAOS_SYNC_STATE_FOLLOWER) { + pMgmt->data.unsyncedVgId = pLoad->vgId; + pMgmt->data.vndState = pLoad->syncState; + } + } + + SMonMloadInfo minfo = {0}; + dmGetMnodeLoads(pMgmt, &minfo); + pMgmt->data.mndState = minfo.load.syncState; + + int32_t contLen = tSerializeSStatusReq(NULL, 0, &req); + void *pHead = rpcMallocCont(contLen); + tSerializeSStatusReq(pHead, contLen, &req); + tFreeSStatusReq(&req); + + SRpcMsg rpcMsg = {.pCont = pHead, .contLen = contLen, .msgType = TDMT_MND_STATUS, .ahandle = (void *)0x9527}; + SRpcMsg rpcRsp = {0}; + + dTrace("send req:%s to mnode, app:%p", TMSG_INFO(rpcMsg.msgType), rpcMsg.ahandle); + tmsgSendMnodeRecv(&rpcMsg, &rpcRsp); + dmProcessStatusRsp(pMgmt, &rpcRsp); +} + +int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { + SRpcMsg *pRsp = &pMsg->rpcMsg; + dError("auth rsp is received, but not supported yet"); + return 0; +} + +int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { + SRpcMsg *pRsp = &pMsg->rpcMsg; + dError("grant rsp is received, but not supported yet"); + return 0; +} + +int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { + SRpcMsg *pReq = &pMsg->rpcMsg; + SDCfgDnodeReq *pCfg = pReq->pCont; + dError("config req is received, but not supported yet"); + return TSDB_CODE_OPS_NOT_SUPPORT; +} + +static void dmGetServerRunStatus(SDnodeMgmt *pMgmt, SServerStatusRsp *pStatus) { + pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK; + pStatus->details[0] = 0; + + SServerStatusRsp statusRsp = {0}; + SMonMloadInfo minfo = {0}; + dmGetMnodeLoads(pMgmt, &minfo); + if (minfo.isMnode && minfo.load.syncState != TAOS_SYNC_STATE_LEADER && + minfo.load.syncState != TAOS_SYNC_STATE_CANDIDATE) { + pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED; + snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(minfo.load.syncState)); + return; + } + + SMonVloadInfo vinfo = {0}; + dmGetVnodeLoads(pMgmt, &vinfo); + for (int32_t i = 0; i < taosArrayGetSize(vinfo.pVloads); ++i) { + SVnodeLoad *pLoad = taosArrayGet(vinfo.pVloads, i); + if (pLoad->syncState != TAOS_SYNC_STATE_LEADER && pLoad->syncState != TAOS_SYNC_STATE_FOLLOWER) { + pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED; + snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pLoad->vgId, + syncStr(pLoad->syncState)); + break; + } + } + + taosArrayDestroy(vinfo.pVloads); +} + +int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { + dDebug("server run status req is received"); + SServerStatusRsp statusRsp = {0}; + dmGetServerRunStatus(pMgmt, &statusRsp); + + SRpcMsg rspMsg = {.handle = pMsg->rpcMsg.handle, .ahandle = pMsg->rpcMsg.ahandle, .refId = pMsg->rpcMsg.refId}; + int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp); + if (rspLen < 0) { + rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + void *pRsp = rpcMallocCont(rspLen); + if (pRsp == NULL) { + rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp); + pMsg->pRsp = pRsp; + pMsg->rspLen = rspLen; + return 0; +} + +SArray *dmGetMsgHandles() { + int32_t code = -1; + SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle)); + if (pArray == NULL) goto _OVER; + + // Requests handled by DNODE + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_BNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_BNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_SERVER_STATUS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + + // Requests handled by MNODE + if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + + code = 0; + +_OVER: + if (code != 0) { + taosArrayDestroy(pArray); + return NULL; + } else { + return pArray; + } +} diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmInt.c b/source/dnode/mgmt/mgmt_dnode/src/dmInt.c new file mode 100644 index 0000000000000000000000000000000000000000..9e40b3d02253506839746dd9c42229eda2cf6594 --- /dev/null +++ b/source/dnode/mgmt/mgmt_dnode/src/dmInt.c @@ -0,0 +1,129 @@ +/* + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "dmInt.h" + +static int32_t dmStartMgmt(SDnodeMgmt *pMgmt) { + if (dmStartStatusThread(pMgmt) != 0) { + return -1; + } + if (dmStartMonitorThread(pMgmt) != 0) { + return -1; + } + return 0; +} + +static void dmStopMgmt(SDnodeMgmt *pMgmt) { + pMgmt->data.stopped = true; + dmStopMonitorThread(pMgmt); + dmStopStatusThread(pMgmt); +} + +static int32_t dmOpenMgmt(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { + dInfo("dnode-mgmt start to init"); + SDnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SDnodeMgmt)); + if (pMgmt == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + pMgmt->data.dnodeId = 0; + pMgmt->data.clusterId = 0; + pMgmt->data.dnodeVer = 0; + pMgmt->data.updateTime = 0; + pMgmt->data.rebootTime = taosGetTimestampMs(); + pMgmt->data.dropped = 0; + pMgmt->data.localEp = pInput->localEp; + pMgmt->data.localFqdn = pInput->localFqdn; + pMgmt->data.firstEp = pInput->firstEp; + pMgmt->data.secondEp = pInput->secondEp; + pMgmt->data.supportVnodes = pInput->supportVnodes; + pMgmt->data.serverPort = pInput->serverPort; + pMgmt->pDnode = pInput->pDnode; + pMgmt->msgCb = pInput->msgCb; + pMgmt->path = pInput->path; + pMgmt->name = pInput->name; + pMgmt->processCreateNodeFp = pInput->processCreateNodeFp; + pMgmt->processDropNodeFp = pInput->processDropNodeFp; + pMgmt->isNodeDeployedFp = pInput->isNodeDeployedFp; + taosInitRWLatch(&pMgmt->data.latch); + + pMgmt->data.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); + if (pMgmt->data.dnodeHash == NULL) { + dError("failed to init dnode hash"); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + if (dmReadEps(pMgmt) != 0) { + dError("failed to read file since %s", terrstr()); + return -1; + } + + if (pMgmt->data.dropped) { + dError("dnode will not start since its already dropped"); + return -1; + } + + if (dmStartWorker(pMgmt) != 0) { + return -1; + } + + if (udfStartUdfd(pMgmt->data.dnodeId) != 0) { + dError("failed to start udfd"); + } + + pOutput->pMgmt = pMgmt; + pOutput->mnodeEps = pMgmt->data.mnodeEps; + dInfo("dnode-mgmt is initialized"); + return 0; +} + +static void dmCloseMgmt(SDnodeMgmt *pMgmt) { + dInfo("dnode-mgmt start to clean up"); + dmStopWorker(pMgmt); + + taosWLockLatch(&pMgmt->data.latch); + if (pMgmt->data.dnodeEps != NULL) { + taosArrayDestroy(pMgmt->data.dnodeEps); + pMgmt->data.dnodeEps = NULL; + } + if (pMgmt->data.dnodeHash != NULL) { + taosHashCleanup(pMgmt->data.dnodeHash); + pMgmt->data.dnodeHash = NULL; + } + taosWUnLockLatch(&pMgmt->data.latch); + taosMemoryFree(pMgmt); + + dInfo("dnode-mgmt is cleaned up"); +} + +static int32_t dmRequireMgmt(const SMgmtInputOpt *pInput, bool *required) { + *required = true; + return 0; +} + +SMgmtFunc dmGetMgmtFunc() { + SMgmtFunc mgmtFunc = {0}; + mgmtFunc.openFp = dmOpenMgmt; + mgmtFunc.closeFp = (NodeCloseFp)dmCloseMgmt; + mgmtFunc.startFp = (NodeStartFp)dmStartMgmt; + mgmtFunc.stopFp = (NodeStopFp)dmStopMgmt; + mgmtFunc.requiredFp = dmRequireMgmt; + mgmtFunc.getHandlesFp = dmGetMsgHandles; + + return mgmtFunc; +} diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmMonitor.c b/source/dnode/mgmt/mgmt_dnode/src/dmMonitor.c new file mode 100644 index 0000000000000000000000000000000000000000..d2f16c3ad0ba41d3a6bf20881fdd0ffbdda3e8a7 --- /dev/null +++ b/source/dnode/mgmt/mgmt_dnode/src/dmMonitor.c @@ -0,0 +1,105 @@ +/* + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "dmInt.h" + +#define dmSendLocalRecv(pMgmt, mtype, func, pInfo) \ + if (!tsMultiProcess) { \ + SRpcMsg rsp = {0}; \ + SRpcMsg req = {.msgType = mtype}; \ + SEpSet epset = {.inUse = 0, .numOfEps = 1}; \ + tstrncpy(epset.eps[0].fqdn, pMgmt->data.localFqdn, TSDB_FQDN_LEN); \ + epset.eps[0].port = pMgmt->data.serverPort; \ + \ + rpcSendRecv(pMgmt->msgCb.clientRpc, &epset, &req, &rsp); \ + if (rsp.code == 0 && rsp.contLen > 0) { \ + func(rsp.pCont, rsp.contLen, pInfo); \ + } \ + rpcFreeCont(rsp.pCont); \ + } + +static void dmGetMonitorBasicInfo(SDnodeMgmt *pMgmt, SMonBasicInfo *pInfo) { + pInfo->protocol = 1; + pInfo->dnode_id = pMgmt->data.dnodeId; + pInfo->cluster_id = pMgmt->data.clusterId; + tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); +} + +static void dmGetMonitorDnodeInfo(SDnodeMgmt *pMgmt, SMonDnodeInfo *pInfo) { + pInfo->uptime = (taosGetTimestampMs() - pMgmt->data.rebootTime) / (86400000.0f); + pInfo->has_mnode = (*pMgmt->isNodeDeployedFp)(pMgmt->pDnode, MNODE); + pInfo->has_qnode = (*pMgmt->isNodeDeployedFp)(pMgmt->pDnode, QNODE); + pInfo->has_snode = (*pMgmt->isNodeDeployedFp)(pMgmt->pDnode, SNODE); + pInfo->has_bnode = (*pMgmt->isNodeDeployedFp)(pMgmt->pDnode, BNODE); + tstrncpy(pInfo->logdir.name, tsLogDir, sizeof(pInfo->logdir.name)); + pInfo->logdir.size = tsLogSpace.size; + tstrncpy(pInfo->tempdir.name, tsTempDir, sizeof(pInfo->tempdir.name)); + pInfo->tempdir.size = tsTempSpace.size; +} + +static void dmGetMonitorInfo(SDnodeMgmt *pMgmt, SMonDmInfo *pInfo) { + dmGetMonitorBasicInfo(pMgmt, &pInfo->basic); + dmGetMonitorDnodeInfo(pMgmt, &pInfo->dnode); + dmGetMonitorSystemInfo(&pInfo->sys); +} + +void dmSendMonitorReport(SDnodeMgmt *pMgmt) { + if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; + dTrace("send monitor report to %s:%u", tsMonitorFqdn, tsMonitorPort); + + SMonDmInfo dmInfo = {0}; + SMonMmInfo mmInfo = {0}; + SMonVmInfo vmInfo = {0}; + SMonQmInfo qmInfo = {0}; + SMonSmInfo smInfo = {0}; + SMonBmInfo bmInfo = {0}; + + dmGetMonitorInfo(pMgmt, &dmInfo); + dmSendLocalRecv(pMgmt, TDMT_MON_VM_INFO, tDeserializeSMonVmInfo, &vmInfo); + if (dmInfo.dnode.has_mnode) { + dmSendLocalRecv(pMgmt, TDMT_MON_MM_INFO, tDeserializeSMonMmInfo, &mmInfo); + } + if (dmInfo.dnode.has_qnode) { + dmSendLocalRecv(pMgmt, TDMT_MON_QM_INFO, tDeserializeSMonQmInfo, &qmInfo); + } + if (dmInfo.dnode.has_snode) { + dmSendLocalRecv(pMgmt, TDMT_MON_SM_INFO, tDeserializeSMonSmInfo, &smInfo); + } + if (dmInfo.dnode.has_bnode) { + dmSendLocalRecv(pMgmt, TDMT_MON_BM_INFO, tDeserializeSMonBmInfo, &bmInfo); + } + + monSetDmInfo(&dmInfo); + monSetMmInfo(&mmInfo); + monSetVmInfo(&vmInfo); + monSetQmInfo(&qmInfo); + monSetSmInfo(&smInfo); + monSetBmInfo(&bmInfo); + tFreeSMonMmInfo(&mmInfo); + tFreeSMonVmInfo(&vmInfo); + tFreeSMonQmInfo(&qmInfo); + tFreeSMonSmInfo(&smInfo); + tFreeSMonBmInfo(&bmInfo); + monSendReport(); +} + +void dmGetVnodeLoads(SDnodeMgmt *pMgmt, SMonVloadInfo *pInfo) { + dmSendLocalRecv(pMgmt, TDMT_MON_VM_LOAD, tDeserializeSMonVloadInfo, pInfo); +} + +void dmGetMnodeLoads(SDnodeMgmt *pMgmt, SMonMloadInfo *pInfo) { + dmSendLocalRecv(pMgmt, TDMT_MON_MM_LOAD, tDeserializeSMonMloadInfo, pInfo); +} diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c b/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c new file mode 100644 index 0000000000000000000000000000000000000000..af02e56f0569257b714edd696bac56a0ae877891 --- /dev/null +++ b/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c @@ -0,0 +1,195 @@ +/* + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "dmInt.h" + +static void *dmStatusThreadFp(void *param) { + SDnodeMgmt *pMgmt = param; + int64_t lastTime = taosGetTimestampMs(); + + setThreadName("dnode-status"); + + while (1) { + taosMsleep(200); + if (pMgmt->data.dropped || pMgmt->data.stopped) break; + + int64_t curTime = taosGetTimestampMs(); + float interval = (curTime - lastTime) / 1000.0f; + if (interval >= tsStatusInterval) { + dmSendStatusReq(pMgmt); + lastTime = curTime; + } + } + + return NULL; +} + +static void *dmMonitorThreadFp(void *param) { + SDnodeMgmt *pMgmt = param; + int64_t lastTime = taosGetTimestampMs(); + + setThreadName("dnode-monitor"); + + while (1) { + taosMsleep(200); + if (pMgmt->data.dropped || pMgmt->data.stopped) break; + + int64_t curTime = taosGetTimestampMs(); + float interval = (curTime - lastTime) / 1000.0f; + if (interval >= tsMonitorInterval) { + dmSendMonitorReport(pMgmt); + lastTime = curTime; + } + } + + return NULL; +} + +int32_t dmStartStatusThread(SDnodeMgmt *pMgmt) { + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); + if (taosThreadCreate(&pMgmt->statusThread, &thAttr, dmStatusThreadFp, pMgmt) != 0) { + dError("failed to create status thread since %s", strerror(errno)); + return -1; + } + + taosThreadAttrDestroy(&thAttr); + tmsgReportStartup("dnode-status", "initialized"); + return 0; +} + +void dmStopStatusThread(SDnodeMgmt *pMgmt) { + if (taosCheckPthreadValid(pMgmt->statusThread)) { + taosThreadJoin(pMgmt->statusThread, NULL); + } +} + +int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt) { + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); + if (taosThreadCreate(&pMgmt->monitorThread, &thAttr, dmMonitorThreadFp, pMgmt) != 0) { + dError("failed to create monitor thread since %s", strerror(errno)); + return -1; + } + + taosThreadAttrDestroy(&thAttr); + tmsgReportStartup("dnode-monitor", "initialized"); + return 0; +} + +void dmStopMonitorThread(SDnodeMgmt *pMgmt) { + if (taosCheckPthreadValid(pMgmt->monitorThread)) { + taosThreadJoin(pMgmt->monitorThread, NULL); + } +} + +static void dmProcessMgmtQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { + SDnodeMgmt *pMgmt = pInfo->ahandle; + int32_t code = -1; + tmsg_t msgType = pMsg->rpcMsg.msgType; + bool isRequest = msgType & 1u; + dTrace("msg:%p, will be processed in dnode-mgmt queue, type:%s", pMsg, TMSG_INFO(msgType)); + + switch (msgType) { + case TDMT_DND_CONFIG_DNODE: + code = dmProcessConfigReq(pMgmt, pMsg); + break; + case TDMT_MND_AUTH_RSP: + code = dmProcessAuthRsp(pMgmt, pMsg); + break; + case TDMT_MND_GRANT_RSP: + code = dmProcessGrantRsp(pMgmt, pMsg); + break; + case TDMT_DND_CREATE_MNODE: + code = (*pMgmt->processCreateNodeFp)(pMgmt->pDnode, MNODE, pMsg); + break; + case TDMT_DND_DROP_MNODE: + code = (*pMgmt->processDropNodeFp)(pMgmt->pDnode, MNODE, pMsg); + break; + case TDMT_DND_CREATE_QNODE: + code = (*pMgmt->processCreateNodeFp)(pMgmt->pDnode, QNODE, pMsg); + break; + case TDMT_DND_DROP_QNODE: + code = (*pMgmt->processDropNodeFp)(pMgmt->pDnode, QNODE, pMsg); + break; + case TDMT_DND_CREATE_SNODE: + code = (*pMgmt->processCreateNodeFp)(pMgmt->pDnode, SNODE, pMsg); + break; + case TDMT_DND_DROP_SNODE: + code = (*pMgmt->processDropNodeFp)(pMgmt->pDnode, SNODE, pMsg); + break; + case TDMT_DND_CREATE_BNODE: + code = (*pMgmt->processCreateNodeFp)(pMgmt->pDnode, BNODE, pMsg); + break; + case TDMT_DND_DROP_BNODE: + code = (*pMgmt->processDropNodeFp)(pMgmt->pDnode, BNODE, pMsg); + break; + case TDMT_DND_SERVER_STATUS: + code = dmProcessServerRunStatus(pMgmt, pMsg); + break; + default: + terrno = TSDB_CODE_MSG_NOT_PROCESSED; + break; + } + + if (isRequest) { + if (code != 0 && terrno != 0) code = terrno; + SRpcMsg rsp = { + .handle = pMsg->rpcMsg.handle, + .ahandle = pMsg->rpcMsg.ahandle, + .code = code, + .refId = pMsg->rpcMsg.refId, + .pCont = pMsg->pRsp, + .contLen = pMsg->rspLen, + }; + rpcSendResponse(&rsp); + } + + dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code)); + rpcFreeCont(pMsg->rpcMsg.pCont); + taosFreeQitem(pMsg); +} + +int32_t dmStartWorker(SDnodeMgmt *pMgmt) { + SSingleWorkerCfg cfg = { + .min = 1, + .max = 1, + .name = "dnode-mgmt", + .fp = (FItem)dmProcessMgmtQueue, + .param = pMgmt, + }; + if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) { + dError("failed to start dnode-mgmt worker since %s", terrstr()); + return -1; + } + + dDebug("dnode workers are initialized"); + return 0; +} + +void dmStopWorker(SDnodeMgmt *pMgmt) { + tSingleWorkerCleanup(&pMgmt->mgmtWorker); + dDebug("dnode workers are closed"); +} + +int32_t dmPutNodeMsgToMgmtQueue(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { + SSingleWorker *pWorker = &pMgmt->mgmtWorker; + dTrace("msg:%p, put into worker %s", pMsg, pWorker->name); + taosWriteQitem(pWorker->queue, pMsg); + return 0; +} diff --git a/source/dnode/mgmt/mgmt_mnode/CMakeLists.txt b/source/dnode/mgmt/mgmt_mnode/CMakeLists.txt index 5ca9af5628cb9bc54b8af860b9efdc753cd93121..04118590a2ef0fce1f1a6d38e3862a41a0f9ae32 100644 --- a/source/dnode/mgmt/mgmt_mnode/CMakeLists.txt +++ b/source/dnode/mgmt/mgmt_mnode/CMakeLists.txt @@ -5,5 +5,5 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( - mgmt_mnode dnode_interface + mgmt_mnode node_util ) \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_mnode/inc/mmInt.h b/source/dnode/mgmt/mgmt_mnode/inc/mmInt.h index 4d40d1fa287417aff15600ae7dc56618c44a3cb0..87dbe702beb45948d61bd44025ab3d3363c801e6 100644 --- a/source/dnode/mgmt/mgmt_mnode/inc/mmInt.h +++ b/source/dnode/mgmt/mgmt_mnode/inc/mmInt.h @@ -16,8 +16,7 @@ #ifndef _TD_DND_MNODE_INT_H_ #define _TD_DND_MNODE_INT_H_ -#include "dmInt.h" - +#include "dmUtil.h" #include "mnode.h" #ifdef __cplusplus @@ -26,9 +25,10 @@ extern "C" { typedef struct SMnodeMgmt { SMnode *pMnode; - SDnode *pDnode; - SMgmtWrapper *pWrapper; + SMsgCb msgCb; const char *path; + const char *name; + int32_t dnodeId; SSingleWorker queryWorker; SSingleWorker readWorker; SSingleWorker writeWorker; @@ -41,33 +41,31 @@ typedef struct SMnodeMgmt { // mmFile.c int32_t mmReadFile(SMnodeMgmt *pMgmt, bool *pDeployed); -int32_t mmWriteFile(SMgmtWrapper *pWrapper, SDCreateMnodeReq *pReq, bool deployed); +int32_t mmWriteFile(SMnodeMgmt *pMgmt, SDCreateMnodeReq *pReq, bool deployed); // mmInt.c int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq); // mmHandle.c -void mmInitMsgHandle(SMgmtWrapper *pWrapper); -int32_t mmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t mmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); +SArray *mmGetMsgHandles(); +int32_t mmProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg); +int32_t mmProcessDropReq(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); int32_t mmProcessAlterReq(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); -int32_t mmProcessGetMonMmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); -int32_t mmProcessGetMnodeLoadsReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); -void mmGetMnodeLoads(SMgmtWrapper *pWrapper, SMonMloadInfo *pInfo); +int32_t mmProcessGetMonitorInfoReq(SMnodeMgmt *pMgmt, SNodeMsg *pReq); +int32_t mmProcessGetLoadsReq(SMnodeMgmt *pMgmt, SNodeMsg *pReq); // mmWorker.c int32_t mmStartWorker(SMnodeMgmt *pMgmt); void mmStopWorker(SMnodeMgmt *pMgmt); -int32_t mmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t mmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t mmProcessReadMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t mmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t mmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); - -int32_t mmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc); -int32_t mmPutMsgToReadQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc); -int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc); -int32_t mmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc); +int32_t mmPutNodeMsgToWriteQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t mmPutNodeMsgToSyncQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t mmPutNodeMsgToReadQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t mmPutNodeMsgToQueryQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t mmPutNodeMsgToMonitorQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t mmPutRpcMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc); +int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc); +int32_t mmPutRpcMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc); +int32_t mmPutRpcMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c index 83c832a41eca6f220e1de218cad4bd88e8c3554d..df377fefe796960c6d793c7b3550ccec8c4e50c3 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c @@ -28,7 +28,6 @@ int32_t mmReadFile(SMnodeMgmt *pMgmt, bool *pDeployed) { snprintf(file, sizeof(file), "%s%smnode.json", pMgmt->path, TD_DIRSEP); pFile = taosOpenFile(file, TD_FILE_READ); if (pFile == NULL) { - // dDebug("file %s not exist", file); code = 0; goto _OVER; } @@ -105,11 +104,11 @@ _OVER: return code; } -int32_t mmWriteFile(SMgmtWrapper *pWrapper, SDCreateMnodeReq *pReq, bool deployed) { +int32_t mmWriteFile(SMnodeMgmt *pMgmt, SDCreateMnodeReq *pReq, bool deployed) { char file[PATH_MAX] = {0}; char realfile[PATH_MAX] = {0}; - snprintf(file, sizeof(file), "%s%smnode.json.bak", pWrapper->path, TD_DIRSEP); - snprintf(realfile, sizeof(realfile), "%s%smnode.json", pWrapper->path, TD_DIRSEP); + snprintf(file, sizeof(file), "%s%smnode.json.bak", pMgmt->path, TD_DIRSEP); + snprintf(realfile, sizeof(realfile), "%s%smnode.json", pMgmt->path, TD_DIRSEP); TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { @@ -125,22 +124,19 @@ int32_t mmWriteFile(SMgmtWrapper *pWrapper, SDCreateMnodeReq *pReq, bool deploye len += snprintf(content + len, maxLen - len, "{\n"); len += snprintf(content + len, maxLen - len, " \"mnodes\": [{\n"); - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - if (pReq != NULL || pMgmt != NULL) { - int8_t replica = (pReq != NULL ? pReq->replica : pMgmt->replica); - for (int32_t i = 0; i < replica; ++i) { - SReplica *pReplica = &pMgmt->replicas[i]; - if (pReq != NULL) { - pReplica = &pReq->replicas[i]; - } - len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pReplica->id); - len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pReplica->fqdn); - len += snprintf(content + len, maxLen - len, " \"port\": %u\n", pReplica->port); - if (i < replica - 1) { - len += snprintf(content + len, maxLen - len, " },{\n"); - } else { - len += snprintf(content + len, maxLen - len, " }],\n"); - } + int8_t replica = (pReq != NULL ? pReq->replica : pMgmt->replica); + for (int32_t i = 0; i < replica; ++i) { + SReplica *pReplica = &pMgmt->replicas[i]; + if (pReq != NULL) { + pReplica = &pReq->replicas[i]; + } + len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pReplica->id); + len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pReplica->fqdn); + len += snprintf(content + len, maxLen - len, " \"port\": %u\n", pReplica->port); + if (i < replica - 1) { + len += snprintf(content + len, maxLen - len, " },{\n"); + } else { + len += snprintf(content + len, maxLen - len, " }],\n"); } } diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c b/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c index ed9384a869562535045f8bfc9e718530854d8372..5548a23c55210b47c4b2147239228a71e99bb8ce 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c @@ -16,15 +16,14 @@ #define _DEFAULT_SOURCE #include "mmInt.h" -void mmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonMmInfo *mmInfo) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; +static void mmGetMonitorInfo(SMnodeMgmt *pMgmt, SMonMmInfo *mmInfo) { mndGetMonitorInfo(pMgmt->pMnode, &mmInfo->cluster, &mmInfo->vgroup, &mmInfo->grant); } -int32_t mmProcessGetMonMmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t mmProcessGetMonitorInfoReq(SMnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonMmInfo mmInfo = {0}; - mmGetMonitorInfo(pWrapper, &mmInfo); - dmGetMonitorSysInfo(&mmInfo.sys); + mmGetMonitorInfo(pMgmt, &mmInfo); + dmGetMonitorSystemInfo(&mmInfo.sys); monGetLogs(&mmInfo.log); int32_t rspLen = tSerializeSMonMmInfo(NULL, 0, &mmInfo); @@ -46,15 +45,14 @@ int32_t mmProcessGetMonMmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { return 0; } -void mmGetMnodeLoads(SMgmtWrapper *pWrapper, SMonMloadInfo *pInfo) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; +static void mmGetMnodeLoads(SMnodeMgmt *pMgmt, SMonMloadInfo *pInfo) { pInfo->isMnode = 1; mndGetLoad(pMgmt->pMnode, &pInfo->load); } -int32_t mmProcessGetMnodeLoadsReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t mmProcessGetLoadsReq(SMnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonMloadInfo mloads = {0}; - mmGetMnodeLoads(pWrapper, &mloads); + mmGetMnodeLoads(pMgmt, &mloads); int32_t rspLen = tSerializeSMonMloadInfo(NULL, 0, &mloads); if (rspLen < 0) { @@ -74,8 +72,7 @@ int32_t mmProcessGetMnodeLoadsReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { return 0; } -int32_t mmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t mmProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDCreateMnodeReq createReq = {0}; @@ -84,14 +81,18 @@ int32_t mmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (createReq.replica <= 1 || createReq.dnodeId != pDnode->data.dnodeId) { + if (createReq.replica <= 1 || (createReq.dnodeId != pInput->dnodeId && pInput->dnodeId != 0)) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to create mnode since %s", terrstr()); return -1; } bool deployed = true; - if (mmWriteFile(pWrapper, &createReq, deployed) != 0) { + + SMnodeMgmt mgmt = {0}; + mgmt.path = pInput->path; + mgmt.name = pInput->name; + if (mmWriteFile(&mgmt, &createReq, deployed) != 0) { dError("failed to write mnode file since %s", terrstr()); return -1; } @@ -99,8 +100,7 @@ int32_t mmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t mmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t mmProcessDropReq(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDDropMnodeReq dropReq = {0}; @@ -109,14 +109,14 @@ int32_t mmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (dropReq.dnodeId != pDnode->data.dnodeId) { + if (pMgmt->dnodeId != 0 && dropReq.dnodeId != pMgmt->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to drop mnode since %s", terrstr()); return -1; } bool deployed = false; - if (mmWriteFile(pWrapper, NULL, deployed) != 0) { + if (mmWriteFile(pMgmt, NULL, deployed) != 0) { dError("failed to write mnode file since %s", terrstr()); return -1; } @@ -125,7 +125,6 @@ int32_t mmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { } int32_t mmProcessAlterReq(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { - SDnode *pDnode = pMgmt->pDnode; SRpcMsg *pReq = &pMsg->rpcMsg; SDAlterMnodeReq alterReq = {0}; @@ -134,104 +133,118 @@ int32_t mmProcessAlterReq(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return -1; } - if (pDnode->data.dnodeId != 0 && alterReq.dnodeId != pDnode->data.dnodeId) { + if (pMgmt->dnodeId != 0 && alterReq.dnodeId != pMgmt->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; - dError("failed to alter mnode since %s, input:%d cur:%d", terrstr(), alterReq.dnodeId, pDnode->data.dnodeId); + dError("failed to alter mnode since %s, input:%d cur:%d", terrstr(), alterReq.dnodeId, pMgmt->dnodeId); return -1; } else { return mmAlter(pMgmt, &alterReq); } } -void mmInitMsgHandle(SMgmtWrapper *pWrapper) { - dmSetMsgHandle(pWrapper, TDMT_MON_MM_INFO, mmProcessMonitorMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MON_MM_LOAD, mmProcessMonitorMsg, DEFAULT_HANDLE); +SArray *mmGetMsgHandles() { + int32_t code = -1; + SArray *pArray = taosArrayInit(64, sizeof(SMgmtHandle)); + if (pArray == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_MON_MM_INFO, mmPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MON_MM_LOAD, mmPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; // Requests handled by DNODE - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_MNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_ALTER_MNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_MNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_QNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_QNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_SNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_SNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_BNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_BNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_VNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_VNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CONFIG_DNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_BNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_BNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_VNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_VNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; // Requests handled by MNODE - dmSetMsgHandle(pWrapper, TDMT_MND_CONNECT, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_ACCT, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_ALTER_ACCT, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_ACCT, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_USER, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_ALTER_USER, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_USER, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_GET_USER_AUTH, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_DNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CONFIG_DNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_DNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_MNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_MNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_QNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_QNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_QNODE_LIST, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_SNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_SNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_BNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_BNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_DB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_DB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_USE_DB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_ALTER_DB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_COMPACT_DB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_FUNC, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_RETRIEVE_FUNC, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_FUNC, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_STB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_ALTER_STB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_STB, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_SMA, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_SMA, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_TABLE_META, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_VGROUP_LIST, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_KILL_QUERY, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_KILL_CONN, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_HEARTBEAT, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_SYSTABLE_RETRIEVE, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_STATUS, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_KILL_TRANS, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_GRANT, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_AUTH, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_ALTER_MNODE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_TOPIC, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_ALTER_TOPIC, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_DROP_TOPIC, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_SUBSCRIBE, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_MQ_COMMIT_OFFSET, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_MQ_ASK_EP, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_VG_CHANGE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_CREATE_STREAM, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASK_DEPLOY_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_GET_DB_CFG, mmProcessReadMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MND_GET_INDEX, mmProcessReadMsg, DEFAULT_HANDLE); + if (dmSetMgmtHandle(pArray, TDMT_MND_CONNECT, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_ACCT, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_ALTER_ACCT, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_ACCT, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_USER, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_ALTER_USER, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_USER, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_GET_USER_AUTH, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_DNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CONFIG_DNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_DNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_MNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_MNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_QNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_QNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_QNODE_LIST, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_SNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_SNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_BNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_BNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_DB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_DB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_USE_DB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_ALTER_DB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_COMPACT_DB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_FUNC, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_RETRIEVE_FUNC, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_FUNC, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_STB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_ALTER_STB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_STB, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_SMA, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_SMA, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_TABLE_META, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_VGROUP_LIST, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_KILL_QUERY, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_KILL_CONN, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_HEARTBEAT, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_SYSTABLE_RETRIEVE, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_STATUS, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_KILL_TRANS, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_TOPIC, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_ALTER_TOPIC, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_DROP_TOPIC, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_SUBSCRIBE, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_MQ_COMMIT_OFFSET, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_MQ_ASK_EP, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_VG_CHANGE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_CREATE_STREAM, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASK_DEPLOY_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_GET_DB_CFG, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MND_GET_INDEX, mmPutNodeMsgToReadQueue, 0) == NULL) goto _OVER; // Requests handled by VNODE - dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_STB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_ALTER_STB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_STB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_SMA_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_SMA_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY, mmProcessQueryMsg, MNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY_CONTINUE, mmProcessQueryMsg, MNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_FETCH, mmProcessQueryMsg, MNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_TASK, mmProcessQueryMsg, MNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY_HEARTBEAT, mmProcessQueryMsg, MNODE_HANDLE); - - dmSetMsgHandle(pWrapper, TDMT_VND_ALTER_VNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_VNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_COMPACT_VNODE_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); + if (dmSetMgmtHandle(pArray, TDMT_VND_CREATE_STB_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_ALTER_STB_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_STB_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CREATE_SMA_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_SMA_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY, mmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY_CONTINUE, mmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_FETCH, mmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_TASK, mmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY_HEARTBEAT, mmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_VND_ALTER_VNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_VNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_COMPACT_VNODE_RSP, mmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + + code = 0; + +_OVER: + if (code != 0) { + taosArrayDestroy(pArray); + return NULL; + } else { + return pArray; + } } diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmInt.c b/source/dnode/mgmt/mgmt_mnode/src/mmInt.c index 0bf846b7fc772952965a43ca2fd979a85bd661a3..8815af2f22944e7495ad7afe0bfcf243e7e65f0c 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmInt.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmInt.c @@ -17,45 +17,35 @@ #include "mmInt.h" #include "wal.h" -static bool mmDeployRequired(SDnode *pDnode) { - if (pDnode->data.dnodeId > 0) return false; - if (pDnode->data.clusterId > 0) return false; - if (strcmp(pDnode->data.localEp, pDnode->data.firstEp) != 0) return false; +static bool mmDeployRequired(const SMgmtInputOpt *pInput) { + if (pInput->dnodeId > 0) return false; + if (pInput->clusterId > 0) return false; + if (strcmp(pInput->localEp, pInput->firstEp) != 0) return false; return true; } -static int32_t mmRequire(SMgmtWrapper *pWrapper, bool *required) { +static int32_t mmRequire(const SMgmtInputOpt *pInput, bool *required) { SMnodeMgmt mgmt = {0}; - mgmt.path = pWrapper->path; + mgmt.path = pInput->path; if (mmReadFile(&mgmt, required) != 0) { return -1; } if (!(*required)) { - *required = mmDeployRequired(pWrapper->pDnode); + *required = mmDeployRequired(pInput); } return 0; } -static void mmInitOption(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) { - SMsgCb msgCb = pMgmt->pDnode->data.msgCb; - msgCb.pWrapper = pMgmt->pWrapper; - msgCb.queueFps[QUERY_QUEUE] = mmPutMsgToQueryQueue; - msgCb.queueFps[READ_QUEUE] = mmPutMsgToReadQueue; - msgCb.queueFps[WRITE_QUEUE] = mmPutMsgToWriteQueue; - msgCb.queueFps[SYNC_QUEUE] = mmPutMsgToWriteQueue; - pOption->msgCb = msgCb; -} - -static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) { - mmInitOption(pMgmt, pOption); +static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, const SMgmtInputOpt *pInput, SMnodeOpt *pOption) { + pOption->msgCb = pMgmt->msgCb; pOption->replica = 1; pOption->selfIndex = 0; SReplica *pReplica = &pOption->replicas[0]; pReplica->id = 1; - pReplica->port = pMgmt->pDnode->data.serverPort; - tstrncpy(pReplica->fqdn, pMgmt->pDnode->data.localFqdn, TSDB_FQDN_LEN); + pReplica->port = pInput->serverPort; + tstrncpy(pReplica->fqdn, pInput->localFqdn, TSDB_FQDN_LEN); pOption->deploy = true; pMgmt->selfIndex = pOption->selfIndex; @@ -64,7 +54,7 @@ static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) { } static void mmBuildOptionForOpen(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) { - mmInitOption(pMgmt, pOption); + pOption->msgCb = pMgmt->msgCb; pOption->selfIndex = pMgmt->selfIndex; pOption->replica = pMgmt->replica; memcpy(&pOption->replicas, pMgmt->replicas, sizeof(SReplica) * TSDB_MAX_REPLICA); @@ -72,8 +62,7 @@ static void mmBuildOptionForOpen(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) { } static int32_t mmBuildOptionFromReq(SMnodeMgmt *pMgmt, SMnodeOpt *pOption, SDCreateMnodeReq *pCreate) { - mmInitOption(pMgmt, pOption); - + pOption->msgCb = pMgmt->msgCb; pOption->replica = pCreate->replica; pOption->selfIndex = -1; for (int32_t i = 0; i < pCreate->replica; ++i) { @@ -81,7 +70,7 @@ static int32_t mmBuildOptionFromReq(SMnodeMgmt *pMgmt, SMnodeOpt *pOption, SDCre pReplica->id = pCreate->replicas[i].id; pReplica->port = pCreate->replicas[i].port; memcpy(pReplica->fqdn, pCreate->replicas[i].fqdn, TSDB_FQDN_LEN); - if (pReplica->id == pMgmt->pDnode->data.dnodeId) { + if (pReplica->id == pMgmt->dnodeId) { pOption->selfIndex = i; } } @@ -109,7 +98,7 @@ int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq) { } bool deployed = true; - if (mmWriteFile(pMgmt->pWrapper, pReq, deployed) != 0) { + if (mmWriteFile(pMgmt, pReq, deployed) != 0) { dError("failed to write mnode file since %s", terrstr()); return -1; } @@ -117,10 +106,7 @@ int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq) { return 0; } -static void mmClose(SMgmtWrapper *pWrapper) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - if (pMgmt == NULL) return; - +static void mmClose(SMnodeMgmt *pMgmt) { dInfo("mnode-mgmt start to cleanup"); if (pMgmt->pMnode != NULL) { mmStopWorker(pMgmt); @@ -128,12 +114,11 @@ static void mmClose(SMgmtWrapper *pWrapper) { pMgmt->pMnode = NULL; } - pWrapper->pMgmt = NULL; taosMemoryFree(pMgmt); dInfo("mnode-mgmt is cleaned up"); } -static int32_t mmOpen(SMgmtWrapper *pWrapper) { +static int32_t mmOpen(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { dInfo("mnode-mgmt start to init"); if (walInit() != 0) { dError("failed to init wal since %s", terrstr()); @@ -146,23 +131,28 @@ static int32_t mmOpen(SMgmtWrapper *pWrapper) { return -1; } - pMgmt->path = pWrapper->path; - pMgmt->pDnode = pWrapper->pDnode; - pMgmt->pWrapper = pWrapper; - pWrapper->pMgmt = pMgmt; + pMgmt->path = pInput->path; + pMgmt->name = pInput->name; + pMgmt->dnodeId = pInput->dnodeId; + pMgmt->msgCb = pInput->msgCb; + pMgmt->msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)mmPutRpcMsgToQueryQueue; + pMgmt->msgCb.queueFps[READ_QUEUE] = (PutToQueueFp)mmPutRpcMsgToReadQueue; + pMgmt->msgCb.queueFps[WRITE_QUEUE] = (PutToQueueFp)mmPutRpcMsgToWriteQueue; + pMgmt->msgCb.queueFps[SYNC_QUEUE] = (PutToQueueFp)mmPutRpcMsgToWriteQueue; + pMgmt->msgCb.pMgmt = pMgmt; bool deployed = false; if (mmReadFile(pMgmt, &deployed) != 0) { dError("failed to read file since %s", terrstr()); - mmClose(pWrapper); + mmClose(pMgmt); return -1; } SMnodeOpt option = {0}; if (!deployed) { dInfo("mnode start to deploy"); - pWrapper->pDnode->data.dnodeId = 1; - mmBuildOptionForDeploy(pMgmt, &option); + pMgmt->dnodeId = 1; + mmBuildOptionForDeploy(pMgmt, pInput, &option); } else { dInfo("mnode start to open"); mmBuildOptionForOpen(pMgmt, &option); @@ -171,55 +161,52 @@ static int32_t mmOpen(SMgmtWrapper *pWrapper) { pMgmt->pMnode = mndOpen(pMgmt->path, &option); if (pMgmt->pMnode == NULL) { dError("failed to open mnode since %s", terrstr()); - mmClose(pWrapper); + mmClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "mnode-impl", "initialized"); + tmsgReportStartup("mnode-impl", "initialized"); if (mmStartWorker(pMgmt) != 0) { dError("failed to start mnode worker since %s", terrstr()); - mmClose(pWrapper); + mmClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "mnode-worker", "initialized"); + tmsgReportStartup("mnode-worker", "initialized"); if (!deployed) { deployed = true; - if (mmWriteFile(pWrapper, NULL, deployed) != 0) { + if (mmWriteFile(pMgmt, NULL, deployed) != 0) { dError("failed to write mnode file since %s", terrstr()); return -1; } } + pOutput->dnodeId = pMgmt->dnodeId; + pOutput->pMgmt = pMgmt; dInfo("mnode-mgmt is initialized"); return 0; } -static int32_t mmStart(SMgmtWrapper *pWrapper) { +static int32_t mmStart(SMnodeMgmt *pMgmt) { dDebug("mnode-mgmt start to run"); - SMnodeMgmt *pMgmt = pWrapper->pMgmt; return mndStart(pMgmt->pMnode); } -static void mmStop(SMgmtWrapper *pWrapper) { +static void mmStop(SMnodeMgmt *pMgmt) { dDebug("mnode-mgmt start to stop"); - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - if (pMgmt != NULL) { - mndStop(pMgmt->pMnode); - } + mndStop(pMgmt->pMnode); } -void mmSetMgmtFp(SMgmtWrapper *pWrapper) { - SMgmtFp mgmtFp = {0}; - mgmtFp.openFp = mmOpen; - mgmtFp.closeFp = mmClose; - mgmtFp.startFp = mmStart; - mgmtFp.stopFp = mmStop; - mgmtFp.createFp = mmProcessCreateReq; - mgmtFp.dropFp = mmProcessDropReq; - mgmtFp.requiredFp = mmRequire; - - mmInitMsgHandle(pWrapper); - pWrapper->name = "mnode"; - pWrapper->fp = mgmtFp; +SMgmtFunc mmGetMgmtFunc() { + SMgmtFunc mgmtFunc = {0}; + mgmtFunc.openFp = mmOpen; + mgmtFunc.closeFp = (NodeCloseFp)mmClose; + mgmtFunc.startFp = (NodeStartFp)mmStart; + mgmtFunc.stopFp = (NodeStopFp)mmStop; + mgmtFunc.createFp = (NodeCreateFp)mmProcessCreateReq; + mgmtFunc.dropFp = (NodeDropFp)mmProcessDropReq; + mgmtFunc.requiredFp = mmRequire; + mgmtFunc.getHandlesFp = mmGetMsgHandles; + + return mgmtFunc; } diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c b/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c index aac5bbc16afdc074d785a15f836f6c9d637bc1ce..f55e02e50a24f5718ecc0d679f7aa7d2f6d3bbee 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c @@ -30,27 +30,27 @@ static inline void mmSendRsp(SNodeMsg *pMsg, int32_t code) { static void mmProcessQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { SMnodeMgmt *pMgmt = pInfo->ahandle; - - int32_t code = -1; - tmsg_t msgType = pMsg->rpcMsg.msgType; - dTrace("msg:%p, get from mnode queue", pMsg); + int32_t code = -1; + tmsg_t msgType = pMsg->rpcMsg.msgType; + bool isRequest = msgType & 1U; + dTrace("msg:%p, get from mnode queue, type:%s", pMsg, TMSG_INFO(msgType)); switch (msgType) { case TDMT_DND_ALTER_MNODE: code = mmProcessAlterReq(pMgmt, pMsg); break; case TDMT_MON_MM_INFO: - code = mmProcessGetMonMmInfoReq(pMgmt->pWrapper, pMsg); + code = mmProcessGetMonitorInfoReq(pMgmt, pMsg); break; case TDMT_MON_MM_LOAD: - code = mmProcessGetMnodeLoadsReq(pMgmt->pWrapper, pMsg); + code = mmProcessGetLoadsReq(pMgmt, pMsg); break; default: pMsg->pNode = pMgmt->pMnode; code = mndProcessMsg(pMsg); } - if (msgType & 1U) { + if (isRequest) { if (pMsg->rpcMsg.handle != NULL && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { if (code != 0 && terrno != 0) code = terrno; mmSendRsp(pMsg, code); @@ -64,62 +64,46 @@ static void mmProcessQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { static void mmProcessQueryQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { SMnodeMgmt *pMgmt = pInfo->ahandle; - + int32_t code = -1; + tmsg_t msgType = pMsg->rpcMsg.msgType; + bool isRequest = msgType & 1U; dTrace("msg:%p, get from mnode-query queue", pMsg); - SRpcMsg *pRpc = &pMsg->rpcMsg; - int32_t code = -1; pMsg->pNode = pMgmt->pMnode; code = mndProcessMsg(pMsg); - if (pRpc->msgType & 1U) { - if (pRpc->handle != NULL && code != 0) { - dError("msg:%p, failed to process since %s", pMsg, terrstr()); + if (isRequest) { + if (pMsg->rpcMsg.handle != NULL && code != 0) { + if (code != 0 && terrno != 0) code = terrno; mmSendRsp(pMsg, code); } } dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code)); - rpcFreeCont(pRpc->pCont); + rpcFreeCont(pMsg->rpcMsg.pCont); taosFreeQitem(pMsg); } -static void mmPutNodeMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) { - dTrace("msg:%p, put into worker %s", pMsg, pWorker->name); +static int32_t mmPutNodeMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) { + dTrace("msg:%p, put into worker %s, type:%s", pMsg, pWorker->name, TMSG_INFO(pMsg->rpcMsg.msgType)); taosWriteQitem(pWorker->queue, pMsg); -} - -int32_t mmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - mmPutNodeMsgToWorker(&pMgmt->writeWorker, pMsg); return 0; } -int32_t mmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - mmPutNodeMsgToWorker(&pMgmt->syncWorker, pMsg); - return 0; -} +int32_t mmPutNodeMsgToWriteQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->writeWorker, pMsg); } -int32_t mmProcessReadMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - mmPutNodeMsgToWorker(&pMgmt->readWorker, pMsg); - return 0; -} +int32_t mmPutNodeMsgToSyncQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->syncWorker, pMsg); } -int32_t mmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - mmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg); - return 0; +int32_t mmPutNodeMsgToReadQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->readWorker, pMsg); } + +int32_t mmPutNodeMsgToQueryQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { return mmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg); } -int32_t mmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - mmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg); - return 0; +int32_t mmPutNodeMsgToMonitorQueue(SMnodeMgmt *pMgmt, SNodeMsg *pMsg) { + return mmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg); } -static int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) { +static inline int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) { SNodeMsg *pMsg = taosAllocateQitem(sizeof(SNodeMsg)); if (pMsg == NULL) return -1; @@ -129,25 +113,19 @@ static int32_t mmPutRpcMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pRpc) { return 0; } -int32_t mmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t mmPutRpcMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) { return mmPutRpcMsgToWorker(&pMgmt->queryWorker, pRpc); } -int32_t mmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t mmPutRpcMsgToWriteQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) { return mmPutRpcMsgToWorker(&pMgmt->writeWorker, pRpc); } -int32_t mmPutMsgToReadQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t mmPutRpcMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) { return mmPutRpcMsgToWorker(&pMgmt->readWorker, pRpc); } -int32_t mmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - SMnodeMgmt *pMgmt = pWrapper->pMgmt; - return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pRpc); -} +int32_t mmPutMsgToSyncQueue(SMnodeMgmt *pMgmt, SRpcMsg *pRpc) { return mmPutRpcMsgToWorker(&pMgmt->syncWorker, pRpc); } int32_t mmStartWorker(SMnodeMgmt *pMgmt) { SSingleWorkerCfg qCfg = { @@ -198,18 +176,16 @@ int32_t mmStartWorker(SMnodeMgmt *pMgmt) { return -1; } - if (tsMultiProcess) { - SSingleWorkerCfg mCfg = { - .min = 1, - .max = 1, - .name = "mnode-monitor", - .fp = (FItem)mmProcessQueue, - .param = pMgmt, - }; - if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { - dError("failed to start mnode mnode-monitor worker since %s", terrstr()); - return -1; - } + SSingleWorkerCfg mCfg = { + .min = 1, + .max = 1, + .name = "mnode-monitor", + .fp = (FItem)mmProcessQueue, + .param = pMgmt, + }; + if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { + dError("failed to start mnode mnode-monitor worker since %s", terrstr()); + return -1; } dDebug("mnode workers are initialized"); diff --git a/source/dnode/mgmt/mgmt_qnode/CMakeLists.txt b/source/dnode/mgmt/mgmt_qnode/CMakeLists.txt index bf31b2afc3349593d3e54ef63c76c74762e87cc5..64f8a45ac428dff21cf241feda0669f87f9b8686 100644 --- a/source/dnode/mgmt/mgmt_qnode/CMakeLists.txt +++ b/source/dnode/mgmt/mgmt_qnode/CMakeLists.txt @@ -5,5 +5,5 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( - mgmt_qnode dnode_interface + mgmt_qnode node_util ) \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_qnode/inc/qmInt.h b/source/dnode/mgmt/mgmt_qnode/inc/qmInt.h index d52fbff683a5c5d43de21151d3e28b3e132e9620..8b48113dd31f3bdfd42887a75c386ced1cba85e9 100644 --- a/source/dnode/mgmt/mgmt_qnode/inc/qmInt.h +++ b/source/dnode/mgmt/mgmt_qnode/inc/qmInt.h @@ -16,7 +16,7 @@ #ifndef _TD_DND_QNODE_INT_H_ #define _TD_DND_QNODE_INT_H_ -#include "dmInt.h" +#include "dmUtil.h" #include "qnode.h" @@ -26,30 +26,31 @@ extern "C" { typedef struct SQnodeMgmt { SQnode *pQnode; - SDnode *pDnode; - SMgmtWrapper *pWrapper; + SMsgCb msgCb; const char *path; + const char *name; + int32_t dnodeId; SSingleWorker queryWorker; SSingleWorker fetchWorker; SSingleWorker monitorWorker; } SQnodeMgmt; // qmHandle.c -void qmInitMsgHandle(SMgmtWrapper *pWrapper); -int32_t qmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t qmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t qmProcessGetMonQmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); +SArray *qmGetMsgHandles(); +int32_t qmProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg); +int32_t qmProcessDropReq(SQnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t qmProcessGetMonitorInfoReq(SQnodeMgmt *pMgmt, SNodeMsg *pReq); // qmWorker.c -int32_t qmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t qmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t qmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype); +int32_t qmPutRpcMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t qmPutRpcMsgToFetchQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t qmGetQueueSize(SQnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype); int32_t qmStartWorker(SQnodeMgmt *pMgmt); void qmStopWorker(SQnodeMgmt *pMgmt); -int32_t qmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t qmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t qmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); +int32_t qmPutNodeMsgToQueryQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t qmPutNodeMsgToFetchQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t qmPutNodeMsgToMonitorQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/mgmt_qnode/src/qmHandle.c b/source/dnode/mgmt/mgmt_qnode/src/qmHandle.c index 11b91f056871eb5065d758c9c8d8a58b48828e19..c500176b15143d4e7fd744a99403fd5943ffa642 100644 --- a/source/dnode/mgmt/mgmt_qnode/src/qmHandle.c +++ b/source/dnode/mgmt/mgmt_qnode/src/qmHandle.c @@ -16,12 +16,12 @@ #define _DEFAULT_SOURCE #include "qmInt.h" -void qmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonQmInfo *qmInfo) {} +static void qmGetMonitorInfo(SQnodeMgmt *pMgmt, SMonQmInfo *qmInfo) {} -int32_t qmProcessGetMonQmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t qmProcessGetMonitorInfoReq(SQnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonQmInfo qmInfo = {0}; - qmGetMonitorInfo(pWrapper, &qmInfo); - dmGetMonitorSysInfo(&qmInfo.sys); + qmGetMonitorInfo(pMgmt, &qmInfo); + dmGetMonitorSystemInfo(&qmInfo.sys); monGetLogs(&qmInfo.log); int32_t rspLen = tSerializeSMonQmInfo(NULL, 0, &qmInfo); @@ -43,8 +43,7 @@ int32_t qmProcessGetMonQmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { return 0; } -int32_t qmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t qmProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDCreateQnodeReq createReq = {0}; @@ -53,14 +52,14 @@ int32_t qmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (createReq.dnodeId != pDnode->data.dnodeId) { + if (pInput->dnodeId != 0 && createReq.dnodeId != pInput->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to create qnode since %s", terrstr()); return -1; } bool deployed = true; - if (dmWriteFile(pWrapper, deployed) != 0) { + if (dmWriteFile(pInput->path, pInput->name, deployed) != 0) { dError("failed to write qnode file since %s", terrstr()); return -1; } @@ -68,8 +67,7 @@ int32_t qmProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t qmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t qmProcessDropReq(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDDropQnodeReq dropReq = {0}; @@ -78,14 +76,14 @@ int32_t qmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (dropReq.dnodeId != pDnode->data.dnodeId) { + if (pMgmt->dnodeId != 0 && dropReq.dnodeId != pMgmt->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to drop qnode since %s", terrstr()); return -1; } bool deployed = false; - if (dmWriteFile(pWrapper, deployed) != 0) { + if (dmWriteFile(pMgmt->path, pMgmt->name, deployed) != 0) { dError("failed to write qnode file since %s", terrstr()); return -1; } @@ -93,18 +91,31 @@ int32_t qmProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -void qmInitMsgHandle(SMgmtWrapper *pWrapper) { - dmSetMsgHandle(pWrapper, TDMT_MON_QM_INFO, qmProcessMonitorMsg, DEFAULT_HANDLE); +SArray *qmGetMsgHandles() { + int32_t code = -1; + SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle)); + if (pArray == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_MON_QM_INFO, qmPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; // Requests handled by VNODE - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY, qmProcessQueryMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY_CONTINUE, qmProcessQueryMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_FETCH, qmProcessFetchMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_FETCH_RSP, qmProcessFetchMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY_HEARTBEAT, qmProcessFetchMsg, QNODE_HANDLE); - - dmSetMsgHandle(pWrapper, TDMT_VND_RES_READY, qmProcessFetchMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASKS_STATUS, qmProcessFetchMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CANCEL_TASK, qmProcessFetchMsg, QNODE_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_TASK, qmProcessFetchMsg, QNODE_HANDLE); + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY, qmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY_CONTINUE, qmPutNodeMsgToQueryQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_FETCH, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_FETCH_RSP, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY_HEARTBEAT, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_VND_RES_READY, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASKS_STATUS, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CANCEL_TASK, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_TASK, qmPutNodeMsgToFetchQueue, 1) == NULL) goto _OVER; + + code = 0; +_OVER: + if (code != 0) { + taosArrayDestroy(pArray); + return NULL; + } else { + return pArray; + } } diff --git a/source/dnode/mgmt/mgmt_qnode/src/qmInt.c b/source/dnode/mgmt/mgmt_qnode/src/qmInt.c index d03f001a8d71c7a6fcfa329c6d667db7157ea461..5e86a307321f583849de0f6c412f22a4da05a5ad 100644 --- a/source/dnode/mgmt/mgmt_qnode/src/qmInt.c +++ b/source/dnode/mgmt/mgmt_qnode/src/qmInt.c @@ -16,21 +16,13 @@ #define _DEFAULT_SOURCE #include "qmInt.h" -static int32_t qmRequire(SMgmtWrapper *pWrapper, bool *required) { return dmReadFile(pWrapper, required); } - -static void qmInitOption(SQnodeMgmt *pMgmt, SQnodeOpt *pOption) { - SMsgCb msgCb = pMgmt->pDnode->data.msgCb; - msgCb.pWrapper = pMgmt->pWrapper; - msgCb.queueFps[QUERY_QUEUE] = qmPutMsgToQueryQueue; - msgCb.queueFps[FETCH_QUEUE] = qmPutMsgToFetchQueue; - msgCb.qsizeFp = qmGetQueueSize; - pOption->msgCb = msgCb; +static int32_t qmRequire(const SMgmtInputOpt *pInput, bool *required) { + return dmReadFile(pInput->path, pInput->name, required); } -static void qmClose(SMgmtWrapper *pWrapper) { - SQnodeMgmt *pMgmt = pWrapper->pMgmt; - if (pMgmt == NULL) return; +static void qmInitOption(SQnodeMgmt *pMgmt, SQnodeOpt *pOption) { pOption->msgCb = pMgmt->msgCb; } +static void qmClose(SQnodeMgmt *pMgmt) { dInfo("qnode-mgmt start to cleanup"); if (pMgmt->pQnode != NULL) { qmStopWorker(pMgmt); @@ -38,12 +30,11 @@ static void qmClose(SMgmtWrapper *pWrapper) { pMgmt->pQnode = NULL; } - pWrapper->pMgmt = NULL; taosMemoryFree(pMgmt); dInfo("qnode-mgmt is cleaned up"); } -static int32_t qmOpen(SMgmtWrapper *pWrapper) { +static int32_t qmOpen(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { dInfo("qnode-mgmt start to init"); SQnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SQnodeMgmt)); if (pMgmt == NULL) { @@ -51,41 +42,51 @@ static int32_t qmOpen(SMgmtWrapper *pWrapper) { return -1; } - pMgmt->path = pWrapper->path; - pMgmt->pDnode = pWrapper->pDnode; - pMgmt->pWrapper = pWrapper; - pWrapper->pMgmt = pMgmt; + pMgmt->path = pInput->path; + pMgmt->name = pInput->name; + pMgmt->dnodeId = pInput->dnodeId; + pMgmt->msgCb = pInput->msgCb; + pMgmt->msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qmPutRpcMsgToQueryQueue; + pMgmt->msgCb.queueFps[FETCH_QUEUE] = (PutToQueueFp)qmPutRpcMsgToFetchQueue; + pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)qmGetQueueSize; + pMgmt->msgCb.pMgmt = pMgmt; SQnodeOpt option = {0}; qmInitOption(pMgmt, &option); pMgmt->pQnode = qndOpen(&option); if (pMgmt->pQnode == NULL) { dError("failed to open qnode since %s", terrstr()); - qmClose(pWrapper); + qmClose(pMgmt); + return -1; + } + tmsgReportStartup("qnode-impl", "initialized"); + + if (udfcOpen() != 0) { + dError("qnode can not open udfc"); + qmClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "qnode-impl", "initialized"); if (qmStartWorker(pMgmt) != 0) { dError("failed to start qnode worker since %s", terrstr()); - qmClose(pWrapper); + qmClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "qnode-worker", "initialized"); + tmsgReportStartup("qnode-worker", "initialized"); + pOutput->pMgmt = pMgmt; dInfo("qnode-mgmt is initialized"); return 0; } -void qmSetMgmtFp(SMgmtWrapper *pWrapper) { - SMgmtFp mgmtFp = {0}; - mgmtFp.openFp = qmOpen; - mgmtFp.closeFp = qmClose; - mgmtFp.createFp = qmProcessCreateReq; - mgmtFp.dropFp = qmProcessDropReq; - mgmtFp.requiredFp = qmRequire; +SMgmtFunc qmGetMgmtFunc() { + SMgmtFunc mgmtFunc = {0}; + mgmtFunc.openFp = qmOpen; + mgmtFunc.closeFp = (NodeCloseFp)qmClose; + mgmtFunc.createFp = (NodeCreateFp)qmProcessCreateReq; + mgmtFunc.dropFp = (NodeDropFp)qmProcessDropReq; + mgmtFunc.requiredFp = qmRequire; + mgmtFunc.getHandlesFp = qmGetMsgHandles; - qmInitMsgHandle(pWrapper); - pWrapper->name = "qnode"; - pWrapper->fp = mgmtFp; + return mgmtFunc; } diff --git a/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c b/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c index 965d35cb3e4546bdf7e4b48d4c4d9b13aef9c6ca..c95a4e8292cbf28c573c0f6e6eb8c47ce80c3c66 100644 --- a/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c +++ b/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c @@ -36,7 +36,7 @@ static void qmProcessMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { int32_t code = -1; if (pMsg->rpcMsg.msgType == TDMT_MON_QM_INFO) { - code = qmProcessGetMonQmInfoReq(pMgmt->pWrapper, pMsg); + code = qmProcessGetMonitorInfoReq(pMgmt, pMsg); } else { terrno = TSDB_CODE_MSG_NOT_PROCESSED; } @@ -83,27 +83,22 @@ static void qmProcessFetchQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { taosFreeQitem(pMsg); } -static void qmPutMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) { +static int32_t qmPutNodeMsgToWorker(SSingleWorker *pWorker, SNodeMsg *pMsg) { dTrace("msg:%p, put into worker %s", pMsg, pWorker->name); taosWriteQitem(pWorker->queue, pMsg); + return 0; } -int32_t qmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SQnodeMgmt *pMgmt = pWrapper->pMgmt; - qmPutMsgToWorker(&pMgmt->queryWorker, pMsg); - return 0; +int32_t qmPutNodeMsgToQueryQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) { + return qmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg); } -int32_t qmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SQnodeMgmt *pMgmt = pWrapper->pMgmt; - qmPutMsgToWorker(&pMgmt->fetchWorker, pMsg); - return 0; +int32_t qmPutNodeMsgToFetchQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) { + return qmPutNodeMsgToWorker(&pMgmt->fetchWorker, pMsg); } -int32_t qmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SQnodeMgmt *pMgmt = pWrapper->pMgmt; - qmPutMsgToWorker(&pMgmt->monitorWorker, pMsg); - return 0; +int32_t qmPutNodeMsgToMonitorQueue(SQnodeMgmt *pMgmt, SNodeMsg *pMsg) { + return qmPutNodeMsgToWorker(&pMgmt->monitorWorker, pMsg); } static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SRpcMsg *pRpc) { @@ -118,19 +113,16 @@ static int32_t qmPutRpcMsgToWorker(SQnodeMgmt *pMgmt, SSingleWorker *pWorker, SR return 0; } -int32_t qmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - SQnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t qmPutRpcMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pRpc) { return qmPutRpcMsgToWorker(pMgmt, &pMgmt->queryWorker, pRpc); } -int32_t qmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - SQnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t qmPutRpcMsgToFetchQueue(SQnodeMgmt *pMgmt, SRpcMsg *pRpc) { return qmPutRpcMsgToWorker(pMgmt, &pMgmt->fetchWorker, pRpc); } -int32_t qmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) { - int32_t size = -1; - SQnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t qmGetQueueSize(SQnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) { + int32_t size = -1; switch (qtype) { case QUERY_QUEUE: @@ -173,18 +165,16 @@ int32_t qmStartWorker(SQnodeMgmt *pMgmt) { return -1; } - if (tsMultiProcess) { - SSingleWorkerCfg mCfg = { - .min = 1, - .max = 1, - .name = "qnode-monitor", - .fp = (FItem)qmProcessMonitorQueue, - .param = pMgmt, - }; - if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { - dError("failed to start qnode-monitor worker since %s", terrstr()); - return -1; - } + SSingleWorkerCfg mCfg = { + .min = 1, + .max = 1, + .name = "qnode-monitor", + .fp = (FItem)qmProcessMonitorQueue, + .param = pMgmt, + }; + if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { + dError("failed to start qnode-monitor worker since %s", terrstr()); + return -1; } dDebug("qnode workers are initialized"); diff --git a/source/dnode/mgmt/mgmt_snode/CMakeLists.txt b/source/dnode/mgmt/mgmt_snode/CMakeLists.txt index b8a99c9d4df894c58854bef6a56b9b3daebcff6a..62dc41a0aec602f826ccdf5ea00f55691da62483 100644 --- a/source/dnode/mgmt/mgmt_snode/CMakeLists.txt +++ b/source/dnode/mgmt/mgmt_snode/CMakeLists.txt @@ -5,5 +5,5 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( - mgmt_snode dnode_interface + mgmt_snode node_util ) \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_snode/inc/smInt.h b/source/dnode/mgmt/mgmt_snode/inc/smInt.h index 9eb48af733f5947b9318972719d7fae8c97a1119..a1ab9ba0776e206fd439e5389fb2b97d2d4899f1 100644 --- a/source/dnode/mgmt/mgmt_snode/inc/smInt.h +++ b/source/dnode/mgmt/mgmt_snode/inc/smInt.h @@ -16,7 +16,7 @@ #ifndef _TD_DND_SNODE_INT_H_ #define _TD_DND_SNODE_INT_H_ -#include "dmInt.h" +#include "dmUtil.h" #include "snode.h" @@ -26,9 +26,10 @@ extern "C" { typedef struct SSnodeMgmt { SSnode *pSnode; - SDnode *pDnode; - SMgmtWrapper *pWrapper; + SMsgCb msgCb; const char *path; + const char *name; + int32_t dnodeId; SRWLatch latch; int8_t uniqueWorkerInUse; SArray *uniqueWorkers; // SArray @@ -37,19 +38,19 @@ typedef struct SSnodeMgmt { } SSnodeMgmt; // smHandle.c -void smInitMsgHandle(SMgmtWrapper *pWrapper); -int32_t smProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t smProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t smProcessGetMonSmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); +SArray *smGetMsgHandles(); +int32_t smProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg); +int32_t smProcessDropReq(SSnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t smProcessGetMonitorInfoReq(SSnodeMgmt *pMgmt, SNodeMsg *pReq); // smWorker.c int32_t smStartWorker(SSnodeMgmt *pMgmt); void smStopWorker(SSnodeMgmt *pMgmt); -int32_t smProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t smProcessUniqueMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t smProcessSharedMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t smProcessExecMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t smProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); +int32_t smPutNodeMsgToMgmtQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t smPutNodeMsgToUniqueQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t smPutNodeMsgToSharedQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t smPutNodeMsgToExecQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t smPutNodeMsgToMonitorQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/mgmt_snode/src/smHandle.c b/source/dnode/mgmt/mgmt_snode/src/smHandle.c index defc5ab136b4703273f649e65385c6c3b291a92d..b0f0b73cbc2cdfda06fbe5853ebc92129c3552ec 100644 --- a/source/dnode/mgmt/mgmt_snode/src/smHandle.c +++ b/source/dnode/mgmt/mgmt_snode/src/smHandle.c @@ -16,12 +16,12 @@ #define _DEFAULT_SOURCE #include "smInt.h" -void smGetMonitorInfo(SMgmtWrapper *pWrapper, SMonSmInfo *smInfo) {} +static void smGetMonitorInfo(SSnodeMgmt *pMgmt, SMonSmInfo *smInfo) {} -int32_t smProcessGetMonSmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t smProcessGetMonitorInfoReq(SSnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonSmInfo smInfo = {0}; - smGetMonitorInfo(pWrapper, &smInfo); - dmGetMonitorSysInfo(&smInfo.sys); + smGetMonitorInfo(pMgmt, &smInfo); + dmGetMonitorSystemInfo(&smInfo.sys); monGetLogs(&smInfo.log); int32_t rspLen = tSerializeSMonSmInfo(NULL, 0, &smInfo); @@ -43,8 +43,7 @@ int32_t smProcessGetMonSmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { return 0; } -int32_t smProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t smProcessCreateReq(const SMgmtInputOpt *pInput, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDCreateSnodeReq createReq = {0}; @@ -53,14 +52,14 @@ int32_t smProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (createReq.dnodeId != pDnode->data.dnodeId) { + if (pInput->dnodeId != 0 && createReq.dnodeId != pInput->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to create snode since %s", terrstr()); return -1; } bool deployed = true; - if (dmWriteFile(pWrapper, deployed) != 0) { + if (dmWriteFile(pInput->path, pInput->name, deployed) != 0) { dError("failed to write snode file since %s", terrstr()); return -1; } @@ -68,8 +67,7 @@ int32_t smProcessCreateReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t smProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SDnode *pDnode = pWrapper->pDnode; +int32_t smProcessDropReq(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDDropSnodeReq dropReq = {0}; @@ -78,14 +76,14 @@ int32_t smProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return -1; } - if (dropReq.dnodeId != pDnode->data.dnodeId) { + if (pMgmt->dnodeId != 0 && dropReq.dnodeId != pMgmt->dnodeId) { terrno = TSDB_CODE_INVALID_OPTION; dError("failed to drop snode since %s", terrstr()); return -1; } bool deployed = false; - if (dmWriteFile(pWrapper, deployed) != 0) { + if (dmWriteFile(pMgmt->path, pMgmt->name, deployed) != 0) { dError("failed to write snode file since %s", terrstr()); return -1; } @@ -93,10 +91,23 @@ int32_t smProcessDropReq(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -void smInitMsgHandle(SMgmtWrapper *pWrapper) { - dmSetMsgHandle(pWrapper, TDMT_MON_SM_INFO, smProcessMonitorMsg, DEFAULT_HANDLE); +SArray *smGetMsgHandles() { + int32_t code = -1; + SArray *pArray = taosArrayInit(4, sizeof(SMgmtHandle)); + if (pArray == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_MON_SM_INFO, smPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; // Requests handled by SNODE - dmSetMsgHandle(pWrapper, TDMT_SND_TASK_DEPLOY, smProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_SND_TASK_EXEC, smProcessExecMsg, DEFAULT_HANDLE); + if (dmSetMgmtHandle(pArray, TDMT_SND_TASK_DEPLOY, smPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_SND_TASK_EXEC, smPutNodeMsgToExecQueue, 0) == NULL) goto _OVER; + + code = 0; +_OVER: + if (code != 0) { + taosArrayDestroy(pArray); + return NULL; + } else { + return pArray; + } } diff --git a/source/dnode/mgmt/mgmt_snode/src/smInt.c b/source/dnode/mgmt/mgmt_snode/src/smInt.c index 26300a9fe3f22eda80354ab7188b32676f7a764c..5b9f2fc7f7055686f08022f69b845c8f3b34c426 100644 --- a/source/dnode/mgmt/mgmt_snode/src/smInt.c +++ b/source/dnode/mgmt/mgmt_snode/src/smInt.c @@ -17,34 +17,25 @@ #include "smInt.h" #include "libs/function/function.h" -static int32_t smRequire(SMgmtWrapper *pWrapper, bool *required) { return dmReadFile(pWrapper, required); } - -static void smInitOption(SSnodeMgmt *pMgmt, SSnodeOpt *pOption) { - SMsgCb msgCb = pMgmt->pDnode->data.msgCb; - msgCb.pWrapper = pMgmt->pWrapper; - pOption->msgCb = msgCb; +static int32_t smRequire(const SMgmtInputOpt *pInput, bool *required) { + return dmReadFile(pInput->path, pInput->name, required); } -static void smClose(SMgmtWrapper *pWrapper) { - SSnodeMgmt *pMgmt = pWrapper->pMgmt; - if (pMgmt == NULL) return; +static void smInitOption(SSnodeMgmt *pMgmt, SSnodeOpt *pOption) { pOption->msgCb = pMgmt->msgCb; } +static void smClose(SSnodeMgmt *pMgmt) { dInfo("snode-mgmt start to cleanup"); - - udfcClose(); - if (pMgmt->pSnode != NULL) { smStopWorker(pMgmt); sndClose(pMgmt->pSnode); pMgmt->pSnode = NULL; } - pWrapper->pMgmt = NULL; taosMemoryFree(pMgmt); dInfo("snode-mgmt is cleaned up"); } -int32_t smOpen(SMgmtWrapper *pWrapper) { +int32_t smOpen(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { dInfo("snode-mgmt start to init"); SSnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SSnodeMgmt)); if (pMgmt == NULL) { @@ -52,42 +43,47 @@ int32_t smOpen(SMgmtWrapper *pWrapper) { return -1; } - pMgmt->path = pWrapper->path; - pMgmt->pDnode = pWrapper->pDnode; - pMgmt->pWrapper = pWrapper; - pWrapper->pMgmt = pMgmt; + pMgmt->path = pInput->path; + pMgmt->name = pInput->name; + pMgmt->dnodeId = pInput->dnodeId; + pMgmt->msgCb = pInput->msgCb; + pMgmt->msgCb.pMgmt = pMgmt; SSnodeOpt option = {0}; smInitOption(pMgmt, &option); pMgmt->pSnode = sndOpen(pMgmt->path, &option); if (pMgmt->pSnode == NULL) { dError("failed to open snode since %s", terrstr()); + smClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "snode-impl", "initialized"); + tmsgReportStartup("snode-impl", "initialized"); if (smStartWorker(pMgmt) != 0) { dError("failed to start snode worker since %s", terrstr()); + smClose(pMgmt); return -1; } - dmReportStartup(pWrapper->pDnode, "snode-worker", "initialized"); + tmsgReportStartup("snode-worker", "initialized"); if (udfcOpen() != 0) { dError("failed to open udfc in snode"); + smClose(pMgmt); + return -1; } + pOutput->pMgmt = pMgmt; return 0; } -void smSetMgmtFp(SMgmtWrapper *pWrapper) { - SMgmtFp mgmtFp = {0}; - mgmtFp.openFp = smOpen; - mgmtFp.closeFp = smClose; - mgmtFp.createFp = smProcessCreateReq; - mgmtFp.dropFp = smProcessDropReq; - mgmtFp.requiredFp = smRequire; +SMgmtFunc smGetMgmtFunc() { + SMgmtFunc mgmtFunc = {0}; + mgmtFunc.openFp = smOpen; + mgmtFunc.closeFp = (NodeCloseFp)smClose; + mgmtFunc.createFp = (NodeCreateFp)smProcessCreateReq; + mgmtFunc.dropFp = (NodeDropFp)smProcessDropReq; + mgmtFunc.requiredFp = smRequire; + mgmtFunc.getHandlesFp = smGetMsgHandles; - smInitMsgHandle(pWrapper); - pWrapper->name = "snode"; - pWrapper->fp = mgmtFp; + return mgmtFunc; } diff --git a/source/dnode/mgmt/mgmt_snode/src/smWorker.c b/source/dnode/mgmt/mgmt_snode/src/smWorker.c index 2ae439bbd6a081bd21036bb3667d5aa0645df896..90e20f5fc5d0c6fbed35b940c5066b069ded84e5 100644 --- a/source/dnode/mgmt/mgmt_snode/src/smWorker.c +++ b/source/dnode/mgmt/mgmt_snode/src/smWorker.c @@ -36,7 +36,7 @@ static void smProcessMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { int32_t code = -1; if (pMsg->rpcMsg.msgType == TDMT_MON_SM_INFO) { - code = smProcessGetMonSmInfoReq(pMgmt->pWrapper, pMsg); + code = smProcessGetMonitorInfoReq(pMgmt, pMsg); } else { terrno = TSDB_CODE_MSG_NOT_PROCESSED; } @@ -121,18 +121,16 @@ int32_t smStartWorker(SSnodeMgmt *pMgmt) { return -1; } - if (tsMultiProcess) { - SSingleWorkerCfg mCfg = { - .min = 1, - .max = 1, - .name = "snode-monitor", - .fp = (FItem)smProcessMonitorQueue, - .param = pMgmt, - }; - if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { - dError("failed to start snode-monitor worker since %s", terrstr()); - return -1; - } + SSingleWorkerCfg mCfg = { + .min = 1, + .max = 1, + .name = "snode-monitor", + .fp = (FItem)smProcessMonitorQueue, + .param = pMgmt, + }; + if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { + dError("failed to start snode-monitor worker since %s", terrstr()); + return -1; } dDebug("snode workers are initialized"); @@ -163,8 +161,7 @@ static FORCE_INLINE int32_t smGetSWTypeFromMsg(SRpcMsg *pMsg) { return 0; } -int32_t smProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SSnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t smPutNodeMsgToMgmtQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) { SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, 0); if (pWorker == NULL) { terrno = TSDB_CODE_INVALID_MSG; @@ -176,8 +173,7 @@ int32_t smProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t smProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SSnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t smPutNodeMsgToMonitorQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) { SSingleWorker *pWorker = &pMgmt->monitorWorker; dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name); @@ -185,8 +181,7 @@ int32_t smProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t smProcessUniqueMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SSnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t smPutNodeMsgToUniqueQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) { int32_t index = smGetSWIdFromMsg(&pMsg->rpcMsg); SMultiWorker *pWorker = taosArrayGetP(pMgmt->uniqueWorkers, index); if (pWorker == NULL) { @@ -199,8 +194,7 @@ int32_t smProcessUniqueMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t smProcessSharedMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SSnodeMgmt *pMgmt = pWrapper->pMgmt; +int32_t smPutNodeMsgToSharedQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) { SSingleWorker *pWorker = &pMgmt->sharedWorker; dTrace("msg:%p, put into worker:%s", pMsg, pWorker->name); @@ -208,11 +202,11 @@ int32_t smProcessSharedMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -int32_t smProcessExecMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { +int32_t smPutNodeMsgToExecQueue(SSnodeMgmt *pMgmt, SNodeMsg *pMsg) { int32_t workerType = smGetSWTypeFromMsg(&pMsg->rpcMsg); if (workerType == SND_WORKER_TYPE__SHARED) { - return smProcessSharedMsg(pWrapper, pMsg); + return smPutNodeMsgToSharedQueue(pMgmt, pMsg); } else { - return smProcessUniqueMsg(pWrapper, pMsg); + return smPutNodeMsgToUniqueQueue(pMgmt, pMsg); } } diff --git a/source/dnode/mgmt/mgmt_vnode/CMakeLists.txt b/source/dnode/mgmt/mgmt_vnode/CMakeLists.txt index 55a76cf772ea66bdceded16f50e49001485dfbbd..15b822ad92e5367bcf15196f927e77d49a63f1bd 100644 --- a/source/dnode/mgmt/mgmt_vnode/CMakeLists.txt +++ b/source/dnode/mgmt/mgmt_vnode/CMakeLists.txt @@ -5,5 +5,5 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( - mgmt_vnode dnode_interface + mgmt_vnode node_util ) \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h b/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h index 5d8ec50e81b8df13285733abdbbde1ad49f9f6e5..ca43ef8c223a9675ee4b150655623e37fccf4957 100644 --- a/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h +++ b/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h @@ -16,7 +16,7 @@ #ifndef _TD_DND_VNODES_INT_H_ #define _TD_DND_VNODES_INT_H_ -#include "dmInt.h" +#include "dmUtil.h" #include "sync.h" #include "vnode.h" @@ -25,14 +25,11 @@ extern "C" { #endif -typedef struct SVnodesMgmt { - SHashObj *hash; - SRWLatch latch; - SVnodesStat state; +typedef struct SVnodeMgmt { + SMsgCb msgCb; const char *path; - SDnode *pDnode; - SMgmtWrapper *pWrapper; - STfs *pTfs; + const char *name; + int32_t dnodeId; SQWorkerPool queryPool; SQWorkerPool fetchPool; SWWorkerPool syncPool; @@ -40,7 +37,11 @@ typedef struct SVnodesMgmt { SWWorkerPool mergePool; SSingleWorker mgmtWorker; SSingleWorker monitorWorker; -} SVnodesMgmt; + SHashObj *hash; + SRWLatch latch; + SVnodesStat state; + STfs *pTfs; +} SVnodeMgmt; typedef struct { int32_t vgId; @@ -63,7 +64,6 @@ typedef struct { STaosQueue *pQueryQ; STaosQueue *pFetchQ; STaosQueue *pMergeQ; - SMgmtWrapper *pWrapper; } SVnodeObj; typedef struct { @@ -72,50 +72,49 @@ typedef struct { int32_t failed; int32_t threadIndex; TdThread thread; - SVnodesMgmt *pMgmt; + SVnodeMgmt *pMgmt; SWrapperCfg *pCfgs; } SVnodeThread; // vmInt.c -SVnodeObj *vmAcquireVnode(SVnodesMgmt *pMgmt, int32_t vgId); -void vmReleaseVnode(SVnodesMgmt *pMgmt, SVnodeObj *pVnode); -int32_t vmOpenVnode(SVnodesMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl); -void vmCloseVnode(SVnodesMgmt *pMgmt, SVnodeObj *pVnode); +SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId); +void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode); +int32_t vmOpenVnode(SVnodeMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl); +void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode); // vmHandle.c -void vmInitMsgHandle(SMgmtWrapper *pWrapper); -int32_t vmProcessCreateVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pReq); -int32_t vmProcessDropVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pReq); -int32_t vmProcessGetMonVmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); -int32_t vmProcessGetVnodeLoadsReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq); -void vmGetVnodeLoads(SMgmtWrapper *pWrapper, SMonVloadInfo *pInfo); +SArray *vmGetMsgHandles(); +int32_t vmProcessCreateVnodeReq(SVnodeMgmt *pMgmt, SNodeMsg *pReq); +int32_t vmProcessDropVnodeReq(SVnodeMgmt *pMgmt, SNodeMsg *pReq); +int32_t vmProcessGetMonitorInfoReq(SVnodeMgmt *pMgmt, SNodeMsg *pReq); +int32_t vmProcessGetLoadsReq(SVnodeMgmt *pMgmt, SNodeMsg *pReq); // vmFile.c -int32_t vmGetVnodeListFromFile(SVnodesMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes); -int32_t vmWriteVnodeListToFile(SVnodesMgmt *pMgmt); -SVnodeObj **vmGetVnodeListFromHash(SVnodesMgmt *pMgmt, int32_t *numOfVnodes); +int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes); +int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt); +SVnodeObj **vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes); // vmWorker.c -int32_t vmStartWorker(SVnodesMgmt *pMgmt); -void vmStopWorker(SVnodesMgmt *pMgmt); -int32_t vmAllocQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode); -void vmFreeQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode); - -int32_t vmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t vmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t vmPutMsgToApplyQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t vmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t vmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t vmPutMsgToMergeQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); -int32_t vmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype); - -int32_t vmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t vmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t vmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t vmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t vmProcessMergeMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); -int32_t vmProcessMgmtMsg(SMgmtWrapper *pWrappert, SNodeMsg *pMsg); -int32_t vmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg); +int32_t vmStartWorker(SVnodeMgmt *pMgmt); +void vmStopWorker(SVnodeMgmt *pMgmt); +int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode); +void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode); + +int32_t vmPutRpcMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t vmPutRpcMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t vmPutRpcMsgToApplyQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t vmPutRpcMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t vmPutRpcMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t vmPutRpcMsgToMergeQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg); +int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype); + +int32_t vmPutNodeMsgToWriteQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t vmPutNodeMsgToSyncQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t vmPutNodeMsgToQueryQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t vmPutNodeMsgToFetchQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t vmPutNodeMsgToMergeQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t vmPutNodeMsgToMgmtQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); +int32_t vmPutNodeMsgToMonitorQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmFile.c b/source/dnode/mgmt/mgmt_vnode/src/vmFile.c index ba4293eeb2ceb6a775281d624956d107643d7445..f6c7bb33e61595f886552baae062401c53aacf77 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmFile.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmFile.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "vmInt.h" -SVnodeObj **vmGetVnodeListFromHash(SVnodesMgmt *pMgmt, int32_t *numOfVnodes) { +SVnodeObj **vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes) { taosRLockLatch(&pMgmt->latch); int32_t num = 0; @@ -44,7 +44,7 @@ SVnodeObj **vmGetVnodeListFromHash(SVnodesMgmt *pMgmt, int32_t *numOfVnodes) { return pVnodes; } -int32_t vmGetVnodeListFromFile(SVnodesMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) { +int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) { int32_t code = TSDB_CODE_INVALID_JSON_FORMAT; int32_t len = 0; int32_t maxLen = 1024 * 1024; @@ -139,9 +139,9 @@ _OVER: return code; } -int32_t vmWriteVnodeListToFile(SVnodesMgmt *pMgmt) { - char file[PATH_MAX]; - char realfile[PATH_MAX]; +int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) { + char file[PATH_MAX] = {0}; + char realfile[PATH_MAX] = {0}; snprintf(file, sizeof(file), "%s%svnodes.json.bak", pMgmt->path, TD_DIRSEP); snprintf(realfile, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP); diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c index 528f6a0ffe22d381b537c4c04eea985dc4aeae78..0814568b7324af50ad7847b4ab910e6d42752d92 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c @@ -16,9 +16,7 @@ #define _DEFAULT_SOURCE #include "vmInt.h" -void vmGetVnodeLoads(SMgmtWrapper *pWrapper, SMonVloadInfo *pInfo) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; - +static void vmGetVnodeLoads(SVnodeMgmt *pMgmt, SMonVloadInfo *pInfo) { pInfo->pVloads = taosArrayInit(pMgmt->state.totalVnodes, sizeof(SVnodeLoad)); if (pInfo->pVloads == NULL) return; @@ -39,11 +37,9 @@ void vmGetVnodeLoads(SMgmtWrapper *pWrapper, SMonVloadInfo *pInfo) { taosRUnLockLatch(&pMgmt->latch); } -void vmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonVmInfo *pInfo) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; - +static void vmGetMonitorInfo(SVnodeMgmt *pMgmt, SMonVmInfo *pInfo) { SMonVloadInfo vloads = {0}; - vmGetVnodeLoads(pWrapper, &vloads); + vmGetVnodeLoads(pMgmt, &vloads); SArray *pVloads = vloads.pVloads; if (pVloads == NULL) return; @@ -86,10 +82,10 @@ void vmGetMonitorInfo(SMgmtWrapper *pWrapper, SMonVmInfo *pInfo) { taosArrayDestroy(pVloads); } -int32_t vmProcessGetMonVmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t vmProcessGetMonitorInfoReq(SVnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonVmInfo vmInfo = {0}; - vmGetMonitorInfo(pWrapper, &vmInfo); - dmGetMonitorSysInfo(&vmInfo.sys); + vmGetMonitorInfo(pMgmt, &vmInfo); + dmGetMonitorSystemInfo(&vmInfo.sys); monGetLogs(&vmInfo.log); int32_t rspLen = tSerializeSMonVmInfo(NULL, 0, &vmInfo); @@ -111,9 +107,9 @@ int32_t vmProcessGetMonVmInfoReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { return 0; } -int32_t vmProcessGetVnodeLoadsReq(SMgmtWrapper *pWrapper, SNodeMsg *pReq) { +int32_t vmProcessGetLoadsReq(SVnodeMgmt *pMgmt, SNodeMsg *pReq) { SMonVloadInfo vloads = {0}; - vmGetVnodeLoads(pWrapper, &vloads); + vmGetVnodeLoads(pMgmt, &vloads); int32_t rspLen = tSerializeSMonVloadInfo(NULL, 0, &vloads); if (rspLen < 0) { @@ -170,14 +166,14 @@ static void vmGenerateVnodeCfg(SCreateVnodeReq *pCreate, SVnodeCfg *pCfg) { } } -static void vmGenerateWrapperCfg(SVnodesMgmt *pMgmt, SCreateVnodeReq *pCreate, SWrapperCfg *pCfg) { +static void vmGenerateWrapperCfg(SVnodeMgmt *pMgmt, SCreateVnodeReq *pCreate, SWrapperCfg *pCfg) { pCfg->vgId = pCreate->vgId; pCfg->vgVersion = pCreate->vgVersion; pCfg->dropped = 0; snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCreate->vgId); } -int32_t vmProcessCreateVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pMsg) { +int32_t vmProcessCreateVnodeReq(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SCreateVnodeReq createReq = {0}; int32_t code = -1; @@ -212,17 +208,7 @@ int32_t vmProcessCreateVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pMsg) { return -1; } - SMsgCb msgCb = pMgmt->pDnode->data.msgCb; - msgCb.pWrapper = pMgmt->pWrapper; - msgCb.queueFps[WRITE_QUEUE] = vmPutMsgToWriteQueue; - msgCb.queueFps[SYNC_QUEUE] = vmPutMsgToSyncQueue; - msgCb.queueFps[APPLY_QUEUE] = vmPutMsgToApplyQueue; - msgCb.queueFps[QUERY_QUEUE] = vmPutMsgToQueryQueue; - msgCb.queueFps[FETCH_QUEUE] = vmPutMsgToFetchQueue; - msgCb.queueFps[MERGE_QUEUE] = vmPutMsgToMergeQueue; - msgCb.qsizeFp = vmGetQueueSize; - - SVnode *pImpl = vnodeOpen(path, pMgmt->pTfs, msgCb); + SVnode *pImpl = vnodeOpen(path, pMgmt->pTfs, pMgmt->msgCb); if (pImpl == NULL) { dError("vgId:%d, failed to create vnode since %s", createReq.vgId, terrstr()); code = terrno; @@ -255,7 +241,7 @@ _OVER: return code; } -int32_t vmProcessDropVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pMsg) { +int32_t vmProcessDropVnodeReq(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { SRpcMsg *pReq = &pMsg->rpcMsg; SDropVnodeReq dropReq = {0}; if (tDeserializeSDropVnodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { @@ -286,57 +272,71 @@ int32_t vmProcessDropVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pMsg) { return 0; } -void vmInitMsgHandle(SMgmtWrapper *pWrapper) { - dmSetMsgHandle(pWrapper, TDMT_MON_VM_INFO, vmProcessMonitorMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_MON_VM_LOAD, vmProcessMonitorMsg, DEFAULT_HANDLE); +SArray *vmGetMsgHandles() { + int32_t code = -1; + SArray *pArray = taosArrayInit(32, sizeof(SMgmtHandle)); + if (pArray == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_MON_VM_INFO, vmPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_MON_VM_LOAD, vmPutNodeMsgToMonitorQueue, 0) == NULL) goto _OVER; // Requests handled by VNODE - dmSetMsgHandle(pWrapper, TDMT_VND_SUBMIT, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY, vmProcessQueryMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY_CONTINUE, vmProcessQueryMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_FETCH, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_ALTER_TABLE, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_UPDATE_TAG_VAL, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TABLE_META, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TABLES_META, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_CONSUME, vmProcessQueryMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_QUERY, vmProcessQueryMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_CONNECT, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_DISCONNECT, vmProcessWriteMsg, DEFAULT_HANDLE); - // dmSetMsgHandle(pWrapper, TDMT_VND_MQ_SET_CUR, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_RES_READY, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASKS_STATUS, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CANCEL_TASK, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_TASK, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_STB, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_ALTER_STB, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_STB, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_TABLE, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_TABLE, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_SMA, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CANCEL_SMA, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_DROP_SMA, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SUBMIT_RSMA, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_VG_CHANGE, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_CONSUME, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASK_DEPLOY, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_QUERY_HEARTBEAT, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASK_PIPE_EXEC, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASK_MERGE_EXEC, vmProcessMergeMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_TASK_WRITE_EXEC, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_STREAM_TRIGGER, vmProcessFetchMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_ALTER_VNODE, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_COMPACT_VNODE, vmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_CREATE_VNODE, vmProcessMgmtMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_DND_DROP_VNODE, vmProcessMgmtMsg, DEFAULT_HANDLE); - - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_TIMEOUT, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_PING, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_PING_REPLY, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_CLIENT_REQUEST, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_CLIENT_REQUEST_REPLY, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_REQUEST_VOTE, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_REQUEST_VOTE_REPLY, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_APPEND_ENTRIES, vmProcessSyncMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_APPEND_ENTRIES_REPLY, vmProcessSyncMsg, DEFAULT_HANDLE); + if (dmSetMgmtHandle(pArray, TDMT_VND_SUBMIT, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY, vmPutNodeMsgToQueryQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY_CONTINUE, vmPutNodeMsgToQueryQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_FETCH, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_ALTER_TABLE, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_UPDATE_TAG_VAL, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TABLE_META, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TABLES_META, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_CONSUME, vmPutNodeMsgToQueryQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_QUERY, vmPutNodeMsgToQueryQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_CONNECT, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_DISCONNECT, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + // if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_SET_CUR, vmPutNodeMsgToWriteQueue, 0)== NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_RES_READY, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASKS_STATUS, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CANCEL_TASK, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_TASK, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CREATE_STB, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_ALTER_STB, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_STB, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CREATE_TABLE, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_TABLE, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CREATE_SMA, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CANCEL_SMA, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_DROP_SMA, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SUBMIT_RSMA, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_MQ_VG_CHANGE, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_CONSUME, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASK_DEPLOY, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_QUERY_HEARTBEAT, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASK_PIPE_EXEC, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASK_MERGE_EXEC, vmPutNodeMsgToMergeQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_TASK_WRITE_EXEC, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_STREAM_TRIGGER, vmPutNodeMsgToFetchQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_ALTER_VNODE, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_COMPACT_VNODE, vmPutNodeMsgToWriteQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_VNODE, vmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_VNODE, vmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER; + + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_TIMEOUT, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_PING, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_PING_REPLY, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_CLIENT_REQUEST, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_CLIENT_REQUEST_REPLY, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_REQUEST_VOTE, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_REQUEST_VOTE_REPLY, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_APPEND_ENTRIES, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + if (dmSetMgmtHandle(pArray, TDMT_VND_SYNC_APPEND_ENTRIES_REPLY, vmPutNodeMsgToSyncQueue, 0) == NULL) goto _OVER; + + code = 0; + +_OVER: + if (code != 0) { + taosArrayDestroy(pArray); + return NULL; + } else { + return pArray; + } } diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index 9a0a524267a85c90872579ca07d93f35d0e31668..635467559e485582cc616139081c2d9579272ff2 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "vmInt.h" -SVnodeObj *vmAcquireVnode(SVnodesMgmt *pMgmt, int32_t vgId) { +SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { SVnodeObj *pVnode = NULL; int32_t refCount = 0; @@ -36,7 +36,7 @@ SVnodeObj *vmAcquireVnode(SVnodesMgmt *pMgmt, int32_t vgId) { return pVnode; } -void vmReleaseVnode(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { +void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) { if (pVnode == NULL) return; taosRLockLatch(&pMgmt->latch); @@ -45,7 +45,7 @@ void vmReleaseVnode(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { dTrace("vgId:%d, release vnode, refCount:%d", pVnode->vgId, refCount); } -int32_t vmOpenVnode(SVnodesMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) { +int32_t vmOpenVnode(SVnodeMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) { SVnodeObj *pVnode = taosMemoryCalloc(1, sizeof(SVnodeObj)); if (pVnode == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -59,7 +59,6 @@ int32_t vmOpenVnode(SVnodesMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) { pVnode->accessState = TSDB_VN_ALL_ACCCESS; pVnode->path = tstrdup(pCfg->path); pVnode->pImpl = pImpl; - pVnode->pWrapper = pMgmt->pWrapper; if (pVnode->path == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -78,7 +77,7 @@ int32_t vmOpenVnode(SVnodesMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) { return code; } -void vmCloseVnode(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { +void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) { char path[TSDB_FILENAME_LEN] = {0}; taosWLockLatch(&pMgmt->latch); @@ -112,8 +111,7 @@ void vmCloseVnode(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { static void *vmOpenVnodeInThread(void *param) { SVnodeThread *pThread = param; - SVnodesMgmt *pMgmt = pThread->pMgmt; - SDnode *pDnode = pMgmt->pDnode; + SVnodeMgmt *pMgmt = pThread->pMgmt; char path[TSDB_FILENAME_LEN]; dDebug("thread:%d, start to open %d vnodes", pThread->threadIndex, pThread->vnodeNum); @@ -125,19 +123,10 @@ static void *vmOpenVnodeInThread(void *param) { char stepDesc[TSDB_STEP_DESC_LEN] = {0}; snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId, pMgmt->state.openVnodes, pMgmt->state.totalVnodes); - dmReportStartup(pDnode, "vnode-open", stepDesc); - - SMsgCb msgCb = pMgmt->pDnode->data.msgCb; - msgCb.pWrapper = pMgmt->pWrapper; - msgCb.queueFps[WRITE_QUEUE] = vmPutMsgToWriteQueue; - msgCb.queueFps[SYNC_QUEUE] = vmPutMsgToSyncQueue; - msgCb.queueFps[APPLY_QUEUE] = vmPutMsgToApplyQueue; - msgCb.queueFps[QUERY_QUEUE] = vmPutMsgToQueryQueue; - msgCb.queueFps[FETCH_QUEUE] = vmPutMsgToFetchQueue; - msgCb.queueFps[MERGE_QUEUE] = vmPutMsgToMergeQueue; - msgCb.qsizeFp = vmGetQueueSize; + tmsgReportStartup("vnode-open", stepDesc); + snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId); - SVnode *pImpl = vnodeOpen(path, pMgmt->pTfs, msgCb); + SVnode *pImpl = vnodeOpen(path, pMgmt->pTfs, pMgmt->msgCb); if (pImpl == NULL) { dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex); pThread->failed++; @@ -154,9 +143,7 @@ static void *vmOpenVnodeInThread(void *param) { return NULL; } -static int32_t vmOpenVnodes(SVnodesMgmt *pMgmt) { - SDnode *pDnode = pMgmt->pDnode; - +static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) { pMgmt->hash = taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); if (pMgmt->hash == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -224,7 +211,7 @@ static int32_t vmOpenVnodes(SVnodesMgmt *pMgmt) { } } -static void vmCloseVnodes(SVnodesMgmt *pMgmt) { +static void vmCloseVnodes(SVnodeMgmt *pMgmt) { dInfo("start to close all vnodes"); int32_t numOfVnodes = 0; @@ -246,40 +233,44 @@ static void vmCloseVnodes(SVnodesMgmt *pMgmt) { dInfo("total vnodes:%d are all closed", numOfVnodes); } -static void vmCleanup(SMgmtWrapper *pWrapper) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; - if (pMgmt == NULL) return; - +static void vmCleanup(SVnodeMgmt *pMgmt) { dInfo("vnode-mgmt start to cleanup"); vmCloseVnodes(pMgmt); vmStopWorker(pMgmt); vnodeCleanup(); tfsClose(pMgmt->pTfs); taosMemoryFree(pMgmt); - pWrapper->pMgmt = NULL; dInfo("vnode-mgmt is cleaned up"); } -static int32_t vmInit(SMgmtWrapper *pWrapper) { - SDnode *pDnode = pWrapper->pDnode; - SVnodesMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodesMgmt)); - int32_t code = -1; - +static int32_t vmInit(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { dInfo("vnode-mgmt start to init"); + int32_t code = -1; + + SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt)); if (pMgmt == NULL) goto _OVER; - pMgmt->path = pWrapper->path; - pMgmt->pDnode = pWrapper->pDnode; - pMgmt->pWrapper = pWrapper; + pMgmt->path = pInput->path; + pMgmt->name = pInput->name; + pMgmt->dnodeId = pInput->dnodeId; + pMgmt->msgCb = pInput->msgCb; + pMgmt->msgCb.queueFps[WRITE_QUEUE] = (PutToQueueFp)vmPutRpcMsgToWriteQueue; + pMgmt->msgCb.queueFps[SYNC_QUEUE] = (PutToQueueFp)vmPutRpcMsgToSyncQueue; + pMgmt->msgCb.queueFps[APPLY_QUEUE] = (PutToQueueFp)vmPutRpcMsgToApplyQueue; + pMgmt->msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)vmPutRpcMsgToQueryQueue; + pMgmt->msgCb.queueFps[FETCH_QUEUE] = (PutToQueueFp)vmPutRpcMsgToFetchQueue; + pMgmt->msgCb.queueFps[MERGE_QUEUE] = (PutToQueueFp)vmPutRpcMsgToMergeQueue; + pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize; + pMgmt->msgCb.pMgmt = pMgmt; taosInitRWLatch(&pMgmt->latch); SDiskCfg dCfg = {0}; - tstrncpy(dCfg.dir, pDnode->data.dataDir, TSDB_FILENAME_LEN); + tstrncpy(dCfg.dir, pInput->dataDir, TSDB_FILENAME_LEN); dCfg.level = 0; dCfg.primary = 1; - SDiskCfg *pDisks = pDnode->data.disks; - int32_t numOfDisks = pDnode->data.numOfDisks; + SDiskCfg *pDisks = pInput->disks; + int32_t numOfDisks = pInput->numOfDisks; if (numOfDisks <= 0 || pDisks == NULL) { pDisks = &dCfg; numOfDisks = 1; @@ -290,64 +281,64 @@ static int32_t vmInit(SMgmtWrapper *pWrapper) { dError("failed to init tfs since %s", terrstr()); goto _OVER; } - dmReportStartup(pDnode, "vnode-tfs", "initialized"); + tmsgReportStartup("vnode-tfs", "initialized"); if (walInit() != 0) { dError("failed to init wal since %s", terrstr()); goto _OVER; } - dmReportStartup(pDnode, "vnode-wal", "initialized"); + tmsgReportStartup("vnode-wal", "initialized"); if (syncInit() != 0) { dError("failed to open sync since %s", terrstr()); - return -1; + goto _OVER; } + tmsgReportStartup("vnode-sync", "initialized"); if (vnodeInit(tsNumOfCommitThreads) != 0) { dError("failed to init vnode since %s", terrstr()); goto _OVER; } - dmReportStartup(pDnode, "vnode-commit", "initialized"); + tmsgReportStartup("vnode-commit", "initialized"); if (vmStartWorker(pMgmt) != 0) { - dError("failed to init workers since %s", terrstr()) goto _OVER; + dError("failed to init workers since %s", terrstr()); + goto _OVER; } - dmReportStartup(pDnode, "vnode-worker", "initialized"); + tmsgReportStartup("vnode-worker", "initialized"); if (vmOpenVnodes(pMgmt) != 0) { dError("failed to open vnode since %s", terrstr()); - return -1; + goto _OVER; } - dmReportStartup(pDnode, "vnode-vnodes", "initialized"); + tmsgReportStartup("vnode-vnodes", "initialized"); if (udfcOpen() != 0) { dError("failed to open udfc in vnode"); + goto _OVER; } code = 0; _OVER: if (code == 0) { - pWrapper->pMgmt = pMgmt; + pOutput->pMgmt = pMgmt; dInfo("vnodes-mgmt is initialized"); } else { dError("failed to init vnodes-mgmt since %s", terrstr()); - vmCleanup(pWrapper); + vmCleanup(pMgmt); } - return 0; + return code; } -static int32_t vmRequire(SMgmtWrapper *pWrapper, bool *required) { - SDnode *pDnode = pWrapper->pDnode; - *required = pDnode->data.supportVnodes > 0; +static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) { + *required = pInput->supportVnodes > 0; return 0; } -static int32_t vmStart(SMgmtWrapper *pWrapper) { +static int32_t vmStart(SVnodeMgmt *pMgmt) { dDebug("vnode-mgmt start to run"); - SVnodesMgmt *pMgmt = pWrapper->pMgmt; - taosRLockLatch(&pMgmt->latch); void *pIter = taosHashIterate(pMgmt->hash, NULL); @@ -364,20 +355,18 @@ static int32_t vmStart(SMgmtWrapper *pWrapper) { return 0; } -static void vmStop(SMgmtWrapper *pWrapper) { +static void vmStop(SVnodeMgmt *pMgmt) { // process inside the vnode } -void vmSetMgmtFp(SMgmtWrapper *pWrapper) { - SMgmtFp mgmtFp = {0}; - mgmtFp.openFp = vmInit; - mgmtFp.closeFp = vmCleanup; - mgmtFp.startFp = vmStart; - mgmtFp.stopFp = vmStop; - mgmtFp.requiredFp = vmRequire; - - vmInitMsgHandle(pWrapper); - pWrapper->name = "vnode"; - pWrapper->fp = mgmtFp; -} +SMgmtFunc vmGetMgmtFunc() { + SMgmtFunc mgmtFunc = {0}; + mgmtFunc.openFp = vmInit; + mgmtFunc.closeFp = (NodeCloseFp)vmCleanup; + mgmtFunc.startFp = (NodeStartFp)vmStart; + mgmtFunc.stopFp = (NodeStopFp)vmStop; + mgmtFunc.requiredFp = vmRequire; + mgmtFunc.getHandlesFp = vmGetMsgHandles; + return mgmtFunc; +} diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 2baa8b8942c996f818fc2ad56ce8798933b5b0d0..cf8f4d5010e425d1c98b4f55d0a9a3d5287a523f 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -32,7 +32,7 @@ static inline void vmSendRsp(SNodeMsg *pMsg, int32_t code) { } static void vmProcessMgmtMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pInfo->ahandle; + SVnodeMgmt *pMgmt = pInfo->ahandle; int32_t code = -1; tmsg_t msgType = pMsg->rpcMsg.msgType; @@ -40,10 +40,10 @@ static void vmProcessMgmtMonitorQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { switch (msgType) { case TDMT_MON_VM_INFO: - code = vmProcessGetMonVmInfoReq(pMgmt->pWrapper, pMsg); + code = vmProcessGetMonitorInfoReq(pMgmt, pMsg); break; case TDMT_MON_VM_LOAD: - code = vmProcessGetVnodeLoadsReq(pMgmt->pWrapper, pMsg); + code = vmProcessGetLoadsReq(pMgmt, pMsg); break; case TDMT_DND_CREATE_VNODE: code = vmProcessCreateVnodeReq(pMgmt, pMsg); @@ -240,7 +240,7 @@ static void vmProcessMergeQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO } } -static int32_t vmPutNodeMsgToQueue(SVnodesMgmt *pMgmt, SNodeMsg *pMsg, EQueueType qtype) { +static int32_t vmPutNodeMsgToQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg, EQueueType qtype) { SRpcMsg *pRpc = &pMsg->rpcMsg; SMsgHead *pHead = pRpc->pCont; int32_t code = 0; @@ -285,41 +285,34 @@ static int32_t vmPutNodeMsgToQueue(SVnodesMgmt *pMgmt, SNodeMsg *pMsg, EQueueTyp return code; } -int32_t vmProcessSyncMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToSyncQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { return vmPutNodeMsgToQueue(pMgmt, pMsg, SYNC_QUEUE); } -int32_t vmProcessWriteMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToWriteQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { return vmPutNodeMsgToQueue(pMgmt, pMsg, WRITE_QUEUE); } -int32_t vmProcessQueryMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToQueryQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { return vmPutNodeMsgToQueue(pMgmt, pMsg, QUERY_QUEUE); } -int32_t vmProcessFetchMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToFetchQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { return vmPutNodeMsgToQueue(pMgmt, pMsg, FETCH_QUEUE); } -int32_t vmProcessMergeMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToMergeQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { return vmPutNodeMsgToQueue(pMgmt, pMsg, MERGE_QUEUE); } -int32_t vmProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToMgmtQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { SSingleWorker *pWorker = &pMgmt->mgmtWorker; dTrace("msg:%p, will be put into vnode-mgmt queue, worker:%s", pMsg, pWorker->name); taosWriteQitem(pWorker->queue, pMsg); return 0; } -int32_t vmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; +int32_t vmPutNodeMsgToMonitorQueue(SVnodeMgmt *pMgmt, SNodeMsg *pMsg) { SSingleWorker *pWorker = &pMgmt->monitorWorker; dTrace("msg:%p, will be put into vnode-monitor queue, worker:%s", pMsg, pWorker->name); @@ -327,9 +320,8 @@ int32_t vmProcessMonitorMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) { return 0; } -static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueType qtype) { - SVnodesMgmt *pMgmt = pWrapper->pMgmt; - SMsgHead *pHead = pRpc->pCont; +static int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc, EQueueType qtype) { + SMsgHead *pHead = pRpc->pCont; SVnodeObj *pVnode = vmAcquireVnode(pMgmt, pHead->vgId); if (pVnode == NULL) return -1; @@ -377,33 +369,31 @@ static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueT return code; } -int32_t vmPutMsgToWriteQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - return vmPutRpcMsgToQueue(pWrapper, pRpc, WRITE_QUEUE); +int32_t vmPutRpcMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { + return vmPutRpcMsgToQueue(pMgmt, pRpc, WRITE_QUEUE); } -int32_t vmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - return vmPutRpcMsgToQueue(pWrapper, pRpc, SYNC_QUEUE); -} +int32_t vmPutRpcMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { return vmPutRpcMsgToQueue(pMgmt, pRpc, SYNC_QUEUE); } -int32_t vmPutMsgToApplyQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - return vmPutRpcMsgToQueue(pWrapper, pRpc, APPLY_QUEUE); +int32_t vmPutRpcMsgToApplyQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { + return vmPutRpcMsgToQueue(pMgmt, pRpc, APPLY_QUEUE); } -int32_t vmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - return vmPutRpcMsgToQueue(pWrapper, pRpc, QUERY_QUEUE); +int32_t vmPutRpcMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { + return vmPutRpcMsgToQueue(pMgmt, pRpc, QUERY_QUEUE); } -int32_t vmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - return vmPutRpcMsgToQueue(pWrapper, pRpc, FETCH_QUEUE); +int32_t vmPutRpcMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { + return vmPutRpcMsgToQueue(pMgmt, pRpc, FETCH_QUEUE); } -int32_t vmPutMsgToMergeQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { - return vmPutRpcMsgToQueue(pWrapper, pRpc, MERGE_QUEUE); +int32_t vmPutRpcMsgToMergeQueue(SVnodeMgmt *pMgmt, SRpcMsg *pRpc) { + return vmPutRpcMsgToQueue(pMgmt, pRpc, MERGE_QUEUE); } -int32_t vmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) { +int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) { int32_t size = -1; - SVnodeObj *pVnode = vmAcquireVnode(pWrapper->pMgmt, vgId); + SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId); if (pVnode != NULL) { switch (qtype) { case WRITE_QUEUE: @@ -428,11 +418,11 @@ int32_t vmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) { break; } } - vmReleaseVnode(pWrapper->pMgmt, pVnode); + vmReleaseVnode(pMgmt, pVnode); return size; } -int32_t vmAllocQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { +int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) { pVnode->pWriteQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessWriteQueue); pVnode->pSyncQ = tWWorkerAllocQueue(&pMgmt->syncPool, pVnode, (FItems)vmProcessSyncQueue); pVnode->pApplyQ = tWWorkerAllocQueue(&pMgmt->writePool, pVnode, (FItems)vmProcessApplyQueue); @@ -450,7 +440,7 @@ int32_t vmAllocQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { return 0; } -void vmFreeQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { +void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) { tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pWriteQ); tWWorkerFreeQueue(&pMgmt->syncPool, pVnode->pSyncQ); tWWorkerFreeQueue(&pMgmt->writePool, pVnode->pApplyQ); @@ -466,7 +456,7 @@ void vmFreeQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode) { dDebug("vgId:%d, vnode queue is freed", pVnode->vgId); } -int32_t vmStartWorker(SVnodesMgmt *pMgmt) { +int32_t vmStartWorker(SVnodeMgmt *pMgmt) { SQWorkerPool *pQPool = &pMgmt->queryPool; pQPool->name = "vnode-query"; pQPool->min = tsNumOfVnodeQueryThreads; @@ -506,25 +496,23 @@ int32_t vmStartWorker(SVnodesMgmt *pMgmt) { return -1; } - if (tsMultiProcess) { - SSingleWorkerCfg mCfg = { - .min = 1, - .max = 1, - .name = "vnode-monitor", - .fp = (FItem)vmProcessMgmtMonitorQueue, - .param = pMgmt, - }; - if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { - dError("failed to start mnode vnode-monitor worker since %s", terrstr()); - return -1; - } + SSingleWorkerCfg mCfg = { + .min = 1, + .max = 1, + .name = "vnode-monitor", + .fp = (FItem)vmProcessMgmtMonitorQueue, + .param = pMgmt, + }; + if (tSingleWorkerInit(&pMgmt->monitorWorker, &mCfg) != 0) { + dError("failed to start mnode vnode-monitor worker since %s", terrstr()); + return -1; } dDebug("vnode workers are initialized"); return 0; } -void vmStopWorker(SVnodesMgmt *pMgmt) { +void vmStopWorker(SVnodeMgmt *pMgmt) { tSingleWorkerCleanup(&pMgmt->monitorWorker); tSingleWorkerCleanup(&pMgmt->mgmtWorker); tWWorkerCleanup(&pMgmt->writePool); diff --git a/source/dnode/mgmt/implement/CMakeLists.txt b/source/dnode/mgmt/node_mgmt/CMakeLists.txt similarity index 94% rename from source/dnode/mgmt/implement/CMakeLists.txt rename to source/dnode/mgmt/node_mgmt/CMakeLists.txt index fbe7530395e074bde7a780afa9914fcc31ec4d03..98027d80d4af858eef2a2108ef6d19b4b1851883 100644 --- a/source/dnode/mgmt/implement/CMakeLists.txt +++ b/source/dnode/mgmt/node_mgmt/CMakeLists.txt @@ -1,9 +1,9 @@ aux_source_directory(src IMPLEMENT_SRC) add_library(dnode STATIC ${IMPLEMENT_SRC}) target_link_libraries( - dnode mgmt_bnode mgmt_mnode mgmt_qnode mgmt_snode mgmt_vnode + dnode mgmt_bnode mgmt_mnode mgmt_qnode mgmt_snode mgmt_vnode mgmt_dnode ) target_include_directories( dnode PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) \ No newline at end of file +) diff --git a/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h b/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h new file mode 100644 index 0000000000000000000000000000000000000000..d717408fc63f6496cc762aa3d033f9487a7127ed --- /dev/null +++ b/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h @@ -0,0 +1,133 @@ +/* + * 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 _TD_DND_IMP_H_ +#define _TD_DND_IMP_H_ + +// tobe deleted +#include "uv.h" + +#include "dmUtil.h" +#include "dmInt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SMgmtWrapper { + SDnode *pDnode; + SMgmtFunc func; + void *pMgmt; + const char *name; + char *path; + int32_t refCount; + SRWLatch latch; + EDndNodeType nodeType; + bool deployed; + bool required; + EDndProcType procType; + int32_t procId; + SProcObj *procObj; + SShm procShm; + NodeMsgFp msgFps[TDMT_MAX]; +} SMgmtWrapper; + +typedef struct { + EDndNodeType defaultNtype; + bool needCheckVgId; +} SMsgHandle; + +typedef struct { + void *serverRpc; + void *clientRpc; + SMsgHandle msgHandles[TDMT_MAX]; +} SDnodeTrans; + +typedef struct { + char name[TSDB_STEP_NAME_LEN]; + char desc[TSDB_STEP_DESC_LEN]; +} SStartupInfo; + +typedef struct SUdfdData { + bool startCalled; + bool needCleanUp; + uv_loop_t loop; + uv_thread_t thread; + uv_barrier_t barrier; + uv_process_t process; + int spawnErr; + uv_pipe_t ctrlPipe; + uv_async_t stopAsync; + int32_t stopCalled; + int32_t dnodeId; +} SUdfdData; + +typedef struct SDnode { + EDndProcType ptype; + EDndNodeType ntype; + EDndEvent event; + EDndRunStatus status; + SStartupInfo startup; + SDnodeTrans trans; + SUdfdData udfdData; + TdThreadMutex mutex; + SRWLatch latch; + SEpSet mnodeEps; + TdFilePtr lockfile; + SMgmtInputOpt input; + SMgmtWrapper wrappers[NODE_END]; +} SDnode; + +// dmExec.c +int32_t dmOpenNode(SMgmtWrapper *pWrapper); +void dmCloseNode(SMgmtWrapper *pWrapper); + +// dmObj.c +SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType nType); +int32_t dmMarkWrapper(SMgmtWrapper *pWrapper); +void dmReleaseWrapper(SMgmtWrapper *pWrapper); + +void dmSetStatus(SDnode *pDnode, EDndRunStatus stype); +void dmSetEvent(SDnode *pDnode, EDndEvent event); +void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc); +void dmReportStartupByWrapper(SMgmtWrapper *pWrapper, const char *pName, const char *pDesc); + +void dmProcessServerStartupStatus(SDnode *pDnode, SRpcMsg *pMsg); +void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pMsg); +int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg); +int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg); + +// dmTransport.c +int32_t dmInitServer(SDnode *pDnode); +void dmCleanupServer(SDnode *pDnode); +int32_t dmInitClient(SDnode *pDnode); +void dmCleanupClient(SDnode *pDnode); +SProcCfg dmGenProcCfg(SMgmtWrapper *pWrapper); +SMsgCb dmGetMsgcb(SMgmtWrapper *pWrapper); +int32_t dmInitMsgHandle(SDnode *pDnode); + +// mgmt nodes +SMgmtFunc dmGetMgmtFunc(); +SMgmtFunc bmGetMgmtFunc(); +SMgmtFunc qmGetMgmtFunc(); +SMgmtFunc smGetMgmtFunc(); +SMgmtFunc vmGetMgmtFunc(); +SMgmtFunc mmGetMgmtFunc(); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DND_IMP_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c new file mode 100644 index 0000000000000000000000000000000000000000..dbd861e6f7480e07c8c2f0f19848b14cfccb175f --- /dev/null +++ b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c @@ -0,0 +1,358 @@ +/* + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "dmMgmt.h" + +static bool dmIsNodeDeployedFp(SDnode *pDnode, EDndNodeType ntype) { return pDnode->wrappers[ntype].required; } + +static int32_t dmInitVars(SDnode *pDnode, const SDnodeOpt *pOption) { + pDnode->input.dnodeId = 0; + pDnode->input.clusterId = 0; + pDnode->input.localEp = strdup(pOption->localEp); + pDnode->input.localFqdn = strdup(pOption->localFqdn); + pDnode->input.firstEp = strdup(pOption->firstEp); + pDnode->input.secondEp = strdup(pOption->secondEp); + pDnode->input.serverPort = pOption->serverPort; + pDnode->input.supportVnodes = pOption->numOfSupportVnodes; + pDnode->input.numOfDisks = pOption->numOfDisks; + pDnode->input.disks = pOption->disks; + pDnode->input.dataDir = strdup(pOption->dataDir); + pDnode->input.pDnode = pDnode; + pDnode->input.processCreateNodeFp = dmProcessCreateNodeReq; + pDnode->input.processDropNodeFp = dmProcessDropNodeReq; + pDnode->input.isNodeDeployedFp = dmIsNodeDeployedFp; + + if (pDnode->input.dataDir == NULL || pDnode->input.localEp == NULL || pDnode->input.localFqdn == NULL || + pDnode->input.firstEp == NULL || pDnode->input.secondEp == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + pDnode->ntype = pOption->ntype; + if (!tsMultiProcess || pDnode->ntype == DNODE || pDnode->ntype == NODE_END) { + pDnode->lockfile = dmCheckRunning(pOption->dataDir); + if (pDnode->lockfile == NULL) { + return -1; + } + } + + taosThreadMutexInit(&pDnode->mutex, NULL); + return 0; +} + +static void dmClearVars(SDnode *pDnode) { + for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { + SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; + taosMemoryFreeClear(pWrapper->path); + } + if (pDnode->lockfile != NULL) { + taosUnLockFile(pDnode->lockfile); + taosCloseFile(&pDnode->lockfile); + pDnode->lockfile = NULL; + } + + taosMemoryFreeClear(pDnode->input.localEp); + taosMemoryFreeClear(pDnode->input.localFqdn); + taosMemoryFreeClear(pDnode->input.firstEp); + taosMemoryFreeClear(pDnode->input.secondEp); + taosMemoryFreeClear(pDnode->input.dataDir); + + taosThreadMutexDestroy(&pDnode->mutex); + memset(&pDnode->mutex, 0, sizeof(pDnode->mutex)); + taosMemoryFree(pDnode); + dDebug("dnode memory is cleared, data:%p", pDnode); +} + +static bool dmRequireNode(SMgmtWrapper *pWrapper) { + SMgmtInputOpt *pInput = &pWrapper->pDnode->input; + pInput->name = pWrapper->name; + pInput->path = pWrapper->path; + + bool required = false; + int32_t code = (*pWrapper->func.requiredFp)(pInput, &required); + if (!required) { + dDebug("node:%s, does not require startup", pWrapper->name); + } + return required; +} + +SDnode *dmCreate(const SDnodeOpt *pOption) { + dInfo("start to create dnode"); + int32_t code = -1; + char path[PATH_MAX + 100] = {0}; + SDnode *pDnode = NULL; + + pDnode = taosMemoryCalloc(1, sizeof(SDnode)); + if (pDnode == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto _OVER; + } + + if (dmInitVars(pDnode, pOption) != 0) { + goto _OVER; + } + + dmSetStatus(pDnode, DND_STAT_INIT); + pDnode->wrappers[DNODE].func = dmGetMgmtFunc(); + pDnode->wrappers[MNODE].func = mmGetMgmtFunc(); + pDnode->wrappers[VNODE].func = vmGetMgmtFunc(); + pDnode->wrappers[QNODE].func = qmGetMgmtFunc(); + pDnode->wrappers[SNODE].func = smGetMgmtFunc(); + pDnode->wrappers[BNODE].func = bmGetMgmtFunc(); + + for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { + SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; + pWrapper->pDnode = pDnode; + pWrapper->name = dmNodeName(ntype); + pWrapper->procShm.id = -1; + pWrapper->nodeType = ntype; + pWrapper->procType = DND_PROC_SINGLE; + taosInitRWLatch(&pWrapper->latch); + + snprintf(path, sizeof(path), "%s%s%s", pOption->dataDir, TD_DIRSEP, pWrapper->name); + pWrapper->path = strdup(path); + if (pWrapper->path == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto _OVER; + } + + if (ntype != DNODE && dmReadShmFile(pWrapper->path, pWrapper->name, pDnode->ntype, &pWrapper->procShm) != 0) { + dError("node:%s, failed to read shm file since %s", pWrapper->name, terrstr()); + goto _OVER; + } + + pWrapper->required = dmRequireNode(pWrapper); + } + + if (dmInitMsgHandle(pDnode) != 0) { + dError("failed to init msg handles since %s", terrstr()); + goto _OVER; + } + + if (dmInitClient(pDnode) != 0) { + goto _OVER; + } + + dInfo("dnode is created, data:%p", pDnode); + code = 0; + +_OVER: + if (code != 0 && pDnode != NULL) { + dmClearVars(pDnode); + pDnode = NULL; + dError("failed to create dnode since %s", terrstr()); + } + + return pDnode; +} + +void dmClose(SDnode *pDnode) { + if (pDnode == NULL) return; + + dmCleanupClient(pDnode); + dmCleanupServer(pDnode); + + dmClearVars(pDnode); + dInfo("dnode is closed, data:%p", pDnode); +} + +void dmSetStatus(SDnode *pDnode, EDndRunStatus status) { + if (pDnode->status != status) { + dDebug("dnode status set from %s to %s", dmStatStr(pDnode->status), dmStatStr(status)); + pDnode->status = status; + } +} + +void dmSetEvent(SDnode *pDnode, EDndEvent event) { + if (event == DND_EVENT_STOP) { + pDnode->event = event; + } +} + +SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) { + SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; + SMgmtWrapper *pRetWrapper = pWrapper; + + taosRLockLatch(&pWrapper->latch); + if (pWrapper->deployed) { + int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1); + dTrace("node:%s, is acquired, refCount:%d", pWrapper->name, refCount); + } else { + terrno = TSDB_CODE_NODE_NOT_DEPLOYED; + pRetWrapper = NULL; + } + taosRUnLockLatch(&pWrapper->latch); + + return pRetWrapper; +} + +int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) { + int32_t code = 0; + + taosRLockLatch(&pWrapper->latch); + if (pWrapper->deployed || (pWrapper->procType == DND_PROC_PARENT && pWrapper->required)) { + int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1); + dTrace("node:%s, is marked, refCount:%d", pWrapper->name, refCount); + } else { + terrno = TSDB_CODE_NODE_NOT_DEPLOYED; + code = -1; + } + taosRUnLockLatch(&pWrapper->latch); + + return code; +} + +void dmReleaseWrapper(SMgmtWrapper *pWrapper) { + if (pWrapper == NULL) return; + + taosRLockLatch(&pWrapper->latch); + int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1); + taosRUnLockLatch(&pWrapper->latch); + dTrace("node:%s, is released, refCount:%d", pWrapper->name, refCount); +} + +void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) { + SStartupInfo *pStartup = &pDnode->startup; + tstrncpy(pStartup->name, pName, TSDB_STEP_NAME_LEN); + tstrncpy(pStartup->desc, pDesc, TSDB_STEP_DESC_LEN); + dInfo("step:%s, %s", pStartup->name, pStartup->desc); +} + +void dmReportStartupByWrapper(SMgmtWrapper *pWrapper, const char *pName, const char *pDesc) { + dmReportStartup(pWrapper->pDnode, pName, pDesc); +} + +static void dmGetServerStartupStatus(SDnode *pDnode, SServerStatusRsp *pStatus) { + SDnodeMgmt *pMgmt = pDnode->wrappers[DNODE].pMgmt; + pStatus->details[0] = 0; + + if (pDnode->status == DND_STAT_INIT) { + pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK; + snprintf(pStatus->details, sizeof(pStatus->details), "%s: %s", pDnode->startup.name, pDnode->startup.desc); + } else if (pDnode->status == DND_STAT_STOPPED) { + pStatus->statusCode = TSDB_SRV_STATUS_EXTING; + } else { + pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK; + } +} + +void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pReq) { + dDebug("net test req is received"); + SRpcMsg rsp = {.handle = pReq->handle, .refId = pReq->refId, .ahandle = pReq->ahandle, .code = 0}; + rsp.pCont = rpcMallocCont(pReq->contLen); + if (rsp.pCont == NULL) { + rsp.code = TSDB_CODE_OUT_OF_MEMORY; + } else { + rsp.contLen = pReq->contLen; + } + rpcSendResponse(&rsp); + rpcFreeCont(pReq->pCont); +} + +void dmProcessServerStartupStatus(SDnode *pDnode, SRpcMsg *pReq) { + dDebug("server startup status req is received"); + + SServerStatusRsp statusRsp = {0}; + dmGetServerStartupStatus(pDnode, &statusRsp); + + SRpcMsg rspMsg = {.handle = pReq->handle, .ahandle = pReq->ahandle, .refId = pReq->refId}; + int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp); + if (rspLen < 0) { + rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; + goto _OVER; + } + + void *pRsp = rpcMallocCont(rspLen); + if (pRsp == NULL) { + rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; + goto _OVER; + } + + tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp); + rspMsg.pCont = pRsp; + rspMsg.contLen = rspLen; + +_OVER: + rpcSendResponse(&rspMsg); + rpcFreeCont(pReq->pCont); +} + +int32_t dmProcessCreateNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) { + SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype); + if (pWrapper != NULL) { + dmReleaseWrapper(pWrapper); + terrno = TSDB_CODE_NODE_ALREADY_DEPLOYED; + dError("failed to create node since %s", terrstr()); + return -1; + } + + taosThreadMutexLock(&pDnode->mutex); + pWrapper = &pDnode->wrappers[ntype]; + + if (taosMkDir(pWrapper->path) != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + dError("failed to create dir:%s since %s", pWrapper->path, terrstr()); + return -1; + } + + SMgmtInputOpt *pInput = &pWrapper->pDnode->input; + pInput->name = pWrapper->name; + pInput->path = pWrapper->path; + pInput->msgCb = dmGetMsgcb(pWrapper); + + int32_t code = (*pWrapper->func.createFp)(pInput, pMsg); + if (code != 0) { + dError("node:%s, failed to create since %s", pWrapper->name, terrstr()); + } else { + dDebug("node:%s, has been created", pWrapper->name); + (void)dmOpenNode(pWrapper); + pWrapper->required = true; + pWrapper->deployed = true; + pWrapper->procType = pDnode->ptype; + } + + taosThreadMutexUnlock(&pDnode->mutex); + return code; +} + +int32_t dmProcessDropNodeReq(SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg) { + SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, ntype); + if (pWrapper == NULL) { + terrno = TSDB_CODE_NODE_NOT_DEPLOYED; + dError("failed to drop node since %s", terrstr()); + return -1; + } + + taosThreadMutexLock(&pDnode->mutex); + + int32_t code = (*pWrapper->func.dropFp)(pWrapper->pMgmt, pMsg); + if (code != 0) { + dError("node:%s, failed to drop since %s", pWrapper->name, terrstr()); + } else { + dDebug("node:%s, has been dropped", pWrapper->name); + pWrapper->required = false; + pWrapper->deployed = false; + } + + dmReleaseWrapper(pWrapper); + + if (code == 0) { + dmCloseNode(pWrapper); + taosRemoveDir(pWrapper->path); + } + taosThreadMutexUnlock(&pDnode->mutex); + return code; +} \ No newline at end of file diff --git a/source/dnode/mgmt/implement/src/dmExec.c b/source/dnode/mgmt/node_mgmt/src/dmRun.c similarity index 73% rename from source/dnode/mgmt/implement/src/dmExec.c rename to source/dnode/mgmt/node_mgmt/src/dmRun.c index 6999cee0370059deca9af77cc9a8e036f85feeda..4d6290048aaac766b896f342b35ef715f4c64f4c 100644 --- a/source/dnode/mgmt/implement/src/dmExec.c +++ b/source/dnode/mgmt/node_mgmt/src/dmRun.c @@ -14,34 +14,25 @@ */ #define _DEFAULT_SOURCE -#include "dmImp.h" - -static bool dmRequireNode(SMgmtWrapper *pWrapper) { - bool required = false; - int32_t code = (*pWrapper->fp.requiredFp)(pWrapper, &required); - if (!required) { - dDebug("node:%s, does not require startup", pWrapper->name); - } - return required; -} +#include "dmMgmt.h" static int32_t dmInitParentProc(SMgmtWrapper *pWrapper) { int32_t shmsize = tsMnodeShmSize; - if (pWrapper->ntype == VNODE) { + if (pWrapper->nodeType == VNODE) { shmsize = tsVnodeShmSize; - } else if (pWrapper->ntype == QNODE) { + } else if (pWrapper->nodeType == QNODE) { shmsize = tsQnodeShmSize; - } else if (pWrapper->ntype == SNODE) { + } else if (pWrapper->nodeType == SNODE) { shmsize = tsSnodeShmSize; - } else if (pWrapper->ntype == MNODE) { + } else if (pWrapper->nodeType == MNODE) { shmsize = tsMnodeShmSize; - } else if (pWrapper->ntype == BNODE) { + } else if (pWrapper->nodeType == BNODE) { shmsize = tsBnodeShmSize; } else { return -1; } - if (taosCreateShm(&pWrapper->procShm, pWrapper->ntype, shmsize) != 0) { + if (taosCreateShm(&pWrapper->procShm, pWrapper->nodeType, shmsize) != 0) { terrno = TAOS_SYSTEM_ERROR(terrno); dError("node:%s, failed to create shm size:%d since %s", pWrapper->name, shmsize, terrstr()); return -1; @@ -86,7 +77,7 @@ static int32_t dmRunParentProc(SMgmtWrapper *pWrapper) { if (pWrapper->pDnode->ntype == NODE_END) { dInfo("node:%s, should be started manually in child process", pWrapper->name); } else { - if (dmNewNodeProc(pWrapper, pWrapper->ntype) != 0) { + if (dmNewNodeProc(pWrapper, pWrapper->nodeType) != 0) { return -1; } } @@ -123,8 +114,17 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) { return -1; } + SMgmtOutputOpt output = {0}; + SMgmtInputOpt *pInput = &pWrapper->pDnode->input; + pInput->name = pWrapper->name; + pInput->path = pWrapper->path; + pInput->msgCb = dmGetMsgcb(pWrapper); + if (pWrapper->nodeType == DNODE || pWrapper->procType == DND_PROC_CHILD) { + tmsgSetDefaultMsgCb(&pInput->msgCb); + } + if (pWrapper->procType == DND_PROC_SINGLE || pWrapper->procType == DND_PROC_CHILD) { - if ((*pWrapper->fp.openFp)(pWrapper) != 0) { + if ((*pWrapper->func.openFp)(pInput, &output) != 0) { dError("node:%s, failed to open since %s", pWrapper->name, terrstr()); return -1; } @@ -136,27 +136,39 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) { pWrapper->deployed = true; } else { if (dmInitParentProc(pWrapper) != 0) return -1; - if (dmWriteShmFile(pWrapper) != 0) return -1; + if (dmWriteShmFile(pWrapper->path, pWrapper->name, &pWrapper->procShm) != 0) return -1; if (dmRunParentProc(pWrapper) != 0) return -1; } + if (output.dnodeId != 0) { + pInput->dnodeId = output.dnodeId; + } + if (output.pMgmt != NULL) { + pWrapper->pMgmt = output.pMgmt; + } + if (output.mnodeEps.numOfEps != 0) { + pWrapper->pDnode->mnodeEps = output.mnodeEps; + } + dmReportStartup(pWrapper->pDnode, pWrapper->name, "openned"); return 0; } int32_t dmStartNode(SMgmtWrapper *pWrapper) { + if (!pWrapper->required) return 0; + if (pWrapper->procType == DND_PROC_PARENT) { dInfo("node:%s, not start in parent process", pWrapper->name); } else if (pWrapper->procType == DND_PROC_CHILD) { dInfo("node:%s, start in child process", pWrapper->name); - if (pWrapper->ntype != DNODE) { - if (pWrapper->fp.startFp != NULL && (*pWrapper->fp.startFp)(pWrapper) != 0) { + if (pWrapper->nodeType != DNODE) { + if (pWrapper->func.startFp != NULL && (*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) { dError("node:%s, failed to start since %s", pWrapper->name, terrstr()); return -1; } } } else { - if (pWrapper->fp.startFp != NULL && (*pWrapper->fp.startFp)(pWrapper) != 0) { + if (pWrapper->func.startFp != NULL && (*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) { dError("node:%s, failed to start since %s", pWrapper->name, terrstr()); return -1; } @@ -167,8 +179,9 @@ int32_t dmStartNode(SMgmtWrapper *pWrapper) { } void dmStopNode(SMgmtWrapper *pWrapper) { - if (pWrapper->fp.stopFp != NULL) { - (*pWrapper->fp.stopFp)(pWrapper); + if (pWrapper->func.stopFp != NULL && pWrapper->pMgmt != NULL) { + (*pWrapper->func.stopFp)(pWrapper->pMgmt); + dDebug("node:%s, has been stopped", pWrapper->name); } } @@ -190,10 +203,11 @@ void dmCloseNode(SMgmtWrapper *pWrapper) { } } - dmStopNode(pWrapper); - taosWLockLatch(&pWrapper->latch); - (*pWrapper->fp.closeFp)(pWrapper); + if (pWrapper->pMgmt != NULL) { + (*pWrapper->func.closeFp)(pWrapper->pMgmt); + pWrapper->pMgmt = NULL; + } taosWUnLockLatch(&pWrapper->latch); if (pWrapper->procObj) { @@ -205,49 +219,29 @@ void dmCloseNode(SMgmtWrapper *pWrapper) { } static int32_t dmOpenNodes(SDnode *pDnode) { - if (pDnode->ptype == DND_PROC_CHILD) { - SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype]; - pWrapper->required = dmRequireNode(pWrapper); - if (!pWrapper->required) { - dError("dnode:%s, failed to open since not required", pWrapper->name); - } - - pWrapper->procType = DND_PROC_CHILD; - if (dmInitClient(pDnode) != 0) { - return -1; - } - - pDnode->data.msgCb = dmGetMsgcb(pWrapper); - tmsgSetDefaultMsgCb(&pDnode->data.msgCb); - - if (dmOpenNode(pWrapper) != 0) { - dError("node:%s, failed to open since %s", pWrapper->name, terrstr()); - return -1; - } - } else { - for (EDndNodeType n = DNODE; n < NODE_END; ++n) { - SMgmtWrapper *pWrapper = &pDnode->wrappers[n]; - pWrapper->required = dmRequireNode(pWrapper); - if (!pWrapper->required) continue; - - if (pDnode->ptype == DND_PROC_PARENT && n != DNODE) { - pWrapper->procType = DND_PROC_PARENT; - } else { - pWrapper->procType = DND_PROC_SINGLE; + for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { + SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; + if (!pWrapper->required) continue; + if (ntype == DNODE) { + pWrapper->procType = DND_PROC_SINGLE; + if (dmOpenNode(pWrapper) != 0) { + return -1; } - - if (n == DNODE) { - if (dmInitClient(pDnode) != 0) { + } else { + if (pDnode->ptype == DND_PROC_CHILD) { + if (pDnode->ntype == ntype) { + pWrapper->procType = DND_PROC_CHILD; + if (dmOpenNode(pWrapper) != 0) { + return -1; + } + } else { + pWrapper->required = false; + } + } else { + pWrapper->procType = pDnode->ptype; + if (dmOpenNode(pWrapper) != 0) { return -1; } - - pDnode->data.msgCb = dmGetMsgcb(pWrapper); - tmsgSetDefaultMsgCb(&pDnode->data.msgCb); - } - - if (dmOpenNode(pWrapper) != 0) { - dError("node:%s, failed to open since %s", pWrapper->name, terrstr()); - return -1; } } } @@ -257,9 +251,9 @@ static int32_t dmOpenNodes(SDnode *pDnode) { } static int32_t dmStartNodes(SDnode *pDnode) { - for (EDndNodeType n = DNODE; n < NODE_END; ++n) { - SMgmtWrapper *pWrapper = &pDnode->wrappers[n]; - if (!pWrapper->required) continue; + for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { + SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; + if (ntype == DNODE && (pDnode->ptype == DND_PROC_CHILD || pDnode->ptype == DND_PROC_TEST)) continue; if (dmStartNode(pWrapper) != 0) { dError("node:%s, failed to start since %s", pWrapper->name, terrstr()); return -1; @@ -313,16 +307,27 @@ static void dmWatchNodes(SDnode *pDnode) { } int32_t dmRun(SDnode *pDnode) { - if (!tsMultiProcess) { + if (tsMultiProcess == 0) { pDnode->ptype = DND_PROC_SINGLE; - dInfo("dnode run in single process"); + dInfo("dnode run in single process mode"); + } else if (tsMultiProcess == 2) { + pDnode->ptype = DND_PROC_TEST; + dInfo("dnode run in multi-process test mode"); } else if (pDnode->ntype == DNODE || pDnode->ntype == NODE_END) { pDnode->ptype = DND_PROC_PARENT; - dInfo("dnode run in parent process"); + dInfo("dnode run in parent process mode"); } else { pDnode->ptype = DND_PROC_CHILD; SMgmtWrapper *pWrapper = &pDnode->wrappers[pDnode->ntype]; - dInfo("%s run in child process", pWrapper->name); + dInfo("%s run in child process mode", pWrapper->name); + } + + if (pDnode->ptype != DND_PROC_CHILD) { + if (dmInitServer(pDnode) != 0) { + dError("failed to init transport since %s", terrstr()); + return -1; + } + dmReportStartup(pDnode, "dnode-transport", "initialized"); } if (dmOpenNodes(pDnode) != 0) { diff --git a/source/dnode/mgmt/implement/src/dmTransport.c b/source/dnode/mgmt/node_mgmt/src/dmTransport.c similarity index 84% rename from source/dnode/mgmt/implement/src/dmTransport.c rename to source/dnode/mgmt/node_mgmt/src/dmTransport.c index a58999bf2d852e7af5bcb90f1865c26784221831..30ec04d9ef2f9b69b7f5242bc0a37e36b3d455e3 100644 --- a/source/dnode/mgmt/implement/src/dmTransport.c +++ b/source/dnode/mgmt/node_mgmt/src/dmTransport.c @@ -14,8 +14,7 @@ */ #define _DEFAULT_SOURCE -#include "dmImp.h" - +#include "dmMgmt.h" #include "qworker.h" #define INTERNAL_USER "_dnd" @@ -23,21 +22,21 @@ #define INTERNAL_SECRET "_pwd" static void dmGetMnodeEpSet(SDnode *pDnode, SEpSet *pEpSet) { - taosRLockLatch(&pDnode->data.latch); - *pEpSet = pDnode->data.mnodeEps; - taosRUnLockLatch(&pDnode->data.latch); + taosRLockLatch(&pDnode->latch); + *pEpSet = pDnode->mnodeEps; + taosRUnLockLatch(&pDnode->latch); } static void dmSetMnodeEpSet(SDnode *pDnode, SEpSet *pEpSet) { dInfo("mnode is changed, num:%d use:%d", pEpSet->numOfEps, pEpSet->inUse); - taosWLockLatch(&pDnode->data.latch); - pDnode->data.mnodeEps = *pEpSet; + taosWLockLatch(&pDnode->latch); + pDnode->mnodeEps = *pEpSet; for (int32_t i = 0; i < pEpSet->numOfEps; ++i) { dInfo("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port); } - taosWUnLockLatch(&pDnode->data.latch); + taosWUnLockLatch(&pDnode->latch); } static inline NodeMsgFp dmGetMsgFp(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { @@ -64,7 +63,7 @@ static inline int32_t dmBuildMsg(SNodeMsg *pMsg, SRpcMsg *pRpc) { if ((pRpc->msgType & 1u)) { assert(pRpc->refId != 0); } - // assert(pRpc->handle != NULL && pRpc->refId != 0 && pMsg->rpcMsg.refId != 0); + return 0; } @@ -76,20 +75,16 @@ static void dmProcessRpcMsg(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, SEpSet *pEpSe bool needRelease = false; bool isReq = msgType & 1U; - if (pEpSet && pEpSet->numOfEps > 0 && msgType == TDMT_MND_STATUS_RSP) { - dmSetMnodeEpSet(pWrapper->pDnode, pEpSet); - } - if (dmMarkWrapper(pWrapper) != 0) goto _OVER; - needRelease = true; + if ((msgFp = dmGetMsgFp(pWrapper, pRpc)) == NULL) goto _OVER; if ((pMsg = taosAllocateQitem(sizeof(SNodeMsg))) == NULL) goto _OVER; if (dmBuildMsg(pMsg, pRpc) != 0) goto _OVER; if (pWrapper->procType != DND_PROC_PARENT) { dTrace("msg:%p, created, type:%s handle:%p user:%s", pMsg, TMSG_INFO(msgType), pRpc->handle, pMsg->user); - code = (*msgFp)(pWrapper, pMsg); + code = (*msgFp)(pWrapper->pMgmt, pMsg); } else { dTrace("msg:%p, created and put into child queue, type:%s handle:%p code:0x%04x user:%s contLen:%d", pMsg, TMSG_INFO(msgType), pRpc->handle, pMsg->rpcMsg.code & 0XFFFF, pMsg->user, pRpc->contLen); @@ -133,13 +128,17 @@ static void dmProcessMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { tmsg_t msgType = pMsg->msgType; bool isReq = msgType & 1u; SMsgHandle *pHandle = &pTrans->msgHandles[TMSG_INDEX(msgType)]; - SMgmtWrapper *pWrapper = pHandle->pNdWrapper; + SMgmtWrapper *pWrapper = NULL; switch (msgType) { case TDMT_DND_SERVER_STATUS: - dTrace("server status req will be processed, handle:%p, app:%p", pMsg->handle, pMsg->ahandle); - dmProcessServerStatusReq(pDnode, pMsg); - return; + if (pDnode->status != DND_STAT_RUNNING) { + dTrace("server status req will be processed, handle:%p, app:%p", pMsg->handle, pMsg->ahandle); + dmProcessServerStartupStatus(pDnode, pMsg); + return; + } else { + break; + } case TDMT_DND_NET_TEST: dTrace("net test req will be processed, handle:%p, app:%p", pMsg->handle, pMsg->ahandle); dmProcessNetTestReq(pDnode, pMsg); @@ -171,7 +170,7 @@ static void dmProcessMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { return; } - if (pWrapper == NULL) { + if (pHandle->defaultNtype == NODE_END) { dError("msg:%s not processed since no handle, handle:%p app:%p", TMSG_INFO(msgType), pMsg->handle, pMsg->ahandle); if (isReq) { SRpcMsg rspMsg = { @@ -182,13 +181,14 @@ static void dmProcessMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { return; } - if (pHandle->pMndWrapper != NULL || pHandle->pQndWrapper != NULL) { + pWrapper = &pDnode->wrappers[pHandle->defaultNtype]; + if (pHandle->needCheckVgId) { SMsgHead *pHead = pMsg->pCont; int32_t vgId = ntohl(pHead->vgId); if (vgId == QNODE_HANDLE) { - pWrapper = pHandle->pQndWrapper; + pWrapper = &pDnode->wrappers[QNODE]; } else if (vgId == MNODE_HANDLE) { - pWrapper = pHandle->pMndWrapper; + pWrapper = &pDnode->wrappers[MNODE]; } else { } } @@ -203,35 +203,24 @@ static void dmProcessMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { int32_t dmInitMsgHandle(SDnode *pDnode) { SDnodeTrans *pTrans = &pDnode->trans; - for (EDndNodeType n = DNODE; n < NODE_END; ++n) { - SMgmtWrapper *pWrapper = &pDnode->wrappers[n]; - - for (int32_t msgIndex = 0; msgIndex < TDMT_MAX; ++msgIndex) { - NodeMsgFp msgFp = pWrapper->msgFps[msgIndex]; - int8_t vgId = pWrapper->msgVgIds[msgIndex]; - if (msgFp == NULL) continue; + for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { + SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; + SArray *pArray = (*pWrapper->func.getHandlesFp)(); + if (pArray == NULL) return -1; - SMsgHandle *pHandle = &pTrans->msgHandles[msgIndex]; - if (vgId == QNODE_HANDLE) { - if (pHandle->pQndWrapper != NULL) { - dError("msg:%s has multiple process nodes", tMsgInfo[msgIndex]); - return -1; - } - pHandle->pQndWrapper = pWrapper; - } else if (vgId == MNODE_HANDLE) { - if (pHandle->pMndWrapper != NULL) { - dError("msg:%s has multiple process nodes", tMsgInfo[msgIndex]); - return -1; - } - pHandle->pMndWrapper = pWrapper; - } else { - if (pHandle->pNdWrapper != NULL) { - dError("msg:%s has multiple process nodes", tMsgInfo[msgIndex]); - return -1; - } - pHandle->pNdWrapper = pWrapper; + for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) { + SMgmtHandle *pMgmt = taosArrayGet(pArray, i); + SMsgHandle *pHandle = &pTrans->msgHandles[TMSG_INDEX(pMgmt->msgType)]; + if (pMgmt->needCheckVgId) { + pHandle->needCheckVgId = pMgmt->needCheckVgId; + } + if (!pMgmt->needCheckVgId) { + pHandle->defaultNtype = ntype; } + pWrapper->msgFps[TMSG_INDEX(pMgmt->msgType)] = pMgmt->msgFp; } + + taosArrayDestroy(pArray); } return 0; @@ -244,7 +233,7 @@ static void dmSendRpcRedirectRsp(SDnode *pDnode, const SRpcMsg *pReq) { dDebug("RPC %p, req is redirected, num:%d use:%d", pReq->handle, epSet.numOfEps, epSet.inUse); for (int32_t i = 0; i < epSet.numOfEps; ++i) { dDebug("mnode index:%d %s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port); - if (strcmp(epSet.eps[i].fqdn, pDnode->data.localFqdn) == 0 && epSet.eps[i].port == pDnode->data.serverPort) { + if (strcmp(epSet.eps[i].fqdn, pDnode->input.localFqdn) == 0 && epSet.eps[i].port == pDnode->input.serverPort) { epSet.inUse = (i + 1) % epSet.numOfEps; } @@ -271,29 +260,32 @@ static inline void dmSendRpcRsp(SDnode *pDnode, const SRpcMsg *pRsp) { } } -void dmSendRecv(SDnode *pDnode, SEpSet *pEpSet, SRpcMsg *pReq, SRpcMsg *pRsp) { - rpcSendRecv(pDnode->trans.clientRpc, pEpSet, pReq, pRsp); +static inline void dmSendRecv(SDnode *pDnode, SEpSet *pEpSet, SRpcMsg *pReq, SRpcMsg *pRsp) { + if (pDnode->status != DND_STAT_RUNNING) { + pRsp->code = TSDB_CODE_NODE_OFFLINE; + rpcFreeCont(pReq->pCont); + pReq->pCont = NULL; + } else { + rpcSendRecv(pDnode->trans.clientRpc, pEpSet, pReq, pRsp); + } } -void dmSendToMnodeRecv(SDnode *pDnode, SRpcMsg *pReq, SRpcMsg *pRsp) { +static inline void dmSendToMnodeRecv(SMgmtWrapper *pWrapper, SRpcMsg *pReq, SRpcMsg *pRsp) { SEpSet epSet = {0}; - dmGetMnodeEpSet(pDnode, &epSet); - rpcSendRecv(pDnode->trans.clientRpc, &epSet, pReq, pRsp); + dmGetMnodeEpSet(pWrapper->pDnode, &epSet); + dmSendRecv(pWrapper->pDnode, &epSet, pReq, pRsp); } static inline int32_t dmSendReq(SMgmtWrapper *pWrapper, const SEpSet *pEpSet, SRpcMsg *pReq) { SDnode *pDnode = pWrapper->pDnode; - if (pDnode->status != DND_STAT_RUNNING) { + if (pDnode->status != DND_STAT_RUNNING || pDnode->trans.clientRpc == NULL) { + rpcFreeCont(pReq->pCont); + pReq->pCont = NULL; terrno = TSDB_CODE_NODE_OFFLINE; dError("failed to send rpc msg since %s, handle:%p", terrstr(), pReq->handle); return -1; } - if (pDnode->trans.clientRpc == NULL) { - terrno = TSDB_CODE_NODE_OFFLINE; - return -1; - } - rpcSendRequest(pDnode->trans.clientRpc, pEpSet, pReq, NULL); return 0; } @@ -326,17 +318,6 @@ static inline void dmSendRedirectRsp(SMgmtWrapper *pWrapper, const SRpcMsg *pRsp } } -#if 0 -static inline void dmSendRedirectRsp(SMgmtWrapper *pWrapper, const SRpcMsg *pRsp, const SEpSet *pNewEpSet) { - ASSERT(pRsp->code == TSDB_CODE_RPC_REDIRECT); - if (pWrapper->procType != DND_PROC_CHILD) { - rpcSendRedirectRsp(pRsp->handle, pNewEpSet); - } else { - taosProcPutToParentQ(pWrapper->procObj, pRsp, sizeof(SRpcMsg), pRsp->pCont, pRsp->contLen, PROC_FUNC_RSP); - } -} -#endif - static inline void dmRegisterBrokenLinkArg(SMgmtWrapper *pWrapper, SRpcMsg *pMsg) { if (pWrapper->procType != DND_PROC_CHILD) { rpcRegisterBrokenLinkArg(pMsg); @@ -361,7 +342,7 @@ static void dmConsumeChildQueue(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int16_t dTrace("msg:%p, get from child queue, handle:%p app:%p", pMsg, pRpc->handle, pRpc->ahandle); NodeMsgFp msgFp = pWrapper->msgFps[TMSG_INDEX(pRpc->msgType)]; - int32_t code = (*msgFp)(pWrapper, pMsg); + int32_t code = (*msgFp)(pWrapper->pMgmt, pMsg); if (code != 0) { dError("msg:%p, failed to process since code:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code)); @@ -460,11 +441,7 @@ int32_t dmInitClient(SDnode *pDnode) { return -1; } - pDnode->data.msgCb = dmGetMsgcb(&pDnode->wrappers[DNODE]); - tmsgSetDefaultMsgCb(&pDnode->data.msgCb); - dDebug("dnode rpc client is initialized"); - return 0; } @@ -515,8 +492,10 @@ static inline int32_t dmRetrieveUserAuthInfo(SDnode *pDnode, char *user, char *s SRpcMsg rpcMsg = {.pCont = pReq, .contLen = contLen, .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528}; SRpcMsg rpcRsp = {0}; + SEpSet epSet = {0}; dTrace("user:%s, send user auth req to other mnodes, spi:%d encrypt:%d", user, authReq.spi, authReq.encrypt); - dmSendToMnodeRecv(pDnode, &rpcMsg, &rpcRsp); + dmGetMnodeEpSet(pDnode, &epSet); + dmSendRecv(pDnode, &epSet, &rpcMsg, &rpcRsp); if (rpcRsp.code != 0) { terrno = rpcRsp.code; @@ -541,8 +520,8 @@ int32_t dmInitServer(SDnode *pDnode) { SRpcInit rpcInit = {0}; - strncpy(rpcInit.localFqdn, pDnode->data.localFqdn, strlen(pDnode->data.localFqdn)); - rpcInit.localPort = pDnode->data.serverPort; + strncpy(rpcInit.localFqdn, pDnode->input.localFqdn, strlen(pDnode->input.localFqdn)); + rpcInit.localPort = pDnode->input.serverPort; rpcInit.label = "DND"; rpcInit.numOfThreads = tsNumOfRpcThreads; rpcInit.cfp = (RpcCfp)dmProcessMsg; @@ -573,14 +552,15 @@ void dmCleanupServer(SDnode *pDnode) { SMsgCb dmGetMsgcb(SMgmtWrapper *pWrapper) { SMsgCb msgCb = { + .pWrapper = pWrapper, + .clientRpc = pWrapper->pDnode->trans.clientRpc, .sendReqFp = dmSendReq, .sendRspFp = dmSendRsp, + .sendMnodeRecvFp = dmSendToMnodeRecv, .sendRedirectRspFp = dmSendRedirectRsp, .registerBrokenLinkArgFp = dmRegisterBrokenLinkArg, .releaseHandleFp = dmReleaseHandle, .reportStartupFp = dmReportStartupByWrapper, - .pWrapper = pWrapper, - .clientRpc = pWrapper->pDnode->trans.clientRpc, }; return msgCb; } diff --git a/source/dnode/mgmt/node_util/CMakeLists.txt b/source/dnode/mgmt/node_util/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5d879fdbcf356f6cefcfdc8046696fa4010cd1b4 --- /dev/null +++ b/source/dnode/mgmt/node_util/CMakeLists.txt @@ -0,0 +1,10 @@ +aux_source_directory(src NODE_UTIL) +add_library(node_util STATIC ${NODE_UTIL}) +target_include_directories( + node_util + PUBLIC "${TD_SOURCE_DIR}/include/dnode/mgmt" + PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + node_util cjson mnode vnode qnode snode bnode wal sync taos_static tfs monitor +) \ No newline at end of file diff --git a/source/dnode/mgmt/node_util/inc/dmUtil.h b/source/dnode/mgmt/node_util/inc/dmUtil.h new file mode 100644 index 0000000000000000000000000000000000000000..8d4ea88d420554973846050ceaf5f15263705c1c --- /dev/null +++ b/source/dnode/mgmt/node_util/inc/dmUtil.h @@ -0,0 +1,188 @@ +/* + * 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 _TD_DM_INT_H_ +#define _TD_DM_INT_H_ + +#include "cJSON.h" +#include "tcache.h" +#include "tcrc32c.h" +#include "tdatablock.h" +#include "tglobal.h" +#include "thash.h" +#include "tlockfree.h" +#include "tlog.h" +#include "tmsg.h" +#include "tmsgcb.h" +#include "tprocess.h" +#include "tqueue.h" +#include "trpc.h" +#include "tthread.h" +#include "ttime.h" +#include "tworker.h" + +#include "dnode.h" +#include "mnode.h" +#include "monitor.h" +#include "sync.h" +#include "wal.h" + +#include "libs/function/function.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define dFatal(...) { if (dDebugFlag & DEBUG_FATAL) { taosPrintLog("DND FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} +#define dError(...) { if (dDebugFlag & DEBUG_ERROR) { taosPrintLog("DND ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} +#define dWarn(...) { if (dDebugFlag & DEBUG_WARN) { taosPrintLog("DND WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} +#define dInfo(...) { if (dDebugFlag & DEBUG_INFO) { taosPrintLog("DND ", DEBUG_INFO, 255, __VA_ARGS__); }} +#define dDebug(...) { if (dDebugFlag & DEBUG_DEBUG) { taosPrintLog("DND ", DEBUG_DEBUG, dDebugFlag, __VA_ARGS__); }} +#define dTrace(...) { if (dDebugFlag & DEBUG_TRACE) { taosPrintLog("DND ", DEBUG_TRACE, dDebugFlag, __VA_ARGS__); }} + +typedef enum { + DNODE = 0, + MNODE = 1, + VNODE = 2, + QNODE = 3, + SNODE = 4, + BNODE = 5, + NODE_END = 6, +} EDndNodeType; + +typedef enum { + DND_STAT_INIT, + DND_STAT_RUNNING, + DND_STAT_STOPPED, +} EDndRunStatus; + +typedef enum { + DND_ENV_INIT, + DND_ENV_READY, + DND_ENV_CLEANUP, +} EDndEnvStatus; + +typedef enum { + DND_PROC_SINGLE, + DND_PROC_CHILD, + DND_PROC_PARENT, + DND_PROC_TEST, +} EDndProcType; + +typedef int32_t (*ProcessCreateNodeFp)(struct SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg); +typedef int32_t (*ProcessDropNodeFp)(struct SDnode *pDnode, EDndNodeType ntype, SNodeMsg *pMsg); +typedef bool (*IsNodeDeployedFp)(struct SDnode *pDnode, EDndNodeType ntype); + +typedef struct { + const char *path; + const char *name; + SMsgCb msgCb; + int32_t dnodeId; + int64_t clusterId; + const char *localEp; + const char *firstEp; + const char *secondEp; + const char *localFqdn; + uint16_t serverPort; + int32_t supportVnodes; + int32_t numOfDisks; + SDiskCfg *disks; + const char *dataDir; + struct SDnode *pDnode; + ProcessCreateNodeFp processCreateNodeFp; + ProcessDropNodeFp processDropNodeFp; + IsNodeDeployedFp isNodeDeployedFp; +} SMgmtInputOpt; + +typedef struct { + int32_t dnodeId; + void *pMgmt; + SEpSet mnodeEps; +} SMgmtOutputOpt; + +typedef int32_t (*NodeMsgFp)(void *pMgmt, SNodeMsg *pMsg); +typedef int32_t (*NodeOpenFp)(const SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput); +typedef void (*NodeCloseFp)(void *pMgmt); +typedef int32_t (*NodeStartFp)(void *pMgmt); +typedef void (*NodeStopFp)(void *pMgmt); +typedef int32_t (*NodeCreateFp)(const SMgmtInputOpt *pInput, SNodeMsg *pMsg); +typedef int32_t (*NodeDropFp)(void *pMgmt, SNodeMsg *pMsg); +typedef int32_t (*NodeRequireFp)(const SMgmtInputOpt *pInput, bool *required); +typedef SArray *(*NodeGetHandlesFp)(); // array of SMgmtHandle + +typedef struct { + NodeOpenFp openFp; + NodeCloseFp closeFp; + NodeStartFp startFp; + NodeStopFp stopFp; + NodeCreateFp createFp; + NodeDropFp dropFp; + NodeRequireFp requiredFp; + NodeGetHandlesFp getHandlesFp; +} SMgmtFunc; + +typedef struct { + tmsg_t msgType; + bool needCheckVgId; + NodeMsgFp msgFp; +} SMgmtHandle; + +// dmUtil.c +const char *dmStatStr(EDndRunStatus stype); +const char *dmNodeLogName(EDndNodeType ntype); +const char *dmNodeProcName(EDndNodeType ntype); +const char *dmNodeName(EDndNodeType ntype); +const char *dmEventStr(EDndEvent etype); +const char *dmProcStr(EDndProcType ptype); +void *dmSetMgmtHandle(SArray *pArray, tmsg_t msgType, void *nodeMsgFp, bool needCheckVgId); +void dmGetMonitorSystemInfo(SMonSysInfo *pInfo); + +// dmFile.c +int32_t dmReadFile(const char *path, const char *name, bool *pDeployed); +int32_t dmWriteFile(const char *path, const char *name, bool deployed); +TdFilePtr dmCheckRunning(const char *dataDir); +int32_t dmReadShmFile(const char *path, const char *name, EDndNodeType runType, SShm *pShm); +int32_t dmWriteShmFile(const char *path, const char *name, const SShm *pShm); + +// common define +typedef struct { + int32_t dnodeId; + int64_t clusterId; + int64_t dnodeVer; + int64_t updateTime; + int64_t rebootTime; + int32_t unsyncedVgId; + ESyncState vndState; + ESyncState mndState; + bool dropped; + bool stopped; + SEpSet mnodeEps; + SArray *dnodeEps; + SHashObj *dnodeHash; + SRWLatch latch; + SMsgCb msgCb; + const char *localEp; + const char *localFqdn; + const char *firstEp; + const char *secondEp; + int32_t supportVnodes; + uint16_t serverPort; +} SDnodeData; + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DM_INT_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/interface/src/dmEnv.c b/source/dnode/mgmt/node_util/src/dmEnv.c similarity index 82% rename from source/dnode/mgmt/interface/src/dmEnv.c rename to source/dnode/mgmt/node_util/src/dmEnv.c index 2c836714cea3fd22c2c3fb647b74b00998a0ebbf..582df8055b8e6c389efe6bf9fba60d4070b03cfe 100644 --- a/source/dnode/mgmt/interface/src/dmEnv.c +++ b/source/dnode/mgmt/node_util/src/dmEnv.c @@ -14,16 +14,15 @@ */ #define _DEFAULT_SOURCE -#include "dmInt.h" -#include "wal.h" +#include "dmUtil.h" static int8_t once = DND_ENV_INIT; int32_t dmInit() { - dDebug("start to init dnode env"); + dInfo("start to init env"); if (atomic_val_compare_exchange_8(&once, DND_ENV_INIT, DND_ENV_READY) != DND_ENV_INIT) { + dError("env is already initialized"); terrno = TSDB_CODE_REPEAT_INIT; - dError("failed to init dnode env since %s", terrstr()); return -1; } @@ -41,14 +40,14 @@ int32_t dmInit() { return -1; } - dInfo("dnode env is initialized"); + dInfo("env is initialized"); return 0; } void dmCleanup() { - dDebug("start to cleanup dnode env"); + dDebug("start to cleanup env"); if (atomic_val_compare_exchange_8(&once, DND_ENV_READY, DND_ENV_CLEANUP) != DND_ENV_READY) { - dError("dnode env is already cleaned up"); + dError("env is already cleaned up"); return; } @@ -56,6 +55,7 @@ void dmCleanup() { syncCleanUp(); walCleanUp(); udfcClose(); + udfStopUdfd(); taosStopCacheRefreshWorker(); - dInfo("dnode env is cleaned up"); + dInfo("env is cleaned up"); } diff --git a/source/dnode/mgmt/interface/src/dmFile.c b/source/dnode/mgmt/node_util/src/dmFile.c similarity index 72% rename from source/dnode/mgmt/interface/src/dmFile.c rename to source/dnode/mgmt/node_util/src/dmFile.c index 38acf169be47f51c96cbb0339f58b4b964c3d382..7ac6fc129d2bb591706d6ed722878359c4993515 100644 --- a/source/dnode/mgmt/interface/src/dmFile.c +++ b/source/dnode/mgmt/node_util/src/dmFile.c @@ -14,11 +14,11 @@ */ #define _DEFAULT_SOURCE -#include "dmInt.h" +#include "dmUtil.h" #define MAXLEN 1024 -int32_t dmReadFile(SMgmtWrapper *pWrapper, bool *pDeployed) { +int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) { int32_t code = TSDB_CODE_INVALID_JSON_FORMAT; int64_t len = 0; char content[MAXLEN + 1] = {0}; @@ -26,10 +26,9 @@ int32_t dmReadFile(SMgmtWrapper *pWrapper, bool *pDeployed) { char file[PATH_MAX] = {0}; TdFilePtr pFile = NULL; - snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name); + snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name); pFile = taosOpenFile(file, TD_FILE_READ); if (pFile == NULL) { - // dDebug("file %s not exist", file); code = 0; goto _OVER; } @@ -64,7 +63,7 @@ _OVER: return code; } -int32_t dmWriteFile(SMgmtWrapper *pWrapper, bool deployed) { +int32_t dmWriteFile(const char *path, const char *name, bool deployed) { int32_t code = -1; int32_t len = 0; char content[MAXLEN + 1] = {0}; @@ -72,8 +71,8 @@ int32_t dmWriteFile(SMgmtWrapper *pWrapper, bool deployed) { char realfile[PATH_MAX] = {0}; TdFilePtr pFile = NULL; - snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name); - snprintf(realfile, sizeof(realfile), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name); + snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name); + snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name); pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { @@ -140,17 +139,16 @@ TdFilePtr dmCheckRunning(const char *dataDir) { return pFile; } -int32_t dmReadShmFile(SMgmtWrapper *pWrapper) { +int32_t dmReadShmFile(const char *path, const char *name, EDndNodeType runType, SShm *pShm) { int32_t code = -1; char content[MAXLEN + 1] = {0}; char file[PATH_MAX] = {0}; cJSON *root = NULL; TdFilePtr pFile = NULL; - snprintf(file, sizeof(file), "%s%sshmfile", pWrapper->path, TD_DIRSEP); + snprintf(file, sizeof(file), "%s%sshmfile", path, TD_DIRSEP); pFile = taosOpenFile(file, TD_FILE_READ); if (pFile == NULL) { - // dDebug("node:%s, file %s not exist", pWrapper->name, file); code = 0; goto _OVER; } @@ -159,36 +157,36 @@ int32_t dmReadShmFile(SMgmtWrapper *pWrapper) { root = cJSON_Parse(content); if (root == NULL) { terrno = TSDB_CODE_INVALID_JSON_FORMAT; - dError("node:%s, failed to read %s since invalid json format", pWrapper->name, file); + dError("node:%s, failed to read %s since invalid json format", name, file); goto _OVER; } cJSON *shmid = cJSON_GetObjectItem(root, "shmid"); if (shmid && shmid->type == cJSON_Number) { - pWrapper->procShm.id = shmid->valueint; + pShm->id = shmid->valueint; } cJSON *shmsize = cJSON_GetObjectItem(root, "shmsize"); if (shmsize && shmsize->type == cJSON_Number) { - pWrapper->procShm.size = shmsize->valueint; + pShm->size = shmsize->valueint; } } - if (!tsMultiProcess || pWrapper->pDnode->ntype == DNODE || pWrapper->pDnode->ntype == NODE_END) { - if (pWrapper->procShm.id >= 0) { - dDebug("node:%s, shmid:%d, is closed, size:%d", pWrapper->name, pWrapper->procShm.id, pWrapper->procShm.size); - taosDropShm(&pWrapper->procShm); + if (!tsMultiProcess || runType == DNODE || runType == NODE_END) { + if (pShm->id >= 0) { + dDebug("node:%s, shmid:%d, is closed, size:%d", name, pShm->id, pShm->size); + taosDropShm(pShm); } } else { - if (taosAttachShm(&pWrapper->procShm) != 0) { + if (taosAttachShm(pShm) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); - dError("shmid:%d, failed to attach shm since %s", pWrapper->procShm.id, terrstr()); + dError("shmid:%d, failed to attach shm since %s", pShm->id, terrstr()); goto _OVER; } - dInfo("node:%s, shmid:%d is attached, size:%d", pWrapper->name, pWrapper->procShm.id, pWrapper->procShm.size); + dInfo("node:%s, shmid:%d is attached, size:%d", name, pShm->id, pShm->size); } - dDebug("node:%s, successed to load %s", pWrapper->name, file); + dDebug("node:%s, successed to load %s", name, file); code = 0; _OVER: @@ -198,7 +196,7 @@ _OVER: return code; } -int32_t dmWriteShmFile(SMgmtWrapper *pWrapper) { +int32_t dmWriteShmFile(const char *path, const char *name, const SShm *pShm) { int32_t code = -1; int32_t len = 0; char content[MAXLEN + 1] = {0}; @@ -206,30 +204,30 @@ int32_t dmWriteShmFile(SMgmtWrapper *pWrapper) { char realfile[PATH_MAX] = {0}; TdFilePtr pFile = NULL; - snprintf(file, sizeof(file), "%s%sshmfile.bak", pWrapper->path, TD_DIRSEP); - snprintf(realfile, sizeof(realfile), "%s%sshmfile", pWrapper->path, TD_DIRSEP); + snprintf(file, sizeof(file), "%s%sshmfile.bak", path, TD_DIRSEP); + snprintf(realfile, sizeof(realfile), "%s%sshmfile", path, TD_DIRSEP); pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { terrno = TAOS_SYSTEM_ERROR(errno); - dError("node:%s, failed to open file:%s since %s", pWrapper->name, file, terrstr()); + dError("node:%s, failed to open file:%s since %s", name, file, terrstr()); goto _OVER; } len += snprintf(content + len, MAXLEN - len, "{\n"); - len += snprintf(content + len, MAXLEN - len, " \"shmid\":%d,\n", pWrapper->procShm.id); - len += snprintf(content + len, MAXLEN - len, " \"shmsize\":%d\n", pWrapper->procShm.size); + len += snprintf(content + len, MAXLEN - len, " \"shmid\":%d,\n", pShm->id); + len += snprintf(content + len, MAXLEN - len, " \"shmsize\":%d\n", pShm->size); len += snprintf(content + len, MAXLEN - len, "}\n"); if (taosWriteFile(pFile, content, len) != len) { terrno = TAOS_SYSTEM_ERROR(errno); - dError("node:%s, failed to write file:%s since %s", pWrapper->name, file, terrstr()); + dError("node:%s, failed to write file:%s since %s", name, file, terrstr()); goto _OVER; } if (taosFsyncFile(pFile) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); - dError("node:%s, failed to fsync file:%s since %s", pWrapper->name, file, terrstr()); + dError("node:%s, failed to fsync file:%s since %s", name, file, terrstr()); goto _OVER; } @@ -237,11 +235,11 @@ int32_t dmWriteShmFile(SMgmtWrapper *pWrapper) { if (taosRenameFile(file, realfile) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); - dError("node:%s, failed to rename %s to %s since %s", pWrapper->name, file, realfile, terrstr()); + dError("node:%s, failed to rename %s to %s since %s", name, file, realfile, terrstr()); return -1; } - dInfo("node:%s, successed to write %s", pWrapper->name, realfile); + dInfo("node:%s, successed to write %s", name, realfile); code = 0; _OVER: diff --git a/source/dnode/mgmt/node_util/src/dmUtil.c b/source/dnode/mgmt/node_util/src/dmUtil.c new file mode 100644 index 0000000000000000000000000000000000000000..e913af203b76063e176da433e5773d400764e7d8 --- /dev/null +++ b/source/dnode/mgmt/node_util/src/dmUtil.c @@ -0,0 +1,132 @@ +/* + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "dmUtil.h" + +const char *dmStatStr(EDndRunStatus stype) { + switch (stype) { + case DND_STAT_INIT: + return "init"; + case DND_STAT_RUNNING: + return "running"; + case DND_STAT_STOPPED: + return "stopped"; + default: + return "UNKNOWN"; + } +} + +const char *dmNodeLogName(EDndNodeType ntype) { + switch (ntype) { + case VNODE: + return "vnode"; + case QNODE: + return "qnode"; + case SNODE: + return "snode"; + case MNODE: + return "mnode"; + case BNODE: + return "bnode"; + default: + return "taosd"; + } +} + +const char *dmNodeProcName(EDndNodeType ntype) { + switch (ntype) { + case VNODE: + return "taosv"; + case QNODE: + return "taosq"; + case SNODE: + return "taoss"; + case MNODE: + return "taosm"; + case BNODE: + return "taosb"; + default: + return "taosd"; + } +} + +const char *dmNodeName(EDndNodeType ntype) { + switch (ntype) { + case VNODE: + return "vnode"; + case QNODE: + return "qnode"; + case SNODE: + return "snode"; + case MNODE: + return "mnode"; + case BNODE: + return "bnode"; + default: + return "dnode"; + } +} + +const char *dmEventStr(EDndEvent ev) { + switch (ev) { + case DND_EVENT_START: + return "start"; + case DND_EVENT_STOP: + return "stop"; + case DND_EVENT_CHILD: + return "child"; + default: + return "UNKNOWN"; + } +} + +const char *dmProcStr(EDndProcType etype) { + switch (etype) { + case DND_PROC_SINGLE: + return "start"; + case DND_PROC_CHILD: + return "stop"; + case DND_PROC_PARENT: + return "child"; + case DND_PROC_TEST: + return "test"; + default: + return "UNKNOWN"; + } +} + +void *dmSetMgmtHandle(SArray *pArray, tmsg_t msgType, void *nodeMsgFp, bool needCheckVgId) { + SMgmtHandle handle = { + .msgType = msgType, + .msgFp = (NodeMsgFp)nodeMsgFp, + .needCheckVgId = needCheckVgId, + }; + + return taosArrayPush(pArray, &handle); +} + +void dmGetMonitorSystemInfo(SMonSysInfo *pInfo) { + taosGetCpuUsage(&pInfo->cpu_engine, &pInfo->cpu_system); + taosGetCpuCores(&pInfo->cpu_cores); + taosGetProcMemory(&pInfo->mem_engine); + taosGetSysMemory(&pInfo->mem_system); + pInfo->mem_total = tsTotalMemoryKB; + pInfo->disk_engine = 0; + pInfo->disk_used = tsDataSpace.size.used; + pInfo->disk_total = tsDataSpace.size.total; + taosGetCardInfoDelta(&pInfo->net_in, &pInfo->net_out); + taosGetProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk); +} diff --git a/source/dnode/mgmt/test/CMakeLists.txt b/source/dnode/mgmt/test/CMakeLists.txt index e1656ceb34d222fb13ef524b087349756d46d6ff..6b1919bf1862b5eeca9047de4731dae306ca275a 100644 --- a/source/dnode/mgmt/test/CMakeLists.txt +++ b/source/dnode/mgmt/test/CMakeLists.txt @@ -3,7 +3,7 @@ if(${BUILD_TEST}) add_subdirectory(qnode) add_subdirectory(bnode) add_subdirectory(snode) - add_subdirectory(mnode) + #add_subdirectory(mnode) add_subdirectory(vnode) add_subdirectory(sut) endif(${BUILD_TEST}) diff --git a/source/dnode/mgmt/test/bnode/dbnode.cpp b/source/dnode/mgmt/test/bnode/dbnode.cpp index 9016bf49eac82b41ab5c8663d75c812f2be18cf2..4cc2f2386fa84cc52338628d13e2b2b051f162bc 100644 --- a/source/dnode/mgmt/test/bnode/dbnode.cpp +++ b/source/dnode/mgmt/test/bnode/dbnode.cpp @@ -84,6 +84,7 @@ TEST_F(DndTestBnode, 01_Create_Bnode) { } TEST_F(DndTestBnode, 02_Drop_Bnode) { +#if 0 { SDDropBnodeReq dropReq = {0}; dropReq.dnodeId = 2; @@ -96,7 +97,7 @@ TEST_F(DndTestBnode, 02_Drop_Bnode) { ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, TSDB_CODE_INVALID_OPTION); } - +#endif { SDDropBnodeReq dropReq = {0}; dropReq.dnodeId = 1; diff --git a/source/dnode/mgmt/test/qnode/dqnode.cpp b/source/dnode/mgmt/test/qnode/dqnode.cpp index 8a0d97abb14e33054671f99bff3074effdc8af63..b610681b69d8d36f50aa112938a1b9f81c47993d 100644 --- a/source/dnode/mgmt/test/qnode/dqnode.cpp +++ b/source/dnode/mgmt/test/qnode/dqnode.cpp @@ -82,6 +82,7 @@ TEST_F(DndTestQnode, 01_Create_Qnode) { } TEST_F(DndTestQnode, 02_Drop_Qnode) { +#if 0 { SDDropQnodeReq dropReq = {0}; dropReq.dnodeId = 2; @@ -94,6 +95,7 @@ TEST_F(DndTestQnode, 02_Drop_Qnode) { ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, TSDB_CODE_INVALID_OPTION); } +#endif { SDDropQnodeReq dropReq = {0}; diff --git a/source/dnode/mgmt/test/snode/dsnode.cpp b/source/dnode/mgmt/test/snode/dsnode.cpp index a744240a1aab7d781912d13af08ce042b9d13add..507531308580a6a112a62e6ab0d838c85f8b984d 100644 --- a/source/dnode/mgmt/test/snode/dsnode.cpp +++ b/source/dnode/mgmt/test/snode/dsnode.cpp @@ -82,6 +82,7 @@ TEST_F(DndTestSnode, 01_Create_Snode) { } TEST_F(DndTestSnode, 01_Drop_Snode) { +#if 0 { SDDropSnodeReq dropReq = {0}; dropReq.dnodeId = 2; @@ -94,6 +95,7 @@ TEST_F(DndTestSnode, 01_Drop_Snode) { ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, TSDB_CODE_INVALID_OPTION); } +#endif { SDDropSnodeReq dropReq = {0}; diff --git a/source/dnode/mnode/impl/inc/mndInt.h b/source/dnode/mnode/impl/inc/mndInt.h index b96444bebcc7d9df5eff88d7dc878b9fa170be5f..75dda28a73ee048c2c0fc156727e7a65e708f756 100644 --- a/source/dnode/mnode/impl/inc/mndInt.h +++ b/source/dnode/mnode/impl/inc/mndInt.h @@ -87,13 +87,11 @@ typedef struct { typedef struct SMnode { int32_t selfId; int64_t clusterId; + TdThread thread; + bool stopped; int8_t replica; int8_t selfIndex; SReplica replicas[TSDB_MAX_REPLICA]; - tmr_h timer; - tmr_h transTimer; - tmr_h mqTimer; - tmr_h telemTimer; char *path; int64_t checkTime; SSdb *pSdb; diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index 690399f09995b484e1649f14ec25534d05ffa119..bc3dcfbe8c03dcf75ee15bd0af6db2c7d39192ea 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -56,82 +56,76 @@ static void *mndBuildTimerMsg(int32_t *pContLen) { return pReq; } -static void mndPullupTrans(void *param, void *tmrId) { - SMnode *pMnode = param; - if (mndIsMaster(pMnode)) { - int32_t contLen = 0; - void *pReq = mndBuildTimerMsg(&contLen); - SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRANS_TIMER, .pCont = pReq, .contLen = contLen}; - tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg); - } - - taosTmrReset(mndPullupTrans, tsTransPullupInterval * 1000, pMnode, pMnode->timer, &pMnode->transTimer); +static void mndPullupTrans(SMnode *pMnode) { + int32_t contLen = 0; + void *pReq = mndBuildTimerMsg(&contLen); + SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRANS_TIMER, .pCont = pReq, .contLen = contLen}; + tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg); } -static void mndCalMqRebalance(void *param, void *tmrId) { - SMnode *pMnode = param; - if (mndIsMaster(pMnode)) { - int32_t contLen = 0; - void *pReq = mndBuildTimerMsg(&contLen); - SRpcMsg rpcMsg = { - .msgType = TDMT_MND_MQ_TIMER, - .pCont = pReq, - .contLen = contLen, - }; - tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg); - } +static void mndCalMqRebalance(SMnode *pMnode) { + int32_t contLen = 0; + void *pReq = mndBuildTimerMsg(&contLen); + SRpcMsg rpcMsg = { + .msgType = TDMT_MND_MQ_TIMER, + .pCont = pReq, + .contLen = contLen, + }; + tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg); +} - taosTmrReset(mndCalMqRebalance, tsMqRebalanceInterval * 1000, pMnode, pMnode->timer, &pMnode->mqTimer); +static void mndPullupTelem(SMnode *pMnode) { + int32_t contLen = 0; + void *pReq = mndBuildTimerMsg(&contLen); + SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen}; + tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg); } -static void mndPullupTelem(void *param, void *tmrId) { +static void *mndThreadFp(void *param) { SMnode *pMnode = param; - if (mndIsMaster(pMnode)) { - int32_t contLen = 0; - void *pReq = mndBuildTimerMsg(&contLen); - SRpcMsg rpcMsg = {.msgType = TDMT_MND_TELEM_TIMER, .pCont = pReq, .contLen = contLen}; - tmsgPutToQueue(&pMnode->msgCb, READ_QUEUE, &rpcMsg); - } + int64_t lastTime = 0; + setThreadName("mnode-timer"); - taosTmrReset(mndPullupTelem, tsTelemInterval * 1000, pMnode, pMnode->timer, &pMnode->telemTimer); -} + while (1) { + lastTime++; + taosMsleep(100); + if (pMnode->stopped) break; + if (!mndIsMaster(pMnode)) continue; -static int32_t mndInitTimer(SMnode *pMnode) { - pMnode->timer = taosTmrInit(5000, 200, 3600000, "MND"); - if (pMnode->timer == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } + if (lastTime % (tsTransPullupInterval * 10) == 0) { + mndPullupTrans(pMnode); + } - if (taosTmrReset(mndPullupTrans, tsTransPullupInterval * 1000, pMnode, pMnode->timer, &pMnode->transTimer)) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } + if (lastTime % (tsMqRebalanceInterval * 10) == 0) { + mndCalMqRebalance(pMnode); + } - if (taosTmrReset(mndCalMqRebalance, tsMqRebalanceInterval * 1000, pMnode, pMnode->timer, &pMnode->mqTimer)) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + if (lastTime % (tsTelemInterval * 10) == 0) { + mndPullupTelem(pMnode); + } } - int32_t interval = tsTelemInterval < 10 ? tsTelemInterval : 10; - if (taosTmrReset(mndPullupTelem, interval * 1000, pMnode, pMnode->timer, &pMnode->telemTimer)) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; +} + +static int32_t mndInitTimer(SMnode *pMnode) { + TdThreadAttr thAttr; + taosThreadAttrInit(&thAttr); + taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); + if (taosThreadCreate(&pMnode->thread, &thAttr, mndThreadFp, pMnode) != 0) { + mError("failed to create timer thread since %s", strerror(errno)); return -1; } + taosThreadAttrDestroy(&thAttr); + tmsgReportStartup("mnode-timer", "initialized"); return 0; } static void mndCleanupTimer(SMnode *pMnode) { - if (pMnode->timer != NULL) { - taosTmrStop(pMnode->transTimer); - pMnode->transTimer = NULL; - taosTmrStop(pMnode->mqTimer); - pMnode->mqTimer = NULL; - taosTmrStop(pMnode->telemTimer); - pMnode->telemTimer = NULL; - taosTmrCleanUp(pMnode->timer); - pMnode->timer = NULL; + pMnode->stopped = true; + if (taosCheckPthreadValid(pMnode->thread)) { + taosThreadJoin(pMnode->thread, NULL); } } diff --git a/source/dnode/mnode/impl/test/CMakeLists.txt b/source/dnode/mnode/impl/test/CMakeLists.txt index feeebad67484b950cb74d78c6c05396f83364532..b6e3c8f3b4b4e32d150f5989a2168358b9344c3e 100644 --- a/source/dnode/mnode/impl/test/CMakeLists.txt +++ b/source/dnode/mnode/impl/test/CMakeLists.txt @@ -5,7 +5,7 @@ add_subdirectory(bnode) add_subdirectory(db) add_subdirectory(dnode) add_subdirectory(func) -add_subdirectory(mnode) +#add_subdirectory(mnode) add_subdirectory(profile) add_subdirectory(qnode) add_subdirectory(sdb) diff --git a/source/dnode/qnode/src/qnode.c b/source/dnode/qnode/src/qnode.c index 54b29f546cc888af80adaf1ec13c3b1a8c4d3efb..1259363f9468c2667536a17652010914dd4e2eac 100644 --- a/source/dnode/qnode/src/qnode.c +++ b/source/dnode/qnode/src/qnode.c @@ -26,10 +26,6 @@ SQnode *qndOpen(const SQnodeOpt *pOption) { return NULL; } - if (udfcOpen() != 0) { - qError("qnode can not open udfc"); - } - if (qWorkerInit(NODE_TYPE_QNODE, pQnode->qndId, NULL, (void **)&pQnode->pQuery, &pOption->msgCb)) { taosMemoryFreeClear(pQnode); return NULL; @@ -41,9 +37,6 @@ SQnode *qndOpen(const SQnodeOpt *pOption) { void qndClose(SQnode *pQnode) { qWorkerDestroy((void **)&pQnode->pQuery); - - udfcClose(); - taosMemoryFree(pQnode); } diff --git a/source/dnode/vnode/src/vnd/vnodeCfg.c b/source/dnode/vnode/src/vnd/vnodeCfg.c index 32866d74696648fbeff7c6ea87d2ef489eb06ff5..a66ecc493d7cbef19370349568398d084dc5bc27 100644 --- a/source/dnode/vnode/src/vnd/vnodeCfg.c +++ b/source/dnode/vnode/src/vnd/vnodeCfg.c @@ -114,24 +114,42 @@ int vnodeEncodeConfig(const void *pObj, SJson *pJson) { int vnodeDecodeConfig(const SJson *pJson, void *pObj) { SVnodeCfg *pCfg = (SVnodeCfg *)pObj; - if (tjsonGetNumberValue(pJson, "vgId", pCfg->vgId) < 0) return -1; + int32_t code; + tjsonGetNumberValue(pJson, "vgId", pCfg->vgId, code); + if(code < 0) return -1; if (tjsonGetStringValue(pJson, "dbname", pCfg->dbname) < 0) return -1; - if (tjsonGetNumberValue(pJson, "dbId", pCfg->dbId) < 0) return -1; - if (tjsonGetNumberValue(pJson, "szPage", pCfg->szPage) < 0) return -1; - if (tjsonGetNumberValue(pJson, "szCache", pCfg->szCache) < 0) return -1; - if (tjsonGetNumberValue(pJson, "szBuf", pCfg->szBuf) < 0) return -1; - if (tjsonGetNumberValue(pJson, "isHeap", pCfg->isHeap) < 0) return -1; - if (tjsonGetNumberValue(pJson, "isWeak", pCfg->isWeak) < 0) return -1; - if (tjsonGetNumberValue(pJson, "precision", pCfg->tsdbCfg.precision) < 0) return -1; - if (tjsonGetNumberValue(pJson, "update", pCfg->tsdbCfg.update) < 0) return -1; - if (tjsonGetNumberValue(pJson, "compression", pCfg->tsdbCfg.compression) < 0) return -1; - if (tjsonGetNumberValue(pJson, "slLevel", pCfg->tsdbCfg.slLevel) < 0) return -1; - if (tjsonGetNumberValue(pJson, "daysPerFile", pCfg->tsdbCfg.days) < 0) return -1; - if (tjsonGetNumberValue(pJson, "minRows", pCfg->tsdbCfg.minRows) < 0) return -1; - if (tjsonGetNumberValue(pJson, "maxRows", pCfg->tsdbCfg.maxRows) < 0) return -1; - if (tjsonGetNumberValue(pJson, "keep0", pCfg->tsdbCfg.keep0) < 0) return -1; - if (tjsonGetNumberValue(pJson, "keep1", pCfg->tsdbCfg.keep1) < 0) return -1; - if (tjsonGetNumberValue(pJson, "keep2", pCfg->tsdbCfg.keep2) < 0) return -1; + tjsonGetNumberValue(pJson, "dbId", pCfg->dbId, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "szPage", pCfg->szPage, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "szCache", pCfg->szCache, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "szBuf", pCfg->szBuf, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "isHeap", pCfg->isHeap, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "isWeak", pCfg->isWeak, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "precision", pCfg->tsdbCfg.precision, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "update", pCfg->tsdbCfg.update, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "compression", pCfg->tsdbCfg.compression, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "slLevel", pCfg->tsdbCfg.slLevel, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "daysPerFile", pCfg->tsdbCfg.days, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "minRows", pCfg->tsdbCfg.minRows, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "maxRows", pCfg->tsdbCfg.maxRows, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "keep0", pCfg->tsdbCfg.keep0, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "keep1", pCfg->tsdbCfg.keep1, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "keep2", pCfg->tsdbCfg.keep2, code); + if(code < 0) return -1; SJson *pNodeRetentions = tjsonGetObjectItem(pJson, "retentions"); int32_t nRetention = tjsonGetArraySize(pNodeRetentions); if (nRetention > TSDB_RETENTION_MAX) { @@ -140,24 +158,36 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) { for (int32_t i = 0; i < nRetention; ++i) { SJson *pNodeRetention = tjsonGetArrayItem(pNodeRetentions, i); ASSERT(pNodeRetention != NULL); - tjsonGetNumberValue(pNodeRetention, "freq", (pCfg->tsdbCfg.retentions)[i].freq); - tjsonGetNumberValue(pNodeRetention, "freqUnit", (pCfg->tsdbCfg.retentions)[i].freqUnit); - tjsonGetNumberValue(pNodeRetention, "keep", (pCfg->tsdbCfg.retentions)[i].keep); - tjsonGetNumberValue(pNodeRetention, "keepUnit", (pCfg->tsdbCfg.retentions)[i].keepUnit); + tjsonGetNumberValue(pNodeRetention, "freq", (pCfg->tsdbCfg.retentions)[i].freq, code); + tjsonGetNumberValue(pNodeRetention, "freqUnit", (pCfg->tsdbCfg.retentions)[i].freqUnit, code); + tjsonGetNumberValue(pNodeRetention, "keep", (pCfg->tsdbCfg.retentions)[i].keep, code); + tjsonGetNumberValue(pNodeRetention, "keepUnit", (pCfg->tsdbCfg.retentions)[i].keepUnit, code); } - if (tjsonGetNumberValue(pJson, "wal.vgId", pCfg->walCfg.vgId) < 0) return -1; - if (tjsonGetNumberValue(pJson, "wal.fsyncPeriod", pCfg->walCfg.fsyncPeriod) < 0) return -1; - if (tjsonGetNumberValue(pJson, "wal.retentionPeriod", pCfg->walCfg.retentionPeriod) < 0) return -1; - if (tjsonGetNumberValue(pJson, "wal.rollPeriod", pCfg->walCfg.rollPeriod) < 0) return -1; - if (tjsonGetNumberValue(pJson, "wal.retentionSize", pCfg->walCfg.retentionSize) < 0) return -1; - if (tjsonGetNumberValue(pJson, "wal.segSize", pCfg->walCfg.segSize) < 0) return -1; - if (tjsonGetNumberValue(pJson, "wal.level", pCfg->walCfg.level) < 0) return -1; - if (tjsonGetNumberValue(pJson, "hashBegin", pCfg->hashBegin) < 0) return -1; - if (tjsonGetNumberValue(pJson, "hashEnd", pCfg->hashEnd) < 0) return -1; - if (tjsonGetNumberValue(pJson, "hashMethod", pCfg->hashMethod) < 0) return -1; - - if (tjsonGetNumberValue(pJson, "syncCfg.replicaNum", pCfg->syncCfg.replicaNum) < 0) return -1; - if (tjsonGetNumberValue(pJson, "syncCfg.myIndex", pCfg->syncCfg.myIndex) < 0) return -1; + tjsonGetNumberValue(pJson, "wal.vgId", pCfg->walCfg.vgId, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "wal.fsyncPeriod", pCfg->walCfg.fsyncPeriod, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "wal.retentionPeriod", pCfg->walCfg.retentionPeriod, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "wal.rollPeriod", pCfg->walCfg.rollPeriod, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "wal.retentionSize", pCfg->walCfg.retentionSize, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "wal.segSize", pCfg->walCfg.segSize, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "wal.level", pCfg->walCfg.level, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "hashBegin", pCfg->hashBegin, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "hashEnd", pCfg->hashEnd, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "hashMethod", pCfg->hashMethod, code); + if(code < 0) return -1; + + tjsonGetNumberValue(pJson, "syncCfg.replicaNum", pCfg->syncCfg.replicaNum, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "syncCfg.myIndex", pCfg->syncCfg.myIndex, code); + if(code < 0) return -1; SJson *pNodeInfoArr = tjsonGetObjectItem(pJson, "syncCfg.nodeInfo"); int arraySize = tjsonGetArraySize(pNodeInfoArr); @@ -166,7 +196,7 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) { for (int i = 0; i < arraySize; ++i) { SJson *pNodeInfo = tjsonGetArrayItem(pNodeInfoArr, i); assert(pNodeInfo != NULL); - tjsonGetNumberValue(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort); + tjsonGetNumberValue(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort, code); tjsonGetStringValue(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn); } diff --git a/source/dnode/vnode/src/vnd/vnodeCommit.c b/source/dnode/vnode/src/vnd/vnodeCommit.c index 6d8bcb35c84a683b627b4a85dfc5e216c08b76e9..e7bee3342a39813e2825c9b9f29d201b6e68ca6e 100644 --- a/source/dnode/vnode/src/vnd/vnodeCommit.c +++ b/source/dnode/vnode/src/vnd/vnodeCommit.c @@ -310,8 +310,11 @@ static int vnodeEncodeState(const void *pObj, SJson *pJson) { static int vnodeDecodeState(const SJson *pJson, void *pObj) { SVState *pState = (SVState *)pObj; - if (tjsonGetNumberValue(pJson, "commit version", pState->committed) < 0) return -1; - if (tjsonGetNumberValue(pJson, "applied version", pState->applied) < 0) return -1; + int32_t code; + tjsonGetNumberValue(pJson, "commit version", pState->committed, code); + if(code < 0) return -1; + tjsonGetNumberValue(pJson, "applied version", pState->applied, code); + if(code < 0) return -1; return 0; } diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index f79d3c8450cac0c17c09f44007cfa8cc44fbd638..dd0bcbff0e911980e0d8211707b8e2cf1d8bc71a 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -564,7 +564,7 @@ typedef struct SStateWindowOperatorInfo { SAggSupporter aggSup; SGroupResInfo groupResInfo; SWindowRowsSup winSup; - int32_t colIndex; // start row index + SColumn stateCol; // start row index bool hasKey; SStateKeys stateKey; int32_t tsSlotId; // primary timestamp column slot id @@ -636,7 +636,7 @@ int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t int32_t initAggInfo(SOptrBasicInfo* pBasicInfo, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, size_t keyBufSize, const char* pkey); void initResultSizeInfo(SOperatorInfo* pOperator, int32_t numOfRows); -void doBuildResultDatablock(SExecTaskInfo *taskInfo, SOptrBasicInfo *pbInfo, SGroupResInfo* pGroupResInfo, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf); +void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo, SDiskbasedBuf* pBuf); void finalizeMultiTupleQueryResult(int32_t numOfOutput, SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset); void doApplyFunctions(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, STimeWindow* pWin, SColumnInfoData* pTimeWindowData, int32_t offset, @@ -659,6 +659,7 @@ void cleanupAggSup(SAggSupporter* pAggSup); void destroyBasicOperatorInfo(void* param, int32_t numOfOutput); void appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHandle); SInterval extractIntervalInfo(const STableScanPhysiNode* pTableScanNode); +SColumn extractColumnFromColumnNode(SColumnNode* pColNode); SSDataBlock* getSortedBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, int32_t capacity, SArray* pColMatchInfo); SSDataBlock* loadNextDataBlock(void* param); @@ -712,7 +713,7 @@ SOperatorInfo* createFillOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExp SInterval* pInterval, STimeWindow* pWindow, SSDataBlock* pResBlock, int32_t fillType, SNodeListNode* fillVal, bool multigroupResult, SExecTaskInfo* pTaskInfo); SOperatorInfo* createStatewindowOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfCols, - SSDataBlock* pResBlock, STimeWindowAggSupp *pTwAggSupp, int32_t tsSlotId, SExecTaskInfo* pTaskInfo); + SSDataBlock* pResBlock, STimeWindowAggSupp *pTwAggSupp, int32_t tsSlotId, SColumn* pStateKeyCol, SExecTaskInfo* pTaskInfo); SOperatorInfo* createPartitionOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, SArray* pGroupColList, SExecTaskInfo* pTaskInfo, diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index df446def5ea3d665986a7575f7b723f3f96ea09c..f04ab24fe7213d268ad3d666da418983d386fec7 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -155,9 +155,8 @@ SOperatorFpSet createOperatorFpSet(__optr_open_fn_t openFn, __optr_fn_t nextFn, void operatorDummyCloseFn(void* param, int32_t numOfCols) {} -static int32_t doCopyToSDataBlock(SExecTaskInfo *taskInfo, SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, - SGroupResInfo* pGroupResInfo, int32_t orderType, int32_t* rowCellOffset, - SqlFunctionCtx* pCtx); +static int32_t doCopyToSDataBlock(SExecTaskInfo* taskInfo, SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, + int32_t orderType, int32_t* rowCellOffset, SqlFunctionCtx* pCtx, int32_t numOfExprs); static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size); static void setResultBufSize(STaskAttr* pQueryAttr, SResultInfo* pResultInfo); @@ -898,6 +897,7 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc scalarCalculate(pExpr[k].pExpr->_optrRoot.pRootNode, pBlockList, &dest); int32_t startOffset = createNewColModel ? 0 : pResult->info.rows; + colInfoDataEnsureCapacity(pResColData, startOffset, pResult->info.capacity); colDataMergeCol(pResColData, startOffset, &pResult->info.capacity, &idata, dest.numOfRows); numOfRows = dest.numOfRows; @@ -937,6 +937,7 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc } int32_t startOffset = createNewColModel ? 0 : pResult->info.rows; + colInfoDataEnsureCapacity(pResColData, startOffset, pResult->info.capacity); colDataMergeCol(pResColData, startOffset, &pResult->info.capacity, &idata, dest.numOfRows); numOfRows = dest.numOfRows; @@ -2214,7 +2215,7 @@ void setExecutionContext(int32_t numOfOutput, uint64_t groupId, SExecTaskInfo* p * @param result */ int32_t doCopyToSDataBlock(SExecTaskInfo* taskInfo, SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, - int32_t orderType, int32_t* rowCellOffset, SqlFunctionCtx* pCtx) { + int32_t orderType, int32_t* rowCellOffset, SqlFunctionCtx* pCtx, int32_t numOfExprs) { int32_t numOfRows = getNumOfTotalRes(pGroupResInfo); int32_t numOfResult = pBlock->info.rows; // there are already exists result rows @@ -2248,13 +2249,12 @@ int32_t doCopyToSDataBlock(SExecTaskInfo* taskInfo, SSDataBlock* pBlock, SExprIn pGroupResInfo->index += 1; - for (int32_t j = 0; j < pBlock->info.numOfCols; ++j) { + for (int32_t j = 0; j < numOfExprs; ++j) { int32_t slotId = pExprInfo[j].base.resSchema.slotId; pCtx[j].resultInfo = getResultCell(pRow, j, rowCellOffset); if (pCtx[j].fpSet.finalize) { - int32_t code = TSDB_CODE_SUCCESS; - code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock); + int32_t code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock); if (TAOS_FAILED(code)) { qError("%s build result data block error, code %s", GET_TASKID(taskInfo), tstrerror(code)); taskInfo->code = code; @@ -2286,10 +2286,13 @@ int32_t doCopyToSDataBlock(SExecTaskInfo* taskInfo, SSDataBlock* pBlock, SExprIn return 0; } -void doBuildResultDatablock(SExecTaskInfo *taskInfo, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo, SExprInfo* pExprInfo, - SDiskbasedBuf* pBuf) { +void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo, SDiskbasedBuf* pBuf) { assert(pGroupResInfo->currentGroup <= pGroupResInfo->totalGroup); + SExprInfo* pExprInfo = pOperator->pExpr; + int32_t numOfExprs = pOperator->numOfExprs; + SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo; + int32_t* rowCellOffset = pbInfo->rowCellInfoOffset; SSDataBlock* pBlock = pbInfo->pRes; SqlFunctionCtx* pCtx = pbInfo->pCtx; @@ -2300,7 +2303,7 @@ void doBuildResultDatablock(SExecTaskInfo *taskInfo, SOptrBasicInfo* pbInfo, SGr } int32_t orderType = TSDB_ORDER_ASC; - doCopyToSDataBlock(taskInfo, pBlock, pExprInfo, pBuf, pGroupResInfo, orderType, rowCellOffset, pCtx); + doCopyToSDataBlock(pTaskInfo, pBlock, pExprInfo, pBuf, pGroupResInfo, orderType, rowCellOffset, pCtx, numOfExprs); // add condition (pBlock->info.rows >= 1) just to runtime happy blockDataUpdateTsWindow(pBlock); @@ -3803,7 +3806,7 @@ static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator) { } blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pTaskInfo, pInfo, &pAggInfo->groupResInfo, pOperator->pExpr, pAggInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf); if (pInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pAggInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); } @@ -4963,7 +4966,9 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc); int32_t tsSlotId = ((SColumnNode*)pStateNode->window.pTspk)->slotId; - pOptr = createStatewindowOperatorInfo(ops[0], pExprInfo, num, pResBlock, &as, tsSlotId, pTaskInfo); + SColumnNode* pColNode = (SColumnNode*)((STargetNode*)pStateNode->pStateKey)->pExpr; + SColumn col = extractColumnFromColumnNode(pColNode); + pOptr = createStatewindowOperatorInfo(ops[0], pExprInfo, num, pResBlock, &as, tsSlotId, &col, pTaskInfo); } else if (QUERY_NODE_PHYSICAL_PLAN_JOIN == type) { SJoinPhysiNode* pJoinNode = (SJoinPhysiNode*)pPhyNode; SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc); @@ -5028,6 +5033,17 @@ int32_t initQueryTableDataCond(SQueryTableDataCond* pCond, const STableScanPhysi return TSDB_CODE_SUCCESS; } +SColumn extractColumnFromColumnNode(SColumnNode* pColNode) { + SColumn c = {0}; + c.slotId = pColNode->slotId; + c.colId = pColNode->colId; + c.type = pColNode->node.resType.type; + c.bytes = pColNode->node.resType.bytes; + c.scale = pColNode->node.resType.scale; + c.precision = pColNode->node.resType.precision; + return c; +} + SArray* extractColumnInfo(SNodeList* pNodeList) { size_t numOfCols = LIST_LENGTH(pNodeList); SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn)); @@ -5042,15 +5058,7 @@ SArray* extractColumnInfo(SNodeList* pNodeList) { if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) { SColumnNode* pColNode = (SColumnNode*)pNode->pExpr; - // todo extract method - SColumn c = {0}; - c.slotId = pColNode->slotId; - c.colId = pColNode->colId; - c.type = pColNode->node.resType.type; - c.bytes = pColNode->node.resType.bytes; - c.scale = pColNode->node.resType.scale; - c.precision = pColNode->node.resType.precision; - + SColumn c = extractColumnFromColumnNode(pColNode); taosArrayPush(pList, &c); } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) { SValueNode* pValNode = (SValueNode*)pNode->pExpr; diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index ac6f0cf8813def4c989202dce9b3bde14e5f1524..d8ccac8cea12bf210b962a58fa1274153ea8510e 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -268,7 +268,7 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator) { SSDataBlock* pRes = pInfo->binfo.pRes; if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pTaskInfo, &pInfo->binfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { pOperator->status = OP_EXEC_DONE; } @@ -317,7 +317,7 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator) { initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, false); while(1) { - doBuildResultDatablock(pTaskInfo, &pInfo->binfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); doFilter(pInfo->pCondition, pRes); bool hasRemain = hasRemainDataInCurrentGroup(&pInfo->groupResInfo); diff --git a/source/libs/executor/src/timewindowoperator.c b/source/libs/executor/src/timewindowoperator.c index 332a116f762fb26f3892c884a83e9d8ae264015f..10dc482462f84a4dcdd7c53cb4bd164baa74f6af 100644 --- a/source/libs/executor/src/timewindowoperator.c +++ b/source/libs/executor/src/timewindowoperator.c @@ -806,10 +806,22 @@ static int32_t doOpenIntervalAgg(SOperatorInfo* pOperator) { return TSDB_CODE_SUCCESS; } +static bool compareVal(const char* v, const SStateKeys* pKey) { + if (IS_VAR_DATA_TYPE(pKey->type)) { + if (varDataLen(v) != varDataLen(pKey->pData)) { + return false; + } else { + return strncmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0; + } + } else { + return memcmp(pKey->pData, v, pKey->bytes) == 0; + } +} + static void doStateWindowAggImpl(SOperatorInfo* pOperator, SStateWindowOperatorInfo* pInfo, SSDataBlock* pBlock) { SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo; - SColumnInfoData* pStateColInfoData = taosArrayGet(pBlock->pDataBlock, pInfo->colIndex); + SColumnInfoData* pStateColInfoData = taosArrayGet(pBlock->pDataBlock, pInfo->stateCol.slotId); int64_t gid = pBlock->info.groupId; bool masterScan = true; @@ -822,20 +834,28 @@ static void doStateWindowAggImpl(SOperatorInfo* pOperator, SStateWindowOperatorI SWindowRowsSup* pRowSup = &pInfo->winSup; pRowSup->numOfRows = 0; + struct SColumnDataAgg* pAgg = NULL; for (int32_t j = 0; j < pBlock->info.rows; ++j) { - if (colDataIsNull(pStateColInfoData, pBlock->info.rows, j, pBlock->pBlockAgg[pInfo->colIndex])) { + pAgg = (pBlock->pBlockAgg != NULL)? pBlock->pBlockAgg[pInfo->stateCol.slotId]: NULL; + if (colDataIsNull(pStateColInfoData, pBlock->info.rows, j, pAgg)) { continue; } char* val = colDataGetData(pStateColInfoData, j); if (!pInfo->hasKey) { - memcpy(pInfo->stateKey.pData, val, bytes); + // todo extract method + if (IS_VAR_DATA_TYPE(pInfo->stateKey.type)) { + varDataCopy(pInfo->stateKey.pData, val); + } else { + memcpy(pInfo->stateKey.pData, val, bytes); + } + pInfo->hasKey = true; doKeepNewWindowStartInfo(pRowSup, tsList, j); doKeepTuple(pRowSup, tsList[j]); - } else if (memcmp(pInfo->stateKey.pData, val, bytes) == 0) { + } else if (compareVal(val, &pInfo->stateKey)) { doKeepTuple(pRowSup, tsList[j]); if (j == 0 && pRowSup->startRowIndex != 0) { pRowSup->startRowIndex = 0; @@ -861,6 +881,13 @@ static void doStateWindowAggImpl(SOperatorInfo* pOperator, SStateWindowOperatorI // here we start a new session window doKeepNewWindowStartInfo(pRowSup, tsList, j); doKeepTuple(pRowSup, tsList[j]); + + // todo extract method + if (IS_VAR_DATA_TYPE(pInfo->stateKey.type)) { + varDataCopy(pInfo->stateKey.pData, val); + } else { + memcpy(pInfo->stateKey.pData, val, bytes); + } } } @@ -888,7 +915,7 @@ static SSDataBlock* doStateWindowAgg(SOperatorInfo* pOperator) { SOptrBasicInfo* pBInfo = &pInfo->binfo; if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pTaskInfo, pBInfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, pBInfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); return NULL; @@ -921,7 +948,7 @@ static SSDataBlock* doStateWindowAgg(SOperatorInfo* pOperator) { initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, true); blockDataEnsureCapacity(pBInfo->pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pTaskInfo, pBInfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, pBInfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); } @@ -948,7 +975,7 @@ static SSDataBlock* doBuildIntervalResult(SOperatorInfo* pOperator) { } blockDataEnsureCapacity(pBlock, pOperator->resultInfo.capacity); - doBuildResultDatablock(pTaskInfo, &pInfo->binfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pBlock->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); @@ -1012,7 +1039,7 @@ static SSDataBlock* doStreamIntervalAgg(SOperatorInfo* pOperator) { } if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pOperator->pTaskInfo, &pInfo->binfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { pOperator->status = OP_EXEC_DONE; } @@ -1053,7 +1080,7 @@ static SSDataBlock* doStreamIntervalAgg(SOperatorInfo* pOperator) { initMultiResInfoFromArrayList(&pInfo->groupResInfo, pUpdated); blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pOperator->pTaskInfo, &pInfo->binfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); // TODO: remove for stream /*ASSERT(pInfo->binfo.pRes->info.rows > 0);*/ @@ -1283,7 +1310,7 @@ static SSDataBlock* doSessionWindowAgg(SOperatorInfo* pOperator) { SOptrBasicInfo* pBInfo = &pInfo->binfo; if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pOperator->pTaskInfo, pBInfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, pBInfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); return NULL; @@ -1316,7 +1343,7 @@ static SSDataBlock* doSessionWindowAgg(SOperatorInfo* pOperator) { initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, true); blockDataEnsureCapacity(pBInfo->pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pOperator->pTaskInfo, pBInfo, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf); + doBuildResultDatablock(pOperator, pBInfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); } @@ -1406,14 +1433,21 @@ _error: SOperatorInfo* createStatewindowOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfCols, SSDataBlock* pResBlock, STimeWindowAggSupp* pTwAggSup, int32_t tsSlotId, - SExecTaskInfo* pTaskInfo) { + SColumn* pStateKeyCol, SExecTaskInfo* pTaskInfo) { SStateWindowOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SStateWindowOperatorInfo)); SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); if (pInfo == NULL || pOperator == NULL) { goto _error; } - pInfo->colIndex = -1; + pInfo->stateCol = *pStateKeyCol; + pInfo->stateKey.type = pInfo->stateCol.type; + pInfo->stateKey.bytes = pInfo->stateCol.bytes; + pInfo->stateKey.pData = taosMemoryCalloc(1, pInfo->stateCol.bytes); + if (pInfo->stateKey.pData == NULL) { + goto _error; + } + size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES; initResultSizeInfo(pOperator, 4096); @@ -1423,15 +1457,15 @@ SOperatorInfo* createStatewindowOperatorInfo(SOperatorInfo* downstream, SExprInf pInfo->twAggSup = *pTwAggSup; initExecTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pTaskInfo->window); - pInfo->tsSlotId = tsSlotId; - pOperator->name = "StateWindowOperator"; + pInfo->tsSlotId = tsSlotId; + pOperator->name = "StateWindowOperator"; pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW; - pOperator->blocking = true; - pOperator->status = OP_NOT_OPENED; - pOperator->pExpr = pExpr; + pOperator->blocking = true; + pOperator->status = OP_NOT_OPENED; + pOperator->pExpr = pExpr; pOperator->numOfExprs = numOfCols; - pOperator->pTaskInfo = pTaskInfo; - pOperator->info = pInfo; + pOperator->pTaskInfo = pTaskInfo; + pOperator->info = pInfo; pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doStateWindowAgg, NULL, NULL, destroyStateWindowOperatorInfo, aggEncodeResultRow, aggDecodeResultRow, NULL); diff --git a/source/libs/function/inc/builtinsimpl.h b/source/libs/function/inc/builtinsimpl.h index ea303eeadf745aa56f029771c3bfce42f4d0ea63..807234a1b15a7a5ed6040112e12eec6e7a6503bc 100644 --- a/source/libs/function/inc/builtinsimpl.h +++ b/source/libs/function/inc/builtinsimpl.h @@ -102,6 +102,11 @@ bool getMavgFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool mavgFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t mavgFunction(SqlFunctionCtx* pCtx); +bool getSampleFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); +bool sampleFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); +int32_t sampleFunction(SqlFunctionCtx* pCtx); +int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); + bool getSelectivityFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv); #ifdef __cplusplus diff --git a/source/libs/function/inc/fnLog.h b/source/libs/function/inc/fnLog.h index f57294890794927e840a1a749f7e5adc5bbf9164..d85dd024331d0b2ec7374079cad9af344368a3c4 100644 --- a/source/libs/function/inc/fnLog.h +++ b/source/libs/function/inc/fnLog.h @@ -10,12 +10,12 @@ extern "C" { #endif -#define fnFatal(...) { if (fnDebugFlag & DEBUG_FATAL) { taosPrintLog("FN FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} -#define fnError(...) { if (fnDebugFlag & DEBUG_ERROR) { taosPrintLog("FN ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} -#define fnWarn(...) { if (fnDebugFlag & DEBUG_WARN) { taosPrintLog("FN WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} -#define fnInfo(...) { if (fnDebugFlag & DEBUG_INFO) { taosPrintLog("FN ", DEBUG_INFO, 255, __VA_ARGS__); }} -#define fnDebug(...) { if (fnDebugFlag & DEBUG_DEBUG) { taosPrintLog("FN ", DEBUG_DEBUG, dDebugFlag, __VA_ARGS__); }} -#define fnTrace(...) { if (fnDebugFlag & DEBUG_TRACE) { taosPrintLog("FN ", DEBUG_TRACE, dDebugFlag, __VA_ARGS__); }} +#define fnFatal(...) { if (fnDebugFlag & DEBUG_FATAL) { taosPrintLog("FN FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} +#define fnError(...) { if (fnDebugFlag & DEBUG_ERROR) { taosPrintLog("FN ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} +#define fnWarn(...) { if (fnDebugFlag & DEBUG_WARN) { taosPrintLog("FN WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} +#define fnInfo(...) { if (fnDebugFlag & DEBUG_INFO) { taosPrintLog("FN ", DEBUG_INFO, 255, __VA_ARGS__); }} +#define fnDebug(...) { if (fnDebugFlag & DEBUG_DEBUG) { taosPrintLog("FN ", DEBUG_DEBUG, dDebugFlag, __VA_ARGS__); }} +#define fnTrace(...) { if (fnDebugFlag & DEBUG_TRACE) { taosPrintLog("FN ", DEBUG_TRACE, dDebugFlag, __VA_ARGS__); }} #ifdef __cplusplus } diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index f5d1a5bf82b8e862892200fe7098df7d29a5996b..fc930083128c8bfe79f69e21f3c6737547ea67f1 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -360,6 +360,32 @@ static int32_t translateMavg(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { return TSDB_CODE_SUCCESS; } +static int32_t translateSample(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { + if (2 != LIST_LENGTH(pFunc->pParameterList)) { + return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName); + } + + SNode* pPara = nodesListGetNode(pFunc->pParameterList, 0); + if (QUERY_NODE_COLUMN != nodeType(pPara)) { + return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + "The input parameter of SAMPLE function can only be column"); + } + + uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type; + if (!IS_INTEGER_TYPE(paraType)) { + return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName); + } + + SExprNode* pCol = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0); + uint8_t colType = pCol->resType.type; + if (IS_VAR_DATA_TYPE(colType)) { + pFunc->node.resType = (SDataType){.bytes = pCol->resType.bytes, .type = colType}; + } else { + pFunc->node.resType = (SDataType){.bytes = tDataTypes[colType].bytes, .type = colType}; + } + return TSDB_CODE_SUCCESS; +} + static int32_t translateLastRow(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { // todo return TSDB_CODE_SUCCESS; @@ -814,6 +840,16 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = { .processFunc = mavgFunction, .finalizeFunc = NULL }, + { + .name = "sample", + .type = FUNCTION_TYPE_SAMPLE, + .classification = FUNC_MGT_NONSTANDARD_SQL_FUNC | FUNC_MGT_TIMELINE_FUNC, + .translateFunc = translateSample, + .getEnvFunc = getSampleFuncEnv, + .initFunc = sampleFunctionSetup, + .processFunc = sampleFunction, + .finalizeFunc = NULL + }, { .name = "abs", .type = FUNCTION_TYPE_ABS, diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 2ec050d82d70a26e7a02b5a6571d496e6e43dc3c..af86eb4e90d2fab886b2c9c7f49d5df64d6176ca 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -23,6 +23,7 @@ #define HISTOGRAM_MAX_BINS_NUM 1000 #define MAVG_MAX_POINTS_NUM 1000 +#define SAMPLE_MAX_POINTS_NUM 1000 typedef struct SSumRes { union { @@ -150,6 +151,16 @@ typedef struct SMavgInfo { double points[]; } SMavgInfo; +typedef struct SSampleInfo { + int32_t samples; + int32_t totalPoints; + int32_t numSampled; + uint8_t colType; + int16_t colBytes; + char *data; + int64_t *timestamp; +} SSampleInfo; + #define SET_VAL(_info, numOfElem, res) \ do { \ if ((numOfElem) <= 0) { \ @@ -3034,3 +3045,99 @@ int32_t mavgFunction(SqlFunctionCtx* pCtx) { return numOfElems; } + +bool getSampleFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { + SColumnNode* pCol = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0); + SValueNode* pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1); + int32_t numOfSamples = pVal->datum.i; + pEnv->calcMemSize = sizeof(SSampleInfo) + numOfSamples * (pCol->node.resType.bytes + sizeof(int64_t)); + return true; +} + +bool sampleFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo *pResultInfo) { + if (!functionSetup(pCtx, pResultInfo)) { + return false; + } + + taosSeedRand(taosSafeRand()); + + SSampleInfo *pInfo = GET_ROWCELL_INTERBUF(pResultInfo); + pInfo->samples = pCtx->param[1].param.i; + pInfo->totalPoints = 0; + pInfo->numSampled = 0; + pInfo->colType = pCtx->resDataInfo.type; + pInfo->colBytes = pCtx->resDataInfo.bytes; + if (pInfo->samples < 1 || pInfo->samples > SAMPLE_MAX_POINTS_NUM) { + return false; + } + pInfo->data = (char *)pInfo + sizeof(SSampleInfo); + pInfo->timestamp = (int64_t *)((char *)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes); + + return true; +} + +static void sampleAssignResult(SSampleInfo* pInfo, char *data, TSKEY ts, int32_t index) { + assignVal(pInfo->data + index * pInfo->colBytes, data, pInfo->colBytes, pInfo->colType); + *(pInfo->timestamp + index) = ts; +} + +static void doReservoirSample(SSampleInfo* pInfo, char *data, TSKEY ts, int32_t index) { + pInfo->totalPoints++; + if (pInfo->numSampled < pInfo->samples) { + sampleAssignResult(pInfo, data, ts, pInfo->numSampled); + pInfo->numSampled++; + } else { + int32_t j = taosRand() % (pInfo->totalPoints); + if (j < pInfo->samples) { + sampleAssignResult(pInfo, data, ts, j); + } + } +} + +int32_t sampleFunction(SqlFunctionCtx* pCtx) { + SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); + SSampleInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); + + SInputColumnInfoData* pInput = &pCtx->input; + TSKEY* tsList = (int64_t*)pInput->pPTS->pData; + + SColumnInfoData* pInputCol = pInput->pData[0]; + SColumnInfoData* pTsOutput = pCtx->pTsOutput; + SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput; + + int32_t startOffset = pCtx->offset; + for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) { + if (colDataIsNull_f(pInputCol->nullbitmap, i)) { + //colDataAppendNULL(pOutput, i); + continue; + } + + char* data = colDataGetData(pInputCol, i); + doReservoirSample(pInfo, data, tsList[i], i); + } + + for (int32_t i = 0; i < pInfo->numSampled; ++i) { + int32_t pos = startOffset + i; + colDataAppend(pOutput, pos, pInfo->data + i * pInfo->colBytes, false); + //TODO: handle ts output + } + + return pInfo->numSampled; +} + +//int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { +// SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); +// SSampleInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); +// int32_t slotId = pCtx->pExpr->base.resSchema.slotId; +// SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); +// +// //int32_t currentRow = pBlock->info.rows; +// pResInfo->numOfRes = pInfo->numSampled; +// +// for (int32_t i = 0; i < pInfo->numSampled; ++i) { +// colDataAppend(pCol, i, pInfo->data + i * pInfo->colBytes, false); +// //TODO: handle ts output +// } +// +// return pResInfo->numOfRes; +//} diff --git a/source/libs/function/src/tudf.c b/source/libs/function/src/tudf.c index 8e96a2a0630ce86c05b3c87a83e9166c089ba1cf..638a0701928294fd5643ced77da30598ec032627 100644 --- a/source/libs/function/src/tudf.c +++ b/source/libs/function/src/tudf.c @@ -34,6 +34,9 @@ typedef struct SUdfdData { uv_thread_t thread; uv_barrier_t barrier; uv_process_t process; +#ifdef WINDOWS + HANDLE jobHandle; +#endif int spawnErr; uv_pipe_t ctrlPipe; uv_async_t stopAsync; @@ -104,6 +107,24 @@ static int32_t udfSpawnUdfd(SUdfdData* pData) { int err = uv_spawn(&pData->loop, &pData->process, &options); pData->process.data = (void*)pData; +#ifdef WINDOWS + // End udfd.exe by Job. + if (pData->jobHandle != NULL) CloseHandle(pData->jobHandle); + pData->jobHandle = CreateJobObject(NULL, NULL); + bool add_job_ok = AssignProcessToJobObject(pData->jobHandle, pData->process.process_handle); + if (!add_job_ok) { + fnError("Assign udfd to job failed."); + } else { + JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info; + memset(&limit_info, 0x0, sizeof(limit_info)); + limit_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + bool set_auto_kill_ok = SetInformationJobObject(pData->jobHandle, JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info)); + if (!set_auto_kill_ok) { + fnError("Set job auto kill udfd failed."); + } + } +#endif + if (err != 0) { fnError("can not spawn udfd. path: %s, error: %s", path, uv_strerror(err)); } @@ -145,7 +166,7 @@ int32_t udfStartUdfd(int32_t startDnodeId) { } SUdfdData *pData = &udfdGlobal; if (pData->startCalled) { - fnInfo("dnode-mgmt start udfd already called"); + fnInfo("dnode start udfd already called"); return 0; } pData->startCalled = true; @@ -163,7 +184,7 @@ int32_t udfStartUdfd(int32_t startDnodeId) { uv_async_send(&pData->stopAsync); uv_thread_join(&pData->thread); pData->needCleanUp = false; - fnInfo("dnode-mgmt udfd cleaned up after spawn err"); + fnInfo("dnode udfd cleaned up after spawn err"); } else { pData->needCleanUp = true; } @@ -172,7 +193,7 @@ int32_t udfStartUdfd(int32_t startDnodeId) { int32_t udfStopUdfd() { SUdfdData *pData = &udfdGlobal; - fnInfo("dnode-mgmt to stop udfd. need cleanup: %d, spawn err: %d", + fnInfo("dnode to stop udfd. need cleanup: %d, spawn err: %d", pData->needCleanUp, pData->spawnErr); if (!pData->needCleanUp || atomic_load_32(&pData->stopCalled)) { return 0; @@ -182,7 +203,10 @@ int32_t udfStopUdfd() { uv_barrier_destroy(&pData->barrier); uv_async_send(&pData->stopAsync); uv_thread_join(&pData->thread); - fnInfo("dnode-mgmt udfd cleaned up"); +#ifdef WINDOWS + if (pData->jobHandle != NULL) CloseHandle(pData->jobHandle); +#endif + fnInfo("dnode udfd cleaned up"); return 0; } diff --git a/source/libs/function/test/udf2.c b/source/libs/function/test/udf2.c index ba39b09f56302964923cdfc5d073936bf46da3ee..6410af2a4b45b89b4aa894248507c176203430b6 100644 --- a/source/libs/function/test/udf2.c +++ b/source/libs/function/test/udf2.c @@ -44,7 +44,7 @@ int32_t udf2(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInterBuf *newInte case TSDB_DATA_TYPE_INT: { char* cell = udfColDataGetData(col, j); int32_t num = *(int32_t*)cell; - sumSquares += num * num; + sumSquares += (double)num * num; break; } case TSDB_DATA_TYPE_DOUBLE: { diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index 71b0774ca64b796aeb042cc1cfcfbd199faa1e09..6e0775ff17de6f8f89eeb19a6aa0ddc6f835480a 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -317,15 +317,16 @@ static int32_t tableComInfoToJson(const void* pObj, SJson* pJson) { static int32_t jsonToTableComInfo(const SJson* pJson, void* pObj) { STableComInfo* pNode = (STableComInfo*)pObj; - int32_t code = tjsonGetNumberValue(pJson, jkTableComInfoNumOfTags, pNode->numOfTags); + int32_t code; + tjsonGetNumberValue(pJson, jkTableComInfoNumOfTags, pNode->numOfTags, code);; if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableComInfoPrecision, pNode->precision); + tjsonGetNumberValue(pJson, jkTableComInfoPrecision, pNode->precision, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableComInfoNumOfColumns, pNode->numOfColumns); + tjsonGetNumberValue(pJson, jkTableComInfoNumOfColumns, pNode->numOfColumns, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableComInfoRowSize, pNode->rowSize); + tjsonGetNumberValue(pJson, jkTableComInfoRowSize, pNode->rowSize, code);; } return code; @@ -356,12 +357,13 @@ static int32_t schemaToJson(const void* pObj, SJson* pJson) { static int32_t jsonToSchema(const SJson* pJson, void* pObj) { SSchema* pNode = (SSchema*)pObj; - int32_t code = tjsonGetNumberValue(pJson, jkSchemaType, pNode->type); + int32_t code; + tjsonGetNumberValue(pJson, jkSchemaType, pNode->type, code);; if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkSchemaColId, pNode->colId); + tjsonGetNumberValue(pJson, jkSchemaColId, pNode->colId, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkSchemaBytes, pNode->bytes); + tjsonGetNumberValue(pJson, jkSchemaBytes, pNode->bytes, code);; } if (TSDB_CODE_SUCCESS == code) { code = tjsonGetStringValue(pJson, jkSchemaName, pNode->name); @@ -412,21 +414,22 @@ static int32_t tableMetaToJson(const void* pObj, SJson* pJson) { static int32_t jsonToTableMeta(const SJson* pJson, void* pObj) { STableMeta* pNode = (STableMeta*)pObj; - int32_t code = tjsonGetNumberValue(pJson, jkTableMetaVgId, pNode->vgId); + int32_t code; + tjsonGetNumberValue(pJson, jkTableMetaVgId, pNode->vgId, code);; if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableMetaTableType, pNode->tableType); + tjsonGetNumberValue(pJson, jkTableMetaTableType, pNode->tableType, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableMetaUid, pNode->uid); + tjsonGetNumberValue(pJson, jkTableMetaUid, pNode->uid, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableMetaSuid, pNode->suid); + tjsonGetNumberValue(pJson, jkTableMetaSuid, pNode->suid, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableMetaSversion, pNode->sversion); + tjsonGetNumberValue(pJson, jkTableMetaSversion, pNode->sversion, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableMetaTversion, pNode->tversion); + tjsonGetNumberValue(pJson, jkTableMetaTversion, pNode->tversion, code);; } if (TSDB_CODE_SUCCESS == code) { code = tjsonToObject(pJson, jkTableMetaComInfo, jsonToTableComInfo, &pNode->tableInfo); @@ -602,7 +605,7 @@ static int32_t jsonToLogicFillNode(const SJson* pJson, void* pObj) { int32_t code = jsonToLogicPlanNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkFillLogicPlanMode, pNode->mode); + tjsonGetNumberValue(pJson, jkFillLogicPlanMode, pNode->mode, code);; } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeObject(pJson, jkFillLogicPlanWStartTs, &pNode->pWStartTs); @@ -878,7 +881,7 @@ static int32_t jsonToLogicSubplan(const SJson* pJson, void* pObj) { code = jsonToNodeObject(pJson, jkLogicSubplanRootNode, (SNode**)&pNode->pNode); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkLogicSubplanType, pNode->subplanType); + tjsonGetNumberValue(pJson, jkLogicSubplanType, pNode->subplanType, code);; } int32_t objSize = 0; if (TSDB_CODE_SUCCESS == code) { @@ -1118,25 +1121,25 @@ static int32_t jsonToPhysiTableScanNode(const SJson* pJson, void* pObj) { code = tjsonGetDoubleValue(pJson, jkTableScanPhysiPlanRatio, &pNode->ratio); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableScanPhysiPlanDataRequired, pNode->dataRequired); + tjsonGetNumberValue(pJson, jkTableScanPhysiPlanDataRequired, pNode->dataRequired, code);; } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeList(pJson, jkTableScanPhysiPlanDynamicScanFuncs, &pNode->pDynamicScanFuncs); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableScanPhysiPlanInterval, pNode->interval); + tjsonGetNumberValue(pJson, jkTableScanPhysiPlanInterval, pNode->interval, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableScanPhysiPlanOffset, pNode->offset); + tjsonGetNumberValue(pJson, jkTableScanPhysiPlanOffset, pNode->offset, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableScanPhysiPlanSliding, pNode->sliding); + tjsonGetNumberValue(pJson, jkTableScanPhysiPlanSliding, pNode->sliding, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableScanPhysiPlanIntervalUnit, pNode->intervalUnit); + tjsonGetNumberValue(pJson, jkTableScanPhysiPlanIntervalUnit, pNode->intervalUnit, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkTableScanPhysiPlanSlidingUnit, pNode->slidingUnit); + tjsonGetNumberValue(pJson, jkTableScanPhysiPlanSlidingUnit, pNode->slidingUnit, code);; } return code; @@ -1178,7 +1181,7 @@ static int32_t jsonToPhysiSysTableScanNode(const SJson* pJson, void* pObj) { code = tjsonGetBoolValue(pJson, jkSysTableScanPhysiPlanShowRewrite, &pNode->showRewrite); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkSysTableScanPhysiPlanAccountId, pNode->accountId); + tjsonGetNumberValue(pJson, jkSysTableScanPhysiPlanAccountId, pNode->accountId, code);; } return code; @@ -1262,7 +1265,7 @@ static int32_t jsonToPhysiJoinNode(const SJson* pJson, void* pObj) { int32_t code = jsonToPhysicPlanNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkJoinPhysiPlanJoinType, pNode->joinType); + tjsonGetNumberValue(pJson, jkJoinPhysiPlanJoinType, pNode->joinType, code);; } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeObject(pJson, jkJoinPhysiPlanOnConditions, &pNode->pOnConditions); @@ -1424,10 +1427,10 @@ static int32_t jsonToPhysiWindowNode(const SJson* pJson, void* pObj) { code = jsonToNodeObject(pJson, jkWindowPhysiPlanTsPk, (SNode**)&pNode->pTspk); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkWindowPhysiPlanTriggerType, pNode->triggerType); + tjsonGetNumberValue(pJson, jkWindowPhysiPlanTriggerType, pNode->triggerType, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkWindowPhysiPlanWatermark, pNode->watermark); + tjsonGetNumberValue(pJson, jkWindowPhysiPlanWatermark, pNode->watermark, code);; } return code; @@ -1523,7 +1526,7 @@ static int32_t jsonToPhysiFillNode(const SJson* pJson, void* pObj) { int32_t code = jsonToPhysicPlanNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkFillPhysiPlanMode, pNode->mode); + tjsonGetNumberValue(pJson, jkFillPhysiPlanMode, pNode->mode, code);; } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeObject(pJson, jkFillPhysiPlanWStartTs, &pNode->pWStartTs); @@ -1562,7 +1565,7 @@ static int32_t jsonToPhysiSessionWindowNode(const SJson* pJson, void* pObj) { int32_t code = jsonToPhysiWindowNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkSessionWindowPhysiPlanGap, pNode->gap); + tjsonGetNumberValue(pJson, jkSessionWindowPhysiPlanGap, pNode->gap, code);; } return code; @@ -1724,7 +1727,7 @@ static int32_t jsonToSubplan(const SJson* pJson, void* pObj) { int32_t code = tjsonToObject(pJson, jkSubplanId, jsonToSubplanId, &pNode->id); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkSubplanType, pNode->subplanType); + tjsonGetNumberValue(pJson, jkSubplanType, pNode->subplanType, code);; } if (TSDB_CODE_SUCCESS == code) { code = tjsonGetIntValue(pJson, jkSubplanMsgType, &pNode->msgType); @@ -1914,7 +1917,7 @@ static int32_t jsonToColumnNode(const SJson* pJson, void* pObj) { code = tjsonGetSmallIntValue(pJson, jkColumnColId, &pNode->colId); } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkColumnColType, pNode->colType); + tjsonGetNumberValue(pJson, jkColumnColType, pNode->colType, code);; } if (TSDB_CODE_SUCCESS == code) { code = tjsonGetStringValue(pJson, jkColumnDbName, pNode->dbName); @@ -2168,7 +2171,7 @@ static int32_t jsonToOperatorNode(const SJson* pJson, void* pObj) { int32_t code = jsonToExprNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkOperatorType, pNode->opType); + tjsonGetNumberValue(pJson, jkOperatorType, pNode->opType, code);; } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeObject(pJson, jkOperatorLeft, &pNode->pLeft); @@ -2202,7 +2205,7 @@ static int32_t jsonToLogicConditionNode(const SJson* pJson, void* pObj) { int32_t code = jsonToExprNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkLogicCondType, pNode->condType); + tjsonGetNumberValue(pJson, jkLogicCondType, pNode->condType, code);; } if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeList(pJson, jkLogicCondParameters, &pNode->pParameterList); @@ -2384,10 +2387,10 @@ static int32_t jsonToOrderByExprNode(const SJson* pJson, void* pObj) { int32_t code = jsonToNodeObject(pJson, jkOrderByExprExpr, &pNode->pExpr); if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkOrderByExprOrder, pNode->order); + tjsonGetNumberValue(pJson, jkOrderByExprOrder, pNode->order, code);; } if (TSDB_CODE_SUCCESS == code) { - code = tjsonGetNumberValue(pJson, jkOrderByExprNullOrder, pNode->nullOrder); + tjsonGetNumberValue(pJson, jkOrderByExprNullOrder, pNode->nullOrder, code);; } return code; @@ -2493,7 +2496,8 @@ static int32_t fillNodeToJson(const void* pObj, SJson* pJson) { static int32_t jsonToFillNode(const SJson* pJson, void* pObj) { SFillNode* pNode = (SFillNode*)pObj; - int32_t code = tjsonGetNumberValue(pJson, jkFillMode, pNode->mode); + int32_t code; + tjsonGetNumberValue(pJson, jkFillMode, pNode->mode, code);; if (TSDB_CODE_SUCCESS == code) { code = jsonToNodeObject(pJson, jkFillValues, &pNode->pValues); } @@ -3033,7 +3037,8 @@ static int32_t nodeToJson(const void* pObj, SJson* pJson) { static int32_t jsonToNode(const SJson* pJson, void* pObj) { SNode* pNode = (SNode*)pObj; - int32_t code = tjsonGetNumberValue(pJson, jkNodeType, pNode->type); + int32_t code; + tjsonGetNumberValue(pJson, jkNodeType, pNode->type, code);; if (TSDB_CODE_SUCCESS == code) { code = tjsonToObject(pJson, nodesNodeName(pNode->type), jsonToSpecificNode, pNode); if (TSDB_CODE_SUCCESS != code) { diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index 228cb5a44bf722ed2875b7e482e4bd97c9b0c03f..81f55d80463115b501d1d48c40e09e826d90c433 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -758,7 +758,7 @@ static int32_t KvRowAppend(SMsgBuf* pMsgBuf, const void* value, int32_t len, voi int32_t output = 0; if (!taosMbsToUcs4(value, len, (TdUcs4*)varDataVal(pa->buf), pa->schema->bytes - VARSTR_HEADER_SIZE, &output)) { char buf[512] = {0}; - snprintf(buf, tListLen(buf), "%s", strerror(errno)); + snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(errno)); return buildSyntaxErrMsg(pMsgBuf, buf, value); } @@ -1668,7 +1668,11 @@ static int32_t smlBuildTagRow(SArray* cols, SKVRowBuilder* tagsBuilder, SParsedD SSchema* pTagSchema = &pSchema[tags->boundColumns[i] - 1]; // colId starts with 1 param.schema = pTagSchema; SSmlKv* kv = taosArrayGetP(cols, i); - KvRowAppend(msg, kv->value, kv->valueLen, ¶m); + if(IS_VAR_DATA_TYPE(kv->type)){ + KvRowAppend(msg, kv->value, kv->length, ¶m); + }else{ + KvRowAppend(msg, &(kv->value), kv->length, ¶m); + } } *row = tdGetKVRowFromBuilder(tagsBuilder); @@ -1766,14 +1770,16 @@ int32_t smlBindData(void *handle, SArray *tags, SArray *colsSchema, SArray *cols if (!kv || kv->length == 0) { MemRowAppend(&pBuf, NULL, 0, ¶m); } else { - int32_t colLen = pColSchema->bytes; - if (IS_VAR_DATA_TYPE(pColSchema->type)) { - colLen = kv->length; - } else if (pColSchema->type == TSDB_DATA_TYPE_TIMESTAMP) { + int32_t colLen = kv->length; + if (pColSchema->type == TSDB_DATA_TYPE_TIMESTAMP) { kv->i = convertTimePrecision(kv->i, TSDB_TIME_PRECISION_NANO, pTableMeta->tableInfo.precision); } - MemRowAppend(&pBuf, &(kv->value), colLen, ¶m); + if(IS_VAR_DATA_TYPE(kv->type)){ + MemRowAppend(&pBuf, kv->value, colLen, ¶m); + }else{ + MemRowAppend(&pBuf, &(kv->value), colLen, ¶m); + } } if (PRIMARYKEY_TIMESTAMP_COL_ID == pColSchema->colId) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index c86c1ac2e97034a423d1949abd7e0a767be675e7..b7f007fce66afc477770f4916267e1ab16fba1d0 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1538,7 +1538,9 @@ static EDealRes checkStateExpr(SNode* pNode, void* pContext) { if (QUERY_NODE_COLUMN == nodeType(pNode)) { STranslateContext* pCxt = pContext; SColumnNode* pCol = (SColumnNode*)pNode; - if (!IS_INTEGER_TYPE(pCol->node.resType.type)) { + + int32_t type = pCol->node.resType.type; + if (!IS_INTEGER_TYPE(type) && type != TSDB_DATA_TYPE_BOOL && !IS_VAR_DATA_TYPE(type)) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE); } if (COLUMN_TYPE_TAG == pCol->colType) { diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 43aea8de7c7dfbdc9b2652d9fb4a8073ab1c42e8..7183d956a9f02eda15539f3a2c55a65076807cf3 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -91,7 +91,7 @@ static char* getSyntaxErrFormat(int32_t errCode) { case TSDB_CODE_PAR_AGG_FUNC_NESTING: return "Aggregate functions do not support nesting"; case TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE: - return "Only support STATE_WINDOW on integer column"; + return "Only support STATE_WINDOW on integer/bool/varchar column"; case TSDB_CODE_PAR_INVALID_STATE_WIN_COL: return "Not support STATE_WINDOW on tag column"; case TSDB_CODE_PAR_INVALID_STATE_WIN_TABLE: diff --git a/source/libs/qcom/src/queryUtil.c b/source/libs/qcom/src/queryUtil.c index 4b4c0796491b1c6565d9e359250c16f48257c15d..4c05358a13cbbf4195363b231f7854dcc0405ab3 100644 --- a/source/libs/qcom/src/queryUtil.c +++ b/source/libs/qcom/src/queryUtil.c @@ -97,7 +97,7 @@ bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_t numOfTag static void* pTaskQueue = NULL; int32_t initTaskQueue() { - int32_t queueSize = tsMaxConnections * 2; + int32_t queueSize = tsMaxShellConns * 2; pTaskQueue = taosInitScheduler(queueSize, tsNumOfTaskQueueThreads, "tsc"); if (NULL == pTaskQueue) { qError("failed to init task queue"); diff --git a/source/libs/scalar/inc/sclInt.h b/source/libs/scalar/inc/sclInt.h index 9257d2c0d49d2921044252ad9d5297cecb4fcdf0..c485c81dd60abc918a96d4a03f4ed5b3e09f8947 100644 --- a/source/libs/scalar/inc/sclInt.h +++ b/source/libs/scalar/inc/sclInt.h @@ -27,11 +27,13 @@ typedef struct SScalarCtx { SArray *pBlockList; /* element is SSDataBlock* */ SHashObj *pRes; /* element is SScalarParam */ void *param; // additional parameter (meta actually) for acquire value such as tbname/tags values + SHashObj *udf2Handle; } SScalarCtx; #define SCL_DATA_TYPE_DUMMY_HASH 9000 #define SCL_DEFAULT_OP_NUM 10 +#define SCL_DEFAULT_UDF_NUM 8 #define SCL_IS_CONST_NODE(_node) ((NULL == (_node)) || (QUERY_NODE_VALUE == (_node)->type) || (QUERY_NODE_NODE_LIST == (_node)->type)) #define SCL_IS_CONST_CALC(_ctx) (NULL == (_ctx)->pBlockList) diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 9843f0ac916e98ecc633d2ee8585cb41ebf458a0..c9917d8544fb00b8fb59d3f86455a843da2ddb01 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -153,6 +153,18 @@ void sclFreeRes(SHashObj *res) { taosHashCleanup(res); } +void sclFreeUdfHandles(SHashObj *udf2handle) { + void *pIter = taosHashIterate(udf2handle, NULL); + while (pIter) { + UdfcFuncHandle *handle = (UdfcFuncHandle *)pIter; + if (handle) { + teardownUdf(*handle); + } + pIter = taosHashIterate(udf2handle, pIter); + } + taosHashCleanup(udf2handle); +} + void sclFreeParam(SScalarParam *param) { if (param->columnData != NULL) { colDataDestroy(param->columnData); @@ -362,18 +374,24 @@ int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *outp if (fmIsUserDefinedFunc(node->funcId)) { UdfcFuncHandle udfHandle = NULL; - - code = setupUdf(node->functionName, &udfHandle); - if (code != 0) { - sclError("fmExecFunction error. setupUdf. function name: %s, code:%d", node->functionName, code); - goto _return; + char* udfName = node->functionName; + if (ctx->udf2Handle) { + UdfcFuncHandle *pHandle = taosHashGet(ctx->udf2Handle, udfName, strlen(udfName)); + if (pHandle) { + udfHandle = *pHandle; + } } - code = callUdfScalarFunc(udfHandle, params, paramNum, output); - if (code != 0) { - sclError("fmExecFunction error. callUdfScalarFunc. function name: %s, udf code:%d", node->functionName, code); - goto _return; + if (udfHandle == NULL) { + code = setupUdf(udfName, &udfHandle); + if (code != 0) { + sclError("fmExecFunction error. setupUdf. function name: %s, code:%d", udfName, code); + goto _return; + } + if (ctx->udf2Handle) { + taosHashPut(ctx->udf2Handle, udfName, strlen(udfName), &udfHandle, sizeof(UdfcFuncHandle)); + } } - code = teardownUdf(udfHandle); + code = callUdfScalarFunc(udfHandle, params, paramNum, output); if (code != 0) { sclError("fmExecFunction error. callUdfScalarFunc. function name: %s, udf code:%d", node->functionName, code); goto _return; @@ -891,15 +909,20 @@ int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) { SScalarCtx ctx = {0}; ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); if (NULL == ctx.pRes) { - sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM); + sclError("taosHashInit result map failed, num:%d", SCL_DEFAULT_OP_NUM); + SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + ctx.udf2Handle = taosHashInit(SCL_DEFAULT_UDF_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + if (NULL == ctx.udf2Handle) { + sclError("taosHashInit udf to handle map failed, num:%d", SCL_DEFAULT_OP_NUM); SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - nodesRewriteExprPostOrder(&pNode, sclConstantsRewriter, (void *)&ctx); SCL_ERR_JRET(ctx.code); *pRes = pNode; _return: + sclFreeUdfHandles(ctx.udf2Handle); sclFreeRes(ctx.pRes); return code; } @@ -915,10 +938,14 @@ int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) { // TODO: OPT performance ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); if (NULL == ctx.pRes) { - sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM); + sclError("taosHashInit result map failed, num:%d", SCL_DEFAULT_OP_NUM); + SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + ctx.udf2Handle = taosHashInit(SCL_DEFAULT_UDF_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + if (NULL == ctx.udf2Handle) { + sclError("taosHashInit udf to handle map failed, num:%d", SCL_DEFAULT_OP_NUM); SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - nodesWalkExprPostOrder(pNode, sclCalcWalker, (void *)&ctx); SCL_ERR_JRET(ctx.code); @@ -936,6 +963,7 @@ int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) { _return: //nodesDestroyNode(pNode); + sclFreeUdfHandles(ctx.udf2Handle); sclFreeRes(ctx.pRes); return code; } diff --git a/source/os/src/osAtomic.c b/source/os/src/osAtomic.c index 0fe946bf68e669d4b5b152ec61e5b38c59883a34..e4d880f40a86da2c179151aaccc03a5849616edc 100644 --- a/source/os/src/osAtomic.c +++ b/source/os/src/osAtomic.c @@ -36,7 +36,7 @@ int64_t interlocked_add_fetch_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_add_fetch_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)(_InterlockedExchangeAdd((int32_t volatile*)(ptr), (int32_t)val) + (int32_t)val); #else return (void*)(InterlockedExchangeAdd64((int64_t volatile*)(ptr), (int64_t)val) + (int64_t)val); @@ -56,7 +56,7 @@ int32_t interlocked_and_fetch_32(int32_t volatile* ptr, int32_t val) { } int64_t interlocked_and_fetch_64(int64_t volatile* ptr, int64_t val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS int64_t old, res; do { old = *ptr; @@ -69,7 +69,7 @@ int64_t interlocked_and_fetch_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_and_fetch_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)interlocked_and_fetch_32((int32_t volatile*)ptr, (int32_t)val); #else return (void*)interlocked_and_fetch_64((int64_t volatile*)ptr, (int64_t)val); @@ -77,7 +77,7 @@ void* interlocked_and_fetch_ptr(void* volatile* ptr, void* val) { } int64_t interlocked_fetch_and_64(int64_t volatile* ptr, int64_t val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS int64_t old; do { old = *ptr; @@ -89,7 +89,7 @@ int64_t interlocked_fetch_and_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_fetch_and_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)_InterlockedAnd((int32_t volatile*)(ptr), (int32_t)(val)); #else return (void*)_InterlockedAnd64((int64_t volatile*)(ptr), (int64_t)(val)); @@ -109,7 +109,7 @@ int32_t interlocked_or_fetch_32(int32_t volatile* ptr, int32_t val) { } int64_t interlocked_or_fetch_64(int64_t volatile* ptr, int64_t val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS int64_t old, res; do { old = *ptr; @@ -122,7 +122,7 @@ int64_t interlocked_or_fetch_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_or_fetch_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)interlocked_or_fetch_32((int32_t volatile*)ptr, (int32_t)val); #else return (void*)interlocked_or_fetch_64((int64_t volatile*)ptr, (int64_t)val); @@ -130,7 +130,7 @@ void* interlocked_or_fetch_ptr(void* volatile* ptr, void* val) { } int64_t interlocked_fetch_or_64(int64_t volatile* ptr, int64_t val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS int64_t old; do { old = *ptr; @@ -142,7 +142,7 @@ int64_t interlocked_fetch_or_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_fetch_or_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)_InterlockedOr((int32_t volatile*)(ptr), (int32_t)(val)); #else return (void*)interlocked_fetch_or_64((int64_t volatile*)(ptr), (int64_t)(val)); @@ -162,7 +162,7 @@ int32_t interlocked_xor_fetch_32(int32_t volatile* ptr, int32_t val) { } int64_t interlocked_xor_fetch_64(int64_t volatile* ptr, int64_t val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS int64_t old, res; do { old = *ptr; @@ -175,7 +175,7 @@ int64_t interlocked_xor_fetch_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_xor_fetch_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)interlocked_xor_fetch_32((int32_t volatile*)(ptr), (int32_t)(val)); #else return (void*)interlocked_xor_fetch_64((int64_t volatile*)(ptr), (int64_t)(val)); @@ -183,7 +183,7 @@ void* interlocked_xor_fetch_ptr(void* volatile* ptr, void* val) { } int64_t interlocked_fetch_xor_64(int64_t volatile* ptr, int64_t val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS int64_t old; do { old = *ptr; @@ -195,7 +195,7 @@ int64_t interlocked_fetch_xor_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_fetch_xor_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)_InterlockedXor((int32_t volatile*)(ptr), (int32_t)(val)); #else return (void*)interlocked_fetch_xor_64((int64_t volatile*)(ptr), (int64_t)(val)); @@ -211,7 +211,7 @@ int64_t interlocked_sub_fetch_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_sub_fetch_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)interlocked_sub_fetch_32((int32_t volatile*)ptr, (int32_t)val); #else return (void*)interlocked_add_fetch_64((int64_t volatile*)ptr, (int64_t)val); @@ -226,7 +226,7 @@ int64_t interlocked_fetch_sub_64(int64_t volatile* ptr, int64_t val) { } void* interlocked_fetch_sub_ptr(void* volatile* ptr, void* val) { -#ifdef _TD_WINDOWS_32 +#ifdef WINDOWS return (void*)interlocked_fetch_sub_32((int32_t volatile*)ptr, (int32_t)val); #else return (void*)interlocked_fetch_sub_64((int64_t volatile*)ptr, (int64_t)val); diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 425bf8b7ac220ac92a7c490377fde283e541de9a..3cd05b65cd2e00f2f7308ac8e8fb1de7d1b7f9a8 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -109,6 +109,7 @@ void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, cha int64_t taosCopyFile(const char *from, const char *to) { #ifdef WINDOWS + assert(0); return 0; #else char buffer[4096]; @@ -152,16 +153,16 @@ int32_t taosRemoveFile(const char *path) { return remove(path); } int32_t taosRenameFile(const char *oldName, const char *newName) { #ifdef WINDOWS - int32_t code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); - if (code < 0) { - // printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); + bool code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); + if (!code) { + printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); } - return code; + return !code; #else int32_t code = rename(oldName, newName); if (code < 0) { - // printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); + printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno)); } return code; @@ -169,11 +170,12 @@ int32_t taosRenameFile(const char *oldName, const char *newName) { } int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime) { + struct stat fileStat; #ifdef WINDOWS - return 0; + int32_t code = _stat(path, &fileStat); #else - struct stat fileStat; int32_t code = stat(path, &fileStat); +#endif if (code < 0) { return code; } @@ -187,14 +189,15 @@ int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime) { } return 0; -#endif } int32_t taosDevInoFile(const char *path, int64_t *stDev, int64_t *stIno) { + + struct stat fileStat; #ifdef WINDOWS - return 0; + int32_t code = _stat(path, &fileStat); #else - struct stat fileStat; int32_t code = stat(path, &fileStat); +#endif if (code < 0) { return code; } @@ -208,7 +211,6 @@ int32_t taosDevInoFile(const char *path, int64_t *stDev, int64_t *stIno) { } return 0; -#endif } void autoDelFileListAdd(const char *path) { return; } @@ -276,9 +278,6 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { } int64_t taosCloseFile(TdFilePtr *ppFile) { -#ifdef WINDOWS - return 0; -#else if (ppFile == NULL || *ppFile == NULL) { return 0; } @@ -294,7 +293,12 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { (*ppFile)->fp = NULL; } if ((*ppFile)->fd >= 0) { + #ifdef WINDOWS + HANDLE h = (HANDLE)_get_osfhandle((*ppFile)->fd); + !FlushFileBuffers(h); + #else fsync((*ppFile)->fd); + #endif close((*ppFile)->fd); (*ppFile)->fd = -1; } @@ -306,7 +310,6 @@ int64_t taosCloseFile(TdFilePtr *ppFile) { taosMemoryFree(*ppFile); *ppFile = NULL; return 0; -#endif } int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { @@ -412,13 +415,17 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { } int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { -#ifdef WINDOWS - return 0; -#else + if (pFile == NULL) { + return 0; + } assert(pFile->fd >= 0); // Please check if you have closed the file. struct stat fileStat; +#ifdef WINDOWS + int32_t code = _fstat(pFile->fd, &fileStat); +#else int32_t code = fstat(pFile->fd, &fileStat); +#endif if (code < 0) { return code; } @@ -432,7 +439,6 @@ int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { } return 0; -#endif } int32_t taosLockFile(TdFilePtr pFile) { @@ -459,7 +465,7 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { #ifdef WINDOWS if (pFile->fd < 0) { errno = EBADF; - printf("%s\n", "fd arg was negative"); + printf("Ftruncate file error, fd arg was negative\n"); return -1; } @@ -516,26 +522,20 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { } int32_t taosFsyncFile(TdFilePtr pFile) { -#ifdef WINDOWS - if (pFile->fd < 0) { - errno = EBADF; - printf("%s\n", "fd arg was negative"); - return -1; - } - - HANDLE h = (HANDLE)_get_osfhandle(pFile->fd); - - return !FlushFileBuffers(h); -#else if (pFile == NULL) { return 0; } if (pFile->fp != NULL) return fflush(pFile->fp); - if (pFile->fd >= 0) return fsync(pFile->fd); - + if (pFile->fd >= 0) { + #ifdef WINDOWS + HANDLE h = (HANDLE)_get_osfhandle(pFile->fd); + return !FlushFileBuffers(h); + #else + return fsync(pFile->fd); + #endif + } return 0; -#endif } int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size) { diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index 7c877b463a785f46e4b04694462539bcadeeb405..e3791af618d341d1cd08a5fc46c9d62055be2e13 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -138,7 +138,7 @@ static void print_line(Dwarf_Debug dbg, Dwarf_Line line, Dwarf_Addr pc) { dwarf_linesrc(line, &linesrc, NULL); dwarf_lineno(line, &lineno, NULL); } - printf("%s:%" DW_PR_DUu "\n", linesrc, lineno); + printf("BackTrace %08" PRId64 " %s:%" DW_PR_DUu "\n", taosGetSelfPthreadId(), linesrc, lineno); if (line) dwarf_dealloc(dbg, linesrc, DW_DLA_STRING); } void taosPrintBackTrace() { diff --git a/source/os/src/osProc.c b/source/os/src/osProc.c index f92a3b37836a787ac17f870ab0377ce54d369523..74f1356abf3e24a7b997571989471da761273c4d 100644 --- a/source/os/src/osProc.c +++ b/source/os/src/osProc.c @@ -19,6 +19,7 @@ int32_t taosNewProc(char **args) { #ifdef WINDOWS + assert(0); return 0; #else int32_t pid = fork(); @@ -36,6 +37,7 @@ int32_t taosNewProc(char **args) { void taosWaitProc(int32_t pid) { #ifdef WINDOWS + assert(0); #else int32_t status = -1; waitpid(pid, &status, 0); @@ -44,6 +46,7 @@ void taosWaitProc(int32_t pid) { void taosKillProc(int32_t pid) { #ifdef WINDOWS + assert(0); #else kill(pid, SIGINT); #endif @@ -51,6 +54,7 @@ void taosKillProc(int32_t pid) { bool taosProcExist(int32_t pid) { #ifdef WINDOWS + assert(0); return false; #else int32_t p = getpgid(pid); diff --git a/source/os/src/osShm.c b/source/os/src/osShm.c index 1cd51f94a09f770914c67e46cdb65f6334d83b6c..cb09e2fb38c5edcc00ae753245d98b9ca498e825 100644 --- a/source/os/src/osShm.c +++ b/source/os/src/osShm.c @@ -23,6 +23,7 @@ static int32_t shmids[MAX_SHMIDS] = {0}; static void taosDeleteCreatedShms() { #if defined(WINDOWS) + assert(0); #else for (int32_t i = 0; i < MAX_SHMIDS; ++i) { int32_t shmid = shmids[i] - 1; @@ -35,6 +36,7 @@ static void taosDeleteCreatedShms() { int32_t taosCreateShm(SShm* pShm, int32_t key, int32_t shmsize) { #if defined(WINDOWS) + assert(0); #else pShm->id = -1; @@ -75,6 +77,7 @@ int32_t taosCreateShm(SShm* pShm, int32_t key, int32_t shmsize) { void taosDropShm(SShm* pShm) { #if defined(WINDOWS) + assert(0); #else if (pShm->id >= 0) { if (pShm->ptr != NULL) { @@ -90,6 +93,7 @@ void taosDropShm(SShm* pShm) { int32_t taosAttachShm(SShm* pShm) { #if defined(WINDOWS) + assert(0); #else errno = 0; diff --git a/source/os/src/osSocket.c b/source/os/src/osSocket.c index 2410586287952e690349f44d84f928d98962c804..105acb188a4236b6889cfb1c26d08479ea32f387 100644 --- a/source/os/src/osSocket.c +++ b/source/os/src/osSocket.c @@ -285,6 +285,7 @@ int32_t taosGetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void return -1; } #ifdef WINDOWS + assert(0); return 0; #else return getsockopt(pSocket->fd, level, optname, optval, (int *)optlen); @@ -642,6 +643,7 @@ int32_t taosKeepTcpAlive(TdSocketPtr pSocket) { int taosGetLocalIp(const char *eth, char *ip) { #if defined(WINDOWS) // DO NOTHAING + assert(0); return 0; #else int fd; @@ -668,6 +670,7 @@ int taosGetLocalIp(const char *eth, char *ip) { int taosValidIp(uint32_t ip) { #if defined(WINDOWS) // DO NOTHAING + assert(0); return 0; #else int ret = -1; @@ -866,6 +869,7 @@ int64_t taosCopyFds(TdSocketPtr pSrcSocket, TdSocketPtr pDestSocket, int64_t len void taosBlockSIGPIPE() { #ifdef WINDOWS + // assert(0); #else sigset_t signal_mask; sigemptyset(&signal_mask); @@ -976,14 +980,12 @@ void tinet_ntoa(char *ipstr, uint32_t ip) { } void taosIgnSIGPIPE() { -#ifdef WINDOWS -#else signal(SIGPIPE, SIG_IGN); -#endif } void taosSetMaskSIGPIPE() { #ifdef WINDOWS + // assert(0); #else sigset_t signal_mask; sigemptyset(&signal_mask); @@ -1005,6 +1007,7 @@ int32_t taosGetSocketName(TdSocketPtr pSocket, struct sockaddr *destAddr, int *a TdEpollPtr taosCreateEpoll(int32_t size) { EpollFd fd = -1; #ifdef WINDOWS + assert(0); #else fd = epoll_create(size); #endif @@ -1027,6 +1030,7 @@ int32_t taosCtlEpoll(TdEpollPtr pEpoll, int32_t epollOperate, TdSocketPtr pSocke return -1; } #ifdef WINDOWS + assert(0); #else code = epoll_ctl(pEpoll->fd, epollOperate, pSocket->fd, event); #endif @@ -1038,6 +1042,7 @@ int32_t taosWaitEpoll(TdEpollPtr pEpoll, struct epoll_event *event, int32_t maxE return -1; } #ifdef WINDOWS + assert(0); #else code = epoll_wait(pEpoll->fd, event, maxEvents, timeout); #endif diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 348424b37246222cfdb74dcff0513c0f2a5711e9..fd6172e04fe8d034d4f0b4a0eb5b56f6416db10d 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -129,7 +129,21 @@ static void taosGetProcIOnfos() { static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { #ifdef WINDOWS + FILETIME pre_idleTime = {0}; + FILETIME pre_kernelTime = {0}; + FILETIME pre_userTime = {0}; + FILETIME idleTime; + FILETIME kernelTime; + FILETIME userTime; + bool res = GetSystemTimes(&idleTime, &kernelTime, &userTime); + if (res) { + cpuInfo->idle = CompareFileTime(&pre_idleTime, &idleTime); + cpuInfo->system = CompareFileTime(&pre_kernelTime, &kernelTime); + cpuInfo->user = CompareFileTime(&pre_userTime, &userTime); + cpuInfo->nice = 0; + } #elif defined(_TD_DARWIN_64) + assert(0); #else TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { @@ -155,7 +169,18 @@ static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { #ifdef WINDOWS + FILETIME pre_krnlTm = {0}; + FILETIME pre_usrTm = {0}; + FILETIME creatTm, exitTm, krnlTm, usrTm; + + if (GetThreadTimes(GetCurrentThread(), &creatTm, &exitTm, &krnlTm, &usrTm)) { + cpuInfo->stime = CompareFileTime(&pre_krnlTm, &krnlTm); + cpuInfo->utime = CompareFileTime(&pre_usrTm, &usrTm); + cpuInfo->cutime = 0; + cpuInfo->cstime = 0; + } #elif defined(_TD_DARWIN_64) + assert(0); #else TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { @@ -219,6 +244,7 @@ void taosGetSystemInfo() { int32_t taosGetEmail(char *email, int32_t maxLen) { #ifdef WINDOWS + // assert(0); #elif defined(_TD_DARWIN_64) const char *filepath = "/usr/local/taos/email"; @@ -250,6 +276,7 @@ int32_t taosGetEmail(char *email, int32_t maxLen) { int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { #ifdef WINDOWS + assert(0); #elif defined(_TD_DARWIN_64) char *line = NULL; size_t size = 0; @@ -305,6 +332,7 @@ int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { #ifdef WINDOWS + assert(0); #elif defined(_TD_DARWIN_64) char *line = NULL; size_t size = 0; @@ -716,9 +744,7 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { #ifdef WINDOWS GUID guid; CoCreateGuid(&guid); - - sprintf(uid, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], - guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); + memcpy(uid, &guid, uidlen); return 0; #elif defined(_TD_DARWIN_64) @@ -750,6 +776,7 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { char *taosGetCmdlineByPID(int pid) { #ifdef WINDOWS + assert(0); return ""; #elif defined(_TD_DARWIN_64) static char cmdline[1024]; diff --git a/source/os/src/osSystem.c b/source/os/src/osSystem.c index 62c1747619154257c68bac98bf1e26b1b38d25c5..ba07b6c3dd8e8d56e886e176bc14063a2db464e9 100644 --- a/source/os/src/osSystem.c +++ b/source/os/src/osSystem.c @@ -33,6 +33,7 @@ typedef struct FILE TdCmd; void* taosLoadDll(const char* filename) { #if defined(WINDOWS) + assert(0); return NULL; #elif defined(_TD_DARWIN_64) return NULL; @@ -51,6 +52,7 @@ void* taosLoadDll(const char* filename) { void* taosLoadSym(void* handle, char* name) { #if defined(WINDOWS) + assert(0); return NULL; #elif defined(_TD_DARWIN_64) return NULL; @@ -71,6 +73,7 @@ void* taosLoadSym(void* handle, char* name) { void taosCloseDll(void* handle) { #if defined(WINDOWS) + assert(0); return; #elif defined(_TD_DARWIN_64) return; @@ -121,6 +124,7 @@ int taosSetConsoleEcho(bool on) { void taosSetTerminalMode() { #if defined(WINDOWS) + // assert(0); #else struct termios newtio; @@ -154,7 +158,7 @@ void taosSetTerminalMode() { int32_t taosGetOldTerminalMode() { #if defined(WINDOWS) - + // assert(0); #else /* Make sure stdin is a terminal. */ if (!isatty(STDIN_FILENO)) { @@ -172,7 +176,7 @@ int32_t taosGetOldTerminalMode() { void taosResetTerminalMode() { #if defined(WINDOWS) - + // assert(0); #else if (tcsetattr(0, TCSANOW, &oldtio) != 0) { fprintf(stderr, "Fail to reset the terminal properties!\n"); diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 8f48e0585e79294dd65649a37a12a2fbcb17beb4..11d7d9831aadf9f419e618d745c70066147bfa45 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -640,13 +640,14 @@ int32_t cfgLoadFromEnvVar(SConfig *pConfig) { } int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { - char *buf, *name, *value, *value2, *value3; + char buf[1024], *name, *value, *value2, *value3; int32_t olen, vlen, vlen2, vlen3; int32_t index = 0; if (envCmd == NULL) return 0; while (envCmd[index]!=NULL) { - buf = taosMemoryMalloc(strlen(envCmd[index])); - taosEnvToCfg(envCmd[index], buf); + strncpy(buf, envCmd[index], sizeof(buf)-1); + buf[sizeof(buf)-1] = 0; + taosEnvToCfg(buf, buf); index++; name = value = value2 = value3 = NULL; @@ -671,8 +672,6 @@ int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { if (value2 != NULL && value3 != NULL && value2[0] != 0 && value3[0] != 0 && strcasecmp(name, "dataDir") == 0) { cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_ENV_CMD); } - - taosMemoryFree(buf); } uInfo("load from env cmd cfg success"); diff --git a/tests/pytest/util/dnodes.py b/tests/pytest/util/dnodes.py index 3c23e784c5e7eca90ea60b01f35a0a396c8dcccc..c78efb4e8dbcc95c71f5a863fcd93413fcdf1736 100644 --- a/tests/pytest/util/dnodes.py +++ b/tests/pytest/util/dnodes.py @@ -30,17 +30,12 @@ class TDSimClient: "locale": "en_US.UTF-8", "charset": "UTF-8", "asyncLog": "0", - "minTablesPerVnode": "4", - "maxTablesPerVnode": "1000", - "tableIncStepPerVnode": "10000", - "maxVgroupsPerDb": "1000", - "sdbDebugFlag": "143", "rpcDebugFlag": "143", "tmrDebugFlag": "131", - "cDebugFlag": "135", - "udebugFlag": "135", - "jnidebugFlag": "135", - "qdebugFlag": "135", + "cDebugFlag": "143", + "udebugFlag": "143", + "jnidebugFlag": "143", + "qdebugFlag": "143", "telemetryReporting": "0", } @@ -115,36 +110,29 @@ class TDDnode: self.testCluster = False self.valgrind = 0 self.cfgDict = { - "numOfLogLines": "100000000", - "mnodeEqualVnodeNum": "0", "walLevel": "2", "fsync": "1000", - "statusInterval": "1", - "numOfMnodes": "3", - "numOfThreadsPerCore": "2.0", "monitor": "0", - "maxVnodeConnections": "30000", - "maxMgmtConnections": "30000", - "maxMeterConnections": "30000", "maxShellConns": "30000", "locale": "en_US.UTF-8", "charset": "UTF-8", "asyncLog": "0", - "anyIp": "0", - "telemetryReporting": "0", - "dDebugFlag": "135", - "tsdbDebugFlag": "135", - "mDebugFlag": "135", - "sdbDebugFlag": "135", + "mDebugFlag": "143", + "dDebugFlag": "143", + "vDebugFlag": "143", + "tqDebugFlag": "143", + "cDebugFlag": "143", + "jniDebugFlag": "143", + "qDebugFlag": "143", "rpcDebugFlag": "143", "tmrDebugFlag": "131", - "cDebugFlag": "135", - "httpDebugFlag": "135", - "monitorDebugFlag": "135", - "udebugFlag": "135", - "jnidebugFlag": "135", - "qdebugFlag": "135", - "maxSQLLength": "1048576" + "uDebugFlag": "143", + "sDebugFlag": "135", + "wDebugFlag": "143", + "qdebugFlag": "143", + "numOfLogLines": "100000000", + "statusInterval": "1", + "telemetryReporting": "0" } def init(self, path): diff --git a/tests/script/jenkins/basic.txt b/tests/script/jenkins/basic.txt index f6bc9f83064bca6da216fae4e23260e8bff83645..bddee2c68a18651380ffe324e611c9df681efa5e 100644 --- a/tests/script/jenkins/basic.txt +++ b/tests/script/jenkins/basic.txt @@ -63,7 +63,7 @@ # ---- tstream ./test.sh -f tsim/tstream/basic0.sim -./test.sh -f tsim/tstream/basic1.sim +#./test.sh -f tsim/tstream/basic1.sim # ---- transaction ./test.sh -f tsim/trans/create_db.sim diff --git a/tests/script/tsim/query/udf.sim b/tests/script/tsim/query/udf.sim index 19e133e9496ea6161656d830432049eebe407e8c..00dbcf0f6d777352235b5195970ebfdcfa5622c2 100644 --- a/tests/script/tsim/query/udf.sim +++ b/tests/script/tsim/query/udf.sim @@ -94,6 +94,31 @@ endi if $data00 != 2.645751311 then return -1 endi + +sql insert into t2 values(now+4s, 4, 8)(now+5s, 5, 9); +sql select udf2(f1-f2), udf2(f1+f2) from t2; +print $rows , $data00 , $data01 +if $rows != 1 then + return -1; +endi +if $data00 != 5.656854249 then + return -1 +endi +if $data01 != 18.547236991 then + return -1 +endi + +sql select udf2(udf1(f2-f1)), udf2(udf1(f2/f1)) from t2; +if $rows != 1 then + return -1 +endi +if $data00 != 176.000000000 then + return -1 +endi +if $data01 != 152.420471066 then + return -1 +endi +print $rows , $data00 , $data01 sql drop function udf1; sql show functions; if $rows != 1 then diff --git a/tests/system-test/2-query/join.py b/tests/system-test/2-query/join.py index 289dd3d62df0b38fc3d6d857ba9bcd22bfd14aae..8fc131e58173faf31fcc4ffbc8fab08f6e937aea 100644 --- a/tests/system-test/2-query/join.py +++ b/tests/system-test/2-query/join.py @@ -28,7 +28,7 @@ class TDTestCase: def init(self, conn, logSql): tdLog.debug(f"start to excute {__file__}") - tdSql.init(conn.cursor()) + tdSql.init(conn.cursor(), True) def __query_condition(self,tbname): query_condition = [] diff --git a/tests/system-test/7-tmq/subscribeDb.py b/tests/system-test/7-tmq/subscribeDb.py new file mode 100644 index 0000000000000000000000000000000000000000..b8d3abca5c1cb8828777ff434c82ec8f3b659e30 --- /dev/null +++ b/tests/system-test/7-tmq/subscribeDb.py @@ -0,0 +1,400 @@ + +import taos +import sys +import time +import socket +import os +import threading + +from util.log import * +from util.sql import * +from util.cases import * +from util.dnodes import * + +class TDTestCase: + hostname = socket.gethostname() + #rpcDebugFlagVal = '143' + #clientCfgDict = {'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''} + #clientCfgDict["rpcDebugFlag"] = rpcDebugFlagVal + #updatecfgDict = {'clientCfg': {}, 'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''} + #updatecfgDict["rpcDebugFlag"] = rpcDebugFlagVal + #print ("===================: ", updatecfgDict) + + def init(self, conn, logSql): + tdLog.debug(f"start to excute {__file__}") + #tdSql.init(conn.cursor()) + tdSql.init(conn.cursor(), logSql) # output sql.txt file + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root) - len("/build/bin")] + break + return buildPath + + def newcur(self,cfg,host,port): + user = "root" + password = "taosdata" + con=taos.connect(host=host, user=user, password=password, config=cfg ,port=port) + cur=con.cursor() + print(cur) + return cur + + def startTmqSimProcess(self,buildPath,cfgPath,pollDelay,dbName,showMsg,showRow,cdbName,valgrind=0): + shellCmd = 'nohup ' + if valgrind == 1: + logFile = cfgPath + '/../log/valgrind-tmq.log' + shellCmd = 'nohup valgrind --log-file=' + logFile + shellCmd += '--tool=memcheck --leak-check=full --show-reachable=no --track-origins=yes --show-leak-kinds=all --num-callers=20 -v --workaround-gcc296-bugs=yes ' + + shellCmd += buildPath + '/build/bin/tmq_sim -c ' + cfgPath + shellCmd += " -y %d -d %s -g %d -r %d -w %s "%(pollDelay, dbName, showMsg, showRow, cdbName) + shellCmd += "> /dev/null 2>&1 &" + tdLog.info(shellCmd) + os.system(shellCmd) + + def create_tables(self,tsql, dbName,vgroups,stbName,ctbNum,rowsPerTbl): + tsql.execute("create database if not exists %s vgroups %d"%(dbName, vgroups)) + tsql.execute("use %s" %dbName) + tsql.execute("create table if not exists %s (ts timestamp, c1 bigint, c2 binary(16)) tags(t1 int)"%stbName) + pre_create = "create table" + sql = pre_create + #tdLog.debug("doing create one stable %s and %d child table in %s ..." %(stbname, count ,dbname)) + for i in range(ctbNum): + sql += " %s_%d using %s tags(%d)"%(stbName,i,stbName,i+1) + if (i > 0) and (i%100 == 0): + tsql.execute(sql) + sql = pre_create + if sql != pre_create: + tsql.execute(sql) + + event.set() + tdLog.debug("complete to create database[%s], stable[%s] and %d child tables" %(dbName, stbName, ctbNum)) + return + + def insert_data(self,tsql,dbName,stbName,ctbNum,rowsPerTbl,batchNum,startTs): + tdLog.debug("start to insert data ............") + tsql.execute("use %s" %dbName) + pre_insert = "insert into " + sql = pre_insert + + #tdLog.debug("doing insert data into stable:%s rows:%d ..."%(stbName, allRows)) + for i in range(ctbNum): + sql += " %s_%d values "%(stbName,i) + for j in range(rowsPerTbl): + sql += "(%d, %d, 'tmqrow_%d') "%(startTs + j, j, j) + if (j > 0) and ((j%batchNum == 0) or (j == rowsPerTbl - 1)): + tsql.execute(sql) + if j < rowsPerTbl - 1: + sql = "insert into %s_%d values " %(stbName,i) + else: + sql = "insert into " + #end sql + if sql != pre_insert: + #print("insert sql:%s"%sql) + tsql.execute(sql) + tdLog.debug("insert data ............ [OK]") + return + + def prepareEnv(self, **parameterDict): + print ("input parameters:") + print (parameterDict) + # create new connector for my thread + tsql=self.newcur(parameterDict['cfg'], 'localhost', 6030) + self.create_tables(tsql,\ + parameterDict["dbName"],\ + parameterDict["vgroups"],\ + parameterDict["stbName"],\ + parameterDict["ctbNum"],\ + parameterDict["rowsPerTbl"]) + + self.insert_data(tsql,\ + parameterDict["dbName"],\ + parameterDict["stbName"],\ + parameterDict["ctbNum"],\ + parameterDict["rowsPerTbl"],\ + parameterDict["batchNum"],\ + parameterDict["startTs"]) + return + + def tmqCase1(self, cfgPath, buildPath): + tdLog.printNoPrefix("======== test case 1: Produce while one consume to subscribe one db") + tdLog.info("step 1: create database, stb, ctb and insert data") + # create and start thread + parameterDict = {'cfg': '', \ + 'dbName': 'db1', \ + 'vgroups': 4, \ + 'stbName': 'stb', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 200, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + tdSql.execute("create database if not exists %s vgroups %d" %(parameterDict['dbName'], parameterDict['vgroups'])) + + prepareEnvThread = threading.Thread(target=self.prepareEnv, kwargs=parameterDict) + prepareEnvThread.start() + + tdLog.info("create topics from db") + topicName1 = 'topic_db1' + + tdSql.execute("create topic %s as %s" %(topicName1, parameterDict['dbName'])) + + tdLog.info("create consume info table and consume result table") + cdbName = parameterDict["dbName"] + tdSql.query("create table %s.consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int)"%cdbName) + tdSql.query("create table %s.consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int)"%cdbName) + + consumerId = 0 + expectrowcnt = parameterDict["rowsPerTbl"] * parameterDict["ctbNum"] + topicList = topicName1 + ifcheckdata = 0 + keyList = 'group.id:cgrp1,\ + enable.auto.commit:false,\ + auto.commit.interval.ms:6000,\ + auto.offset.reset:earliest' + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + event.wait() + + tdLog.info("start consume processor") + pollDelay = 5 + showMsg = 1 + showRow = 1 + self.startTmqSimProcess(buildPath,cfgPath,pollDelay,parameterDict["dbName"],showMsg, showRow, cdbName) + + # wait for data ready + prepareEnvThread.join() + + tdLog.info("insert process end, and start to check consume result") + while 1: + tdSql.query("select * from %s.consumeresult"%cdbName) + #tdLog.info("row: %d, %l64d, %l64d"%(tdSql.getData(0, 1),tdSql.getData(0, 2),tdSql.getData(0, 3)) + if tdSql.getRows() == 1: + break + else: + time.sleep(5) + + tdLog.info("consumer result: %d, %d"%(tdSql.getData(0 , 2), tdSql.getData(0 , 3))) + tdSql.checkData(0 , 1, consumerId) + # mulit rows and mulit tables in one sql, this num of msg is not sure + #tdSql.checkData(0 , 2, expectmsgcnt) + tdSql.checkData(0 , 3, expectrowcnt+1) + + tdSql.query("drop topic %s"%topicName1) + + tdLog.printNoPrefix("======== test case 1 end ...... ") + + def tmqCase2(self, cfgPath, buildPath): + tdLog.printNoPrefix("======== test case 2: Produce while two consumers to subscribe one db") + tdLog.info("step 1: create database, stb, ctb and insert data") + # create and start thread + parameterDict = {'cfg': '', \ + 'dbName': 'db2', \ + 'vgroups': 4, \ + 'stbName': 'stb', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 100, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + tdSql.execute("create database if not exists %s vgroups %d" %(parameterDict['dbName'], parameterDict['vgroups'])) + + prepareEnvThread = threading.Thread(target=self.prepareEnv, kwargs=parameterDict) + prepareEnvThread.start() + + tdLog.info("create topics from db") + topicName1 = 'topic_db1' + + tdSql.execute("create topic %s as %s" %(topicName1, parameterDict['dbName'])) + + tdLog.info("create consume info table and consume result table") + cdbName = parameterDict["dbName"] + tdSql.query("create table %s.consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int)"%cdbName) + tdSql.query("create table %s.consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int)"%cdbName) + + consumerId = 0 + expectrowcnt = parameterDict["rowsPerTbl"] * parameterDict["ctbNum"] + topicList = topicName1 + ifcheckdata = 0 + keyList = 'group.id:cgrp1,\ + enable.auto.commit:false,\ + auto.commit.interval.ms:6000,\ + auto.offset.reset:earliest' + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + consumerId = 1 + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + event.wait() + + tdLog.info("start consume processor") + pollDelay = 5 + showMsg = 1 + showRow = 1 + self.startTmqSimProcess(buildPath,cfgPath,pollDelay,parameterDict["dbName"],showMsg, showRow, cdbName) + + # wait for data ready + prepareEnvThread.join() + + tdLog.info("insert process end, and start to check consume result") + while 1: + tdSql.query("select * from %s.consumeresult"%cdbName) + #tdLog.info("row: %d, %l64d, %l64d"%(tdSql.getData(0, 1),tdSql.getData(0, 2),tdSql.getData(0, 3)) + if tdSql.getRows() == 2: + break + else: + time.sleep(5) + + consumerId0 = tdSql.getData(0 , 1) + consumerId1 = tdSql.getData(1 , 1) + actConsumeRows0 = tdSql.getData(0 , 3) + actConsumeRows1 = tdSql.getData(1 , 3) + + tdLog.info("consumer %d rows: %d"%(consumerId0, actConsumeRows0)) + tdLog.info("consumer %d rows: %d"%(consumerId1, actConsumeRows1)) + + totalConsumeRows = actConsumeRows0 + actConsumeRows1 + if totalConsumeRows != expectrowcnt + 2: + tdLog.exit("tmq consume rows error!") + + tdSql.query("drop topic %s"%topicName1) + + tdLog.printNoPrefix("======== test case 2 end ...... ") + + def tmqCase3(self, cfgPath, buildPath): + tdLog.printNoPrefix("======== test case 3: Produce while one consumers to subscribe one db, include 2 stb") + tdLog.info("step 1: create database, stb, ctb and insert data") + # create and start thread + parameterDict = {'cfg': '', \ + 'dbName': 'db3', \ + 'vgroups': 4, \ + 'stbName': 'stb', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 100, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + tdSql.execute("create database if not exists %s vgroups %d" %(parameterDict['dbName'], parameterDict['vgroups'])) + + prepareEnvThread = threading.Thread(target=self.prepareEnv, kwargs=parameterDict) + prepareEnvThread.start() + + parameterDict2 = {'cfg': '', \ + 'dbName': 'db3', \ + 'vgroups': 4, \ + 'stbName': 'stb2', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 100, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + prepareEnvThread2 = threading.Thread(target=self.prepareEnv, kwargs=parameterDict2) + prepareEnvThread2.start() + + tdLog.info("create topics from db") + topicName1 = 'topic_db1' + + tdSql.execute("create topic %s as %s" %(topicName1, parameterDict['dbName'])) + + tdLog.info("create consume info table and consume result table") + cdbName = parameterDict["dbName"] + tdSql.query("create table %s.consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int)"%cdbName) + tdSql.query("create table %s.consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int)"%cdbName) + + consumerId = 0 + expectrowcnt = parameterDict["rowsPerTbl"] * parameterDict["ctbNum"] + parameterDict2["rowsPerTbl"] * parameterDict2["ctbNum"] + topicList = topicName1 + ifcheckdata = 0 + keyList = 'group.id:cgrp1,\ + enable.auto.commit:false,\ + auto.commit.interval.ms:6000,\ + auto.offset.reset:earliest' + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + # consumerId = 1 + # sql = "insert into %s.consumeinfo values "%cdbName + # sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + # tdSql.query(sql) + + event.wait() + + tdLog.info("start consume processor") + pollDelay = 5 + showMsg = 1 + showRow = 1 + self.startTmqSimProcess(buildPath,cfgPath,pollDelay,parameterDict["dbName"],showMsg, showRow, cdbName) + + # wait for data ready + prepareEnvThread.join() + prepareEnvThread2.join() + + tdLog.info("insert process end, and start to check consume result") + while 1: + tdSql.query("select * from %s.consumeresult"%cdbName) + #tdLog.info("row: %d, %l64d, %l64d"%(tdSql.getData(0, 1),tdSql.getData(0, 2),tdSql.getData(0, 3)) + if tdSql.getRows() == 1: + break + else: + time.sleep(5) + + consumerId0 = tdSql.getData(0 , 1) + #consumerId1 = tdSql.getData(1 , 1) + actConsumeRows0 = tdSql.getData(0 , 3) + #actConsumeRows1 = tdSql.getData(1 , 3) + + tdLog.info("consumer %d rows: %d"%(consumerId0, actConsumeRows0)) + #tdLog.info("consumer %d rows: %d"%(consumerId1, actConsumeRows1)) + + #totalConsumeRows = actConsumeRows0 + actConsumeRows1 + if actConsumeRows0 != expectrowcnt + 1: + tdLog.exit("tmq consume rows error!") + + tdSql.query("drop topic %s"%topicName1) + + tdLog.printNoPrefix("======== test case 3 end ...... ") + + def run(self): + tdSql.prepare() + + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + cfgPath = buildPath + "/../sim/psim/cfg" + tdLog.info("cfgPath: %s" % cfgPath) + + #self.tmqCase1(cfgPath, buildPath) + self.tmqCase2(cfgPath, buildPath) + #self.tmqCase3(cfgPath, buildPath) + + def stop(self): + tdSql.close() + tdLog.success(f"{__file__} successfully executed") + +event = threading.Event() + +tdCases.addLinux(__file__, TDTestCase()) +tdCases.addWindows(__file__, TDTestCase()) diff --git a/tests/system-test/99-TDcase/TD-15563.py b/tests/system-test/99-TDcase/TD-15563.py new file mode 100644 index 0000000000000000000000000000000000000000..b8d3abca5c1cb8828777ff434c82ec8f3b659e30 --- /dev/null +++ b/tests/system-test/99-TDcase/TD-15563.py @@ -0,0 +1,400 @@ + +import taos +import sys +import time +import socket +import os +import threading + +from util.log import * +from util.sql import * +from util.cases import * +from util.dnodes import * + +class TDTestCase: + hostname = socket.gethostname() + #rpcDebugFlagVal = '143' + #clientCfgDict = {'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''} + #clientCfgDict["rpcDebugFlag"] = rpcDebugFlagVal + #updatecfgDict = {'clientCfg': {}, 'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''} + #updatecfgDict["rpcDebugFlag"] = rpcDebugFlagVal + #print ("===================: ", updatecfgDict) + + def init(self, conn, logSql): + tdLog.debug(f"start to excute {__file__}") + #tdSql.init(conn.cursor()) + tdSql.init(conn.cursor(), logSql) # output sql.txt file + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root) - len("/build/bin")] + break + return buildPath + + def newcur(self,cfg,host,port): + user = "root" + password = "taosdata" + con=taos.connect(host=host, user=user, password=password, config=cfg ,port=port) + cur=con.cursor() + print(cur) + return cur + + def startTmqSimProcess(self,buildPath,cfgPath,pollDelay,dbName,showMsg,showRow,cdbName,valgrind=0): + shellCmd = 'nohup ' + if valgrind == 1: + logFile = cfgPath + '/../log/valgrind-tmq.log' + shellCmd = 'nohup valgrind --log-file=' + logFile + shellCmd += '--tool=memcheck --leak-check=full --show-reachable=no --track-origins=yes --show-leak-kinds=all --num-callers=20 -v --workaround-gcc296-bugs=yes ' + + shellCmd += buildPath + '/build/bin/tmq_sim -c ' + cfgPath + shellCmd += " -y %d -d %s -g %d -r %d -w %s "%(pollDelay, dbName, showMsg, showRow, cdbName) + shellCmd += "> /dev/null 2>&1 &" + tdLog.info(shellCmd) + os.system(shellCmd) + + def create_tables(self,tsql, dbName,vgroups,stbName,ctbNum,rowsPerTbl): + tsql.execute("create database if not exists %s vgroups %d"%(dbName, vgroups)) + tsql.execute("use %s" %dbName) + tsql.execute("create table if not exists %s (ts timestamp, c1 bigint, c2 binary(16)) tags(t1 int)"%stbName) + pre_create = "create table" + sql = pre_create + #tdLog.debug("doing create one stable %s and %d child table in %s ..." %(stbname, count ,dbname)) + for i in range(ctbNum): + sql += " %s_%d using %s tags(%d)"%(stbName,i,stbName,i+1) + if (i > 0) and (i%100 == 0): + tsql.execute(sql) + sql = pre_create + if sql != pre_create: + tsql.execute(sql) + + event.set() + tdLog.debug("complete to create database[%s], stable[%s] and %d child tables" %(dbName, stbName, ctbNum)) + return + + def insert_data(self,tsql,dbName,stbName,ctbNum,rowsPerTbl,batchNum,startTs): + tdLog.debug("start to insert data ............") + tsql.execute("use %s" %dbName) + pre_insert = "insert into " + sql = pre_insert + + #tdLog.debug("doing insert data into stable:%s rows:%d ..."%(stbName, allRows)) + for i in range(ctbNum): + sql += " %s_%d values "%(stbName,i) + for j in range(rowsPerTbl): + sql += "(%d, %d, 'tmqrow_%d') "%(startTs + j, j, j) + if (j > 0) and ((j%batchNum == 0) or (j == rowsPerTbl - 1)): + tsql.execute(sql) + if j < rowsPerTbl - 1: + sql = "insert into %s_%d values " %(stbName,i) + else: + sql = "insert into " + #end sql + if sql != pre_insert: + #print("insert sql:%s"%sql) + tsql.execute(sql) + tdLog.debug("insert data ............ [OK]") + return + + def prepareEnv(self, **parameterDict): + print ("input parameters:") + print (parameterDict) + # create new connector for my thread + tsql=self.newcur(parameterDict['cfg'], 'localhost', 6030) + self.create_tables(tsql,\ + parameterDict["dbName"],\ + parameterDict["vgroups"],\ + parameterDict["stbName"],\ + parameterDict["ctbNum"],\ + parameterDict["rowsPerTbl"]) + + self.insert_data(tsql,\ + parameterDict["dbName"],\ + parameterDict["stbName"],\ + parameterDict["ctbNum"],\ + parameterDict["rowsPerTbl"],\ + parameterDict["batchNum"],\ + parameterDict["startTs"]) + return + + def tmqCase1(self, cfgPath, buildPath): + tdLog.printNoPrefix("======== test case 1: Produce while one consume to subscribe one db") + tdLog.info("step 1: create database, stb, ctb and insert data") + # create and start thread + parameterDict = {'cfg': '', \ + 'dbName': 'db1', \ + 'vgroups': 4, \ + 'stbName': 'stb', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 200, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + tdSql.execute("create database if not exists %s vgroups %d" %(parameterDict['dbName'], parameterDict['vgroups'])) + + prepareEnvThread = threading.Thread(target=self.prepareEnv, kwargs=parameterDict) + prepareEnvThread.start() + + tdLog.info("create topics from db") + topicName1 = 'topic_db1' + + tdSql.execute("create topic %s as %s" %(topicName1, parameterDict['dbName'])) + + tdLog.info("create consume info table and consume result table") + cdbName = parameterDict["dbName"] + tdSql.query("create table %s.consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int)"%cdbName) + tdSql.query("create table %s.consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int)"%cdbName) + + consumerId = 0 + expectrowcnt = parameterDict["rowsPerTbl"] * parameterDict["ctbNum"] + topicList = topicName1 + ifcheckdata = 0 + keyList = 'group.id:cgrp1,\ + enable.auto.commit:false,\ + auto.commit.interval.ms:6000,\ + auto.offset.reset:earliest' + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + event.wait() + + tdLog.info("start consume processor") + pollDelay = 5 + showMsg = 1 + showRow = 1 + self.startTmqSimProcess(buildPath,cfgPath,pollDelay,parameterDict["dbName"],showMsg, showRow, cdbName) + + # wait for data ready + prepareEnvThread.join() + + tdLog.info("insert process end, and start to check consume result") + while 1: + tdSql.query("select * from %s.consumeresult"%cdbName) + #tdLog.info("row: %d, %l64d, %l64d"%(tdSql.getData(0, 1),tdSql.getData(0, 2),tdSql.getData(0, 3)) + if tdSql.getRows() == 1: + break + else: + time.sleep(5) + + tdLog.info("consumer result: %d, %d"%(tdSql.getData(0 , 2), tdSql.getData(0 , 3))) + tdSql.checkData(0 , 1, consumerId) + # mulit rows and mulit tables in one sql, this num of msg is not sure + #tdSql.checkData(0 , 2, expectmsgcnt) + tdSql.checkData(0 , 3, expectrowcnt+1) + + tdSql.query("drop topic %s"%topicName1) + + tdLog.printNoPrefix("======== test case 1 end ...... ") + + def tmqCase2(self, cfgPath, buildPath): + tdLog.printNoPrefix("======== test case 2: Produce while two consumers to subscribe one db") + tdLog.info("step 1: create database, stb, ctb and insert data") + # create and start thread + parameterDict = {'cfg': '', \ + 'dbName': 'db2', \ + 'vgroups': 4, \ + 'stbName': 'stb', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 100, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + tdSql.execute("create database if not exists %s vgroups %d" %(parameterDict['dbName'], parameterDict['vgroups'])) + + prepareEnvThread = threading.Thread(target=self.prepareEnv, kwargs=parameterDict) + prepareEnvThread.start() + + tdLog.info("create topics from db") + topicName1 = 'topic_db1' + + tdSql.execute("create topic %s as %s" %(topicName1, parameterDict['dbName'])) + + tdLog.info("create consume info table and consume result table") + cdbName = parameterDict["dbName"] + tdSql.query("create table %s.consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int)"%cdbName) + tdSql.query("create table %s.consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int)"%cdbName) + + consumerId = 0 + expectrowcnt = parameterDict["rowsPerTbl"] * parameterDict["ctbNum"] + topicList = topicName1 + ifcheckdata = 0 + keyList = 'group.id:cgrp1,\ + enable.auto.commit:false,\ + auto.commit.interval.ms:6000,\ + auto.offset.reset:earliest' + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + consumerId = 1 + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + event.wait() + + tdLog.info("start consume processor") + pollDelay = 5 + showMsg = 1 + showRow = 1 + self.startTmqSimProcess(buildPath,cfgPath,pollDelay,parameterDict["dbName"],showMsg, showRow, cdbName) + + # wait for data ready + prepareEnvThread.join() + + tdLog.info("insert process end, and start to check consume result") + while 1: + tdSql.query("select * from %s.consumeresult"%cdbName) + #tdLog.info("row: %d, %l64d, %l64d"%(tdSql.getData(0, 1),tdSql.getData(0, 2),tdSql.getData(0, 3)) + if tdSql.getRows() == 2: + break + else: + time.sleep(5) + + consumerId0 = tdSql.getData(0 , 1) + consumerId1 = tdSql.getData(1 , 1) + actConsumeRows0 = tdSql.getData(0 , 3) + actConsumeRows1 = tdSql.getData(1 , 3) + + tdLog.info("consumer %d rows: %d"%(consumerId0, actConsumeRows0)) + tdLog.info("consumer %d rows: %d"%(consumerId1, actConsumeRows1)) + + totalConsumeRows = actConsumeRows0 + actConsumeRows1 + if totalConsumeRows != expectrowcnt + 2: + tdLog.exit("tmq consume rows error!") + + tdSql.query("drop topic %s"%topicName1) + + tdLog.printNoPrefix("======== test case 2 end ...... ") + + def tmqCase3(self, cfgPath, buildPath): + tdLog.printNoPrefix("======== test case 3: Produce while one consumers to subscribe one db, include 2 stb") + tdLog.info("step 1: create database, stb, ctb and insert data") + # create and start thread + parameterDict = {'cfg': '', \ + 'dbName': 'db3', \ + 'vgroups': 4, \ + 'stbName': 'stb', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 100, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + tdSql.execute("create database if not exists %s vgroups %d" %(parameterDict['dbName'], parameterDict['vgroups'])) + + prepareEnvThread = threading.Thread(target=self.prepareEnv, kwargs=parameterDict) + prepareEnvThread.start() + + parameterDict2 = {'cfg': '', \ + 'dbName': 'db3', \ + 'vgroups': 4, \ + 'stbName': 'stb2', \ + 'ctbNum': 10, \ + 'rowsPerTbl': 100000, \ + 'batchNum': 100, \ + 'startTs': 1640966400000} # 2022-01-01 00:00:00.000 + parameterDict['cfg'] = cfgPath + + prepareEnvThread2 = threading.Thread(target=self.prepareEnv, kwargs=parameterDict2) + prepareEnvThread2.start() + + tdLog.info("create topics from db") + topicName1 = 'topic_db1' + + tdSql.execute("create topic %s as %s" %(topicName1, parameterDict['dbName'])) + + tdLog.info("create consume info table and consume result table") + cdbName = parameterDict["dbName"] + tdSql.query("create table %s.consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int)"%cdbName) + tdSql.query("create table %s.consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int)"%cdbName) + + consumerId = 0 + expectrowcnt = parameterDict["rowsPerTbl"] * parameterDict["ctbNum"] + parameterDict2["rowsPerTbl"] * parameterDict2["ctbNum"] + topicList = topicName1 + ifcheckdata = 0 + keyList = 'group.id:cgrp1,\ + enable.auto.commit:false,\ + auto.commit.interval.ms:6000,\ + auto.offset.reset:earliest' + sql = "insert into %s.consumeinfo values "%cdbName + sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + tdSql.query(sql) + + # consumerId = 1 + # sql = "insert into %s.consumeinfo values "%cdbName + # sql += "(now, %d, '%s', '%s', %d, %d)"%(consumerId, topicList, keyList, expectrowcnt, ifcheckdata) + # tdSql.query(sql) + + event.wait() + + tdLog.info("start consume processor") + pollDelay = 5 + showMsg = 1 + showRow = 1 + self.startTmqSimProcess(buildPath,cfgPath,pollDelay,parameterDict["dbName"],showMsg, showRow, cdbName) + + # wait for data ready + prepareEnvThread.join() + prepareEnvThread2.join() + + tdLog.info("insert process end, and start to check consume result") + while 1: + tdSql.query("select * from %s.consumeresult"%cdbName) + #tdLog.info("row: %d, %l64d, %l64d"%(tdSql.getData(0, 1),tdSql.getData(0, 2),tdSql.getData(0, 3)) + if tdSql.getRows() == 1: + break + else: + time.sleep(5) + + consumerId0 = tdSql.getData(0 , 1) + #consumerId1 = tdSql.getData(1 , 1) + actConsumeRows0 = tdSql.getData(0 , 3) + #actConsumeRows1 = tdSql.getData(1 , 3) + + tdLog.info("consumer %d rows: %d"%(consumerId0, actConsumeRows0)) + #tdLog.info("consumer %d rows: %d"%(consumerId1, actConsumeRows1)) + + #totalConsumeRows = actConsumeRows0 + actConsumeRows1 + if actConsumeRows0 != expectrowcnt + 1: + tdLog.exit("tmq consume rows error!") + + tdSql.query("drop topic %s"%topicName1) + + tdLog.printNoPrefix("======== test case 3 end ...... ") + + def run(self): + tdSql.prepare() + + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + cfgPath = buildPath + "/../sim/psim/cfg" + tdLog.info("cfgPath: %s" % cfgPath) + + #self.tmqCase1(cfgPath, buildPath) + self.tmqCase2(cfgPath, buildPath) + #self.tmqCase3(cfgPath, buildPath) + + def stop(self): + tdSql.close() + tdLog.success(f"{__file__} successfully executed") + +event = threading.Event() + +tdCases.addLinux(__file__, TDTestCase()) +tdCases.addWindows(__file__, TDTestCase()) diff --git a/tests/system-test/fulltest.sh b/tests/system-test/fulltest.sh index c80206abbcb13b0420da9e7d3dcb4f65471b9ab4..4b64f1044a479e733df40c38af6358bd2d255b8f 100755 --- a/tests/system-test/fulltest.sh +++ b/tests/system-test/fulltest.sh @@ -54,4 +54,4 @@ python3 ./test.py -f 2-query/arccos.py python3 ./test.py -f 2-query/arctan.py # python3 ./test.py -f 2-query/query_cols_tags_and_or.py -python3 ./test.py -f 7-tmq/basic5.py +#python3 ./test.py -f 7-tmq/basic5.py diff --git a/tests/test/c/tmqSim.c b/tests/test/c/tmqSim.c index 33ddd23d8c407995fffc9ba07278dc6f2de28332..1228d6174c0e1ff672cc7d6fc6cb64729b50b280 100644 --- a/tests/test/c/tmqSim.c +++ b/tests/test/c/tmqSim.c @@ -179,7 +179,7 @@ void parseArgument(int32_t argc, char* argv[]) { } else if (strcmp(argv[i], "-y") == 0) { g_stConfInfo.consumeDelay = atol(argv[++i]); } else { - printf("%s unknow para: %s %s", GREEN, argv[++i], NC); + pError("%s unknow para: %s %s", GREEN, argv[++i], NC); exit(-1); } } @@ -259,7 +259,7 @@ int queryDB(TAOS* taos, char* command) { } static void tmq_commit_cb_print(tmq_t* tmq, tmq_resp_err_t resp, tmq_topic_vgroup_list_t* offsets, void* param) { - printf("tmq_commit_cb_print() commit %d\n", resp); + pError("tmq_commit_cb_print() commit %d\n", resp); } void build_consumer(SThreadInfo* pInfo) { @@ -318,7 +318,7 @@ int32_t saveConsumeResult(SThreadInfo* pInfo) { TAOS_RES* pRes = taos_query(pConn, sqlStr); if (taos_errno(pRes) != 0) { - printf("error in save consumeinfo, reason:%s\n", taos_errstr(pRes)); + pError("error in save consumeinfo, reason:%s\n", taos_errstr(pRes)); taos_free_result(pRes); exit(-1); } @@ -375,7 +375,7 @@ void* consumeThreadFunc(void* param) { tmq_resp_err_t err = tmq_subscribe(pInfo->tmq, pInfo->topicList); if (err) { - printf("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err)); + pError("tmq_subscribe() fail, reason: %s\n", tmq_err2str(err)); exit(-1); } @@ -388,14 +388,14 @@ void* consumeThreadFunc(void* param) { err = tmq_unsubscribe(pInfo->tmq); if (err) { - printf("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err)); + pError("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err)); pInfo->consumeMsgCnt = -1; return NULL; } err = tmq_consumer_close(pInfo->tmq); if (err) { - printf("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err)); + pError("tmq_consumer_close() fail, reason: %s\n", tmq_err2str(err)); exit(-1); } pInfo->tmq = NULL; @@ -451,7 +451,7 @@ int32_t getConsumeInfo() { sprintf(sqlStr, "select * from %s.consumeinfo", g_stConfInfo.cdbName); TAOS_RES* pRes = taos_query(pConn, sqlStr); if (taos_errno(pRes) != 0) { - printf("error in get consumeinfo, reason:%s\n", taos_errstr(pRes)); + pError("error in get consumeinfo, reason:%s\n", taos_errstr(pRes)); taosFprintfFile(g_fp, "error in get consumeinfo, reason:%s\n", taos_errstr(pRes)); taosCloseFile(&g_fp); taos_free_result(pRes); diff --git a/tools/shell/src/shellCommand.c b/tools/shell/src/shellCommand.c index f4f7c893c43166fe5d0ca03725122dc20b6665fe..ef71f3fce6f8b21f09f85ed373233cc5714e6316 100644 --- a/tools/shell/src/shellCommand.c +++ b/tools/shell/src/shellCommand.c @@ -53,79 +53,6 @@ static void shellResetCommand(SShellCmd *cmd, const char s[]); static void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos); static void shellShowOnScreen(SShellCmd *cmd); -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) -// static void shellPrintContinuePrompt() { printf("%s", shell.args.promptContinue); } -// static void shellPrintPrompt() { printf("%s", shell.args.promptHeader); } - -void shellUpdateBuffer(SShellCmd *cmd) { - if (shellRegexMatch(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); - strcat(cmd->buffer, cmd->command); - - memset(cmd->command, 0, SHELL_MAX_COMMAND_SIZE); - cmd->cursorOffset = 0; -} - -int shellIsReadyGo(SShellCmd *cmd) { - char *total = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE); - memset(total, 0, SHELL_MAX_COMMAND_SIZE); - sprintf(total, "%s%s", cmd->buffer, cmd->command); - - char *reg_str = - "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^" - "\\s*clear\\s*$)"; - if (shellRegexMatch(total, reg_str, REG_EXTENDED | REG_ICASE)) { - taosMemoryFree(total); - return 1; - } - - taosMemoryFree(total); - return 0; -} - -void shellInsertChar(SShellCmd *cmd, char c) { - if (cmd->cursorOffset >= SHELL_MAX_COMMAND_SIZE) { - fprintf(stdout, "sql is larger than %d bytes", SHELL_MAX_COMMAND_SIZE); - return; - } - cmd->command[cmd->cursorOffset++] = c; -} - -int32_t shellReadCommand(char command[]) { - SShellCmd cmd; - memset(&cmd, 0, sizeof(cmd)); - cmd.buffer = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE); - cmd.command = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE); - - // Read input. - char c; - while (1) { - c = getchar(); - - switch (c) { - case '\n': - case '\r': - if (shellIsReadyGo(&cmd)) { - sprintf(command, "%s%s", cmd.buffer, cmd.command); - taosMemoryFree(cmd.buffer); - cmd.buffer = NULL; - taosMemoryFree(cmd.command); - cmd.command = NULL; - return 0; - } else { - // shellPrintContinuePrompt(); - shellUpdateBuffer(&cmd); - } - break; - default: - shellInsertChar(&cmd, c); - } - } - - return 0; -} - -#else - int32_t shellCountPrefixOnes(uint8_t c) { uint8_t mask = 127; mask = ~mask; @@ -181,7 +108,10 @@ void shellInsertChar(SShellCmd *cmd, char *c, int32_t size) { cmd->cursorOffset += size; cmd->screenOffset += taosWcharWidth(wc); cmd->endOffset += taosWcharWidth(wc); +#ifdef WINDOWS +#else shellShowOnScreen(cmd); +#endif } void shellBackspaceChar(SShellCmd *cmd) { @@ -371,17 +301,33 @@ void shellResetCommand(SShellCmd *cmd, const char s[]) { shellShowOnScreen(cmd); } -void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) { + +void shellGetScreenSize(int32_t *ws_col, int32_t *ws_row) { +#ifdef WINDOWS + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + if (ws_col != NULL) *ws_col = csbi.srWindow.Right - csbi.srWindow.Left + 1; + if (ws_row != NULL) *ws_row = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; +#else struct winsize w; if (ioctl(0, TIOCGWINSZ, &w) < 0 || w.ws_col == 0 || w.ws_row == 0) { // fprintf(stderr, "No stream device, and use default value(col 120, row 30)\n"); - w.ws_col = 120; - w.ws_row = 30; + if (ws_col != NULL) *ws_col = 120; + if (ws_row != NULL) *ws_row = 30; + } else { + if (ws_col != NULL) *ws_col = w.ws_col; + if (ws_row != NULL) *ws_row = w.ws_row; } +#endif +} + +void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) { + int32_t ws_col; + shellGetScreenSize(&ws_col, NULL); - int32_t cursor_x = cursor_pos / w.ws_col; - int32_t cursor_y = cursor_pos % w.ws_col; - int32_t command_x = ecmd_pos / w.ws_col; + int32_t cursor_x = cursor_pos / ws_col; + int32_t cursor_y = cursor_pos % ws_col; + int32_t command_x = ecmd_pos / ws_col; shellPositionCursor(cursor_y, LEFT); shellPositionCursor(command_x - cursor_x, DOWN); fprintf(stdout, "\033[2K"); @@ -393,12 +339,8 @@ void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) { } void shellShowOnScreen(SShellCmd *cmd) { - struct winsize w; - if (ioctl(0, TIOCGWINSZ, &w) < 0 || w.ws_col == 0 || w.ws_row == 0) { - // fprintf(stderr, "No stream device\n"); - w.ws_col = 120; - w.ws_row = 30; - } + int32_t ws_col; + shellGetScreenSize(&ws_col, NULL); TdWchar wc; int32_t size = 0; @@ -411,8 +353,7 @@ void shellShowOnScreen(SShellCmd *cmd) { } else { sprintf(total_string, "%s%s", shell.info.promptContinue, cmd->command); } - - int32_t remain_column = w.ws_col; + int32_t remain_column = ws_col; for (char *str = total_string; size < cmd->commandSize + PSIZE;) { int32_t ret = taosMbToWchar(&wc, str, MB_CUR_MAX); if (ret < 0) break; @@ -425,10 +366,10 @@ void shellShowOnScreen(SShellCmd *cmd) { } else { if (remain_column == width) { printf("%lc\n\r", wc); - remain_column = w.ws_col; + remain_column = ws_col; } else { printf("\n\r%lc", wc); - remain_column = w.ws_col - width; + remain_column = ws_col - width; } } @@ -436,17 +377,16 @@ void shellShowOnScreen(SShellCmd *cmd) { } taosMemoryFree(total_string); - // Position the cursor int32_t cursor_pos = cmd->screenOffset + PSIZE; int32_t ecmd_pos = cmd->endOffset + PSIZE; - int32_t cursor_x = cursor_pos / w.ws_col; - int32_t cursor_y = cursor_pos % w.ws_col; - // int32_t cursor_y = cursor % w.ws_col; - int32_t command_x = ecmd_pos / w.ws_col; - int32_t command_y = ecmd_pos % w.ws_col; - // int32_t command_y = (command.size() + PSIZE) % w.ws_col; + int32_t cursor_x = cursor_pos / ws_col; + int32_t cursor_y = cursor_pos % ws_col; + // int32_t cursor_y = cursor % ws_col; + int32_t command_x = ecmd_pos / ws_col; + int32_t command_y = ecmd_pos % ws_col; + // int32_t command_y = (command.size() + PSIZE) % ws_col; shellPositionCursor(command_y, LEFT); shellPositionCursor(command_x, UP); shellPositionCursor(cursor_x, DOWN); @@ -490,7 +430,11 @@ int32_t shellReadCommand(char *command) { case 3: printf("\n"); shellResetCommand(&cmd, ""); - kill(0, SIGINT); + #ifdef WINDOWS + raise(SIGINT); + #else + kill(0, SIGINT); + #endif break; case 4: // EOF or Ctrl+D printf("\n"); @@ -503,7 +447,10 @@ int32_t shellReadCommand(char *command) { break; case '\n': case '\r': + #ifdef WINDOWS + #else printf("\n"); + #endif if (shellIsReadyGo(&cmd)) { sprintf(command, "%s%s", cmd.buffer, cmd.command); taosMemoryFreeClear(cmd.buffer); @@ -608,5 +555,3 @@ int32_t shellReadCommand(char *command) { return 0; } - -#endif \ No newline at end of file diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index 8b126cf90ee32f68299dc95ad2444803995f83c1..1e832c0c46b95dd3732efbdf296326bfdac43146 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -307,7 +307,7 @@ void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, i break; case TSDB_DATA_TYPE_DOUBLE: n = snprintf(buf, TSDB_MAX_BYTES_PER_ROW, "%*.9f", length, GET_DOUBLE_VAL(val)); - if (n > MAX(25, length)) { + if (n > TMAX(25, length)) { taosFprintfFile(pFile, "%*.15e", length, GET_DOUBLE_VAL(val)); } else { taosFprintfFile(pFile, "%s", buf); @@ -488,7 +488,7 @@ void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t break; case TSDB_DATA_TYPE_DOUBLE: n = snprintf(buf, TSDB_MAX_BYTES_PER_ROW, "%*.9f", width, GET_DOUBLE_VAL(val)); - if (n > MAX(25, width)) { + if (n > TMAX(25, width)) { printf("%*.15e", width, GET_DOUBLE_VAL(val)); } else { printf("%s", buf); diff --git a/tools/shell/src/shellNettest.c b/tools/shell/src/shellNettest.c index 345b85d896c92c3b28a0e6cc78f9ce775a03bd4a..3355c20109fbfabbbf3ce50184235829f0cb1eb7 100644 --- a/tools/shell/src/shellNettest.c +++ b/tools/shell/src/shellNettest.c @@ -119,7 +119,7 @@ static void shellWorkAsServer() { memcpy(rpcInit.localFqdn, tsLocalFqdn, strlen(tsLocalFqdn)); rpcInit.localPort = pArgs->port; rpcInit.label = "CHK"; - rpcInit.numOfThreads = tsNumOfRpcThreads; + rpcInit.numOfThreads = 2; rpcInit.cfp = (RpcCfp)shellProcessMsg; rpcInit.sessions = 10; rpcInit.connType = TAOS_CONN_SERVER; diff --git a/tools/taos-tools b/tools/taos-tools index 0ae9f872c26d5da8cb61aa9eb00b5c7aeba10ec4..0aad27d725f4ee6b18daf1db0c07d933aed16eea 160000 --- a/tools/taos-tools +++ b/tools/taos-tools @@ -1 +1 @@ -Subproject commit 0ae9f872c26d5da8cb61aa9eb00b5c7aeba10ec4 +Subproject commit 0aad27d725f4ee6b18daf1db0c07d933aed16eea