From 4e647ab0c65453f03c97f7bdfabebb31070a59bc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 2 Dec 2021 14:21:33 +0800 Subject: [PATCH 01/11] disable doc build --- cmake/cmake.options | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/cmake.options b/cmake/cmake.options index 4c082fe79a..105ff17c58 100644 --- a/cmake/cmake.options +++ b/cmake/cmake.options @@ -52,5 +52,5 @@ option( option( BUILD_DOCS "If use doxygen build documents" - ON + OFF ) From cb470d7afe15bca43a03448eb7c4db185c67287d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 2 Dec 2021 18:10:06 +0800 Subject: [PATCH 02/11] [TD-11760]: use sqlite to store suid to uid list --- deps/CMakeLists.txt | 5 +- source/dnode/vnode/meta/CMakeLists.txt | 1 + source/dnode/vnode/meta/inc/metaDB.h | 3 +- source/dnode/vnode/meta/src/metaDB.c | 77 ++++++++++++++++---------- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index afd606c403..0ef8c7e3be 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -82,7 +82,7 @@ endif(${BUILD_WITH_NURAFT}) # BDB if(${BUILD_WITH_BDB}) - add_library(bdb STATIC IMPORTED) + add_library(bdb STATIC IMPORTED GLOBAL) set_target_properties(bdb PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/bdb/libdb.a" INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/bdb" @@ -93,8 +93,9 @@ if(${BUILD_WITH_BDB}) endif(${BUILD_WITH_BDB}) # SQLite +# see https://stackoverflow.com/questions/8774593/cmake-link-to-external-library#comment58570736_10550334 if(${BUILD_WITH_SQLITE}) - add_library(sqlite STATIC IMPORTED) + add_library(sqlite STATIC IMPORTED GLOBAL) set_target_properties(sqlite PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/sqlite/.libs/libsqlite3.a" INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/sqlite" diff --git a/source/dnode/vnode/meta/CMakeLists.txt b/source/dnode/vnode/meta/CMakeLists.txt index 8d37f5842e..34de051441 100644 --- a/source/dnode/vnode/meta/CMakeLists.txt +++ b/source/dnode/vnode/meta/CMakeLists.txt @@ -7,6 +7,7 @@ target_include_directories( ) target_link_libraries( meta + PUBLIC sqlite PUBLIC common PUBLIC tkv ) diff --git a/source/dnode/vnode/meta/inc/metaDB.h b/source/dnode/vnode/meta/inc/metaDB.h index f758af0673..babff30e94 100644 --- a/source/dnode/vnode/meta/inc/metaDB.h +++ b/source/dnode/vnode/meta/inc/metaDB.h @@ -17,6 +17,7 @@ #define _TD_META_DB_H_ #include "rocksdb/c.h" +#include "sqlite3.h" #include "meta.h" @@ -29,7 +30,7 @@ typedef struct { rocksdb_t *nameDb; // name -> uid rocksdb_t *tagDb; // uid -> tag rocksdb_t *schemaDb; // uid+version -> schema - rocksdb_t *mapDb; // suid -> uid_list + sqlite3 * mapDb; // suid -> uid_list } meta_db_t; int metaOpenDB(SMeta *pMeta); diff --git a/source/dnode/vnode/meta/src/metaDB.c b/source/dnode/vnode/meta/src/metaDB.c index d1fb65d2ed..eec0910c07 100644 --- a/source/dnode/vnode/meta/src/metaDB.c +++ b/source/dnode/vnode/meta/src/metaDB.c @@ -17,7 +17,7 @@ static void metaSaveSchemaDB(SMeta *pMeta, tb_uid_t uid, STSchema *pSchema); static void metaGetSchemaDBKey(char key[], tb_uid_t uid, int sversion); -static int metaSaveMapDB(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid); +// static int metaSaveMapDB(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid); #define SCHEMA_KEY_LEN (sizeof(tb_uid_t) + sizeof(int)) @@ -65,8 +65,14 @@ int metaOpenDB(SMeta *pMeta) { META_OPEN_DB_IMPL(pMeta->pDB->schemaDb, options, dir, err); // mapDb - sprintf(dir, "%s/map_db", pMeta->path); - META_OPEN_DB_IMPL(pMeta->pDB->mapDb, options, dir, err); + sprintf(dir, "%s/meta.db", pMeta->path); + if (sqlite3_open(dir, &(pMeta->pDB->mapDb)) != SQLITE_OK) { + // TODO + } + + // // set read uncommitted + sqlite3_exec(pMeta->pDB->mapDb, "PRAGMA read_uncommitted=true;", 0, 0, 0); + sqlite3_exec(pMeta->pDB->mapDb, "BEGIN;", 0, 0, 0); rocksdb_options_destroy(options); return 0; @@ -82,7 +88,12 @@ int metaOpenDB(SMeta *pMeta) { void metaCloseDB(SMeta *pMeta) { if (pMeta->pDB) { - META_CLOSE_DB_IMPL(pMeta->pDB->mapDb); + if (pMeta->pDB->mapDb) { + sqlite3_exec(pMeta->pDB->mapDb, "COMMIT;", 0, 0, 0); + sqlite3_close(pMeta->pDB->mapDb); + pMeta->pDB->mapDb = NULL; + } + META_CLOSE_DB_IMPL(pMeta->pDB->schemaDb); META_CLOSE_DB_IMPL(pMeta->pDB->tagDb); META_CLOSE_DB_IMPL(pMeta->pDB->nameDb); @@ -97,6 +108,7 @@ int metaSaveTableToDB(SMeta *pMeta, const STbCfg *pTbOptions) { char * err = NULL; size_t size; char pBuf[1024]; // TODO + char sql[128]; rocksdb_writeoptions_t *wopt = rocksdb_writeoptions_create(); @@ -124,8 +136,12 @@ int metaSaveTableToDB(SMeta *pMeta, const STbCfg *pTbOptions) { // save schemaDB metaSaveSchemaDB(pMeta, uid, pTbOptions->stbCfg.pSchema); - // save mapDB (really need?) - rocksdb_put(pMeta->pDB->mapDb, wopt, (char *)(&uid), sizeof(uid), "", 0, &err); + // // save mapDB (really need?) + // rocksdb_put(pMeta->pDB->mapDb, wopt, (char *)(&uid), sizeof(uid), "", 0, &err); + sprintf(sql, "create table st_%" PRIu64 " (uid BIGINT);", uid); + if (sqlite3_exec(pMeta->pDB->mapDb, sql, NULL, NULL, &err) != SQLITE_OK) { + // fprintf(stderr, "Failed to create table, since %s\n", err); + } break; case META_CHILD_TABLE: // save tagDB @@ -133,7 +149,10 @@ int metaSaveTableToDB(SMeta *pMeta, const STbCfg *pTbOptions) { kvRowLen(pTbOptions->ctbCfg.pTag), &err); // save mapDB - metaSaveMapDB(pMeta, pTbOptions->ctbCfg.suid, uid); + sprintf(sql, "insert into st_%" PRIu64 " values (%" PRIu64 ");", pTbOptions->ctbCfg.suid, uid); + if (sqlite3_exec(pMeta->pDB->mapDb, sql, NULL, NULL, &err) != SQLITE_OK) { + fprintf(stderr, "failed to insert data, since %s\n", err); + } break; default: ASSERT(0); @@ -172,32 +191,32 @@ static void metaGetSchemaDBKey(char *key, tb_uid_t uid, int sversion) { *(int *)POINTER_SHIFT(key, sizeof(tb_uid_t)) = sversion; } -static int metaSaveMapDB(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid) { - size_t vlen; - char * val; - char * err = NULL; +// static int metaSaveMapDB(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid) { +// size_t vlen; +// char * val; +// char * err = NULL; - rocksdb_readoptions_t *ropt = rocksdb_readoptions_create(); - val = rocksdb_get(pMeta->pDB->mapDb, ropt, (char *)(&suid), sizeof(suid), &vlen, &err); - rocksdb_readoptions_destroy(ropt); +// rocksdb_readoptions_t *ropt = rocksdb_readoptions_create(); +// val = rocksdb_get(pMeta->pDB->mapDb, ropt, (char *)(&suid), sizeof(suid), &vlen, &err); +// rocksdb_readoptions_destroy(ropt); - void *nval = malloc(vlen + sizeof(uid)); - if (nval == NULL) { - return -1; - } +// void *nval = malloc(vlen + sizeof(uid)); +// if (nval == NULL) { +// return -1; +// } - if (vlen) { - memcpy(nval, val, vlen); - } - memcpy(POINTER_SHIFT(nval, vlen), (void *)(&uid), sizeof(uid)); +// if (vlen) { +// memcpy(nval, val, vlen); +// } +// memcpy(POINTER_SHIFT(nval, vlen), (void *)(&uid), sizeof(uid)); - rocksdb_writeoptions_t *wopt = rocksdb_writeoptions_create(); - rocksdb_writeoptions_disable_WAL(wopt, 1); +// rocksdb_writeoptions_t *wopt = rocksdb_writeoptions_create(); +// rocksdb_writeoptions_disable_WAL(wopt, 1); - rocksdb_put(pMeta->pDB->mapDb, wopt, (char *)(&suid), sizeof(suid), nval, vlen + sizeof(uid), &err); +// rocksdb_put(pMeta->pDB->mapDb, wopt, (char *)(&suid), sizeof(suid), nval, vlen + sizeof(uid), &err); - rocksdb_writeoptions_destroy(wopt); - free(nval); +// rocksdb_writeoptions_destroy(wopt); +// free(nval); - return 0; -} \ No newline at end of file +// return 0; +// } \ No newline at end of file From 6d7388de9109bddff5b130fbde14b126d2652dac Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 2 Dec 2021 19:56:43 +0800 Subject: [PATCH 03/11] TD-10431 process show msg --- include/common/taosmsg.h | 5 +- source/dnode/mgmt/impl/src/dndTransport.c | 2 +- source/dnode/mnode/impl/inc/mndDef.h | 34 +-- source/dnode/mnode/impl/inc/mndInt.h | 14 + source/dnode/mnode/impl/inc/mndShow.h | 4 + source/dnode/mnode/impl/src/mndCluster.c | 69 +++++ source/dnode/mnode/impl/src/mndConnect.c | 155 +++++++++++ source/dnode/mnode/impl/src/mndDb.c | 20 +- source/dnode/mnode/impl/src/mndShow.c | 314 +++++++++++++++++++++- 9 files changed, 593 insertions(+), 24 deletions(-) create mode 100644 source/dnode/mnode/impl/src/mndConnect.c diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 9bae38e3a7..826934c4a8 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -153,7 +153,8 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY9, "dummy9" ) #define TSDB_IE_TYPE_DNODE_EXT 6 #define TSDB_IE_TYPE_DNODE_STATE 7 -enum _mgmt_table { +typedef enum _mgmt_table { + TSDB_MGMT_TABLE_START, TSDB_MGMT_TABLE_ACCT, TSDB_MGMT_TABLE_USER, TSDB_MGMT_TABLE_DB, @@ -175,7 +176,7 @@ enum _mgmt_table { TSDB_MGMT_TABLE_TP, TSDB_MGMT_TABLE_FUNCTION, TSDB_MGMT_TABLE_MAX, -}; +} EShowType; #define TSDB_ALTER_TABLE_ADD_TAG_COLUMN 1 #define TSDB_ALTER_TABLE_DROP_TAG_COLUMN 2 diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index 44ca1d1407..f3e71564b2 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -157,7 +157,7 @@ static int32_t dndInitClient(SDnode *pDnode) { rpcInit.label = "DND-C"; rpcInit.numOfThreads = 1; rpcInit.cfp = dndProcessResponse; - rpcInit.sessions = 8; + rpcInit.sessions = 1024; rpcInit.connType = TAOS_CONN_CLIENT; rpcInit.idleTime = pDnode->opt.shellActivityTimer * 1000; rpcInit.user = INTERNAL_USER; diff --git a/source/dnode/mnode/impl/inc/mndDef.h b/source/dnode/mnode/impl/inc/mndDef.h index 9facb8829c..dc27656dbf 100644 --- a/source/dnode/mnode/impl/inc/mndDef.h +++ b/source/dnode/mnode/impl/inc/mndDef.h @@ -281,22 +281,24 @@ typedef struct SFuncObj { int16_t type; } SFuncObj; -typedef struct { - int8_t type; - int8_t maxReplica; - int16_t numOfColumns; - int32_t index; - int32_t rowSize; - int32_t numOfRows; - int32_t numOfReads; - uint16_t payloadLen; - void *pIter; - void *pVgIter; - void **ppShow; - char db[TSDB_FULL_DB_NAME_LEN]; - int16_t offset[TSDB_MAX_COLUMNS]; - int32_t bytes[TSDB_MAX_COLUMNS]; - char payload[]; +typedef struct SShowObj SShowObj; +typedef struct SShowObj { + int8_t type; + int8_t maxReplica; + int16_t numOfColumns; + int32_t id; + int32_t rowSize; + int32_t numOfRows; + int32_t numOfReads; + uint16_t payloadLen; + void *pIter; + void *pVgIter; + SMnode *pMnode; + SShowObj **ppShow; + char db[TSDB_FULL_DB_NAME_LEN]; + int16_t offset[TSDB_MAX_COLUMNS]; + int32_t bytes[TSDB_MAX_COLUMNS]; + char payload[]; } SShowObj; typedef struct SMnodeMsg { diff --git a/source/dnode/mnode/impl/inc/mndInt.h b/source/dnode/mnode/impl/inc/mndInt.h index 8910ed4e63..e5643847a5 100644 --- a/source/dnode/mnode/impl/inc/mndInt.h +++ b/source/dnode/mnode/impl/inc/mndInt.h @@ -18,6 +18,7 @@ #include "mndDef.h" #include "sdb.h" +#include "tcache.h" #include "tqueue.h" #ifdef __cplusplus @@ -27,6 +28,9 @@ extern "C" { typedef int32_t (*MndMsgFp)(SMnode *pMnode, SMnodeMsg *pMsg); typedef int32_t (*MndInitFp)(SMnode *pMnode); typedef void (*MndCleanupFp)(SMnode *pMnode); +typedef int32_t (*ShowMetaFp)(SMnode *pMnode, STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); +typedef int32_t (*ShowRetrieveFp)(SMnode *pMnode, SShowObj *pShow, char *data, int32_t rows, void *pConn); +typedef void (*ShowFreeIterFp)(SMnode *pMnode, void *pIter); typedef struct { const char *name; @@ -34,6 +38,14 @@ typedef struct { MndCleanupFp cleanupFp; } SMnodeStep; +typedef struct { + int32_t showId; + ShowMetaFp metaFps[TSDB_MGMT_TABLE_MAX]; + ShowRetrieveFp retrieveFps[TSDB_MGMT_TABLE_MAX]; + ShowFreeIterFp freeIterFps[TSDB_MGMT_TABLE_MAX]; + SCacheObj *cache; +} SShowMgmt; + typedef struct SMnode { int32_t dnodeId; int32_t clusterId; @@ -45,6 +57,7 @@ typedef struct SMnode { SSdb *pSdb; SDnode *pDnode; SArray *pSteps; + SShowMgmt showMgmt; MndMsgFp msgFp[TSDB_MSG_TYPE_MAX]; SendMsgToDnodeFp sendMsgToDnodeFp; SendMsgToMnodeFp sendMsgToMnodeFp; @@ -53,6 +66,7 @@ typedef struct SMnode { int32_t sver; int32_t statusInterval; int32_t mnodeEqualVnodeNum; + int32_t shellActivityTimer; char *timezone; char *locale; char *charset; diff --git a/source/dnode/mnode/impl/inc/mndShow.h b/source/dnode/mnode/impl/inc/mndShow.h index 06c18cb029..31c0b69c85 100644 --- a/source/dnode/mnode/impl/inc/mndShow.h +++ b/source/dnode/mnode/impl/inc/mndShow.h @@ -24,6 +24,10 @@ extern "C" { int32_t mndInitShow(SMnode *pMnode); void mndCleanupShow(SMnode *pMnode); +void mnodeAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp); +void mnodeAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp); +void mnodeAddShowFreeIterHandle(SMnode *pMnode, EShowType msgType, ShowFreeIterFp fp); +void mnodeVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capacity, SShowObj *pShow); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/src/mndCluster.c b/source/dnode/mnode/impl/src/mndCluster.c index 1780c88d6b..dbe5dd4f89 100644 --- a/source/dnode/mnode/impl/src/mndCluster.c +++ b/source/dnode/mnode/impl/src/mndCluster.c @@ -16,6 +16,7 @@ #define _DEFAULT_SOURCE #include "mndCluster.h" #include "mndTrans.h" +#include "mndShow.h" #define SDB_CLUSTER_VER 1 @@ -94,6 +95,71 @@ static int32_t mndCreateDefaultCluster(SMnode *pMnode) { return sdbWrite(pMnode->pSdb, pRaw); } + +// static int32_t mnodeGetClusterMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { +// int32_t cols = 0; +// SSchema *pSchema = pMeta->schema; + +// pShow->bytes[cols] = TSDB_CLUSTER_ID_LEN + VARSTR_HEADER_SIZE; +// pSchema[cols].type = TSDB_DATA_TYPE_BINARY; +// strcpy(pSchema[cols].name, "clusterId"); +// pSchema[cols].bytes = htons(pShow->bytes[cols]); +// cols++; + +// pShow->bytes[cols] = 8; +// pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; +// strcpy(pSchema[cols].name, "create_time"); +// pSchema[cols].bytes = htons(pShow->bytes[cols]); +// cols++; + +// pMeta->numOfColumns = htons(cols); +// strcpy(pMeta->tableFname, "show cluster"); +// pShow->numOfColumns = cols; + +// pShow->offset[0] = 0; +// for (int32_t i = 1; i < cols; ++i) { +// pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; +// } + +// pShow->numOfRows = 1; +// pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; + +// return 0; +// } + +// static int32_t mnodeRetrieveClusters(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +// int32_t numOfRows = 0; +// int32_t cols = 0; +// char * pWrite; +// SClusterObj *pCluster = NULL; + +// while (numOfRows < rows) { +// pShow->pIter = mnodeGetNextCluster(pShow->pIter, &pCluster); +// if (pCluster == NULL) break; + +// cols = 0; + +// pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; +// STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pCluster->uid, TSDB_CLUSTER_ID_LEN); +// cols++; + +// pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; +// *(int64_t *) pWrite = pCluster->createdTime; +// cols++; + +// mnodeDecClusterRef(pCluster); +// numOfRows++; +// } + +// mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow); +// pShow->numOfReads += numOfRows; +// return numOfRows; +// } + +// static void mnodeCancelGetNextCluster(void *pIter) { +// sdbFreeIter(tsClusterSdb, pIter); +// } + int32_t mndInitCluster(SMnode *pMnode) { SSdbTable table = {.sdbType = SDB_CLUSTER, .keyType = SDB_KEY_INT32, @@ -104,6 +170,9 @@ int32_t mndInitCluster(SMnode *pMnode) { .updateFp = (SdbUpdateFp)mndClusterActionUpdate, .deleteFp = (SdbDeleteFp)mndClusterActionDelete}; + // mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeGetClusterMeta); + // mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeRetrieveClusters); + // mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeCancelGetNextCluster); return sdbSetTable(pMnode->pSdb, table); } diff --git a/source/dnode/mnode/impl/src/mndConnect.c b/source/dnode/mnode/impl/src/mndConnect.c new file mode 100644 index 0000000000..8e62eacfab --- /dev/null +++ b/source/dnode/mnode/impl/src/mndConnect.c @@ -0,0 +1,155 @@ +/* + * 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 "mndShow.h" + + + +// static int32_t mnodeProcessHeartBeatMsg(SMnodeMsg *pMsg) { +// SHeartBeatRsp *pRsp = (SHeartBeatRsp *)rpcMallocCont(sizeof(SHeartBeatRsp)); +// if (pRsp == NULL) { +// return TSDB_CODE_MND_OUT_OF_MEMORY; +// } + +// SHeartBeatMsg *pHBMsg = pMsg->rpcMsg.pCont; +// if (taosCheckVersion(pHBMsg->clientVer, version, 3) != TSDB_CODE_SUCCESS) { +// rpcFreeCont(pRsp); +// return TSDB_CODE_TSC_INVALID_VERSION; // todo change the error code +// } + +// SRpcConnInfo connInfo = {0}; +// rpcGetConnInfo(pMsg->rpcMsg.handle, &connInfo); + +// int32_t connId = htonl(pHBMsg->connId); +// SConnObj *pConn = mnodeAccquireConn(connId, connInfo.user, connInfo.clientIp, connInfo.clientPort); +// if (pConn == NULL) { +// pHBMsg->pid = htonl(pHBMsg->pid); +// pConn = mnodeCreateConn(connInfo.user, connInfo.clientIp, connInfo.clientPort, pHBMsg->pid, pHBMsg->appName); +// } + +// if (pConn == NULL) { +// // do not close existing links, otherwise +// // mError("failed to create connId, close connect"); +// // pRsp->killConnection = 1; +// } else { +// pRsp->connId = htonl(pConn->connId); +// mnodeSaveQueryStreamList(pConn, pHBMsg); + +// if (pConn->killed != 0) { +// pRsp->killConnection = 1; +// } + +// if (pConn->streamId != 0) { +// pRsp->streamId = htonl(pConn->streamId); +// pConn->streamId = 0; +// } + +// if (pConn->queryId != 0) { +// pRsp->queryId = htonl(pConn->queryId); +// pConn->queryId = 0; +// } +// } + +// int32_t onlineDnodes = 0, totalDnodes = 0; +// mnodeGetOnlineAndTotalDnodesNum(&onlineDnodes, &totalDnodes); + +// pRsp->onlineDnodes = htonl(onlineDnodes); +// pRsp->totalDnodes = htonl(totalDnodes); +// mnodeGetMnodeEpSetForShell(&pRsp->epSet, false); + +// pMsg->rpcRsp.rsp = pRsp; +// pMsg->rpcRsp.len = sizeof(SHeartBeatRsp); + +// mnodeReleaseConn(pConn); +// return TSDB_CODE_SUCCESS; +// } + +// static int32_t mnodeProcessConnectMsg(SMnodeMsg *pMsg) { +// SConnectMsg *pConnectMsg = pMsg->rpcMsg.pCont; +// SConnectRsp *pConnectRsp = NULL; +// int32_t code = TSDB_CODE_SUCCESS; + +// SRpcConnInfo connInfo = {0}; +// if (rpcGetConnInfo(pMsg->rpcMsg.handle, &connInfo) != 0) { +// mError("thandle:%p is already released while process connect msg", pMsg->rpcMsg.handle); +// code = TSDB_CODE_MND_INVALID_CONNECTION; +// goto connect_over; +// } + +// code = taosCheckVersion(pConnectMsg->clientVersion, version, 3); +// if (code != TSDB_CODE_SUCCESS) { +// goto connect_over; +// } + +// SUserObj *pUser = pMsg->pUser; +// SAcctObj *pAcct = pUser->pAcct; + +// if (pConnectMsg->db[0]) { +// char dbName[TSDB_TABLE_FNAME_LEN * 3] = {0}; +// sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); +// SDbObj *pDb = mnodeGetDb(dbName); +// if (pDb == NULL) { +// code = TSDB_CODE_MND_INVALID_DB; +// goto connect_over; +// } + +// if (pDb->status != TSDB_DB_STATUS_READY) { +// mError("db:%s, status:%d, in dropping", pDb->name, pDb->status); +// code = TSDB_CODE_MND_DB_IN_DROPPING; +// mnodeDecDbRef(pDb); +// goto connect_over; +// } +// mnodeDecDbRef(pDb); +// } + +// pConnectRsp = rpcMallocCont(sizeof(SConnectRsp)); +// if (pConnectRsp == NULL) { +// code = TSDB_CODE_MND_OUT_OF_MEMORY; +// goto connect_over; +// } + +// pConnectMsg->pid = htonl(pConnectMsg->pid); +// SConnObj *pConn = mnodeCreateConn(connInfo.user, connInfo.clientIp, connInfo.clientPort, pConnectMsg->pid, pConnectMsg->appName); +// if (pConn == NULL) { +// code = terrno; +// } else { +// pConnectRsp->connId = htonl(pConn->connId); +// mnodeReleaseConn(pConn); +// } + +// sprintf(pConnectRsp->acctId, "%x", pAcct->acctId); +// memcpy(pConnectRsp->serverVersion, version, TSDB_VERSION_LEN); +// pConnectRsp->writeAuth = pUser->writeAuth; +// pConnectRsp->superAuth = pUser->superAuth; + +// mnodeGetMnodeEpSetForShell(&pConnectRsp->epSet, false); + +// dnodeGetClusterId(pConnectRsp->clusterId); + +// connect_over: +// if (code != TSDB_CODE_SUCCESS) { +// if (pConnectRsp) rpcFreeCont(pConnectRsp); +// mLError("user:%s login from %s, result:%s", connInfo.user, taosIpStr(connInfo.clientIp), tstrerror(code)); +// } else { +// mLInfo("user:%s login from %s, result:%s", connInfo.user, taosIpStr(connInfo.clientIp), tstrerror(code)); +// pMsg->rpcRsp.rsp = pConnectRsp; +// pMsg->rpcRsp.len = sizeof(SConnectRsp); +// } + +// return code; +// } + + diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 3a5a221677..0ad144d2df 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -18,4 +18,22 @@ #include "mndInt.h" int32_t mndInitDb(SMnode *pMnode) { return 0; } -void mndCleanupDb(SMnode *pMnode) {} \ No newline at end of file +void mndCleanupDb(SMnode *pMnode) {} + + +// static int32_t mnodeProcessUseMsg(SMnodeMsg *pMsg) { +// SUseDbMsg *pUseDbMsg = pMsg->rpcMsg.pCont; + +// int32_t code = TSDB_CODE_SUCCESS; +// if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pUseDbMsg->db); +// if (pMsg->pDb == NULL) { +// return TSDB_CODE_MND_INVALID_DB; +// } + +// if (pMsg->pDb->status != TSDB_DB_STATUS_READY) { +// mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status); +// return TSDB_CODE_MND_DB_IN_DROPPING; +// } + +// return code; +// } diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index 9938e95a73..af6a203006 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -14,8 +14,314 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "mndInt.h" +#include "mndShow.h" -int32_t mndInitShow(SMnode *pMnode) { return 0; } -void mndCleanupShow(SMnode *pMnode) {} \ No newline at end of file +static int32_t mndProcessShowMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg); +static int32_t mndProcessRetrieveMsg(SMnode *pMnode, SMnodeMsg *pMsg); +static bool mndCheckRetrieveFinished(SShowObj *pShow); +static int32_t mndAcquireShowObj(SMnode *pMnode, SShowObj *pShow); +static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove); +static int32_t mndPutShowObj(SMnode *pMnode, SShowObj *pShow); +static void mndFreeShowObj(void *ppShow); +static char *mndShowStr(int32_t showType); + +int32_t mndInitShow(SMnode *pMnode) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + + pMgmt->cache = taosCacheInit(TSDB_CACHE_PTR_KEY, 5, true, mndFreeShowObj, "show"); + if (pMgmt->cache == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("failed to alloc show cache since %s", terrstr()); + return -1; + } + + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_SHOW, mndProcessShowMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_SHOW_RETRIEVE, mndProcessRetrieveMsg); + return 0; +} + +void mndCleanupShow(SMnode *pMnode) {} + +static int32_t mndProcessShowMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + SShowMsg *pMsg = pMnodeMsg->rpcMsg.pCont; + int8_t type = pMsg->type; + uint16_t payloadLen = htonl(pMsg->payloadLen); + + if (type <= TSDB_MGMT_TABLE_START || type >= TSDB_MGMT_TABLE_MAX) { + terrno = TSDB_CODE_MND_INVALID_MSG_TYPE; + mError("failed to process show msg since %s", terrstr()); + return -1; + } + + ShowMetaFp metaFp = pMgmt->metaFps[type]; + if (metaFp == NULL) { + terrno = TSDB_CODE_MND_INVALID_MSG_TYPE; + mError("failed to process show-meta msg:%s since no message handle", mndShowStr(type)); + return -1; + } + + int32_t size = sizeof(SShowObj) + payloadLen; + SShowObj *pShow = calloc(1, size); + if (pShow != NULL) { + pShow->pMnode = pMnode; + pShow->type = type; + pShow->payloadLen = payloadLen; + memcpy(pShow->db, pMsg->db, TSDB_FULL_DB_NAME_LEN); + memcpy(pShow->payload, pMsg->payload, payloadLen); + } else { + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("failed to process show-meta msg:%s since %s", mndShowStr(type), terrstr()); + return -1; + } + + if (mndPutShowObj(pMnode, pShow) == 0) { + mError("failed to process show-meta msg:%s since %s", mndShowStr(type), terrstr()); + free(pShow); + return -1; + } + + size = sizeof(SShowRsp) + sizeof(SSchema) * TSDB_MAX_COLUMNS + TSDB_EXTRA_PAYLOAD_SIZE; + SShowRsp *pRsp = rpcMallocCont(size); + if (pRsp == NULL) { + mndReleaseShowObj(pShow, true); + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("show:%d, failed to process show-meta msg:%s since malloc rsp error", pShow->id, mndShowStr(type)); + return -1; + } + + pRsp->qhandle = htobe64((uint64_t)pShow); + + int32_t code = (*metaFp)(pMnode, &pRsp->tableMeta, pShow, pMnodeMsg->rpcMsg.handle); + mDebug("show:%d, type:%s, get meta finished, numOfRows:%d cols:%d result:%s", pShow->id, mndShowStr(type), + pShow->numOfRows, pShow->numOfColumns, tstrerror(code)); + + if (code == TSDB_CODE_SUCCESS) { + pMnodeMsg->contLen = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns; + pMnodeMsg->pCont = pRsp; + mndReleaseShowObj(pShow, false); + return TSDB_CODE_SUCCESS; + } else { + rpcFreeCont(pRsp); + mndReleaseShowObj(pShow, true); + return code; + } +} + +static int32_t mndProcessRetrieveMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + int32_t rowsToRead = 0; + int32_t size = 0; + int32_t rowsRead = 0; + + SRetrieveTableMsg *pRetrieve = pMnodeMsg->rpcMsg.pCont; + pRetrieve->qhandle = htobe64(pRetrieve->qhandle); + SShowObj *pShow = (SShowObj *)pRetrieve->qhandle; + + /* + * in case of server restart, apps may hold qhandle created by server before + * restart, which is actually invalid, therefore, signature check is required. + */ + if (mndAcquireShowObj(pMnode, pShow) != 0) { + terrno = TSDB_CODE_MND_INVALID_SHOWOBJ; + mError("failed to process show-retrieve msg:%p since %s", pShow, terrstr()); + return -1; + } + + ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type]; + if (retrieveFp == NULL) { + mndReleaseShowObj(pShow, false); + terrno = TSDB_CODE_MSG_NOT_PROCESSED; + mError("show:%d, failed to retrieve data since %s", pShow->id, terrstr()); + return -1; + } + + mDebug("show:%d, type:%s, start retrieve data, numOfReads:%d numOfRows:%d", pShow->id, mndShowStr(pShow->type), + pShow->numOfReads, pShow->numOfRows); + + if (mndCheckRetrieveFinished(pShow)) { + mDebug("show:%d, read finished, numOfReads:%d numOfRows:%d", pShow->id, pShow->numOfReads, pShow->numOfRows); + pShow->numOfReads = pShow->numOfRows; + } + + if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { + rowsToRead = pShow->numOfRows - pShow->numOfReads; + } + + /* return no more than 100 tables in one round trip */ + if (rowsToRead > 100) rowsToRead = 100; + + /* + * the actual number of table may be larger than the value of pShow->numOfRows, if a query is + * issued during a continuous create table operation. Therefore, rowToRead may be less than 0. + */ + if (rowsToRead < 0) rowsToRead = 0; + size = pShow->rowSize * rowsToRead; + + size += 100; + SRetrieveTableRsp *pRsp = rpcMallocCont(size); + if (pRsp == NULL) { + mndReleaseShowObj(pShow, false); + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("show:%d, failed to retrieve data since %s", pShow->id, terrstr()); + return -1; + } + + // if free flag is set, client wants to clean the resources + if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { + rowsRead = (*retrieveFp)(pMnode, pShow, pRsp->data, rowsToRead, pMnodeMsg->rpcMsg.handle); + } + + mDebug("show:%d, stop retrieve data, rowsRead:%d rowsToRead:%d", pShow->id, rowsRead, rowsToRead); + + pRsp->numOfRows = htonl(rowsRead); + pRsp->precision = (int16_t)htonl(TSDB_TIME_PRECISION_MILLI); // millisecond time precision + + pMnodeMsg->pCont = pRsp; + pMnodeMsg->contLen = size; + + if (rowsToRead == 0 || (rowsRead == rowsToRead && pShow->numOfRows == pShow->numOfReads)) { + pRsp->completed = 1; + mDebug("%p, retrieve completed", pShow); + mndReleaseShowObj(pShow, true); + } else { + mDebug("%p, retrieve not completed yet", pShow); + mndReleaseShowObj(pShow, false); + } + + return TSDB_CODE_SUCCESS; +} + +static char *mndShowStr(int32_t showType) { + switch (showType) { + case TSDB_MGMT_TABLE_ACCT: + return "show accounts"; + case TSDB_MGMT_TABLE_USER: + return "show users"; + case TSDB_MGMT_TABLE_DB: + return "show databases"; + case TSDB_MGMT_TABLE_TABLE: + return "show tables"; + case TSDB_MGMT_TABLE_DNODE: + return "show dnodes"; + case TSDB_MGMT_TABLE_MNODE: + return "show mnodes"; + case TSDB_MGMT_TABLE_VGROUP: + return "show vgroups"; + case TSDB_MGMT_TABLE_METRIC: + return "show stables"; + case TSDB_MGMT_TABLE_MODULE: + return "show modules"; + case TSDB_MGMT_TABLE_QUERIES: + return "show queries"; + case TSDB_MGMT_TABLE_STREAMS: + return "show streams"; + case TSDB_MGMT_TABLE_VARIABLES: + return "show configs"; + case TSDB_MGMT_TABLE_CONNS: + return "show connections"; + case TSDB_MGMT_TABLE_SCORES: + return "show scores"; + case TSDB_MGMT_TABLE_GRANTS: + return "show grants"; + case TSDB_MGMT_TABLE_VNODES: + return "show vnodes"; + case TSDB_MGMT_TABLE_CLUSTER: + return "show clusters"; + case TSDB_MGMT_TABLE_STREAMTABLES: + return "show streamtables"; + case TSDB_MGMT_TABLE_TP: + return "show topics"; + default: + return "undefined"; + } +} + +static bool mndCheckRetrieveFinished(SShowObj *pShow) { + if (pShow->pIter == NULL && pShow->numOfReads != 0) { + return true; + } + return false; +} + +static int32_t mndAcquireShowObj(SMnode *pMnode, SShowObj *pShow) { + TSDB_CACHE_PTR_TYPE handleVal = (TSDB_CACHE_PTR_TYPE)pShow; + + SShowMgmt *pMgmt = &pMnode->showMgmt; + SShowObj **ppShow = taosCacheAcquireByKey(pMgmt->cache, &handleVal, sizeof(TSDB_CACHE_PTR_TYPE)); + if (ppShow) { + mTrace("show:%d, data:%p acquired from cache", pShow->id, ppShow); + return 0; + } + + return -1; +} + +static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) { + SMnode *pMnode = pShow->pMnode; + SShowMgmt *pMgmt = &pMnode->showMgmt; + SShowObj **ppShow = (SShowObj **)pShow->ppShow; + taosCacheRelease(pMgmt->cache, (void **)(&ppShow), forceRemove); + mDebug("show:%d, data:%p released from cache, force:%d", pShow->id, ppShow, forceRemove); +} + +static int32_t mndPutShowObj(SMnode *pMnode, SShowObj *pShow) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + int32_t lifeSpan = pMnode->shellActivityTimer * 6 * 1000; + + TSDB_CACHE_PTR_TYPE val = (TSDB_CACHE_PTR_TYPE)pShow; + pShow->id = atomic_add_fetch_32(&pMgmt->showId, 1); + SShowObj **ppShow = + taosCachePut(pMgmt->cache, &val, sizeof(TSDB_CACHE_PTR_TYPE), &pShow, sizeof(TSDB_CACHE_PTR_TYPE), lifeSpan); + if (ppShow == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("show:%d, failed to put into cache", pShow->id); + return -1; + } + + mTrace("show:%d, data:%p put into cache", pShow->id, ppShow); + return 0; +} + +static void mndFreeShowObj(void *ppShow) { + SShowObj *pShow = *(SShowObj **)ppShow; + SMnode *pMnode = pShow->pMnode; + SShowMgmt *pMgmt = &pMnode->showMgmt; + + ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type]; + if (freeFp != NULL) { + if (pShow->pVgIter != NULL) { + // only used in 'show vnodes "ep"' + (*freeFp)(pMnode, pShow->pVgIter); + } + if (pShow->pIter != NULL) { + (*freeFp)(pMnode, pShow->pIter); + } + } + + mDebug("show:%d, data:%p destroyed", pShow->id, ppShow); + tfree(pShow); +} + +void mnodeVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capacity, SShowObj *pShow) { + if (rows < capacity) { + for (int32_t i = 0; i < numOfCols; ++i) { + memmove(data + pShow->offset[i] * rows, data + pShow->offset[i] * capacity, pShow->bytes[i] * rows); + } + } +} + +void mnodeAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + pMgmt->metaFps[showType] = fp; +} + +void mnodeAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + pMgmt->retrieveFps[showType] = fp; +} + +void mnodeAddShowFreeIterHandle(SMnode *pMnode, EShowType showType, ShowFreeIterFp fp) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + pMgmt->freeIterFps[showType] = fp; +} From f2df8dba6586835615363e9c26bdd77d91e62044 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 3 Dec 2021 14:31:11 +0800 Subject: [PATCH 04/11] change deps to contrib --- .gitignore | 6 +- CMakeLists.txt | 66 +------------------ cmake/bdb_CMakeLists.txt.in | 4 +- cmake/cjson_CMakeLists.txt.in | 2 +- cmake/gtest_CMakeLists.txt.in | 2 +- cmake/leveldb_CMakeLists.txt.in | 2 +- cmake/lucene_CMakeLists.txt.in | 2 +- cmake/lz4_CMakeLists.txt.in | 2 +- cmake/nuraft_CMakeLists.txt.in | 4 +- cmake/rocksdb_CMakeLists.txt.in | 2 +- cmake/sqlite_CMakeLists.txt.in | 4 +- cmake/zlib_CMakeLists.txt.in | 2 +- {deps => contrib}/CMakeLists.txt | 61 +++++++++++++++++ {deps => contrib}/test/CMakeLists.txt | 0 {deps => contrib}/test/bdb/CMakeLists.txt | 0 {deps => contrib}/test/bdb/bdbTest.c | 0 {deps => contrib}/test/lucene/CMakeLists.txt | 0 {deps => contrib}/test/lucene/main.cpp | 0 {deps => contrib}/test/rocksdb/CMakeLists.txt | 0 {deps => contrib}/test/rocksdb/main.c | 0 {deps => contrib}/test/sqlite/CMakeLists.txt | 0 {deps => contrib}/test/sqlite/sqliteTest.c | 0 {deps => contrib}/test/tdev/CMakeLists.txt | 0 {deps => contrib}/test/tdev/src/main.c | 0 24 files changed, 80 insertions(+), 79 deletions(-) rename {deps => contrib}/CMakeLists.txt (67%) rename {deps => contrib}/test/CMakeLists.txt (100%) rename {deps => contrib}/test/bdb/CMakeLists.txt (100%) rename {deps => contrib}/test/bdb/bdbTest.c (100%) rename {deps => contrib}/test/lucene/CMakeLists.txt (100%) rename {deps => contrib}/test/lucene/main.cpp (100%) rename {deps => contrib}/test/rocksdb/CMakeLists.txt (100%) rename {deps => contrib}/test/rocksdb/main.c (100%) rename {deps => contrib}/test/sqlite/CMakeLists.txt (100%) rename {deps => contrib}/test/sqlite/sqliteTest.c (100%) rename {deps => contrib}/test/tdev/CMakeLists.txt (100%) rename {deps => contrib}/test/tdev/src/main.c (100%) diff --git a/.gitignore b/.gitignore index 8df75abe20..ba8543e518 100644 --- a/.gitignore +++ b/.gitignore @@ -98,6 +98,6 @@ tramp .\#* TAGS -deps/* -!deps/CMakeLists.txt -!deps/test +contrib/* +!contrib/CMakeLists.txt +!contrib/test diff --git a/CMakeLists.txt b/CMakeLists.txt index a9450220a7..056fb6b509 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,73 +10,13 @@ project( # DEPENDENCIES # ============================================================================ set(CMAKE_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/cmake") +set(CMAKE_CONTRIB_DIR "${CMAKE_SOURCE_DIR}/contrib") include(${CMAKE_SUPPORT_DIR}/cmake.options) -function(cat IN_FILE OUT_FILE) - file(READ ${IN_FILE} CONTENTS) - file(APPEND ${OUT_FILE} "${CONTENTS}") -endfunction(cat IN_FILE OUT_FILE) - -set(DEPS_TMP_FILE "${CMAKE_BINARY_DIR}/deps_tmp_CMakeLists.txt.in") -configure_file("${CMAKE_SUPPORT_DIR}/deps_CMakeLists.txt.in" ${DEPS_TMP_FILE}) - -## googletest -if(${BUILD_TEST}) - cat("${CMAKE_SUPPORT_DIR}/gtest_CMakeLists.txt.in" ${DEPS_TMP_FILE}) -endif(${BUILD_TEST}) - -## lz4 -cat("${CMAKE_SUPPORT_DIR}/lz4_CMakeLists.txt.in" ${DEPS_TMP_FILE}) - -## zlib -cat("${CMAKE_SUPPORT_DIR}/zlib_CMakeLists.txt.in" ${DEPS_TMP_FILE}) - -## cJson -cat("${CMAKE_SUPPORT_DIR}/cjson_CMakeLists.txt.in" ${DEPS_TMP_FILE}) - -## leveldb -if(${BUILD_WITH_LEVELDB}) - cat("${CMAKE_SUPPORT_DIR}/leveldb_CMakeLists.txt.in" ${DEPS_TMP_FILE}) -endif(${BUILD_WITH_LEVELDB}) - -## rocksdb -if(${BUILD_WITH_ROCKSDB}) - cat("${CMAKE_SUPPORT_DIR}/rocksdb_CMakeLists.txt.in" ${DEPS_TMP_FILE}) - add_definitions(-DUSE_ROCKSDB) -endif(${BUILD_WITH_ROCKSDB}) - -## bdb -if(${BUILD_WITH_BDB}) - cat("${CMAKE_SUPPORT_DIR}/bdb_CMakeLists.txt.in" ${DEPS_TMP_FILE}) -endif(${BUILD_WITH_DBD}) - -## sqlite -if(${BUILD_WITH_SQLITE}) - cat("${CMAKE_SUPPORT_DIR}/sqlite_CMakeLists.txt.in" ${DEPS_TMP_FILE}) -endif(${BUILD_WITH_SQLITE}) - -## lucene -if(${BUILD_WITH_LUCENE}) - cat("${CMAKE_SUPPORT_DIR}/lucene_CMakeLists.txt.in" ${DEPS_TMP_FILE}) - add_definitions(-DUSE_LUCENE) -endif(${BUILD_WITH_LUCENE}) - -## NuRaft -if(${BUILD_WITH_NURAFT}) - cat("${CMAKE_SUPPORT_DIR}/nuraft_CMakeLists.txt.in" ${DEPS_TMP_FILE}) -endif(${BUILD_WITH_NURAFT}) - -## download dependencies -configure_file(${DEPS_TMP_FILE} "${CMAKE_SOURCE_DIR}/deps/deps-download/CMakeLists.txt") -execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/deps/deps-download") -execute_process(COMMAND "${CMAKE_COMMAND}" --build . - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/deps/deps-download") - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -gdwarf-2 -msse4.2 -mfma") -# deps -add_subdirectory(deps) +# contrib +add_subdirectory(contrib) # api add_library(api INTERFACE) diff --git a/cmake/bdb_CMakeLists.txt.in b/cmake/bdb_CMakeLists.txt.in index ecb7b91d09..57acc95a11 100644 --- a/cmake/bdb_CMakeLists.txt.in +++ b/cmake/bdb_CMakeLists.txt.in @@ -3,8 +3,8 @@ ExternalProject_Add(bdb GIT_REPOSITORY https://github.com/berkeleydb/libdb.git GIT_TAG v5.3.28 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/bdb" - BINARY_DIR "${CMAKE_SOURCE_DIR}/deps/bdb" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/bdb" + BINARY_DIR "${CMAKE_CONTRIB_DIR}/bdb" #BUILD_IN_SOURCE TRUE CONFIGURE_COMMAND "./dist/configure" BUILD_COMMAND "$(MAKE)" diff --git a/cmake/cjson_CMakeLists.txt.in b/cmake/cjson_CMakeLists.txt.in index 6c285d1a7e..c9ec90e809 100644 --- a/cmake/cjson_CMakeLists.txt.in +++ b/cmake/cjson_CMakeLists.txt.in @@ -3,7 +3,7 @@ ExternalProject_Add(cjson GIT_REPOSITORY https://github.com/taosdata-contrib/cJSON.git GIT_TAG v1.7.15 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/cJson" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/cJson" BINARY_DIR "" CONFIGURE_COMMAND "" BUILD_COMMAND "" diff --git a/cmake/gtest_CMakeLists.txt.in b/cmake/gtest_CMakeLists.txt.in index e54806ae3b..98701456cf 100644 --- a/cmake/gtest_CMakeLists.txt.in +++ b/cmake/gtest_CMakeLists.txt.in @@ -3,7 +3,7 @@ ExternalProject_Add(googletest GIT_REPOSITORY https://github.com/taosdata-contrib/googletest.git GIT_TAG release-1.11.0 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/googletest" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/googletest" BINARY_DIR "" CONFIGURE_COMMAND "" BUILD_COMMAND "" diff --git a/cmake/leveldb_CMakeLists.txt.in b/cmake/leveldb_CMakeLists.txt.in index 7392f3d245..ac8a097b25 100644 --- a/cmake/leveldb_CMakeLists.txt.in +++ b/cmake/leveldb_CMakeLists.txt.in @@ -3,7 +3,7 @@ ExternalProject_Add(leveldb GIT_REPOSITORY https://github.com/taosdata-contrib/leveldb.git GIT_TAG master - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/leveldb" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/leveldb" BINARY_DIR "" #BUILD_IN_SOURCE TRUE CONFIGURE_COMMAND "" diff --git a/cmake/lucene_CMakeLists.txt.in b/cmake/lucene_CMakeLists.txt.in index 436ac64475..d2a453df2f 100644 --- a/cmake/lucene_CMakeLists.txt.in +++ b/cmake/lucene_CMakeLists.txt.in @@ -2,7 +2,7 @@ # lucene ExternalProject_Add(lucene GIT_REPOSITORY https://github.com/yihaoDeng/LucenePlusPlus.git - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/lucene" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/lucene" BINARY_DIR "" #BUILD_IN_SOURCE TRUE CONFIGURE_COMMAND "" diff --git a/cmake/lz4_CMakeLists.txt.in b/cmake/lz4_CMakeLists.txt.in index 4b25d48e86..199b325b09 100644 --- a/cmake/lz4_CMakeLists.txt.in +++ b/cmake/lz4_CMakeLists.txt.in @@ -3,7 +3,7 @@ ExternalProject_Add(lz4 GIT_REPOSITORY https://github.com/taosdata-contrib/lz4.git GIT_TAG v1.9.3 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/lz4" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/lz4" BINARY_DIR "" CONFIGURE_COMMAND "" BUILD_COMMAND "" diff --git a/cmake/nuraft_CMakeLists.txt.in b/cmake/nuraft_CMakeLists.txt.in index ba31480850..970cb3a830 100644 --- a/cmake/nuraft_CMakeLists.txt.in +++ b/cmake/nuraft_CMakeLists.txt.in @@ -3,8 +3,8 @@ ExternalProject_Add(NuRaft GIT_REPOSITORY https://github.com/eBay/NuRaft.git GIT_TAG v1.3.0 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/nuraft" - BINARY_DIR "${CMAKE_SOURCE_DIR}/deps/nuraft" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/nuraft" + BINARY_DIR "${CMAKE_CONTRIB_DIR}/nuraft" CONFIGURE_COMMAND "./prepare.sh" BUILD_COMMAND "" INSTALL_COMMAND "" diff --git a/cmake/rocksdb_CMakeLists.txt.in b/cmake/rocksdb_CMakeLists.txt.in index bacc48ecac..e38cd876f6 100644 --- a/cmake/rocksdb_CMakeLists.txt.in +++ b/cmake/rocksdb_CMakeLists.txt.in @@ -3,7 +3,7 @@ ExternalProject_Add(rocksdb GIT_REPOSITORY https://github.com/taosdata-contrib/rocksdb.git GIT_TAG v6.23.3 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/rocksdb" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/rocksdb" CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" diff --git a/cmake/sqlite_CMakeLists.txt.in b/cmake/sqlite_CMakeLists.txt.in index 6fd981aeff..95308833db 100644 --- a/cmake/sqlite_CMakeLists.txt.in +++ b/cmake/sqlite_CMakeLists.txt.in @@ -3,8 +3,8 @@ ExternalProject_Add(sqlite GIT_REPOSITORY https://github.com/sqlite/sqlite.git GIT_TAG version-3.36.0 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/sqlite" - BINARY_DIR "${CMAKE_SOURCE_DIR}/deps/sqlite" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/sqlite" + BINARY_DIR "${CMAKE_CONTRIB_DIR}/sqlite" #BUILD_IN_SOURCE TRUE CONFIGURE_COMMAND "./configure" BUILD_COMMAND "$(MAKE)" diff --git a/cmake/zlib_CMakeLists.txt.in b/cmake/zlib_CMakeLists.txt.in index 2176486519..160d3305d0 100644 --- a/cmake/zlib_CMakeLists.txt.in +++ b/cmake/zlib_CMakeLists.txt.in @@ -3,7 +3,7 @@ ExternalProject_Add(zlib GIT_REPOSITORY https://github.com/taosdata-contrib/zlib.git GIT_TAG v1.2.11 - SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps/zlib" + SOURCE_DIR "${CMAKE_CONTRIB_DIR}/zlib" BINARY_DIR "" #BUILD_IN_SOURCE TRUE CONFIGURE_COMMAND "" diff --git a/deps/CMakeLists.txt b/contrib/CMakeLists.txt similarity index 67% rename from deps/CMakeLists.txt rename to contrib/CMakeLists.txt index 0ef8c7e3be..37241bba9f 100644 --- a/deps/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -1,3 +1,64 @@ +function(cat IN_FILE OUT_FILE) + file(READ ${IN_FILE} CONTENTS) + file(APPEND ${OUT_FILE} "${CONTENTS}") +endfunction(cat IN_FILE OUT_FILE) + +set(CONTRIB_TMP_FILE "${CMAKE_BINARY_DIR}/deps_tmp_CMakeLists.txt.in") +configure_file("${CMAKE_SUPPORT_DIR}/deps_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) + +## googletest +if(${BUILD_TEST}) + cat("${CMAKE_SUPPORT_DIR}/gtest_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif(${BUILD_TEST}) + +## lz4 +cat("${CMAKE_SUPPORT_DIR}/lz4_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) + +## zlib +cat("${CMAKE_SUPPORT_DIR}/zlib_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) + +## cJson +cat("${CMAKE_SUPPORT_DIR}/cjson_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) + +## leveldb +if(${BUILD_WITH_LEVELDB}) + cat("${CMAKE_SUPPORT_DIR}/leveldb_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif(${BUILD_WITH_LEVELDB}) + +## rocksdb +if(${BUILD_WITH_ROCKSDB}) + cat("${CMAKE_SUPPORT_DIR}/rocksdb_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) + add_definitions(-DUSE_ROCKSDB) +endif(${BUILD_WITH_ROCKSDB}) + +## bdb +if(${BUILD_WITH_BDB}) + cat("${CMAKE_SUPPORT_DIR}/bdb_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif(${BUILD_WITH_DBD}) + +## sqlite +if(${BUILD_WITH_SQLITE}) + cat("${CMAKE_SUPPORT_DIR}/sqlite_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif(${BUILD_WITH_SQLITE}) + +## lucene +if(${BUILD_WITH_LUCENE}) + cat("${CMAKE_SUPPORT_DIR}/lucene_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) + add_definitions(-DUSE_LUCENE) +endif(${BUILD_WITH_LUCENE}) + +## NuRaft +if(${BUILD_WITH_NURAFT}) + cat("${CMAKE_SUPPORT_DIR}/nuraft_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) +endif(${BUILD_WITH_NURAFT}) + +## download dependencies +configure_file(${CONTRIB_TMP_FILE} "${CMAKE_CONTRIB_DIR}/deps-download/CMakeLists.txt") +execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . + WORKING_DIRECTORY "${CMAKE_CONTRIB_DIR}/deps-download") +execute_process(COMMAND "${CMAKE_COMMAND}" --build . + WORKING_DIRECTORY "${CMAKE_CONTRIB_DIR}/deps-download") + # ================================================================================================ # DEPENDENCIES # ================================================================================================ diff --git a/deps/test/CMakeLists.txt b/contrib/test/CMakeLists.txt similarity index 100% rename from deps/test/CMakeLists.txt rename to contrib/test/CMakeLists.txt diff --git a/deps/test/bdb/CMakeLists.txt b/contrib/test/bdb/CMakeLists.txt similarity index 100% rename from deps/test/bdb/CMakeLists.txt rename to contrib/test/bdb/CMakeLists.txt diff --git a/deps/test/bdb/bdbTest.c b/contrib/test/bdb/bdbTest.c similarity index 100% rename from deps/test/bdb/bdbTest.c rename to contrib/test/bdb/bdbTest.c diff --git a/deps/test/lucene/CMakeLists.txt b/contrib/test/lucene/CMakeLists.txt similarity index 100% rename from deps/test/lucene/CMakeLists.txt rename to contrib/test/lucene/CMakeLists.txt diff --git a/deps/test/lucene/main.cpp b/contrib/test/lucene/main.cpp similarity index 100% rename from deps/test/lucene/main.cpp rename to contrib/test/lucene/main.cpp diff --git a/deps/test/rocksdb/CMakeLists.txt b/contrib/test/rocksdb/CMakeLists.txt similarity index 100% rename from deps/test/rocksdb/CMakeLists.txt rename to contrib/test/rocksdb/CMakeLists.txt diff --git a/deps/test/rocksdb/main.c b/contrib/test/rocksdb/main.c similarity index 100% rename from deps/test/rocksdb/main.c rename to contrib/test/rocksdb/main.c diff --git a/deps/test/sqlite/CMakeLists.txt b/contrib/test/sqlite/CMakeLists.txt similarity index 100% rename from deps/test/sqlite/CMakeLists.txt rename to contrib/test/sqlite/CMakeLists.txt diff --git a/deps/test/sqlite/sqliteTest.c b/contrib/test/sqlite/sqliteTest.c similarity index 100% rename from deps/test/sqlite/sqliteTest.c rename to contrib/test/sqlite/sqliteTest.c diff --git a/deps/test/tdev/CMakeLists.txt b/contrib/test/tdev/CMakeLists.txt similarity index 100% rename from deps/test/tdev/CMakeLists.txt rename to contrib/test/tdev/CMakeLists.txt diff --git a/deps/test/tdev/src/main.c b/contrib/test/tdev/src/main.c similarity index 100% rename from deps/test/tdev/src/main.c rename to contrib/test/tdev/src/main.c From 5857ab5b28a6fa6a21749b3bc2d2b54d81688962 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 3 Dec 2021 14:36:41 +0800 Subject: [PATCH 05/11] TD-10431 process mnode profile --- include/common/taosmsg.h | 51 +- include/util/tdef.h | 2 +- include/util/tutil.h | 4 +- source/dnode/mnode/impl/inc/mndDb.h | 2 + source/dnode/mnode/impl/inc/mndDef.h | 8 +- source/dnode/mnode/impl/inc/mndInt.h | 10 +- source/dnode/mnode/impl/inc/mndMnode.h | 1 + source/dnode/mnode/impl/inc/mndShow.h | 6 +- source/dnode/mnode/impl/src/mndCluster.c | 6 +- source/dnode/mnode/impl/src/mndConnect.c | 155 ----- source/dnode/mnode/impl/src/mndDb.c | 48 +- source/dnode/mnode/impl/src/mndMnode.c | 4 +- source/dnode/mnode/impl/src/mndProfile.c | 761 ++++++++++++++++++++++- source/dnode/mnode/impl/src/mndShow.c | 10 +- source/dnode/mnode/impl/src/mndUser.c | 21 +- source/util/src/tutil.c | 7 + 16 files changed, 862 insertions(+), 234 deletions(-) delete mode 100644 source/dnode/mnode/impl/src/mndConnect.c diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 826934c4a8..e29accd249 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -353,11 +353,9 @@ typedef struct { } SUpdateTableTagValMsg; typedef struct { - char clientVersion[TSDB_VERSION_LEN]; - char msgVersion[TSDB_VERSION_LEN]; - char db[TSDB_TABLE_FNAME_LEN]; - char appName[TSDB_APPNAME_LEN]; int32_t pid; + char app[TSDB_APP_NAME_LEN]; + char db[TSDB_DB_NAME_LEN]; } SConnectMsg; typedef struct SEpSet { @@ -368,15 +366,14 @@ typedef struct SEpSet { } SEpSet; typedef struct { - char acctId[TSDB_ACCT_ID_LEN]; - char serverVersion[TSDB_VERSION_LEN]; - char clusterId[TSDB_CLUSTER_ID_LEN]; - int8_t writeAuth; - int8_t superAuth; - int8_t reserved1; - int8_t reserved2; - int32_t connId; - SEpSet epSet; + int32_t acctId; + int32_t clusterId; + int32_t connId; + int8_t superAuth; + int8_t readAuth; + int8_t writeAuth; + int8_t reserved[5]; + SEpSet epSet; } SConnectRsp; typedef struct { @@ -875,23 +872,23 @@ typedef struct { } SStreamDesc; typedef struct { - char clientVer[TSDB_VERSION_LEN]; - uint32_t connId; - int32_t pid; - int32_t numOfQueries; - int32_t numOfStreams; - char appName[TSDB_APPNAME_LEN]; - char pData[]; + int32_t connId; + int32_t pid; + int32_t numOfQueries; + int32_t numOfStreams; + char app[TSDB_APP_NAME_LEN]; + char pData[]; } SHeartBeatMsg; typedef struct { - uint32_t queryId; - uint32_t streamId; - uint32_t totalDnodes; - uint32_t onlineDnodes; - uint32_t connId; - int8_t killConnection; - SEpSet epSet; + int32_t connId; + int32_t queryId; + int32_t streamId; + int32_t totalDnodes; + int32_t onlineDnodes; + int8_t killConnection; + int8_t reserved[3]; + SEpSet epSet; } SHeartBeatRsp; typedef struct { diff --git a/include/util/tdef.h b/include/util/tdef.h index 76df8887a0..897f51f5c1 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -174,7 +174,7 @@ do { \ #define TSDB_MAX_SQL_SHOW_LEN 512 #define TSDB_MAX_ALLOWED_SQL_LEN (1*1024*1024u) // sql length should be less than 1mb -#define TSDB_APPNAME_LEN TSDB_UNI_LEN +#define TSDB_APP_NAME_LEN TSDB_UNI_LEN /** * In some scenarios uint16_t (0~65535) is used to store the row len. diff --git a/include/util/tutil.h b/include/util/tutil.h index 8dbcb7e8d5..573dee9339 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -41,8 +41,10 @@ char * paGetToken(char *src, char **token, int32_t *tokenLen); int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]); int32_t taosHexStrToByteArray(char hexstr[], char bytes[]); -char * taosIpStr(uint32_t ipInt); +char *taosIpStr(uint32_t ipInt); uint32_t ip2uint(const char *const ip_addr); +void taosIp2String(uint32_t ip, char *str); +void taosIpPort2String(uint32_t ip, uint16_t port, char *str); static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *target) { MD5_CTX context; diff --git a/source/dnode/mnode/impl/inc/mndDb.h b/source/dnode/mnode/impl/inc/mndDb.h index acccb62603..91f502be7d 100644 --- a/source/dnode/mnode/impl/inc/mndDb.h +++ b/source/dnode/mnode/impl/inc/mndDb.h @@ -24,6 +24,8 @@ extern "C" { int32_t mndInitDb(SMnode *pMnode); void mndCleanupDb(SMnode *pMnode); +SDbObj *mndAcquireDb(SMnode *pMnode, char *db); +void mndReleaseDb(SMnode *pMnode, SDbObj *pDb); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/inc/mndDef.h b/source/dnode/mnode/impl/inc/mndDef.h index dc27656dbf..aaf86c15b6 100644 --- a/source/dnode/mnode/impl/inc/mndDef.h +++ b/source/dnode/mnode/impl/inc/mndDef.h @@ -185,9 +185,11 @@ typedef struct SUserObj { char acct[TSDB_USER_LEN]; int64_t createdTime; int64_t updateTime; - int8_t rootAuth; + int8_t superAuth; + int8_t readAuth; + int8_t writeAuth; + int32_t acctId; SHashObj *prohibitDbHash; - SAcctObj *pAcct; } SUserObj; typedef struct { @@ -303,6 +305,8 @@ typedef struct SShowObj { typedef struct SMnodeMsg { char user[TSDB_USER_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; + int32_t acctId; SMnode *pMnode; int16_t received; int16_t successed; diff --git a/source/dnode/mnode/impl/inc/mndInt.h b/source/dnode/mnode/impl/inc/mndInt.h index e5643847a5..0f4dd0633b 100644 --- a/source/dnode/mnode/impl/inc/mndInt.h +++ b/source/dnode/mnode/impl/inc/mndInt.h @@ -28,8 +28,8 @@ extern "C" { typedef int32_t (*MndMsgFp)(SMnode *pMnode, SMnodeMsg *pMsg); typedef int32_t (*MndInitFp)(SMnode *pMnode); typedef void (*MndCleanupFp)(SMnode *pMnode); -typedef int32_t (*ShowMetaFp)(SMnode *pMnode, STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); -typedef int32_t (*ShowRetrieveFp)(SMnode *pMnode, SShowObj *pShow, char *data, int32_t rows, void *pConn); +typedef int32_t (*ShowMetaFp)(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta); +typedef int32_t (*ShowRetrieveFp)(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows); typedef void (*ShowFreeIterFp)(SMnode *pMnode, void *pIter); typedef struct { @@ -46,6 +46,11 @@ typedef struct { SCacheObj *cache; } SShowMgmt; +typedef struct { + int32_t connId; + SCacheObj *cache; +} SProfileMgmt; + typedef struct SMnode { int32_t dnodeId; int32_t clusterId; @@ -58,6 +63,7 @@ typedef struct SMnode { SDnode *pDnode; SArray *pSteps; SShowMgmt showMgmt; + SProfileMgmt profileMgmt; MndMsgFp msgFp[TSDB_MSG_TYPE_MAX]; SendMsgToDnodeFp sendMsgToDnodeFp; SendMsgToMnodeFp sendMsgToMnodeFp; diff --git a/source/dnode/mnode/impl/inc/mndMnode.h b/source/dnode/mnode/impl/inc/mndMnode.h index 53f7b733f2..64bbb13b60 100644 --- a/source/dnode/mnode/impl/inc/mndMnode.h +++ b/source/dnode/mnode/impl/inc/mndMnode.h @@ -25,6 +25,7 @@ extern "C" { int32_t mndInitMnode(SMnode *pMnode); void mndCleanupMnode(SMnode *pMnode); bool mndIsMnode(SMnode *pMnode, int32_t dnodeId); +void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/inc/mndShow.h b/source/dnode/mnode/impl/inc/mndShow.h index 31c0b69c85..e7a3fcd45f 100644 --- a/source/dnode/mnode/impl/inc/mndShow.h +++ b/source/dnode/mnode/impl/inc/mndShow.h @@ -24,9 +24,9 @@ extern "C" { int32_t mndInitShow(SMnode *pMnode); void mndCleanupShow(SMnode *pMnode); -void mnodeAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp); -void mnodeAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp); -void mnodeAddShowFreeIterHandle(SMnode *pMnode, EShowType msgType, ShowFreeIterFp fp); +void mndAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp); +void mndAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp); +void mndAddShowFreeIterHandle(SMnode *pMnode, EShowType msgType, ShowFreeIterFp fp); void mnodeVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capacity, SShowObj *pShow); #ifdef __cplusplus diff --git a/source/dnode/mnode/impl/src/mndCluster.c b/source/dnode/mnode/impl/src/mndCluster.c index dbe5dd4f89..6013ac0c31 100644 --- a/source/dnode/mnode/impl/src/mndCluster.c +++ b/source/dnode/mnode/impl/src/mndCluster.c @@ -170,9 +170,9 @@ int32_t mndInitCluster(SMnode *pMnode) { .updateFp = (SdbUpdateFp)mndClusterActionUpdate, .deleteFp = (SdbDeleteFp)mndClusterActionDelete}; - // mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeGetClusterMeta); - // mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeRetrieveClusters); - // mnodeAddShowFreeIterHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeCancelGetNextCluster); + // mndAddShowMetaHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeGetClusterMeta); + // mndAddShowRetrieveHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeRetrieveClusters); + // mndAddShowFreeIterHandle(TSDB_MGMT_TABLE_CLUSTER, mnodeCancelGetNextCluster); return sdbSetTable(pMnode->pSdb, table); } diff --git a/source/dnode/mnode/impl/src/mndConnect.c b/source/dnode/mnode/impl/src/mndConnect.c deleted file mode 100644 index 8e62eacfab..0000000000 --- a/source/dnode/mnode/impl/src/mndConnect.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "mndShow.h" - - - -// static int32_t mnodeProcessHeartBeatMsg(SMnodeMsg *pMsg) { -// SHeartBeatRsp *pRsp = (SHeartBeatRsp *)rpcMallocCont(sizeof(SHeartBeatRsp)); -// if (pRsp == NULL) { -// return TSDB_CODE_MND_OUT_OF_MEMORY; -// } - -// SHeartBeatMsg *pHBMsg = pMsg->rpcMsg.pCont; -// if (taosCheckVersion(pHBMsg->clientVer, version, 3) != TSDB_CODE_SUCCESS) { -// rpcFreeCont(pRsp); -// return TSDB_CODE_TSC_INVALID_VERSION; // todo change the error code -// } - -// SRpcConnInfo connInfo = {0}; -// rpcGetConnInfo(pMsg->rpcMsg.handle, &connInfo); - -// int32_t connId = htonl(pHBMsg->connId); -// SConnObj *pConn = mnodeAccquireConn(connId, connInfo.user, connInfo.clientIp, connInfo.clientPort); -// if (pConn == NULL) { -// pHBMsg->pid = htonl(pHBMsg->pid); -// pConn = mnodeCreateConn(connInfo.user, connInfo.clientIp, connInfo.clientPort, pHBMsg->pid, pHBMsg->appName); -// } - -// if (pConn == NULL) { -// // do not close existing links, otherwise -// // mError("failed to create connId, close connect"); -// // pRsp->killConnection = 1; -// } else { -// pRsp->connId = htonl(pConn->connId); -// mnodeSaveQueryStreamList(pConn, pHBMsg); - -// if (pConn->killed != 0) { -// pRsp->killConnection = 1; -// } - -// if (pConn->streamId != 0) { -// pRsp->streamId = htonl(pConn->streamId); -// pConn->streamId = 0; -// } - -// if (pConn->queryId != 0) { -// pRsp->queryId = htonl(pConn->queryId); -// pConn->queryId = 0; -// } -// } - -// int32_t onlineDnodes = 0, totalDnodes = 0; -// mnodeGetOnlineAndTotalDnodesNum(&onlineDnodes, &totalDnodes); - -// pRsp->onlineDnodes = htonl(onlineDnodes); -// pRsp->totalDnodes = htonl(totalDnodes); -// mnodeGetMnodeEpSetForShell(&pRsp->epSet, false); - -// pMsg->rpcRsp.rsp = pRsp; -// pMsg->rpcRsp.len = sizeof(SHeartBeatRsp); - -// mnodeReleaseConn(pConn); -// return TSDB_CODE_SUCCESS; -// } - -// static int32_t mnodeProcessConnectMsg(SMnodeMsg *pMsg) { -// SConnectMsg *pConnectMsg = pMsg->rpcMsg.pCont; -// SConnectRsp *pConnectRsp = NULL; -// int32_t code = TSDB_CODE_SUCCESS; - -// SRpcConnInfo connInfo = {0}; -// if (rpcGetConnInfo(pMsg->rpcMsg.handle, &connInfo) != 0) { -// mError("thandle:%p is already released while process connect msg", pMsg->rpcMsg.handle); -// code = TSDB_CODE_MND_INVALID_CONNECTION; -// goto connect_over; -// } - -// code = taosCheckVersion(pConnectMsg->clientVersion, version, 3); -// if (code != TSDB_CODE_SUCCESS) { -// goto connect_over; -// } - -// SUserObj *pUser = pMsg->pUser; -// SAcctObj *pAcct = pUser->pAcct; - -// if (pConnectMsg->db[0]) { -// char dbName[TSDB_TABLE_FNAME_LEN * 3] = {0}; -// sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); -// SDbObj *pDb = mnodeGetDb(dbName); -// if (pDb == NULL) { -// code = TSDB_CODE_MND_INVALID_DB; -// goto connect_over; -// } - -// if (pDb->status != TSDB_DB_STATUS_READY) { -// mError("db:%s, status:%d, in dropping", pDb->name, pDb->status); -// code = TSDB_CODE_MND_DB_IN_DROPPING; -// mnodeDecDbRef(pDb); -// goto connect_over; -// } -// mnodeDecDbRef(pDb); -// } - -// pConnectRsp = rpcMallocCont(sizeof(SConnectRsp)); -// if (pConnectRsp == NULL) { -// code = TSDB_CODE_MND_OUT_OF_MEMORY; -// goto connect_over; -// } - -// pConnectMsg->pid = htonl(pConnectMsg->pid); -// SConnObj *pConn = mnodeCreateConn(connInfo.user, connInfo.clientIp, connInfo.clientPort, pConnectMsg->pid, pConnectMsg->appName); -// if (pConn == NULL) { -// code = terrno; -// } else { -// pConnectRsp->connId = htonl(pConn->connId); -// mnodeReleaseConn(pConn); -// } - -// sprintf(pConnectRsp->acctId, "%x", pAcct->acctId); -// memcpy(pConnectRsp->serverVersion, version, TSDB_VERSION_LEN); -// pConnectRsp->writeAuth = pUser->writeAuth; -// pConnectRsp->superAuth = pUser->superAuth; - -// mnodeGetMnodeEpSetForShell(&pConnectRsp->epSet, false); - -// dnodeGetClusterId(pConnectRsp->clusterId); - -// connect_over: -// if (code != TSDB_CODE_SUCCESS) { -// if (pConnectRsp) rpcFreeCont(pConnectRsp); -// mLError("user:%s login from %s, result:%s", connInfo.user, taosIpStr(connInfo.clientIp), tstrerror(code)); -// } else { -// mLInfo("user:%s login from %s, result:%s", connInfo.user, taosIpStr(connInfo.clientIp), tstrerror(code)); -// pMsg->rpcRsp.rsp = pConnectRsp; -// pMsg->rpcRsp.len = sizeof(SConnectRsp); -// } - -// return code; -// } - - diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 0ad144d2df..d990e926bb 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -14,26 +14,38 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "mndInt.h" +#include "mndDb.h" -int32_t mndInitDb(SMnode *pMnode) { return 0; } -void mndCleanupDb(SMnode *pMnode) {} +static int32_t mnodeProcessUseMsg(SMnode *pMnode, SMnodeMsg *pMsg); +int32_t mndInitDb(SMnode *pMnode) { + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_USE_DB, mnodeProcessUseMsg); + return 0; +} -// static int32_t mnodeProcessUseMsg(SMnodeMsg *pMsg) { -// SUseDbMsg *pUseDbMsg = pMsg->rpcMsg.pCont; +void mndCleanupDb(SMnode *pMnode) {} -// int32_t code = TSDB_CODE_SUCCESS; -// if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pUseDbMsg->db); -// if (pMsg->pDb == NULL) { -// return TSDB_CODE_MND_INVALID_DB; -// } - -// if (pMsg->pDb->status != TSDB_DB_STATUS_READY) { -// mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status); -// return TSDB_CODE_MND_DB_IN_DROPPING; -// } +SDbObj *mndAcquireDb(SMnode *pMnode, char *db) { + SSdb *pSdb = pMnode->pSdb; + return sdbAcquire(pSdb, SDB_DB, db); +} -// return code; -// } +void mndReleaseDb(SMnode *pMnode, SDbObj *pDb) { + SSdb *pSdb = pMnode->pSdb; + sdbRelease(pSdb, pDb); +} + +static int32_t mnodeProcessUseMsg(SMnode *pMnode, SMnodeMsg *pMsg) { + SUseDbMsg *pUseDbMsg = pMsg->rpcMsg.pCont; + + strncpy(pMsg->db, pUseDbMsg->db, TSDB_FULL_DB_NAME_LEN); + + SDbObj *pDb = mndAcquireDb(pMnode, pMsg->db); + if (pDb != NULL) { + mndReleaseDb(pMnode, pDb); + return 0; + } else { + mError("db:%s, failed to process use db msg since %s", pMsg->db, terrstr()); + return -1; + } +} diff --git a/source/dnode/mnode/impl/src/mndMnode.c b/source/dnode/mnode/impl/src/mndMnode.c index d2ace31a36..c1c17e4983 100644 --- a/source/dnode/mnode/impl/src/mndMnode.c +++ b/source/dnode/mnode/impl/src/mndMnode.c @@ -135,4 +135,6 @@ bool mndIsMnode(SMnode *pMnode, int32_t dnodeId) { sdbRelease(pSdb, pMnodeObj); return true; -} \ No newline at end of file +} + +void mndGetMnodeEpSet(SMnode *pMnode, SEpSet *pEpSet) {} \ No newline at end of file diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 845e50210a..ef596f8ee1 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -14,8 +14,761 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "mndInt.h" +#include "mndProfile.h" +#include "mndDb.h" +#include "mndMnode.h" +#include "mndShow.h" +#include "mndUser.h" -int32_t mndInitProfile(SMnode *pMnode) { return 0; } -void mndCleanupProfile(SMnode *pMnode) {} \ No newline at end of file +#define QUERY_ID_SIZE 20 +#define QUERY_OBJ_ID_SIZE 18 +#define SUBQUERY_INFO_SIZE 6 +#define QUERY_STREAM_SAVE_SIZE 20 + +typedef struct { + char user[TSDB_USER_LEN]; + char app[TSDB_APP_NAME_LEN]; // app name that invokes taosc + int32_t pid; // pid of app that invokes taosc + int32_t connId; + int8_t killed; + int8_t align; + uint16_t port; + uint32_t ip; + int64_t stime; + int64_t lastAccess; + int32_t queryId; + int32_t streamId; + int32_t numOfQueries; + int32_t numOfStreams; + SStreamDesc *pStreams; + SQueryDesc *pQueries; +} SConnObj; + +static SConnObj *mndCreateConn(SMnode *pMnode, char *user, uint32_t ip, uint16_t port, int32_t pid, const char *app); +static void mndFreeConn(SConnObj *pConn); +static SConnObj *mndAcquireConn(SMnode *pMnode, int32_t connId, char *user, uint32_t ip, uint16_t port); +static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn); +static void *mndGetNextConn(SMnode *pMnode, void *pIter, SConnObj **pConn); +static void mndCancelGetNextConn(SMnode *pMnode, void *pIter); +static int32_t mndProcessHeartBeatMsg(SMnode *pMnode, SMnodeMsg *pMsg); +static int32_t mndProcessConnectMsg(SMnode *pMnode, SMnodeMsg *pMsg); +static int32_t mndGetConnsMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta); +static int32_t mndRetrieveConns(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows); +static int32_t mndGetQueryMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj *pShow); +static int32_t mndRetrieveQueries(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows); +static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter); +static int32_t mndGetStreamMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj *pShow); +static int32_t mndRetrieveStreams(SShowObj *pShow, char *data, int32_t rows, SMnodeMsg *pMsg); +static void mndCancelGetNextStream(SMnode *pMnode, void *pIter); + +int32_t mndInitProfile(SMnode *pMnode) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + int32_t connCheckTime = pMnode->shellActivityTimer * 2; + pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, connCheckTime, true, (__cache_free_fn_t)mndFreeConn, "conn"); + if (pMgmt->cache == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("failed to alloc profile cache since %s", terrstr()); + return -1; + } + + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_HEARTBEAT, mndProcessHeartBeatMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CONNECT, mndProcessConnectMsg); + + mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndGetConnsMeta); + mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns); + mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndCancelGetNextConn); + return 0; +} + +void mndCleanupProfile(SMnode *pMnode) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + if (pMgmt->cache != NULL) { + taosCacheCleanup(pMgmt->cache); + pMgmt->cache = NULL; + } +} + +static SConnObj *mndCreateConn(SMnode *pMnode, char *user, uint32_t ip, uint16_t port, int32_t pid, const char *app) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + int32_t connId = atomic_add_fetch_32(&pMgmt->connId, 1); + if (connId == 0) atomic_add_fetch_32(&pMgmt->connId, 1); + + SConnObj connObj = {.pid = pid, + .connId = connId, + .killed = 0, + .port = port, + .ip = ip, + .stime = taosGetTimestampMs(), + .lastAccess = 0, + .queryId = 0, + .streamId = 0, + .numOfQueries = 0, + .numOfStreams = 0, + .pStreams = NULL, + .pQueries = NULL}; + + connObj.lastAccess = connObj.stime; + tstrncpy(connObj.user, user, TSDB_USER_LEN); + tstrncpy(connObj.app, app, TSDB_APP_NAME_LEN); + + int32_t keepTime = pMnode->shellActivityTimer * 3; + SConnObj *pConn = taosCachePut(pMgmt->cache, &connId, sizeof(int32_t), &connObj, sizeof(connObj), keepTime * 1000); + + mDebug("conn:%d, is created, user:%s", connId, user); + return pConn; +} + +static void mndFreeConn(SConnObj *pConn) { + tfree(pConn->pQueries); + tfree(pConn->pStreams); + mDebug("conn:%d, is destroyed", pConn->connId); +} + +static SConnObj *mndAcquireConn(SMnode *pMnode, int32_t connId, char *newUser, uint32_t newIp, uint16_t newPort) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &connId, sizeof(int32_t)); + if (pConn == NULL) { + mDebug("conn:%d, already destroyed, user:%s", connId, newUser); + return NULL; + } + + if (pConn->ip != newIp || pConn->port != newPort /* || strcmp(pConn->user, newUser) != 0 */) { + char oldIpStr[30]; + char newIpStr[30]; + taosIp2String(pConn->ip, oldIpStr); + taosIp2String(newIp, newIpStr); + mDebug("conn:%d, incoming conn user:%s ip:%s:%u, not match exist user:%s ip:%s:%u", connId, newUser, newIpStr, + newPort, pConn->user, oldIpStr, pConn->port); + + if (pMgmt->connId < connId) pMgmt->connId = connId + 1; + taosCacheRelease(pMgmt->cache, (void **)&pConn, false); + return NULL; + } + + int32_t keepTime = pMnode->shellActivityTimer * 3; + pConn->lastAccess = keepTime * 1000 + (uint64_t)taosGetTimestampMs(); + return pConn; +} + +static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + if (pConn == NULL) return; + taosCacheRelease(pMgmt->cache, (void **)&pConn, false); +} + +static void *mndGetNextConn(SMnode *pMnode, void *pIter, SConnObj **pConn) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + *pConn = NULL; + + pIter = taosHashIterate(pMgmt->cache->pHashTable, pIter); + if (pIter == NULL) return NULL; + + SCacheDataNode **pNode = pIter; + if (pNode == NULL || *pNode == NULL) { + taosHashCancelIterate(pMgmt->cache->pHashTable, pIter); + return NULL; + } + + *pConn = (SConnObj *)((*pNode)->data); + return pIter; +} + +static void mndCancelGetNextConn(SMnode *pMnode, void *pIter) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + taosHashCancelIterate(pMgmt->cache->pHashTable, pIter); +} + +static int32_t mndProcessConnectMsg(SMnode *pMnode, SMnodeMsg *pMsg) { + SConnectMsg *pReq = pMsg->rpcMsg.pCont; + pReq->pid = htonl(pReq->pid); + + SRpcConnInfo info = {0}; + if (rpcGetConnInfo(pMsg->rpcMsg.handle, &info) != 0) { + mError("user:%s, failed to login while get connection info since %s", pMsg->user, terrstr()); + return -1; + } + + char ip[30]; + taosIp2String(info.clientIp, ip); + + if (pReq->db[0]) { + snprintf(pMsg->db, TSDB_FULL_DB_NAME_LEN, "%d%s%s", pMsg->acctId, TS_PATH_DELIMITER, pReq->db); + SDbObj *pDb = mndAcquireDb(pMnode, pMsg->db); + if (pDb == NULL) { + terrno = TSDB_CODE_MND_INVALID_DB; + mError("user:%s, failed to login from %s while use db:%s since %s", pMsg->user, ip, pReq->db, terrstr()); + return -1; + } + mndReleaseDb(pMnode, pDb); + } + + SConnObj *pConn = mndCreateConn(pMnode, info.user, info.clientIp, info.clientPort, pReq->pid, pReq->app); + if (pConn == NULL) { + mError("user:%s, failed to login from %s while create connection since %s", pMsg->user, ip, terrstr()); + return -1; + } + + SConnectRsp *pRsp = rpcMallocCont(sizeof(SConnectRsp)); + if (pRsp == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("user:%s, failed to login from %s while create rsp since %s", pMsg->user, ip, terrstr()); + return -1; + } + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser != NULL) { + pRsp->acctId = htonl(pUser->acctId); + pRsp->superAuth = pUser->superAuth; + pRsp->readAuth = pUser->readAuth; + pRsp->writeAuth = pUser->writeAuth; + mndReleaseUser(pMnode, pUser); + } + + pRsp->acctId = htonl(pUser->acctId); + pRsp->clusterId = htonl(pMnode->clusterId); + pRsp->connId = htonl(pConn->connId); + mndGetMnodeEpSet(pMnode, &pRsp->epSet); + + pMsg->contLen = sizeof(SConnectRsp); + pMsg->pCont = pRsp; + mDebug("user:%s, login from %s, conn:%d", info.user, ip, pConn->connId); + return 0; +} + +static int32_t mndProcessHeartBeatMsg(SMnode *pMnode, SMnodeMsg *pMsg) { + SHeartBeatMsg *pReq = pMsg->rpcMsg.pCont; + pReq->connId = htonl(pReq->connId); + pReq->pid = htonl(pReq->pid); + + SRpcConnInfo info = {0}; + if (rpcGetConnInfo(pMsg->rpcMsg.handle, &info) != 0) { + mError("user:%s, connId:%d failed to process hb since %s", pMsg->user, pReq->connId, terrstr()); + return -1; + } + + SConnObj *pConn = mndAcquireConn(pMnode, pReq->connId, info.user, info.clientIp, info.clientPort); + if (pConn == NULL) { + pConn = mndCreateConn(pMnode, info.user, info.clientIp, info.clientPort, pReq->pid, pReq->app); + if (pConn == NULL) { + mError("user:%s, conn:%d is freed and failed to create new conn since %s", pMsg->user, pReq->connId, terrstr()); + return -1; + } else { + mDebug("user:%s, conn:%d is freed and create a new conn:%d", pMsg->user, pReq->connId, pConn->connId); + } + } + + SHeartBeatRsp *pRsp = rpcMallocCont(sizeof(SHeartBeatRsp)); + if (pRsp == NULL) { + mndReleaseConn(pMnode, pConn); + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("user:%s, conn:%d failed to process hb while create rsp since %s", pMsg->user, pReq->connId, terrstr()); + return -1; + } + + pRsp->connId = htonl(pConn->connId); + pRsp->killConnection = pConn->killed; + mndGetMnodeEpSet(pMnode, &pRsp->epSet); + mndReleaseConn(pMnode, pConn); + + pMsg->contLen = sizeof(SConnectRsp); + pMsg->pCont = pRsp; + return 0; +} + +static int32_t mndGetConnsMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) { + SMnode *pMnode = pMsg->pMnode; + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser == NULL) return 0; + if (!pUser->superAuth) { + mndReleaseUser(pMnode, pUser); + return TSDB_CODE_MND_NO_RIGHTS; + } + mndReleaseUser(pMnode, pUser); + + int32_t cols = 0; + SSchema *pSchema = pMeta->schema; + + pShow->bytes[cols] = 4; + pSchema[cols].type = TSDB_DATA_TYPE_INT; + strcpy(pSchema[cols].name, "connId"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "user"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + // app name + pShow->bytes[cols] = TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "program"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + // app pid + pShow->bytes[cols] = 4; + pSchema[cols].type = TSDB_DATA_TYPE_INT; + strcpy(pSchema[cols].name, "pid"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "ip:port"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; + strcpy(pSchema[cols].name, "login_time"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; + strcpy(pSchema[cols].name, "last_access"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pMeta->numOfColumns = htons(cols); + pShow->numOfColumns = cols; + + pShow->offset[0] = 0; + for (int32_t i = 1; i < cols; ++i) { + pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; + } + + pShow->numOfRows = taosHashGetSize(pMgmt->cache->pHashTable); + pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; + + return 0; +} + +static int32_t mndRetrieveConns(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) { + SMnode *pMnode = pMsg->pMnode; + int32_t numOfRows = 0; + SConnObj *pConnObj = NULL; + int32_t cols = 0; + char *pWrite; + char ipStr[TSDB_IPv4ADDR_LEN + 6]; + + while (numOfRows < rows) { + pShow->pIter = mndGetNextConn(pMnode, pShow->pIter, &pConnObj); + if (pConnObj == NULL) break; + + cols = 0; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int32_t *)pWrite = pConnObj->connId; + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pConnObj->user, pShow->bytes[cols]); + cols++; + + // app name + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pConnObj->app, pShow->bytes[cols]); + cols++; + + // app pid + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int32_t *)pWrite = pConnObj->pid; + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + taosIpPort2String(pConnObj->ip, pConnObj->port, ipStr); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, ipStr, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int64_t *)pWrite = pConnObj->stime; + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + if (pConnObj->lastAccess < pConnObj->stime) pConnObj->lastAccess = pConnObj->stime; + *(int64_t *)pWrite = pConnObj->lastAccess; + cols++; + + numOfRows++; + } + + pShow->numOfReads += numOfRows; + + return numOfRows; +} + +static int32_t mndGetQueryMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj *pShow) { + SMnode *pMnode = pMsg->pMnode; + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser == NULL) return 0; + if (!pUser->superAuth) { + mndReleaseUser(pMnode, pUser); + return TSDB_CODE_MND_NO_RIGHTS; + } + mndReleaseUser(pMnode, pUser); + + int32_t cols = 0; + SSchema *pSchema = pMeta->schema; + + pShow->bytes[cols] = QUERY_ID_SIZE + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "query_id"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "user"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "ip:port"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 24; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "qid"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; + strcpy(pSchema[cols].name, "created_time"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_BIGINT; + strcpy(pSchema[cols].name, "time"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = QUERY_OBJ_ID_SIZE + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "sql_obj_id"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 4; + pSchema[cols].type = TSDB_DATA_TYPE_INT; + strcpy(pSchema[cols].name, "pid"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_EP_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "ep"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 1; + pSchema[cols].type = TSDB_DATA_TYPE_BOOL; + strcpy(pSchema[cols].name, "stable_query"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 4; + pSchema[cols].type = TSDB_DATA_TYPE_INT; + strcpy(pSchema[cols].name, "sub_queries"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "sub_query_info"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "sql"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pMeta->numOfColumns = htons(cols); + pShow->numOfColumns = cols; + + pShow->offset[0] = 0; + for (int32_t i = 1; i < cols; ++i) { + pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; + } + + pShow->numOfRows = 1000000; + pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; + + return 0; +} + +static int32_t mndRetrieveQueries(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) { + SMnode *pMnode = pMsg->pMnode; + int32_t numOfRows = 0; + SConnObj *pConnObj = NULL; + int32_t cols = 0; + char * pWrite; + void * pIter; + char str[TSDB_IPv4ADDR_LEN + 6] = {0}; + + while (numOfRows < rows) { + pIter = mndGetNextConn(pMnode, pShow->pIter, &pConnObj); + if (pConnObj == NULL) { + pShow->pIter = pIter; + break; + } + + if (numOfRows + pConnObj->numOfQueries >= rows) { + mndCancelGetNextConn(pMnode, pIter); + break; + } + + pShow->pIter = pIter; + for (int32_t i = 0; i < pConnObj->numOfQueries; ++i) { + SQueryDesc *pDesc = pConnObj->pQueries + i; + cols = 0; + + snprintf(str, QUERY_ID_SIZE + 1, "%u:%u", pConnObj->connId, htonl(pDesc->queryId)); + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, str, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pConnObj->user, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + snprintf(str, tListLen(str), "%s:%u", taosIpStr(pConnObj->ip), pConnObj->port); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, str, pShow->bytes[cols]); + cols++; + + char handleBuf[24] = {0}; + snprintf(handleBuf, tListLen(handleBuf), "%" PRIu64, htobe64(pDesc->qId)); + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, handleBuf, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int64_t *)pWrite = htobe64(pDesc->stime); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int64_t *)pWrite = htobe64(pDesc->useconds); + cols++; + + snprintf(str, tListLen(str), "0x%" PRIx64, htobe64(pDesc->sqlObjId)); + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, str, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int32_t *)pWrite = htonl(pDesc->pid); + cols++; + + char epBuf[TSDB_EP_LEN + 1] = {0}; + snprintf(epBuf, tListLen(epBuf), "%s:%u", pDesc->fqdn, pConnObj->port); + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, epBuf, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(bool *)pWrite = pDesc->stableQuery; + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int32_t *)pWrite = htonl(pDesc->numOfSub); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDesc->subSqlInfo, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDesc->sql, pShow->bytes[cols]); + cols++; + + numOfRows++; + } + } + + pShow->numOfReads += numOfRows; + return numOfRows; +} + +static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + taosHashCancelIterate(pMgmt->cache->pHashTable, pIter); +} + +static int32_t mndGetStreamMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj *pShow) { + SMnode *pMnode = pMsg->pMnode; + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser == NULL) return 0; + if (!pUser->superAuth) { + mndReleaseUser(pMnode, pUser); + return TSDB_CODE_MND_NO_RIGHTS; + } + mndReleaseUser(pMnode, pUser); + + int32_t cols = 0; + SSchema *pSchema = pMeta->schema; + + pShow->bytes[cols] = QUERY_ID_SIZE + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "streamId"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "user"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "dest table"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_IPv4ADDR_LEN + 6 + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "ip:port"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; + strcpy(pSchema[cols].name, "created time"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; + strcpy(pSchema[cols].name, "exec time"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 8; + pSchema[cols].type = TSDB_DATA_TYPE_BIGINT; + strcpy(pSchema[cols].name, "time(us)"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE; + pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + strcpy(pSchema[cols].name, "sql"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pShow->bytes[cols] = 4; + pSchema[cols].type = TSDB_DATA_TYPE_INT; + strcpy(pSchema[cols].name, "cycles"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + + pMeta->numOfColumns = htons(cols); + pShow->numOfColumns = cols; + + pShow->offset[0] = 0; + for (int32_t i = 1; i < cols; ++i) { + pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; + } + + pShow->numOfRows = 1000000; + pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; + + return 0; +} + +static int32_t mndRetrieveStreams(SShowObj *pShow, char *data, int32_t rows, SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + int32_t numOfRows = 0; + SConnObj *pConnObj = NULL; + int32_t cols = 0; + char *pWrite; + void *pIter; + char ipStr[TSDB_IPv4ADDR_LEN + 6]; + + while (numOfRows < rows) { + pIter = mndGetNextConn(pMnode, pShow->pIter, &pConnObj); + if (pConnObj == NULL) { + pShow->pIter = pIter; + break; + } + + if (numOfRows + pConnObj->numOfStreams >= rows) { + mndCancelGetNextConn(pMnode, pIter); + break; + } + + pShow->pIter = pIter; + for (int32_t i = 0; i < pConnObj->numOfStreams; ++i) { + SStreamDesc *pDesc = pConnObj->pStreams + i; + cols = 0; + + snprintf(ipStr, QUERY_ID_SIZE + 1, "%u:%u", pConnObj->connId, htonl(pDesc->streamId)); + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, ipStr, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pConnObj->user, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDesc->dstTable, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + snprintf(ipStr, sizeof(ipStr), "%s:%u", taosIpStr(pConnObj->ip), pConnObj->port); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, ipStr, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int64_t *)pWrite = htobe64(pDesc->ctime); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int64_t *)pWrite = htobe64(pDesc->stime); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int64_t *)pWrite = htobe64(pDesc->useconds); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pDesc->sql, pShow->bytes[cols]); + cols++; + + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int32_t *)pWrite = (int32_t)htobe64(pDesc->num); + cols++; + + numOfRows++; + } + } + + pShow->numOfReads += numOfRows; + return numOfRows; +} + +static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) { + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + taosHashCancelIterate(pMgmt->cache->pHashTable, pIter); +} diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index af6a203006..790f5c1737 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -92,7 +92,7 @@ static int32_t mndProcessShowMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { pRsp->qhandle = htobe64((uint64_t)pShow); - int32_t code = (*metaFp)(pMnode, &pRsp->tableMeta, pShow, pMnodeMsg->rpcMsg.handle); + int32_t code = (*metaFp)(pMnodeMsg,pShow, &pRsp->tableMeta); mDebug("show:%d, type:%s, get meta finished, numOfRows:%d cols:%d result:%s", pShow->id, mndShowStr(type), pShow->numOfRows, pShow->numOfColumns, tstrerror(code)); @@ -169,7 +169,7 @@ static int32_t mndProcessRetrieveMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { // if free flag is set, client wants to clean the resources if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { - rowsRead = (*retrieveFp)(pMnode, pShow, pRsp->data, rowsToRead, pMnodeMsg->rpcMsg.handle); + rowsRead = (*retrieveFp)(pMnodeMsg, pShow, pRsp->data, rowsToRead); } mDebug("show:%d, stop retrieve data, rowsRead:%d rowsToRead:%d", pShow->id, rowsRead, rowsToRead); @@ -311,17 +311,17 @@ void mnodeVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capa } } -void mnodeAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp) { +void mndAddShowMetaHandle(SMnode *pMnode, EShowType showType, ShowMetaFp fp) { SShowMgmt *pMgmt = &pMnode->showMgmt; pMgmt->metaFps[showType] = fp; } -void mnodeAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp) { +void mndAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp) { SShowMgmt *pMgmt = &pMnode->showMgmt; pMgmt->retrieveFps[showType] = fp; } -void mnodeAddShowFreeIterHandle(SMnode *pMnode, EShowType showType, ShowFreeIterFp fp) { +void mndAddShowFreeIterHandle(SMnode *pMnode, EShowType showType, ShowFreeIterFp fp) { SShowMgmt *pMgmt = &pMnode->showMgmt; pMgmt->freeIterFps[showType] = fp; } diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index abbe41a60d..78869bd416 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -30,7 +30,7 @@ static SSdbRaw *mndUserActionEncode(SUserObj *pUser) { SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN) SDB_SET_INT64(pRaw, dataPos, pUser->createdTime) SDB_SET_INT64(pRaw, dataPos, pUser->updateTime) - SDB_SET_INT8(pRaw, dataPos, pUser->rootAuth) + SDB_SET_INT8(pRaw, dataPos, pUser->superAuth) SDB_SET_DATALEN(pRaw, dataPos); return pRaw; @@ -56,7 +56,7 @@ static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) { SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->acct, TSDB_USER_LEN) SDB_GET_INT64(pRaw, pRow, dataPos, &pUser->createdTime) SDB_GET_INT64(pRaw, pRow, dataPos, &pUser->updateTime) - SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->rootAuth) + SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->superAuth) return pRow; } @@ -70,12 +70,14 @@ static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) { return -1; } - pUser->pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct); - if (pUser->pAcct == NULL) { + SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct); + if (pAcct == NULL) { terrno = TSDB_CODE_MND_ACCT_NOT_EXIST; mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr()); return -1; } + pUser->acctId = pAcct->acctId; + sdbRelease(pSdb, pAcct); return 0; } @@ -87,11 +89,6 @@ static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) { pUser->prohibitDbHash = NULL; } - if (pUser->pAcct != NULL) { - sdbRelease(pSdb, pUser->pAcct); - pUser->pAcct = NULL; - } - return 0; } @@ -102,7 +99,7 @@ static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDs memcpy(pSrcUser->acct, pDstUser->acct, TSDB_USER_LEN); pSrcUser->createdTime = pDstUser->createdTime; pSrcUser->updateTime = pDstUser->updateTime; - pSrcUser->rootAuth = pDstUser->rootAuth; + pSrcUser->superAuth = pDstUser->superAuth; return 0; } @@ -115,7 +112,7 @@ static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char userObj.updateTime = userObj.createdTime; if (strcmp(user, TSDB_DEFAULT_USER) == 0) { - userObj.rootAuth = 1; + userObj.superAuth = 1; } SSdbRaw *pRaw = mndUserActionEncode(&userObj); @@ -145,7 +142,7 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass); userObj.createdTime = taosGetTimestampMs(); userObj.updateTime = userObj.createdTime; - userObj.rootAuth = 0; + userObj.superAuth = 0; STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle); if (pTrans == NULL) return -1; diff --git a/source/util/src/tutil.c b/source/util/src/tutil.c index ee524e4448..b9d2da6939 100644 --- a/source/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -410,3 +410,10 @@ char *taosIpStr(uint32_t ipInt) { return ipStr; } +void taosIp2String(uint32_t ip, char *str) { + sprintf(str, "%u.%u.%u.%u", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (uint8_t)(ip >> 24)); +} + +void taosIpPort2String(uint32_t ip, uint16_t port, char *str) { + sprintf(str, "%u.%u.%u.%u:%u", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (uint8_t)(ip >> 24), port); +} \ No newline at end of file From 92ec87906ee2460e4dd6ea43cc4915220afe6b70 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 3 Dec 2021 14:40:35 +0800 Subject: [PATCH 06/11] reformat --- CMakeLists.txt | 3 --- contrib/CMakeLists.txt | 29 ++++++++++++++++------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 056fb6b509..b626977588 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,9 +6,6 @@ project( DESCRIPTION "An open-source big data platform designed and optimized for the Internet of Things(IOT)" ) -# ============================================================================ -# DEPENDENCIES -# ============================================================================ set(CMAKE_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/cmake") set(CMAKE_CONTRIB_DIR "${CMAKE_SOURCE_DIR}/contrib") include(${CMAKE_SUPPORT_DIR}/cmake.options) diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index 37241bba9f..cffe164488 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -1,3 +1,6 @@ +# ================================================================================================ +# Download +# ================================================================================================ function(cat IN_FILE OUT_FILE) file(READ ${IN_FILE} CONTENTS) file(APPEND ${OUT_FILE} "${CONTENTS}") @@ -6,53 +9,53 @@ endfunction(cat IN_FILE OUT_FILE) set(CONTRIB_TMP_FILE "${CMAKE_BINARY_DIR}/deps_tmp_CMakeLists.txt.in") configure_file("${CMAKE_SUPPORT_DIR}/deps_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) -## googletest +# googletest if(${BUILD_TEST}) cat("${CMAKE_SUPPORT_DIR}/gtest_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) endif(${BUILD_TEST}) -## lz4 +# lz4 cat("${CMAKE_SUPPORT_DIR}/lz4_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) -## zlib +# zlib cat("${CMAKE_SUPPORT_DIR}/zlib_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) -## cJson +# cJson cat("${CMAKE_SUPPORT_DIR}/cjson_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) -## leveldb +# leveldb if(${BUILD_WITH_LEVELDB}) cat("${CMAKE_SUPPORT_DIR}/leveldb_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) endif(${BUILD_WITH_LEVELDB}) -## rocksdb +# rocksdb if(${BUILD_WITH_ROCKSDB}) cat("${CMAKE_SUPPORT_DIR}/rocksdb_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) add_definitions(-DUSE_ROCKSDB) endif(${BUILD_WITH_ROCKSDB}) -## bdb +# bdb if(${BUILD_WITH_BDB}) cat("${CMAKE_SUPPORT_DIR}/bdb_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) endif(${BUILD_WITH_DBD}) -## sqlite +# sqlite if(${BUILD_WITH_SQLITE}) cat("${CMAKE_SUPPORT_DIR}/sqlite_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) endif(${BUILD_WITH_SQLITE}) -## lucene +# lucene if(${BUILD_WITH_LUCENE}) cat("${CMAKE_SUPPORT_DIR}/lucene_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) add_definitions(-DUSE_LUCENE) endif(${BUILD_WITH_LUCENE}) -## NuRaft +# NuRaft if(${BUILD_WITH_NURAFT}) cat("${CMAKE_SUPPORT_DIR}/nuraft_CMakeLists.txt.in" ${CONTRIB_TMP_FILE}) endif(${BUILD_WITH_NURAFT}) -## download dependencies +# download dependencies configure_file(${CONTRIB_TMP_FILE} "${CMAKE_CONTRIB_DIR}/deps-download/CMakeLists.txt") execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${CMAKE_CONTRIB_DIR}/deps-download") @@ -60,7 +63,7 @@ execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_CONTRIB_DIR}/deps-download") # ================================================================================================ -# DEPENDENCIES +# Build # ================================================================================================ # googletest if(${BUILD_TEST}) @@ -171,7 +174,7 @@ endif(${BUILD_WITH_SQLITE}) # ================================================================================================ -# DEPENDENCY TEST +# Build test # ================================================================================================ if(${BUILD_DEPENDENCY_TESTS}) add_subdirectory(test) From 8a850dd3f2fb650e7c5b367e5f2b4fe2d4f7e84b Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 3 Dec 2021 15:01:26 +0800 Subject: [PATCH 07/11] TD-10431 process mnode profile --- include/common/taosmsg.h | 1 + source/dnode/mgmt/impl/src/dndTransport.c | 1 + source/dnode/mnode/impl/inc/mndInt.h | 2 +- source/dnode/mnode/impl/src/mndDb.c | 9 +- source/dnode/mnode/impl/src/mndDnode.c | 9 +- source/dnode/mnode/impl/src/mndMnode.c | 4 +- source/dnode/mnode/impl/src/mndProfile.c | 142 ++++++++++++++++++++-- source/dnode/mnode/impl/src/mndShow.c | 10 +- source/dnode/mnode/impl/src/mndUser.c | 3 +- source/dnode/mnode/impl/src/mnode.c | 2 +- 10 files changed, 157 insertions(+), 26 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index e29accd249..e14788be47 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -81,6 +81,7 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE, "alter-stable" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE, "drop-stable" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_STABLE_VGROUP, "stable-vgroup" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_KILL_QUERY, "kill-query" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_KILL_STREAM, "kill-stream" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_KILL_CONN, "kill-conn" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_HEARTBEAT, "heartbeat" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW, "show" ) diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index f3e71564b2..98a0b8e308 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -76,6 +76,7 @@ static void dndInitMsgFp(STransMgmt *pMgmt) { pMgmt->msgFp[TSDB_MSG_TYPE_DROP_STABLE] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = dndProcessMnodeReadMsg; pMgmt->msgFp[TSDB_MSG_TYPE_KILL_QUERY] = dndProcessMnodeWriteMsg; + pMgmt->msgFp[TSDB_MSG_TYPE_KILL_STREAM] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TSDB_MSG_TYPE_KILL_CONN] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TSDB_MSG_TYPE_HEARTBEAT] = dndProcessMnodeReadMsg; pMgmt->msgFp[TSDB_MSG_TYPE_SHOW] = dndProcessMnodeReadMsg; diff --git a/source/dnode/mnode/impl/inc/mndInt.h b/source/dnode/mnode/impl/inc/mndInt.h index 0f4dd0633b..dbca6ad057 100644 --- a/source/dnode/mnode/impl/inc/mndInt.h +++ b/source/dnode/mnode/impl/inc/mndInt.h @@ -25,7 +25,7 @@ extern "C" { #endif -typedef int32_t (*MndMsgFp)(SMnode *pMnode, SMnodeMsg *pMsg); +typedef int32_t (*MndMsgFp)(SMnodeMsg *pMsg); typedef int32_t (*MndInitFp)(SMnode *pMnode); typedef void (*MndCleanupFp)(SMnode *pMnode); typedef int32_t (*ShowMetaFp)(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta); diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index d990e926bb..f01a43dfef 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "mndDb.h" -static int32_t mnodeProcessUseMsg(SMnode *pMnode, SMnodeMsg *pMsg); +static int32_t mnodeProcessUseMsg(SMnodeMsg *pMsg); int32_t mndInitDb(SMnode *pMnode) { mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_USE_DB, mnodeProcessUseMsg); @@ -35,10 +35,11 @@ void mndReleaseDb(SMnode *pMnode, SDbObj *pDb) { sdbRelease(pSdb, pDb); } -static int32_t mnodeProcessUseMsg(SMnode *pMnode, SMnodeMsg *pMsg) { - SUseDbMsg *pUseDbMsg = pMsg->rpcMsg.pCont; +static int32_t mnodeProcessUseMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + SUseDbMsg *pUse = pMsg->rpcMsg.pCont; - strncpy(pMsg->db, pUseDbMsg->db, TSDB_FULL_DB_NAME_LEN); + strncpy(pMsg->db, pUse->db, TSDB_FULL_DB_NAME_LEN); SDbObj *pDb = mndAcquireDb(pMnode, pMsg->db); if (pDb != NULL) { diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index a6cee3efc5..24e6113161 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -226,7 +226,8 @@ static void mndParseStatusMsg(SStatusMsg *pStatus) { pStatus->clusterCfg.checkTime = htobe64(pStatus->clusterCfg.checkTime); } -static int32_t mndProcessStatusMsg(SMnode *pMnode, SMnodeMsg *pMsg) { +static int32_t mndProcessStatusMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; SStatusMsg *pStatus = pMsg->rpcMsg.pCont; mndParseStatusMsg(pStatus); @@ -315,11 +316,11 @@ static int32_t mndProcessStatusMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessCreateDnodeMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } +static int32_t mndProcessCreateDnodeMsg(SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessDropDnodeMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } +static int32_t mndProcessDropDnodeMsg(SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessConfigDnodeMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } +static int32_t mndProcessConfigDnodeMsg(SMnodeMsg *pMsg) { return 0; } int32_t mndInitDnode(SMnode *pMnode) { SSdbTable table = {.sdbType = SDB_DNODE, diff --git a/source/dnode/mnode/impl/src/mndMnode.c b/source/dnode/mnode/impl/src/mndMnode.c index c1c17e4983..e3464cd327 100644 --- a/source/dnode/mnode/impl/src/mndMnode.c +++ b/source/dnode/mnode/impl/src/mndMnode.c @@ -103,9 +103,9 @@ static int32_t mndCreateDefaultMnode(SMnode *pMnode) { return sdbWrite(pMnode->pSdb, pRaw); } -static int32_t mndProcessCreateMnodeMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } +static int32_t mndProcessCreateMnodeMsg(SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessDropMnodeMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } +static int32_t mndProcessDropMnodeMsg(SMnodeMsg *pMsg) { return 0; } int32_t mndInitMnode(SMnode *pMnode) { SSdbTable table = {.sdbType = SDB_MNODE, diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index ef596f8ee1..d70d045a04 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -50,8 +50,11 @@ static SConnObj *mndAcquireConn(SMnode *pMnode, int32_t connId, char *user, uint static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn); static void *mndGetNextConn(SMnode *pMnode, void *pIter, SConnObj **pConn); static void mndCancelGetNextConn(SMnode *pMnode, void *pIter); -static int32_t mndProcessHeartBeatMsg(SMnode *pMnode, SMnodeMsg *pMsg); -static int32_t mndProcessConnectMsg(SMnode *pMnode, SMnodeMsg *pMsg); +static int32_t mndProcessHeartBeatMsg(SMnodeMsg *pMsg); +static int32_t mndProcessConnectMsg(SMnodeMsg *pMsg); +static int32_t mndProcessKillQueryMsg(SMnodeMsg *pMsg); +static int32_t mndProcessKillStreamMsg(SMnodeMsg *pMsg); +static int32_t mndProcessKillConnectionMsg(SMnodeMsg *pMsg); static int32_t mndGetConnsMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta); static int32_t mndRetrieveConns(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows); static int32_t mndGetQueryMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj *pShow); @@ -74,10 +77,14 @@ int32_t mndInitProfile(SMnode *pMnode) { mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_HEARTBEAT, mndProcessHeartBeatMsg); mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CONNECT, mndProcessConnectMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_KILL_QUERY, mndProcessKillQueryMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CONNECT, mndProcessKillStreamMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_KILL_CONN, mndProcessKillConnectionMsg); mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndGetConnsMeta); mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns); mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndCancelGetNextConn); + return 0; } @@ -183,7 +190,8 @@ static void mndCancelGetNextConn(SMnode *pMnode, void *pIter) { taosHashCancelIterate(pMgmt->cache->pHashTable, pIter); } -static int32_t mndProcessConnectMsg(SMnode *pMnode, SMnodeMsg *pMsg) { +static int32_t mndProcessConnectMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; SConnectMsg *pReq = pMsg->rpcMsg.pCont; pReq->pid = htonl(pReq->pid); @@ -240,7 +248,8 @@ static int32_t mndProcessConnectMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessHeartBeatMsg(SMnode *pMnode, SMnodeMsg *pMsg) { +static int32_t mndProcessHeartBeatMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; SHeartBeatMsg *pReq = pMsg->rpcMsg.pCont; pReq->connId = htonl(pReq->connId); pReq->pid = htonl(pReq->pid); @@ -280,6 +289,118 @@ static int32_t mndProcessHeartBeatMsg(SMnode *pMnode, SMnodeMsg *pMsg) { return 0; } +static int32_t mndProcessKillQueryMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser == NULL) return 0; + if (!pUser->superAuth) { + mndReleaseUser(pMnode, pUser); + terrno = TSDB_CODE_MND_NO_RIGHTS; + return -1; + } + mndReleaseUser(pMnode, pUser); + + SKillQueryMsg *pKill = pMsg->rpcMsg.pCont; + mInfo("kill query msg is received, queryId:%s", pKill->queryId); + + const char delim = ':'; + char *connIdStr = strtok(pKill->queryId, &delim); + char *queryIdStr = strtok(NULL, &delim); + + if (queryIdStr == NULL || connIdStr == NULL) { + mError("failed to kill query, queryId:%s", pKill->queryId); + terrno = TSDB_CODE_MND_INVALID_QUERY_ID; + return -1; + } + + int32_t queryId = (int32_t)strtol(queryIdStr, NULL, 10); + + int32_t connId = atoi(connIdStr); + SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &connId, sizeof(int32_t)); + if (pConn == NULL) { + mError("connId:%s, failed to kill queryId:%d, conn not exist", connIdStr, queryId); + terrno = TSDB_CODE_MND_INVALID_CONN_ID; + return -1; + } else { + mInfo("connId:%s, queryId:%d is killed by user:%s", connIdStr, queryId, pMsg->user); + pConn->queryId = queryId; + taosCacheRelease(pMgmt->cache, (void **)&pConn, false); + return 0; + } +} + +static int32_t mndProcessKillStreamMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser == NULL) return 0; + if (!pUser->superAuth) { + mndReleaseUser(pMnode, pUser); + terrno = TSDB_CODE_MND_NO_RIGHTS; + return -1; + } + mndReleaseUser(pMnode, pUser); + + SKillQueryMsg *pKill = pMsg->rpcMsg.pCont; + mInfo("kill stream msg is received, streamId:%s", pKill->queryId); + + const char delim = ':'; + char *connIdStr = strtok(pKill->queryId, &delim); + char *streamIdStr = strtok(NULL, &delim); + + if (streamIdStr == NULL || connIdStr == NULL) { + mError("failed to kill stream, streamId:%s", pKill->queryId); + terrno = TSDB_CODE_MND_INVALID_STREAM_ID; + return -1; + } + + int32_t streamId = (int32_t)strtol(streamIdStr, NULL, 10); + int32_t connId = atoi(connIdStr); + + SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &connId, sizeof(int32_t)); + if (pConn == NULL) { + mError("connId:%s, failed to kill streamId:%d, conn not exist", connIdStr, streamId); + terrno = TSDB_CODE_MND_INVALID_CONN_ID; + return -1; + } else { + mInfo("connId:%s, streamId:%d is killed by user:%s", connIdStr, streamId, pMsg->user); + pConn->streamId = streamId; + taosCacheRelease(pMgmt->cache, (void **)&pConn, false); + return TSDB_CODE_SUCCESS; + } +} + +static int32_t mndProcessKillConnectionMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + SProfileMgmt *pMgmt = &pMnode->profileMgmt; + + SUserObj *pUser = mndAcquireUser(pMnode, pMsg->user); + if (pUser == NULL) return 0; + if (!pUser->superAuth) { + mndReleaseUser(pMnode, pUser); + terrno = TSDB_CODE_MND_NO_RIGHTS; + return -1; + } + mndReleaseUser(pMnode, pUser); + + SKillConnMsg *pKill = pMsg->rpcMsg.pCont; + int32_t connId = atoi(pKill->queryId); + SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &connId, sizeof(int32_t)); + if (pConn == NULL) { + mError("connId:%s, failed to kill, conn not exist", pKill->queryId); + terrno = TSDB_CODE_MND_INVALID_CONN_ID; + return -1; + } else { + mInfo("connId:%s, is killed by user:%s", pKill->queryId, pMsg->user); + pConn->killed = 1; + taosCacheRelease(pMgmt->cache, (void **)&pConn, false); + return TSDB_CODE_SUCCESS; + } +} + static int32_t mndGetConnsMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) { SMnode *pMnode = pMsg->pMnode; SProfileMgmt *pMgmt = &pMnode->profileMgmt; @@ -288,7 +409,8 @@ static int32_t mndGetConnsMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg * if (pUser == NULL) return 0; if (!pUser->superAuth) { mndReleaseUser(pMnode, pUser); - return TSDB_CODE_MND_NO_RIGHTS; + terrno = TSDB_CODE_MND_NO_RIGHTS; + return -1; } mndReleaseUser(pMnode, pUser); @@ -415,7 +537,8 @@ static int32_t mndGetQueryMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj * if (pUser == NULL) return 0; if (!pUser->superAuth) { mndReleaseUser(pMnode, pUser); - return TSDB_CODE_MND_NO_RIGHTS; + terrno = TSDB_CODE_MND_NO_RIGHTS; + return -1; } mndReleaseUser(pMnode, pUser); @@ -519,8 +642,8 @@ static int32_t mndRetrieveQueries(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t numOfRows = 0; SConnObj *pConnObj = NULL; int32_t cols = 0; - char * pWrite; - void * pIter; + char *pWrite; + void *pIter; char str[TSDB_IPv4ADDR_LEN + 6] = {0}; while (numOfRows < rows) { @@ -621,7 +744,8 @@ static int32_t mndGetStreamMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta, SShowObj if (pUser == NULL) return 0; if (!pUser->superAuth) { mndReleaseUser(pMnode, pUser); - return TSDB_CODE_MND_NO_RIGHTS; + terrno = TSDB_CODE_MND_NO_RIGHTS; + return -1; } mndReleaseUser(pMnode, pUser); diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index 790f5c1737..ff9f2a9805 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -16,8 +16,8 @@ #define _DEFAULT_SOURCE #include "mndShow.h" -static int32_t mndProcessShowMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg); -static int32_t mndProcessRetrieveMsg(SMnode *pMnode, SMnodeMsg *pMsg); +static int32_t mndProcessShowMsg(SMnodeMsg *pMnodeMsg); +static int32_t mndProcessRetrieveMsg( SMnodeMsg *pMsg); static bool mndCheckRetrieveFinished(SShowObj *pShow); static int32_t mndAcquireShowObj(SMnode *pMnode, SShowObj *pShow); static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove); @@ -42,7 +42,8 @@ int32_t mndInitShow(SMnode *pMnode) { void mndCleanupShow(SMnode *pMnode) {} -static int32_t mndProcessShowMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { +static int32_t mndProcessShowMsg(SMnodeMsg *pMnodeMsg) { + SMnode *pMnode = pMnodeMsg->pMnode; SShowMgmt *pMgmt = &pMnode->showMgmt; SShowMsg *pMsg = pMnodeMsg->rpcMsg.pCont; int8_t type = pMsg->type; @@ -108,7 +109,8 @@ static int32_t mndProcessShowMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { } } -static int32_t mndProcessRetrieveMsg(SMnode *pMnode, SMnodeMsg *pMnodeMsg) { +static int32_t mndProcessRetrieveMsg(SMnodeMsg *pMnodeMsg) { + SMnode *pMnode = pMnodeMsg->pMnode; SShowMgmt *pMgmt = &pMnode->showMgmt; int32_t rowsToRead = 0; int32_t size = 0; diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index 78869bd416..d3a8cf56be 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -180,7 +180,8 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, return 0; } -static int32_t mndProcessCreateUserMsg(SMnode *pMnode, SMnodeMsg *pMsg) { +static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont; if (pCreate->user[0] == 0) { diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index e390deda6d..3a5b6c470b 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -386,7 +386,7 @@ static void mndProcessRpcMsg(SMnodeMsg *pMsg) { goto PROCESS_RPC_END; } - code = (*fp)(pMnode, pMsg); + code = (*fp)(pMsg); if (code != 0) { code = terrno; mError("msg:%p, app:%p failed to process since %s", pMsg, ahandle, terrstr()); From 3e34c4cd2645bd9ceaeadb4eeaff5fbc7ec70824 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 3 Dec 2021 17:22:44 +0800 Subject: [PATCH 08/11] TD-10431 process mnode profile --- source/dnode/mnode/impl/src/mndProfile.c | 53 +++++++++- source/dnode/mnode/impl/src/mndShow.c | 126 ++++++++++++----------- 2 files changed, 118 insertions(+), 61 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index d70d045a04..eb9926cfb6 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -248,6 +248,41 @@ static int32_t mndProcessConnectMsg(SMnodeMsg *pMsg) { return 0; } +static int32_t mnodeSaveQueryStreamList(SConnObj *pConn, SHeartBeatMsg *pMsg) { + pConn->numOfQueries = 0; + pConn->numOfStreams = 0; + int32_t numOfQueries = htonl(pMsg->numOfQueries); + int32_t numOfStreams = htonl(pMsg->numOfStreams); + + if (numOfQueries > 0) { + if (pConn->pQueries == NULL) { + pConn->pQueries = calloc(sizeof(SQueryDesc), QUERY_STREAM_SAVE_SIZE); + } + + pConn->numOfQueries = MIN(QUERY_STREAM_SAVE_SIZE, numOfQueries); + + int32_t saveSize = pConn->numOfQueries * sizeof(SQueryDesc); + if (saveSize > 0 && pConn->pQueries != NULL) { + memcpy(pConn->pQueries, pMsg->pData, saveSize); + } + } + + if (numOfStreams > 0) { + if (pConn->pStreams == NULL) { + pConn->pStreams = calloc(sizeof(SStreamDesc), QUERY_STREAM_SAVE_SIZE); + } + + pConn->numOfStreams = MIN(QUERY_STREAM_SAVE_SIZE, numOfStreams); + + int32_t saveSize = pConn->numOfStreams * sizeof(SStreamDesc); + if (saveSize > 0 && pConn->pStreams != NULL) { + memcpy(pConn->pStreams, pMsg->pData + numOfQueries * sizeof(SQueryDesc), saveSize); + } + } + + return TSDB_CODE_SUCCESS; +} + static int32_t mndProcessHeartBeatMsg(SMnodeMsg *pMsg) { SMnode *pMnode = pMsg->pMnode; SHeartBeatMsg *pReq = pMsg->rpcMsg.pCont; @@ -279,8 +314,24 @@ static int32_t mndProcessHeartBeatMsg(SMnodeMsg *pMsg) { return -1; } + mnodeSaveQueryStreamList(pConn, pReq); + if (pConn->killed != 0) { + pRsp->killConnection = 1; + } + + if (pConn->streamId != 0) { + pRsp->streamId = htonl(pConn->streamId); + pConn->streamId = 0; + } + + if (pConn->queryId != 0) { + pRsp->queryId = htonl(pConn->queryId); + pConn->queryId = 0; + } + pRsp->connId = htonl(pConn->connId); - pRsp->killConnection = pConn->killed; + pRsp->totalDnodes = htnol(1); + pRsp->onlineDnodes = htonl(1); mndGetMnodeEpSet(pMnode, &pRsp->epSet); mndReleaseConn(pMnode, pConn); diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index ff9f2a9805..e652f94957 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -40,7 +40,72 @@ int32_t mndInitShow(SMnode *pMnode) { return 0; } -void mndCleanupShow(SMnode *pMnode) {} +void mndCleanupShow(SMnode *pMnode) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + if (pMgmt->cache != NULL) { + taosCacheCleanup(pMgmt->cache); + pMgmt->cache = NULL; + } +} + +static int32_t mndAcquireShowObj(SMnode *pMnode, SShowObj *pShow) { + TSDB_CACHE_PTR_TYPE handleVal = (TSDB_CACHE_PTR_TYPE)pShow; + + SShowMgmt *pMgmt = &pMnode->showMgmt; + SShowObj **ppShow = taosCacheAcquireByKey(pMgmt->cache, &handleVal, sizeof(TSDB_CACHE_PTR_TYPE)); + if (ppShow) { + mTrace("show:%d, data:%p acquired from cache", pShow->id, ppShow); + return 0; + } + + return -1; +} + +static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) { + SMnode *pMnode = pShow->pMnode; + SShowMgmt *pMgmt = &pMnode->showMgmt; + SShowObj **ppShow = (SShowObj **)pShow->ppShow; + taosCacheRelease(pMgmt->cache, (void **)(&ppShow), forceRemove); + mDebug("show:%d, data:%p released from cache, force:%d", pShow->id, ppShow, forceRemove); +} + +static int32_t mndPutShowObj(SMnode *pMnode, SShowObj *pShow) { + SShowMgmt *pMgmt = &pMnode->showMgmt; + int32_t lifeSpan = pMnode->shellActivityTimer * 6 * 1000; + + TSDB_CACHE_PTR_TYPE val = (TSDB_CACHE_PTR_TYPE)pShow; + pShow->id = atomic_add_fetch_32(&pMgmt->showId, 1); + SShowObj **ppShow = + taosCachePut(pMgmt->cache, &val, sizeof(TSDB_CACHE_PTR_TYPE), &pShow, sizeof(TSDB_CACHE_PTR_TYPE), lifeSpan); + if (ppShow == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + mError("show:%d, failed to put into cache", pShow->id); + return -1; + } + + mTrace("show:%d, data:%p put into cache", pShow->id, ppShow); + return 0; +} + +static void mndFreeShowObj(void *ppShow) { + SShowObj *pShow = *(SShowObj **)ppShow; + SMnode *pMnode = pShow->pMnode; + SShowMgmt *pMgmt = &pMnode->showMgmt; + + ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type]; + if (freeFp != NULL) { + if (pShow->pVgIter != NULL) { + // only used in 'show vnodes "ep"' + (*freeFp)(pMnode, pShow->pVgIter); + } + if (pShow->pIter != NULL) { + (*freeFp)(pMnode, pShow->pIter); + } + } + + mDebug("show:%d, data:%p destroyed", pShow->id, ppShow); + tfree(pShow); +} static int32_t mndProcessShowMsg(SMnodeMsg *pMnodeMsg) { SMnode *pMnode = pMnodeMsg->pMnode; @@ -246,65 +311,6 @@ static bool mndCheckRetrieveFinished(SShowObj *pShow) { return false; } -static int32_t mndAcquireShowObj(SMnode *pMnode, SShowObj *pShow) { - TSDB_CACHE_PTR_TYPE handleVal = (TSDB_CACHE_PTR_TYPE)pShow; - - SShowMgmt *pMgmt = &pMnode->showMgmt; - SShowObj **ppShow = taosCacheAcquireByKey(pMgmt->cache, &handleVal, sizeof(TSDB_CACHE_PTR_TYPE)); - if (ppShow) { - mTrace("show:%d, data:%p acquired from cache", pShow->id, ppShow); - return 0; - } - - return -1; -} - -static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) { - SMnode *pMnode = pShow->pMnode; - SShowMgmt *pMgmt = &pMnode->showMgmt; - SShowObj **ppShow = (SShowObj **)pShow->ppShow; - taosCacheRelease(pMgmt->cache, (void **)(&ppShow), forceRemove); - mDebug("show:%d, data:%p released from cache, force:%d", pShow->id, ppShow, forceRemove); -} - -static int32_t mndPutShowObj(SMnode *pMnode, SShowObj *pShow) { - SShowMgmt *pMgmt = &pMnode->showMgmt; - int32_t lifeSpan = pMnode->shellActivityTimer * 6 * 1000; - - TSDB_CACHE_PTR_TYPE val = (TSDB_CACHE_PTR_TYPE)pShow; - pShow->id = atomic_add_fetch_32(&pMgmt->showId, 1); - SShowObj **ppShow = - taosCachePut(pMgmt->cache, &val, sizeof(TSDB_CACHE_PTR_TYPE), &pShow, sizeof(TSDB_CACHE_PTR_TYPE), lifeSpan); - if (ppShow == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - mError("show:%d, failed to put into cache", pShow->id); - return -1; - } - - mTrace("show:%d, data:%p put into cache", pShow->id, ppShow); - return 0; -} - -static void mndFreeShowObj(void *ppShow) { - SShowObj *pShow = *(SShowObj **)ppShow; - SMnode *pMnode = pShow->pMnode; - SShowMgmt *pMgmt = &pMnode->showMgmt; - - ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type]; - if (freeFp != NULL) { - if (pShow->pVgIter != NULL) { - // only used in 'show vnodes "ep"' - (*freeFp)(pMnode, pShow->pVgIter); - } - if (pShow->pIter != NULL) { - (*freeFp)(pMnode, pShow->pIter); - } - } - - mDebug("show:%d, data:%p destroyed", pShow->id, ppShow); - tfree(pShow); -} - void mnodeVacuumResult(char *data, int32_t numOfCols, int32_t rows, int32_t capacity, SShowObj *pShow) { if (rows < capacity) { for (int32_t i = 0; i < numOfCols; ++i) { From 608b96d38474c99d8347a1537199dfaec208693c Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 3 Dec 2021 20:52:44 +0800 Subject: [PATCH 09/11] TD-10431 dnode test01 --- source/dnode/mgmt/impl/CMakeLists.txt | 4 + source/dnode/mgmt/impl/test/CMakeLists.txt | 1 + .../mgmt/impl/test/test01/CMakeLists.txt | 25 +++ source/dnode/mgmt/impl/test/test01/deploy.cpp | 0 .../dnode/mgmt/impl/test/test01/dndTest01.cpp | 156 ++++++++++++++++++ source/dnode/mnode/impl/src/mndAcct.c | 99 +++++++---- source/dnode/mnode/impl/src/mndUser.c | 132 +++++++++------ 7 files changed, 330 insertions(+), 87 deletions(-) create mode 100644 source/dnode/mgmt/impl/test/CMakeLists.txt create mode 100644 source/dnode/mgmt/impl/test/test01/CMakeLists.txt create mode 100644 source/dnode/mgmt/impl/test/test01/deploy.cpp create mode 100644 source/dnode/mgmt/impl/test/test01/dndTest01.cpp diff --git a/source/dnode/mgmt/impl/CMakeLists.txt b/source/dnode/mgmt/impl/CMakeLists.txt index 3eadf24164..51131ede6a 100644 --- a/source/dnode/mgmt/impl/CMakeLists.txt +++ b/source/dnode/mgmt/impl/CMakeLists.txt @@ -14,3 +14,7 @@ target_include_directories( PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mgmt" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) + +if(${BUILD_TEST}) + add_subdirectory(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/dnode/mgmt/impl/test/CMakeLists.txt b/source/dnode/mgmt/impl/test/CMakeLists.txt new file mode 100644 index 0000000000..95ffa548b2 --- /dev/null +++ b/source/dnode/mgmt/impl/test/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(test01) \ No newline at end of file diff --git a/source/dnode/mgmt/impl/test/test01/CMakeLists.txt b/source/dnode/mgmt/impl/test/test01/CMakeLists.txt new file mode 100644 index 0000000000..313f21a2f6 --- /dev/null +++ b/source/dnode/mgmt/impl/test/test01/CMakeLists.txt @@ -0,0 +1,25 @@ +add_executable(dndTest01 "") + +target_sources(dndTest01 + PRIVATE + "dndTest01.cpp" +) + +target_link_libraries( + dndTest01 + PUBLIC dnode + PUBLIC gtest_main +) + +target_include_directories(dndTest01 + PUBLIC + "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" + "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" +) + +enable_testing() + +add_test( + NAME dndTest01 + COMMAND dndTest01 +) diff --git a/source/dnode/mgmt/impl/test/test01/deploy.cpp b/source/dnode/mgmt/impl/test/test01/deploy.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/source/dnode/mgmt/impl/test/test01/dndTest01.cpp b/source/dnode/mgmt/impl/test/test01/dndTest01.cpp new file mode 100644 index 0000000000..8cff0d32a1 --- /dev/null +++ b/source/dnode/mgmt/impl/test/test01/dndTest01.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include "tthread.h" + +#include "dnode.h" +#include "trpc.h" + +typedef struct { + SDnode* pDnode; + pthread_t* threadId; +} SServer; + +void* runServer(void* param) { + SServer* pServer = param; + while (1) { + taosMsleep(100); + pthread_testcancel(); + } +} + +void initOption(SDnodeOpt* pOption) { + pOption->sver = 1; + pOption->numOfCores = 1; + pOption->numOfSupportMnodes = 1; + pOption->numOfSupportVnodes = 1; + pOption->numOfSupportQnodes = 1; + pOption->statusInterval = 1; + pOption->mnodeEqualVnodeNum = 1; + pOption->numOfThreadsPerCore = 1; + pOption->ratioOfQueryCores = 1; + pOption->maxShellConns = 1000; + pOption->shellActivityTimer = 30; + pOption->serverPort = 9527; + strncpy(pOption->dataDir, "./test01"); + strncpy(pOption->localEp, "localhost:9527"); + strncpy(pOption->localFqdn, "localhost"); + tstrncpy(pOption->firstEp, "localhost:9527"); +} + +Server* createServer() { + SDnodeOpt option = {0}; + initOption(&option); + + SDnode* pDnode = dndInit(&option); + ASSERT(pDnode); + + Server* pServer = calloc(1, sizeof(SServer)); + ASSERT(pServer); + + pServer->pDnode = pDnode; + pServer->threadId = taosCreateThread(runServer, pServer); + ASSERT(pServer->threadId); + + return pServer; +} + +void dropServer(SServer* pServer) { + if (pServer->threadId != NULL) { + taosDestoryThread(pServer->threadId); + } +} + +typedef struct { + void* clientRpc; + SRpcMsg* pRsp; + tsem_t sem; +} SClient; + +static void processClientRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) { + SClient* pClient = parent; + pClient->pRsp = pMsg; + tsem_post(pMgmt->clientRpc); +} + +SClient* createClient() { + SClient* pClient = calloc(1, sizeof(SClient)); + ASSERT(pClient); + + char secretEncrypt[32] = {0}; + char* pass = "taosdata"; + taosEncryptPass((uint8_t*)pass, strlen(pass), secretEncrypt); + + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.label = "DND-C"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = processClientRsp; + rpcInit.sessions = 1024; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.idleTime = 30 * 1000; + rpcInit.user = "root"; + rpcInit.ckey = "key"; + rpcInit.parent = pDnode; + rpcInit.secret = (char*)secretEncrypt; + rpcInit.parent = pClient; + // rpcInit.spi = 1; + + pClient->clientRpc = rpcOpen(&rpcInit); + ASSERT(pClient->clientRpc); + + tsem_init(&pClient->sem, 0, 0); +} + +void dropClient(SClient* pClient) { + tsem_destroy(&pClient->sem); + rpcClose(pClient->clientRpc); +} + +void sendMsg(SClient* pClient, SRpcMsg* pMsg) { + SEpSet epSet = {0}; + epSet.inUse = 0; + epSet.numOfEps = 1; + epSet.port[0] = 9527; + strcpy(epSet.fqdn[0], "localhost"); + + rpcSendRequest(pMgmt->clientRpc, &epSet, pMsg, NULL); + tsem_wait(pMgmt->clientRpc); +} + +class DndTest01 : public ::testing::Test { + protected: + void SetUp() override { + pServer = createServer(); + pClient = createClient(); + } + void TearDown() override { + dropServer(pServer); + dropClient(pClient); + } + + SServer* pServer; + SClient* pClient; +}; + +TEST_F(DndTest01, connectMsg) { + SConnectMsg *pReq = rpcMallocCont() + + +} diff --git a/source/dnode/mnode/impl/src/mndAcct.c b/source/dnode/mnode/impl/src/mndAcct.c index 23328506e6..e3d37cd9f9 100644 --- a/source/dnode/mnode/impl/src/mndAcct.c +++ b/source/dnode/mnode/impl/src/mndAcct.c @@ -14,10 +14,61 @@ */ #define _DEFAULT_SOURCE -#include "mndInt.h" +#include "mndAcct.h" +#include "mndShow.h" #define SDB_ACCT_VER 1 +static int32_t mnodeCreateDefaultAcct(SMnode *pMnode); +static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct); +static SSdbRow *mnodeAcctActionDecode(SSdbRaw *pRaw); +static int32_t mnodeAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct); +static int32_t mnodeAcctActionDelete(SSdb *pSdb, SAcctObj *pAcct); +static int32_t mnodeAcctActionUpdate(SSdb *pSdb, SAcctObj *pSrcAcct, SAcctObj *pDstAcct); +static int32_t mndProcessCreateAcctMsg(SMnodeMsg *pMnodeMsg); +static int32_t mndProcessAlterAcctMsg(SMnodeMsg *pMnodeMsg); +static int32_t mndProcessDropAcctMsg(SMnodeMsg *pMnodeMsg); + +int32_t mndInitAcct(SMnode *pMnode) { + SSdbTable table = {.sdbType = SDB_ACCT, + .keyType = SDB_KEY_BINARY, + .deployFp = mnodeCreateDefaultAcct, + .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, + .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, + .insertFp = (SdbInsertFp)mnodeAcctActionInsert, + .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; + + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_ACCT, mndProcessCreateAcctMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_ACCT, mndProcessAlterAcctMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_ACCT, mndProcessDropAcctMsg); + + return sdbSetTable(pMnode->pSdb, table); +} + +void mndCleanupAcct(SMnode *pMnode) {} + +static int32_t mnodeCreateDefaultAcct(SMnode *pMnode) { + SAcctObj acctObj = {0}; + tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN); + acctObj.createdTime = taosGetTimestampMs(); + acctObj.updateTime = acctObj.createdTime; + acctObj.acctId = 1; + acctObj.cfg = (SAcctCfg){.maxUsers = 1024, + .maxDbs = 1024, + .maxTimeSeries = INT32_MAX, + .maxStreams = 8092, + .maxStorage = INT64_MAX, + .accessState = TSDB_VN_ALL_ACCCESS}; + + SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj); + if (pRaw == NULL) return -1; + sdbSetRawStatus(pRaw, SDB_STATUS_READY); + + mTrace("acct:%s, will be created while deploy sdb", acctObj.acct); + return sdbWrite(pMnode->pSdb, pRaw); +} + static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) { SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, SDB_ACCT_VER, sizeof(SAcctObj)); if (pRaw == NULL) return NULL; @@ -92,40 +143,20 @@ static int32_t mnodeAcctActionUpdate(SSdb *pSdb, SAcctObj *pSrcAcct, SAcctObj *p return 0; } -static int32_t mnodeCreateDefaultAcct(SMnode *pMnode) { - int32_t code = 0; - - SAcctObj acctObj = {0}; - tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN); - acctObj.createdTime = taosGetTimestampMs(); - acctObj.updateTime = acctObj.createdTime; - acctObj.acctId = 1; - acctObj.cfg = (SAcctCfg){.maxUsers = 1024, - .maxDbs = 1024, - .maxTimeSeries = INT32_MAX, - .maxStreams = 8092, - .maxStorage = INT64_MAX, - .accessState = TSDB_VN_ALL_ACCCESS}; - - SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj); - if (pRaw == NULL) return -1; - sdbSetRawStatus(pRaw, SDB_STATUS_READY); - - mTrace("acct:%s, will be created while deploy sdb", acctObj.acct); - return sdbWrite(pMnode->pSdb, pRaw); +static int32_t mndProcessCreateAcctMsg(SMnodeMsg *pMnodeMsg) { + terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED; + mError("failed to process create acct msg since %s", terrstr()); + return -1; } -int32_t mndInitAcct(SMnode *pMnode) { - SSdbTable table = {.sdbType = SDB_ACCT, - .keyType = SDB_KEY_BINARY, - .deployFp = mnodeCreateDefaultAcct, - .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, - .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, - .insertFp = (SdbInsertFp)mnodeAcctActionInsert, - .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, - .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; - - return sdbSetTable(pMnode->pSdb, table); +static int32_t mndProcessAlterAcctMsg(SMnodeMsg *pMnodeMsg) { + terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED; + mError("failed to process create acct msg since %s", terrstr()); + return -1; } -void mndCleanupAcct(SMnode *pMnode) {} +static int32_t mndProcessDropAcctMsg(SMnodeMsg *pMnodeMsg) { + terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED; + mError("failed to process create acct msg since %s", terrstr()); + return -1; +} \ No newline at end of file diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index d3a8cf56be..020de13e36 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -14,12 +14,76 @@ */ #define _DEFAULT_SOURCE +#include "mndUser.h" +#include "mndShow.h" #include "mndSync.h" #include "mndTrans.h" #include "tkey.h" #define SDB_USER_VER 1 +static int32_t mndCreateDefaultUsers(SMnode *pMnode); +static SSdbRaw *mndUserActionEncode(SUserObj *pUser); +static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw); +static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser); +static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser); +static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDstUser); +static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg); +static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg); +static int32_t mndProcessAlterUserMsg(SMnodeMsg *pMsg); +static int32_t mndProcessDropUserMsg(SMnodeMsg *pMsg); + +int32_t mndInitUser(SMnode *pMnode) { + SSdbTable table = {.sdbType = SDB_USER, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mndCreateDefaultUsers, + .encodeFp = (SdbEncodeFp)mndUserActionEncode, + .decodeFp = (SdbDecodeFp)mndUserActionDecode, + .insertFp = (SdbInsertFp)mndUserActionInsert, + .updateFp = (SdbUpdateFp)mndUserActionUpdate, + .deleteFp = (SdbDeleteFp)mndUserActionDelete}; + + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_USER, mndProcessCreateUserMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_USER, mndProcessAlterUserMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_USER, mndProcessDropUserMsg); + + return sdbSetTable(pMnode->pSdb, table); +} + +void mndCleanupUser(SMnode *pMnode) {} + +static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) { + SUserObj userObj = {0}; + tstrncpy(userObj.user, user, TSDB_USER_LEN); + tstrncpy(userObj.acct, acct, TSDB_USER_LEN); + taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass); + userObj.createdTime = taosGetTimestampMs(); + userObj.updateTime = userObj.createdTime; + + if (strcmp(user, TSDB_DEFAULT_USER) == 0) { + userObj.superAuth = 1; + } + + SSdbRaw *pRaw = mndUserActionEncode(&userObj); + if (pRaw == NULL) return -1; + sdbSetRawStatus(pRaw, SDB_STATUS_READY); + + mTrace("user:%s, will be created while deploy sdb", userObj.user); + return sdbWrite(pMnode->pSdb, pRaw); +} + +static int32_t mndCreateDefaultUsers(SMnode *pMnode) { + if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) { + return -1; + } + + if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) { + return -1; + } + + return 0; +} + static SSdbRaw *mndUserActionEncode(SUserObj *pUser) { SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, SDB_USER_VER, sizeof(SUserObj)); if (pRaw == NULL) return NULL; @@ -103,36 +167,14 @@ static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDs return 0; } -static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) { - SUserObj userObj = {0}; - tstrncpy(userObj.user, user, TSDB_USER_LEN); - tstrncpy(userObj.acct, acct, TSDB_USER_LEN); - taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass); - userObj.createdTime = taosGetTimestampMs(); - userObj.updateTime = userObj.createdTime; - - if (strcmp(user, TSDB_DEFAULT_USER) == 0) { - userObj.superAuth = 1; - } - - SSdbRaw *pRaw = mndUserActionEncode(&userObj); - if (pRaw == NULL) return -1; - sdbSetRawStatus(pRaw, SDB_STATUS_READY); - - mTrace("user:%s, will be created while deploy sdb", userObj.user); - return sdbWrite(pMnode->pSdb, pRaw); +SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) { + SSdb *pSdb = pMnode->pSdb; + return sdbAcquire(pSdb, SDB_USER, &userName); } -static int32_t mndCreateDefaultUsers(SMnode *pMnode) { - if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) { - return -1; - } - - if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) { - return -1; - } - - return 0; +void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) { + SSdb *pSdb = pMnode->pSdb; + sdbRelease(pSdb, pUser); } static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg) { @@ -222,30 +264,14 @@ static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg) { return TSDB_CODE_MND_ACTION_IN_PROGRESS; } -int32_t mndInitUser(SMnode *pMnode) { - SSdbTable table = {.sdbType = SDB_USER, - .keyType = SDB_KEY_BINARY, - .deployFp = (SdbDeployFp)mndCreateDefaultUsers, - .encodeFp = (SdbEncodeFp)mndUserActionEncode, - .decodeFp = (SdbDecodeFp)mndUserActionDecode, - .insertFp = (SdbInsertFp)mndUserActionInsert, - .updateFp = (SdbUpdateFp)mndUserActionUpdate, - .deleteFp = (SdbDeleteFp)mndUserActionDelete}; - - mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_USER, mndProcessCreateUserMsg); - - return sdbSetTable(pMnode->pSdb, table); -} - -void mndCleanupUser(SMnode *pMnode) {} - -SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) { - SSdb *pSdb = pMnode->pSdb; - return sdbAcquire(pSdb, SDB_USER, &userName); -} - -void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) { - SSdb *pSdb = pMnode->pSdb; - sdbRelease(pSdb, pUser); +static int32_t mndProcessAlterUserMsg(SMnodeMsg *pMsg) { + terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED; + mError("failed to process alter user msg since %s", terrstr()); + return -1; } +static int32_t mndProcessDropUserMsg(SMnodeMsg *pMsg) { + terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED; + mError("failed to process drop user msg since %s", terrstr()); + return -1; +} \ No newline at end of file From aff44f1a47d7a3c166306ad488815823f8cd8aa5 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 3 Dec 2021 21:55:22 +0800 Subject: [PATCH 10/11] TD-10431 dnode test01 --- .../mgmt/impl/test/test01/CMakeLists.txt | 5 +- .../test/test01/{dndTest01.cpp => test01.cpp} | 56 +++++++++++++------ .../impl/test/{test01 => util}/deploy.cpp | 0 3 files changed, 43 insertions(+), 18 deletions(-) rename source/dnode/mgmt/impl/test/test01/{dndTest01.cpp => test01.cpp} (71%) rename source/dnode/mgmt/impl/test/{test01 => util}/deploy.cpp (100%) diff --git a/source/dnode/mgmt/impl/test/test01/CMakeLists.txt b/source/dnode/mgmt/impl/test/test01/CMakeLists.txt index 313f21a2f6..0652ee8381 100644 --- a/source/dnode/mgmt/impl/test/test01/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/test01/CMakeLists.txt @@ -2,12 +2,15 @@ add_executable(dndTest01 "") target_sources(dndTest01 PRIVATE - "dndTest01.cpp" + "test01.cpp" + "../util/deploy.cpp" ) target_link_libraries( dndTest01 PUBLIC dnode + PUBLIC util + PUBLIC os PUBLIC gtest_main ) diff --git a/source/dnode/mgmt/impl/test/test01/dndTest01.cpp b/source/dnode/mgmt/impl/test/test01/test01.cpp similarity index 71% rename from source/dnode/mgmt/impl/test/test01/dndTest01.cpp rename to source/dnode/mgmt/impl/test/test01/test01.cpp index 8cff0d32a1..e1f3729633 100644 --- a/source/dnode/mgmt/impl/test/test01/dndTest01.cpp +++ b/source/dnode/mgmt/impl/test/test01/test01.cpp @@ -14,13 +14,16 @@ */ #include -#include -#include -#include -#include "tthread.h" +#include "os.h" #include "dnode.h" +#include "taosmsg.h" +#include "tconfig.h" +#include "tglobal.h" +#include "tnote.h" #include "trpc.h" +#include "tthread.h" +#include "ulog.h" typedef struct { SDnode* pDnode; @@ -28,7 +31,7 @@ typedef struct { } SServer; void* runServer(void* param) { - SServer* pServer = param; + SServer* pServer = (SServer*)param; while (1) { taosMsleep(100); pthread_testcancel(); @@ -49,19 +52,19 @@ void initOption(SDnodeOpt* pOption) { pOption->shellActivityTimer = 30; pOption->serverPort = 9527; strncpy(pOption->dataDir, "./test01"); - strncpy(pOption->localEp, "localhost:9527"); - strncpy(pOption->localFqdn, "localhost"); - tstrncpy(pOption->firstEp, "localhost:9527"); + strcpy(pOption->localEp, "localhost:9527"); + strcpy(pOption->localFqdn, "localhost"); + strcpy(pOption->firstEp, "localhost:9527"); } -Server* createServer() { +SServer* createServer() { SDnodeOpt option = {0}; initOption(&option); SDnode* pDnode = dndInit(&option); ASSERT(pDnode); - Server* pServer = calloc(1, sizeof(SServer)); + SServer* pServer = (SServer*)calloc(1, sizeof(SServer)); ASSERT(pServer); pServer->pDnode = pDnode; @@ -84,13 +87,13 @@ typedef struct { } SClient; static void processClientRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) { - SClient* pClient = parent; + SClient* pClient = (SClient*)parent; pClient->pRsp = pMsg; - tsem_post(pMgmt->clientRpc); + tsem_post(&pClient->sem); } SClient* createClient() { - SClient* pClient = calloc(1, sizeof(SClient)); + SClient* pClient = (SClient*)calloc(1, sizeof(SClient)); ASSERT(pClient); char secretEncrypt[32] = {0}; @@ -107,7 +110,7 @@ SClient* createClient() { rpcInit.idleTime = 30 * 1000; rpcInit.user = "root"; rpcInit.ckey = "key"; - rpcInit.parent = pDnode; + rpcInit.parent = pClient; rpcInit.secret = (char*)secretEncrypt; rpcInit.parent = pClient; // rpcInit.spi = 1; @@ -130,8 +133,8 @@ void sendMsg(SClient* pClient, SRpcMsg* pMsg) { epSet.port[0] = 9527; strcpy(epSet.fqdn[0], "localhost"); - rpcSendRequest(pMgmt->clientRpc, &epSet, pMsg, NULL); - tsem_wait(pMgmt->clientRpc); + rpcSendRequest(pClient->clientRpc, &epSet, pMsg, NULL); + tsem_wait(&pClient->sem); } class DndTest01 : public ::testing::Test { @@ -150,7 +153,26 @@ class DndTest01 : public ::testing::Test { }; TEST_F(DndTest01, connectMsg) { - SConnectMsg *pReq = rpcMallocCont() + SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(sizeof(SConnectMsg)); + pReq->pid = 1234; + strcpy(pReq->app, "test01"); + strcpy(pReq->app, ""); + SRpcMsg rpcMsg = {.pCont = pReq, .contLen = sizeof(SConnectMsg), .msgType = TSDB_MSG_TYPE_AUTH}; + sendMsg(pClient, &rpcMsg); + + SConnectRsp* pRsp = (SConnectRsp*)pClient->pRsp; + EXPECT_NE(pRsp, NULL); + EXPECT_EQ(pRsp->acctId, 1); + EXPECT_GT(pRsp->clusterId, 0); + EXPECT_GT(pRsp->connId, 1); + EXPECT_EQ(pRsp->superAuth, 1); + EXPECT_EQ(pRsp->readAuth, 1); + EXPECT_EQ(pRsp->writeAuth, 1); + + EXPECT_EQ(pRsp->epSet.inUse, 0); + EXPECT_EQ(pRsp->epSet.numOfEps, 1); + EXPECT_EQ(pRsp->epSet.port[0], 9527); + EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost"); } diff --git a/source/dnode/mgmt/impl/test/test01/deploy.cpp b/source/dnode/mgmt/impl/test/util/deploy.cpp similarity index 100% rename from source/dnode/mgmt/impl/test/test01/deploy.cpp rename to source/dnode/mgmt/impl/test/util/deploy.cpp From 7d3a09c5013025c45335fef1509479665942fb3b Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 4 Dec 2021 10:23:39 +0800 Subject: [PATCH 11/11] TD-10431 dnode test01 --- include/dnode/mnode/mnode.h | 1 + source/dnode/mgmt/impl/src/dndMnode.c | 3 +- .../mgmt/impl/test/test01/CMakeLists.txt | 5 +- .../dnode/mgmt/impl/test/test01/dndTest01.cpp | 95 +++++++++++++++++++ source/dnode/mgmt/impl/test/util/deploy.cpp | 0 .../test01.cpp => util/dndTestDeploy.cpp} | 93 +++++------------- .../dnode/mgmt/impl/test/util/dndTestDeploy.h | 43 +++++++++ source/dnode/mnode/impl/inc/mndUser.h | 2 +- source/dnode/mnode/impl/src/mndProfile.c | 5 +- source/dnode/mnode/impl/src/mndUser.c | 4 +- source/dnode/mnode/impl/src/mnode.c | 1 + source/libs/transport/src/rpcMain.c | 4 - 12 files changed, 173 insertions(+), 83 deletions(-) create mode 100644 source/dnode/mgmt/impl/test/test01/dndTest01.cpp delete mode 100644 source/dnode/mgmt/impl/test/util/deploy.cpp rename source/dnode/mgmt/impl/test/{test01/test01.cpp => util/dndTestDeploy.cpp} (62%) create mode 100644 source/dnode/mgmt/impl/test/util/dndTestDeploy.h diff --git a/include/dnode/mnode/mnode.h b/include/dnode/mnode/mnode.h index c7415af0d5..0a897b93f8 100644 --- a/include/dnode/mnode/mnode.h +++ b/include/dnode/mnode/mnode.h @@ -57,6 +57,7 @@ typedef struct { int32_t sver; int32_t statusInterval; int32_t mnodeEqualVnodeNum; + int32_t shellActivityTimer; char *timezone; char *locale; char *charset; diff --git a/source/dnode/mgmt/impl/src/dndMnode.c b/source/dnode/mgmt/impl/src/dndMnode.c index 4ec08d5fb3..4afce4655d 100644 --- a/source/dnode/mgmt/impl/src/dndMnode.c +++ b/source/dnode/mgmt/impl/src/dndMnode.c @@ -334,6 +334,7 @@ static void dndInitMnodeOption(SDnode *pDnode, SMnodeOpt *pOption) { pOption->sver = pDnode->opt.sver; pOption->statusInterval = pDnode->opt.statusInterval; pOption->mnodeEqualVnodeNum = pDnode->opt.mnodeEqualVnodeNum; + pOption->shellActivityTimer = pDnode->opt.shellActivityTimer; pOption->timezone = pDnode->opt.timezone; pOption->charset = pDnode->opt.charset; pOption->locale = pDnode->opt.locale; @@ -675,7 +676,7 @@ void dndProcessMnodeSyncMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { void dndProcessMnodeReadMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet) { SMnodeMgmt *pMgmt = &pDnode->mmgmt; SMnode *pMnode = dndAcquireMnode(pDnode); - if (pMnode == NULL || dndWriteMnodeMsgToQueue(pMnode, pMgmt->pSyncQ, pMsg) != 0) { + if (pMnode == NULL || dndWriteMnodeMsgToQueue(pMnode, pMgmt->pReadQ, pMsg) != 0) { SRpcMsg rsp = {.handle = pMsg->handle, .code = terrno}; rpcSendResponse(&rsp); rpcFreeCont(pMsg->pCont); diff --git a/source/dnode/mgmt/impl/test/test01/CMakeLists.txt b/source/dnode/mgmt/impl/test/test01/CMakeLists.txt index 0652ee8381..c3037dfdff 100644 --- a/source/dnode/mgmt/impl/test/test01/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/test01/CMakeLists.txt @@ -2,8 +2,8 @@ add_executable(dndTest01 "") target_sources(dndTest01 PRIVATE - "test01.cpp" - "../util/deploy.cpp" + "dndTest01.cpp" + "../util/dndTestDeploy.cpp" ) target_link_libraries( @@ -18,6 +18,7 @@ target_include_directories(dndTest01 PUBLIC "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" + "${CMAKE_CURRENT_SOURCE_DIR}/../util" ) enable_testing() diff --git a/source/dnode/mgmt/impl/test/test01/dndTest01.cpp b/source/dnode/mgmt/impl/test/test01/dndTest01.cpp new file mode 100644 index 0000000000..70f649cc07 --- /dev/null +++ b/source/dnode/mgmt/impl/test/test01/dndTest01.cpp @@ -0,0 +1,95 @@ +/* + * 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 "dndTestDeploy.h" + +class DndTest01 : public ::testing::Test { + protected: + void SetUp() override { + pServer = createServer("/tmp/dndTest01"); + pClient = createClient("root", "taosdata"); + } + void TearDown() override { + dropServer(pServer); + dropClient(pClient); + } + + SServer* pServer; + SClient* pClient; +}; + +TEST_F(DndTest01, connectMsg) { + SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(sizeof(SConnectMsg)); + pReq->pid = htonl(1234); + strcpy(pReq->app, "test01"); + strcpy(pReq->db, ""); + + SRpcMsg rpcMsg = {0}; + rpcMsg.pCont = pReq; + rpcMsg.contLen = sizeof(SConnectMsg); + rpcMsg.msgType = TSDB_MSG_TYPE_CONNECT; + + sendMsg(pClient, &rpcMsg); + + SConnectRsp* pRsp = (SConnectRsp*)pClient->pRsp; + ASSERT(pRsp); + pRsp->acctId = htonl(pRsp->acctId); + pRsp->clusterId = htonl(pRsp->clusterId); + pRsp->connId = htonl(pRsp->connId); + pRsp->epSet.port[0] = htonl(pRsp->epSet.port[0]); + + EXPECT_EQ(pRsp->acctId, 1); + EXPECT_GT(pRsp->clusterId, 0); + EXPECT_GT(pRsp->connId, 1); + EXPECT_EQ(pRsp->superAuth, 1); + EXPECT_EQ(pRsp->readAuth, 1); + EXPECT_EQ(pRsp->writeAuth, 1); + + EXPECT_EQ(pRsp->epSet.inUse, 0); + EXPECT_EQ(pRsp->epSet.numOfEps, 1); + EXPECT_EQ(pRsp->epSet.port[0], 9527); + EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost"); +} + +// TEST_F(DndTest01, heartbeatMsg) { +// SHeartBeatMsg* pReq = (SHeartBeatMsg*)rpcMallocCont(sizeof(SHeartBeatMsg)); +// pReq->connId = htonl(1); +// pReq->pid = htonl(1234); +// pReq->numOfQueries = htonl(0); +// pReq->numOfStreams = htonl(0); +// strcpy(pReq->app, "test01"); + +// SRpcMsg rpcMsg = {0}; +// rpcMsg.pCont = pReq; +// rpcMsg.contLen = sizeof(SHeartBeatMsg); +// rpcMsg.msgType = TSDB_MSG_TYPE_HEARTBEAT; + +// sendMsg(pClient, &rpcMsg); + +// SHeartBeatRsp* pRsp = (SHeartBeatRsp*)pClient->pRsp; +// ASSERT(pRsp); + // pRsp->epSet.port[0] = htonl(pRsp->epSet.port[0]); + +// EXPECT_EQ(htonl(pRsp->connId), 1); +// EXPECT_GT(htonl(pRsp->queryId), 0); +// EXPECT_GT(htonl(pRsp->streamId), 1); +// EXPECT_EQ(htonl(pRsp->totalDnodes), 1); +// EXPECT_EQ(htonl(pRsp->onlineDnodes), 1); +// EXPECT_EQ(pRsp->killConnection, 0); + // EXPECT_EQ(pRsp->epSet.inUse, 0); + // EXPECT_EQ(pRsp->epSet.numOfEps, 1); + // EXPECT_EQ(pRsp->epSet.port[0], 9527); + // EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost"); +// } diff --git a/source/dnode/mgmt/impl/test/util/deploy.cpp b/source/dnode/mgmt/impl/test/util/deploy.cpp deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/source/dnode/mgmt/impl/test/test01/test01.cpp b/source/dnode/mgmt/impl/test/util/dndTestDeploy.cpp similarity index 62% rename from source/dnode/mgmt/impl/test/test01/test01.cpp rename to source/dnode/mgmt/impl/test/util/dndTestDeploy.cpp index e1f3729633..4b4f0fa763 100644 --- a/source/dnode/mgmt/impl/test/test01/test01.cpp +++ b/source/dnode/mgmt/impl/test/util/dndTestDeploy.cpp @@ -13,22 +13,16 @@ * along with this program. If not, see . */ -#include -#include "os.h" +#include "dndTestDeploy.h" -#include "dnode.h" -#include "taosmsg.h" -#include "tconfig.h" -#include "tglobal.h" -#include "tnote.h" -#include "trpc.h" -#include "tthread.h" -#include "ulog.h" - -typedef struct { - SDnode* pDnode; - pthread_t* threadId; -} SServer; +void initLog(char *path) { + mDebugFlag = 207; + char temp[PATH_MAX]; + snprintf(temp, PATH_MAX, "%s/taosdlog", path); + if (taosInitLog(temp, tsNumOfLogLines, 1) != 0) { + printf("failed to init log file\n"); + } +} void* runServer(void* param) { SServer* pServer = (SServer*)param; @@ -38,7 +32,7 @@ void* runServer(void* param) { } } -void initOption(SDnodeOpt* pOption) { +void initOption(SDnodeOpt* pOption, char *path) { pOption->sver = 1; pOption->numOfCores = 1; pOption->numOfSupportMnodes = 1; @@ -51,15 +45,18 @@ void initOption(SDnodeOpt* pOption) { pOption->maxShellConns = 1000; pOption->shellActivityTimer = 30; pOption->serverPort = 9527; - strncpy(pOption->dataDir, "./test01"); + strcpy(pOption->dataDir, path); strcpy(pOption->localEp, "localhost:9527"); strcpy(pOption->localFqdn, "localhost"); strcpy(pOption->firstEp, "localhost:9527"); + + taosRemoveDir(path); + taosMkDir(path); } -SServer* createServer() { +SServer* createServer(char *path) { SDnodeOpt option = {0}; - initOption(&option); + initOption(&option, path); SDnode* pDnode = dndInit(&option); ASSERT(pDnode); @@ -80,24 +77,18 @@ void dropServer(SServer* pServer) { } } -typedef struct { - void* clientRpc; - SRpcMsg* pRsp; - tsem_t sem; -} SClient; - -static void processClientRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) { +void processClientRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) { SClient* pClient = (SClient*)parent; pClient->pRsp = pMsg; + taosMsleep(100000); tsem_post(&pClient->sem); } -SClient* createClient() { +SClient* createClient(char *user, char *pass) { SClient* pClient = (SClient*)calloc(1, sizeof(SClient)); ASSERT(pClient); - char secretEncrypt[32] = {0}; - char* pass = "taosdata"; + char secretEncrypt[32] = {0}; taosEncryptPass((uint8_t*)pass, strlen(pass), secretEncrypt); SRpcInit rpcInit; @@ -108,7 +99,7 @@ SClient* createClient() { rpcInit.sessions = 1024; rpcInit.connType = TAOS_CONN_CLIENT; rpcInit.idleTime = 30 * 1000; - rpcInit.user = "root"; + rpcInit.user = user; rpcInit.ckey = "key"; rpcInit.parent = pClient; rpcInit.secret = (char*)secretEncrypt; @@ -119,6 +110,8 @@ SClient* createClient() { ASSERT(pClient->clientRpc); tsem_init(&pClient->sem, 0, 0); + + return pClient; } void dropClient(SClient* pClient) { @@ -136,43 +129,3 @@ void sendMsg(SClient* pClient, SRpcMsg* pMsg) { rpcSendRequest(pClient->clientRpc, &epSet, pMsg, NULL); tsem_wait(&pClient->sem); } - -class DndTest01 : public ::testing::Test { - protected: - void SetUp() override { - pServer = createServer(); - pClient = createClient(); - } - void TearDown() override { - dropServer(pServer); - dropClient(pClient); - } - - SServer* pServer; - SClient* pClient; -}; - -TEST_F(DndTest01, connectMsg) { - SConnectMsg* pReq = (SConnectMsg*)rpcMallocCont(sizeof(SConnectMsg)); - pReq->pid = 1234; - strcpy(pReq->app, "test01"); - strcpy(pReq->app, ""); - - SRpcMsg rpcMsg = {.pCont = pReq, .contLen = sizeof(SConnectMsg), .msgType = TSDB_MSG_TYPE_AUTH}; - - sendMsg(pClient, &rpcMsg); - - SConnectRsp* pRsp = (SConnectRsp*)pClient->pRsp; - EXPECT_NE(pRsp, NULL); - EXPECT_EQ(pRsp->acctId, 1); - EXPECT_GT(pRsp->clusterId, 0); - EXPECT_GT(pRsp->connId, 1); - EXPECT_EQ(pRsp->superAuth, 1); - EXPECT_EQ(pRsp->readAuth, 1); - EXPECT_EQ(pRsp->writeAuth, 1); - - EXPECT_EQ(pRsp->epSet.inUse, 0); - EXPECT_EQ(pRsp->epSet.numOfEps, 1); - EXPECT_EQ(pRsp->epSet.port[0], 9527); - EXPECT_STREQ(pRsp->epSet.fqdn[0], "localhost"); -} diff --git a/source/dnode/mgmt/impl/test/util/dndTestDeploy.h b/source/dnode/mgmt/impl/test/util/dndTestDeploy.h new file mode 100644 index 0000000000..6c47b03fe7 --- /dev/null +++ b/source/dnode/mgmt/impl/test/util/dndTestDeploy.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include "os.h" + +#include "dnode.h" +#include "taosmsg.h" +#include "tconfig.h" +#include "tglobal.h" +#include "tnote.h" +#include "trpc.h" +#include "tthread.h" +#include "ulog.h" + +typedef struct { + SDnode* pDnode; + pthread_t* threadId; +} SServer; + +typedef struct { + void* clientRpc; + SRpcMsg* pRsp; + tsem_t sem; +} SClient; + +SServer* createServer(char* path); +void dropServer(SServer* pServer); +SClient* createClient(char *user, char *pass); +void dropClient(SClient* pClient); +void sendMsg(SClient* pClient, SRpcMsg* pMsg); diff --git a/source/dnode/mnode/impl/inc/mndUser.h b/source/dnode/mnode/impl/inc/mndUser.h index 4d31a87b19..e4ecdf3283 100644 --- a/source/dnode/mnode/impl/inc/mndUser.h +++ b/source/dnode/mnode/impl/inc/mndUser.h @@ -24,7 +24,7 @@ extern "C" { int32_t mndInitUser(SMnode *pMnode); void mndCleanupUser(SMnode *pMnode); -SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName); +SUserObj *mndAcquireUser(SMnode *pMnode, char *userName); void mndReleaseUser(SMnode *pMnode, SUserObj *pUser); #ifdef __cplusplus diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index eb9926cfb6..2afb5f0665 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -78,7 +78,7 @@ int32_t mndInitProfile(SMnode *pMnode) { mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_HEARTBEAT, mndProcessHeartBeatMsg); mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CONNECT, mndProcessConnectMsg); mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_KILL_QUERY, mndProcessKillQueryMsg); - mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CONNECT, mndProcessKillStreamMsg); + mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_KILL_STREAM, mndProcessKillStreamMsg); mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_KILL_CONN, mndProcessKillConnectionMsg); mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndGetConnsMeta); @@ -237,7 +237,6 @@ static int32_t mndProcessConnectMsg(SMnodeMsg *pMsg) { mndReleaseUser(pMnode, pUser); } - pRsp->acctId = htonl(pUser->acctId); pRsp->clusterId = htonl(pMnode->clusterId); pRsp->connId = htonl(pConn->connId); mndGetMnodeEpSet(pMnode, &pRsp->epSet); @@ -330,7 +329,7 @@ static int32_t mndProcessHeartBeatMsg(SMnodeMsg *pMsg) { } pRsp->connId = htonl(pConn->connId); - pRsp->totalDnodes = htnol(1); + pRsp->totalDnodes = htonl(1); pRsp->onlineDnodes = htonl(1); mndGetMnodeEpSet(pMnode, &pRsp->epSet); mndReleaseConn(pMnode, pConn); diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index 020de13e36..f59ef8f8e3 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -167,9 +167,9 @@ static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDs return 0; } -SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) { +SUserObj *mndAcquireUser(SMnode *pMnode, char *userName) { SSdb *pSdb = pMnode->pSdb; - return sdbAcquire(pSdb, SDB_USER, &userName); + return sdbAcquire(pSdb, SDB_USER, userName); } void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) { diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index 3a5b6c470b..b99de4a019 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -206,6 +206,7 @@ static int32_t mndSetOptions(SMnode *pMnode, const SMnodeOpt *pOption) { pMnode->sver = pOption->sver; pMnode->statusInterval = pOption->statusInterval; pMnode->mnodeEqualVnodeNum = pOption->mnodeEqualVnodeNum; + pMnode->shellActivityTimer = pOption->shellActivityTimer; pMnode->timezone = strdup(pOption->timezone); pMnode->locale = strdup(pOption->locale); pMnode->charset = strdup(pOption->charset); diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index a2041c76fc..e392351366 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -507,7 +507,6 @@ void rpcSendRedirectRsp(void *thandle, const SEpSet *pEpSet) { } int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo) { -#if 0 SRpcConn *pConn = (SRpcConn *)thandle; if (pConn->user[0] == 0) return -1; @@ -516,9 +515,6 @@ int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo) { // pInfo->serverIp = pConn->destIp; tstrncpy(pInfo->user, pConn->user, sizeof(pInfo->user)); -#else - strcpy(pInfo->user, "root"); -#endif return 0; }