From f29880fee31cfa293bef18a0a85d401fe607722e Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 12 Sep 2023 11:31:06 +0800 Subject: [PATCH] add rpc update interface --- source/common/src/tmsg.c | 2 ++ source/dnode/mnode/impl/inc/mndDef.h | 1 + source/dnode/mnode/impl/inc/mndUser.h | 2 +- source/dnode/mnode/impl/src/mndDnode.c | 6 ++--- source/dnode/mnode/impl/src/mndUser.c | 32 ++++++++++++++++++++------ 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 05b12344f6..4d58335944 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -1384,6 +1384,8 @@ int32_t tDeserializeSDropUserReq(void *buf, int32_t bufLen, SDropUserReq *pReq) } SIpWhiteList *cloneIpWhiteList(SIpWhiteList *pIpWhiteList) { + if (pIpWhiteList == NULL) return NULL; + int32_t sz = sizeof(SIpWhiteList) + pIpWhiteList->num * sizeof(SIpV4Range); SIpWhiteList *pNew = taosMemoryCalloc(1, sz); memcpy(pNew, pIpWhiteList, sz); diff --git a/source/dnode/mnode/impl/inc/mndDef.h b/source/dnode/mnode/impl/inc/mndDef.h index 6b42fceaba..844e69e659 100644 --- a/source/dnode/mnode/impl/inc/mndDef.h +++ b/source/dnode/mnode/impl/inc/mndDef.h @@ -289,6 +289,7 @@ typedef struct { int32_t acctId; int32_t authVersion; int32_t passVersion; + int64_t ipWhiteListVer; SIpWhiteList* pIpWhiteList; SHashObj* readDbs; diff --git a/source/dnode/mnode/impl/inc/mndUser.h b/source/dnode/mnode/impl/inc/mndUser.h index a995af55f3..f4dfb58cb8 100644 --- a/source/dnode/mnode/impl/inc/mndUser.h +++ b/source/dnode/mnode/impl/inc/mndUser.h @@ -47,7 +47,7 @@ void mndUserFreeObj(SUserObj *pUser); int64_t mndGetIpWhiteVer(SMnode *pMnode); -void mndUpdateIpWhite(char *user, char *fqdn, int8_t type, int8_t lock); +void mndUpdateIpWhite(SMnode *pMnode, char *user, char *fqdn, int8_t type, int8_t lock); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index d31b132db9..f38cf70dc6 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -139,7 +139,7 @@ static int32_t mndCreateDefaultDnode(SMnode *pMnode) { if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER; code = 0; - mndUpdateIpWhite(TSDB_DEFAULT_USER, dnodeObj.fqdn, IP_WHITE_ADD, 1); + mndUpdateIpWhite(pMnode, TSDB_DEFAULT_USER, dnodeObj.fqdn, IP_WHITE_ADD, 1); _OVER: mndTransDrop(pTrans); @@ -705,7 +705,7 @@ static int32_t mndCreateDnode(SMnode *pMnode, SRpcMsg *pReq, SCreateDnodeReq *pC if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER; code = 0; - mndUpdateIpWhite(TSDB_DEFAULT_USER, dnodeObj.fqdn, IP_WHITE_ADD, 1); + mndUpdateIpWhite(pMnode, TSDB_DEFAULT_USER, dnodeObj.fqdn, IP_WHITE_ADD, 1); _OVER: mndTransDrop(pTrans); sdbFreeRaw(pRaw); @@ -998,7 +998,7 @@ static int32_t mndDropDnode(SMnode *pMnode, SRpcMsg *pReq, SDnodeObj *pDnode, SM if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER; - mndUpdateIpWhite(TSDB_DEFAULT_USER, pDnode->fqdn, IP_WHITE_DROP, 1); + mndUpdateIpWhite(pMnode, TSDB_DEFAULT_USER, pDnode->fqdn, IP_WHITE_DROP, 1); code = 0; _OVER: diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index 8f43d7ee49..d9459777f4 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -262,8 +262,14 @@ bool mndUpdateIpWhiteImpl(SHashObj *pIpWhiteTab, char *user, char *fqdn, int8_t return update; } -void mndUpdateIpWhite(char *user, char *fqdn, int8_t type, int8_t lock) { - if (lock) taosThreadRwlockWrlock(&ipWhiteMgt.rw); +void mndUpdateIpWhite(SMnode *pMnode, char *user, char *fqdn, int8_t type, int8_t lock) { + if (lock) { + taosThreadRwlockWrlock(&ipWhiteMgt.rw); + if (ipWhiteMgt.ver == 0) { + ipWhiteMgtUpdateAll(pMnode); + ipWhiteMgt.ver = taosGetTimestampMs(); + } + } bool update = mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, type); if (update) ipWhiteMgt.ver++; @@ -526,6 +532,7 @@ static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char userObj.updateTime = userObj.createdTime; userObj.sysInfo = 1; userObj.enable = 1; + userObj.ipWhiteListVer = taosGetTimestampMs(); userObj.pIpWhiteList = createDefaultIpWhiteList(); if (strcmp(user, TSDB_DEFAULT_USER) == 0) { userObj.superUser = 1; @@ -707,6 +714,8 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) { SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER); taosMemoryFree(buf); + SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER); + SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER) SDB_SET_DATALEN(pRaw, dataPos, _OVER) @@ -875,10 +884,13 @@ static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) { pUser->pIpWhiteList = createIpWhiteList(buf, len); taosMemoryFree(buf); + + SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER); } if (pUser->pIpWhiteList == NULL) { pUser->pIpWhiteList = createDefaultIpWhiteList(); + pUser->ipWhiteListVer = taosGetTimestampMs(); } SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER) @@ -896,7 +908,7 @@ _OVER: taosHashCleanup(pUser->readTbs); taosHashCleanup(pUser->writeTbs); taosHashCleanup(pUser->useDbs); - taosMemoryFree(pUser->pIpWhiteList); + taosMemoryFreeClear(pUser->pIpWhiteList); } taosMemoryFreeClear(pRow); return NULL; @@ -1001,7 +1013,7 @@ void mndUserFreeObj(SUserObj *pUser) { taosHashCleanup(pUser->readTbs); taosHashCleanup(pUser->writeTbs); taosHashCleanup(pUser->useDbs); - taosMemoryFree(pUser->pIpWhiteList); + taosMemoryFreeClear(pUser->pIpWhiteList); pUser->readDbs = NULL; pUser->writeDbs = NULL; pUser->topics = NULL; @@ -1083,6 +1095,8 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate userObj.pIpWhiteList = p; } + userObj.ipWhiteListVer = taosGetTimestampMs(); + STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user"); if (pTrans == NULL) { mError("user:%s, failed to create since %s", pCreate->user, terrstr()); @@ -1591,6 +1605,7 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) { } pNew->num = idx; newUser.pIpWhiteList = pNew; + newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1; } if (alterReq.alterType == TSDB_ALTER_USER_DROP_WHITE_LIST) { taosMemoryFree(newUser.pIpWhiteList); @@ -1617,9 +1632,12 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) { } pNew->num = idx; newUser.pIpWhiteList = pNew; + newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1; + } else { pNew->num = 0; newUser.pIpWhiteList = pNew; + newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1; } } @@ -1827,9 +1845,9 @@ static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBl cols++; - char *buf = NULL; - // int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf); - int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf); + char *buf = NULL; + int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf); + // int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf); if (tlen != 0) { char *varstr = taosMemoryCalloc(1, VARSTR_HEADER_SIZE + tlen); varDataSetLen(varstr, tlen);