From 03c026a54149f84a8087a49f9336d7123d83a657 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 13:36:03 +0800 Subject: [PATCH 01/11] serialize status msg --- include/common/tmsg.h | 8 +- source/common/src/tmsg.c | 174 ++++++++++++++----------- source/dnode/mgmt/impl/src/dndMgmt.c | 8 +- source/dnode/mnode/impl/src/mndDnode.c | 10 +- 4 files changed, 115 insertions(+), 85 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 59f8d38345..6f35dd797e 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -696,8 +696,8 @@ typedef struct { SArray* pVloads; // array of SVnodeLoad } SStatusReq; -int32_t tSerializeSStatusReq(void** buf, SStatusReq* pReq); -void* tDeserializeSStatusReq(void* buf, SStatusReq* pReq); +int32_t tSerializeSStatusReq(void* buf, int32_t bufLen, SStatusReq* pReq); +int32_t tDeserializeSStatusReq(void* buf, int32_t bufLen, SStatusReq* pReq); typedef struct { int32_t dnodeId; @@ -716,8 +716,8 @@ typedef struct { SArray* pDnodeEps; // Array of SDnodeEp } SStatusRsp; -int32_t tSerializeSStatusRsp(void** buf, SStatusRsp* pRsp); -void* tDeserializeSStatusRsp(void* buf, SStatusRsp* pRsp); +int32_t tSerializeSStatusRsp(void* buf, int32_t bufLen, SStatusRsp* pRsp); +int32_t tDeserializeSStatusRsp(void* buf, int32_t bufLen, SStatusRsp* pRsp); typedef struct { int32_t reserve; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index e763c0c85f..144054d078 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -583,144 +583,172 @@ void tFreeSMAltertbReq(SMAltertbReq *pReq) { pReq->pFields = NULL; } -int32_t tSerializeSStatusReq(void **buf, SStatusReq *pReq) { - int32_t tlen = 0; +int32_t tSerializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; // status - tlen += taosEncodeFixedI32(buf, pReq->sver); - tlen += taosEncodeFixedI64(buf, pReq->dver); - tlen += taosEncodeFixedI32(buf, pReq->dnodeId); - tlen += taosEncodeFixedI64(buf, pReq->clusterId); - tlen += taosEncodeFixedI64(buf, pReq->rebootTime); - tlen += taosEncodeFixedI64(buf, pReq->updateTime); - tlen += taosEncodeFixedI32(buf, pReq->numOfCores); - tlen += taosEncodeFixedI32(buf, pReq->numOfSupportVnodes); - tlen += taosEncodeString(buf, pReq->dnodeEp); + if (tEncodeI32(&encoder, pReq->sver) < 0) return -1; + if (tEncodeI64(&encoder, pReq->dver) < 0) return -1; + if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1; + if (tEncodeI64(&encoder, pReq->clusterId) < 0) return -1; + if (tEncodeI64(&encoder, pReq->rebootTime) < 0) return -1; + if (tEncodeI64(&encoder, pReq->updateTime) < 0) return -1; + if (tEncodeI32(&encoder, pReq->numOfCores) < 0) return -1; + if (tEncodeI32(&encoder, pReq->numOfSupportVnodes) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->dnodeEp) < 0) return -1; // cluster cfg - tlen += taosEncodeFixedI32(buf, pReq->clusterCfg.statusInterval); - tlen += taosEncodeFixedI64(buf, pReq->clusterCfg.checkTime); - tlen += taosEncodeString(buf, pReq->clusterCfg.timezone); - tlen += taosEncodeString(buf, pReq->clusterCfg.locale); - tlen += taosEncodeString(buf, pReq->clusterCfg.charset); + if (tEncodeI32(&encoder, pReq->clusterCfg.statusInterval) < 0) return -1; + if (tEncodeI64(&encoder, pReq->clusterCfg.checkTime) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->clusterCfg.timezone) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->clusterCfg.locale) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->clusterCfg.charset) < 0) return -1; // vnode loads int32_t vlen = (int32_t)taosArrayGetSize(pReq->pVloads); - tlen += taosEncodeFixedI32(buf, vlen); + if (tEncodeI32(&encoder, vlen) < 0) return -1; for (int32_t i = 0; i < vlen; ++i) { SVnodeLoad *pload = taosArrayGet(pReq->pVloads, i); - tlen += taosEncodeFixedI32(buf, pload->vgId); - tlen += taosEncodeFixedI8(buf, pload->role); - tlen += taosEncodeFixedI64(buf, pload->numOfTables); - tlen += taosEncodeFixedI64(buf, pload->numOfTimeSeries); - tlen += taosEncodeFixedI64(buf, pload->totalStorage); - tlen += taosEncodeFixedI64(buf, pload->compStorage); - tlen += taosEncodeFixedI64(buf, pload->pointsWritten); + if (tEncodeI32(&encoder, pload->vgId) < 0) return -1; + if (tEncodeI8(&encoder, pload->role) < 0) return -1; + if (tEncodeI64(&encoder, pload->numOfTables) < 0) return -1; + if (tEncodeI64(&encoder, pload->numOfTimeSeries) < 0) return -1; + if (tEncodeI64(&encoder, pload->totalStorage) < 0) return -1; + if (tEncodeI64(&encoder, pload->compStorage) < 0) return -1; + if (tEncodeI64(&encoder, pload->pointsWritten) < 0) return -1; } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); return tlen; } -void *tDeserializeSStatusReq(void *buf, SStatusReq *pReq) { +int32_t tDeserializeSStatusReq(void *buf, int32_t bufLen, SStatusReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + // status - buf = taosDecodeFixedI32(buf, &pReq->sver); - buf = taosDecodeFixedI64(buf, &pReq->dver); - buf = taosDecodeFixedI32(buf, &pReq->dnodeId); - buf = taosDecodeFixedI64(buf, &pReq->clusterId); - buf = taosDecodeFixedI64(buf, &pReq->rebootTime); - buf = taosDecodeFixedI64(buf, &pReq->updateTime); - buf = taosDecodeFixedI32(buf, &pReq->numOfCores); - buf = taosDecodeFixedI32(buf, &pReq->numOfSupportVnodes); - buf = taosDecodeStringTo(buf, pReq->dnodeEp); + if (tDecodeI32(&decoder, &pReq->sver) < 0) return -1; + if (tDecodeI64(&decoder, &pReq->dver) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1; + if (tDecodeI64(&decoder, &pReq->clusterId) < 0) return -1; + if (tDecodeI64(&decoder, &pReq->rebootTime) < 0) return -1; + if (tDecodeI64(&decoder, &pReq->updateTime) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->numOfCores) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->numOfSupportVnodes) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->dnodeEp) < 0) return -1; // cluster cfg - buf = taosDecodeFixedI32(buf, &pReq->clusterCfg.statusInterval); - buf = taosDecodeFixedI64(buf, &pReq->clusterCfg.checkTime); - buf = taosDecodeStringTo(buf, pReq->clusterCfg.timezone); - buf = taosDecodeStringTo(buf, pReq->clusterCfg.locale); - buf = taosDecodeStringTo(buf, pReq->clusterCfg.charset); + if (tDecodeI32(&decoder, &pReq->clusterCfg.statusInterval) < 0) return -1; + if (tDecodeI64(&decoder, &pReq->clusterCfg.checkTime) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->clusterCfg.timezone) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->clusterCfg.locale) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->clusterCfg.charset) < 0) return -1; // vnode loads int32_t vlen = 0; - buf = taosDecodeFixedI32(buf, &vlen); + if (tDecodeI32(&decoder, &vlen) < 0) return -1; pReq->pVloads = taosArrayInit(vlen, sizeof(SVnodeLoad)); if (pReq->pVloads == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + return -1; } for (int32_t i = 0; i < vlen; ++i) { SVnodeLoad vload = {0}; - buf = taosDecodeFixedI32(buf, &vload.vgId); - buf = taosDecodeFixedI8(buf, &vload.role); - buf = taosDecodeFixedI64(buf, &vload.numOfTables); - buf = taosDecodeFixedI64(buf, &vload.numOfTimeSeries); - buf = taosDecodeFixedI64(buf, &vload.totalStorage); - buf = taosDecodeFixedI64(buf, &vload.compStorage); - buf = taosDecodeFixedI64(buf, &vload.pointsWritten); + if (tDecodeI32(&decoder, &vload.vgId) < 0) return -1; + if (tDecodeI8(&decoder, &vload.role) < 0) return -1; + if (tDecodeI64(&decoder, &vload.numOfTables) < 0) return -1; + if (tDecodeI64(&decoder, &vload.numOfTimeSeries) < 0) return -1; + if (tDecodeI64(&decoder, &vload.totalStorage) < 0) return -1; + if (tDecodeI64(&decoder, &vload.compStorage) < 0) return -1; + if (tDecodeI64(&decoder, &vload.pointsWritten) < 0) return -1; if (taosArrayPush(pReq->pVloads, &vload) == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + return -1; } } - return buf; + tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; } -int32_t tSerializeSStatusRsp(void **buf, SStatusRsp *pRsp) { - int32_t tlen = 0; +int32_t tSerializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); - // status; - tlen += taosEncodeFixedI64(buf, pRsp->dver); + if (tStartEncode(&encoder) < 0) return -1; + + // status + if (tEncodeI64(&encoder, pRsp->dver) < 0) return -1; // dnode cfg - tlen += taosEncodeFixedI32(buf, pRsp->dnodeCfg.dnodeId); - tlen += taosEncodeFixedI64(buf, pRsp->dnodeCfg.clusterId); + if (tEncodeI32(&encoder, pRsp->dnodeCfg.dnodeId) < 0) return -1; + if (tEncodeI64(&encoder, pRsp->dnodeCfg.clusterId) < 0) return -1; // dnode eps int32_t dlen = (int32_t)taosArrayGetSize(pRsp->pDnodeEps); - tlen += taosEncodeFixedI32(buf, dlen); + if (tEncodeI32(&encoder, dlen) < 0) return -1; for (int32_t i = 0; i < dlen; ++i) { SDnodeEp *pDnodeEp = taosArrayGet(pRsp->pDnodeEps, i); - tlen += taosEncodeFixedI32(buf, pDnodeEp->id); - tlen += taosEncodeFixedI8(buf, pDnodeEp->isMnode); - tlen += taosEncodeString(buf, pDnodeEp->ep.fqdn); - tlen += taosEncodeFixedU16(buf, pDnodeEp->ep.port); + if (tEncodeI32(&encoder, pDnodeEp->id) < 0) return -1; + if (tEncodeI8(&encoder, pDnodeEp->isMnode) < 0) return -1; + if (tEncodeCStr(&encoder, pDnodeEp->ep.fqdn) < 0) return -1; + if (tEncodeU16(&encoder, pDnodeEp->ep.port) < 0) return -1; } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); return tlen; } -void *tDeserializeSStatusRsp(void *buf, SStatusRsp *pRsp) { +int32_t tDeserializeSStatusRsp(void *buf, int32_t bufLen, SStatusRsp *pRsp) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + // status - buf = taosDecodeFixedI64(buf, &pRsp->dver); + if (tDecodeI64(&decoder, &pRsp->dver) < 0) return -1; // cluster cfg - buf = taosDecodeFixedI32(buf, &pRsp->dnodeCfg.dnodeId); - buf = taosDecodeFixedI64(buf, &pRsp->dnodeCfg.clusterId); + if (tDecodeI32(&decoder, &pRsp->dnodeCfg.dnodeId) < 0) return -1; + if (tDecodeI64(&decoder, &pRsp->dnodeCfg.clusterId) < 0) return -1; // dnode eps int32_t dlen = 0; - buf = taosDecodeFixedI32(buf, &dlen); + if (tDecodeI32(&decoder, &dlen) < 0) return -1; pRsp->pDnodeEps = taosArrayInit(dlen, sizeof(SDnodeEp)); if (pRsp->pDnodeEps == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + return -1; } for (int32_t i = 0; i < dlen; ++i) { SDnodeEp dnodeEp = {0}; - buf = taosDecodeFixedI32(buf, &dnodeEp.id); - buf = taosDecodeFixedI8(buf, &dnodeEp.isMnode); - buf = taosDecodeStringTo(buf, dnodeEp.ep.fqdn); - buf = taosDecodeFixedU16(buf, &dnodeEp.ep.port); + if (tDecodeI32(&decoder, &dnodeEp.id) < 0) return -1; + if (tDecodeI8(&decoder, &dnodeEp.isMnode) < 0) return -1; + if (tDecodeCStrTo(&decoder, dnodeEp.ep.fqdn) < 0) return -1; + if (tDecodeU16(&decoder, &dnodeEp.ep.port) < 0) return -1; if (taosArrayPush(pRsp->pDnodeEps, &dnodeEp) == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + return -1; } } - return buf; + tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; } int32_t tSerializeSCreateAcctReq(void *buf, int32_t bufLen, SCreateAcctReq *pReq) { diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 41dd54a0ff..ff8efb6a1d 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -379,10 +379,9 @@ void dndSendStatusReq(SDnode *pDnode) { req.pVloads = taosArrayInit(TSDB_MAX_VNODES, sizeof(SVnodeLoad)); dndGetVnodeLoads(pDnode, req.pVloads); - int32_t contLen = tSerializeSStatusReq(NULL, &req); + int32_t contLen = tSerializeSStatusReq(NULL, 0, &req); void *pHead = rpcMallocCont(contLen); - void *pBuf = pHead; - tSerializeSStatusReq(&pBuf, &req); + tSerializeSStatusReq(pHead, contLen, &req); taosArrayDestroy(req.pVloads); SRpcMsg rpcMsg = {.pCont = pHead, .contLen = contLen, .msgType = TDMT_MND_STATUS, .ahandle = (void *)9527}; @@ -437,7 +436,8 @@ static void dndProcessStatusRsp(SDnode *pDnode, SRpcMsg *pRsp) { } } else { SStatusRsp statusRsp = {0}; - if (pRsp->pCont != NULL && pRsp->contLen != 0 && tDeserializeSStatusRsp(pRsp->pCont, &statusRsp) != NULL) { + if (pRsp->pCont != NULL && pRsp->contLen != 0 && + tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) { pMgmt->dver = statusRsp.dver; dndUpdateDnodeCfg(pDnode, &statusRsp.dnodeCfg); dndUpdateDnodeEps(pDnode, statusRsp.pDnodeEps); diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index bad0aa8a0b..07b38bdc6a 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -302,7 +302,10 @@ static int32_t mndProcessStatusReq(SMnodeMsg *pReq) { SDnodeObj *pDnode = NULL; int32_t code = -1; - if (tDeserializeSStatusReq(pReq->rpcMsg.pCont, &statusReq) == NULL) goto PROCESS_STATUS_MSG_OVER; + if (tDeserializeSStatusReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &statusReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto PROCESS_STATUS_MSG_OVER; + } if (statusReq.dnodeId == 0) { pDnode = mndAcquireDnodeByEp(pMnode, statusReq.dnodeEp); @@ -410,10 +413,9 @@ static int32_t mndProcessStatusReq(SMnodeMsg *pReq) { mndGetDnodeData(pMnode, statusRsp.pDnodeEps); - int32_t contLen = tSerializeSStatusRsp(NULL, &statusRsp); + int32_t contLen = tSerializeSStatusRsp(NULL, 0, &statusRsp); void *pHead = rpcMallocCont(contLen); - void *pBuf = pHead; - tSerializeSStatusRsp(&pBuf, &statusRsp); + tSerializeSStatusRsp(pHead, contLen, &statusRsp); taosArrayDestroy(statusRsp.pDnodeEps); pReq->contLen = contLen; From 43f7e18c65dfc8a1f68031e9f15afb6b9197cd06 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 13:44:43 +0800 Subject: [PATCH 02/11] serialize timer msg --- include/common/tmsg.h | 11 +++++------ include/common/tmsgdef.h | 2 +- source/common/src/tmsg.c | 27 ++++++++++++++++++++++++++- source/dnode/mgmt/impl/src/dndMgmt.c | 2 +- source/dnode/mnode/impl/src/mnode.c | 23 +++++++++++++++++++---- 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 6f35dd797e..e46bc24137 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -720,8 +720,11 @@ int32_t tSerializeSStatusRsp(void* buf, int32_t bufLen, SStatusRsp* pRsp); int32_t tDeserializeSStatusRsp(void* buf, int32_t bufLen, SStatusRsp* pRsp); typedef struct { - int32_t reserve; -} STransReq; + int32_t reserved; +} SMTimerReq; + +int32_t tSerializeSMTimerMsg(void* buf, int32_t bufLen, SMTimerReq* pReq); +int32_t tDeserializeSMTimerMsg(void* buf, int32_t bufLen, SMTimerReq* pReq); typedef struct { int32_t id; @@ -1195,10 +1198,6 @@ static FORCE_INLINE void* tDeserializeSMVSubscribeReq(void* buf, SMVSubscribeReq return buf; } -typedef struct { - int32_t reserved; -} SMqTmrMsg; - typedef struct { const char* key; SArray* lostConsumers; // SArray diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index 2f41e574bd..e5e6fbd0ca 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -142,7 +142,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_MND_DROP_TOPIC, "mnode-drop-topic", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_SUBSCRIBE, "mnode-subscribe", SCMSubscribeReq, SCMSubscribeRsp) TD_DEF_MSG_TYPE(TDMT_MND_GET_SUB_EP, "mnode-get-sub-ep", SMqCMGetSubEpReq, SMqCMGetSubEpRsp) - TD_DEF_MSG_TYPE(TDMT_MND_MQ_TIMER, "mnode-mq-timer", SMqTmrMsg, SMqTmrMsg) + TD_DEF_MSG_TYPE(TDMT_MND_MQ_TIMER, "mnode-mq-timer", SMTimerReq, SMTimerReq) TD_DEF_MSG_TYPE(TDMT_MND_MQ_DO_REBALANCE, "mnode-mq-do-rebalance", SMqDoRebalanceMsg, SMqDoRebalanceMsg) // Requests handled by VNODE diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 144054d078..8f4ebffd12 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -1984,7 +1984,7 @@ int32_t tDeserializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) { if (tStartDecode(&decoder) < 0) return -1; if (tDecodeI32(&decoder, &pRsp->acctId) < 0) return -1; - if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1; + if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1; if (tDecodeI32(&decoder, &pRsp->connId) < 0) return -1; if (tDecodeI8(&decoder, &pRsp->superUser) < 0) return -1; if (tDecodeSEpSet(&decoder, &pRsp->epSet) < 0) return -1; @@ -1994,3 +1994,28 @@ int32_t tDeserializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) { tCoderClear(&decoder); return 0; } + +int32_t tSerializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->reserved) < 0) return -1; + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->reserved) < 0) return -1; + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index ff8efb6a1d..93c2d4e3e1 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -394,7 +394,7 @@ void dndSendStatusReq(SDnode *pDnode) { static void dndUpdateDnodeCfg(SDnode *pDnode, SDnodeCfg *pCfg) { SDnodeMgmt *pMgmt = &pDnode->dmgmt; if (pMgmt->dnodeId == 0) { - dInfo("set dnodeId:%d clusterId:0x%" PRId64, pCfg->dnodeId, pCfg->clusterId); + dInfo("set dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId); taosWLockLatch(&pMgmt->latch); pMgmt->dnodeId = pCfg->dnodeId; pMgmt->clusterId = pCfg->clusterId; diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index 211cfbd1f0..699ccab92c 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -60,11 +60,25 @@ void mndSendRedirectRsp(SMnode *pMnode, SRpcMsg *pMsg) { } } +static void *mndBuildTimerMsg(int32_t *pContLen) { + SMTimerReq timerReq = {0}; + + int32_t contLen = tSerializeSMTimerMsg(NULL, 0, &timerReq); + if (contLen <= 0) return NULL; + void *pReq = rpcMallocCont(contLen); + if (pReq == NULL) return NULL; + + tSerializeSMTimerMsg(pReq, contLen, &timerReq); + *pContLen = contLen; + return pReq; +} + static void mndTransReExecute(void *param, void *tmrId) { SMnode *pMnode = param; if (mndIsMaster(pMnode)) { - STransReq *pMsg = rpcMallocCont(sizeof(STransReq)); - SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRANS, .pCont = pMsg, .contLen = sizeof(STransReq)}; + int32_t contLen = 0; + void *pReq = mndBuildTimerMsg(&contLen); + SRpcMsg rpcMsg = {.msgType = TDMT_MND_TRANS, .pCont = pReq, .contLen = contLen}; pMnode->putReqToMWriteQFp(pMnode->pDnode, &rpcMsg); } @@ -74,8 +88,9 @@ static void mndTransReExecute(void *param, void *tmrId) { static void mndCalMqRebalance(void *param, void *tmrId) { SMnode *pMnode = param; if (mndIsMaster(pMnode)) { - SMqTmrMsg *pMsg = rpcMallocCont(sizeof(SMqTmrMsg)); - SRpcMsg rpcMsg = {.msgType = TDMT_MND_MQ_TIMER, .pCont = pMsg, .contLen = sizeof(SMqTmrMsg)}; + int32_t contLen = 0; + void *pReq = mndBuildTimerMsg(&contLen); + SRpcMsg rpcMsg = {.msgType = TDMT_MND_MQ_TIMER, .pCont = pReq, .contLen = contLen}; pMnode->putReqToMReadQFp(pMnode->pDnode, &rpcMsg); } From 69ca3783ef9dd8f3f32a54b60503b8e2148dd8e4 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 13:55:37 +0800 Subject: [PATCH 03/11] minor changes --- include/common/tmsg.h | 9 +++++---- include/common/tmsgdef.h | 1 - source/common/src/tmsg.c | 2 +- source/dnode/mgmt/impl/src/dndMgmt.c | 3 --- source/dnode/mgmt/impl/src/dndTransport.c | 2 -- source/dnode/mgmt/impl/src/dndVnodes.c | 23 ----------------------- 6 files changed, 6 insertions(+), 34 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index e46bc24137..cd327d61ca 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -759,6 +759,9 @@ typedef struct { SReplica replicas[TSDB_MAX_REPLICA]; } SCreateVnodeReq, SAlterVnodeReq; +int32_t tSerializeSCreateVnodeReq(void* buf, int32_t bufLen, SCreateVnodeReq* pReq); +int32_t tDeserializeSCreateVnodeReq(void* buf, int32_t bufLen, SCreateVnodeReq* pReq); + typedef struct { int32_t vgId; int32_t dnodeId; @@ -766,10 +769,8 @@ typedef struct { char db[TSDB_DB_FNAME_LEN]; } SDropVnodeReq, SSyncVnodeReq, SCompactVnodeReq; -typedef struct { - int32_t vgId; - int8_t accessState; -} SAuthVnodeReq; +int32_t tSerializeSDropVnodeReq(void* buf, int32_t bufLen, SDropVnodeReq* pReq); +int32_t tDeserializeSDropVnodeReq(void* buf, int32_t bufLen, SDropVnodeReq* pReq); typedef struct { SMsgHead header; diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index e5e6fbd0ca..d43d334248 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -82,7 +82,6 @@ enum { TD_DEF_MSG_TYPE(TDMT_DND_CREATE_VNODE, "dnode-create-vnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_DND_ALTER_VNODE, "dnode-alter-vnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_DND_DROP_VNODE, "dnode-drop-vnode", NULL, NULL) - TD_DEF_MSG_TYPE(TDMT_DND_AUTH_VNODE, "dnode-auth-vnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_DND_SYNC_VNODE, "dnode-sync-vnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_DND_COMPACT_VNODE, "dnode-compact-vnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_DND_CONFIG_DNODE, "dnode-config-dnode", NULL, NULL) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 8f4ebffd12..de812db68b 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -1984,7 +1984,7 @@ int32_t tDeserializeSConnectRsp(void *buf, int32_t bufLen, SConnectRsp *pRsp) { if (tStartDecode(&decoder) < 0) return -1; if (tDecodeI32(&decoder, &pRsp->acctId) < 0) return -1; - if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1; + if (tDecodeI64(&decoder, &pRsp->clusterId) < 0) return -1; if (tDecodeI32(&decoder, &pRsp->connId) < 0) return -1; if (tDecodeI8(&decoder, &pRsp->superUser) < 0) return -1; if (tDecodeSEpSet(&decoder, &pRsp->epSet) < 0) return -1; diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 93c2d4e3e1..3d149a3e60 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -652,9 +652,6 @@ static void dndProcessMgmtQueue(SDnode *pDnode, SRpcMsg *pMsg) { case TDMT_DND_DROP_VNODE: code = dndProcessDropVnodeReq(pDnode, pMsg); break; - case TDMT_DND_AUTH_VNODE: - code = dndProcessAuthVnodeReq(pDnode, pMsg); - break; case TDMT_DND_SYNC_VNODE: code = dndProcessSyncVnodeReq(pDnode, pMsg); break; diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index 239fe9ca4d..e65e62070e 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -57,8 +57,6 @@ static void dndInitMsgFp(STransMgmt *pMgmt) { pMgmt->msgFp[TMSG_INDEX(TDMT_DND_DROP_VNODE_RSP)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_DND_SYNC_VNODE)] = dndProcessMgmtMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_DND_SYNC_VNODE_RSP)] = dndProcessMnodeWriteMsg; - pMgmt->msgFp[TMSG_INDEX(TDMT_DND_AUTH_VNODE)] = dndProcessMgmtMsg; - pMgmt->msgFp[TMSG_INDEX(TDMT_DND_AUTH_VNODE_RSP)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_DND_COMPACT_VNODE)] = dndProcessMgmtMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_DND_COMPACT_VNODE_RSP)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_DND_CONFIG_DNODE)] = dndProcessMgmtMsg; diff --git a/source/dnode/mgmt/impl/src/dndVnodes.c b/source/dnode/mgmt/impl/src/dndVnodes.c index 3eeb28e532..91bf0dfa83 100644 --- a/source/dnode/mgmt/impl/src/dndVnodes.c +++ b/source/dnode/mgmt/impl/src/dndVnodes.c @@ -562,12 +562,6 @@ static SDropVnodeReq *dndParseDropVnodeReq(SRpcMsg *pReq) { return pDrop; } -static SAuthVnodeReq *dndParseAuthVnodeReq(SRpcMsg *pReq) { - SAuthVnodeReq *pAuth = pReq->pCont; - pAuth->vgId = htonl(pAuth->vgId); - return pAuth; -} - int32_t dndProcessCreateVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { SCreateVnodeReq *pCreate = dndParseCreateVnodeReq(pReq); dDebug("vgId:%d, create vnode req is received", pCreate->vgId); @@ -683,23 +677,6 @@ int32_t dndProcessDropVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { return 0; } -int32_t dndProcessAuthVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SAuthVnodeReq *pAuth = (SAuthVnodeReq *)dndParseAuthVnodeReq(pReq); - - int32_t vgId = pAuth->vgId; - dDebug("vgId:%d, auth vnode req is received", vgId); - - SVnodeObj *pVnode = dndAcquireVnode(pDnode, vgId); - if (pVnode == NULL) { - dDebug("vgId:%d, failed to auth since %s", vgId, terrstr()); - return -1; - } - - pVnode->accessState = pAuth->accessState; - dndReleaseVnode(pDnode, pVnode); - return 0; -} - int32_t dndProcessSyncVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { SSyncVnodeReq *pSync = (SSyncVnodeReq *)dndParseDropVnodeReq(pReq); From 2b73e4f4248ab1db75c14f7ca0d70c447d697369 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 14:10:09 +0800 Subject: [PATCH 04/11] serialize drop vnode req --- source/common/src/tmsg.c | 31 +++++++++++++++++++++ source/dnode/mgmt/impl/src/dndVnodes.c | 21 ++++++-------- source/dnode/mgmt/impl/test/vnode/vnode.cpp | 16 ++++++----- source/dnode/mnode/impl/inc/mndVgroup.h | 2 +- source/dnode/mnode/impl/src/mndDb.c | 10 ++++--- source/dnode/mnode/impl/src/mndVgroup.c | 26 +++++++++++------ 6 files changed, 74 insertions(+), 32 deletions(-) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index de812db68b..0aca468acb 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2019,3 +2019,34 @@ int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) { tCoderClear(&decoder); return 0; } + +int32_t tSerializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1; + if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1; + if (tEncodeU64(&encoder, pReq->dbUid) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->db) < 0) return -1; + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1; + if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1; + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} \ No newline at end of file diff --git a/source/dnode/mgmt/impl/src/dndVnodes.c b/source/dnode/mgmt/impl/src/dndVnodes.c index 91bf0dfa83..0a417788f9 100644 --- a/source/dnode/mgmt/impl/src/dndVnodes.c +++ b/source/dnode/mgmt/impl/src/dndVnodes.c @@ -556,12 +556,6 @@ static void dndGenerateWrapperCfg(SDnode *pDnode, SCreateVnodeReq *pCreate, SWra pCfg->vgVersion = pCreate->vgVersion; } -static SDropVnodeReq *dndParseDropVnodeReq(SRpcMsg *pReq) { - SDropVnodeReq *pDrop = pReq->pCont; - pDrop->vgId = htonl(pDrop->vgId); - return pDrop; -} - int32_t dndProcessCreateVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { SCreateVnodeReq *pCreate = dndParseCreateVnodeReq(pReq); dDebug("vgId:%d, create vnode req is received", pCreate->vgId); @@ -652,9 +646,10 @@ int32_t dndProcessAlterVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { } int32_t dndProcessDropVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SDropVnodeReq *pDrop = dndParseDropVnodeReq(pReq); + SDropVnodeReq dropReq = {0}; + tDeserializeSDropVnodeReq(pReq->pCont, pReq->contLen, &dropReq); - int32_t vgId = pDrop->vgId; + int32_t vgId = dropReq.vgId; dDebug("vgId:%d, drop vnode req is received", vgId); SVnodeObj *pVnode = dndAcquireVnode(pDnode, vgId); @@ -678,9 +673,10 @@ int32_t dndProcessDropVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { } int32_t dndProcessSyncVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SSyncVnodeReq *pSync = (SSyncVnodeReq *)dndParseDropVnodeReq(pReq); + SSyncVnodeReq syncReq = {0}; + tDeserializeSDropVnodeReq(pReq->pCont, pReq->contLen, &syncReq); - int32_t vgId = pSync->vgId; + int32_t vgId = syncReq.vgId; dDebug("vgId:%d, sync vnode req is received", vgId); SVnodeObj *pVnode = dndAcquireVnode(pDnode, vgId); @@ -700,9 +696,10 @@ int32_t dndProcessSyncVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { } int32_t dndProcessCompactVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SCompactVnodeReq *pCompact = (SCompactVnodeReq *)dndParseDropVnodeReq(pReq); + SCompactVnodeReq compatcReq = {0}; + tDeserializeSDropVnodeReq(pReq->pCont, pReq->contLen, &compatcReq); - int32_t vgId = pCompact->vgId; + int32_t vgId = compatcReq.vgId; dDebug("vgId:%d, compact vnode req is received", vgId); SVnodeObj *pVnode = dndAcquireVnode(pDnode, vgId); diff --git a/source/dnode/mgmt/impl/test/vnode/vnode.cpp b/source/dnode/mgmt/impl/test/vnode/vnode.cpp index 380a837c58..0e9f0bd5bf 100644 --- a/source/dnode/mgmt/impl/test/vnode/vnode.cpp +++ b/source/dnode/mgmt/impl/test/vnode/vnode.cpp @@ -312,17 +312,19 @@ TEST_F(DndTestVnode, 05_DROP_Stb) { TEST_F(DndTestVnode, 06_Drop_Vnode) { for (int i = 0; i < 3; ++i) { - int32_t contLen = sizeof(SDropVnodeReq); + SDropVnodeReq dropReq = {0}; + dropReq.vgId = 2; + dropReq.dnodeId = 1; + strcpy(dropReq.db, "1.d1"); + dropReq.dbUid = 9527; - SDropVnodeReq* pReq = (SDropVnodeReq*)rpcMallocCont(contLen); - pReq->vgId = htonl(2); - pReq->dnodeId = htonl(1); - strcpy(pReq->db, "1.d1"); - pReq->dbUid = htobe64(9527); + int32_t contLen = tSerializeSDropVnodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDropVnodeReq(pReq, contLen, &dropReq); SRpcMsg rpcMsg = {0}; rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SDropVnodeReq); + rpcMsg.contLen = contLen; rpcMsg.msgType = TDMT_DND_DROP_VNODE; SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_VNODE, pReq, contLen); diff --git a/source/dnode/mnode/impl/inc/mndVgroup.h b/source/dnode/mnode/impl/inc/mndVgroup.h index 6ab11aa1b4..ea9d509815 100644 --- a/source/dnode/mnode/impl/inc/mndVgroup.h +++ b/source/dnode/mnode/impl/inc/mndVgroup.h @@ -32,7 +32,7 @@ SEpSet mndGetVgroupEpset(SMnode *pMnode, SVgObj *pVgroup); int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId); SCreateVnodeReq *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup); -SDropVnodeReq *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup); +SDropVnodeReq *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 0cceb1084b..ce308e221f 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -365,11 +365,12 @@ static int32_t mndSetCreateDbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - SDropVnodeReq *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup); + int32_t contLen = 0; + SDropVnodeReq *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); if (pReq == NULL) return -1; action.pCont = pReq; - action.contLen = sizeof(SDropVnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_VNODE; action.acceptableCode = TSDB_CODE_DND_VNODE_NOT_DEPLOYED; if (mndTransAppendUndoAction(pTrans, &action) != 0) { @@ -754,11 +755,12 @@ static int32_t mndBuildDropVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj * action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - SDropVnodeReq *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup); + int32_t contLen = 0; + SDropVnodeReq *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); if (pReq == NULL) return -1; action.pCont = pReq; - action.contLen = sizeof(SDropVnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_VNODE; action.acceptableCode = TSDB_CODE_DND_VNODE_NOT_DEPLOYED; if (mndTransAppendRedoAction(pTrans, &action) != 0) { diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index 819dea4910..997388c828 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -248,19 +248,29 @@ SCreateVnodeReq *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbOb return pCreate; } -SDropVnodeReq *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup) { - SDropVnodeReq *pDrop = calloc(1, sizeof(SDropVnodeReq)); - if (pDrop == NULL) { +SDropVnodeReq *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, + int32_t *pContLen) { + SDropVnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + dropReq.vgId = pVgroup->vgId; + memcpy(dropReq.db, pDb->name, TSDB_DB_FNAME_LEN); + dropReq.dbUid = pDb->uid; + + int32_t contLen = tSerializeSDropVnodeReq(NULL, 0, &dropReq); + if (contLen < 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } - pDrop->dnodeId = htonl(pDnode->id); - pDrop->vgId = htonl(pVgroup->vgId); - memcpy(pDrop->db, pDb->name, TSDB_DB_FNAME_LEN); - pDrop->dbUid = htobe64(pDb->uid); + void *pReq = malloc(contLen); + if (pReq == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } - return pDrop; + tSerializeSDropVnodeReq(pReq, contLen, &dropReq); + *pContLen = contLen; + return pReq; } static bool mndResetDnodesArrayFp(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) { From 7ce10a868b08c16b5cbf11551384bca3150aa7ad Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 14:57:47 +0800 Subject: [PATCH 05/11] serialize create vnode msg --- source/common/src/tmsg.c | 83 ++++++++- source/dnode/mgmt/impl/src/dndVnodes.c | 132 +++++++------- source/dnode/mgmt/impl/test/vnode/vnode.cpp | 186 ++++++++++---------- source/dnode/mnode/impl/inc/mndVgroup.h | 4 +- source/dnode/mnode/impl/src/mndDb.c | 18 +- source/dnode/mnode/impl/src/mndVgroup.c | 81 +++++---- 6 files changed, 294 insertions(+), 210 deletions(-) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 0aca468acb..f6af9782ad 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2020,6 +2020,87 @@ int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) { return 0; } +int32_t tSerializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->vgId) < 0) return -1; + if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->db) < 0) return -1; + if (tEncodeU64(&encoder, pReq->dbUid) < 0) return -1; + if (tEncodeI32(&encoder, pReq->vgVersion) < 0) return -1; + if (tEncodeI32(&encoder, pReq->cacheBlockSize) < 0) return -1; + if (tEncodeI32(&encoder, pReq->totalBlocks) < 0) return -1; + if (tEncodeI32(&encoder, pReq->daysPerFile) < 0) return -1; + if (tEncodeI32(&encoder, pReq->daysToKeep0) < 0) return -1; + if (tEncodeI32(&encoder, pReq->daysToKeep1) < 0) return -1; + if (tEncodeI32(&encoder, pReq->daysToKeep2) < 0) return -1; + if (tEncodeI32(&encoder, pReq->minRows) < 0) return -1; + if (tEncodeI32(&encoder, pReq->maxRows) < 0) return -1; + if (tEncodeI32(&encoder, pReq->commitTime) < 0) return -1; + if (tEncodeI32(&encoder, pReq->fsyncPeriod) < 0) return -1; + if (tEncodeI8(&encoder, pReq->walLevel) < 0) return -1; + if (tEncodeI8(&encoder, pReq->precision) < 0) return -1; + if (tEncodeI8(&encoder, pReq->compression) < 0) return -1; + if (tEncodeI8(&encoder, pReq->quorum) < 0) return -1; + if (tEncodeI8(&encoder, pReq->update) < 0) return -1; + if (tEncodeI8(&encoder, pReq->cacheLastRow) < 0) return -1; + if (tEncodeI8(&encoder, pReq->replica) < 0) return -1; + if (tEncodeI8(&encoder, pReq->selfIndex) < 0) return -1; + for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) { + SReplica *pReplica = &pReq->replicas[i]; + if (tEncodeI32(&encoder, pReplica->id) < 0) return -1; + if (tEncodeU16(&encoder, pReplica->port) < 0) return -1; + if (tEncodeCStr(&encoder, pReplica->fqdn) < 0) return -1; + } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1; + if (tDecodeU64(&decoder, &pReq->dbUid) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->vgVersion) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->cacheBlockSize) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->totalBlocks) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->daysPerFile) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->daysToKeep0) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->daysToKeep1) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->daysToKeep2) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->minRows) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->maxRows) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->commitTime) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->fsyncPeriod) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->walLevel) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->precision) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->compression) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->quorum) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->update) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->cacheLastRow) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->replica) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->selfIndex) < 0) return -1; + for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) { + SReplica *pReplica = &pReq->replicas[i]; + if (tDecodeI32(&decoder, &pReplica->id) < 0) return -1; + if (tDecodeU16(&decoder, &pReplica->port) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReplica->fqdn) < 0) return -1; + } + + tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; +} + int32_t tSerializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) { SCoder encoder = {0}; tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); @@ -2043,7 +2124,7 @@ int32_t tDeserializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq if (tStartDecode(&decoder) < 0) return -1; if (tDecodeI32(&decoder, &pReq->vgId) < 0) return -1; if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1; - if (tDecodeI64(&decoder, &pReq->dbUid) < 0) return -1; + if (tDecodeU64(&decoder, &pReq->dbUid) < 0) return -1; if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1; tEndDecode(&decoder); diff --git a/source/dnode/mgmt/impl/src/dndVnodes.c b/source/dnode/mgmt/impl/src/dndVnodes.c index 0a417788f9..ebb2d1b4f0 100644 --- a/source/dnode/mgmt/impl/src/dndVnodes.c +++ b/source/dnode/mgmt/impl/src/dndVnodes.c @@ -15,8 +15,8 @@ #define _DEFAULT_SOURCE #include "dndVnodes.h" -#include "dndTransport.h" #include "dndMgmt.h" +#include "dndTransport.h" typedef struct { int32_t vgId; @@ -34,9 +34,9 @@ typedef struct { int8_t dropped; int8_t accessState; uint64_t dbUid; - char * db; - char * path; - SVnode * pImpl; + char *db; + char *path; + SVnode *pImpl; STaosQueue *pWriteQ; STaosQueue *pSyncQ; STaosQueue *pApplyQ; @@ -50,7 +50,7 @@ typedef struct { int32_t failed; int32_t threadIndex; pthread_t thread; - SDnode * pDnode; + SDnode *pDnode; SWrapperCfg *pCfgs; } SVnodeThread; @@ -68,7 +68,7 @@ void dndProcessVnodeWriteMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pE void dndProcessVnodeSyncMsg(SDnode *pDnode, SRpcMsg *pMsg, SEpSet *pEpSet); static int32_t dndPutMsgIntoVnodeApplyQueue(SDnode *pDnode, int32_t vgId, SRpcMsg *pMsg); -static SVnodeObj * dndAcquireVnode(SDnode *pDnode, int32_t vgId); +static SVnodeObj *dndAcquireVnode(SDnode *pDnode, int32_t vgId); static void dndReleaseVnode(SDnode *pDnode, SVnodeObj *pVnode); static int32_t dndOpenVnode(SDnode *pDnode, SWrapperCfg *pCfg, SVnode *pImpl); static void dndCloseVnode(SDnode *pDnode, SVnodeObj *pVnode); @@ -81,7 +81,7 @@ static void dndCloseVnodes(SDnode *pDnode); static SVnodeObj *dndAcquireVnode(SDnode *pDnode, int32_t vgId) { SVnodesMgmt *pMgmt = &pDnode->vmgmt; - SVnodeObj * pVnode = NULL; + SVnodeObj *pVnode = NULL; int32_t refCount = 0; taosRLockLatch(&pMgmt->latch); @@ -112,7 +112,7 @@ static void dndReleaseVnode(SDnode *pDnode, SVnodeObj *pVnode) { static int32_t dndOpenVnode(SDnode *pDnode, SWrapperCfg *pCfg, SVnode *pImpl) { SVnodesMgmt *pMgmt = &pDnode->vmgmt; - SVnodeObj * pVnode = calloc(1, sizeof(SVnodeObj)); + SVnodeObj *pVnode = calloc(1, sizeof(SVnodeObj)); if (pVnode == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; @@ -189,7 +189,7 @@ static SVnodeObj **dndGetVnodesFromHash(SDnode *pDnode, int32_t *numOfVnodes) { void *pIter = taosHashIterate(pMgmt->hash, NULL); while (pIter) { SVnodeObj **ppVnode = pIter; - SVnodeObj * pVnode = *ppVnode; + SVnodeObj *pVnode = *ppVnode; if (pVnode && num < size) { int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1); dTrace("vgId:%d, acquire vnode, refCount:%d", pVnode->vgId, refCount); @@ -211,9 +211,9 @@ static int32_t dndGetVnodesFromFile(SDnode *pDnode, SWrapperCfg **ppCfgs, int32_ int32_t code = TSDB_CODE_DND_VNODE_READ_FILE_ERROR; int32_t len = 0; int32_t maxLen = 30000; - char * content = calloc(1, maxLen + 1); - cJSON * root = NULL; - FILE * fp = NULL; + char *content = calloc(1, maxLen + 1); + cJSON *root = NULL; + FILE *fp = NULL; char file[PATH_MAX + 20] = {0}; SWrapperCfg *pCfgs = NULL; @@ -254,7 +254,7 @@ static int32_t dndGetVnodesFromFile(SDnode *pDnode, SWrapperCfg **ppCfgs, int32_ } for (int32_t i = 0; i < vnodesNum; ++i) { - cJSON * vnode = cJSON_GetArrayItem(vnodes, i); + cJSON *vnode = cJSON_GetArrayItem(vnodes, i); SWrapperCfg *pCfg = &pCfgs[i]; cJSON *vgId = cJSON_GetObjectItem(vnode, "vgId"); @@ -326,7 +326,7 @@ static int32_t dndWriteVnodesToFile(SDnode *pDnode) { int32_t len = 0; int32_t maxLen = 65536; - char * content = calloc(1, maxLen + 1); + char *content = calloc(1, maxLen + 1); len += snprintf(content + len, maxLen - len, "{\n"); len += snprintf(content + len, maxLen - len, " \"vnodes\": [\n"); @@ -368,8 +368,8 @@ static int32_t dndWriteVnodesToFile(SDnode *pDnode) { static void *dnodeOpenVnodeFunc(void *param) { SVnodeThread *pThread = param; - SDnode * pDnode = pThread->pDnode; - SVnodesMgmt * pMgmt = &pDnode->vmgmt; + SDnode *pDnode = pThread->pDnode; + SVnodesMgmt *pMgmt = &pDnode->vmgmt; dDebug("thread:%d, start to open %d vnodes", pThread->threadIndex, pThread->vnodeNum); setThreadName("open-vnodes"); @@ -383,7 +383,7 @@ static void *dnodeOpenVnodeFunc(void *param) { dndReportStartup(pDnode, "open-vnodes", stepDesc); SVnodeCfg cfg = {.pDnode = pDnode, .pTfs = pDnode->pTfs, .vgId = pCfg->vgId, .dbId = pCfg->dbUid}; - SVnode * pImpl = vnodeOpen(pCfg->path, &cfg); + SVnode *pImpl = vnodeOpen(pCfg->path, &cfg); if (pImpl == NULL) { dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex); pThread->failed++; @@ -499,31 +499,6 @@ static void dndCloseVnodes(SDnode *pDnode) { dInfo("total vnodes:%d are all closed", numOfVnodes); } -static SCreateVnodeReq *dndParseCreateVnodeReq(SRpcMsg *pReq) { - SCreateVnodeReq *pCreate = pReq->pCont; - pCreate->vgId = htonl(pCreate->vgId); - pCreate->dnodeId = htonl(pCreate->dnodeId); - pCreate->dbUid = htobe64(pCreate->dbUid); - pCreate->vgVersion = htonl(pCreate->vgVersion); - pCreate->cacheBlockSize = htonl(pCreate->cacheBlockSize); - pCreate->totalBlocks = htonl(pCreate->totalBlocks); - pCreate->daysPerFile = htonl(pCreate->daysPerFile); - pCreate->daysToKeep0 = htonl(pCreate->daysToKeep0); - pCreate->daysToKeep1 = htonl(pCreate->daysToKeep1); - pCreate->daysToKeep2 = htonl(pCreate->daysToKeep2); - pCreate->minRows = htonl(pCreate->minRows); - pCreate->maxRows = htonl(pCreate->maxRows); - pCreate->commitTime = htonl(pCreate->commitTime); - pCreate->fsyncPeriod = htonl(pCreate->fsyncPeriod); - for (int r = 0; r < pCreate->replica; ++r) { - SReplica *pReplica = &pCreate->replicas[r]; - pReplica->id = htonl(pReplica->id); - pReplica->port = htons(pReplica->port); - } - - return pCreate; -} - static void dndGenerateVnodeCfg(SCreateVnodeReq *pCreate, SVnodeCfg *pCfg) { pCfg->vgId = pCreate->vgId; pCfg->wsize = pCreate->cacheBlockSize; @@ -557,24 +532,29 @@ static void dndGenerateWrapperCfg(SDnode *pDnode, SCreateVnodeReq *pCreate, SWra } int32_t dndProcessCreateVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SCreateVnodeReq *pCreate = dndParseCreateVnodeReq(pReq); - dDebug("vgId:%d, create vnode req is received", pCreate->vgId); - - SVnodeCfg vnodeCfg = {0}; - dndGenerateVnodeCfg(pCreate, &vnodeCfg); - - SWrapperCfg wrapperCfg = {0}; - dndGenerateWrapperCfg(pDnode, pCreate, &wrapperCfg); - - if (pCreate->dnodeId != dndGetDnodeId(pDnode)) { - terrno = TSDB_CODE_DND_VNODE_INVALID_OPTION; - dDebug("vgId:%d, failed to create vnode since %s", pCreate->vgId, terrstr()); + SCreateVnodeReq createReq = {0}; + if (tDeserializeSCreateVnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; return -1; } - SVnodeObj *pVnode = dndAcquireVnode(pDnode, pCreate->vgId); + dDebug("vgId:%d, create vnode req is received", createReq.vgId); + + SVnodeCfg vnodeCfg = {0}; + dndGenerateVnodeCfg(&createReq, &vnodeCfg); + + SWrapperCfg wrapperCfg = {0}; + dndGenerateWrapperCfg(pDnode, &createReq, &wrapperCfg); + + if (createReq.dnodeId != dndGetDnodeId(pDnode)) { + terrno = TSDB_CODE_DND_VNODE_INVALID_OPTION; + dDebug("vgId:%d, failed to create vnode since %s", createReq.vgId, terrstr()); + return -1; + } + + SVnodeObj *pVnode = dndAcquireVnode(pDnode, createReq.vgId); if (pVnode != NULL) { - dDebug("vgId:%d, already exist", pCreate->vgId); + dDebug("vgId:%d, already exist", createReq.vgId); dndReleaseVnode(pDnode, pVnode); terrno = TSDB_CODE_DND_VNODE_ALREADY_DEPLOYED; return -1; @@ -585,13 +565,13 @@ int32_t dndProcessCreateVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { vnodeCfg.dbId = wrapperCfg.dbUid; SVnode *pImpl = vnodeOpen(wrapperCfg.path, &vnodeCfg); if (pImpl == NULL) { - dError("vgId:%d, failed to create vnode since %s", pCreate->vgId, terrstr()); + dError("vgId:%d, failed to create vnode since %s", createReq.vgId, terrstr()); return -1; } int32_t code = dndOpenVnode(pDnode, &wrapperCfg, pImpl); if (code != 0) { - dError("vgId:%d, failed to open vnode since %s", pCreate->vgId, terrstr()); + dError("vgId:%d, failed to open vnode since %s", createReq.vgId, terrstr()); vnodeClose(pImpl); vnodeDestroy(wrapperCfg.path); terrno = code; @@ -610,32 +590,37 @@ int32_t dndProcessCreateVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { } int32_t dndProcessAlterVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SAlterVnodeReq *pAlter = (SAlterVnodeReq *)dndParseCreateVnodeReq(pReq); - dDebug("vgId:%d, alter vnode req is received", pAlter->vgId); - - SVnodeCfg vnodeCfg = {0}; - dndGenerateVnodeCfg(pAlter, &vnodeCfg); - - SVnodeObj *pVnode = dndAcquireVnode(pDnode, pAlter->vgId); - if (pVnode == NULL) { - dDebug("vgId:%d, failed to alter vnode since %s", pAlter->vgId, terrstr()); + SAlterVnodeReq alterReq = {0}; + if (tDeserializeSCreateVnodeReq(pReq->pCont, pReq->contLen, &alterReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; return -1; } - if (pAlter->vgVersion == pVnode->vgVersion) { + dDebug("vgId:%d, alter vnode req is received", alterReq.vgId); + + SVnodeCfg vnodeCfg = {0}; + dndGenerateVnodeCfg(&alterReq, &vnodeCfg); + + SVnodeObj *pVnode = dndAcquireVnode(pDnode, alterReq.vgId); + if (pVnode == NULL) { + dDebug("vgId:%d, failed to alter vnode since %s", alterReq.vgId, terrstr()); + return -1; + } + + if (alterReq.vgVersion == pVnode->vgVersion) { dndReleaseVnode(pDnode, pVnode); - dDebug("vgId:%d, no need to alter vnode cfg for version unchanged ", pAlter->vgId); + dDebug("vgId:%d, no need to alter vnode cfg for version unchanged ", alterReq.vgId); return 0; } if (vnodeAlter(pVnode->pImpl, &vnodeCfg) != 0) { - dError("vgId:%d, failed to alter vnode since %s", pAlter->vgId, terrstr()); + dError("vgId:%d, failed to alter vnode since %s", alterReq.vgId, terrstr()); dndReleaseVnode(pDnode, pVnode); return -1; } int32_t oldVersion = pVnode->vgVersion; - pVnode->vgVersion = pAlter->vgVersion; + pVnode->vgVersion = alterReq.vgVersion; int32_t code = dndWriteVnodesToFile(pDnode); if (code != 0) { pVnode->vgVersion = oldVersion; @@ -647,7 +632,10 @@ int32_t dndProcessAlterVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { int32_t dndProcessDropVnodeReq(SDnode *pDnode, SRpcMsg *pReq) { SDropVnodeReq dropReq = {0}; - tDeserializeSDropVnodeReq(pReq->pCont, pReq->contLen, &dropReq); + if (tDeserializeSDropVnodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } int32_t vgId = dropReq.vgId; dDebug("vgId:%d, drop vnode req is received", vgId); diff --git a/source/dnode/mgmt/impl/test/vnode/vnode.cpp b/source/dnode/mgmt/impl/test/vnode/vnode.cpp index 0e9f0bd5bf..4457faf7b1 100644 --- a/source/dnode/mgmt/impl/test/vnode/vnode.cpp +++ b/source/dnode/mgmt/impl/test/vnode/vnode.cpp @@ -27,38 +27,40 @@ Testbase DndTestVnode::test; TEST_F(DndTestVnode, 01_Create_Vnode) { for (int i = 0; i < 3; ++i) { - int32_t contLen = sizeof(SCreateVnodeReq); - - SCreateVnodeReq* pReq = (SCreateVnodeReq*)rpcMallocCont(contLen); - pReq->vgId = htonl(2); - pReq->dnodeId = htonl(1); - strcpy(pReq->db, "1.d1"); - pReq->dbUid = htobe64(9527); - pReq->vgVersion = htonl(1); - pReq->cacheBlockSize = htonl(16); - pReq->totalBlocks = htonl(10); - pReq->daysPerFile = htonl(10); - pReq->daysToKeep0 = htonl(3650); - pReq->daysToKeep1 = htonl(3650); - pReq->daysToKeep2 = htonl(3650); - pReq->minRows = htonl(100); - pReq->minRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replica = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->selfIndex = 0; - for (int r = 0; r < pReq->replica; ++r) { - SReplica* pReplica = &pReq->replicas[r]; - pReplica->id = htonl(1); - pReplica->port = htons(9527); + SCreateVnodeReq createReq = {0}; + createReq.vgId = 2; + createReq.dnodeId = 1; + strcpy(createReq.db, "1.d1"); + createReq.dbUid = 9527; + createReq.vgVersion = 1; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.minRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replica = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.selfIndex = 0; + for (int r = 0; r < createReq.replica; ++r) { + SReplica* pReplica = &createReq.replicas[r]; + pReplica->id = 1; + pReplica->port = 9527; } + int32_t contLen = tSerializeSCreateVnodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateVnodeReq(pReq, contLen, &createReq); + SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_VNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); if (i == 0) { @@ -70,38 +72,40 @@ TEST_F(DndTestVnode, 01_Create_Vnode) { } { - int32_t contLen = sizeof(SCreateVnodeReq); - - SCreateVnodeReq* pReq = (SCreateVnodeReq*)rpcMallocCont(contLen); - pReq->vgId = htonl(2); - pReq->dnodeId = htonl(3); - strcpy(pReq->db, "1.d1"); - pReq->dbUid = htobe64(9527); - pReq->vgVersion = htonl(1); - pReq->cacheBlockSize = htonl(16); - pReq->totalBlocks = htonl(10); - pReq->daysPerFile = htonl(10); - pReq->daysToKeep0 = htonl(3650); - pReq->daysToKeep1 = htonl(3650); - pReq->daysToKeep2 = htonl(3650); - pReq->minRows = htonl(100); - pReq->minRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replica = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->selfIndex = 0; - for (int r = 0; r < pReq->replica; ++r) { - SReplica* pReplica = &pReq->replicas[r]; - pReplica->id = htonl(1); - pReplica->port = htons(9527); + SCreateVnodeReq createReq = {0}; + createReq.vgId = 2; + createReq.dnodeId = 3; + strcpy(createReq.db, "1.d1"); + createReq.dbUid = 9527; + createReq.vgVersion = 1; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.minRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replica = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.selfIndex = 0; + for (int r = 0; r < createReq.replica; ++r) { + SReplica* pReplica = &createReq.replicas[r]; + pReplica->id = 1; + pReplica->port = 9527; } + int32_t contLen = tSerializeSCreateVnodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateVnodeReq(pReq, contLen, &createReq); + SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_VNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, TSDB_CODE_DND_VNODE_INVALID_OPTION); @@ -110,38 +114,40 @@ TEST_F(DndTestVnode, 01_Create_Vnode) { TEST_F(DndTestVnode, 02_Alter_Vnode) { for (int i = 0; i < 3; ++i) { - int32_t contLen = sizeof(SAlterVnodeReq); - - SAlterVnodeReq* pReq = (SAlterVnodeReq*)rpcMallocCont(contLen); - pReq->vgId = htonl(2); - pReq->dnodeId = htonl(1); - strcpy(pReq->db, "1.d1"); - pReq->dbUid = htobe64(9527); - pReq->vgVersion = htonl(2); - pReq->cacheBlockSize = htonl(16); - pReq->totalBlocks = htonl(10); - pReq->daysPerFile = htonl(10); - pReq->daysToKeep0 = htonl(3650); - pReq->daysToKeep1 = htonl(3650); - pReq->daysToKeep2 = htonl(3650); - pReq->minRows = htonl(100); - pReq->minRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replica = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->selfIndex = 0; - for (int r = 0; r < pReq->replica; ++r) { - SReplica* pReplica = &pReq->replicas[r]; - pReplica->id = htonl(1); - pReplica->port = htons(9527); + SAlterVnodeReq alterReq = {0}; + alterReq.vgId = 2; + alterReq.dnodeId = 1; + strcpy(alterReq.db, "1.d1"); + alterReq.dbUid = 9527; + alterReq.vgVersion = 2; + alterReq.cacheBlockSize = 16; + alterReq.totalBlocks = 10; + alterReq.daysPerFile = 10; + alterReq.daysToKeep0 = 3650; + alterReq.daysToKeep1 = 3650; + alterReq.daysToKeep2 = 3650; + alterReq.minRows = 100; + alterReq.minRows = 4096; + alterReq.commitTime = 3600; + alterReq.fsyncPeriod = 3000; + alterReq.walLevel = 1; + alterReq.precision = 0; + alterReq.compression = 2; + alterReq.replica = 1; + alterReq.quorum = 1; + alterReq.update = 0; + alterReq.cacheLastRow = 0; + alterReq.selfIndex = 0; + for (int r = 0; r < alterReq.replica; ++r) { + SReplica* pReplica = &alterReq.replicas[r]; + pReplica->id = 1; + pReplica->port = 9527; } + int32_t contLen = tSerializeSCreateVnodeReq(NULL, 0, &alterReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateVnodeReq(pReq, contLen, &alterReq); + SRpcMsg* pRsp = test.SendReq(TDMT_DND_ALTER_VNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); diff --git a/source/dnode/mnode/impl/inc/mndVgroup.h b/source/dnode/mnode/impl/inc/mndVgroup.h index ea9d509815..85e9a15bd4 100644 --- a/source/dnode/mnode/impl/inc/mndVgroup.h +++ b/source/dnode/mnode/impl/inc/mndVgroup.h @@ -31,8 +31,8 @@ int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups); SEpSet mndGetVgroupEpset(SMnode *pMnode, SVgObj *pVgroup); int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId); -SCreateVnodeReq *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup); -SDropVnodeReq *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen); +void *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen); +void *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index ce308e221f..e772ff326c 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -335,11 +335,12 @@ static int32_t mndSetCreateDbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - SCreateVnodeReq *pReq = mndBuildCreateVnodeReq(pMnode, pDnode, pDb, pVgroup); + int32_t contLen = 0; + void *pReq = mndBuildCreateVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); if (pReq == NULL) return -1; action.pCont = pReq; - action.contLen = sizeof(SCreateVnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_CREATE_VNODE; action.acceptableCode = TSDB_CODE_DND_VNODE_ALREADY_DEPLOYED; if (mndTransAppendRedoAction(pTrans, &action) != 0) { @@ -365,8 +366,8 @@ static int32_t mndSetCreateDbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - int32_t contLen = 0; - SDropVnodeReq *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); + int32_t contLen = 0; + void *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); if (pReq == NULL) return -1; action.pCont = pReq; @@ -578,11 +579,12 @@ static int32_t mndBuildUpdateVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - SAlterVnodeReq *pReq = (SAlterVnodeReq *)mndBuildCreateVnodeReq(pMnode, pDnode, pDb, pVgroup); + int32_t contLen = 0; + void *pReq = mndBuildCreateVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); if (pReq == NULL) return -1; action.pCont = pReq; - action.contLen = sizeof(SAlterVnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_ALTER_VNODE; if (mndTransAppendRedoAction(pTrans, &action) != 0) { free(pReq); @@ -755,8 +757,8 @@ static int32_t mndBuildDropVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj * action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - int32_t contLen = 0; - SDropVnodeReq *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); + int32_t contLen = 0; + void *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); if (pReq == NULL) return -1; action.pCont = pReq; diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index 997388c828..90d190c1be 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -189,43 +189,37 @@ void mndReleaseVgroup(SMnode *pMnode, SVgObj *pVgroup) { sdbRelease(pSdb, pVgroup); } -SCreateVnodeReq *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup) { - SCreateVnodeReq *pCreate = calloc(1, sizeof(SCreateVnodeReq)); - if (pCreate == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - pCreate->vgId = htonl(pVgroup->vgId); - pCreate->dnodeId = htonl(pDnode->id); - memcpy(pCreate->db, pDb->name, TSDB_DB_FNAME_LEN); - pCreate->dbUid = htobe64(pDb->uid); - pCreate->vgVersion = htonl(pVgroup->version); - pCreate->cacheBlockSize = htonl(pDb->cfg.cacheBlockSize); - pCreate->totalBlocks = htonl(pDb->cfg.totalBlocks); - pCreate->daysPerFile = htonl(pDb->cfg.daysPerFile); - pCreate->daysToKeep0 = htonl(pDb->cfg.daysToKeep0); - pCreate->daysToKeep1 = htonl(pDb->cfg.daysToKeep1); - pCreate->daysToKeep2 = htonl(pDb->cfg.daysToKeep2); - pCreate->minRows = htonl(pDb->cfg.minRows); - pCreate->maxRows = htonl(pDb->cfg.maxRows); - pCreate->commitTime = htonl(pDb->cfg.commitTime); - pCreate->fsyncPeriod = htonl(pDb->cfg.fsyncPeriod); - pCreate->walLevel = pDb->cfg.walLevel; - pCreate->precision = pDb->cfg.precision; - pCreate->compression = pDb->cfg.compression; - pCreate->quorum = pDb->cfg.quorum; - pCreate->update = pDb->cfg.update; - pCreate->cacheLastRow = pDb->cfg.cacheLastRow; - pCreate->replica = pVgroup->replica; - pCreate->selfIndex = -1; +void *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen) { + SCreateVnodeReq createReq = {0}; + createReq.vgId = pVgroup->vgId; + createReq.dnodeId = pDnode->id; + memcpy(createReq.db, pDb->name, TSDB_DB_FNAME_LEN); + createReq.dbUid = pDb->uid; + createReq.vgVersion = pVgroup->version; + createReq.cacheBlockSize = pDb->cfg.cacheBlockSize; + createReq.totalBlocks = pDb->cfg.totalBlocks; + createReq.daysPerFile = pDb->cfg.daysPerFile; + createReq.daysToKeep0 = pDb->cfg.daysToKeep0; + createReq.daysToKeep1 = pDb->cfg.daysToKeep1; + createReq.daysToKeep2 = pDb->cfg.daysToKeep2; + createReq.minRows = pDb->cfg.minRows; + createReq.maxRows = pDb->cfg.maxRows; + createReq.commitTime = pDb->cfg.commitTime; + createReq.fsyncPeriod = pDb->cfg.fsyncPeriod; + createReq.walLevel = pDb->cfg.walLevel; + createReq.precision = pDb->cfg.precision; + createReq.compression = pDb->cfg.compression; + createReq.quorum = pDb->cfg.quorum; + createReq.update = pDb->cfg.update; + createReq.cacheLastRow = pDb->cfg.cacheLastRow; + createReq.replica = pVgroup->replica; + createReq.selfIndex = -1; for (int32_t v = 0; v < pVgroup->replica; ++v) { - SReplica *pReplica = &pCreate->replicas[v]; + SReplica *pReplica = &createReq.replicas[v]; SVnodeGid *pVgid = &pVgroup->vnodeGid[v]; SDnodeObj *pVgidDnode = mndAcquireDnode(pMnode, pVgid->dnodeId); if (pVgidDnode == NULL) { - free(pCreate); return NULL; } @@ -235,20 +229,33 @@ SCreateVnodeReq *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbOb mndReleaseDnode(pMnode, pVgidDnode); if (pDnode->id == pVgid->dnodeId) { - pCreate->selfIndex = v; + createReq.selfIndex = v; } } - if (pCreate->selfIndex == -1) { - free(pCreate); + if (createReq.selfIndex == -1) { terrno = TSDB_CODE_MND_APP_ERROR; return NULL; } - return pCreate; + int32_t contLen = tSerializeSCreateVnodeReq(NULL, 0, &createReq); + if (contLen < 0) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + + void *pReq = malloc(contLen); + if (pReq == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + + tSerializeSCreateVnodeReq(pReq, contLen, &createReq); + *pContLen = contLen; + return pReq; } -SDropVnodeReq *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, +void *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen) { SDropVnodeReq dropReq = {0}; dropReq.dnodeId = pDnode->id; From 246311d4fcfe20f35a7b3d910393f5a76341ad58 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 15:27:19 +0800 Subject: [PATCH 06/11] minor changes --- include/common/tmsg.h | 33 ++++++------------- include/libs/parser/parsenodes.h | 2 +- source/common/src/tmsg.c | 6 ++-- source/dnode/mnode/impl/src/mndProfile.c | 3 -- source/dnode/vnode/inc/tsdb.h | 2 +- source/dnode/vnode/inc/vnode.h | 4 +-- source/dnode/vnode/src/inc/tsdbMemTable.h | 2 +- source/dnode/vnode/src/tq/tq.c | 4 +-- source/dnode/vnode/src/tq/tqRead.c | 2 +- source/dnode/vnode/src/tsdb/tsdbMemTable.c | 12 +++---- source/dnode/vnode/src/tsdb/tsdbWrite.c | 2 +- source/dnode/vnode/src/vnd/vnodeWrite.c | 2 +- source/libs/parser/src/dataBlockMgt.c | 2 +- source/libs/parser/src/insertParser.c | 2 +- source/libs/parser/test/insertParserTest.cpp | 4 +-- source/libs/scheduler/src/scheduler.c | 2 +- source/libs/scheduler/test/schedulerTests.cpp | 2 +- 17 files changed, 35 insertions(+), 51 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index cd327d61ca..015678f193 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -201,18 +201,6 @@ typedef struct SSubmitBlk { char data[]; } SSubmitBlk; -typedef struct { - /* data */ -} SSubmitReq; - -typedef struct { - /* data */ -} SSubmitRsp; - -typedef struct { - /* data */ -} SSubmitReqReader; - // Submit message for this TSDB typedef struct { SMsgHead header; @@ -220,7 +208,7 @@ typedef struct { int32_t length; int32_t numOfBlocks; char blocks[]; -} SSubmitMsg; +} SSubmitReq; typedef struct { int32_t totalLen; @@ -234,7 +222,7 @@ typedef struct { void* pMsg; } SSubmitMsgIter; -int32_t tInitSubmitMsgIter(SSubmitMsg* pMsg, SSubmitMsgIter* pIter); +int32_t tInitSubmitMsgIter(SSubmitReq* pMsg, SSubmitMsgIter* pIter); int32_t tGetSubmitMsgNext(SSubmitMsgIter* pIter, SSubmitBlk** pPBlock); int32_t tInitSubmitBlkIter(SSubmitBlk* pBlock, SSubmitBlkIter* pIter); STSRow* tGetSubmitBlkNext(SSubmitBlkIter* pIter); @@ -244,16 +232,16 @@ typedef struct { int32_t vnode; // vnode index of failed block int32_t sid; // table index of failed block int32_t code; // errorcode while write data to vnode, such as not created, dropped, no space, invalid table -} SShellSubmitRspBlock; +} SSubmitRspBlock; typedef struct { - int32_t code; // 0-success, > 0 error code - int32_t numOfRows; // number of records the client is trying to write - int32_t affectedRows; // number of records actually written - int32_t failedRows; // number of failed records (exclude duplicate records) - int32_t numOfFailedBlocks; - SShellSubmitRspBlock failedBlocks[]; -} SShellSubmitRsp; + int32_t code; // 0-success, > 0 error code + int32_t numOfRows; // number of records the client is trying to write + int32_t affectedRows; // number of records actually written + int32_t failedRows; // number of failed records (exclude duplicate records) + int32_t numOfFailedBlocks; + SSubmitRspBlock failedBlocks[]; +} SSubmitRsp; typedef struct SSchema { int8_t type; @@ -888,7 +876,6 @@ typedef struct { int8_t precision; int8_t compressed; int32_t compLen; - int32_t numOfRows; char data[]; } SRetrieveTableRsp; diff --git a/include/libs/parser/parsenodes.h b/include/libs/parser/parsenodes.h index 292b4ab2c9..6cd5feca24 100644 --- a/include/libs/parser/parsenodes.h +++ b/include/libs/parser/parsenodes.h @@ -135,7 +135,7 @@ typedef struct SVgDataBlocks { SVgroupInfo vg; int32_t numOfTables; // number of tables in current submit block uint32_t size; - char *pData; // SMsgDesc + SSubmitMsg + SSubmitBlk + ... + char *pData; // SMsgDesc + SSubmitReq + SSubmitBlk + ... } SVgDataBlocks; typedef struct SVnodeModifOpStmtInfo { diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index f6af9782ad..d450b0a6ca 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -27,7 +27,7 @@ #undef TD_MSG_SEG_CODE_ #include "tmsgdef.h" -int32_t tInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter) { +int32_t tInitSubmitMsgIter(SSubmitReq *pMsg, SSubmitMsgIter *pIter) { if (pMsg == NULL) { terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP; return -1; @@ -36,7 +36,7 @@ int32_t tInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter) { pIter->totalLen = pMsg->length; pIter->len = 0; pIter->pMsg = pMsg; - if (pMsg->length <= sizeof(SSubmitMsg)) { + if (pMsg->length <= sizeof(SSubmitReq)) { terrno = TSDB_CODE_TDB_SUBMIT_MSG_MSSED_UP; return -1; } @@ -46,7 +46,7 @@ int32_t tInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter) { int32_t tGetSubmitMsgNext(SSubmitMsgIter *pIter, SSubmitBlk **pPBlock) { if (pIter->len == 0) { - pIter->len += sizeof(SSubmitMsg); + pIter->len += sizeof(SSubmitReq); } else { SSubmitBlk *pSubmitBlk = (SSubmitBlk *)POINTER_SHIFT(pIter->pMsg, pIter->len); pIter->len += (sizeof(SSubmitBlk) + pSubmitBlk->dataLen + pSubmitBlk->schemaLen); diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 566dcff380..2b3bf6ca68 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -16,14 +16,11 @@ #define _DEFAULT_SOURCE #include "tglobal.h" #include "mndProfile.h" -//#include "mndConsumer.h" #include "mndDb.h" #include "mndStb.h" #include "mndMnode.h" #include "mndShow.h" -//#include "mndTopic.h" #include "mndUser.h" -//#include "mndVgroup.h" #define QUERY_ID_SIZE 20 #define QUERY_OBJ_ID_SIZE 18 diff --git a/source/dnode/vnode/inc/tsdb.h b/source/dnode/vnode/inc/tsdb.h index a2f935683e..7bdd8fc266 100644 --- a/source/dnode/vnode/inc/tsdb.h +++ b/source/dnode/vnode/inc/tsdb.h @@ -83,7 +83,7 @@ typedef struct { STsdb *tsdbOpen(const char *path, int32_t vgId, const STsdbCfg *pTsdbCfg, SMemAllocatorFactory *pMAF, SMeta *pMeta, STfs *pTfs); void tsdbClose(STsdb *); void tsdbRemove(const char *path); -int tsdbInsertData(STsdb *pTsdb, SSubmitMsg *pMsg, SSubmitRsp *pRsp); +int tsdbInsertData(STsdb *pTsdb, SSubmitReq *pMsg, SSubmitRsp *pRsp); int tsdbPrepareCommit(STsdb *pTsdb); int tsdbCommit(STsdb *pTsdb); diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 394076f433..eaf30da1bb 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -72,7 +72,7 @@ typedef struct { int64_t ver; int64_t tbUid; SHashObj *tbIdHash; - const SSubmitMsg *pMsg; + const SSubmitReq *pMsg; SSubmitBlk *pBlock; SSubmitMsgIter msgIter; SSubmitBlkIter blkIter; @@ -225,7 +225,7 @@ static FORCE_INLINE int tqReadHandleSetTbUidList(STqReadHandle *pHandle, const S return 0; } -void tqReadHandleSetMsg(STqReadHandle *pHandle, SSubmitMsg *pMsg, int64_t ver); +void tqReadHandleSetMsg(STqReadHandle *pHandle, SSubmitReq *pMsg, int64_t ver); bool tqNextDataBlock(STqReadHandle *pHandle); int tqRetrieveDataBlockInfo(STqReadHandle *pHandle, SDataBlockInfo *pBlockInfo); // return SArray diff --git a/source/dnode/vnode/src/inc/tsdbMemTable.h b/source/dnode/vnode/src/inc/tsdbMemTable.h index c6fbdb407c..0b9d153239 100644 --- a/source/dnode/vnode/src/inc/tsdbMemTable.h +++ b/source/dnode/vnode/src/inc/tsdbMemTable.h @@ -54,7 +54,7 @@ typedef struct STsdbMemTable { STsdbMemTable *tsdbNewMemTable(STsdb *pTsdb); void tsdbFreeMemTable(STsdb *pTsdb, STsdbMemTable *pMemTable); -int tsdbMemTableInsert(STsdb *pTsdb, STsdbMemTable *pMemTable, SSubmitMsg *pMsg, SShellSubmitRsp *pRsp); +int tsdbMemTableInsert(STsdb *pTsdb, STsdbMemTable *pMemTable, SSubmitReq *pMsg, SSubmitRsp *pRsp); int tsdbLoadDataFromCache(STable *pTable, SSkipListIterator *pIter, TSKEY maxKey, int maxRowsToRead, SDataCols *pCols, TKEY *filterKeys, int nFilterKeys, bool keepDup, SMergeInfo *pMergeInfo); diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index 252bc889f5..bbb5e56533 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -251,7 +251,7 @@ int32_t tqProcessConsumeReq(STQ* pTq, SRpcMsg* pMsg) { } pHead = pTopic->pReadhandle->pHead; if (pHead->head.msgType == TDMT_VND_SUBMIT) { - SSubmitMsg* pCont = (SSubmitMsg*)&pHead->head.body; + SSubmitReq* pCont = (SSubmitReq*)&pHead->head.body; qTaskInfo_t task = pTopic->buffer.output[pos].task; qSetStreamInput(task, pCont); SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock)); @@ -397,7 +397,7 @@ int32_t tqProcessConsumeReqV0(STQ* pTq, SRpcMsg* pMsg) { fetchOffset++; } if (skip == 1) continue; - SSubmitMsg* pCont = (SSubmitMsg*)&pHead->head.body; + SSubmitReq* pCont = (SSubmitReq*)&pHead->head.body; qTaskInfo_t task = pTopic->buffer.output[pos].task; printf("current fetch offset %ld\n", fetchOffset); diff --git a/source/dnode/vnode/src/tq/tqRead.c b/source/dnode/vnode/src/tq/tqRead.c index 9f76c6b76a..e76d43becd 100644 --- a/source/dnode/vnode/src/tq/tqRead.c +++ b/source/dnode/vnode/src/tq/tqRead.c @@ -31,7 +31,7 @@ STqReadHandle* tqInitSubmitMsgScanner(SMeta* pMeta) { return pReadHandle; } -void tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitMsg* pMsg, int64_t ver) { +void tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitReq* pMsg, int64_t ver) { pReadHandle->pMsg = pMsg; pMsg->length = htonl(pMsg->length); pMsg->numOfBlocks = htonl(pMsg->numOfBlocks); diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable.c b/source/dnode/vnode/src/tsdb/tsdbMemTable.c index b51539f697..01813af556 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable.c @@ -15,7 +15,7 @@ #include "tsdbDef.h" -static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitMsg *pMsg); +static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg); static int tsdbMemTableInsertTbData(STsdb *pRepo, SSubmitBlk *pBlock, int32_t *pAffectedRows); static STbData *tsdbNewTbData(tb_uid_t uid); static void tsdbFreeTbData(STbData *pTbData); @@ -73,7 +73,7 @@ void tsdbFreeMemTable(STsdb *pTsdb, STsdbMemTable *pMemTable) { } } -int tsdbMemTableInsert(STsdb *pTsdb, STsdbMemTable *pMemTable, SSubmitMsg *pMsg, SShellSubmitRsp *pRsp) { +int tsdbMemTableInsert(STsdb *pTsdb, STsdbMemTable *pMemTable, SSubmitReq *pMsg, SSubmitRsp *pRsp) { SSubmitBlk * pBlock = NULL; SSubmitMsgIter msgIter = {0}; int32_t affectedrows = 0, numOfRows = 0; @@ -227,7 +227,7 @@ int tsdbLoadDataFromCache(STable *pTable, SSkipListIterator *pIter, TSKEY maxKey return 0; } -static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitMsg *pMsg) { +static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg) { ASSERT(pMsg != NULL); // STsdbMeta * pMeta = pTsdb->tsdbMeta; SSubmitMsgIter msgIter = {0}; @@ -455,7 +455,7 @@ static int tsdbAppendTableRowToCols(STable *pTable, SDataCols *pCols, STSchema * /* ------------------------ REFACTORING ------------------------ */ #if 0 -int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitMsg *pMsg) { +int tsdbInsertDataToMemTable(STsdbMemTable *pMemTable, SSubmitReq *pMsg) { SMemAllocator *pMA = pMemTable->pMA; STbData * pTbData = (STbData *)TD_MA_MALLOC(pMA, sizeof(*pTbData)); if (pTbData == NULL) { @@ -496,9 +496,9 @@ static int tsdbAdjustMemMaxTables(SMemTable *pMemTable, int maxTables); static int tsdbAppendTableRowToCols(STable *pTable, SDataCols *pCols, STSchema **ppSchema, STSRow* row); static int tsdbInitSubmitBlkIter(SSubmitBlk *pBlock, SSubmitBlkIter *pIter); static STSRow* tsdbGetSubmitBlkNext(SSubmitBlkIter *pIter); -static int tsdbScanAndConvertSubmitMsg(STsdbRepo *pRepo, SSubmitMsg *pMsg); +static int tsdbScanAndConvertSubmitMsg(STsdbRepo *pRepo, SSubmitReq *pMsg); static int tsdbInsertDataToTable(STsdbRepo *pRepo, SSubmitBlk *pBlock, int32_t *affectedrows); -static int tsdbInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter); +static int tsdbInitSubmitMsgIter(SSubmitReq *pMsg, SSubmitMsgIter *pIter); static int tsdbGetSubmitMsgNext(SSubmitMsgIter *pIter, SSubmitBlk **pPBlock); static int tsdbCheckTableSchema(STsdbRepo *pRepo, SSubmitBlk *pBlock, STable *pTable); static int tsdbUpdateTableLatestInfo(STsdbRepo *pRepo, STable *pTable, STSRow* row); diff --git a/source/dnode/vnode/src/tsdb/tsdbWrite.c b/source/dnode/vnode/src/tsdb/tsdbWrite.c index 5f937f17e9..78067f8f83 100644 --- a/source/dnode/vnode/src/tsdb/tsdbWrite.c +++ b/source/dnode/vnode/src/tsdb/tsdbWrite.c @@ -15,7 +15,7 @@ #include "tsdbDef.h" -int tsdbInsertData(STsdb *pTsdb, SSubmitMsg *pMsg, SSubmitRsp *pRsp) { +int tsdbInsertData(STsdb *pTsdb, SSubmitReq *pMsg, SSubmitRsp *pRsp) { // Check if mem is there. If not, create one. if (pTsdb->mem == NULL) { pTsdb->mem = tsdbNewMemTable(pTsdb); diff --git a/source/dnode/vnode/src/vnd/vnodeWrite.c b/source/dnode/vnode/src/vnd/vnodeWrite.c index 46e60329ae..5c39c65f9f 100644 --- a/source/dnode/vnode/src/vnd/vnodeWrite.c +++ b/source/dnode/vnode/src/vnd/vnodeWrite.c @@ -109,7 +109,7 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { // } break; case TDMT_VND_SUBMIT: - if (tsdbInsertData(pVnode->pTsdb, (SSubmitMsg *)ptr, NULL) < 0) { + if (tsdbInsertData(pVnode->pTsdb, (SSubmitReq *)ptr, NULL) < 0) { // TODO: handle error } break; diff --git a/source/libs/parser/src/dataBlockMgt.c b/source/libs/parser/src/dataBlockMgt.c index eefaa2c147..381dec4f15 100644 --- a/source/libs/parser/src/dataBlockMgt.c +++ b/source/libs/parser/src/dataBlockMgt.c @@ -417,7 +417,7 @@ static int trimDataBlock(void* pDataBlock, STableDataBlocks* pTableDataBlock, SB } int32_t mergeTableDataBlocks(SHashObj* pHashObj, int8_t schemaAttached, uint8_t payloadType, SArray** pVgDataBlocks) { - const int INSERT_HEAD_SIZE = sizeof(SSubmitMsg); + const int INSERT_HEAD_SIZE = sizeof(SSubmitReq); int code = 0; bool isRawPayload = IS_RAW_PAYLOAD(payloadType); SHashObj* pVnodeDataBlockHashList = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false); diff --git a/source/libs/parser/src/insertParser.c b/source/libs/parser/src/insertParser.c index 08fe0852b1..e138e583f5 100644 --- a/source/libs/parser/src/insertParser.c +++ b/source/libs/parser/src/insertParser.c @@ -121,7 +121,7 @@ static int32_t findCol(SToken* pColname, int32_t start, int32_t end, SSchema* pS } static void buildMsgHeader(SVgDataBlocks* blocks) { - SSubmitMsg* submit = (SSubmitMsg*)blocks->pData; + SSubmitReq* submit = (SSubmitReq*)blocks->pData; submit->header.vgId = htonl(blocks->vg.vgId); submit->header.contLen = htonl(blocks->size); submit->length = submit->header.contLen; diff --git a/source/libs/parser/test/insertParserTest.cpp b/source/libs/parser/test/insertParserTest.cpp index 86e8b1d7aa..b6992e5157 100644 --- a/source/libs/parser/test/insertParserTest.cpp +++ b/source/libs/parser/test/insertParserTest.cpp @@ -70,7 +70,7 @@ protected: for (size_t i = 0; i < num; ++i) { SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(res_->pDataBlocks, i); cout << "vgId:" << vg->vg.vgId << ", numOfTables:" << vg->numOfTables << ", dataSize:" << vg->size << endl; - SSubmitMsg* submit = (SSubmitMsg*)vg->pData; + SSubmitReq* submit = (SSubmitReq*)vg->pData; cout << "length:" << ntohl(submit->length) << ", numOfBlocks:" << ntohl(submit->numOfBlocks) << endl; int32_t numOfBlocks = ntohl(submit->numOfBlocks); SSubmitBlk* blk = (SSubmitBlk*)(submit + 1); @@ -93,7 +93,7 @@ protected: SVgDataBlocks* vg = (SVgDataBlocks*)taosArrayGetP(res_->pDataBlocks, i); ASSERT_EQ(vg->numOfTables, numOfTables); ASSERT_GE(vg->size, 0); - SSubmitMsg* submit = (SSubmitMsg*)vg->pData; + SSubmitReq* submit = (SSubmitReq*)vg->pData; ASSERT_GE(ntohl(submit->length), 0); ASSERT_GE(ntohl(submit->numOfBlocks), 0); int32_t numOfBlocks = ntohl(submit->numOfBlocks); diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 1b6d871274..c14ae40b59 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -823,7 +823,7 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch SCH_ERR_RET(schProcessOnTaskFailure(pJob, pTask, rspCode)); } - SShellSubmitRsp *rsp = (SShellSubmitRsp *)msg; + SSubmitRsp *rsp = (SSubmitRsp *)msg; if (rsp) { pJob->resNumOfRows += rsp->affectedRows; } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index 2c75683ecb..6bfff0197c 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -273,7 +273,7 @@ void *schtSendRsp(void *param) { while (pIter) { SSchTask *task = *(SSchTask **)pIter; - SShellSubmitRsp rsp = {0}; + SSubmitRsp rsp = {0}; rsp.affectedRows = 10; schHandleResponseMsg(job, task, TDMT_VND_SUBMIT_RSP, (char *)&rsp, sizeof(rsp), 0); From 2011914dd66552f3641c95d639bc27afb099a160 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 15:44:20 +0800 Subject: [PATCH 07/11] serialze kill req --- include/common/tmsg.h | 7 +++- source/common/src/tmsg.c | 52 ++++++++++++++++++++++++ source/dnode/mnode/impl/src/mndProfile.c | 47 +++++++++++---------- 3 files changed, 84 insertions(+), 22 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 015678f193..4cd36acefa 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -964,10 +964,16 @@ typedef struct { int32_t queryId; } SKillQueryReq; +int32_t tSerializeSKillQueryReq(void* buf, int32_t bufLen, SKillQueryReq* pReq); +int32_t tDeserializeSKillQueryReq(void* buf, int32_t bufLen, SKillQueryReq* pReq); + typedef struct { int32_t connId; } SKillConnReq; +int32_t tSerializeSKillConnReq(void* buf, int32_t bufLen, SKillConnReq* pReq); +int32_t tDeserializeSKillConnReq(void* buf, int32_t bufLen, SKillConnReq* pReq); + typedef struct { char user[TSDB_USER_LEN]; char spi; @@ -1365,7 +1371,6 @@ typedef struct { int8_t precision; int8_t compressed; int32_t compLen; - int32_t numOfRows; char data[]; } SVShowTablesFetchRsp; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index d450b0a6ca..7b9682df14 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2128,6 +2128,58 @@ int32_t tDeserializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1; tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; +} + +int32_t tSerializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->connId) < 0) return -1; + if (tEncodeI32(&encoder, pReq->queryId) < 0) return -1; + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSKillQueryReq(void *buf, int32_t bufLen, SKillQueryReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->connId) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->queryId) < 0) return -1; + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} + +int32_t tSerializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->connId) < 0) return -1; + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->connId) < 0) return -1; + tEndDecode(&decoder); + tCoderClear(&decoder); return 0; } \ 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 2b3bf6ca68..3504c6ced7 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -14,13 +14,13 @@ */ #define _DEFAULT_SOURCE -#include "tglobal.h" #include "mndProfile.h" #include "mndDb.h" -#include "mndStb.h" #include "mndMnode.h" #include "mndShow.h" +#include "mndStb.h" #include "mndUser.h" +#include "tglobal.h" #define QUERY_ID_SIZE 20 #define QUERY_OBJ_ID_SIZE 18 @@ -276,7 +276,7 @@ static int32_t mndSaveQueryStreamList(SConnObj *pConn, SHeartBeatReq *pReq) { return TSDB_CODE_SUCCESS; } -static SClientHbRsp* mndMqHbBuildRsp(SMnode* pMnode, SClientHbReq* pReq) { +static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) { #if 0 SClientHbRsp* pRsp = malloc(sizeof(SClientHbRsp)); if (pRsp == NULL) { @@ -350,14 +350,13 @@ static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { return -1; } - SArray *pArray = batchReq.reqs; - int32_t sz = taosArrayGetSize(pArray); - + SClientHbBatchRsp batchRsp = {0}; batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp)); + int32_t sz = taosArrayGetSize(batchReq.reqs); for (int i = 0; i < sz; i++) { - SClientHbReq *pHbReq = taosArrayGet(pArray, i); + SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i); if (pHbReq->connKey.hbType == HEARTBEAT_TYPE_QUERY) { int32_t kvNum = taosHashGetSize(pHbReq->info); if (NULL == pHbReq->info || kvNum <= 0) { @@ -409,7 +408,7 @@ static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { } } } - taosArrayDestroyEx(pArray, tFreeClientHbReq); + taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq); int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp); void *buf = rpcMallocCont(tlen); @@ -517,19 +516,22 @@ static int32_t mndProcessKillQueryReq(SMnodeMsg *pReq) { } mndReleaseUser(pMnode, pUser); - SKillQueryReq *pKill = pReq->rpcMsg.pCont; - int32_t connId = htonl(pKill->connId); - int32_t queryId = htonl(pKill->queryId); - mInfo("kill query msg is received, queryId:%d", pKill->queryId); + SKillQueryReq killReq = {0}; + if (tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &connId, sizeof(int32_t)); + mInfo("kill query msg is received, queryId:%d", killReq.queryId); + + SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &killReq.connId, sizeof(int32_t)); if (pConn == NULL) { - mError("connId:%d, failed to kill queryId:%d, conn not exist", connId, queryId); + mError("connId:%d, failed to kill queryId:%d, conn not exist", killReq.connId, killReq.queryId); terrno = TSDB_CODE_MND_INVALID_CONN_ID; return -1; } else { - mInfo("connId:%d, queryId:%d is killed by user:%s", connId, queryId, pReq->user); - pConn->queryId = queryId; + mInfo("connId:%d, queryId:%d is killed by user:%s", killReq.connId, killReq.queryId, pReq->user); + pConn->queryId = killReq.queryId; taosCacheRelease(pMgmt->cache, (void **)&pConn, false); return 0; } @@ -548,16 +550,19 @@ static int32_t mndProcessKillConnReq(SMnodeMsg *pReq) { } mndReleaseUser(pMnode, pUser); - SKillConnReq *pKill = pReq->rpcMsg.pCont; - int32_t connId = htonl(pKill->connId); + SKillConnReq killReq = {0}; + if (tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &connId, sizeof(int32_t)); + SConnObj *pConn = taosCacheAcquireByKey(pMgmt->cache, &killReq.connId, sizeof(int32_t)); if (pConn == NULL) { - mError("connId:%d, failed to kill connection, conn not exist", connId); + mError("connId:%d, failed to kill connection, conn not exist", killReq.connId); terrno = TSDB_CODE_MND_INVALID_CONN_ID; return -1; } else { - mInfo("connId:%d, is killed by user:%s", connId, pReq->user); + mInfo("connId:%d, is killed by user:%s", killReq.connId, pReq->user); pConn->killed = 1; taosCacheRelease(pMgmt->cache, (void **)&pConn, false); return TSDB_CODE_SUCCESS; From bec7a0d533e8e164f1c907c0ee64b74810f5ad61 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 16:36:05 +0800 Subject: [PATCH 08/11] serialize mnode msg --- include/common/tmsg.h | 7 +- source/common/src/tmsg.c | 92 ++++++++++- source/dnode/mgmt/impl/src/dndMnode.c | 39 ++--- source/dnode/mgmt/impl/src/dndTransport.c | 11 +- source/dnode/mgmt/impl/test/mnode/dmnode.cpp | 165 +++++++++++-------- source/dnode/mnode/impl/src/mndAuth.c | 22 ++- source/dnode/mnode/impl/src/mndMnode.c | 63 +++---- 7 files changed, 254 insertions(+), 145 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 4cd36acefa..035a767b87 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -917,6 +917,9 @@ typedef struct { SReplica replicas[TSDB_MAX_REPLICA]; } SDCreateMnodeReq, SDAlterMnodeReq; +int32_t tSerializeSDCreateMnodeReq(void* buf, int32_t bufLen, SDCreateMnodeReq* pReq); +int32_t tDeserializeSDCreateMnodeReq(void* buf, int32_t bufLen, SDCreateMnodeReq* pReq); + typedef struct { int32_t dnodeId; } SMCreateQnodeReq, SMDropQnodeReq, SDCreateQnodeReq, SDDropQnodeReq, SMCreateSnodeReq, SMDropSnodeReq, @@ -982,9 +985,11 @@ typedef struct { char ckey[TSDB_PASSWORD_LEN]; } SAuthReq, SAuthRsp; +int32_t tSerializeSAuthReq(void* buf, int32_t bufLen, SAuthReq* pReq); +int32_t tDeserializeSAuthReq(void* buf, int32_t bufLen, SAuthReq* pReq); + typedef struct { int8_t finished; - int8_t align[7]; char name[TSDB_STEP_NAME_LEN]; char desc[TSDB_STEP_DESC_LEN]; } SStartupReq; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 7b9682df14..611416499f 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2020,6 +2020,20 @@ int32_t tDeserializeSMTimerMsg(void *buf, int32_t bufLen, SMTimerReq *pReq) { return 0; } +int32_t tEncodeSReplica(SCoder *pEncoder, SReplica *pReplica) { + if (tEncodeI32(pEncoder, pReplica->id) < 0) return -1; + if (tEncodeU16(pEncoder, pReplica->port) < 0) return -1; + if (tEncodeCStr(pEncoder, pReplica->fqdn) < 0) return -1; + return 0; +} + +int32_t tDecodeSReplica(SCoder *pDecoder, SReplica *pReplica) { + if (tDecodeI32(pDecoder, &pReplica->id) < 0) return -1; + if (tDecodeU16(pDecoder, &pReplica->port) < 0) return -1; + if (tDecodeCStrTo(pDecoder, pReplica->fqdn) < 0) return -1; + return 0; +} + int32_t tSerializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pReq) { SCoder encoder = {0}; tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); @@ -2050,9 +2064,7 @@ int32_t tSerializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq *pR if (tEncodeI8(&encoder, pReq->selfIndex) < 0) return -1; for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) { SReplica *pReplica = &pReq->replicas[i]; - if (tEncodeI32(&encoder, pReplica->id) < 0) return -1; - if (tEncodeU16(&encoder, pReplica->port) < 0) return -1; - if (tEncodeCStr(&encoder, pReplica->fqdn) < 0) return -1; + if (tEncodeSReplica(&encoder, pReplica) < 0) return -1; } tEndEncode(&encoder); @@ -2091,9 +2103,7 @@ int32_t tDeserializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq * if (tDecodeI8(&decoder, &pReq->selfIndex) < 0) return -1; for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) { SReplica *pReplica = &pReq->replicas[i]; - if (tDecodeI32(&decoder, &pReplica->id) < 0) return -1; - if (tDecodeU16(&decoder, &pReplica->port) < 0) return -1; - if (tDecodeCStrTo(&decoder, pReplica->fqdn) < 0) return -1; + if (tDecodeSReplica(&decoder, pReplica) < 0) return -1; } tEndDecode(&decoder); @@ -2182,4 +2192,72 @@ int32_t tDeserializeSKillConnReq(void *buf, int32_t bufLen, SKillConnReq *pReq) tCoderClear(&decoder); return 0; -} \ No newline at end of file +} + +int32_t tSerializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->dnodeId) < 0) return -1; + if (tEncodeI8(&encoder, pReq->replica) < 0) return -1; + for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) { + SReplica *pReplica = &pReq->replicas[i]; + if (tEncodeSReplica(&encoder, pReplica) < 0) return -1; + } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSDCreateMnodeReq(void *buf, int32_t bufLen, SDCreateMnodeReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->dnodeId) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->replica) < 0) return -1; + for (int32_t i = 0; i < TSDB_MAX_REPLICA; ++i) { + SReplica *pReplica = &pReq->replicas[i]; + if (tDecodeSReplica(&decoder, pReplica) < 0) return -1; + } + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} + +int32_t tSerializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->user) < 0) return -1; + if (tEncodeI8(&encoder, pReq->spi) < 0) return -1; + if (tEncodeI8(&encoder, pReq->encrypt) < 0) return -1; + if (tEncodeBinary(&encoder, pReq->secret, TSDB_PASSWORD_LEN) < 0) return -1; + if (tEncodeBinary(&encoder, pReq->ckey, TSDB_PASSWORD_LEN) < 0) return -1; + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSAuthReq(void *buf, int32_t bufLen, SAuthReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->spi) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->encrypt) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->secret) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->ckey) < 0) return -1; + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} diff --git a/source/dnode/mgmt/impl/src/dndMnode.c b/source/dnode/mgmt/impl/src/dndMnode.c index 0874c633bf..c6db75c057 100644 --- a/source/dnode/mgmt/impl/src/dndMnode.c +++ b/source/dnode/mgmt/impl/src/dndMnode.c @@ -424,28 +424,22 @@ static int32_t dndDropMnode(SDnode *pDnode) { return 0; } -static SDCreateMnodeReq *dndParseCreateMnodeReq(SRpcMsg *pReq) { - SDCreateMnodeReq *pCreate = pReq->pCont; - pCreate->dnodeId = htonl(pCreate->dnodeId); - for (int32_t i = 0; i < pCreate->replica; ++i) { - pCreate->replicas[i].id = htonl(pCreate->replicas[i].id); - pCreate->replicas[i].port = htons(pCreate->replicas[i].port); - } - - return pCreate; -} int32_t dndProcessCreateMnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SDCreateMnodeReq *pCreate = dndParseCreateMnodeReq(pReq); + SDCreateMnodeReq createReq = {0}; + if (tDeserializeSDCreateMnodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pCreate->replica <= 1 || pCreate->dnodeId != dndGetDnodeId(pDnode)) { + if (createReq.replica <= 1 || createReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_MNODE_INVALID_OPTION; dError("failed to create mnode since %s", terrstr()); return -1; } SMnodeOpt option = {0}; - if (dndBuildMnodeOptionFromReq(pDnode, &option, pCreate) != 0) { + if (dndBuildMnodeOptionFromReq(pDnode, &option, &createReq) != 0) { terrno = TSDB_CODE_DND_MNODE_INVALID_OPTION; dError("failed to create mnode since %s", terrstr()); return -1; @@ -464,16 +458,20 @@ int32_t dndProcessCreateMnodeReq(SDnode *pDnode, SRpcMsg *pReq) { } int32_t dndProcessAlterMnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SDAlterMnodeReq *pAlter = dndParseCreateMnodeReq(pReq); + SDAlterMnodeReq alterReq = {0}; + if (tDeserializeSDCreateMnodeReq(pReq->pCont, pReq->contLen, &alterReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pAlter->dnodeId != dndGetDnodeId(pDnode)) { + if (alterReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_MNODE_INVALID_OPTION; dError("failed to alter mnode since %s", terrstr()); return -1; } SMnodeOpt option = {0}; - if (dndBuildMnodeOptionFromReq(pDnode, &option, pAlter) != 0) { + if (dndBuildMnodeOptionFromReq(pDnode, &option, &alterReq) != 0) { terrno = TSDB_CODE_DND_MNODE_INVALID_OPTION; dError("failed to alter mnode since %s", terrstr()); return -1; @@ -494,10 +492,13 @@ int32_t dndProcessAlterMnodeReq(SDnode *pDnode, SRpcMsg *pReq) { } int32_t dndProcessDropMnodeReq(SDnode *pDnode, SRpcMsg *pReq) { - SDDropMnodeReq *pDrop = pReq->pCont; - pDrop->dnodeId = htonl(pDrop->dnodeId); + SDDropMnodeReq dropReq = {0}; + if (tDeserializeSMCreateDropMnodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pDrop->dnodeId != dndGetDnodeId(pDnode)) { + if (dropReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_MNODE_INVALID_OPTION; dError("failed to drop mnode since %s", terrstr()); return -1; diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index e65e62070e..d2b4b50598 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -308,12 +308,15 @@ static int32_t dndRetrieveUserAuthInfo(void *parent, char *user, char *spi, char return -1; } - SAuthReq *pReq = rpcMallocCont(sizeof(SAuthReq)); - tstrncpy(pReq->user, user, TSDB_USER_LEN); + SAuthReq authReq = {0}; + tstrncpy(authReq.user, user, TSDB_USER_LEN); + int32_t contLen = tSerializeSAuthReq(NULL, 0, &authReq); + void *pReq = rpcMallocCont(contLen); + tSerializeSAuthReq(pReq, contLen, &authReq); - SRpcMsg rpcMsg = {.pCont = pReq, .contLen = sizeof(SAuthReq), .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528}; + SRpcMsg rpcMsg = {.pCont = pReq, .contLen = contLen, .msgType = TDMT_MND_AUTH, .ahandle = (void *)9528}; SRpcMsg rpcRsp = {0}; - dTrace("user:%s, send user auth req to other mnodes, spi:%d encrypt:%d", user, pReq->spi, pReq->encrypt); + dTrace("user:%s, send user auth req to other mnodes, spi:%d encrypt:%d", user, authReq.spi, authReq.encrypt); dndSendMsgToMnodeRecv(pDnode, &rpcMsg, &rpcRsp); if (rpcRsp.code != 0) { diff --git a/source/dnode/mgmt/impl/test/mnode/dmnode.cpp b/source/dnode/mgmt/impl/test/mnode/dmnode.cpp index edaecef49e..8655bcb774 100644 --- a/source/dnode/mgmt/impl/test/mnode/dmnode.cpp +++ b/source/dnode/mgmt/impl/test/mnode/dmnode.cpp @@ -16,25 +16,27 @@ class DndTestMnode : public ::testing::Test { static void SetUpTestSuite() { test.Init("/tmp/dnode_test_mnode", 9114); } static void TearDownTestSuite() { test.Cleanup(); } - static Testbase test; + static Testbase test; public: void SetUp() override {} void TearDown() override {} }; -Testbase DndTestMnode::test; +Testbase DndTestMnode::test; TEST_F(DndTestMnode, 01_Create_Mnode) { { - int32_t contLen = sizeof(SDCreateMnodeReq); + SDCreateMnodeReq createReq = {0}; + createReq.dnodeId = 2; + createReq.replica = 1; + createReq.replicas[0].id = 1; + createReq.replicas[0].port = 9113; + strcpy(createReq.replicas[0].fqdn, "localhost"); - SDCreateMnodeReq* pReq = (SDCreateMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); - pReq->replica = 1; - pReq->replicas[0].id = htonl(1); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -42,14 +44,16 @@ TEST_F(DndTestMnode, 01_Create_Mnode) { } { - int32_t contLen = sizeof(SDCreateMnodeReq); + SDCreateMnodeReq createReq = {0}; + createReq.dnodeId = 1; + createReq.replica = 1; + createReq.replicas[0].id = 2; + createReq.replicas[0].port = 9113; + strcpy(createReq.replicas[0].fqdn, "localhost"); - SDCreateMnodeReq* pReq = (SDCreateMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); - pReq->replica = 1; - pReq->replicas[0].id = htonl(2); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -57,17 +61,19 @@ TEST_F(DndTestMnode, 01_Create_Mnode) { } { - int32_t contLen = sizeof(SDCreateMnodeReq); + SDCreateMnodeReq createReq = {0}; + createReq.dnodeId = 1; + createReq.replica = 2; + createReq.replicas[0].id = 1; + createReq.replicas[0].port = 9113; + strcpy(createReq.replicas[0].fqdn, "localhost"); + createReq.replicas[1].id = 1; + createReq.replicas[1].port = 9114; + strcpy(createReq.replicas[1].fqdn, "localhost"); - SDCreateMnodeReq* pReq = (SDCreateMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); - pReq->replica = 2; - pReq->replicas[0].id = htonl(1); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); - pReq->replicas[1].id = htonl(1); - pReq->replicas[1].port = htonl(9114); - strcpy(pReq->replicas[1].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -76,15 +82,17 @@ TEST_F(DndTestMnode, 01_Create_Mnode) { } TEST_F(DndTestMnode, 02_Alter_Mnode) { - { - int32_t contLen = sizeof(SDAlterMnodeReq); + { + SDAlterMnodeReq alterReq = {0}; + alterReq.dnodeId = 2; + alterReq.replica = 1; + alterReq.replicas[0].id = 1; + alterReq.replicas[0].port = 9113; + strcpy(alterReq.replicas[0].fqdn, "localhost"); - SDAlterMnodeReq* pReq = (SDAlterMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); - pReq->replica = 1; - pReq->replicas[0].id = htonl(1); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_ALTER_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -92,14 +100,16 @@ TEST_F(DndTestMnode, 02_Alter_Mnode) { } { - int32_t contLen = sizeof(SDAlterMnodeReq); + SDAlterMnodeReq alterReq = {0}; + alterReq.dnodeId = 1; + alterReq.replica = 1; + alterReq.replicas[0].id = 2; + alterReq.replicas[0].port = 9113; + strcpy(alterReq.replicas[0].fqdn, "localhost"); - SDAlterMnodeReq* pReq = (SDAlterMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); - pReq->replica = 1; - pReq->replicas[0].id = htonl(2); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_ALTER_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -107,14 +117,16 @@ TEST_F(DndTestMnode, 02_Alter_Mnode) { } { - int32_t contLen = sizeof(SDAlterMnodeReq); + SDAlterMnodeReq alterReq = {0}; + alterReq.dnodeId = 1; + alterReq.replica = 1; + alterReq.replicas[0].id = 1; + alterReq.replicas[0].port = 9113; + strcpy(alterReq.replicas[0].fqdn, "localhost"); - SDAlterMnodeReq* pReq = (SDAlterMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); - pReq->replica = 1; - pReq->replicas[0].id = htonl(1); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_ALTER_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -124,10 +136,12 @@ TEST_F(DndTestMnode, 02_Alter_Mnode) { TEST_F(DndTestMnode, 03_Drop_Mnode) { { - int32_t contLen = sizeof(SDDropMnodeReq); + SDDropMnodeReq dropReq = {0}; + dropReq.dnodeId = 2; - SDDropMnodeReq* pReq = (SDDropMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropMnodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropMnodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -135,10 +149,12 @@ TEST_F(DndTestMnode, 03_Drop_Mnode) { } { - int32_t contLen = sizeof(SDDropMnodeReq); + SDDropMnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropMnodeReq* pReq = (SDDropMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropMnodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropMnodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -146,10 +162,12 @@ TEST_F(DndTestMnode, 03_Drop_Mnode) { } { - int32_t contLen = sizeof(SDDropMnodeReq); + SDDropMnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropMnodeReq* pReq = (SDDropMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropMnodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropMnodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -157,30 +175,33 @@ TEST_F(DndTestMnode, 03_Drop_Mnode) { } { - int32_t contLen = sizeof(SDAlterMnodeReq); + SDAlterMnodeReq alterReq = {0}; + alterReq.dnodeId = 1; + alterReq.replica = 1; + alterReq.replicas[0].id = 1; + alterReq.replicas[0].port = 9113; + strcpy(alterReq.replicas[0].fqdn, "localhost"); - SDAlterMnodeReq* pReq = (SDAlterMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); - pReq->replica = 1; - pReq->replicas[0].id = htonl(1); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_ALTER_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, TSDB_CODE_DND_MNODE_NOT_DEPLOYED); } - { - int32_t contLen = sizeof(SDCreateMnodeReq); + SDCreateMnodeReq createReq = {0}; + createReq.dnodeId = 1; + createReq.replica = 2; + createReq.replicas[0].id = 1; + createReq.replicas[0].port = 9113; + strcpy(createReq.replicas[0].fqdn, "localhost"); - SDCreateMnodeReq* pReq = (SDCreateMnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); - pReq->replica = 2; - pReq->replicas[0].id = htonl(1); - pReq->replicas[0].port = htonl(9113); - strcpy(pReq->replicas[0].fqdn, "localhost"); + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_MNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/dnode/mnode/impl/src/mndAuth.c b/source/dnode/mnode/impl/src/mndAuth.c index afebb066b3..3d70dd3717 100644 --- a/source/dnode/mnode/impl/src/mndAuth.c +++ b/source/dnode/mnode/impl/src/mndAuth.c @@ -46,15 +46,25 @@ int32_t mndRetriveAuth(SMnode *pMnode, char *user, char *spi, char *encrypt, cha } static int32_t mndProcessAuthReq(SMnodeMsg *pReq) { - SAuthReq *pAuth = pReq->rpcMsg.pCont; + SAuthReq authReq = {0}; + if (tDeserializeSAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - int32_t contLen = sizeof(SAuthRsp); - SAuthRsp *pRsp = rpcMallocCont(contLen); + SAuthReq authRsp = {0}; + memcpy(authRsp.user, authReq.user, TSDB_USER_LEN); + + int32_t code = + mndRetriveAuth(pReq->pMnode, authRsp.user, &authRsp.spi, &authRsp.encrypt, authRsp.secret, authRsp.ckey); + mTrace("user:%s, auth req received, spi:%d encrypt:%d ruser:%s", pReq->user, authRsp.spi, authRsp.encrypt, + authRsp.user); + + int32_t contLen = tSerializeSAuthReq(NULL, 0, &authRsp); + void *pRsp = rpcMallocCont(contLen); + tSerializeSAuthReq(pRsp, contLen, &authRsp); pReq->pCont = pRsp; pReq->contLen = contLen; - - int32_t code = mndRetriveAuth(pReq->pMnode, pAuth->user, &pRsp->spi, &pRsp->encrypt, pRsp->secret, pRsp->ckey); - mTrace("user:%s, auth req received, spi:%d encrypt:%d ruser:%s", pReq->user, pAuth->spi, pAuth->encrypt, pAuth->user); return code; } diff --git a/source/dnode/mnode/impl/src/mndMnode.c b/source/dnode/mnode/impl/src/mndMnode.c index 1ba9f77b45..720dc1b535 100644 --- a/source/dnode/mnode/impl/src/mndMnode.c +++ b/source/dnode/mnode/impl/src/mndMnode.c @@ -284,8 +284,8 @@ static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDno if (pIter == NULL) break; SReplica *pReplica = &createReq.replicas[numOfReplicas]; - pReplica->id = htonl(pMObj->id); - pReplica->port = htons(pMObj->pDnode->port); + pReplica->id = pMObj->id; + pReplica->port = pMObj->pDnode->port; memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN); numOfReplicas++; @@ -293,8 +293,8 @@ static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDno } SReplica *pReplica = &createReq.replicas[numOfReplicas]; - pReplica->id = htonl(pDnode->id); - pReplica->port = htons(pDnode->port); + pReplica->id = pDnode->id; + pReplica->port = pDnode->port; memcpy(pReplica->fqdn, pDnode->fqdn, TSDB_FQDN_LEN); numOfReplicas++; @@ -307,18 +307,14 @@ static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDno STransAction action = {0}; - SDAlterMnodeReq *pReq = malloc(sizeof(SDAlterMnodeReq)); - if (pReq == NULL) { - sdbCancelFetch(pSdb, pIter); - sdbRelease(pSdb, pMObj); - return -1; - } - memcpy(pReq, &createReq, sizeof(SDAlterMnodeReq)); + createReq.dnodeId = pMObj->id; + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq); + void *pReq = malloc(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &createReq); - pReq->dnodeId = htonl(pMObj->id); action.epSet = mndGetDnodeEpset(pMObj->pDnode); action.pCont = pReq; - action.contLen = sizeof(SDAlterMnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_ALTER_MNODE; action.acceptableCode = TSDB_CODE_DND_MNODE_ALREADY_DEPLOYED; @@ -336,14 +332,14 @@ static int32_t mndSetCreateMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDno STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); - SDCreateMnodeReq *pReq = malloc(sizeof(SDCreateMnodeReq)); - if (pReq == NULL) return -1; - memcpy(pReq, &createReq, sizeof(SDAlterMnodeReq)); - pReq->dnodeId = htonl(pObj->id); + createReq.dnodeId = pObj->id; + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &createReq); + void *pReq = malloc(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &createReq); action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDCreateMnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_CREATE_MNODE; action.acceptableCode = TSDB_CODE_DND_MNODE_ALREADY_DEPLOYED; if (mndTransAppendRedoAction(pTrans, &action) != 0) { @@ -463,8 +459,8 @@ static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnode if (pMObj->id != pObj->id) { SReplica *pReplica = &alterReq.replicas[numOfReplicas]; - pReplica->id = htonl(pMObj->id); - pReplica->port = htons(pMObj->pDnode->port); + pReplica->id = pMObj->id; + pReplica->port = pMObj->pDnode->port; memcpy(pReplica->fqdn, pMObj->pDnode->fqdn, TSDB_FQDN_LEN); numOfReplicas++; } @@ -481,18 +477,14 @@ static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnode if (pMObj->id != pObj->id) { STransAction action = {0}; - SDAlterMnodeReq *pReq = malloc(sizeof(SDAlterMnodeReq)); - if (pReq == NULL) { - sdbCancelFetch(pSdb, pIter); - sdbRelease(pSdb, pMObj); - return -1; - } - memcpy(pReq, &alterReq, sizeof(SDAlterMnodeReq)); + alterReq.dnodeId = pMObj->id; + int32_t contLen = tSerializeSDCreateMnodeReq(NULL, 0, &alterReq); + void *pReq = malloc(contLen); + tSerializeSDCreateMnodeReq(pReq, contLen, &alterReq); - pReq->dnodeId = htonl(pMObj->id); action.epSet = mndGetDnodeEpset(pMObj->pDnode); action.pCont = pReq; - action.contLen = sizeof(SDAlterMnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_ALTER_MNODE; action.acceptableCode = TSDB_CODE_DND_MNODE_ALREADY_DEPLOYED; @@ -511,16 +503,15 @@ static int32_t mndSetDropMnodeRedoActions(SMnode *pMnode, STrans *pTrans, SDnode STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); - SDDropMnodeReq *pReq = malloc(sizeof(SDDropMnodeReq)); - if (pReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; - } - pReq->dnodeId = htonl(pObj->id); + SDDropMnodeReq dropReq = {0}; + dropReq.dnodeId = pObj->id; + int32_t contLen = tSerializeSMCreateDropMnodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); + tSerializeSMCreateDropMnodeReq(pReq, contLen, &dropReq); action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropMnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_MNODE; action.acceptableCode = TSDB_CODE_DND_MNODE_NOT_DEPLOYED; if (mndTransAppendRedoAction(pTrans, &action) != 0) { From f0e697479fe8a4a57c6a3da9de3d6664cf7ae758 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 17:33:07 +0800 Subject: [PATCH 09/11] serialize node msg --- source/client/src/clientHb.c | 2 +- source/dnode/mgmt/impl/src/dndBnode.c | 22 ++++-- source/dnode/mgmt/impl/src/dndQnode.c | 22 ++++-- source/dnode/mgmt/impl/src/dndSnode.c | 22 ++++-- source/dnode/mgmt/impl/test/bnode/dbnode.cpp | 73 ++++++++++++------- source/dnode/mgmt/impl/test/qnode/dqnode.cpp | 72 +++++++++++------- source/dnode/mgmt/impl/test/snode/dsnode.cpp | 72 +++++++++++------- source/dnode/mnode/impl/src/mndAuth.c | 2 +- source/dnode/mnode/impl/src/mndBnode.c | 30 +++++--- source/dnode/mnode/impl/src/mndProfile.c | 4 +- source/dnode/mnode/impl/src/mndQnode.c | 30 +++++--- source/dnode/mnode/impl/src/mndSnode.c | 32 +++++--- .../dnode/mnode/impl/test/profile/profile.cpp | 36 +++++---- 13 files changed, 267 insertions(+), 152 deletions(-) diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index f70a1ab126..73a09f473e 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -315,7 +315,7 @@ void hbFreeReq(void *req) { SClientHbBatchReq* hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { - SClientHbBatchReq* pBatchReq = malloc(sizeof(SClientHbBatchReq)); + SClientHbBatchReq* pBatchReq = calloc(1, sizeof(SClientHbBatchReq)); if (pBatchReq == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; return NULL; diff --git a/source/dnode/mgmt/impl/src/dndBnode.c b/source/dnode/mgmt/impl/src/dndBnode.c index f26ec72f1d..e37a164660 100644 --- a/source/dnode/mgmt/impl/src/dndBnode.c +++ b/source/dnode/mgmt/impl/src/dndBnode.c @@ -258,11 +258,14 @@ static int32_t dndDropBnode(SDnode *pDnode) { return 0; } -int32_t dndProcessCreateBnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { - SDCreateBnodeReq *pMsg = pRpcMsg->pCont; - pMsg->dnodeId = htonl(pMsg->dnodeId); +int32_t dndProcessCreateBnodeReq(SDnode *pDnode, SRpcMsg *pReq) { + SDCreateBnodeReq createReq = {0}; + if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pMsg->dnodeId != dndGetDnodeId(pDnode)) { + if (createReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_BNODE_INVALID_OPTION; dError("failed to create bnode since %s", terrstr()); return -1; @@ -271,11 +274,14 @@ int32_t dndProcessCreateBnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { } } -int32_t dndProcessDropBnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { - SDDropBnodeReq *pMsg = pRpcMsg->pCont; - pMsg->dnodeId = htonl(pMsg->dnodeId); +int32_t dndProcessDropBnodeReq(SDnode *pDnode, SRpcMsg *pReq) { + SDDropBnodeReq dropReq = {0}; + if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pMsg->dnodeId != dndGetDnodeId(pDnode)) { + if (dropReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_BNODE_INVALID_OPTION; dError("failed to drop bnode since %s", terrstr()); return -1; diff --git a/source/dnode/mgmt/impl/src/dndQnode.c b/source/dnode/mgmt/impl/src/dndQnode.c index fa53375381..64545ec09f 100644 --- a/source/dnode/mgmt/impl/src/dndQnode.c +++ b/source/dnode/mgmt/impl/src/dndQnode.c @@ -264,11 +264,14 @@ static int32_t dndDropQnode(SDnode *pDnode) { return 0; } -int32_t dndProcessCreateQnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { - SDCreateQnodeReq *pMsg = pRpcMsg->pCont; - pMsg->dnodeId = htonl(pMsg->dnodeId); +int32_t dndProcessCreateQnodeReq(SDnode *pDnode, SRpcMsg *pReq) { + SDCreateQnodeReq createReq = {0}; + if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pMsg->dnodeId != dndGetDnodeId(pDnode)) { + if (createReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_QNODE_INVALID_OPTION; dError("failed to create qnode since %s", terrstr()); return -1; @@ -277,11 +280,14 @@ int32_t dndProcessCreateQnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { } } -int32_t dndProcessDropQnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { - SDDropQnodeReq *pMsg = pRpcMsg->pCont; - pMsg->dnodeId = htonl(pMsg->dnodeId); +int32_t dndProcessDropQnodeReq(SDnode *pDnode, SRpcMsg *pReq) { + SDDropQnodeReq dropReq = {0}; + if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pMsg->dnodeId != dndGetDnodeId(pDnode)) { + if (dropReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_QNODE_INVALID_OPTION; dError("failed to drop qnode since %s", terrstr()); return -1; diff --git a/source/dnode/mgmt/impl/src/dndSnode.c b/source/dnode/mgmt/impl/src/dndSnode.c index 6f22e5b00c..77686a6027 100644 --- a/source/dnode/mgmt/impl/src/dndSnode.c +++ b/source/dnode/mgmt/impl/src/dndSnode.c @@ -258,11 +258,14 @@ static int32_t dndDropSnode(SDnode *pDnode) { return 0; } -int32_t dndProcessCreateSnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { - SDCreateSnodeReq *pMsg = pRpcMsg->pCont; - pMsg->dnodeId = htonl(pMsg->dnodeId); +int32_t dndProcessCreateSnodeReq(SDnode *pDnode, SRpcMsg *pReq) { + SDCreateSnodeReq createReq = {0}; + if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &createReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pMsg->dnodeId != dndGetDnodeId(pDnode)) { + if (createReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_SNODE_INVALID_OPTION; dError("failed to create snode since %s", terrstr()); return -1; @@ -271,11 +274,14 @@ int32_t dndProcessCreateSnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { } } -int32_t dndProcessDropSnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg) { - SDDropSnodeReq *pMsg = pRpcMsg->pCont; - pMsg->dnodeId = htonl(pMsg->dnodeId); +int32_t dndProcessDropSnodeReq(SDnode *pDnode, SRpcMsg *pReq) { + SDDropSnodeReq dropReq = {0}; + if (tDeserializeSMCreateDropQSBNodeReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - if (pMsg->dnodeId != dndGetDnodeId(pDnode)) { + if (dropReq.dnodeId != dndGetDnodeId(pDnode)) { terrno = TSDB_CODE_DND_SNODE_INVALID_OPTION; dError("failed to drop snode since %s", terrstr()); return -1; diff --git a/source/dnode/mgmt/impl/test/bnode/dbnode.cpp b/source/dnode/mgmt/impl/test/bnode/dbnode.cpp index 398d530648..75f111587f 100644 --- a/source/dnode/mgmt/impl/test/bnode/dbnode.cpp +++ b/source/dnode/mgmt/impl/test/bnode/dbnode.cpp @@ -27,10 +27,12 @@ Testbase DndTestBnode::test; TEST_F(DndTestBnode, 01_Create_Bnode) { { - int32_t contLen = sizeof(SDCreateBnodeReq); + SDCreateBnodeReq createReq = {0}; + createReq.dnodeId = 2; - SDCreateBnodeReq* pReq = (SDCreateBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -38,10 +40,12 @@ TEST_F(DndTestBnode, 01_Create_Bnode) { } { - int32_t contLen = sizeof(SDCreateBnodeReq); + SDCreateBnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateBnodeReq* pReq = (SDCreateBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -49,10 +53,12 @@ TEST_F(DndTestBnode, 01_Create_Bnode) { } { - int32_t contLen = sizeof(SDCreateBnodeReq); + SDCreateBnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateBnodeReq* pReq = (SDCreateBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -62,11 +68,12 @@ TEST_F(DndTestBnode, 01_Create_Bnode) { test.Restart(); { - int32_t contLen = sizeof(SDCreateBnodeReq); - - SDCreateBnodeReq* pReq = (SDCreateBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + SDCreateBnodeReq createReq = {0}; + createReq.dnodeId = 1; + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, TSDB_CODE_DND_BNODE_ALREADY_DEPLOYED); @@ -75,10 +82,12 @@ TEST_F(DndTestBnode, 01_Create_Bnode) { TEST_F(DndTestBnode, 01_Drop_Bnode) { { - int32_t contLen = sizeof(SDDropBnodeReq); + SDDropBnodeReq dropReq = {0}; + dropReq.dnodeId = 2; - SDDropBnodeReq* pReq = (SDDropBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -86,10 +95,12 @@ TEST_F(DndTestBnode, 01_Drop_Bnode) { } { - int32_t contLen = sizeof(SDDropBnodeReq); + SDDropBnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropBnodeReq* pReq = (SDDropBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -97,10 +108,12 @@ TEST_F(DndTestBnode, 01_Drop_Bnode) { } { - int32_t contLen = sizeof(SDDropBnodeReq); + SDDropBnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropBnodeReq* pReq = (SDDropBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -110,10 +123,12 @@ TEST_F(DndTestBnode, 01_Drop_Bnode) { test.Restart(); { - int32_t contLen = sizeof(SDDropBnodeReq); + SDDropBnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropBnodeReq* pReq = (SDDropBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -121,10 +136,12 @@ TEST_F(DndTestBnode, 01_Drop_Bnode) { } { - int32_t contLen = sizeof(SDCreateBnodeReq); + SDCreateBnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateBnodeReq* pReq = (SDCreateBnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_BNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/dnode/mgmt/impl/test/qnode/dqnode.cpp b/source/dnode/mgmt/impl/test/qnode/dqnode.cpp index 19fd6b4b12..46c31539a2 100644 --- a/source/dnode/mgmt/impl/test/qnode/dqnode.cpp +++ b/source/dnode/mgmt/impl/test/qnode/dqnode.cpp @@ -27,10 +27,12 @@ Testbase DndTestQnode::test; TEST_F(DndTestQnode, 01_Create_Qnode) { { - int32_t contLen = sizeof(SDCreateQnodeReq); + SDCreateQnodeReq createReq = {0}; + createReq.dnodeId = 2; - SDCreateQnodeReq* pReq = (SDCreateQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -38,10 +40,12 @@ TEST_F(DndTestQnode, 01_Create_Qnode) { } { - int32_t contLen = sizeof(SDCreateQnodeReq); + SDCreateQnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateQnodeReq* pReq = (SDCreateQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -49,10 +53,12 @@ TEST_F(DndTestQnode, 01_Create_Qnode) { } { - int32_t contLen = sizeof(SDCreateQnodeReq); + SDCreateQnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateQnodeReq* pReq = (SDCreateQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -62,10 +68,12 @@ TEST_F(DndTestQnode, 01_Create_Qnode) { test.Restart(); { - int32_t contLen = sizeof(SDCreateQnodeReq); + SDCreateQnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateQnodeReq* pReq = (SDCreateQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -75,10 +83,12 @@ TEST_F(DndTestQnode, 01_Create_Qnode) { TEST_F(DndTestQnode, 02_Drop_Qnode) { { - int32_t contLen = sizeof(SDDropQnodeReq); + SDDropQnodeReq dropReq = {0}; + dropReq.dnodeId = 2; - SDDropQnodeReq* pReq = (SDDropQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -86,10 +96,12 @@ TEST_F(DndTestQnode, 02_Drop_Qnode) { } { - int32_t contLen = sizeof(SDDropQnodeReq); + SDDropQnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropQnodeReq* pReq = (SDDropQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -97,10 +109,12 @@ TEST_F(DndTestQnode, 02_Drop_Qnode) { } { - int32_t contLen = sizeof(SDDropQnodeReq); + SDDropQnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropQnodeReq* pReq = (SDDropQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -110,10 +124,12 @@ TEST_F(DndTestQnode, 02_Drop_Qnode) { test.Restart(); { - int32_t contLen = sizeof(SDDropQnodeReq); + SDDropQnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropQnodeReq* pReq = (SDDropQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -121,10 +137,12 @@ TEST_F(DndTestQnode, 02_Drop_Qnode) { } { - int32_t contLen = sizeof(SDCreateQnodeReq); + SDCreateQnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateQnodeReq* pReq = (SDCreateQnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_QNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/dnode/mgmt/impl/test/snode/dsnode.cpp b/source/dnode/mgmt/impl/test/snode/dsnode.cpp index 019aa1cbbc..ea98dfdd95 100644 --- a/source/dnode/mgmt/impl/test/snode/dsnode.cpp +++ b/source/dnode/mgmt/impl/test/snode/dsnode.cpp @@ -27,10 +27,12 @@ Testbase DndTestSnode::test; TEST_F(DndTestSnode, 01_Create_Snode) { { - int32_t contLen = sizeof(SDCreateSnodeReq); + SDCreateSnodeReq createReq = {0}; + createReq.dnodeId = 2; - SDCreateSnodeReq* pReq = (SDCreateSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -38,10 +40,12 @@ TEST_F(DndTestSnode, 01_Create_Snode) { } { - int32_t contLen = sizeof(SDCreateSnodeReq); + SDCreateSnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateSnodeReq* pReq = (SDCreateSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -49,10 +53,12 @@ TEST_F(DndTestSnode, 01_Create_Snode) { } { - int32_t contLen = sizeof(SDCreateSnodeReq); + SDCreateSnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateSnodeReq* pReq = (SDCreateSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -62,10 +68,12 @@ TEST_F(DndTestSnode, 01_Create_Snode) { test.Restart(); { - int32_t contLen = sizeof(SDCreateSnodeReq); + SDCreateSnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateSnodeReq* pReq = (SDCreateSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -75,10 +83,12 @@ TEST_F(DndTestSnode, 01_Create_Snode) { TEST_F(DndTestSnode, 01_Drop_Snode) { { - int32_t contLen = sizeof(SDDropSnodeReq); + SDDropSnodeReq dropReq = {0}; + dropReq.dnodeId = 2; - SDDropSnodeReq* pReq = (SDDropSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(2); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -86,10 +96,12 @@ TEST_F(DndTestSnode, 01_Drop_Snode) { } { - int32_t contLen = sizeof(SDDropSnodeReq); + SDDropSnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropSnodeReq* pReq = (SDDropSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -97,10 +109,12 @@ TEST_F(DndTestSnode, 01_Drop_Snode) { } { - int32_t contLen = sizeof(SDDropSnodeReq); + SDDropSnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropSnodeReq* pReq = (SDDropSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -110,10 +124,12 @@ TEST_F(DndTestSnode, 01_Drop_Snode) { test.Restart(); { - int32_t contLen = sizeof(SDDropSnodeReq); + SDDropSnodeReq dropReq = {0}; + dropReq.dnodeId = 1; - SDDropSnodeReq* pReq = (SDDropSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_DROP_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -121,10 +137,12 @@ TEST_F(DndTestSnode, 01_Drop_Snode) { } { - int32_t contLen = sizeof(SDCreateSnodeReq); + SDCreateSnodeReq createReq = {0}; + createReq.dnodeId = 1; - SDCreateSnodeReq* pReq = (SDCreateSnodeReq*)rpcMallocCont(contLen); - pReq->dnodeId = htonl(1); + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_DND_CREATE_SNODE, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/dnode/mnode/impl/src/mndAuth.c b/source/dnode/mnode/impl/src/mndAuth.c index 3d70dd3717..ab8b9350b0 100644 --- a/source/dnode/mnode/impl/src/mndAuth.c +++ b/source/dnode/mnode/impl/src/mndAuth.c @@ -47,7 +47,7 @@ int32_t mndRetriveAuth(SMnode *pMnode, char *user, char *spi, char *encrypt, cha static int32_t mndProcessAuthReq(SMnodeMsg *pReq) { SAuthReq authReq = {0}; - if (tDeserializeSAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) { + if (tDeserializeSAuthReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &authReq) != 0) { terrno = TSDB_CODE_INVALID_MSG; return -1; } diff --git a/source/dnode/mnode/impl/src/mndBnode.c b/source/dnode/mnode/impl/src/mndBnode.c index ab696dd804..fd029212f5 100644 --- a/source/dnode/mnode/impl/src/mndBnode.c +++ b/source/dnode/mnode/impl/src/mndBnode.c @@ -187,17 +187,21 @@ static int32_t mndSetCreateBnodeCommitLogs(STrans *pTrans, SBnodeObj *pObj) { } static int32_t mndSetCreateBnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SBnodeObj *pObj) { - SDCreateBnodeReq *pReq = malloc(sizeof(SDCreateBnodeReq)); + SDCreateBnodeReq createReq = {0}; + createReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDCreateBnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_CREATE_BNODE; action.acceptableCode = TSDB_CODE_DND_BNODE_ALREADY_DEPLOYED; @@ -210,17 +214,21 @@ static int32_t mndSetCreateBnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, S } static int32_t mndSetCreateBnodeUndoActions(STrans *pTrans, SDnodeObj *pDnode, SBnodeObj *pObj) { - SDDropBnodeReq *pReq = malloc(sizeof(SDDropBnodeReq)); + SDDropBnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropBnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_BNODE; action.acceptableCode = TSDB_CODE_DND_BNODE_NOT_DEPLOYED; @@ -329,17 +337,21 @@ static int32_t mndSetDropBnodeCommitLogs(STrans *pTrans, SBnodeObj *pObj) { } static int32_t mndSetDropBnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SBnodeObj *pObj) { - SDDropBnodeReq *pReq = malloc(sizeof(SDDropBnodeReq)); + SDDropBnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropBnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_BNODE; action.acceptableCode = TSDB_CODE_DND_BNODE_NOT_DEPLOYED; diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 3504c6ced7..df892e2242 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -517,7 +517,7 @@ static int32_t mndProcessKillQueryReq(SMnodeMsg *pReq) { mndReleaseUser(pMnode, pUser); SKillQueryReq killReq = {0}; - if (tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq) != 0) { + if (tDeserializeSKillQueryReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &killReq) != 0) { terrno = TSDB_CODE_INVALID_MSG; return -1; } @@ -551,7 +551,7 @@ static int32_t mndProcessKillConnReq(SMnodeMsg *pReq) { mndReleaseUser(pMnode, pUser); SKillConnReq killReq = {0}; - if (tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq) != 0) { + if (tDeserializeSKillConnReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &killReq) != 0) { terrno = TSDB_CODE_INVALID_MSG; return -1; } diff --git a/source/dnode/mnode/impl/src/mndQnode.c b/source/dnode/mnode/impl/src/mndQnode.c index 76e2dcb9d7..f1335ad7e4 100644 --- a/source/dnode/mnode/impl/src/mndQnode.c +++ b/source/dnode/mnode/impl/src/mndQnode.c @@ -187,17 +187,21 @@ static int32_t mndSetCreateQnodeCommitLogs(STrans *pTrans, SQnodeObj *pObj) { } static int32_t mndSetCreateQnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) { - SDCreateQnodeReq *pReq = malloc(sizeof(SDCreateQnodeReq)); + SDCreateQnodeReq createReq = {0}; + createReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDCreateQnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_CREATE_QNODE; action.acceptableCode = TSDB_CODE_DND_QNODE_ALREADY_DEPLOYED; @@ -210,17 +214,21 @@ static int32_t mndSetCreateQnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, S } static int32_t mndSetCreateQnodeUndoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) { - SDDropQnodeReq *pReq = malloc(sizeof(SDDropQnodeReq)); + SDDropQnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropQnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_QNODE; action.acceptableCode = TSDB_CODE_DND_QNODE_NOT_DEPLOYED; @@ -329,17 +337,21 @@ static int32_t mndSetDropQnodeCommitLogs(STrans *pTrans, SQnodeObj *pObj) { } static int32_t mndSetDropQnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SQnodeObj *pObj) { - SDDropQnodeReq *pReq = malloc(sizeof(SDDropQnodeReq)); + SDDropQnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropQnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_QNODE; action.acceptableCode = TSDB_CODE_DND_QNODE_NOT_DEPLOYED; diff --git a/source/dnode/mnode/impl/src/mndSnode.c b/source/dnode/mnode/impl/src/mndSnode.c index 7f2aeefcaa..5904ca0502 100644 --- a/source/dnode/mnode/impl/src/mndSnode.c +++ b/source/dnode/mnode/impl/src/mndSnode.c @@ -187,17 +187,21 @@ static int32_t mndSetCreateSnodeCommitLogs(STrans *pTrans, SSnodeObj *pObj) { } static int32_t mndSetCreateSnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SSnodeObj *pObj) { - SDCreateSnodeReq *pReq = malloc(sizeof(SDCreateSnodeReq)); + SDCreateSnodeReq createReq = {0}; + createReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDCreateSnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_CREATE_SNODE; action.acceptableCode = TSDB_CODE_DND_SNODE_ALREADY_DEPLOYED; @@ -210,17 +214,21 @@ static int32_t mndSetCreateSnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, S } static int32_t mndSetCreateSnodeUndoActions(STrans *pTrans, SDnodeObj *pDnode, SSnodeObj *pObj) { - SDDropSnodeReq *pReq = malloc(sizeof(SDDropSnodeReq)); + SDDropSnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropSnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_SNODE; action.acceptableCode = TSDB_CODE_DND_SNODE_NOT_DEPLOYED; @@ -302,7 +310,7 @@ static int32_t mndProcessCreateSnodeReq(SMnodeMsg *pReq) { if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS; CREATE_SNODE_OVER: - if(code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { + if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { mError("snode:%d, failed to create since %s", createReq.dnodeId, terrstr()); return -1; } @@ -331,17 +339,21 @@ static int32_t mndSetDropSnodeCommitLogs(STrans *pTrans, SSnodeObj *pObj) { } static int32_t mndSetDropSnodeRedoActions(STrans *pTrans, SDnodeObj *pDnode, SSnodeObj *pObj) { - SDDropSnodeReq *pReq = malloc(sizeof(SDDropSnodeReq)); + SDDropSnodeReq dropReq = {0}; + dropReq.dnodeId = pDnode->id; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &dropReq); + void *pReq = malloc(contLen); if (pReq == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - pReq->dnodeId = htonl(pDnode->id); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &dropReq); STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); action.pCont = pReq; - action.contLen = sizeof(SDDropSnodeReq); + action.contLen = contLen; action.msgType = TDMT_DND_DROP_SNODE; action.acceptableCode = TSDB_CODE_DND_SNODE_NOT_DEPLOYED; diff --git a/source/dnode/mnode/impl/test/profile/profile.cpp b/source/dnode/mnode/impl/test/profile/profile.cpp index 4f4197cd64..14b31e5282 100644 --- a/source/dnode/mnode/impl/test/profile/profile.cpp +++ b/source/dnode/mnode/impl/test/profile/profile.cpp @@ -167,10 +167,12 @@ TEST_F(MndTestProfile, 05_KillConnMsg) { // temporary remove since kill will use new heartbeat msg #if 0 { - int32_t contLen = sizeof(SKillConnReq); + SKillConnReq killReq = {0}; + killReq.connId = connId; - SKillConnReq* pReq = (SKillConnReq*)rpcMallocCont(contLen); - pReq->connId = htonl(connId); + int32_t contLen = tSerializeSKillConnReq(NULL, 0, &killReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSKillConnReq(pReq, contLen, &killReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_CONN, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -226,10 +228,12 @@ TEST_F(MndTestProfile, 05_KillConnMsg) { } TEST_F(MndTestProfile, 06_KillConnMsg_InvalidConn) { - int32_t contLen = sizeof(SKillConnReq); + SKillConnReq killReq = {0}; + killReq.connId = 2345; - SKillConnReq* pReq = (SKillConnReq*)rpcMallocCont(contLen); - pReq->connId = htonl(2345); + int32_t contLen = tSerializeSKillConnReq(NULL, 0, &killReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSKillConnReq(pReq, contLen, &killReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_CONN, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -240,11 +244,13 @@ TEST_F(MndTestProfile, 07_KillQueryMsg) { // temporary remove since kill will use new heartbeat msg #if 0 { - int32_t contLen = sizeof(SKillQueryReq); + SKillQueryReq killReq = {0}; + killReq.connId = connId; + killReq.queryId = 1234; - SKillQueryReq* pReq = (SKillQueryReq*)rpcMallocCont(contLen); - pReq->connId = htonl(connId); - pReq->queryId = htonl(1234); + int32_t contLen = tSerializeSKillQueryReq(NULL, 0, &killReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSKillQueryReq(pReq, contLen, &killReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_QUERY, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -291,11 +297,13 @@ TEST_F(MndTestProfile, 07_KillQueryMsg) { } TEST_F(MndTestProfile, 08_KillQueryMsg_InvalidConn) { - int32_t contLen = sizeof(SKillQueryReq); + SKillQueryReq killReq = {0}; + killReq.connId = 2345; + killReq.queryId = 2345; - SKillQueryReq* pReq = (SKillQueryReq*)rpcMallocCont(contLen); - pReq->connId = htonl(2345); - pReq->queryId = htonl(1234); + int32_t contLen = tSerializeSKillQueryReq(NULL, 0, &killReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSKillQueryReq(pReq, contLen, &killReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_QUERY, pReq, contLen); ASSERT_NE(pRsp, nullptr); From d2253e0b7260278852a27e3e329b243a07fa11d7 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 17:39:37 +0800 Subject: [PATCH 10/11] minor changes --- source/dnode/mnode/impl/src/mndDnode.c | 12 +++++------- source/dnode/mnode/impl/src/mndVgroup.c | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index 07b38bdc6a..6d0b8e7c8d 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -609,14 +609,12 @@ static int32_t mndProcessConfigDnodeReq(SMnodeMsg *pReq) { SEpSet epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); - SDCfgDnodeReq *pCfgDnode = rpcMallocCont(sizeof(SDCfgDnodeReq)); - pCfgDnode->dnodeId = htonl(cfgReq.dnodeId); - memcpy(pCfgDnode->config, cfgReq.config, TSDB_DNODE_CONFIG_LEN); + int32_t bufLen = tSerializeSMCfgDnodeReq(NULL, 0, &cfgReq); + void *pBuf = rpcMallocCont(bufLen); + tSerializeSMCfgDnodeReq(pBuf, bufLen, &cfgReq); - SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, - .pCont = pCfgDnode, - .contLen = sizeof(SDCfgDnodeReq), - .ahandle = pReq->rpcMsg.ahandle}; + SRpcMsg rpcMsg = { + .msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen, .ahandle = pReq->rpcMsg.ahandle}; mInfo("dnode:%d, app:%p config:%s req send to dnode", cfgReq.dnodeId, rpcMsg.ahandle, cfgReq.config); mndSendReqToDnode(pMnode, &epSet, &rpcMsg); diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index 90d190c1be..a2438a3b8e 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -223,8 +223,8 @@ void *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVg return NULL; } - pReplica->id = htonl(pVgidDnode->id); - pReplica->port = htons(pVgidDnode->port); + pReplica->id = pVgidDnode->id; + pReplica->port = pVgidDnode->port; memcpy(pReplica->fqdn, pVgidDnode->fqdn, TSDB_FQDN_LEN); mndReleaseDnode(pMnode, pVgidDnode); From 88ee9d94e4781f096eb11a3c0a1f66760b8f0a66 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 16 Feb 2022 17:42:20 +0800 Subject: [PATCH 11/11] auth failure --- source/dnode/mgmt/impl/src/dndTransport.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index d2b4b50598..931cda475c 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -323,12 +323,14 @@ static int32_t dndRetrieveUserAuthInfo(void *parent, char *user, char *spi, char terrno = rpcRsp.code; dError("user:%s, failed to get user auth from other mnodes since %s", user, terrstr()); } else { - SAuthRsp *pRsp = rpcRsp.pCont; - memcpy(secret, pRsp->secret, TSDB_PASSWORD_LEN); - memcpy(ckey, pRsp->ckey, TSDB_PASSWORD_LEN); - *spi = pRsp->spi; - *encrypt = pRsp->encrypt; - dTrace("user:%s, success to get user auth from other mnodes, spi:%d encrypt:%d", user, pRsp->spi, pRsp->encrypt); + SAuthRsp authRsp = {0}; + tDeserializeSAuthReq(rpcRsp.pCont, rpcRsp.contLen, &authRsp); + memcpy(secret, authRsp.secret, TSDB_PASSWORD_LEN); + memcpy(ckey, authRsp.ckey, TSDB_PASSWORD_LEN); + *spi = authRsp.spi; + *encrypt = authRsp.encrypt; + dTrace("user:%s, success to get user auth from other mnodes, spi:%d encrypt:%d", user, authRsp.spi, + authRsp.encrypt); } rpcFreeCont(rpcRsp.pCont);