From d3857cbc558ac44e547b267ffda71825d5b9d66a Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Thu, 21 Apr 2022 21:37:33 +0800 Subject: [PATCH 01/51] register rsma info --- include/common/tmsg.h | 4 +- source/dnode/vnode/src/inc/tsdbSma.h | 33 ++++++ source/dnode/vnode/src/inc/vnodeInt.h | 2 + source/dnode/vnode/src/tsdb/tsdbSma.c | 160 ++++++++++++++++++++++---- source/dnode/vnode/src/vnd/vnodeSvr.c | 10 +- 5 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 source/dnode/vnode/src/inc/tsdbSma.h diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 38308a1293..aa7b466328 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1483,8 +1483,8 @@ typedef struct { int32_t qmsg1Len; int32_t qmsg2Len; func_id_t* pFuncIds; - char* qmsg1; // not null: pAst1:qmsg1:SRetention1 => trigger aggr task1 - char* qmsg2; // not null: pAst2:qmsg2:SRetention2 => trigger aggr task2 + char* qmsg1; // pAst1:qmsg1:SRetention1 => trigger aggr task1 + char* qmsg2; // pAst2:qmsg2:SRetention2 => trigger aggr task2 int8_t nFuncIds; } SRSmaParam; diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h new file mode 100644 index 0000000000..a178b2e4c0 --- /dev/null +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -0,0 +1,33 @@ +/* + * 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_VNODE_TSDB_SMA_H_ +#define _TD_VNODE_TSDB_SMA_H_ + +#include "os.h" +#include "thash.h" +#include "tmsg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_VNODE_TSDB_SMA_H_*/ \ No newline at end of file diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 0ff1408c91..23a6d97a66 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -113,6 +113,8 @@ void smaHandleRes(void* pVnode, int64_t smaId, const SArray* data); #include "tq.h" +#include "tsdbSma.h" + #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 273b7447ff..51b492b297 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -33,6 +33,8 @@ static const char *TSDB_SMA_DNAME[] = { #define SMA_TEST_INDEX_NAME "smaTestIndexName" // TODO: just for test #define SMA_TEST_INDEX_UID 2000000001 // TODO: just for test + +typedef struct SRSmaInfo SRSmaInfo; typedef enum { SMA_STORAGE_LEVEL_TSDB = 0, // use days of self-defined e.g. vnode${N}/tsdb/tsma/sma_index_uid/v2f200.tsma SMA_STORAGE_LEVEL_DFILESET = 1 // use days of TS data e.g. vnode${N}/tsdb/tsma/sma_index_uid/v2f1906.tsma @@ -98,9 +100,38 @@ typedef struct { } SSmaStatItem; struct SSmaStat { - SHashObj *smaStatItems; // key: indexUid, value: SSmaStatItem + union { + SHashObj *smaStatItems; // key: indexUid, value: SSmaStatItem for tsma + SHashObj *rsmaInfoHash; // key: stbUid, value: SRSmaInfo; + }; + T_REF_DECLARE() }; +#define SSMA_STAT_ITEMS(s) ((s)->smaStatItems) +#define SSMA_STAT_INFO_HASH(s) ((s)->rsmaInfoHash) + +#define RSMA_MAX_LEVEL 2 +#define RSMA_TASK_INFO_HASH_SLOT 8 + struct SRSmaInfo { + void *taskInfo[RSMA_MAX_LEVEL]; // qTaskInfo_t +}; + +static FORCE_INLINE void tsdbFreeTaskHandle(qTaskInfo_t *taskHandle) { + // Note: free/kill may in RC + qTaskInfo_t otaskHandle = atomic_load_ptr(taskHandle); + if (otaskHandle && atomic_val_compare_exchange_ptr(taskHandle, otaskHandle, NULL)) { + qDestroyTask(otaskHandle); + } +} + +static FORCE_INLINE void *tsdbFreeRSmaInfo(SRSmaInfo *pInfo) { + for (int32_t i = 0; i < RSMA_MAX_LEVEL; ++i) { + if (pInfo->taskInfo[i]) { + tsdbFreeTaskHandle(pInfo->taskInfo[i]); + } + } + return NULL; +} // declaration of static functions @@ -108,11 +139,11 @@ struct SSmaStat { static int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t version); static int32_t tsdbSetExpiredWindow(STsdb *pTsdb, SHashObj *pItemsHash, int64_t indexUid, int64_t winSKey, int64_t version); -static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat); +static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType); static void *tsdbFreeSmaStatItem(SSmaStatItem *pSmaStatItem); static int32_t tsdbDestroySmaState(SSmaStat *pSmaStat); -static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, const char *path, SDiskID did); -static int32_t tsdbInitSmaEnv(STsdb *pTsdb, const char *path, SDiskID did, SSmaEnv **pEnv); +static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *path, SDiskID did); +static int32_t tsdbInitSmaEnv(STsdb *pTsdb, int8_t smaType, const char *path, SDiskID did, SSmaEnv **pEnv); static int32_t tsdbResetExpiredWindow(STsdb *pTsdb, SSmaStat *pStat, int64_t indexUid, TSKEY skey); static int32_t tsdbRefSmaStat(STsdb *pTsdb, SSmaStat *pStat); static int32_t tsdbUnRefSmaStat(STsdb *pTsdb, SSmaStat *pStat); @@ -317,7 +348,7 @@ static void tsdbGetSmaDir(int32_t vgId, ETsdbSmaType smaType, char dirName[]) { snprintf(dirName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s", TD_DIRSEP, vgId, TD_DIRSEP, TSDB_SMA_DNAME[smaType]); } -static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, const char *path, SDiskID did) { +static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *path, SDiskID did) { SSmaEnv *pEnv = NULL; pEnv = (SSmaEnv *)taosMemoryCalloc(1, sizeof(SSmaEnv)); @@ -342,7 +373,7 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, const char *path, SDiskID did) pEnv->did = did; - if (tsdbInitSmaStat(&pEnv->pStat) != TSDB_CODE_SUCCESS) { + if (tsdbInitSmaStat(&pEnv->pStat, smaType) != TSDB_CODE_SUCCESS) { tsdbFreeSmaEnv(pEnv); return NULL; } @@ -362,14 +393,14 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, const char *path, SDiskID did) return pEnv; } -static int32_t tsdbInitSmaEnv(STsdb *pTsdb, const char *path, SDiskID did, SSmaEnv **pEnv) { +static int32_t tsdbInitSmaEnv(STsdb *pTsdb, int8_t smaType, const char *path, SDiskID did, SSmaEnv **pEnv) { if (!pEnv) { terrno = TSDB_CODE_INVALID_PTR; return TSDB_CODE_FAILED; } if (*pEnv == NULL) { - if ((*pEnv = tsdbNewSmaEnv(pTsdb, path, did)) == NULL) { + if ((*pEnv = tsdbNewSmaEnv(pTsdb, smaType, path, did)) == NULL) { return TSDB_CODE_FAILED; } } @@ -416,7 +447,7 @@ static int32_t tsdbUnRefSmaStat(STsdb *pTsdb, SSmaStat *pStat) { return 0; } -static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat) { +static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType) { ASSERT(pSmaStat != NULL); if (*pSmaStat != NULL) { // no lock @@ -435,12 +466,24 @@ static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat) { return TSDB_CODE_FAILED; } - (*pSmaStat)->smaStatItems = - taosHashInit(SMA_STATE_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); + if (smaType == TSDB_SMA_TYPE_ROLLUP) { + SSMA_STAT_INFO_HASH(*pSmaStat) = taosHashInit( + RSMA_TASK_INFO_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK); - if ((*pSmaStat)->smaStatItems == NULL) { - taosMemoryFreeClear(*pSmaStat); - return TSDB_CODE_FAILED; + if (SSMA_STAT_INFO_HASH(*pSmaStat) == NULL) { + taosMemoryFreeClear(*pSmaStat); + return TSDB_CODE_FAILED; + } + } else if (smaType == TSDB_SMA_TYPE_TIME_RANGE) { + SSMA_STAT_ITEMS(*pSmaStat) = + taosHashInit(SMA_STATE_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); + + if (SSMA_STAT_ITEMS(*pSmaStat) == NULL) { + taosMemoryFreeClear(*pSmaStat); + return TSDB_CODE_FAILED; + } + } else { + ASSERT(0); } } return TSDB_CODE_SUCCESS; @@ -531,7 +574,7 @@ static int32_t tsdbCheckAndInitSmaEnv(STsdb *pTsdb, int8_t smaType) { return TSDB_CODE_FAILED; } - if (tsdbInitSmaEnv(pTsdb, rname, did, &pEnv) != TSDB_CODE_SUCCESS) { + if (tsdbInitSmaEnv(pTsdb, smaType, rname, did, &pEnv) != TSDB_CODE_SUCCESS) { tsdbUnlockRepo(pTsdb); return TSDB_CODE_FAILED; } @@ -1290,12 +1333,6 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, const char *msg) { return terrno; } - if (pEnv == NULL) { - terrno = TSDB_CODE_INVALID_PTR; - tsdbWarn("vgId:%d insert rSma data failed since pTSmaEnv is NULL", REPO_ID(pTsdb)); - return terrno; - } - if (pDataBlocks == NULL) { terrno = TSDB_CODE_INVALID_PTR; tsdbWarn("vgId:%d insert rSma data failed since pDataBlocks is NULL", REPO_ID(pTsdb)); @@ -1633,6 +1670,87 @@ int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg) { return TSDB_CODE_SUCCESS; } +/** + * @brief Only applicable to stable. + * + * @param pTsdb + * @param pMeta + * @param pReq + * @return int32_t + */ +int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { + SRSmaParam *param = pReq->stbCfg.pRSmaParam; + + if (param == NULL) { + tsdbDebug("vgId:%d return directly since no rollup for stable %s %" PRIi64, REPO_ID(pTsdb), pReq->name, + pReq->stbCfg.suid); + return TSDB_CODE_SUCCESS; + } + + if ((param->qmsg1Len == 0) && (param->qmsg2Len == 0)) { + tsdbWarn("vgId:%d no qmsg1/qmsg2 for rollup stable %s %" PRIi64, REPO_ID(pTsdb), pReq->name, pReq->stbCfg.suid); + return TSDB_CODE_SUCCESS; + } + + if (tsdbCheckAndInitSmaEnv(pTsdb, TSDB_SMA_TYPE_ROLLUP) != TSDB_CODE_SUCCESS) { + terrno = TSDB_CODE_TDB_INIT_FAILED; + return TSDB_CODE_FAILED; + } + + SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); + SSmaStat *pStat = SMA_ENV_STAT(pEnv); + SRSmaInfo *pRSmaInfo = NULL; + + TASSERT(pEnv != NULL && pStat != NULL); + + pRSmaInfo = taosHashGet(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t)); + if (pRSmaInfo != NULL) { + pRSmaInfo = tsdbFreeRSmaInfo(pRSmaInfo); + } + + STqReadHandle *pReadHandle = tqInitSubmitMsgScanner(pMeta); + if (pReadHandle == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + + pRSmaInfo = (SRSmaInfo *)taosMemoryCalloc(1, sizeof(SRSmaInfo)); + if (pRSmaInfo == NULL) { + taosMemoryFree(pReadHandle); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + + SReadHandle handle = { + .reader = pReadHandle, + .meta = pMeta, + }; + + if (param->qmsg1) { + pRSmaInfo->taskInfo[0] = qCreateStreamExecTaskInfo(param->qmsg1, &handle); + if (pRSmaInfo->taskInfo[0] == NULL) { + taosMemoryFree(pRSmaInfo); + taosMemoryFree(pReadHandle); + return TSDB_CODE_FAILED; + } + } + + if (param->qmsg2) { + pRSmaInfo->taskInfo[1] = qCreateStreamExecTaskInfo(param->qmsg2, &handle); + if (pRSmaInfo->taskInfo[1] == NULL) { + taosMemoryFree(pRSmaInfo); + taosMemoryFree(pReadHandle); + return TSDB_CODE_FAILED; + } + } + + if (taosHashPut(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != 0) { + return TSDB_CODE_FAILED; + } + + return TSDB_CODE_SUCCESS; +} + #if 0 /** * @brief Get the start TS key of the last data block of one interval/sliding. diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 1c3a3787f3..4b570c8045 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -215,14 +215,8 @@ static int vnodeProcessCreateStbReq(SVnode *pVnode, void *pReq) { return -1; } - // TODO: remove the debug log - SRSmaParam *param = vCreateTbReq.stbCfg.pRSmaParam; - if (param) { - printf("qmsg1 len = %d, body = %s\n", param->qmsg1 ? (int32_t)strlen(param->qmsg1) : 0, - param->qmsg1 ? param->qmsg1 : ""); - printf("qmsg1 len = %d, body = %s\n", param->qmsg2 ? (int32_t)strlen(param->qmsg2) : 0, - param->qmsg2 ? param->qmsg2 : ""); - } + // deploy Rollup SMA + tsdbRegisterRSma(pVnode->pTsdb, pVnode->pMeta, &vCreateTbReq); taosMemoryFree(vCreateTbReq.stbCfg.pSchema); taosMemoryFree(vCreateTbReq.stbCfg.pTagSchema); From 1ec6fa12f603bab900c87dd7be3e7127b0725c68 Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Thu, 21 Apr 2022 23:33:47 +0800 Subject: [PATCH 02/51] add release method for rsma --- source/dnode/vnode/src/tsdb/tsdbMain.c | 4 +-- source/dnode/vnode/src/tsdb/tsdbSma.c | 44 ++++++++++++++++++-------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMain.c b/source/dnode/vnode/src/tsdb/tsdbMain.c index 2753579e9e..8691920c8f 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMain.c +++ b/source/dnode/vnode/src/tsdb/tsdbMain.c @@ -81,8 +81,8 @@ static STsdb *tsdbNew(const char *path, SVnode *pVnode, const STsdbCfg *pTsdbCfg static void tsdbFree(STsdb *pTsdb) { if (pTsdb) { - // tsdbFreeSmaEnv(REPO_TSMA_ENV(pTsdb)); - // tsdbFreeSmaEnv(REPO_RSMA_ENV(pTsdb)); + tsdbFreeSmaEnv(REPO_TSMA_ENV(pTsdb)); + tsdbFreeSmaEnv(REPO_RSMA_ENV(pTsdb)); tsdbFreeFS(pTsdb->fs); taosMemoryFreeClear(pTsdb->path); taosMemoryFree(pTsdb); diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 51b492b297..03ad5c10a5 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -48,6 +48,7 @@ typedef struct SPoolMem { struct SSmaEnv { TdThreadRwlock lock; + int8_t type; TXN txn; SPoolMem *pPool; SDiskID did; @@ -57,6 +58,7 @@ struct SSmaEnv { }; #define SMA_ENV_LOCK(env) ((env)->lock) +#define SMA_ENV_TYPE(env) ((env)->type) #define SMA_ENV_DID(env) ((env)->did) #define SMA_ENV_ENV(env) ((env)->dbEnv) #define SMA_ENV_PATH(env) ((env)->path) @@ -141,7 +143,7 @@ static int32_t tsdbSetExpiredWindow(STsdb *pTsdb, SHashObj *pItemsHash, int64_t int64_t version); static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType); static void *tsdbFreeSmaStatItem(SSmaStatItem *pSmaStatItem); -static int32_t tsdbDestroySmaState(SSmaStat *pSmaStat); +static int32_t tsdbDestroySmaState(SSmaStat *pSmaStat, int8_t smaType); static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *path, SDiskID did); static int32_t tsdbInitSmaEnv(STsdb *pTsdb, int8_t smaType, const char *path, SDiskID did, SSmaEnv **pEnv); static int32_t tsdbResetExpiredWindow(STsdb *pTsdb, SSmaStat *pStat, int64_t indexUid, TSKEY skey); @@ -357,6 +359,8 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *pa return NULL; } + SMA_ENV_TYPE(pEnv) = smaType; + int code = taosThreadRwlockInit(&(pEnv->lock), NULL); if (code) { terrno = TAOS_SYSTEM_ERROR(code); @@ -365,15 +369,15 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *pa } ASSERT(path && (strlen(path) > 0)); - pEnv->path = strdup(path); - if (pEnv->path == NULL) { + SMA_ENV_PATH(pEnv) = strdup(path); + if (SMA_ENV_PATH(pEnv) == NULL) { tsdbFreeSmaEnv(pEnv); return NULL; } - pEnv->did = did; + SMA_ENV_DID(pEnv) = did; - if (tsdbInitSmaStat(&pEnv->pStat, smaType) != TSDB_CODE_SUCCESS) { + if (tsdbInitSmaStat(&SMA_ENV_STAT(pEnv), smaType) != TSDB_CODE_SUCCESS) { tsdbFreeSmaEnv(pEnv); return NULL; } @@ -416,7 +420,7 @@ static int32_t tsdbInitSmaEnv(STsdb *pTsdb, int8_t smaType, const char *path, SD */ void tsdbDestroySmaEnv(SSmaEnv *pSmaEnv) { if (pSmaEnv) { - tsdbDestroySmaState(pSmaEnv->pStat); + tsdbDestroySmaState(pSmaEnv->pStat, SMA_ENV_TYPE(pSmaEnv)); taosMemoryFreeClear(pSmaEnv->pStat); taosMemoryFreeClear(pSmaEnv->path); taosThreadRwlockDestroy(&(pSmaEnv->lock)); @@ -520,16 +524,28 @@ static void *tsdbFreeSmaStatItem(SSmaStatItem *pSmaStatItem) { * @param pSmaStat * @return int32_t */ -int32_t tsdbDestroySmaState(SSmaStat *pSmaStat) { +int32_t tsdbDestroySmaState(SSmaStat *pSmaStat, int8_t smaType) { if (pSmaStat) { // TODO: use taosHashSetFreeFp when taosHashSetFreeFp is ready. - void *item = taosHashIterate(pSmaStat->smaStatItems, NULL); - while (item != NULL) { - SSmaStatItem *pItem = *(SSmaStatItem **)item; - tsdbFreeSmaStatItem(pItem); - item = taosHashIterate(pSmaStat->smaStatItems, item); + if (smaType == TSDB_SMA_TYPE_TIME_RANGE) { + void *item = taosHashIterate(SSMA_STAT_ITEMS(pSmaStat), NULL); + while (item != NULL) { + SSmaStatItem *pItem = *(SSmaStatItem **)item; + tsdbFreeSmaStatItem(pItem); + item = taosHashIterate(SSMA_STAT_ITEMS(pSmaStat), item); + } + taosHashCleanup(SSMA_STAT_ITEMS(pSmaStat)); + } else if (smaType == TSDB_SMA_TYPE_ROLLUP) { + void *infoHash = taosHashIterate(SSMA_STAT_INFO_HASH(pSmaStat), NULL); + while (infoHash != NULL) { + SRSmaInfo *pInfoHash = *(SRSmaInfo **)infoHash; + tsdbFreeRSmaInfo(pInfoHash); + infoHash = taosHashIterate(SSMA_STAT_INFO_HASH(pSmaStat), infoHash); + } + taosHashCleanup(SSMA_STAT_INFO_HASH(pSmaStat)); + } else { + ASSERT(0); } - taosHashCleanup(pSmaStat->smaStatItems); } return TSDB_CODE_SUCCESS; } @@ -1744,7 +1760,7 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { } } - if (taosHashPut(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != 0) { + if (taosHashPut(SSMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != 0) { return TSDB_CODE_FAILED; } From 2d328c8930908c622534646820816462214c10f3 Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Thu, 21 Apr 2022 23:55:20 +0800 Subject: [PATCH 03/51] refactor --- source/dnode/vnode/src/inc/tsdbSma.h | 1 + source/dnode/vnode/src/tsdb/tsdbSma.c | 41 ++++++++++++++------------- source/dnode/vnode/src/vnd/vnodeSvr.c | 1 - 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index a178b2e4c0..322ba215ad 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -25,6 +25,7 @@ extern "C" { #endif int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq); +int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, SSubmitReq *pMsg); #ifdef __cplusplus } diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 03ad5c10a5..34d41b1c79 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -109,12 +109,12 @@ struct SSmaStat { T_REF_DECLARE() }; -#define SSMA_STAT_ITEMS(s) ((s)->smaStatItems) -#define SSMA_STAT_INFO_HASH(s) ((s)->rsmaInfoHash) +#define SMA_STAT_ITEMS(s) ((s)->smaStatItems) +#define SMA_STAT_INFO_HASH(s) ((s)->rsmaInfoHash) #define RSMA_MAX_LEVEL 2 #define RSMA_TASK_INFO_HASH_SLOT 8 - struct SRSmaInfo { +struct SRSmaInfo { void *taskInfo[RSMA_MAX_LEVEL]; // qTaskInfo_t }; @@ -471,18 +471,18 @@ static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType) { } if (smaType == TSDB_SMA_TYPE_ROLLUP) { - SSMA_STAT_INFO_HASH(*pSmaStat) = taosHashInit( + SMA_STAT_INFO_HASH(*pSmaStat) = taosHashInit( RSMA_TASK_INFO_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK); - if (SSMA_STAT_INFO_HASH(*pSmaStat) == NULL) { + if (SMA_STAT_INFO_HASH(*pSmaStat) == NULL) { taosMemoryFreeClear(*pSmaStat); return TSDB_CODE_FAILED; } } else if (smaType == TSDB_SMA_TYPE_TIME_RANGE) { - SSMA_STAT_ITEMS(*pSmaStat) = + SMA_STAT_ITEMS(*pSmaStat) = taosHashInit(SMA_STATE_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); - if (SSMA_STAT_ITEMS(*pSmaStat) == NULL) { + if (SMA_STAT_ITEMS(*pSmaStat) == NULL) { taosMemoryFreeClear(*pSmaStat); return TSDB_CODE_FAILED; } @@ -528,21 +528,21 @@ int32_t tsdbDestroySmaState(SSmaStat *pSmaStat, int8_t smaType) { if (pSmaStat) { // TODO: use taosHashSetFreeFp when taosHashSetFreeFp is ready. if (smaType == TSDB_SMA_TYPE_TIME_RANGE) { - void *item = taosHashIterate(SSMA_STAT_ITEMS(pSmaStat), NULL); + void *item = taosHashIterate(SMA_STAT_ITEMS(pSmaStat), NULL); while (item != NULL) { SSmaStatItem *pItem = *(SSmaStatItem **)item; tsdbFreeSmaStatItem(pItem); - item = taosHashIterate(SSMA_STAT_ITEMS(pSmaStat), item); + item = taosHashIterate(SMA_STAT_ITEMS(pSmaStat), item); } - taosHashCleanup(SSMA_STAT_ITEMS(pSmaStat)); + taosHashCleanup(SMA_STAT_ITEMS(pSmaStat)); } else if (smaType == TSDB_SMA_TYPE_ROLLUP) { - void *infoHash = taosHashIterate(SSMA_STAT_INFO_HASH(pSmaStat), NULL); + void *infoHash = taosHashIterate(SMA_STAT_INFO_HASH(pSmaStat), NULL); while (infoHash != NULL) { SRSmaInfo *pInfoHash = *(SRSmaInfo **)infoHash; tsdbFreeRSmaInfo(pInfoHash); - infoHash = taosHashIterate(SSMA_STAT_INFO_HASH(pSmaStat), infoHash); + infoHash = taosHashIterate(SMA_STAT_INFO_HASH(pSmaStat), infoHash); } - taosHashCleanup(SSMA_STAT_INFO_HASH(pSmaStat)); + taosHashCleanup(SMA_STAT_INFO_HASH(pSmaStat)); } else { ASSERT(0); } @@ -1687,7 +1687,7 @@ int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg) { } /** - * @brief Only applicable to stable. + * @brief Check and init qTaskInfo_t, only applicable to stable with SRSmaParam. * * @param pTsdb * @param pMeta @@ -1724,15 +1724,15 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { pRSmaInfo = tsdbFreeRSmaInfo(pRSmaInfo); } - STqReadHandle *pReadHandle = tqInitSubmitMsgScanner(pMeta); - if (pReadHandle == NULL) { + pRSmaInfo = (SRSmaInfo *)taosMemoryCalloc(1, sizeof(SRSmaInfo)); + if (pRSmaInfo == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_FAILED; } - pRSmaInfo = (SRSmaInfo *)taosMemoryCalloc(1, sizeof(SRSmaInfo)); - if (pRSmaInfo == NULL) { - taosMemoryFree(pReadHandle); + STqReadHandle *pReadHandle = tqInitSubmitMsgScanner(pMeta); + if (pReadHandle == NULL) { + taosMemoryFree(pRSmaInfo); terrno = TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_FAILED; } @@ -1760,7 +1760,8 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { } } - if (taosHashPut(SSMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != 0) { + if (taosHashPut(SMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != + 0) { return TSDB_CODE_FAILED; } diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 4b570c8045..a81bbf4298 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -215,7 +215,6 @@ static int vnodeProcessCreateStbReq(SVnode *pVnode, void *pReq) { return -1; } - // deploy Rollup SMA tsdbRegisterRSma(pVnode->pTsdb, pVnode->pMeta, &vCreateTbReq); taosMemoryFree(vCreateTbReq.stbCfg.pSchema); From 4a8db28554fdc6fc444748013b900356414aa712 Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Fri, 22 Apr 2022 00:19:50 +0800 Subject: [PATCH 04/51] trigger CI From 978b369e8bd7afcfdb0880184c533558ec9dab86 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 22 Apr 2022 22:40:19 +0800 Subject: [PATCH 05/51] fix(query): set timestamp window for the returned datablock. --- source/libs/executor/src/executorimpl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 4a0206f55e..9c437d3f88 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -3861,6 +3861,7 @@ int32_t setSDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadI } pRes->info.rows = numOfRows; + blockDataUpdateTsWindow(pRes); int64_t el = taosGetTimestampUs() - startTs; From 4b36763429b83275c6bc473d098ee6486eabe26f Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 22 Apr 2022 22:43:07 +0800 Subject: [PATCH 06/51] refactor(query): do some internal refactor. --- include/libs/function/function.h | 21 +-- source/client/src/clientImpl.c | 4 +- source/libs/executor/src/executorimpl.c | 26 ++-- source/libs/function/inc/builtinsimpl.h | 8 +- source/libs/function/src/builtinsimpl.c | 193 ++++++++++++++++++------ 5 files changed, 179 insertions(+), 73 deletions(-) diff --git a/include/libs/function/function.h b/include/libs/function/function.h index 004d834287..ed8d54cef8 100644 --- a/include/libs/function/function.h +++ b/include/libs/function/function.h @@ -37,7 +37,7 @@ typedef struct SFuncExecEnv { typedef bool (*FExecGetEnv)(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); typedef bool (*FExecInit)(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo* pResultCellInfo); typedef int32_t (*FExecProcess)(struct SqlFunctionCtx *pCtx); -typedef int32_t (*FExecFinalize)(struct SqlFunctionCtx *pCtx, SSDataBlock* pBlock, int32_t slotId); +typedef int32_t (*FExecFinalize)(struct SqlFunctionCtx *pCtx, SSDataBlock* pBlock); typedef int32_t (*FScalarExecProcess)(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput); typedef struct SScalarFuncExecFuncs { @@ -141,8 +141,7 @@ struct SResultRowEntryInfo; //for selectivity query, the corresponding tag value is assigned if the data is qualified typedef struct SSubsidiaryResInfo { - int16_t bufLen; // keep the tags data for top/bottom query result - int16_t numOfCols; + int16_t num; struct SqlFunctionCtx **pCtx; } SSubsidiaryResInfo; @@ -187,8 +186,8 @@ typedef struct SqlFunctionCtx { uint8_t currentStage; // record current running step, default: 0 bool isAggSet; int64_t startTs; // timestamp range of current query when function is executed on a specific data block, TODO remove it - ///////////////////////////////////////////////////////////////// bool stableQuery; + ///////////////////////////////////////////////////////////////// int16_t functionId; // function id char * pOutput; // final result output buffer, point to sdata->data int32_t numOfParams; @@ -198,11 +197,15 @@ typedef struct SqlFunctionCtx { int32_t offset; SVariant tag; struct SResultRowEntryInfo *resultInfo; - SSubsidiaryResInfo subsidiaryRes; - SPoint1 start; - SPoint1 end; - SFuncExecFuncs fpSet; - SScalarFuncExecFuncs sfp; + SSubsidiaryResInfo subsidiaries; + SPoint1 start; + SPoint1 end; + SFuncExecFuncs fpSet; + SScalarFuncExecFuncs sfp; + SExprInfo *pExpr; + struct SDiskbasedBuf *pBuf; + struct SSDataBlock *pSrcBlock; + int32_t curBufPage; } SqlFunctionCtx; enum { diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index aae9303bdc..8fcd16a622 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -105,9 +105,9 @@ TAOS* taos_connect_internal(const char* ip, const char* user, const char* pass, epSet.epSet.eps[0].port = port; } - char* key = getClusterKey(user, secretEncrypt, ip, port); - SAppInstInfo** pInst = NULL; + char* key = getClusterKey(user, secretEncrypt, ip, port); + SAppInstInfo** pInst = NULL; taosThreadMutexLock(&appInfo.mutex); pInst = taosHashGet(appInfo.pInstMap, key, strlen(key)); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 9c437d3f88..b28dadb594 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1082,8 +1082,9 @@ static int32_t doSetInputDataBlock(SOperatorInfo* pOperator, SqlFunctionCtx* pCt int32_t code = TSDB_CODE_SUCCESS; for (int32_t i = 0; i < pOperator->numOfOutput; ++i) { - pCtx[i].order = order; - pCtx[i].size = pBlock->info.rows; + pCtx[i].order = order; + pCtx[i].size = pBlock->info.rows; + pCtx[i].pSrcBlock = pBlock; pCtx[i].currentStage = MAIN_SCAN; SInputColumnInfoData* pInput = &pCtx[i].input; @@ -1836,9 +1837,8 @@ static int32_t setCtxTagColumnInfo(SqlFunctionCtx* pCtx, int32_t numOfOutput) { } } if (p != NULL) { - p->subsidiaryRes.pCtx = pTagCtx; - p->subsidiaryRes.numOfCols = num; - p->subsidiaryRes.bufLen = tagLen; + p->subsidiaries.pCtx = pTagCtx; + p->subsidiaries.num = num; } else { taosMemoryFreeClear(pTagCtx); } @@ -1865,6 +1865,9 @@ SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, SqlFunctionCtx* pCtx = &pFuncCtx[i]; pCtx->functionId = -1; + pCtx->curBufPage = -1; + pCtx->pExpr = pExpr; + if (pExpr->pExpr->nodeType == QUERY_NODE_FUNCTION) { SFuncExecEnv env = {0}; pCtx->functionId = pExpr->pExpr->_function.pFunctNode->funcId; @@ -1892,9 +1895,9 @@ SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, pCtx->pTsOutput = NULL; pCtx->resDataInfo.bytes = pFunct->resSchema.bytes; pCtx->resDataInfo.type = pFunct->resSchema.type; - pCtx->order = TSDB_ORDER_ASC; + pCtx->order = TSDB_ORDER_ASC; pCtx->start.key = INT64_MIN; - pCtx->end.key = INT64_MIN; + pCtx->end.key = INT64_MIN; pCtx->numOfParams = pExpr->base.numOfParams; pCtx->param = pFunct->pParam; @@ -1953,7 +1956,7 @@ static void* destroySqlFunctionCtx(SqlFunctionCtx* pCtx, int32_t numOfOutput) { } taosVariantDestroy(&pCtx[i].tag); - taosMemoryFreeClear(pCtx[i].subsidiaryRes.pCtx); + taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx); } taosMemoryFreeClear(pCtx); @@ -3139,7 +3142,7 @@ int32_t doCopyToSDataBlock(SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbased pCtx[j].resultInfo = getResultCell(pRow, j, rowCellOffset); if (pCtx[j].fpSet.process) { - pCtx[j].fpSet.finalize(&pCtx[j], pBlock, slotId); + pCtx[j].fpSet.finalize(&pCtx[j], pBlock); } else { SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId); @@ -5633,6 +5636,11 @@ int32_t initAggInfo(SOptrBasicInfo* pBasicInfo, SAggSupporter* pAggSup, SExprInf pBasicInfo->pRes = pResultBlock; doInitAggInfoSup(pAggSup, pBasicInfo->pCtx, numOfCols, keyBufSize, pkey); + + for(int32_t i = 0; i < numOfCols; ++i) { + pBasicInfo->pCtx[i].pBuf = pAggSup->pResultBuf; + } + return TSDB_CODE_SUCCESS; } diff --git a/source/libs/function/inc/builtinsimpl.h b/source/libs/function/inc/builtinsimpl.h index db92740fff..e2b6d9c392 100644 --- a/source/libs/function/inc/builtinsimpl.h +++ b/source/libs/function/inc/builtinsimpl.h @@ -24,7 +24,7 @@ extern "C" { #include "functionMgt.h" bool functionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); -int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); +int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow); bool getCountFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); @@ -43,12 +43,12 @@ int32_t maxFunction(SqlFunctionCtx *pCtx); bool getStddevFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool stddevFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t stddevFunction(SqlFunctionCtx* pCtx); -int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); +int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getPercentileFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool percentileFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t percentileFunction(SqlFunctionCtx *pCtx); -int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); +int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getDiffFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool diffFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResInfo); @@ -60,7 +60,7 @@ int32_t lastFunction(SqlFunctionCtx *pCtx); bool getTopBotFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv); int32_t topFunction(SqlFunctionCtx *pCtx); -int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); +int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); #ifdef __cplusplus } diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 3dc2e62e92..f5f1b38e35 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -14,7 +14,7 @@ */ #include "builtinsimpl.h" -#include +#include "function.h" #include "querynodes.h" #include "taggfunction.h" #include "tdatablock.h" @@ -49,7 +49,8 @@ bool functionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo) { return true; } -int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { +int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { + int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); @@ -303,16 +304,16 @@ bool getMinmaxFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { } \ } while (0); -#define DO_UPDATE_SUBSID_RES(ctx, ts) \ - do { \ - for (int32_t _i = 0; _i < (ctx)->subsidiaryRes.numOfCols; ++_i) { \ - SqlFunctionCtx *__ctx = (ctx)->subsidiaryRes.pCtx[_i]; \ - if (__ctx->functionId == FUNCTION_TS_DUMMY) { \ - __ctx->tag.i = (ts); \ - __ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; \ - } \ - __ctx->fpSet.process(__ctx); \ - } \ +#define DO_UPDATE_SUBSID_RES(ctx, ts) \ + do { \ + for (int32_t _i = 0; _i < (ctx)->subsidiaries.num; ++_i) { \ + SqlFunctionCtx* __ctx = (ctx)->subsidiaries.pCtx[_i]; \ + if (__ctx->functionId == FUNCTION_TS_DUMMY) { \ + __ctx->tag.i = (ts); \ + __ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; \ + } \ + __ctx->fpSet.process(__ctx); \ + } \ } while (0) #define UPDATE_DATA(ctx, left, right, num, sign, _ts) \ @@ -378,8 +379,8 @@ int32_t doMinMaxHelper(SqlFunctionCtx *pCtx, int32_t isMinFunc) { int64_t val = GET_INT64_VAL(tval); if ((prev < val) ^ isMinFunc) { *(int64_t*) buf = val; - for (int32_t i = 0; i < (pCtx)->subsidiaryRes.numOfCols; ++i) { - SqlFunctionCtx* __ctx = pCtx->subsidiaryRes.pCtx[i]; + for (int32_t i = 0; i < (pCtx)->subsidiaries.num; ++i) { + SqlFunctionCtx* __ctx = pCtx->subsidiaries.pCtx[i]; if (__ctx->functionId == FUNCTION_TS_DUMMY) { // TODO refactor __ctx->tag.i = key; __ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; @@ -395,8 +396,8 @@ int32_t doMinMaxHelper(SqlFunctionCtx *pCtx, int32_t isMinFunc) { uint64_t val = GET_UINT64_VAL(tval); if ((prev < val) ^ isMinFunc) { *(uint64_t*) buf = val; - for (int32_t i = 0; i < (pCtx)->subsidiaryRes.numOfCols; ++i) { - SqlFunctionCtx* __ctx = pCtx->subsidiaryRes.pCtx[i]; + for (int32_t i = 0; i < (pCtx)->subsidiaries.num; ++i) { + SqlFunctionCtx* __ctx = pCtx->subsidiaries.pCtx[i]; if (__ctx->functionId == FUNCTION_TS_DUMMY) { // TODO refactor __ctx->tag.i = key; __ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; @@ -618,11 +619,11 @@ int32_t stddevFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; } -int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { +int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SStddevRes* pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); double avg = pStddevRes->isum / ((double) pStddevRes->count); pStddevRes->result = sqrt(pStddevRes->quadraticISum/((double)pStddevRes->count) - avg*avg); - return functionFinalize(pCtx, pBlock, slotId); + return functionFinalize(pCtx, pBlock); } typedef struct SPercentileInfo { @@ -744,7 +745,7 @@ int32_t percentileFunction(SqlFunctionCtx *pCtx) { return TSDB_CODE_SUCCESS; } -int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { +int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SVariant* pVal = &pCtx->param[1].param; double v = pVal->nType == TSDB_DATA_TYPE_INT ? pVal->i : pVal->d; @@ -757,7 +758,7 @@ int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t sl } tMemBucketDestroy(pMemBucket); - return functionFinalize(pCtx, pBlock, slotId); + return functionFinalize(pCtx, pBlock); } bool getFirstLastFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { @@ -1178,7 +1179,7 @@ typedef struct STopBotResItem { } STopBotResItem; typedef struct STopBotRes { - int32_t pageId; +// int32_t pageId; // int32_t num; STopBotResItem *pItems; } STopBotRes; @@ -1197,15 +1198,16 @@ static STopBotRes *getTopBotOutputInfo(SqlFunctionCtx *pCtx) { return pRes; } -static void doAddIntoResult(STopBotRes* pRes, int32_t maxSize, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, +static void doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo); +static void saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STopBotResItem* pItem); +static void copyTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STopBotResItem* pItem); + int32_t topFunction(SqlFunctionCtx *pCtx) { int32_t numOfElems = 0; SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx); - STopBotRes *pRes = getTopBotOutputInfo(pCtx); - // if ((void *)pRes->res[0] != (void *)((char *)pRes + sizeof(STopBotRes) + POINTER_BYTES * pCtx->param[0].i)) { // buildTopBotStruct(pRes, pCtx); // } @@ -1225,7 +1227,7 @@ int32_t topFunction(SqlFunctionCtx *pCtx) { numOfElems++; char* data = colDataGetData(pCol, i); - doAddIntoResult(pRes, pCtx->param[1].param.i, data, i, NULL, type, pInput->uid, pResInfo); + doAddIntoResult(pCtx, data, i, pCtx->pSrcBlock, type, pInput->uid, pResInfo); } return TSDB_CODE_SUCCESS; @@ -1258,9 +1260,11 @@ static int32_t topBotResComparFn(const void *p1, const void *p2, const void *par return (val1->v.d > val2->v.d) ? 1 : -1; } +void doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type, + uint64_t uid, SResultRowEntryInfo* pEntryInfo) { + STopBotRes *pRes = getTopBotOutputInfo(pCtx); + int32_t maxSize = pCtx->param[1].param.i; -void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type, - uint64_t uid, SResultRowEntryInfo* pEntryInfo) { SVariant val = {0}; taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type); @@ -1272,22 +1276,9 @@ void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, int32_t row STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes]; pItem->v = val; pItem->uid = uid; - pItem->tuplePos.pageId = -1; // todo set the corresponding tuple data in the disk-based buffer - if (pRes->pageId == -1) { - SFilePage* pPage = getNewBufPage(NULL, 0, &pRes->pageId); - pPage->num = sizeof(SFilePage); - - // keep the current row data - for(int32_t i = 0; i < pSrcBlock->info.numOfCols; ++i) { - SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, i); - bool isNull = colDataIsNull_s(pCol, rowIndex); - - - colDataGetData(pCol, rowIndex); - } - - } + // save the data of this tuple + saveTupleData(pCtx, rowIndex, pSrcBlock, pItem); // allocate the buffer and keep the data of this row into the new allocated buffer pEntryInfo->numOfRes++; @@ -1296,22 +1287,100 @@ void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, int32_t row if ((IS_SIGNED_NUMERIC_TYPE(type) && val.i > pItems[0].v.i) || (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) || (IS_FLOAT_TYPE(type) && val.d > pItems[0].v.d)) { + // replace the old data and the coresponding tuple data STopBotResItem* pItem = &pItems[0]; pItem->v = val; pItem->uid = uid; - pItem->tuplePos.pageId = -1; // todo set the corresponding tuple data in the disk-based buffer + + // save the data of this tuple by over writing the old data + copyTupleData(pCtx, rowIndex, pSrcBlock, pItem); taosheapadjust((void *) pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void *) &type, topBotResComparFn, NULL, false); } } } -int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { +void saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STopBotResItem* pItem) { + SFilePage* pPage = NULL; + + int32_t completeRowSize = pSrcBlock->info.rowSize + pSrcBlock->info.numOfCols * sizeof(bool); + + if (pCtx->curBufPage == -1) { + pPage = getNewBufPage(pCtx->pBuf, 0, &pCtx->curBufPage); + pPage->num = sizeof(SFilePage); + } else { + pPage = getBufPage(pCtx->pBuf, pCtx->curBufPage); + if (pPage->num + completeRowSize > getBufPageSize(pCtx->pBuf)) { + pPage = getNewBufPage(pCtx->pBuf, 0, &pCtx->curBufPage); + pPage->num = sizeof(SFilePage); + } + } + + pItem->tuplePos.pageId = pCtx->curBufPage; + + // keep the current row data, extract method + int32_t offset = 0; + bool* nullList = (bool*)((char*)pPage + pPage->num); + char* pStart = (char*)(nullList + sizeof(bool) * pSrcBlock->info.numOfCols); + for (int32_t i = 0; i < pSrcBlock->info.numOfCols; ++i) { + SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, i); + bool isNull = colDataIsNull_s(pCol, rowIndex); + if (isNull) { + nullList[i] = true; + continue; + } + + char* p = colDataGetData(pCol, rowIndex); + if (IS_VAR_DATA_TYPE(pCol->info.type)) { + memcpy(pStart + offset, p, varDataTLen(p)); + } else { + memcpy(pStart + offset, p, pCol->info.bytes); + } + + offset += pCol->info.bytes; + } + + pItem->tuplePos.offset = pPage->num; + pPage->num += completeRowSize; + + setBufPageDirty(pPage, true); + releaseBufPage(pCtx->pBuf, pPage); +} + +void copyTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STopBotResItem* pItem) { + SFilePage* pPage = getBufPage(pCtx->pBuf, pItem->tuplePos.pageId); + + bool* nullList = (bool*)((char*)pPage + pItem->tuplePos.offset); + char* pStart = (char*)(nullList + pSrcBlock->info.numOfCols * sizeof(bool)); + + int32_t offset = 0; + for(int32_t i = 0; i < pSrcBlock->info.numOfCols; ++i) { + SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, i); + if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) { + continue; + } + + char* p = colDataGetData(pCol, rowIndex); + if (IS_VAR_DATA_TYPE(pCol->info.type)) { + memcpy(pStart + offset, p, varDataTLen(p)); + } else { + memcpy(pStart + offset, p, pCol->info.bytes); + } + + offset += pCol->info.bytes; + } + + setBufPageDirty(pPage, true); + releaseBufPage(pCtx->pBuf, pPage); +} + +int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SResultRowEntryInfo *pEntryInfo = GET_RES_INFO(pCtx); STopBotRes* pRes = GET_ROWCELL_INTERBUF(pEntryInfo); pEntryInfo->complete = true; int32_t type = pCtx->input.pData[0]->info.type; + int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); // todo assign the tag value and the corresponding row data @@ -1320,19 +1389,45 @@ int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId case TSDB_DATA_TYPE_INT: { for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) { STopBotResItem* pItem = &pRes->pItems[i]; - colDataAppendInt32(pCol, currentRow++, (int32_t*)&pItem->v.i); + colDataAppendInt32(pCol, currentRow, (int32_t*)&pItem->v.i); int32_t pageId = pItem->tuplePos.pageId; int32_t offset = pItem->tuplePos.offset; - if (pageId != -1) { - // todo + if (pItem->tuplePos.pageId != -1) { + SFilePage* pPage = getBufPage(pCtx->pBuf, pageId); + + bool* nullList = (bool*)((char*)pPage + offset); + char* pStart = (char*)(nullList + pCtx->pSrcBlock->info.numOfCols * sizeof(bool)); + + // todo set the offset value to optimize the performance. + for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) { + SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j]; + + SFunctParam *pFuncParam = &pc->pExpr->base.pParam[0]; + int32_t srcSlotId = pFuncParam->pCol->slotId; + int32_t dstSlotId = pCtx->pExpr->base.resSchema.slotId; + + int32_t ps = 0; + for(int32_t k = 0; k < srcSlotId; ++k) { + SColumnInfoData* pSrcCol = taosArrayGet(pCtx->pSrcBlock->pDataBlock, k); + ps += pSrcCol->info.bytes; + } + + SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId); + if (nullList[srcSlotId]) { + colDataAppendNULL(pDstCol, currentRow); + } else { + colDataAppend(pDstCol, currentRow, (pStart + ps), false); + } + } } + + currentRow += 1; } + break; } } return pEntryInfo->numOfRes; - -// return functionFinalize(pCtx, pBlock, slotId); } \ No newline at end of file From fb45d0d9356eec2646877900733da2e0253d0a9e Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Sat, 23 Apr 2022 19:15:20 +0800 Subject: [PATCH 07/51] roll up sma generate --- include/libs/executor/executor.h | 4 +- source/dnode/mnode/impl/src/mndStb.c | 1 - source/dnode/vnode/inc/vnode.h | 1 + source/dnode/vnode/src/inc/meta.h | 4 +- source/dnode/vnode/src/inc/tsdbSma.h | 31 +++- source/dnode/vnode/src/inc/vnodeInt.h | 2 + source/dnode/vnode/src/meta/metaTDBImpl.c | 8 +- source/dnode/vnode/src/meta/metaTable.c | 4 +- source/dnode/vnode/src/tq/tq.c | 4 +- source/dnode/vnode/src/tq/tqRead.c | 16 ++ source/dnode/vnode/src/tsdb/tsdbSma.c | 176 ++++++++++++++++++++-- source/dnode/vnode/src/vnd/vnodeSvr.c | 15 +- source/libs/executor/src/executor.c | 21 ++- source/libs/stream/src/tstream.c | 4 +- 14 files changed, 253 insertions(+), 38 deletions(-) diff --git a/include/libs/executor/executor.h b/include/libs/executor/executor.h index 4d289147d0..04f67b584f 100644 --- a/include/libs/executor/executor.h +++ b/include/libs/executor/executor.h @@ -57,7 +57,7 @@ qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, void* streamReadHandle); * @param type * @return */ -int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type); +int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool converted); /** * Set multiple input data blocks for the stream scan. @@ -67,7 +67,7 @@ int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type); * @param type * @return */ -int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type); +int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type, bool converted); /** * Update the table id list, add or remove. diff --git a/source/dnode/mnode/impl/src/mndStb.c b/source/dnode/mnode/impl/src/mndStb.c index f304e3153d..1e63dd9833 100644 --- a/source/dnode/mnode/impl/src/mndStb.c +++ b/source/dnode/mnode/impl/src/mndStb.c @@ -453,7 +453,6 @@ static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pSt } } if (pStb->ast2Len > 0) { - int32_t qmsgLen2 = 0; if (mndConvertRSmaTask(pStb->pAst2, 0, 0, &pRSmaParam->qmsg2, &pRSmaParam->qmsg2Len) != TSDB_CODE_SUCCESS) { taosMemoryFreeClear(pRSmaParam->pFuncIds); taosMemoryFreeClear(pRSmaParam->qmsg1); diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 2b52d333da..9f80f0beff 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -108,6 +108,7 @@ void tqReadHandleSetColIdList(STqReadHandle *pReadHandle, SArray *pColIdList) int tqReadHandleSetTbUidList(STqReadHandle *pHandle, const SArray *tbUidList); int tqReadHandleAddTbUidList(STqReadHandle *pHandle, const SArray *tbUidList); int32_t tqReadHandleSetMsg(STqReadHandle *pHandle, SSubmitReq *pMsg, int64_t ver); +int32_t tqReadHandleSetMsgEx(STqReadHandle *pHandle, SSubmitReq *pMsg, int64_t ver); bool tqNextDataBlock(STqReadHandle *pHandle); int32_t tqRetrieveDataBlock(SArray **ppCols, STqReadHandle *pHandle, uint64_t *pGroupId, int32_t *pNumOfRows); diff --git a/source/dnode/vnode/src/inc/meta.h b/source/dnode/vnode/src/inc/meta.h index fb875a46e0..2d417fd626 100644 --- a/source/dnode/vnode/src/inc/meta.h +++ b/source/dnode/vnode/src/inc/meta.h @@ -51,7 +51,7 @@ static FORCE_INLINE tb_uid_t metaGenerateUid(SMeta* pMeta) { return tGenIdPI64() #define META_CHILD_TABLE TD_CHILD_TABLE #define META_NORMAL_TABLE TD_NORMAL_TABLE -int metaCreateTable(SMeta* pMeta, STbCfg* pTbCfg); +int metaCreateTable(SMeta* pMeta, STbCfg* pTbCfg, STbDdlHandle *pHandle); int metaDropTable(SMeta* pMeta, tb_uid_t uid); int metaCommit(SMeta* pMeta); int32_t metaCreateTSma(SMeta* pMeta, SSmaCfg* pCfg); @@ -74,7 +74,7 @@ tb_uid_t metaCtbCursorNext(SMCtbCursor* pCtbCur); // SMetaDB int metaOpenDB(SMeta* pMeta); void metaCloseDB(SMeta* pMeta); -int metaSaveTableToDB(SMeta* pMeta, STbCfg* pTbCfg); +int metaSaveTableToDB(SMeta* pMeta, STbCfg* pTbCfg, STbDdlHandle *pHandle); int metaRemoveTableFromDb(SMeta* pMeta, tb_uid_t uid); int metaSaveSmaToDB(SMeta* pMeta, STSma* pTbCfg); int metaRemoveSmaFromDb(SMeta* pMeta, int64_t indexUid); diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index 322ba215ad..3aad11a6b5 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -24,8 +24,37 @@ extern "C" { #endif +typedef int32_t (*__tb_ddl_fn_t)(void *ahandle, void **result, void *p1, void *p2); + +struct STbDdlHandle { + void *ahandle; + void *result; + __tb_ddl_fn_t fp; +}; + +typedef struct { + tb_uid_t suid; + SArray *tbUids; + SHashObj *uidHash; +} STbUidStore; + +static FORCE_INLINE int32_t tsdbUidStoreInit(STbUidStore **pStore) { + ASSERT(*pStore == NULL); + *pStore = taosMemoryCalloc(1, sizeof(STbUidStore)); + if (*pStore == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + return TSDB_CODE_SUCCESS; +} + +int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t uid); +void *tsdbUidStoreFree(STbUidStore *pStore); + int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq); -int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, SSubmitReq *pMsg); +int32_t tsdbFetchTbUidList(void *pTsdb, void **result, void *suid, void *uid); +int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pUidStore); +int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType); #ifdef __cplusplus } diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 23a6d97a66..5566681dc0 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -102,6 +102,8 @@ struct SVnode { #define TD_VID(PVNODE) (PVNODE)->config.vgId +typedef struct STbDdlHandle STbDdlHandle; + // sma void smaHandleRes(void* pVnode, int64_t smaId, const SArray* data); diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 149a7ca3cb..96e2a34b7b 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -248,7 +248,7 @@ void metaCloseDB(SMeta *pMeta) { } } -int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { +int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg, STbDdlHandle *pHandle) { tb_uid_t uid; SMetaDB *pMetaDb; void *pKey; @@ -347,6 +347,12 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { if (ret < 0) { return -1; } + // child table handle for rsma + if (pHandle && pHandle->fp) { + if (((*pHandle->fp)(pHandle->ahandle, &pHandle->result, &ctbIdxKey.suid, &uid)) < 0) { + return -1; + }; + } } else if (pTbCfg->type == META_NORMAL_TABLE) { pKey = &uid; kLen = sizeof(uid); diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index 7f06ba8855..32e7c24b64 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -15,7 +15,7 @@ #include "vnodeInt.h" -int metaCreateTable(SMeta *pMeta, STbCfg *pTbCfg) { +int metaCreateTable(SMeta *pMeta, STbCfg *pTbCfg, STbDdlHandle *pHandle) { // Validate the tbOptions // if (metaValidateTbCfg(pMeta, pTbCfg) < 0) { // // TODO: handle error @@ -24,7 +24,7 @@ int metaCreateTable(SMeta *pMeta, STbCfg *pTbCfg) { // TODO: add atomicity - if (metaSaveTableToDB(pMeta, pTbCfg) < 0) { + if (metaSaveTableToDB(pMeta, pTbCfg, pHandle) < 0) { // TODO: handle error return -1; } diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index 510dd32459..1d693ddff5 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -287,7 +287,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { SSubmitReq* pCont = (SSubmitReq*)&pHead->body; qTaskInfo_t task = pExec->task[workerId]; ASSERT(task); - qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK); + qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK, false); while (1) { SSDataBlock* pDataBlock = NULL; uint64_t ts = 0; @@ -450,7 +450,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { SSubmitReq* pCont = (SSubmitReq*)&pHead->body; qTaskInfo_t task = pTopic->buffer.output[workerId].task; ASSERT(task); - qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK); + qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK, false); SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock)); while (1) { SSDataBlock* pDataBlock = NULL; diff --git a/source/dnode/vnode/src/tq/tqRead.c b/source/dnode/vnode/src/tq/tqRead.c index 02ce6c4aad..3cdd88a597 100644 --- a/source/dnode/vnode/src/tq/tqRead.c +++ b/source/dnode/vnode/src/tq/tqRead.c @@ -56,6 +56,22 @@ int32_t tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t return 0; } +int32_t tqReadHandleSetMsgEx(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t ver) { + pReadHandle->pMsg = pMsg; + + // iterate + if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; + while (true) { + if (tGetSubmitMsgNext(&pReadHandle->msgIter, &pReadHandle->pBlock) < 0) return -1; + if (pReadHandle->pBlock == NULL) break; + } + + if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; + pReadHandle->ver = ver; + memset(&pReadHandle->blkIter, 0, sizeof(SSubmitBlkIter)); + return 0; +} + bool tqNextDataBlock(STqReadHandle* pHandle) { while (1) { if (tGetSubmitMsgNext(&pHandle->msgIter, &pHandle->pBlock) < 0) { diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 34d41b1c79..4722e27391 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -101,23 +101,24 @@ typedef struct { STSma *pSma; // cache schema } SSmaStatItem; -struct SSmaStat { - union { - SHashObj *smaStatItems; // key: indexUid, value: SSmaStatItem for tsma - SHashObj *rsmaInfoHash; // key: stbUid, value: SRSmaInfo; - }; - - T_REF_DECLARE() -}; -#define SMA_STAT_ITEMS(s) ((s)->smaStatItems) -#define SMA_STAT_INFO_HASH(s) ((s)->rsmaInfoHash) - #define RSMA_MAX_LEVEL 2 #define RSMA_TASK_INFO_HASH_SLOT 8 struct SRSmaInfo { void *taskInfo[RSMA_MAX_LEVEL]; // qTaskInfo_t }; +struct SSmaStat { + union { + SHashObj *smaStatItems; // key: indexUid, value: SSmaStatItem for tsma + SHashObj *rsmaInfoHash; // key: stbUid, value: SRSmaInfo; + }; + SRSmaInfo rsmaInfo; + + T_REF_DECLARE() +}; +#define SMA_STAT_ITEMS(s) ((s)->smaStatItems) +#define SMA_STAT_INFO_HASH(s) ((s)->rsmaInfoHash) + static FORCE_INLINE void tsdbFreeTaskHandle(qTaskInfo_t *taskHandle) { // Note: free/kill may in RC qTaskInfo_t otaskHandle = atomic_load_ptr(taskHandle); @@ -1120,9 +1121,8 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, int64_t indexUid, const char int32_t storageLevel = tsdbGetSmaStorageLevel(pSma->interval, pSma->intervalUnit); int32_t daysPerFile = tsdbGetTSmaDays(pTsdb, tSmaH.interval, storageLevel); - // key: skey + groupId - char smaKey[SMA_KEY_LEN] = {0}; - char dataBuf[512] = {0}; + char smaKey[SMA_KEY_LEN] = {0}; // key: skey + groupId + char dataBuf[512] = {0}; // val: aggr data // TODO: handle 512 buffer? void *pDataBuf = NULL; int32_t sz = taosArrayGetSize(pDataBlocks); for (int32_t i = 0; i < sz; ++i) { @@ -1716,6 +1716,7 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); SSmaStat *pStat = SMA_ENV_STAT(pEnv); SRSmaInfo *pRSmaInfo = NULL; + SRSmaInfo *tRSmaInfo = &pStat->rsmaInfo; TASSERT(pEnv != NULL && pStat != NULL); @@ -1749,6 +1750,7 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { taosMemoryFree(pReadHandle); return TSDB_CODE_FAILED; } + tRSmaInfo->taskInfo[0] = pRSmaInfo->taskInfo[0]; } if (param->qmsg2) { @@ -1758,6 +1760,7 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { taosMemoryFree(pReadHandle); return TSDB_CODE_FAILED; } + tRSmaInfo->taskInfo[1] = pRSmaInfo->taskInfo[1]; } if (taosHashPut(SMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != @@ -1768,6 +1771,151 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { return TSDB_CODE_SUCCESS; } +int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t uid) { + if ((suid == pStore->suid) || (pStore->suid == 0)) { + if (pStore->tbUids == NULL) { + if ((pStore->tbUids = taosArrayInit(1, sizeof(tb_uid_t))) == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + pStore->suid = suid; + } + if (taosArrayPush(pStore->tbUids, &uid) == NULL) { + return TSDB_CODE_FAILED; + } + } else { + if (pStore->uidHash == NULL) { + pStore->uidHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK); + if (pStore->uidHash == NULL) { + return TSDB_CODE_FAILED; + } + } + SArray *hashVal = taosHashGet(pStore->uidHash, &suid, sizeof(tb_uid_t)); + if (hashVal && ((hashVal = *(SArray **)hashVal) != NULL)) { + taosArrayPush(hashVal, &uid); + } else { + SArray *pItem = taosArrayInit(1, sizeof(tb_uid_t)); + if (pItem == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + if (taosArrayPush(pItem, &uid) == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), &pItem, sizeof(pItem)) != 0) { + return TSDB_CODE_FAILED; + } + } + } + return TSDB_CODE_SUCCESS; +} + +int32_t tsdbFetchTbUidList(void *pTsdb, void **ppStore, void *suid, void *uid) { + SSmaEnv *pEnv = REPO_RSMA_ENV((STsdb *)pTsdb); + if (!pEnv) { + return TSDB_CODE_SUCCESS; + } + + SSmaStat *pStat = SMA_ENV_STAT(pEnv); + SHashObj *infoHash = NULL; + if (!pStat || !(infoHash = SMA_STAT_INFO_HASH(pStat))) { + terrno = TSDB_CODE_TDB_INVALID_SMA_STAT; + return TSDB_CODE_FAILED; + } + + // info cached when create rsma stable + if (!taosHashGet(infoHash, suid, sizeof(suid))) { + return TSDB_CODE_SUCCESS; + } + + if (*ppStore == NULL) { + if (tsdbUidStoreInit((STbUidStore **)ppStore) != 0) { + return TSDB_CODE_FAILED; + } + } + if (tsdbUidStorePut(*ppStore, *(tb_uid_t *)suid, *(tb_uid_t *)uid) != 0) { + *ppStore = tsdbUidStoreFree(*ppStore); + return TSDB_CODE_FAILED; + } + return TSDB_CODE_SUCCESS; +} + +void *tsdbUidStoreFree(STbUidStore *pStore) { + if (pStore) { + taosArrayDestroy(pStore->tbUids); + if (pStore->uidHash) { + void *pIter = taosHashIterate(pStore->uidHash, NULL); + while (pIter != NULL) { + SArray *arr = *(SArray **)pIter; + taosArrayDestroy(arr); + pIter = taosHashIterate(pStore->uidHash, pIter); + } + } + taosMemoryFree(pStore); + } + return NULL; +} + +int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pUidStore) { + if (!pUidStore || (taosArrayGetSize(pUidStore->tbUids) == 0)) { + tsdbDebug("vgId:%d empty uidStore and no need to update", REPO_ID(pTsdb)); + return TSDB_CODE_SUCCESS; + } + SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); + SSmaStat *pStat = SMA_ENV_STAT(pEnv); + SRSmaInfo *pRSmaInfo = &pStat->rsmaInfo; + + if (qUpdateQualifiedTableId(pRSmaInfo->taskInfo[0], pUidStore->tbUids, true) != 0) { + tsdbUidStoreFree(pUidStore); + tsdbError("vgId:%d update tbUidList failed since %s", REPO_ID(pTsdb), terrstr(terrno)); + return TSDB_CODE_FAILED; + } + + tsdbUidStoreFree(pUidStore); + return TSDB_CODE_SUCCESS; +} + +int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType) { + SArray *pResult = NULL; + + pResult = taosArrayInit(0, sizeof(SSDataBlock)); + if (pResult == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); + SSmaStat *pStat = SMA_ENV_STAT(pEnv); + SRSmaInfo *pRSmaInfo = NULL; + + TASSERT(pEnv != NULL && pStat != NULL); + + if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) { + // pRSmaInfo = taosHashGet(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t)); + // if (pRSmaInfo != NULL) { + // pRSmaInfo = tsdbFreeRSmaInfo(pRSmaInfo); + // } + + SRSmaInfo *tRSmaInfo = &pStat->rsmaInfo; + + qSetStreamInput(tRSmaInfo->taskInfo[0], pMsg, inputType, true); + while (1) { + SSDataBlock *output; + uint64_t ts; + if (qExecTask(tRSmaInfo->taskInfo[0], &output, &ts) < 0) { + ASSERT(false); + } + if (output == NULL) { + break; + } + taosArrayPush(pResult, output); + } + } + blockDebugShowData(pResult); + return TSDB_CODE_SUCCESS; +} + #if 0 /** * @brief Get the start TS key of the last data block of one interval/sliding. diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index a81bbf4298..310a2751dd 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -79,6 +79,7 @@ int vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg case TDMT_VND_SUBMIT: pRsp->msgType = TDMT_VND_SUBMIT_RSP; vnodeProcessSubmitReq(pVnode, ptr, pRsp); + tsdbTriggerRSma(pVnode->pTsdb, pVnode->pMeta, ptr, STREAM_DATA_TYPE_SUBMIT_BLOCK); break; case TDMT_VND_MQ_VG_CHANGE: if (tqProcessVgChangeReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), @@ -112,7 +113,6 @@ int vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg } } break; case TDMT_VND_CREATE_SMA: { // timeRangeSMA - if (tsdbCreateTSma(pVnode->pTsdb, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) { // TODO } @@ -210,7 +210,7 @@ int vnodeProcessSyncReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { static int vnodeProcessCreateStbReq(SVnode *pVnode, void *pReq) { SVCreateTbReq vCreateTbReq = {0}; tDeserializeSVCreateTbReq(pReq, &vCreateTbReq); - if (metaCreateTable(pVnode->pMeta, &(vCreateTbReq)) < 0) { + if (metaCreateTable(pVnode->pMeta, &(vCreateTbReq), NULL) < 0) { // TODO return -1; } @@ -235,6 +235,13 @@ static int vnodeProcessCreateTbReq(SVnode *pVnode, SRpcMsg *pMsg, void *pReq, SR SVCreateTbBatchRsp vCreateTbBatchRsp = {0}; tDeserializeSVCreateTbBatchReq(pReq, &vCreateTbBatchReq); int reqNum = taosArrayGetSize(vCreateTbBatchReq.pArray); + + STbDdlHandle ddlHandle = { + .ahandle = pVnode->pTsdb, + .result = NULL, + .fp = tsdbFetchTbUidList, + }; + for (int i = 0; i < reqNum; i++) { SVCreateTbReq *pCreateTbReq = taosArrayGet(vCreateTbBatchReq.pArray, i); @@ -250,7 +257,7 @@ static int vnodeProcessCreateTbReq(SVnode *pVnode, SRpcMsg *pMsg, void *pReq, SR taosArrayPush(vCreateTbBatchRsp.rspList, &rsp); } - if (metaCreateTable(pVnode->pMeta, pCreateTbReq) < 0) { + if (metaCreateTable(pVnode->pMeta, pCreateTbReq, &ddlHandle) < 0) { // TODO: handle error vError("vgId:%d, failed to create table: %s", TD_VID(pVnode), pCreateTbReq->name); } @@ -286,6 +293,8 @@ static int vnodeProcessCreateTbReq(SVnode *pVnode, SRpcMsg *pMsg, void *pReq, SR pRsp->contLen = contLen; } + tsdbUpdateTbUidList(pVnode->pTsdb, ddlHandle.result); + return 0; } diff --git a/source/libs/executor/src/executor.c b/source/libs/executor/src/executor.c index 4863b03fb9..8f7f55eb33 100644 --- a/source/libs/executor/src/executor.c +++ b/source/libs/executor/src/executor.c @@ -19,7 +19,7 @@ #include "tdatablock.h" #include "vnode.h" -static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) { +static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id, bool converted) { ASSERT(pOperator != NULL); if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) { if (pOperator->numOfDownstream == 0) { @@ -32,7 +32,7 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t nu return TSDB_CODE_QRY_APP_ERROR; } pOperator->status = OP_NOT_OPENED; - return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id); + return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id, converted); } else { pOperator->status = OP_NOT_OPENED; @@ -46,8 +46,13 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t nu } if (type == STREAM_DATA_TYPE_SUBMIT_BLOCK) { - if (tqReadHandleSetMsg(pInfo->readerHandle, input, 0) < 0) { - qError("submit msg messed up when initing stream block, %s" PRIx64, id); + if (converted) { + if (tqReadHandleSetMsgEx(pInfo->readerHandle, input, 0) < 0) { + qError("converted: submit msg messed up when initing stream block, %s" PRIx64, id); + return TSDB_CODE_QRY_APP_ERROR; + } + } else if (tqReadHandleSetMsg(pInfo->readerHandle, input, 0) < 0) { + qError("raw msg: submit msg messed up when initing stream block, %s" PRIx64, id); return TSDB_CODE_QRY_APP_ERROR; } } else { @@ -67,11 +72,11 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t nu } } -int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type) { - return qSetMultiStreamInput(tinfo, input, 1, type); +int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool converted) { + return qSetMultiStreamInput(tinfo, input, 1, type, converted); } -int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) { +int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type, bool converted) { if (tinfo == NULL) { return TSDB_CODE_QRY_APP_ERROR; } @@ -82,7 +87,7 @@ int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numO SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo; - int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo)); + int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo), converted); if (code != TSDB_CODE_SUCCESS) { qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo)); } else { diff --git a/source/libs/stream/src/tstream.c b/source/libs/stream/src/tstream.c index 8aaaa414ca..1f0c269ca1 100644 --- a/source/libs/stream/src/tstream.c +++ b/source/libs/stream/src/tstream.c @@ -110,7 +110,7 @@ int32_t streamExecTask(SStreamTask* pTask, SMsgCb* pMsgCb, const void* input, in return -1; } if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) { - qSetStreamInput(exec, input, inputType); + qSetStreamInput(exec, input, inputType, true); while (1) { SSDataBlock* output; uint64_t ts; @@ -128,7 +128,7 @@ int32_t streamExecTask(SStreamTask* pTask, SMsgCb* pMsgCb, const void* input, in /*for (int32_t i = 0; i < sz; i++) {*/ /*SSDataBlock* pBlock = taosArrayGet(blocks, i);*/ /*qSetStreamInput(exec, pBlock, inputType);*/ - qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK); + qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK, true); while (1) { SSDataBlock* output; uint64_t ts; From fa4cb8794bf4cd3bfd16562f8f71d281cfd06e4d Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Sat, 23 Apr 2022 19:47:05 +0800 Subject: [PATCH 08/51] more logic --- source/dnode/vnode/src/tsdb/tsdbSma.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 4722e27391..ddef1a649d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -1877,6 +1877,14 @@ int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pUidStore) { } int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType) { + SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); + if (!pEnv) { + return TSDB_CODE_SUCCESS; + } + + SSmaStat *pStat = SMA_ENV_STAT(pEnv); + SRSmaInfo *pRSmaInfo = NULL; + SArray *pResult = NULL; pResult = taosArrayInit(0, sizeof(SSDataBlock)); @@ -1885,12 +1893,6 @@ int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t in return -1; } - SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); - SSmaStat *pStat = SMA_ENV_STAT(pEnv); - SRSmaInfo *pRSmaInfo = NULL; - - TASSERT(pEnv != NULL && pStat != NULL); - if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) { // pRSmaInfo = taosHashGet(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t)); // if (pRSmaInfo != NULL) { From ef0365ebeb61c80b9b6e0f1ed971c57ee0ebf35f Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Sun, 24 Apr 2022 16:27:09 +0800 Subject: [PATCH 09/51] feature/qnode --- source/libs/qworker/src/qworkerMsg.c | 2 +- source/libs/scalar/inc/sclInt.h | 3 + source/libs/scalar/src/scalar.c | 98 +++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/source/libs/qworker/src/qworkerMsg.c b/source/libs/qworker/src/qworkerMsg.c index 6723eb21c1..e0bcfabdd3 100644 --- a/source/libs/qworker/src/qworkerMsg.c +++ b/source/libs/qworker/src/qworkerMsg.c @@ -339,7 +339,7 @@ int32_t qwRegisterHbBrokenLinkArg(SQWorkerMgmt *mgmt, uint64_t sId, SQWConnInfo .ahandle = pConn->ahandle, .msgType = TDMT_VND_QUERY_HEARTBEAT, .pCont = msg, - .contLen = sizeof(SSchedulerHbReq), + .contLen = msgSize, .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; diff --git a/source/libs/scalar/inc/sclInt.h b/source/libs/scalar/inc/sclInt.h index 3d59ffd93d..399fb8f47d 100644 --- a/source/libs/scalar/inc/sclInt.h +++ b/source/libs/scalar/inc/sclInt.h @@ -32,6 +32,9 @@ typedef struct SScalarCtx { #define SCL_DATA_TYPE_DUMMY_HASH 9000 #define SCL_DEFAULT_OP_NUM 10 +#define SCL_IS_CONST_NODE(_node) ((NULL == (_node)) || (QUERY_NODE_VALUE == (_node)->type)) +#define SCL_IS_CONST_CALC(_ctx) (NULL == (_ctx)->pBlockList) + #define sclFatal(...) qFatal(__VA_ARGS__) #define sclError(...) qError(__VA_ARGS__) #define sclWarn(...) qWarn(__VA_ARGS__) diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 145afbe984..4783a2701e 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -244,23 +244,55 @@ int32_t sclInitParam(SNode* node, SScalarParam *param, SScalarCtx *ctx, int32_t return TSDB_CODE_SUCCESS; } -int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *rowNum) { +int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *rowNum) { int32_t code = 0; - SScalarParam *paramList = taosMemoryCalloc(pParamList->length, sizeof(SScalarParam)); + int32_t paramNum = 0; + if (NULL == pParamList) { + if (ctx->pBlockList) { + SSDataBlock *pBlock = taosArrayGet(ctx->pBlockList, 0); + *rowNum = pBlock->info.rows; + } else { + *rowNum = 1; + } + + paramNum = 1; + } else { + paramNum = pParamList->length; + } + + SScalarParam *paramList = taosMemoryCalloc(paramNum, sizeof(SScalarParam)); if (NULL == paramList) { - sclError("calloc %d failed", (int32_t)(pParamList->length * sizeof(SScalarParam))); + sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam))); SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - SListCell *cell = pParamList->pHead; - for (int32_t i = 0; i < pParamList->length; ++i) { - if (NULL == cell || NULL == cell->pNode) { - sclError("invalid cell, cell:%p, pNode:%p", cell, cell->pNode); - SCL_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); + if (pParamList) { + SNode *tnode = NULL; + int32_t i = 0; + if (SCL_IS_CONST_CALC(ctx)) { + WHERE_EACH (tnode, pParamList) { + if (!SCL_IS_CONST_NODE(tnode)) { + continue; + } else { + SCL_ERR_JRET(sclInitParam(tnode, ¶mList[i], ctx, rowNum)); + ERASE_NODE(pParamList); + } + + ++i; + WHERE_NEXT; + } + } else { + FOREACH(tnode, pParamList) { + SCL_ERR_JRET(sclInitParam(tnode, ¶mList[i], ctx, rowNum)); + ++i; + } } + } else { + paramList[0].numOfRows = *rowNum; + } - SCL_ERR_JRET(sclInitParam(cell->pNode, ¶mList[i], ctx, rowNum)); - cell = cell->pNext; + if (0 == *rowNum) { + taosMemoryFreeClear(paramList); } *pParams = paramList; @@ -299,11 +331,6 @@ _return: } int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) { - if (NULL == node->pParameterList || node->pParameterList->length <= 0) { - sclError("invalid function parameter list, list:%p, paramNum:%d", node->pParameterList, node->pParameterList ? node->pParameterList->length : 0); - SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } - SScalarFuncExecFuncs ffpSet = {0}; int32_t code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet); if (code) { @@ -357,6 +384,10 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o int32_t rowNum = 0; int32_t code = 0; SCL_ERR_RET(sclInitParamList(¶ms, node->pParameterList, ctx, &rowNum)); + if (NULL == params) { + output->numOfRows = 0; + return TSDB_CODE_SUCCESS; + } int32_t type = node->node.resType.type; output->numOfRows = rowNum; @@ -369,14 +400,20 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o } bool value = false; + bool complete = false; for (int32_t i = 0; i < rowNum; ++i) { for (int32_t m = 0; m < node->pParameterList->length; ++m) { + if (NULL == params[m].columnData) { + continue; + } char* p = colDataGetData(params[m].columnData, i); GET_TYPED_DATA(value, bool, params[m].columnData->info.type, p); if (LOGIC_COND_TYPE_AND == node->condType && (false == value)) { + complete = true; break; } else if (LOGIC_COND_TYPE_OR == node->condType && value) { + complete = true; break; } else if (LOGIC_COND_TYPE_NOT == node->condType) { value = !value; @@ -386,7 +423,13 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o colDataAppend(output->columnData, i, (char*) &value, false); } + if (SCL_IS_CONST_CALC(ctx) && (false == complete)) { + sclFreeParam(output); + output->numOfRows = 0; + } + _return: + for (int32_t i = 0; i < node->pParameterList->length; ++i) { // sclFreeParamNoData(params + i); } @@ -426,6 +469,17 @@ _return: EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) { SFunctionNode *node = (SFunctionNode *)*pNode; + SNode* tnode = NULL; + if (fmIsUserDefinedFunc(node->funcId)) { + return DEAL_RES_CONTINUE; + } + + FOREACH(tnode, node->pParameterList) { + if (!SCL_IS_CONST_NODE(tnode)) { + return DEAL_RES_CONTINUE; + } + } + SScalarParam output = {0}; ctx->code = sclExecFunction(node, ctx, &output); @@ -470,6 +524,10 @@ EDealRes sclRewriteLogic(SNode** pNode, SScalarCtx *ctx) { return DEAL_RES_ERROR; } + if (0 == output.numOfRows) { + return DEAL_RES_CONTINUE; + } + SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE); if (NULL == res) { sclError("make value node failed"); @@ -498,6 +556,14 @@ EDealRes sclRewriteLogic(SNode** pNode, SScalarCtx *ctx) { EDealRes sclRewriteOperator(SNode** pNode, SScalarCtx *ctx) { SOperatorNode *node = (SOperatorNode *)*pNode; + if (!SCL_IS_CONST_NODE(node->pLeft)) { + return DEAL_RES_CONTINUE; + } + + if (!SCL_IS_CONST_NODE(node->pRight)) { + return DEAL_RES_CONTINUE; + } + SScalarParam output = {.columnData = taosMemoryCalloc(1, sizeof(SColumnInfoData))}; ctx->code = sclExecOperator(node, ctx, &output); if (ctx->code) { @@ -530,7 +596,7 @@ EDealRes sclRewriteOperator(SNode** pNode, SScalarCtx *ctx) { } EDealRes sclConstantsRewriter(SNode** pNode, void* pContext) { - if (QUERY_NODE_VALUE == nodeType(*pNode) || QUERY_NODE_NODE_LIST == nodeType(*pNode)) { + if (QUERY_NODE_VALUE == nodeType(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode) || QUERY_NODE_NODE_LIST == nodeType(*pNode)) { return DEAL_RES_CONTINUE; } From 575f64197df124e57806b778a4c01fc7cbded9a4 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Sun, 24 Apr 2022 16:51:32 +0800 Subject: [PATCH 10/51] enh(wal): skip read for specific msg --- include/common/tmsg.h | 6 - include/libs/wal/wal.h | 8 +- source/client/src/tmq.c | 3 - source/common/src/tmsg.c | 5 +- source/dnode/mnode/impl/inc/mndDef.h | 2 - source/dnode/mnode/impl/src/mndDef.c | 3 - source/dnode/mnode/impl/src/mndSubscribe.c | 179 +-------------------- source/dnode/mnode/impl/src/mndTopic.c | 2 - source/dnode/vnode/src/inc/tq.h | 1 - source/dnode/vnode/src/tq/tq.c | 36 ++++- source/libs/wal/src/walRead.c | 87 ++++++++++ source/libs/wal/src/walWrite.c | 8 +- 12 files changed, 134 insertions(+), 206 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 757fa8e74b..75a59a7f75 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1969,7 +1969,6 @@ typedef struct { int8_t withTbName; int8_t withSchema; int8_t withTag; - int8_t withTagSchema; char* qmsg; } SMqRebVgReq; @@ -1984,7 +1983,6 @@ static FORCE_INLINE int32_t tEncodeSMqRebVgReq(void** buf, const SMqRebVgReq* pR tlen += taosEncodeFixedI8(buf, pReq->withTbName); tlen += taosEncodeFixedI8(buf, pReq->withSchema); tlen += taosEncodeFixedI8(buf, pReq->withTag); - tlen += taosEncodeFixedI8(buf, pReq->withTagSchema); if (pReq->subType == TOPIC_SUB_TYPE__TABLE) { tlen += taosEncodeString(buf, pReq->qmsg); } @@ -2001,7 +1999,6 @@ static FORCE_INLINE void* tDecodeSMqRebVgReq(const void* buf, SMqRebVgReq* pReq) buf = taosDecodeFixedI8(buf, &pReq->withTbName); buf = taosDecodeFixedI8(buf, &pReq->withSchema); buf = taosDecodeFixedI8(buf, &pReq->withTag); - buf = taosDecodeFixedI8(buf, &pReq->withTagSchema); if (pReq->subType == TOPIC_SUB_TYPE__TABLE) { buf = taosDecodeString(buf, &pReq->qmsg); } @@ -2590,7 +2587,6 @@ typedef struct { int8_t withTbName; int8_t withSchema; int8_t withTag; - int8_t withTagSchema; SArray* blockDataLen; // SArray SArray* blockData; // SArray SArray* blockTbName; // SArray @@ -2609,7 +2605,6 @@ static FORCE_INLINE int32_t tEncodeSMqDataBlkRsp(void** buf, const SMqDataBlkRsp tlen += taosEncodeFixedI8(buf, pRsp->withTbName); tlen += taosEncodeFixedI8(buf, pRsp->withSchema); tlen += taosEncodeFixedI8(buf, pRsp->withTag); - tlen += taosEncodeFixedI8(buf, pRsp->withTagSchema); for (int32_t i = 0; i < pRsp->blockNum; i++) { int32_t bLen = *(int32_t*)taosArrayGet(pRsp->blockDataLen, i); @@ -2632,7 +2627,6 @@ static FORCE_INLINE void* tDecodeSMqDataBlkRsp(const void* buf, SMqDataBlkRsp* p buf = taosDecodeFixedI8(buf, &pRsp->withTbName); buf = taosDecodeFixedI8(buf, &pRsp->withSchema); buf = taosDecodeFixedI8(buf, &pRsp->withTag); - buf = taosDecodeFixedI8(buf, &pRsp->withTagSchema); for (int32_t i = 0; i < pRsp->blockNum; i++) { int32_t bLen = 0; diff --git a/include/libs/wal/wal.h b/include/libs/wal/wal.h index d41e797536..7b0d70b769 100644 --- a/include/libs/wal/wal.h +++ b/include/libs/wal/wal.h @@ -192,7 +192,13 @@ int32_t walEndSnapshot(SWal *); SWalReadHandle *walOpenReadHandle(SWal *); void walCloseReadHandle(SWalReadHandle *); int32_t walReadWithHandle(SWalReadHandle *pRead, int64_t ver); -int32_t walReadWithHandle_s(SWalReadHandle *pRead, int64_t ver, SWalReadHead **ppHead); + +// only for tq usage +// int32_t walReadWithHandle_s(SWalReadHandle *pRead, int64_t ver, SWalReadHead **ppHead); +void walSetReaderCapacity(SWalReadHandle *pRead, int32_t capacity); +int32_t walFetchHead(SWalReadHandle *pRead, int64_t ver, SWalHead *pHead); +int32_t walFetchBody(SWalReadHandle *pRead, SWalHead **ppHead); +int32_t walSkipFetchBody(SWalReadHandle *pRead, const SWalHead *pHead); // deprecated #if 0 diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index 699fa7ecf5..2ccd88eea8 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -382,12 +382,9 @@ tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) { pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, conf->db, conf->port, CONN_TYPE__TMQ); if (pTmq->pTscObj == NULL) return NULL; - /*pTmq->inWaiting = 0;*/ pTmq->status = 0; pTmq->pollCnt = 0; pTmq->epoch = 0; - /*pTmq->waitingRequest = 0;*/ - /*pTmq->readyRequest = 0;*/ pTmq->epStatus = 0; pTmq->epSkipCnt = 0; // set conf diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index eb7c9a763f..0d3009fc65 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -126,7 +126,6 @@ int32_t tDecodeSQueryNodeAddr(SCoder *pDecoder, SQueryNodeAddr *pAddr) { return 0; } - int32_t taosEncodeSEpSet(void **buf, const SEpSet *pEp) { int32_t tlen = 0; tlen += taosEncodeFixedI8(buf, pEp->inUse); @@ -2747,11 +2746,11 @@ int32_t tSerializeSCMCreateTopicReq(void *buf, int32_t bufLen, const SCMCreateTo if (tEncodeI8(&encoder, pReq->withTbName) < 0) return -1; if (tEncodeI8(&encoder, pReq->withSchema) < 0) return -1; if (tEncodeI8(&encoder, pReq->withTag) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->subscribeDbName) < 0) return -1; if (tEncodeI32(&encoder, sqlLen) < 0) return -1; if (tEncodeI32(&encoder, astLen) < 0) return -1; if (sqlLen > 0 && tEncodeCStr(&encoder, pReq->sql) < 0) return -1; if (astLen > 0 && tEncodeCStr(&encoder, pReq->ast) < 0) return -1; - if (0 == astLen && tEncodeCStr(&encoder, pReq->subscribeDbName) < 0) return -1; tEndEncode(&encoder); @@ -2773,6 +2772,7 @@ int32_t tDeserializeSCMCreateTopicReq(void *buf, int32_t bufLen, SCMCreateTopicR if (tDecodeI8(&decoder, &pReq->withTbName) < 0) return -1; if (tDecodeI8(&decoder, &pReq->withSchema) < 0) return -1; if (tDecodeI8(&decoder, &pReq->withTag) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->subscribeDbName) < 0) return -1; if (tDecodeI32(&decoder, &sqlLen) < 0) return -1; if (tDecodeI32(&decoder, &astLen) < 0) return -1; @@ -2787,7 +2787,6 @@ int32_t tDeserializeSCMCreateTopicReq(void *buf, int32_t bufLen, SCMCreateTopicR if (pReq->ast == NULL) return -1; if (tDecodeCStrTo(&decoder, pReq->ast) < 0) return -1; } else { - if (tDecodeCStrTo(&decoder, pReq->subscribeDbName) < 0) return -1; } tEndDecode(&decoder); diff --git a/source/dnode/mnode/impl/inc/mndDef.h b/source/dnode/mnode/impl/inc/mndDef.h index 59a00f0eaf..d2bc8c9282 100644 --- a/source/dnode/mnode/impl/inc/mndDef.h +++ b/source/dnode/mnode/impl/inc/mndDef.h @@ -449,7 +449,6 @@ typedef struct { int8_t withTbName; int8_t withSchema; int8_t withTag; - int8_t withTagSchema; SRWLatch lock; int32_t sqlLen; int32_t astLen; @@ -516,7 +515,6 @@ typedef struct { int8_t withTbName; int8_t withSchema; int8_t withTag; - int8_t withTagSchema; SHashObj* consumerHash; // consumerId -> SMqConsumerEpInSub // TODO put -1 into unassignVgs // SArray* unassignedVgs; diff --git a/source/dnode/mnode/impl/src/mndDef.c b/source/dnode/mnode/impl/src/mndDef.c index 78d54d273d..800a013c4f 100644 --- a/source/dnode/mnode/impl/src/mndDef.c +++ b/source/dnode/mnode/impl/src/mndDef.c @@ -237,7 +237,6 @@ SMqSubscribeObj *tCloneSubscribeObj(const SMqSubscribeObj *pSub) { pSubNew->withTbName = pSub->withTbName; pSubNew->withSchema = pSub->withSchema; pSubNew->withTag = pSub->withTag; - pSubNew->withTagSchema = pSub->withTagSchema; pSubNew->vgNum = pSub->vgNum; pSubNew->consumerHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); @@ -270,7 +269,6 @@ int32_t tEncodeSubscribeObj(void **buf, const SMqSubscribeObj *pSub) { tlen += taosEncodeFixedI8(buf, pSub->withTbName); tlen += taosEncodeFixedI8(buf, pSub->withSchema); tlen += taosEncodeFixedI8(buf, pSub->withTag); - tlen += taosEncodeFixedI8(buf, pSub->withTagSchema); void *pIter = NULL; int32_t sz = taosHashGetSize(pSub->consumerHash); @@ -297,7 +295,6 @@ void *tDecodeSubscribeObj(const void *buf, SMqSubscribeObj *pSub) { buf = taosDecodeFixedI8(buf, &pSub->withTbName); buf = taosDecodeFixedI8(buf, &pSub->withSchema); buf = taosDecodeFixedI8(buf, &pSub->withTag); - buf = taosDecodeFixedI8(buf, &pSub->withTagSchema); int32_t sz; buf = taosDecodeFixedI32(buf, &sz); diff --git a/source/dnode/mnode/impl/src/mndSubscribe.c b/source/dnode/mnode/impl/src/mndSubscribe.c index e37bd60e12..6a1994d7b8 100644 --- a/source/dnode/mnode/impl/src/mndSubscribe.c +++ b/source/dnode/mnode/impl/src/mndSubscribe.c @@ -35,11 +35,6 @@ #define MND_SUBSCRIBE_REBALANCE_CNT 3 -enum { - MQ_SUBSCRIBE_STATUS__ACTIVE = 1, - MQ_SUBSCRIBE_STATUS__DELETED, -}; - static SSdbRaw *mndSubActionEncode(SMqSubscribeObj *); static SSdbRow *mndSubActionDecode(SSdbRaw *pRaw); static int32_t mndSubActionInsert(SSdb *pSdb, SMqSubscribeObj *); @@ -89,7 +84,6 @@ static SMqSubscribeObj *mndCreateSub(SMnode *pMnode, const SMqTopicObj *pTopic, pSub->withTbName = pTopic->withTbName; pSub->withSchema = pTopic->withSchema; pSub->withTag = pTopic->withTag; - pSub->withTagSchema = pTopic->withTagSchema; ASSERT(taosHashGetSize(pSub->consumerHash) == 1); @@ -115,7 +109,6 @@ static int32_t mndBuildSubChangeReq(void **pBuf, int32_t *pLen, const SMqSubscri req.withTbName = pSub->withTbName; req.withSchema = pSub->withSchema; req.withTag = pSub->withTag; - req.withTagSchema = pSub->withTagSchema; strncpy(req.subKey, pSub->key, TSDB_SUBSCRIBE_KEY_LEN); int32_t tlen = sizeof(SMsgHead) + tEncodeSMqRebVgReq(NULL, &req); @@ -514,9 +507,11 @@ static int32_t mndProcessRebalanceReq(SNodeMsg *pMsg) { // TODO replace assert with error check ASSERT(mndDoRebalance(pMnode, &rebInput, &rebOutput) == 0); + // if add more consumer to balanced subscribe, // possibly no vg is changed /*ASSERT(taosArrayGetSize(rebOutput.rebVgs) != 0);*/ + ASSERT(mndPersistRebResult(pMnode, pMsg, &rebOutput) == 0); if (rebInput.pTopic) { @@ -673,177 +668,7 @@ void mndReleaseSubscribe(SMnode *pMnode, SMqSubscribeObj *pSub) { sdbRelease(pSdb, pSub); } -#if 0 -static int32_t mndProcessSubscribeReq(SNodeMsg *pMsg) { - SMnode *pMnode = pMsg->pNode; - char *msgStr = pMsg->rpcMsg.pCont; - SCMSubscribeReq subscribe; - tDeserializeSCMSubscribeReq(msgStr, &subscribe); - int64_t consumerId = subscribe.consumerId; - char *cgroup = subscribe.consumerGroup; - - SArray *newSub = subscribe.topicNames; - int32_t newTopicNum = subscribe.topicNum; - - taosArraySortString(newSub, taosArrayCompareString); - - SArray *oldSub = NULL; - int32_t oldTopicNum = 0; - bool createConsumer = false; - // create consumer if not exist - SMqConsumerObj *pConsumer = mndAcquireConsumer(pMnode, consumerId); - if (pConsumer == NULL) { - // create consumer - pConsumer = mndCreateConsumer(consumerId, cgroup); - createConsumer = true; - } else { - pConsumer->epoch++; - oldSub = pConsumer->currentTopics; - } - pConsumer->currentTopics = newSub; - - if (oldSub != NULL) { - oldTopicNum = taosArrayGetSize(oldSub); - } - - STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_SUBSCRIBE, &pMsg->rpcMsg); - if (pTrans == NULL) { - // TODO: free memory - return -1; - } - - int32_t i = 0, j = 0; - while (i < newTopicNum || j < oldTopicNum) { - char *newTopicName = NULL; - char *oldTopicName = NULL; - if (i >= newTopicNum) { - // encode unset topic msg to all vnodes related to that topic - oldTopicName = taosArrayGetP(oldSub, j); - j++; - } else if (j >= oldTopicNum) { - newTopicName = taosArrayGetP(newSub, i); - i++; - } else { - newTopicName = taosArrayGetP(newSub, i); - oldTopicName = taosArrayGetP(oldSub, j); - - int32_t comp = compareLenPrefixedStr(newTopicName, oldTopicName); - if (comp == 0) { - // do nothing - oldTopicName = newTopicName = NULL; - i++; - j++; - continue; - } else if (comp < 0) { - oldTopicName = NULL; - i++; - } else { - newTopicName = NULL; - j++; - } - } - - if (oldTopicName != NULL) { - ASSERT(newTopicName == NULL); - - // cancel subscribe of old topic - SMqSubscribeObj *pSub = mndAcquireSubscribe(pMnode, cgroup, oldTopicName); - ASSERT(pSub); - int32_t csz = taosArrayGetSize(pSub->consumers); - for (int32_t ci = 0; ci < csz; ci++) { - SMqSubConsumer *pSubConsumer = taosArrayGet(pSub->consumers, ci); - if (pSubConsumer->consumerId == consumerId) { - int32_t vgsz = taosArrayGetSize(pSubConsumer->vgInfo); - for (int32_t vgi = 0; vgi < vgsz; vgi++) { - SMqConsumerEp *pConsumerEp = taosArrayGet(pSubConsumer->vgInfo, vgi); - mndPersistCancelConnReq(pMnode, pTrans, pConsumerEp, oldTopicName); - taosArrayPush(pSub->unassignedVg, pConsumerEp); - } - taosArrayRemove(pSub->consumers, ci); - break; - } - } - char *oldTopicNameDup = strdup(oldTopicName); - taosArrayPush(pConsumer->recentRemovedTopics, &oldTopicNameDup); - atomic_store_32(&pConsumer->status, MQ_CONSUMER_STATUS__MODIFY); - /*pSub->status = MQ_SUBSCRIBE_STATUS__DELETED;*/ - } else if (newTopicName != NULL) { - ASSERT(oldTopicName == NULL); - - SMqTopicObj *pTopic = mndAcquireTopic(pMnode, newTopicName); - if (pTopic == NULL) { - mError("topic being subscribed not exist: %s", newTopicName); - continue; - } - - SMqSubscribeObj *pSub = mndAcquireSubscribe(pMnode, cgroup, newTopicName); - bool createSub = false; - if (pSub == NULL) { - mDebug("create new subscription by consumer %" PRId64 ", group: %s, topic %s", consumerId, cgroup, - newTopicName); - pSub = mndCreateSubscription(pMnode, pTopic, cgroup); - createSub = true; - - mndCreateOffset(pTrans, cgroup, newTopicName, pSub->unassignedVg); - } - - SMqSubConsumer mqSubConsumer; - mqSubConsumer.consumerId = consumerId; - mqSubConsumer.vgInfo = taosArrayInit(0, sizeof(SMqConsumerEp)); - taosArrayPush(pSub->consumers, &mqSubConsumer); - - // if have un assigned vg, assign one to the consumer - if (taosArrayGetSize(pSub->unassignedVg) > 0) { - SMqConsumerEp *pConsumerEp = taosArrayPop(pSub->unassignedVg); - pConsumerEp->oldConsumerId = pConsumerEp->consumerId; - pConsumerEp->consumerId = consumerId; - taosArrayPush(mqSubConsumer.vgInfo, pConsumerEp); - if (pConsumerEp->oldConsumerId == -1) { - mInfo("mq set conn: assign vgroup %d of topic %s to consumer %" PRId64 "", pConsumerEp->vgId, newTopicName, - pConsumerEp->consumerId); - mndPersistMqSetConnReq(pMnode, pTrans, pTopic, cgroup, pConsumerEp); - } else { - mndPersistRebalanceMsg(pMnode, pTrans, pConsumerEp, newTopicName); - } - // to trigger rebalance at once, do not set status active - /*atomic_store_32(&pConsumer->status, MQ_CONSUMER_STATUS__ACTIVE);*/ - } - - SSdbRaw *pRaw = mndSubActionEncode(pSub); - sdbSetRawStatus(pRaw, SDB_STATUS_READY); - mndTransAppendRedolog(pTrans, pRaw); - - if (!createSub) mndReleaseSubscribe(pMnode, pSub); - mndReleaseTopic(pMnode, pTopic); - } - } - - /*if (oldSub) taosArrayDestroyEx(oldSub, (void (*)(void *))taosMemoryFree);*/ - - // persist consumerObj - SSdbRaw *pConsumerRaw = mndConsumerActionEncode(pConsumer); - sdbSetRawStatus(pConsumerRaw, SDB_STATUS_READY); - mndTransAppendRedolog(pTrans, pConsumerRaw); - - if (mndTransPrepare(pMnode, pTrans) != 0) { - mError("mq-subscribe-trans:%d, failed to prepare since %s", pTrans->id, terrstr()); - mndTransDrop(pTrans); - if (!createConsumer) mndReleaseConsumer(pMnode, pConsumer); - return -1; - } - - mndTransDrop(pTrans); - if (!createConsumer) mndReleaseConsumer(pMnode, pConsumer); - return TSDB_CODE_MND_ACTION_IN_PROGRESS; -} -#endif - static int32_t mndProcessSubscribeInternalRsp(SNodeMsg *pRsp) { mndTransProcessRsp(pRsp); return 0; } - -static void mndCancelGetNextConsumer(SMnode *pMnode, void *pIter) { - SSdb *pSdb = pMnode->pSdb; - sdbCancelFetch(pSdb, pIter); -} diff --git a/source/dnode/mnode/impl/src/mndTopic.c b/source/dnode/mnode/impl/src/mndTopic.c index a4b98ba01a..1d9b55f7e5 100644 --- a/source/dnode/mnode/impl/src/mndTopic.c +++ b/source/dnode/mnode/impl/src/mndTopic.c @@ -82,7 +82,6 @@ SSdbRaw *mndTopicActionEncode(SMqTopicObj *pTopic) { SDB_SET_INT8(pRaw, dataPos, pTopic->withTbName, TOPIC_ENCODE_OVER); SDB_SET_INT8(pRaw, dataPos, pTopic->withSchema, TOPIC_ENCODE_OVER); SDB_SET_INT8(pRaw, dataPos, pTopic->withTag, TOPIC_ENCODE_OVER); - SDB_SET_INT8(pRaw, dataPos, pTopic->withTagSchema, TOPIC_ENCODE_OVER); SDB_SET_INT32(pRaw, dataPos, pTopic->sqlLen, TOPIC_ENCODE_OVER); SDB_SET_BINARY(pRaw, dataPos, pTopic->sql, pTopic->sqlLen, TOPIC_ENCODE_OVER); SDB_SET_INT32(pRaw, dataPos, pTopic->astLen, TOPIC_ENCODE_OVER); @@ -146,7 +145,6 @@ SSdbRow *mndTopicActionDecode(SSdbRaw *pRaw) { SDB_GET_INT8(pRaw, dataPos, &pTopic->withTbName, TOPIC_DECODE_OVER); SDB_GET_INT8(pRaw, dataPos, &pTopic->withSchema, TOPIC_DECODE_OVER); SDB_GET_INT8(pRaw, dataPos, &pTopic->withTag, TOPIC_DECODE_OVER); - SDB_GET_INT8(pRaw, dataPos, &pTopic->withTagSchema, TOPIC_DECODE_OVER); SDB_GET_INT32(pRaw, dataPos, &pTopic->sqlLen, TOPIC_DECODE_OVER); pTopic->sql = taosMemoryCalloc(pTopic->sqlLen, sizeof(char)); diff --git a/source/dnode/vnode/src/inc/tq.h b/source/dnode/vnode/src/inc/tq.h index 6e004a89fc..347d28f1ea 100644 --- a/source/dnode/vnode/src/inc/tq.h +++ b/source/dnode/vnode/src/inc/tq.h @@ -159,7 +159,6 @@ typedef struct { int8_t withTbName; int8_t withSchema; int8_t withTag; - int8_t withTagSchema; char* qmsg; STqPushHandle pushHandle; // SRWLatch lock; diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index 0b3feb5010..434c5bc5bb 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -31,6 +31,7 @@ STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal) { pTq->path = strdup(path); pTq->pVnode = pVnode; pTq->pWal = pWal; + #if 0 pTq->tqMeta = tqStoreOpen(pTq, path, (FTqSerialize)tqSerializeConsumer, (FTqDeserialize)tqDeserializeConsumer, (FTqDelete)taosMemoryFree, 0); @@ -401,6 +402,13 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { consumerEpoch = atomic_val_compare_exchange_32(&pExec->epoch, consumerEpoch, reqEpoch); } + SWalHead* pHeadWithCkSum = taosMemoryMalloc(sizeof(SWalHead) + 2048); + if (pHeadWithCkSum == NULL) { + return -1; + } + + walSetReaderCapacity(pExec->pWalReader, 2048); + SMqDataBlkRsp rsp = {0}; rsp.reqOffset = pReq->currentOffset; rsp.blockData = taosArrayInit(0, sizeof(void*)); @@ -414,6 +422,26 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { break; } + taosThreadMutexLock(&pExec->pWalReader->mutex); + + if (walFetchHead(pExec->pWalReader, fetchOffset, pHeadWithCkSum) < 0) { + vDebug("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, no more log to return", consumerId, pReq->epoch, + TD_VID(pTq->pVnode), fetchOffset); + taosThreadMutexUnlock(&pExec->pWalReader->mutex); + break; + } + + if (pHeadWithCkSum->head.msgType != TDMT_VND_SUBMIT) { + walSkipFetchBody(pExec->pWalReader, pHeadWithCkSum); + } else { + walFetchBody(pExec->pWalReader, &pHeadWithCkSum); + } + + SWalReadHead* pHead = &pHeadWithCkSum->head; + + taosThreadMutexUnlock(&pExec->pWalReader->mutex); + +#if 0 SWalReadHead* pHead; if (walReadWithHandle_s(pExec->pWalReader, fetchOffset, &pHead) < 0) { // TODO: no more log, set timer to wait blocking time @@ -443,14 +471,16 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { return 0; #endif - break; - } + break; + } +#endif vDebug("tmq poll: consumer %ld (epoch %d) iter log, vg %d offset %ld msgType %d", consumerId, pReq->epoch, TD_VID(pTq->pVnode), fetchOffset, pHead->msgType); if (pHead->msgType == TDMT_VND_SUBMIT) { SSubmitReq* pCont = (SSubmitReq*)&pHead->body; + // table subscribe if (pExec->subType == TOPIC_SUB_TYPE__TABLE) { qTaskInfo_t task = pExec->task[workerId]; ASSERT(task); @@ -484,6 +514,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { taosArrayPush(rsp.blockData, &buf); rsp.blockNum++; } + // db subscribe } else if (pExec->subType == TOPIC_SUB_TYPE__DB) { STqReadHandle* pReader = pExec->pExecReader[workerId]; tqReadHandleSetMsg(pReader, pCont, 0); @@ -789,7 +820,6 @@ int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) { pExec->withTbName = req.withTbName; pExec->withSchema = req.withSchema; pExec->withTag = req.withTag; - pExec->withTagSchema = req.withTagSchema; pExec->qmsg = req.qmsg; req.qmsg = NULL; diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index 6d598699c5..70a3559cd9 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -138,6 +138,91 @@ static int32_t walReadSeekVer(SWalReadHandle *pRead, int64_t ver) { return 0; } +void walSetReaderCapacity(SWalReadHandle *pRead, int32_t capacity) { pRead->capacity = capacity; } + +int32_t walFetchHead(SWalReadHandle *pRead, int64_t ver, SWalHead *pHead) { + int32_t code; + // TODO: valid ver + + if (pRead->curVersion != ver) { + code = walReadSeekVer(pRead, ver); + if (code < 0) return -1; + } + + if (!taosValidFile(pRead->pReadLogTFile)) { + return -1; + } + + code = taosReadFile(pRead->pReadLogTFile, pHead, sizeof(SWalHead)); + if (code != sizeof(SWalHead)) { + return -1; + } + + code = walValidHeadCksum(pHead); + + if (code != 0) { + wError("unexpected wal log version: % " PRId64 ", since head checksum not passed", ver); + terrno = TSDB_CODE_WAL_FILE_CORRUPTED; + return -1; + } + + return 0; +} + +int32_t walSkipFetchBody(SWalReadHandle *pRead, const SWalHead *pHead) { + int32_t code; + + ASSERT(pRead->curVersion == pHead->head.version); + + code = taosLSeekFile(pRead->pReadLogTFile, pHead->head.bodyLen, SEEK_CUR); + if (code < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + pRead->curVersion = -1; + return -1; + } + + pRead->curVersion++; + + return 0; +} + +int32_t walFetchBody(SWalReadHandle *pRead, SWalHead **ppHead) { + SWalReadHead *pReadHead = &((*ppHead)->head); + int64_t ver = pReadHead->version; + + if (pRead->capacity < pReadHead->bodyLen) { + void *ptr = taosMemoryRealloc(*ppHead, sizeof(SWalHead) + pReadHead->bodyLen); + if (ptr == NULL) { + terrno = TSDB_CODE_WAL_OUT_OF_MEMORY; + return -1; + } + *ppHead = ptr; + pRead->capacity = pReadHead->bodyLen; + } + + if (pReadHead->bodyLen != taosReadFile(pRead->pReadLogTFile, pReadHead->body, pReadHead->bodyLen)) { + return -1; + } + + if (pReadHead->version != ver) { + wError("unexpected wal log version: %" PRId64 ", read request version:%" PRId64 "", pRead->pHead->head.version, + ver); + pRead->curVersion = -1; + terrno = TSDB_CODE_WAL_FILE_CORRUPTED; + return -1; + } + + if (walValidBodyCksum(*ppHead) != 0) { + wError("unexpected wal log version: % " PRId64 ", since body checksum not passed", ver); + pRead->curVersion = -1; + terrno = TSDB_CODE_WAL_FILE_CORRUPTED; + return -1; + } + + pRead->curVersion = ver + 1; + return 0; +} + int32_t walReadWithHandle_s(SWalReadHandle *pRead, int64_t ver, SWalReadHead **ppHead) { taosThreadMutexLock(&pRead->mutex); if (walReadWithHandle(pRead, ver) < 0) { @@ -172,12 +257,14 @@ int32_t walReadWithHandle(SWalReadHandle *pRead, int64_t ver) { if (code != sizeof(SWalHead)) { return -1; } + code = walValidHeadCksum(pRead->pHead); if (code != 0) { wError("unexpected wal log version: % " PRId64 ", since head checksum not passed", ver); terrno = TSDB_CODE_WAL_FILE_CORRUPTED; return -1; } + if (pRead->capacity < pRead->pHead->head.bodyLen) { void *ptr = taosMemoryRealloc(pRead->pHead, sizeof(SWalHead) + pRead->pHead->head.bodyLen); if (ptr == NULL) { diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 530930c261..207b7d8421 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -13,8 +13,6 @@ * along with this program. If not, see . */ -#define _DEFAULT_SOURCE - #include "os.h" #include "taoserror.h" #include "tchecksum.h" @@ -298,14 +296,14 @@ int64_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLog pWal->writeHead.head.bodyLen = bodyLen; pWal->writeHead.head.msgType = msgType; - // sync info + // sync info for sync module pWal->writeHead.head.syncMeta = syncMeta; pWal->writeHead.cksumHead = walCalcHeadCksum(&pWal->writeHead); pWal->writeHead.cksumBody = walCalcBodyCksum(body, bodyLen); if (taosWriteFile(pWal->pWriteLogTFile, &pWal->writeHead, sizeof(SWalHead)) != sizeof(SWalHead)) { - // ftruncate + // TODO ftruncate terrno = TAOS_SYSTEM_ERROR(errno); wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal), strerror(errno)); @@ -313,7 +311,7 @@ int64_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLog } if (taosWriteFile(pWal->pWriteLogTFile, (char *)body, bodyLen) != bodyLen) { - // ftruncate + // TODO ftruncate terrno = TAOS_SYSTEM_ERROR(errno); wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal), strerror(errno)); From f4728e826eb1b5e20185879dc1a8c3415039f3f0 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Sun, 24 Apr 2022 18:02:57 +0800 Subject: [PATCH 11/51] fix: add connection error handle --- source/client/src/clientMsgHandler.c | 30 +-- source/libs/qworker/src/qworkerMsg.c | 269 +++++++++++++-------------- 2 files changed, 150 insertions(+), 149 deletions(-) diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index a33351373e..8096ce395a 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -13,13 +13,13 @@ * along with this program. If not, see . */ -#include "os.h" -#include "tdef.h" -#include "tname.h" +#include "catalog.h" #include "clientInt.h" #include "clientLog.h" -#include "catalog.h" +#include "os.h" #include "query.h" +#include "tdef.h" +#include "tname.h" int32_t (*handleRequestRspFp[TDMT_MAX])(void*, const SDataBuf* pMsg, int32_t code); @@ -50,7 +50,13 @@ int32_t processConnectRsp(void* param, const SDataBuf* pMsg, int32_t code) { SConnectRsp connectRsp = {0}; tDeserializeSConnectRsp(pMsg->pData, pMsg->len, &connectRsp); - assert(connectRsp.epSet.numOfEps > 0); + /*assert(connectRsp.epSet.numOfEps > 0);*/ + if (connectRsp.epSet.numOfEps == 0) { + taosMemoryFree(pMsg->pData); + setErrno(pRequest, TSDB_CODE_MND_APP_ERROR); + tsem_post(&pRequest->body.rspSem); + return code; + } if (!isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) { updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet); @@ -82,18 +88,20 @@ int32_t processConnectRsp(void* param, const SDataBuf* pMsg, int32_t code) { return 0; } -SMsgSendInfo* buildMsgInfoImpl(SRequestObj *pRequest) { +SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pRequest) { SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo)); pMsgSendInfo->requestObjRefId = pRequest->self; - pMsgSendInfo->requestId = pRequest->requestId; - pMsgSendInfo->param = pRequest; - pMsgSendInfo->msgType = pRequest->type; + pMsgSendInfo->requestId = pRequest->requestId; + pMsgSendInfo->param = pRequest; + pMsgSendInfo->msgType = pRequest->type; assert(pRequest != NULL); pMsgSendInfo->msgInfo = pRequest->body.requestMsg; - pMsgSendInfo->fp = (handleRequestRspFp[TMSG_INDEX(pRequest->type)] == NULL)? genericRspCallback:handleRequestRspFp[TMSG_INDEX(pRequest->type)]; + pMsgSendInfo->fp = (handleRequestRspFp[TMSG_INDEX(pRequest->type)] == NULL) + ? genericRspCallback + : handleRequestRspFp[TMSG_INDEX(pRequest->type)]; return pMsgSendInfo; } @@ -114,7 +122,7 @@ int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) { if (TSDB_CODE_MND_DB_NOT_EXIST == code) { SUseDbRsp usedbRsp = {0}; tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp); - struct SCatalog *pCatalog = NULL; + struct SCatalog* pCatalog = NULL; if (usedbRsp.vgVersion >= 0) { int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); diff --git a/source/libs/qworker/src/qworkerMsg.c b/source/libs/qworker/src/qworkerMsg.c index 6723eb21c1..311458de49 100644 --- a/source/libs/qworker/src/qworkerMsg.c +++ b/source/libs/qworker/src/qworkerMsg.c @@ -1,18 +1,17 @@ -#include "qworker.h" -#include "tcommon.h" +#include "qworkerMsg.h" +#include "dataSinkMgt.h" #include "executor.h" #include "planner.h" #include "query.h" +#include "qworker.h" #include "qworkerInt.h" -#include "qworkerMsg.h" +#include "tcommon.h" #include "tmsg.h" #include "tname.h" -#include "dataSinkMgt.h" - int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) { int32_t msgSize = sizeof(SRetrieveTableRsp) + length; - + SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(msgSize); if (NULL == pRsp) { qError("rpcMallocCont %d failed", msgSize); @@ -26,11 +25,9 @@ int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) { return TSDB_CODE_SUCCESS; } - - void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) { SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg; - + rsp->useconds = htobe64(input->useconds); rsp->completed = qComplete; rsp->precision = input->precision; @@ -39,7 +36,6 @@ void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) rsp->numOfRows = htonl(input->numOfRows); } - void qwFreeFetchRsp(void *msg) { if (msg) { rpcFreeCont(msg); @@ -48,18 +44,18 @@ void qwFreeFetchRsp(void *msg) { int32_t qwBuildAndSendQueryRsp(SQWConnInfo *pConn, int32_t code) { SQueryTableRsp rsp = {.code = code}; - + int32_t contLen = tSerializeSQueryTableRsp(NULL, 0, &rsp); - void *msg = rpcMallocCont(contLen); + void *msg = rpcMallocCont(contLen); tSerializeSQueryTableRsp(msg, contLen, &rsp); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_QUERY_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = msg, - .contLen = contLen, - .code = code, + .msgType = TDMT_VND_QUERY_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = msg, + .contLen = contLen, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -72,12 +68,12 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_RES_READY_RSP, - .handle = pConn->handle, - .ahandle = NULL, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_RES_READY_RSP, + .handle = pConn->handle, + .ahandle = NULL, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -85,20 +81,20 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { return TSDB_CODE_SUCCESS; } -int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { +int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { SExplainRsp rsp = {.numOfPlans = num, .subplanInfo = execInfo}; int32_t contLen = tSerializeSExplainRsp(NULL, 0, &rsp); - void *pRsp = rpcMallocCont(contLen); + void *pRsp = rpcMallocCont(contLen); tSerializeSExplainRsp(pRsp, contLen, &rsp); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_EXPLAIN_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = contLen, - .code = 0, + .msgType = TDMT_VND_EXPLAIN_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = contLen, + .code = 0, }; tmsgSendRsp(&rpcRsp); @@ -108,16 +104,16 @@ int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t qwBuildAndSendHbRsp(SQWConnInfo *pConn, SSchedulerHbRsp *pStatus, int32_t code) { int32_t contLen = tSerializeSSchedulerHbRsp(NULL, 0, pStatus); - void *pRsp = rpcMallocCont(contLen); + void *pRsp = rpcMallocCont(contLen); tSerializeSSchedulerHbRsp(pRsp, contLen, pStatus); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_QUERY_HEARTBEAT_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = contLen, - .code = code, + .msgType = TDMT_VND_QUERY_HEARTBEAT_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = contLen, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -133,12 +129,12 @@ int32_t qwBuildAndSendFetchRsp(SQWConnInfo *pConn, SRetrieveTableRsp *pRsp, int3 } SRpcMsg rpcRsp = { - .msgType = TDMT_VND_FETCH_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp) + dataLength, - .code = code, + .msgType = TDMT_VND_FETCH_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = sizeof(*pRsp) + dataLength, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -151,12 +147,12 @@ int32_t qwBuildAndSendCancelRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_CANCEL_TASK_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_CANCEL_TASK_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -168,12 +164,12 @@ int32_t qwBuildAndSendDropRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_DROP_TASK_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_DROP_TASK_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -191,7 +187,7 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { return -1; } - col_id_t cols = 0; + col_id_t cols = 0; SSchema *pSchema = showRsp.tableMeta.pSchemas; const SSchema *s = tGetTbnameColumnSchema(); @@ -235,17 +231,17 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { return TSDB_CODE_SUCCESS; } -int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq* pFetchReq) { +int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq *pFetchReq) { SVShowTablesFetchRsp *pRsp = (SVShowTablesFetchRsp *)rpcMallocCont(sizeof(SVShowTablesFetchRsp)); - int32_t handle = htonl(pFetchReq->id); + int32_t handle = htonl(pFetchReq->id); pRsp->numOfRows = 0; SRpcMsg rpcMsg = { - .handle = pMsg->handle, + .handle = pMsg->handle, .ahandle = pMsg->ahandle, - .pCont = pRsp, + .pCont = pRsp, .contLen = sizeof(*pRsp), - .code = 0, + .code = 0, }; tmsgSendRsp(&rpcMsg); @@ -253,7 +249,7 @@ int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq* pFetchRe } int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { - SQueryContinueReq * req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq)); + SQueryContinueReq *req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq)); if (NULL == req) { QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(SQueryContinueReq)); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); @@ -265,12 +261,12 @@ int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { req->taskId = tId; SRpcMsg pNewMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_QUERY_CONTINUE, - .pCont = req, - .contLen = sizeof(SQueryContinueReq), - .code = 0, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_QUERY_CONTINUE, + .pCont = req, + .contLen = sizeof(SQueryContinueReq), + .code = 0, }; int32_t code = tmsgPutToQueue(&mgmt->msgCb, QUERY_QUEUE, &pNewMsg); @@ -285,29 +281,28 @@ int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { return TSDB_CODE_SUCCESS; } - int32_t qwRegisterQueryBrokenLinkArg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { - STaskDropReq * req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq)); + STaskDropReq *req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq)); if (NULL == req) { QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(STaskDropReq)); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } + } req->header.vgId = htonl(mgmt->nodeId); req->sId = htobe64(sId); req->queryId = htobe64(qId); req->taskId = htobe64(tId); req->refId = htobe64(rId); - + SRpcMsg pMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_DROP_TASK, - .pCont = req, - .contLen = sizeof(STaskDropReq), - .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_DROP_TASK, + .pCont = req, + .contLen = sizeof(STaskDropReq), + .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; - + tmsgRegisterBrokenLinkArg(&mgmt->msgCb, &pMsg); return TSDB_CODE_SUCCESS; @@ -333,43 +328,41 @@ int32_t qwRegisterHbBrokenLinkArg(SQWorkerMgmt *mgmt, uint64_t sId, SQWConnInfo taosMemoryFree(msg); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - + SRpcMsg pMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_QUERY_HEARTBEAT, - .pCont = msg, - .contLen = sizeof(SSchedulerHbReq), - .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_QUERY_HEARTBEAT, + .pCont = msg, + .contLen = msgSize, + .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; - + tmsgRegisterBrokenLinkArg(&mgmt->msgCb, &pMsg); return TSDB_CODE_SUCCESS; } - - int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) { QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - int32_t code = 0; + int32_t code = 0; SSubQueryMsg *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen <= sizeof(*msg)) { QW_ELOG("invalid query msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - msg->sId = be64toh(msg->sId); + msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); - msg->taskId = be64toh(msg->taskId); - msg->refId = be64toh(msg->refId); - msg->phyLen = ntohl(msg->phyLen); - msg->sqlLen = ntohl(msg->sqlLen); + msg->taskId = be64toh(msg->taskId); + msg->refId = be64toh(msg->refId); + msg->phyLen = ntohl(msg->phyLen); + msg->sqlLen = ntohl(msg->sqlLen); uint64_t sId = msg->sId; uint64_t qId = msg->queryId; @@ -380,7 +373,7 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; - char* sql = strndup(msg->msg, msg->sqlLen); + char *sql = strndup(msg->msg, msg->sqlLen); QW_SCH_TASK_DLOG("processQuery start, node:%p, handle:%p, sql:%s", node, pMsg->handle, sql); taosMemoryFreeClear(sql); @@ -388,17 +381,17 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processQuery end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { - int32_t code = 0; - int8_t status = 0; - bool queryDone = false; + int32_t code = 0; + int8_t status = 0; + bool queryDone = false; SQueryContinueReq *msg = (SQueryContinueReq *)pMsg->pCont; - bool needStop = false; - SQWTaskCtx *handles = NULL; - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + bool needStop = false; + SQWTaskCtx *handles = NULL; + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid cquery msg, msg:%p, msgLen:%d", msg, pMsg->contLen); @@ -408,7 +401,7 @@ int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; @@ -420,10 +413,10 @@ int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processCQuery end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } -int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ +int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } @@ -433,7 +426,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid task ready msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -442,7 +435,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; @@ -453,7 +446,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ QW_ERR_RET(qwProcessReady(QW_FPARAMS(), &qwMsg)); QW_SCH_TASK_DLOG("processReady end, node:%p", node); - + return TSDB_CODE_SUCCESS; } @@ -462,24 +455,24 @@ int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SSchTasksStatusReq *msg = pMsg->pCont; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { qError("invalid task status msg"); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; msg->sId = htobe64(msg->sId); uint64_t sId = msg->sId; SSchedulerStatusRsp *sStatus = NULL; - - //QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus)); + + // QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus)); _return: - //QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus)); + // QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus)); return TSDB_CODE_SUCCESS; } @@ -491,11 +484,11 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { SResFetchReq *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen < sizeof(*msg)) { - QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen); + QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -504,7 +497,7 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; @@ -516,7 +509,7 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processFetch end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t qWorkerProcessFetchRsp(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { @@ -529,13 +522,13 @@ int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - int32_t code = 0; + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + int32_t code = 0; STaskCancelReq *msg = pMsg->pCont; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { - qError("invalid task cancel msg"); + qError("invalid task cancel msg"); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -551,7 +544,7 @@ int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; - //QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId)); + // QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId)); _return: @@ -566,14 +559,14 @@ int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; STaskDropReq *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid task drop msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -590,7 +583,7 @@ int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { qwMsg.connInfo.ahandle = pMsg->ahandle; if (TSDB_CODE_RPC_NETWORK_UNAVAIL == pMsg->code) { - QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code)); + QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code)); } QW_SCH_TASK_DLOG("processDrop start, node:%p, handle:%p", node, pMsg->handle); @@ -607,14 +600,14 @@ int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SSchedulerHbReq req = {0}; - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + if (NULL == pMsg->pCont) { QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } if (tDeserializeSSchedulerHbReq(pMsg->pCont, pMsg->contLen, &req)) { QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen); @@ -623,12 +616,12 @@ int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { } uint64_t sId = req.sId; - SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; + SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; if (TSDB_CODE_RPC_NETWORK_UNAVAIL == pMsg->code) { - QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code)); + QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code)); } QW_SCH_DLOG("processHb start, node:%p, handle:%p", node, pMsg->handle); @@ -645,7 +638,7 @@ int32_t qWorkerProcessShowMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SVShowTablesReq *pReq = pMsg->pCont; QW_RET(qwBuildAndSendShowRsp(pMsg, code)); } From eda3b192255341a70dd21db4ba5b629b3d42188d Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Sun, 24 Apr 2022 18:39:43 +0800 Subject: [PATCH 12/51] fix compile --- source/libs/qworker/src/qworkerMsg.c | 269 ++++++++++++++------------- 1 file changed, 138 insertions(+), 131 deletions(-) diff --git a/source/libs/qworker/src/qworkerMsg.c b/source/libs/qworker/src/qworkerMsg.c index 311458de49..e0bcfabdd3 100644 --- a/source/libs/qworker/src/qworkerMsg.c +++ b/source/libs/qworker/src/qworkerMsg.c @@ -1,17 +1,18 @@ -#include "qworkerMsg.h" -#include "dataSinkMgt.h" +#include "qworker.h" +#include "tcommon.h" #include "executor.h" #include "planner.h" #include "query.h" -#include "qworker.h" #include "qworkerInt.h" -#include "tcommon.h" +#include "qworkerMsg.h" #include "tmsg.h" #include "tname.h" +#include "dataSinkMgt.h" + int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) { int32_t msgSize = sizeof(SRetrieveTableRsp) + length; - + SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(msgSize); if (NULL == pRsp) { qError("rpcMallocCont %d failed", msgSize); @@ -25,9 +26,11 @@ int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) { return TSDB_CODE_SUCCESS; } + + void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) { SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg; - + rsp->useconds = htobe64(input->useconds); rsp->completed = qComplete; rsp->precision = input->precision; @@ -36,6 +39,7 @@ void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) rsp->numOfRows = htonl(input->numOfRows); } + void qwFreeFetchRsp(void *msg) { if (msg) { rpcFreeCont(msg); @@ -44,18 +48,18 @@ void qwFreeFetchRsp(void *msg) { int32_t qwBuildAndSendQueryRsp(SQWConnInfo *pConn, int32_t code) { SQueryTableRsp rsp = {.code = code}; - + int32_t contLen = tSerializeSQueryTableRsp(NULL, 0, &rsp); - void *msg = rpcMallocCont(contLen); + void *msg = rpcMallocCont(contLen); tSerializeSQueryTableRsp(msg, contLen, &rsp); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_QUERY_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = msg, - .contLen = contLen, - .code = code, + .msgType = TDMT_VND_QUERY_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = msg, + .contLen = contLen, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -68,12 +72,12 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_RES_READY_RSP, - .handle = pConn->handle, - .ahandle = NULL, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_RES_READY_RSP, + .handle = pConn->handle, + .ahandle = NULL, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -81,20 +85,20 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { return TSDB_CODE_SUCCESS; } -int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { +int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { SExplainRsp rsp = {.numOfPlans = num, .subplanInfo = execInfo}; int32_t contLen = tSerializeSExplainRsp(NULL, 0, &rsp); - void *pRsp = rpcMallocCont(contLen); + void *pRsp = rpcMallocCont(contLen); tSerializeSExplainRsp(pRsp, contLen, &rsp); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_EXPLAIN_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = contLen, - .code = 0, + .msgType = TDMT_VND_EXPLAIN_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = contLen, + .code = 0, }; tmsgSendRsp(&rpcRsp); @@ -104,16 +108,16 @@ int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t qwBuildAndSendHbRsp(SQWConnInfo *pConn, SSchedulerHbRsp *pStatus, int32_t code) { int32_t contLen = tSerializeSSchedulerHbRsp(NULL, 0, pStatus); - void *pRsp = rpcMallocCont(contLen); + void *pRsp = rpcMallocCont(contLen); tSerializeSSchedulerHbRsp(pRsp, contLen, pStatus); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_QUERY_HEARTBEAT_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = contLen, - .code = code, + .msgType = TDMT_VND_QUERY_HEARTBEAT_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = contLen, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -129,12 +133,12 @@ int32_t qwBuildAndSendFetchRsp(SQWConnInfo *pConn, SRetrieveTableRsp *pRsp, int3 } SRpcMsg rpcRsp = { - .msgType = TDMT_VND_FETCH_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp) + dataLength, - .code = code, + .msgType = TDMT_VND_FETCH_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = sizeof(*pRsp) + dataLength, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -147,12 +151,12 @@ int32_t qwBuildAndSendCancelRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_CANCEL_TASK_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_CANCEL_TASK_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -164,12 +168,12 @@ int32_t qwBuildAndSendDropRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_DROP_TASK_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_DROP_TASK_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -187,7 +191,7 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { return -1; } - col_id_t cols = 0; + col_id_t cols = 0; SSchema *pSchema = showRsp.tableMeta.pSchemas; const SSchema *s = tGetTbnameColumnSchema(); @@ -231,17 +235,17 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { return TSDB_CODE_SUCCESS; } -int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq *pFetchReq) { +int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq* pFetchReq) { SVShowTablesFetchRsp *pRsp = (SVShowTablesFetchRsp *)rpcMallocCont(sizeof(SVShowTablesFetchRsp)); - int32_t handle = htonl(pFetchReq->id); + int32_t handle = htonl(pFetchReq->id); pRsp->numOfRows = 0; SRpcMsg rpcMsg = { - .handle = pMsg->handle, + .handle = pMsg->handle, .ahandle = pMsg->ahandle, - .pCont = pRsp, + .pCont = pRsp, .contLen = sizeof(*pRsp), - .code = 0, + .code = 0, }; tmsgSendRsp(&rpcMsg); @@ -249,7 +253,7 @@ int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq *pFetchRe } int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { - SQueryContinueReq *req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq)); + SQueryContinueReq * req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq)); if (NULL == req) { QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(SQueryContinueReq)); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); @@ -261,12 +265,12 @@ int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { req->taskId = tId; SRpcMsg pNewMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_QUERY_CONTINUE, - .pCont = req, - .contLen = sizeof(SQueryContinueReq), - .code = 0, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_QUERY_CONTINUE, + .pCont = req, + .contLen = sizeof(SQueryContinueReq), + .code = 0, }; int32_t code = tmsgPutToQueue(&mgmt->msgCb, QUERY_QUEUE, &pNewMsg); @@ -281,28 +285,29 @@ int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { return TSDB_CODE_SUCCESS; } + int32_t qwRegisterQueryBrokenLinkArg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { - STaskDropReq *req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq)); + STaskDropReq * req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq)); if (NULL == req) { QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(STaskDropReq)); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } + } req->header.vgId = htonl(mgmt->nodeId); req->sId = htobe64(sId); req->queryId = htobe64(qId); req->taskId = htobe64(tId); req->refId = htobe64(rId); - + SRpcMsg pMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_DROP_TASK, - .pCont = req, - .contLen = sizeof(STaskDropReq), - .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_DROP_TASK, + .pCont = req, + .contLen = sizeof(STaskDropReq), + .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; - + tmsgRegisterBrokenLinkArg(&mgmt->msgCb, &pMsg); return TSDB_CODE_SUCCESS; @@ -328,41 +333,43 @@ int32_t qwRegisterHbBrokenLinkArg(SQWorkerMgmt *mgmt, uint64_t sId, SQWConnInfo taosMemoryFree(msg); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - + SRpcMsg pMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_QUERY_HEARTBEAT, - .pCont = msg, - .contLen = msgSize, - .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_QUERY_HEARTBEAT, + .pCont = msg, + .contLen = msgSize, + .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; - + tmsgRegisterBrokenLinkArg(&mgmt->msgCb, &pMsg); return TSDB_CODE_SUCCESS; } + + int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) { QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - int32_t code = 0; + int32_t code = 0; SSubQueryMsg *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen <= sizeof(*msg)) { QW_ELOG("invalid query msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - msg->sId = be64toh(msg->sId); + msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); - msg->taskId = be64toh(msg->taskId); - msg->refId = be64toh(msg->refId); - msg->phyLen = ntohl(msg->phyLen); - msg->sqlLen = ntohl(msg->sqlLen); + msg->taskId = be64toh(msg->taskId); + msg->refId = be64toh(msg->refId); + msg->phyLen = ntohl(msg->phyLen); + msg->sqlLen = ntohl(msg->sqlLen); uint64_t sId = msg->sId; uint64_t qId = msg->queryId; @@ -373,7 +380,7 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; - char *sql = strndup(msg->msg, msg->sqlLen); + char* sql = strndup(msg->msg, msg->sqlLen); QW_SCH_TASK_DLOG("processQuery start, node:%p, handle:%p, sql:%s", node, pMsg->handle, sql); taosMemoryFreeClear(sql); @@ -381,17 +388,17 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processQuery end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { - int32_t code = 0; - int8_t status = 0; - bool queryDone = false; + int32_t code = 0; + int8_t status = 0; + bool queryDone = false; SQueryContinueReq *msg = (SQueryContinueReq *)pMsg->pCont; - bool needStop = false; - SQWTaskCtx *handles = NULL; - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + bool needStop = false; + SQWTaskCtx *handles = NULL; + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid cquery msg, msg:%p, msgLen:%d", msg, pMsg->contLen); @@ -401,7 +408,7 @@ int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; @@ -413,10 +420,10 @@ int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processCQuery end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } -int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { +int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } @@ -426,7 +433,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid task ready msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -435,7 +442,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; @@ -446,7 +453,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_ERR_RET(qwProcessReady(QW_FPARAMS(), &qwMsg)); QW_SCH_TASK_DLOG("processReady end, node:%p", node); - + return TSDB_CODE_SUCCESS; } @@ -455,24 +462,24 @@ int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SSchTasksStatusReq *msg = pMsg->pCont; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { qError("invalid task status msg"); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; msg->sId = htobe64(msg->sId); uint64_t sId = msg->sId; SSchedulerStatusRsp *sStatus = NULL; - - // QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus)); + + //QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus)); _return: - // QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus)); + //QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus)); return TSDB_CODE_SUCCESS; } @@ -484,11 +491,11 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { SResFetchReq *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen < sizeof(*msg)) { - QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen); + QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -497,7 +504,7 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; @@ -509,7 +516,7 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processFetch end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t qWorkerProcessFetchRsp(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { @@ -522,13 +529,13 @@ int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - int32_t code = 0; + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + int32_t code = 0; STaskCancelReq *msg = pMsg->pCont; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { - qError("invalid task cancel msg"); + qError("invalid task cancel msg"); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -544,7 +551,7 @@ int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; - // QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId)); + //QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId)); _return: @@ -559,14 +566,14 @@ int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; STaskDropReq *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid task drop msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -583,7 +590,7 @@ int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { qwMsg.connInfo.ahandle = pMsg->ahandle; if (TSDB_CODE_RPC_NETWORK_UNAVAIL == pMsg->code) { - QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code)); + QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code)); } QW_SCH_TASK_DLOG("processDrop start, node:%p, handle:%p", node, pMsg->handle); @@ -600,14 +607,14 @@ int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SSchedulerHbReq req = {0}; - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + if (NULL == pMsg->pCont) { QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } if (tDeserializeSSchedulerHbReq(pMsg->pCont, pMsg->contLen, &req)) { QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen); @@ -616,12 +623,12 @@ int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { } uint64_t sId = req.sId; - SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; + SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; if (TSDB_CODE_RPC_NETWORK_UNAVAIL == pMsg->code) { - QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code)); + QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code)); } QW_SCH_DLOG("processHb start, node:%p, handle:%p", node, pMsg->handle); @@ -638,7 +645,7 @@ int32_t qWorkerProcessShowMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SVShowTablesReq *pReq = pMsg->pCont; QW_RET(qwBuildAndSendShowRsp(pMsg, code)); } From bb33139572f045ef2ad900d1d592e943072ef317 Mon Sep 17 00:00:00 2001 From: dapan Date: Sun, 24 Apr 2022 20:11:34 +0800 Subject: [PATCH 13/51] feature/qnode --- include/libs/catalog/catalog.h | 2 +- source/common/src/tmsg.c | 11 +- source/dnode/mgmt/mgmt_mnode/src/mmHandle.c | 1 + source/dnode/mnode/impl/src/mndQnode.c | 5 +- source/dnode/qnode/src/qnode.c | 2 +- source/dnode/vnode/src/vnd/vnodeSvr.c | 2 +- source/libs/catalog/src/catalog.c | 9 +- source/libs/qcom/src/querymsg.c | 16 +-- source/libs/scalar/inc/sclInt.h | 2 +- source/libs/scalar/src/scalar.c | 36 +++--- .../libs/scalar/test/scalar/scalarTests.cpp | 116 ++++++++++++++++++ 11 files changed, 161 insertions(+), 41 deletions(-) diff --git a/include/libs/catalog/catalog.h b/include/libs/catalog/catalog.h index b88afcb39d..30d1bd0a51 100644 --- a/include/libs/catalog/catalog.h +++ b/include/libs/catalog/catalog.h @@ -51,7 +51,7 @@ typedef struct SMetaData { SArray *pTableMeta; // STableMeta array SArray *pVgroupInfo; // SVgroupInfo list SArray *pUdfList; // udf info list - SArray *pEpSetList; // qnode epset list, SArray + SArray *pQnodeList; // qnode list, SArray } SMetaData; typedef struct SCatalogCfg { diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index eb7c9a763f..fd4b071e64 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2091,10 +2091,15 @@ int32_t tDeserializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp if (tStartDecode(&decoder) < 0) return -1; int32_t num = 0; if (tDecodeI32(&decoder, &num) < 0) return -1; - pRsp->addrsList = taosArrayInit(num, sizeof(SQueryNodeAddr)); - if (NULL == pRsp->addrsList) return -1; + if (NULL == pRsp->addrsList) { + pRsp->addrsList = taosArrayInit(num, sizeof(SQueryNodeAddr)); + if (NULL == pRsp->addrsList) return -1; + } + for (int32_t i = 0; i < num; ++i) { - if (tDecodeSQueryNodeAddr(&decoder, TARRAY_GET_ELEM(pRsp->addrsList, i)) < 0) return -1; + SQueryNodeAddr addr = {0}; + if (tDecodeSQueryNodeAddr(&decoder, &addr) < 0) return -1; + taosArrayPush(pRsp->addrsList, &addr); } tEndDecode(&decoder); diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c b/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c index 159e14a3d5..7c874f63e1 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c @@ -177,6 +177,7 @@ void mmInitMsgHandle(SMgmtWrapper *pWrapper) { 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); diff --git a/source/dnode/mnode/impl/src/mndQnode.c b/source/dnode/mnode/impl/src/mndQnode.c index 1c03ca30f4..cec5933ba3 100644 --- a/source/dnode/mnode/impl/src/mndQnode.c +++ b/source/dnode/mnode/impl/src/mndQnode.c @@ -451,8 +451,9 @@ static int32_t mndProcessQnodeListReq(SNodeMsg *pReq) { goto _OVER; } + void *pIter = NULL; while (1) { - void *pIter = sdbFetch(pSdb, SDB_QNODE, NULL, (void **)&pObj); + pIter = sdbFetch(pSdb, SDB_QNODE, pIter, (void **)&pObj); if (pIter == NULL) break; SQueryNodeAddr nodeAddr = {0}; @@ -472,7 +473,7 @@ static int32_t mndProcessQnodeListReq(SNodeMsg *pReq) { } int32_t rspLen = tSerializeSQnodeListRsp(NULL, 0, &qlistRsp); - void *pRsp = taosMemoryMalloc(rspLen); + void *pRsp = rpcMallocCont(rspLen); if (pRsp == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; goto _OVER; diff --git a/source/dnode/qnode/src/qnode.c b/source/dnode/qnode/src/qnode.c index 7b299f1f3c..e3d7621fb0 100644 --- a/source/dnode/qnode/src/qnode.c +++ b/source/dnode/qnode/src/qnode.c @@ -43,7 +43,7 @@ void qndClose(SQnode *pQnode) { int32_t qndGetLoad(SQnode *pQnode, SQnodeLoad *pLoad) { return 0; } int32_t qndProcessQueryMsg(SQnode *pQnode, SRpcMsg *pMsg) { - qTrace("message in query queue is processing"); + qTrace("message in qnode query queue is processing"); SReadHandle handle = {0}; switch (pMsg->msgType) { diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index aa90a04478..e3c504c93d 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -135,7 +135,7 @@ int vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg } int vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) { - vTrace("message in query queue is processing"); + vTrace("message in vnode query queue is processing"); SReadHandle handle = {.reader = pVnode->pTsdb, .meta = pVnode->pMeta, .config = &pVnode->config}; switch (pMsg->msgType) { diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index 410527c9e6..272c370ca3 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -494,7 +494,7 @@ _return: return TSDB_CODE_SUCCESS; } -int32_t ctgGetQnodeListFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray **out) { +int32_t ctgGetQnodeListFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, SArray *out) { char *msg = NULL; int32_t msgLen = 0; @@ -526,7 +526,7 @@ int32_t ctgGetQnodeListFromMnode(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmt CTG_ERR_RET(code); } - ctgDebug("Got qnode list from mnode, listNum:%d", (int32_t)taosArrayGetSize(*out)); + ctgDebug("Got qnode list from mnode, listNum:%d", (int32_t)taosArrayGetSize(out)); return TSDB_CODE_SUCCESS; } @@ -2778,7 +2778,8 @@ int32_t catalogGetAllMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, } if (pReq->qNodeRequired) { - CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pTrans, pMgmtEps, &pRsp->pEpSetList)); + pRsp->pQnodeList = taosArrayInit(10, sizeof(SQueryNodeAddr)); + CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pTrans, pMgmtEps, pRsp->pQnodeList)); } CTG_API_LEAVE(TSDB_CODE_SUCCESS); @@ -2807,7 +2808,7 @@ int32_t catalogGetQnodeList(SCatalog* pCtg, void *pRpc, const SEpSet* pMgmtEps, CTG_API_LEAVE(TSDB_CODE_CTG_INVALID_INPUT); } - CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pRpc, pMgmtEps, &pQnodeList)); + CTG_ERR_JRET(ctgGetQnodeListFromMnode(pCtg, pRpc, pMgmtEps, pQnodeList)); _return: diff --git a/source/libs/qcom/src/querymsg.c b/source/libs/qcom/src/querymsg.c index de805d2c77..fd7c831399 100644 --- a/source/libs/qcom/src/querymsg.c +++ b/source/libs/qcom/src/querymsg.c @@ -342,28 +342,20 @@ PROCESS_META_OVER: int32_t queryProcessQnodeListRsp(void *output, char *msg, int32_t msgSize) { SQnodeListRsp out = {0}; - int32_t code = -1; + int32_t code = 0; if (NULL == output || NULL == msg || msgSize <= 0) { code = TSDB_CODE_TSC_INVALID_INPUT; - goto PROCESS_QLIST_OVER; + return code; } + out.addrsList = (SArray *)output; if (tDeserializeSQnodeListRsp(msg, msgSize, &out) != 0) { qError("invalid qnode list rsp msg, msgSize:%d", msgSize); code = TSDB_CODE_INVALID_MSG; - goto PROCESS_QLIST_OVER; + return code; } -PROCESS_QLIST_OVER: - - if (code != 0) { - tFreeSQnodeListRsp(&out); - out.addrsList = NULL; - } - - *(SArray **)output = out.addrsList; - return code; } diff --git a/source/libs/scalar/inc/sclInt.h b/source/libs/scalar/inc/sclInt.h index 399fb8f47d..9142cc41c4 100644 --- a/source/libs/scalar/inc/sclInt.h +++ b/source/libs/scalar/inc/sclInt.h @@ -32,7 +32,7 @@ typedef struct SScalarCtx { #define SCL_DATA_TYPE_DUMMY_HASH 9000 #define SCL_DEFAULT_OP_NUM 10 -#define SCL_IS_CONST_NODE(_node) ((NULL == (_node)) || (QUERY_NODE_VALUE == (_node)->type)) +#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) #define sclFatal(...) qFatal(__VA_ARGS__) diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 4783a2701e..6b2e7c17d3 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -244,9 +244,8 @@ int32_t sclInitParam(SNode* node, SScalarParam *param, SScalarCtx *ctx, int32_t return TSDB_CODE_SUCCESS; } -int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *rowNum) { +int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *paramNum, int32_t *rowNum) { int32_t code = 0; - int32_t paramNum = 0; if (NULL == pParamList) { if (ctx->pBlockList) { SSDataBlock *pBlock = taosArrayGet(ctx->pBlockList, 0); @@ -255,14 +254,14 @@ int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarC *rowNum = 1; } - paramNum = 1; + *paramNum = 1; } else { - paramNum = pParamList->length; + *paramNum = pParamList->length; } - SScalarParam *paramList = taosMemoryCalloc(paramNum, sizeof(SScalarParam)); + SScalarParam *paramList = taosMemoryCalloc(*paramNum, sizeof(SScalarParam)); if (NULL == paramList) { - sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam))); + sclError("calloc %d failed", (int32_t)((*paramNum) * sizeof(SScalarParam))); SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } @@ -272,14 +271,13 @@ int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarC if (SCL_IS_CONST_CALC(ctx)) { WHERE_EACH (tnode, pParamList) { if (!SCL_IS_CONST_NODE(tnode)) { - continue; + WHERE_NEXT; } else { SCL_ERR_JRET(sclInitParam(tnode, ¶mList[i], ctx, rowNum)); ERASE_NODE(pParamList); } ++i; - WHERE_NEXT; } } else { FOREACH(tnode, pParamList) { @@ -340,7 +338,8 @@ int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *outp SScalarParam *params = NULL; int32_t rowNum = 0; - SCL_ERR_RET(sclInitParamList(¶ms, node->pParameterList, ctx, &rowNum)); + int32_t paramNum = 0; + SCL_ERR_RET(sclInitParamList(¶ms, node->pParameterList, ctx, ¶mNum, &rowNum)); output->columnData = createColumnInfoData(&node->node.resType, rowNum); if (output->columnData == NULL) { @@ -348,7 +347,7 @@ int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *outp SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - code = (*ffpSet.process)(params, node->pParameterList->length, output); + code = (*ffpSet.process)(params, paramNum, output); if (code) { sclError("scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code)); SCL_ERR_JRET(code); @@ -356,7 +355,7 @@ int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *outp _return: - for (int32_t i = 0; i < node->pParameterList->length; ++i) { + for (int32_t i = 0; i < paramNum; ++i) { // sclFreeParamNoData(params + i); } @@ -382,8 +381,9 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o SScalarParam *params = NULL; int32_t rowNum = 0; + int32_t paramNum = 0; int32_t code = 0; - SCL_ERR_RET(sclInitParamList(¶ms, node->pParameterList, ctx, &rowNum)); + SCL_ERR_RET(sclInitParamList(¶ms, node->pParameterList, ctx, ¶mNum, &rowNum)); if (NULL == params) { output->numOfRows = 0; return TSDB_CODE_SUCCESS; @@ -400,10 +400,12 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o } bool value = false; - bool complete = false; + bool complete = true; for (int32_t i = 0; i < rowNum; ++i) { - for (int32_t m = 0; m < node->pParameterList->length; ++m) { + complete = true; + for (int32_t m = 0; m < paramNum; ++m) { if (NULL == params[m].columnData) { + complete = false; continue; } char* p = colDataGetData(params[m].columnData, i); @@ -420,7 +422,9 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o } } - colDataAppend(output->columnData, i, (char*) &value, false); + if (complete) { + colDataAppend(output->columnData, i, (char*) &value, false); + } } if (SCL_IS_CONST_CALC(ctx) && (false == complete)) { @@ -430,7 +434,7 @@ int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *o _return: - for (int32_t i = 0; i < node->pParameterList->length; ++i) { + for (int32_t i = 0; i < paramNum; ++i) { // sclFreeParamNoData(params + i); } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 04ab26d8a4..ed52e48f6d 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -137,6 +137,11 @@ void scltMakeColumnNode(SNode **pNode, SSDataBlock **block, int32_t dataType, in rnode->node.resType.bytes = dataBytes; rnode->dataBlockId = 0; + if (NULL == block) { + *pNode = (SNode *)rnode; + return; + } + if (NULL == *block) { SSDataBlock *res = (SSDataBlock *)taosMemoryCalloc(1, sizeof(SSDataBlock)); res->info.numOfCols = 3; @@ -889,6 +894,8 @@ TEST(constantTest, int_greater_int_is_true2) { } TEST(constantTest, greater_and_lower) { + scltInitLogFile(); + SNode *pval1 = NULL, *pval2 = NULL, *opNode1 = NULL, *opNode2 = NULL, *logicNode = NULL, *res = NULL; bool eRes[5] = {false, false, true, true, true}; int64_t v1 = 333, v2 = 222, v3 = -10, v4 = 20; @@ -913,6 +920,115 @@ TEST(constantTest, greater_and_lower) { nodesDestroyNode(res); } +TEST(constantTest, column_and_value1) { + scltInitLogFile(); + + SNode *pval1 = NULL, *pval2 = NULL, *opNode1 = NULL, *opNode2 = NULL, *logicNode = NULL, *res = NULL; + bool eRes[5] = {false, false, true, true, true}; + int64_t v1 = 333, v2 = 222, v3 = -10, v4 = 20; + SNode *list[2] = {0}; + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v1); + scltMakeValueNode(&pval2, TSDB_DATA_TYPE_BIGINT, &v2); + scltMakeOpNode(&opNode1, OP_TYPE_GREATER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v3); + scltMakeColumnNode(&pval2, NULL, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t), 0, NULL); + scltMakeOpNode(&opNode2, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + list[0] = opNode1; + list[1] = opNode2; + scltMakeLogicNode(&logicNode, LOGIC_COND_TYPE_AND, list, 2); + + int32_t code = scalarCalculateConstants(logicNode, &res); + ASSERT_EQ(code, 0); + ASSERT_TRUE(res); + ASSERT_EQ(nodeType(res), QUERY_NODE_LOGIC_CONDITION); + SLogicConditionNode *v = (SLogicConditionNode *)res; + ASSERT_EQ(v->condType, LOGIC_COND_TYPE_AND); + ASSERT_EQ(v->pParameterList->length, 1); + nodesDestroyNode(res); +} + +TEST(constantTest, column_and_value2) { + scltInitLogFile(); + + SNode *pval1 = NULL, *pval2 = NULL, *opNode1 = NULL, *opNode2 = NULL, *logicNode = NULL, *res = NULL; + bool eRes[5] = {false, false, true, true, true}; + int64_t v1 = 333, v2 = 222, v3 = -10, v4 = 20; + SNode *list[2] = {0}; + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v1); + scltMakeValueNode(&pval2, TSDB_DATA_TYPE_BIGINT, &v2); + scltMakeOpNode(&opNode1, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v3); + scltMakeColumnNode(&pval2, NULL, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t), 0, NULL); + scltMakeOpNode(&opNode2, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + list[0] = opNode1; + list[1] = opNode2; + scltMakeLogicNode(&logicNode, LOGIC_COND_TYPE_AND, list, 2); + + int32_t code = scalarCalculateConstants(logicNode, &res); + ASSERT_EQ(code, 0); + ASSERT_TRUE(res); + ASSERT_EQ(nodeType(res), QUERY_NODE_VALUE); + SValueNode *v = (SValueNode *)res; + ASSERT_EQ(v->node.resType.type, TSDB_DATA_TYPE_BOOL); + ASSERT_EQ(v->datum.b, false); + nodesDestroyNode(res); +} + +TEST(constantTest, column_and_value3) { + scltInitLogFile(); + + SNode *pval1 = NULL, *pval2 = NULL, *opNode1 = NULL, *opNode2 = NULL, *logicNode = NULL, *res = NULL; + bool eRes[5] = {false, false, true, true, true}; + int64_t v1 = 333, v2 = 222, v3 = -10, v4 = 20; + SNode *list[2] = {0}; + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v1); + scltMakeValueNode(&pval2, TSDB_DATA_TYPE_BIGINT, &v2); + scltMakeOpNode(&opNode1, OP_TYPE_GREATER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v3); + scltMakeColumnNode(&pval2, NULL, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t), 0, NULL); + scltMakeOpNode(&opNode2, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + list[0] = opNode1; + list[1] = opNode2; + scltMakeLogicNode(&logicNode, LOGIC_COND_TYPE_OR, list, 2); + + int32_t code = scalarCalculateConstants(logicNode, &res); + ASSERT_EQ(code, 0); + ASSERT_TRUE(res); + ASSERT_EQ(nodeType(res), QUERY_NODE_VALUE); + SValueNode *v = (SValueNode *)res; + ASSERT_EQ(v->node.resType.type, TSDB_DATA_TYPE_BOOL); + ASSERT_EQ(v->datum.b, true); + nodesDestroyNode(res); +} + +TEST(constantTest, column_and_value4) { + scltInitLogFile(); + + SNode *pval1 = NULL, *pval2 = NULL, *opNode1 = NULL, *opNode2 = NULL, *logicNode = NULL, *res = NULL; + bool eRes[5] = {false, false, true, true, true}; + int64_t v1 = 333, v2 = 222, v3 = -10, v4 = 20; + SNode *list[2] = {0}; + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v1); + scltMakeValueNode(&pval2, TSDB_DATA_TYPE_BIGINT, &v2); + scltMakeOpNode(&opNode1, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + scltMakeValueNode(&pval1, TSDB_DATA_TYPE_BIGINT, &v3); + scltMakeColumnNode(&pval2, NULL, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t), 0, NULL); + scltMakeOpNode(&opNode2, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_BOOL, pval1, pval2); + list[0] = opNode1; + list[1] = opNode2; + scltMakeLogicNode(&logicNode, LOGIC_COND_TYPE_OR, list, 2); + + int32_t code = scalarCalculateConstants(logicNode, &res); + ASSERT_EQ(code, 0); + ASSERT_TRUE(res); + ASSERT_EQ(nodeType(res), QUERY_NODE_LOGIC_CONDITION); + SLogicConditionNode *v = (SLogicConditionNode *)res; + ASSERT_EQ(v->condType, LOGIC_COND_TYPE_OR); + ASSERT_EQ(v->pParameterList->length, 1); + nodesDestroyNode(res); +} + + void makeJsonArrow(SSDataBlock **src, SNode **opNode, void *json, char *key){ char keyVar[32] = {0}; memcpy(varDataVal(keyVar), key, strlen(key)); From 1d681a9e4b89a7eb896192423c549305af6e6581 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 24 Apr 2022 20:48:42 +0800 Subject: [PATCH 14/51] enh(query): Add the group id in the serialized block format to support the multi-table(super table) interval query. --- include/common/tdatablock.h | 13 ++- source/client/src/clientImpl.c | 15 ++- source/common/src/tdatablock.c | 5 +- source/dnode/vnode/src/tsdb/tsdbRead.c | 9 +- source/libs/command/src/explain.c | 42 ++++---- source/libs/executor/inc/executil.h | 11 +- source/libs/executor/src/dataDispatcher.c | 8 +- source/libs/executor/src/executil.c | 42 +++++++- source/libs/executor/src/executorimpl.c | 118 ++++++++++++---------- source/libs/executor/src/groupoperator.c | 2 +- source/libs/executor/src/scanoperator.c | 34 ++----- source/libs/parser/src/parTranslater.c | 4 +- source/util/src/tarray.c | 1 + 13 files changed, 186 insertions(+), 118 deletions(-) diff --git a/include/common/tdatablock.h b/include/common/tdatablock.h index fa203e231a..41e91ce6a6 100644 --- a/include/common/tdatablock.h +++ b/include/common/tdatablock.h @@ -238,10 +238,16 @@ static FORCE_INLINE int32_t blockCompressColData(SColumnInfoData* pColRes, int32 static FORCE_INLINE void blockCompressEncode(const SSDataBlock* pBlock, char* data, int32_t* dataLen, int32_t numOfCols, int8_t needCompress) { - int32_t* colSizes = (int32_t*)data; + int32_t* actualLen = (int32_t*) data; + data += sizeof(int32_t); + uint64_t* groupId = (uint64_t*) data; + data += sizeof(uint64_t); + + int32_t* colSizes = (int32_t*)data; data += numOfCols * sizeof(int32_t); - *dataLen = (numOfCols * sizeof(int32_t)); + + *dataLen = (numOfCols * sizeof(int32_t) + sizeof(uint64_t) + sizeof(int32_t)); int32_t numOfRows = pBlock->info.rows; for (int32_t col = 0; col < numOfCols; ++col) { @@ -273,6 +279,9 @@ static FORCE_INLINE void blockCompressEncode(const SSDataBlock* pBlock, char* da colSizes[col] = htonl(colSizes[col]); } + + *actualLen = *dataLen; + *groupId = pBlock->info.groupId; } #ifdef __cplusplus diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 8fcd16a622..73b0a0e4dc 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -835,10 +835,21 @@ int32_t setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32 return code; } - int32_t* colLength = (int32_t*)pResultInfo->pData; - char* pStart = ((char*)pResultInfo->pData) + sizeof(int32_t) * numOfCols; + char* p = (char*) pResultInfo->pData; + + int32_t dataLen = *(int32_t*) p; + p += sizeof(int32_t); + + uint64_t groupId = *(uint64_t*) p; + p += sizeof(uint64_t); + + int32_t* colLength = (int32_t*)p; + p += sizeof(int32_t) * numOfCols; + + char* pStart = p; for (int32_t i = 0; i < numOfCols; ++i) { colLength[i] = htonl(colLength[i]); + ASSERT(colLength[i] < dataLen); if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) { pResultInfo->pCol[i].offset = (int32_t*)pStart; diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 800bcd70ce..09452b584d 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -644,12 +644,12 @@ size_t blockDataGetRowSize(SSDataBlock* pBlock) { /** * @refitem blockDataToBuf for the meta size - * * @param pBlock * @return */ size_t blockDataGetSerialMetaSize(const SSDataBlock* pBlock) { - return sizeof(int32_t) + pBlock->info.numOfCols * sizeof(int32_t); + // | total rows/total length | block group id | each column length | + return sizeof(int32_t) + sizeof(uint64_t) + pBlock->info.numOfCols * sizeof(int32_t); } double blockDataGetSerialRowSize(const SSDataBlock* pBlock) { @@ -1219,7 +1219,6 @@ void colDataDestroy(SColumnInfoData* pColData) { taosMemoryFree(pColData->pData); } - static void doShiftBitmap(char* nullBitmap, size_t n, size_t total) { int32_t len = BitmapLen(total); diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index fd640471bd..c73566355d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -3223,8 +3223,13 @@ void tsdbRetrieveDataBlockInfo(tsdbReaderT* pTsdbReadHandle, SDataBlockInfo* pDa tsdbDebug("data block generated, uid:%" PRIu64 " numOfRows:%d, tsrange:%" PRId64 " - %" PRId64 " %s", uid, cur->rows, cur->win.skey, cur->win.ekey, pHandle->idStr); - // pDataBlockInfo->uid = uid; // block Id may be over write by assigning uid fro this data block. Do NOT assign - // the table uid + pDataBlockInfo->uid = uid; + +#if 0 + // for multi-group data query processing test purpose + pDataBlockInfo->groupId = uid; +#endif + pDataBlockInfo->rows = cur->rows; pDataBlockInfo->window = cur->win; pDataBlockInfo->numOfCols = (int32_t)(QH_GET_NUM_OF_COLS(pHandle)); diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index 4853bb4eb3..7af4546ff3 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -304,8 +304,8 @@ int32_t qExplainResAppendRow(SExplainCtx *ctx, char *tbuf, int32_t len, int32_t memcpy(row.buf, tbuf, len); row.level = level; - row.len = len; - ctx->dataSize += len; + row.len = len; + ctx->dataSize += row.len; if (NULL == taosArrayPush(ctx->rows, &row)) { qError("taosArrayPush row to explain res rows failed"); @@ -783,7 +783,7 @@ int32_t qExplainGetRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { } int32_t colNum = 1; - int32_t rspSize = sizeof(SRetrieveTableRsp) + sizeof(int32_t) * colNum + sizeof(int32_t) * rowNum + pCtx->dataSize; + int32_t rspSize = sizeof(SRetrieveTableRsp) + sizeof(int32_t) + sizeof(uint64_t) + sizeof(int32_t) * colNum + sizeof(int32_t) * rowNum + pCtx->dataSize; SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)taosMemoryCalloc(1, rspSize); if (NULL == rsp) { qError("malloc SRetrieveTableRsp failed, size:%d", rspSize); @@ -793,29 +793,38 @@ int32_t qExplainGetRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { rsp->completed = 1; rsp->numOfRows = htonl(rowNum); - *(int32_t *)rsp->data = htonl(pCtx->dataSize); + // payload length + *(int32_t *)rsp->data = sizeof(int32_t) + sizeof(uint64_t) + sizeof(int32_t) * colNum + sizeof(int32_t) * rowNum + pCtx->dataSize; - int32_t *offset = (int32_t *)((char *)rsp->data + sizeof(int32_t)); + // group id + *(uint64_t*)(rsp->data + sizeof(int32_t)) = 0; + + // column length + int32_t* colLength = (int32_t *)(rsp->data + sizeof(int32_t) + sizeof(uint64_t)); + + // varchar column offset segment + int32_t *offset = (int32_t *)((char *)colLength + sizeof(int32_t)); + + // varchar data real payload char *data = (char *)(offset + rowNum); - int32_t tOffset = 0; - + + char* start = data; for (int32_t i = 0; i < rowNum; ++i) { SQueryExplainRowInfo *row = taosArrayGet(pCtx->rows, i); - *offset = tOffset; - tOffset += row->len; + offset[i] = data - start; - memcpy(data, row->buf, row->len); - - ++offset; + varDataCopy(data, row->buf); + ASSERT(varDataTLen(row->buf) == row->len); data += row->len; } - *pRsp = rsp; + *colLength = htonl(data - start); + rsp->compLen = htonl(rspSize); + *pRsp = rsp; return TSDB_CODE_SUCCESS; } - int32_t qExplainPrepareCtx(SQueryPlan *pDag, SExplainCtx **pCtx) { int32_t code = 0; SNodeListNode *plans = NULL; @@ -922,9 +931,7 @@ int32_t qExplainAppendPlanRows(SExplainCtx *pCtx) { int32_t qExplainGenerateRsp(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp) { QRY_ERR_RET(qExplainAppendGroupResRows(pCtx, pCtx->rootGroupId, 0)); - QRY_ERR_RET(qExplainAppendPlanRows(pCtx)); - QRY_ERR_RET(qExplainGetRspFromCtx(pCtx, pRsp)); return TSDB_CODE_SUCCESS; @@ -994,13 +1001,10 @@ int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp) { SExplainCtx *pCtx = NULL; QRY_ERR_RET(qExplainPrepareCtx(pDag, &pCtx)); - QRY_ERR_JRET(qExplainGenerateRsp(pCtx, pRsp)); _return: - qExplainFreeCtx(pCtx); - QRY_RET(code); } diff --git a/source/libs/executor/inc/executil.h b/source/libs/executor/inc/executil.h index 834d37927a..f0cb1c1107 100644 --- a/source/libs/executor/inc/executil.h +++ b/source/libs/executor/inc/executil.h @@ -40,8 +40,6 @@ #define GET_TASKID(_t) (((SExecTaskInfo*)(_t))->id.str) -#define curTimeWindowIndex(_winres) ((_winres)->curIndex) - typedef struct SGroupResInfo { int32_t totalGroup; int32_t currentGroup; @@ -68,11 +66,16 @@ typedef struct SResultRowPosition { int32_t offset; } SResultRowPosition; +typedef struct SResKeyPos { + SResultRowPosition pos; + uint64_t groupId; + char key[]; +} SResKeyPos; + typedef struct SResultRowInfo { SResultRowPosition *pPosition; int32_t size; // number of result set int32_t capacity; // max capacity -// int32_t curPos; // current active result row index of pResult list SResultRowPosition cur; } SResultRowInfo; @@ -135,7 +138,7 @@ typedef struct { int32_t colId; } SStddevInterResult; -void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo); +void initGroupedResultInfo(SGroupResInfo* pGroupResInfo, SHashObj* pHashmap, bool sortGroupResult); void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList); void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo); diff --git a/source/libs/executor/src/dataDispatcher.c b/source/libs/executor/src/dataDispatcher.c index e897bc8892..a5ce01dba2 100644 --- a/source/libs/executor/src/dataDispatcher.c +++ b/source/libs/executor/src/dataDispatcher.c @@ -64,10 +64,10 @@ static bool needCompress(const SSDataBlock* pData, int32_t numOfCols) { } // data format: -// +----------------+--------------------------------------+-------------+-----------+-------------+-----------+ -// |SDataCacheEntry | column#1 length, column#2 length ... | col1 bitmap | col1 data | col2 bitmap | col2 data | .... -// | | sizeof(int32_t) * numOfCols | actual size | | actual size | | -// +----------------+--------------------------------------+-------------+-----------+-------------+-----------+ +// +----------------+--------------+----------+--------------------------------------+-------------+-----------+-------------+-----------+ +// |SDataCacheEntry | total length | group id | column#1 length, column#2 length ... | col1 bitmap | col1 data | col2 bitmap | col2 data | .... +// | | (4 bytes) |(8 bytes) | sizeof(int32_t) * numOfCols | actual size | | actual size | | +// +----------------+--------------+----------+--------------------------------------+-------------+-----------+-------------+-----------+ // The length of bitmap is decided by number of rows of this data block, and the length of each column data is // recorded in the first segment, next to the struct header static void toDataCacheEntry(const SDataDispatchHandle* pHandle, const SInputData* pInput, SDataDispatchBuf* pBuf) { diff --git a/source/libs/executor/src/executil.c b/source/libs/executor/src/executil.c index 98b72cf4c2..c3fa777779 100644 --- a/source/libs/executor/src/executil.c +++ b/source/libs/executor/src/executil.c @@ -186,12 +186,50 @@ void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo) { pGroupResInfo->index = 0; } -void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo) { +static int32_t resultrowCompar1(const void* p1, const void* p2) { + SResKeyPos* pp1 = *(SResKeyPos**) p1; + SResKeyPos* pp2 = *(SResKeyPos**) p2; + + if (pp1->groupId == pp2->groupId) { + int64_t pts1 = *(int64_t*) pp1->key; + int64_t pts2 = *(int64_t*) pp2->key; + + if (pts1 == pts2) { + return 0; + } else { + return pts1 < pts2? -1:1; + } + } else { + return pp1->groupId < pp2->groupId? -1:1; + } +} + +void initGroupedResultInfo(SGroupResInfo* pGroupResInfo, SHashObj* pHashmap, bool sortGroupResult) { if (pGroupResInfo->pRows != NULL) { taosArrayDestroy(pGroupResInfo->pRows); } - pGroupResInfo->pRows = taosArrayFromList(pResultInfo->pPosition, pResultInfo->size, sizeof(SResultRowPosition)); + // extract the result rows information from the hash map + void* pData = NULL; + pGroupResInfo->pRows = taosArrayInit(10, POINTER_BYTES); + + size_t keyLen = 0; + while((pData = taosHashIterate(pHashmap, pData)) != NULL) { + void* key = taosHashGetKey(pData, &keyLen); + + SResKeyPos* p = taosMemoryMalloc(keyLen + sizeof(SResultRowPosition)); + + p->groupId = *(uint64_t*) key; + p->pos = *(SResultRowPosition*) pData; + memcpy(p->key, key + sizeof(uint64_t), keyLen - sizeof(uint64_t)); + + taosArrayPush(pGroupResInfo->pRows, &p); + } + + if (sortGroupResult) { + qsort(pGroupResInfo->pRows->pData, taosArrayGetSize(pGroupResInfo->pRows), POINTER_BYTES, resultrowCompar1); + } + pGroupResInfo->index = 0; assert(pGroupResInfo->index <= getNumOfTotalRes(pGroupResInfo)); } diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index b28dadb594..be3e95224b 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -431,6 +431,13 @@ SResultRow* getNewResultRow_rv(SDiskbasedBuf* pResultBuf, int64_t tableGroupId, return pResultRow; } +/** + * the struct of key in hash table + * +----------+---------------+ + * | group id | key data | + * | 8 bytes | actual length | + * +----------+---------------+ + */ static SResultRow* doSetResultOutBufByKey_rv(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, int64_t uid, char* pData, int16_t bytes, bool masterscan, uint64_t groupId, SExecTaskInfo* pTaskInfo, bool isIntervalQuery, SAggSupporter* pSup) { @@ -1447,7 +1454,7 @@ static SArray* hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pRe SArray* pUpdated = NULL; if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM) { - pUpdated = taosArrayInit(4, sizeof(SResultRowPosition)); + pUpdated = taosArrayInit(4, POINTER_BYTES); } int32_t step = 1; @@ -1459,8 +1466,6 @@ static SArray* hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pRe if (pSDataBlock->pDataBlock != NULL) { SColumnInfoData* pColDataInfo = taosArrayGet(pSDataBlock->pDataBlock, pInfo->primaryTsIndex); tsCols = (int64_t*)pColDataInfo->pData; -// assert(tsCols[0] == pSDataBlock->info.window.skey && tsCols[pSDataBlock->info.rows - 1] == -// pSDataBlock->info.window.ekey); } int32_t startPos = ascScan? 0 : (pSDataBlock->info.rows - 1); @@ -1478,7 +1483,11 @@ static SArray* hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pRe } if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM) { - SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset}; + SResKeyPos* pos = taosMemoryMalloc(sizeof(SResKeyPos) + sizeof(uint64_t)); + pos->groupId = tableGroupId; + pos->pos = (SResultRowPosition) {.pageId = pResult->pageId, .offset = pResult->offset}; + *(int64_t*) pos->key = pResult->win.skey; + taosArrayPush(pUpdated, &pos); } @@ -1550,7 +1559,11 @@ static SArray* hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pRe } if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM) { - SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset}; + SResKeyPos* pos = taosMemoryMalloc(sizeof(SResKeyPos) + sizeof(uint64_t)); + pos->groupId = tableGroupId; + pos->pos = (SResultRowPosition) {.pageId = pResult->pageId, .offset = pResult->offset}; + *(int64_t*) pos->key = pResult->win.skey; + taosArrayPush(pUpdated, &pos); } @@ -2812,6 +2825,7 @@ void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) { } } +// todo merged with the build group result. void finalizeMultiTupleQueryResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset) { for (int32_t i = 0; i < pResultRowInfo->size; ++i) { @@ -2846,40 +2860,36 @@ void finalizeMultiTupleQueryResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SD } } +// todo merged with the build group result. void finalizeUpdatedResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SDiskbasedBuf* pBuf, SArray* pUpdateList, int32_t* rowCellInfoOffset) { size_t num = taosArrayGetSize(pUpdateList); for (int32_t i = 0; i < num; ++i) { - SResultRowPosition* pPos = taosArrayGet(pUpdateList, i); - - SFilePage* bufPage = getBufPage(pBuf, pPos->pageId); - SResultRow* pRow = (SResultRow*)((char*)bufPage + pPos->offset); + SResKeyPos * pPos = taosArrayGetP(pUpdateList, i); + SFilePage* bufPage = getBufPage(pBuf, pPos->pos.pageId); + SResultRow* pRow = (SResultRow*)((char*)bufPage + pPos->pos.offset); +// for (int32_t j = 0; j < numOfOutput; ++j) { pCtx[j].resultInfo = getResultCell(pRow, j, rowCellInfoOffset); - +// struct SResultRowEntryInfo* pResInfo = pCtx[j].resultInfo; - if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) { - continue; - } - - if (pCtx[j].fpSet.process) { // TODO set the dummy function. -// pCtx[j].fpSet.finalize(&pCtx[j]); - pResInfo->initialized = true; - } - +// if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) { +// continue; +// } +// +// if (pCtx[j].fpSet.process) { // TODO set the dummy function. +//// pCtx[j].fpSet.finalize(&pCtx[j]); +// pResInfo->initialized = true; +// } +// if (pRow->numOfRows < pResInfo->numOfRes) { pRow->numOfRows = pResInfo->numOfRes; } } releaseBufPage(pBuf, bufPage); - /* - * set the number of output results for group by normal columns, the number of output rows usually is 1 except - * the top and bottom query - */ - // buf->numOfRows = (uint16_t)getNumOfResult(pCtx, numOfOutput); } } @@ -3092,7 +3102,6 @@ void setIntervalQueryRange(STableQueryInfo* pTableQueryInfo, TSKEY key, STimeWin } /** - * copyToOutputBuf support copy data in ascending/descending order * For interval query of both super table and table, copy the data in ascending order, since the output results are * ordered in SWindowResutl already. While handling the group by query for both table and super table, * all group result are completed already. @@ -3101,7 +3110,7 @@ void setIntervalQueryRange(STableQueryInfo* pTableQueryInfo, TSKEY key, STimeWin * @param result */ int32_t doCopyToSDataBlock(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 numOfRows = getNumOfTotalRes(pGroupResInfo); int32_t numOfResult = pBlock->info.rows; // there are already exists result rows @@ -3120,10 +3129,10 @@ int32_t doCopyToSDataBlock(SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbased } for (int32_t i = start; (i < numOfRows) && (i >= 0); i += step) { - SResultRowPosition* pPos = taosArrayGet(pGroupResInfo->pRows, i); - SFilePage* page = getBufPage(pBuf, pPos->pageId); + SResKeyPos *pPos = taosArrayGetP(pGroupResInfo->pRows, i); + SFilePage* page = getBufPage(pBuf, pPos->pos.pageId); - SResultRow* pRow = (SResultRow*)((char*)page + pPos->offset); + SResultRow* pRow = (SResultRow*)((char*)page + pPos->pos.offset); if (pRow->numOfRows == 0) { pGroupResInfo->index += 1; continue; @@ -3767,9 +3776,15 @@ int32_t setSDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadI blockDataEnsureCapacity(pRes, numOfRows); if (pColList == NULL) { // data from other sources - int32_t* colLen = (int32_t*)pData; - char* pStart = pData + sizeof(int32_t) * numOfOutput; + int32_t dataLen = *(int32_t*) pData; + pData += sizeof(int32_t); + pRes->info.groupId = *(uint64_t*) pData; + pData += sizeof(uint64_t); + + int32_t* colLen = (int32_t*)pData; + + char* pStart = pData + sizeof(int32_t) * numOfOutput; for (int32_t i = 0; i < numOfOutput; ++i) { colLen[i] = htonl(colLen[i]); ASSERT(colLen[i] > 0); @@ -3822,7 +3837,11 @@ int32_t setSDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadI blockDataEnsureCapacity(&block, numOfRows); - int32_t* colLen = (int32_t*) pStart; + int32_t dataLen = *(int32_t*) pStart; + uint64_t groupId = *(uint64_t*) (pStart + sizeof(int32_t)); + pStart += sizeof(int32_t) + sizeof(uint64_t); + + int32_t* colLen = (int32_t*) (pStart); pStart += sizeof(int32_t) * numOfCols; for (int32_t i = 0; i < numOfCols; ++i) { @@ -4277,18 +4296,6 @@ static void assignExprInfo(SExprInfo* dst, const SExprInfo* src) { // } } -static SExprInfo* exprArrayDup(SArray* pExprList) { - size_t numOfOutput = taosArrayGetSize(pExprList); - - SExprInfo* p = taosMemoryCalloc(numOfOutput, sizeof(SExprInfo)); - for (int32_t i = 0; i < numOfOutput; ++i) { - SExprInfo* pExpr = taosArrayGetP(pExprList, i); - assignExprInfo(&p[i], pExpr); - } - - return p; -} - // TODO merge aggregate super table static void appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHandle) { for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { @@ -4779,7 +4786,7 @@ static int32_t doOpenAggregateOptr(SOperatorInfo* pOperator) { finalizeMultiTupleQueryResult(pAggInfo->binfo.pCtx, pOperator->numOfOutput, pAggInfo->aggSup.pResultBuf, &pAggInfo->binfo.resultRowInfo, pAggInfo->binfo.rowCellInfoOffset); - initGroupResInfo(&pAggInfo->groupResInfo, &pAggInfo->binfo.resultRowInfo); + initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, false); OPTR_SET_OPENED(pOperator); return TSDB_CODE_SUCCESS; } @@ -5077,7 +5084,7 @@ static int32_t doOpenIntervalAgg(SOperatorInfo* pOperator) { STableQueryInfo* pTableQueryInfo = pInfo->pCurrent; setIntervalQueryRange(pTableQueryInfo, pBlock->info.window.skey, &pTaskInfo->window); - hashIntervalAgg(pOperator, &pInfo->binfo.resultRowInfo, pBlock, 0); + hashIntervalAgg(pOperator, &pInfo->binfo.resultRowInfo, pBlock, pBlock->info.groupId); #if 0 // test for encode/decode result info if(pOperator->encodeResultRow){ @@ -5099,7 +5106,7 @@ static int32_t doOpenIntervalAgg(SOperatorInfo* pOperator) { finalizeMultiTupleQueryResult(pInfo->binfo.pCtx, pOperator->numOfOutput, pInfo->aggSup.pResultBuf, &pInfo->binfo.resultRowInfo, pInfo->binfo.rowCellInfoOffset); - initGroupResInfo(&pInfo->groupResInfo, &pInfo->binfo.resultRowInfo); + initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, true); OPTR_SET_OPENED(pOperator); return TSDB_CODE_SUCCESS; } @@ -5224,7 +5231,7 @@ static SSDataBlock* doAllIntervalAgg(SOperatorInfo *pOperator, bool* newgroup) { setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED); // finalizeQueryResult(pSliceInfo->binfo.pCtx, pOperator->numOfOutput); - initGroupResInfo(&pSliceInfo->groupResInfo, &pSliceInfo->binfo.resultRowInfo); +// initGroupedResultInfo(&pSliceInfo->groupResInfo, &pSliceInfo->binfo.resultRowInfo); // doBuildResultDatablock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pSliceInfo->pRes); if (pSliceInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pSliceInfo->groupResInfo)) { @@ -5275,7 +5282,7 @@ static SSDataBlock* doSTableIntervalAgg(SOperatorInfo* pOperator, bool* newgroup finalizeMultiTupleQueryResult(pInfo->binfo.pCtx, pOperator->numOfOutput, pInfo->aggSup.pResultBuf, &pInfo->binfo.resultRowInfo, pInfo->binfo.rowCellInfoOffset); - initGroupResInfo(&pInfo->groupResInfo, &pInfo->binfo.resultRowInfo); +// initGroupedResultInfo(&pInfo->groupResInfo, &pInfo->binfo.resultRowInfo); OPTR_SET_OPENED(pOperator); blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity); @@ -5400,7 +5407,7 @@ static SSDataBlock* doStateWindowAgg(SOperatorInfo* pOperator, bool* newgroup) { closeAllResultRows(&pBInfo->resultRowInfo); finalizeMultiTupleQueryResult(pBInfo->pCtx, pOperator->numOfOutput, pInfo->aggSup.pResultBuf, &pBInfo->resultRowInfo, pBInfo->rowCellInfoOffset); - initGroupResInfo(&pInfo->groupResInfo, &pBInfo->resultRowInfo); + initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, true); blockDataEnsureCapacity(pBInfo->pRes, pOperator->resultInfo.capacity); doBuildResultDatablock(pBInfo->pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset, pInfo->binfo.pCtx); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { @@ -5449,7 +5456,7 @@ static SSDataBlock* doSessionWindowAgg(SOperatorInfo* pOperator, bool* newgroup) closeAllResultRows(&pBInfo->resultRowInfo); finalizeMultiTupleQueryResult(pBInfo->pCtx, pOperator->numOfOutput, pInfo->aggSup.pResultBuf, &pBInfo->resultRowInfo, pBInfo->rowCellInfoOffset); - initGroupResInfo(&pInfo->groupResInfo, &pBInfo->resultRowInfo); + initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, true); blockDataEnsureCapacity(pBInfo->pRes, pOperator->resultInfo.capacity); doBuildResultDatablock(pBInfo->pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset, pInfo->binfo.pCtx); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { @@ -5881,7 +5888,8 @@ SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SExprInfo* pInfo->order = TSDB_ORDER_ASC; pInfo->interval = *pInterval; - pInfo->execModel = pTaskInfo->execModel; + pInfo->execModel = OPTR_EXEC_MODEL_STREAM; +// pInfo->execModel = pTaskInfo->execModel; pInfo->win = pTaskInfo->window; pInfo->twAggSup = *pTwAggSupp; pInfo->primaryTsIndex = primaryTsSlotId; @@ -6492,7 +6500,8 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo }; return createTableScanOperatorInfo(pDataReader, pScanPhyNode->order, numOfCols, pTableScanNode->dataRequired, - pScanPhyNode->count, pScanPhyNode->reverse, pColList, pResBlock, pScanPhyNode->node.pConditions, &interval, pTableScanNode->ratio, pTaskInfo); + pScanPhyNode->count, pScanPhyNode->reverse, pColList, pResBlock, pScanPhyNode->node.pConditions, &interval, pTableScanNode->ratio, + pTaskInfo); } else if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == type) { SExchangePhysiNode* pExchange = (SExchangePhysiNode*)pPhyNode; SSDataBlock* pResBlock = createResDataBlock(pExchange->node.pOutputDataBlockDesc); @@ -6895,8 +6904,7 @@ int32_t createExecTaskInfoImpl(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SRead goto _complete; } - STableGroupInfo group = {0}; - (*pTaskInfo)->pRoot = createOperatorTree(pPlan->pNode, *pTaskInfo, pHandle, queryId, taskId, &group); + (*pTaskInfo)->pRoot = createOperatorTree(pPlan->pNode, *pTaskInfo, pHandle, queryId, taskId, &(*pTaskInfo)->tableqinfoGroupInfo); if (NULL == (*pTaskInfo)->pRoot) { code = terrno; goto _complete; diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index 8a0da52a8e..3ea51b61e8 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -308,7 +308,7 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator, bool* newgrou // } blockDataEnsureCapacity(pRes, pOperator->resultInfo.capacity); - initGroupResInfo(&pInfo->groupResInfo, &pInfo->binfo.resultRowInfo); + initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, false); while(1) { doBuildResultDatablock(pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index ce3de02c9f..4d5ea6cc7a 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -271,9 +271,7 @@ static void setupEnvForReverseScan(STableScanInfo* pTableScanInfo, SqlFunctionCt static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator, bool* newgroup) { STableScanInfo* pTableScanInfo = pOperator->info; - SSDataBlock* pBlock = pTableScanInfo->pResBlock; - STableGroupInfo* pTableGroupInfo = &pOperator->pTaskInfo->tableqinfoGroupInfo; - + SSDataBlock* pBlock = pTableScanInfo->pResBlock; *newgroup = false; while (tsdbNextDataBlock(pTableScanInfo->dataReader)) { @@ -284,18 +282,6 @@ static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator, bool* newgroup) { pTableScanInfo->numOfBlocks += 1; tsdbRetrieveDataBlockInfo(pTableScanInfo->dataReader, &pBlock->info); - // todo opt - // if (pTableGroupInfo->numOfTables > 1 || (pRuntimeEnv->current == NULL && pTableGroupInfo->numOfTables == 1)) { - // STableQueryInfo** pTableQueryInfo = - // (STableQueryInfo**)taosHashGet(pTableGroupInfo->map, &pBlock->info.uid, sizeof(pBlock->info.uid)); - // if (pTableQueryInfo == NULL) { - // break; - // } - // - // doTableQueryInfoTimeWindowCheck(pTaskInfo, *pTableQueryInfo, pTableScanInfo->order); - // } - - // this function never returns error? uint32_t status = 0; int32_t code = loadDataBlock(pOperator, pTableScanInfo, pBlock, &status); // int32_t code = loadDataBlockOnDemand(pOperator->pRuntimeEnv, pTableScanInfo, pBlock, &status); @@ -308,6 +294,8 @@ static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator, bool* newgroup) { continue; } + // reset the block to be 0 by default, this blockId is assigned by physical plan and is used by direct upstream operator. + pBlock->info.blockId = 0; return pBlock; } @@ -405,11 +393,11 @@ SOperatorInfo* createTableScanOperatorInfo(void* pDataReader, int32_t order, int pOperator->name = "TableScanOperator"; pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN; pOperator->blockingOptr = false; - pOperator->status = OP_NOT_OPENED; - pOperator->info = pInfo; - pOperator->numOfOutput = numOfOutput; - pOperator->getNextFn = doTableScan; - pOperator->pTaskInfo = pTaskInfo; + pOperator->status = OP_NOT_OPENED; + pOperator->info = pInfo; + pOperator->numOfOutput = numOfOutput; + pOperator->getNextFn = doTableScan; + pOperator->pTaskInfo = pTaskInfo; static int32_t cost = 0; pOperator->cost.openCost = ++cost; @@ -823,12 +811,12 @@ static SSDataBlock* doSysTableScan(SOperatorInfo* pOperator, bool* newgroup) { int32_t tableNameSlotId = 1; SColumnInfoData* pTableNameCol = taosArrayGet(pInfo->pRes->pDataBlock, tableNameSlotId); - char* name = NULL; + char* tb = NULL; int32_t numOfRows = 0; char n[TSDB_TABLE_NAME_LEN] = {0}; - while ((name = metaTbCursorNext(pInfo->pCur)) != NULL) { - STR_TO_VARSTR(n, name); + while ((tb = metaTbCursorNext(pInfo->pCur)) != NULL) { + STR_TO_VARSTR(n, tb); colDataAppend(pTableNameCol, numOfRows, n, false); numOfRows += 1; if (numOfRows >= pInfo->capacity) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 8506cfc33d..85cba79510 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3600,7 +3600,9 @@ static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) { return TSDB_CODE_OUT_OF_MEMORY; } - pQuery->precision = extractResultTsPrecision((SSelectStmt*)pQuery->pRoot); + if (nodeType(pQuery->pRoot) == QUERY_NODE_SELECT_STMT) { + pQuery->precision = extractResultTsPrecision((SSelectStmt*)pQuery->pRoot); + } } if (NULL != pCxt->pDbs) { diff --git a/source/util/src/tarray.c b/source/util/src/tarray.c index 1c655fc2bf..2d741b18f6 100644 --- a/source/util/src/tarray.c +++ b/source/util/src/tarray.c @@ -476,6 +476,7 @@ void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t return (void*)buf; } +// todo remove it // order array void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) { taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param); From 5abee12eff470339ecc0f4b5807e89d52daf5a40 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 24 Apr 2022 20:49:55 +0800 Subject: [PATCH 15/51] test: update some tsim test cases. --- tests/script/tsim/parser/groupby-basic.sim | 22 +++++++++++----------- tests/script/tsim/stable/disk.sim | 1 + 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/script/tsim/parser/groupby-basic.sim b/tests/script/tsim/parser/groupby-basic.sim index f073200a05..4d6b33612f 100644 --- a/tests/script/tsim/parser/groupby-basic.sim +++ b/tests/script/tsim/parser/groupby-basic.sim @@ -115,19 +115,19 @@ endi if $data00 != 10 then return -1 endi -if $data01 != 0 then - return -1 -endi if $data10 != 10 then return -1 endi -if $data11 != 1 then - return -1 -endi if $data90 != 10 then return -1 endi -if $data91 != 9 then +if $data01 != 7 then + return -1 +endi +if $data11 != 6 then + return -1 +endi +if $data91 != 3 then return -1 endi @@ -143,16 +143,16 @@ if $row != 10 then return -1 endi -if $data00 != @22-01-01 00:00:00.000@ then +if $data00 != @22-01-01 00:00:00.007@ then return -1 endi -if $data01 != 0 then +if $data01 != 7 then return -1 endi -if $data90 != @22-01-01 00:00:00.009@ then +if $data90 != @22-01-01 00:00:00.003@ then return -1 endi -if $data91 != 9 then +if $data91 != 3 then return -1 endi diff --git a/tests/script/tsim/stable/disk.sim b/tests/script/tsim/stable/disk.sim index 97ef779ff2..c1ced6ae10 100644 --- a/tests/script/tsim/stable/disk.sim +++ b/tests/script/tsim/stable/disk.sim @@ -53,6 +53,7 @@ endi sql select count(tbcol) from $mt print select count(tbcol) from $mt ===> $data00 if $data00 != $totalNum then + print expect $totalNum , actual: $data00 return -1 endi From 69575e46b58bc4391c58de60b5ea15dfadbf54c6 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 24 Apr 2022 20:50:32 +0800 Subject: [PATCH 16/51] other: merge 3.0 --- Jenkinsfile2 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Jenkinsfile2 b/Jenkinsfile2 index 441cf89626..a2043797c0 100644 --- a/Jenkinsfile2 +++ b/Jenkinsfile2 @@ -88,6 +88,12 @@ def pre_test(){ cmake .. > /dev/null make -j4> /dev/null ''' + sh''' + cd ${WKPY} + git reset --hard + git pull + pip3 install . + ''' return 1 } @@ -97,6 +103,7 @@ pipeline { environment{ WK = '/var/lib/jenkins/workspace/TDinternal' WKC= '/var/lib/jenkins/workspace/TDengine' + WKPY= '/var/lib/jenkins/workspace/taos-connector-python' } stages { stage('pre_build'){ @@ -117,6 +124,11 @@ pipeline { ./test-all.sh b1fq ''' sh''' + export LD_LIBRARY_PATH=${WKC}/debug/build/lib + cd ${WKC}/tests/system-test + ./fulltest.sh + ''' + sh''' cd ${WKC}/debug ctest ''' From 173abdeedd645b2131a9008556716291f730a83f Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Sun, 24 Apr 2022 21:00:05 +0800 Subject: [PATCH 17/51] refactor --- include/common/tmsg.h | 21 +- include/libs/executor/executor.h | 4 +- source/common/src/tmsg.c | 81 +++- source/dnode/vnode/inc/vnode.h | 1 - source/dnode/vnode/src/inc/tsdb.h | 1 - source/dnode/vnode/src/inc/tsdbSma.h | 3 +- source/dnode/vnode/src/tq/tq.c | 6 +- source/dnode/vnode/src/tq/tqRead.c | 48 +-- source/dnode/vnode/src/tsdb/tsdbMemTable.c | 28 -- source/dnode/vnode/src/tsdb/tsdbSma.c | 434 ++++++++++++++------- source/dnode/vnode/src/vnd/vnodeSvr.c | 6 +- source/dnode/vnode/test/tsdbSmaTest.cpp | 2 +- source/libs/executor/src/executor.c | 21 +- source/libs/stream/src/tstream.c | 4 +- 14 files changed, 424 insertions(+), 236 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index fbfdfc1e39..e0c99e6054 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -227,8 +227,16 @@ typedef struct { } SSubmitBlkIter; typedef struct { - int32_t totalLen; - int32_t len; + int32_t totalLen; + int32_t len; + // head of SSubmitBlk + int64_t uid; // table unique id + int64_t suid; // stable id + int32_t sversion; // data schema version + int32_t dataLen; // data part length, not including the SSubmitBlk head + int32_t schemaLen; // schema length, if length is 0, no schema exists + int16_t numOfRows; // total number of rows in current submit block + // head of SSubmitBlk const void* pMsg; } SSubmitMsgIter; @@ -237,6 +245,15 @@ int32_t tGetSubmitMsgNext(SSubmitMsgIter* pIter, SSubmitBlk** pPBlock); int32_t tInitSubmitBlkIter(SSubmitBlk* pBlock, SSubmitBlkIter* pIter); STSRow* tGetSubmitBlkNext(SSubmitBlkIter* pIter); +// TODO: KEEP one suite of iterator API finally. +// 1) use tInitSubmitMsgIterEx firstly as not decrease the merge conflicts +// 2) replace tInitSubmitMsgIterEx with tInitSubmitMsgIter later +// 3) finally, rename tInitSubmitMsgIterEx to tInitSubmitMsgIter +int32_t tInitSubmitMsgIterEx(const SSubmitReq* pMsg, SSubmitMsgIter* pIter); +int32_t tGetSubmitMsgNextEx(SSubmitMsgIter* pIter, SSubmitBlk** pPBlock); +int32_t tInitSubmitBlkIterEx(SSubmitMsgIter* pMsgIter, SSubmitBlk* pBlock, SSubmitBlkIter* pIter); +STSRow* tGetSubmitBlkNextEx(SSubmitBlkIter* pIter); + typedef struct { int32_t index; // index of failed block in submit blocks int32_t vnode; // vnode index of failed block diff --git a/include/libs/executor/executor.h b/include/libs/executor/executor.h index 04f67b584f..4d289147d0 100644 --- a/include/libs/executor/executor.h +++ b/include/libs/executor/executor.h @@ -57,7 +57,7 @@ qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, void* streamReadHandle); * @param type * @return */ -int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool converted); +int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type); /** * Set multiple input data blocks for the stream scan. @@ -67,7 +67,7 @@ int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool * @param type * @return */ -int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type, bool converted); +int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type); /** * Update the table id list, add or remove. diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index eb7c9a763f..14195471b6 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -94,6 +94,86 @@ STSRow *tGetSubmitBlkNext(SSubmitBlkIter *pIter) { } } +// TODO: KEEP one suite of iterator API finally. +// 1) use tInitSubmitMsgIterEx firstly as not decrease the merge conflicts +// 2) replace tInitSubmitMsgIterEx with tInitSubmitMsgIter later +// 3) finally, rename tInitSubmitMsgIterEx to tInitSubmitMsgIter + +int32_t tInitSubmitMsgIterEx(const SSubmitReq *pMsg, SSubmitMsgIter *pIter) { + if (pMsg == NULL) { + terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP; + return -1; + } + + pIter->totalLen = htonl(pMsg->length); + ASSERT(pIter->totalLen > 0); + pIter->len = 0; + pIter->pMsg = pMsg; + if (pIter->totalLen <= sizeof(SSubmitReq)) { + terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP; + return -1; + } + + return 0; +} + +int32_t tGetSubmitMsgNextEx(SSubmitMsgIter *pIter, SSubmitBlk **pPBlock) { + ASSERT(pIter->len >= 0); + + if (pIter->len == 0) { + pIter->len += sizeof(SSubmitReq); + } else { + if (pIter->len >= pIter->totalLen) { + ASSERT(0); + } + + SSubmitBlk *pSubmitBlk = (SSubmitBlk *)POINTER_SHIFT(pIter->pMsg, pIter->len); + pIter->len += (sizeof(SSubmitBlk) + pIter->dataLen + pIter->schemaLen); + ASSERT(pIter->len > 0); + } + + if (pIter->len > pIter->totalLen) { + terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP; + *pPBlock = NULL; + return -1; + } + + if (pIter->len == pIter->totalLen) { + *pPBlock = NULL; + } else { + *pPBlock = (SSubmitBlk *)POINTER_SHIFT(pIter->pMsg, pIter->len); + pIter->uid = htobe64((*pPBlock)->uid); + pIter->suid = htobe64((*pPBlock)->suid); + pIter->sversion = htonl((*pPBlock)->sversion); + pIter->dataLen = htonl((*pPBlock)->dataLen); + pIter->schemaLen = htonl((*pPBlock)->schemaLen); + pIter->numOfRows = htons((*pPBlock)->numOfRows); + } + return 0; +} + +int32_t tInitSubmitBlkIterEx(SSubmitMsgIter *pMsgIter, SSubmitBlk *pBlock, SSubmitBlkIter *pIter) { + if (pMsgIter->dataLen <= 0) return -1; + pIter->totalLen = pMsgIter->dataLen; + pIter->len = 0; + pIter->row = (STSRow *)(pBlock->data + pMsgIter->schemaLen); + return 0; +} + +STSRow *tGetSubmitBlkNextEx(SSubmitBlkIter *pIter) { + STSRow *row = pIter->row; + + if (pIter->len >= pIter->totalLen) { + return NULL; + } else { + pIter->len += TD_ROW_LEN(row); + if (pIter->len < pIter->totalLen) { + pIter->row = POINTER_SHIFT(row, TD_ROW_LEN(row)); + } + return row; + } +} + int32_t tEncodeSEpSet(SCoder *pEncoder, const SEpSet *pEp) { if (tEncodeI8(pEncoder, pEp->inUse) < 0) return -1; if (tEncodeI8(pEncoder, pEp->numOfEps) < 0) return -1; @@ -126,7 +206,6 @@ int32_t tDecodeSQueryNodeAddr(SCoder *pDecoder, SQueryNodeAddr *pAddr) { return 0; } - int32_t taosEncodeSEpSet(void **buf, const SEpSet *pEp) { int32_t tlen = 0; tlen += taosEncodeFixedI8(buf, pEp->inUse); diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 76e56bcfa2..f4975c183e 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -112,7 +112,6 @@ void tqReadHandleSetColIdList(STqReadHandle *pReadHandle, SArray *pColIdList) int tqReadHandleSetTbUidList(STqReadHandle *pHandle, const SArray *tbUidList); int tqReadHandleAddTbUidList(STqReadHandle *pHandle, const SArray *tbUidList); int32_t tqReadHandleSetMsg(STqReadHandle *pHandle, SSubmitReq *pMsg, int64_t ver); -int32_t tqReadHandleSetMsgEx(STqReadHandle *pHandle, SSubmitReq *pMsg, int64_t ver); bool tqNextDataBlock(STqReadHandle *pHandle); int32_t tqRetrieveDataBlock(SArray **ppCols, STqReadHandle *pHandle, uint64_t *pGroupId, int32_t *pNumOfRows, int16_t *pNumOfCols); diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index a84776abfd..8cfbffc962 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -56,7 +56,6 @@ int32_t tsdbInsertTSmaData(STsdb *pTsdb, int64_t indexUid, const char *msg); int32_t tsdbDropTSmaData(STsdb *pTsdb, int64_t indexUid); int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg); void tsdbCleanupReadHandle(tsdbReaderT queryHandle); -int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg); typedef enum { TSDB_FILE_HEAD = 0, // .head diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index 28d1a6e9a0..3a865ddc51 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -48,7 +48,8 @@ static FORCE_INLINE int32_t tsdbUidStoreInit(STbUidStore **pStore) { return TSDB_CODE_SUCCESS; } -int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t uid); +int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t *uid); +void tsdbUidStoreDestory(STbUidStore *pStore); void *tsdbUidStoreFree(STbUidStore *pStore); int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq); diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index f933c6e8de..0b3feb5010 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -83,7 +83,7 @@ int32_t tqPushMsgNew(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_ if (pExec->subType == TOPIC_SUB_TYPE__TABLE) { qTaskInfo_t task = pExec->task[workerId]; ASSERT(task); - qSetStreamInput(task, pReq, STREAM_DATA_TYPE_SUBMIT_BLOCK, false); + qSetStreamInput(task, pReq, STREAM_DATA_TYPE_SUBMIT_BLOCK); while (1) { SSDataBlock* pDataBlock = NULL; uint64_t ts = 0; @@ -454,7 +454,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { if (pExec->subType == TOPIC_SUB_TYPE__TABLE) { qTaskInfo_t task = pExec->task[workerId]; ASSERT(task); - qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK, false); + qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK); while (1) { SSDataBlock* pDataBlock = NULL; uint64_t ts = 0; @@ -648,7 +648,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { SSubmitReq* pCont = (SSubmitReq*)&pHead->body; qTaskInfo_t task = pTopic->buffer.output[workerId].task; ASSERT(task); - qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK, false); + qSetStreamInput(task, pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK); SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock)); while (1) { SSDataBlock* pDataBlock = NULL; diff --git a/source/dnode/vnode/src/tq/tqRead.c b/source/dnode/vnode/src/tq/tqRead.c index dfc3fba1b4..ada2460d97 100644 --- a/source/dnode/vnode/src/tq/tqRead.c +++ b/source/dnode/vnode/src/tq/tqRead.c @@ -33,40 +33,15 @@ STqReadHandle* tqInitSubmitMsgScanner(SMeta* pMeta) { int32_t tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t ver) { pReadHandle->pMsg = pMsg; - pMsg->length = htonl(pMsg->length); - pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); - - // iterate and convert - if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; - while (true) { - if (tGetSubmitMsgNext(&pReadHandle->msgIter, &pReadHandle->pBlock) < 0) return -1; - if (pReadHandle->pBlock == NULL) break; - - pReadHandle->pBlock->uid = htobe64(pReadHandle->pBlock->uid); - pReadHandle->pBlock->suid = htobe64(pReadHandle->pBlock->suid); - pReadHandle->pBlock->sversion = htonl(pReadHandle->pBlock->sversion); - pReadHandle->pBlock->dataLen = htonl(pReadHandle->pBlock->dataLen); - pReadHandle->pBlock->schemaLen = htonl(pReadHandle->pBlock->schemaLen); - pReadHandle->pBlock->numOfRows = htons(pReadHandle->pBlock->numOfRows); - } - - if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; - pReadHandle->ver = ver; - memset(&pReadHandle->blkIter, 0, sizeof(SSubmitBlkIter)); - return 0; -} - -int32_t tqReadHandleSetMsgEx(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t ver) { - pReadHandle->pMsg = pMsg; // iterate - if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; + if (tInitSubmitMsgIterEx(pMsg, &pReadHandle->msgIter) < 0) return -1; while (true) { - if (tGetSubmitMsgNext(&pReadHandle->msgIter, &pReadHandle->pBlock) < 0) return -1; + if (tGetSubmitMsgNextEx(&pReadHandle->msgIter, &pReadHandle->pBlock) < 0) return -1; if (pReadHandle->pBlock == NULL) break; } - if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; + if (tInitSubmitMsgIterEx(pMsg, &pReadHandle->msgIter) < 0) return -1; pReadHandle->ver = ver; memset(&pReadHandle->blkIter, 0, sizeof(SSubmitBlkIter)); return 0; @@ -74,7 +49,7 @@ int32_t tqReadHandleSetMsgEx(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64 bool tqNextDataBlock(STqReadHandle* pHandle) { while (1) { - if (tGetSubmitMsgNext(&pHandle->msgIter, &pHandle->pBlock) < 0) { + if (tGetSubmitMsgNextEx(&pHandle->msgIter, &pHandle->pBlock) < 0) { return false; } if (pHandle->pBlock == NULL) return false; @@ -82,7 +57,8 @@ bool tqNextDataBlock(STqReadHandle* pHandle) { /*pHandle->pBlock->uid = htobe64(pHandle->pBlock->uid);*/ /*if (pHandle->tbUid == pHandle->pBlock->uid) {*/ ASSERT(pHandle->tbIdHash); - void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->pBlock->uid, sizeof(int64_t)); + // void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->pBlock->uid, sizeof(int64_t)); + void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->msgIter.uid, sizeof(int64_t)); if (ret != NULL) { /*printf("retrieve one tb %ld\n", pHandle->pBlock->uid);*/ /*pHandle->pBlock->tid = htonl(pHandle->pBlock->tid);*/ @@ -104,14 +80,14 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p // TODO set to real sversion int32_t sversion = 0; if (pHandle->sver != sversion) { - pHandle->pSchema = metaGetTbTSchema(pHandle->pVnodeMeta, pHandle->pBlock->uid, sversion); + pHandle->pSchema = metaGetTbTSchema(pHandle->pVnodeMeta, pHandle->msgIter.uid, sversion); tb_uid_t quid; - STbCfg* pTbCfg = metaGetTbInfoByUid(pHandle->pVnodeMeta, pHandle->pBlock->uid); + STbCfg* pTbCfg = metaGetTbInfoByUid(pHandle->pVnodeMeta, pHandle->msgIter.uid); if (pTbCfg->type == META_CHILD_TABLE) { quid = pTbCfg->ctbCfg.suid; } else { - quid = pHandle->pBlock->uid; + quid = pHandle->msgIter.uid; } pHandle->pSchemaWrapper = metaGetTableSchema(pHandle->pVnodeMeta, quid, sversion, true); pHandle->sver = sversion; @@ -120,7 +96,7 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p STSchema* pTschema = pHandle->pSchema; SSchemaWrapper* pSchemaWrapper = pHandle->pSchemaWrapper; - *pNumOfRows = pHandle->pBlock->numOfRows; + *pNumOfRows = pHandle->msgIter.numOfRows; int32_t colNumNeed = taosArrayGetSize(pHandle->pColIdList); if (colNumNeed > pSchemaWrapper->nCols) { @@ -167,8 +143,8 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p tdSTSRowIterInit(&iter, pTschema); STSRow* row; int32_t curRow = 0; - tInitSubmitBlkIter(pHandle->pBlock, &pHandle->blkIter); - while ((row = tGetSubmitBlkNext(&pHandle->blkIter)) != NULL) { + tInitSubmitBlkIterEx(&pHandle->msgIter, pHandle->pBlock, &pHandle->blkIter); + while ((row = tGetSubmitBlkNextEx(&pHandle->blkIter)) != NULL) { tdSTSRowIterReset(&iter, row); // get all wanted col of that block for (int32_t i = 0; i < colActual; i++) { diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable.c b/source/dnode/vnode/src/tsdb/tsdbMemTable.c index bea2b66af8..3082ee6f2b 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable.c @@ -227,34 +227,6 @@ int tsdbLoadDataFromCache(STable *pTable, SSkipListIterator *pIter, TSKEY maxKey return 0; } -int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg) { - ASSERT(pMsg != NULL); - SSubmitMsgIter msgIter = {0}; - SSubmitBlk *pBlock = NULL; - SSubmitBlkIter blkIter = {0}; - STSRow *row = NULL; - - terrno = TSDB_CODE_SUCCESS; - pMsg->length = htonl(pMsg->length); - pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); - - if (tInitSubmitMsgIter(pMsg, &msgIter) < 0) return -1; - while (true) { - if (tGetSubmitMsgNext(&msgIter, &pBlock) < 0) return -1; - if (pBlock == NULL) break; - - pBlock->uid = htobe64(pBlock->uid); - pBlock->suid = htobe64(pBlock->suid); - pBlock->sversion = htonl(pBlock->sversion); - pBlock->dataLen = htonl(pBlock->dataLen); - pBlock->schemaLen = htonl(pBlock->schemaLen); - pBlock->numOfRows = htons(pBlock->numOfRows); - } - - if (terrno != TSDB_CODE_SUCCESS) return -1; - return 0; -} - static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg) { ASSERT(pMsg != NULL); // STsdbMeta * pMeta = pTsdb->tsdbMeta; diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index ddef1a649d..9c2daaa6d7 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -95,6 +95,7 @@ typedef struct { * - TSDB_SMA_STAT_EXPIRED: 1) If sma calculation of history TS data is not finished; 2) Or if the TSDB is open, * without information about its previous state. * - TSDB_SMA_STAT_DROPPED: 1)sma dropped + * N.B. only applicable to tsma */ int8_t state; // ETsdbSmaStat SHashObj *expiredWindows; // key: skey of time window, value: N/A @@ -112,8 +113,6 @@ struct SSmaStat { SHashObj *smaStatItems; // key: indexUid, value: SSmaStatItem for tsma SHashObj *rsmaInfoHash; // key: stbUid, value: SRSmaInfo; }; - SRSmaInfo rsmaInfo; - T_REF_DECLARE() }; #define SMA_STAT_ITEMS(s) ((s)->smaStatItems) @@ -173,6 +172,7 @@ static void tsdbGetSmaDir(int32_t vgId, ETsdbSmaType smaType, char dirName[]) static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, int64_t indexUid, const char *msg); static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, const char *msg); +static FORCE_INLINE int32_t tsdbUpdateTbUidListImpl(STsdb *pTsdb, tb_uid_t *suid, SArray *tbUids); // mgmt interface static int32_t tsdbDropTSmaDataImpl(STsdb *pTsdb, int64_t indexUid); @@ -263,7 +263,7 @@ static void *poolMalloc(void *arg, size_t size) { SPoolMem *pMem; pMem = (SPoolMem *)tdbOsMalloc(sizeof(*pMem) + size); - if (pMem == NULL) { + if (!pMem) { assert(0); } @@ -355,7 +355,7 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *pa SSmaEnv *pEnv = NULL; pEnv = (SSmaEnv *)taosMemoryCalloc(1, sizeof(SSmaEnv)); - if (pEnv == NULL) { + if (!pEnv) { terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -371,7 +371,7 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *pa ASSERT(path && (strlen(path) > 0)); SMA_ENV_PATH(pEnv) = strdup(path); - if (SMA_ENV_PATH(pEnv) == NULL) { + if (!SMA_ENV_PATH(pEnv)) { tsdbFreeSmaEnv(pEnv); return NULL; } @@ -390,7 +390,7 @@ static SSmaEnv *tsdbNewSmaEnv(const STsdb *pTsdb, int8_t smaType, const char *pa return NULL; } - if ((pEnv->pPool = openPool()) == NULL) { + if (!(pEnv->pPool = openPool())) { tsdbFreeSmaEnv(pEnv); return NULL; } @@ -404,8 +404,8 @@ static int32_t tsdbInitSmaEnv(STsdb *pTsdb, int8_t smaType, const char *path, SD return TSDB_CODE_FAILED; } - if (*pEnv == NULL) { - if ((*pEnv = tsdbNewSmaEnv(pTsdb, smaType, path, did)) == NULL) { + if (!(*pEnv)) { + if (!(*pEnv = tsdbNewSmaEnv(pTsdb, smaType, path, did))) { return TSDB_CODE_FAILED; } } @@ -437,7 +437,7 @@ void *tsdbFreeSmaEnv(SSmaEnv *pSmaEnv) { } static int32_t tsdbRefSmaStat(STsdb *pTsdb, SSmaStat *pStat) { - if (pStat == NULL) return 0; + if (!pStat) return 0; int ref = T_REF_INC(pStat); tsdbDebug("vgId:%d ref sma stat:%p, val:%d", REPO_ID(pTsdb), pStat, ref); @@ -445,7 +445,7 @@ static int32_t tsdbRefSmaStat(STsdb *pTsdb, SSmaStat *pStat) { } static int32_t tsdbUnRefSmaStat(STsdb *pTsdb, SSmaStat *pStat) { - if (pStat == NULL) return 0; + if (!pStat) return 0; int ref = T_REF_DEC(pStat); tsdbDebug("vgId:%d unref sma stat:%p, val:%d", REPO_ID(pTsdb), pStat, ref); @@ -455,7 +455,7 @@ static int32_t tsdbUnRefSmaStat(STsdb *pTsdb, SSmaStat *pStat) { static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType) { ASSERT(pSmaStat != NULL); - if (*pSmaStat != NULL) { // no lock + if (*pSmaStat) { // no lock return TSDB_CODE_SUCCESS; } @@ -464,9 +464,9 @@ static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType) { * 2. Currently, there is mutex lock when init SSmaEnv, thus no need add lock on SSmaStat, and please add lock if * tsdbInitSmaStat invoked in other multithread environment later. */ - if (*pSmaStat == NULL) { + if (!(*pSmaStat)) { *pSmaStat = (SSmaStat *)taosMemoryCalloc(1, sizeof(SSmaStat)); - if (*pSmaStat == NULL) { + if (!(*pSmaStat)) { terrno = TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_FAILED; } @@ -475,7 +475,7 @@ static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType) { SMA_STAT_INFO_HASH(*pSmaStat) = taosHashInit( RSMA_TASK_INFO_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK); - if (SMA_STAT_INFO_HASH(*pSmaStat) == NULL) { + if (!SMA_STAT_INFO_HASH(*pSmaStat)) { taosMemoryFreeClear(*pSmaStat); return TSDB_CODE_FAILED; } @@ -483,7 +483,7 @@ static int32_t tsdbInitSmaStat(SSmaStat **pSmaStat, int8_t smaType) { SMA_STAT_ITEMS(*pSmaStat) = taosHashInit(SMA_STATE_HASH_SLOT, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); - if (SMA_STAT_ITEMS(*pSmaStat) == NULL) { + if (!SMA_STAT_ITEMS(*pSmaStat)) { taosMemoryFreeClear(*pSmaStat); return TSDB_CODE_FAILED; } @@ -510,7 +510,7 @@ static SSmaStatItem *tsdbNewSmaStatItem(int8_t state) { } static void *tsdbFreeSmaStatItem(SSmaStatItem *pSmaStatItem) { - if (pSmaStatItem != NULL) { + if (pSmaStatItem) { tdDestroyTSma(pSmaStatItem->pSma); taosMemoryFreeClear(pSmaStatItem->pSma); taosHashCleanup(pSmaStatItem->expiredWindows); @@ -530,7 +530,7 @@ int32_t tsdbDestroySmaState(SSmaStat *pSmaStat, int8_t smaType) { // TODO: use taosHashSetFreeFp when taosHashSetFreeFp is ready. if (smaType == TSDB_SMA_TYPE_TIME_RANGE) { void *item = taosHashIterate(SMA_STAT_ITEMS(pSmaStat), NULL); - while (item != NULL) { + while (item) { SSmaStatItem *pItem = *(SSmaStatItem **)item; tsdbFreeSmaStatItem(pItem); item = taosHashIterate(SMA_STAT_ITEMS(pSmaStat), item); @@ -538,7 +538,7 @@ int32_t tsdbDestroySmaState(SSmaStat *pSmaStat, int8_t smaType) { taosHashCleanup(SMA_STAT_ITEMS(pSmaStat)); } else if (smaType == TSDB_SMA_TYPE_ROLLUP) { void *infoHash = taosHashIterate(SMA_STAT_INFO_HASH(pSmaStat), NULL); - while (infoHash != NULL) { + while (infoHash) { SRSmaInfo *pInfoHash = *(SRSmaInfo **)infoHash; tsdbFreeRSmaInfo(pInfoHash); infoHash = taosHashIterate(SMA_STAT_INFO_HASH(pSmaStat), infoHash); @@ -557,12 +557,12 @@ static int32_t tsdbCheckAndInitSmaEnv(STsdb *pTsdb, int8_t smaType) { // return if already init switch (smaType) { case TSDB_SMA_TYPE_TIME_RANGE: - if ((pEnv = (SSmaEnv *)atomic_load_ptr(&REPO_TSMA_ENV(pTsdb))) != NULL) { + if ((pEnv = (SSmaEnv *)atomic_load_ptr(&REPO_TSMA_ENV(pTsdb)))) { return TSDB_CODE_SUCCESS; } break; case TSDB_SMA_TYPE_ROLLUP: - if ((pEnv = (SSmaEnv *)atomic_load_ptr(&REPO_RSMA_ENV(pTsdb))) != NULL) { + if ((pEnv = (SSmaEnv *)atomic_load_ptr(&REPO_RSMA_ENV(pTsdb)))) { return TSDB_CODE_SUCCESS; } break; @@ -575,7 +575,7 @@ static int32_t tsdbCheckAndInitSmaEnv(STsdb *pTsdb, int8_t smaType) { tsdbLockRepo(pTsdb); pEnv = (smaType == TSDB_SMA_TYPE_TIME_RANGE) ? atomic_load_ptr(&REPO_TSMA_ENV(pTsdb)) : atomic_load_ptr(&REPO_RSMA_ENV(pTsdb)); - if (pEnv == NULL) { + if (!pEnv) { char rname[TSDB_FILENAME_LEN] = {0}; SDiskID did = {0}; @@ -607,10 +607,10 @@ static int32_t tsdbCheckAndInitSmaEnv(STsdb *pTsdb, int8_t smaType) { static int32_t tsdbSetExpiredWindow(STsdb *pTsdb, SHashObj *pItemsHash, int64_t indexUid, int64_t winSKey, int64_t version) { SSmaStatItem *pItem = taosHashGet(pItemsHash, &indexUid, sizeof(indexUid)); - if (pItem == NULL) { + if (!pItem) { // TODO: use TSDB_SMA_STAT_EXPIRED and update by stream computing later pItem = tsdbNewSmaStatItem(TSDB_SMA_STAT_OK); // TODO use the real state - if (pItem == NULL) { + if (!pItem) { // Response to stream computing: OOM // For query, if the indexUid not found, the TSDB should tell query module to query raw TS data. return TSDB_CODE_FAILED; @@ -618,7 +618,7 @@ static int32_t tsdbSetExpiredWindow(STsdb *pTsdb, SHashObj *pItemsHash, int64_t // cache smaMeta STSma *pSma = metaGetSmaInfoByIndex(REPO_META(pTsdb), indexUid, true); - if (pSma == NULL) { + if (!pSma) { terrno = TSDB_CODE_TDB_NO_SMA_INDEX_IN_META; taosHashCleanup(pItem->expiredWindows); taosMemoryFree(pItem); @@ -634,7 +634,7 @@ static int32_t tsdbSetExpiredWindow(STsdb *pTsdb, SHashObj *pItemsHash, int64_t taosMemoryFree(pItem); return TSDB_CODE_FAILED; } - } else if ((pItem = *(SSmaStatItem **)pItem) == NULL) { + } else if (!(pItem = *(SSmaStatItem **)pItem)) { terrno = TSDB_CODE_INVALID_PTR; return TSDB_CODE_FAILED; } @@ -678,9 +678,9 @@ int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t vers return TSDB_CODE_FAILED; } - if (tdScanAndConvertSubmitMsg(pMsg) != TSDB_CODE_SUCCESS) { - return TSDB_CODE_FAILED; - } + // if (tdScanAndConvertSubmitMsg(pMsg) != TSDB_CODE_SUCCESS) { + // return TSDB_CODE_FAILED; + // } if (tsdbCheckAndInitSmaEnv(pTsdb, TSDB_SMA_TYPE_TIME_RANGE) != TSDB_CODE_SUCCESS) { terrno = TSDB_CODE_TDB_INIT_FAILED; @@ -694,7 +694,7 @@ int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t vers SSmaStat *pStat = SMA_ENV_STAT(pEnv); SHashObj *pItemsHash = SMA_ENV_STAT_ITEMS(pEnv); - TASSERT(pEnv != NULL && pStat != NULL && pItemsHash != NULL); + TASSERT(pEnv && pStat && pItemsHash); // basic procedure // TODO: optimization @@ -705,26 +705,26 @@ int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t vers SInterval interval = {0}; TSKEY lastWinSKey = INT64_MIN; - if (tInitSubmitMsgIter(pMsg, &msgIter) != TSDB_CODE_SUCCESS) { + if (tInitSubmitMsgIterEx(pMsg, &msgIter) != TSDB_CODE_SUCCESS) { return TSDB_CODE_FAILED; } while (true) { - tGetSubmitMsgNext(&msgIter, &pBlock); - if (pBlock == NULL) break; + tGetSubmitMsgNextEx(&msgIter, &pBlock); + if (!pBlock) break; STSmaWrapper *pSW = NULL; STSma *pTSma = NULL; SSubmitBlkIter blkIter = {0}; - if (tInitSubmitBlkIter(pBlock, &blkIter) != TSDB_CODE_SUCCESS) { + if (tInitSubmitBlkIterEx(&msgIter, pBlock, &blkIter) != TSDB_CODE_SUCCESS) { pSW = tdFreeTSmaWrapper(pSW); break; } while (true) { - STSRow *row = tGetSubmitBlkNext(&blkIter); - if (row == NULL) { + STSRow *row = tGetSubmitBlkNextEx(&blkIter); + if (!row) { tdFreeTSmaWrapper(pSW); break; } @@ -732,10 +732,10 @@ int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t vers if (pSW) { pSW = tdFreeTSmaWrapper(pSW); } - if ((pSW = metaGetSmaInfoByTable(REPO_META(pTsdb), pBlock->suid)) == NULL) { + if (!(pSW = metaGetSmaInfoByTable(REPO_META(pTsdb), pBlock->suid))) { break; } - if ((pSW->number) <= 0 || (pSW->tSma == NULL)) { + if ((pSW->number) <= 0 || !pSW->tSma) { pSW = tdFreeTSmaWrapper(pSW); break; } @@ -781,10 +781,10 @@ static int32_t tsdbResetExpiredWindow(STsdb *pTsdb, SSmaStat *pStat, int64_t ind tsdbRefSmaStat(pTsdb, pStat); - if (pStat && pStat->smaStatItems) { - pItem = taosHashGet(pStat->smaStatItems, &indexUid, sizeof(indexUid)); + if (pStat && SMA_STAT_ITEMS(pStat)) { + pItem = taosHashGet(SMA_STAT_ITEMS(pStat), &indexUid, sizeof(indexUid)); } - if ((pItem != NULL) && ((pItem = *(SSmaStatItem **)pItem) != NULL)) { + if ((pItem) && ((pItem = *(SSmaStatItem **)pItem))) { // pItem resides in hash buffer all the time unless drop sma index // TODO: multithread protect if (taosHashRemove(pItem->expiredWindows, &skey, sizeof(TSKEY)) != 0) { @@ -994,7 +994,7 @@ static void tsdbDestroyTSmaWriteH(STSmaWriteH *pSmaH) { static int32_t tsdbSetTSmaDataFile(STSmaWriteH *pSmaH, int64_t indexUid, int32_t fid) { STsdb *pTsdb = pSmaH->pTsdb; - ASSERT(pSmaH->dFile.path == NULL && pSmaH->dFile.pDB == NULL); + ASSERT(!pSmaH->dFile.path && !pSmaH->dFile.pDB); pSmaH->dFile.fid = fid; char tSmaFile[TSDB_FILENAME_LEN] = {0}; @@ -1064,6 +1064,8 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, int64_t indexUid, const char STsdbCfg *pCfg = REPO_CFG(pTsdb); const SArray *pDataBlocks = (const SArray *)msg; + // TODO: destroy SSDataBlocks(msg) + // For super table aggregation, the sma data is stored in vgroup calculated from the hash value of stable name. Thus // the sma data would arrive ahead of the update-expired-window msg. if (tsdbCheckAndInitSmaEnv(pTsdb, TSDB_SMA_TYPE_TIME_RANGE) != TSDB_CODE_SUCCESS) { @@ -1071,7 +1073,7 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, int64_t indexUid, const char return TSDB_CODE_FAILED; } - if (pDataBlocks == NULL) { + if (!pDataBlocks) { terrno = TSDB_CODE_INVALID_PTR; tsdbWarn("vgId:%d insert tSma data failed since pDataBlocks is NULL", REPO_ID(pTsdb)); return terrno; @@ -1089,11 +1091,11 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, int64_t indexUid, const char tsdbRefSmaStat(pTsdb, pStat); - if (pStat && pStat->smaStatItems) { - pItem = taosHashGet(pStat->smaStatItems, &indexUid, sizeof(indexUid)); + if (pStat && SMA_STAT_ITEMS(pStat)) { + pItem = taosHashGet(SMA_STAT_ITEMS(pStat), &indexUid, sizeof(indexUid)); } - if ((pItem == NULL) || ((pItem = *(SSmaStatItem **)pItem) == NULL) || tsdbSmaStatIsDropped(pItem)) { + if (!pItem || !(pItem = *(SSmaStatItem **)pItem) || tsdbSmaStatIsDropped(pItem)) { terrno = TSDB_CODE_TDB_INVALID_SMA_STAT; tsdbUnRefSmaStat(pTsdb, pStat); return TSDB_CODE_FAILED; @@ -1287,7 +1289,7 @@ static int32_t tsdbDropTSmaDataImpl(STsdb *pTsdb, int64_t indexUid) { tsdbDebug("vgId:%d drop tSma local cache for %" PRIi64, REPO_ID(pTsdb), indexUid); SSmaStatItem *pItem = taosHashGet(SMA_ENV_STAT_ITEMS(pEnv), &indexUid, sizeof(indexUid)); - if ((pItem != NULL) || ((pItem = *(SSmaStatItem **)pItem) != NULL)) { + if ((pItem) || ((pItem = *(SSmaStatItem **)pItem))) { if (tsdbSmaStatIsDropped(pItem)) { tsdbDebug("vgId:%d tSma stat is already dropped for %" PRIi64, REPO_ID(pTsdb), indexUid); return TSDB_CODE_TDB_INVALID_ACTION; // TODO: duplicate drop msg would be intercepted by mnode @@ -1343,13 +1345,13 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, const char *msg) { SSmaEnv *pEnv = atomic_load_ptr(&REPO_RSMA_ENV(pTsdb)); int64_t indexUid = SMA_TEST_INDEX_UID; - if (pEnv == NULL) { + if (!pEnv) { terrno = TSDB_CODE_INVALID_PTR; tsdbWarn("vgId:%d insert rSma data failed since pTSmaEnv is NULL", REPO_ID(pTsdb)); return terrno; } - if (pDataBlocks == NULL) { + if (!pDataBlocks) { terrno = TSDB_CODE_INVALID_PTR; tsdbWarn("vgId:%d insert rSma data failed since pDataBlocks is NULL", REPO_ID(pTsdb)); return terrno; @@ -1366,11 +1368,11 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, const char *msg) { tsdbRefSmaStat(pTsdb, pStat); - if (pStat && pStat->smaStatItems) { - pItem = taosHashGet(pStat->smaStatItems, &indexUid, sizeof(indexUid)); + if (pStat && SMA_STAT_ITEMS(pStat)) { + pItem = taosHashGet(SMA_STAT_ITEMS(pStat), &indexUid, sizeof(indexUid)); } - if ((pItem == NULL) || ((pItem = *(SSmaStatItem **)pItem) == NULL) || tsdbSmaStatIsDropped(pItem)) { + if (!pItem || !(pItem = *(SSmaStatItem **)pItem) || tsdbSmaStatIsDropped(pItem)) { terrno = TSDB_CODE_TDB_INVALID_SMA_STAT; tsdbUnRefSmaStat(pTsdb, pStat); return TSDB_CODE_FAILED; @@ -1491,7 +1493,7 @@ static bool tsdbSetAndOpenTSmaFile(STSmaReadH *pReadH, TSKEY *queryKey) { ++pReadH->smaFsIter.iter; } - if (pReadH->pDFile != NULL) { + if (pReadH->pDFile) { tsdbDebug("vg%d: smaFile %s matched", REPO_ID(pReadH->pTsdb), "[pSmaFile dir]"); return true; } @@ -1524,7 +1526,7 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, char *pData, int64_t indexUid, tsdbRefSmaStat(pTsdb, pStat); SSmaStatItem *pItem = taosHashGet(SMA_ENV_STAT_ITEMS(pEnv), &indexUid, sizeof(indexUid)); - if ((pItem == NULL) || ((pItem = *(SSmaStatItem **)pItem) == NULL)) { + if (!pItem || !(pItem = *(SSmaStatItem **)pItem)) { // Normally pItem should not be NULL, mark all windows as expired and notify query module to fetch raw TS data if // it's NULL. tsdbUnRefSmaStat(pTsdb, pStat); @@ -1537,7 +1539,7 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, char *pData, int64_t indexUid, int32_t nQueryWin = taosArrayGetSize(pQuerySKey); for (int32_t n = 0; n < nQueryWin; ++n) { TSKEY skey = taosArrayGet(pQuerySKey, n); - if (taosHashGet(pItem->expiredWindows, &skey, sizeof(TSKEY)) != NULL) { + if (taosHashGet(pItem->expiredWindows, &skey, sizeof(TSKEY))) { // TODO: mark this window as expired. } } @@ -1553,7 +1555,7 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, char *pData, int64_t indexUid, return TSDB_CODE_FAILED; } - if (taosHashGet(pItem->expiredWindows, &querySKey, sizeof(TSKEY)) != NULL) { + if (taosHashGet(pItem->expiredWindows, &querySKey, sizeof(TSKEY))) { // TODO: mark this window as expired. tsdbDebug("vgId:%d skey %" PRIi64 " of window exists in expired window for index %" PRIi64, REPO_ID(pTsdb), querySKey, indexUid); @@ -1588,7 +1590,7 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, char *pData, int64_t indexUid, void *result = NULL; int32_t valueSize = 0; - if ((result = tsdbGetSmaDataByKey(&tReadH.dFile, smaKey, SMA_KEY_LEN, &valueSize)) == NULL) { + if (!(result = tsdbGetSmaDataByKey(&tReadH.dFile, smaKey, SMA_KEY_LEN, &valueSize))) { tsdbWarn("vgId:%d get sma data failed from smaIndex %" PRIi64 ", smaKey %" PRIx64 "-%" PRIx64 " since %s", REPO_ID(pTsdb), indexUid, *(int64_t *)smaKey, *(int64_t *)POINTER_SHIFT(smaKey, 8), tstrerror(terrno)); tsdbCloseDBF(&tReadH.dFile); @@ -1632,7 +1634,7 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, char *pData, int64_t indexUid, int32_t tsdbCreateTSma(STsdb *pTsdb, char *pMsg) { SSmaCfg vCreateSmaReq = {0}; - if (tDeserializeSVCreateTSmaReq(pMsg, &vCreateSmaReq) == NULL) { + if (!tDeserializeSVCreateTSmaReq(pMsg, &vCreateSmaReq)) { terrno = TSDB_CODE_OUT_OF_MEMORY; tsdbWarn("vgId:%d TDMT_VND_CREATE_SMA received but deserialize failed since %s", REPO_ID(pTsdb), terrstr(terrno)); return -1; @@ -1658,7 +1660,7 @@ int32_t tsdbCreateTSma(STsdb *pTsdb, char *pMsg) { int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg) { SVDropTSmaReq vDropSmaReq = {0}; - if (tDeserializeSVDropTSmaReq(pMsg, &vDropSmaReq) == NULL) { + if (!tDeserializeSVDropTSmaReq(pMsg, &vDropSmaReq)) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -1697,7 +1699,7 @@ int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg) { int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { SRSmaParam *param = pReq->stbCfg.pRSmaParam; - if (param == NULL) { + if (!param) { tsdbDebug("vgId:%d return directly since no rollup for stable %s %" PRIi64, REPO_ID(pTsdb), pReq->name, pReq->stbCfg.suid); return TSDB_CODE_SUCCESS; @@ -1716,23 +1718,21 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); SSmaStat *pStat = SMA_ENV_STAT(pEnv); SRSmaInfo *pRSmaInfo = NULL; - SRSmaInfo *tRSmaInfo = &pStat->rsmaInfo; - TASSERT(pEnv != NULL && pStat != NULL); - - pRSmaInfo = taosHashGet(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t)); - if (pRSmaInfo != NULL) { - pRSmaInfo = tsdbFreeRSmaInfo(pRSmaInfo); + pRSmaInfo = taosHashGet(SMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t)); + if (pRSmaInfo) { + tsdbWarn("vgId:%d rsma info already exists for stb: %s, %" PRIi64, REPO_ID(pTsdb), pReq->name, pReq->stbCfg.suid); + return TSDB_CODE_SUCCESS; } pRSmaInfo = (SRSmaInfo *)taosMemoryCalloc(1, sizeof(SRSmaInfo)); - if (pRSmaInfo == NULL) { + if (!pRSmaInfo) { terrno = TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_FAILED; } STqReadHandle *pReadHandle = tqInitSubmitMsgScanner(pMeta); - if (pReadHandle == NULL) { + if (!pReadHandle) { taosMemoryFree(pRSmaInfo); terrno = TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_FAILED; @@ -1745,65 +1745,83 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { if (param->qmsg1) { pRSmaInfo->taskInfo[0] = qCreateStreamExecTaskInfo(param->qmsg1, &handle); - if (pRSmaInfo->taskInfo[0] == NULL) { + if (!pRSmaInfo->taskInfo[0]) { taosMemoryFree(pRSmaInfo); taosMemoryFree(pReadHandle); return TSDB_CODE_FAILED; } - tRSmaInfo->taskInfo[0] = pRSmaInfo->taskInfo[0]; } if (param->qmsg2) { pRSmaInfo->taskInfo[1] = qCreateStreamExecTaskInfo(param->qmsg2, &handle); - if (pRSmaInfo->taskInfo[1] == NULL) { + if (!pRSmaInfo->taskInfo[1]) { taosMemoryFree(pRSmaInfo); taosMemoryFree(pReadHandle); return TSDB_CODE_FAILED; } - tRSmaInfo->taskInfo[1] = pRSmaInfo->taskInfo[1]; } if (taosHashPut(SMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != - 0) { + TSDB_CODE_SUCCESS) { return TSDB_CODE_FAILED; } return TSDB_CODE_SUCCESS; } -int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t uid) { +/** + * @brief store suid/[uids], prefer to use array and then hash + * + * @param pStore + * @param suid + * @param uid + * @return int32_t + */ +int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t *uid) { + // prefer to store suid/uids in array if ((suid == pStore->suid) || (pStore->suid == 0)) { - if (pStore->tbUids == NULL) { - if ((pStore->tbUids = taosArrayInit(1, sizeof(tb_uid_t))) == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return TSDB_CODE_FAILED; - } + if (pStore->suid == 0) { pStore->suid = suid; } - if (taosArrayPush(pStore->tbUids, &uid) == NULL) { - return TSDB_CODE_FAILED; + if (uid) { + if (!pStore->tbUids) { + if (!(pStore->tbUids = taosArrayInit(1, sizeof(tb_uid_t)))) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + } + if (!taosArrayPush(pStore->tbUids, &uid)) { + return TSDB_CODE_FAILED; + } } } else { - if (pStore->uidHash == NULL) { + // store other suid/uids in hash when multiple stable/table included in 1 batch of request + if (!pStore->uidHash) { pStore->uidHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK); - if (pStore->uidHash == NULL) { + if (!pStore->uidHash) { return TSDB_CODE_FAILED; } } - SArray *hashVal = taosHashGet(pStore->uidHash, &suid, sizeof(tb_uid_t)); - if (hashVal && ((hashVal = *(SArray **)hashVal) != NULL)) { - taosArrayPush(hashVal, &uid); + if (uid) { + SArray *uidArray = taosHashGet(pStore->uidHash, &suid, sizeof(tb_uid_t)); + if (uidArray && ((uidArray = *(SArray **)uidArray))) { + taosArrayPush(uidArray, &uid); + } else { + SArray *pUidArray = taosArrayInit(1, sizeof(tb_uid_t)); + if (!pUidArray) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + if (!taosArrayPush(pUidArray, &uid)) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_FAILED; + } + if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), &pUidArray, sizeof(pUidArray)) != 0) { + return TSDB_CODE_FAILED; + } + } } else { - SArray *pItem = taosArrayInit(1, sizeof(tb_uid_t)); - if (pItem == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return TSDB_CODE_FAILED; - } - if (taosArrayPush(pItem, &uid) == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return TSDB_CODE_FAILED; - } - if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), &pItem, sizeof(pItem)) != 0) { + if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), NULL, 0) != 0) { return TSDB_CODE_FAILED; } } @@ -1811,8 +1829,42 @@ int32_t tsdbUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t uid) { return TSDB_CODE_SUCCESS; } +void tsdbUidStoreDestory(STbUidStore *pStore) { + if (pStore) { + if (pStore->uidHash) { + if (pStore->tbUids) { + void *pIter = taosHashIterate(pStore->uidHash, NULL); + while (pIter) { + SArray *arr = *(SArray **)pIter; + taosArrayDestroy(arr); + pIter = taosHashIterate(pStore->uidHash, pIter); + } + } + taosHashCleanup(pStore->uidHash); + } + taosArrayDestroy(pStore->tbUids); + } +} + +void *tsdbUidStoreFree(STbUidStore *pStore) { + tsdbUidStoreDestory(pStore); + taosMemoryFree(pStore); + return NULL; +} + +/** + * @brief fetch suid/uids when create child tables of rollup SMA + * + * @param pTsdb + * @param ppStore + * @param suid + * @param uid + * @return int32_t + */ int32_t tsdbFetchTbUidList(void *pTsdb, void **ppStore, void *suid, void *uid) { SSmaEnv *pEnv = REPO_RSMA_ENV((STsdb *)pTsdb); + + // only applicable to rollup SMA ctables if (!pEnv) { return TSDB_CODE_SUCCESS; } @@ -1824,97 +1876,194 @@ int32_t tsdbFetchTbUidList(void *pTsdb, void **ppStore, void *suid, void *uid) { return TSDB_CODE_FAILED; } - // info cached when create rsma stable - if (!taosHashGet(infoHash, suid, sizeof(suid))) { + // info cached when create rsma stable and return directly for non-rsma ctables + if (!taosHashGet(infoHash, suid, sizeof(tb_uid_t))) { return TSDB_CODE_SUCCESS; } - if (*ppStore == NULL) { + if (!(*ppStore)) { if (tsdbUidStoreInit((STbUidStore **)ppStore) != 0) { return TSDB_CODE_FAILED; } } - if (tsdbUidStorePut(*ppStore, *(tb_uid_t *)suid, *(tb_uid_t *)uid) != 0) { + + if (tsdbUidStorePut(*ppStore, *(tb_uid_t *)suid, (tb_uid_t *)uid) != 0) { *ppStore = tsdbUidStoreFree(*ppStore); return TSDB_CODE_FAILED; } + return TSDB_CODE_SUCCESS; } -void *tsdbUidStoreFree(STbUidStore *pStore) { - if (pStore) { - taosArrayDestroy(pStore->tbUids); - if (pStore->uidHash) { - void *pIter = taosHashIterate(pStore->uidHash, NULL); - while (pIter != NULL) { - SArray *arr = *(SArray **)pIter; - taosArrayDestroy(arr); - pIter = taosHashIterate(pStore->uidHash, pIter); - } - } - taosMemoryFree(pStore); - } - return NULL; -} - -int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pUidStore) { - if (!pUidStore || (taosArrayGetSize(pUidStore->tbUids) == 0)) { - tsdbDebug("vgId:%d empty uidStore and no need to update", REPO_ID(pTsdb)); - return TSDB_CODE_SUCCESS; - } +static FORCE_INLINE int32_t tsdbUpdateTbUidListImpl(STsdb *pTsdb, tb_uid_t *suid, SArray *tbUids) { SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); SSmaStat *pStat = SMA_ENV_STAT(pEnv); - SRSmaInfo *pRSmaInfo = &pStat->rsmaInfo; + SRSmaInfo *pRSmaInfo = NULL; - if (qUpdateQualifiedTableId(pRSmaInfo->taskInfo[0], pUidStore->tbUids, true) != 0) { - tsdbUidStoreFree(pUidStore); - tsdbError("vgId:%d update tbUidList failed since %s", REPO_ID(pTsdb), terrstr(terrno)); + if (!suid || !tbUids) { + terrno = TSDB_CODE_INVALID_PTR; + tsdbError("vgId:%d failed to get rsma info for uid:%" PRIi64 " since %s", REPO_ID(pTsdb), *suid, terrstr(terrno)); + return TSDB_CODE_FAILED; + } + + pRSmaInfo = taosHashGet(SMA_STAT_INFO_HASH(pStat), suid, sizeof(tb_uid_t)); + if (!pRSmaInfo || !(pRSmaInfo = *(SRSmaInfo **)pRSmaInfo)) { + tsdbError("vgId:%d failed to get rsma info for uid:%" PRIi64, REPO_ID(pTsdb), *suid); + terrno = TSDB_CODE_TDB_INVALID_SMA_STAT; + return TSDB_CODE_FAILED; + } + + if (pRSmaInfo->taskInfo[0] && (qUpdateQualifiedTableId(pRSmaInfo->taskInfo[0], tbUids, true) != 0)) { + tsdbError("vgId:%d update tbUidList failed for uid:%" PRIi64 " since %s", REPO_ID(pTsdb), *suid, terrstr(terrno)); + return TSDB_CODE_FAILED; + } + if (pRSmaInfo->taskInfo[1] && (qUpdateQualifiedTableId(pRSmaInfo->taskInfo[1], tbUids, true) != 0)) { + tsdbError("vgId:%d update tbUidList failed for uid:%" PRIi64 " since %s", REPO_ID(pTsdb), *suid, terrstr(terrno)); return TSDB_CODE_FAILED; } - tsdbUidStoreFree(pUidStore); return TSDB_CODE_SUCCESS; } -int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType) { +int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pStore) { + if (!pStore || (taosArrayGetSize(pStore->tbUids) == 0)) { + tsdbDebug("vgId:%d no need to update tbUids since empty uidStore", REPO_ID(pTsdb)); + tsdbUidStoreFree(pStore); + return TSDB_CODE_SUCCESS; + } + + if (tsdbUpdateTbUidListImpl(pTsdb, &pStore->suid, pStore->tbUids) != TSDB_CODE_SUCCESS) { + tsdbUidStoreFree(pStore); + return TSDB_CODE_FAILED; + } + + void *pIter = taosHashIterate(pStore->uidHash, NULL); + while (pIter) { + tb_uid_t *pTbSuid = (tb_uid_t *)taosHashGetKey(pIter, NULL); + SArray *pTbUids = *(SArray **)pIter; + + if (tsdbUpdateTbUidListImpl(pTsdb, pTbSuid, pTbUids) != TSDB_CODE_SUCCESS) { + taosHashCancelIterate(pStore->uidHash, pIter); + tsdbUidStoreFree(pStore); + return TSDB_CODE_FAILED; + } + + pIter = taosHashIterate(pStore->uidHash, pIter); + } + + tsdbUidStoreFree(pStore); + + return TSDB_CODE_SUCCESS; +} + +static int32_t tsdbFetchSubmitReqSuids(const SSubmitReq *pMsg, STbUidStore *pStore) { + ASSERT(pMsg != NULL); + SSubmitMsgIter msgIter = {0}; + SSubmitBlk *pBlock = NULL; + SSubmitBlkIter blkIter = {0}; + STSRow *row = NULL; + + terrno = TSDB_CODE_SUCCESS; + // pMsg->length = htonl(pMsg->length); + // pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); + + if (tInitSubmitMsgIterEx(pMsg, &msgIter) < 0) return -1; + while (true) { + if (tGetSubmitMsgNextEx(&msgIter, &pBlock) < 0) return -1; + if (!pBlock) break; + tsdbUidStorePut(pStore, msgIter.suid, NULL); + } + + if (terrno != TSDB_CODE_SUCCESS) return -1; + return 0; +} + +int32_t tsdbExecuteRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType, tb_uid_t *suid) { SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); if (!pEnv) { + // only applicable when rsma env exists return TSDB_CODE_SUCCESS; } SSmaStat *pStat = SMA_ENV_STAT(pEnv); SRSmaInfo *pRSmaInfo = NULL; + pRSmaInfo = taosHashGet(SMA_STAT_INFO_HASH(pStat), suid, sizeof(tb_uid_t)); + + if (!pRSmaInfo || !(pRSmaInfo = *(SRSmaInfo **)pRSmaInfo)) { + tsdbDebug("vgId:%d no rsma info for suid:%" PRIu64, REPO_ID(pTsdb), *suid); + return TSDB_CODE_SUCCESS; + } + SArray *pResult = NULL; pResult = taosArrayInit(0, sizeof(SSDataBlock)); - if (pResult == NULL) { + if (!pResult) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) { - // pRSmaInfo = taosHashGet(pStat->rsmaInfoHash, &pReq->stbCfg.suid, sizeof(tb_uid_t)); - // if (pRSmaInfo != NULL) { - // pRSmaInfo = tsdbFreeRSmaInfo(pRSmaInfo); + if (pRSmaInfo->taskInfo[0]) { + qSetStreamInput(pRSmaInfo->taskInfo[0], pMsg, inputType); + while (1) { + SSDataBlock *output; + uint64_t ts; + if (qExecTask(pRSmaInfo->taskInfo[0], &output, &ts) < 0) { + ASSERT(false); + } + if (!output) { + break; + } + taosArrayPush(pResult, output); + } + blockDebugShowData(pResult); + } + + // if (pRSmaInfo->taskInfo[1]) { + // qSetStreamInput(pRSmaInfo->taskInfo[1], pMsg, inputType); + // while (1) { + // SSDataBlock *output; + // uint64_t ts; + // if (qExecTask(pRSmaInfo->taskInfo[1], &output, &ts) < 0) { + // ASSERT(false); + // } + // if (!output) { + // break; + // } + // taosArrayPush(pResult, output); + // } + // blockDebugShowData(pResult); // } + } - SRSmaInfo *tRSmaInfo = &pStat->rsmaInfo; + return TSDB_CODE_SUCCESS; +} - qSetStreamInput(tRSmaInfo->taskInfo[0], pMsg, inputType, true); - while (1) { - SSDataBlock *output; - uint64_t ts; - if (qExecTask(tRSmaInfo->taskInfo[0], &output, &ts) < 0) { - ASSERT(false); +int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType) { + SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); + if (!pEnv) { + // only applicable when rsma env exists + return TSDB_CODE_SUCCESS; + } + + if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) { + STbUidStore uidStore = {0}; + tsdbFetchSubmitReqSuids(pMsg, &uidStore); + + if (uidStore.suid != 0) { + tsdbExecuteRSma(pTsdb, pMeta, pMsg, inputType, &uidStore.suid); + + void *pIter = taosHashIterate(uidStore.uidHash, NULL); + while (pIter) { + tb_uid_t *pTbSuid = (tb_uid_t *)taosHashGetKey(pIter, NULL); + tsdbExecuteRSma(pTsdb, pMeta, pMsg, inputType, pTbSuid); + pIter = taosHashIterate(uidStore.uidHash, pIter); } - if (output == NULL) { - break; - } - taosArrayPush(pResult, output); + + tsdbUidStoreDestory(&uidStore); } } - blockDebugShowData(pResult); return TSDB_CODE_SUCCESS; } @@ -1960,6 +2109,7 @@ int32_t tsdbInsertTSmaData(STsdb *pTsdb, int64_t indexUid, const char *msg) { if ((code = tsdbInsertTSmaDataImpl(pTsdb, indexUid, msg)) < 0) { tsdbWarn("vgId:%d insert tSma data failed since %s", REPO_ID(pTsdb), tstrerror(terrno)); } + // TODO: destroy SSDataBlocks(msg) return code; } diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 0417acf290..6c82edbadc 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -83,7 +83,7 @@ int vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg case TDMT_VND_SUBMIT: pRsp->msgType = TDMT_VND_SUBMIT_RSP; vnodeProcessSubmitReq(pVnode, ptr, pRsp); - tsdbTriggerRSma(pVnode->pTsdb, pVnode->pMeta, ptr, STREAM_DATA_TYPE_SUBMIT_BLOCK); + tsdbTriggerRSma(pVnode->pTsdb, pVnode->pMeta, pMsg->pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK); break; case TDMT_VND_MQ_VG_CHANGE: if (tqProcessVgChangeReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), @@ -348,6 +348,8 @@ static int vnodeProcessCreateTbReq(SVnode *pVnode, SRpcMsg *pMsg, void *pReq, SR } } + tsdbUpdateTbUidList(pVnode->pTsdb, ddlHandle.result); + vTrace("vgId:%d process create %" PRIzu " tables", TD_VID(pVnode), taosArrayGetSize(vCreateTbBatchReq.pArray)); taosArrayDestroy(vCreateTbBatchReq.pArray); if (vCreateTbBatchRsp.rspList) { @@ -360,8 +362,6 @@ static int vnodeProcessCreateTbReq(SVnode *pVnode, SRpcMsg *pMsg, void *pReq, SR pRsp->contLen = contLen; } - tsdbUpdateTbUidList(pVnode->pTsdb, ddlHandle.result); - return 0; } diff --git a/source/dnode/vnode/test/tsdbSmaTest.cpp b/source/dnode/vnode/test/tsdbSmaTest.cpp index b0217a0462..ab617cb186 100644 --- a/source/dnode/vnode/test/tsdbSmaTest.cpp +++ b/source/dnode/vnode/test/tsdbSmaTest.cpp @@ -407,7 +407,7 @@ TEST(testCase, tSma_Data_Insert_Query_Test) { } } - EXPECT_EQ(tdScanAndConvertSubmitMsg(pMsg), TSDB_CODE_SUCCESS); + // EXPECT_EQ(tdScanAndConvertSubmitMsg(pMsg), TSDB_CODE_SUCCESS); EXPECT_EQ(tsdbUpdateSmaWindow(pTsdb, pMsg, 0), 0); diff --git a/source/libs/executor/src/executor.c b/source/libs/executor/src/executor.c index 8f7f55eb33..4863b03fb9 100644 --- a/source/libs/executor/src/executor.c +++ b/source/libs/executor/src/executor.c @@ -19,7 +19,7 @@ #include "tdatablock.h" #include "vnode.h" -static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id, bool converted) { +static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) { ASSERT(pOperator != NULL); if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) { if (pOperator->numOfDownstream == 0) { @@ -32,7 +32,7 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t nu return TSDB_CODE_QRY_APP_ERROR; } pOperator->status = OP_NOT_OPENED; - return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id, converted); + return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id); } else { pOperator->status = OP_NOT_OPENED; @@ -46,13 +46,8 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t nu } if (type == STREAM_DATA_TYPE_SUBMIT_BLOCK) { - if (converted) { - if (tqReadHandleSetMsgEx(pInfo->readerHandle, input, 0) < 0) { - qError("converted: submit msg messed up when initing stream block, %s" PRIx64, id); - return TSDB_CODE_QRY_APP_ERROR; - } - } else if (tqReadHandleSetMsg(pInfo->readerHandle, input, 0) < 0) { - qError("raw msg: submit msg messed up when initing stream block, %s" PRIx64, id); + if (tqReadHandleSetMsg(pInfo->readerHandle, input, 0) < 0) { + qError("submit msg messed up when initing stream block, %s" PRIx64, id); return TSDB_CODE_QRY_APP_ERROR; } } else { @@ -72,11 +67,11 @@ static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t nu } } -int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool converted) { - return qSetMultiStreamInput(tinfo, input, 1, type, converted); +int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type) { + return qSetMultiStreamInput(tinfo, input, 1, type); } -int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type, bool converted) { +int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) { if (tinfo == NULL) { return TSDB_CODE_QRY_APP_ERROR; } @@ -87,7 +82,7 @@ int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numO SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo; - int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo), converted); + int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo)); if (code != TSDB_CODE_SUCCESS) { qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo)); } else { diff --git a/source/libs/stream/src/tstream.c b/source/libs/stream/src/tstream.c index 1f0c269ca1..8aaaa414ca 100644 --- a/source/libs/stream/src/tstream.c +++ b/source/libs/stream/src/tstream.c @@ -110,7 +110,7 @@ int32_t streamExecTask(SStreamTask* pTask, SMsgCb* pMsgCb, const void* input, in return -1; } if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) { - qSetStreamInput(exec, input, inputType, true); + qSetStreamInput(exec, input, inputType); while (1) { SSDataBlock* output; uint64_t ts; @@ -128,7 +128,7 @@ int32_t streamExecTask(SStreamTask* pTask, SMsgCb* pMsgCb, const void* input, in /*for (int32_t i = 0; i < sz; i++) {*/ /*SSDataBlock* pBlock = taosArrayGet(blocks, i);*/ /*qSetStreamInput(exec, pBlock, inputType);*/ - qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK, true); + qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK); while (1) { SSDataBlock* output; uint64_t ts; From df1ebbc25ba497fe2500b81d9c58c9ef71850668 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Sun, 24 Apr 2022 21:51:27 +0800 Subject: [PATCH 18/51] refactor --- source/libs/qworker/src/qworkerMsg.c | 288 ++++++++++++++------------- 1 file changed, 150 insertions(+), 138 deletions(-) diff --git a/source/libs/qworker/src/qworkerMsg.c b/source/libs/qworker/src/qworkerMsg.c index e0bcfabdd3..64df4f02bf 100644 --- a/source/libs/qworker/src/qworkerMsg.c +++ b/source/libs/qworker/src/qworkerMsg.c @@ -1,18 +1,17 @@ -#include "qworker.h" -#include "tcommon.h" +#include "qworkerMsg.h" +#include "dataSinkMgt.h" #include "executor.h" #include "planner.h" #include "query.h" +#include "qworker.h" #include "qworkerInt.h" -#include "qworkerMsg.h" +#include "tcommon.h" #include "tmsg.h" #include "tname.h" -#include "dataSinkMgt.h" - int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) { int32_t msgSize = sizeof(SRetrieveTableRsp) + length; - + SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(msgSize); if (NULL == pRsp) { qError("rpcMallocCont %d failed", msgSize); @@ -26,11 +25,9 @@ int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) { return TSDB_CODE_SUCCESS; } - - void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) { SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg; - + rsp->useconds = htobe64(input->useconds); rsp->completed = qComplete; rsp->precision = input->precision; @@ -39,7 +36,6 @@ void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) rsp->numOfRows = htonl(input->numOfRows); } - void qwFreeFetchRsp(void *msg) { if (msg) { rpcFreeCont(msg); @@ -48,18 +44,19 @@ void qwFreeFetchRsp(void *msg) { int32_t qwBuildAndSendQueryRsp(SQWConnInfo *pConn, int32_t code) { SQueryTableRsp rsp = {.code = code}; - + int32_t contLen = tSerializeSQueryTableRsp(NULL, 0, &rsp); - void *msg = rpcMallocCont(contLen); + void *msg = rpcMallocCont(contLen); tSerializeSQueryTableRsp(msg, contLen, &rsp); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_QUERY_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = msg, - .contLen = contLen, - .code = code, + .msgType = TDMT_VND_QUERY_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .pCont = msg, + .contLen = contLen, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -72,12 +69,13 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_RES_READY_RSP, - .handle = pConn->handle, - .ahandle = NULL, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_RES_READY_RSP, + .handle = pConn->handle, + .refId = pConn->refId, + .ahandle = NULL, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -85,20 +83,21 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { return TSDB_CODE_SUCCESS; } -int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { +int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { SExplainRsp rsp = {.numOfPlans = num, .subplanInfo = execInfo}; int32_t contLen = tSerializeSExplainRsp(NULL, 0, &rsp); - void *pRsp = rpcMallocCont(contLen); + void *pRsp = rpcMallocCont(contLen); tSerializeSExplainRsp(pRsp, contLen, &rsp); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_EXPLAIN_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = contLen, - .code = 0, + .msgType = TDMT_VND_EXPLAIN_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .pCont = pRsp, + .contLen = contLen, + .code = 0, }; tmsgSendRsp(&rpcRsp); @@ -108,16 +107,17 @@ int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t qwBuildAndSendHbRsp(SQWConnInfo *pConn, SSchedulerHbRsp *pStatus, int32_t code) { int32_t contLen = tSerializeSSchedulerHbRsp(NULL, 0, pStatus); - void *pRsp = rpcMallocCont(contLen); + void *pRsp = rpcMallocCont(contLen); tSerializeSSchedulerHbRsp(pRsp, contLen, pStatus); SRpcMsg rpcRsp = { - .msgType = TDMT_VND_QUERY_HEARTBEAT_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = contLen, - .code = code, + .msgType = TDMT_VND_QUERY_HEARTBEAT_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .pCont = pRsp, + .contLen = contLen, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -133,12 +133,13 @@ int32_t qwBuildAndSendFetchRsp(SQWConnInfo *pConn, SRetrieveTableRsp *pRsp, int3 } SRpcMsg rpcRsp = { - .msgType = TDMT_VND_FETCH_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp) + dataLength, - .code = code, + .msgType = TDMT_VND_FETCH_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .pCont = pRsp, + .contLen = sizeof(*pRsp) + dataLength, + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -151,12 +152,13 @@ int32_t qwBuildAndSendCancelRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_CANCEL_TASK_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_CANCEL_TASK_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -168,12 +170,13 @@ int32_t qwBuildAndSendDropRsp(SQWConnInfo *pConn, int32_t code) { pRsp->code = code; SRpcMsg rpcRsp = { - .msgType = TDMT_VND_DROP_TASK_RSP, - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .pCont = pRsp, - .contLen = sizeof(*pRsp), - .code = code, + .msgType = TDMT_VND_DROP_TASK_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .pCont = pRsp, + .contLen = sizeof(*pRsp), + .code = code, }; tmsgSendRsp(&rpcRsp); @@ -191,7 +194,7 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { return -1; } - col_id_t cols = 0; + col_id_t cols = 0; SSchema *pSchema = showRsp.tableMeta.pSchemas; const SSchema *s = tGetTbnameColumnSchema(); @@ -226,6 +229,7 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { SRpcMsg rpcMsg = { .handle = pMsg->handle, .ahandle = pMsg->ahandle, + .refId = pMsg->refId, .pCont = pBuf, .contLen = bufLen, .code = code, @@ -235,17 +239,18 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) { return TSDB_CODE_SUCCESS; } -int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq* pFetchReq) { +int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq *pFetchReq) { SVShowTablesFetchRsp *pRsp = (SVShowTablesFetchRsp *)rpcMallocCont(sizeof(SVShowTablesFetchRsp)); - int32_t handle = htonl(pFetchReq->id); + int32_t handle = htonl(pFetchReq->id); pRsp->numOfRows = 0; SRpcMsg rpcMsg = { - .handle = pMsg->handle, + .handle = pMsg->handle, .ahandle = pMsg->ahandle, - .pCont = pRsp, + .refId = pMsg->refId, + .pCont = pRsp, .contLen = sizeof(*pRsp), - .code = 0, + .code = 0, }; tmsgSendRsp(&rpcMsg); @@ -253,7 +258,7 @@ int32_t qwBuildAndSendShowFetchRsp(SRpcMsg *pMsg, SVShowTablesFetchReq* pFetchRe } int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { - SQueryContinueReq * req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq)); + SQueryContinueReq *req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq)); if (NULL == req) { QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(SQueryContinueReq)); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); @@ -265,12 +270,13 @@ int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { req->taskId = tId; SRpcMsg pNewMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_QUERY_CONTINUE, - .pCont = req, - .contLen = sizeof(SQueryContinueReq), - .code = 0, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .msgType = TDMT_VND_QUERY_CONTINUE, + .refId = pConn->refId, + .pCont = req, + .contLen = sizeof(SQueryContinueReq), + .code = 0, }; int32_t code = tmsgPutToQueue(&mgmt->msgCb, QUERY_QUEUE, &pNewMsg); @@ -285,29 +291,29 @@ int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { return TSDB_CODE_SUCCESS; } - int32_t qwRegisterQueryBrokenLinkArg(QW_FPARAMS_DEF, SQWConnInfo *pConn) { - STaskDropReq * req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq)); + STaskDropReq *req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq)); if (NULL == req) { QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(STaskDropReq)); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } + } req->header.vgId = htonl(mgmt->nodeId); req->sId = htobe64(sId); req->queryId = htobe64(qId); req->taskId = htobe64(tId); req->refId = htobe64(rId); - + SRpcMsg pMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_DROP_TASK, - .pCont = req, - .contLen = sizeof(STaskDropReq), - .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .msgType = TDMT_VND_DROP_TASK, + .pCont = req, + .contLen = sizeof(STaskDropReq), + .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; - + tmsgRegisterBrokenLinkArg(&mgmt->msgCb, &pMsg); return TSDB_CODE_SUCCESS; @@ -333,43 +339,42 @@ int32_t qwRegisterHbBrokenLinkArg(SQWorkerMgmt *mgmt, uint64_t sId, SQWConnInfo taosMemoryFree(msg); QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - + SRpcMsg pMsg = { - .handle = pConn->handle, - .ahandle = pConn->ahandle, - .msgType = TDMT_VND_QUERY_HEARTBEAT, - .pCont = msg, - .contLen = msgSize, - .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .refId = pConn->refId, + .msgType = TDMT_VND_QUERY_HEARTBEAT, + .pCont = msg, + .contLen = msgSize, + .code = TSDB_CODE_RPC_NETWORK_UNAVAIL, }; - + tmsgRegisterBrokenLinkArg(&mgmt->msgCb, &pMsg); return TSDB_CODE_SUCCESS; } - - int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) { QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - int32_t code = 0; + int32_t code = 0; SSubQueryMsg *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen <= sizeof(*msg)) { QW_ELOG("invalid query msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - msg->sId = be64toh(msg->sId); + msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); - msg->taskId = be64toh(msg->taskId); - msg->refId = be64toh(msg->refId); - msg->phyLen = ntohl(msg->phyLen); - msg->sqlLen = ntohl(msg->sqlLen); + msg->taskId = be64toh(msg->taskId); + msg->refId = be64toh(msg->refId); + msg->phyLen = ntohl(msg->phyLen); + msg->sqlLen = ntohl(msg->sqlLen); uint64_t sId = msg->sId; uint64_t qId = msg->queryId; @@ -379,8 +384,9 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { SQWMsg qwMsg = {.node = node, .msg = msg->msg + msg->sqlLen, .msgLen = msg->phyLen}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; - char* sql = strndup(msg->msg, msg->sqlLen); + char *sql = strndup(msg->msg, msg->sqlLen); QW_SCH_TASK_DLOG("processQuery start, node:%p, handle:%p, sql:%s", node, pMsg->handle, sql); taosMemoryFreeClear(sql); @@ -388,17 +394,17 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processQuery end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { - int32_t code = 0; - int8_t status = 0; - bool queryDone = false; + int32_t code = 0; + int8_t status = 0; + bool queryDone = false; SQueryContinueReq *msg = (SQueryContinueReq *)pMsg->pCont; - bool needStop = false; - SQWTaskCtx *handles = NULL; - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + bool needStop = false; + SQWTaskCtx *handles = NULL; + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid cquery msg, msg:%p, msgLen:%d", msg, pMsg->contLen); @@ -408,11 +414,12 @@ int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; QW_SCH_TASK_DLOG("processCQuery start, node:%p, handle:%p", node, pMsg->handle); @@ -420,10 +427,10 @@ int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processCQuery end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } -int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ +int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } @@ -433,7 +440,7 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid task ready msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -442,18 +449,19 @@ int32_t qWorkerProcessReadyMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg){ uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; QW_SCH_TASK_DLOG("processReady start, node:%p, handle:%p", node, pMsg->handle); QW_ERR_RET(qwProcessReady(QW_FPARAMS(), &qwMsg)); QW_SCH_TASK_DLOG("processReady end, node:%p", node); - + return TSDB_CODE_SUCCESS; } @@ -462,24 +470,24 @@ int32_t qWorkerProcessStatusMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SSchTasksStatusReq *msg = pMsg->pCont; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { qError("invalid task status msg"); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; msg->sId = htobe64(msg->sId); uint64_t sId = msg->sId; SSchedulerStatusRsp *sStatus = NULL; - - //QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus)); + + // QW_ERR_JRET(qwGetSchTasksStatus(qWorkerMgmt, msg->sId, &sStatus)); _return: - //QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus)); + // QW_ERR_RET(qwBuildAndSendStatusRsp(pMsg, sStatus)); return TSDB_CODE_SUCCESS; } @@ -491,11 +499,11 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { SResFetchReq *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen < sizeof(*msg)) { - QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen); + QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -504,11 +512,12 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { uint64_t sId = msg->sId; uint64_t qId = msg->queryId; uint64_t tId = msg->taskId; - int64_t rId = 0; + int64_t rId = 0; SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; QW_SCH_TASK_DLOG("processFetch start, node:%p, handle:%p", node, pMsg->handle); @@ -516,7 +525,7 @@ int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processFetch end, node:%p", node); - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t qWorkerProcessFetchRsp(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { @@ -529,13 +538,13 @@ int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - int32_t code = 0; + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + int32_t code = 0; STaskCancelReq *msg = pMsg->pCont; if (NULL == msg || pMsg->contLen < sizeof(*msg)) { - qError("invalid task cancel msg"); + qError("invalid task cancel msg"); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -550,8 +559,9 @@ int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; - //QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId)); + // QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId)); _return: @@ -566,14 +576,14 @@ int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; STaskDropReq *msg = pMsg->pCont; SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + if (NULL == msg || pMsg->contLen < sizeof(*msg)) { QW_ELOG("invalid task drop msg, msg:%p, msgLen:%d", msg, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } msg->sId = be64toh(msg->sId); msg->queryId = be64toh(msg->queryId); @@ -588,9 +598,10 @@ int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; if (TSDB_CODE_RPC_NETWORK_UNAVAIL == pMsg->code) { - QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code)); + QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code)); } QW_SCH_TASK_DLOG("processDrop start, node:%p, handle:%p", node, pMsg->handle); @@ -607,14 +618,14 @@ int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SSchedulerHbReq req = {0}; - SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; - + SQWorkerMgmt *mgmt = (SQWorkerMgmt *)qWorkerMgmt; + if (NULL == pMsg->pCont) { QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen); QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } + } if (tDeserializeSSchedulerHbReq(pMsg->pCont, pMsg->contLen, &req)) { QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen); @@ -623,12 +634,13 @@ int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { } uint64_t sId = req.sId; - SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; + SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code}; qwMsg.connInfo.handle = pMsg->handle; qwMsg.connInfo.ahandle = pMsg->ahandle; + qwMsg.connInfo.refId = pMsg->refId; if (TSDB_CODE_RPC_NETWORK_UNAVAIL == pMsg->code) { - QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code)); + QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code)); } QW_SCH_DLOG("processHb start, node:%p, handle:%p", node, pMsg->handle); @@ -645,7 +657,7 @@ int32_t qWorkerProcessShowMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { return TSDB_CODE_QRY_INVALID_INPUT; } - int32_t code = 0; + int32_t code = 0; SVShowTablesReq *pReq = pMsg->pCont; QW_RET(qwBuildAndSendShowRsp(pMsg, code)); } From 6f29209464115ea4707e4a931787c30fcb5bda94 Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Mon, 25 Apr 2022 05:47:09 +0800 Subject: [PATCH 19/51] trigger CI From e29141a4ad5a43f2b9469dd50c4cb5ab158449fb Mon Sep 17 00:00:00 2001 From: slzhou Date: Mon, 25 Apr 2022 09:30:20 +0800 Subject: [PATCH 20/51] udfc open/close in vm init/cleanup --- include/libs/function/function.h | 14 ++++++++++++++ source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 7 ++++++- source/libs/function/inc/tudf.h | 12 ------------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/libs/function/function.h b/include/libs/function/function.h index 004d834287..474aba36d4 100644 --- a/include/libs/function/function.h +++ b/include/libs/function/function.h @@ -319,6 +319,20 @@ struct SUdfInfo; void qAddUdfInfo(uint64_t id, struct SUdfInfo* pUdfInfo); void qRemoveUdfInfo(uint64_t id, struct SUdfInfo* pUdfInfo); +/** + * create udfd proxy, called once in process that call setupUdf/callUdfxxx/teardownUdf + * @return error code + */ +int32_t udfcOpen(); + +/** + * destroy udfd proxy + * @return error code + */ +int32_t udfcClose(); + +typedef void *UdfcFuncHandle; + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index e784272581..687a799c57 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "vmInt.h" +#include "libs/function/function.h" SVnodeObj *vmAcquireVnode(SVnodesMgmt *pMgmt, int32_t vgId) { SVnodeObj *pVnode = NULL; @@ -275,7 +276,7 @@ static void vmCleanup(SMgmtWrapper *pWrapper) { pWrapper->pMgmt = NULL; // syncCleanUp(); - + udfcClose(); dInfo("vnode-mgmt is cleaned up"); } @@ -339,6 +340,10 @@ static int32_t vmInit(SMgmtWrapper *pWrapper) { } dmReportStartup(pDnode, "vnode-vnodes", "initialized"); + if (udfcOpen() != 0) { + dError("failed to open udfc in dnode"); + } + code = 0; _OVER: diff --git a/source/libs/function/inc/tudf.h b/source/libs/function/inc/tudf.h index 158f79667b..b72905b872 100644 --- a/source/libs/function/inc/tudf.h +++ b/source/libs/function/inc/tudf.h @@ -42,19 +42,7 @@ enum { UDFC_CODE_INVALID_STATE = -5 }; -typedef void *UdfcFuncHandle; -/** - * create udfd proxy, called once in process that call setupUdf/callUdfxxx/teardownUdf - * @return error code - */ -int32_t udfcOpen(); - -/** - * destroy udfd proxy - * @return error code - */ -int32_t udfcClose(); /** From 54dd9e3ca49da4bc2b706f99e215361d6e5649ce Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 25 Apr 2022 09:32:46 +0800 Subject: [PATCH 21/51] feature/qnode --- source/dnode/qnode/src/qnode.c | 5 ++++ source/libs/scalar/src/scalar.c | 44 +++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/source/dnode/qnode/src/qnode.c b/source/dnode/qnode/src/qnode.c index e3d7621fb0..907fddaec2 100644 --- a/source/dnode/qnode/src/qnode.c +++ b/source/dnode/qnode/src/qnode.c @@ -17,6 +17,7 @@ #include "qndInt.h" #include "query.h" #include "qworker.h" +//#include "tudf.h" SQnode *qndOpen(const SQnodeOpt *pOption) { SQnode *pQnode = taosMemoryCalloc(1, sizeof(SQnode)); @@ -25,6 +26,8 @@ SQnode *qndOpen(const SQnodeOpt *pOption) { return NULL; } + //udfcOpen(); + if (qWorkerInit(NODE_TYPE_QNODE, pQnode->qndId, NULL, (void **)&pQnode->pQuery, &pOption->msgCb)) { taosMemoryFreeClear(pQnode); return NULL; @@ -37,6 +40,8 @@ SQnode *qndOpen(const SQnodeOpt *pOption) { void qndClose(SQnode *pQnode) { qWorkerDestroy((void **)&pQnode->pQuery); + //udfcClose(); + taosMemoryFree(pQnode); } diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 6b2e7c17d3..820a4894b5 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -329,28 +329,40 @@ _return: } int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) { - SScalarFuncExecFuncs ffpSet = {0}; - int32_t code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet); - if (code) { - sclError("fmGetFuncExecFuncs failed, funcId:%d, code:%s", node->funcId, tstrerror(code)); - SCL_ERR_RET(code); - } - SScalarParam *params = NULL; int32_t rowNum = 0; int32_t paramNum = 0; + int32_t code = 0; SCL_ERR_RET(sclInitParamList(¶ms, node->pParameterList, ctx, ¶mNum, &rowNum)); - output->columnData = createColumnInfoData(&node->node.resType, rowNum); - if (output->columnData == NULL) { - sclError("calloc %d failed", (int32_t)(rowNum * output->columnData->info.bytes)); - SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - code = (*ffpSet.process)(params, paramNum, output); - if (code) { - sclError("scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code)); + if (fmIsUserDefinedFunc(node->funcId)) { +#if 0 + UdfcFuncHandle udfHandle = NULL; + + SCL_ERR_JRET(setupUdf(node->functionName, &udfHandle)); + code = callUdfScalarFunc(udfHandle, params, paramNum, output); + teardownUdf(udfHandle); SCL_ERR_JRET(code); +#endif + } else { + SScalarFuncExecFuncs ffpSet = {0}; + code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet); + if (code) { + sclError("fmGetFuncExecFuncs failed, funcId:%d, code:%s", node->funcId, tstrerror(code)); + SCL_ERR_JRET(code); + } + + output->columnData = createColumnInfoData(&node->node.resType, rowNum); + if (output->columnData == NULL) { + sclError("calloc %d failed", (int32_t)(rowNum * output->columnData->info.bytes)); + SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + code = (*ffpSet.process)(params, paramNum, output); + if (code) { + sclError("scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code)); + SCL_ERR_JRET(code); + } } _return: From 199a3610f774ec04a3dec10b9fdf9912dd650ae7 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 09:44:39 +0800 Subject: [PATCH 22/51] fix: fix some syntax errors. --- source/dnode/vnode/src/tq/tqPush.c | 14 -- source/libs/executor/inc/executorimpl.h | 21 ++- source/libs/executor/src/executorimpl.c | 227 ------------------------ source/libs/executor/src/scanoperator.c | 164 +++++++++++++++++ source/libs/function/inc/builtinsimpl.h | 2 +- source/libs/function/src/builtinsimpl.c | 35 +--- 6 files changed, 178 insertions(+), 285 deletions(-) delete mode 100644 source/dnode/vnode/src/tq/tqPush.c diff --git a/source/dnode/vnode/src/tq/tqPush.c b/source/dnode/vnode/src/tq/tqPush.c deleted file mode 100644 index f2f48bbc8a..0000000000 --- a/source/dnode/vnode/src/tq/tqPush.c +++ /dev/null @@ -1,14 +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 . - */ diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index c6d209e706..db0dd1ffbb 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -347,10 +347,11 @@ typedef struct STableScanInfo { } STableScanInfo; typedef struct STagScanInfo { - SColumnInfo* pCols; - SSDataBlock* pRes; + SColumnInfo *pCols; + SSDataBlock *pRes; int32_t totalTables; int32_t curPos; + void *pReader; } STagScanInfo; typedef struct SStreamBlockScanInfo { @@ -376,13 +377,11 @@ typedef struct SSysTableScanInfo { SEpSet epSet; tsem_t ready; - int32_t accountId; - bool showRewrite; - SNode* pCondition; // db_name filter condition, to discard data that are not in current database - void* pCur; // cursor for iterate the local table meta store. - SArray* scanCols; // SArray scan column id list - -// int32_t type; // show type, TODO remove it + int32_t accountId; + bool showRewrite; + SNode* pCondition; // db_name filter condition, to discard data that are not in current database + void* pCur; // cursor for iterate the local table meta store. + SArray* scanCols; // SArray scan column id list SName name; SSDataBlock* pRes; int32_t capacity; @@ -628,7 +627,7 @@ SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, SOperatorInfo* createExchangeOperatorInfo(const SNodeList* pSources, SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo); -SOperatorInfo* createTableScanOperatorInfo(void* pTsdbReadHandle, int32_t order, int32_t numOfCols, int32_t dataLoadFlag, int32_t repeatTime, +SOperatorInfo* createTableScanOperatorInfo(void* pReaderHandle, int32_t order, int32_t numOfCols, int32_t dataLoadFlag, int32_t repeatTime, int32_t reverseTime, SArray* pColMatchInfo, SSDataBlock* pResBlock, SNode* pCondition, SInterval* pInterval, double ratio, SExecTaskInfo* pTaskInfo); SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, SExprInfo* pScalarExprInfo, @@ -668,12 +667,12 @@ SOperatorInfo* createTimeSliceOperatorInfo(SOperatorInfo* downstream, SExprInfo* SSDataBlock* pResultBlock, SExecTaskInfo* pTaskInfo); SOperatorInfo* createJoinOperatorInfo(SOperatorInfo** pDownstream, int32_t numOfDownstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResBlock, SNode* pOnCondition, SExecTaskInfo* pTaskInfo); +SOperatorInfo* createTagScanOperatorInfo(void* pReaderHandle, SExprInfo* pExpr, int32_t numOfOutput, SExecTaskInfo* pTaskInfo); #if 0 SOperatorInfo* createTableSeqScanOperatorInfo(void* pTsdbReadHandle, STaskRuntimeEnv* pRuntimeEnv); SOperatorInfo* createMultiTableTimeIntervalOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfOutput); -SOperatorInfo* createTagScanOperatorInfo(SReaderHandle* pReaderHandle, SExprInfo* pExpr, int32_t numOfOutput); #endif void projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, SqlFunctionCtx* pCtx, diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 32d24e1975..3e58326cc9 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -235,7 +235,6 @@ static int32_t doCopyToSDataBlock(SSDataBlock* pBlock, SExprInfo* pExprInfo, SDi static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size); static void setResultBufSize(STaskAttr* pQueryAttr, SResultInfo* pResultInfo); -static void setCtxTagForJoin(STaskRuntimeEnv* pRuntimeEnv, SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, void* pTable); static void doSetTableGroupOutputBuf(SAggOperatorInfo* pAggInfo, int32_t numOfOutput, uint64_t groupId, SExecTaskInfo* pTaskInfo); @@ -298,26 +297,6 @@ SSDataBlock* createResDataBlock(SDataBlockDescNode* pNode) { return pBlock; } -static bool isSelectivityWithTagsQuery(SqlFunctionCtx* pCtx, int32_t numOfOutput) { - return true; - // bool hasTags = false; - // int32_t numOfSelectivity = 0; - // - // for (int32_t i = 0; i < numOfOutput; ++i) { - // int32_t functId = pCtx[i].functionId; - // if (functId == FUNCTION_TAG_DUMMY || functId == FUNCTION_TS_DUMMY) { - // hasTags = true; - // continue; - // } - // - // if ((aAggs[functId].status & FUNCSTATE_SELECTIVITY) != 0) { - // numOfSelectivity++; - // } - // } - // - // return (numOfSelectivity > 0 && hasTags); -} - static bool hasNull(SColumn* pColumn, SColumnDataAgg* pStatis) { if (TSDB_COL_IS_TAG(pColumn->flag) || TSDB_COL_IS_UD_COL(pColumn->flag) || pColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID) { @@ -1858,10 +1837,6 @@ void setBlockStatisInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* // set the output buffer for the selectivity + tag query static int32_t setCtxTagColumnInfo(SqlFunctionCtx* pCtx, int32_t numOfOutput) { - if (!isSelectivityWithTagsQuery(pCtx, numOfOutput)) { - return TSDB_CODE_SUCCESS; - } - int32_t num = 0; int16_t tagLen = 0; @@ -2111,25 +2086,6 @@ static int32_t updateBlockLoadStatus(STaskAttr* pQuery, int32_t status) { return status; } -static void doUpdateLastKey(STaskAttr* pQueryAttr) { - STimeWindow* win = &pQueryAttr->window; - - size_t num = taosArrayGetSize(pQueryAttr->tableGroupInfo.pGroupList); - for (int32_t i = 0; i < num; ++i) { - SArray* p1 = taosArrayGetP(pQueryAttr->tableGroupInfo.pGroupList, i); - - size_t len = taosArrayGetSize(p1); - for (int32_t j = 0; j < len; ++j) { - // STableKeyInfo* pInfo = taosArrayGet(p1, j); - // - // // update the new lastkey if it is equalled to the value of the old skey - // if (pInfo->lastKey == win->ekey) { - // pInfo->lastKey = win->skey; - // } - } - } -} - // static void updateDataCheckOrder(SQInfo *pQInfo, SQueryTableReq* pQueryMsg, bool stableQuery) { // STaskAttr* pQueryAttr = pQInfo->runtimeEnv.pQueryAttr; // @@ -3075,33 +3031,6 @@ void setExecutionContext(int32_t numOfOutput, uint64_t groupId, SExecTaskInfo* p pAggInfo->groupId = groupId; } -void setCtxTagForJoin(STaskRuntimeEnv* pRuntimeEnv, SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, void* pTable) { - STaskAttr* pQueryAttr = pRuntimeEnv->pQueryAttr; - - SExprBasicInfo* pExpr = &pExprInfo->base; - // if (pQueryAttr->stableQuery && (pRuntimeEnv->pTsBuf != NULL) && - // (pExpr->functionId == FUNCTION_TS || pExpr->functionId == FUNCTION_PRJ) && - // (pExpr->colInfo.colIndex == PRIMARYKEY_TIMESTAMP_COL_ID)) { - // assert(pExpr->numOfParams == 1); - // - // int16_t tagColId = (int16_t)pExprInfo->base.param[0].i; - // SColumnInfo* pColInfo = doGetTagColumnInfoById(pQueryAttr->tagColList, pQueryAttr->numOfTags, tagColId); - // - // doSetTagValueInParam(pTable, tagColId, &pCtx->tag, pColInfo->type, pColInfo->bytes); - // - // int16_t tagType = pCtx[0].tag.nType; - // if (tagType == TSDB_DATA_TYPE_BINARY || tagType == TSDB_DATA_TYPE_NCHAR) { - // //qDebug("QInfo:0x%"PRIx64" set tag value for join comparison, colId:%" PRId64 ", val:%s", - // GET_TASKID(pRuntimeEnv), - //// pExprInfo->base.param[0].i, pCtx[0].tag.pz); - // } else { - // //qDebug("QInfo:0x%"PRIx64" set tag value for join comparison, colId:%" PRId64 ", val:%" PRId64, - // GET_TASKID(pRuntimeEnv), - //// pExprInfo->base.param[0].i, pCtx[0].tag.i); - // } - // } -} - /* * There are two cases to handle: * @@ -5863,11 +5792,6 @@ static void destroyProjectOperatorInfo(void* param, int32_t numOfOutput) { doDestroyBasicInfo(&pInfo->binfo, numOfOutput); } -static void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput) { - STagScanInfo* pInfo = (STagScanInfo*)param; - pInfo->pRes = blockDataDestroy(pInfo->pRes); -} - static void destroyOrderOperatorInfo(void* param, int32_t numOfOutput) { SSortOperatorInfo* pInfo = (SSortOperatorInfo*)param; pInfo->pDataBlock = blockDataDestroy(pInfo->pDataBlock); @@ -6221,157 +6145,6 @@ _error: return NULL; } -static SSDataBlock* doTagScan(SOperatorInfo* pOperator, bool* newgroup) { -#if 0 - SOperatorInfo* pOperator = (SOperatorInfo*) param; - if (pOperator->status == OP_EXEC_DONE) { - return NULL; - } - - int32_t maxNumOfTables = (int32_t)pResultInfo->capacity; - - STagScanInfo *pInfo = pOperator->info; - SSDataBlock *pRes = pInfo->pRes; - *newgroup = false; - - int32_t count = 0; - SArray* pa = GET_TABLEGROUP(pRuntimeEnv, 0); - - int32_t functionId = getExprFunctionId(&pOperator->pExpr[0]); - if (functionId == FUNCTION_TID_TAG) { // return the tags & table Id - assert(pQueryAttr->numOfOutput == 1); - - SExprInfo* pExprInfo = &pOperator->pExpr[0]; - int32_t rsize = pExprInfo->base.resSchema.bytes; - - count = 0; - - int16_t bytes = pExprInfo->base.resSchema.bytes; - int16_t type = pExprInfo->base.resSchema.type; - - for(int32_t i = 0; i < pQueryAttr->numOfTags; ++i) { - if (pQueryAttr->tagColList[i].colId == pExprInfo->base.pColumns->info.colId) { - bytes = pQueryAttr->tagColList[i].bytes; - type = pQueryAttr->tagColList[i].type; - break; - } - } - - SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0); - - while(pInfo->curPos < pInfo->totalTables && count < maxNumOfTables) { - int32_t i = pInfo->curPos++; - STableQueryInfo *item = taosArrayGetP(pa, i); - - char *output = pColInfo->pData + count * rsize; - varDataSetLen(output, rsize - VARSTR_HEADER_SIZE); - - output = varDataVal(output); - STableId* id = TSDB_TABLEID(item->pTable); - - *(int16_t *)output = 0; - output += sizeof(int16_t); - - *(int64_t *)output = id->uid; // memory align problem, todo serialize - output += sizeof(id->uid); - - *(int32_t *)output = id->tid; - output += sizeof(id->tid); - - *(int32_t *)output = pQueryAttr->vgId; - output += sizeof(pQueryAttr->vgId); - - char* data = NULL; - if (pExprInfo->base.pColumns->info.colId == TSDB_TBNAME_COLUMN_INDEX) { - data = tsdbGetTableName(item->pTable); - } else { - data = tsdbGetTableTagVal(item->pTable, pExprInfo->base.pColumns->info.colId, type, bytes); - } - - doSetTagValueToResultBuf(output, data, type, bytes); - count += 1; - } - - //qDebug("QInfo:0x%"PRIx64" create (tableId, tag) info completed, rows:%d", GET_TASKID(pRuntimeEnv), count); - } else if (functionId == FUNCTION_COUNT) {// handle the "count(tbname)" query - SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0); - *(int64_t*)pColInfo->pData = pInfo->totalTables; - count = 1; - - pOperator->status = OP_EXEC_DONE; - //qDebug("QInfo:0x%"PRIx64" create count(tbname) query, res:%d rows:1", GET_TASKID(pRuntimeEnv), count); - } else { // return only the tags|table name etc. - SExprInfo* pExprInfo = &pOperator->pExpr[0]; // todo use the column list instead of exprinfo - - count = 0; - while(pInfo->curPos < pInfo->totalTables && count < maxNumOfTables) { - int32_t i = pInfo->curPos++; - - STableQueryInfo* item = taosArrayGetP(pa, i); - - char *data = NULL, *dst = NULL; - int16_t type = 0, bytes = 0; - for(int32_t j = 0; j < pOperator->numOfOutput; ++j) { - // not assign value in case of user defined constant output column - if (TSDB_COL_IS_UD_COL(pExprInfo[j].base.pColumns->flag)) { - continue; - } - - SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, j); - type = pExprInfo[j].base.resSchema.type; - bytes = pExprInfo[j].base.resSchema.bytes; - - if (pExprInfo[j].base.pColumns->info.colId == TSDB_TBNAME_COLUMN_INDEX) { - data = tsdbGetTableName(item->pTable); - } else { - data = tsdbGetTableTagVal(item->pTable, pExprInfo[j].base.pColumns->info.colId, type, bytes); - } - - dst = pColInfo->pData + count * pExprInfo[j].base.resSchema.bytes; - doSetTagValueToResultBuf(dst, data, type, bytes); - } - - count += 1; - } - - if (pInfo->curPos >= pInfo->totalTables) { - pOperator->status = OP_EXEC_DONE; - } - - //qDebug("QInfo:0x%"PRIx64" create tag values results completed, rows:%d", GET_TASKID(pRuntimeEnv), count); - } - - if (pOperator->status == OP_EXEC_DONE) { - setTaskStatus(pOperator->pRuntimeEnv, TASK_COMPLETED); - } - - pRes->info.rows = count; - return (pRes->info.rows == 0)? NULL:pInfo->pRes; - -#endif - return TSDB_CODE_SUCCESS; -} - -SOperatorInfo* createTagScanOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput) { - STagScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STagScanInfo)); - size_t numOfGroup = GET_NUM_OF_TABLEGROUP(pRuntimeEnv); - assert(numOfGroup == 0 || numOfGroup == 1); - - pInfo->curPos = 0; - - SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); - pOperator->name = "SeqTableTagScan"; - pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN; - pOperator->blockingOptr = false; - pOperator->status = OP_NOT_OPENED; - pOperator->info = pInfo; - pOperator->getNextFn = doTagScan; - pOperator->pExpr = pExpr; - pOperator->numOfOutput = numOfOutput; - pOperator->closeFn = destroyTagScanOperatorInfo; - - return pOperator; -} static int32_t getColumnIndexInSource(SQueriedTableInfo* pTableInfo, SExprBasicInfo* pExpr, SColumnInfo* pTagCols) { int32_t j = 0; diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index 3f3f6a6fb3..a70e402871 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -980,3 +980,167 @@ SOperatorInfo* createSysTableScanOperatorInfo(void* pSysTableReadHandle, SSDataB return pOperator; } + +static SSDataBlock* doTagScan(SOperatorInfo* pOperator, bool* newgroup) { +#if 0 + SOperatorInfo* pOperator = (SOperatorInfo*) param; + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } + + int32_t maxNumOfTables = (int32_t)pResultInfo->capacity; + + STagScanInfo *pInfo = pOperator->info; + SSDataBlock *pRes = pInfo->pRes; + *newgroup = false; + + int32_t count = 0; + SArray* pa = GET_TABLEGROUP(pRuntimeEnv, 0); + + int32_t functionId = getExprFunctionId(&pOperator->pExpr[0]); + if (functionId == FUNCTION_TID_TAG) { // return the tags & table Id + assert(pQueryAttr->numOfOutput == 1); + + SExprInfo* pExprInfo = &pOperator->pExpr[0]; + int32_t rsize = pExprInfo->base.resSchema.bytes; + + count = 0; + + int16_t bytes = pExprInfo->base.resSchema.bytes; + int16_t type = pExprInfo->base.resSchema.type; + + for(int32_t i = 0; i < pQueryAttr->numOfTags; ++i) { + if (pQueryAttr->tagColList[i].colId == pExprInfo->base.pColumns->info.colId) { + bytes = pQueryAttr->tagColList[i].bytes; + type = pQueryAttr->tagColList[i].type; + break; + } + } + + SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0); + + while(pInfo->curPos < pInfo->totalTables && count < maxNumOfTables) { + int32_t i = pInfo->curPos++; + STableQueryInfo *item = taosArrayGetP(pa, i); + + char *output = pColInfo->pData + count * rsize; + varDataSetLen(output, rsize - VARSTR_HEADER_SIZE); + + output = varDataVal(output); + STableId* id = TSDB_TABLEID(item->pTable); + + *(int16_t *)output = 0; + output += sizeof(int16_t); + + *(int64_t *)output = id->uid; // memory align problem, todo serialize + output += sizeof(id->uid); + + *(int32_t *)output = id->tid; + output += sizeof(id->tid); + + *(int32_t *)output = pQueryAttr->vgId; + output += sizeof(pQueryAttr->vgId); + + char* data = NULL; + if (pExprInfo->base.pColumns->info.colId == TSDB_TBNAME_COLUMN_INDEX) { + data = tsdbGetTableName(item->pTable); + } else { + data = tsdbGetTableTagVal(item->pTable, pExprInfo->base.pColumns->info.colId, type, bytes); + } + + doSetTagValueToResultBuf(output, data, type, bytes); + count += 1; + } + + //qDebug("QInfo:0x%"PRIx64" create (tableId, tag) info completed, rows:%d", GET_TASKID(pRuntimeEnv), count); + } else if (functionId == FUNCTION_COUNT) {// handle the "count(tbname)" query + SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0); + *(int64_t*)pColInfo->pData = pInfo->totalTables; + count = 1; + + pOperator->status = OP_EXEC_DONE; + //qDebug("QInfo:0x%"PRIx64" create count(tbname) query, res:%d rows:1", GET_TASKID(pRuntimeEnv), count); + } else { // return only the tags|table name etc. + SExprInfo* pExprInfo = &pOperator->pExpr[0]; // todo use the column list instead of exprinfo + + count = 0; + while(pInfo->curPos < pInfo->totalTables && count < maxNumOfTables) { + int32_t i = pInfo->curPos++; + + STableQueryInfo* item = taosArrayGetP(pa, i); + + char *data = NULL, *dst = NULL; + int16_t type = 0, bytes = 0; + for(int32_t j = 0; j < pOperator->numOfOutput; ++j) { + // not assign value in case of user defined constant output column + if (TSDB_COL_IS_UD_COL(pExprInfo[j].base.pColumns->flag)) { + continue; + } + + SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, j); + type = pExprInfo[j].base.resSchema.type; + bytes = pExprInfo[j].base.resSchema.bytes; + + if (pExprInfo[j].base.pColumns->info.colId == TSDB_TBNAME_COLUMN_INDEX) { + data = tsdbGetTableName(item->pTable); + } else { + data = tsdbGetTableTagVal(item->pTable, pExprInfo[j].base.pColumns->info.colId, type, bytes); + } + + dst = pColInfo->pData + count * pExprInfo[j].base.resSchema.bytes; + doSetTagValueToResultBuf(dst, data, type, bytes); + } + + count += 1; + } + + if (pInfo->curPos >= pInfo->totalTables) { + pOperator->status = OP_EXEC_DONE; + } + + //qDebug("QInfo:0x%"PRIx64" create tag values results completed, rows:%d", GET_TASKID(pRuntimeEnv), count); + } + + if (pOperator->status == OP_EXEC_DONE) { + setTaskStatus(pOperator->pRuntimeEnv, TASK_COMPLETED); + } + + pRes->info.rows = count; + return (pRes->info.rows == 0)? NULL:pInfo->pRes; + +#endif + return TSDB_CODE_SUCCESS; +} + +static void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput) { + STagScanInfo* pInfo = (STagScanInfo*)param; + pInfo->pRes = blockDataDestroy(pInfo->pRes); +} + +SOperatorInfo* createTagScanOperatorInfo(void* pReaderHandle, SExprInfo* pExpr, int32_t numOfOutput, SExecTaskInfo* pTaskInfo) { + STagScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STagScanInfo)); + SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); + if (pInfo == NULL || pOperator == NULL) { + goto _error; + } + + pInfo->pReader = pReaderHandle; + pInfo->curPos = 0; + pOperator->name = "TagScanOperator"; + pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN; + pOperator->blockingOptr = false; + pOperator->status = OP_NOT_OPENED; + pOperator->info = pInfo; + pOperator->getNextFn = doTagScan; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfOutput; + pOperator->pTaskInfo = pTaskInfo; + pOperator->closeFn = destroyTagScanOperatorInfo; + + return pOperator; + _error: + taosMemoryFree(pInfo); + taosMemoryFree(pOperator); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; +} diff --git a/source/libs/function/inc/builtinsimpl.h b/source/libs/function/inc/builtinsimpl.h index d424ed41bc..0ad3730b5a 100644 --- a/source/libs/function/inc/builtinsimpl.h +++ b/source/libs/function/inc/builtinsimpl.h @@ -43,7 +43,7 @@ int32_t maxFunction(SqlFunctionCtx *pCtx); bool getAvgFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool avgFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t avgFunction(SqlFunctionCtx* pCtx); -int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); +int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getStddevFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool stddevFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 129bebe174..0b9765ef15 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -44,8 +44,6 @@ typedef struct STopBotResItem { } STopBotResItem; typedef struct STopBotRes { - int32_t pageId; -// int32_t num; STopBotResItem *pItems; } STopBotRes; @@ -92,18 +90,6 @@ typedef struct SDiffInfo { } \ } while (0); -#define DO_UPDATE_SUBSID_RES(ctx, ts) \ - do { \ - for (int32_t _i = 0; _i < (ctx)->subsidiaryRes.numOfCols; ++_i) { \ - SqlFunctionCtx *__ctx = (ctx)->subsidiaryRes.pCtx[_i]; \ - if (__ctx->functionId == FUNCTION_TS_DUMMY) { \ - __ctx->tag.i = (ts); \ - __ctx->tag.nType = TSDB_DATA_TYPE_BIGINT; \ - } \ - __ctx->fpSet.process(__ctx); \ - } \ - } while (0) - #define UPDATE_DATA(ctx, left, right, num, sign, _ts) \ do { \ if (((left) < (right)) ^ (sign)) { \ @@ -407,7 +393,7 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; } -int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { +int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SInputColumnInfoData* pInput = &pCtx->input; int32_t type = pInput->pData[0]->info.type; SAvgRes* pAvgRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); @@ -417,7 +403,7 @@ int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { pAvgRes->result = pAvgRes->sum.dsum / ((double) pAvgRes->count); } - return functionFinalize(pCtx, pBlock, slotId); + return functionFinalize(pCtx, pBlock); } EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow){ @@ -841,7 +827,7 @@ int32_t stddevFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; } -int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { +int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SInputColumnInfoData* pInput = &pCtx->input; int32_t type = pInput->pData[0]->info.type; SStddevRes* pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); @@ -1383,21 +1369,6 @@ int32_t diffFunction(SqlFunctionCtx *pCtx) { } } -typedef struct STopBotResItem { - SVariant v; - uint64_t uid; // it is a table uid, used to extract tag data during building of the final result for the tag data - struct { - int32_t pageId; - int32_t offset; - } tuplePos; // tuple data of this chosen row -} STopBotResItem; - -typedef struct STopBotRes { -// int32_t pageId; -// int32_t num; - STopBotResItem *pItems; -} STopBotRes; - bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { SValueNode* pkNode = (SValueNode*) nodesListGetNode(pFunc->pParameterList, 1); pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * sizeof(STopBotResItem); From 6a83b17713853e4e61aae7ef3b39862d3ab5bedf Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 10:16:00 +0800 Subject: [PATCH 23/51] other: merge 3.0 --- source/dnode/vnode/src/tq/tqPush.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 source/dnode/vnode/src/tq/tqPush.c diff --git a/source/dnode/vnode/src/tq/tqPush.c b/source/dnode/vnode/src/tq/tqPush.c new file mode 100644 index 0000000000..f2f48bbc8a --- /dev/null +++ b/source/dnode/vnode/src/tq/tqPush.c @@ -0,0 +1,14 @@ +/* + * 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 . + */ From 7968144c5c5708728d41266b9aead1962f2977d5 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 10:22:48 +0800 Subject: [PATCH 24/51] refactor(query): do some internal refactor. --- source/libs/executor/src/executorimpl.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 3e58326cc9..6a5f5b2dc3 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -6352,6 +6352,9 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo int32_t numOfCols = 0; tsdbReaderT pDataReader = doCreateDataReader(pTableScanNode, pHandle, pTableGroupInfo, (uint64_t)queryId, taskId); + if (pDataReader == NULL) { + return NULL; + } SArray* pColList = extractColMatchInfo(pScanPhyNode->pScanCols, pScanPhyNode->node.pOutputDataBlockDesc, &numOfCols); @@ -6375,17 +6378,14 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN == type) { SScanPhysiNode* pScanPhyNode = (SScanPhysiNode*)pPhyNode; // simple child table. - int32_t code = doCreateTableGroup(pHandle->meta, pScanPhyNode->tableType, pScanPhyNode->uid, pTableGroupInfo, - queryId, taskId); + int32_t code = doCreateTableGroup(pHandle->meta, pScanPhyNode->tableType, pScanPhyNode->uid, pTableGroupInfo, queryId, taskId); SArray* tableIdList = extractTableIdList(pTableGroupInfo); SSDataBlock* pResBlock = createResDataBlock(pScanPhyNode->node.pOutputDataBlockDesc); int32_t numOfCols = 0; - SArray* pColList = - extractColMatchInfo(pScanPhyNode->pScanCols, pScanPhyNode->node.pOutputDataBlockDesc, &numOfCols); - SOperatorInfo* pOperator = - createStreamScanOperatorInfo(pHandle->reader, pResBlock, pColList, tableIdList, pTaskInfo); + SArray* pCols = extractColMatchInfo(pScanPhyNode->pScanCols, pScanPhyNode->node.pOutputDataBlockDesc, &numOfCols); + SOperatorInfo* pOperator = createStreamScanOperatorInfo(pHandle->reader, pResBlock, pCols, tableIdList, pTaskInfo); taosArrayDestroy(tableIdList); return pOperator; } else if (QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == type) { @@ -6411,6 +6411,9 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo for (int32_t i = 0; i < size; ++i) { SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pPhyNode->pChildren, i); ops[i] = createOperatorTree(pChildNode, pTaskInfo, pHandle, queryId, taskId, pTableGroupInfo); + if (ops[i] == NULL) { + return NULL; + } } SOperatorInfo* pOptr = NULL; From ac881a7e03643954075ddd99fe7077a5b509455b Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 10:36:13 +0800 Subject: [PATCH 25/51] other: merge 3.0 --- tools/shell/inc/shell.h | 93 ---- tools/shell/inc/shellCommand.h | 55 -- tools/shell/inc/shellInt.h | 117 ++++ tools/shell/inc/syncMsg.h | 141 ----- tools/shell/inc/tsync.h | 127 ----- tools/shell/src/backup/shellCheck.c | 202 ------- tools/shell/src/backup/shellDarwin.c | 498 ------------------ tools/shell/src/backup/shellImport.c | 279 ---------- tools/shell/src/backup/shellWindows.c | 325 ------------ tools/shell/src/shellArguments.c | 354 +++++++++++++ tools/shell/src/shellCommand.c | 496 ++++++++++++++--- tools/shell/src/shellEngine.c | 732 +++++++++++--------------- tools/shell/src/shellMain.c | 712 ++----------------------- tools/shell/src/shellNettest.c | 145 +++++ tools/shell/src/shellUtil.c | 132 +++++ tools/shell/src/tnettest.c | 514 ------------------ 16 files changed, 1525 insertions(+), 3397 deletions(-) delete mode 100644 tools/shell/inc/shell.h delete mode 100644 tools/shell/inc/shellCommand.h create mode 100644 tools/shell/inc/shellInt.h delete mode 100644 tools/shell/inc/syncMsg.h delete mode 100644 tools/shell/inc/tsync.h delete mode 100644 tools/shell/src/backup/shellCheck.c delete mode 100644 tools/shell/src/backup/shellDarwin.c delete mode 100644 tools/shell/src/backup/shellImport.c delete mode 100644 tools/shell/src/backup/shellWindows.c create mode 100644 tools/shell/src/shellArguments.c create mode 100644 tools/shell/src/shellNettest.c create mode 100644 tools/shell/src/shellUtil.c delete mode 100644 tools/shell/src/tnettest.c diff --git a/tools/shell/inc/shell.h b/tools/shell/inc/shell.h deleted file mode 100644 index 866cd63bdb..0000000000 --- a/tools/shell/inc/shell.h +++ /dev/null @@ -1,93 +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_SHELL_H_ -#define _TD_SHELL_H_ - -#include "os.h" - -#include "taos.h" -#include "taosdef.h" - -#define MAX_USERNAME_SIZE 64 -#define MAX_DBNAME_SIZE 64 -#define MAX_IP_SIZE 20 -#define MAX_HISTORY_SIZE 1000 -#define MAX_COMMAND_SIZE 1048586 -#define HISTORY_FILE ".taos_history" - -#define DEFAULT_RES_SHOW_NUM 100 - -typedef struct SShellHistory { - char* hist[MAX_HISTORY_SIZE]; - int hstart; - int hend; -} SShellHistory; - -typedef struct SShellArguments { - char* host; - char* password; - char* user; - char* auth; - char* database; - char* timezone; - bool is_raw_time; - bool is_use_passwd; - bool dump_config; - char file[TSDB_FILENAME_LEN]; - char dir[TSDB_FILENAME_LEN]; - int threadNum; - int check; - bool status; - bool verbose; - char* commands; - int abort; - int port; - int pktLen; - int pktNum; - char* pktType; - char* netTestRole; -} SShellArguments; - -/**************** Function declarations ****************/ -extern void shellParseArgument(int argc, char* argv[], SShellArguments* arguments); -extern TAOS* shellInit(SShellArguments* args); -extern void* shellLoopQuery(void* arg); -extern void taos_error(TAOS_RES* tres, int64_t st); -extern int regex_match(const char* s, const char* reg, int cflags); -int32_t shellReadCommand(TAOS* con, char command[]); -int32_t shellRunCommand(TAOS* con, char* command); -void shellRunCommandOnServer(TAOS* con, char command[]); -void read_history(); -void write_history(); -void source_file(TAOS* con, char* fptr); -void source_dir(TAOS* con, SShellArguments* args); -void get_history_path(char* history); -void shellCheck(TAOS* con, SShellArguments* args); -void cleanup_handler(void* arg); -void exitShell(); -int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode); -void shellGetGrantInfo(void *con); -int isCommentLine(char *line); - -/**************** Global variable declarations ****************/ -extern char PROMPT_HEADER[]; -extern char CONTINUE_PROMPT[]; -extern int prompt_size; -extern SShellHistory history; -extern SShellArguments args; -extern int64_t result; - -#endif diff --git a/tools/shell/inc/shellCommand.h b/tools/shell/inc/shellCommand.h deleted file mode 100644 index 49f7dc0133..0000000000 --- a/tools/shell/inc/shellCommand.h +++ /dev/null @@ -1,55 +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_SHELL_COMMAND_H_ -#define _TD_SHELL_COMMAND_H_ - -#include "shell.h" - -#define LEFT 1 -#define RIGHT 2 -#define UP 3 -#define DOWN 4 - -typedef struct Command Command; -struct Command { - char * buffer; - char * command; - unsigned commandSize; - unsigned bufferSize; - unsigned cursorOffset; - unsigned screenOffset; - unsigned endOffset; -}; - -extern void backspaceChar(Command *cmd); -extern void clearLineBefore(Command *cmd); -extern void clearLineAfter(Command *cmd); -extern void deleteChar(Command *cmd); -extern void moveCursorLeft(Command *cmd); -extern void moveCursorRight(Command *cmd); -extern void positionCursorHome(Command *cmd); -extern void positionCursorEnd(Command *cmd); -extern void showOnScreen(Command *cmd); -extern void updateBuffer(Command *cmd); -extern int isReadyGo(Command *cmd); -extern void resetCommand(Command *cmd, const char s[]); - -int countPrefixOnes(unsigned char c); -void clearScreen(int ecmd_pos, int cursor_pos); -void printChar(char c, int times); -void positionCursor(int step, int direction); - -#endif diff --git a/tools/shell/inc/shellInt.h b/tools/shell/inc/shellInt.h new file mode 100644 index 0000000000..af28373510 --- /dev/null +++ b/tools/shell/inc/shellInt.h @@ -0,0 +1,117 @@ +/* + * 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_SHELL_INT_H_ +#define _TD_SHELL_INT_H_ + +#include "os.h" +#include "taos.h" +#include "taosdef.h" +#include "taoserror.h" +#include "tconfig.h" +#include "tglobal.h" +#include "trpc.h" +#include "ttypes.h" +#include "tutil.h" + +#define SHELL_MAX_HISTORY_SIZE 1000 +#define SHELL_MAX_COMMAND_SIZE 1048586 +#define SHELL_HISTORY_FILE ".taos_history" +#define SHELL_DEFAULT_RES_SHOW_NUM 100 +#define SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH 30 +#define SHELL_MAX_PKG_LEN 2 * 1024 * 1024 +#define SHELL_MIN_PKG_LEN 1 +#define SHELL_DEF_PKG_LEN 1024 +#define SHELL_MAX_PKG_NUM 1 * 1024 * 1024 +#define SHELL_MIN_PKG_NUM 1 +#define SHELL_DEF_PKG_NUM 100 + +typedef struct { + char* hist[SHELL_MAX_HISTORY_SIZE]; + char file[TSDB_FILENAME_LEN]; + int32_t hstart; + int32_t hend; +} SShellHistory; + +typedef struct { + const char* host; + const char* user; + const char* auth; + const char* database; + const char* cfgdir; + const char* commands; + const char* netrole; + char file[PATH_MAX]; + char password[TSDB_USET_PASSWORD_LEN]; + bool is_gen_auth; + bool is_raw_time; + bool is_version; + bool is_dump_config; + bool is_check; + bool is_startup; + bool is_help; + uint16_t port; + int32_t pktLen; + int32_t pktNum; + int32_t displayWidth; + int32_t abort; +} SShellArgs; + +typedef struct { + const char* clientVersion; + const char* promptHeader; + const char* promptContinue; + const char* osname; + int32_t promptSize; + char programVersion[32]; +} SShellOsDetails; + +typedef struct { + SShellArgs args; + SShellHistory history; + SShellOsDetails info; + TAOS* conn; + TdThread pid; + tsem_t cancelSem; + int64_t result; +} SShellObj; + +// shellArguments.c +int32_t shellParseArgs(int32_t argc, char* argv[]); + +// shellCommand.c +int32_t shellReadCommand(char* command); + +// shellEngine.c +int32_t shellExecute(); + +// shellUtil.c +int32_t shellCheckIntSize(); +void shellPrintVersion(); +void shellPrintHelp(); +void shellGenerateAuth(); +void shellDumpConfig(); +void shellCheckServerStatus(); +bool shellRegexMatch(const char* s, const char* reg, int32_t cflags); +void shellExit(); + +// shellNettest.c +void shellTestNetWork(); + +// shellMain.c +extern SShellObj shell; +extern void taos_init(); + +#endif /*_TD_SHELL_INT_H_*/ diff --git a/tools/shell/inc/syncMsg.h b/tools/shell/inc/syncMsg.h deleted file mode 100644 index 85ac9c78af..0000000000 --- a/tools/shell/inc/syncMsg.h +++ /dev/null @@ -1,141 +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 TDENGINE_SYNC_MSG_H -#define TDENGINE_SYNC_MSG_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "tsync.h" - -typedef enum { - TAOS_SMSG_START = 0, - TAOS_SMSG_SYNC_DATA = 1, - TAOS_SMSG_SYNC_DATA_RSP = 2, - TAOS_SMSG_SYNC_FWD = 3, - TAOS_SMSG_SYNC_FWD_RSP = 4, - TAOS_SMSG_SYNC_REQ = 5, - TAOS_SMSG_SYNC_REQ_RSP = 6, - TAOS_SMSG_SYNC_MUST = 7, - TAOS_SMSG_SYNC_MUST_RSP = 8, - TAOS_SMSG_STATUS = 9, - TAOS_SMSG_STATUS_RSP = 10, - TAOS_SMSG_SETUP = 11, - TAOS_SMSG_SETUP_RSP = 12, - TAOS_SMSG_SYNC_FILE = 13, - TAOS_SMSG_SYNC_FILE_RSP = 14, - TAOS_SMSG_TEST = 15, - TAOS_SMSG_END = 16 -} ESyncMsgType; - -typedef enum { - SYNC_STATUS_BROADCAST, - SYNC_STATUS_BROADCAST_RSP, - SYNC_STATUS_SETUP_CONN, - SYNC_STATUS_SETUP_CONN_RSP, - SYNC_STATUS_EXCHANGE_DATA, - SYNC_STATUS_EXCHANGE_DATA_RSP, - SYNC_STATUS_CHECK_ROLE, - SYNC_STATUS_CHECK_ROLE_RSP -} ESyncStatusType; - -#pragma pack(push, 1) - -typedef struct { - int8_t type; // msg type - int8_t protocol; // protocol version - uint16_t signature; // fixed value - int32_t code; // - int32_t cId; // cluster Id - int32_t vgId; // vg ID - int32_t len; // content length, does not include head - uint32_t cksum; -} SSyncHead; - -typedef struct { - SSyncHead head; - uint16_t port; - uint16_t tranId; - int32_t sourceId; // only for arbitrator - char fqdn[TSDB_FQDN_LEN]; -} SSyncMsg; - -typedef struct { - SSyncHead head; - int8_t sync; - int8_t reserved; - uint16_t tranId; - int8_t reserverd[4]; -} SSyncRsp; - -typedef struct { - int8_t role; - uint64_t version; -} SPeerStatus; - -typedef struct { - SSyncHead head; - int8_t role; - int8_t ack; - int8_t type; - int8_t reserved[3]; - uint16_t tranId; - uint64_t version; - SPeerStatus peersStatus[TAOS_SYNC_MAX_REPLICA]; -} SPeersStatus; - -typedef struct { - SSyncHead head; - uint64_t fversion; -} SFileVersion; - -typedef struct { - SSyncHead head; - int8_t ack; -} SFileAck; - -typedef struct { - SSyncHead head; - uint64_t version; - int32_t code; -} SFwdRsp; - -#pragma pack(pop) - -#define SYNC_PROTOCOL_VERSION 1 -#define SYNC_SIGNATURE ((uint16_t)(0xCDEF)) - -extern char *statusType[]; - -uint16_t syncGenTranId(); -int32_t syncCheckHead(SSyncHead *pHead); - -void syncBuildSyncFwdMsg(SSyncHead *pHead, int32_t vgId, int32_t len); -void syncBuildSyncFwdRsp(SFwdRsp *pMsg, int32_t vgId, uint64_t version, int32_t code); -void syncBuildSyncReqMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildSyncDataMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildSyncSetupMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildPeersStatus(SPeersStatus *pMsg, int32_t vgId); -void syncBuildSyncTestMsg(SSyncMsg *pMsg, int32_t vgId); - -void syncBuildFileAck(SFileAck *pMsg, int32_t vgId); -void syncBuildFileVersion(SFileVersion *pMsg, int32_t vgId); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_VNODEPEER_H diff --git a/tools/shell/inc/tsync.h b/tools/shell/inc/tsync.h deleted file mode 100644 index d1b68e3f5a..0000000000 --- a/tools/shell/inc/tsync.h +++ /dev/null @@ -1,127 +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 TDENGINE_SYNC_H -#define TDENGINE_SYNC_H - -#ifdef __cplusplus -extern "C" { -#endif - -#define TAOS_SYNC_MAX_REPLICA 5 -#define TAOS_SYNC_MAX_INDEX 0x7FFFFFFF - -typedef enum { - TAOS_SYNC_ROLE_OFFLINE = 0, - TAOS_SYNC_ROLE_UNSYNCED = 1, - TAOS_SYNC_ROLE_SYNCING = 2, - TAOS_SYNC_ROLE_SLAVE = 3, - TAOS_SYNC_ROLE_MASTER = 4 -} ESyncRole; - -typedef enum { - TAOS_SYNC_STATUS_INIT = 0, - TAOS_SYNC_STATUS_START = 1, - TAOS_SYNC_STATUS_FILE = 2, - TAOS_SYNC_STATUS_CACHE = 3 -} ESyncStatus; - -typedef struct { - uint32_t nodeId; // node ID assigned by TDengine - uint16_t nodePort; // node sync Port - char nodeFqdn[TSDB_FQDN_LEN]; // node FQDN -} SNodeInfo; - -typedef struct { - int8_t quorum; // number of confirms required, >=1 - int8_t replica; // number of replications, >=1 - SNodeInfo nodeInfo[TAOS_SYNC_MAX_REPLICA]; -} SSyncCfg; - -typedef struct { - int32_t selfIndex; - uint32_t nodeId[TAOS_SYNC_MAX_REPLICA]; - int32_t role[TAOS_SYNC_MAX_REPLICA]; -} SNodesRole; - -// get the wal file from index or after -// return value, -1: error, 1:more wal files, 0:last WAL. if name[0]==0, no WAL file -typedef int32_t (*FGetWalInfo)(int32_t vgId, char *fileName, int64_t *fileId); - -// when a forward pkt is received, call this to handle data -typedef int32_t (*FWriteToCache)(int32_t vgId, void *pHead, int32_t qtype, void *pMsg); - -// when forward is confirmed by peer, master call this API to notify app -typedef void (*FConfirmForward)(int32_t vgId, void *mhandle, int32_t code); - -// when role is changed, call this to notify app -typedef void (*FNotifyRole)(int32_t vgId, int8_t role); - -// if a number of retrieving data failed, call this to start flow control -typedef void (*FNotifyFlowCtrl)(int32_t vgId, int32_t level); - -// when data file is synced successfully, notity app -typedef void (*FStartSyncFile)(int32_t vgId); -typedef void (*FStopSyncFile)(int32_t vgId, uint64_t fversion); - -// get file version -typedef int32_t (*FGetVersion)(int32_t vgId, uint64_t *fver, uint64_t *vver); - -typedef int32_t (*FSendFile)(void *tsdb, SOCKET socketFd); -typedef int32_t (*FRecvFile)(void *tsdb, SOCKET socketFd); - -typedef struct { - int32_t vgId; // vgroup ID - uint64_t version; // initial version - SSyncCfg syncCfg; // configuration from mgmt - char path[TSDB_FILENAME_LEN]; // path to the file - void * pTsdb; - FGetWalInfo getWalInfoFp; - FWriteToCache writeToCacheFp; - FConfirmForward confirmForward; - FNotifyRole notifyRoleFp; - FNotifyFlowCtrl notifyFlowCtrlFp; - FStartSyncFile startSyncFileFp; - FStopSyncFile stopSyncFileFp; - FGetVersion getVersionFp; - FSendFile sendFileFp; - FRecvFile recvFileFp; -} SSyncInfo; - -typedef void *tsync_h; - -int32_t syncInit(); -void syncCleanUp(); - -int64_t syncStart(const SSyncInfo *); -void syncStop(int64_t rid); -int32_t syncReconfig(int64_t rid, const SSyncCfg *); -int32_t syncForwardToPeer(int64_t rid, void *pHead, void *mhandle, int32_t qtype, bool force); -void syncConfirmForward(int64_t rid, uint64_t version, int32_t code, bool force); -void syncRecover(int64_t rid); // recover from other nodes: -int32_t syncGetNodesRole(int64_t rid, SNodesRole *); - -extern char *syncRole[]; - -//global configurable parameters -extern int32_t sDebugFlag; -extern char tsArbitrator[]; -extern uint16_t tsSyncPort; - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_SYNC_H diff --git a/tools/shell/src/backup/shellCheck.c b/tools/shell/src/backup/shellCheck.c deleted file mode 100644 index d1f0683fea..0000000000 --- a/tools/shell/src/backup/shellCheck.c +++ /dev/null @@ -1,202 +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 _GNU_SOURCE -#define _XOPEN_SOURCE -#define _DEFAULT_SOURCE - -#include "os.h" -#include "shell.h" -#include "shellCommand.h" -#include "tglobal.h" -#include "tutil.h" - -#define SHELL_SQL_LEN 1024 -static int32_t tbNum = 0; -static int32_t tbMallocNum = 0; -static char ** tbNames = NULL; -static int32_t checkedNum = 0; -static int32_t errorNum = 0; - -typedef struct { - TdThread threadID; - int threadIndex; - int totalThreads; - void * taos; - char * db; -} ShellThreadObj; - -static int32_t shellUseDb(TAOS *con, char *db) { - if (db == NULL) { - fprintf(stdout, "no dbname input\n"); - return -1; - } - - char sql[SHELL_SQL_LEN] = {0}; - snprintf(sql, SHELL_SQL_LEN, "use %s", db); - - TAOS_RES *pSql = taos_query(con, sql); - int32_t code = taos_errno(pSql); - if (code != 0) { - fprintf(stdout, "failed to execute sql:%s since %s", sql, taos_errstr(pSql)); - } - - taos_free_result(pSql); - return code; -} - -static int32_t shellShowTables(TAOS *con, char *db) { - char sql[SHELL_SQL_LEN] = {0}; - snprintf(sql, SHELL_SQL_LEN, "show %s.tables", db); - - TAOS_RES *pSql = taos_query(con, sql); - int32_t code = taos_errno(pSql); - - if (code != 0) { - fprintf(stdout, "failed to execute sql:%s since %s\n", sql, taos_errstr(pSql)); - } else { - TAOS_ROW row; - while ((row = taos_fetch_row(pSql))) { - int32_t tbIndex = tbNum++; - if (tbMallocNum < tbNum) { - tbMallocNum = (tbMallocNum * 2 + 1); - char** tbNames1 = taosMemoryRealloc(tbNames, tbMallocNum * sizeof(char *)); - if (tbNames1 == NULL) { - fprintf(stdout, "failed to malloc tablenames, num:%d\n", tbMallocNum); - code = TSDB_CODE_TSC_OUT_OF_MEMORY; - break; - } - tbNames = tbNames1; - } - - tbNames[tbIndex] = taosMemoryMalloc(TSDB_TABLE_NAME_LEN); - strncpy(tbNames[tbIndex], (const char *)row[0], TSDB_TABLE_NAME_LEN); - if (tbIndex % 100000 == 0 && tbIndex != 0) { - fprintf(stdout, "%d tablenames fetched\n", tbIndex); - } - } - } - - taos_free_result(pSql); - - fprintf(stdout, "total %d tablenames fetched, over\n", tbNum); - return code; -} - -static void shellFreeTbnames() { - for (int32_t i = 0; i < tbNum; ++i) { - taosMemoryFree(tbNames[i]); - } - taosMemoryFree(tbNames); -} - -static void *shellCheckThreadFp(void *arg) { - ShellThreadObj *pThread = (ShellThreadObj *)arg; - - setThreadName("shellCheckThrd"); - - int32_t interval = tbNum / pThread->totalThreads + 1; - int32_t start = pThread->threadIndex * interval; - int32_t end = (pThread->threadIndex + 1) * interval; - - if (end > tbNum) end = tbNum + 1; - - char file[32] = {0}; - snprintf(file, 32, "tb%d.txt", pThread->threadIndex); - - TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); - if (!fp) { - fprintf(stdout, "failed to open %s, reason:%s", file, strerror(errno)); - return NULL; - } - - char sql[SHELL_SQL_LEN]; - for (int32_t t = start; t < end; ++t) { - char *tbname = tbNames[t]; - if (tbname == NULL) break; - - snprintf(sql, SHELL_SQL_LEN, "select * from %s limit 1", tbname); - - TAOS_RES *pSql = taos_query(pThread->taos, sql); - int32_t code = taos_errno(pSql); - if (code != 0) { - int32_t len = snprintf(sql, SHELL_SQL_LEN, "drop table %s.%s;\n", pThread->db, tbname); - taosWriteFile(pFile, sql, len); - atomic_add_fetch_32(&errorNum, 1); - } - - int32_t cnum = atomic_add_fetch_32(&checkedNum, 1); - if (cnum % 5000 == 0 && cnum != 0) { - fprintf(stdout, "%d tables checked\n", cnum); - } - - taos_free_result(pSql); - } - - taosFsync(pFile); - taosCloseFile(&pFile); - - return NULL; -} - -static void shellRunCheckThreads(TAOS *con, SShellArguments *_args) { - TdThreadAttr thattr; - ShellThreadObj *threadObj = (ShellThreadObj *)taosMemoryCalloc(_args->threadNum, sizeof(ShellThreadObj)); - for (int t = 0; t < _args->threadNum; ++t) { - ShellThreadObj *pThread = threadObj + t; - pThread->threadIndex = t; - pThread->totalThreads = _args->threadNum; - pThread->taos = con; - pThread->db = _args->database; - - taosThreadAttrInit(&thattr); - taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - - if (taosThreadCreate(&(pThread->threadID), &thattr, shellCheckThreadFp, (void *)pThread) != 0) { - fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex); - exit(0); - } - } - - for (int t = 0; t < _args->threadNum; ++t) { - taosThreadJoin(threadObj[t].threadID, NULL); - } - - for (int t = 0; t < _args->threadNum; ++t) { - taos_close(threadObj[t].taos); - } - taosMemoryFree(threadObj); -} - -void shellCheck(TAOS *con, SShellArguments *_args) { - int64_t start = taosGetTimestampMs(); - - if (shellUseDb(con, _args->database) != 0) { - shellFreeTbnames(); - return; - } - - if (shellShowTables(con, _args->database) != 0) { - shellFreeTbnames(); - return; - } - - fprintf(stdout, "total %d tables will be checked by %d threads\n", tbNum, _args->threadNum); - shellRunCheckThreads(con, _args); - - int64_t end = taosGetTimestampMs(); - fprintf(stdout, "total %d tables checked, failed:%d, time spent %.2f seconds\n", checkedNum, errorNum, - (end - start) / 1000.0); -} diff --git a/tools/shell/src/backup/shellDarwin.c b/tools/shell/src/backup/shellDarwin.c deleted file mode 100644 index 93335776ba..0000000000 --- a/tools/shell/src/backup/shellDarwin.c +++ /dev/null @@ -1,498 +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 __USE_XOPEN - -#include "os.h" - -#include "shell.h" -#include "shellCommand.h" -#include "tbase64.h" - -#include "tscLog.h" - -#define OPT_ABORT 1 /* �Cabort */ - -int indicator = 1; -struct termios oldtio; - -void insertChar(Command *cmd, char *c, int size); - - -void printHelp() { - char indent[10] = " "; - printf("taos shell is used to test the TDengine database\n"); - - printf("%s%s\n", indent, "-h"); - printf("%s%s%s\n", indent, indent, "TDengine server IP address to connect. The default host is localhost."); - printf("%s%s\n", indent, "-p"); - printf("%s%s%s\n", indent, indent, "The password to use when connecting to the server."); - printf("%s%s\n", indent, "-P"); - printf("%s%s%s\n", indent, indent, "The TCP/IP port number to use for the connection"); - printf("%s%s\n", indent, "-u"); - printf("%s%s%s\n", indent, indent, "The user name to use when connecting to the server."); - printf("%s%s\n", indent, "-c"); - printf("%s%s%s\n", indent, indent, "Configuration directory."); - printf("%s%s\n", indent, "-s"); - printf("%s%s%s\n", indent, indent, "Commands to run without enter the shell."); - printf("%s%s\n", indent, "-r"); - printf("%s%s%s\n", indent, indent, "Output time as unsigned long.."); - printf("%s%s\n", indent, "-f"); - printf("%s%s%s\n", indent, indent, "Script to run without enter the shell."); - printf("%s%s\n", indent, "-d"); - printf("%s%s%s\n", indent, indent, "Database to use when connecting to the server."); - printf("%s%s\n", indent, "-t"); - printf("%s%s%s\n", indent, indent, "Time zone of the shell, default is local."); - printf("%s%s\n", indent, "-D"); - printf("%s%s%s\n", indent, indent, "Use multi-thread to import all SQL files in the directory separately."); - printf("%s%s\n", indent, "-T"); - printf("%s%s%s\n", indent, indent, "Number of threads when using multi-thread to import data."); - - exit(EXIT_SUCCESS); -} - -char DARWINCLIENT_VERSION[] = "Welcome to the TDengine shell from %s, Client Version:%s\n" - "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; -char g_password[SHELL_MAX_PASSWORD_LEN]; - -void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { - wordexp_t full_path; - for (int i = 1; i < argc; i++) { - // for host - if (strcmp(argv[i], "-h") == 0) { - if (i < argc - 1) { - arguments->host = argv[++i]; - } else { - fprintf(stderr, "option -h requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // for password - else if ((strncmp(argv[i], "-p", 2) == 0) - || (strncmp(argv[i], "--password", 10) == 0)) { - strcpy(tsOsName, "Darwin"); - printf(DARWINCLIENT_VERSION, tsOsName, taos_get_client_info()); - if ((strlen(argv[i]) == 2) - || (strncmp(argv[i], "--password", 10) == 0)) { - printf("Enter password: "); - taosSetConsoleEcho(false); - if (scanf("%s", g_password) > 1) { - fprintf(stderr, "password read error\n"); - } - taosSetConsoleEcho(true); - getchar(); - } else { - tstrncpy(g_password, (char *)(argv[i] + 2), SHELL_MAX_PASSWORD_LEN); - } - arguments->password = g_password; - arguments->is_use_passwd = true; - strcpy(argv[i], ""); - argc -= 1; - } - // for management port - else if (strcmp(argv[i], "-P") == 0) { - if (i < argc - 1) { - arguments->port = atoi(argv[++i]); - } else { - fprintf(stderr, "option -P requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // for user - else if (strcmp(argv[i], "-u") == 0) { - if (i < argc - 1) { - arguments->user = argv[++i]; - } else { - fprintf(stderr, "option -u requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-c") == 0) { - if (i < argc - 1) { - if (strlen(argv[++i]) >= TSDB_FILENAME_LEN) { - fprintf(stderr, "config file path: %s overflow max len %d\n", argv[i], TSDB_FILENAME_LEN - 1); - exit(EXIT_FAILURE); - } - strcpy(configDir, argv[i]); - } else { - fprintf(stderr, "Option -c requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-s") == 0) { - if (i < argc - 1) { - arguments->commands = argv[++i]; - } else { - fprintf(stderr, "option -s requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-r") == 0) { - arguments->is_raw_time = true; - } - // For temperory batch commands to run TODO - else if (strcmp(argv[i], "-f") == 0) { - if (i < argc - 1) { - strcpy(arguments->file, argv[++i]); - } else { - fprintf(stderr, "option -f requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // for default database - else if (strcmp(argv[i], "-d") == 0) { - if (i < argc - 1) { - arguments->database = argv[++i]; - } else { - fprintf(stderr, "option -d requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // For time zone - else if (strcmp(argv[i], "-t") == 0) { - if (i < argc - 1) { - arguments->timezone = argv[++i]; - } else { - fprintf(stderr, "option -t requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // For import directory - else if (strcmp(argv[i], "-D") == 0) { - if (i < argc - 1) { - if (wordexp(argv[++i], &full_path, 0) != 0) { - fprintf(stderr, "Invalid path %s\n", argv[i]); - exit(EXIT_FAILURE); - } - strcpy(arguments->dir, full_path.we_wordv[0]); - wordfree(&full_path); - } else { - fprintf(stderr, "option -D requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // For time zone - else if (strcmp(argv[i], "-T") == 0) { - if (i < argc - 1) { - arguments->threadNum = atoi(argv[++i]); - } else { - fprintf(stderr, "option -T requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // For temperory command TODO - else if (strcmp(argv[i], "--help") == 0) { - printHelp(); - exit(EXIT_FAILURE); - } else { - fprintf(stderr, "wrong options\n"); - printHelp(); - exit(EXIT_FAILURE); - } - } -} - -int32_t shellReadCommand(TAOS *con, char *command) { - unsigned hist_counter = history.hend; - char utf8_array[10] = "\0"; - Command cmd; - memset(&cmd, 0, sizeof(cmd)); - cmd.buffer = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - cmd.command = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - showOnScreen(&cmd); - - // Read input. - char c; - while (1) { - c = getchar(); - - if (c < 0) { // For UTF-8 - int count = countPrefixOnes(c); - utf8_array[0] = c; - for (int k = 1; k < count; k++) { - c = getchar(); - utf8_array[k] = c; - } - insertChar(&cmd, utf8_array, count); - } else if (c < '\033') { - // Ctrl keys. TODO: Implement ctrl combinations - switch (c) { - case 1: // ctrl A - positionCursorHome(&cmd); - break; - case 3: - printf("\n"); - resetCommand(&cmd, ""); - kill(0, SIGINT); - break; - case 4: // EOF or Ctrl+D - printf("\n"); - taos_close(con); - // write the history - write_history(); - exitShell(); - break; - case 5: // ctrl E - positionCursorEnd(&cmd); - break; - case 8: - backspaceChar(&cmd); - break; - case '\n': - case '\r': - printf("\n"); - if (isReadyGo(&cmd)) { - sprintf(command, "%s%s", cmd.buffer, cmd.command); - taosMemoryFreeClear(cmd.buffer); - taosMemoryFreeClear(cmd.command); - return 0; - } else { - updateBuffer(&cmd); - } - break; - case 11: // Ctrl + K; - clearLineAfter(&cmd); - break; - case 12: // Ctrl + L; - system("clear"); - showOnScreen(&cmd); - break; - case 21: // Ctrl + U - clearLineBefore(&cmd); - break; - } - } else if (c == '\033') { - c = getchar(); - switch (c) { - case '[': - c = getchar(); - switch (c) { - case 'A': // Up arrow - if (hist_counter != history.hstart) { - hist_counter = (hist_counter + MAX_HISTORY_SIZE - 1) % MAX_HISTORY_SIZE; - resetCommand(&cmd, (history.hist[hist_counter] == NULL) ? "" : history.hist[hist_counter]); - } - break; - case 'B': // Down arrow - if (hist_counter != history.hend) { - int next_hist = (hist_counter + 1) % MAX_HISTORY_SIZE; - - if (next_hist != history.hend) { - resetCommand(&cmd, (history.hist[next_hist] == NULL) ? "" : history.hist[next_hist]); - } else { - resetCommand(&cmd, ""); - } - hist_counter = next_hist; - } - break; - case 'C': // Right arrow - moveCursorRight(&cmd); - break; - case 'D': // Left arrow - moveCursorLeft(&cmd); - break; - case '1': - if ((c = getchar()) == '~') { - // Home key - positionCursorHome(&cmd); - } - break; - case '2': - if ((c = getchar()) == '~') { - // Insert key - } - break; - case '3': - if ((c = getchar()) == '~') { - // Delete key - deleteChar(&cmd); - } - break; - case '4': - if ((c = getchar()) == '~') { - // End key - positionCursorEnd(&cmd); - } - break; - case '5': - if ((c = getchar()) == '~') { - // Page up key - } - break; - case '6': - if ((c = getchar()) == '~') { - // Page down key - } - break; - case 72: - // Home key - positionCursorHome(&cmd); - break; - case 70: - // End key - positionCursorEnd(&cmd); - break; - } - break; - } - } else if (c == 0x7f) { - // press delete key - backspaceChar(&cmd); - } else { - insertChar(&cmd, &c, 1); - } - } - - return 0; -} - -void *shellLoopQuery(void *arg) { - if (indicator) { - getOldTerminalMode(); - indicator = 0; - } - - TAOS *con = (TAOS *)arg; - - setThreadName("shellLoopQuery"); - - taosThreadCleanupPush(cleanup_handler, NULL); - - char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); - if (command == NULL){ - tscError("failed to malloc command"); - return NULL; - } - - int32_t err = 0; - - do { - // Read command from shell. - memset(command, 0, MAX_COMMAND_SIZE); - setTerminalMode(); - err = shellReadCommand(con, command); - if (err) { - break; - } - resetTerminalMode(); - } while (shellRunCommand(con, command) == 0); - - taosMemoryFreeClear(command); - exitShell(); - - taosThreadCleanupPop(1); - - return NULL; -} - -void get_history_path(char *history) { sprintf(history, "%s/%s", getpwuid(getuid())->pw_dir, HISTORY_FILE); } - -void clearScreen(int ecmd_pos, int cursor_pos) { - 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; - } - - int cursor_x = cursor_pos / w.ws_col; - int cursor_y = cursor_pos % w.ws_col; - int command_x = ecmd_pos / w.ws_col; - positionCursor(cursor_y, LEFT); - positionCursor(command_x - cursor_x, DOWN); - fprintf(stdout, "\033[2K"); - for (int i = 0; i < command_x; i++) { - positionCursor(1, UP); - fprintf(stdout, "\033[2K"); - } - fflush(stdout); -} - -void showOnScreen(Command *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; - } - - TdWchar wc; - int size = 0; - - // Print out the command. - char *total_string = taosMemoryMalloc(MAX_COMMAND_SIZE); - memset(total_string, '\0', MAX_COMMAND_SIZE); - if (strcmp(cmd->buffer, "") == 0) { - sprintf(total_string, "%s%s", PROMPT_HEADER, cmd->command); - } else { - sprintf(total_string, "%s%s", CONTINUE_PROMPT, cmd->command); - } - - int remain_column = w.ws_col; - /* size = cmd->commandSize + prompt_size; */ - for (char *str = total_string; size < cmd->commandSize + prompt_size;) { - int ret = taosMbToWchar(&wc, str, MB_CUR_MAX); - if (ret < 0) break; - size += ret; - /* assert(size >= 0); */ - int width = taosWcharWidth(wc); - if (remain_column > width) { - printf("%lc", wc); - remain_column -= width; - } else { - if (remain_column == width) { - printf("%lc\n\r", wc); - remain_column = w.ws_col; - } else { - printf("\n\r%lc", wc); - remain_column = w.ws_col - width; - } - } - - str = total_string + size; - } - - taosMemoryFree(total_string); - /* for (int i = 0; i < size; i++){ */ - /* char c = total_string[i]; */ - /* if (k % w.ws_col == 0) { */ - /* printf("%c\n\r", c); */ - /* } */ - /* else { */ - /* printf("%c", c); */ - /* } */ - /* k += 1; */ - /* } */ - - // Position the cursor - int cursor_pos = cmd->screenOffset + prompt_size; - int ecmd_pos = cmd->endOffset + prompt_size; - - int cursor_x = cursor_pos / w.ws_col; - int cursor_y = cursor_pos % w.ws_col; - // int cursor_y = cursor % w.ws_col; - int command_x = ecmd_pos / w.ws_col; - int command_y = ecmd_pos % w.ws_col; - // int command_y = (command.size() + prompt_size) % w.ws_col; - positionCursor(command_y, LEFT); - positionCursor(command_x, UP); - positionCursor(cursor_x, DOWN); - positionCursor(cursor_y, RIGHT); - fflush(stdout); -} - -void cleanup_handler(void *arg) { resetTerminalMode(); } - -void exitShell() { - resetTerminalMode(); - exit(EXIT_SUCCESS); -} diff --git a/tools/shell/src/backup/shellImport.c b/tools/shell/src/backup/shellImport.c deleted file mode 100644 index 130c72a20b..0000000000 --- a/tools/shell/src/backup/shellImport.c +++ /dev/null @@ -1,279 +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 _GNU_SOURCE -#define _XOPEN_SOURCE -#define _DEFAULT_SOURCE - -#include "os.h" -#include "shell.h" -#include "shellCommand.h" -#include "tglobal.h" -#include "tutil.h" - -static char **shellSQLFiles = NULL; -static int32_t shellSQLFileNum = 0; -static char shellTablesSQLFile[TSDB_FILENAME_LEN] = {0}; - -typedef struct { - TdThread threadID; - int threadIndex; - int totalThreads; - void *taos; -} ShellThreadObj; - -static int shellGetFilesNum(const char *directoryName, const char *prefix) -{ - char cmd[1024] = { 0 }; - sprintf(cmd, "ls %s/*.%s | wc -l ", directoryName, prefix); - - char buf[1024] = { 0 }; - if (taosSystem(cmd, buf, sizeof(buf)) < 0) { - fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); - exit(0); - } - - int fileNum = 0; - if (sscanf(buf, "%d", &fileNum) != 1) { - fprintf(stderr, "ERROR: failed to execute:%s, parse result error\n", cmd); - exit(0); - } - - if (fileNum <= 0) { - fprintf(stderr, "ERROR: directory:%s is empry\n", directoryName); - exit(0); - } - - return fileNum; -} - -static void shellParseDirectory(const char *directoryName, const char *prefix, char **fileArray, int totalFiles) -{ - char cmd[1024] = { 0 }; - sprintf(cmd, "ls %s/*.%s | sort", directoryName, prefix); - - char buf[1024] = { 0 }; - if (taosSystem(cmd, buf, sizeof(buf)) < 0) { - fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); - exit(0); - } - - int fileNum = 0; - while (sscanf(buf, "%128s", fileArray[fileNum++])) { - if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) { - fileNum--; - } - if (fileNum >= totalFiles) { - break; - } - } - - if (fileNum != totalFiles) { - fprintf(stderr, "ERROR: directory:%s changed while read\n", directoryName); - exit(0); - } -} - -static void shellCheckTablesSQLFile(const char *directoryName) -{ - sprintf(shellTablesSQLFile, "%s/tables.sql", directoryName); - - if (taosFStatFile(shellTablesSQLFile, NULL, NULL) < 0) { - shellTablesSQLFile[0] = 0; - } -} - -static void shellMallocSQLFiles() -{ - shellSQLFiles = (char**)taosMemoryCalloc(shellSQLFileNum, sizeof(char*)); - for (int i = 0; i < shellSQLFileNum; i++) { - shellSQLFiles[i] = taosMemoryCalloc(1, TSDB_FILENAME_LEN); - } -} - -static void shellGetDirectoryFileList(char *inputDir) -{ - if (!taosDirExist(inputDir)) { - fprintf(stderr, "ERROR: %s not exist\n", inputDir); - exit(0); - } - - if (taosIsDir(inputDir)) { - shellCheckTablesSQLFile(inputDir); - shellSQLFileNum = shellGetFilesNum(inputDir, "sql"); - int totalSQLFileNum = shellSQLFileNum; - if (shellTablesSQLFile[0] != 0) { - shellSQLFileNum--; - } - shellMallocSQLFiles(); - shellParseDirectory(inputDir, "sql", shellSQLFiles, shellSQLFileNum); - fprintf(stdout, "\nstart to dispose %d files in %s\n", totalSQLFileNum, inputDir); - } - else { - fprintf(stderr, "ERROR: %s is not a directory\n", inputDir); - exit(0); - } -} - -static void shellSourceFile(TAOS *con, char *fptr) { - wordexp_t full_path; - int read_len = 0; - char * cmd = taosMemoryMalloc(tsMaxSQLStringLen); - size_t cmd_len = 0; - char * line = NULL; - - if (wordexp(fptr, &full_path, 0) != 0) { - fprintf(stderr, "ERROR: illegal file name\n"); - taosMemoryFree(cmd); - return; - } - - char *fname = full_path.we_wordv[0]; - if (fname == NULL) { - fprintf(stderr, "ERROR: invalid filename\n"); - taosMemoryFree(cmd); - return; - } - - /* - if (access(fname, F_OK) != 0) { - fprintf(stderr, "ERROR: file %s is not exist\n", fptr); - - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - - if (access(fname, R_OK) != 0) { - fprintf(stderr, "ERROR: file %s is not readable\n", fptr); - - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - */ - - // FILE *f = fopen(fname, "r"); - TdFilePtr pFile = taosOpenFile(fname, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - fprintf(stderr, "ERROR: failed to open file %s\n", fname); - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - - fprintf(stdout, "begin import file:%s\n", fname); - - int lineNo = 0; - while ((read_len = taosGetLineFile(pFile, &line)) != -1) { - ++lineNo; - if (read_len >= tsMaxSQLStringLen) continue; - line[--read_len] = '\0'; - - if (read_len == 0 || isCommentLine(line)) { // line starts with # - continue; - } - - if (line[read_len - 1] == '\\') { - line[read_len - 1] = ' '; - memcpy(cmd + cmd_len, line, read_len); - cmd_len += read_len; - continue; - } - - memcpy(cmd + cmd_len, line, read_len); - - TAOS_RES* pSql = taos_query(con, cmd); - int32_t code = taos_errno(pSql); - - if (code != 0) { - fprintf(stderr, "DB error: %s: %s (%d)\n", taos_errstr(pSql), fname, lineNo); - } - - /* free local resouce: allocated memory/metric-meta refcnt */ - taos_free_result(pSql); - - memset(cmd, 0, MAX_COMMAND_SIZE); - cmd_len = 0; - } - - taosMemoryFree(cmd); - if(line != NULL) taosMemoryFree(line); - wordfree(&full_path); - taosCloseFile(&pFile); -} - -void* shellImportThreadFp(void *arg) -{ - ShellThreadObj *pThread = (ShellThreadObj*)arg; - setThreadName("shellImportThrd"); - - for (int f = 0; f < shellSQLFileNum; ++f) { - if (f % pThread->totalThreads == pThread->threadIndex) { - char *SQLFileName = shellSQLFiles[f]; - shellSourceFile(pThread->taos, SQLFileName); - } - } - - return NULL; -} - -static void shellRunImportThreads(SShellArguments* _args) -{ - TdThreadAttr thattr; - ShellThreadObj *threadObj = (ShellThreadObj *)taosMemoryCalloc(_args->threadNum, sizeof(ShellThreadObj)); - for (int t = 0; t < _args->threadNum; ++t) { - ShellThreadObj *pThread = threadObj + t; - pThread->threadIndex = t; - pThread->totalThreads = _args->threadNum; - pThread->taos = taos_connect(_args->host, _args->user, _args->password, _args->database, tsDnodeShellPort); - if (pThread->taos == NULL) { - fprintf(stderr, "ERROR: thread:%d failed connect to TDengine, error:%s\n", pThread->threadIndex, "null taos"/*taos_errstr(pThread->taos)*/); - exit(0); - } - - taosThreadAttrInit(&thattr); - taosThreadAttrSetDetachState(&thattr, PTHREAD_CREATE_JOINABLE); - - if (taosThreadCreate(&(pThread->threadID), &thattr, shellImportThreadFp, (void*)pThread) != 0) { - fprintf(stderr, "ERROR: thread:%d failed to start\n", pThread->threadIndex); - exit(0); - } - } - - for (int t = 0; t < _args->threadNum; ++t) { - taosThreadJoin(threadObj[t].threadID, NULL); - } - - for (int t = 0; t < _args->threadNum; ++t) { - taos_close(threadObj[t].taos); - } - taosMemoryFree(threadObj); -} - -void source_dir(TAOS* con, SShellArguments* _args) { - shellGetDirectoryFileList(_args->dir); - int64_t start = taosGetTimestampMs(); - - if (shellTablesSQLFile[0] != 0) { - shellSourceFile(con, shellTablesSQLFile); - int64_t end = taosGetTimestampMs(); - fprintf(stdout, "import %s finished, time spent %.2f seconds\n", shellTablesSQLFile, (end - start) / 1000.0); - } - - shellRunImportThreads(_args); - int64_t end = taosGetTimestampMs(); - fprintf(stdout, "import %s finished, time spent %.2f seconds\n", _args->dir, (end - start) / 1000.0); -} diff --git a/tools/shell/src/backup/shellWindows.c b/tools/shell/src/backup/shellWindows.c deleted file mode 100644 index 92ac7fd721..0000000000 --- a/tools/shell/src/backup/shellWindows.c +++ /dev/null @@ -1,325 +0,0 @@ -/******************************************************************* -* Copyright (c) 2017 by TAOS Technologies, Inc. -* All rights reserved. -* -* This file is proprietary and confidential to TAOS Technologies. -* No part of this file may be reproduced, stored, transmitted, -* disclosed or used in any form or by any means other than as -* expressly provided by the written permission from Jianhui Tao -* -* ****************************************************************/ - -#include -#include -#include -#include "../../../../include/client/taos.h" -#include "os.h" -#include "shell.h" -#include "shellCommand.h" - -extern char configDir[]; - -char WINCLIENT_VERSION[] = "Welcome to the TDengine shell from %s, Client Version:%s\n" - "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; - -void printVersion() { - printf("version: %s\n", version); -} - -void printHelp() { - char indent[10] = " "; - printf("taos shell is used to test the TDengine database\n"); - - printf("%s%s\n", indent, "-h"); - printf("%s%s%s\n", indent, indent, "TDengine server FQDN to connect. The default host is localhost."); - printf("%s%s\n", indent, "-p"); - printf("%s%s%s\n", indent, indent, "The password to use when connecting to the server."); - printf("%s%s\n", indent, "-P"); - printf("%s%s%s\n", indent, indent, "The TCP/IP port number to use for the connection"); - printf("%s%s\n", indent, "-u"); - printf("%s%s%s\n", indent, indent, "The user name to use when connecting to the server."); - printf("%s%s\n", indent, "-A"); - printf("%s%s%s\n", indent, indent, "The user auth to use when connecting to the server."); - printf("%s%s\n", indent, "-c"); - printf("%s%s%s\n", indent, indent, "Configuration directory."); - printf("%s%s\n", indent, "-C"); - printf("%s%s%s\n", indent, indent, "Dump configuration."); - printf("%s%s\n", indent, "-s"); - printf("%s%s%s\n", indent, indent, "Commands to run without enter the shell."); - printf("%s%s\n", indent, "-r"); - printf("%s%s%s\n", indent, indent, "Output time as unsigned long.."); - printf("%s%s\n", indent, "-f"); - printf("%s%s%s\n", indent, indent, "Script to run without enter the shell."); - printf("%s%s\n", indent, "-d"); - printf("%s%s%s\n", indent, indent, "Database to use when connecting to the server."); - printf("%s%s\n", indent, "-t"); - printf("%s%s%s\n", indent, indent, "Time zone of the shell, default is local."); - printf("%s%s\n", indent, "-n"); - printf("%s%s%s\n", indent, indent, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup|sync|speed|fqdn."); - printf("%s%s\n", indent, "-l"); - printf("%s%s%s\n", indent, indent, "Packet length used for net test, default is 1000 bytes."); - printf("%s%s\n", indent, "-N"); - printf("%s%s%s\n", indent, indent, "Packet numbers used for net test, default is 100."); - printf("%s%s\n", indent, "-S"); - printf("%s%s%s\n", indent, indent, "Packet type used for net test, default is TCP."); - printf("%s%s\n", indent, "-V"); - printf("%s%s%s\n", indent, indent, "Print program version."); - - exit(EXIT_SUCCESS); -} - -char g_password[SHELL_MAX_PASSWORD_LEN]; - -void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { - for (int i = 1; i < argc; i++) { - // for host - if (strcmp(argv[i], "-h") == 0) { - if (i < argc - 1) { - arguments->host = argv[++i]; - } else { - fprintf(stderr, "option -h requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // for password - else if ((strncmp(argv[i], "-p", 2) == 0) - || (strncmp(argv[i], "--password", 10) == 0)) { - arguments->is_use_passwd = true; - strcpy(tsOsName, "Windows"); - printf(WINCLIENT_VERSION, tsOsName, taos_get_client_info()); - if ((strlen(argv[i]) == 2) - || (strncmp(argv[i], "--password", 10) == 0)) { - printf("Enter password: "); - taosSetConsoleEcho(false); - if (scanf("%s", g_password) > 1) { - fprintf(stderr, "password read error!\n"); - } - taosSetConsoleEcho(true); - getchar(); - } else { - tstrncpy(g_password, (char *)(argv[i] + 2), SHELL_MAX_PASSWORD_LEN); - } - arguments->password = g_password; - strcpy(argv[i], ""); - argc -= 1; - } - // for management port - else if (strcmp(argv[i], "-P") == 0) { - if (i < argc - 1) { - arguments->port = atoi(argv[++i]); - } else { - fprintf(stderr, "option -P requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // for user - else if (strcmp(argv[i], "-u") == 0) { - if (i < argc - 1) { - arguments->user = argv[++i]; - } else { - fprintf(stderr, "option -u requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-A") == 0) { - if (i < argc - 1) { - arguments->auth = argv[++i]; - } else { - fprintf(stderr, "option -A requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-c") == 0) { - if (i < argc - 1) { - char *tmp = argv[++i]; - if (strlen(tmp) >= TSDB_FILENAME_LEN) { - fprintf(stderr, "config file path: %s overflow max len %d\n", tmp, TSDB_FILENAME_LEN - 1); - exit(EXIT_FAILURE); - } - strcpy(configDir, tmp); - } else { - fprintf(stderr, "Option -c requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-C") == 0) { - arguments->dump_config = true; - } else if (strcmp(argv[i], "-s") == 0) { - if (i < argc - 1) { - arguments->commands = argv[++i]; - } else { - fprintf(stderr, "option -s requires an argument\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(argv[i], "-r") == 0) { - arguments->is_raw_time = true; - } - // For temperory batch commands to run TODO - else if (strcmp(argv[i], "-f") == 0) { - if (i < argc - 1) { - strcpy(arguments->file, argv[++i]); - } else { - fprintf(stderr, "option -f requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // for default database - else if (strcmp(argv[i], "-d") == 0) { - if (i < argc - 1) { - arguments->database = argv[++i]; - } else { - fprintf(stderr, "option -d requires an argument\n"); - exit(EXIT_FAILURE); - } - } - // For time zone - else if (strcmp(argv[i], "-t") == 0) { - if (i < argc - 1) { - arguments->timezone = argv[++i]; - } else { - fprintf(stderr, "option -t requires an argument\n"); - exit(EXIT_FAILURE); - } - } - else if (strcmp(argv[i], "-n") == 0) { - if (i < argc - 1) { - arguments->netTestRole = argv[++i]; - } else { - fprintf(stderr, "option -n requires an argument\n"); - exit(EXIT_FAILURE); - } - } - else if (strcmp(argv[i], "-l") == 0) { - if (i < argc - 1) { - arguments->pktLen = atoi(argv[++i]); - } else { - fprintf(stderr, "option -l requires an argument\n"); - exit(EXIT_FAILURE); - } - } - else if (strcmp(argv[i], "-N") == 0) { - if (i < argc - 1) { - arguments->pktNum = atoi(argv[++i]); - } else { - fprintf(stderr, "option -N requires an argument\n"); - exit(EXIT_FAILURE); - } - } - else if (strcmp(argv[i], "-S") == 0) { - if (i < argc - 1) { - arguments->pktType = argv[++i]; - } else { - fprintf(stderr, "option -S requires an argument\n"); - exit(EXIT_FAILURE); - } - } - else if (strcmp(argv[i], "-V") == 0) { - printVersion(); - exit(EXIT_SUCCESS); - } - // For temperory command TODO - else if (strcmp(argv[i], "--help") == 0) { - printHelp(); - exit(EXIT_SUCCESS); - } else { - fprintf(stderr, "wrong options\n"); - printHelp(); - exit(EXIT_FAILURE); - } - } -} - -void shellPrintContinuePrompt() { printf("%s", CONTINUE_PROMPT); } - -void shellPrintPrompt() { printf("%s", PROMPT_HEADER); } - -void updateBuffer(Command *cmd) { - if (regex_match(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); - strcat(cmd->buffer, cmd->command); - - memset(cmd->command, 0, MAX_COMMAND_SIZE); - cmd->cursorOffset = 0; -} - -int isReadyGo(Command *cmd) { - char *total = taosMemoryMalloc(MAX_COMMAND_SIZE); - memset(total, 0, 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 (regex_match(total, reg_str, REG_EXTENDED | REG_ICASE)) { - taosMemoryFree(total); - return 1; - } - - taosMemoryFree(total); - return 0; -} - -void insertChar(Command *cmd, char c) { - // TODO: Check if the length enough. - if (cmd->cursorOffset >= MAX_COMMAND_SIZE) { - fprintf(stdout, "sql is larger than %d bytes", MAX_COMMAND_SIZE); - return; - } - - cmd->command[cmd->cursorOffset++] = c; -} - -int32_t shellReadCommand(TAOS *con, char command[]) { - Command cmd; - memset(&cmd, 0, sizeof(cmd)); - cmd.buffer = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - cmd.command = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - - // Read input. - char c; - while (1) { - c = getchar(); - - switch (c) { - case '\n': - case '\r': - if (isReadyGo(&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(); - updateBuffer(&cmd); - } - break; - default: - insertChar(&cmd, c); - } - } - - return 0; -} - -void *shellLoopQuery(void *arg) { - TAOS *con = (TAOS *)arg; - char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); - if (command == NULL) return NULL; - - int32_t err = 0; - - do { - memset(command, 0, MAX_COMMAND_SIZE); - shellPrintPrompt(); - - // Read command from shell. - err = shellReadCommand(con, command); - if (err) { - break; - } - } while (shellRunCommand(con, command) == 0); - - return NULL; -} - -void get_history_path(char *history) { sprintf(history, "C:/TDengine/%s", HISTORY_FILE); } - -void exitShell() { exit(EXIT_SUCCESS); } diff --git a/tools/shell/src/shellArguments.c b/tools/shell/src/shellArguments.c new file mode 100644 index 0000000000..5391d28277 --- /dev/null +++ b/tools/shell/src/shellArguments.c @@ -0,0 +1,354 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "shellInt.h" + +#define SHELL_HOST "The auth string to use when connecting to the server." +#define SHELL_PORT "The TCP/IP port number to use for the connection." +#define SHELL_USER "The user name to use when connecting to the server." +#define SHELL_PASSWORD "The password to use when connecting to the server." +#define SHELL_AUTH "The auth string to use when connecting to the server." +#define SHELL_GEN_AUTH "Generate auth string from password." +#define SHELL_CFG_DIR "Configuration directory." +#define SHELL_DMP_CFG "Dump configuration." +#define SHELL_CMD "Commands to run without enter the shell." +#define SHELL_RAW_TIME "Output time as uint64_t." +#define SHELL_FILE "Script to run without enter the shell." +#define SHELL_DB "Database to use when connecting to the server." +#define SHELL_CHECK "Check the service status." +#define SHELL_STARTUP "Check the details of the service status." +#define SHELL_WIDTH "Set the default binary display width, default is 30." +#define SHELL_NET_ROLE "Net role when network connectivity test, options: client|server." +#define SHELL_PKG_LEN "Packet length used for net test, default is 1024 bytes." +#define SHELL_PKT_NUM "Packet numbers used for net test, default is 100." +#define SHELL_VERSION "Print program version." +#define SHELL_EMAIL "" + +void shellPrintHelp() { + char indent[] = " "; + printf("Usage: taos [OPTION...] \n\n"); + printf("%s%s%s%s\n", indent, "-a,", indent, SHELL_AUTH); + printf("%s%s%s%s\n", indent, "-A,", indent, SHELL_GEN_AUTH); + printf("%s%s%s%s\n", indent, "-c,", indent, SHELL_CFG_DIR); + printf("%s%s%s%s\n", indent, "-C,", indent, SHELL_DMP_CFG); + printf("%s%s%s%s\n", indent, "-d,", indent, SHELL_DB); + printf("%s%s%s%s\n", indent, "-f,", indent, SHELL_FILE); + printf("%s%s%s%s\n", indent, "-h,", indent, SHELL_HOST); + printf("%s%s%s%s\n", indent, "-k,", indent, SHELL_CHECK); + printf("%s%s%s%s\n", indent, "-l,", indent, SHELL_PKG_LEN); + printf("%s%s%s%s\n", indent, "-n,", indent, SHELL_NET_ROLE); + printf("%s%s%s%s\n", indent, "-N,", indent, SHELL_PKT_NUM); + printf("%s%s%s%s\n", indent, "-p,", indent, SHELL_PASSWORD); + printf("%s%s%s%s\n", indent, "-P,", indent, SHELL_PORT); + printf("%s%s%s%s\n", indent, "-r,", indent, SHELL_RAW_TIME); + printf("%s%s%s%s\n", indent, "-s,", indent, SHELL_CMD); + printf("%s%s%s%s\n", indent, "-t,", indent, SHELL_STARTUP); + printf("%s%s%s%s\n", indent, "-u,", indent, SHELL_USER); + printf("%s%s%s%s\n", indent, "-w,", indent, SHELL_WIDTH); + printf("%s%s%s%s\n", indent, "-V,", indent, SHELL_VERSION); + printf("\n\nReport bugs to %s.\n", SHELL_EMAIL); +} + +static int32_t shellParseSingleOpt(int32_t key, char *arg) { + SShellArgs *pArgs = &shell.args; + + switch (key) { + case 'h': + pArgs->host = arg; + break; + case 'P': + pArgs->port = atoi(arg); + break; + case 'u': + pArgs->user = arg; + break; + case 'p': + break; + case 'a': + pArgs->auth = arg; + break; + case 'A': + pArgs->is_gen_auth = true; + break; + case 'c': + pArgs->cfgdir = arg; + break; + case 'C': + pArgs->is_dump_config = true; + break; + case 's': + pArgs->commands = arg; + break; + case 'r': + pArgs->is_raw_time = true; + break; + case 'f': + tstrncpy(pArgs->file, arg, sizeof(pArgs->file)); + break; + case 'd': + pArgs->database = arg; + break; + case 'k': + pArgs->is_check = true; + break; + case 't': + pArgs->is_startup = true; + break; + case 'w': + pArgs->displayWidth = atoi(arg); + break; + case 'n': + pArgs->netrole = arg; + break; + case 'l': + pArgs->pktLen = atoi(arg); + break; + case 'N': + pArgs->pktNum = atoi(arg); + break; + case 'V': + pArgs->is_version = true; + break; + case '?': + pArgs->is_help = true; + break; + case 1: + pArgs->abort = 1; + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) { + SShellArgs *pArgs = &shell.args; + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 || strcmp(argv[i], "-?") == 0) { + shellParseSingleOpt('?', NULL); + return 0; + } + + char *key = argv[i]; + int32_t keyLen = strlen(key); + if (keyLen != 2) { + fprintf(stderr, "invalid option %s\n", key); + return -1; + } + if (key[0] != '-') { + fprintf(stderr, "invalid option %s\n", key); + return -1; + } + + if (key[1] == 'h' || key[1] == 'P' || key[1] == 'u' || key[1] == 'a' || key[1] == 'c' || key[1] == 's' || + key[1] == 'f' || key[1] == 'd' || key[1] == 'w' || key[1] == 'n' || key[1] == 'l' || key[1] == 'N') { + if (i + 1 >= argc) { + fprintf(stderr, "option %s requires an argument\n", key); + return -1; + } + char *val = argv[i + 1]; + if (val[0] == '-') { + fprintf(stderr, "option %s requires an argument\n", key); + return -1; + } + shellParseSingleOpt(key[1], val); + i++; + } else if (key[1] == 'p' || key[1] == 'A' || key[1] == 'c' || key[1] == 'r' || key[1] == 'k' || key[1] == 't' || + key[1] == 'V') { + shellParseSingleOpt(key[1], NULL); + } else { + fprintf(stderr, "invalid option %s\n", key); + return -1; + } + } + + return 0; +} + +#ifdef LINUX +#include +#include + +const char *argp_program_version = version; +const char *argp_program_bug_address = SHELL_EMAIL; + +static struct argp_option shellOptions[] = { + {"host", 'h', "HOST", 0, SHELL_HOST}, + {"port", 'P', "PORT", 0, SHELL_PORT}, + {"user", 'u', "USER", 0, SHELL_USER}, + {0, 'p', 0, 0, SHELL_PASSWORD}, + {"auth", 'a', "AUTH", 0, SHELL_AUTH}, + {"generate-auth", 'A', 0, 0, SHELL_GEN_AUTH}, + {"config-dir", 'c', "DIR", 0, SHELL_CFG_DIR}, + {"dump-config", 'C', 0, 0, SHELL_DMP_CFG}, + {"commands", 's', "COMMANDS", 0, SHELL_CMD}, + {"raw-time", 'r', 0, 0, SHELL_RAW_TIME}, + {"file", 'f', "FILE", 0, SHELL_FILE}, + {"database", 'd', "DATABASE", 0, SHELL_DB}, + {"check", 'k', 0, 0, SHELL_CHECK}, + {"startup", 't', 0, 0, SHELL_STARTUP}, + {"display-width", 'w', "WIDTH", 0, SHELL_WIDTH}, + {"netrole", 'n', "NETROLE", 0, SHELL_NET_ROLE}, + {"pktlen", 'l', "PKTLEN", 0, SHELL_PKG_LEN}, + {"pktnum", 'N', "PKTNUM", 0, SHELL_PKT_NUM}, + {0}, +}; + +static error_t shellParseOpt(int32_t key, char *arg, struct argp_state *state) { return shellParseSingleOpt(key, arg); } + +static struct argp shellArgp = {shellOptions, shellParseOpt, "", ""}; + +static void shellParseArgsUseArgp(int argc, char *argv[]) { + argp_program_version = shell.info.programVersion; + argp_parse(&shellArgp, argc, argv, 0, 0, &shell.args); +} + +#endif + +static void shellInitArgs(int argc, char *argv[]) { + for (int i = 1; i < argc; i++) { + if (strncmp(argv[i], "-p", 2) == 0) { + printf(shell.info.clientVersion, tsOsName, taos_get_client_info()); + if (strlen(argv[i]) == 2) { + printf("Enter password: "); + taosSetConsoleEcho(false); + if (scanf("%20s", shell.args.password) > 1) { + fprintf(stderr, "password reading error\n"); + } + taosSetConsoleEcho(true); + if (EOF == getchar()) { + fprintf(stderr, "getchar() return EOF\n"); + } + } else { + tstrncpy(shell.args.password, (char *)(argv[i] + 2), sizeof(shell.args.password)); + strcpy(argv[i], "-p"); + } + } + } + if (strlen(shell.args.password) == 0) { + tstrncpy(shell.args.password, TSDB_DEFAULT_PASS, sizeof(shell.args.password)); + } + + SShellArgs *pArgs = &shell.args; + pArgs->user = TSDB_DEFAULT_USER; + pArgs->pktLen = SHELL_DEF_PKG_LEN; + pArgs->pktNum = SHELL_DEF_PKG_NUM; + pArgs->displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH; +} + +static int32_t shellCheckArgs() { + SShellArgs *pArgs = &shell.args; + if (pArgs->host != NULL && (strlen(pArgs->host) <= 0 || strlen(pArgs->host) > TSDB_FQDN_LEN)) { + printf("Invalid host:%s\n", pArgs->host); + return -1; + } + + if (pArgs->user != NULL && (strlen(pArgs->user) <= 0 || strlen(pArgs->user) > TSDB_USER_LEN)) { + printf("Invalid user:%s\n", pArgs->user); + return -1; + } + + if (pArgs->auth != NULL && (strlen(pArgs->auth) <= 0 || strlen(pArgs->auth) > TSDB_PASSWORD_LEN)) { + printf("Invalid auth:%s\n", pArgs->auth); + return -1; + } + + if (pArgs->database != NULL && (strlen(pArgs->database) <= 0 || strlen(pArgs->database) > TSDB_DB_NAME_LEN)) { + printf("Invalid database:%s\n", pArgs->database); + return -1; + } + + if (pArgs->file[0] != 0) { + char fullname[PATH_MAX] = {0}; + if (taosExpandDir(pArgs->file, fullname, PATH_MAX) == 0) { + tstrncpy(pArgs->file, fullname, PATH_MAX); + } + } + + if (pArgs->cfgdir != NULL) { + if (strlen(pArgs->cfgdir) <= 0 || strlen(pArgs->cfgdir) >= PATH_MAX) { + printf("Invalid cfgdir:%s\n", pArgs->cfgdir); + return -1; + } else { + if (taosExpandDir(pArgs->cfgdir, configDir, PATH_MAX) != 0) { + tstrncpy(configDir, pArgs->cfgdir, PATH_MAX); + } + } + } + + if (pArgs->commands != NULL && (strlen(pArgs->commands) <= 0)) { + printf("Invalid commands:%s\n", pArgs->commands); + return -1; + } + + if (pArgs->netrole != NULL && !(strcmp(pArgs->netrole, "client") == 0 || strcmp(pArgs->netrole, "server") == 0)) { + printf("Invalid netrole:%s\n", pArgs->netrole); + return -1; + } + + if (pArgs->password != NULL && (strlen(pArgs->password) <= 0)) { + printf("Invalid password\n"); + return -1; + } + + if (pArgs->pktLen < SHELL_MIN_PKG_LEN || pArgs->pktLen > SHELL_MAX_PKG_LEN) { + printf("Invalid pktLen:%d, range:[%d, %d]\n", pArgs->pktLen, SHELL_MIN_PKG_LEN, SHELL_MAX_PKG_LEN); + return -1; + } + + if (pArgs->pktNum < SHELL_MIN_PKG_NUM || pArgs->pktNum > SHELL_MAX_PKG_NUM) { + printf("Invalid pktNum:%d, range:[%d, %d]\n", pArgs->pktNum, SHELL_MIN_PKG_NUM, SHELL_MAX_PKG_NUM); + return -1; + } + + if (pArgs->displayWidth <= 0 || pArgs->displayWidth > 10 * 1024) { + printf("Invalid displayWidth:%d, range:[1, 10 * 1024]\n", pArgs->displayWidth); + return -1; + } + + return 0; +} + +int32_t shellParseArgs(int32_t argc, char *argv[]) { + shellInitArgs(argc, argv); + shell.info.clientVersion = + "Welcome to the TDengine shell from %s, Client Version:%s\n" + "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; + shell.info.promptHeader = "taos> "; + shell.info.promptContinue = " -> "; + shell.info.promptSize = 6; + snprintf(shell.info.programVersion, sizeof(shell.info.programVersion), "version: %s", version); + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + shell.info.osname = "Windows"; + snprintf(shell.history.file, TSDB_FILENAME_LEN, "C:/TDengine/%s", SHELL_HISTORY_FILE); + if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1; +#elif defined(_TD_DARWIN_64) + shell.info.osname = "Darwin"; + snprintf(shell.history.file, TSDB_FILENAME_LEN, "%s/%s", getpwuid(getuid())->pw_dir, SHELL_HISTORY_FILE); + if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1; +#else + shell.info.osname = "Linux"; + snprintf(shell.history.file, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), SHELL_HISTORY_FILE); + shellParseArgsUseArgp(argc, argv); + // if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1; + if (shell.args.abort) { + return -1; + } +#endif + + return shellCheckArgs(); +} diff --git a/tools/shell/src/shellCommand.c b/tools/shell/src/shellCommand.c index 546b19f83c..c34ee7a22c 100644 --- a/tools/shell/src/shellCommand.c +++ b/tools/shell/src/shellCommand.c @@ -14,22 +14,122 @@ */ #define __USE_XOPEN +#include "shellInt.h" -#include "shellCommand.h" -#include "os.h" -#include "shell.h" - -#include +#define LEFT 1 +#define RIGHT 2 +#define UP 3 +#define DOWN 4 +#define PSIZE shell.info.promptSize typedef struct { - char widthInString; - char widthOnScreen; -} UTFCodeInfo; + char *buffer; + char *command; + uint32_t commandSize; + uint32_t bufferSize; + uint32_t cursorOffset; + uint32_t screenOffset; + uint32_t endOffset; +} SShellCmd; -int countPrefixOnes(unsigned char c) { - unsigned char mask = 127; +static int32_t shellCountPrefixOnes(uint8_t c); +static void shellGetPrevCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width); +static void shellGetNextCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width); +static void shellInsertChar(SShellCmd *cmd, char *c, int size); +static void shellBackspaceChar(SShellCmd *cmd); +static void shellClearLineBefore(SShellCmd *cmd); +static void shellClearLineAfter(SShellCmd *cmd); +static void shellDeleteChar(SShellCmd *cmd); +static void shellMoveCursorLeft(SShellCmd *cmd); +static void shellMoveCursorRight(SShellCmd *cmd); +static void shellPositionCursorHome(SShellCmd *cmd); +static void shellPositionCursorEnd(SShellCmd *cmd); +static void shellPrintChar(char c, int32_t times); +static void shellPositionCursor(int32_t step, int32_t direction); +static void shellUpdateBuffer(SShellCmd *cmd); +static int32_t shellIsReadyGo(SShellCmd *cmd); +static void shellGetMbSizeInfo(const char *str, int32_t *size, int32_t *width); +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; - int ret = 0; + int32_t ret = 0; while ((c & mask) != 0) { ret++; c <<= 1; @@ -38,7 +138,7 @@ int countPrefixOnes(unsigned char c) { return ret; } -void getPrevCharSize(const char *str, int pos, int *size, int *width) { +void shellGetPrevCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width) { assert(pos > 0); TdWchar wc; @@ -48,16 +148,16 @@ void getPrevCharSize(const char *str, int pos, int *size, int *width) { while (--pos >= 0) { *size += 1; - if (str[pos] > 0 || countPrefixOnes((unsigned char)str[pos]) > 1) break; + if (str[pos] > 0 || shellCountPrefixOnes((uint8_t)str[pos]) > 1) break; } - int rc = taosMbToWchar(&wc, str + pos, MB_CUR_MAX); + int32_t rc = taosMbToWchar(&wc, str + pos, MB_CUR_MAX); assert(rc == *size); *width = taosWcharWidth(wc); } -void getNextCharSize(const char *str, int pos, int *size, int *width) { +void shellGetNextCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width) { assert(pos >= 0); TdWchar wc; @@ -65,13 +165,13 @@ void getNextCharSize(const char *str, int pos, int *size, int *width) { *width = taosWcharWidth(wc); } -void insertChar(Command *cmd, char *c, int size) { +void shellInsertChar(SShellCmd *cmd, char *c, int32_t size) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); TdWchar wc; if (taosMbToWchar(&wc, c, size) < 0) return; - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); /* update the buffer */ memmove(cmd->command + cmd->cursorOffset + size, cmd->command + cmd->cursorOffset, cmd->commandSize - cmd->cursorOffset); @@ -81,122 +181,122 @@ void insertChar(Command *cmd, char *c, int size) { cmd->cursorOffset += size; cmd->screenOffset += taosWcharWidth(wc); cmd->endOffset += taosWcharWidth(wc); - showOnScreen(cmd); + shellShowOnScreen(cmd); } -void backspaceChar(Command *cmd) { +void shellBackspaceChar(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); if (cmd->cursorOffset > 0) { - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); - int size = 0; - int width = 0; - getPrevCharSize(cmd->command, cmd->cursorOffset, &size, &width); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); + int32_t size = 0; + int32_t width = 0; + shellGetPrevCharSize(cmd->command, cmd->cursorOffset, &size, &width); memmove(cmd->command + cmd->cursorOffset - size, cmd->command + cmd->cursorOffset, cmd->commandSize - cmd->cursorOffset); cmd->commandSize -= size; cmd->cursorOffset -= size; cmd->screenOffset -= width; cmd->endOffset -= width; - showOnScreen(cmd); + shellShowOnScreen(cmd); } } -void clearLineBefore(Command *cmd) { +void shellClearLineBefore(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); memmove(cmd->command, cmd->command + cmd->cursorOffset, cmd->commandSize - cmd->cursorOffset); cmd->commandSize -= cmd->cursorOffset; cmd->cursorOffset = 0; cmd->screenOffset = 0; cmd->endOffset = cmd->commandSize; - showOnScreen(cmd); + shellShowOnScreen(cmd); } -void clearLineAfter(Command *cmd) { +void shellClearLineAfter(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); cmd->commandSize -= cmd->endOffset - cmd->cursorOffset; cmd->endOffset = cmd->cursorOffset; - showOnScreen(cmd); + shellShowOnScreen(cmd); } -void deleteChar(Command *cmd) { +void shellDeleteChar(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); if (cmd->cursorOffset < cmd->commandSize) { - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); - int size = 0; - int width = 0; - getNextCharSize(cmd->command, cmd->cursorOffset, &size, &width); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); + int32_t size = 0; + int32_t width = 0; + shellGetNextCharSize(cmd->command, cmd->cursorOffset, &size, &width); memmove(cmd->command + cmd->cursorOffset, cmd->command + cmd->cursorOffset + size, cmd->commandSize - cmd->cursorOffset - size); cmd->commandSize -= size; cmd->endOffset -= width; - showOnScreen(cmd); + shellShowOnScreen(cmd); } } -void moveCursorLeft(Command *cmd) { +void shellMoveCursorLeft(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); if (cmd->cursorOffset > 0) { - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); - int size = 0; - int width = 0; - getPrevCharSize(cmd->command, cmd->cursorOffset, &size, &width); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); + int32_t size = 0; + int32_t width = 0; + shellGetPrevCharSize(cmd->command, cmd->cursorOffset, &size, &width); cmd->cursorOffset -= size; cmd->screenOffset -= width; - showOnScreen(cmd); + shellShowOnScreen(cmd); } } -void moveCursorRight(Command *cmd) { +void shellMoveCursorRight(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); if (cmd->cursorOffset < cmd->commandSize) { - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); - int size = 0; - int width = 0; - getNextCharSize(cmd->command, cmd->cursorOffset, &size, &width); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); + int32_t size = 0; + int32_t width = 0; + shellGetNextCharSize(cmd->command, cmd->cursorOffset, &size, &width); cmd->cursorOffset += size; cmd->screenOffset += width; - showOnScreen(cmd); + shellShowOnScreen(cmd); } } -void positionCursorHome(Command *cmd) { +void shellPositionCursorHome(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); if (cmd->cursorOffset > 0) { - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); cmd->cursorOffset = 0; cmd->screenOffset = 0; - showOnScreen(cmd); + shellShowOnScreen(cmd); } } -void positionCursorEnd(Command *cmd) { +void shellPositionCursorEnd(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); if (cmd->cursorOffset < cmd->commandSize) { - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); cmd->cursorOffset = cmd->commandSize; cmd->screenOffset = cmd->endOffset; - showOnScreen(cmd); + shellShowOnScreen(cmd); } } -void printChar(char c, int times) { - for (int i = 0; i < times; i++) { +void shellPrintChar(char c, int32_t times) { + for (int32_t i = 0; i < times; i++) { fprintf(stdout, "%c", c); } fflush(stdout); } -void positionCursor(int step, int direction) { +void shellPositionCursor(int32_t step, int32_t direction) { if (step > 0) { if (direction == LEFT) { fprintf(stdout, "\033[%dD", step); @@ -211,32 +311,32 @@ void positionCursor(int step, int direction) { } } -void updateBuffer(Command *cmd) { +void shellUpdateBuffer(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); - if (regex_match(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); + if (shellRegexMatch(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " "); strcat(cmd->buffer, cmd->command); cmd->bufferSize += cmd->commandSize; - memset(cmd->command, 0, MAX_COMMAND_SIZE); + memset(cmd->command, 0, SHELL_MAX_COMMAND_SIZE); cmd->cursorOffset = 0; cmd->screenOffset = 0; cmd->commandSize = 0; cmd->endOffset = 0; - showOnScreen(cmd); + shellShowOnScreen(cmd); } -int isReadyGo(Command *cmd) { +int32_t shellIsReadyGo(SShellCmd *cmd) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); - char *total = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - memset(cmd->command + cmd->commandSize, 0, MAX_COMMAND_SIZE - cmd->commandSize); + char *total = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE); + memset(cmd->command + cmd->commandSize, 0, SHELL_MAX_COMMAND_SIZE - cmd->commandSize); 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 (regex_match(total, reg_str, REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(total, reg_str, REG_EXTENDED | REG_ICASE)) { taosMemoryFree(total); return 1; } @@ -245,28 +345,268 @@ int isReadyGo(Command *cmd) { return 0; } -void getMbSizeInfo(const char *str, int *size, int *width) { - TdWchar *wc = (TdWchar *)taosMemoryCalloc(sizeof(TdWchar), MAX_COMMAND_SIZE); +void shellGetMbSizeInfo(const char *str, int32_t *size, int32_t *width) { + TdWchar *wc = (TdWchar *)taosMemoryCalloc(sizeof(TdWchar), SHELL_MAX_COMMAND_SIZE); *size = strlen(str); - taosMbsToWchars(wc, str, MAX_COMMAND_SIZE); - *width = taosWcharsWidth(wc, MAX_COMMAND_SIZE); + taosMbsToWchars(wc, str, SHELL_MAX_COMMAND_SIZE); + *width = taosWcharsWidth(wc, SHELL_MAX_COMMAND_SIZE); taosMemoryFree(wc); } -void resetCommand(Command *cmd, const char s[]) { +void shellResetCommand(SShellCmd *cmd, const char s[]) { assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset); - clearScreen(cmd->endOffset + prompt_size, cmd->screenOffset + prompt_size); - memset(cmd->buffer, 0, MAX_COMMAND_SIZE); - memset(cmd->command, 0, MAX_COMMAND_SIZE); - strncpy(cmd->command, s, MAX_COMMAND_SIZE); - int size = 0; - int width = 0; - getMbSizeInfo(s, &size, &width); + shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE); + memset(cmd->buffer, 0, SHELL_MAX_COMMAND_SIZE); + memset(cmd->command, 0, SHELL_MAX_COMMAND_SIZE); + strncpy(cmd->command, s, SHELL_MAX_COMMAND_SIZE); + int32_t size = 0; + int32_t width = 0; + shellGetMbSizeInfo(s, &size, &width); cmd->bufferSize = 0; cmd->commandSize = size; cmd->cursorOffset = size; cmd->screenOffset = width; cmd->endOffset = width; - showOnScreen(cmd); + shellShowOnScreen(cmd); } + +void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) { + 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; + } + + 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; + shellPositionCursor(cursor_y, LEFT); + shellPositionCursor(command_x - cursor_x, DOWN); + fprintf(stdout, "\033[2K"); + for (int32_t i = 0; i < command_x; i++) { + shellPositionCursor(1, UP); + fprintf(stdout, "\033[2K"); + } + fflush(stdout); +} + +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; + } + + TdWchar wc; + int32_t size = 0; + + // Print out the command. + char *total_string = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE); + memset(total_string, '\0', SHELL_MAX_COMMAND_SIZE); + if (strcmp(cmd->buffer, "") == 0) { + sprintf(total_string, "%s%s", shell.info.promptHeader, cmd->command); + } else { + sprintf(total_string, "%s%s", shell.info.promptContinue, cmd->command); + } + + int32_t remain_column = w.ws_col; + for (char *str = total_string; size < cmd->commandSize + PSIZE;) { + int32_t ret = taosMbToWchar(&wc, str, MB_CUR_MAX); + if (ret < 0) break; + size += ret; + /* assert(size >= 0); */ + int32_t width = taosWcharWidth(wc); + if (remain_column > width) { + printf("%lc", wc); + remain_column -= width; + } else { + if (remain_column == width) { + printf("%lc\n\r", wc); + remain_column = w.ws_col; + } else { + printf("\n\r%lc", wc); + remain_column = w.ws_col - width; + } + } + + str = total_string + size; + } + + 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; + shellPositionCursor(command_y, LEFT); + shellPositionCursor(command_x, UP); + shellPositionCursor(cursor_x, DOWN); + shellPositionCursor(cursor_y, RIGHT); + fflush(stdout); +} + +int32_t shellReadCommand(char *command) { + SShellHistory *pHistory = &shell.history; + SShellCmd cmd = {0}; + uint32_t hist_counter = pHistory->hend; + char utf8_array[10] = "\0"; + + cmd.buffer = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE); + cmd.command = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE); + shellShowOnScreen(&cmd); + + // Read input. + char c; + while (1) { + c = (char)getchar(); // getchar() return an 'int32_t' value + + if (c == EOF) { + return c; + } + + if (c < 0) { // For UTF-8 + int32_t count = shellCountPrefixOnes(c); + utf8_array[0] = c; + for (int32_t k = 1; k < count; k++) { + c = (char)getchar(); + utf8_array[k] = c; + } + shellInsertChar(&cmd, utf8_array, count); + } else if (c < '\033') { + // Ctrl keys. TODO: Implement ctrl combinations + switch (c) { + case 1: // ctrl A + shellPositionCursorHome(&cmd); + break; + case 3: + printf("\n"); + shellResetCommand(&cmd, ""); + kill(0, SIGINT); + break; + case 4: // EOF or Ctrl+D + printf("\n"); + return -1; + case 5: // ctrl E + shellPositionCursorEnd(&cmd); + break; + case 8: + shellBackspaceChar(&cmd); + break; + case '\n': + case '\r': + printf("\n"); + if (shellIsReadyGo(&cmd)) { + sprintf(command, "%s%s", cmd.buffer, cmd.command); + taosMemoryFreeClear(cmd.buffer); + taosMemoryFreeClear(cmd.command); + return 0; + } else { + shellUpdateBuffer(&cmd); + } + break; + case 11: // Ctrl + K; + shellClearLineAfter(&cmd); + break; + case 12: // Ctrl + L; + system("clear"); + shellShowOnScreen(&cmd); + break; + case 21: // Ctrl + U; + shellClearLineBefore(&cmd); + break; + } + } else if (c == '\033') { + c = (char)getchar(); + switch (c) { + case '[': + c = (char)getchar(); + switch (c) { + case 'A': // Up arrow + if (hist_counter != pHistory->hstart) { + hist_counter = (hist_counter + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; + shellResetCommand(&cmd, (pHistory->hist[hist_counter] == NULL) ? "" : pHistory->hist[hist_counter]); + } + break; + case 'B': // Down arrow + if (hist_counter != pHistory->hend) { + int32_t next_hist = (hist_counter + 1) % SHELL_MAX_HISTORY_SIZE; + + if (next_hist != pHistory->hend) { + shellResetCommand(&cmd, (pHistory->hist[next_hist] == NULL) ? "" : pHistory->hist[next_hist]); + } else { + shellResetCommand(&cmd, ""); + } + hist_counter = next_hist; + } + break; + case 'C': // Right arrow + shellMoveCursorRight(&cmd); + break; + case 'D': // Left arrow + shellMoveCursorLeft(&cmd); + break; + case '1': + if ((c = (char)getchar()) == '~') { + // Home key + shellPositionCursorHome(&cmd); + } + break; + case '2': + if ((c = (char)getchar()) == '~') { + // Insert key + } + break; + case '3': + if ((c = (char)getchar()) == '~') { + // Delete key + shellDeleteChar(&cmd); + } + break; + case '4': + if ((c = (char)getchar()) == '~') { + // End key + shellPositionCursorEnd(&cmd); + } + break; + case '5': + if ((c = (char)getchar()) == '~') { + // Page up key + } + break; + case '6': + if ((c = (char)getchar()) == '~') { + // Page down key + } + break; + case 72: + // Home key + shellPositionCursorHome(&cmd); + break; + case 70: + // End key + shellPositionCursorEnd(&cmd); + break; + } + break; + } + } else if (c == 0x7f) { + // press delete key + shellBackspaceChar(&cmd); + } else { + shellInsertChar(&cmd, &c, 1); + } + } + + return 0; +} + +#endif \ No newline at end of file diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index 36d2866fb5..ac2be3c3cf 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -17,135 +17,35 @@ #define _GNU_SOURCE #define _XOPEN_SOURCE #define _DEFAULT_SOURCE +#include "shellInt.h" -#include "os.h" -#include "shell.h" -#include "shellCommand.h" -#include "taosdef.h" -#include "taoserror.h" -#include "tconfig.h" -#include "tglobal.h" -#include "ttypes.h" -#include "tutil.h" +static bool shellIsEmptyCommand(const char *cmd); +static int32_t shellRunSingleCommand(char *command); +static int32_t shellRunCommand(char *command); +static void shellRunSingleCommandImp(char *command); +static char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision); +static void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, + int32_t precision); +static int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres); +static void shellPrintNChar(const char *str, int32_t length, int32_t width); +static void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision); +static int32_t shellVerticalPrintResult(TAOS_RES *tres); +static int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision); +static void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields); +static int32_t shellHorizontalPrintResult(TAOS_RES *tres); +static int32_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical); +static void shellReadHistory(); +static void shellWriteHistory(); +static void shellPrintError(TAOS_RES *tres, int64_t st); +static bool shellIsCommentLine(char *line); +static void shellSourceFile(const char *file); +static void shellGetGrantInfo(); +static void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context); +static void shellCleanup(void *arg); +static void *shellCancelHandler(void *arg); +static void *shellThreadLoop(void *arg); -#include -#include - -/**************** Global variables ****************/ -#ifdef _TD_POWER_ -char CLIENT_VERSION[] = - "Welcome to the PowerDB shell from %s, Client Version:%s\n" - "Copyright (c) 2020 by PowerDB, Inc. All rights reserved.\n\n"; -char PROMPT_HEADER[] = "power> "; - -char CONTINUE_PROMPT[] = " -> "; -int prompt_size = 7; -#elif (_TD_TQ_ == true) -char CLIENT_VERSION[] = - "Welcome to the TQ shell from %s, Client Version:%s\n" - "Copyright (c) 2020 by TQ, Inc. All rights reserved.\n\n"; -char PROMPT_HEADER[] = "tq> "; - -char CONTINUE_PROMPT[] = " -> "; -int prompt_size = 4; -#else -char CLIENT_VERSION[] = - "Welcome to the TDengine shell from %s, Client Version:%s\n" - "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; -char PROMPT_HEADER[] = "taos> "; - -char CONTINUE_PROMPT[] = " -> "; -int prompt_size = 6; -#endif - -int64_t result = 0; -SShellHistory history; - -#define DEFAULT_MAX_BINARY_DISPLAY_WIDTH 30 -extern int32_t tsMaxBinaryDisplayWidth; -extern TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port); - -/* - * FUNCTION: Initialize the shell. - */ -TAOS *shellInit(SShellArguments *_args) { - printf("\n"); - if (!_args->is_use_passwd) { -#ifdef TD_WINDOWS - strcpy(tsOsName, "Windows"); -#elif defined(TD_DARWIN) - strcpy(tsOsName, "Darwin"); -#endif - printf(CLIENT_VERSION, tsOsName, taos_get_client_info()); - } - - fflush(stdout); - - // set options before initializing - if (_args->timezone != NULL) { - taos_options(TSDB_OPTION_TIMEZONE, _args->timezone); - } - - if (!_args->is_use_passwd) { - _args->password = TSDB_DEFAULT_PASS; - } - - if (_args->user == NULL) { - _args->user = TSDB_DEFAULT_USER; - } - - // Connect to the database. - TAOS *con = NULL; - if (_args->auth == NULL) { - con = taos_connect(_args->host, _args->user, _args->password, _args->database, _args->port); - } else { - con = taos_connect_auth(_args->host, _args->user, _args->auth, _args->database, _args->port); - } - - if (con == NULL) { - fflush(stdout); - return con; - } - - /* Read history TODO : release resources here*/ - read_history(); - - // Check if it is temperory run - if (_args->commands != NULL || _args->file[0] != 0) { - if (_args->commands != NULL) { - printf("%s%s\n", PROMPT_HEADER, _args->commands); - shellRunCommand(con, _args->commands); - } - - if (_args->file[0] != 0) { - source_file(con, _args->file); - } - - taos_close(con); - write_history(); - exit(EXIT_SUCCESS); - } - -#if 0 -#ifndef WINDOWS - if (_args->dir[0] != 0) { - source_dir(con, _args); - taos_close(con); - exit(EXIT_SUCCESS); - } - - if (_args->check != 0) { - shellCheck(con, _args); - taos_close(con); - exit(EXIT_SUCCESS); - } -#endif -#endif - - return con; -} - -static bool isEmptyCommand(const char *cmd) { +bool shellIsEmptyCommand(const char *cmd) { for (char c = *cmd++; c != 0; c = *cmd++) { if (c != ' ' && c != '\t' && c != ';') { return false; @@ -154,73 +54,67 @@ static bool isEmptyCommand(const char *cmd) { return true; } -static int32_t shellRunSingleCommand(TAOS *con, char *command) { - /* If command is empty just return */ - if (isEmptyCommand(command)) { +int32_t shellRunSingleCommand(char *command) { + if (shellIsEmptyCommand(command)) { return 0; } - // Analyse the command. - if (regex_match(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) { - taos_close(con); - write_history(); -#ifdef WINDOWS - exit(EXIT_SUCCESS); -#endif + if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) { + shellWriteHistory(); return -1; } - if (regex_match(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) { - // If clear the screen. + if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) { system("clear"); return 0; } - if (regex_match(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$", - REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$", + REG_EXTENDED | REG_ICASE)) { strtok(command, " \t"); strtok(NULL, " \t"); char *p = strtok(NULL, " \t"); - if (strcasecmp(p, "default") == 0) { - tsMaxBinaryDisplayWidth = DEFAULT_MAX_BINARY_DISPLAY_WIDTH; + if (strncasecmp(p, "default", 7) == 0) { + shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH; } else { - tsMaxBinaryDisplayWidth = atoi(p); + int32_t displayWidth = atoi(p); + displayWidth = TRANGE(displayWidth, 1, 10 * 1024); + shell.args.displayWidth = displayWidth; } return 0; } - if (regex_match(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) { /* If source file. */ char *c_ptr = strtok(command, " ;"); assert(c_ptr != NULL); c_ptr = strtok(NULL, " ;"); assert(c_ptr != NULL); - source_file(con, c_ptr); + shellSourceFile(c_ptr); return 0; } - shellRunCommandOnServer(con, command); + shellRunSingleCommandImp(command); return 0; } -int32_t shellRunCommand(TAOS *con, char *command) { - /* If command is empty just return */ - if (isEmptyCommand(command)) { +int32_t shellRunCommand(char *command) { + if (shellIsEmptyCommand(command)) { return 0; } - /* Update the history vector. */ - if (history.hstart == history.hend || - history.hist[(history.hend + MAX_HISTORY_SIZE - 1) % MAX_HISTORY_SIZE] == NULL || - strcmp(command, history.hist[(history.hend + MAX_HISTORY_SIZE - 1) % MAX_HISTORY_SIZE]) != 0) { - if (history.hist[history.hend] != NULL) { - taosMemoryFreeClear(history.hist[history.hend]); + SShellHistory *pHistory = &shell.history; + if (pHistory->hstart == pHistory->hend || + pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL || + strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) { + if (pHistory->hist[pHistory->hend] != NULL) { + taosMemoryFreeClear(pHistory->hist[pHistory->hend]); } - history.hist[history.hend] = strdup(command); + pHistory->hist[pHistory->hend] = strdup(command); - history.hend = (history.hend + 1) % MAX_HISTORY_SIZE; - if (history.hend == history.hstart) { - history.hstart = (history.hstart + 1) % MAX_HISTORY_SIZE; + pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE; + if (pHistory->hend == pHistory->hstart) { + pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE; } } @@ -272,7 +166,7 @@ int32_t shellRunCommand(TAOS *con, char *command) { if (c == ';' && quote == 0) { c = *p; *p = 0; - if (shellRunSingleCommand(con, cmd) < 0) { + if (shellRunSingleCommand(cmd) < 0) { return -1; } *p = c; @@ -281,26 +175,15 @@ int32_t shellRunCommand(TAOS *con, char *command) { } *p = 0; - return shellRunSingleCommand(con, cmd); + return shellRunSingleCommand(cmd); } -void freeResultWithRid(int64_t rid) { -#if 0 - SSqlObj* pSql = taosAcquireRef(tscObjRef, rid); - if(pSql){ - taos_free_result(pSql); - taosReleaseRef(tscObjRef, rid); - } -#endif -} - -void shellRunCommandOnServer(TAOS *con, char command[]) { - int64_t st, et; - wordexp_t full_path; - char *sptr = NULL; - char *cptr = NULL; - char *fname = NULL; - bool printMode = false; +void shellRunSingleCommandImp(char *command) { + int64_t st, et; + char *sptr = NULL; + char *cptr = NULL; + char *fname = NULL; + bool printMode = false; if ((sptr = strstr(command, ">>")) != NULL) { cptr = strstr(command, ";"); @@ -308,12 +191,8 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { *cptr = '\0'; } - if (wordexp(sptr + 2, &full_path, 0) != 0) { - fprintf(stderr, "ERROR: invalid filename: %s\n", sptr + 2); - return; - } + fname = sptr + 2; *sptr = '\0'; - fname = full_path.we_wordv[0]; } if ((sptr = strstr(command, "\\G")) != NULL) { @@ -328,20 +207,19 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { st = taosGetTimestampUs(); - TAOS_RES *pSql = taos_query(con, command); + TAOS_RES *pSql = taos_query(shell.conn, command); if (taos_errno(pSql)) { - taos_error(pSql, st); + shellPrintError(pSql, st); return; } - int64_t oresult = atomic_load_64(&result); + int64_t oresult = atomic_load_64(&shell.result); - if (regex_match(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) { + if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) { fprintf(stdout, "Database changed.\n\n"); fflush(stdout); - atomic_store_64(&result, 0); - freeResultWithRid(oresult); + atomic_store_64(&shell.result, 0); taos_free_result(pSql); return; @@ -349,12 +227,11 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { TAOS_FIELD *pFields = taos_fetch_fields(pSql); if (pFields != NULL) { // select and show kinds of commands - int error_no = 0; + int32_t error_no = 0; - int numOfRows = shellDumpResult(pSql, fname, &error_no, printMode); + int32_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode); if (numOfRows < 0) { - atomic_store_64(&result, 0); - freeResultWithRid(oresult); + atomic_store_64(&shell.result, 0); return; } @@ -366,7 +243,7 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { } taos_free_result(pSql); } else { - int num_rows_affacted = taos_affected_rows(pSql); + int32_t num_rows_affacted = taos_affected_rows(pSql); taos_free_result(pSql); et = taosGetTimestampUs(); printf("Query OK, %d of %d row(s) in database (%.6fs)\n", num_rows_affacted, num_rows_affacted, (et - st) / 1E6); @@ -374,45 +251,11 @@ void shellRunCommandOnServer(TAOS *con, char command[]) { printf("\n"); - if (fname != NULL) { - wordfree(&full_path); - } - - atomic_store_64(&result, 0); - freeResultWithRid(oresult); + atomic_store_64(&shell.result, 0); } -/* Function to do regular expression check */ -int regex_match(const char *s, const char *reg, int cflags) { - regex_t regex; - char msgbuf[100] = {0}; - - /* Compile regular expression */ - if (regcomp(®ex, reg, cflags) != 0) { - fprintf(stderr, "Fail to compile regex"); - exitShell(); - } - - /* Execute regular expression */ - int reti = regexec(®ex, s, 0, NULL, 0); - if (!reti) { - regfree(®ex); - return 1; - } else if (reti == REG_NOMATCH) { - regfree(®ex); - return 0; - } else { - regerror(reti, ®ex, msgbuf, sizeof(msgbuf)); - fprintf(stderr, "Regex match failed: %s\n", msgbuf); - regfree(®ex); - exitShell(); - } - - return 0; -} - -static char *formatTimestamp(char *buf, int64_t val, int precision) { - if (args.is_raw_time) { +char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) { + if (shell.args.is_raw_time) { sprintf(buf, "%" PRId64, val); return buf; } @@ -430,13 +273,14 @@ static char *formatTimestamp(char *buf, int64_t val, int precision) { ms = val % 1000; } - /* comment out as it make testcases like select_with_tags.sim fail. + /* + comment out as it make testcases like select_with_tags.sim fail. but in windows, this may cause the call to localtime crash if tt < 0, need to find a better solution. if (tt < 0) { tt = 0; } - */ + */ #ifdef WINDOWS if (tt < 0) tt = 0; @@ -466,7 +310,7 @@ static char *formatTimestamp(char *buf, int64_t val, int precision) { return buf; } -static void dumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int precision) { +void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) { if (val == NULL) { taosFprintfFile(pFile, "%s", TSDB_DATA_NULL_STR); return; @@ -502,7 +346,7 @@ static void dumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, taosFprintfFile(pFile, "\'%s\'", buf); break; case TSDB_DATA_TYPE_TIMESTAMP: - formatTimestamp(buf, *(int64_t *)val, precision); + shellFormatTimestamp(buf, *(int64_t *)val, precision); taosFprintfFile(pFile, "'%s'", buf); break; default: @@ -510,35 +354,28 @@ static void dumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, } } -static int dumpResultToFile(const char *fname, TAOS_RES *tres) { +int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) { + char fullname[PATH_MAX] = {0}; + if (taosExpandDir(fname, fullname, PATH_MAX) != 0) { + tstrncpy(fullname, fname, PATH_MAX); + } + TAOS_ROW row = taos_fetch_row(tres); if (row == NULL) { return 0; } - wordexp_t full_path; - - if (wordexp((char *)fname, &full_path, 0) != 0) { - fprintf(stderr, "ERROR: invalid file name: %s\n", fname); - return -1; - } - - // FILE *fp = fopen(full_path.we_wordv[0], "w"); - TdFilePtr pFile = - taosOpenFile(full_path.we_wordv[0], TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM); + TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM); if (pFile == NULL) { - fprintf(stderr, "ERROR: failed to open file: %s\n", full_path.we_wordv[0]); - wordfree(&full_path); + fprintf(stderr, "failed to open file: %s\n", fullname); return -1; } - wordfree(&full_path); - - int num_fields = taos_num_fields(tres); TAOS_FIELD *fields = taos_fetch_fields(tres); - int precision = taos_result_precision(tres); + int32_t num_fields = taos_num_fields(tres); + int32_t precision = taos_result_precision(tres); - for (int col = 0; col < num_fields; col++) { + for (int32_t col = 0; col < num_fields; col++) { if (col > 0) { taosFprintfFile(pFile, ","); } @@ -546,14 +383,14 @@ static int dumpResultToFile(const char *fname, TAOS_RES *tres) { } taosFprintfFile(pFile, "\n"); - int numOfRows = 0; + int32_t numOfRows = 0; do { int32_t *length = taos_fetch_lengths(tres); - for (int i = 0; i < num_fields; i++) { + for (int32_t i = 0; i < num_fields; i++) { if (i > 0) { taosFprintfFile(pFile, "\n"); } - dumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision); + shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision); } taosFprintfFile(pFile, "\n"); @@ -561,19 +398,19 @@ static int dumpResultToFile(const char *fname, TAOS_RES *tres) { row = taos_fetch_row(tres); } while (row != NULL); - result = 0; + atomic_store_64(&shell.result, 0); taosCloseFile(&pFile); return numOfRows; } -static void shellPrintNChar(const char *str, int length, int width) { +void shellPrintNChar(const char *str, int32_t length, int32_t width) { TdWchar tail[3]; - int pos = 0, cols = 0, totalCols = 0, tailLen = 0; + int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0; while (pos < length) { TdWchar wc; - int bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX); + int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX); if (bytes == 0) { break; } @@ -583,9 +420,9 @@ static void shellPrintNChar(const char *str, int length, int width) { } #ifdef WINDOWS - int w = bytes; + int32_t w = bytes; #else - int w = taosWcharWidth(wc); + int32_t w = taosWcharWidth(wc); #endif if (w <= 0) { continue; @@ -611,7 +448,7 @@ static void shellPrintNChar(const char *str, int length, int width) { if (totalCols > width) { // width could be 1 or 2, so printf("...") cannot be used - for (int i = 0; i < 3; i++) { + for (int32_t i = 0; i < 3; i++) { if (cols >= width) { break; } @@ -619,7 +456,7 @@ static void shellPrintNChar(const char *str, int length, int width) { ++cols; } } else { - for (int i = 0; i < tailLen; i++) { + for (int32_t i = 0; i < tailLen; i++) { printf("%lc", tail[i]); } cols = totalCols; @@ -630,9 +467,9 @@ static void shellPrintNChar(const char *str, int length, int width) { } } -static void printField(const char *val, TAOS_FIELD *field, int width, int32_t length, int precision) { +void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) { if (val == NULL) { - int w = width; + int32_t w = width; if (field->type < TSDB_DATA_TYPE_TINYINT || field->type > TSDB_DATA_TYPE_DOUBLE) { w = 0; } @@ -683,7 +520,7 @@ static void printField(const char *val, TAOS_FIELD *field, int width, int32_t le shellPrintNChar(val, length, width); break; case TSDB_DATA_TYPE_TIMESTAMP: - formatTimestamp(buf, *(int64_t *)val, precision); + shellFormatTimestamp(buf, *(int64_t *)val, precision); printf("%s", buf); break; default: @@ -691,30 +528,19 @@ static void printField(const char *val, TAOS_FIELD *field, int width, int32_t le } } -bool isSelectQuery(TAOS_RES *tres) { -#if 0 - char *sql = tscGetSqlStr(tres); - - if (regex_match(sql, "^[\t ]*select[ \t]*", REG_EXTENDED | REG_ICASE)) { - return true; - } -#endif - return false; -} - -static int verticalPrintResult(TAOS_RES *tres) { +int32_t shellVerticalPrintResult(TAOS_RES *tres) { TAOS_ROW row = taos_fetch_row(tres); if (row == NULL) { return 0; } - int num_fields = taos_num_fields(tres); + int32_t num_fields = taos_num_fields(tres); TAOS_FIELD *fields = taos_fetch_fields(tres); - int precision = taos_result_precision(tres); + int32_t precision = taos_result_precision(tres); - int maxColNameLen = 0; - for (int col = 0; col < num_fields; col++) { - int len = (int)strlen(fields[col].name); + int32_t maxColNameLen = 0; + for (int32_t col = 0; col < num_fields; col++) { + int32_t len = (int32_t)strlen(fields[col].name); if (len > maxColNameLen) { maxColNameLen = len; } @@ -722,25 +548,25 @@ static int verticalPrintResult(TAOS_RES *tres) { uint64_t resShowMaxNum = UINT64_MAX; - if (args.commands == NULL && args.file[0] == 0 && isSelectQuery(tres) /*&& !tscIsQueryWithLimit(tres)*/) { - resShowMaxNum = DEFAULT_RES_SHOW_NUM; + if (shell.args.commands == NULL && shell.args.file[0] == 0) { + resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM; } - int numOfRows = 0; - int showMore = 1; + int32_t numOfRows = 0; + int32_t showMore = 1; do { if (numOfRows < resShowMaxNum) { printf("*************************** %d.row ***************************\n", numOfRows + 1); int32_t *length = taos_fetch_lengths(tres); - for (int i = 0; i < num_fields; i++) { + for (int32_t i = 0; i < num_fields; i++) { TAOS_FIELD *field = fields + i; - int padding = (int)(maxColNameLen - strlen(field->name)); + int32_t padding = (int32_t)(maxColNameLen - strlen(field->name)); printf("%*.s%s: ", padding, " ", field->name); - printField((const char *)row[i], field, 0, length[i], precision); + shellPrintField((const char *)row[i], field, 0, length[i], precision); putchar('\n'); } } else if (showMore) { @@ -756,8 +582,8 @@ static int verticalPrintResult(TAOS_RES *tres) { return numOfRows; } -static int calcColWidth(TAOS_FIELD *field, int precision) { - int width = (int)strlen(field->name); +int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) { + int32_t width = (int32_t)strlen(field->name); switch (field->type) { case TSDB_DATA_TYPE_BOOL: @@ -786,23 +612,23 @@ static int calcColWidth(TAOS_FIELD *field, int precision) { return TMAX(25, width); case TSDB_DATA_TYPE_BINARY: - if (field->bytes > tsMaxBinaryDisplayWidth) { - return TMAX(tsMaxBinaryDisplayWidth, width); + if (field->bytes > shell.args.displayWidth) { + return TMAX(shell.args.displayWidth, width); } else { return TMAX(field->bytes, width); } case TSDB_DATA_TYPE_NCHAR: { int16_t bytes = field->bytes * TSDB_NCHAR_SIZE; - if (bytes > tsMaxBinaryDisplayWidth) { - return TMAX(tsMaxBinaryDisplayWidth, width); + if (bytes > shell.args.displayWidth) { + return TMAX(shell.args.displayWidth, width); } else { return TMAX(bytes, width); } } case TSDB_DATA_TYPE_TIMESTAMP: - if (args.is_raw_time) { + if (shell.args.is_raw_time) { return TMAX(14, width); } if (precision == TSDB_TIME_PRECISION_NANO) { @@ -820,55 +646,55 @@ static int calcColWidth(TAOS_FIELD *field, int precision) { return 0; } -static void printHeader(TAOS_FIELD *fields, int *width, int num_fields) { - int rowWidth = 0; - for (int col = 0; col < num_fields; col++) { +void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) { + int32_t rowWidth = 0; + for (int32_t col = 0; col < num_fields; col++) { TAOS_FIELD *field = fields + col; - int padding = (int)(width[col] - strlen(field->name)); - int left = padding / 2; + int32_t padding = (int32_t)(width[col] - strlen(field->name)); + int32_t left = padding / 2; printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " "); rowWidth += width[col] + 3; } putchar('\n'); - for (int i = 0; i < rowWidth; i++) { + for (int32_t i = 0; i < rowWidth; i++) { putchar('='); } putchar('\n'); } -static int horizontalPrintResult(TAOS_RES *tres) { +int32_t shellHorizontalPrintResult(TAOS_RES *tres) { TAOS_ROW row = taos_fetch_row(tres); if (row == NULL) { return 0; } - int num_fields = taos_num_fields(tres); + int32_t num_fields = taos_num_fields(tres); TAOS_FIELD *fields = taos_fetch_fields(tres); - int precision = taos_result_precision(tres); + int32_t precision = taos_result_precision(tres); - int width[TSDB_MAX_COLUMNS]; - for (int col = 0; col < num_fields; col++) { - width[col] = calcColWidth(fields + col, precision); + int32_t width[TSDB_MAX_COLUMNS]; + for (int32_t col = 0; col < num_fields; col++) { + width[col] = shellCalcColWidth(fields + col, precision); } - printHeader(fields, width, num_fields); + shellPrintHeader(fields, width, num_fields); uint64_t resShowMaxNum = UINT64_MAX; - if (args.commands == NULL && args.file[0] == 0 && isSelectQuery(tres) /* && !tscIsQueryWithLimit(tres)*/) { - resShowMaxNum = DEFAULT_RES_SHOW_NUM; + if (shell.args.commands == NULL && shell.args.file[0] == 0) { + resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM; } - int numOfRows = 0; - int showMore = 1; + int32_t numOfRows = 0; + int32_t showMore = 1; do { int32_t *length = taos_fetch_lengths(tres); if (numOfRows < resShowMaxNum) { - for (int i = 0; i < num_fields; i++) { + for (int32_t i = 0; i < num_fields; i++) { putchar(' '); - printField((const char *)row[i], fields + i, width[i], length[i], precision); + shellPrintField((const char *)row[i], fields + i, width[i], length[i], precision); putchar(' '); putchar('|'); } @@ -886,50 +712,35 @@ static int horizontalPrintResult(TAOS_RES *tres) { return numOfRows; } -int shellDumpResult(TAOS_RES *tres, char *fname, int *error_no, bool vertical) { - int numOfRows = 0; +int32_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical) { + int32_t numOfRows = 0; if (fname != NULL) { - numOfRows = dumpResultToFile(fname, tres); + numOfRows = shellDumpResultToFile(fname, tres); } else if (vertical) { - numOfRows = verticalPrintResult(tres); + numOfRows = shellVerticalPrintResult(tres); } else { - numOfRows = horizontalPrintResult(tres); + numOfRows = shellHorizontalPrintResult(tres); } *error_no = taos_errno(tres); return numOfRows; } -void read_history() { - // Initialize history - memset(history.hist, 0, sizeof(char *) * MAX_HISTORY_SIZE); - history.hstart = 0; - history.hend = 0; - char *line = NULL; - int read_size = 0; - - char f_history[TSDB_FILENAME_LEN]; - get_history_path(f_history); - - // FILE *f = fopen(f_history, "r"); - TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { -#ifndef WINDOWS - if (errno != ENOENT) { - fprintf(stderr, "Failed to open file %s, reason:%s\n", f_history, strerror(errno)); - } -#endif - return; - } +void shellReadHistory() { + SShellHistory *pHistory = &shell.history; + TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) return; + char *line = NULL; + int32_t read_size = 0; while ((read_size = taosGetLineFile(pFile, &line)) != -1) { line[read_size - 1] = '\0'; - history.hist[history.hend] = strdup(line); + pHistory->hist[pHistory->hend] = strdup(line); - history.hend = (history.hend + 1) % MAX_HISTORY_SIZE; + pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE; - if (history.hend == history.hstart) { - history.hstart = (history.hstart + 1) % MAX_HISTORY_SIZE; + if (pHistory->hend == pHistory->hstart) { + pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE; } } @@ -937,72 +748,48 @@ void read_history() { taosCloseFile(&pFile); } -void write_history() { - char f_history[TSDB_FILENAME_LEN]; - get_history_path(f_history); +void shellWriteHistory() { + SShellHistory *pHistory = &shell.history; + TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_WRITE | TD_FILE_STREAM); + if (pFile == NULL) return; - // FILE *f = fopen(f_history, "w"); - TdFilePtr pFile = taosOpenFile(f_history, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM); - if (pFile == NULL) { -#ifndef WINDOWS - fprintf(stderr, "Failed to open file %s for write, reason:%s\n", f_history, strerror(errno)); -#endif - return; - } - - for (int i = history.hstart; i != history.hend;) { - if (history.hist[i] != NULL) { - taosFprintfFile(pFile, "%s\n", history.hist[i]); - taosMemoryFreeClear(history.hist[i]); + for (int32_t i = pHistory->hstart; i != pHistory->hend;) { + if (pHistory->hist[i] != NULL) { + taosFprintfFile(pFile, "%s\n", pHistory->hist[i]); + taosMemoryFreeClear(pHistory->hist[i]); } - i = (i + 1) % MAX_HISTORY_SIZE; + i = (i + 1) % SHELL_MAX_HISTORY_SIZE; } + taosFsyncFile(pFile); taosCloseFile(&pFile); } -void taos_error(TAOS_RES *tres, int64_t st) { +void shellPrintError(TAOS_RES *tres, int64_t st) { int64_t et = taosGetTimestampUs(); - atomic_store_ptr(&result, 0); + atomic_store_ptr(&shell.result, 0); fprintf(stderr, "\nDB error: %s (%.6fs)\n", taos_errstr(tres), (et - st) / 1E6); taos_free_result(tres); } -int isCommentLine(char *line) { - if (line == NULL) return 1; - - return regex_match(line, "^\\s*#.*", REG_EXTENDED); +bool shellIsCommentLine(char *line) { + if (line == NULL) return true; + return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED); } -void source_file(TAOS *con, char *fptr) { - wordexp_t full_path; - int read_len = 0; - char *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1); - size_t cmd_len = 0; - char *line = NULL; +void shellSourceFile(const char *file) { + int32_t read_len = 0; + char *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1); + size_t cmd_len = 0; + char *line = NULL; + char fullname[PATH_MAX] = {0}; - if (wordexp(fptr, &full_path, 0) != 0) { - fprintf(stderr, "ERROR: illegal file name\n"); - taosMemoryFree(cmd); - return; + if (taosExpandDir(file, fullname, PATH_MAX) != 0) { + tstrncpy(fullname, file, PATH_MAX); } - char *fname = full_path.we_wordv[0]; - - /* - if (access(fname, F_OK) != 0) { - fprintf(stderr, "ERROR: file %s is not exist\n", fptr); - - wordfree(&full_path); - taosMemoryFree(cmd); - return; - } - */ - - // FILE *f = fopen(fname, "r"); - TdFilePtr pFile = taosOpenFile(fname, TD_FILE_READ | TD_FILE_STREAM); + TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - fprintf(stderr, "ERROR: failed to open file %s\n", fname); - wordfree(&full_path); + fprintf(stderr, "failed to open file %s\n", fullname); taosMemoryFree(cmd); return; } @@ -1011,7 +798,7 @@ void source_file(TAOS *con, char *fptr) { if (read_len >= TSDB_MAX_ALLOWED_SQL_LEN) continue; line[--read_len] = '\0'; - if (read_len == 0 || isCommentLine(line)) { // line starts with # + if (read_len == 0 || shellIsCommentLine(line)) { // line starts with # continue; } @@ -1023,36 +810,38 @@ void source_file(TAOS *con, char *fptr) { } memcpy(cmd + cmd_len, line, read_len); - printf("%s%s\n", PROMPT_HEADER, cmd); - shellRunCommand(con, cmd); + printf("%s%s\n", shell.info.promptHeader, cmd); + shellRunCommand(cmd); memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN); cmd_len = 0; } taosMemoryFree(cmd); if (line != NULL) taosMemoryFree(line); - wordfree(&full_path); taosCloseFile(&pFile); } -void shellGetGrantInfo(void *con) { - return; -#if 0 +void shellGetGrantInfo() { + char sinfo[1024] = {0}; + tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo)); + strtok(sinfo, "\n"); + char sql[] = "show grants"; - TAOS_RES* tres = taos_query(con, sql); + TAOS_RES *tres = taos_query(shell.conn, sql); - int code = taos_errno(tres); + int32_t code = taos_errno(tres); if (code != TSDB_CODE_SUCCESS) { - if (code == TSDB_CODE_COM_OPS_NOT_SUPPORT) { - fprintf(stdout, "Server is Community Edition, version is %s\n\n", taos_get_server_info(con)); + if (code == TSDB_CODE_OPS_NOT_SUPPORT) { + fprintf(stdout, "Server is Community Edition, %s\n\n", sinfo); } else { - fprintf(stderr, "Failed to check Server Edition, Reason:%d:%s\n\n", taos_errno(con), taos_errstr(con)); + fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\n\n", taos_errno(shell.conn), + taos_errstr(shell.conn)); } return; } - int num_fields = taos_field_count(tres); + int32_t num_fields = taos_field_count(tres); if (num_fields == 0) { fprintf(stderr, "\nInvalid grant information.\n"); exit(0); @@ -1063,7 +852,7 @@ void shellGetGrantInfo(void *con) { } TAOS_FIELD *fields = taos_fetch_fields(tres); - TAOS_ROW row = taos_fetch_row(tres); + TAOS_ROW row = taos_fetch_row(tres); if (row == NULL) { fprintf(stderr, "\nFailed to get grant information from server. Abort.\n"); exit(0); @@ -1078,15 +867,128 @@ void shellGetGrantInfo(void *con) { memcpy(expired, row[2], fields[2].bytes); if (strcmp(expiretime, "unlimited") == 0) { - fprintf(stdout, "Server is Enterprise %s Edition, version is %s and will never expire.\n", serverVersion, taos_get_server_info(con)); + fprintf(stdout, "Server is Enterprise %s Edition, %s and will never expire.\n", serverVersion, sinfo); } else { - fprintf(stdout, "Server is Enterprise %s Edition, version is %s and will expire at %s.\n", serverVersion, taos_get_server_info(con), expiretime); + fprintf(stdout, "Server is Enterprise %s Edition, %s and will expire at %s.\n", serverVersion, sinfo, expiretime); } - result = NULL; + atomic_store_64(&shell.result, 0); taos_free_result(tres); } fprintf(stdout, "\n"); -#endif +} + +void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); } + +void shellSigintHandler(int32_t signum, void *sigInfo, void *context) { + // do nothing +} + +void shellCleanup(void *arg) { taosResetTerminalMode(); } + +void *shellCancelHandler(void *arg) { + setThreadName("shellCancelHandler"); + while (1) { + if (tsem_wait(&shell.cancelSem) != 0) { + taosMsleep(10); + continue; + } + + taosResetTerminalMode(); + printf("\nReceive SIGTERM or other signal, quit shell.\n"); + shellWriteHistory(); + shellExit(); + } + + return NULL; +} + +void *shellThreadLoop(void *arg) { + setThreadName("shellThreadLoop"); + taosGetOldTerminalMode(); + taosThreadCleanupPush(shellCleanup, NULL); + + char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE); + if (command == NULL) { + printf("failed to malloc command\n"); + return NULL; + } + + do { + memset(command, 0, SHELL_MAX_COMMAND_SIZE); + taosSetTerminalMode(); + + if (shellReadCommand(command) != 0) { + break; + } + + taosResetTerminalMode(); + } while (shellRunCommand(command) == 0); + + taosMemoryFreeClear(command); + shellWriteHistory(); + shellExit(); + + taosThreadCleanupPop(1); + return NULL; +} + +int32_t shellExecute() { + printf(shell.info.clientVersion, shell.info.osname, taos_get_client_info()); + fflush(stdout); + + SShellArgs *pArgs = &shell.args; + if (shell.args.auth == NULL) { + shell.conn = taos_connect(pArgs->host, pArgs->user, pArgs->password, pArgs->database, pArgs->port); + } else { + shell.conn = taos_connect_auth(pArgs->host, pArgs->user, pArgs->auth, pArgs->database, pArgs->port); + } + + if (shell.conn == NULL) { + fflush(stdout); + return -1; + } + + shellReadHistory(); + + if (pArgs->commands != NULL || pArgs->file[0] != 0) { + if (pArgs->commands != NULL) { + printf("%s%s\n", shell.info.promptHeader, pArgs->commands); + char *cmd = strdup(pArgs->commands); + shellRunCommand(cmd); + taosMemoryFree(cmd); + } + + if (pArgs->file[0] != 0) { + shellSourceFile(pArgs->file); + } + + taos_close(shell.conn); + shellWriteHistory(); + return 0; + } + + if (tsem_init(&shell.cancelSem, 0, 0) != 0) { + printf("failed to create cancel semphore\n"); + return -1; + } + + TdThread spid = {0}; + taosThreadCreate(&spid, NULL, shellCancelHandler, NULL); + + taosSetSignal(SIGTERM, shellQueryInterruptHandler); + taosSetSignal(SIGHUP, shellQueryInterruptHandler); + taosSetSignal(SIGABRT, shellQueryInterruptHandler); + + taosSetSignal(SIGINT, shellSigintHandler); + + shellGetGrantInfo(shell.conn); + + while (1) { + taosThreadCreate(&shell.pid, NULL, shellThreadLoop, shell.conn); + taosThreadJoin(shell.pid, NULL); + } + + return 0; } diff --git a/tools/shell/src/shellMain.c b/tools/shell/src/shellMain.c index 1ee42358f6..6672cee367 100644 --- a/tools/shell/src/shellMain.c +++ b/tools/shell/src/shellMain.c @@ -14,681 +14,53 @@ */ #define __USE_XOPEN -#include "shellCommand.h" -#include "tglobal.h" -#include "tlog.h" +#include "shellInt.h" -#include -#include -#include - -#define OPT_ABORT 1 /* abort */ - -int indicator = 1; - -void insertChar(Command *cmd, char *c, int size); -void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, int32_t pkgNum, char *pkgType); -const char *argp_program_version = version; -const char *argp_program_bug_address = ""; -static char doc[] = ""; -static char args_doc[] = ""; - -TdThread pid; -static tsem_t cancelSem; -extern void taos_init(); - -static struct argp_option options[] = { - {"host", 'h', "HOST", 0, "TDengine server FQDN to connect. The default host is localhost."}, - {"password", 'p', NULL, 0, "The password to use when connecting to the server."}, - {"port", 'P', "PORT", 0, "The TCP/IP port number to use for the connection."}, - {"user", 'u', "USER", 0, "The user name to use when connecting to the server."}, - {"auth", 'A', "Auth", 0, "The auth string to use when connecting to the server."}, - {"config-dir", 'c', "CONFIG_DIR", 0, "Configuration directory."}, - {"dump-config",'C', NULL, 0, "Dump configuration."}, - {"commands", 's', "COMMANDS", 0, "Commands to run without enter the shell."}, - {"raw-time", 'r', NULL, 0, "Output time as uint64_t."}, - {"file", 'f', "FILE", 0, "Script to run without enter the shell."}, - {"directory", 'D', "DIRECTORY", 0, "Use multi-thread to import all SQL files in the directory separately."}, - {"thread", 'T', "THREADNUM", 0, "Number of threads when using multi-thread to import data."}, - {"check", 'k', "CHECK", 0, "Check tables."}, - {"database", 'd', "DATABASE", 0, "Database to use when connecting to the server."}, - {"timezone", 'z', "TIMEZONE", 0, "Time zone of the shell, default is local."}, - {"status", 't', NULL, 0, "Check the service status."}, - {"verbose", 'v', NULL, 0, "Check the details of the service status."}, - {"netrole", 'n', "NETROLE", 0, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup|sync|speed|fqdn."}, - {"pktlen", 'l', "PKTLEN", 0, "Packet length used for net test, default is 1000 bytes."}, - {"pktnum", 'N', "PKTNUM", 0, "Packet numbers used for net test, default is 100."}, -// Shuduo: 3.0 does not support UDP any more -// {"pkttype", 'S', "PKTTYPE", 0, "Packet type used for net test, default is TCP."}, - {0}}; - -static error_t parse_opt(int key, char *arg, struct argp_state *state) { - /* Get the input argument from argp_parse, which we - know is a pointer to our arguments structure. */ - SShellArguments *arguments = state->input; - wordexp_t full_path; - - switch (key) { - case 'h': - arguments->host = arg; - break; - case 'p': - break; - case 'P': - if (arg) { - arguments->port = atoi(arg); - } else { - fprintf(stderr, "Invalid port\n"); - return -1; - } - - break; - case 'z': - arguments->timezone = arg; - break; - case 'u': - arguments->user = arg; - break; - case 'A': - arguments->auth = arg; - break; - case 'c': - if (wordexp(arg, &full_path, 0) != 0) { - fprintf(stderr, "Invalid path %s\n", arg); - return -1; - } - if (strlen(full_path.we_wordv[0]) >= TSDB_FILENAME_LEN) { - fprintf(stderr, "config file path: %s overflow max len %d\n", full_path.we_wordv[0], TSDB_FILENAME_LEN - 1); - wordfree(&full_path); - return -1; - } - tstrncpy(configDir, full_path.we_wordv[0], TSDB_FILENAME_LEN); - wordfree(&full_path); - break; - case 'C': - arguments->dump_config = true; - break; - case 's': - arguments->commands = arg; - break; - case 'r': - arguments->is_raw_time = true; - break; - case 'f': - if ((0 == strlen(arg)) || (wordexp(arg, &full_path, 0) != 0)) { - fprintf(stderr, "Invalid path %s\n", arg); - return -1; - } - tstrncpy(arguments->file, full_path.we_wordv[0], TSDB_FILENAME_LEN); - wordfree(&full_path); - break; - case 'D': - if (wordexp(arg, &full_path, 0) != 0) { - fprintf(stderr, "Invalid path %s\n", arg); - return -1; - } - tstrncpy(arguments->dir, full_path.we_wordv[0], TSDB_FILENAME_LEN); - wordfree(&full_path); - break; - case 'T': - if (arg) { - arguments->threadNum = atoi(arg); - } else { - fprintf(stderr, "Invalid number of threads\n"); - return -1; - } - break; - case 'k': - arguments->check = atoi(arg); - break; - case 't': - arguments->status = true; - break; - case 'v': - arguments->verbose = true; - break; - case 'd': - arguments->database = arg; - break; - case 'n': - arguments->netTestRole = arg; - break; - case 'l': - if (arg) { - arguments->pktLen = atoi(arg); - } else { - fprintf(stderr, "Invalid packet length\n"); - return -1; - } - break; - case 'N': - if (arg) { - arguments->pktNum = atoi(arg); - } else { - fprintf(stderr, "Invalid packet number\n"); - return -1; - } - break; - case 'S': - arguments->pktType = arg; - break; - case OPT_ABORT: - arguments->abort = 1; - break; - default: - return ARGP_ERR_UNKNOWN; - } - return 0; -} - -/* Our argp parser. */ -static struct argp argp = {options, parse_opt, args_doc, doc}; - -char LINUXCLIENT_VERSION[] = - "Welcome to the TDengine shell from %s, Client Version:%s\n" - "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n"; -char g_password[SHELL_MAX_PASSWORD_LEN]; - -static void parse_args(int argc, char *argv[], SShellArguments *arguments) { - for (int i = 1; i < argc; i++) { - if ((strncmp(argv[i], "-p", 2) == 0) || (strncmp(argv[i], "--password", 10) == 0)) { - printf(LINUXCLIENT_VERSION, tsOsName, taos_get_client_info()); - if ((strlen(argv[i]) == 2) || (strncmp(argv[i], "--password", 10) == 0)) { - printf("Enter password: "); - taosSetConsoleEcho(false); - if (scanf("%20s", g_password) > 1) { - fprintf(stderr, "password reading error\n"); - } - taosSetConsoleEcho(true); - if (EOF == getchar()) { - fprintf(stderr, "getchar() return EOF\n"); - } - } else { - tstrncpy(g_password, (char *)(argv[i] + 2), SHELL_MAX_PASSWORD_LEN); - strcpy(argv[i], "-p"); - } - arguments->password = g_password; - arguments->is_use_passwd = true; - } - } -} - -void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { - static char verType[32] = {0}; - sprintf(verType, "version: %s\n", version); - - argp_program_version = verType; - - if (argc > 1) { - parse_args(argc, argv, arguments); - } - - argp_parse(&argp, argc, argv, 0, 0, arguments); - if (arguments->abort) { -#ifndef _ALPINE -#if 0 - error(10, 0, "ABORTED"); -#endif -#else - abort(); -#endif - } -} - -int32_t shellReadCommand(TAOS *con, char *command) { - unsigned hist_counter = history.hend; - char utf8_array[10] = "\0"; - Command cmd; - memset(&cmd, 0, sizeof(cmd)); - cmd.buffer = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - cmd.command = (char *)taosMemoryCalloc(1, MAX_COMMAND_SIZE); - showOnScreen(&cmd); - - // Read input. - char c; - while (1) { - c = (char)getchar(); // getchar() return an 'int' value - - if (c == EOF) { - return c; - } - - if (c < 0) { // For UTF-8 - int count = countPrefixOnes(c); - utf8_array[0] = c; - for (int k = 1; k < count; k++) { - c = (char)getchar(); - utf8_array[k] = c; - } - insertChar(&cmd, utf8_array, count); - } else if (c < '\033') { - // Ctrl keys. TODO: Implement ctrl combinations - switch (c) { - case 1: // ctrl A - positionCursorHome(&cmd); - break; - case 3: - printf("\n"); - resetCommand(&cmd, ""); - kill(0, SIGINT); - break; - case 4: // EOF or Ctrl+D - printf("\n"); - taos_close(con); - // write the history - write_history(); - exitShell(); - break; - case 5: // ctrl E - positionCursorEnd(&cmd); - break; - case 8: - backspaceChar(&cmd); - break; - case '\n': - case '\r': - printf("\n"); - if (isReadyGo(&cmd)) { - sprintf(command, "%s%s", cmd.buffer, cmd.command); - taosMemoryFreeClear(cmd.buffer); - taosMemoryFreeClear(cmd.command); - return 0; - } else { - updateBuffer(&cmd); - } - break; - case 11: // Ctrl + K; - clearLineAfter(&cmd); - break; - case 12: // Ctrl + L; - system("clear"); - showOnScreen(&cmd); - break; - case 21: // Ctrl + U; - clearLineBefore(&cmd); - break; - } - } else if (c == '\033') { - c = (char)getchar(); - switch (c) { - case '[': - c = (char)getchar(); - switch (c) { - case 'A': // Up arrow - if (hist_counter != history.hstart) { - hist_counter = (hist_counter + MAX_HISTORY_SIZE - 1) % MAX_HISTORY_SIZE; - resetCommand(&cmd, (history.hist[hist_counter] == NULL) ? "" : history.hist[hist_counter]); - } - break; - case 'B': // Down arrow - if (hist_counter != history.hend) { - int next_hist = (hist_counter + 1) % MAX_HISTORY_SIZE; - - if (next_hist != history.hend) { - resetCommand(&cmd, (history.hist[next_hist] == NULL) ? "" : history.hist[next_hist]); - } else { - resetCommand(&cmd, ""); - } - hist_counter = next_hist; - } - break; - case 'C': // Right arrow - moveCursorRight(&cmd); - break; - case 'D': // Left arrow - moveCursorLeft(&cmd); - break; - case '1': - if ((c = (char)getchar()) == '~') { - // Home key - positionCursorHome(&cmd); - } - break; - case '2': - if ((c = (char)getchar()) == '~') { - // Insert key - } - break; - case '3': - if ((c = (char)getchar()) == '~') { - // Delete key - deleteChar(&cmd); - } - break; - case '4': - if ((c = (char)getchar()) == '~') { - // End key - positionCursorEnd(&cmd); - } - break; - case '5': - if ((c = (char)getchar()) == '~') { - // Page up key - } - break; - case '6': - if ((c = (char)getchar()) == '~') { - // Page down key - } - break; - case 72: - // Home key - positionCursorHome(&cmd); - break; - case 70: - // End key - positionCursorEnd(&cmd); - break; - } - break; - } - } else if (c == 0x7f) { - // press delete key - backspaceChar(&cmd); - } else { - insertChar(&cmd, &c, 1); - } - } - - return 0; -} - -void *shellLoopQuery(void *arg) { - if (indicator) { - getOldTerminalMode(); - indicator = 0; - } - - TAOS *con = (TAOS *)arg; - - setThreadName("shellLoopQuery"); - - taosThreadCleanupPush(cleanup_handler, NULL); - - char *command = taosMemoryMalloc(MAX_COMMAND_SIZE); - if (command == NULL) { - uError("failed to malloc command"); - return NULL; - } - - int32_t err = 0; - - do { - // Read command from shell. - memset(command, 0, MAX_COMMAND_SIZE); - setTerminalMode(); - err = shellReadCommand(con, command); - if (err) { - break; - } - resetTerminalMode(); - } while (shellRunCommand(con, command) == 0); - - taosMemoryFreeClear(command); - exitShell(); - - taosThreadCleanupPop(1); - - return NULL; -} - -void get_history_path(char *_history) { snprintf(_history, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), HISTORY_FILE); } - -void clearScreen(int ecmd_pos, int cursor_pos) { - 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; - } - - int cursor_x = cursor_pos / w.ws_col; - int cursor_y = cursor_pos % w.ws_col; - int command_x = ecmd_pos / w.ws_col; - positionCursor(cursor_y, LEFT); - positionCursor(command_x - cursor_x, DOWN); - fprintf(stdout, "\033[2K"); - for (int i = 0; i < command_x; i++) { - positionCursor(1, UP); - fprintf(stdout, "\033[2K"); - } - fflush(stdout); -} - -void showOnScreen(Command *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; - } - - TdWchar wc; - int size = 0; - - // Print out the command. - char *total_string = taosMemoryMalloc(MAX_COMMAND_SIZE); - memset(total_string, '\0', MAX_COMMAND_SIZE); - if (strcmp(cmd->buffer, "") == 0) { - sprintf(total_string, "%s%s", PROMPT_HEADER, cmd->command); - } else { - sprintf(total_string, "%s%s", CONTINUE_PROMPT, cmd->command); - } - - int remain_column = w.ws_col; - /* size = cmd->commandSize + prompt_size; */ - for (char *str = total_string; size < cmd->commandSize + prompt_size;) { - int ret = taosMbToWchar(&wc, str, MB_CUR_MAX); - if (ret < 0) break; - size += ret; - /* assert(size >= 0); */ - int width = taosWcharWidth(wc); - if (remain_column > width) { - printf("%lc", wc); - remain_column -= width; - } else { - if (remain_column == width) { - printf("%lc\n\r", wc); - remain_column = w.ws_col; - } else { - printf("\n\r%lc", wc); - remain_column = w.ws_col - width; - } - } - - str = total_string + size; - } - - taosMemoryFree(total_string); - /* for (int i = 0; i < size; i++){ */ - /* char c = total_string[i]; */ - /* if (k % w.ws_col == 0) { */ - /* printf("%c\n\r", c); */ - /* } */ - /* else { */ - /* printf("%c", c); */ - /* } */ - /* k += 1; */ - /* } */ - - // Position the cursor - int cursor_pos = cmd->screenOffset + prompt_size; - int ecmd_pos = cmd->endOffset + prompt_size; - - int cursor_x = cursor_pos / w.ws_col; - int cursor_y = cursor_pos % w.ws_col; - // int cursor_y = cursor % w.ws_col; - int command_x = ecmd_pos / w.ws_col; - int command_y = ecmd_pos % w.ws_col; - // int command_y = (command.size() + prompt_size) % w.ws_col; - positionCursor(command_y, LEFT); - positionCursor(command_x, UP); - positionCursor(cursor_x, DOWN); - positionCursor(cursor_y, RIGHT); - fflush(stdout); -} - -void cleanup_handler(void *arg) { resetTerminalMode(); } - -void exitShell() { - taos_cleanup(); - exit(EXIT_SUCCESS); -} - -void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&cancelSem); } - -void *cancelHandler(void *arg) { - setThreadName("cancelHandler"); - - while (1) { - if (tsem_wait(&cancelSem) != 0) { - taosMsleep(10); - continue; - } - - resetTerminalMode(); - printf("\nReceive ctrl+c or other signal, quit shell.\n"); - exitShell(); - } - - return NULL; -} - -int checkVersion() { - if (sizeof(int8_t) != 1) { - printf("taos int8 size is %d(!= 1)", (int)sizeof(int8_t)); - return 0; - } - if (sizeof(int16_t) != 2) { - printf("taos int16 size is %d(!= 2)", (int)sizeof(int16_t)); - return 0; - } - if (sizeof(int32_t) != 4) { - printf("taos int32 size is %d(!= 4)", (int)sizeof(int32_t)); - return 0; - } - if (sizeof(int64_t) != 8) { - printf("taos int64 size is %d(!= 8)", (int)sizeof(int64_t)); - return 0; - } - return 1; -} - -// Global configurations -SShellArguments args = { - .host = NULL, - .user = NULL, - .database = NULL, - .timezone = NULL, - .is_raw_time = false, - .is_use_passwd = false, - .dump_config = false, - .file = "\0", - .dir = "\0", - .threadNum = 5, - .commands = NULL, - .pktLen = 1000, - .pktNum = 100, - .pktType = "TCP", - .netTestRole = NULL, -#ifndef TD_WINDOWS - .password = NULL, -#endif -}; - -void shellDumpConfig() { - if (!args.dump_config) return; - - SConfig *pCfg = taosGetCfg(); - if (NULL == pCfg) { - printf("TDengine read global config failed!\n"); - exit(EXIT_FAILURE); - } - cfgDumpCfg(pCfg, 0, 1); - exitShell(); -} - -void shellTestNetWork() { - if (args.netTestRole && args.netTestRole[0] != 0) { - taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType); - exitShell(); - } -} - -void shellCheckServerStatus() { - if (!args.status && !args.verbose) return; - - TSDB_SERVER_STATUS code; - do { - char details[1024] = {0}; - code = taos_check_server_status(args.host, args.port, details, args.verbose ? 1024 : 0); - switch (code) { - case TSDB_SRV_STATUS_UNAVAILABLE: - printf("0: unavailable\n"); - break; - case TSDB_SRV_STATUS_NETWORK_OK: - printf("1: network ok\n"); - break; - case TSDB_SRV_STATUS_SERVICE_OK: - printf("2: service ok\n"); - break; - case TSDB_SRV_STATUS_SERVICE_DEGRADED: - printf("3: service degraded\n"); - break; - case TSDB_SRV_STATUS_EXTING: - printf("4: exiting\n"); - break; - } - if (strlen(details) != 0) { - printf("%s\n\n", details); - } - if (code == TSDB_SRV_STATUS_NETWORK_OK && args.verbose) { - taosMsleep(1000); - } else { - break; - } - } while (1); - - exitShell(); -} - -void shellExecute() { - TAOS *con = shellInit(&args); - if (con == NULL) { - exitShell(); - } - - if (tsem_init(&cancelSem, 0, 0) != 0) { - printf("failed to create cancel semphore\n"); - exitShell(); - } - - TdThread spid; - taosThreadCreate(&spid, NULL, cancelHandler, NULL); - - taosSetSignal(SIGTERM, shellQueryInterruptHandler); - taosSetSignal(SIGINT, shellQueryInterruptHandler); - taosSetSignal(SIGHUP, shellQueryInterruptHandler); - taosSetSignal(SIGABRT, shellQueryInterruptHandler); - - shellGetGrantInfo(con); - - while (1) { - taosThreadCreate(&pid, NULL, shellLoopQuery, con); - taosThreadJoin(pid, NULL); - } -} +SShellObj shell = {0}; int main(int argc, char *argv[]) { - if (!checkVersion()) exitShell(); + if (shellCheckIntSize() != 0) { + return -1; + } - shellParseArgument(argc, argv, &args); + if (shellParseArgs(argc, argv) != 0) { + return -1; + } + + if (shell.args.is_version) { + shellPrintVersion(); + return 0; + } + + if (shell.args.is_gen_auth) { + shellGenerateAuth(); + return 0; + } + + if (shell.args.is_help) { + shellPrintHelp(); + return 0; + } taos_init(); - shellDumpConfig(); - shellCheckServerStatus(); - shellTestNetWork(); - shellExecute(); - return 0; + if (shell.args.is_dump_config) { + shellDumpConfig(); + taos_cleanup(); + return 0; + } + + if (shell.args.is_startup || shell.args.is_check) { + shellCheckServerStatus(); + taos_cleanup(); + return 0; + } + + if (shell.args.netrole != NULL) { + shellTestNetWork(); + taos_cleanup(); + return 0; + } + + return shellExecute(); } diff --git a/tools/shell/src/shellNettest.c b/tools/shell/src/shellNettest.c new file mode 100644 index 0000000000..c8ec31c48b --- /dev/null +++ b/tools/shell/src/shellNettest.c @@ -0,0 +1,145 @@ +/* + * 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 _GNU_SOURCE +#include "shellInt.h" + +static void shellWorkAsClient() { + SShellArgs *pArgs = &shell.args; + SRpcInit rpcInit = {0}; + SEpSet epSet = {.inUse = 0, .numOfEps = 1}; + SRpcMsg rpcRsp = {0}; + void *clientRpc = NULL; + char pass[TSDB_PASSWORD_LEN + 1] = {0}; + + taosEncryptPass_c((uint8_t *)("_pwd"), strlen("_pwd"), pass); + rpcInit.label = "CHK"; + rpcInit.numOfThreads = 1; + rpcInit.sessions = 16; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.user = "_dnd"; + rpcInit.ckey = "_key"; + rpcInit.spi = 1; + rpcInit.secret = pass; + + clientRpc = rpcOpen(&rpcInit); + if (clientRpc == NULL) { + printf("failed to init net test client since %s\n", terrstr()); + goto _OVER; + } + + if (pArgs->host == NULL) { + pArgs->host = tsFirst; + } + char fqdn[TSDB_FQDN_LEN] = {0}; + tstrncpy(fqdn, pArgs->host, TSDB_FQDN_LEN); + strtok(fqdn, ":"); + + if (pArgs->port == 0) { + pArgs->port = tsServerPort; + } + + printf("network test client is initialized, the server is %s:%u\n", fqdn, pArgs->port); + + tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN); + epSet.eps[0].port = (uint16_t)pArgs->port; + + int32_t totalSucc = 0; + uint64_t startTime = taosGetTimestampUs(); + + for (int32_t i = 0; i < pArgs->pktNum; ++i) { + SRpcMsg rpcMsg = {.ahandle = (void *)0x9525, .msgType = TDMT_DND_NET_TEST}; + rpcMsg.pCont = rpcMallocCont(pArgs->pktLen); + rpcMsg.contLen = pArgs->pktLen; + + printf("request is sent, size:%d\n", rpcMsg.contLen); + rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp); + if (rpcRsp.code == 0 && rpcRsp.contLen == rpcMsg.contLen) { + printf("response is received, size:%d\n", rpcMsg.contLen); + if (rpcRsp.code == 0) totalSucc++; + } else { + printf("response not received since %s\n", tstrerror(rpcRsp.code)); + } + + rpcFreeCont(rpcRsp.pCont); + rpcRsp.pCont = NULL; + } + + uint64_t endTime = taosGetTimestampUs(); + uint64_t elT = endTime - startTime; + + printf("\ntotal succ:%5d/%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", totalSucc, pArgs->pktNum, elT / 1000.0, + pArgs->pktLen / (elT / 1000000.0) / 1024.0 / 1024.0 * totalSucc); + +_OVER: + if (clientRpc != NULL) { + rpcClose(clientRpc); + } + if (rpcRsp.pCont != NULL) { + rpcFreeCont(rpcRsp.pCont); + } +} + +static void shellProcessMsg(void *p, SRpcMsg *pRpc, SEpSet *pEpSet) { + printf("request is received, size:%d\n", pRpc->contLen); + fflush(stdout); + SRpcMsg rsp = {.handle = pRpc->handle, .refId = pRpc->refId, .ahandle = pRpc->ahandle, .code = 0}; + rsp.pCont = rpcMallocCont(pRpc->contLen); + if (rsp.pCont == NULL) { + rsp.code = TSDB_CODE_OUT_OF_MEMORY; + } else { + rsp.contLen = pRpc->contLen; + } + rpcSendResponse(&rsp); +} + +void shellNettestHandler(int32_t signum, void *sigInfo, void *context) { shellExit(); } + +static void shellWorkAsServer() { + SShellArgs *pArgs = &shell.args; + + if (pArgs->port == 0) { + pArgs->port = tsServerPort; + } + + SRpcInit rpcInit = {0}; + rpcInit.localPort = pArgs->port; + rpcInit.label = "CHK"; + rpcInit.numOfThreads = tsNumOfRpcThreads; + rpcInit.cfp = (RpcCfp)shellProcessMsg; + rpcInit.sessions = 10; + rpcInit.connType = TAOS_CONN_SERVER; + rpcInit.idleTime = tsShellActivityTimer * 1000; + + void *serverRpc = rpcOpen(&rpcInit); + if (serverRpc == NULL) { + printf("failed to init net test server since %s", terrstr()); + } else { + printf("network test server is initialized, port:%u\n", pArgs->port); + taosSetSignal(SIGTERM, shellNettestHandler); + while (1) taosMsleep(10); + } +} + +void shellTestNetWork() { + if (strcmp(shell.args.netrole, "client") == 0) { + shellWorkAsClient(); + } + + if (strcmp(shell.args.netrole, "server") == 0) { + shellWorkAsServer(); + } +} diff --git a/tools/shell/src/shellUtil.c b/tools/shell/src/shellUtil.c new file mode 100644 index 0000000000..1529ac0e52 --- /dev/null +++ b/tools/shell/src/shellUtil.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 _BSD_SOURCE +#define _GNU_SOURCE +#define _XOPEN_SOURCE +#define _DEFAULT_SOURCE + +#include "shellInt.h" + +bool shellRegexMatch(const char *s, const char *reg, int32_t cflags) { + regex_t regex = {0}; + char msgbuf[100] = {0}; + + /* Compile regular expression */ + if (regcomp(®ex, reg, cflags) != 0) { + fprintf(stderr, "Fail to compile regex"); + shellExit(); + } + + /* Execute regular expression */ + int32_t reti = regexec(®ex, s, 0, NULL, 0); + if (!reti) { + regfree(®ex); + return true; + } else if (reti == REG_NOMATCH) { + regfree(®ex); + return false; + } else { + regerror(reti, ®ex, msgbuf, sizeof(msgbuf)); + fprintf(stderr, "Regex match failed: %s\n", msgbuf); + regfree(®ex); + shellExit(); + } + + return false; +} + +int32_t shellCheckIntSize() { + if (sizeof(int8_t) != 1) { + printf("taos int8 size is %d(!= 1)", (int)sizeof(int8_t)); + return -1; + } + if (sizeof(int16_t) != 2) { + printf("taos int16 size is %d(!= 2)", (int)sizeof(int16_t)); + return -1; + } + if (sizeof(int32_t) != 4) { + printf("taos int32 size is %d(!= 4)", (int)sizeof(int32_t)); + return -1; + } + if (sizeof(int64_t) != 8) { + printf("taos int64 size is %d(!= 8)", (int)sizeof(int64_t)); + return -1; + } + return 0; +} + +void shellPrintVersion() { printf("version: %s\n", version); } + +void shellGenerateAuth() { + char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0}; + taosEncryptPass_c((uint8_t *)shell.args.password, strlen(shell.args.password), secretEncrypt); + printf("%s\n", secretEncrypt); + fflush(stdout); +} + +void shellDumpConfig() { + SConfig *pCfg = taosGetCfg(); + if (pCfg == NULL) { + printf("TDengine read global config failed!\n"); + } else { + cfgDumpCfg(pCfg, 1, true); + } + fflush(stdout); +} + +void shellCheckServerStatus() { + TSDB_SERVER_STATUS code; + + do { + char details[1024] = {0}; + code = taos_check_server_status(shell.args.host, shell.args.port, details, 1024); + switch (code) { + case TSDB_SRV_STATUS_UNAVAILABLE: + printf("0: unavailable\n"); + break; + case TSDB_SRV_STATUS_NETWORK_OK: + printf("1: network ok\n"); + break; + case TSDB_SRV_STATUS_SERVICE_OK: + printf("2: service ok\n"); + break; + case TSDB_SRV_STATUS_SERVICE_DEGRADED: + printf("3: service degraded\n"); + break; + case TSDB_SRV_STATUS_EXTING: + printf("4: exiting\n"); + break; + } + if (strlen(details) != 0) { + printf("%s\n\n", details); + } + fflush(stdout); + if (code == TSDB_SRV_STATUS_NETWORK_OK && shell.args.is_startup) { + taosMsleep(1000); + } else { + break; + } + } while (1); +} + +void shellExit() { + if (shell.conn != NULL) { + taos_close(shell.conn); + shell.conn = NULL; + } + taos_cleanup(); + exit(EXIT_FAILURE); +} \ No newline at end of file diff --git a/tools/shell/src/tnettest.c b/tools/shell/src/tnettest.c deleted file mode 100644 index 04b99ebc75..0000000000 --- a/tools/shell/src/tnettest.c +++ /dev/null @@ -1,514 +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 -#define ALLOW_FORBID_FUNC -#include "os.h" -#include "taosdef.h" -#include "tmsg.h" -#include "taoserror.h" -#include "tlog.h" -#include "tglobal.h" -#include "trpc.h" -#include "rpcHead.h" -#include "tchecksum.h" -#include "syncMsg.h" - -#include "osSocket.h" - -#define MAX_PKG_LEN (64 * 1000) -#define MAX_SPEED_PKG_LEN (1024 * 1024 * 1024) -#define MIN_SPEED_PKG_LEN 1024 -#define MAX_SPEED_PKG_NUM 10000 -#define MIN_SPEED_PKG_NUM 1 -#define BUFFER_SIZE (MAX_PKG_LEN + 1024) - -extern int tsRpcMaxUdpSize; - -typedef struct { - char * hostFqdn; - uint32_t hostIp; - int32_t port; - int32_t pktLen; -} STestInfo; - -static void *taosNetBindUdpPort(void *sarg) { - STestInfo *pinfo = (STestInfo *)sarg; - int32_t port = pinfo->port; - SOCKET serverSocket; - char buffer[BUFFER_SIZE]; - int32_t iDataNum; - socklen_t sin_size; - int32_t bufSize = 1024000; - - struct sockaddr_in server_addr; - struct sockaddr_in clientAddr; - - setThreadName("netBindUdpPort"); - - if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - uError("failed to create UDP socket since %s", strerror(errno)); - return NULL; - } - - bzero(&server_addr, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - uError("failed to bind UDP port:%d since %s", port, strerror(errno)); - return NULL; - } - - TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket)); - if (pSocket == NULL) { - taosCloseSocketNoCheck1(serverSocket); - return NULL; - } - pSocket->fd = serverSocket; - pSocket->refId = 0; - - if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the send buffer size for UDP socket\n"); - taosCloseSocket(&pSocket); - return NULL; - } - - if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the receive buffer size for UDP socket\n"); - taosCloseSocket(&pSocket); - return NULL; - } - - uInfo("UDP server at port:%d is listening", port); - - while (1) { - memset(buffer, 0, BUFFER_SIZE); - sin_size = sizeof(*(struct sockaddr *)&server_addr); - iDataNum = recvfrom(serverSocket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&clientAddr, &sin_size); - - if (iDataNum < 0) { - uDebug("failed to perform recvfrom func at %d since %s", port, strerror(errno)); - continue; - } - - uInfo("UDP: recv:%d bytes from %s at %d", iDataNum, taosInetNtoa(clientAddr.sin_addr), port); - - if (iDataNum > 0) { - iDataNum = taosSendto(pSocket, buffer, iDataNum, 0, (struct sockaddr *)&clientAddr, (int32_t)sin_size); - } - - uInfo("UDP: send:%d bytes to %s at %d", iDataNum, taosInetNtoa(clientAddr.sin_addr), port); - } - - taosCloseSocket(&pSocket); - return NULL; -} - -static void *taosNetBindTcpPort(void *sarg) { - struct sockaddr_in server_addr; - struct sockaddr_in clientAddr; - - STestInfo *pinfo = sarg; - int32_t port = pinfo->port; - SOCKET serverSocket; - int32_t addr_len = sizeof(clientAddr); - SOCKET client; - char buffer[BUFFER_SIZE]; - - setThreadName("netBindTcpPort"); - - if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - uError("failed to create TCP socket since %s", strerror(errno)); - return NULL; - } - - bzero(&server_addr, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - int32_t reuse = 1; - TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket)); - if (pSocket == NULL) { - taosCloseSocketNoCheck1(serverSocket); - return NULL; - } - pSocket->fd = serverSocket; - pSocket->refId = 0; - - if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) { - uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno)); - taosCloseSocket(&pSocket); - return NULL; - } - - if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - uError("failed to bind TCP port:%d since %s", port, strerror(errno)); - taosCloseSocket(&pSocket); - return NULL; - } - - if (taosKeepTcpAlive(pSocket) < 0) { - uError("failed to set tcp server keep-alive option since %s", strerror(errno)); - taosCloseSocket(&pSocket); - return NULL; - } - - if (listen(serverSocket, 10) < 0) { - uError("failed to listen TCP port:%d since %s", port, strerror(errno)); - taosCloseSocket(&pSocket); - return NULL; - } - - uInfo("TCP server at port:%d is listening", port); - - while (1) { - client = accept(serverSocket, (struct sockaddr *)&clientAddr, (socklen_t *)&addr_len); - if (client < 0) { - uDebug("TCP: failed to accept at port:%d since %s", port, strerror(errno)); - continue; - } - - int32_t ret = taosReadMsg(pSocket, buffer, pinfo->pktLen); - if (ret < 0 || ret != pinfo->pktLen) { - uError("TCP: failed to read %d bytes at port:%d since %s", pinfo->pktLen, port, strerror(errno)); - taosCloseSocket(&pSocket); - return NULL; - } - - uInfo("TCP: read:%d bytes from %s at %d", pinfo->pktLen, taosInetNtoa(clientAddr.sin_addr), port); - - ret = taosWriteMsg(pSocket, buffer, pinfo->pktLen); - if (ret < 0) { - uError("TCP: failed to write %d bytes at %d since %s", pinfo->pktLen, port, strerror(errno)); - taosCloseSocket(&pSocket); - return NULL; - } - - uInfo("TCP: write:%d bytes to %s at %d", pinfo->pktLen, taosInetNtoa(clientAddr.sin_addr), port); - } - - taosCloseSocket(&pSocket); - return NULL; -} - -static int32_t taosNetCheckTcpPort(STestInfo *info) { - SOCKET clientSocket; - char buffer[BUFFER_SIZE] = {0}; - - if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - uError("failed to create TCP client socket since %s", strerror(errno)); - return -1; - } - - int32_t reuse = 1; - TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket)); - if (pSocket == NULL) { - taosCloseSocketNoCheck1(clientSocket); - return -1; - } - pSocket->fd = clientSocket; - pSocket->refId = 0; - - if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) { - uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno)); - taosCloseSocket(&pSocket); - return -1; - } - - struct sockaddr_in serverAddr; - memset((char *)&serverAddr, 0, sizeof(serverAddr)); - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = (uint16_t)htons((uint16_t)info->port); - serverAddr.sin_addr.s_addr = info->hostIp; - - if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { - uError("TCP: failed to connect port %s:%d since %s", taosIpStr(info->hostIp), info->port, strerror(errno)); - taosCloseSocket(&pSocket); - return -1; - } - - taosKeepTcpAlive(pSocket); - - sprintf(buffer, "client send TCP pkg to %s:%d, content: 1122334455", taosIpStr(info->hostIp), info->port); - sprintf(buffer + info->pktLen - 16, "1122334455667788"); - - int32_t ret = taosWriteMsg(pSocket, buffer, info->pktLen); - if (ret < 0) { - uError("TCP: failed to write msg to %s:%d since %s", taosIpStr(info->hostIp), info->port, strerror(errno)); - taosCloseSocket(&pSocket); - return -1; - } - - ret = taosReadMsg(pSocket, buffer, info->pktLen); - if (ret < 0) { - uError("TCP: failed to read msg from %s:%d since %s", taosIpStr(info->hostIp), info->port, strerror(errno)); - taosCloseSocket(&pSocket); - return -1; - } - - taosCloseSocket(&pSocket); - return 0; -} - -static int32_t taosNetCheckUdpPort(STestInfo *info) { - SOCKET clientSocket; - char buffer[BUFFER_SIZE] = {0}; - int32_t iDataNum = 0; - int32_t bufSize = 1024000; - - struct sockaddr_in serverAddr; - - if ((clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - uError("failed to create udp client socket since %s", strerror(errno)); - return -1; - } - - TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket)); - if (pSocket == NULL) { - taosCloseSocketNoCheck1(clientSocket); - return -1; - } - pSocket->fd = clientSocket; - pSocket->refId = 0; - - if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the send buffer size for UDP socket\n"); - taosCloseSocket(&pSocket); - return -1; - } - - if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) { - uError("failed to set the receive buffer size for UDP socket\n"); - taosCloseSocket(&pSocket); - return -1; - } - - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(info->port); - serverAddr.sin_addr.s_addr = info->hostIp; - - struct in_addr ipStr; - memcpy(&ipStr, &info->hostIp, 4); - sprintf(buffer, "client send udp pkg to %s:%d, content: 1122334455", taosInetNtoa(ipStr), info->port); - sprintf(buffer + info->pktLen - 16, "1122334455667788"); - - socklen_t sin_size = sizeof(*(struct sockaddr *)&serverAddr); - - iDataNum = taosSendto(pSocket, buffer, info->pktLen, 0, (struct sockaddr *)&serverAddr, (int32_t)sin_size); - if (iDataNum < 0 || iDataNum != info->pktLen) { - uError("UDP: failed to perform sendto func since %s", strerror(errno)); - taosCloseSocket(&pSocket); - return -1; - } - - memset(buffer, 0, BUFFER_SIZE); - sin_size = sizeof(*(struct sockaddr *)&serverAddr); - iDataNum = recvfrom(clientSocket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&serverAddr, &sin_size); - - if (iDataNum < 0 || iDataNum != info->pktLen) { - uError("UDP: received ack:%d bytes(expect:%d) from port:%d since %s", iDataNum, info->pktLen, info->port, strerror(errno)); - taosCloseSocket(&pSocket); - return -1; - } - - taosCloseSocket(&pSocket); - return 0; -} - -static void taosNetCheckPort(uint32_t hostIp, int32_t startPort, int32_t endPort, int32_t pktLen) { - int32_t ret; - STestInfo info; - - memset(&info, 0, sizeof(STestInfo)); - info.hostIp = hostIp; - info.pktLen = pktLen; - - for (int32_t port = startPort; port <= endPort; port++) { - info.port = port; - ret = taosNetCheckTcpPort(&info); - if (ret != 0) { - printf("failed to test TCP port:%d\n", port); - } else { - printf("successed to test TCP port:%d\n", port); - } - - ret = taosNetCheckUdpPort(&info); - if (ret != 0) { - printf("failed to test UDP port:%d\n", port); - } else { - printf("successed to test UDP port:%d\n", port); - } - } -} - -static void taosNetTestClient(char *host, int32_t startPort, int32_t pkgLen) { - uInfo("work as client, host:%s Port:%d pkgLen:%d\n", host, startPort, pkgLen); - - uint32_t serverIp = taosGetIpv4FromFqdn(host); - if (serverIp == 0xFFFFFFFF) { - uError("failed to resolve fqdn:%s", host); - exit(-1); - } - - uInfo("server ip:%s is resolved from host:%s", taosIpStr(serverIp), host); - taosNetCheckPort(serverIp, startPort, startPort, pkgLen); -} - -static void taosNetTestServer(char *host, int32_t startPort, int32_t pkgLen) { - uInfo("work as server, host:%s Port:%d pkgLen:%d\n", host, startPort, pkgLen); - - int32_t port = startPort; - int32_t num = 1; - if (num < 0) num = 1; - - TdThread *pids = taosMemoryMalloc(2 * num * sizeof(TdThread)); - STestInfo *tinfos = taosMemoryMalloc(num * sizeof(STestInfo)); - STestInfo *uinfos = taosMemoryMalloc(num * sizeof(STestInfo)); - - for (int32_t i = 0; i < num; i++) { - STestInfo *tcpInfo = tinfos + i; - tcpInfo->port = port + i; - tcpInfo->pktLen = pkgLen; - - if (taosThreadCreate(pids + i, NULL, taosNetBindTcpPort, tcpInfo) != 0) { - uInfo("failed to create TCP test thread, %s:%d", tcpInfo->hostFqdn, tcpInfo->port); - exit(-1); - } - - STestInfo *udpInfo = uinfos + i; - udpInfo->port = port + i; - tcpInfo->pktLen = pkgLen; - if (taosThreadCreate(pids + num + i, NULL, taosNetBindUdpPort, udpInfo) != 0) { - uInfo("failed to create UDP test thread, %s:%d", tcpInfo->hostFqdn, tcpInfo->port); - exit(-1); - } - } - - for (int32_t i = 0; i < num; i++) { - taosThreadJoin(pids[i], NULL); - taosThreadJoin(pids[(num + i)], NULL); - } -} - -static void taosNetCheckSpeed(char *host, int32_t port, int32_t pkgLen, - int32_t pkgNum, char *pkgType) { -#if 0 - - // record config - int32_t compressTmp = tsCompressMsgSize; - int32_t maxUdpSize = tsRpcMaxUdpSize; - int32_t forceTcp = tsRpcForceTcp; - - if (0 == strcmp("tcp", pkgType)){ - tsRpcForceTcp = 1; - tsRpcMaxUdpSize = 0; // force tcp - } else { - tsRpcForceTcp = 0; - tsRpcMaxUdpSize = INT_MAX; - } - tsCompressMsgSize = -1; - - SEpSet epSet; - SRpcMsg reqMsg; - SRpcMsg rspMsg; - void * pRpcConn; - char secretEncrypt[32] = {0}; - char spi = 0; - pRpcConn = taosNetInitRpc(secretEncrypt, spi); - if (NULL == pRpcConn) { - uError("failed to init client rpc"); - return; - } - - printf("check net spend, host:%s port:%d pkgLen:%d pkgNum:%d pkgType:%s\n\n", host, port, pkgLen, pkgNum, pkgType); - int32_t totalSucc = 0; - uint64_t startT = taosGetTimestampUs(); - for (int32_t i = 1; i <= pkgNum; i++) { - uint64_t startTime = taosGetTimestampUs(); - - memset(&epSet, 0, sizeof(SEpSet)); - strcpy(epSet.eps[0].fqdn, host); - epSet.eps[0].port = port; - epSet.numOfEps = 1; - - reqMsg.msgType = TDMT_DND_NETWORK_TEST; - reqMsg.pCont = rpcMallocCont(pkgLen); - reqMsg.contLen = pkgLen; - reqMsg.code = 0; - reqMsg.handle = NULL; // rpc handle returned to app - reqMsg.ahandle = NULL; // app handle set by client - strcpy(reqMsg.pCont, "nettest speed"); - - rpcSendRecv(pRpcConn, &epSet, &reqMsg, &rspMsg); - - int code = 0; - if ((rspMsg.code != 0) || (rspMsg.msgType != TDMT_DND_NETWORK_TEST + 1)) { - uError("ret code 0x%x %s", rspMsg.code, tstrerror(rspMsg.code)); - code = -1; - }else{ - totalSucc ++; - } - - rpcFreeCont(rspMsg.pCont); - - uint64_t endTime = taosGetTimestampUs(); - uint64_t el = endTime - startTime; - printf("progress:%5d/%d\tstatus:%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", i, pkgNum, code, el/1000.0, pkgLen/(el/1000000.0)/1024.0/1024.0); - } - int64_t endT = taosGetTimestampUs(); - uint64_t elT = endT - startT; - printf("\ntotal succ:%5d/%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", totalSucc, pkgNum, elT/1000.0, pkgLen/(elT/1000000.0)/1024.0/1024.0*totalSucc); - - rpcClose(pRpcConn); - - // return config - tsCompressMsgSize = compressTmp; - tsRpcMaxUdpSize = maxUdpSize; - tsRpcForceTcp = forceTcp; - return; -#endif -} - -void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, int32_t pkgNum, char *pkgType) { - tsLogEmbedded = 1; - if (host == NULL) host = tsLocalFqdn; - if (port == 0) port = tsServerPort; - if (0 == strcmp("speed", role)) { - if (pkgLen <= MIN_SPEED_PKG_LEN) pkgLen = MIN_SPEED_PKG_LEN; - if (pkgLen > MAX_SPEED_PKG_LEN) pkgLen = MAX_SPEED_PKG_LEN; - if (pkgNum <= MIN_SPEED_PKG_NUM) pkgNum = MIN_SPEED_PKG_NUM; - if (pkgNum > MAX_SPEED_PKG_NUM) pkgNum = MAX_SPEED_PKG_NUM; - } else { - if (pkgLen <= 10) pkgLen = 1000; - if (pkgLen > MAX_PKG_LEN) pkgLen = MAX_PKG_LEN; - } - - if (0 == strcmp("client", role)) { - taosNetTestClient(host, port, pkgLen); - } else if (0 == strcmp("server", role)) { - taosNetTestServer(host, port, pkgLen); - } else if (0 == strcmp("speed", role)) { - tsLogEmbedded = 0; - char type[10] = {0}; - taosNetCheckSpeed(host, port, pkgLen, pkgNum, strtolower(type, pkgType)); - } else { - TASSERT(1); - } - - tsLogEmbedded = 0; -} From 06a370c8debee0b094495b83b32a28702ce09da5 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 25 Apr 2022 10:42:17 +0800 Subject: [PATCH 26/51] enh(tmq): remove topic out of db --- example/src/tmq.c | 8 +- include/common/tmsg.h | 16 ++-- source/client/src/tmq.c | 9 +- source/dnode/mnode/impl/src/mndTopic.c | 10 +- source/libs/parser/src/parTranslater.c | 127 ++++++++++++++----------- 5 files changed, 97 insertions(+), 73 deletions(-) diff --git a/example/src/tmq.c b/example/src/tmq.c index 2abf915fd8..b2cbf856c0 100644 --- a/example/src/tmq.c +++ b/example/src/tmq.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "taos.h" static int running = 1; @@ -47,6 +48,7 @@ int32_t init_env() { return -1; } taos_free_result(pRes); + sleep(1); pRes = taos_query(pConn, "use abc1"); if (taos_errno(pRes) != 0) { @@ -58,6 +60,7 @@ int32_t init_env() { pRes = taos_query(pConn, "create stable if not exists st1 (ts timestamp, c1 int, c2 float, c3 binary(10)) tags(t1 int)"); if (taos_errno(pRes) != 0) { + assert(0); printf("failed to create super table st1, reason:%s\n", taos_errstr(pRes)); return -1; } @@ -265,10 +268,11 @@ void perf_loop(tmq_t* tmq, tmq_list_t* topics) { } int main(int argc, char* argv[]) { - int code; if (argc > 1) { printf("env init\n"); - code = init_env(); + if (init_env() < 0) { + return -1; + } create_topic(); } tmq_t* tmq = build_consumer(); diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 75a59a7f75..5a2dd502c1 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1314,7 +1314,6 @@ typedef struct { } SMqConsumerLostMsg; typedef struct { - int32_t topicNum; int64_t consumerId; char cgroup[TSDB_CGROUP_LEN]; SArray* topicNames; // SArray @@ -1322,22 +1321,27 @@ typedef struct { static FORCE_INLINE int32_t tSerializeSCMSubscribeReq(void** buf, const SCMSubscribeReq* pReq) { int32_t tlen = 0; - tlen += taosEncodeFixedI32(buf, pReq->topicNum); tlen += taosEncodeFixedI64(buf, pReq->consumerId); tlen += taosEncodeString(buf, pReq->cgroup); - for (int32_t i = 0; i < pReq->topicNum; i++) { + int32_t topicNum = taosArrayGetSize(pReq->topicNames); + tlen += taosEncodeFixedI32(buf, topicNum); + + for (int32_t i = 0; i < topicNum; i++) { tlen += taosEncodeString(buf, (char*)taosArrayGetP(pReq->topicNames, i)); } return tlen; } static FORCE_INLINE void* tDeserializeSCMSubscribeReq(void* buf, SCMSubscribeReq* pReq) { - buf = taosDecodeFixedI32(buf, &pReq->topicNum); buf = taosDecodeFixedI64(buf, &pReq->consumerId); buf = taosDecodeStringTo(buf, pReq->cgroup); - pReq->topicNames = taosArrayInit(pReq->topicNum, sizeof(void*)); - for (int32_t i = 0; i < pReq->topicNum; i++) { + + int32_t topicNum; + buf = taosDecodeFixedI32(buf, &topicNum); + + pReq->topicNames = taosArrayInit(topicNum, sizeof(void*)); + for (int32_t i = 0; i < topicNum; i++) { char* name; buf = taosDecodeString(buf, &name); taosArrayPush(pReq->topicNames, &name); diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index 2ccd88eea8..d576f28cb4 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -506,7 +506,6 @@ tmq_resp_err_t tmq_subscribe(tmq_t* tmq, tmq_list_t* topic_list) { tmq->clientTopics = taosArrayInit(sz, sizeof(SMqClientTopic)); SCMSubscribeReq req; - req.topicNum = sz; req.consumerId = tmq->consumerId; strcpy(req.cgroup, tmq->groupId); req.topicNames = taosArrayInit(sz, sizeof(void*)); @@ -516,12 +515,16 @@ tmq_resp_err_t tmq_subscribe(tmq_t* tmq, tmq_list_t* topic_list) { char* topicName = taosArrayGetP(container, i); SName name = {0}; +#if 0 char* dbName = getDbOfConnection(tmq->pTscObj); if (dbName == NULL) { return TMQ_RESP_ERR__FAIL; } - tNameSetDbName(&name, tmq->pTscObj->acctId, dbName, strlen(dbName)); +#endif + tNameSetDbName(&name, tmq->pTscObj->acctId, topicName, strlen(topicName)); +#if 0 tNameFromString(&name, topicName, T_NAME_TABLE); +#endif char* topicFname = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN); if (topicFname == NULL) { @@ -539,7 +542,9 @@ tmq_resp_err_t tmq_subscribe(tmq_t* tmq, tmq_list_t* topic_list) { topic.vgs = taosArrayInit(0, sizeof(SMqClientVg)); taosArrayPush(tmq->clientTopics, &topic); taosArrayPush(req.topicNames, &topicFname); +#if 0 taosMemoryFree(dbName); +#endif } int tlen = tSerializeSCMSubscribeReq(NULL, &req); diff --git a/source/dnode/mnode/impl/src/mndTopic.c b/source/dnode/mnode/impl/src/mndTopic.c index 1d9b55f7e5..faddc3a1c5 100644 --- a/source/dnode/mnode/impl/src/mndTopic.c +++ b/source/dnode/mnode/impl/src/mndTopic.c @@ -232,6 +232,7 @@ void mndReleaseTopic(SMnode *pMnode, SMqTopicObj *pTopic) { sdbRelease(pSdb, pTopic); } +#if 0 static SDbObj *mndAcquireDbByTopic(SMnode *pMnode, char *topicName) { SName name = {0}; tNameFromString(&name, topicName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); @@ -241,6 +242,7 @@ static SDbObj *mndAcquireDbByTopic(SMnode *pMnode, char *topicName) { return mndAcquireDb(pMnode, db); } +#endif static SDDropTopicReq *mndBuildDropTopicMsg(SMnode *pMnode, SVgObj *pVgroup, SMqTopicObj *pTopic) { int32_t contLen = sizeof(SDDropTopicReq); @@ -260,15 +262,11 @@ static SDDropTopicReq *mndBuildDropTopicMsg(SMnode *pMnode, SVgObj *pVgroup, SMq } static int32_t mndCheckCreateTopicReq(SCMCreateTopicReq *pCreate) { - if (pCreate->name[0] == 0 || pCreate->sql == NULL || pCreate->sql[0] == 0) { + if (pCreate->name[0] == 0 || pCreate->sql == NULL || pCreate->sql[0] == 0 || pCreate->subscribeDbName[0] == 0) { terrno = TSDB_CODE_MND_INVALID_TOPIC_OPTION; return -1; } - if ((pCreate->ast == NULL || pCreate->ast[0] == 0) && pCreate->subscribeDbName[0] == 0) { - terrno = TSDB_CODE_MND_INVALID_TOPIC_OPTION; - return -1; - } return 0; } @@ -384,7 +382,7 @@ static int32_t mndProcessCreateTopicReq(SNodeMsg *pReq) { goto CREATE_TOPIC_OVER; } - pDb = mndAcquireDbByTopic(pMnode, createTopicReq.name); + pDb = mndAcquireDb(pMnode, createTopicReq.subscribeDbName); if (pDb == NULL) { terrno = TSDB_CODE_MND_DB_NOT_SELECTED; goto CREATE_TOPIC_OVER; diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index a3f8c3aed5..31cc629e0a 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -198,7 +198,7 @@ static int32_t getDBVgVersion(STranslateContext* pCxt, const char* pDbFName, int static int32_t getDBCfg(STranslateContext* pCxt, const char* pDbName, SDbCfgInfo* pInfo) { SParseContext* pParCxt = pCxt->pParseCxt; - SName name; + SName name; tNameSetDbName(&name, pCxt->pParseCxt->acctId, pDbName, strlen(pDbName)); char dbFname[TSDB_DB_FNAME_LEN] = {0}; tNameGetFullDbName(&name, dbFname); @@ -560,8 +560,7 @@ static EDealRes translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) { pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes; } } else if (nodesIsComparisonOp(pOp)) { - if (TSDB_DATA_TYPE_BLOB == ldt.type || TSDB_DATA_TYPE_JSON == rdt.type || - TSDB_DATA_TYPE_BLOB == rdt.type) { + if (TSDB_DATA_TYPE_BLOB == ldt.type || TSDB_DATA_TYPE_JSON == rdt.type || TSDB_DATA_TYPE_BLOB == rdt.type) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName); } if (OP_TYPE_IN == pOp->opType || OP_TYPE_NOT_IN == pOp->opType) { @@ -569,7 +568,7 @@ static EDealRes translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) { } pOp->node.resType.type = TSDB_DATA_TYPE_BOOL; pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes; - } else if (nodesIsJsonOp(pOp)){ + } else if (nodesIsJsonOp(pOp)) { if (TSDB_DATA_TYPE_JSON != ldt.type || TSDB_DATA_TYPE_BINARY != rdt.type) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName); } @@ -588,7 +587,9 @@ static EDealRes haveAggFunction(SNode* pNode, void* pContext) { } static EDealRes translateFunction(STranslateContext* pCxt, SFunctionNode* pFunc) { - SFmGetFuncInfoParam param = { .pCtg = pCxt->pParseCxt->pCatalog, .pRpc = pCxt->pParseCxt->pTransporter, .pMgmtEps = &pCxt->pParseCxt->mgmtEpSet}; + SFmGetFuncInfoParam param = {.pCtg = pCxt->pParseCxt->pCatalog, + .pRpc = pCxt->pParseCxt->pTransporter, + .pMgmtEps = &pCxt->pParseCxt->mgmtEpSet}; if (TSDB_CODE_SUCCESS != fmGetFuncInfo(¶m, pFunc->functionName, &pFunc->funcId, &pFunc->funcType)) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_FUNTION, pFunc->functionName); } @@ -1268,7 +1269,7 @@ static int32_t checkIntervalWindow(STranslateContext* pCxt, SIntervalWindowNode* static EDealRes checkStateExpr(SNode* pNode, void* pContext) { if (QUERY_NODE_COLUMN == nodeType(pNode)) { STranslateContext* pCxt = pContext; - SColumnNode* pCol = (SColumnNode*)pNode; + SColumnNode* pCol = (SColumnNode*)pNode; if (!IS_INTEGER_TYPE(pCol->node.resType.type)) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE); } @@ -1345,7 +1346,8 @@ static int32_t translateFrom(STranslateContext* pCxt, SSelectStmt* pSelect) { } static int32_t checkLimit(STranslateContext* pCxt, SSelectStmt* pSelect) { - if ((NULL != pSelect->pLimit && pSelect->pLimit->offset < 0) || (NULL != pSelect->pSlimit && pSelect->pSlimit->offset < 0)) { + if ((NULL != pSelect->pLimit && pSelect->pLimit->offset < 0) || + (NULL != pSelect->pSlimit && pSelect->pSlimit->offset < 0)) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OFFSET_LESS_ZERO); } @@ -1438,7 +1440,7 @@ static int32_t translateSetOperatorImpl(STranslateContext* pCxt, SSetOperator* p SExprNode* pLeftExpr = (SExprNode*)pLeft; SExprNode* pRightExpr = (SExprNode*)pRight; if (!dataTypeEqual(&pLeftExpr->resType, &pRightExpr->resType)) { - SNode* pRightFunc = NULL; + SNode* pRightFunc = NULL; int32_t code = createCastFunc(pCxt, pRight, pLeftExpr->resType, &pRightFunc); if (TSDB_CODE_SUCCESS != code) { return code; @@ -1650,7 +1652,8 @@ static int32_t checkKeepOption(STranslateContext* pCxt, SNodeList* pKeep) { (TIME_UNIT_MINUTE != pKeep1->unit && TIME_UNIT_HOUR != pKeep1->unit && TIME_UNIT_DAY != pKeep1->unit)) || (pKeep2->isDuration && (TIME_UNIT_MINUTE != pKeep2->unit && TIME_UNIT_HOUR != pKeep2->unit && TIME_UNIT_DAY != pKeep2->unit))) { - return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_UNIT, pKeep0->unit, pKeep1->unit, pKeep2->unit); + return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_UNIT, pKeep0->unit, pKeep1->unit, + pKeep2->unit); } int32_t daysToKeep0 = getBigintFromValueNode(pKeep0); @@ -1659,7 +1662,7 @@ static int32_t checkKeepOption(STranslateContext* pCxt, SNodeList* pKeep) { if (daysToKeep0 < TSDB_MIN_KEEP || daysToKeep1 < TSDB_MIN_KEEP || daysToKeep2 < TSDB_MIN_KEEP || daysToKeep0 > TSDB_MAX_KEEP || daysToKeep1 > TSDB_MAX_KEEP || daysToKeep2 > TSDB_MAX_KEEP) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_VALUE, daysToKeep0, daysToKeep1, daysToKeep2, - TSDB_MIN_KEEP, TSDB_MAX_KEEP); + TSDB_MIN_KEEP, TSDB_MAX_KEEP); } if (!((daysToKeep0 <= daysToKeep1) && (daysToKeep1 <= daysToKeep2))) { @@ -1691,7 +1694,8 @@ static int32_t checkDbRetentionsOption(STranslateContext* pCxt, SNodeList* pRete return TSDB_CODE_SUCCESS; } -static int32_t checkOptionsDependency(STranslateContext* pCxt, const char* pDbName, SDatabaseOptions* pOptions, bool alter) { +static int32_t checkOptionsDependency(STranslateContext* pCxt, const char* pDbName, SDatabaseOptions* pOptions, + bool alter) { if (NULL == pOptions->pDaysPerFile && NULL == pOptions->pKeep) { return TSDB_CODE_SUCCESS; } @@ -1699,7 +1703,7 @@ static int32_t checkOptionsDependency(STranslateContext* pCxt, const char* pDbNa int64_t daysToKeep0 = GET_OPTION_VAL(nodesListGetNode(pOptions->pKeep, 0), alter ? -1 : TSDB_DEFAULT_KEEP); if (alter && (-1 == daysPerFile || -1 == daysToKeep0)) { SDbCfgInfo dbCfg; - int32_t code = getDBCfg(pCxt, pDbName, &dbCfg); + int32_t code = getDBCfg(pCxt, pDbName, &dbCfg); if (TSDB_CODE_SUCCESS != code) { return code; } @@ -1712,7 +1716,8 @@ static int32_t checkOptionsDependency(STranslateContext* pCxt, const char* pDbNa return TSDB_CODE_SUCCESS; } -static int32_t checkDatabaseOptions(STranslateContext* pCxt, const char* pDbName, SDatabaseOptions* pOptions, bool alter) { +static int32_t checkDatabaseOptions(STranslateContext* pCxt, const char* pDbName, SDatabaseOptions* pOptions, + bool alter) { int32_t code = checkRangeOption(pCxt, "totalBlocks", pOptions->pNumOfBlocks, TSDB_MIN_TOTAL_BLOCKS, TSDB_MAX_TOTAL_BLOCKS); if (TSDB_CODE_SUCCESS == code) { @@ -1918,7 +1923,7 @@ static int32_t checTableFactorOption(STranslateContext* pCxt, SValueNode* pVal) } if (pVal->datum.d < TSDB_MIN_DB_FILE_FACTOR || pVal->datum.d > TSDB_MAX_DB_FILE_FACTOR) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_F_RANGE_OPTION, "file_factor", pVal->datum.d, - TSDB_MIN_DB_FILE_FACTOR, TSDB_MAX_DB_FILE_FACTOR); + TSDB_MIN_DB_FILE_FACTOR, TSDB_MAX_DB_FILE_FACTOR); } } return TSDB_CODE_SUCCESS; @@ -1945,7 +1950,7 @@ static int32_t checkTableTags(STranslateContext* pCxt, SCreateTableStmt* pStmt) SNode* pNode; FOREACH(pNode, pStmt->pTags) { SColumnDefNode* pCol = (SColumnDefNode*)pNode; - if(pCol->dataType.type == TSDB_DATA_TYPE_JSON && LIST_LENGTH(pStmt->pTags) > 1){ + if (pCol->dataType.type == TSDB_DATA_TYPE_JSON && LIST_LENGTH(pStmt->pTags) > 1) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ONLY_ONE_JSON_TAG); } } @@ -1960,8 +1965,10 @@ static int32_t checkTableRollupOption(STranslateContext* pCxt, SNodeList* pFuncs if (1 != LIST_LENGTH(pFuncs)) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ROLLUP_OPTION); } - SFunctionNode* pFunc = nodesListGetNode(pFuncs, 0); - SFmGetFuncInfoParam param = { .pCtg = pCxt->pParseCxt->pCatalog, .pRpc = pCxt->pParseCxt->pTransporter, .pMgmtEps = &pCxt->pParseCxt->mgmtEpSet}; + SFunctionNode* pFunc = nodesListGetNode(pFuncs, 0); + SFmGetFuncInfoParam param = {.pCtg = pCxt->pParseCxt->pCatalog, + .pRpc = pCxt->pParseCxt->pTransporter, + .pMgmtEps = &pCxt->pParseCxt->mgmtEpSet}; if (TSDB_CODE_SUCCESS != fmGetFuncInfo(¶m, pFunc->functionName, &pFunc->funcId, &pFunc->funcType)) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_FUNTION, pFunc->functionName); } @@ -2016,10 +2023,10 @@ static void toSchema(const SColumnDefNode* pCol, col_id_t colId, SSchema* pSchem typedef struct SSampleAstInfo { const char* pDbName; const char* pTableName; - SNodeList* pFuncs; - SNode* pInterval; - SNode* pOffset; - SNode* pSliding; + SNodeList* pFuncs; + SNode* pInterval; + SNode* pOffset; + SNode* pSliding; STableMeta* pRollupTableMeta; } SSampleAstInfo; @@ -2089,8 +2096,8 @@ static SNode* makeIntervalVal(SRetention* pRetension, int8_t precision) { return NULL; } int64_t timeVal = convertTimeFromPrecisionToUnit(pRetension->freq, precision, pRetension->freqUnit); - char buf[20] = {0}; - int32_t len = snprintf(buf, sizeof(buf), "%"PRId64"%c", timeVal, pRetension->freqUnit); + char buf[20] = {0}; + int32_t len = snprintf(buf, sizeof(buf), "%" PRId64 "%c", timeVal, pRetension->freqUnit); pVal->literal = strndup(buf, len); if (NULL == pVal->literal) { nodesDestroyNode(pVal); @@ -2133,7 +2140,7 @@ static SNodeList* createRollupFuncs(SCreateTableStmt* pStmt) { SNode* pFunc = NULL; FOREACH(pFunc, pStmt->pOptions->pFuncs) { SNode* pCol = NULL; - bool primaryKey = true; + bool primaryKey = true; FOREACH(pCol, pStmt->pCols) { if (primaryKey) { primaryKey = false; @@ -2150,7 +2157,7 @@ static SNodeList* createRollupFuncs(SCreateTableStmt* pStmt) { } static STableMeta* createRollupTableMeta(SCreateTableStmt* pStmt, int8_t precision) { - int32_t numOfField = LIST_LENGTH(pStmt->pCols) + LIST_LENGTH(pStmt->pTags); + int32_t numOfField = LIST_LENGTH(pStmt->pCols) + LIST_LENGTH(pStmt->pTags); STableMeta* pMeta = taosMemoryCalloc(1, sizeof(STableMeta) + numOfField * sizeof(SSchema)); if (NULL == pMeta) { return NULL; @@ -2161,7 +2168,7 @@ static STableMeta* createRollupTableMeta(SCreateTableStmt* pStmt, int8_t precisi pMeta->tableInfo.numOfColumns = LIST_LENGTH(pStmt->pCols); int32_t index = 0; - SNode* pCol = NULL; + SNode* pCol = NULL; FOREACH(pCol, pStmt->pCols) { toSchema((SColumnDefNode*)pCol, index + 1, pMeta->schema + index); ++index; @@ -2175,8 +2182,8 @@ static STableMeta* createRollupTableMeta(SCreateTableStmt* pStmt, int8_t precisi return pMeta; } -static int32_t buildSampleAstInfoByTable(STranslateContext* pCxt, - SCreateTableStmt* pStmt, SRetention* pRetension, int8_t precision, SSampleAstInfo* pInfo) { +static int32_t buildSampleAstInfoByTable(STranslateContext* pCxt, SCreateTableStmt* pStmt, SRetention* pRetension, + int8_t precision, SSampleAstInfo* pInfo) { pInfo->pDbName = pStmt->dbName; pInfo->pTableName = pStmt->tableName; pInfo->pFuncs = createRollupFuncs(pStmt); @@ -2188,10 +2195,10 @@ static int32_t buildSampleAstInfoByTable(STranslateContext* pCxt, return TSDB_CODE_SUCCESS; } -static int32_t getRollupAst(STranslateContext* pCxt, - SCreateTableStmt* pStmt, SRetention* pRetension, int8_t precision, char** pAst, int32_t* pLen) { +static int32_t getRollupAst(STranslateContext* pCxt, SCreateTableStmt* pStmt, SRetention* pRetension, int8_t precision, + char** pAst, int32_t* pLen) { SSampleAstInfo info = {0}; - int32_t code = buildSampleAstInfoByTable(pCxt, pStmt, pRetension, precision, &info); + int32_t code = buildSampleAstInfoByTable(pCxt, pStmt, pRetension, precision, &info); if (TSDB_CODE_SUCCESS == code) { code = buildSampleAst(pCxt, &info, pAst, pLen); } @@ -2201,17 +2208,17 @@ static int32_t getRollupAst(STranslateContext* pCxt, static int32_t buildRollupAst(STranslateContext* pCxt, SCreateTableStmt* pStmt, SMCreateStbReq* pReq) { SDbCfgInfo dbCfg = {0}; - int32_t code = getDBCfg(pCxt, pStmt->dbName, &dbCfg); - int32_t num = taosArrayGetSize(dbCfg.pRetensions); + int32_t code = getDBCfg(pCxt, pStmt->dbName, &dbCfg); + int32_t num = taosArrayGetSize(dbCfg.pRetensions); if (TSDB_CODE_SUCCESS != code || num < 2) { return code; } for (int32_t i = 1; i < num; ++i) { - SRetention *pRetension = taosArrayGet(dbCfg.pRetensions, i); + SRetention* pRetension = taosArrayGet(dbCfg.pRetensions, i); STranslateContext cxt = {0}; initTranslateContext(pCxt->pParseCxt, &cxt); - code = getRollupAst(&cxt, pStmt, pRetension, dbCfg.precision, - 1 == i ? &pReq->pAst1 : &pReq->pAst2, 1 == i ? &pReq->ast1Len : &pReq->ast2Len); + code = getRollupAst(&cxt, pStmt, pRetension, dbCfg.precision, 1 == i ? &pReq->pAst1 : &pReq->pAst2, + 1 == i ? &pReq->ast1Len : &pReq->ast2Len); destroyTranslateContext(&cxt); if (TSDB_CODE_SUCCESS != code) { break; @@ -2245,7 +2252,7 @@ static int32_t buildCreateStbReq(STranslateContext* pCxt, SCreateTableStmt* pStm static int32_t translateCreateSuperTable(STranslateContext* pCxt, SCreateTableStmt* pStmt) { SMCreateStbReq createReq = {0}; - int32_t code = checkCreateTable(pCxt, pStmt); + int32_t code = checkCreateTable(pCxt, pStmt); if (TSDB_CODE_SUCCESS == code) { code = buildCreateStbReq(pCxt, pStmt, &createReq); } @@ -2289,7 +2296,8 @@ static int32_t translateDropTable(STranslateContext* pCxt, SDropTableStmt* pStmt static int32_t translateDropSuperTable(STranslateContext* pCxt, SDropSuperTableStmt* pStmt) { SName tableName; - return doTranslateDropSuperTable(pCxt, toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->tableName, &tableName), pStmt->ignoreNotExists); + return doTranslateDropSuperTable(pCxt, toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->tableName, &tableName), + pStmt->ignoreNotExists); } static int32_t setAlterTableField(SAlterTableStmt* pStmt, SMAltertbReq* pAlterReq) { @@ -2421,7 +2429,7 @@ static int32_t nodeTypeToShowType(ENodeType nt) { case QUERY_NODE_SHOW_QUERIES_STMT: return TSDB_MGMT_TABLE_QUERIES; case QUERY_NODE_SHOW_VARIABLE_STMT: - return 0; // todo + return 0; // todo default: break; } @@ -2463,8 +2471,8 @@ static int32_t buildSampleAstInfoByIndex(STranslateContext* pCxt, SCreateIndexSt pInfo->pOffset = nodesCloneNode(pStmt->pOptions->pOffset); pInfo->pSliding = nodesCloneNode(pStmt->pOptions->pSliding); if (NULL == pInfo->pFuncs || NULL == pInfo->pInterval || - (NULL != pStmt->pOptions->pOffset && NULL == pInfo->pOffset) || - (NULL != pStmt->pOptions->pSliding && NULL == pInfo->pSliding)) { + (NULL != pStmt->pOptions->pOffset && NULL == pInfo->pOffset) || + (NULL != pStmt->pOptions->pSliding && NULL == pInfo->pSliding)) { return TSDB_CODE_OUT_OF_MEMORY; } return TSDB_CODE_SUCCESS; @@ -2472,7 +2480,7 @@ static int32_t buildSampleAstInfoByIndex(STranslateContext* pCxt, SCreateIndexSt static int32_t getSmaIndexAst(STranslateContext* pCxt, SCreateIndexStmt* pStmt, char** pAst, int32_t* pLen) { SSampleAstInfo info = {0}; - int32_t code = buildSampleAstInfoByIndex(pCxt, pStmt, &info); + int32_t code = buildSampleAstInfoByIndex(pCxt, pStmt, &info); if (TSDB_CODE_SUCCESS == code) { code = buildSampleAst(pCxt, &info, pAst, pLen); } @@ -2618,9 +2626,9 @@ static int32_t translateDropComponentNode(STranslateContext* pCxt, SDropComponen static int32_t buildCreateTopicReq(STranslateContext* pCxt, SCreateTopicStmt* pStmt, SCMCreateTopicReq* pReq) { SName name; - // tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->topicName, strlen(pStmt->topicName)); - // tNameGetFullDbName(&name, pReq->name); - tNameExtractFullName(toName(pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, pStmt->topicName, &name), pReq->name); + tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->topicName, strlen(pStmt->topicName)); + tNameGetFullDbName(&name, pReq->name); + /*tNameExtractFullName(toName(pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, pStmt->topicName, &name), pReq->name);*/ pReq->igExists = pStmt->ignoreExists; pReq->withTbName = pStmt->pOptions->withTable; pReq->withSchema = pStmt->pOptions->withSchema; @@ -2633,16 +2641,19 @@ static int32_t buildCreateTopicReq(STranslateContext* pCxt, SCreateTopicStmt* pS int32_t code = TSDB_CODE_SUCCESS; + const char* dbName; if (NULL != pStmt->pQuery) { - strcpy(pReq->subscribeDbName, ((SRealTableNode*)(((SSelectStmt*)pStmt->pQuery)->pFromTable))->table.dbName); + dbName = ((SRealTableNode*)(((SSelectStmt*)pStmt->pQuery)->pFromTable))->table.dbName; pCxt->pParseCxt->topicQuery = true; code = translateQuery(pCxt, pStmt->pQuery); if (TSDB_CODE_SUCCESS == code) { code = nodesNodeToString(pStmt->pQuery, false, &pReq->ast, NULL); } } else { - strcpy(pReq->subscribeDbName, pStmt->subscribeDbName); + dbName = pStmt->subscribeDbName; } + tNameSetDbName(&name, pCxt->pParseCxt->acctId, dbName, strlen(dbName)); + tNameGetFullDbName(&name, pReq->subscribeDbName); return code; } @@ -2654,8 +2665,9 @@ static int32_t checkCreateTopic(STranslateContext* pCxt, SCreateTopicStmt* pStmt if (QUERY_NODE_SELECT_STMT == nodeType(pStmt->pQuery)) { SSelectStmt* pSelect = (SSelectStmt*)pStmt->pQuery; - if (!pSelect->isDistinct && QUERY_NODE_REAL_TABLE == nodeType(pSelect->pFromTable) && NULL == pSelect->pGroupByList && - NULL == pSelect->pLimit && NULL == pSelect->pSlimit && NULL == pSelect->pOrderByList && NULL == pSelect->pPartitionByList) { + if (!pSelect->isDistinct && QUERY_NODE_REAL_TABLE == nodeType(pSelect->pFromTable) && + NULL == pSelect->pGroupByList && NULL == pSelect->pLimit && NULL == pSelect->pSlimit && + NULL == pSelect->pOrderByList && NULL == pSelect->pPartitionByList) { return TSDB_CODE_SUCCESS; } } @@ -2665,7 +2677,7 @@ static int32_t checkCreateTopic(STranslateContext* pCxt, SCreateTopicStmt* pStmt static int32_t translateCreateTopic(STranslateContext* pCxt, SCreateTopicStmt* pStmt) { SCMCreateTopicReq createReq = {0}; - int32_t code = checkCreateTopic(pCxt, pStmt); + int32_t code = checkCreateTopic(pCxt, pStmt); if (TSDB_CODE_SUCCESS == code) { code = buildCreateTopicReq(pCxt, pStmt, &createReq); } @@ -2763,7 +2775,7 @@ static int32_t translateDropStream(STranslateContext* pCxt, SDropStreamStmt* pSt return TSDB_CODE_SUCCESS; } -static int32_t readFromFile(char* pName, int32_t *len, char **buf) { +static int32_t readFromFile(char* pName, int32_t* len, char** buf) { int64_t filesize = 0; if (taosStatFile(pName, &filesize, NULL) < 0) { return TAOS_SYSTEM_ERROR(errno); @@ -3408,8 +3420,8 @@ static void addCreateTbReqIntoVgroup(int32_t acctId, SHashObj* pVgroupHashmap, c static int32_t addValToKVRow(STranslateContext* pCxt, SValueNode* pVal, const SSchema* pSchema, SKVRowBuilder* pBuilder) { - if(pSchema->type == TSDB_DATA_TYPE_JSON){ - if(pVal->literal && strlen(pVal->literal) > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE){ + if (pSchema->type == TSDB_DATA_TYPE_JSON) { + if (pVal->literal && strlen(pVal->literal) > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) { return buildSyntaxErrMsg(&pCxt->msgBuf, "json string too long than 4095", pVal->literal); } @@ -3420,10 +3432,11 @@ static int32_t addValToKVRow(STranslateContext* pCxt, SValueNode* pVal, const SS return pCxt->errCode; } - if(pVal->node.resType.type == TSDB_DATA_TYPE_NULL){ + if (pVal->node.resType.type == TSDB_DATA_TYPE_NULL) { // todo - }else{ - tdAddColToKVRow(pBuilder, pSchema->colId, &(pVal->datum.p), IS_VAR_DATA_TYPE(pSchema->type) ? varDataTLen(pVal->datum.p) : TYPE_BYTES[pSchema->type]); + } else { + tdAddColToKVRow(pBuilder, pSchema->colId, &(pVal->datum.p), + IS_VAR_DATA_TYPE(pSchema->type) ? varDataTLen(pVal->datum.p) : TYPE_BYTES[pSchema->type]); } return TSDB_CODE_SUCCESS; @@ -3692,7 +3705,7 @@ static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) { int32_t translate(SParseContext* pParseCxt, SQuery* pQuery) { STranslateContext cxt = {0}; - int32_t code = initTranslateContext(pParseCxt, &cxt); + int32_t code = initTranslateContext(pParseCxt, &cxt); if (TSDB_CODE_SUCCESS == code) { code = fmFuncMgtInit(); } From 3d9e27edb83180d7741cd6baa9cbcebfb0f2dd95 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 25 Apr 2022 10:53:06 +0800 Subject: [PATCH 27/51] fix: remove un-necessary logs --- tools/shell/src/shellCommand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/shell/src/shellCommand.c b/tools/shell/src/shellCommand.c index c34ee7a22c..fcdbf2d020 100644 --- a/tools/shell/src/shellCommand.c +++ b/tools/shell/src/shellCommand.c @@ -395,7 +395,7 @@ 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"); + // fprintf(stderr, "No stream device\n"); w.ws_col = 120; w.ws_row = 30; } From 8f14f8e8b3513e2d35d18e7804a304823182239a Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 11:40:56 +0800 Subject: [PATCH 28/51] fix(query): enable mnd support show topics processing. --- include/util/taoserror.h | 2 +- source/dnode/mnode/impl/src/mndInfoSchema.c | 6 +++--- source/dnode/mnode/impl/src/mndPerfSchema.c | 2 +- source/dnode/mnode/impl/src/mndShow.c | 13 +++++++++---- source/dnode/mnode/impl/src/mndTopic.c | 9 ++++++--- source/util/src/terror.c | 2 +- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 3ac440346c..2545b33574 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -252,7 +252,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_COLUMN_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x03AE) // mnode-infoSchema -#define TSDB_CODE_MND_INVALID_INFOS_TBL TAOS_DEF_ERROR_CODE(0, 0x03B0) +#define TSDB_CODE_MND_INVALID_SYS_TABLENAME TAOS_DEF_ERROR_CODE(0, 0x03B0) // mnode-func #define TSDB_CODE_MND_FUNC_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x03C0) diff --git a/source/dnode/mnode/impl/src/mndInfoSchema.c b/source/dnode/mnode/impl/src/mndInfoSchema.c index 5dbbd27930..2b46fc9274 100644 --- a/source/dnode/mnode/impl/src/mndInfoSchema.c +++ b/source/dnode/mnode/impl/src/mndInfoSchema.c @@ -325,7 +325,7 @@ static int32_t mndInsInitMeta(SHashObj *hash) { return -1; } - if (taosHashPut(hash, meta.tbName, strlen(meta.tbName) + 1, &meta, sizeof(meta))) { + if (taosHashPut(hash, meta.tbName, strlen(meta.tbName), &meta, sizeof(meta))) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -340,10 +340,10 @@ int32_t mndBuildInsTableSchema(SMnode *pMnode, const char *dbFName, const char * return -1; } - STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName) + 1); + STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, tbName, strlen(tbName)); if (NULL == pMeta) { mError("invalid information schema table name:%s", tbName); - terrno = TSDB_CODE_MND_INVALID_INFOS_TBL; + terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME; return -1; } diff --git a/source/dnode/mnode/impl/src/mndPerfSchema.c b/source/dnode/mnode/impl/src/mndPerfSchema.c index 737068a2dd..a0ecbe9ae4 100644 --- a/source/dnode/mnode/impl/src/mndPerfSchema.c +++ b/source/dnode/mnode/impl/src/mndPerfSchema.c @@ -128,7 +128,7 @@ int32_t mndBuildPerfsTableSchema(SMnode *pMnode, const char *dbFName, const char STableMetaRsp *meta = (STableMetaRsp *)taosHashGet(pMnode->perfsMeta, tbName, strlen(tbName)); if (NULL == meta) { mError("invalid performance schema table name:%s", tbName); - terrno = TSDB_CODE_MND_INVALID_INFOS_TBL; + terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME; return -1; } diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index 1ab25f795f..94366a241d 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -100,6 +100,8 @@ static int32_t convertToRetrieveType(char* name, int32_t len) { type = TSDB_MGMT_TABLE_QUERIES; } else if (strncasecmp(name, TSDB_INS_TABLE_VNODES, len) == 0) { type = TSDB_MGMT_TABLE_VNODES; + } else if (strncasecmp(name, TSDB_PERFS_TABLE_TOPICS, len) == 0) { + type = TSDB_MGMT_TABLE_TOPICS; } else { // ASSERT(0); } @@ -187,11 +189,14 @@ static int32_t mndProcessRetrieveSysTableReq(SNodeMsg *pReq) { } if (retrieveReq.showId == 0) { - STableMetaRsp *pMeta = (STableMetaRsp *)taosHashGet(pMnode->infosMeta, retrieveReq.tb, strlen(retrieveReq.tb) + 1); + STableMetaRsp *pMeta = (STableMetaRsp *)taosHashGet(pMnode->infosMeta, retrieveReq.tb, strlen(retrieveReq.tb)); if (pMeta == NULL) { - terrno = TSDB_CODE_MND_INVALID_INFOS_TBL; - mError("failed to process show-retrieve req:%p since %s", pShow, terrstr()); - return -1; + pMeta = (STableMetaRsp *)taosHashGet(pMnode->perfsMeta, retrieveReq.tb, strlen(retrieveReq.tb)); + if (pMeta == NULL) { + terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME; + mError("failed to process show-retrieve req:%p since %s", pShow, terrstr()); + return -1; + } } pShow = mndCreateShowObj(pMnode, &retrieveReq); diff --git a/source/dnode/mnode/impl/src/mndTopic.c b/source/dnode/mnode/impl/src/mndTopic.c index faddc3a1c5..2516c43860 100644 --- a/source/dnode/mnode/impl/src/mndTopic.c +++ b/source/dnode/mnode/impl/src/mndTopic.c @@ -520,8 +520,11 @@ static int32_t mndRetrieveTopic(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pB int32_t cols = 0; char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE] = {0}; - tstrncpy(&topicName[VARSTR_HEADER_SIZE], pTopic->name, TSDB_TOPIC_NAME_LEN); - varDataSetLen(topicName, strlen(&topicName[VARSTR_HEADER_SIZE])); + + SName n; + tNameFromString(&n, pTopic->name, T_NAME_ACCT|T_NAME_DB); + tNameGetDbName(&n, varDataVal(topicName)); + varDataSetLen(topicName, strlen(varDataVal(topicName))); SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); colDataAppend(pColInfo, numOfRows, (const char *)topicName, false); @@ -535,7 +538,7 @@ static int32_t mndRetrieveTopic(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pB varDataSetLen(sql, strlen(&sql[VARSTR_HEADER_SIZE])); colDataAppend(pColInfo, numOfRows, (const char *)sql, false); - taosMemoryFree(sql); +// taosMemoryFree(sql); numOfRows++; sdbRelease(pSdb, pTopic); diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 9332cb481e..50c924bbb9 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -258,7 +258,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_COLUMN_ALREADY_EXIST, "Column already exists TAOS_DEFINE_ERROR(TSDB_CODE_MND_COLUMN_NOT_EXIST, "Column does not exist") // mnode-infoSchema -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_INFOS_TBL, "Invalid information schema table name") +TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_SYS_TABLENAME, "Invalid system table name") // mnode-func From ecf00f7f579b1ff45263bfd32b8864ca51fd81e9 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 25 Apr 2022 11:41:42 +0800 Subject: [PATCH 29/51] enable hb --- example/src/tmq.c | 1 - source/client/src/clientHb.c | 64 ++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/example/src/tmq.c b/example/src/tmq.c index b2cbf856c0..32390f71c9 100644 --- a/example/src/tmq.c +++ b/example/src/tmq.c @@ -60,7 +60,6 @@ int32_t init_env() { pRes = taos_query(pConn, "create stable if not exists st1 (ts timestamp, c1 int, c2 float, c3 binary(10)) tags(t1 int)"); if (taos_errno(pRes) != 0) { - assert(0); printf("failed to create super table st1, reason:%s\n", taos_errstr(pRes)); return -1; } diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 6159da9cb1..653f9a0634 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -14,9 +14,9 @@ */ #include "catalog.h" -#include "scheduler.h" #include "clientInt.h" #include "clientLog.h" +#include "scheduler.h" #include "trpc.h" static SClientHbMgr clientHbMgr = {0}; @@ -110,7 +110,8 @@ static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalo static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { SHbConnInfo *info = taosHashGet(pAppHbMgr->connInfo, &pRsp->connKey, sizeof(SClientHbKey)); if (NULL == info) { - tscWarn("fail to get connInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid, pRsp->connKey.connType); + tscWarn("fail to get connInfo, may be dropped, refId:%" PRIx64 ", type:%d", pRsp->connKey.tscRid, + pRsp->connKey.connType); return TSDB_CODE_SUCCESS; } @@ -121,7 +122,7 @@ static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { } else { updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &pRsp->query->epSet); pTscObj->connId = pRsp->query->connId; - + if (pRsp->query->killRid) { SRequestObj *pRequest = acquireRequest(pRsp->query->killRid); if (NULL == pRequest) { @@ -131,7 +132,7 @@ static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { releaseRequest(pRsp->query->killRid); } } - + if (pRsp->query->killConnection) { taos_close(pTscObj); } @@ -139,7 +140,7 @@ static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { releaseTscObj(pRsp->connKey.tscRid); } } - + int32_t kvNum = pRsp->info ? taosArrayGetSize(pRsp->info) : 0; tscDebug("hb got %d rsp kv", kvNum); @@ -236,24 +237,24 @@ static int32_t hbAsyncCallBack(void *param, const SDataBuf *pMsg, int32_t code) } int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { - int64_t now = taosGetTimestampUs(); + int64_t now = taosGetTimestampUs(); SQueryDesc desc = {0}; - int32_t code = 0; + int32_t code = 0; - void *pIter = taosHashIterate(pObj->pRequests, NULL); + void *pIter = taosHashIterate(pObj->pRequests, NULL); while (pIter != NULL) { - int64_t *rid = pIter; + int64_t *rid = pIter; SRequestObj *pRequest = acquireRequest(*rid); if (NULL == pRequest) { continue; } tstrncpy(desc.sql, pRequest->sqlstr, sizeof(desc.sql)); - desc.stime = pRequest->metric.start; - desc.queryId = pRequest->requestId; + desc.stime = pRequest->metric.start; + desc.queryId = pRequest->requestId; desc.useconds = now - pRequest->metric.start; - desc.reqRid = pRequest->self; - desc.pid = hbBasic->pid; + desc.reqRid = pRequest->self; + desc.pid = hbBasic->pid; taosGetFqdn(desc.fqdn); desc.subPlanNum = pRequest->body.pDag ? pRequest->body.pDag->numOfSubplans : 0; @@ -271,9 +272,9 @@ int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { } } - releaseRequest(*rid); + releaseRequest(*rid); taosArrayPush(hbBasic->queryDesc, &desc); - + pIter = taosHashIterate(pObj->pRequests, pIter); } @@ -286,14 +287,14 @@ int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) { tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid); return TSDB_CODE_QRY_APP_ERROR; } - + int32_t numOfQueries = pTscObj->pRequests ? taosHashGetSize(pTscObj->pRequests) : 0; if (numOfQueries <= 0) { releaseTscObj(connKey->tscRid); tscDebug("no queries on connection"); return TSDB_CODE_QRY_APP_ERROR; } - + SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic)); if (NULL == hbBasic) { tscError("calloc %d failed", (int32_t)sizeof(SQueryHbReqBasic)); @@ -308,7 +309,7 @@ int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) { taosMemoryFree(hbBasic); return TSDB_CODE_QRY_OUT_OF_MEMORY; } - + hbBasic->connId = pTscObj->connId; hbBasic->pid = taosGetPId(); taosGetAppName(hbBasic->app, NULL); @@ -405,7 +406,7 @@ int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req } hbGetQueryBasicInfo(connKey, req); - + code = hbGetExpiredDBInfo(connKey, pCatalog, req); if (TSDB_CODE_SUCCESS != code) { return code; @@ -471,10 +472,10 @@ SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { pIter = taosHashIterate(pAppHbMgr->activeInfo, pIter); } -// if (code) { -// taosArrayDestroyEx(pBatchReq->reqs, hbFreeReq); -// taosMemoryFreeClear(pBatchReq); -// } + // if (code) { + // taosArrayDestroyEx(pBatchReq->reqs, hbFreeReq); + // taosMemoryFreeClear(pBatchReq); + // } return pBatchReq; } @@ -630,24 +631,23 @@ void appHbMgrCleanup(void) { int sz = taosArrayGetSize(clientHbMgr.appHbMgrs); for (int i = 0; i < sz; i++) { SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i); - - void *pIter = taosHashIterate(pTarget->activeInfo, NULL); + + void *pIter = taosHashIterate(pTarget->activeInfo, NULL); while (pIter != NULL) { SClientHbReq *pOneReq = pIter; hbFreeReq(pOneReq); taosHashCleanup(pOneReq->info); pIter = taosHashIterate(pTarget->activeInfo, pIter); - } + } taosHashCleanup(pTarget->activeInfo); pTarget->activeInfo = NULL; - pIter = taosHashIterate(pTarget->connInfo, NULL); while (pIter != NULL) { SHbConnInfo *info = pIter; taosMemoryFree(info->param); pIter = taosHashIterate(pTarget->connInfo, pIter); - } + } taosHashCleanup(pTarget->connInfo); pTarget->connInfo = NULL; @@ -668,13 +668,13 @@ int hbMgrInit() { hbMgrInitHandle(); // init backgroud thread - //hbCreateThread(); + hbCreateThread(); return 0; } void hbMgrCleanUp() { - //hbStopThread(); + // hbStopThread(); // destroy all appHbMgr int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0); @@ -747,11 +747,11 @@ void hbDeregisterConn(SAppHbMgr *pAppHbMgr, SClientHbKey connKey) { taosMemoryFree(info->param); taosHashRemove(pAppHbMgr->connInfo, &connKey, sizeof(SClientHbKey)); } - + if (NULL == pReq || NULL == info) { return; } - + atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1); } From 21feb859159cc91f288c8733c1c060f636764289 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Mon, 25 Apr 2022 12:42:26 +0800 Subject: [PATCH 30/51] fix(query): fix timezone function output string incomplete TD-15116 --- source/libs/scalar/src/sclfunc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index 08b58cf810..28514c3605 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -1265,8 +1265,11 @@ int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOut } int32_t timezoneFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) { + char output[TD_TIMEZONE_LEN + VARSTR_HEADER_SIZE] = {0}; + memcpy(varDataVal(output), tsTimezoneStr, TD_TIMEZONE_LEN); + varDataSetLen(output, strlen(tsTimezoneStr)); for (int32_t i = 0; i < pInput->numOfRows; ++i) { - colDataAppend(pOutput->columnData, i, tsTimezoneStr, false); + colDataAppend(pOutput->columnData, i, output, false); } pOutput->numOfRows = pInput->numOfRows; return TSDB_CODE_SUCCESS; From f966242047645e06fcd1bd086cc98181b4a310d3 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Mon, 25 Apr 2022 13:57:55 +0800 Subject: [PATCH 31/51] feature(dnode): mul parameter start. --- CMakeLists.txt | 2 +- examples/c/CMakeLists.txt | 4 +- include/common/tglobal.h | 6 +- include/util/tconfig.h | 5 +- include/util/tenv.h | 33 +++ include/util/tjson.h | 3 + source/client/src/clientEnv.c | 4 +- source/common/src/tglobal.c | 23 +- source/dnode/mgmt/exe/dmMain.c | 26 +- source/dnode/mgmt/interface/src/dmFile.c | 2 +- source/libs/function/src/udfd.c | 4 +- source/libs/function/test/runUdf.c | 4 +- source/libs/sync/CMakeLists.txt | 2 +- source/libs/sync/test/CMakeLists.txt | 78 ++--- source/util/src/tconfig.c | 351 ++++++++++++++++++++++- source/util/src/tenv.c | 78 +++++ source/util/src/tjson.c | 20 +- source/util/test/CMakeLists.txt | 2 +- source/util/test/cfgTest.cpp | 1 + tests/tsim/src/simSystem.c | 4 +- tools/shell/CMakeLists.txt | 2 +- 21 files changed, 576 insertions(+), 78 deletions(-) create mode 100644 include/util/tenv.h create mode 100644 source/util/src/tenv.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e1fbd54be3..0436f5b259 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ project( ) if (NOT DEFINED TD_SOURCE_DIR) - set( TD_SOURCE_DIR ${CMAKE_SOURCE_DIR} ) + set( TD_SOURCE_DIR ${PROJECT_SOURCE_DIR} ) endif() set(TD_SUPPORT_DIR "${TD_SOURCE_DIR}/cmake") diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index e94de3cbca..17a9257c49 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -1,7 +1,7 @@ PROJECT(TDengine) IF (TD_LINUX) - INCLUDE_DIRECTORIES(. ${TD_COMMUNITY_DIR}/src/inc ${TD_COMMUNITY_DIR}/src/client/inc ${TD_COMMUNITY_DIR}/inc) + INCLUDE_DIRECTORIES(. ${TD_SOURCE_DIR}/src/inc ${TD_SOURCE_DIR}/src/client/inc ${TD_SOURCE_DIR}/inc) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(demo apitest.c) TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread ) @@ -13,7 +13,7 @@ IF (TD_LINUX) TARGET_LINK_LIBRARIES(epoll taos_static trpc tutil pthread lua) ENDIF () IF (TD_DARWIN) - INCLUDE_DIRECTORIES(. ${TD_COMMUNITY_DIR}/src/inc ${TD_COMMUNITY_DIR}/src/client/inc ${TD_COMMUNITY_DIR}/inc) + INCLUDE_DIRECTORIES(. ${TD_SOURCE_DIR}/src/inc ${TD_SOURCE_DIR}/src/client/inc ${TD_SOURCE_DIR}/inc) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(demo demo.c) TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread lua) diff --git a/include/common/tglobal.h b/include/common/tglobal.h index 72f67665ba..cde423c3cd 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -123,9 +123,9 @@ extern SDiskCfg tsDiskCfg[]; #define NEEDTO_COMPRESSS_MSG(size) (tsCompressMsgSize != -1 && (size) > tsCompressMsgSize) -int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDir, const char *envFile, - const char *apolloUrl, SArray *pArgs, bool tsc); -int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloUrl, SArray *pArgs, bool tsc); +int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char **envCmd, const char *cfgDir, const char *envFile, + char *apolloUrl, SArray *pArgs, bool tsc); +int32_t taosInitCfg(const char **envCmd, const char *cfgDir, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc); void taosCleanupCfg(); void taosCfgDynamicOptions(const char *option, const char *value); void taosAddDataDir(int32_t index, char *v1, int32_t level, int32_t primary); diff --git a/include/util/tconfig.h b/include/util/tconfig.h index 012368d9cb..06fa9fd9aa 100644 --- a/include/util/tconfig.h +++ b/include/util/tconfig.h @@ -30,6 +30,7 @@ typedef enum { CFG_STYPE_CFG_FILE, CFG_STYPE_ENV_FILE, CFG_STYPE_ENV_VAR, + CFG_STYPE_ENV_CMD, CFG_STYPE_APOLLO_URL, CFG_STYPE_ARG_LIST, CFG_STYPE_TAOS_OPTIONS @@ -82,7 +83,7 @@ typedef struct SConfig { } SConfig; SConfig *cfgInit(); -int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr); +int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const void *sourceStr); int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs); // SConfigPair void cfgCleanup(SConfig *pCfg); @@ -105,6 +106,8 @@ const char *cfgDtypeStr(ECfgDataType type); void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump); +int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char* apolloUrl); + #ifdef __cplusplus } #endif diff --git a/include/util/tenv.h b/include/util/tenv.h new file mode 100644 index 0000000000..1410e23f90 --- /dev/null +++ b/include/util/tenv.h @@ -0,0 +1,33 @@ + +/* + * 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_ENV_H_ +#define _TD_ENV_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t taosEnvNameToCfgName(const char *envNameStr, char *cfgNameStr, int32_t cfgNameMaxLen); +int32_t taosEnvToCfg(const char *envStr, char *cfgStr); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_ENV_H_*/ diff --git a/include/util/tjson.h b/include/util/tjson.h index 286a7e65fc..d23f7b402e 100644 --- a/include/util/tjson.h +++ b/include/util/tjson.h @@ -49,6 +49,8 @@ int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem); int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem); SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName); +int32_t tjsonGetObjectName(const SJson* pJson, char** pName); +int32_t tjsonGetObjectValueString(const SJson* pJson, char** pStringValue); int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal); int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal); int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal); @@ -81,6 +83,7 @@ char* tjsonToUnformattedString(const SJson* pJson); SJson* tjsonParse(const char* pStr); bool tjsonValidateJson(const char* pJson); +const char* tjsonGetError(); #ifdef __cplusplus } diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 9a34334466..56eab18a55 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -254,12 +254,12 @@ void taos_init_imp(void) { deltaToUtcInitOnce(); - if (taosCreateLog("taoslog", 10, configDir, NULL, NULL, NULL, 1) != 0) { + if (taosCreateLog("taoslog", 10, NULL, configDir, NULL, NULL, NULL, 1) != 0) { tscInitRes = -1; return; } - if (taosInitCfg(configDir, NULL, NULL, NULL, 1) != 0) { + if (taosInitCfg(NULL, configDir, NULL, NULL, NULL, 1) != 0) { tscInitRes = -1; return; } diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 33999ea14c..c74f53f0a0 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -220,7 +220,7 @@ struct SConfig *taosGetCfg() { return tsCfg; } -static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *envFile, const char *apolloUrl) { +static int32_t taosLoadCfg(SConfig *pCfg, const char **envCmd, const char *inputCfgDir, const char *envFile, char *apolloUrl) { char cfgDir[PATH_MAX] = {0}; char cfgFile[PATH_MAX + 100] = {0}; @@ -231,6 +231,8 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e tstrncpy(cfgFile, cfgDir, sizeof(cfgDir)); } + if (apolloUrl == NULL || apolloUrl[0] == '\0') cfgGetApollUrl(envCmd, envFile, apolloUrl); + if (cfgLoad(pCfg, CFG_STYPE_APOLLO_URL, apolloUrl) != 0) { uError("failed to load from apollo url:%s since %s", apolloUrl, terrstr()); return -1; @@ -251,6 +253,11 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e return -1; } + if (cfgLoad(pCfg, CFG_STYPE_ENV_CMD, envCmd) != 0) { + uError("failed to load from cmd env variables since %s", terrstr()); + return -1; + } + return 0; } @@ -325,8 +332,8 @@ static int32_t taosAddSystemCfg(SConfig *pCfg) { if (cfgAddFloat(pCfg, "numOfCores", tsNumOfCores, 0, 100000, 1) != 0) return -1; if (cfgAddInt64(pCfg, "openMax", tsOpenMax, 0, INT64_MAX, 1) != 0) return -1; if (cfgAddInt64(pCfg, "streamMax", tsStreamMax, 0, INT64_MAX, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "pageSize(KB)", tsPageSizeKB, 0, INT64_MAX, 1) != 0) return -1; - if (cfgAddInt64(pCfg, "totalMemory(KB)", tsTotalMemoryKB, 0, INT64_MAX, 1) != 0) return -1; + if (cfgAddInt32(pCfg, "pageSizeKB", tsPageSizeKB, 0, INT64_MAX, 1) != 0) return -1; + if (cfgAddInt64(pCfg, "totalMemoryKB", tsTotalMemoryKB, 0, INT64_MAX, 1) != 0) return -1; if (cfgAddString(pCfg, "os sysname", info.sysname, 1) != 0) return -1; if (cfgAddString(pCfg, "os nodename", info.nodename, 1) != 0) return -1; if (cfgAddString(pCfg, "os release", info.release, 1) != 0) return -1; @@ -572,8 +579,8 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { return 0; } -int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDir, const char *envFile, - const char *apolloUrl, SArray *pArgs, bool tsc) { +int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char **envCmd, const char *cfgDir, const char *envFile, + char *apolloUrl, SArray *pArgs, bool tsc) { osDefaultInit(); SConfig *pCfg = cfgInit(); @@ -588,7 +595,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi if (taosAddServerLogCfg(pCfg) != 0) return -1; } - if (taosLoadCfg(pCfg, cfgDir, envFile, apolloUrl) != 0) { + if (taosLoadCfg(pCfg, envCmd, cfgDir, envFile, apolloUrl) != 0) { uError("failed to load cfg since %s", terrstr()); cfgCleanup(pCfg); return -1; @@ -625,7 +632,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi return 0; } -int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloUrl, SArray *pArgs, bool tsc) { +int32_t taosInitCfg(const char **envCmd, const char *cfgDir, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) { if (tsCfg != NULL) return 0; tsCfg = cfgInit(); @@ -640,7 +647,7 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU } taosAddSystemCfg(tsCfg); - if (taosLoadCfg(tsCfg, cfgDir, envFile, apolloUrl) != 0) { + if (taosLoadCfg(tsCfg, envCmd, cfgDir, envFile, apolloUrl) != 0) { uError("failed to load cfg since %s", terrstr()); cfgCleanup(tsCfg); tsCfg = NULL; diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index 4b01c540f7..b85072faf6 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -24,6 +24,7 @@ static struct { bool printVersion; char envFile[PATH_MAX]; char apolloUrl[PATH_MAX]; + const char **envCmd; SArray *pArgs; // SConfigPair SDnode *pDnode; EDndNodeType ntype; @@ -56,6 +57,9 @@ static void dmSetSignalHandle() { } static int32_t dmParseArgs(int32_t argc, char const *argv[]) { + int32_t cmdEnvIndex = 0; + global.envCmd = taosMemoryMalloc(argc-1); + memset(global.envCmd, 0, argc-1); for (int32_t i = 1; i < argc; ++i) { if (strcmp(argv[i], "-c") == 0) { if (i < argc - 1) { @@ -70,7 +74,7 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) { } } else if (strcmp(argv[i], "-a") == 0) { tstrncpy(global.apolloUrl, argv[++i], PATH_MAX); - } else if (strcmp(argv[i], "-e") == 0) { + } else if (strcmp(argv[i], "-E") == 0) { tstrncpy(global.envFile, argv[++i], PATH_MAX); } else if (strcmp(argv[i], "-n") == 0) { global.ntype = atoi(argv[++i]); @@ -84,6 +88,9 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) { global.dumpConfig = true; } else if (strcmp(argv[i], "-V") == 0) { global.printVersion = true; + } else if (strcmp(argv[i], "-e") == 0) { + global.envCmd[cmdEnvIndex] = argv[++i]; + cmdEnvIndex++; } else { } } @@ -129,7 +136,7 @@ static SDnodeOpt dmGetOpt() { static int32_t dmInitLog() { char logName[12] = {0}; snprintf(logName, sizeof(logName), "%slog", dmLogName(global.ntype)); - return taosCreateLog(logName, 1, configDir, global.envFile, global.apolloUrl, global.pArgs, 0); + return taosCreateLog(logName, 1, global.envCmd, configDir, global.envFile, global.apolloUrl, global.pArgs, 0); } static void dmSetProcInfo(int32_t argc, char **argv) { @@ -168,6 +175,10 @@ static int32_t dmRunDnode() { return code; } +static void taosCleanupArgs() { + if (global.envCmd != NULL) taosMemoryFree(global.envCmd); +} + int main(int argc, char const *argv[]) { if (!taosCheckSystemIsSmallEnd()) { printf("failed to start since on non-small-end machines\n"); @@ -176,26 +187,31 @@ int main(int argc, char const *argv[]) { if (dmParseArgs(argc, argv) != 0) { printf("failed to start since parse args error\n"); + taosCleanupArgs(); return -1; } if (global.generateGrant) { dmGenerateGrant(); + taosCleanupArgs(); return 0; } if (global.printVersion) { dmPrintVersion(); + taosCleanupArgs(); return 0; } if (dmInitLog() != 0) { - dError("failed to start since init log error"); + printf("failed to start since init log error"); + taosCleanupArgs(); return -1; } - if (taosInitCfg(configDir, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) { + if (taosInitCfg(global.envCmd, configDir, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) { dError("failed to start since read config error"); + taosCleanupArgs(); return -1; } @@ -203,9 +219,11 @@ int main(int argc, char const *argv[]) { dmDumpCfg(); taosCleanupCfg(); taosCloseLog(); + taosCleanupArgs(); return 0; } dmSetProcInfo(argc, (char **)argv); + taosCleanupArgs(); return dmRunDnode(); } diff --git a/source/dnode/mgmt/interface/src/dmFile.c b/source/dnode/mgmt/interface/src/dmFile.c index e9117939d7..38acf169be 100644 --- a/source/dnode/mgmt/interface/src/dmFile.c +++ b/source/dnode/mgmt/interface/src/dmFile.c @@ -124,7 +124,7 @@ TdFilePtr dmCheckRunning(const char *dataDir) { TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { terrno = TAOS_SYSTEM_ERROR(errno); - dError("failed to lock file:%s since %s", filepath, terrstr()); + dError("failed to open file:%s since %s", filepath, terrstr()); return NULL; } diff --git a/source/libs/function/src/udfd.c b/source/libs/function/src/udfd.c index e4c4cb4893..faea595a42 100644 --- a/source/libs/function/src/udfd.c +++ b/source/libs/function/src/udfd.c @@ -548,7 +548,7 @@ static int32_t udfdParseArgs(int32_t argc, char *argv[]) { static int32_t udfdInitLog() { char logName[12] = {0}; snprintf(logName, sizeof(logName), "%slog", "udfd"); - return taosCreateLog(logName, 1, configDir, NULL, NULL, NULL, 0); + return taosCreateLog(logName, 1, NULL, configDir, NULL, NULL, NULL, 0); } void udfdCtrlAllocBufCb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { @@ -656,7 +656,7 @@ int main(int argc, char *argv[]) { return -1; } - if (taosInitCfg(configDir, NULL, NULL, NULL, 0) != 0) { + if (taosInitCfg(NULL, configDir, NULL, NULL, NULL, 0) != 0) { fnError("failed to start since read config error"); return -1; } diff --git a/source/libs/function/test/runUdf.c b/source/libs/function/test/runUdf.c index 8e5eac538e..9184d3d6e0 100644 --- a/source/libs/function/test/runUdf.c +++ b/source/libs/function/test/runUdf.c @@ -31,13 +31,13 @@ static int32_t parseArgs(int32_t argc, char *argv[]) { static int32_t initLog() { char logName[12] = {0}; snprintf(logName, sizeof(logName), "%slog", "udfc"); - return taosCreateLog(logName, 1, configDir, NULL, NULL, NULL, 0); + return taosCreateLog(logName, 1, NULL, configDir, NULL, NULL, NULL, 0); } int main(int argc, char *argv[]) { parseArgs(argc, argv); initLog(); - if (taosInitCfg(configDir, NULL, NULL, NULL, 0) != 0) { + if (taosInitCfg(NULL, configDir, NULL, NULL, NULL, 0) != 0) { fnError("failed to start since read config error"); return -1; } diff --git a/source/libs/sync/CMakeLists.txt b/source/libs/sync/CMakeLists.txt index cb196acc02..551849c6f2 100644 --- a/source/libs/sync/CMakeLists.txt +++ b/source/libs/sync/CMakeLists.txt @@ -11,7 +11,7 @@ target_link_libraries( target_include_directories( sync - PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" + PUBLIC "${TD_SOURCE_DIR}/include/libs/sync" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 3319a73fcf..8afe9ff2a7 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -199,197 +199,197 @@ target_sources(syncApplyMsgTest target_include_directories(syncTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEnvTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingTimerTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOTickQTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOTickPingTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOSendMsgTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOClientTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOServerTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRaftStoreTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEnqTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIndexTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncInitTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncUtilTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncVotesGrantedTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncVotesRespondTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIndexMgrTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncLogStoreTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEntryTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRequestVoteTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRequestVoteReplyTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncAppendEntriesTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncAppendEntriesReplyTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncClientRequestTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncTimeoutTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingReplyTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRpcMsgTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingTimerTest2 PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingSelfTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncElectTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEncodeTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncWriteTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncReplicateTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRefTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncLogStoreCheck PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRaftCfgTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRespMgrTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncSnapshotTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncApplyMsgTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${TD_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 699a7aa080..5ab349be4c 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -18,6 +18,9 @@ #include "taoserror.h" #include "tlog.h" #include "tutil.h" +#include "tenv.h" +#include "cJSON.h" +#include "tjson.h" #define CFG_NAME_PRINT_LEN 24 #define CFG_SRC_PRINT_LEN 12 @@ -25,6 +28,7 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath); int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath); int32_t cfgLoadFromEnvVar(SConfig *pConfig); +int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd); int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url); int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype); @@ -45,7 +49,7 @@ SConfig *cfgInit() { return pCfg; } -int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr) { +int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const void *sourceStr) { switch (cfgType) { case CFG_STYPE_CFG_FILE: return cfgLoadFromCfgFile(pCfg, sourceStr); @@ -55,6 +59,8 @@ int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr) { return cfgLoadFromEnvVar(pCfg); case CFG_STYPE_APOLLO_URL: return cfgLoadFromApollUrl(pCfg, sourceStr); + case CFG_STYPE_ENV_CMD: + return cfgLoadFromEnvCmd(pCfg, (const char **)sourceStr); default: return -1; } @@ -463,6 +469,8 @@ const char *cfgStypeStr(ECfgSrcType type) { return "arg_list"; case CFG_STYPE_TAOS_OPTIONS: return "taos_options"; + case CFG_STYPE_ENV_CMD: + return "env_cmd"; default: return "invalid"; } @@ -584,12 +592,154 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) { } int32_t cfgLoadFromEnvVar(SConfig *pConfig) { - uDebug("load from env variables not implemented yet"); + char *line = NULL, *name, *value, *value2, *value3; + int32_t olen, vlen, vlen2, vlen3; + ssize_t _bytes = 0; + TdCmdPtr pCmd = taosOpenCmd("set"); + if (pCmd == NULL) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + while (!taosEOFCmd(pCmd)) { + name = value = value2 = value3 = NULL; + olen = vlen = vlen2 = vlen3 = 0; + + _bytes = taosGetLineCmd(pCmd, &line); + if (_bytes < 0) { + break; + } + if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; + taosEnvToCfg(line, line); + + paGetToken(line, &name, &olen); + if (olen == 0) continue; + name[olen] = 0; + + paGetToken(name + olen + 1, &value, &vlen); + if (vlen == 0) continue; + value[vlen] = 0; + + paGetToken(value + vlen + 1, &value2, &vlen2); + if (vlen2 != 0) { + value2[vlen2] = 0; + paGetToken(value2 + vlen2 + 1, &value3, &vlen3); + if (vlen3 != 0) value3[vlen3] = 0; + } + + cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_VAR); + if (value2 != NULL && value3 != NULL && value2[0] != 0 && value3[0] != 0 && strcasecmp(name, "dataDir") == 0) { + cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_ENV_VAR); + } + } + + taosCloseCmd(&pCmd); + if (line != NULL) taosMemoryFreeClear(line); + + uInfo("load from env variables cfg success"); return 0; } -int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath) { - uDebug("load from env file not implemented yet"); +int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { + char *buf, *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); + index++; + + name = value = value2 = value3 = NULL; + olen = vlen = vlen2 = vlen3 = 0; + + paGetToken(buf, &name, &olen); + if (olen == 0) continue; + name[olen] = 0; + + paGetToken(name + olen + 1, &value, &vlen); + if (vlen == 0) continue; + value[vlen] = 0; + + paGetToken(value + vlen + 1, &value2, &vlen2); + if (vlen2 != 0) { + value2[vlen2] = 0; + paGetToken(value2 + vlen2 + 1, &value3, &vlen3); + if (vlen3 != 0) value3[vlen3] = 0; + } + + cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_CMD); + 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"); + return 0; +} + +int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { + char *line = NULL, *name, *value, *value2, *value3; + int32_t olen, vlen, vlen2, vlen3; + ssize_t _bytes = 0; + + const char *filepath = ".env"; + if (envFile != NULL && strlen(envFile)>0) { + if (!taosCheckExistFile(envFile)) { + uError("fial to load env file: %s", envFile); + return -1; + } + filepath = envFile; + }else { + if (!taosCheckExistFile(filepath)) { + uInfo("fial to load env file: %s", filepath); + return 0; + } + } + + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM); + if (pFile == NULL) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + while (!taosEOFFile(pFile)) { + name = value = value2 = value3 = NULL; + olen = vlen = vlen2 = vlen3 = 0; + + _bytes = taosGetLineFile(pFile, &line); + if (_bytes <= 0) { + break; + } + if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; + taosEnvToCfg(line, line); + + paGetToken(line, &name, &olen); + if (olen == 0) continue; + name[olen] = 0; + + paGetToken(name + olen + 1, &value, &vlen); + if (vlen == 0) continue; + value[vlen] = 0; + + paGetToken(value + vlen + 1, &value2, &vlen2); + if (vlen2 != 0) { + value2[vlen2] = 0; + paGetToken(value2 + vlen2 + 1, &value3, &vlen3); + if (vlen3 != 0) value3[vlen3] = 0; + } + + cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_FILE); + if (value2 != NULL && value3 != NULL && value2[0] != 0 && value3[0] != 0 && strcasecmp(name, "dataDir") == 0) { + cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_ENV_FILE); + } + } + + taosCloseFile(&pFile); + if (line != NULL) taosMemoryFreeClear(line); + + uInfo("load from env cfg file %s success", filepath); return 0; } @@ -617,11 +767,11 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { olen = vlen = vlen2 = vlen3 = 0; _bytes = taosGetLineFile(pFile, &line); - if (_bytes < 0) { + if (_bytes <= 0) { break; } - line[_bytes - 1] = 0; + if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; paGetToken(line, &name, &olen); if (olen == 0) continue; @@ -659,6 +809,193 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { } int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { - uDebug("load from apoll url not implemented yet"); + char *cfgLineBuf = NULL, *name, *value, *value2, *value3; + int32_t olen, vlen, vlen2, vlen3; + if (url == NULL || strlen(url) == 0) { + uInfo("fail to load apoll url"); + return 0; + } + + char *p = strchr(url, ':'); + if (p == NULL) { + uError("fail to load apoll url: %s, unknown format", url); + return -1; + } + p++; + + if (bcmp(url, "jsonFile", 8) == 0) { + char *filepath = p; + if (!taosCheckExistFile(filepath)) { + uError("fial to load json file: %s", filepath); + return -1; + } + + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); + if (pFile == NULL) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + size_t fileSize = taosLSeekFile(pFile, 0, SEEK_END); + char *buf = taosMemoryMalloc(fileSize); + taosLSeekFile(pFile, 0, SEEK_SET); + if(taosReadFile(pFile, buf, fileSize) <= 0) { + taosCloseFile(&pFile); + uError("load json file error: %s", filepath); + return -1; + } + taosCloseFile(&pFile); + SJson* pJson = tjsonParse(buf); + if (NULL == pJson) { + const char *jsonParseError = tjsonGetError(); + if (jsonParseError != NULL) { + uError("load json file parse error: %s", jsonParseError); + } + return -1; + } + taosMemoryFreeClear(buf); + + int32_t jsonArraySize = tjsonGetArraySize(pJson); + for(int32_t i = 0; i < jsonArraySize; i++) { + cJSON* item = tjsonGetArrayItem(pJson, i); + if (item == NULL) break; + char *itemName = NULL, *itemValueString = NULL; + tjsonGetObjectName(item, &itemName); + tjsonGetObjectName(item, &itemName); + tjsonGetObjectValueString(item, &itemValueString); + if (itemValueString != NULL && itemName != NULL) { + size_t itemNameLen = strlen(itemName); + size_t itemValueStringLen = strlen(itemValueString); + cfgLineBuf = taosMemoryMalloc(itemNameLen + itemValueStringLen + 2); + memcpy(cfgLineBuf, itemName, itemNameLen); + cfgLineBuf[itemNameLen] = ' '; + memcpy(&cfgLineBuf[itemNameLen+1], itemValueString, itemValueStringLen); + cfgLineBuf[itemNameLen + itemValueStringLen + 1] = '\0'; + + paGetToken(cfgLineBuf, &name, &olen); + if (olen == 0) continue; + name[olen] = 0; + + paGetToken(name + olen + 1, &value, &vlen); + if (vlen == 0) continue; + value[vlen] = 0; + + paGetToken(value + vlen + 1, &value2, &vlen2); + if (vlen2 != 0) { + value2[vlen2] = 0; + paGetToken(value2 + vlen2 + 1, &value3, &vlen3); + if (vlen3 != 0) value3[vlen3] = 0; + } + printf("%s(%d) %s name=%s, value=%s\n", __FILE__, __LINE__,__func__,name, value); + cfgSetItem(pConfig, name, value, CFG_STYPE_APOLLO_URL); + if (value2 != NULL && value3 != NULL && value2[0] != 0 && value3[0] != 0 && strcasecmp(name, "dataDir") == 0) { + cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_APOLLO_URL); + } + } + } + tjsonDelete(pJson); + + // } else if (bcmp(url, "jsonUrl", 7) == 0) { + // } else if (bcmp(url, "etcdUrl", 7) == 0) { + } else { + uError("Unsupported url: %s", url); + return -1; + } + + uInfo("load from apoll url not implemented yet"); return 0; } + +int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char* apolloUrl) { + int32_t index = 0; + if (envCmd == NULL) return 0; + while (envCmd[index]!=NULL) { + if (bcmp(envCmd[index], "TAOS_APOLL_URL", 14) == 0) { + char *p = strchr(envCmd[index], '='); + if (p != NULL) { + p++; + if (*p == '\'') { + p++; + p[strlen(p)-1] = '\0'; + } + memcpy(apolloUrl, p, TMIN(strlen(p)+1,PATH_MAX)); + uInfo("get apollo url from env cmd success"); + return 0; + } + } + index++; + } + + char *line = NULL; + ssize_t _bytes = 0; + TdCmdPtr pCmd = taosOpenCmd("set"); + if (pCmd != NULL) { + while (!taosEOFCmd(pCmd)) { + _bytes = taosGetLineCmd(pCmd, &line); + if (_bytes < 0) { + break; + } + if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; + if (bcmp(line, "TAOS_APOLL_URL", 14) == 0) { + char *p = strchr(line, '='); + if (p != NULL) { + p++; + if (*p == '\'') { + p++; + p[strlen(p)-1] = '\0'; + } + memcpy(apolloUrl, p, TMIN(strlen(p)+1,PATH_MAX)); + uInfo("get apollo url from env variables success, apolloUrl=%s",apolloUrl); + taosCloseCmd(&pCmd); + if (line != NULL) taosMemoryFreeClear(line); + return 0; + } + } + } + taosCloseCmd(&pCmd); + if (line != NULL) taosMemoryFreeClear(line); + } + + const char *filepath = ".env"; + if (envFile != NULL && strlen(envFile)>0) { + if (!taosCheckExistFile(envFile)) { + uError("fial to load env file: %s", envFile); + return -1; + } + filepath = envFile; + }else { + if (!taosCheckExistFile(filepath)) { + uInfo("fial to load env file: %s", filepath); + return 0; + } + } + TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM); + if (pFile != NULL) { + while (!taosEOFFile(pFile)) { + _bytes = taosGetLineFile(pFile, &line); + if (_bytes <= 0) { + break; + } + if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; + if (bcmp(line, "TAOS_APOLL_URL", 14) == 0) { + char *p = strchr(line, '='); + if (p != NULL) { + p++; + if (*p == '\'') { + p++; + p[strlen(p)-1] = '\0'; + } + memcpy(apolloUrl, p, TMIN(strlen(p)+1,PATH_MAX)); + taosCloseFile(&pFile); + if (line != NULL) taosMemoryFreeClear(line); + uInfo("get apollo url from env file success"); + return 0; + } + } + } + taosCloseFile(&pFile); + if (line != NULL) taosMemoryFreeClear(line); + } + + uInfo("fail get apollo url from cmd env file"); + return -1; +} \ No newline at end of file diff --git a/source/util/src/tenv.c b/source/util/src/tenv.c new file mode 100644 index 0000000000..e717e82c5b --- /dev/null +++ b/source/util/src/tenv.c @@ -0,0 +1,78 @@ +/* + * 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 "tenv.h" +#include "tconfig.h" + +static char toLowChar(char c) { return (c > 'Z' || c < 'A' ? c : (c - 'A' + 'a')); } + +int32_t taosEnvNameToCfgName(const char *envNameStr, char *cfgNameStr, int32_t cfgNameMaxLen) { + if (envNameStr == NULL || cfgNameStr == NULL) return -1; + char *p = cfgNameStr; + if (envNameStr[0] != 'T' || envNameStr[1] != 'A' || envNameStr[2] != 'O' || envNameStr[3] != 'S' || + envNameStr[4] != '_') { + // if(p != envNameStr) strncpy(p, envNameStr, cfgNameMaxLen - 1); + // p[cfgNameMaxLen - 1] = '\0'; + // return strlen(cfgNameStr); + cfgNameStr[0] = '\0'; + return -1; + } + envNameStr += 5; + if (*envNameStr != '\0') { + *p = toLowChar(*envNameStr); + p++; + envNameStr++; + } + + for (size_t i = 1; i < cfgNameMaxLen && *envNameStr != '\0'; i++) { + if (*envNameStr == '_') { + envNameStr++; + *p = *envNameStr; + if (*envNameStr == '\0') break; + } else { + *p = toLowChar(*envNameStr); + } + p++; + envNameStr++; + } + + *p = '\0'; + return strlen(cfgNameStr); +} + +int32_t taosEnvToCfg(const char *envStr, char *cfgStr) { + if (envStr == NULL && cfgStr == NULL) { + return -1; + } + if (cfgStr != envStr) strcpy(cfgStr, envStr); + char *p = strchr(cfgStr, '='); + + if (p != NULL) { + char buf[CFG_NAME_MAX_LEN]; + if (*(p+1) == '\'') { + *(p+1)= ' '; + char *pEnd = &cfgStr[strlen(cfgStr)-1]; + if (*pEnd == '\'') *pEnd = '\0'; + } + *p = '\0'; + int32_t cfgNameLen = taosEnvNameToCfgName(cfgStr, buf, CFG_NAME_MAX_LEN); + if (cfgNameLen > 0) { + memcpy(cfgStr, buf, cfgNameLen); + memset(&cfgStr[cfgNameLen], ' ', p - cfgStr - cfgNameLen + 1); + } + } + return strlen(cfgStr); +} \ No newline at end of file diff --git a/source/util/src/tjson.c b/source/util/src/tjson.c index 0b9727e0bf..558c2258f4 100644 --- a/source/util/src/tjson.c +++ b/source/util/src/tjson.c @@ -144,6 +144,22 @@ char* tjsonToUnformattedString(const SJson* pJson) { return cJSON_PrintUnformatt SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); } +int32_t tjsonGetObjectName(const SJson* pJson, char** pName) { + *pName = ((cJSON*)pJson)->string; + if (NULL == *pName) { + return TSDB_CODE_FAILED; + } + return TSDB_CODE_SUCCESS; +} + +int32_t tjsonGetObjectValueString(const SJson* pJson, char** pValueString) { + *pValueString = ((cJSON*)pJson)->valuestring; + if (NULL == *pValueString) { + return TSDB_CODE_FAILED; + } + return TSDB_CODE_SUCCESS; +} + int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal) { char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName)); if (NULL == p) { @@ -309,4 +325,6 @@ bool tjsonValidateJson(const char *jIn) { } } return true; -} \ No newline at end of file +} + +const char* tjsonGetError() { return cJSON_GetErrorPtr(); } \ No newline at end of file diff --git a/source/util/test/CMakeLists.txt b/source/util/test/CMakeLists.txt index d98f9f677d..3c52d52d50 100644 --- a/source/util/test/CMakeLists.txt +++ b/source/util/test/CMakeLists.txt @@ -31,7 +31,7 @@ ENDIF() # TARGET_LINK_LIBRARIES(trefTest util common) #ENDIF () -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/util/inc) +INCLUDE_DIRECTORIES(${TD_SOURCE_DIR}/src/util/inc) # freelistTest add_executable(freelistTest "") diff --git a/source/util/test/cfgTest.cpp b/source/util/test/cfgTest.cpp index 712fb2d09a..cd13ebe8ae 100644 --- a/source/util/test/cfgTest.cpp +++ b/source/util/test/cfgTest.cpp @@ -32,6 +32,7 @@ TEST_F(CfgTest, 01_Str) { EXPECT_STREQ(cfgStypeStr(CFG_STYPE_CFG_FILE), "cfg_file"); EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_FILE), "env_file"); EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_VAR), "env_var"); + EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_CMD), "env_cmd"); EXPECT_STREQ(cfgStypeStr(CFG_STYPE_APOLLO_URL), "apollo_url"); EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ARG_LIST), "arg_list"); EXPECT_STREQ(cfgStypeStr(ECfgSrcType(1024)), "invalid"); diff --git a/tests/tsim/src/simSystem.c b/tests/tsim/src/simSystem.c index 5bbcceada5..57fc7717f5 100644 --- a/tests/tsim/src/simSystem.c +++ b/tests/tsim/src/simSystem.c @@ -28,8 +28,8 @@ char simScriptDir[PATH_MAX] = {0}; extern bool simExecSuccess; int32_t simInitCfg() { - taosCreateLog("simlog", 1, configDir, NULL, NULL, NULL, 1); - taosInitCfg(configDir, NULL, NULL, NULL, 1); + taosCreateLog("simlog", 1, NULL, configDir, NULL, NULL, NULL, 1); + taosInitCfg(NULL, configDir, NULL, NULL, NULL, 1); SConfig *pCfg = taosGetCfg(); simDebugFlag = cfgGetItem(pCfg, "simDebugFlag")->i32; diff --git a/tools/shell/CMakeLists.txt b/tools/shell/CMakeLists.txt index 3b22b30852..284693795e 100644 --- a/tools/shell/CMakeLists.txt +++ b/tools/shell/CMakeLists.txt @@ -3,7 +3,7 @@ aux_source_directory(src SHELL_SRC) add_executable(shell ${SHELL_SRC}) target_link_libraries( shell - PUBLIC taos_static + PUBLIC taos PRIVATE os common transport util ) target_include_directories( From 276eb84746dba3ae8e0d157e6daa77fe47b91927 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Mon, 25 Apr 2022 14:05:09 +0800 Subject: [PATCH 32/51] feature(dnode): mul parameter start. --- include/common/tglobal.h | 4 ++-- source/client/src/clientEnv.c | 4 ++-- source/common/src/tglobal.c | 4 ++-- source/dnode/mgmt/exe/dmMain.c | 4 ++-- source/libs/function/src/udfd.c | 4 ++-- source/libs/function/test/runUdf.c | 4 ++-- tests/tsim/src/simSystem.c | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/common/tglobal.h b/include/common/tglobal.h index cde423c3cd..fc3d575317 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -123,9 +123,9 @@ extern SDiskCfg tsDiskCfg[]; #define NEEDTO_COMPRESSS_MSG(size) (tsCompressMsgSize != -1 && (size) > tsCompressMsgSize) -int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char **envCmd, const char *cfgDir, const char *envFile, +int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDir, const char **envCmd, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc); -int32_t taosInitCfg(const char **envCmd, const char *cfgDir, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc); +int32_t taosInitCfg(const char *cfgDir, const char **envCmd, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc); void taosCleanupCfg(); void taosCfgDynamicOptions(const char *option, const char *value); void taosAddDataDir(int32_t index, char *v1, int32_t level, int32_t primary); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 56eab18a55..596c3d3fbf 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -254,12 +254,12 @@ void taos_init_imp(void) { deltaToUtcInitOnce(); - if (taosCreateLog("taoslog", 10, NULL, configDir, NULL, NULL, NULL, 1) != 0) { + if (taosCreateLog("taoslog", 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) { tscInitRes = -1; return; } - if (taosInitCfg(NULL, configDir, NULL, NULL, NULL, 1) != 0) { + if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1) != 0) { tscInitRes = -1; return; } diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index c74f53f0a0..bbf14b2fdc 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -579,7 +579,7 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { return 0; } -int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char **envCmd, const char *cfgDir, const char *envFile, +int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDir, const char **envCmd, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) { osDefaultInit(); @@ -632,7 +632,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char **envC return 0; } -int32_t taosInitCfg(const char **envCmd, const char *cfgDir, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) { +int32_t taosInitCfg(const char *cfgDir, const char **envCmd, const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) { if (tsCfg != NULL) return 0; tsCfg = cfgInit(); diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index b85072faf6..43237e6e6c 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -136,7 +136,7 @@ static SDnodeOpt dmGetOpt() { static int32_t dmInitLog() { char logName[12] = {0}; snprintf(logName, sizeof(logName), "%slog", dmLogName(global.ntype)); - return taosCreateLog(logName, 1, global.envCmd, configDir, global.envFile, global.apolloUrl, global.pArgs, 0); + return taosCreateLog(logName, 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0); } static void dmSetProcInfo(int32_t argc, char **argv) { @@ -209,7 +209,7 @@ int main(int argc, char const *argv[]) { return -1; } - if (taosInitCfg(global.envCmd, configDir, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) { + if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) { dError("failed to start since read config error"); taosCleanupArgs(); return -1; diff --git a/source/libs/function/src/udfd.c b/source/libs/function/src/udfd.c index faea595a42..11bbd6442c 100644 --- a/source/libs/function/src/udfd.c +++ b/source/libs/function/src/udfd.c @@ -548,7 +548,7 @@ static int32_t udfdParseArgs(int32_t argc, char *argv[]) { static int32_t udfdInitLog() { char logName[12] = {0}; snprintf(logName, sizeof(logName), "%slog", "udfd"); - return taosCreateLog(logName, 1, NULL, configDir, NULL, NULL, NULL, 0); + return taosCreateLog(logName, 1, configDir, NULL, NULL, NULL, NULL, 0); } void udfdCtrlAllocBufCb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { @@ -656,7 +656,7 @@ int main(int argc, char *argv[]) { return -1; } - if (taosInitCfg(NULL, configDir, NULL, NULL, NULL, 0) != 0) { + if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0) != 0) { fnError("failed to start since read config error"); return -1; } diff --git a/source/libs/function/test/runUdf.c b/source/libs/function/test/runUdf.c index 9184d3d6e0..a8d6fbd715 100644 --- a/source/libs/function/test/runUdf.c +++ b/source/libs/function/test/runUdf.c @@ -31,13 +31,13 @@ static int32_t parseArgs(int32_t argc, char *argv[]) { static int32_t initLog() { char logName[12] = {0}; snprintf(logName, sizeof(logName), "%slog", "udfc"); - return taosCreateLog(logName, 1, NULL, configDir, NULL, NULL, NULL, 0); + return taosCreateLog(logName, 1, configDir, NULL, NULL, NULL, NULL, 0); } int main(int argc, char *argv[]) { parseArgs(argc, argv); initLog(); - if (taosInitCfg(NULL, configDir, NULL, NULL, NULL, 0) != 0) { + if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0) != 0) { fnError("failed to start since read config error"); return -1; } diff --git a/tests/tsim/src/simSystem.c b/tests/tsim/src/simSystem.c index 57fc7717f5..69f15a164e 100644 --- a/tests/tsim/src/simSystem.c +++ b/tests/tsim/src/simSystem.c @@ -28,8 +28,8 @@ char simScriptDir[PATH_MAX] = {0}; extern bool simExecSuccess; int32_t simInitCfg() { - taosCreateLog("simlog", 1, NULL, configDir, NULL, NULL, NULL, 1); - taosInitCfg(NULL, configDir, NULL, NULL, NULL, 1); + taosCreateLog("simlog", 1, configDir, NULL, NULL, NULL, NULL, 1); + taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1); SConfig *pCfg = taosGetCfg(); simDebugFlag = cfgGetItem(pCfg, "simDebugFlag")->i32; From 758a155c92c111658e815b44f96f69cb9057ded7 Mon Sep 17 00:00:00 2001 From: plum-lihui Date: Mon, 25 Apr 2022 14:13:46 +0800 Subject: [PATCH 33/51] [test: modify deploy scripte] --- tests/script/sh/massiveTable/compileVersion.sh | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/tests/script/sh/massiveTable/compileVersion.sh b/tests/script/sh/massiveTable/compileVersion.sh index dcd1f8aeea..6b45e98ea3 100755 --- a/tests/script/sh/massiveTable/compileVersion.sh +++ b/tests/script/sh/massiveTable/compileVersion.sh @@ -57,7 +57,8 @@ function compileTDengineVersion() { cd ${debugDir} cmake .. - make -j24 + make -j24 + make install } ######################################################################################## ############################### main process ########################################## @@ -67,20 +68,5 @@ cd ${projectDir} gitPullBranchInfo $TDengineBrVer compileTDengineVersion -taos_dir=${projectDir}/debug/build/bin -taosd_dir=${projectDir}/debug/build/bin -exec_process_dir=${projectDir}/debug/build/bin - -rm -f /usr/bin/taos -rm -f /usr/bin/taosd -rm -f /usr/bin/create_table -rm -f /usr/bin/tmq_demo -rm -f /usr/bin/tmq_sim - -ln -s $taos_dir/taos /usr/bin/taos -ln -s $taosd_dir/taosd /usr/bin/taosd -ln -s $exec_process_dir/create_table /usr/bin/create_table -ln -s $exec_process_dir/tmq_demo /usr/bin/tmq_demo -ln -s $exec_process_dir/tmq_sim /usr/bin/tmq_sim From 970f2d788e37ebc63f9c703572eece199d91dcc7 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Mon, 25 Apr 2022 14:47:54 +0800 Subject: [PATCH 34/51] fix(query): count function result on stable inconsistant with normal table TD-15116 --- source/libs/executor/src/executorimpl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 32c3a60b0f..41122ab7b1 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -3828,6 +3828,9 @@ int32_t setSDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadI } memcpy(pColInfoData->pData, pStart, colLen[i]); + //TODO setting this flag to true temporarily so aggregate function on stable will + //examine NULL value for non-primary key column + pColInfoData->hasNull = true; pStart += colLen[i]; } } else { // extract data according to pColList From 50cd7bfe4a67abc5c86ca07201b75b466160677e Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Mon, 25 Apr 2022 15:33:36 +0800 Subject: [PATCH 35/51] feature(dnode): param name error. --- source/util/src/tconfig.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 5ab349be4c..ee4236dce5 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -885,7 +885,6 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { paGetToken(value2 + vlen2 + 1, &value3, &vlen3); if (vlen3 != 0) value3[vlen3] = 0; } - printf("%s(%d) %s name=%s, value=%s\n", __FILE__, __LINE__,__func__,name, value); cfgSetItem(pConfig, name, value, CFG_STYPE_APOLLO_URL); if (value2 != NULL && value3 != NULL && value2[0] != 0 && value3[0] != 0 && strcasecmp(name, "dataDir") == 0) { cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_APOLLO_URL); @@ -909,7 +908,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char* apolloUrl int32_t index = 0; if (envCmd == NULL) return 0; while (envCmd[index]!=NULL) { - if (bcmp(envCmd[index], "TAOS_APOLL_URL", 14) == 0) { + if (bcmp(envCmd[index], "TAOS_APOLLO_URL", 14) == 0) { char *p = strchr(envCmd[index], '='); if (p != NULL) { p++; @@ -935,7 +934,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char* apolloUrl break; } if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; - if (bcmp(line, "TAOS_APOLL_URL", 14) == 0) { + if (bcmp(line, "TAOS_APOLLO_URL", 14) == 0) { char *p = strchr(line, '='); if (p != NULL) { p++; @@ -976,7 +975,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char* apolloUrl break; } if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0; - if (bcmp(line, "TAOS_APOLL_URL", 14) == 0) { + if (bcmp(line, "TAOS_APOLLO_URL", 14) == 0) { char *p = strchr(line, '='); if (p != NULL) { p++; From 8864a9ed592fea8af1008188ea1e140849dce056 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 25 Apr 2022 15:39:52 +0800 Subject: [PATCH 36/51] fix(raft): crash while start vnode --- source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 47 +++++++++++++++++++++--- source/dnode/vnode/inc/vnode.h | 8 ++-- source/dnode/vnode/src/vnd/vnodeOpen.c | 11 ++++++ 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index 687a799c57..38f6d7648f 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -74,12 +74,6 @@ int32_t vmOpenVnode(SVnodesMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) { return -1; } - // sync integration - vnodeSyncSetQ(pImpl, NULL); - vnodeSyncSetRpc(pImpl, NULL); - int32_t ret = vnodeSyncStart(pImpl); - assert(ret == 0); - taosWLockLatch(&pMgmt->latch); int32_t code = taosHashPut(pMgmt->hash, &pVnode->vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *)); taosWUnLockLatch(&pMgmt->latch); @@ -153,6 +147,7 @@ static void *vmOpenVnodeFunc(void *param) { pThread->failed++; } else { vmOpenVnode(pMgmt, pCfg, pImpl); + vnodeStart(pImpl); dDebug("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex); pThread->opened++; } @@ -364,10 +359,50 @@ static int32_t vmRequire(SMgmtWrapper *pWrapper, bool *required) { return 0; } +static int32_t vmStart(SMgmtWrapper *pWrapper) { + dDebug("vnode-mgmt start to run"); + SVnodesMgmt *pMgmt = pWrapper->pMgmt; + + taosRLockLatch(&pMgmt->latch); + + void *pIter = taosHashIterate(pMgmt->hash, NULL); + while (pIter) { + SVnodeObj **ppVnode = pIter; + if (ppVnode == NULL || *ppVnode == NULL) continue; + + SVnodeObj *pVnode = *ppVnode; + vnodeStart(pVnode->pImpl); + pIter = taosHashIterate(pMgmt->hash, pIter); + } + + taosRUnLockLatch(&pMgmt->latch); + return 0; +} + +static void vmStop(SMgmtWrapper *pWrapper) { + dDebug("vnode-mgmt start to stop"); + SVnodesMgmt *pMgmt = pWrapper->pMgmt; + taosRLockLatch(&pMgmt->latch); + + void *pIter = taosHashIterate(pMgmt->hash, NULL); + while (pIter) { + SVnodeObj **ppVnode = pIter; + if (ppVnode == NULL || *ppVnode == NULL) continue; + + SVnodeObj *pVnode = *ppVnode; + vnodeStop(pVnode->pImpl); + pIter = taosHashIterate(pMgmt->hash, pIter); + } + + taosRUnLockLatch(&pMgmt->latch); +} + void vmSetMgmtFp(SMgmtWrapper *pWrapper) { SMgmtFp mgmtFp = {0}; mgmtFp.openFp = vmInit; mgmtFp.closeFp = vmCleanup; + mgmtFp.startFp = vmStart; + mgmtFp.stopFp = vmStop; mgmtFp.requiredFp = vmRequire; vmInitMsgHandle(pWrapper); diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index f4975c183e..9eab4d3376 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -61,6 +61,9 @@ int32_t vnodeSync(SVnode *pVnode); int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); int vnodeValidateTableHash(SVnodeCfg *pVnodeOptions, char *tableFName); +int32_t vnodeStart(SVnode *pVnode); +void vnodeStop(SVnode *pVnode); + int64_t vnodeGetSyncHandle(SVnode *pVnode); void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot); @@ -171,11 +174,6 @@ typedef struct { uint64_t uid; } STableKeyInfo; -// sync integration -void vnodeSyncSetQ(SVnode *pVnode, void *qHandle); -void vnodeSyncSetRpc(SVnode *pVnode, void *rpcHandle); -int32_t vnodeSyncStart(SVnode *pVnode); - #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index 2b10afa5ed..11136bc7ba 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -14,6 +14,7 @@ */ #include "vnodeInt.h" +#include "vnodeSync.h" int vnodeCreate(const char *path, SVnodeCfg *pCfg, STfs *pTfs) { SVnodeInfo info = {0}; @@ -171,6 +172,16 @@ void vnodeClose(SVnode *pVnode) { } } +// start the sync timer after the queue is ready +int32_t vnodeStart(SVnode *pVnode) { + vnodeSyncSetQ(pVnode, NULL); + vnodeSyncSetRpc(pVnode, NULL); + vnodeSyncStart(pVnode); + return 0; +} + +void vnodeStop(SVnode *pVnode) {} + int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; } void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot) { pSnapshot->lastApplyIndex = pVnode->state.committed; } \ No newline at end of file From 609b21647bb771bbb4487c18fc6f68103de33e9e Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 25 Apr 2022 15:40:25 +0800 Subject: [PATCH 37/51] fix: msg with schema --- example/src/tmq.c | 7 +- include/common/tmsg.h | 110 ++++++--------------- source/client/inc/clientInt.h | 4 + source/client/src/clientImpl.c | 29 +++--- source/client/src/tmq.c | 6 +- source/dnode/mnode/impl/src/mndScheduler.c | 91 ++++++++--------- source/dnode/mnode/impl/src/mndTopic.c | 7 +- source/dnode/vnode/src/tq/tq.c | 30 ++++-- source/dnode/vnode/src/tq/tqRead.c | 66 +++++++++---- 9 files changed, 178 insertions(+), 172 deletions(-) diff --git a/example/src/tmq.c b/example/src/tmq.c index 32390f71c9..56f210081b 100644 --- a/example/src/tmq.c +++ b/example/src/tmq.c @@ -103,8 +103,8 @@ int32_t create_topic() { } taos_free_result(pRes); - /*pRes = taos_query(pConn, "create topic topic_ctb_column as abc1");*/ - pRes = taos_query(pConn, "create topic topic_ctb_column as select ts, c1, c2, c3 from ct1"); + pRes = taos_query(pConn, "create topic topic_ctb_column as abc1"); + /*pRes = taos_query(pConn, "create topic topic_ctb_column as select ts, c1, c2, c3 from ct1");*/ if (taos_errno(pRes) != 0) { printf("failed to create topic topic_ctb_column, reason:%s\n", taos_errstr(pRes)); return -1; @@ -162,9 +162,10 @@ tmq_t* build_consumer() { tmq_conf_set(conf, "group.id", "tg2"); tmq_conf_set(conf, "td.connect.user", "root"); tmq_conf_set(conf, "td.connect.pass", "taosdata"); - tmq_conf_set(conf, "td.connect.db", "abc1"); + /*tmq_conf_set(conf, "td.connect.db", "abc1");*/ tmq_conf_set_offset_commit_cb(conf, tmq_commit_cb_print); tmq_t* tmq = tmq_consumer_new(conf, NULL, 0); + assert(tmq); return tmq; } diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 5a2dd502c1..a07101f404 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -2052,80 +2052,6 @@ static FORCE_INLINE void* tDecodeSMqSetCVgReq(void* buf, SMqSetCVgReq* pReq) { return buf; } -typedef struct { - int64_t leftForVer; - int32_t vgId; - int32_t epoch; - int64_t consumerId; - char topicName[TSDB_TOPIC_FNAME_LEN]; -} SMqCancelConnReq; - -static FORCE_INLINE int32_t tEncodeSMqCancelConnReq(void** buf, const SMqCancelConnReq* pReq) { - int32_t tlen = 0; - tlen += taosEncodeFixedI64(buf, pReq->leftForVer); - tlen += taosEncodeFixedI32(buf, pReq->vgId); - tlen += taosEncodeFixedI32(buf, pReq->epoch); - tlen += taosEncodeFixedI64(buf, pReq->consumerId); - tlen += taosEncodeString(buf, pReq->topicName); - return tlen; -} - -static FORCE_INLINE void* tDecodeSMqCancelConnReq(void* buf, SMqCancelConnReq* pReq) { - buf = taosDecodeFixedI64(buf, &pReq->leftForVer); - buf = taosDecodeFixedI32(buf, &pReq->vgId); - buf = taosDecodeFixedI32(buf, &pReq->epoch); - buf = taosDecodeFixedI64(buf, &pReq->consumerId); - buf = taosDecodeStringTo(buf, pReq->topicName); - return buf; -} - -typedef struct { - int8_t reserved; -} SMqCancelConnRsp; - -typedef struct { - int64_t leftForVer; - int32_t vgId; - int64_t oldConsumerId; - int64_t newConsumerId; - char* topic; -} SMqMVRebReq; - -static FORCE_INLINE int32_t tEncodeSMqMVRebReq(void** buf, const SMqMVRebReq* pReq) { - int32_t tlen = 0; - tlen += taosEncodeFixedI64(buf, pReq->leftForVer); - tlen += taosEncodeFixedI32(buf, pReq->vgId); - tlen += taosEncodeFixedI64(buf, pReq->oldConsumerId); - tlen += taosEncodeFixedI64(buf, pReq->newConsumerId); - tlen += taosEncodeString(buf, pReq->topic); - return tlen; -} - -static FORCE_INLINE void* tDecodeSMqMVRebReq(void* buf, SMqMVRebReq* pReq) { - buf = taosDecodeFixedI64(buf, &pReq->leftForVer); - buf = taosDecodeFixedI32(buf, &pReq->vgId); - buf = taosDecodeFixedI64(buf, &pReq->oldConsumerId); - buf = taosDecodeFixedI64(buf, &pReq->newConsumerId); - buf = taosDecodeString(buf, &pReq->topic); - return buf; -} - -typedef struct { - SMsgHead header; - int32_t vgId; - int64_t consumerId; - char topicName[TSDB_TOPIC_FNAME_LEN]; - char cgroup[TSDB_CGROUP_LEN]; -} SMqSetCVgRsp; - -typedef struct { - SMsgHead header; - int32_t vgId; - int64_t consumerId; - char topicName[TSDB_TOPIC_FNAME_LEN]; - char cgroup[TSDB_CGROUP_LEN]; -} SMqMVRebRsp; - typedef struct { int32_t vgId; int64_t offset; @@ -2152,6 +2078,24 @@ typedef struct { SSchema* pSchema; } SSchemaWrapper; +static FORCE_INLINE SSchemaWrapper* tCloneSSchemaWrapper(const SSchemaWrapper* pSchemaWrapper) { + SSchemaWrapper* pSW = (SSchemaWrapper*)taosMemoryMalloc(sizeof(SSchemaWrapper)); + if (pSW == NULL) return pSW; + pSW->nCols = pSchemaWrapper->nCols; + pSW->pSchema = (SSchema*)taosMemoryCalloc(pSW->nCols, sizeof(SSchema)); + if (pSW->pSchema == NULL) { + taosMemoryFree(pSW); + return NULL; + } + memcpy(pSW->pSchema, pSchemaWrapper->pSchema, pSW->nCols * sizeof(SSchema)); + return pSW; +} + +static FORCE_INLINE void tDeleteSSchemaWrapper(SSchemaWrapper* pSchemaWrapper) { + taosMemoryFree(pSchemaWrapper->pSchema); + taosMemoryFree(pSchemaWrapper); +} + static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema) { int32_t tlen = 0; tlen += taosEncodeFixedI8(buf, pSchema->type); @@ -2162,13 +2106,13 @@ static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema return tlen; } -static FORCE_INLINE void* taosDecodeSSchema(void* buf, SSchema* pSchema) { +static FORCE_INLINE void* taosDecodeSSchema(const void* buf, SSchema* pSchema) { buf = taosDecodeFixedI8(buf, &pSchema->type); buf = taosDecodeFixedI8(buf, &pSchema->flags); buf = taosDecodeFixedI32(buf, &pSchema->bytes); buf = taosDecodeFixedI16(buf, &pSchema->colId); buf = taosDecodeStringTo(buf, pSchema->name); - return buf; + return (void*)buf; } static FORCE_INLINE int32_t tEncodeSSchema(SCoder* pEncoder, const SSchema* pSchema) { @@ -2198,7 +2142,7 @@ static FORCE_INLINE int32_t taosEncodeSSchemaWrapper(void** buf, const SSchemaWr return tlen; } -static FORCE_INLINE void* taosDecodeSSchemaWrapper(void* buf, SSchemaWrapper* pSW) { +static FORCE_INLINE void* taosDecodeSSchemaWrapper(const void* buf, SSchemaWrapper* pSW) { buf = taosDecodeFixedU32(buf, &pSW->nCols); pSW->pSchema = (SSchema*)taosMemoryCalloc(pSW->nCols, sizeof(SSchema)); if (pSW->pSchema == NULL) { @@ -2208,7 +2152,7 @@ static FORCE_INLINE void* taosDecodeSSchemaWrapper(void* buf, SSchemaWrapper* pS for (int32_t i = 0; i < pSW->nCols; i++) { buf = taosDecodeSSchema(buf, &pSW->pSchema[i]); } - return buf; + return (void*)buf; } static FORCE_INLINE int32_t tEncodeSSchemaWrapper(SCoder* pEncoder, const SSchemaWrapper* pSW) { @@ -2615,6 +2559,10 @@ static FORCE_INLINE int32_t tEncodeSMqDataBlkRsp(void** buf, const SMqDataBlkRsp void* data = taosArrayGetP(pRsp->blockData, i); tlen += taosEncodeFixedI32(buf, bLen); tlen += taosEncodeBinary(buf, data, bLen); + if (pRsp->withSchema) { + SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(pRsp->blockSchema, i); + tlen += taosEncodeSSchemaWrapper(buf, pSW); + } } } return tlen; @@ -2627,6 +2575,7 @@ static FORCE_INLINE void* tDecodeSMqDataBlkRsp(const void* buf, SMqDataBlkRsp* p buf = taosDecodeFixedI32(buf, &pRsp->blockNum); pRsp->blockData = taosArrayInit(pRsp->blockNum, sizeof(void*)); pRsp->blockDataLen = taosArrayInit(pRsp->blockNum, sizeof(void*)); + pRsp->blockSchema = taosArrayInit(pRsp->blockNum, sizeof(void*)); if (pRsp->blockNum != 0) { buf = taosDecodeFixedI8(buf, &pRsp->withTbName); buf = taosDecodeFixedI8(buf, &pRsp->withSchema); @@ -2639,6 +2588,11 @@ static FORCE_INLINE void* tDecodeSMqDataBlkRsp(const void* buf, SMqDataBlkRsp* p buf = taosDecodeBinary(buf, &data, bLen); taosArrayPush(pRsp->blockDataLen, &bLen); taosArrayPush(pRsp->blockData, &data); + if (pRsp->withSchema) { + SSchemaWrapper* pSW = (SSchemaWrapper*)taosMemoryMalloc(sizeof(SSchemaWrapper)); + buf = taosDecodeSSchemaWrapper(buf, pSW); + taosArrayPush(pRsp->blockSchema, &pSW); + } } } return (void*)buf; diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 814caf330a..c0237a184a 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -231,6 +231,10 @@ static FORCE_INLINE SReqResultInfo* tmqGetNextResInfo(TAOS_RES* res, bool conver msg->resIter++; if (msg->resIter < msg->rsp.blockNum) { SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)taosArrayGetP(msg->rsp.blockData, msg->resIter); + if (msg->rsp.withSchema) { + SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(msg->rsp.blockSchema, msg->resIter); + setResSchemaInfo(&msg->resInfo, pSW->pSchema, pSW->nCols); + } setQueryResultFromRsp(&msg->resInfo, pRetrieve, convertUcs4); return &msg->resInfo; } diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 2ae9a11803..a40edbb65a 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -226,17 +226,15 @@ int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) { int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList) { pRequest->type = pQuery->msgType; - SPlanContext cxt = { - .queryId = pRequest->requestId, - .acctId = pRequest->pTscObj->acctId, - .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp), - .pAstRoot = pQuery->pRoot, - .showRewrite = pQuery->showRewrite, - .pTransporter = pRequest->pTscObj->pAppInfo->pTransporter, - .pMsg = pRequest->msgBuf, - .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE - }; - int32_t code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &cxt.pCatalog); + SPlanContext cxt = {.queryId = pRequest->requestId, + .acctId = pRequest->pTscObj->acctId, + .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp), + .pAstRoot = pQuery->pRoot, + .showRewrite = pQuery->showRewrite, + .pTransporter = pRequest->pTscObj->pAppInfo->pTransporter, + .pMsg = pRequest->msgBuf, + .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE}; + int32_t code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &cxt.pCatalog); if (TSDB_CODE_SUCCESS == code) { code = qCreateQueryPlan(&cxt, pPlan, pNodeList); } @@ -247,6 +245,7 @@ void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t ASSERT(pSchema != NULL && numOfCols > 0); pResInfo->numOfCols = numOfCols; + // TODO handle memory leak pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD)); pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD)); @@ -282,7 +281,7 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList SQueryResult res = {.code = 0, .numOfRows = 0, .msgSize = ERROR_MSG_BUF_DEFAULT_SIZE, .msg = pRequest->msgBuf}; int32_t code = schedulerExecJob(pTransporter, pNodeList, pDag, &pRequest->body.queryJob, pRequest->sqlstr, - pRequest->metric.start, &res); + pRequest->metric.start, &res); if (code != TSDB_CODE_SUCCESS) { if (pRequest->body.queryJob != 0) { schedulerFreeJob(pRequest->body.queryJob); @@ -840,12 +839,12 @@ int32_t setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32 return code; } - char* p = (char*) pResultInfo->pData; + char* p = (char*)pResultInfo->pData; - int32_t dataLen = *(int32_t*) p; + int32_t dataLen = *(int32_t*)p; p += sizeof(int32_t); - uint64_t groupId = *(uint64_t*) p; + uint64_t groupId = *(uint64_t*)p; p += sizeof(uint64_t); int32_t* colLength = (int32_t*)p; diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index d576f28cb4..988406c84d 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -376,7 +376,7 @@ tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) { ASSERT(user); ASSERT(pass); - ASSERT(conf->db); + /*ASSERT(conf->db);*/ ASSERT(conf->groupId[0]); pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, conf->db, conf->port, CONN_TYPE__TMQ); @@ -1118,7 +1118,9 @@ SMqRspObj* tmqBuildRspFromWrapper(SMqPollRspWrapper* pWrapper) { pRspObj->resInfo.totalRows = 0; pRspObj->resInfo.precision = TSDB_TIME_PRECISION_MILLI; - setResSchemaInfo(&pRspObj->resInfo, pWrapper->topicHandle->schema.pSchema, pWrapper->topicHandle->schema.nCols); + if (!pWrapper->msg.withSchema) { + setResSchemaInfo(&pRspObj->resInfo, pWrapper->topicHandle->schema.pSchema, pWrapper->topicHandle->schema.nCols); + } taosFreeQitem(pWrapper); return pRspObj; diff --git a/source/dnode/mnode/impl/src/mndScheduler.c b/source/dnode/mnode/impl/src/mndScheduler.c index 73583058f1..3dff65866c 100644 --- a/source/dnode/mnode/impl/src/mndScheduler.c +++ b/source/dnode/mnode/impl/src/mndScheduler.c @@ -476,33 +476,37 @@ int32_t mndScheduleStream(SMnode* pMnode, STrans* pTrans, SStreamObj* pStream) { int32_t mndSchedInitSubEp(SMnode* pMnode, const SMqTopicObj* pTopic, SMqSubscribeObj* pSub) { SSdb* pSdb = pMnode->pSdb; SVgObj* pVgroup = NULL; - SQueryPlan* pPlan = qStringToQueryPlan(pTopic->physicalPlan); - if (pPlan == NULL) { - terrno = TSDB_CODE_QRY_INVALID_INPUT; - return -1; + SQueryPlan* pPlan = NULL; + SSubplan* plan = NULL; + if (pTopic->subType == TOPIC_SUB_TYPE__TABLE) { + pPlan = qStringToQueryPlan(pTopic->physicalPlan); + if (pPlan == NULL) { + terrno = TSDB_CODE_QRY_INVALID_INPUT; + return -1; + } + + ASSERT(pSub->vgNum == -1); + + pSub->vgNum = 0; + + int32_t levelNum = LIST_LENGTH(pPlan->pSubplans); + if (levelNum != 1) { + qDestroyQueryPlan(pPlan); + terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC; + return -1; + } + + SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 0); + + int32_t opNum = LIST_LENGTH(inner->pNodeList); + if (opNum != 1) { + qDestroyQueryPlan(pPlan); + terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC; + return -1; + } + plan = nodesListGetNode(inner->pNodeList, 0); } - ASSERT(pSub->vgNum == -1); - - pSub->vgNum = 0; - - int32_t levelNum = LIST_LENGTH(pPlan->pSubplans); - if (levelNum != 1) { - qDestroyQueryPlan(pPlan); - terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC; - return -1; - } - - SNodeListNode* inner = nodesListGetNode(pPlan->pSubplans, 0); - - int32_t opNum = LIST_LENGTH(inner->pNodeList); - if (opNum != 1) { - qDestroyQueryPlan(pPlan); - terrno = TSDB_CODE_MND_UNSUPPORTED_TOPIC; - return -1; - } - SSubplan* plan = nodesListGetNode(inner->pNodeList, 0); - int64_t unexistKey = -1; SMqConsumerEpInSub* pEpInSub = taosHashGet(pSub->consumerHash, &unexistKey, sizeof(int64_t)); ASSERT(pEpInSub); @@ -519,38 +523,35 @@ int32_t mndSchedInitSubEp(SMnode* pMnode, const SMqTopicObj* pTopic, SMqSubscrib } pSub->vgNum++; - plan->execNode.nodeId = pVgroup->vgId; - plan->execNode.epSet = mndGetVgroupEpset(pMnode, pVgroup); SMqVgEp* pVgEp = taosMemoryMalloc(sizeof(SMqVgEp)); - pVgEp->epSet = plan->execNode.epSet; - pVgEp->vgId = plan->execNode.nodeId; - -#if 0 - SMqConsumerEp consumerEp = {0}; - consumerEp.status = 0; - consumerEp.consumerId = -1; - consumerEp.epSet = plan->execNode.epSet; - consumerEp.vgId = plan->execNode.nodeId; -#endif + pVgEp->epSet = mndGetVgroupEpset(pMnode, pVgroup); + pVgEp->vgId = pVgroup->vgId; + taosArrayPush(pEpInSub->vgs, &pVgEp); mDebug("init subscribption %s, assign vg: %d", pSub->key, pVgEp->vgId); - int32_t msgLen; - if (qSubPlanToString(plan, &pVgEp->qmsg, &msgLen) < 0) { - sdbRelease(pSdb, pVgroup); - qDestroyQueryPlan(pPlan); - terrno = TSDB_CODE_QRY_INVALID_INPUT; - return -1; + if (pTopic->subType == TOPIC_SUB_TYPE__TABLE) { + int32_t msgLen; + + plan->execNode.epSet = pVgEp->epSet; + plan->execNode.nodeId = pVgEp->vgId; + + if (qSubPlanToString(plan, &pVgEp->qmsg, &msgLen) < 0) { + sdbRelease(pSdb, pVgroup); + qDestroyQueryPlan(pPlan); + terrno = TSDB_CODE_QRY_INVALID_INPUT; + return -1; + } + } else { + pVgEp->qmsg = strdup(""); } - taosArrayPush(pEpInSub->vgs, &pVgEp); ASSERT(taosHashGetSize(pSub->consumerHash) == 1); /*taosArrayPush(pSub->unassignedVg, &consumerEp);*/ } - ASSERT(pEpInSub->vgs->size > 0); pEpInSub = taosHashGet(pSub->consumerHash, &unexistKey, sizeof(int64_t)); ASSERT(pEpInSub->vgs->size > 0); diff --git a/source/dnode/mnode/impl/src/mndTopic.c b/source/dnode/mnode/impl/src/mndTopic.c index faddc3a1c5..e7a37658dc 100644 --- a/source/dnode/mnode/impl/src/mndTopic.c +++ b/source/dnode/mnode/impl/src/mndTopic.c @@ -282,10 +282,10 @@ static int32_t mndCreateTopic(SMnode *pMnode, SNodeMsg *pReq, SCMCreateTopicReq topicObj.version = 1; topicObj.sql = strdup(pCreate->sql); topicObj.sqlLen = strlen(pCreate->sql) + 1; - topicObj.ast = strdup(pCreate->ast); - topicObj.astLen = strlen(pCreate->ast) + 1; if (pCreate->ast && pCreate->ast[0]) { + topicObj.ast = strdup(pCreate->ast); + topicObj.astLen = strlen(pCreate->ast) + 1; topicObj.subType = TOPIC_SUB_TYPE__TABLE; topicObj.withTbName = 0; topicObj.withSchema = 0; @@ -314,6 +314,9 @@ static int32_t mndCreateTopic(SMnode *pMnode, SNodeMsg *pReq, SCMCreateTopicReq return -1; } } else { + topicObj.ast = strdup(""); + topicObj.astLen = 1; + topicObj.physicalPlan = strdup(""); topicObj.subType = TOPIC_SUB_TYPE__DB; topicObj.withTbName = 1; topicObj.withSchema = 1; diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index 434c5bc5bb..fdb43a761b 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -411,8 +411,10 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { SMqDataBlkRsp rsp = {0}; rsp.reqOffset = pReq->currentOffset; + rsp.withSchema = pExec->withSchema; rsp.blockData = taosArrayInit(0, sizeof(void*)); rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t)); + rsp.blockSchema = taosArrayInit(0, sizeof(void*)); while (1) { consumerEpoch = atomic_load_32(&pExec->epoch); @@ -512,6 +514,12 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { ASSERT(actualLen <= dataStrLen); taosArrayPush(rsp.blockDataLen, &actualLen); taosArrayPush(rsp.blockData, &buf); + + if (pExec->withSchema) { + SSchemaWrapper* pSW = tCloneSSchemaWrapper(pExec->pExecReader[workerId]->pSchemaWrapper); + taosArrayPush(rsp.blockSchema, &pSW); + } + rsp.blockNum++; } // db subscribe @@ -540,6 +548,10 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { ASSERT(actualLen <= dataStrLen); taosArrayPush(rsp.blockDataLen, &actualLen); taosArrayPush(rsp.blockData, &buf); + + SSchemaWrapper* pSW = tCloneSSchemaWrapper(pExec->pExecReader[workerId]->pSchemaWrapper); + taosArrayPush(rsp.blockSchema, &pSW); + rsp.blockNum++; } } else { @@ -586,6 +598,8 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { // TODO destroy taosArrayDestroy(rsp.blockData); taosArrayDestroy(rsp.blockDataLen); + taosArrayDestroyP(rsp.blockSchema, (FDelete)tDeleteSSchemaWrapper); + return 0; } @@ -827,12 +841,16 @@ int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) { pExec->pWalReader = walOpenReadHandle(pTq->pVnode->pWal); for (int32_t i = 0; i < 5; i++) { pExec->pExecReader[i] = tqInitSubmitMsgScanner(pTq->pVnode->pMeta); - SReadHandle handle = { - .reader = pExec->pExecReader[i], - .meta = pTq->pVnode->pMeta, - }; - pExec->task[i] = qCreateStreamExecTaskInfo(pExec->qmsg, &handle); - ASSERT(pExec->task[i]); + if (pExec->subType == TOPIC_SUB_TYPE__TABLE) { + SReadHandle handle = { + .reader = pExec->pExecReader[i], + .meta = pTq->pVnode->pMeta, + }; + pExec->task[i] = qCreateStreamExecTaskInfo(pExec->qmsg, &handle); + ASSERT(pExec->task[i]); + } else { + pExec->task[i] = NULL; + } } taosHashPut(pTq->execs, req.subKey, strlen(req.subKey), pExec, sizeof(STqExec)); return 0; diff --git a/source/dnode/vnode/src/tq/tqRead.c b/source/dnode/vnode/src/tq/tqRead.c index eb45577e0a..ef7e346867 100644 --- a/source/dnode/vnode/src/tq/tqRead.c +++ b/source/dnode/vnode/src/tq/tqRead.c @@ -65,7 +65,9 @@ bool tqNextDataBlock(STqReadHandle* pHandle) { /*pHandle->pBlock->uid = htobe64(pHandle->pBlock->uid);*/ /*if (pHandle->tbUid == pHandle->pBlock->uid) {*/ - ASSERT(pHandle->tbIdHash); + if (pHandle->tbIdHash == NULL) { + return true; + } void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->pBlock->uid, sizeof(int64_t)); if (ret != NULL) { /*printf("retrieve one tb %ld\n", pHandle->pBlock->uid);*/ @@ -107,26 +109,15 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p *pNumOfRows = pHandle->pBlock->numOfRows; int32_t colNumNeed = taosArrayGetSize(pHandle->pColIdList); - if (colNumNeed > pSchemaWrapper->nCols) { - colNumNeed = pSchemaWrapper->nCols; - } + if (colNumNeed == 0) { + *ppCols = taosArrayInit(pSchemaWrapper->nCols, sizeof(SColumnInfoData)); + if (*ppCols == NULL) { + return -1; + } - *ppCols = taosArrayInit(colNumNeed, sizeof(SColumnInfoData)); - if (*ppCols == NULL) { - return -1; - } - - int32_t colMeta = 0; - int32_t colNeed = 0; - while (colMeta < pSchemaWrapper->nCols && colNeed < colNumNeed) { - SSchema* pColSchema = &pSchemaWrapper->pSchema[colMeta]; - col_id_t colIdSchema = pColSchema->colId; - col_id_t colIdNeed = *(col_id_t*)taosArrayGet(pHandle->pColIdList, colNeed); - if (colIdSchema < colIdNeed) { - colMeta++; - } else if (colIdSchema > colIdNeed) { - colNeed++; - } else { + int32_t colMeta = 0; + while (colMeta < pSchemaWrapper->nCols) { + SSchema* pColSchema = &pSchemaWrapper->pSchema[colMeta]; SColumnInfoData colInfo = {0}; colInfo.info.bytes = pColSchema->bytes; colInfo.info.colId = pColSchema->colId; @@ -137,7 +128,40 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p } taosArrayPush(*ppCols, &colInfo); colMeta++; - colNeed++; + } + } else { + if (colNumNeed > pSchemaWrapper->nCols) { + colNumNeed = pSchemaWrapper->nCols; + } + + *ppCols = taosArrayInit(colNumNeed, sizeof(SColumnInfoData)); + if (*ppCols == NULL) { + return -1; + } + + int32_t colMeta = 0; + int32_t colNeed = 0; + while (colMeta < pSchemaWrapper->nCols && colNeed < colNumNeed) { + SSchema* pColSchema = &pSchemaWrapper->pSchema[colMeta]; + col_id_t colIdSchema = pColSchema->colId; + col_id_t colIdNeed = *(col_id_t*)taosArrayGet(pHandle->pColIdList, colNeed); + if (colIdSchema < colIdNeed) { + colMeta++; + } else if (colIdSchema > colIdNeed) { + colNeed++; + } else { + SColumnInfoData colInfo = {0}; + colInfo.info.bytes = pColSchema->bytes; + colInfo.info.colId = pColSchema->colId; + colInfo.info.type = pColSchema->type; + + if (colInfoDataEnsureCapacity(&colInfo, 0, *pNumOfRows) < 0) { + goto FAIL; + } + taosArrayPush(*ppCols, &colInfo); + colMeta++; + colNeed++; + } } } From d44611e2204e419dd9ae9357431e0aa81d4f2eac Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Mon, 25 Apr 2022 15:58:44 +0800 Subject: [PATCH 38/51] feat: block wise sma aggr query --- source/libs/executor/src/executorimpl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 0a246d573c..ca88bea71c 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1091,6 +1091,7 @@ static int32_t doSetInputDataBlock(SOperatorInfo* pOperator, SqlFunctionCtx* pCt SInputColumnInfoData* pInput = &pCtx[i].input; pInput->uid = pBlock->info.uid; + pInput->colDataAggIsSet = false; SExprInfo* pOneExpr = &pOperator->pExpr[i]; for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) { From 834e6b5711d9e1a6144d94f9644bb6c9ffd9fb3b Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Mon, 25 Apr 2022 16:41:22 +0800 Subject: [PATCH 39/51] feat: rollup refactor --- include/common/tmsg.h | 20 ++++++------- source/common/src/tmsg.c | 4 +-- source/dnode/vnode/src/inc/tsdb.h | 2 +- source/dnode/vnode/src/inc/tsdbSma.h | 2 +- source/dnode/vnode/src/tq/tqRead.c | 34 +++++++++++++--------- source/dnode/vnode/src/tsdb/tsdbMemTable.c | 28 ++++++++++++++++++ source/dnode/vnode/src/tsdb/tsdbRead.c | 2 ++ source/dnode/vnode/src/tsdb/tsdbSma.c | 34 ++++++++++++++-------- 8 files changed, 87 insertions(+), 39 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 335d20d096..d0926948dc 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -230,12 +230,12 @@ typedef struct { int32_t totalLen; int32_t len; // head of SSubmitBlk - int64_t uid; // table unique id - int64_t suid; // stable id - int32_t sversion; // data schema version - int32_t dataLen; // data part length, not including the SSubmitBlk head - int32_t schemaLen; // schema length, if length is 0, no schema exists - int16_t numOfRows; // total number of rows in current submit block + // int64_t uid; // table unique id + // int64_t suid; // stable id + // int32_t sversion; // data schema version + // int32_t dataLen; // data part length, not including the SSubmitBlk head + // int32_t schemaLen; // schema length, if length is 0, no schema exists + // int16_t numOfRows; // total number of rows in current submit block // head of SSubmitBlk const void* pMsg; } SSubmitMsgIter; @@ -249,10 +249,10 @@ STSRow* tGetSubmitBlkNext(SSubmitBlkIter* pIter); // 1) use tInitSubmitMsgIterEx firstly as not decrease the merge conflicts // 2) replace tInitSubmitMsgIterEx with tInitSubmitMsgIter later // 3) finally, rename tInitSubmitMsgIterEx to tInitSubmitMsgIter -int32_t tInitSubmitMsgIterEx(const SSubmitReq* pMsg, SSubmitMsgIter* pIter); -int32_t tGetSubmitMsgNextEx(SSubmitMsgIter* pIter, SSubmitBlk** pPBlock); -int32_t tInitSubmitBlkIterEx(SSubmitMsgIter* pMsgIter, SSubmitBlk* pBlock, SSubmitBlkIter* pIter); -STSRow* tGetSubmitBlkNextEx(SSubmitBlkIter* pIter); +// int32_t tInitSubmitMsgIterEx(const SSubmitReq* pMsg, SSubmitMsgIter* pIter); +// int32_t tGetSubmitMsgNextEx(SSubmitMsgIter* pIter, SSubmitBlk** pPBlock); +// int32_t tInitSubmitBlkIterEx(SSubmitMsgIter* pMsgIter, SSubmitBlk* pBlock, SSubmitBlkIter* pIter); +// STSRow* tGetSubmitBlkNextEx(SSubmitBlkIter* pIter); typedef struct { int32_t index; // index of failed block in submit blocks diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index e03850ef00..05fd9e301b 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -93,7 +93,7 @@ STSRow *tGetSubmitBlkNext(SSubmitBlkIter *pIter) { return row; } } - +#if 0 // TODO: KEEP one suite of iterator API finally. // 1) use tInitSubmitMsgIterEx firstly as not decrease the merge conflicts // 2) replace tInitSubmitMsgIterEx with tInitSubmitMsgIter later @@ -173,7 +173,7 @@ STSRow *tGetSubmitBlkNextEx(SSubmitBlkIter *pIter) { return row; } } - +#endif int32_t tEncodeSEpSet(SCoder *pEncoder, const SEpSet *pEp) { if (tEncodeI8(pEncoder, pEp->inUse) < 0) return -1; if (tEncodeI8(pEncoder, pEp->numOfEps) < 0) return -1; diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 8cfbffc962..f751c860bd 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -56,7 +56,7 @@ int32_t tsdbInsertTSmaData(STsdb *pTsdb, int64_t indexUid, const char *msg); int32_t tsdbDropTSmaData(STsdb *pTsdb, int64_t indexUid); int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg); void tsdbCleanupReadHandle(tsdbReaderT queryHandle); - +int tdScanAndConvertSubmitMsg(SSubmitReq *pMsg); typedef enum { TSDB_FILE_HEAD = 0, // .head TSDB_FILE_DATA, // .data diff --git a/source/dnode/vnode/src/inc/tsdbSma.h b/source/dnode/vnode/src/inc/tsdbSma.h index 3a865ddc51..e20d2989b9 100644 --- a/source/dnode/vnode/src/inc/tsdbSma.h +++ b/source/dnode/vnode/src/inc/tsdbSma.h @@ -55,7 +55,7 @@ void *tsdbUidStoreFree(STbUidStore *pStore); int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq); int32_t tsdbFetchTbUidList(void *pTsdb, void **result, void *suid, void *uid); int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pUidStore); -int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType); +int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, void *pMsg, int32_t inputType); #ifdef __cplusplus } diff --git a/source/dnode/vnode/src/tq/tqRead.c b/source/dnode/vnode/src/tq/tqRead.c index ada2460d97..eb45577e0a 100644 --- a/source/dnode/vnode/src/tq/tqRead.c +++ b/source/dnode/vnode/src/tq/tqRead.c @@ -33,15 +33,24 @@ STqReadHandle* tqInitSubmitMsgScanner(SMeta* pMeta) { int32_t tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t ver) { pReadHandle->pMsg = pMsg; + pMsg->length = htonl(pMsg->length); + pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); - // iterate - if (tInitSubmitMsgIterEx(pMsg, &pReadHandle->msgIter) < 0) return -1; + // iterate and convert + if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; while (true) { - if (tGetSubmitMsgNextEx(&pReadHandle->msgIter, &pReadHandle->pBlock) < 0) return -1; + if (tGetSubmitMsgNext(&pReadHandle->msgIter, &pReadHandle->pBlock) < 0) return -1; if (pReadHandle->pBlock == NULL) break; + + pReadHandle->pBlock->uid = htobe64(pReadHandle->pBlock->uid); + pReadHandle->pBlock->suid = htobe64(pReadHandle->pBlock->suid); + pReadHandle->pBlock->sversion = htonl(pReadHandle->pBlock->sversion); + pReadHandle->pBlock->dataLen = htonl(pReadHandle->pBlock->dataLen); + pReadHandle->pBlock->schemaLen = htonl(pReadHandle->pBlock->schemaLen); + pReadHandle->pBlock->numOfRows = htons(pReadHandle->pBlock->numOfRows); } - if (tInitSubmitMsgIterEx(pMsg, &pReadHandle->msgIter) < 0) return -1; + if (tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter) < 0) return -1; pReadHandle->ver = ver; memset(&pReadHandle->blkIter, 0, sizeof(SSubmitBlkIter)); return 0; @@ -49,7 +58,7 @@ int32_t tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t bool tqNextDataBlock(STqReadHandle* pHandle) { while (1) { - if (tGetSubmitMsgNextEx(&pHandle->msgIter, &pHandle->pBlock) < 0) { + if (tGetSubmitMsgNext(&pHandle->msgIter, &pHandle->pBlock) < 0) { return false; } if (pHandle->pBlock == NULL) return false; @@ -57,8 +66,7 @@ bool tqNextDataBlock(STqReadHandle* pHandle) { /*pHandle->pBlock->uid = htobe64(pHandle->pBlock->uid);*/ /*if (pHandle->tbUid == pHandle->pBlock->uid) {*/ ASSERT(pHandle->tbIdHash); - // void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->pBlock->uid, sizeof(int64_t)); - void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->msgIter.uid, sizeof(int64_t)); + void* ret = taosHashGet(pHandle->tbIdHash, &pHandle->pBlock->uid, sizeof(int64_t)); if (ret != NULL) { /*printf("retrieve one tb %ld\n", pHandle->pBlock->uid);*/ /*pHandle->pBlock->tid = htonl(pHandle->pBlock->tid);*/ @@ -80,14 +88,14 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p // TODO set to real sversion int32_t sversion = 0; if (pHandle->sver != sversion) { - pHandle->pSchema = metaGetTbTSchema(pHandle->pVnodeMeta, pHandle->msgIter.uid, sversion); + pHandle->pSchema = metaGetTbTSchema(pHandle->pVnodeMeta, pHandle->pBlock->uid, sversion); tb_uid_t quid; - STbCfg* pTbCfg = metaGetTbInfoByUid(pHandle->pVnodeMeta, pHandle->msgIter.uid); + STbCfg* pTbCfg = metaGetTbInfoByUid(pHandle->pVnodeMeta, pHandle->pBlock->uid); if (pTbCfg->type == META_CHILD_TABLE) { quid = pTbCfg->ctbCfg.suid; } else { - quid = pHandle->msgIter.uid; + quid = pHandle->pBlock->uid; } pHandle->pSchemaWrapper = metaGetTableSchema(pHandle->pVnodeMeta, quid, sversion, true); pHandle->sver = sversion; @@ -96,7 +104,7 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p STSchema* pTschema = pHandle->pSchema; SSchemaWrapper* pSchemaWrapper = pHandle->pSchemaWrapper; - *pNumOfRows = pHandle->msgIter.numOfRows; + *pNumOfRows = pHandle->pBlock->numOfRows; int32_t colNumNeed = taosArrayGetSize(pHandle->pColIdList); if (colNumNeed > pSchemaWrapper->nCols) { @@ -143,8 +151,8 @@ int32_t tqRetrieveDataBlock(SArray** ppCols, STqReadHandle* pHandle, uint64_t* p tdSTSRowIterInit(&iter, pTschema); STSRow* row; int32_t curRow = 0; - tInitSubmitBlkIterEx(&pHandle->msgIter, pHandle->pBlock, &pHandle->blkIter); - while ((row = tGetSubmitBlkNextEx(&pHandle->blkIter)) != NULL) { + tInitSubmitBlkIter(pHandle->pBlock, &pHandle->blkIter); + while ((row = tGetSubmitBlkNext(&pHandle->blkIter)) != NULL) { tdSTSRowIterReset(&iter, row); // get all wanted col of that block for (int32_t i = 0; i < colActual; i++) { diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable.c b/source/dnode/vnode/src/tsdb/tsdbMemTable.c index 3082ee6f2b..bea2b66af8 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable.c @@ -227,6 +227,34 @@ int tsdbLoadDataFromCache(STable *pTable, SSkipListIterator *pIter, TSKEY maxKey return 0; } +int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg) { + ASSERT(pMsg != NULL); + SSubmitMsgIter msgIter = {0}; + SSubmitBlk *pBlock = NULL; + SSubmitBlkIter blkIter = {0}; + STSRow *row = NULL; + + terrno = TSDB_CODE_SUCCESS; + pMsg->length = htonl(pMsg->length); + pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); + + if (tInitSubmitMsgIter(pMsg, &msgIter) < 0) return -1; + while (true) { + if (tGetSubmitMsgNext(&msgIter, &pBlock) < 0) return -1; + if (pBlock == NULL) break; + + pBlock->uid = htobe64(pBlock->uid); + pBlock->suid = htobe64(pBlock->suid); + pBlock->sversion = htonl(pBlock->sversion); + pBlock->dataLen = htonl(pBlock->dataLen); + pBlock->schemaLen = htonl(pBlock->schemaLen); + pBlock->numOfRows = htons(pBlock->numOfRows); + } + + if (terrno != TSDB_CODE_SUCCESS) return -1; + return 0; +} + static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg) { ASSERT(pMsg != NULL); // STsdbMeta * pMeta = pTsdb->tsdbMeta; diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index 098b86975b..483475601b 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -3636,6 +3636,8 @@ int32_t tsdbQuerySTableByTagCond(void* pMeta, uint64_t uid, TSKEY skey, const ch tsdbError("%p failed to get stable, uid:%" PRIu64 ", TID:0x%" PRIx64 " QID:0x%" PRIx64, pMeta, uid, taskId, reqId); terrno = TSDB_CODE_TDB_INVALID_TABLE_ID; goto _error; + } else { + tsdbDebug("%p succeed to get stable, uid:%" PRIu64 ", TID:0x%" PRIx64 " QID:0x%" PRIx64, pMeta, uid, taskId, reqId); } if (pTbCfg->type != META_SUPER_TABLE) { diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index f3b47c91ef..9143e8ed12 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -678,9 +678,9 @@ int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t vers return TSDB_CODE_FAILED; } - // if (tdScanAndConvertSubmitMsg(pMsg) != TSDB_CODE_SUCCESS) { - // return TSDB_CODE_FAILED; - // } + if (tdScanAndConvertSubmitMsg(pMsg) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_FAILED; + } if (tsdbCheckAndInitSmaEnv(pTsdb, TSDB_SMA_TYPE_TIME_RANGE) != TSDB_CODE_SUCCESS) { terrno = TSDB_CODE_TDB_INIT_FAILED; @@ -705,25 +705,25 @@ int32_t tsdbUpdateExpiredWindowImpl(STsdb *pTsdb, SSubmitReq *pMsg, int64_t vers SInterval interval = {0}; TSKEY lastWinSKey = INT64_MIN; - if (tInitSubmitMsgIterEx(pMsg, &msgIter) != TSDB_CODE_SUCCESS) { + if (tInitSubmitMsgIter(pMsg, &msgIter) != TSDB_CODE_SUCCESS) { return TSDB_CODE_FAILED; } while (true) { - tGetSubmitMsgNextEx(&msgIter, &pBlock); + tGetSubmitMsgNext(&msgIter, &pBlock); if (!pBlock) break; STSmaWrapper *pSW = NULL; STSma *pTSma = NULL; SSubmitBlkIter blkIter = {0}; - if (tInitSubmitBlkIterEx(&msgIter, pBlock, &blkIter) != TSDB_CODE_SUCCESS) { + if (tInitSubmitBlkIter(pBlock, &blkIter) != TSDB_CODE_SUCCESS) { pSW = tdFreeTSmaWrapper(pSW); break; } while (true) { - STSRow *row = tGetSubmitBlkNextEx(&blkIter); + STSRow *row = tGetSubmitBlkNext(&blkIter); if (!row) { tdFreeTSmaWrapper(pSW); break; @@ -1763,6 +1763,8 @@ int32_t tsdbRegisterRSma(STsdb *pTsdb, SMeta *pMeta, SVCreateTbReq *pReq) { if (taosHashPut(SMA_STAT_INFO_HASH(pStat), &pReq->stbCfg.suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != TSDB_CODE_SUCCESS) { return TSDB_CODE_FAILED; + } else { + tsdbDebug("vgId:%d register rsma info succeed for suid:%" PRIi64, REPO_ID(pTsdb), pReq->stbCfg.suid); } return TSDB_CODE_SUCCESS; @@ -1915,10 +1917,17 @@ static FORCE_INLINE int32_t tsdbUpdateTbUidListImpl(STsdb *pTsdb, tb_uid_t *suid if (pRSmaInfo->taskInfo[0] && (qUpdateQualifiedTableId(pRSmaInfo->taskInfo[0], tbUids, true) != 0)) { tsdbError("vgId:%d update tbUidList failed for uid:%" PRIi64 " since %s", REPO_ID(pTsdb), *suid, terrstr(terrno)); return TSDB_CODE_FAILED; + } else { + tsdbDebug("vgId:%d update tbUidList succeed for qTaskInfo:%p with suid:%" PRIi64 ", uid:%" PRIi64, REPO_ID(pTsdb), + pRSmaInfo->taskInfo[0], *suid, *(int64_t *)taosArrayGet(tbUids, 0)); } + if (pRSmaInfo->taskInfo[1] && (qUpdateQualifiedTableId(pRSmaInfo->taskInfo[1], tbUids, true) != 0)) { tsdbError("vgId:%d update tbUidList failed for uid:%" PRIi64 " since %s", REPO_ID(pTsdb), *suid, terrstr(terrno)); return TSDB_CODE_FAILED; + } else { + tsdbDebug("vgId:%d update tbUidList succeed for qTaskInfo:%p with suid:%" PRIi64 ", uid:%" PRIi64, REPO_ID(pTsdb), + pRSmaInfo->taskInfo[1], *suid, *(int64_t *)taosArrayGet(tbUids, 0)); } return TSDB_CODE_SUCCESS; @@ -1955,7 +1964,7 @@ int32_t tsdbUpdateTbUidList(STsdb *pTsdb, STbUidStore *pStore) { return TSDB_CODE_SUCCESS; } -static int32_t tsdbFetchSubmitReqSuids(const SSubmitReq *pMsg, STbUidStore *pStore) { +static int32_t tsdbFetchSubmitReqSuids(SSubmitReq *pMsg, STbUidStore *pStore) { ASSERT(pMsg != NULL); SSubmitMsgIter msgIter = {0}; SSubmitBlk *pBlock = NULL; @@ -1966,11 +1975,12 @@ static int32_t tsdbFetchSubmitReqSuids(const SSubmitReq *pMsg, STbUidStore *pSto // pMsg->length = htonl(pMsg->length); // pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); - if (tInitSubmitMsgIterEx(pMsg, &msgIter) < 0) return -1; + if (tInitSubmitMsgIter(pMsg, &msgIter) < 0) return -1; while (true) { - if (tGetSubmitMsgNextEx(&msgIter, &pBlock) < 0) return -1; + if (tGetSubmitMsgNext(&msgIter, &pBlock) < 0) return -1; + if (!pBlock) break; - tsdbUidStorePut(pStore, msgIter.suid, NULL); + tsdbUidStorePut(pStore, pBlock->suid, NULL); } if (terrno != TSDB_CODE_SUCCESS) return -1; @@ -2039,7 +2049,7 @@ int32_t tsdbExecuteRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t in return TSDB_CODE_SUCCESS; } -int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, const void *pMsg, int32_t inputType) { +int32_t tsdbTriggerRSma(STsdb *pTsdb, SMeta *pMeta, void *pMsg, int32_t inputType) { SSmaEnv *pEnv = REPO_RSMA_ENV(pTsdb); if (!pEnv) { // only applicable when rsma env exists From 57b492624b890ab184399b18f49018596e0562cf Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 16:44:48 +0800 Subject: [PATCH 40/51] refactor(query): do some internal refactor. --- source/libs/executor/inc/executorimpl.h | 4 +- source/libs/executor/src/executorimpl.c | 129 ++++++++++-------------- 2 files changed, 53 insertions(+), 80 deletions(-) diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index db0dd1ffbb..363824f2e2 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -399,8 +399,8 @@ typedef struct SOptrBasicInfo { // TODO move the resultrowsiz together with SOptrBasicInfo:rowCellInfoOffset typedef struct SAggSupporter { SHashObj* pResultRowHashTable; // quick locate the window object for each result - SHashObj* pResultRowListSet; // used to check if current ResultRowInfo has ResultRow object or not - SArray* pResultRowArrayList; // The array list that contains the Result rows +// SHashObj* pResultRowListSet; // used to check if current ResultRowInfo has ResultRow object or not +// SArray* pResultRowArrayList; // The array list that contains the Result rows char* keyBuf; // window key buffer SDiskbasedBuf* pResultBuf; // query result buffer based on blocked-wised disk file int32_t resultRowSize; // the result buffer size for each result row, with the meta data size for each row diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 6a5f5b2dc3..321e613f82 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -324,11 +324,16 @@ static void prepareResultListBuffer(SResultRowInfo* pResultRowInfo, jmp_buf env) newCapacity = (int64_t)(pResultRowInfo->capacity * 1.5); } - if (newCapacity == pResultRowInfo->capacity) { + if (newCapacity <= pResultRowInfo->capacity) { newCapacity += 4; } - pResultRowInfo->pPosition = taosMemoryRealloc(pResultRowInfo->pPosition, newCapacity * sizeof(SResultRowPosition)); + char* p = taosMemoryRealloc(pResultRowInfo->pPosition, newCapacity * sizeof(SResultRowPosition)); + if (p == NULL) { + longjmp(env, TSDB_CODE_OUT_OF_MEMORY); + } + + pResultRowInfo->pPosition = (SResultRowPosition*)p; int32_t inc = (int32_t)newCapacity - pResultRowInfo->capacity; memset(&pResultRowInfo->pPosition[pResultRowInfo->capacity], 0, sizeof(SResultRowPosition) * inc); @@ -419,88 +424,56 @@ SResultRow* getNewResultRow_rv(SDiskbasedBuf* pResultBuf, int64_t tableGroupId, * | 8 bytes | actual length | * +----------+---------------+ */ -static SResultRow* doSetResultOutBufByKey_rv(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, int64_t uid, +static SResultRow* doSetResultOutBufByKey(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, int64_t uid, char* pData, int16_t bytes, bool masterscan, uint64_t groupId, SExecTaskInfo* pTaskInfo, bool isIntervalQuery, SAggSupporter* pSup) { - bool existInCurrentResusltRowInfo = false; SET_RES_WINDOW_KEY(pSup->keyBuf, pData, bytes, groupId); SResultRowPosition* p1 = (SResultRowPosition*)taosHashGet(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes)); + SResultRow* pResult = NULL; + // in case of repeat scan/reverse scan, no new time window added. if (isIntervalQuery) { - if (!masterscan) { // the *p1 may be NULL in case of sliding+offset exists. - if (p1 != NULL) { - return getResultRowByPos(pResultBuf, p1); - } else { - return NULL; - } - } - - if (p1 != NULL) { - if (pResultRowInfo->size == 0) { - existInCurrentResusltRowInfo = - false; // this time window created by other timestamp that does not belongs to current table. - } else if (pResultRowInfo->size == 1) { - SResultRowPosition* p = &pResultRowInfo->pPosition[0]; - existInCurrentResusltRowInfo = (p->pageId == p1->pageId && p->offset == p1->offset); - } else { // check if current pResultRowInfo contains the existInCurrentResusltRowInfo pResultRow - SET_RES_EXT_WINDOW_KEY(pSup->keyBuf, pData, bytes, uid, pResultRowInfo); - int64_t* index = taosHashGet(pSup->pResultRowListSet, pSup->keyBuf, GET_RES_EXT_WINDOW_KEY_LEN(bytes)); - if (index != NULL) { - // TODO check the scan order for current opened time window - existInCurrentResusltRowInfo = true; - } else { - existInCurrentResusltRowInfo = false; - } - } + if (masterscan && p1 != NULL) { // the *p1 may be NULL in case of sliding+offset exists. + pResult = getResultRowByPos(pResultBuf, p1); } } else { // In case of group by column query, the required SResultRow object must be existInCurrentResusltRowInfo in the // pResultRowInfo object. if (p1 != NULL) { - return getResultRowByPos(pResultBuf, p1); - } - } - - SResultRow* pResult = NULL; - if (!existInCurrentResusltRowInfo) { - // 1. close current opened time window - if (pResultRowInfo->cur.pageId != -1) { // todo extract function - SResultRowPosition pos = pResultRowInfo->cur; - SFilePage* pPage = getBufPage(pResultBuf, pos.pageId); - SResultRow* pRow = (SResultRow*)((char*)pPage + pos.offset); - closeResultRow(pRow); - releaseBufPage(pResultBuf, pPage); - } - - prepareResultListBuffer(pResultRowInfo, pTaskInfo->env); - if (p1 == NULL) { - pResult = getNewResultRow_rv(pResultBuf, groupId, pSup->resultRowSize); - initResultRow(pResult); - - // add a new result set for a new group - SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset}; - taosHashPut(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes), &pos, - sizeof(SResultRowPosition)); - SResultRowCell cell = {.groupId = groupId, .pos = pos}; - taosArrayPush(pSup->pResultRowArrayList, &cell); - } else { pResult = getResultRowByPos(pResultBuf, p1); } - - // 2. set the new time window to be the new active time window - pResultRowInfo->pPosition[pResultRowInfo->size++] = - (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset}; - pResultRowInfo->cur = (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset}; - SET_RES_EXT_WINDOW_KEY(pSup->keyBuf, pData, bytes, uid, pResultRowInfo); - taosHashPut(pSup->pResultRowListSet, pSup->keyBuf, GET_RES_EXT_WINDOW_KEY_LEN(bytes), &pResultRowInfo->cur, - POINTER_BYTES); - } else { - pResult = getResultRowByPos(pResultBuf, p1); } + // 1. close current opened time window + if (pResultRowInfo->cur.pageId != -1 && ((pResult == NULL) || (pResult->pageId != pResultRowInfo->cur.pageId && + pResult->offset != pResultRowInfo->cur.offset))) { + // todo extract function + SResultRowPosition pos = pResultRowInfo->cur; + SFilePage* pPage = getBufPage(pResultBuf, pos.pageId); + SResultRow* pRow = (SResultRow*)((char*)pPage + pos.offset); + closeResultRow(pRow); + releaseBufPage(pResultBuf, pPage); + } + + // allocate a new buffer page + prepareResultListBuffer(pResultRowInfo, pTaskInfo->env); + if (pResult == NULL) { + pResult = getNewResultRow_rv(pResultBuf, groupId, pSup->resultRowSize); + initResultRow(pResult); + + // add a new result set for a new group + SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset}; + taosHashPut(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes), &pos, sizeof(SResultRowPosition)); + } + + // 2. set the new time window to be the new active time window + pResultRowInfo->pPosition[pResultRowInfo->size++] = + (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset}; + pResultRowInfo->cur = (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset}; + // too many time window in query if (pResultRowInfo->size > MAX_INTERVAL_TIME_WINDOW) { longjmp(pTaskInfo->env, TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW); @@ -656,7 +629,7 @@ static int32_t setResultOutputBufByKey_rv(SResultRowInfo* pResultRowInfo, int64_ int32_t numOfOutput, int32_t* rowCellInfoOffset, SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo) { assert(win->skey <= win->ekey); - SResultRow* pResultRow = doSetResultOutBufByKey_rv(pAggSup->pResultBuf, pResultRowInfo, id, (char*)&win->skey, + SResultRow* pResultRow = doSetResultOutBufByKey(pAggSup->pResultBuf, pResultRowInfo, id, (char*)&win->skey, TSDB_KEYSIZE, masterscan, tableGroupId, pTaskInfo, true, pAggSup); if (pResultRow == NULL) { @@ -1707,7 +1680,7 @@ int32_t setGroupResultOutputBuf(SOptrBasicInfo* binfo, int32_t numOfCols, char* SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo; SqlFunctionCtx* pCtx = binfo->pCtx; - SResultRow* pResultRow = doSetResultOutBufByKey_rv(pBuf, pResultRowInfo, groupId, (char*)pData, bytes, true, groupId, + SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, groupId, (char*)pData, bytes, true, groupId, pTaskInfo, false, pAggSup); assert(pResultRow != NULL); @@ -2713,7 +2686,7 @@ void setFunctionResultOutput(SOptrBasicInfo* pInfo, SAggSupporter* pSup, int32_t int64_t tid = 0; int64_t groupId = 0; - SResultRow* pRow = doSetResultOutBufByKey_rv(pSup->pResultBuf, pResultRowInfo, tid, (char*)&tid, sizeof(tid), true, + SResultRow* pRow = doSetResultOutBufByKey(pSup->pResultBuf, pResultRowInfo, tid, (char*)&tid, sizeof(tid), true, groupId, pTaskInfo, false, pSup); for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) { @@ -3001,7 +2974,7 @@ void doSetTableGroupOutputBuf(SAggOperatorInfo* pAggInfo, int32_t numOfOutput, u int32_t* rowCellInfoOffset = pAggInfo->binfo.rowCellInfoOffset; SResultRow* pResultRow = - doSetResultOutBufByKey_rv(pAggInfo->aggSup.pResultBuf, pResultRowInfo, uid, (char*)&groupId, sizeof(groupId), + doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, uid, (char*)&groupId, sizeof(groupId), true, groupId, pTaskInfo, false, &pAggInfo->aggSup); assert(pResultRow != NULL); @@ -4895,8 +4868,8 @@ bool aggDecodeResultRow(SOperatorInfo* pOperator, SAggSupporter* pSup, SOptrBasi initResultRow(resultRow); prepareResultListBuffer(&pInfo->resultRowInfo, pOperator->pTaskInfo->env); // pInfo->resultRowInfo.cur = pInfo->resultRowInfo.size; - pInfo->resultRowInfo.pPosition[pInfo->resultRowInfo.size++] = - (SResultRowPosition){.pageId = resultRow->pageId, .offset = resultRow->offset}; +// pInfo->resultRowInfo.pPosition[pInfo->resultRowInfo.size++] = +// (SResultRowPosition){.pageId = resultRow->pageId, .offset = resultRow->offset}; pInfo->resultRowInfo.cur = (SResultRowPosition){.pageId = resultRow->pageId, .offset = resultRow->offset}; } @@ -5609,10 +5582,10 @@ int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t n pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput); pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t)); pAggSup->pResultRowHashTable = taosHashInit(10, hashFn, true, HASH_NO_LOCK); - pAggSup->pResultRowListSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK); - pAggSup->pResultRowArrayList = taosArrayInit(10, sizeof(SResultRowCell)); +// pAggSup->pResultRowListSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK); +// pAggSup->pResultRowArrayList = taosArrayInit(10, sizeof(SResultRowCell)); - if (pAggSup->keyBuf == NULL || pAggSup->pResultRowArrayList == NULL || pAggSup->pResultRowListSet == NULL || + if (pAggSup->keyBuf == NULL /*|| pAggSup->pResultRowArrayList == NULL || pAggSup->pResultRowListSet == NULL*/ || pAggSup->pResultRowHashTable == NULL) { return TSDB_CODE_OUT_OF_MEMORY; } @@ -5628,8 +5601,8 @@ int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t n static void cleanupAggSup(SAggSupporter* pAggSup) { taosMemoryFreeClear(pAggSup->keyBuf); taosHashCleanup(pAggSup->pResultRowHashTable); - taosHashCleanup(pAggSup->pResultRowListSet); - taosArrayDestroy(pAggSup->pResultRowArrayList); +// taosHashCleanup(pAggSup->pResultRowListSet); +// taosArrayDestroy(pAggSup->pResultRowArrayList); destroyDiskbasedBuf(pAggSup->pResultBuf); } @@ -6352,7 +6325,7 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo int32_t numOfCols = 0; tsdbReaderT pDataReader = doCreateDataReader(pTableScanNode, pHandle, pTableGroupInfo, (uint64_t)queryId, taskId); - if (pDataReader == NULL) { + if (pDataReader == NULL && terrno != 0) { return NULL; } From e3e5f8d113b26ee54ef1c5ebf17b0c5870803fd9 Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Mon, 25 Apr 2022 16:46:32 +0800 Subject: [PATCH 41/51] refactor --- source/dnode/vnode/src/inc/tsdb.h | 2 +- source/dnode/vnode/src/vnd/vnodeSvr.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index f751c860bd..dddc170813 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -56,7 +56,7 @@ int32_t tsdbInsertTSmaData(STsdb *pTsdb, int64_t indexUid, const char *msg); int32_t tsdbDropTSmaData(STsdb *pTsdb, int64_t indexUid); int32_t tsdbInsertRSmaData(STsdb *pTsdb, char *msg); void tsdbCleanupReadHandle(tsdbReaderT queryHandle); -int tdScanAndConvertSubmitMsg(SSubmitReq *pMsg); +int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg); typedef enum { TSDB_FILE_HEAD = 0, // .head TSDB_FILE_DATA, // .data diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 17bdcdf964..66cc928c12 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -83,7 +83,7 @@ int vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg case TDMT_VND_SUBMIT: pRsp->msgType = TDMT_VND_SUBMIT_RSP; vnodeProcessSubmitReq(pVnode, ptr, pRsp); - tsdbTriggerRSma(pVnode->pTsdb, pVnode->pMeta, pMsg->pCont, STREAM_DATA_TYPE_SUBMIT_BLOCK); + tsdbTriggerRSma(pVnode->pTsdb, pVnode->pMeta, ptr, STREAM_DATA_TYPE_SUBMIT_BLOCK); break; case TDMT_VND_MQ_VG_CHANGE: if (tqProcessVgChangeReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), From 56f3c9bf612f8a3c1b0bf3413f6ec2c6bceb7b0e Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 25 Apr 2022 17:12:28 +0800 Subject: [PATCH 42/51] fix(query): get the correct data block column number. --- source/libs/executor/src/executorimpl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 321e613f82..d0da970b2c 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -268,7 +268,6 @@ SSDataBlock* createResDataBlock(SDataBlockDescNode* pNode) { int32_t numOfCols = LIST_LENGTH(pNode->pSlots); SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock)); - pBlock->info.numOfCols = numOfCols; pBlock->pDataBlock = taosArrayInit(numOfCols, sizeof(SColumnInfoData)); pBlock->info.blockId = pNode->dataBlockId; @@ -294,6 +293,7 @@ SSDataBlock* createResDataBlock(SDataBlockDescNode* pNode) { taosArrayPush(pBlock->pDataBlock, &idata); } + pBlock->info.numOfCols = taosArrayGetSize(pBlock->pDataBlock); return pBlock; } @@ -1032,6 +1032,8 @@ static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunc pColInfo->info.bytes = tDataTypes[type].bytes; pInput->pData[paramIndex] = pColInfo; + } else { + pColInfo = pInput->pData[paramIndex]; } ASSERT(!IS_VAR_DATA_TYPE(type)); From 1d22c07188c102a7e55b15803ad2a9f8df02fe21 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 25 Apr 2022 17:16:28 +0800 Subject: [PATCH 43/51] remove hb --- source/client/src/clientHb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 653f9a0634..b11a49fa1a 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -668,7 +668,7 @@ int hbMgrInit() { hbMgrInitHandle(); // init backgroud thread - hbCreateThread(); + /*hbCreateThread();*/ return 0; } From 8c36142a9c63b7180764be1b642ac6f04fc6d001 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Mon, 25 Apr 2022 17:16:47 +0800 Subject: [PATCH 44/51] fix(query): fix scalar function input is constant return var type value contains illegal characters. TD-15116 --- source/libs/function/src/builtins.c | 2 +- source/libs/scalar/src/scalar.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index a52e39bd4f..70087ee46b 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -355,7 +355,7 @@ static int32_t translateToIso8601(SFunctionNode* pFunc, char* pErrBuf, int32_t l return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName); } - pFunc->node.resType = (SDataType) { .bytes = 24, .type = TSDB_DATA_TYPE_BINARY}; + pFunc->node.resType = (SDataType) { .bytes = 64, .type = TSDB_DATA_TYPE_BINARY}; return TSDB_CODE_SUCCESS; } diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 820a4894b5..d6c0989f19 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -489,7 +489,7 @@ EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) { if (fmIsUserDefinedFunc(node->funcId)) { return DEAL_RES_CONTINUE; } - + FOREACH(tnode, node->pParameterList) { if (!SCL_IS_CONST_NODE(tnode)) { return DEAL_RES_CONTINUE; @@ -517,8 +517,10 @@ EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) { res->node.resType = node->node.resType; int32_t type = output.columnData->info.type; if (IS_VAR_DATA_TYPE(type)) { - res->datum.p = output.columnData->pData; - output.columnData->pData = NULL; + size_t tLen = varDataLen(output.columnData->pData); + ASSERT(tLen <= res->node.resType.bytes); + res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1); + memcpy(res->datum.p, output.columnData->pData, tLen); } else { memcpy(nodesGetValueFromNode(res), output.columnData->pData, tDataTypes[type].bytes); } From 28855b81b1e3b96ec15967f05b9eda0cc8b625b1 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Mon, 25 Apr 2022 17:16:47 +0800 Subject: [PATCH 45/51] fix(query): fix scalar function input is constant return var type value contains illegal characters. TD-15116 --- source/libs/scalar/src/scalar.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index d6c0989f19..6773f8192b 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -517,10 +517,8 @@ EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) { res->node.resType = node->node.resType; int32_t type = output.columnData->info.type; if (IS_VAR_DATA_TYPE(type)) { - size_t tLen = varDataLen(output.columnData->pData); - ASSERT(tLen <= res->node.resType.bytes); res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1); - memcpy(res->datum.p, output.columnData->pData, tLen); + memcpy(res->datum.p, output.columnData->pData, varDataTLen(output.columnData->pData)); } else { memcpy(nodesGetValueFromNode(res), output.columnData->pData, tDataTypes[type].bytes); } From f2ea7c1fbd2979fe0154c1d69d191b217d54ea49 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 25 Apr 2022 18:17:06 +0800 Subject: [PATCH 46/51] fix: add errstr for sync --- source/util/src/terror.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 9332cb481e..0e2c322317 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -403,6 +403,9 @@ TAOS_DEFINE_ERROR(TSDB_CODE_SYN_INVALID_CHECKSUM, "Invalid msg checksum" TAOS_DEFINE_ERROR(TSDB_CODE_SYN_INVALID_MSGLEN, "Invalid msg length") TAOS_DEFINE_ERROR(TSDB_CODE_SYN_INVALID_MSGTYPE, "Invalid msg type") +TAOS_DEFINE_ERROR(TSDB_CODE_SYN_NOT_LEADER, "Sync not leader") +TAOS_DEFINE_ERROR(TSDB_CODE_SYN_INTERNAL_ERROR, "Sync internal error") + // wal TAOS_DEFINE_ERROR(TSDB_CODE_WAL_APP_ERROR, "Unexpected generic error in wal") TAOS_DEFINE_ERROR(TSDB_CODE_WAL_FILE_CORRUPTED, "WAL file is corrupted") From 3fc1ce71b3e5ad1855c73806d244a1ca37c4c843 Mon Sep 17 00:00:00 2001 From: Li Minghao Date: Mon, 25 Apr 2022 03:26:13 -0700 Subject: [PATCH 47/51] call vnodeStart after vnodeOpen --- source/dnode/mgmt/mgmt_vnode/src/vmHandle.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c index 4cc1b8527c..7fc263c93c 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c @@ -202,6 +202,17 @@ int32_t vmProcessCreateVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pMsg) { return code; } + code = vnodeStart(pImpl); + if (code != 0) { + tFreeSCreateVnodeReq(&createReq); + dError("vgId:%d, failed to start sync since %s", createReq.vgId, terrstr()); + vnodeClose(pImpl); + vnodeDestroy(path, pMgmt->pTfs); + terrno = code; + return code; + } + + code = vmWriteVnodesToFile(pMgmt); if (code != 0) { tFreeSCreateVnodeReq(&createReq); From 39daf0bc98353c3fb217902419138fb6c91ed1e3 Mon Sep 17 00:00:00 2001 From: Li Minghao Date: Mon, 25 Apr 2022 03:34:29 -0700 Subject: [PATCH 48/51] call vnodeStart after vnodeOpen --- source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index 38f6d7648f..f995979ffd 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -147,7 +147,7 @@ static void *vmOpenVnodeFunc(void *param) { pThread->failed++; } else { vmOpenVnode(pMgmt, pCfg, pImpl); - vnodeStart(pImpl); + //vnodeStart(pImpl); dDebug("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex); pThread->opened++; } @@ -431,4 +431,4 @@ void vmGetVnodeLoads(SMgmtWrapper *pWrapper, SMonVloadInfo *pInfo) { } taosRUnLockLatch(&pMgmt->latch); -} \ No newline at end of file +} From c2b06004aa78ed69a355b347673abf47b855a0a2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 25 Apr 2022 19:21:07 +0800 Subject: [PATCH 49/51] fix: crash in multiprocess mode --- source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index f995979ffd..3088c5dea4 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -380,6 +380,7 @@ static int32_t vmStart(SMgmtWrapper *pWrapper) { } static void vmStop(SMgmtWrapper *pWrapper) { +#if 0 dDebug("vnode-mgmt start to stop"); SVnodesMgmt *pMgmt = pWrapper->pMgmt; taosRLockLatch(&pMgmt->latch); @@ -395,6 +396,7 @@ static void vmStop(SMgmtWrapper *pWrapper) { } taosRUnLockLatch(&pMgmt->latch); +#endif } void vmSetMgmtFp(SMgmtWrapper *pWrapper) { From 5a91ecfb7c04771ebf4a9a28e7ceae36973da7a9 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 25 Apr 2022 19:29:15 +0800 Subject: [PATCH 50/51] fix: write histroy in shell --- tools/shell/src/shellEngine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index ac2be3c3cf..1f036ab25b 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -750,7 +750,7 @@ void shellReadHistory() { void shellWriteHistory() { SShellHistory *pHistory = &shell.history; - TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_WRITE | TD_FILE_STREAM); + TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND); if (pFile == NULL) return; for (int32_t i = pHistory->hstart; i != pHistory->hend;) { From 9c510c9071bebe76a3233fb03597c276e8a51f9d Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 25 Apr 2022 21:48:52 +0800 Subject: [PATCH 51/51] enh(tmq): retry ask ep --- include/client/taos.h | 66 ++++---- include/common/tmsg.h | 2 +- include/common/tmsgdef.h | 2 +- include/util/ttimer.h | 4 - source/client/src/tmq.c | 336 ++++++++++++++++----------------------- 5 files changed, 170 insertions(+), 240 deletions(-) diff --git a/include/client/taos.h b/include/client/taos.h index 6afbcee6f1..72cb7bfa96 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -93,12 +93,12 @@ typedef struct taosField { typedef void (*__taos_async_fn_t)(void *param, TAOS_RES *, int code); typedef struct TAOS_BIND_v2 { - int buffer_type; - void *buffer; - int32_t buffer_length; - int32_t *length; - char *is_null; - int num; + int buffer_type; + void *buffer; + int32_t buffer_length; + int32_t *length; + char *is_null; + int num; } TAOS_BIND_v2; typedef enum { @@ -128,35 +128,35 @@ DLL_EXPORT void taos_close(TAOS *taos); const char *taos_data_type(int type); -DLL_EXPORT TAOS_STMT *taos_stmt_init(TAOS *taos); -DLL_EXPORT int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length); -DLL_EXPORT int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_BIND_v2 *tags); -DLL_EXPORT int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name); -DLL_EXPORT int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name); +DLL_EXPORT TAOS_STMT *taos_stmt_init(TAOS *taos); +DLL_EXPORT int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length); +DLL_EXPORT int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_BIND_v2 *tags); +DLL_EXPORT int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name); +DLL_EXPORT int taos_stmt_set_sub_tbname(TAOS_STMT *stmt, const char *name); -DLL_EXPORT int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert); -DLL_EXPORT int taos_stmt_num_params(TAOS_STMT *stmt, int *nums); -DLL_EXPORT int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes); -DLL_EXPORT int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_BIND_v2 *bind); -DLL_EXPORT int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_BIND_v2 *bind); -DLL_EXPORT int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_BIND_v2 *bind, int colIdx); -DLL_EXPORT int taos_stmt_add_batch(TAOS_STMT *stmt); -DLL_EXPORT int taos_stmt_execute(TAOS_STMT *stmt); -DLL_EXPORT TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt); -DLL_EXPORT int taos_stmt_close(TAOS_STMT *stmt); -DLL_EXPORT char *taos_stmt_errstr(TAOS_STMT *stmt); -DLL_EXPORT int taos_stmt_affected_rows(TAOS_STMT *stmt); -DLL_EXPORT int taos_stmt_affected_rows_once(TAOS_STMT *stmt); +DLL_EXPORT int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert); +DLL_EXPORT int taos_stmt_num_params(TAOS_STMT *stmt, int *nums); +DLL_EXPORT int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes); +DLL_EXPORT int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_BIND_v2 *bind); +DLL_EXPORT int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_BIND_v2 *bind); +DLL_EXPORT int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_BIND_v2 *bind, int colIdx); +DLL_EXPORT int taos_stmt_add_batch(TAOS_STMT *stmt); +DLL_EXPORT int taos_stmt_execute(TAOS_STMT *stmt); +DLL_EXPORT TAOS_RES *taos_stmt_use_result(TAOS_STMT *stmt); +DLL_EXPORT int taos_stmt_close(TAOS_STMT *stmt); +DLL_EXPORT char *taos_stmt_errstr(TAOS_STMT *stmt); +DLL_EXPORT int taos_stmt_affected_rows(TAOS_STMT *stmt); +DLL_EXPORT int taos_stmt_affected_rows_once(TAOS_STMT *stmt); -DLL_EXPORT TAOS_RES *taos_query(TAOS *taos, const char *sql); -DLL_EXPORT TAOS_RES *taos_query_l(TAOS *taos, const char *sql, int sqlLen); +DLL_EXPORT TAOS_RES *taos_query(TAOS *taos, const char *sql); +DLL_EXPORT TAOS_RES *taos_query_l(TAOS *taos, const char *sql, int sqlLen); -DLL_EXPORT TAOS_ROW taos_fetch_row(TAOS_RES *res); -DLL_EXPORT int taos_result_precision(TAOS_RES *res); // get the time precision of result -DLL_EXPORT void taos_free_result(TAOS_RES *res); -DLL_EXPORT int taos_field_count(TAOS_RES *res); -DLL_EXPORT int taos_num_fields(TAOS_RES *res); -DLL_EXPORT int taos_affected_rows(TAOS_RES *res); +DLL_EXPORT TAOS_ROW taos_fetch_row(TAOS_RES *res); +DLL_EXPORT int taos_result_precision(TAOS_RES *res); // get the time precision of result +DLL_EXPORT void taos_free_result(TAOS_RES *res); +DLL_EXPORT int taos_field_count(TAOS_RES *res); +DLL_EXPORT int taos_num_fields(TAOS_RES *res); +DLL_EXPORT int taos_affected_rows(TAOS_RES *res); DLL_EXPORT TAOS_FIELD *taos_fetch_fields(TAOS_RES *res); DLL_EXPORT int taos_select_db(TAOS *taos, const char *db); @@ -234,7 +234,7 @@ DLL_EXPORT tmq_t *tmq_consumer_new(tmq_conf_t *conf, char *errstr, int32_t errst DLL_EXPORT const char *tmq_err2str(tmq_resp_err_t); /* ------------------------TMQ CONSUMER INTERFACE------------------------ */ -DLL_EXPORT tmq_resp_err_t tmq_subscribe(tmq_t *tmq, tmq_list_t *topic_list); +DLL_EXPORT tmq_resp_err_t tmq_subscribe(tmq_t *tmq, const tmq_list_t *topic_list); DLL_EXPORT tmq_resp_err_t tmq_unsubscribe(tmq_t *tmq); DLL_EXPORT tmq_resp_err_t tmq_subscription(tmq_t *tmq, tmq_list_t **topics); DLL_EXPORT TAOS_RES *tmq_consumer_poll(tmq_t *tmq, int64_t blocking_time); diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 25d9fcdf85..3bde0b1f03 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1333,7 +1333,7 @@ typedef struct { typedef struct { int64_t consumerId; char cgroup[TSDB_CGROUP_LEN]; - SArray* topicNames; // SArray + SArray* topicNames; // SArray } SCMSubscribeReq; static FORCE_INLINE int32_t tSerializeSCMSubscribeReq(void** buf, const SCMSubscribeReq* pReq) { diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index 691d491eb0..97ee66a2da 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -145,7 +145,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_MND_ALTER_TOPIC, "mnode-alter-topic", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_DROP_TOPIC, "mnode-drop-topic", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_SUBSCRIBE, "mnode-subscribe", SCMSubscribeReq, SCMSubscribeRsp) - TD_DEF_MSG_TYPE(TDMT_MND_GET_SUB_EP, "mnode-get-sub-ep", SMqCMGetSubEpReq, SMqCMGetSubEpRsp) + TD_DEF_MSG_TYPE(TDMT_MND_GET_SUB_EP, "mnode-mq-ask-ep", SMqCMGetSubEpReq, SMqCMGetSubEpRsp) TD_DEF_MSG_TYPE(TDMT_MND_MQ_TIMER, "mnode-mq-tmr", SMTimerReq, SMTimerReq) TD_DEF_MSG_TYPE(TDMT_MND_MQ_CONSUMER_LOST, "mnode-mq-consumer-lost", SMTimerReq, SMTimerReq) TD_DEF_MSG_TYPE(TDMT_MND_MQ_DO_REBALANCE, "mnode-mq-do-rebalance", SMqDoRebalanceMsg, SMqDoRebalanceMsg) diff --git a/include/util/ttimer.h b/include/util/ttimer.h index f2ee825c4e..1022259631 100644 --- a/include/util/ttimer.h +++ b/include/util/ttimer.h @@ -41,10 +41,6 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int32_t mseconds, void *param, void *han void taosTmrCleanUp(void *handle); -int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); - -void taosUninitTimer(); - #ifdef __cplusplus } #endif diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index 988406c84d..f9540bc8fc 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -23,6 +23,9 @@ #include "tmsgtype.h" #include "tqueue.h" #include "tref.h" +#include "ttimer.h" + +int32_t tmqAskEp(tmq_t* tmq, bool sync); typedef struct { int8_t tmqRspType; @@ -61,29 +64,40 @@ struct tmq_conf_t { tmq_commit_cb* commit_cb; }; +typedef struct { + int8_t inited; + tmr_h timer; +} SMqMgmt; + +static SMqMgmt tmqMgmt = {0}; + struct tmq_t { // conf - char groupId[TSDB_CGROUP_LEN]; - char clientId[256]; - int8_t autoCommit; - /*int8_t inWaiting;*/ + char groupId[TSDB_CGROUP_LEN]; + char clientId[256]; + int8_t autoCommit; int64_t consumerId; - int32_t epoch; int32_t resetOffsetCfg; - int64_t status; - STscObj* pTscObj; tmq_commit_cb* commit_cb; - /*int32_t nextTopicIdx;*/ + + // status + int8_t status; int8_t epStatus; + int32_t epoch; int32_t epSkipCnt; - /*int32_t waitingRequest;*/ - /*int32_t readyRequest;*/ - SArray* clientTopics; // SArray - STaosQueue* mqueue; // queue of tmq_message_t - STaosQall* qall; - tsem_t rspSem; - // stat int64_t pollCnt; + + // connection + STscObj* pTscObj; + + // container + SArray* clientTopics; // SArray + STaosQueue* mqueue; // queue of rsp + STaosQall* qall; + STaosQueue* delayedTask; // delayed task queue for heartbeat and auto commit + + // ctl + tsem_t rspSem; }; enum { @@ -93,6 +107,7 @@ enum { enum { TMQ_CONSUMER_STATUS__INIT = 0, + TMQ_CONSUMER_STATUS__SUBSCRIBED, TMQ_CONSUMER_STATUS__READY, }; @@ -110,13 +125,11 @@ typedef struct { typedef struct { // subscribe info - int32_t sqlLen; - char* sql; - char* topicName; - int64_t topicId; - SArray* vgs; // SArray + char* topicName; + + SArray* vgs; // SArray + int8_t isSchemaAdaptive; - int32_t numOfFields; SSchemaWrapper schema; } SMqClientTopic; @@ -156,7 +169,6 @@ typedef struct { int32_t async; tsem_t rspSem; tmq_resp_err_t rspErr; - /*SMqClientVg* pVg;*/ } SMqCommitCbParam; tmq_conf_t* tmq_conf_new() { @@ -251,13 +263,7 @@ int32_t tmq_list_append(tmq_list_t* list, const char* src) { void tmq_list_destroy(tmq_list_t* list) { SArray* container = &list->container; - /*taosArrayDestroy(container);*/ - int32_t sz = taosArrayGetSize(container); - for (int32_t i = 0; i < sz; i++) { - char* str = taosArrayGetP(container, i); - taosMemoryFree(str); - } - taosArrayDestroy(container); + taosArrayDestroyP(container, taosMemoryFree); } int32_t tmq_list_get_size(const tmq_list_t* list) { @@ -298,6 +304,8 @@ void tmqClearUnhandleMsg(tmq_t* tmq) { int32_t tmqSubscribeCb(void* param, const SDataBuf* pMsg, int32_t code) { SMqSubscribeCbParam* pParam = (SMqSubscribeCbParam*)param; pParam->rspErr = code; + tmq_t* tmq = pParam->tmq; + atomic_store_8(&tmq->status, TMQ_CONSUMER_STATUS__SUBSCRIBED); tsem_post(&pParam->rspSem); return 0; } @@ -335,12 +343,9 @@ tmq_t* tmq_consumer_new(void* conn, tmq_conf_t* conf, char* errstr, int32_t errs return NULL; } pTmq->pTscObj = (STscObj*)conn; - /*pTmq->inWaiting = 0;*/ pTmq->status = 0; pTmq->pollCnt = 0; pTmq->epoch = 0; - /*pTmq->waitingRequest = 0;*/ - /*pTmq->readyRequest = 0;*/ pTmq->epStatus = 0; pTmq->epSkipCnt = 0; // set conf @@ -367,26 +372,45 @@ tmq_t* tmq_consumer_new(void* conn, tmq_conf_t* conf, char* errstr, int32_t errs #endif tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) { + // init timer + int8_t inited = atomic_val_compare_exchange_8(&tmqMgmt.inited, 0, 1); + if (inited == 0) { + tmqMgmt.timer = taosTmrInit(1000, 100, 360000, "TMQ"); + if (tmqMgmt.timer == NULL) { + atomic_store_8(&tmqMgmt.inited, 0); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + } + tmq_t* pTmq = taosMemoryCalloc(1, sizeof(tmq_t)); if (pTmq == NULL) { return NULL; } + const char* user = conf->user == NULL ? TSDB_DEFAULT_USER : conf->user; const char* pass = conf->pass == NULL ? TSDB_DEFAULT_PASS : conf->pass; ASSERT(user); ASSERT(pass); - /*ASSERT(conf->db);*/ ASSERT(conf->groupId[0]); - pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, conf->db, conf->port, CONN_TYPE__TMQ); - if (pTmq->pTscObj == NULL) return NULL; + pTmq->clientTopics = taosArrayInit(0, sizeof(SMqClientTopic)); + pTmq->mqueue = taosOpenQueue(); + pTmq->qall = taosAllocateQall(); + pTmq->delayedTask = taosOpenQueue(); - pTmq->status = 0; + if (pTmq->clientTopics == NULL || pTmq->mqueue == NULL || pTmq->qall == NULL || pTmq->delayedTask == NULL) { + goto FAIL; + } + + // init status + pTmq->status = TMQ_CONSUMER_STATUS__INIT; pTmq->pollCnt = 0; pTmq->epoch = 0; pTmq->epStatus = 0; pTmq->epSkipCnt = 0; + // set conf strcpy(pTmq->clientId, conf->clientId); strcpy(pTmq->groupId, conf->groupId); @@ -394,19 +418,30 @@ tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) { pTmq->commit_cb = conf->commit_cb; pTmq->resetOffsetCfg = conf->resetOffset; + // assign consumerId pTmq->consumerId = tGenIdPI64(); - pTmq->clientTopics = taosArrayInit(0, sizeof(SMqClientTopic)); - if (pTmq->clientTopics == NULL) { - taosMemoryFree(pTmq); - return NULL; + + // init semaphore + if (tsem_init(&pTmq->rspSem, 0, 0) != 0) { + goto FAIL; } - pTmq->mqueue = taosOpenQueue(); - pTmq->qall = taosAllocateQall(); - - tsem_init(&pTmq->rspSem, 0, 0); + // init connection + pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, NULL, conf->port, CONN_TYPE__TMQ); + if (pTmq->pTscObj == NULL) { + tsem_destroy(&pTmq->rspSem); + goto FAIL; + } return pTmq; + +FAIL: + if (pTmq->clientTopics) taosArrayDestroy(pTmq->clientTopics); + if (pTmq->mqueue) taosCloseQueue(pTmq->mqueue); + if (pTmq->delayedTask) taosCloseQueue(pTmq->delayedTask); + if (pTmq->qall) taosFreeQall(pTmq->qall); + taosMemoryFree(pTmq); + return NULL; } tmq_resp_err_t tmq_commit(tmq_t* tmq, const tmq_topic_vgroup_list_t* offsets, int32_t async) { @@ -497,86 +532,64 @@ tmq_resp_err_t tmq_commit(tmq_t* tmq, const tmq_topic_vgroup_list_t* offsets, in return resp; } -tmq_resp_err_t tmq_subscribe(tmq_t* tmq, tmq_list_t* topic_list) { - SRequestObj* pRequest = NULL; - SArray* container = &topic_list->container; - int32_t sz = taosArrayGetSize(container); - // destroy ex - taosArrayDestroy(tmq->clientTopics); - tmq->clientTopics = taosArrayInit(sz, sizeof(SMqClientTopic)); +tmq_resp_err_t tmq_subscribe(tmq_t* tmq, const tmq_list_t* topic_list) { + const SArray* container = &topic_list->container; + int32_t sz = taosArrayGetSize(container); + void* buf = NULL; + SCMSubscribeReq req = {0}; + int32_t code = -1; - SCMSubscribeReq req; req.consumerId = tmq->consumerId; - strcpy(req.cgroup, tmq->groupId); + tstrncpy(req.cgroup, tmq->groupId, TSDB_CGROUP_LEN); req.topicNames = taosArrayInit(sz, sizeof(void*)); + if (req.topicNames == NULL) goto FAIL; - for (int i = 0; i < sz; i++) { - /*char* topicName = topic_list->elems[i];*/ - char* topicName = taosArrayGetP(container, i); + for (int32_t i = 0; i < sz; i++) { + char* topic = taosArrayGetP(container, i); SName name = {0}; -#if 0 - char* dbName = getDbOfConnection(tmq->pTscObj); - if (dbName == NULL) { - return TMQ_RESP_ERR__FAIL; - } -#endif - tNameSetDbName(&name, tmq->pTscObj->acctId, topicName, strlen(topicName)); -#if 0 - tNameFromString(&name, topicName, T_NAME_TABLE); -#endif + tNameSetDbName(&name, tmq->pTscObj->acctId, topic, strlen(topic)); - char* topicFname = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN); - if (topicFname == NULL) { - goto _return; + char* topicFName = taosMemoryCalloc(1, TSDB_TOPIC_FNAME_LEN); + if (topicFName == NULL) { + goto FAIL; } - tNameExtractFullName(&name, topicFname); - tscDebug("subscribe topic: %s", topicFname); - SMqClientTopic topic = { - .sql = NULL, - .sqlLen = 0, - .topicId = 0, - .topicName = topicFname, - .vgs = NULL, - }; - topic.vgs = taosArrayInit(0, sizeof(SMqClientVg)); - taosArrayPush(tmq->clientTopics, &topic); - taosArrayPush(req.topicNames, &topicFname); -#if 0 - taosMemoryFree(dbName); -#endif + tNameExtractFullName(&name, topicFName); + + tscDebug("subscribe topic: %s", topicFName); + + taosArrayPush(req.topicNames, &topicFName); } - int tlen = tSerializeSCMSubscribeReq(NULL, &req); - void* buf = taosMemoryMalloc(tlen); - if (buf == NULL) { - goto _return; - } + int32_t tlen = tSerializeSCMSubscribeReq(NULL, &req); + buf = taosMemoryMalloc(tlen); + if (buf == NULL) goto FAIL; void* abuf = buf; tSerializeSCMSubscribeReq(&abuf, &req); - /*printf("formatted: %s\n", dagStr);*/ - pRequest = createRequest(tmq->pTscObj, NULL, NULL, TDMT_MND_SUBSCRIBE); - if (pRequest == NULL) { - tscError("failed to malloc request"); - } + SMsgSendInfo* sendInfo = taosMemoryMalloc(sizeof(SMsgSendInfo)); + if (sendInfo == NULL) goto FAIL; SMqSubscribeCbParam param = { .rspErr = TMQ_RESP_ERR__SUCCESS, .tmq = tmq, }; - tsem_init(¶m.rspSem, 0, 0); - pRequest->body.requestMsg = (SDataBuf){ + if (tsem_init(¶m.rspSem, 0, 0) != 0) goto FAIL; + + sendInfo->msgInfo = (SDataBuf){ .pData = buf, .len = tlen, .handle = NULL, }; - SMsgSendInfo* sendInfo = buildMsgInfoImpl(pRequest); + sendInfo->requestId = generateRequestId(); + sendInfo->requestObjRefId = 0; sendInfo->param = ¶m; sendInfo->fp = tmqSubscribeCb; + sendInfo->msgType = TDMT_MND_SUBSCRIBE; + SEpSet epSet = getEpSet_s(&tmq->pTscObj->pAppInfo->mgmtEp); int64_t transporterId = 0; @@ -585,15 +598,28 @@ tmq_resp_err_t tmq_subscribe(tmq_t* tmq, tmq_list_t* topic_list) { tsem_wait(¶m.rspSem); tsem_destroy(¶m.rspSem); -_return: - /*if (sendInfo != NULL) {*/ - /*destroySendMsgInfo(sendInfo);*/ - /*}*/ + code = param.rspErr; + if (code != 0) goto FAIL; - return param.rspErr; + // TODO: add max retry cnt + while (TSDB_CODE_MND_CONSUMER_NOT_READY == tmqAskEp(tmq, true)) { + tscDebug("not ready, retry\n"); + taosMsleep(500); + } + + code = 0; +FAIL: + if (req.topicNames != NULL) taosArrayDestroyP(req.topicNames, taosMemoryFree); + if (code != 0) { + taosMemoryFree(buf); + } + return code; } -void tmq_conf_set_offset_commit_cb(tmq_conf_t* conf, tmq_commit_cb* cb) { conf->commit_cb = cb; } +void tmq_conf_set_offset_commit_cb(tmq_conf_t* conf, tmq_commit_cb* cb) { + // + conf->commit_cb = cb; +} TAOS_RES* tmq_create_stream(TAOS* taos, const char* streamName, const char* tbName, const char* sql) { STscObj* pTscObj = (STscObj*)taos; @@ -627,9 +653,6 @@ TAOS_RES* tmq_create_stream(TAOS* taos, const char* streamName, const char* tbNa int32_t code = 0; CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return); CHECK_CODE_GOTO(parseSql(pRequest, false, &pQueryNode, NULL), _return); - - // todo check for invalid sql statement and return with error code - CHECK_CODE_GOTO(nodesNodeToString(pQueryNode->pRoot, false, &astStr, NULL), _return); /*printf("%s\n", pStr);*/ @@ -653,7 +676,6 @@ TAOS_RES* tmq_create_stream(TAOS* taos, const char* streamName, const char* tbNa } tSerializeSCMCreateStreamReq(buf, tlen, &req); - /*printf("formatted: %s\n", dagStr);*/ pRequest->body.requestMsg = (SDataBuf){ .pData = buf, @@ -684,94 +706,6 @@ _return: return pRequest; } -#if 0 -TAOS_RES* tmq_create_topic(TAOS* taos, const char* topicName, const char* sql, int sqlLen) { - STscObj* pTscObj = (STscObj*)taos; - SRequestObj* pRequest = NULL; - SQuery* pQueryNode = NULL; - char* astStr = NULL; - - terrno = TSDB_CODE_SUCCESS; - if (taos == NULL || topicName == NULL || sql == NULL) { - tscError("invalid parameters for creating topic, connObj:%p, topic name:%s, sql:%s", taos, topicName, sql); - terrno = TSDB_CODE_TSC_INVALID_INPUT; - goto _return; - } - - if (strlen(topicName) >= TSDB_TOPIC_NAME_LEN) { - tscError("topic name too long, max length:%d", TSDB_TOPIC_NAME_LEN - 1); - terrno = TSDB_CODE_TSC_INVALID_INPUT; - goto _return; - } - - if (sqlLen > TSDB_MAX_ALLOWED_SQL_LEN) { - tscError("sql string exceeds max length:%d", TSDB_MAX_ALLOWED_SQL_LEN); - terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT; - goto _return; - } - - tscDebug("start to create topic: %s", topicName); - - int32_t code = TSDB_CODE_SUCCESS; - CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return); - CHECK_CODE_GOTO(parseSql(pRequest, true, &pQueryNode), _return); - - // todo check for invalid sql statement and return with error code - - CHECK_CODE_GOTO(nodesNodeToString(pQueryNode->pRoot, false, &astStr, NULL), _return); - - /*printf("%s\n", pStr);*/ - - SName name = {.acctId = pTscObj->acctId, .type = TSDB_TABLE_NAME_T}; - strcpy(name.dbname, pRequest->pDb); - strcpy(name.tname, topicName); - - SCMCreateTopicReq req = { - .igExists = 1, - .ast = astStr, - .sql = (char*)sql, - }; - tNameExtractFullName(&name, req.name); - - int tlen = tSerializeSCMCreateTopicReq(NULL, 0, &req); - void* buf = taosMemoryMalloc(tlen); - if (buf == NULL) { - goto _return; - } - - tSerializeSCMCreateTopicReq(buf, tlen, &req); - /*printf("formatted: %s\n", dagStr);*/ - - pRequest->body.requestMsg = (SDataBuf){ - .pData = buf, - .len = tlen, - .handle = NULL, - }; - pRequest->type = TDMT_MND_CREATE_TOPIC; - - SMsgSendInfo* sendInfo = buildMsgInfoImpl(pRequest); - SEpSet epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp); - - int64_t transporterId = 0; - asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &epSet, &transporterId, sendInfo); - - tsem_wait(&pRequest->body.rspSem); - -_return: - taosMemoryFreeClear(astStr); - qDestroyQuery(pQueryNode); - /*if (sendInfo != NULL) {*/ - /*destroySendMsgInfo(sendInfo);*/ - /*}*/ - - if (pRequest != NULL && terrno != TSDB_CODE_SUCCESS) { - pRequest->code = terrno; - } - - return pRequest; -} -#endif - #if 0 int32_t tmqGetSkipLogNum(tmq_message_t* tmq_message) { if (tmq_message == NULL) return 0; @@ -954,7 +888,7 @@ int32_t tmqAskEpCb(void* param, const SDataBuf* pMsg, int32_t code) { /*printf("rsp epoch %ld sz %ld\n", rsp.epoch, rsp.topics->size);*/ /*printf("tmq epoch %ld sz %ld\n", tmq->epoch, tmq->clientTopics->size);*/ if (tmqUpdateEp(tmq, head->epoch, &rsp)) { - atomic_store_64(&tmq->status, TMQ_CONSUMER_STATUS__READY); + atomic_store_8(&tmq->status, TMQ_CONSUMER_STATUS__READY); } tDeleteSMqCMGetSubEpRsp(&rsp); } else { @@ -1189,7 +1123,6 @@ int32_t tmqPollImpl(tmq_t* tmq, int64_t blockingTime) { int64_t transporterId = 0; /*printf("send poll\n");*/ - /*atomic_add_fetch_32(&tmq->waitingRequest, 1);*/ tscDebug("consumer %ld send poll to %s : vg %d, epoch %d, req offset %ld, reqId %lu", tmq->consumerId, pTopic->topicName, pVg->vgId, tmq->epoch, pVg->currentOffset, pReq->reqId); /*printf("send vg %d %ld\n", pVg->vgId, pVg->currentOffset);*/ @@ -1268,12 +1201,14 @@ TAOS_RES* tmq_consumer_poll(tmq_t* tmq, int64_t blocking_time) { SMqRspObj* rspObj; int64_t startTime = taosGetTimestampMs(); - // TODO: put into another thread or delayed queue - int64_t status = atomic_load_64(&tmq->status); - while (0 != tmqAskEp(tmq, status == TMQ_CONSUMER_STATUS__INIT)) { + // TODO: put into delayed queue +#if 0 + int8_t status = atomic_load_8(&tmq->status); + while (0 != tmqAskEp(tmq, status != TMQ_CONSUMER_STATUS__READY)) { tscDebug("not ready, retry\n"); taosSsleep(1); } +#endif rspObj = tmqHandleAllRsp(tmq, blocking_time, false); if (rspObj) { @@ -1281,7 +1216,6 @@ TAOS_RES* tmq_consumer_poll(tmq_t* tmq, int64_t blocking_time) { } while (1) { - /*printf("cycle\n");*/ tmqAskEp(tmq, false); tmqPollImpl(tmq, blocking_time);