serialize func msg

This commit is contained in:
Shengliang Guan 2022-02-12 16:28:50 +08:00
parent b19987ac3a
commit 3d396ca7ff
11 changed files with 594 additions and 339 deletions

View File

@ -394,8 +394,8 @@ typedef struct {
SHashObj* writeDbs; SHashObj* writeDbs;
} SGetUserAuthRsp; } SGetUserAuthRsp;
int32_t tSerializeSGetUserAuthRsp(void* buf, int32_t bufLen, SGetUserAuthRsp* pReq); int32_t tSerializeSGetUserAuthRsp(void* buf, int32_t bufLen, SGetUserAuthRsp* pRsp);
int32_t tDeserializeSGetUserAuthRsp(void* buf, int32_t bufLen, SGetUserAuthRsp* pReq); int32_t tDeserializeSGetUserAuthRsp(void* buf, int32_t bufLen, SGetUserAuthRsp* pRsp);
typedef struct { typedef struct {
int16_t colId; // column id int16_t colId; // column id
@ -616,22 +616,31 @@ typedef struct {
int64_t signature; int64_t signature;
int32_t commentSize; int32_t commentSize;
int32_t codeSize; int32_t codeSize;
char pCont[]; char pComment[TSDB_FUNC_COMMENT_LEN];
char pCode[TSDB_FUNC_CODE_LEN];
} SCreateFuncReq; } SCreateFuncReq;
int32_t tSerializeSCreateFuncReq(void* buf, int32_t bufLen, SCreateFuncReq* pReq);
int32_t tDeserializeSCreateFuncReq(void* buf, int32_t bufLen, SCreateFuncReq* pReq);
typedef struct { typedef struct {
char name[TSDB_FUNC_NAME_LEN]; char name[TSDB_FUNC_NAME_LEN];
int8_t igNotExists; int8_t igNotExists;
} SDropFuncReq; } SDropFuncReq;
int32_t tSerializeSDropFuncReq(void* buf, int32_t bufLen, SDropFuncReq* pReq);
int32_t tDeserializeSDropFuncReq(void* buf, int32_t bufLen, SDropFuncReq* pReq);
typedef struct { typedef struct {
int32_t numOfFuncs; int32_t numOfFuncs;
char pFuncNames[]; SArray* pFuncNames;
} SRetrieveFuncReq; } SRetrieveFuncReq;
int32_t tSerializeSRetrieveFuncReq(void* buf, int32_t bufLen, SRetrieveFuncReq* pReq);
int32_t tDeserializeSRetrieveFuncReq(void* buf, int32_t bufLen, SRetrieveFuncReq* pReq);
typedef struct { typedef struct {
char name[TSDB_FUNC_NAME_LEN]; char name[TSDB_FUNC_NAME_LEN];
int8_t align;
int8_t funcType; int8_t funcType;
int8_t scriptType; int8_t scriptType;
int8_t outputType; int8_t outputType;
@ -640,14 +649,18 @@ typedef struct {
int64_t signature; int64_t signature;
int32_t commentSize; int32_t commentSize;
int32_t codeSize; int32_t codeSize;
char pCont[]; char pComment[TSDB_FUNC_COMMENT_LEN];
char pCode[TSDB_FUNC_CODE_LEN];
} SFuncInfo; } SFuncInfo;
typedef struct { typedef struct {
int32_t numOfFuncs; int32_t numOfFuncs;
char pFuncInfos[]; SArray* pFuncInfos;
} SRetrieveFuncRsp; } SRetrieveFuncRsp;
int32_t tSerializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp* pRsp);
int32_t tDeserializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp* pRsp);
typedef struct { typedef struct {
int32_t statusInterval; int32_t statusInterval;
int64_t checkTime; // 1970-01-01 00:00:00.000 int64_t checkTime; // 1970-01-01 00:00:00.000

View File

@ -753,29 +753,29 @@ int32_t tDeserializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *
return 0; return 0;
} }
int32_t tSerializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pReq) { int32_t tSerializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
SCoder encoder = {0}; SCoder encoder = {0};
tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
if (tStartEncode(&encoder) < 0) return -1; if (tStartEncode(&encoder) < 0) return -1;
if (tEncodeCStr(&encoder, pReq->user) < 0) return -1; if (tEncodeCStr(&encoder, pRsp->user) < 0) return -1;
if (tEncodeI8(&encoder, pReq->superAuth) < 0) return -1; if (tEncodeI8(&encoder, pRsp->superAuth) < 0) return -1;
int32_t numOfReadDbs = taosHashGetSize(pReq->readDbs); int32_t numOfReadDbs = taosHashGetSize(pRsp->readDbs);
int32_t numOfWriteDbs = taosHashGetSize(pReq->writeDbs); int32_t numOfWriteDbs = taosHashGetSize(pRsp->writeDbs);
if (tEncodeI32(&encoder, numOfReadDbs) < 0) return -1; if (tEncodeI32(&encoder, numOfReadDbs) < 0) return -1;
if (tEncodeI32(&encoder, numOfWriteDbs) < 0) return -1; if (tEncodeI32(&encoder, numOfWriteDbs) < 0) return -1;
char *db = taosHashIterate(pReq->readDbs, NULL); char *db = taosHashIterate(pRsp->readDbs, NULL);
while (db != NULL) { while (db != NULL) {
if (tEncodeCStr(&encoder, db) < 0) return -1; if (tEncodeCStr(&encoder, db) < 0) return -1;
db = taosHashIterate(pReq->readDbs, db); db = taosHashIterate(pRsp->readDbs, db);
} }
db = taosHashIterate(pReq->writeDbs, NULL); db = taosHashIterate(pRsp->writeDbs, NULL);
while (db != NULL) { while (db != NULL) {
if (tEncodeCStr(&encoder, db) < 0) return -1; if (tEncodeCStr(&encoder, db) < 0) return -1;
db = taosHashIterate(pReq->writeDbs, db); db = taosHashIterate(pRsp->writeDbs, db);
} }
tEndEncode(&encoder); tEndEncode(&encoder);
@ -785,10 +785,10 @@ int32_t tSerializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pR
return tlen; return tlen;
} }
int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pReq) { int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *pRsp) {
pReq->readDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false); pRsp->readDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false);
pReq->writeDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false); pRsp->writeDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false);
if (pReq->readDbs == NULL || pReq->writeDbs == NULL) { if (pRsp->readDbs == NULL || pRsp->writeDbs == NULL) {
return -1; return -1;
} }
@ -796,8 +796,8 @@ int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *
tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
if (tStartDecode(&decoder) < 0) return -1; if (tStartDecode(&decoder) < 0) return -1;
if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1; if (tDecodeCStrTo(&decoder, pRsp->user) < 0) return -1;
if (tDecodeI8(&decoder, &pReq->superAuth) < 0) return -1; if (tDecodeI8(&decoder, &pRsp->superAuth) < 0) return -1;
int32_t numOfReadDbs = 0; int32_t numOfReadDbs = 0;
int32_t numOfWriteDbs = 0; int32_t numOfWriteDbs = 0;
@ -808,14 +808,14 @@ int32_t tDeserializeSGetUserAuthRsp(void *buf, int32_t bufLen, SGetUserAuthRsp *
char db[TSDB_DB_FNAME_LEN] = {0}; char db[TSDB_DB_FNAME_LEN] = {0};
if (tDecodeCStrTo(&decoder, db) < 0) return -1; if (tDecodeCStrTo(&decoder, db) < 0) return -1;
int32_t len = strlen(db) + 1; int32_t len = strlen(db) + 1;
taosHashPut(pReq->readDbs, db, len, db, len); taosHashPut(pRsp->readDbs, db, len, db, len);
} }
for (int32_t i = 0; i < numOfWriteDbs; ++i) { for (int32_t i = 0; i < numOfWriteDbs; ++i) {
char db[TSDB_DB_FNAME_LEN] = {0}; char db[TSDB_DB_FNAME_LEN] = {0};
if (tDecodeCStrTo(&decoder, db) < 0) return -1; if (tDecodeCStrTo(&decoder, db) < 0) return -1;
int32_t len = strlen(db) + 1; int32_t len = strlen(db) + 1;
taosHashPut(pReq->writeDbs, db, len, db, len); taosHashPut(pRsp->writeDbs, db, len, db, len);
} }
tEndDecode(&decoder); tEndDecode(&decoder);
@ -920,3 +920,180 @@ int32_t tDeserializeSCreateDnodeReq(void *buf, int32_t bufLen, SCreateDnodeReq *
tCoderClear(&decoder); tCoderClear(&decoder);
return 0; return 0;
} }
int32_t tSerializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
SCoder encoder = {0};
tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
if (tStartEncode(&encoder) < 0) return -1;
if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
if (tEncodeI8(&encoder, pReq->igExists) < 0) return -1;
if (tEncodeI8(&encoder, pReq->funcType) < 0) return -1;
if (tEncodeI8(&encoder, pReq->scriptType) < 0) return -1;
if (tEncodeI8(&encoder, pReq->outputType) < 0) return -1;
if (tEncodeI32(&encoder, pReq->outputLen) < 0) return -1;
if (tEncodeI32(&encoder, pReq->bufSize) < 0) return -1;
if (tEncodeI64(&encoder, pReq->signature) < 0) return -1;
if (tEncodeI32(&encoder, pReq->commentSize) < 0) return -1;
if (tEncodeI32(&encoder, pReq->codeSize) < 0) return -1;
if (tEncodeCStr(&encoder, pReq->pComment) < 0) return -1;
if (tEncodeCStr(&encoder, pReq->pCode) < 0) return -1;
tEndEncode(&encoder);
int32_t tlen = encoder.pos;
tCoderClear(&encoder);
return tlen;
}
int32_t tDeserializeSCreateFuncReq(void *buf, int32_t bufLen, SCreateFuncReq *pReq) {
SCoder decoder = {0};
tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
if (tStartDecode(&decoder) < 0) return -1;
if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
if (tDecodeI8(&decoder, &pReq->igExists) < 0) return -1;
if (tDecodeI8(&decoder, &pReq->funcType) < 0) return -1;
if (tDecodeI8(&decoder, &pReq->scriptType) < 0) return -1;
if (tDecodeI8(&decoder, &pReq->outputType) < 0) return -1;
if (tDecodeI32(&decoder, &pReq->outputLen) < 0) return -1;
if (tDecodeI32(&decoder, &pReq->bufSize) < 0) return -1;
if (tDecodeI64(&decoder, &pReq->signature) < 0) return -1;
if (tDecodeI32(&decoder, &pReq->commentSize) < 0) return -1;
if (tDecodeI32(&decoder, &pReq->codeSize) < 0) return -1;
if (tDecodeCStrTo(&decoder, pReq->pComment) < 0) return -1;
if (tDecodeCStrTo(&decoder, pReq->pCode) < 0) return -1;
tEndDecode(&decoder);
tCoderClear(&decoder);
return 0;
}
int32_t tSerializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
SCoder encoder = {0};
tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
if (tStartEncode(&encoder) < 0) return -1;
if (tEncodeCStr(&encoder, pReq->name) < 0) return -1;
if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1;
tEndEncode(&encoder);
int32_t tlen = encoder.pos;
tCoderClear(&encoder);
return tlen;
}
int32_t tDeserializeSDropFuncReq(void *buf, int32_t bufLen, SDropFuncReq *pReq) {
SCoder decoder = {0};
tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
if (tStartDecode(&decoder) < 0) return -1;
if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1;
if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1;
tEndDecode(&decoder);
tCoderClear(&decoder);
return 0;
}
int32_t tSerializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
SCoder encoder = {0};
tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
if (tStartEncode(&encoder) < 0) return -1;
if (tEncodeI32(&encoder, pReq->numOfFuncs) < 0) return -1;
if (pReq->numOfFuncs != (int32_t)taosArrayGetSize(pReq->pFuncNames)) return -1;
for (int32_t i = 0; i < pReq->numOfFuncs; ++i) {
char *fname = taosArrayGet(pReq->pFuncNames, i);
if (tEncodeCStr(&encoder, fname) < 0) return -1;
}
tEndEncode(&encoder);
int32_t tlen = encoder.pos;
tCoderClear(&encoder);
return tlen;
}
int32_t tDeserializeSRetrieveFuncReq(void *buf, int32_t bufLen, SRetrieveFuncReq *pReq) {
SCoder decoder = {0};
tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
if (tStartDecode(&decoder) < 0) return -1;
if (tDecodeI32(&decoder, &pReq->numOfFuncs) < 0) return -1;
pReq->pFuncNames = taosArrayInit(pReq->numOfFuncs, TSDB_FUNC_NAME_LEN);
if (pReq->pFuncNames == NULL) return -1;
for (int32_t i = 0; i < pReq->numOfFuncs; ++i) {
char fname[TSDB_FUNC_NAME_LEN] = {0};
if (tDecodeCStrTo(&decoder, fname) < 0) return -1;
taosArrayPush(pReq->pFuncNames, fname);
}
tEndDecode(&decoder);
tCoderClear(&decoder);
return 0;
}
int32_t tSerializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
SCoder encoder = {0};
tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER);
if (tStartEncode(&encoder) < 0) return -1;
if (tEncodeI32(&encoder, pRsp->numOfFuncs) < 0) return -1;
if (pRsp->numOfFuncs != (int32_t)taosArrayGetSize(pRsp->pFuncInfos)) return -1;
for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
SFuncInfo *pInfo = taosArrayGet(pRsp->pFuncInfos, i);
if (tEncodeCStr(&encoder, pInfo->name) < 0) return -1;
if (tEncodeI8(&encoder, pInfo->funcType) < 0) return -1;
if (tEncodeI8(&encoder, pInfo->scriptType) < 0) return -1;
if (tEncodeI8(&encoder, pInfo->outputType) < 0) return -1;
if (tEncodeI32(&encoder, pInfo->outputLen) < 0) return -1;
if (tEncodeI32(&encoder, pInfo->bufSize) < 0) return -1;
if (tEncodeI64(&encoder, pInfo->signature) < 0) return -1;
if (tEncodeI32(&encoder, pInfo->commentSize) < 0) return -1;
if (tEncodeI32(&encoder, pInfo->codeSize) < 0) return -1;
if (tEncodeCStr(&encoder, pInfo->pComment) < 0) return -1;
if (tEncodeCStr(&encoder, pInfo->pCode) < 0) return -1;
}
tEndEncode(&encoder);
int32_t tlen = encoder.pos;
tCoderClear(&encoder);
return tlen;
}
int32_t tDeserializeSRetrieveFuncRsp(void *buf, int32_t bufLen, SRetrieveFuncRsp *pRsp) {
SCoder decoder = {0};
tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER);
if (tStartDecode(&decoder) < 0) return -1;
if (tDecodeI32(&decoder, &pRsp->numOfFuncs) < 0) return -1;
pRsp->pFuncInfos = taosArrayInit(pRsp->numOfFuncs, sizeof(SFuncInfo));
if (pRsp->pFuncInfos == NULL) return -1;
for (int32_t i = 0; i < pRsp->numOfFuncs; ++i) {
SFuncInfo fInfo = {0};
if (tDecodeCStrTo(&decoder, fInfo.name) < 0) return -1;
if (tDecodeI8(&decoder, &fInfo.funcType) < 0) return -1;
if (tDecodeI8(&decoder, &fInfo.scriptType) < 0) return -1;
if (tDecodeI8(&decoder, &fInfo.outputType) < 0) return -1;
if (tDecodeI32(&decoder, &fInfo.outputLen) < 0) return -1;
if (tDecodeI32(&decoder, &fInfo.bufSize) < 0) return -1;
if (tDecodeI64(&decoder, &fInfo.signature) < 0) return -1;
if (tDecodeI32(&decoder, &fInfo.commentSize) < 0) return -1;
if (tDecodeI32(&decoder, &fInfo.codeSize) < 0) return -1;
if (tDecodeCStrTo(&decoder, fInfo.pComment) < 0) return -1;
if (tDecodeCStrTo(&decoder, fInfo.pCode) < 0) return -1;
taosArrayPush(pRsp->pFuncInfos, &fInfo);
}
tEndDecode(&decoder);
tCoderClear(&decoder);
return 0;
}

View File

@ -29,9 +29,8 @@ int32_t mndCheckCreateUserAuth(SUserObj *pOperUser);
int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SDbObj *pDb, SAlterUserReq *pAlter); int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SDbObj *pDb, SAlterUserReq *pAlter);
int32_t mndCheckDropUserAuth(SUserObj *pOperUser); int32_t mndCheckDropUserAuth(SUserObj *pOperUser);
int32_t mndCheckCreateNodeAuth(SUserObj *pOperUser); int32_t mndCheckOperateNodeAuth(SUserObj *pOperUser);
int32_t mndCheckDropNodeAuth(SUserObj *pOperUser); int32_t mndCheckOperateFuncAuth(SUserObj *pOperUser);
int32_t mndCheckAlterNodeAuth(SUserObj *pOperUser);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -111,7 +111,7 @@ int32_t mndCheckDropUserAuth(SUserObj *pOperUser) {
return -1; return -1;
} }
int32_t mndCheckCreateNodeAuth(SUserObj *pOperUser) { int32_t mndCheckOperateNodeAuth(SUserObj *pOperUser) {
if (pOperUser->superUser) { if (pOperUser->superUser) {
return 0; return 0;
} }
@ -120,6 +120,11 @@ int32_t mndCheckCreateNodeAuth(SUserObj *pOperUser) {
return -1; return -1;
} }
int32_t mndCheckDropNodeAuth(SUserObj *pOperUser) { return mndCheckCreateNodeAuth(pOperUser); } int32_t mndCheckOperateFuncAuth(SUserObj *pOperUser) {
if (pOperUser->superUser) {
return 0;
}
int32_t mndCheckAlterNodeAuth(SUserObj *pOperUser) { return mndCheckCreateNodeAuth(pOperUser); } terrno = TSDB_CODE_MND_NO_RIGHTS;
return -1;
}

View File

@ -293,7 +293,7 @@ static int32_t mndProcessCreateBnodeReq(SMnodeMsg *pReq) {
goto CREATE_BNODE_OVER; goto CREATE_BNODE_OVER;
} }
if (mndCheckDropNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto CREATE_BNODE_OVER; goto CREATE_BNODE_OVER;
} }
@ -400,7 +400,7 @@ static int32_t mndProcessDropBnodeReq(SMnodeMsg *pReq) {
goto DROP_BNODE_OVER; goto DROP_BNODE_OVER;
} }
if (mndCheckCreateNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto DROP_BNODE_OVER; goto DROP_BNODE_OVER;
} }

View File

@ -496,7 +496,7 @@ static int32_t mndProcessCreateDnodeReq(SMnodeMsg *pReq) {
goto CREATE_DNODE_OVER; goto CREATE_DNODE_OVER;
} }
if (mndCheckDropNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto CREATE_DNODE_OVER; goto CREATE_DNODE_OVER;
} }
@ -506,7 +506,6 @@ static int32_t mndProcessCreateDnodeReq(SMnodeMsg *pReq) {
CREATE_DNODE_OVER: CREATE_DNODE_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr()); mError("dnode:%s:%d, failed to create since %s", createReq.fqdn, createReq.port, terrstr());
return -1;
} }
mndReleaseDnode(pMnode, pDnode); mndReleaseDnode(pMnode, pDnode);
@ -571,7 +570,7 @@ static int32_t mndProcessDropDnodeReq(SMnodeMsg *pReq) {
goto DROP_DNODE_OVER; goto DROP_DNODE_OVER;
} }
if (mndCheckCreateNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto DROP_DNODE_OVER; goto DROP_DNODE_OVER;
} }
@ -581,7 +580,6 @@ static int32_t mndProcessDropDnodeReq(SMnodeMsg *pReq) {
DROP_DNODE_OVER: DROP_DNODE_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr()); mError("dnode:%d, failed to drop since %s", dropReq.dnodeId, terrstr());
return -1;
} }
mndReleaseDnode(pMnode, pDnode); mndReleaseDnode(pMnode, pDnode);

View File

@ -15,9 +15,11 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndFunc.h" #include "mndFunc.h"
#include "mndAuth.h"
#include "mndShow.h" #include "mndShow.h"
#include "mndSync.h" #include "mndSync.h"
#include "mndTrans.h" #include "mndTrans.h"
#include "mndUser.h"
#define SDB_FUNC_VER 1 #define SDB_FUNC_VER 1
#define SDB_FUNC_RESERVE_SIZE 64 #define SDB_FUNC_RESERVE_SIZE 64
@ -201,8 +203,8 @@ static int32_t mndCreateFunc(SMnode *pMnode, SMnodeMsg *pReq, SCreateFuncReq *pC
goto CREATE_FUNC_OVER; goto CREATE_FUNC_OVER;
} }
memcpy(func.pComment, pCreate->pCont, pCreate->commentSize); memcpy(func.pComment, pCreate->pComment, pCreate->commentSize);
memcpy(func.pCode, pCreate->pCont + pCreate->commentSize, func.codeSize); memcpy(func.pCode, pCreate->pCode, func.codeSize);
pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg); pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
if (pTrans == NULL) goto CREATE_FUNC_OVER; if (pTrans == NULL) goto CREATE_FUNC_OVER;
@ -261,164 +263,202 @@ DROP_FUNC_OVER:
} }
static int32_t mndProcessCreateFuncReq(SMnodeMsg *pReq) { static int32_t mndProcessCreateFuncReq(SMnodeMsg *pReq) {
SMnode *pMnode = pReq->pMnode; SMnode *pMnode = pReq->pMnode;
int32_t code = -1;
SUserObj *pUser = NULL;
SFuncObj *pFunc = NULL;
SCreateFuncReq createReq = {0};
SCreateFuncReq *pCreate = pReq->rpcMsg.pCont; if (tDeserializeSCreateFuncReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
pCreate->outputLen = htonl(pCreate->outputLen); terrno = TSDB_CODE_INVALID_MSG;
pCreate->bufSize = htonl(pCreate->bufSize); goto CREATE_FUNC_OVER;
pCreate->signature = htobe64(pCreate->signature); }
pCreate->commentSize = htonl(pCreate->commentSize);
pCreate->codeSize = htonl(pCreate->codeSize);
mDebug("func:%s, start to create", pCreate->name); mDebug("func:%s, start to create", createReq.name);
SFuncObj *pFunc = mndAcquireFunc(pMnode, pCreate->name); pFunc = mndAcquireFunc(pMnode, createReq.name);
if (pFunc != NULL) { if (pFunc != NULL) {
mndReleaseFunc(pMnode, pFunc); if (createReq.igExists) {
if (pCreate->igExists) { mDebug("func:%s, already exist, ignore exist is set", createReq.name);
mDebug("stb:%s, already exist, ignore exist is set", pCreate->name); code = 0;
return 0; goto CREATE_FUNC_OVER;
} else { } else {
terrno = TSDB_CODE_MND_FUNC_ALREADY_EXIST; terrno = TSDB_CODE_MND_FUNC_ALREADY_EXIST;
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
} else if (terrno == TSDB_CODE_MND_FUNC_ALREADY_EXIST) { } else if (terrno == TSDB_CODE_MND_FUNC_ALREADY_EXIST) {
mError("stb:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
if (pCreate->name[0] == 0) { if (createReq.name[0] == 0) {
terrno = TSDB_CODE_MND_INVALID_FUNC_NAME; terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
if (pCreate->commentSize <= 0 || pCreate->commentSize > TSDB_FUNC_COMMENT_LEN) { if (createReq.commentSize <= 0 || createReq.commentSize > TSDB_FUNC_COMMENT_LEN) {
terrno = TSDB_CODE_MND_INVALID_FUNC_COMMENT; terrno = TSDB_CODE_MND_INVALID_FUNC_COMMENT;
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
if (pCreate->codeSize <= 0 || pCreate->codeSize > TSDB_FUNC_CODE_LEN) { if (createReq.codeSize <= 0 || createReq.codeSize > TSDB_FUNC_CODE_LEN) {
terrno = TSDB_CODE_MND_INVALID_FUNC_CODE; terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
if (pCreate->pCont[0] == 0) { if (createReq.pCode[0] == 0) {
terrno = TSDB_CODE_MND_INVALID_FUNC_CODE; terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
if (pCreate->bufSize <= 0 || pCreate->bufSize > TSDB_FUNC_BUF_SIZE) { if (createReq.bufSize <= 0 || createReq.bufSize > TSDB_FUNC_BUF_SIZE) {
terrno = TSDB_CODE_MND_INVALID_FUNC_BUFSIZE; terrno = TSDB_CODE_MND_INVALID_FUNC_BUFSIZE;
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); goto CREATE_FUNC_OVER;
return -1;
} }
int32_t code = mndCreateFunc(pMnode, pReq, pCreate); pUser = mndAcquireUser(pMnode, pReq->user);
if (code != 0) { if (pUser == NULL) {
mError("func:%s, failed to create since %s", pCreate->name, terrstr()); terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
return -1; goto CREATE_FUNC_OVER;
} }
return TSDB_CODE_MND_ACTION_IN_PROGRESS; if (mndCheckOperateFuncAuth(pUser)) {
goto CREATE_FUNC_OVER;
}
code = mndCreateFunc(pMnode, pReq, &createReq);
if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
CREATE_FUNC_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("func:%s, failed to create since %s", createReq.name, terrstr());
}
mndReleaseFunc(pMnode, pFunc);
mndReleaseUser(pMnode, pUser);
return code;
} }
static int32_t mndProcessDropFuncReq(SMnodeMsg *pReq) { static int32_t mndProcessDropFuncReq(SMnodeMsg *pReq) {
SMnode *pMnode = pReq->pMnode; SMnode *pMnode = pReq->pMnode;
SDropFuncReq *pDrop = pReq->rpcMsg.pCont; int32_t code = -1;
SUserObj *pUser = NULL;
SFuncObj *pFunc = NULL;
SDropFuncReq dropReq = {0};
mDebug("func:%s, start to drop", pDrop->name); if (tDeserializeSDropFuncReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
if (pDrop->name[0] == 0) { goto DROP_FUNC_OVER;
terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
mError("func:%s, failed to drop since %s", pDrop->name, terrstr());
return -1;
} }
SFuncObj *pFunc = mndAcquireFunc(pMnode, pDrop->name); mDebug("func:%s, start to drop", dropReq.name);
if (dropReq.name[0] == 0) {
terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
goto DROP_FUNC_OVER;
}
pFunc = mndAcquireFunc(pMnode, dropReq.name);
if (pFunc == NULL) { if (pFunc == NULL) {
if (pDrop->igNotExists) { if (dropReq.igNotExists) {
mDebug("func:%s, not exist, ignore not exist is set", pDrop->name); mDebug("func:%s, not exist, ignore not exist is set", dropReq.name);
return 0; code = 0;
goto DROP_FUNC_OVER;
} else { } else {
terrno = TSDB_CODE_MND_FUNC_NOT_EXIST; terrno = TSDB_CODE_MND_FUNC_NOT_EXIST;
mError("func:%s, failed to drop since %s", pDrop->name, terrstr()); goto DROP_FUNC_OVER;
return -1;
} }
} }
int32_t code = mndDropFunc(pMnode, pReq, pFunc); pUser = mndAcquireUser(pMnode, pReq->user);
mndReleaseFunc(pMnode, pFunc); if (pUser == NULL) {
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
if (code != 0) { goto DROP_FUNC_OVER;
mError("func:%s, failed to drop since %s", pDrop->name, terrstr());
return -1;
} }
return TSDB_CODE_MND_ACTION_IN_PROGRESS; if (mndCheckOperateFuncAuth(pUser)) {
goto DROP_FUNC_OVER;
}
code = mndDropFunc(pMnode, pReq, pFunc);
if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
DROP_FUNC_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("func:%s, failed to drop since %s", dropReq.name, terrstr());
}
mndReleaseFunc(pMnode, pFunc);
mndReleaseUser(pMnode, pUser);
return code;
} }
static int32_t mndProcessRetrieveFuncReq(SMnodeMsg *pReq) { static int32_t mndProcessRetrieveFuncReq(SMnodeMsg *pReq) {
int32_t code = -1; SMnode *pMnode = pReq->pMnode;
SMnode *pMnode = pReq->pMnode; int32_t code = -1;
SRetrieveFuncReq retrieveReq = {0};
SRetrieveFuncRsp retrieveRsp = {0};
SRetrieveFuncReq *pRetrieve = pReq->rpcMsg.pCont; if (tDeserializeSRetrieveFuncReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &retrieveReq) != 0) {
pRetrieve->numOfFuncs = htonl(pRetrieve->numOfFuncs); terrno = TSDB_CODE_INVALID_MSG;
if (pRetrieve->numOfFuncs <= 0 || pRetrieve->numOfFuncs > TSDB_FUNC_MAX_RETRIEVE) { goto RETRIEVE_FUNC_OVER;
}
if (retrieveReq.numOfFuncs <= 0 || retrieveReq.numOfFuncs > TSDB_FUNC_MAX_RETRIEVE) {
terrno = TSDB_CODE_MND_INVALID_FUNC_RETRIEVE; terrno = TSDB_CODE_MND_INVALID_FUNC_RETRIEVE;
return -1; goto RETRIEVE_FUNC_OVER;
} }
int32_t fsize = sizeof(SFuncInfo) + TSDB_FUNC_CODE_LEN + TSDB_FUNC_COMMENT_LEN; retrieveRsp.numOfFuncs = retrieveReq.numOfFuncs;
int32_t size = sizeof(SRetrieveFuncRsp) + fsize * pRetrieve->numOfFuncs; retrieveRsp.pFuncInfos = taosArrayInit(retrieveReq.numOfFuncs, sizeof(SFuncInfo));
if (retrieveRsp.pFuncInfos == NULL) {
SRetrieveFuncRsp *pRetrieveRsp = rpcMallocCont(size);
if (pRetrieveRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
goto FUNC_RETRIEVE_OVER; goto RETRIEVE_FUNC_OVER;
} }
pRetrieveRsp->numOfFuncs = htonl(pRetrieve->numOfFuncs); for (int32_t i = 0; i < retrieveReq.numOfFuncs; ++i) {
char *pOutput = pRetrieveRsp->pFuncInfos; char *funcName = taosArrayGet(retrieveReq.pFuncNames, i);
for (int32_t i = 0; i < pRetrieve->numOfFuncs; ++i) {
char funcName[TSDB_FUNC_NAME_LEN] = {0};
memcpy(funcName, pRetrieve->pFuncNames + i * TSDB_FUNC_NAME_LEN, TSDB_FUNC_NAME_LEN);
SFuncObj *pFunc = mndAcquireFunc(pMnode, funcName); SFuncObj *pFunc = mndAcquireFunc(pMnode, funcName);
if (pFunc == NULL) { if (pFunc == NULL) {
terrno = TSDB_CODE_MND_INVALID_FUNC; terrno = TSDB_CODE_MND_INVALID_FUNC;
mError("func:%s, failed to retrieve since %s", funcName, terrstr()); goto RETRIEVE_FUNC_OVER;
goto FUNC_RETRIEVE_OVER;
} }
SFuncInfo *pFuncInfo = (SFuncInfo *)pOutput; SFuncInfo funcInfo = {0};
memcpy(pFuncInfo->name, pFunc->name, TSDB_FUNC_NAME_LEN); memcpy(funcInfo.name, pFunc->name, TSDB_FUNC_NAME_LEN);
pFuncInfo->funcType = pFunc->funcType; funcInfo.funcType = pFunc->funcType;
pFuncInfo->scriptType = pFunc->scriptType; funcInfo.scriptType = pFunc->scriptType;
pFuncInfo->outputType = pFunc->outputType; funcInfo.outputType = pFunc->outputType;
pFuncInfo->outputLen = htonl(pFunc->outputLen); funcInfo.outputLen = pFunc->outputLen;
pFuncInfo->bufSize = htonl(pFunc->bufSize); funcInfo.bufSize = pFunc->bufSize;
pFuncInfo->signature = htobe64(pFunc->signature); funcInfo.signature = pFunc->signature;
pFuncInfo->commentSize = htonl(pFunc->commentSize); funcInfo.commentSize = pFunc->commentSize;
pFuncInfo->codeSize = htonl(pFunc->codeSize); funcInfo.codeSize = pFunc->codeSize;
memcpy(pFuncInfo->pCont, pFunc->pComment, pFunc->commentSize); memcpy(funcInfo.pComment, pFunc->pComment, pFunc->commentSize);
memcpy(pFuncInfo->pCont + pFunc->commentSize, pFunc->pCode, pFunc->codeSize); memcpy(funcInfo.pCode, pFunc->pCode, pFunc->codeSize);
pOutput += (sizeof(SFuncInfo) + pFunc->commentSize + pFunc->codeSize); taosArrayPush(retrieveRsp.pFuncInfos, &funcInfo);
mndReleaseFunc(pMnode, pFunc); mndReleaseFunc(pMnode, pFunc);
} }
pReq->pCont = pRetrieveRsp; int32_t contLen = tSerializeSRetrieveFuncRsp(NULL, 0, &retrieveRsp);
pReq->contLen = (int32_t)(pOutput - (char *)pRetrieveRsp); void *pRsp = rpcMallocCont(contLen);
if (pRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto RETRIEVE_FUNC_OVER;
}
tSerializeSRetrieveFuncRsp(pRsp, contLen, &retrieveRsp);
pReq->pCont = pRsp;
pReq->contLen = contLen;
code = 0; code = 0;
FUNC_RETRIEVE_OVER: RETRIEVE_FUNC_OVER:
if (code != 0) rpcFreeCont(pRetrieveRsp); taosArrayDestroy(retrieveReq.pFuncNames);
taosArrayDestroy(retrieveRsp.pFuncInfos);
return code; return code;
} }

View File

@ -415,7 +415,7 @@ static int32_t mndProcessCreateMnodeReq(SMnodeMsg *pReq) {
goto CREATE_MNODE_OVER; goto CREATE_MNODE_OVER;
} }
if (mndCheckDropNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto CREATE_MNODE_OVER; goto CREATE_MNODE_OVER;
} }
@ -425,7 +425,6 @@ static int32_t mndProcessCreateMnodeReq(SMnodeMsg *pReq) {
CREATE_MNODE_OVER: CREATE_MNODE_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr()); mError("mnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
return -1;
} }
mndReleaseMnode(pMnode, pObj); mndReleaseMnode(pMnode, pObj);
@ -583,7 +582,7 @@ static int32_t mndProcessDropMnodeReq(SMnodeMsg *pReq) {
goto DROP_MNODE_OVER; goto DROP_MNODE_OVER;
} }
if (mndCheckCreateNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto DROP_MNODE_OVER; goto DROP_MNODE_OVER;
} }

View File

@ -293,7 +293,7 @@ static int32_t mndProcessCreateQnodeReq(SMnodeMsg *pReq) {
goto CREATE_QNODE_OVER; goto CREATE_QNODE_OVER;
} }
if (mndCheckDropNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto CREATE_QNODE_OVER; goto CREATE_QNODE_OVER;
} }
@ -303,7 +303,6 @@ static int32_t mndProcessCreateQnodeReq(SMnodeMsg *pReq) {
CREATE_QNODE_OVER: CREATE_QNODE_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("qnode:%d, failed to create since %s", createReq.dnodeId, terrstr()); mError("qnode:%d, failed to create since %s", createReq.dnodeId, terrstr());
return -1;
} }
mndReleaseQnode(pMnode, pObj); mndReleaseQnode(pMnode, pObj);
@ -401,7 +400,7 @@ static int32_t mndProcessDropQnodeReq(SMnodeMsg *pReq) {
goto DROP_QNODE_OVER; goto DROP_QNODE_OVER;
} }
if (mndCheckCreateNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto DROP_QNODE_OVER; goto DROP_QNODE_OVER;
} }
@ -411,7 +410,6 @@ static int32_t mndProcessDropQnodeReq(SMnodeMsg *pReq) {
DROP_QNODE_OVER: DROP_QNODE_OVER:
if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
mError("qnode:%d, failed to drop since %s", pMnode->dnodeId, terrstr()); mError("qnode:%d, failed to drop since %s", pMnode->dnodeId, terrstr());
return -1;
} }
mndReleaseQnode(pMnode, pObj); mndReleaseQnode(pMnode, pObj);

View File

@ -294,7 +294,7 @@ static int32_t mndProcessCreateSnodeReq(SMnodeMsg *pReq) {
goto CREATE_SNODE_OVER; goto CREATE_SNODE_OVER;
} }
if (mndCheckDropNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto CREATE_SNODE_OVER; goto CREATE_SNODE_OVER;
} }
@ -403,7 +403,7 @@ static int32_t mndProcessDropSnodeReq(SMnodeMsg *pReq) {
goto DROP_SNODE_OVER; goto DROP_SNODE_OVER;
} }
if (mndCheckCreateNodeAuth(pUser)) { if (mndCheckOperateNodeAuth(pUser)) {
goto DROP_SNODE_OVER; goto DROP_SNODE_OVER;
} }
@ -413,7 +413,6 @@ static int32_t mndProcessDropSnodeReq(SMnodeMsg *pReq) {
DROP_SNODE_OVER: DROP_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 drop since %s", pMnode->dnodeId, terrstr()); mError("snode:%d, failed to drop since %s", pMnode->dnodeId, terrstr());
return -1;
} }
mndReleaseSnode(pMnode, pObj); mndReleaseSnode(pMnode, pObj);

View File

@ -43,123 +43,147 @@ TEST_F(MndTestFunc, 01_Show_Func) {
TEST_F(MndTestFunc, 02_Create_Func) { TEST_F(MndTestFunc, 02_Create_Func) {
{ {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
strcpy(createReq.name, "");
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "");
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_COMMENT); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_COMMENT);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
createReq.commentSize = TSDB_FUNC_COMMENT_LEN + 1;
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
pReq->commentSize = htonl(TSDB_FUNC_COMMENT_LEN + 1);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_COMMENT); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_COMMENT);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
pReq->commentSize = htonl(TSDB_FUNC_COMMENT_LEN);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
createReq.codeSize = TSDB_FUNC_CODE_LEN + 1;
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
pReq->commentSize = htonl(TSDB_FUNC_COMMENT_LEN);
pReq->codeSize = htonl(TSDB_FUNC_CODE_LEN - 1);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq) + 24; SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
createReq.codeSize = TSDB_FUNC_CODE_LEN;
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
pReq->commentSize = htonl(TSDB_FUNC_COMMENT_LEN);
pReq->codeSize = htonl(TSDB_FUNC_CODE_LEN);
pReq->pCont[0] = 0;
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq) + 24; SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
createReq.codeSize = TSDB_FUNC_CODE_LEN;
createReq.pCode[0] = 'a';
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
pReq->commentSize = htonl(TSDB_FUNC_COMMENT_LEN);
pReq->codeSize = htonl(TSDB_FUNC_CODE_LEN);
pReq->pCont[0] = 'a';
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE);
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq) + 24; SCreateFuncReq createReq = {0};
strcpy(createReq.name, "f1");
createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
createReq.codeSize = TSDB_FUNC_CODE_LEN;
createReq.pCode[0] = 'a';
createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;
int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
void* pReq = rpcMallocCont(contLen);
tSerializeSCreateFuncReq(pReq, contLen, &createReq);
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen);
strcpy(pReq->name, "f1");
pReq->commentSize = htonl(TSDB_FUNC_COMMENT_LEN);
pReq->codeSize = htonl(TSDB_FUNC_CODE_LEN);
pReq->pCont[0] = 'a';
pReq->bufSize = htonl(TSDB_FUNC_BUF_SIZE + 1);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE);
} }
for (int32_t i = 0; i < 3; ++i) { for (int32_t i = 0; i < 3; ++i) {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
int32_t commentSize = TSDB_FUNC_COMMENT_LEN; strcpy(createReq.name, "f1");
int32_t codeSize = TSDB_FUNC_CODE_LEN; createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
contLen = (contLen + codeSize + commentSize); createReq.codeSize = TSDB_FUNC_CODE_LEN;
createReq.pCode[0] = 'a';
createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;
createReq.igExists = 0;
if (i == 2) createReq.igExists = 1;
createReq.funcType = 1;
createReq.scriptType = 2;
createReq.outputType = TSDB_DATA_TYPE_SMALLINT;
createReq.outputLen = 12;
createReq.bufSize = 4;
createReq.signature = 5;
for (int32_t i = 0; i < createReq.commentSize - 1; ++i) {
createReq.pComment[i] = 'm';
}
for (int32_t i = 0; i < createReq.codeSize - 1; ++i) {
createReq.pCode[i] = 'd';
}
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
strcpy(pReq->name, "f1"); void* pReq = rpcMallocCont(contLen);
pReq->igExists = 0; tSerializeSCreateFuncReq(pReq, contLen, &createReq);
if (i == 2) pReq->igExists = 1;
pReq->funcType = 1;
pReq->scriptType = 2;
pReq->outputType = TSDB_DATA_TYPE_SMALLINT;
pReq->outputLen = htonl(12);
pReq->bufSize = htonl(4);
pReq->signature = htobe64(5);
pReq->commentSize = htonl(commentSize);
pReq->codeSize = htonl(codeSize);
for (int32_t i = 0; i < commentSize - 1; ++i) {
pReq->pCont[i] = 'm';
}
for (int32_t i = commentSize; i < commentSize + codeSize - 1; ++i) {
pReq->pCont[i] = 'd';
}
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -187,27 +211,26 @@ TEST_F(MndTestFunc, 02_Create_Func) {
TEST_F(MndTestFunc, 03_Retrieve_Func) { TEST_F(MndTestFunc, 03_Retrieve_Func) {
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = 1; retrieveReq.numOfFuncs = 1;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
taosArrayPush(retrieveReq.pFuncNames, "f1");
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(1); void* pReq = rpcMallocCont(contLen);
strcpy(pReq->pFuncNames, "f1"); tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, 0); ASSERT_EQ(pRsp->code, 0);
SRetrieveFuncRsp* pRetrieveRsp = (SRetrieveFuncRsp*)pRsp->pCont; SRetrieveFuncRsp retrieveRsp = {0};
pRetrieveRsp->numOfFuncs = htonl(pRetrieveRsp->numOfFuncs); tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
SFuncInfo* pFuncInfo = (SFuncInfo*)(pRetrieveRsp->pFuncInfos); SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
pFuncInfo->outputLen = htonl(pFuncInfo->outputLen);
pFuncInfo->bufSize = htonl(pFuncInfo->bufSize);
pFuncInfo->signature = htobe64(pFuncInfo->signature);
pFuncInfo->commentSize = htonl(pFuncInfo->commentSize);
pFuncInfo->codeSize = htonl(pFuncInfo->codeSize);
EXPECT_STREQ(pFuncInfo->name, "f1"); EXPECT_STREQ(pFuncInfo->name, "f1");
EXPECT_EQ(pFuncInfo->funcType, 1); EXPECT_EQ(pFuncInfo->funcType, 1);
@ -219,9 +242,7 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
EXPECT_EQ(pFuncInfo->commentSize, TSDB_FUNC_COMMENT_LEN); EXPECT_EQ(pFuncInfo->commentSize, TSDB_FUNC_COMMENT_LEN);
EXPECT_EQ(pFuncInfo->codeSize, TSDB_FUNC_CODE_LEN); EXPECT_EQ(pFuncInfo->codeSize, TSDB_FUNC_CODE_LEN);
char* pComment = pFuncInfo->pCont; char comments[TSDB_FUNC_COMMENT_LEN] = {0};
char* pCode = pFuncInfo->pCont + pFuncInfo->commentSize;
char comments[TSDB_FUNC_COMMENT_LEN] = {0};
for (int32_t i = 0; i < TSDB_FUNC_COMMENT_LEN - 1; ++i) { for (int32_t i = 0; i < TSDB_FUNC_COMMENT_LEN - 1; ++i) {
comments[i] = 'm'; comments[i] = 'm';
} }
@ -229,17 +250,20 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
for (int32_t i = 0; i < TSDB_FUNC_CODE_LEN - 1; ++i) { for (int32_t i = 0; i < TSDB_FUNC_CODE_LEN - 1; ++i) {
codes[i] = 'd'; codes[i] = 'd';
} }
EXPECT_STREQ(pComment, comments); EXPECT_STREQ(comments, pFuncInfo->pComment);
EXPECT_STREQ(pCode, codes); EXPECT_STREQ(codes, pFuncInfo->pCode);
taosArrayDestroy(retrieveRsp.pFuncInfos);
} }
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = 0; retrieveReq.numOfFuncs = 0;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(numOfFuncs); void* pReq = rpcMallocCont(contLen);
tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -247,12 +271,17 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
} }
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = TSDB_FUNC_MAX_RETRIEVE + 1; retrieveReq.numOfFuncs = TSDB_FUNC_MAX_RETRIEVE + 1;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(TSDB_FUNC_MAX_RETRIEVE + 1, TSDB_FUNC_NAME_LEN);
for (int32_t i = 0; i < TSDB_FUNC_MAX_RETRIEVE + 1; ++i) {
taosArrayPush(retrieveReq.pFuncNames, "1");
}
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(numOfFuncs); void* pReq = rpcMallocCont(contLen);
tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -260,13 +289,15 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
} }
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = 1; retrieveReq.numOfFuncs = 1;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
taosArrayPush(retrieveReq.pFuncNames, "f2");
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(numOfFuncs); void* pReq = rpcMallocCont(contLen);
strcpy(pReq->pFuncNames, "f2"); tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -274,28 +305,27 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
} }
{ {
int32_t contLen = sizeof(SCreateFuncReq); SCreateFuncReq createReq = {0};
int32_t commentSize = 1024; strcpy(createReq.name, "f2");
int32_t codeSize = 9527; createReq.commentSize = 1024;
contLen = (contLen + codeSize + commentSize); createReq.codeSize = 9527;
createReq.igExists = 1;
createReq.funcType = 2;
createReq.scriptType = 3;
createReq.outputType = TSDB_DATA_TYPE_BINARY;
createReq.outputLen = 24;
createReq.bufSize = 6;
createReq.signature = 18;
for (int32_t i = 0; i < createReq.commentSize - 1; ++i) {
createReq.pComment[i] = 'p';
}
for (int32_t i = 0; i < createReq.codeSize - 1; ++i) {
createReq.pCode[i] = 'q';
}
SCreateFuncReq* pReq = (SCreateFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
strcpy(pReq->name, "f2"); void* pReq = rpcMallocCont(contLen);
pReq->igExists = 1; tSerializeSCreateFuncReq(pReq, contLen, &createReq);
pReq->funcType = 2;
pReq->scriptType = 3;
pReq->outputType = TSDB_DATA_TYPE_BINARY;
pReq->outputLen = htonl(24);
pReq->bufSize = htonl(6);
pReq->signature = htobe64(18);
pReq->commentSize = htonl(commentSize);
pReq->codeSize = htonl(codeSize);
for (int32_t i = 0; i < commentSize - 1; ++i) {
pReq->pCont[i] = 'p';
}
for (int32_t i = commentSize; i < commentSize + codeSize - 1; ++i) {
pReq->pCont[i] = 'q';
}
SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -309,27 +339,26 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
} }
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = 1; retrieveReq.numOfFuncs = 1;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
taosArrayPush(retrieveReq.pFuncNames, "f2");
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(1); void* pReq = rpcMallocCont(contLen);
strcpy(pReq->pFuncNames, "f2"); tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, 0); ASSERT_EQ(pRsp->code, 0);
SRetrieveFuncRsp* pRetrieveRsp = (SRetrieveFuncRsp*)pRsp->pCont; SRetrieveFuncRsp retrieveRsp = {0};
pRetrieveRsp->numOfFuncs = htonl(pRetrieveRsp->numOfFuncs); tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
SFuncInfo* pFuncInfo = (SFuncInfo*)(pRetrieveRsp->pFuncInfos); SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
pFuncInfo->outputLen = htonl(pFuncInfo->outputLen);
pFuncInfo->bufSize = htonl(pFuncInfo->bufSize);
pFuncInfo->signature = htobe64(pFuncInfo->signature);
pFuncInfo->commentSize = htonl(pFuncInfo->commentSize);
pFuncInfo->codeSize = htonl(pFuncInfo->codeSize);
EXPECT_STREQ(pFuncInfo->name, "f2"); EXPECT_STREQ(pFuncInfo->name, "f2");
EXPECT_EQ(pFuncInfo->funcType, 2); EXPECT_EQ(pFuncInfo->funcType, 2);
@ -341,47 +370,43 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
EXPECT_EQ(pFuncInfo->commentSize, 1024); EXPECT_EQ(pFuncInfo->commentSize, 1024);
EXPECT_EQ(pFuncInfo->codeSize, 9527); EXPECT_EQ(pFuncInfo->codeSize, 9527);
char* pComment = pFuncInfo->pCont; char comments[TSDB_FUNC_COMMENT_LEN] = {0};
char* pCode = pFuncInfo->pCont + pFuncInfo->commentSize;
char* comments = (char*)calloc(1, 1024);
for (int32_t i = 0; i < 1024 - 1; ++i) { for (int32_t i = 0; i < 1024 - 1; ++i) {
comments[i] = 'p'; comments[i] = 'p';
} }
char* codes = (char*)calloc(1, 9527); char codes[TSDB_FUNC_CODE_LEN] = {0};
for (int32_t i = 0; i < 9527 - 1; ++i) { for (int32_t i = 0; i < 9527 - 1; ++i) {
codes[i] = 'q'; codes[i] = 'q';
} }
EXPECT_STREQ(pComment, comments);
EXPECT_STREQ(pCode, codes); EXPECT_STREQ(comments, pFuncInfo->pComment);
free(comments); EXPECT_STREQ(codes, pFuncInfo->pCode);
free(codes); taosArrayDestroy(retrieveRsp.pFuncInfos);
} }
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = 2; retrieveReq.numOfFuncs = 2;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
taosArrayPush(retrieveReq.pFuncNames, "f2");
taosArrayPush(retrieveReq.pFuncNames, "f1");
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(numOfFuncs); void* pReq = rpcMallocCont(contLen);
strcpy(pReq->pFuncNames, "f2"); tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
strcpy((char*)pReq->pFuncNames + TSDB_FUNC_NAME_LEN, "f1"); taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, 0); ASSERT_EQ(pRsp->code, 0);
SRetrieveFuncRsp* pRetrieveRsp = (SRetrieveFuncRsp*)pRsp->pCont; SRetrieveFuncRsp retrieveRsp = {0};
pRetrieveRsp->numOfFuncs = htonl(pRetrieveRsp->numOfFuncs); tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
EXPECT_EQ(retrieveRsp.numOfFuncs, 2);
EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
{ {
SFuncInfo* pFuncInfo = (SFuncInfo*)(pRetrieveRsp->pFuncInfos); SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
pFuncInfo->outputLen = htonl(pFuncInfo->outputLen);
pFuncInfo->bufSize = htonl(pFuncInfo->bufSize);
pFuncInfo->signature = htobe64(pFuncInfo->signature);
pFuncInfo->commentSize = htonl(pFuncInfo->commentSize);
pFuncInfo->codeSize = htonl(pFuncInfo->codeSize);
EXPECT_STREQ(pFuncInfo->name, "f2"); EXPECT_STREQ(pFuncInfo->name, "f2");
EXPECT_EQ(pFuncInfo->funcType, 2); EXPECT_EQ(pFuncInfo->funcType, 2);
EXPECT_EQ(pFuncInfo->scriptType, 3); EXPECT_EQ(pFuncInfo->scriptType, 3);
@ -392,29 +417,21 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
EXPECT_EQ(pFuncInfo->commentSize, 1024); EXPECT_EQ(pFuncInfo->commentSize, 1024);
EXPECT_EQ(pFuncInfo->codeSize, 9527); EXPECT_EQ(pFuncInfo->codeSize, 9527);
char* pComment = pFuncInfo->pCont; char comments[TSDB_FUNC_COMMENT_LEN] = {0};
char* pCode = pFuncInfo->pCont + pFuncInfo->commentSize;
char* comments = (char*)calloc(1, 1024);
for (int32_t i = 0; i < 1024 - 1; ++i) { for (int32_t i = 0; i < 1024 - 1; ++i) {
comments[i] = 'p'; comments[i] = 'p';
} }
char* codes = (char*)calloc(1, 9527); char codes[TSDB_FUNC_CODE_LEN] = {0};
for (int32_t i = 0; i < 9527 - 1; ++i) { for (int32_t i = 0; i < 9527 - 1; ++i) {
codes[i] = 'q'; codes[i] = 'q';
} }
EXPECT_STREQ(pComment, comments);
EXPECT_STREQ(pCode, codes); EXPECT_STREQ(comments, pFuncInfo->pComment);
free(comments); EXPECT_STREQ(codes, pFuncInfo->pCode);
free(codes);
} }
{ {
SFuncInfo* pFuncInfo = (SFuncInfo*)(pRetrieveRsp->pFuncInfos + sizeof(SFuncInfo) + 1024 + 9527); SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 1);
pFuncInfo->outputLen = htonl(pFuncInfo->outputLen);
pFuncInfo->bufSize = htonl(pFuncInfo->bufSize);
pFuncInfo->signature = htobe64(pFuncInfo->signature);
pFuncInfo->commentSize = htonl(pFuncInfo->commentSize);
pFuncInfo->codeSize = htonl(pFuncInfo->codeSize);
EXPECT_STREQ(pFuncInfo->name, "f1"); EXPECT_STREQ(pFuncInfo->name, "f1");
EXPECT_EQ(pFuncInfo->funcType, 1); EXPECT_EQ(pFuncInfo->funcType, 1);
EXPECT_EQ(pFuncInfo->scriptType, 2); EXPECT_EQ(pFuncInfo->scriptType, 2);
@ -425,9 +442,7 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
EXPECT_EQ(pFuncInfo->commentSize, TSDB_FUNC_COMMENT_LEN); EXPECT_EQ(pFuncInfo->commentSize, TSDB_FUNC_COMMENT_LEN);
EXPECT_EQ(pFuncInfo->codeSize, TSDB_FUNC_CODE_LEN); EXPECT_EQ(pFuncInfo->codeSize, TSDB_FUNC_CODE_LEN);
char* pComment = pFuncInfo->pCont; char comments[TSDB_FUNC_COMMENT_LEN] = {0};
char* pCode = pFuncInfo->pCont + pFuncInfo->commentSize;
char comments[TSDB_FUNC_COMMENT_LEN] = {0};
for (int32_t i = 0; i < TSDB_FUNC_COMMENT_LEN - 1; ++i) { for (int32_t i = 0; i < TSDB_FUNC_COMMENT_LEN - 1; ++i) {
comments[i] = 'm'; comments[i] = 'm';
} }
@ -435,33 +450,38 @@ TEST_F(MndTestFunc, 03_Retrieve_Func) {
for (int32_t i = 0; i < TSDB_FUNC_CODE_LEN - 1; ++i) { for (int32_t i = 0; i < TSDB_FUNC_CODE_LEN - 1; ++i) {
codes[i] = 'd'; codes[i] = 'd';
} }
EXPECT_STREQ(pComment, comments); EXPECT_STREQ(comments, pFuncInfo->pComment);
EXPECT_STREQ(pCode, codes); EXPECT_STREQ(codes, pFuncInfo->pCode);
} }
taosArrayDestroy(retrieveRsp.pFuncInfos);
} }
{ {
int32_t contLen = sizeof(SRetrieveFuncReq); SRetrieveFuncReq retrieveReq = {0};
int32_t numOfFuncs = 2; retrieveReq.numOfFuncs = 2;
contLen = (contLen + numOfFuncs * TSDB_FUNC_NAME_LEN); retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
taosArrayPush(retrieveReq.pFuncNames, "f2");
taosArrayPush(retrieveReq.pFuncNames, "f3");
SRetrieveFuncReq* pReq = (SRetrieveFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
pReq->numOfFuncs = htonl(numOfFuncs); void* pReq = rpcMallocCont(contLen);
strcpy(pReq->pFuncNames, "f2"); tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
strcpy((char*)pReq->pFuncNames + TSDB_FUNC_NAME_LEN, "f3"); taosArrayDestroy(retrieveReq.pFuncNames);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC); ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC);
} }
} }
TEST_F(MndTestFunc, 04_Drop_Func) { TEST_F(MndTestFunc, 04_Drop_Func) {
{ {
int32_t contLen = sizeof(SDropFuncReq); SDropFuncReq dropReq = {0};
strcpy(dropReq.name, "");
SDropFuncReq* pReq = (SDropFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
strcpy(pReq->name, ""); void* pReq = rpcMallocCont(contLen);
tSerializeSDropFuncReq(pReq, contLen, &dropReq);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -469,10 +489,12 @@ TEST_F(MndTestFunc, 04_Drop_Func) {
} }
{ {
int32_t contLen = sizeof(SDropFuncReq); SDropFuncReq dropReq = {0};
strcpy(dropReq.name, "f3");
SDropFuncReq* pReq = (SDropFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
strcpy(pReq->name, "f3"); void* pReq = rpcMallocCont(contLen);
tSerializeSDropFuncReq(pReq, contLen, &dropReq);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -480,11 +502,13 @@ TEST_F(MndTestFunc, 04_Drop_Func) {
} }
{ {
int32_t contLen = sizeof(SDropFuncReq); SDropFuncReq dropReq = {0};
strcpy(dropReq.name, "f3");
dropReq.igNotExists = 1;
SDropFuncReq* pReq = (SDropFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
strcpy(pReq->name, "f3"); void* pReq = rpcMallocCont(contLen);
pReq->igNotExists = 1; tSerializeSDropFuncReq(pReq, contLen, &dropReq);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -492,10 +516,13 @@ TEST_F(MndTestFunc, 04_Drop_Func) {
} }
{ {
int32_t contLen = sizeof(SDropFuncReq); SDropFuncReq dropReq = {0};
strcpy(dropReq.name, "f1");
dropReq.igNotExists = 1;
SDropFuncReq* pReq = (SDropFuncReq*)rpcMallocCont(contLen); int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
strcpy(pReq->name, "f1"); void* pReq = rpcMallocCont(contLen);
tSerializeSDropFuncReq(pReq, contLen, &dropReq);
SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
ASSERT_NE(pRsp, nullptr); ASSERT_NE(pRsp, nullptr);
@ -517,5 +544,5 @@ TEST_F(MndTestFunc, 04_Drop_Func) {
test.SendShowRetrieveReq(); test.SendShowRetrieveReq();
EXPECT_EQ(test.GetShowRows(), 1); EXPECT_EQ(test.GetShowRows(), 1);
CheckBinary("f2", TSDB_FUNC_NAME_LEN); CheckBinary("f2", TSDB_FUNC_NAME_LEN);
} }