mndFunc.c 21.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndFunc.h"
18
#include "mndPrivilege.h"
S
Shengliang Guan 已提交
19 20 21
#include "mndShow.h"
#include "mndSync.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
22
#include "mndUser.h"
S
Shengliang Guan 已提交
23

“happyguoxy” 已提交
24
#define SDB_FUNC_VER          2
S
Shengliang Guan 已提交
25
#define SDB_FUNC_RESERVE_SIZE 64
S
Shengliang Guan 已提交
26 27 28 29 30

static SSdbRaw *mndFuncActionEncode(SFuncObj *pFunc);
static SSdbRow *mndFuncActionDecode(SSdbRaw *pRaw);
static int32_t  mndFuncActionInsert(SSdb *pSdb, SFuncObj *pFunc);
static int32_t  mndFuncActionDelete(SSdb *pSdb, SFuncObj *pFunc);
S
Shengliang 已提交
31
static int32_t  mndFuncActionUpdate(SSdb *pSdb, SFuncObj *pOld, SFuncObj *pNew);
S
Shengliang Guan 已提交
32 33 34 35 36 37
static int32_t  mndCreateFunc(SMnode *pMnode, SRpcMsg *pReq, SCreateFuncReq *pCreate);
static int32_t  mndDropFunc(SMnode *pMnode, SRpcMsg *pReq, SFuncObj *pFunc);
static int32_t  mndProcessCreateFuncReq(SRpcMsg *pReq);
static int32_t  mndProcessDropFuncReq(SRpcMsg *pReq);
static int32_t  mndProcessRetrieveFuncReq(SRpcMsg *pReq);
static int32_t  mndRetrieveFuncs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
38 39 40
static void     mndCancelGetNextFunc(SMnode *pMnode, void *pIter);

int32_t mndInitFunc(SMnode *pMnode) {
S
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49
  SSdbTable table = {
      .sdbType = SDB_FUNC,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndFuncActionEncode,
      .decodeFp = (SdbDecodeFp)mndFuncActionDecode,
      .insertFp = (SdbInsertFp)mndFuncActionInsert,
      .updateFp = (SdbUpdateFp)mndFuncActionUpdate,
      .deleteFp = (SdbDeleteFp)mndFuncActionDelete,
  };
S
Shengliang Guan 已提交
50

S
Shengliang 已提交
51 52 53
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_FUNC, mndProcessCreateFuncReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_FUNC, mndProcessDropFuncReq);
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_FUNC, mndProcessRetrieveFuncReq);
S
Shengliang Guan 已提交
54

S
Shengliang 已提交
55 56
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_FUNC, mndRetrieveFuncs);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_FUNC, mndCancelGetNextFunc);
S
Shengliang Guan 已提交
57

S
Shengliang Guan 已提交
58 59 60 61 62 63
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupFunc(SMnode *pMnode) {}

static SSdbRaw *mndFuncActionEncode(SFuncObj *pFunc) {
64 65
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
66
  int32_t  size = pFunc->commentSize + pFunc->codeSize + sizeof(SFuncObj) + SDB_FUNC_RESERVE_SIZE;
S
Shengliang Guan 已提交
67
  SSdbRaw *pRaw = sdbAllocRaw(SDB_FUNC, SDB_FUNC_VER, size);
68
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
69 70

  int32_t dataPos = 0;
71 72 73 74 75 76 77 78 79 80 81
  SDB_SET_BINARY(pRaw, dataPos, pFunc->name, TSDB_FUNC_NAME_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pFunc->createdTime, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->funcType, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->scriptType, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->align, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->outputType, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->outputLen, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->bufSize, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pFunc->signature, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->commentSize, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->codeSize, _OVER)
82 83 84
  if (pFunc->commentSize > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pFunc->pComment, pFunc->commentSize, _OVER)
  }
85
  SDB_SET_BINARY(pRaw, dataPos, pFunc->pCode, pFunc->codeSize, _OVER)
86
  SDB_SET_INT32(pRaw, dataPos, pFunc->funcVersion, _OVER)
87 88
  SDB_SET_RESERVE(pRaw, dataPos, SDB_FUNC_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER);
89 90 91

  terrno = 0;

92
_OVER:
93 94 95 96 97
  if (terrno != 0) {
    mError("func:%s, failed to encode to raw:%p since %s", pFunc->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
98

S
Shengliang Guan 已提交
99
  mTrace("func:%s, encode to raw:%p, row:%p", pFunc->name, pRaw, pFunc);
S
Shengliang Guan 已提交
100 101 102 103
  return pRaw;
}

static SSdbRow *mndFuncActionDecode(SSdbRaw *pRaw) {
104
  terrno = TSDB_CODE_OUT_OF_MEMORY;
105 106
  SSdbRow  *pRow = NULL;
  SFuncObj *pFunc = NULL;
107

S
Shengliang Guan 已提交
108
  int8_t sver = 0;
109
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
110

“happyguoxy” 已提交
111
  if (sver != 1 && sver != 2) {
S
Shengliang Guan 已提交
112
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
113
    goto _OVER;
S
Shengliang Guan 已提交
114 115
  }

116
  pRow = sdbAllocRow(sizeof(SFuncObj));
117
  if (pRow == NULL) goto _OVER;
118

119
  pFunc = sdbGetRowObj(pRow);
120
  if (pFunc == NULL) goto _OVER;
S
Shengliang Guan 已提交
121 122

  int32_t dataPos = 0;
123 124 125 126 127 128 129 130 131 132 133
  SDB_GET_BINARY(pRaw, dataPos, pFunc->name, TSDB_FUNC_NAME_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pFunc->createdTime, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->funcType, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->scriptType, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->align, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->outputType, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->outputLen, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->bufSize, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pFunc->signature, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->commentSize, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->codeSize, _OVER)
S
Shengliang 已提交
134

135 136 137 138 139 140 141 142
  if (pFunc->commentSize > 0) {
    pFunc->pComment = taosMemoryCalloc(1, pFunc->commentSize);
    if (pFunc->pComment == NULL) {
      goto _OVER;
    }
    SDB_GET_BINARY(pRaw, dataPos, pFunc->pComment, pFunc->commentSize, _OVER)
  }

wafwerar's avatar
wafwerar 已提交
143
  pFunc->pCode = taosMemoryCalloc(1, pFunc->codeSize);
144
  if (pFunc->pCode == NULL) {
145
    goto _OVER;
S
Shengliang 已提交
146
  }
147
  SDB_GET_BINARY(pRaw, dataPos, pFunc->pCode, pFunc->codeSize, _OVER)
“happyguoxy” 已提交
148 149
  
  if(sver >= 2){
150
    SDB_GET_INT32(pRaw, dataPos, &pFunc->funcVersion, _OVER)
“happyguoxy” 已提交
151 152
  }

153
  SDB_GET_RESERVE(pRaw, dataPos, SDB_FUNC_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
154

“happyguoxy” 已提交
155 156
  taosInitRWLatch(&pFunc->lock);

157 158
  terrno = 0;

159
_OVER:
160
  if (terrno != 0) {
161
    mError("func:%s, failed to decode from raw:%p since %s", pFunc == NULL ? "null" : pFunc->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
162
    taosMemoryFreeClear(pRow);
163 164 165 166
    return NULL;
  }

  mTrace("func:%s, decode from raw:%p, row:%p", pFunc->name, pRaw, pFunc);
S
Shengliang Guan 已提交
167 168 169 170
  return pRow;
}

static int32_t mndFuncActionInsert(SSdb *pSdb, SFuncObj *pFunc) {
171
  mTrace("func:%s, perform insert action, row:%p", pFunc->name, pFunc);
S
Shengliang Guan 已提交
172 173 174 175
  return 0;
}

static int32_t mndFuncActionDelete(SSdb *pSdb, SFuncObj *pFunc) {
176
  mTrace("func:%s, perform delete action, row:%p", pFunc->name, pFunc);
wafwerar's avatar
wafwerar 已提交
177 178
  taosMemoryFreeClear(pFunc->pCode);
  taosMemoryFreeClear(pFunc->pComment);
S
Shengliang Guan 已提交
179 180 181
  return 0;
}

S
Shengliang 已提交
182 183
static int32_t mndFuncActionUpdate(SSdb *pSdb, SFuncObj *pOld, SFuncObj *pNew) {
  mTrace("func:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
“happyguoxy” 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  
  taosWLockLatch(&pOld->lock);

  pOld->align = pNew->align;
  pOld->bufSize = pNew->bufSize;
  pOld->codeSize = pNew->codeSize;
  pOld->commentSize = pNew->commentSize;
  pOld->createdTime = pNew->createdTime;
  pOld->funcType = pNew->funcType;
  pOld->funcVersion = pNew->funcVersion;
  pOld->outputLen = pNew->outputLen;
  pOld->outputType = pNew->outputType;

  if(pOld->pComment != NULL){
    taosMemoryFree(pOld->pComment);
    pOld->pComment = NULL;
  }
  if(pNew->commentSize > 0 && pNew->pComment != NULL){
    pOld->commentSize = pNew->commentSize;
    pOld->pComment = taosMemoryMalloc(pOld->commentSize);
    memcpy(pOld->pComment, pNew->pComment, pOld->commentSize);
  }

  if(pOld->pCode != NULL){
    taosMemoryFree(pOld->pCode);
    pOld->pCode = NULL;
  }
  if(pNew->codeSize > 0 && pNew->pCode != NULL){
    pOld->codeSize = pNew->codeSize;
    pOld->pCode = taosMemoryMalloc(pOld->codeSize);
    memcpy(pOld->pCode, pNew->pCode, pOld->codeSize);
  }

  pOld->scriptType = pNew->scriptType;
  pOld->signature = pNew->signature;
  
  taosWUnLockLatch(&pOld->lock);

S
Shengliang Guan 已提交
222 223 224
  return 0;
}

S
Shengliang 已提交
225 226 227 228 229 230 231 232
static SFuncObj *mndAcquireFunc(SMnode *pMnode, char *funcName) {
  SSdb     *pSdb = pMnode->pSdb;
  SFuncObj *pFunc = sdbAcquire(pSdb, SDB_FUNC, funcName);
  if (pFunc == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
    terrno = TSDB_CODE_MND_FUNC_NOT_EXIST;
  }
  return pFunc;
}
S
Shengliang Guan 已提交
233

S
Shengliang 已提交
234 235 236 237 238
static void mndReleaseFunc(SMnode *pMnode, SFuncObj *pFunc) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pFunc);
}

S
Shengliang Guan 已提交
239
static int32_t mndCreateFunc(SMnode *pMnode, SRpcMsg *pReq, SCreateFuncReq *pCreate) {
S
Shengliang 已提交
240 241 242
  int32_t code = -1;
  STrans *pTrans = NULL;

C
Cary Xu 已提交
243 244 245 246
  if ((terrno = grantCheck(TSDB_GRANT_USER)) < 0) {
    return code;
  }

S
Shengliang 已提交
247 248 249 250 251 252 253 254 255
  SFuncObj func = {0};
  memcpy(func.name, pCreate->name, TSDB_FUNC_NAME_LEN);
  func.createdTime = taosGetTimestampMs();
  func.funcType = pCreate->funcType;
  func.scriptType = pCreate->scriptType;
  func.outputType = pCreate->outputType;
  func.outputLen = pCreate->outputLen;
  func.bufSize = pCreate->bufSize;
  func.signature = pCreate->signature;
256 257 258 259 260
  if (NULL != pCreate->pComment) {
    func.commentSize = strlen(pCreate->pComment) + 1;
    func.pComment = taosMemoryMalloc(func.commentSize);
  }
  func.codeSize = pCreate->codeLen;
wafwerar's avatar
wafwerar 已提交
261
  func.pCode = taosMemoryMalloc(func.codeSize);
S
Shengliang 已提交
262 263
  if (func.pCode == NULL || func.pCode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
264
    goto _OVER;
S
Shengliang Guan 已提交
265 266
  }

267 268 269
  if (func.commentSize > 0) {
    memcpy(func.pComment, pCreate->pComment, func.commentSize);
  }
S
Shengliang Guan 已提交
270
  memcpy(func.pCode, pCreate->pCode, func.codeSize);
“happyguoxy” 已提交
271
  
272
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-func");
273
  if (pTrans == NULL) goto _OVER;
274
  mInfo("trans:%d, used to create func:%s", pTrans->id, pCreate->name);
S
Shengliang Guan 已提交
275

“happyguoxy” 已提交
276 277 278
  SFuncObj *oldFunc = mndAcquireFunc(pMnode, pCreate->name);
  if(pCreate->orReplace == 1 && oldFunc != NULL){
    func.funcVersion = oldFunc->funcVersion + 1;
S
Shengliang Guan 已提交
279

“happyguoxy” 已提交
280 281 282
    SSdbRaw *pRedoRaw = mndFuncActionEncode(oldFunc);
    if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto _OVER;
    if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) goto _OVER;
S
Shengliang Guan 已提交
283

“happyguoxy” 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    SSdbRaw *pUndoRaw = mndFuncActionEncode(oldFunc);
    if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto _OVER;
    if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY) != 0) goto _OVER;

    SSdbRaw *pCommitRaw = mndFuncActionEncode(&func);
    if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) goto _OVER;
    if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) goto _OVER;
  }
  else{
    SSdbRaw *pRedoRaw = mndFuncActionEncode(&func);
    if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto _OVER;
    if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) goto _OVER;

    SSdbRaw *pUndoRaw = mndFuncActionEncode(&func);
    if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto _OVER;
    if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) goto _OVER;

    SSdbRaw *pCommitRaw = mndFuncActionEncode(&func);
    if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) goto _OVER;
    if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) goto _OVER;
  }
S
Shengliang Guan 已提交
305

306
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
307

S
Shengliang 已提交
308 309
  code = 0;

310
_OVER:
“happyguoxy” 已提交
311 312 313 314
  if(oldFunc != NULL){
    mndReleaseFunc(pMnode, oldFunc);
  }

wafwerar's avatar
wafwerar 已提交
315 316
  taosMemoryFree(func.pCode);
  taosMemoryFree(func.pComment);
S
Shengliang Guan 已提交
317
  mndTransDrop(pTrans);
S
Shengliang 已提交
318
  return code;
S
Shengliang Guan 已提交
319 320
}

S
Shengliang Guan 已提交
321
static int32_t mndDropFunc(SMnode *pMnode, SRpcMsg *pReq, SFuncObj *pFunc) {
S
Shengliang 已提交
322
  int32_t code = -1;
323
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-func");
324
  if (pTrans == NULL) goto _OVER;
S
Shengliang 已提交
325

326
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pFunc->name);
S
Shengliang Guan 已提交
327 328

  SSdbRaw *pRedoRaw = mndFuncActionEncode(pFunc);
S
Shengliang Guan 已提交
329 330
  if (pRedoRaw == NULL) goto _OVER;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
331
  (void)sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING);
S
Shengliang Guan 已提交
332 333

  SSdbRaw *pUndoRaw = mndFuncActionEncode(pFunc);
S
Shengliang Guan 已提交
334 335
  if (pUndoRaw == NULL) goto _OVER;
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
336
  (void)sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
337 338

  SSdbRaw *pCommitRaw = mndFuncActionEncode(pFunc);
S
Shengliang Guan 已提交
339 340
  if (pCommitRaw == NULL) goto _OVER;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
341
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
342

343
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang 已提交
344 345

  code = 0;
S
Shengliang Guan 已提交
346

347
_OVER:
S
Shengliang Guan 已提交
348
  mndTransDrop(pTrans);
S
Shengliang 已提交
349
  return code;
S
Shengliang Guan 已提交
350 351
}

S
Shengliang Guan 已提交
352 353
static int32_t mndProcessCreateFuncReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
354 355 356 357
  int32_t        code = -1;
  SFuncObj      *pFunc = NULL;
  SCreateFuncReq createReq = {0};

S
Shengliang Guan 已提交
358
  if (tDeserializeSCreateFuncReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
359
    terrno = TSDB_CODE_INVALID_MSG;
360
    goto _OVER;
S
Shengliang Guan 已提交
361
  }
S
Shengliang Guan 已提交
362

S
Shengliang Guan 已提交
363
  mInfo("func:%s, start to create, size:%d", createReq.name, createReq.codeLen);
364
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_FUNC) != 0) {
S
Shengliang Guan 已提交
365 366
    goto _OVER;
  }
S
Shengliang Guan 已提交
367

S
Shengliang Guan 已提交
368
  pFunc = mndAcquireFunc(pMnode, createReq.name);
S
Shengliang Guan 已提交
369
  if (pFunc != NULL) {
S
Shengliang Guan 已提交
370
    if (createReq.igExists) {
371
      mInfo("func:%s, already exist, ignore exist is set", createReq.name);
S
Shengliang Guan 已提交
372
      code = 0;
373
      goto _OVER;
S
slzhou 已提交
374 375 376
    } else if (createReq.orReplace) {
      mInfo("func:%s, replace function is set", createReq.name);
      code = 0;
S
Shengliang 已提交
377 378
    } else {
      terrno = TSDB_CODE_MND_FUNC_ALREADY_EXIST;
379
      goto _OVER;
S
Shengliang 已提交
380
    }
S
Shengliang 已提交
381
  } else if (terrno == TSDB_CODE_MND_FUNC_ALREADY_EXIST) {
382
    goto _OVER;
S
Shengliang Guan 已提交
383 384
  }

S
Shengliang Guan 已提交
385
  if (createReq.name[0] == 0) {
S
Shengliang Guan 已提交
386
    terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
387
    goto _OVER;
S
Shengliang Guan 已提交
388 389
  }

390
  if (createReq.pCode == NULL) {
S
Shengliang Guan 已提交
391
    terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
392
    goto _OVER;
S
Shengliang Guan 已提交
393 394
  }

395 396
  if (createReq.codeLen <= 1) {
    terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
397
    goto _OVER;
S
Shengliang Guan 已提交
398 399
  }

400 401
  if (createReq.bufSize < 0 || createReq.bufSize > TSDB_FUNC_BUF_SIZE) {
    terrno = TSDB_CODE_MND_INVALID_FUNC_BUFSIZE;
402
    goto _OVER;
S
Shengliang Guan 已提交
403 404 405
  }

  code = mndCreateFunc(pMnode, pReq, &createReq);
S
Shengliang Guan 已提交
406
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
407

408
_OVER:
S
Shengliang Guan 已提交
409
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
410
    mError("func:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
411 412
  }

S
Shengliang Guan 已提交
413
  mndReleaseFunc(pMnode, pFunc);
414
  tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
415
  return code;
S
Shengliang Guan 已提交
416 417
}

S
Shengliang Guan 已提交
418 419
static int32_t mndProcessDropFuncReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
420 421 422 423
  int32_t      code = -1;
  SFuncObj    *pFunc = NULL;
  SDropFuncReq dropReq = {0};

S
Shengliang Guan 已提交
424
  if (tDeserializeSDropFuncReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
425
    terrno = TSDB_CODE_INVALID_MSG;
426
    goto _OVER;
S
Shengliang Guan 已提交
427
  }
S
Shengliang Guan 已提交
428

429
  mInfo("func:%s, start to drop", dropReq.name);
430
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_FUNC) != 0) {
S
Shengliang Guan 已提交
431 432
    goto _OVER;
  }
S
Shengliang Guan 已提交
433

S
Shengliang Guan 已提交
434
  if (dropReq.name[0] == 0) {
S
Shengliang Guan 已提交
435
    terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
436
    goto _OVER;
S
Shengliang Guan 已提交
437 438
  }

S
Shengliang Guan 已提交
439
  pFunc = mndAcquireFunc(pMnode, dropReq.name);
S
Shengliang Guan 已提交
440
  if (pFunc == NULL) {
S
Shengliang Guan 已提交
441
    if (dropReq.igNotExists) {
442
      mInfo("func:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
443
      code = 0;
444
      goto _OVER;
S
Shengliang 已提交
445 446
    } else {
      terrno = TSDB_CODE_MND_FUNC_NOT_EXIST;
447
      goto _OVER;
S
Shengliang 已提交
448
    }
S
Shengliang Guan 已提交
449 450
  }

S
Shengliang Guan 已提交
451
  code = mndDropFunc(pMnode, pReq, pFunc);
S
Shengliang Guan 已提交
452
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang 已提交
453

454
_OVER:
S
Shengliang Guan 已提交
455
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
456
    mError("func:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
457 458
  }

S
Shengliang Guan 已提交
459 460
  mndReleaseFunc(pMnode, pFunc);
  return code;
S
Shengliang Guan 已提交
461 462
}

S
Shengliang Guan 已提交
463 464
static int32_t mndProcessRetrieveFuncReq(SRpcMsg *pReq) {
  SMnode          *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
465 466 467 468
  int32_t          code = -1;
  SRetrieveFuncReq retrieveReq = {0};
  SRetrieveFuncRsp retrieveRsp = {0};

S
Shengliang Guan 已提交
469
  if (tDeserializeSRetrieveFuncReq(pReq->pCont, pReq->contLen, &retrieveReq) != 0) {
S
Shengliang Guan 已提交
470 471 472
    terrno = TSDB_CODE_INVALID_MSG;
    goto RETRIEVE_FUNC_OVER;
  }
S
Shengliang Guan 已提交
473

S
Shengliang Guan 已提交
474
  if (retrieveReq.numOfFuncs <= 0 || retrieveReq.numOfFuncs > TSDB_FUNC_MAX_RETRIEVE) {
S
Shengliang 已提交
475
    terrno = TSDB_CODE_MND_INVALID_FUNC_RETRIEVE;
S
Shengliang Guan 已提交
476
    goto RETRIEVE_FUNC_OVER;
S
Shengliang 已提交
477
  }
S
Shengliang Guan 已提交
478

S
Shengliang Guan 已提交
479 480 481
  retrieveRsp.numOfFuncs = retrieveReq.numOfFuncs;
  retrieveRsp.pFuncInfos = taosArrayInit(retrieveReq.numOfFuncs, sizeof(SFuncInfo));
  if (retrieveRsp.pFuncInfos == NULL) {
S
Shengliang 已提交
482
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
483
    goto RETRIEVE_FUNC_OVER;
S
Shengliang 已提交
484 485
  }

486 487
  retrieveRsp.pFuncExtraInfos = taosArrayInit(retrieveReq.numOfFuncs, sizeof(SFuncExtraInfo));
  if (retrieveRsp.pFuncExtraInfos == NULL) {
“happyguoxy” 已提交
488 489 490 491
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto RETRIEVE_FUNC_OVER;
  }

S
Shengliang Guan 已提交
492 493
  for (int32_t i = 0; i < retrieveReq.numOfFuncs; ++i) {
    char *funcName = taosArrayGet(retrieveReq.pFuncNames, i);
S
Shengliang Guan 已提交
494

S
Shengliang 已提交
495
    SFuncObj *pFunc = mndAcquireFunc(pMnode, funcName);
S
Shengliang Guan 已提交
496
    if (pFunc == NULL) {
S
Shengliang Guan 已提交
497
      goto RETRIEVE_FUNC_OVER;
S
Shengliang Guan 已提交
498 499
    }

S
Shengliang Guan 已提交
500 501 502 503 504 505 506 507
    SFuncInfo funcInfo = {0};
    memcpy(funcInfo.name, pFunc->name, TSDB_FUNC_NAME_LEN);
    funcInfo.funcType = pFunc->funcType;
    funcInfo.scriptType = pFunc->scriptType;
    funcInfo.outputType = pFunc->outputType;
    funcInfo.outputLen = pFunc->outputLen;
    funcInfo.bufSize = pFunc->bufSize;
    funcInfo.signature = pFunc->signature;
D
dapan1121 已提交
508 509 510 511 512 513 514 515
    if (retrieveReq.ignoreCodeComment) {
      funcInfo.commentSize = 0;
      funcInfo.codeSize = 0;
    } else {
      funcInfo.commentSize = pFunc->commentSize;
      funcInfo.codeSize = pFunc->codeSize;
      funcInfo.pCode = taosMemoryCalloc(1, funcInfo.codeSize);
      if (funcInfo.pCode == NULL) {
516 517 518
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        goto RETRIEVE_FUNC_OVER;
      }
D
dapan1121 已提交
519 520 521 522 523 524 525 526 527
      memcpy(funcInfo.pCode, pFunc->pCode, pFunc->codeSize);
      if (funcInfo.commentSize > 0) {
        funcInfo.pComment = taosMemoryCalloc(1, funcInfo.commentSize);
        if (funcInfo.pComment == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto RETRIEVE_FUNC_OVER;
        }
        memcpy(funcInfo.pComment, pFunc->pComment, pFunc->commentSize);
      }
528
    }
S
Shengliang Guan 已提交
529
    taosArrayPush(retrieveRsp.pFuncInfos, &funcInfo);
530 531 532
    SFuncExtraInfo extraInfo = {0};
    extraInfo.funcVersion = pFunc->funcVersion;
    extraInfo.funcCreatedTime = pFunc->createdTime;
533
    taosArrayPush(retrieveRsp.pFuncExtraInfos, &extraInfo);
“happyguoxy” 已提交
534

S
Shengliang 已提交
535
    mndReleaseFunc(pMnode, pFunc);
S
Shengliang Guan 已提交
536 537
  }

S
Shengliang Guan 已提交
538 539 540 541 542 543 544 545 546
  int32_t contLen = tSerializeSRetrieveFuncRsp(NULL, 0, &retrieveRsp);
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto RETRIEVE_FUNC_OVER;
  }

  tSerializeSRetrieveFuncRsp(pRsp, contLen, &retrieveRsp);

S
Shengliang Guan 已提交
547 548
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
549

S
Shengliang 已提交
550 551
  code = 0;

S
Shengliang Guan 已提交
552
RETRIEVE_FUNC_OVER:
553 554
  tFreeSRetrieveFuncReq(&retrieveReq);
  tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
555 556

  return code;
S
Shengliang Guan 已提交
557 558 559 560 561 562 563 564 565
}

static void *mnodeGenTypeStr(char *buf, int32_t buflen, uint8_t type, int16_t len) {
  char *msg = "unknown";
  if (type >= sizeof(tDataTypes) / sizeof(tDataTypes[0])) {
    return msg;
  }

  if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BINARY) {
S
Shengliang Guan 已提交
566
    int32_t bytes = len > 0 ? (int32_t)(len - VARSTR_HEADER_SIZE) : len;
S
Shengliang Guan 已提交
567 568 569 570 571 572 573 574 575 576

    snprintf(buf, buflen - 1, "%s(%d)", tDataTypes[type].name, type == TSDB_DATA_TYPE_NCHAR ? bytes / 4 : bytes);
    buf[buflen - 1] = 0;

    return buf;
  }

  return tDataTypes[type].name;
}

S
Shengliang Guan 已提交
577 578
static int32_t mndRetrieveFuncs(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
579 580 581 582 583 584 585 586 587 588 589 590
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   numOfRows = 0;
  SFuncObj *pFunc = NULL;
  int32_t   cols = 0;
  char      buf[TSDB_TYPE_STR_MAX_LEN];

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_FUNC, pShow->pIter, (void **)&pFunc);
    if (pShow->pIter == NULL) break;

    cols = 0;

591
    char b1[tListLen(pFunc->name) + VARSTR_HEADER_SIZE] = {0};
592
    STR_WITH_MAXSIZE_TO_VARSTR(b1, pFunc->name, pShow->pMeta->pSchemas[cols].bytes);
593

594
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
595
    colDataSetVal(pColInfo, numOfRows, (const char *)b1, false);
596

D
dapan1121 已提交
597
    if (pFunc->pComment) {
598 599
      char *b2 = taosMemoryCalloc(1, pShow->pMeta->pSchemas[cols].bytes);
      STR_WITH_MAXSIZE_TO_VARSTR(b2, pFunc->pComment, pShow->pMeta->pSchemas[cols].bytes);
600

D
dapan1121 已提交
601
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
602
      colDataSetVal(pColInfo, numOfRows, (const char *)b2, false);
603
      taosMemoryFree(b2);
D
dapan1121 已提交
604 605
    } else {
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
606
      colDataSetVal(pColInfo, numOfRows, NULL, true);
D
dapan1121 已提交
607
    }
608 609

    int32_t isAgg = (pFunc->funcType == TSDB_FUNC_TYPE_AGGREGATE) ? 1 : 0;
S
Shengliang Guan 已提交
610

611
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
612
    colDataSetVal(pColInfo, numOfRows, (const char *)&isAgg, false);
S
Shengliang Guan 已提交
613

wafwerar's avatar
wafwerar 已提交
614
    char b3[TSDB_TYPE_STR_MAX_LEN + 1] = {0};
615
    STR_WITH_MAXSIZE_TO_VARSTR(b3, mnodeGenTypeStr(buf, TSDB_TYPE_STR_MAX_LEN, pFunc->outputType, pFunc->outputLen),
616
                               pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
617

618
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
619
    colDataSetVal(pColInfo, numOfRows, (const char *)b3, false);
S
Shengliang Guan 已提交
620

621
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
622
    colDataSetVal(pColInfo, numOfRows, (const char *)&pFunc->createdTime, false);
S
Shengliang Guan 已提交
623

624
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
625
    colDataSetVal(pColInfo, numOfRows, (const char *)&pFunc->codeSize, false);
S
Shengliang Guan 已提交
626

627
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
628
    colDataSetVal(pColInfo, numOfRows, (const char *)&pFunc->bufSize, false);
S
Shengliang Guan 已提交
629

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    char* language = "";
    if (pFunc->scriptType == TSDB_FUNC_SCRIPT_BIN_LIB) {
      language = "C";
    } else if (pFunc->scriptType == TSDB_FUNC_SCRIPT_PYTHON) {
      language = "Python";
    }
    char varLang[TSDB_TYPE_STR_MAX_LEN + 1] = {0};
    varDataSetLen(varLang, strlen(language));
    strcpy(varDataVal(varLang), language);
    colDataSetVal(pColInfo, numOfRows, (const char *)varLang, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    int32_t varCodeLen = (pFunc->codeSize + VARSTR_HEADER_SIZE) > TSDB_MAX_BINARY_LEN ? TSDB_MAX_BINARY_LEN : pFunc->codeSize + VARSTR_HEADER_SIZE;
    char *b4 = taosMemoryMalloc(varCodeLen);
    memcpy(varDataVal(b4), pFunc->pCode, varCodeLen - VARSTR_HEADER_SIZE);
    varDataSetLen(b4, varCodeLen - VARSTR_HEADER_SIZE);
    colDataSetVal(pColInfo, numOfRows, (const char*)b4, false);
    taosMemoryFree(b4);

650 651 652
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, numOfRows, (const char*)&pFunc->funcVersion, false);

S
Shengliang Guan 已提交
653 654 655 656
    numOfRows++;
    sdbRelease(pSdb, pFunc);
  }

657
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
658 659 660 661 662 663
  return numOfRows;
}

static void mndCancelGetNextFunc(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
664
}