From e677e41573710073bb792a1dc495883eb422b9c7 Mon Sep 17 00:00:00 2001 From: kailixu Date: Sat, 30 Sep 2023 12:29:44 +0800 Subject: [PATCH] fix: support check and merge activeCode --- include/common/tgrant.h | 3 ++ include/util/taoserror.h | 5 +++ source/dnode/mnode/impl/src/mndDnode.c | 57 +++++++++++++++++++++----- source/util/src/terror.c | 4 ++ 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/include/common/tgrant.h b/include/common/tgrant.h index 31d34add24..4d33889336 100644 --- a/include/common/tgrant.h +++ b/include/common/tgrant.h @@ -51,6 +51,9 @@ typedef enum { } EGrantType; int32_t grantCheck(EGrantType grant); +#ifdef TD_ENTERPRISE +int32_t grantAlterActiveCode(const char* old, const char* new, char* out, int8_t type); +#endif #ifndef GRANTS_CFG #ifdef TD_ENTERPRISE diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 39bf2b5681..46a85661d5 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -541,6 +541,11 @@ int32_t* taosGetErrno(); #define TSDB_CODE_GRANT_CPU_LIMITED TAOS_DEF_ERROR_CODE(0, 0x080B) #define TSDB_CODE_GRANT_STABLE_LIMITED TAOS_DEF_ERROR_CODE(0, 0x080C) #define TSDB_CODE_GRANT_TABLE_LIMITED TAOS_DEF_ERROR_CODE(0, 0x080D) +#define TSDB_CODE_GRANT_PAR_INVALID_ACTIVE TAOS_DEF_ERROR_CODE(0, 0x080E) +#define TSDB_CODE_GRANT_GEN_INVALID_KEY TAOS_DEF_ERROR_CODE(0, 0x080F) +#define TSDB_CODE_GRANT_GEN_APP_LIMIT TAOS_DEF_ERROR_CODE(0, 0x0810) +#define TSDB_CODE_GRANT_GEN_INVALID_KLEN TAOS_DEF_ERROR_CODE(0, 0x0811) + // sync // #define TSDB_CODE_SYN_INVALID_CONFIG TAOS_DEF_ERROR_CODE(0, 0x0900) // 2.x diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index f2a8c82192..2bd99c1afc 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -763,9 +763,14 @@ static int32_t mndConfigDnode(SMnode *pMnode, SRpcMsg *pReq, SMCfgDnodeReq *pCfg SSdbRaw *pRaw = NULL; STrans *pTrans = NULL; SDnodeObj *pDnode = NULL; + SArray *failRecord = NULL; bool cfgAll = pCfgReq->dnodeId == -1; int32_t iter = 0; + if (cfgAll && !(failRecord = taosArrayInit(1, sizeof(int32_t)))) { + goto _OVER; + } + SSdb *pSdb = pMnode->pSdb; void *pIter = NULL; while (1) { @@ -774,6 +779,39 @@ static int32_t mndConfigDnode(SMnode *pMnode, SRpcMsg *pReq, SMCfgDnodeReq *pCfg if (pIter == NULL) break; ++iter; } else if (!(pDnode = mndAcquireDnode(pMnode, pCfgReq->dnodeId))) { + terrno = TSDB_CODE_MND_INVALID_DNODE_ID; + goto _OVER; + } + + SDnodeObj tmpDnode = *pDnode; + if (action == DND_ACTIVE_CODE) { +#ifndef MND_MERGE_ACTIVE_CODE + strncpy(tmpDnode.active, pCfgReq->value, TSDB_ACTIVE_KEY_LEN); +#else + if (grantAlterActiveCode(pDnode->active, pCfgReq->value, tmpDnode.active, 0) != 0) { + if (TSDB_CODE_DUP_KEY != terrno) { + mError("dnode:%d, config dnode, cfg:%d, app:%p config:%s value:%s failed since %s", pDnode->id, + pCfgReq->dnodeId, pReq->info.ahandle, pCfgReq->config, pCfgReq->value, terrstr()); + taosArrayPush(failRecord, pDnode->id); + continue; + } + } +#endif + } else if (action == DND_CONN_ACTIVE_CODE) { +#ifndef MND_MERGE_ACTIVE_CODE + strncpy(tmpDnode.connActive, pCfgReq->value, TSDB_CONN_ACTIVE_KEY_LEN); +#else + if (grantAlterActiveCode(pDnode->connActive, pCfgReq->value, tmpDnode.connActive, 1) != 0) { + if (TSDB_CODE_DUP_KEY != terrno) { + mError("dnode:%d, config dnode, cfg:%d, app:%p config:%s value:%s failed since %s", pDnode->id, + pCfgReq->dnodeId, pReq->info.ahandle, pCfgReq->config, pCfgReq->value, terrstr()); + taosArrayPush(failRecord, pDnode->id); + continue; + } + } +#endif + } else { + terrno = TSDB_CODE_INVALID_CFG; goto _OVER; } @@ -783,16 +821,6 @@ static int32_t mndConfigDnode(SMnode *pMnode, SRpcMsg *pReq, SMCfgDnodeReq *pCfg if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER; } - SDnodeObj tmpDnode = *pDnode; - if (action == DND_ACTIVE_CODE) { - strncpy(tmpDnode.active, pCfgReq->value, TSDB_ACTIVE_KEY_LEN); - } else if (action == DND_CONN_ACTIVE_CODE) { - strncpy(tmpDnode.connActive, pCfgReq->value, TSDB_CONN_ACTIVE_KEY_LEN); - } else { - terrno = TSDB_CODE_INVALID_CFG; - goto _OVER; - } - pRaw = mndDnodeActionEncode(&tmpDnode); if (pRaw == NULL || mndTransAppendCommitlog(pTrans, pRaw) != 0) goto _OVER; (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY); @@ -816,12 +844,18 @@ static int32_t mndConfigDnode(SMnode *pMnode, SRpcMsg *pReq, SMCfgDnodeReq *pCfg _OVER: if (cfgAll) { sdbRelease(pSdb, pDnode); + int32_t nFail = taosArrayGetSize(failRecord); + if (nFail > 0) { + mError("config dnode, cfg:%d, app:%p config:%s value:%s. total:%d, fail:%d", pCfgReq->dnodeId, pReq->info.ahandle, + pCfgReq->config, pCfgReq->value, iter, nFail); + } } else { mndReleaseDnode(pMnode, pDnode); } sdbCancelFetch(pSdb, pIter); mndTransDrop(pTrans); sdbFreeRaw(pRaw); + taosArrayDestroy(failRecord); return terrno; } @@ -1262,7 +1296,8 @@ static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) { snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%s", cfgReq.value); if (mndConfigDnode(pMnode, pReq, &cfgReq, opt) != 0) { - mError("dnode:%d, failed to config activeCode since %s", cfgReq.dnodeId, terrstr()); + mError("dnode:%d, failed to config activeCode since %s. conf:%s, val:%s", cfgReq.dnodeId, terrstr(), + cfgReq.config, cfgReq.value); return -1; } return 0; diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 9832720994..116600f393 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -430,6 +430,10 @@ TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_QUERYTIME_LIMITED, "Query time limited by TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_CPU_LIMITED, "CPU cores limited by license") TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_STABLE_LIMITED, "STable creation limited by license") TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_TABLE_LIMITED, "Table creation limited by license") +TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_PAR_INVALID_ACTIVE, "Invalid active code") +TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_GEN_INVALID_KEY, "Invalid key to gen active code") +TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_GEN_APP_LIMIT, "Gen active code limited by app num") +TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_GEN_INVALID_KLEN, "Invalid klen to gen active code") // sync TAOS_DEFINE_ERROR(TSDB_CODE_SYN_TIMEOUT, "Sync timeout")