From 9748f4be676edbb714f6229f6f059893ecc64bd8 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Dec 2021 03:20:24 -0800 Subject: [PATCH 1/8] minor changes --- include/dnode/vnode/vnode.h | 10 ---------- source/dnode/vnode/impl/src/vnodeInt.c | 5 ----- tests/script/tmp/dnodes.sim | 23 +++++++++++------------ 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/include/dnode/vnode/vnode.h b/include/dnode/vnode/vnode.h index 812f313e71..0313c65acb 100644 --- a/include/dnode/vnode/vnode.h +++ b/include/dnode/vnode/vnode.h @@ -162,16 +162,6 @@ int vnodeProcessQueryReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp); */ int vnodeProcessFetchReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp); -/** - * @brief Process a consume message. - * - * @param pVnode The vnode object. - * @param pMsg The request message - * @param pRsp The response message - * @return int 0 for success, -1 for failure - */ -int vnodeProcessConsumeReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp); - /* ------------------------ SVnodeCfg ------------------------ */ /** * @brief Initialize VNODE options. diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index 65185f4a16..0f33fa65cd 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -28,8 +28,3 @@ int vnodeProcessSyncReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { vInfo("sync message is processed"); return 0; } - -int vnodeProcessConsumeReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { - vInfo("consume message is processed"); - return 0; -} diff --git a/tests/script/tmp/dnodes.sim b/tests/script/tmp/dnodes.sim index f5146620c5..f13f6026f9 100644 --- a/tests/script/tmp/dnodes.sim +++ b/tests/script/tmp/dnodes.sim @@ -2,20 +2,19 @@ system sh/stop_dnodes.sh ############## config parameter ##################### -$node1 = 192.168.101.174 +$node1 = 192.168.0.201 $node2 = 192.168.0.202 -$node2 = 192.168.0.203 -$node3 = 192.168.0.204 +$node3 = 192.168.0.203 +$node4 = 192.168.0.204 -$first = 1 -$num = 5 $self = $node1 +$num = 25 ############### deploy firstEp ##################### $firstEp = $node1 . :7100 $firstPort = 7100 -if $first == 1 then +if $self == $node1 then system sh/deploy.sh -n dnode1 -i 1 system sh/cfg.sh -n dnode1 -c firstEp -v $firstEp system sh/cfg.sh -n dnode1 -c secondEp -v $firstEp @@ -28,7 +27,7 @@ if $first == 1 then $i = 0 while $i < $num $port = $i * 100 - $port = $port + 8000 + $port = $port + 8100 $i = $i + 1 sql create dnode $node1 port $port endw @@ -36,7 +35,7 @@ if $first == 1 then $i = 0 while $i < $num $port = $i * 100 - $port = $port + 8000 + $port = $port + 8100 $i = $i + 1 sql create dnode $node2 port $port endw @@ -44,7 +43,7 @@ if $first == 1 then $i = 0 while $i < $num $port = $i * 100 - $port = $port + 8000 + $port = $port + 8100 $i = $i + 1 sql create dnode $node3 port $port endw @@ -52,7 +51,7 @@ if $first == 1 then $i = 0 while $i < $num $port = $i * 100 - $port = $port + 8000 + $port = $port + 8100 $i = $i + 1 sql create dnode $node4 port $port endw @@ -64,7 +63,7 @@ $i = 0 while $i < $num $index = $i + 80 $port = $i * 100 - $port = $port + 8000 + $port = $port + 8100 $dnodename = dnode . $index $i = $i + 1 @@ -74,5 +73,5 @@ while $i < $num system sh/cfg.sh -n $dnodename -c fqdn -v $self system sh/cfg.sh -n $dnodename -c serverPort -v $port - #system sh/exec.sh -n $dnodename -s start + system sh/exec.sh -n $dnodename -s start endw From c31dc877631eb90800d2ae7ac8e3dda1a7f1205c Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Dec 2021 04:01:04 -0800 Subject: [PATCH 2/8] fix drop dnode errors --- include/common/tmsg.h | 2 -- source/dnode/mgmt/impl/src/dndDnode.c | 23 +++++++++++++---------- source/dnode/mnode/impl/src/mndDnode.c | 1 - 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index c644bfda9e..0b59131644 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -661,8 +661,6 @@ typedef struct { typedef struct { int32_t dnodeId; int64_t clusterId; - int8_t dropped; - char reserved[7]; } SDnodeCfg; typedef struct { diff --git a/source/dnode/mgmt/impl/src/dndDnode.c b/source/dnode/mgmt/impl/src/dndDnode.c index ca0552b8ad..98763fce30 100644 --- a/source/dnode/mgmt/impl/src/dndDnode.c +++ b/source/dnode/mgmt/impl/src/dndDnode.c @@ -393,13 +393,11 @@ void dndSendStatusMsg(SDnode *pDnode) { static void dndUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) { SDnodeMgmt *pMgmt = &pDnode->dmgmt; - if (pMgmt->dnodeId == 0 || pMgmt->dropped != pCfg->dropped) { - dInfo("set dnodeId:%d clusterId:% " PRId64 " dropped:%d", pCfg->dnodeId, pCfg->clusterId, pCfg->dropped); - + if (pMgmt->dnodeId == 0) { + dInfo("set dnodeId:%d clusterId:% " PRId64, pCfg->dnodeId, pCfg->clusterId); taosWLockLatch(&pMgmt->latch); pMgmt->dnodeId = pCfg->dnodeId; pMgmt->clusterId = pCfg->clusterId; - pMgmt->dropped = pCfg->dropped; dndWriteDnodes(pDnode); taosWUnLockLatch(&pMgmt->latch); } @@ -430,6 +428,11 @@ static void dndProcessStatusRsp(SDnode *pDnode, SRpcMsg *pMsg) { if (pMsg->code != TSDB_CODE_SUCCESS) { pMgmt->statusSent = 0; + if (pMsg->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->dropped && pMgmt->dnodeId > 0) { + dInfo("dnode:%d, set to dropped since not exist in mnode", pMgmt->dnodeId); + pMgmt->dropped = 1; + dndWriteDnodes(pDnode); + } return; } @@ -439,11 +442,6 @@ static void dndProcessStatusRsp(SDnode *pDnode, SRpcMsg *pMsg) { pCfg->clusterId = htobe64(pCfg->clusterId); dndUpdateDnodeCfg(pDnode, pCfg); - if (pCfg->dropped) { - pMgmt->statusSent = 0; - return; - } - SDnodeEps *pDnodeEps = &pRsp->dnodeEps; pDnodeEps->num = htonl(pDnodeEps->num); for (int32_t i = 0; i < pDnodeEps->num; ++i) { @@ -487,7 +485,7 @@ static void *dnodeThreadRoutine(void *param) { pthread_testcancel(); taosMsleep(ms); - if (dndGetStat(pDnode) == DND_STAT_RUNNING && !pMgmt->statusSent) { + if (dndGetStat(pDnode) == DND_STAT_RUNNING && !pMgmt->statusSent && !pMgmt->dropped) { dndSendStatusMsg(pDnode); } } @@ -522,6 +520,11 @@ int32_t dndInitDnode(SDnode *pDnode) { return -1; } + if (pMgmt->dropped) { + dError("dnode will not start for its already dropped"); + return -1; + } + if (dndInitMgmtWorker(pDnode) != 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index 2d236906e1..91d2a084af 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -370,7 +370,6 @@ static int32_t mndProcessStatusMsg(SMnodeMsg *pMsg) { } pRsp->dnodeCfg.dnodeId = htonl(pDnode->id); - pRsp->dnodeCfg.dropped = 0; pRsp->dnodeCfg.clusterId = htobe64(pMnode->clusterId); mndGetDnodeData(pMnode, &pRsp->dnodeEps, numOfEps); From cfd231c93f2b9972dd34f92f1749f727ebab7267 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Dec 2021 04:51:05 -0800 Subject: [PATCH 3/8] minor changes --- source/dnode/mnode/impl/src/mnode.c | 10 +++++----- source/dnode/mnode/sdb/src/sdbFile.c | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index 9281e46f4f..84076f8c0a 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -146,15 +146,15 @@ static int32_t mndInitSteps(SMnode *pMnode) { if (mndAllocStep(pMnode, "mnode-sdb", mndInitSdb, mndCleanupSdb) != 0) return -1; if (mndAllocStep(pMnode, "mnode-trans", mndInitTrans, mndCleanupTrans) != 0) return -1; if (mndAllocStep(pMnode, "mnode-cluster", mndInitCluster, mndCleanupCluster) != 0) return -1; - if (mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode) != 0) return -1; if (mndAllocStep(pMnode, "mnode-mnode", mndInitMnode, mndCleanupMnode) != 0) return -1; - if (mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct) != 0) return -1; - if (mndAllocStep(pMnode, "mnode-auth", mndInitAuth, mndCleanupAuth) != 0) return -1; + if (mndAllocStep(pMnode, "mnode-dnode", mndInitDnode, mndCleanupDnode) != 0) return -1; if (mndAllocStep(pMnode, "mnode-user", mndInitUser, mndCleanupUser) != 0) return -1; - if (mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb) != 0) return -1; + if (mndAllocStep(pMnode, "mnode-auth", mndInitAuth, mndCleanupAuth) != 0) return -1; + if (mndAllocStep(pMnode, "mnode-acct", mndInitAcct, mndCleanupAcct) != 0) return -1; + if (mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic) != 0) return -1; if (mndAllocStep(pMnode, "mnode-vgroup", mndInitVgroup, mndCleanupVgroup) != 0) return -1; if (mndAllocStep(pMnode, "mnode-stb", mndInitStb, mndCleanupStb) != 0) return -1; - if (mndAllocStep(pMnode, "mnode-topic", mndInitTopic, mndCleanupTopic) != 0) return -1; + if (mndAllocStep(pMnode, "mnode-db", mndInitDb, mndCleanupDb) != 0) return -1; if (mndAllocStep(pMnode, "mnode-func", mndInitFunc, mndCleanupFunc) != 0) return -1; if (pMnode->clusterId <= 0) { if (mndAllocStep(pMnode, "mnode-sdb-deploy", mndDeploySdb, NULL) != 0) return -1; diff --git a/source/dnode/mnode/sdb/src/sdbFile.c b/source/dnode/mnode/sdb/src/sdbFile.c index 7828e39e56..78cf0a3492 100644 --- a/source/dnode/mnode/sdb/src/sdbFile.c +++ b/source/dnode/mnode/sdb/src/sdbFile.c @@ -151,7 +151,7 @@ int32_t sdbWriteFile(SSdb *pSdb) { if (taosWriteFile(fd, pRaw, writeLen) != writeLen) { code = TAOS_SYSTEM_ERROR(terrno); taosHashCancelIterate(hash, ppRow); - free(pRaw); + sdbFreeRaw(pRaw); break; } @@ -159,7 +159,7 @@ int32_t sdbWriteFile(SSdb *pSdb) { if (taosWriteFile(fd, &cksum, sizeof(int32_t)) != sizeof(int32_t)) { code = TAOS_SYSTEM_ERROR(terrno); taosHashCancelIterate(hash, ppRow); - free(pRaw); + sdbFreeRaw(pRaw); break; } } else { @@ -168,7 +168,7 @@ int32_t sdbWriteFile(SSdb *pSdb) { break; } - free(pRaw); + sdbFreeRaw(pRaw); ppRow = taosHashIterate(hash, ppRow); } taosWUnLockLatch(pLock); From 97f7018f5be307d1b69cfdd6428a00f0b026326d Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Dec 2021 22:35:23 -0800 Subject: [PATCH 4/8] fix invalid write in sdb --- include/dnode/mnode/sdb/sdb.h | 14 ++--- source/dnode/mnode/impl/src/mndProfile.c | 10 +-- source/dnode/mnode/impl/src/mndShow.c | 62 ++++++++----------- source/dnode/mnode/impl/src/mndTrans.c | 14 ++--- source/dnode/mnode/sdb/inc/sdbInt.h | 1 + source/dnode/mnode/sdb/src/sdb.c | 8 +-- source/dnode/mnode/sdb/src/sdbHash.c | 79 ++++++++++++++++++------ source/dnode/mnode/sdb/src/sdbRaw.c | 4 +- source/dnode/mnode/sdb/src/sdbRow.c | 11 +++- tests/script/sh/exec.sh | 4 +- 10 files changed, 122 insertions(+), 85 deletions(-) diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index 0f648b5150..474e526186 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -25,7 +25,7 @@ extern "C" { #define SDB_GET_INT64(pData, pRow, dataPos, val) \ { \ if (sdbGetRawInt64(pRaw, dataPos, val) != 0) { \ - sdbFreeRow(pRow); \ + tfree(pRow); \ return NULL; \ } \ dataPos += sizeof(int64_t); \ @@ -34,7 +34,7 @@ extern "C" { #define SDB_GET_INT32(pData, pRow, dataPos, val) \ { \ if (sdbGetRawInt32(pRaw, dataPos, val) != 0) { \ - sdbFreeRow(pRow); \ + tfree(pRow); \ return NULL; \ } \ dataPos += sizeof(int32_t); \ @@ -43,7 +43,7 @@ extern "C" { #define SDB_GET_INT16(pData, pRow, dataPos, val) \ { \ if (sdbGetRawInt16(pRaw, dataPos, val) != 0) { \ - sdbFreeRow(pRow); \ + tfree(pRow); \ return NULL; \ } \ dataPos += sizeof(int16_t); \ @@ -52,7 +52,7 @@ extern "C" { #define SDB_GET_INT8(pData, pRow, dataPos, val) \ { \ if (sdbGetRawInt8(pRaw, dataPos, val) != 0) { \ - sdbFreeRow(pRow); \ + tfree(pRow); \ return NULL; \ } \ dataPos += sizeof(int8_t); \ @@ -61,7 +61,7 @@ extern "C" { #define SDB_GET_BINARY(pRaw, pRow, dataPos, val, valLen) \ { \ if (sdbGetRawBinary(pRaw, dataPos, val, valLen) != 0) { \ - sdbFreeRow(pRow); \ + tfree(pRow); \ return NULL; \ } \ dataPos += valLen; \ @@ -71,7 +71,7 @@ extern "C" { { \ char val[valLen] = {0}; \ if (sdbGetRawBinary(pRaw, dataPos, val, valLen) != 0) { \ - sdbFreeRow(pRow); \ + tfree(pRow); \ return NULL; \ } \ dataPos += valLen; \ @@ -325,7 +325,7 @@ int32_t sdbGetRawSoftVer(SSdbRaw *pRaw, int8_t *sver); int32_t sdbGetRawTotalSize(SSdbRaw *pRaw); SSdbRow *sdbAllocRow(int32_t objSize); -void sdbFreeRow(SSdbRow *pRow); +void sdbFreeRow(SSdb *pSdb, SSdbRow *pRow); void *sdbGetRowObj(SSdbRow *pRow); #ifdef __cplusplus diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 9a4098857c..77efeb8481 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -118,17 +118,17 @@ static SConnObj *mndCreateConn(SMnode *pMnode, SRpcConnInfo *pInfo, int32_t pid, SConnObj *pConn = taosCachePut(pMgmt->cache, &connId, sizeof(int32_t), &connObj, sizeof(connObj), keepTime * 1000); if (pConn == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; - mError("conn:%d, data:%p failed to put into cache since %s, user:%s", connId, pConn, pInfo->user, terrstr()); + mError("conn:%d, failed to put into cache since %s, user:%s", connId, pInfo->user, terrstr()); return NULL; } else { - mTrace("conn:%d, data:%p created, user:%s", pConn->id, pConn, pInfo->user); + mTrace("conn:%d, is created, data:%p user:%s", pConn->id, pConn, pInfo->user); return pConn; } } static void mndFreeConn(SConnObj *pConn) { tfree(pConn->pQueries); - mTrace("conn:%d, data:%p destroyed", pConn->id, pConn); + mTrace("conn:%d, is destroyed, data:%p", pConn->id, pConn); } static SConnObj *mndAcquireConn(SMnode *pMnode, int32_t connId) { @@ -143,13 +143,13 @@ static SConnObj *mndAcquireConn(SMnode *pMnode, int32_t connId) { int32_t keepTime = pMnode->cfg.shellActivityTimer * 3; pConn->lastAccessTimeMs = keepTime * 1000 + (uint64_t)taosGetTimestampMs(); - mTrace("conn:%d, data:%p acquired from cache", pConn->id, pConn); + mTrace("conn:%d, acquired from cache, data:%p", pConn->id, pConn); return pConn; } static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn) { if (pConn == NULL) return; - mTrace("conn:%d, data:%p released from cache", pConn->id, pConn); + mTrace("conn:%d, released from cache, data:%p", pConn->id, pConn); SProfileMgmt *pMgmt = &pMnode->profileMgmt; taosCacheRelease(pMgmt->cache, (void **)&pConn, false); diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index e50c6af4bb..b89de94452 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -55,32 +55,25 @@ static SShowObj *mndCreateShowObj(SMnode *pMnode, SShowMsg *pMsg) { int32_t showId = atomic_add_fetch_32(&pMgmt->showId, 1); if (showId == 0) atomic_add_fetch_32(&pMgmt->showId, 1); - int32_t size = sizeof(SShowObj) + pMsg->payloadLen; - SShowObj *pShow = calloc(1, size); - if (pShow != NULL) { - pShow->id = showId; - pShow->pMnode = pMnode; - pShow->type = pMsg->type; - pShow->payloadLen = pMsg->payloadLen; - memcpy(pShow->db, pMsg->db, TSDB_DB_FNAME_LEN); - memcpy(pShow->payload, pMsg->payload, pMsg->payloadLen); - } else { - terrno = TSDB_CODE_OUT_OF_MEMORY; - mError("failed to process show-meta msg:%s since %s", mndShowStr(pMsg->type), terrstr()); - return NULL; - } + int32_t size = sizeof(SShowObj) + pMsg->payloadLen; + SShowObj showObj = {0}; + showObj.id = showId; + showObj.pMnode = pMnode; + showObj.type = pMsg->type; + showObj.payloadLen = pMsg->payloadLen; + memcpy(showObj.db, pMsg->db, TSDB_DB_FNAME_LEN); + memcpy(showObj.payload, pMsg->payload, pMsg->payloadLen); int32_t keepTime = pMnode->cfg.shellActivityTimer * 6 * 1000; - SShowObj *pShowRet = taosCachePut(pMgmt->cache, &showId, sizeof(int32_t), pShow, size, keepTime); - free(pShow); - if (pShowRet == NULL) { + SShowObj *pShow = taosCachePut(pMgmt->cache, &showId, sizeof(int32_t), &showObj, size, keepTime); + if (pShow == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; mError("show:%d, failed to put into cache since %s", showId, terrstr()); return NULL; - } else { - mTrace("show:%d, data:%p created", showId, pShowRet); - return pShowRet; } + + mTrace("show:%d, is created, data:%p", showId, pShow); + return pShow; } static void mndFreeShowObj(SShowObj *pShow) { @@ -94,7 +87,7 @@ static void mndFreeShowObj(SShowObj *pShow) { } } - mTrace("show:%d, data:%p destroyed", pShow->id, pShow); + mTrace("show:%d, is destroyed, data:%p", pShow->id, pShow); } static SShowObj *mndAcquireShowObj(SMnode *pMnode, int32_t showId) { @@ -106,14 +99,14 @@ static SShowObj *mndAcquireShowObj(SMnode *pMnode, int32_t showId) { return NULL; } - mTrace("show:%d, data:%p acquired from cache", pShow->id, pShow); + mTrace("show:%d, acquired from cache, data:%p", pShow->id, pShow); return pShow; } static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) { if (pShow == NULL) return; - mTrace("show:%d, data:%p released from cache, force:%d", pShow->id, pShow, forceRemove); - + mTrace("show:%d, released from cache, data:%p force:%d", pShow->id, pShow, forceRemove); + // A bug in tcache.c forceRemove = 0; @@ -158,8 +151,8 @@ static int32_t mndProcessShowMsg(SMnodeMsg *pMnodeMsg) { } int32_t code = (*metaFp)(pMnodeMsg, pShow, &pRsp->tableMeta); - mDebug("show:%d, data:%p get meta finished, numOfRows:%d cols:%d type:%s result:%s", pShow->id, pShow, - pShow->numOfRows, pShow->numOfColumns, mndShowStr(type), tstrerror(code)); + mDebug("show:%d, get meta finished, numOfRows:%d cols:%d type:%s result:%s", pShow->id, pShow->numOfRows, + pShow->numOfColumns, mndShowStr(type), tstrerror(code)); if (code == TSDB_CODE_SUCCESS) { pMnodeMsg->contLen = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns; @@ -195,16 +188,15 @@ static int32_t mndProcessRetrieveMsg(SMnodeMsg *pMnodeMsg) { if (retrieveFp == NULL) { mndReleaseShowObj(pShow, false); terrno = TSDB_CODE_MSG_NOT_PROCESSED; - mError("show:%d, data:%p failed to retrieve data since %s", pShow->id, pShow, terrstr()); + mError("show:%d, failed to retrieve data since %s", pShow->id, terrstr()); return -1; } - mDebug("show:%d, data:%p start retrieve data, numOfReads:%d numOfRows:%d type:%s", pShow->id, pShow, - pShow->numOfReads, pShow->numOfRows, mndShowStr(pShow->type)); + mDebug("show:%d, start retrieve data, numOfReads:%d numOfRows:%d type:%s", pShow->id, pShow->numOfReads, + pShow->numOfRows, mndShowStr(pShow->type)); if (mndCheckRetrieveFinished(pShow)) { - mDebug("show:%d, data:%p read finished, numOfReads:%d numOfRows:%d", pShow->id, pShow, pShow->numOfReads, - pShow->numOfRows); + mDebug("show:%d, read finished, numOfReads:%d numOfRows:%d", pShow->id, pShow->numOfReads, pShow->numOfRows); pShow->numOfReads = pShow->numOfRows; } @@ -227,7 +219,7 @@ static int32_t mndProcessRetrieveMsg(SMnodeMsg *pMnodeMsg) { if (pRsp == NULL) { mndReleaseShowObj(pShow, false); terrno = TSDB_CODE_OUT_OF_MEMORY; - mError("show:%d, data:%p failed to retrieve data since %s", pShow->id, pShow, terrstr()); + mError("show:%d, failed to retrieve data since %s", pShow->id, terrstr()); return -1; } @@ -236,7 +228,7 @@ static int32_t mndProcessRetrieveMsg(SMnodeMsg *pMnodeMsg) { rowsRead = (*retrieveFp)(pMnodeMsg, pShow, pRsp->data, rowsToRead); } - mDebug("show:%d, data:%p stop retrieve data, rowsRead:%d rowsToRead:%d", pShow->id, pShow, rowsRead, rowsToRead); + mDebug("show:%d, stop retrieve data, rowsRead:%d rowsToRead:%d", pShow->id, rowsRead, rowsToRead); pRsp->numOfRows = htonl(rowsRead); pRsp->precision = TSDB_TIME_PRECISION_MILLI; // millisecond time precision @@ -246,10 +238,10 @@ static int32_t mndProcessRetrieveMsg(SMnodeMsg *pMnodeMsg) { if (rowsRead == 0 || rowsToRead == 0 || (rowsRead == rowsToRead && pShow->numOfRows == pShow->numOfReads)) { pRsp->completed = 1; - mDebug("show:%d, data:%p retrieve completed", pShow->id, pShow); + mDebug("show:%d, retrieve completed", pShow->id); mndReleaseShowObj(pShow, true); } else { - mDebug("show:%d, data:%p retrieve not completed yet", pShow->id, pShow); + mDebug("show:%d, retrieve not completed yet", pShow->id); mndReleaseShowObj(pShow, false); } diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index 9459c5e525..dd69a34dcc 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -294,18 +294,18 @@ TRANS_DECODE_OVER: return NULL; } - mTrace("trans:%d, decode from raw:%p", pTrans->id, pRaw); + mTrace("trans:%d, decode from raw:%p, data:%p", pTrans->id, pRaw, pTrans); return pRow; } static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) { pTrans->stage = TRN_STAGE_PREPARE; - mTrace("trans:%d, perform insert action", pTrans->id); + mTrace("trans:%d, perform insert action, data:%p", pTrans->id, pTrans); return 0; } static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans) { - mTrace("trans:%d, perform delete action", pTrans->id); + mTrace("trans:%d, perform delete action, data:%p", pTrans->id, pTrans); mndTransDropLogs(pTrans->redoLogs); mndTransDropLogs(pTrans->undoLogs); @@ -317,7 +317,7 @@ static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans) { } static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOldTrans, STrans *pNewTrans) { - mTrace("trans:%d, perform update action", pOldTrans->id); + mTrace("trans:%d, perform update action, data:%p", pOldTrans->id, pOldTrans); pOldTrans->stage = pNewTrans->stage; return 0; } @@ -362,14 +362,14 @@ STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, SRpcMsg *pMsg) { return NULL; } - mDebug("trans:%d, is created", pTrans->id); + mDebug("trans:%d, is created, data:%p", pTrans->id, pTrans); return pTrans; } static void mndTransDropLogs(SArray *pArray) { for (int32_t i = 0; i < pArray->size; ++i) { SSdbRaw *pRaw = taosArrayGetP(pArray, i); - tfree(pRaw); + sdbFreeRaw(pRaw); } taosArrayDestroy(pArray); @@ -391,7 +391,7 @@ void mndTransDrop(STrans *pTrans) { mndTransDropActions(pTrans->redoActions); mndTransDropActions(pTrans->undoActions); - // mDebug("trans:%d, is dropped, data:%p", pTrans->id, pTrans); + mDebug("trans:%d, is dropped, data:%p", pTrans->id, pTrans); tfree(pTrans); } diff --git a/source/dnode/mnode/sdb/inc/sdbInt.h b/source/dnode/mnode/sdb/inc/sdbInt.h index 00f6d231d6..da90451202 100644 --- a/source/dnode/mnode/sdb/inc/sdbInt.h +++ b/source/dnode/mnode/sdb/inc/sdbInt.h @@ -72,6 +72,7 @@ typedef struct SSdb { } SSdb; int32_t sdbWriteFile(SSdb *pSdb); +void sdbPrintOper(SSdb *pSdb, SSdbRow *pRow, const char *oper); #ifdef __cplusplus } diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index bb0e606463..97bc0ecbdb 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -80,16 +80,12 @@ void sdbCleanup(SSdb *pSdb) { SHashObj *hash = pSdb->hashObjs[i]; if (hash == NULL) continue; - SdbDeleteFp deleteFp = pSdb->deleteFps[i]; - SSdbRow **ppRow = taosHashIterate(hash, NULL); + SSdbRow **ppRow = taosHashIterate(hash, NULL); while (ppRow != NULL) { SSdbRow *pRow = *ppRow; if (pRow == NULL) continue; - if (deleteFp != NULL) { - (*deleteFp)(pSdb, pRow->pObj); - } - sdbFreeRow(pRow); + sdbFreeRow(pSdb, pRow); ppRow = taosHashIterate(hash, ppRow); } } diff --git a/source/dnode/mnode/sdb/src/sdbHash.c b/source/dnode/mnode/sdb/src/sdbHash.c index 27ff8e697d..78a90b9a7d 100644 --- a/source/dnode/mnode/sdb/src/sdbHash.c +++ b/source/dnode/mnode/sdb/src/sdbHash.c @@ -16,6 +16,50 @@ #define _DEFAULT_SOURCE #include "sdbInt.h" +static const char *sdbTableName(ESdbType type) { + switch (type) { + case SDB_TRANS: + return "trans"; + case SDB_CLUSTER: + return "cluster"; + case SDB_MNODE: + return "mnode"; + case SDB_DNODE: + return "dnode"; + case SDB_USER: + return "user"; + case SDB_AUTH: + return "auth"; + case SDB_ACCT: + return "acct"; + case SDB_TOPIC: + return "topic"; + case SDB_VGROUP: + return "vgId"; + case SDB_STB: + return "stb"; + case SDB_DB: + return "db"; + case SDB_FUNC: + return "func"; + default: + return "undefine"; + } +} + +void sdbPrintOper(SSdb *pSdb, SSdbRow *pRow, const char *oper) { + EKeyType keyType = pSdb->keyTypes[pRow->type]; + + if (keyType == SDB_KEY_BINARY) { + mTrace("%s:%s, refCount:%d oper:%s", sdbTableName(pRow->type), (char *)pRow->pObj, pRow->refCount, oper); + } else if (keyType == SDB_KEY_INT32) { + mTrace("%s:%d, refCount:%d oper:%s", sdbTableName(pRow->type), *(int32_t *)pRow->pObj, pRow->refCount, oper); + } else if (keyType == SDB_KEY_INT64) { + mTrace("%s:%" PRId64 ", refCount:%d oper:%s", sdbTableName(pRow->type), *(int64_t *)pRow->pObj, pRow->refCount, oper); + } else { + } +} + static SHashObj *sdbGetHash(SSdb *pSdb, int32_t type) { if (type >= SDB_MAX || type <= SDB_START) { terrno = TSDB_CODE_SDB_INVALID_TABLE_TYPE; @@ -55,17 +99,18 @@ static int32_t sdbInsertRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow * SSdbRow *pOldRow = taosHashGet(hash, pRow->pObj, keySize); if (pOldRow != NULL) { taosWUnLockLatch(pLock); - sdbFreeRow(pRow); + sdbFreeRow(pSdb, pRow); terrno = TSDB_CODE_SDB_OBJ_ALREADY_THERE; return terrno; } - pRow->refCount = 1; + pRow->refCount = 0; pRow->status = pRaw->status; + sdbPrintOper(pSdb, pRow, "insertRow"); if (taosHashPut(hash, pRow->pObj, keySize, &pRow, sizeof(void *)) != 0) { taosWUnLockLatch(pLock); - sdbFreeRow(pRow); + sdbFreeRow(pSdb, pRow); terrno = TSDB_CODE_SDB_OBJ_ALREADY_THERE; return terrno; } @@ -83,7 +128,7 @@ static int32_t sdbInsertRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow * taosWLockLatch(pLock); taosHashRemove(hash, pRow->pObj, keySize); taosWUnLockLatch(pLock); - sdbFreeRow(pRow); + sdbFreeRow(pSdb, pRow); terrno = code; return terrno; } @@ -113,7 +158,7 @@ static int32_t sdbUpdateRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow * code = (*updateFp)(pSdb, pOldRow->pObj, pNewRow->pObj); } - sdbFreeRow(pNewRow); + sdbFreeRow(pSdb, pNewRow); return code; } @@ -123,14 +168,10 @@ static int32_t sdbDeleteRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow * SRWLatch *pLock = &pSdb->locks[pRow->type]; taosWLockLatch(pLock); - // remove attached object such as trans - SdbDeleteFp deleteFp = pSdb->deleteFps[pRow->type]; - if (deleteFp != NULL) (*deleteFp)(pSdb, pRow->pObj); - SSdbRow **ppOldRow = taosHashGet(hash, pRow->pObj, keySize); if (ppOldRow == NULL || *ppOldRow == NULL) { taosWUnLockLatch(pLock); - sdbFreeRow(pRow); + sdbFreeRow(pSdb, pRow); terrno = TSDB_CODE_SDB_OBJ_NOT_THERE; return terrno; } @@ -140,8 +181,8 @@ static int32_t sdbDeleteRow(SSdb *pSdb, SHashObj *hash, SSdbRaw *pRaw, SSdbRow * taosHashRemove(hash, pOldRow->pObj, keySize); taosWUnLockLatch(pLock); - sdbRelease(pSdb, pOldRow->pObj); - sdbFreeRow(pRow); + // sdbRelease(pSdb, pOldRow->pObj); + sdbFreeRow(pSdb, pRow); return code; } @@ -206,6 +247,7 @@ void *sdbAcquire(SSdb *pSdb, ESdbType type, void *pKey) { case SDB_STATUS_UPDATING: atomic_add_fetch_32(&pRow->refCount, 1); pRet = pRow->pObj; + sdbPrintOper(pSdb, pRow, "acquireRow"); break; case SDB_STATUS_CREATING: terrno = TSDB_CODE_SDB_OBJ_CREATING; @@ -232,13 +274,9 @@ void sdbRelease(SSdb *pSdb, void *pObj) { taosRLockLatch(pLock); int32_t ref = atomic_sub_fetch_32(&pRow->refCount, 1); + sdbPrintOper(pSdb, pRow, "releaseRow"); if (ref <= 0 && pRow->status == SDB_STATUS_DROPPED) { - SdbDeleteFp deleteFp = pSdb->deleteFps[pRow->type]; - if (deleteFp != NULL) { - (*deleteFp)(pSdb, pRow->pObj); - } - - sdbFreeRow(pRow); + sdbFreeRow(pSdb, pRow); } taosRUnLockLatch(pLock); @@ -255,9 +293,9 @@ void *sdbFetch(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj) { if (pIter != NULL) { SSdbRow *pLastRow = *(SSdbRow **)pIter; - int32_t ref = atomic_sub_fetch_32(&pLastRow->refCount, 1); + int32_t ref = atomic_load_32(&pLastRow->refCount); if (ref <= 0 && pLastRow->status == SDB_STATUS_DROPPED) { - sdbFreeRow(pLastRow); + sdbFreeRow(pSdb, pLastRow); } } @@ -270,6 +308,7 @@ void *sdbFetch(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj) { } atomic_add_fetch_32(&pRow->refCount, 1); + sdbPrintOper(pSdb, pRow, "fetchRow"); *ppObj = pRow->pObj; break; } diff --git a/source/dnode/mnode/sdb/src/sdbRaw.c b/source/dnode/mnode/sdb/src/sdbRaw.c index 5a0020199f..e37559808e 100644 --- a/source/dnode/mnode/sdb/src/sdbRaw.c +++ b/source/dnode/mnode/sdb/src/sdbRaw.c @@ -27,12 +27,12 @@ SSdbRaw *sdbAllocRaw(ESdbType type, int8_t sver, int32_t dataLen) { pRaw->sver = sver; pRaw->dataLen = dataLen; - // mTrace("raw:%p, is created, len:%d", pRaw, dataLen); + mTrace("raw:%p, is created, len:%d", pRaw, dataLen); return pRaw; } void sdbFreeRaw(SSdbRaw *pRaw) { - // mTrace("raw:%p, is freed", pRaw); + mTrace("raw:%p, is freed", pRaw); free(pRaw); } diff --git a/source/dnode/mnode/sdb/src/sdbRow.c b/source/dnode/mnode/sdb/src/sdbRow.c index ec1dcf39e6..4c889a6d04 100644 --- a/source/dnode/mnode/sdb/src/sdbRow.c +++ b/source/dnode/mnode/sdb/src/sdbRow.c @@ -35,4 +35,13 @@ void *sdbGetRowObj(SSdbRow *pRow) { return pRow->pObj; } -void sdbFreeRow(SSdbRow *pRow) { tfree(pRow); } +void sdbFreeRow(SSdb *pSdb, SSdbRow *pRow) { + // remove attached object such as trans + SdbDeleteFp deleteFp = pSdb->deleteFps[pRow->type]; + if (deleteFp != NULL) { + (*deleteFp)(pSdb, pRow->pObj); + } + + sdbPrintOper(pSdb, pRow, "freeRow"); + tfree(pRow); +} diff --git a/tests/script/sh/exec.sh b/tests/script/sh/exec.sh index d1572bb513..2e95a740d0 100755 --- a/tests/script/sh/exec.sh +++ b/tests/script/sh/exec.sh @@ -17,7 +17,7 @@ OS_TYPE=`$UNAME_BIN` NODE_NAME= EXEC_OPTON= CLEAR_OPTION="false" -while getopts "n:s:u:x:ct" arg +while getopts "n:s:u:x:cv" arg do case $arg in n) @@ -29,7 +29,7 @@ do c) CLEAR_OPTION="clear" ;; - t) + v) SHELL_OPTION="true" ;; u) From 5676735793d49ed4940997ece2c2bcfb5fa14b81 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Dec 2021 22:40:10 -0800 Subject: [PATCH 5/8] test cases --- tests/script/general/table/basic1.sim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/script/general/table/basic1.sim b/tests/script/general/table/basic1.sim index 5892a81f2e..f2341a84ce 100644 --- a/tests/script/general/table/basic1.sim +++ b/tests/script/general/table/basic1.sim @@ -23,7 +23,7 @@ endi print $data00 $data01 $data02 -sql create table st2 (ts timestamp, i float) tags (j bigint) +sql create table st2 (ts timestamp, i float) tags (j int) sql show stables if $rows != 2 then return -1 @@ -39,15 +39,14 @@ if $rows != 1 then return -1 endi -print --> print $data00 $data01 $data02 print $data10 $data11 $data12 -return - print =============== create child table sql create table c1 using st tags(1) sql create table c2 using st tags(2) + +return sql show tables if $rows != 2 then return -1 From c9f8a1109f46ebc5e186e9168d936becfef54217 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Dec 2021 23:10:50 -0800 Subject: [PATCH 6/8] fix show dnodes error --- source/dnode/mnode/impl/inc/mndDef.h | 5 ----- source/dnode/mnode/impl/inc/mndInt.h | 1 + source/dnode/mnode/impl/inc/mndVgroup.h | 1 + source/dnode/mnode/impl/src/mndDnode.c | 5 ++--- source/dnode/mnode/impl/src/mndVgroup.c | 3 ++- tests/script/general/db/basic1.sim | 22 ++++++++++++++++++++-- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/source/dnode/mnode/impl/inc/mndDef.h b/source/dnode/mnode/impl/inc/mndDef.h index ea0fe46302..ac9fe35f53 100644 --- a/source/dnode/mnode/impl/inc/mndDef.h +++ b/source/dnode/mnode/impl/inc/mndDef.h @@ -124,12 +124,7 @@ typedef struct { int64_t rebootTime; int64_t lastAccessTime; int32_t accessTimes; - int16_t numOfMnodes; - int16_t numOfVnodes; - int16_t numOfQnodes; - int16_t numOfSupportMnodes; int16_t numOfSupportVnodes; - int16_t numOfSupportQnodes; int16_t numOfCores; EDndStatus status; EDndReason offlineReason; diff --git a/source/dnode/mnode/impl/inc/mndInt.h b/source/dnode/mnode/impl/inc/mndInt.h index 6eb82daa11..daa87af1f5 100644 --- a/source/dnode/mnode/impl/inc/mndInt.h +++ b/source/dnode/mnode/impl/inc/mndInt.h @@ -19,6 +19,7 @@ #include "mndDef.h" #include "sdb.h" #include "tcache.h" +#include "tep.h" #include "tqueue.h" #include "ttime.h" diff --git a/source/dnode/mnode/impl/inc/mndVgroup.h b/source/dnode/mnode/impl/inc/mndVgroup.h index 8a3a2c798a..6d391450b7 100644 --- a/source/dnode/mnode/impl/inc/mndVgroup.h +++ b/source/dnode/mnode/impl/inc/mndVgroup.h @@ -29,6 +29,7 @@ void mndReleaseVgroup(SMnode *pMnode, SVgObj *pVgroup); SSdbRaw *mndVgroupActionEncode(SVgObj *pVgroup); int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups); SEpSet mndGetVgroupEpset(SMnode *pMnode, SVgObj *pVgroup); +int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId); SCreateVnodeMsg *mndBuildCreateVnodeMsg(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup); SDropVnodeMsg *mndBuildDropVnodeMsg(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup); diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index 91d2a084af..43b458a52a 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -18,8 +18,7 @@ #include "mndMnode.h" #include "mndShow.h" #include "mndTrans.h" -#include "tep.h" -#include "ttime.h" +#include "mndVgroup.h" #define TSDB_DNODE_VER_NUMBER 1 #define TSDB_DNODE_RESERVE_SIZE 64 @@ -699,7 +698,7 @@ static int32_t mndRetrieveDnodes(SMnodeMsg *pMsg, SShowObj *pShow, char *data, i cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int16_t *)pWrite = pDnode->numOfVnodes; + *(int16_t *)pWrite = mndGetVnodesNum(pMnode, pDnode->id); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index b12d2c4718..98382232ef 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -348,6 +348,7 @@ static int32_t mndProcessDropVnodeRsp(SMnodeMsg *pMsg) { } static int32_t mndProcessSyncVnodeRsp(SMnodeMsg *pMsg) { return 0; } + static int32_t mndProcessCompactVnodeRsp(SMnodeMsg *pMsg) { return 0; } static int32_t mndGetVgroupMaxReplica(SMnode *pMnode, char *dbName, int8_t *pReplica, int32_t *pNumOfVgroups) { @@ -478,7 +479,7 @@ static void mndCancelGetNextVgroup(SMnode *pMnode, void *pIter) { sdbCancelFetch(pSdb, pIter); } -static int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId) { +int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId) { SSdb *pSdb = pMnode->pSdb; int32_t numOfVnodes = 0; void *pIter = NULL; diff --git a/tests/script/general/db/basic1.sim b/tests/script/general/db/basic1.sim index 618b1377b8..05ecbbf5ac 100644 --- a/tests/script/general/db/basic1.sim +++ b/tests/script/general/db/basic1.sim @@ -59,9 +59,27 @@ if $data03 != 0 then endi print =============== show vgroups -sql use d4 +sql show databases -if $rows == 0 then +if $rows == 1 then + return -1 +endi + +sql use d1 +sql show vgroups + +if $rows != 2 then + return -1 +endi + +print =============== show dnodes +sql show dnodes + +if $data00 != 1 then + return -1 +endi + +if $data02 != 2 then return -1 endi From a881adcfe46c09f7635d1fb2bc4fece99666be1f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 29 Dec 2021 00:26:16 -0800 Subject: [PATCH 7/8] alloc vgroups --- source/dnode/mgmt/daemon/src/daemon.c | 2 +- source/dnode/mnode/impl/inc/mndDef.h | 1 + source/dnode/mnode/impl/src/mndTrans.c | 4 +- source/dnode/mnode/impl/src/mndVgroup.c | 105 ++++++++++++++++++------ 4 files changed, 84 insertions(+), 28 deletions(-) diff --git a/source/dnode/mgmt/daemon/src/daemon.c b/source/dnode/mgmt/daemon/src/daemon.c index 6c4fae406e..70dca0e4df 100644 --- a/source/dnode/mgmt/daemon/src/daemon.c +++ b/source/dnode/mgmt/daemon/src/daemon.c @@ -139,7 +139,7 @@ void dmnWaitSignal() { void dmnInitOption(SDnodeOpt *pOption) { pOption->sver = 30000000; //3.0.0.0 pOption->numOfCores = tsNumOfCores; - pOption->numOfSupportVnodes = 1; + pOption->numOfSupportVnodes = 16; pOption->numOfCommitThreads = 1; pOption->statusInterval = tsStatusInterval; pOption->numOfThreadsPerCore = tsNumOfThreadsPerCore; diff --git a/source/dnode/mnode/impl/inc/mndDef.h b/source/dnode/mnode/impl/inc/mndDef.h index ac9fe35f53..1a1306c3da 100644 --- a/source/dnode/mnode/impl/inc/mndDef.h +++ b/source/dnode/mnode/impl/inc/mndDef.h @@ -124,6 +124,7 @@ typedef struct { int64_t rebootTime; int64_t lastAccessTime; int32_t accessTimes; + int16_t numOfVnodes; int16_t numOfSupportVnodes; int16_t numOfCores; EDndStatus status; diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index dd69a34dcc..9263fca695 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -442,7 +442,7 @@ static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) { } sdbSetRawStatus(pRaw, SDB_STATUS_READY); - mTrace("trans:%d, sync to other nodes", pTrans->id); + mDebug("trans:%d, sync to other nodes", pTrans->id); int32_t code = mndSyncPropose(pMnode, pRaw); if (code != 0) { mError("trans:%d, failed to sync since %s", pTrans->id, terrstr()); @@ -450,7 +450,7 @@ static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) { return -1; } - mTrace("trans:%d, sync finished", pTrans->id); + mDebug("trans:%d, sync finished", pTrans->id); code = sdbWrite(pMnode->pSdb, pRaw); if (code != 0) { diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index 98382232ef..06e62d2528 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -86,7 +86,6 @@ SSdbRaw *mndVgroupActionEncode(SVgObj *pVgroup) { for (int8_t i = 0; i < pVgroup->replica; ++i) { SVnodeGid *pVgid = &pVgroup->vnodeGid[i]; SDB_SET_INT32(pRaw, dataPos, pVgid->dnodeId) - SDB_SET_INT8(pRaw, dataPos, pVgid->role) } SDB_SET_RESERVE(pRaw, dataPos, TSDB_VGROUP_RESERVE_SIZE) SDB_SET_DATALEN(pRaw, dataPos); @@ -121,7 +120,6 @@ SSdbRow *mndVgroupActionDecode(SSdbRaw *pRaw) { for (int8_t i = 0; i < pVgroup->replica; ++i) { SVnodeGid *pVgid = &pVgroup->vnodeGid[i]; SDB_GET_INT32(pRaw, pRow, dataPos, &pVgid->dnodeId) - SDB_GET_INT8(pRaw, pRow, dataPos, (int8_t *)&pVgid->role) } SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_VGROUP_RESERVE_SIZE) @@ -237,44 +235,95 @@ SDropVnodeMsg *mndBuildDropVnodeMsg(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *p return pDrop; } -static int32_t mndGetAvailableDnode(SMnode *pMnode, SVgObj *pVgroup) { +static SArray *mndBuildDnodesArray(SMnode *pMnode) { SSdb *pSdb = pMnode->pSdb; - int32_t allocedVnodes = 0; - void *pIter = NULL; + int32_t numOfDnodes = mndGetDnodeSize(pMnode); + SArray *pArray = taosArrayInit(numOfDnodes, sizeof(SDnodeObj)); + if (pArray == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } - while (allocedVnodes < pVgroup->replica) { + void *pIter = NULL; + while (1) { SDnodeObj *pDnode = NULL; pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode); if (pIter == NULL) break; - // todo - if (mndIsDnodeInReadyStatus(pMnode, pDnode)) { - SVnodeGid *pVgid = &pVgroup->vnodeGid[allocedVnodes]; - pVgid->dnodeId = pDnode->id; - if (pVgroup->replica == 1) { - pVgid->role = TAOS_SYNC_STATE_LEADER; - } else { - pVgid->role = TAOS_SYNC_STATE_FOLLOWER; - } - allocedVnodes++; + int32_t numOfVnodes = mndGetVnodesNum(pMnode, pDnode->id); + + bool isMnode = mndIsMnode(pMnode, pDnode->id); + if (isMnode) { + pDnode->numOfVnodes++; } + + bool isReady = mndIsDnodeInReadyStatus(pMnode, pDnode); + if (isReady) { + taosArrayPush(pArray, pDnode); + } + + mDebug("dnode:%d, numOfVnodes:%d numOfSupportVnodes:%d isMnode:%d ready:%d", pDnode->id, numOfVnodes, + pDnode->numOfSupportVnodes, isMnode, isReady); sdbRelease(pSdb, pDnode); } - if (allocedVnodes != pVgroup->replica) { - terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; - return -1; + return pArray; +} + +static int32_t mndCompareDnodeVnodes(SDnodeObj *pDnode1, SDnodeObj *pDnode2) { + float d1Score = (float)pDnode1->numOfVnodes / pDnode1->numOfSupportVnodes; + float d2Score = (float)pDnode2->numOfVnodes / pDnode2->numOfSupportVnodes; + return d1Score > d2Score ? 0 : 1; +} + +static int32_t mndGetAvailableDnode(SMnode *pMnode, SVgObj *pVgroup, SArray *pArray) { + SSdb *pSdb = pMnode->pSdb; + int32_t allocedVnodes = 0; + void *pIter = NULL; + + taosArraySort(pArray, (__compar_fn_t)mndCompareDnodeVnodes); + + for (int32_t v = 0; v < pVgroup->replica; ++v) { + SVnodeGid *pVgid = &pVgroup->vnodeGid[v]; + SDnodeObj *pDnode = taosArrayGet(pArray, v); + if (pDnode == NULL || pDnode->numOfVnodes > pDnode->numOfSupportVnodes) { + terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; + return -1; + } + + pVgid->dnodeId = pDnode->id; + if (pVgroup->replica == 1) { + pVgid->role = TAOS_SYNC_STATE_LEADER; + } else { + pVgid->role = TAOS_SYNC_STATE_FOLLOWER; + } + + mDebug("db:%s, vgId:%d, vindex:%d dnodeId:%d is alloced", pVgroup->dbName, pVgroup->vgId, v, pVgid->dnodeId); + pDnode->numOfVnodes++; } + return 0; } int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups) { - SVgObj *pVgroups = calloc(pDb->cfg.numOfVgroups, sizeof(SVgObj)); + int32_t code = -1; + SArray *pArray = NULL; + SVgObj *pVgroups = NULL; + + pVgroups = calloc(pDb->cfg.numOfVgroups, sizeof(SVgObj)); if (pVgroups == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + goto ALLOC_VGROUP_OVER; } + pArray = mndBuildDnodesArray(pMnode); + if (pArray == NULL) { + goto ALLOC_VGROUP_OVER; + } + + mDebug("db:%s, total %d dnodes used to create %d vgroups (%d vnodes)", pDb->name, (int32_t)taosArrayGetSize(pArray), + pDb->cfg.numOfVgroups, pDb->cfg.numOfVgroups * pDb->cfg.replications); + int32_t allocedVgroups = 0; int32_t maxVgId = sdbGetMaxId(pMnode->pSdb, SDB_VGROUP); uint32_t hashMin = 0; @@ -298,17 +347,23 @@ int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups) { pVgroup->dbUid = pDb->uid; pVgroup->replica = pDb->cfg.replications; - if (mndGetAvailableDnode(pMnode, pVgroup) != 0) { + if (mndGetAvailableDnode(pMnode, pVgroup, pArray) != 0) { terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; - free(pVgroups); - return -1; + goto ALLOC_VGROUP_OVER; } allocedVgroups++; } *ppVgroups = pVgroups; - return 0; + code = 0; + + mDebug("db:%s, %d vgroups is alloced, replica:%d", pDb->name, pDb->cfg.numOfVgroups, pDb->cfg.replications); + +ALLOC_VGROUP_OVER: + if (code != 0) free(pVgroups); + taosArrayDestroy(pArray); + return code; } SEpSet mndGetVgroupEpset(SMnode *pMnode, SVgObj *pVgroup) { From 6b020750c23624bbd60b06f31216ec6ebea36fb6 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 29 Dec 2021 00:40:27 -0800 Subject: [PATCH 8/8] fix crash while use db which is not exist --- source/client/src/clientMsgHandler.c | 15 +++++++++++---- tests/script/general/db/basic1.sim | 6 ++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index e2fdf96385..f7cf661019 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -188,14 +188,21 @@ int32_t processCreateDbRsp(void* param, const SDataBuf* pMsg, int32_t code) { } int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) { - SUseDbRsp* pUseDbRsp = (SUseDbRsp*) pMsg->pData; - SName name = {0}; - tNameFromString(&name, pUseDbRsp->db, T_NAME_ACCT|T_NAME_DB); + SRequestObj* pRequest = param; + + if (code != TSDB_CODE_SUCCESS) { + pRequest->code = code; + tsem_post(&pRequest->body.rspSem); + return code; + } + + SUseDbRsp* pUseDbRsp = (SUseDbRsp*)pMsg->pData; + SName name = {0}; + tNameFromString(&name, pUseDbRsp->db, T_NAME_ACCT | T_NAME_DB); char db[TSDB_DB_NAME_LEN] = {0}; tNameGetDbName(&name, db); - SRequestObj* pRequest = param; setConnectionDB(pRequest->pTscObj, db); tsem_post(&pRequest->body.rspSem); diff --git a/tests/script/general/db/basic1.sim b/tests/script/general/db/basic1.sim index 05ecbbf5ac..44d53917f2 100644 --- a/tests/script/general/db/basic1.sim +++ b/tests/script/general/db/basic1.sim @@ -61,11 +61,13 @@ endi print =============== show vgroups sql show databases -if $rows == 1 then +if $rows != 1 then return -1 endi -sql use d1 +sql_error use d1 + +sql use d4 sql show vgroups if $rows != 2 then