mndFunc.c 18.2 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"
S
Shengliang Guan 已提交
18
#include "mndAuth.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

S
Shengliang Guan 已提交
24
#define SDB_FUNC_VER          1
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
static int32_t  mndCreateFunc(SMnode *pMnode, SNodeMsg *pReq, SCreateFuncReq *pCreate);
static int32_t  mndDropFunc(SMnode *pMnode, SNodeMsg *pReq, SFuncObj *pFunc);
static int32_t  mndProcessCreateFuncReq(SNodeMsg *pReq);
static int32_t  mndProcessDropFuncReq(SNodeMsg *pReq);
static int32_t  mndProcessRetrieveFuncReq(SNodeMsg *pReq);
37
static int32_t  mndRetrieveFuncs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48
static void     mndCancelGetNextFunc(SMnode *pMnode, void *pIter);

int32_t mndInitFunc(SMnode *pMnode) {
  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 已提交
49 50 51
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_FUNC, mndProcessCreateFuncReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_FUNC, mndProcessDropFuncReq);
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_FUNC, mndProcessRetrieveFuncReq);
S
Shengliang Guan 已提交
52

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

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

void mndCleanupFunc(SMnode *pMnode) {}

static SSdbRaw *mndFuncActionEncode(SFuncObj *pFunc) {
62 63
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

  int32_t dataPos = 0;
69 70 71 72 73 74 75 76
  SDB_SET_BINARY(pRaw, dataPos, pFunc->name, TSDB_FUNC_NAME_LEN, FUNC_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pFunc->createdTime, FUNC_ENCODE_OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->funcType, FUNC_ENCODE_OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->scriptType, FUNC_ENCODE_OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->align, FUNC_ENCODE_OVER)
  SDB_SET_INT8(pRaw, dataPos, pFunc->outputType, FUNC_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->outputLen, FUNC_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->bufSize, FUNC_ENCODE_OVER)
S
Shengliang 已提交
77
  SDB_SET_INT64(pRaw, dataPos, pFunc->signature, FUNC_ENCODE_OVER)
78 79 80 81
  SDB_SET_INT32(pRaw, dataPos, pFunc->commentSize, FUNC_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pFunc->codeSize, FUNC_ENCODE_OVER)
  SDB_SET_BINARY(pRaw, dataPos, pFunc->pComment, pFunc->commentSize, FUNC_ENCODE_OVER)
  SDB_SET_BINARY(pRaw, dataPos, pFunc->pCode, pFunc->codeSize, FUNC_ENCODE_OVER)
S
Shengliang Guan 已提交
82
  SDB_SET_RESERVE(pRaw, dataPos, SDB_FUNC_RESERVE_SIZE, FUNC_ENCODE_OVER)
83 84 85 86 87 88 89 90 91 92
  SDB_SET_DATALEN(pRaw, dataPos, FUNC_ENCODE_OVER);

  terrno = 0;

FUNC_ENCODE_OVER:
  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 已提交
93

S
Shengliang Guan 已提交
94
  mTrace("func:%s, encode to raw:%p, row:%p", pFunc->name, pRaw, pFunc);
S
Shengliang Guan 已提交
95 96 97 98
  return pRaw;
}

static SSdbRow *mndFuncActionDecode(SSdbRaw *pRaw) {
99 100
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
101
  int8_t sver = 0;
102
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto FUNC_DECODE_OVER;
S
Shengliang Guan 已提交
103 104 105

  if (sver != SDB_FUNC_VER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
106
    goto FUNC_DECODE_OVER;
S
Shengliang Guan 已提交
107 108
  }

S
Shengliang 已提交
109
  SSdbRow *pRow = sdbAllocRow(sizeof(SFuncObj));
110 111
  if (pRow == NULL) goto FUNC_DECODE_OVER;

S
Shengliang Guan 已提交
112
  SFuncObj *pFunc = sdbGetRowObj(pRow);
113
  if (pFunc == NULL) goto FUNC_DECODE_OVER;
S
Shengliang Guan 已提交
114 115

  int32_t dataPos = 0;
116 117 118 119 120 121 122 123
  SDB_GET_BINARY(pRaw, dataPos, pFunc->name, TSDB_FUNC_NAME_LEN, FUNC_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pFunc->createdTime, FUNC_DECODE_OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->funcType, FUNC_DECODE_OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->scriptType, FUNC_DECODE_OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->align, FUNC_DECODE_OVER)
  SDB_GET_INT8(pRaw, dataPos, &pFunc->outputType, FUNC_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->outputLen, FUNC_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->bufSize, FUNC_DECODE_OVER)
S
Shengliang 已提交
124
  SDB_GET_INT64(pRaw, dataPos, &pFunc->signature, FUNC_DECODE_OVER)
125 126
  SDB_GET_INT32(pRaw, dataPos, &pFunc->commentSize, FUNC_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pFunc->codeSize, FUNC_DECODE_OVER)
S
Shengliang 已提交
127

wafwerar's avatar
wafwerar 已提交
128 129
  pFunc->pComment = taosMemoryCalloc(1, pFunc->commentSize);
  pFunc->pCode = taosMemoryCalloc(1, pFunc->codeSize);
S
Shengliang 已提交
130 131 132 133 134 135
  if (pFunc->pComment == NULL || pFunc->pCode == NULL) {
    goto FUNC_DECODE_OVER;
  }

  SDB_GET_BINARY(pRaw, dataPos, pFunc->pComment, pFunc->commentSize, FUNC_DECODE_OVER)
  SDB_GET_BINARY(pRaw, dataPos, pFunc->pCode, pFunc->codeSize, FUNC_DECODE_OVER)
S
Shengliang Guan 已提交
136
  SDB_GET_RESERVE(pRaw, dataPos, SDB_FUNC_RESERVE_SIZE, FUNC_DECODE_OVER)
S
Shengliang Guan 已提交
137

138 139 140 141 142
  terrno = 0;

FUNC_DECODE_OVER:
  if (terrno != 0) {
    mError("func:%s, failed to decode from raw:%p since %s", pFunc->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
143
    taosMemoryFreeClear(pRow);
144 145 146 147
    return NULL;
  }

  mTrace("func:%s, decode from raw:%p, row:%p", pFunc->name, pRaw, pFunc);
S
Shengliang Guan 已提交
148 149 150 151
  return pRow;
}

static int32_t mndFuncActionInsert(SSdb *pSdb, SFuncObj *pFunc) {
152
  mTrace("func:%s, perform insert action, row:%p", pFunc->name, pFunc);
S
Shengliang Guan 已提交
153 154 155 156
  return 0;
}

static int32_t mndFuncActionDelete(SSdb *pSdb, SFuncObj *pFunc) {
157
  mTrace("func:%s, perform delete action, row:%p", pFunc->name, pFunc);
wafwerar's avatar
wafwerar 已提交
158 159
  taosMemoryFreeClear(pFunc->pCode);
  taosMemoryFreeClear(pFunc->pComment);
S
Shengliang Guan 已提交
160 161 162
  return 0;
}

S
Shengliang 已提交
163 164
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);
S
Shengliang Guan 已提交
165 166 167
  return 0;
}

S
Shengliang 已提交
168 169 170 171 172 173 174 175
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 已提交
176

S
Shengliang 已提交
177 178 179 180 181
static void mndReleaseFunc(SMnode *pMnode, SFuncObj *pFunc) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pFunc);
}

S
Shengliang Guan 已提交
182
static int32_t mndCreateFunc(SMnode *pMnode, SNodeMsg *pReq, SCreateFuncReq *pCreate) {
S
Shengliang 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196
  int32_t code = -1;
  STrans *pTrans = NULL;

  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;
  func.commentSize = pCreate->commentSize;
  func.codeSize = pCreate->codeSize;
wafwerar's avatar
wafwerar 已提交
197 198
  func.pComment = taosMemoryMalloc(func.commentSize);
  func.pCode = taosMemoryMalloc(func.codeSize);
S
Shengliang 已提交
199 200 201
  if (func.pCode == NULL || func.pCode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
202 203
  }

S
Shengliang Guan 已提交
204 205
  memcpy(func.pComment, pCreate->pComment, pCreate->commentSize);
  memcpy(func.pCode, pCreate->pCode, func.codeSize);
S
Shengliang 已提交
206

S
Shengliang Guan 已提交
207
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_FUNC, &pReq->rpcMsg);
S
Shengliang 已提交
208 209
  if (pTrans == NULL) goto CREATE_FUNC_OVER;

S
Shengliang Guan 已提交
210 211
  mDebug("trans:%d, used to create func:%s", pTrans->id, pCreate->name);

S
Shengliang 已提交
212 213 214
  SSdbRaw *pRedoRaw = mndFuncActionEncode(&func);
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto CREATE_FUNC_OVER;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
215

S
Shengliang 已提交
216 217 218
  SSdbRaw *pUndoRaw = mndFuncActionEncode(&func);
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto CREATE_FUNC_OVER;
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
219

S
Shengliang 已提交
220 221 222
  SSdbRaw *pCommitRaw = mndFuncActionEncode(&func);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) goto CREATE_FUNC_OVER;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
223

S
Shengliang 已提交
224
  if (mndTransPrepare(pMnode, pTrans) != 0) goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
225

S
Shengliang 已提交
226 227 228
  code = 0;

CREATE_FUNC_OVER:
wafwerar's avatar
wafwerar 已提交
229 230
  taosMemoryFree(func.pCode);
  taosMemoryFree(func.pComment);
S
Shengliang Guan 已提交
231
  mndTransDrop(pTrans);
S
Shengliang 已提交
232
  return code;
S
Shengliang Guan 已提交
233 234
}

S
Shengliang Guan 已提交
235
static int32_t mndDropFunc(SMnode *pMnode, SNodeMsg *pReq, SFuncObj *pFunc) {
S
Shengliang 已提交
236
  int32_t code = -1;
S
Shengliang Guan 已提交
237
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_FUNC, &pReq->rpcMsg);
S
Shengliang 已提交
238 239
  if (pTrans == NULL) goto DROP_FUNC_OVER;

S
Shengliang Guan 已提交
240 241 242
  mDebug("trans:%d, used to drop user:%s", pTrans->id, pFunc->name);

  SSdbRaw *pRedoRaw = mndFuncActionEncode(pFunc);
S
Shengliang 已提交
243
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto DROP_FUNC_OVER;
S
Shengliang Guan 已提交
244 245 246
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING);

  SSdbRaw *pUndoRaw = mndFuncActionEncode(pFunc);
S
Shengliang 已提交
247
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto DROP_FUNC_OVER;
S
Shengliang Guan 已提交
248 249 250
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY);

  SSdbRaw *pCommitRaw = mndFuncActionEncode(pFunc);
S
Shengliang 已提交
251
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) goto DROP_FUNC_OVER;
S
Shengliang Guan 已提交
252 253
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);

S
Shengliang 已提交
254 255 256
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_FUNC_OVER;

  code = 0;
S
Shengliang Guan 已提交
257

S
Shengliang 已提交
258
DROP_FUNC_OVER:
S
Shengliang Guan 已提交
259
  mndTransDrop(pTrans);
S
Shengliang 已提交
260
  return code;
S
Shengliang Guan 已提交
261 262
}

S
Shengliang Guan 已提交
263 264
static int32_t mndProcessCreateFuncReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
265 266 267 268 269 270 271 272 273
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SFuncObj      *pFunc = NULL;
  SCreateFuncReq createReq = {0};

  if (tDeserializeSCreateFuncReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_FUNC_OVER;
  }
S
Shengliang Guan 已提交
274

S
Shengliang Guan 已提交
275
  mDebug("func:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
276

S
Shengliang Guan 已提交
277
  pFunc = mndAcquireFunc(pMnode, createReq.name);
S
Shengliang Guan 已提交
278
  if (pFunc != NULL) {
S
Shengliang Guan 已提交
279 280 281 282
    if (createReq.igExists) {
      mDebug("func:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
      goto CREATE_FUNC_OVER;
S
Shengliang 已提交
283 284
    } else {
      terrno = TSDB_CODE_MND_FUNC_ALREADY_EXIST;
S
Shengliang Guan 已提交
285
      goto CREATE_FUNC_OVER;
S
Shengliang 已提交
286
    }
S
Shengliang 已提交
287
  } else if (terrno == TSDB_CODE_MND_FUNC_ALREADY_EXIST) {
S
Shengliang Guan 已提交
288
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
289 290
  }

S
Shengliang Guan 已提交
291
  if (createReq.name[0] == 0) {
S
Shengliang Guan 已提交
292
    terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
S
Shengliang Guan 已提交
293
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
294 295
  }

S
Shengliang Guan 已提交
296
  if (createReq.commentSize <= 0 || createReq.commentSize > TSDB_FUNC_COMMENT_LEN) {
S
Shengliang Guan 已提交
297
    terrno = TSDB_CODE_MND_INVALID_FUNC_COMMENT;
S
Shengliang Guan 已提交
298
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
299 300
  }

S
Shengliang Guan 已提交
301
  if (createReq.codeSize <= 0 || createReq.codeSize > TSDB_FUNC_CODE_LEN) {
S
Shengliang Guan 已提交
302
    terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
S
Shengliang Guan 已提交
303
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
304 305
  }

S
Shengliang Guan 已提交
306
  if (createReq.pCode[0] == 0) {
S
Shengliang Guan 已提交
307
    terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
S
Shengliang Guan 已提交
308
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
309 310
  }

S
Shengliang Guan 已提交
311
  if (createReq.bufSize <= 0 || createReq.bufSize > TSDB_FUNC_BUF_SIZE) {
S
Shengliang Guan 已提交
312
    terrno = TSDB_CODE_MND_INVALID_FUNC_BUFSIZE;
S
Shengliang Guan 已提交
313
    goto CREATE_FUNC_OVER;
S
Shengliang Guan 已提交
314 315
  }

S
Shengliang Guan 已提交
316 317 318 319 320 321
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    goto CREATE_FUNC_OVER;
  }

S
Shengliang Guan 已提交
322
  if (mndCheckFuncAuth(pUser)) {
S
Shengliang Guan 已提交
323 324 325 326 327 328 329 330 331
    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());
S
Shengliang Guan 已提交
332 333
  }

S
Shengliang Guan 已提交
334 335 336 337
  mndReleaseFunc(pMnode, pFunc);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
338 339
}

S
Shengliang Guan 已提交
340 341
static int32_t mndProcessDropFuncReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
342 343 344 345 346 347 348 349 350
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SFuncObj    *pFunc = NULL;
  SDropFuncReq dropReq = {0};

  if (tDeserializeSDropFuncReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_FUNC_OVER;
  }
S
Shengliang Guan 已提交
351

S
Shengliang Guan 已提交
352
  mDebug("func:%s, start to drop", dropReq.name);
S
Shengliang Guan 已提交
353

S
Shengliang Guan 已提交
354
  if (dropReq.name[0] == 0) {
S
Shengliang Guan 已提交
355
    terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
S
Shengliang Guan 已提交
356
    goto DROP_FUNC_OVER;
S
Shengliang Guan 已提交
357 358
  }

S
Shengliang Guan 已提交
359
  pFunc = mndAcquireFunc(pMnode, dropReq.name);
S
Shengliang Guan 已提交
360
  if (pFunc == NULL) {
S
Shengliang Guan 已提交
361 362 363 364
    if (dropReq.igNotExists) {
      mDebug("func:%s, not exist, ignore not exist is set", dropReq.name);
      code = 0;
      goto DROP_FUNC_OVER;
S
Shengliang 已提交
365 366
    } else {
      terrno = TSDB_CODE_MND_FUNC_NOT_EXIST;
S
Shengliang Guan 已提交
367
      goto DROP_FUNC_OVER;
S
Shengliang 已提交
368
    }
S
Shengliang Guan 已提交
369 370
  }

S
Shengliang Guan 已提交
371 372 373 374 375 376
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    goto DROP_FUNC_OVER;
  }

S
Shengliang Guan 已提交
377
  if (mndCheckFuncAuth(pUser)) {
S
Shengliang Guan 已提交
378 379 380 381 382
    goto DROP_FUNC_OVER;
  }

  code = mndDropFunc(pMnode, pReq, pFunc);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang 已提交
383

S
Shengliang Guan 已提交
384 385 386
DROP_FUNC_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("func:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
387 388
  }

S
Shengliang Guan 已提交
389 390 391 392
  mndReleaseFunc(pMnode, pFunc);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
393 394
}

S
Shengliang Guan 已提交
395 396
static int32_t mndProcessRetrieveFuncReq(SNodeMsg *pReq) {
  SMnode          *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
397 398 399 400 401 402 403 404
  int32_t          code = -1;
  SRetrieveFuncReq retrieveReq = {0};
  SRetrieveFuncRsp retrieveRsp = {0};

  if (tDeserializeSRetrieveFuncReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &retrieveReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto RETRIEVE_FUNC_OVER;
  }
S
Shengliang Guan 已提交
405

S
Shengliang Guan 已提交
406
  if (retrieveReq.numOfFuncs <= 0 || retrieveReq.numOfFuncs > TSDB_FUNC_MAX_RETRIEVE) {
S
Shengliang 已提交
407
    terrno = TSDB_CODE_MND_INVALID_FUNC_RETRIEVE;
S
Shengliang Guan 已提交
408
    goto RETRIEVE_FUNC_OVER;
S
Shengliang 已提交
409
  }
S
Shengliang Guan 已提交
410

S
Shengliang Guan 已提交
411 412 413
  retrieveRsp.numOfFuncs = retrieveReq.numOfFuncs;
  retrieveRsp.pFuncInfos = taosArrayInit(retrieveReq.numOfFuncs, sizeof(SFuncInfo));
  if (retrieveRsp.pFuncInfos == NULL) {
S
Shengliang 已提交
414
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
415
    goto RETRIEVE_FUNC_OVER;
S
Shengliang 已提交
416 417
  }

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

S
Shengliang 已提交
421
    SFuncObj *pFunc = mndAcquireFunc(pMnode, funcName);
S
Shengliang Guan 已提交
422 423
    if (pFunc == NULL) {
      terrno = TSDB_CODE_MND_INVALID_FUNC;
S
Shengliang Guan 已提交
424
      goto RETRIEVE_FUNC_OVER;
S
Shengliang Guan 已提交
425 426
    }

S
Shengliang Guan 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439
    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;
    funcInfo.commentSize = pFunc->commentSize;
    funcInfo.codeSize = pFunc->codeSize;
    memcpy(funcInfo.pComment, pFunc->pComment, pFunc->commentSize);
    memcpy(funcInfo.pCode, pFunc->pCode, pFunc->codeSize);
    taosArrayPush(retrieveRsp.pFuncInfos, &funcInfo);
S
Shengliang 已提交
440
    mndReleaseFunc(pMnode, pFunc);
S
Shengliang Guan 已提交
441 442
  }

S
Shengliang Guan 已提交
443 444 445 446 447 448 449 450 451
  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 已提交
452 453
  pReq->pRsp = pRsp;
  pReq->rspLen = contLen;
S
Shengliang Guan 已提交
454

S
Shengliang 已提交
455 456
  code = 0;

S
Shengliang Guan 已提交
457 458 459
RETRIEVE_FUNC_OVER:
  taosArrayDestroy(retrieveReq.pFuncNames);
  taosArrayDestroy(retrieveRsp.pFuncInfos);
S
Shengliang 已提交
460 461

  return code;
S
Shengliang Guan 已提交
462 463 464 465 466 467 468 469 470
}

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 已提交
471
    int32_t bytes = len > 0 ? (int32_t)(len - VARSTR_HEADER_SIZE) : len;
S
Shengliang Guan 已提交
472 473 474 475 476 477 478 479 480 481

    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;
}

482
static int32_t mndRetrieveFuncs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows) {
S
Shengliang Guan 已提交
483
  SMnode   *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
484 485 486 487 488 489 490 491 492 493 494 495
  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;

496 497 498 499 500 501 502 503 504 505 506 507 508
    char b1[tListLen(pFunc->name) + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(b1, pFunc->name, pShow->bytes[cols]);

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

    char* b2 = taosMemoryCalloc(1, pShow->bytes[cols]);
    STR_WITH_MAXSIZE_TO_VARSTR(b2, pFunc->pComment, pShow->bytes[cols]);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char*) b2, false);

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

510 511
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char*) &isAgg, false);
S
Shengliang Guan 已提交
512

513 514
    char b3[TSDB_TYPE_STR_MAX_LEN] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(b3, mnodeGenTypeStr(buf, TSDB_TYPE_STR_MAX_LEN, pFunc->outputType, pFunc->outputLen), pShow->bytes[cols]);
S
Shengliang Guan 已提交
515

516 517
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char*) b3, false);
S
Shengliang Guan 已提交
518

519 520
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char*) &pFunc->createdTime, false);
S
Shengliang Guan 已提交
521

522 523
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char*) &pFunc->codeSize, false);
S
Shengliang Guan 已提交
524

525 526
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char*) &pFunc->bufSize, false);
S
Shengliang Guan 已提交
527 528 529 530 531

    numOfRows++;
    sdbRelease(pSdb, pFunc);
  }

532
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
533 534 535 536 537 538 539
  return numOfRows;
}

static void mndCancelGetNextFunc(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}