mndFunc.c 17.9 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 _OVER;
S
Shengliang Guan 已提交
67 68

  int32_t dataPos = 0;
69 70 71 72 73 74 75 76 77 78 79
  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)
80 81 82
  if (pFunc->commentSize > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pFunc->pComment, pFunc->commentSize, _OVER)
  }
83 84 85
  SDB_SET_BINARY(pRaw, dataPos, pFunc->pCode, pFunc->codeSize, _OVER)
  SDB_SET_RESERVE(pRaw, dataPos, SDB_FUNC_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER);
86 87 88

  terrno = 0;

89
_OVER:
90 91 92 93 94
  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 已提交
95

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

static SSdbRow *mndFuncActionDecode(SSdbRaw *pRaw) {
101 102
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
103
  int8_t sver = 0;
104
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
105 106 107

  if (sver != SDB_FUNC_VER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
108
    goto _OVER;
S
Shengliang Guan 已提交
109 110
  }

S
Shengliang 已提交
111
  SSdbRow *pRow = sdbAllocRow(sizeof(SFuncObj));
112
  if (pRow == NULL) goto _OVER;
113

S
Shengliang Guan 已提交
114
  SFuncObj *pFunc = sdbGetRowObj(pRow);
115
  if (pFunc == NULL) goto _OVER;
S
Shengliang Guan 已提交
116 117

  int32_t dataPos = 0;
118 119 120 121 122 123 124 125 126 127 128
  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 已提交
129

130 131 132 133 134 135 136 137
  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 已提交
138
  pFunc->pCode = taosMemoryCalloc(1, pFunc->codeSize);
139
  if (pFunc->pCode == NULL) {
140
    goto _OVER;
S
Shengliang 已提交
141
  }
142 143
  SDB_GET_BINARY(pRaw, dataPos, pFunc->pCode, pFunc->codeSize, _OVER)
  SDB_GET_RESERVE(pRaw, dataPos, SDB_FUNC_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
144

145 146
  terrno = 0;

147
_OVER:
148 149
  if (terrno != 0) {
    mError("func:%s, failed to decode from raw:%p since %s", pFunc->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
150
    taosMemoryFreeClear(pRow);
151 152 153 154
    return NULL;
  }

  mTrace("func:%s, decode from raw:%p, row:%p", pFunc->name, pRaw, pFunc);
S
Shengliang Guan 已提交
155 156 157 158
  return pRow;
}

static int32_t mndFuncActionInsert(SSdb *pSdb, SFuncObj *pFunc) {
159
  mTrace("func:%s, perform insert action, row:%p", pFunc->name, pFunc);
S
Shengliang Guan 已提交
160 161 162 163
  return 0;
}

static int32_t mndFuncActionDelete(SSdb *pSdb, SFuncObj *pFunc) {
164
  mTrace("func:%s, perform delete action, row:%p", pFunc->name, pFunc);
wafwerar's avatar
wafwerar 已提交
165 166
  taosMemoryFreeClear(pFunc->pCode);
  taosMemoryFreeClear(pFunc->pComment);
S
Shengliang Guan 已提交
167 168 169
  return 0;
}

S
Shengliang 已提交
170 171
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 已提交
172 173 174
  return 0;
}

S
Shengliang 已提交
175 176 177 178 179 180 181 182
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 已提交
183

S
Shengliang 已提交
184 185 186 187 188
static void mndReleaseFunc(SMnode *pMnode, SFuncObj *pFunc) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pFunc);
}

S
Shengliang Guan 已提交
189
static int32_t mndCreateFunc(SMnode *pMnode, SNodeMsg *pReq, SCreateFuncReq *pCreate) {
S
Shengliang 已提交
190 191 192 193 194 195 196 197 198 199 200 201
  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;
202 203 204 205 206
  if (NULL != pCreate->pComment) {
    func.commentSize = strlen(pCreate->pComment) + 1;
    func.pComment = taosMemoryMalloc(func.commentSize);
  }
  func.codeSize = pCreate->codeLen;
wafwerar's avatar
wafwerar 已提交
207
  func.pCode = taosMemoryMalloc(func.codeSize);
S
Shengliang 已提交
208 209
  if (func.pCode == NULL || func.pCode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
210
    goto _OVER;
S
Shengliang Guan 已提交
211 212
  }

213 214 215
  if (func.commentSize > 0) {
    memcpy(func.pComment, pCreate->pComment, func.commentSize);
  }
S
Shengliang Guan 已提交
216
  memcpy(func.pCode, pCreate->pCode, func.codeSize);
S
Shengliang 已提交
217

S
Shengliang Guan 已提交
218
  pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_FUNC, &pReq->rpcMsg);
219
  if (pTrans == NULL) goto _OVER;
S
Shengliang 已提交
220

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

S
Shengliang 已提交
223
  SSdbRaw *pRedoRaw = mndFuncActionEncode(&func);
224 225
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto _OVER;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) goto _OVER;
S
Shengliang Guan 已提交
226

S
Shengliang 已提交
227
  SSdbRaw *pUndoRaw = mndFuncActionEncode(&func);
228 229
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto _OVER;
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) goto _OVER;
S
Shengliang Guan 已提交
230

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

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

S
Shengliang 已提交
237 238
  code = 0;

239
_OVER:
wafwerar's avatar
wafwerar 已提交
240 241
  taosMemoryFree(func.pCode);
  taosMemoryFree(func.pComment);
S
Shengliang Guan 已提交
242
  mndTransDrop(pTrans);
S
Shengliang 已提交
243
  return code;
S
Shengliang Guan 已提交
244 245
}

S
Shengliang Guan 已提交
246
static int32_t mndDropFunc(SMnode *pMnode, SNodeMsg *pReq, SFuncObj *pFunc) {
S
Shengliang 已提交
247
  int32_t code = -1;
S
Shengliang Guan 已提交
248
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_FUNC, &pReq->rpcMsg);
249
  if (pTrans == NULL) goto _OVER;
S
Shengliang 已提交
250

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

  SSdbRaw *pRedoRaw = mndFuncActionEncode(pFunc);
254
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
255 256 257
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING);

  SSdbRaw *pUndoRaw = mndFuncActionEncode(pFunc);
258
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
259 260 261
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY);

  SSdbRaw *pCommitRaw = mndFuncActionEncode(pFunc);
262
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) goto _OVER;
S
Shengliang Guan 已提交
263 264
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);

265
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang 已提交
266 267

  code = 0;
S
Shengliang Guan 已提交
268

269
_OVER:
S
Shengliang Guan 已提交
270
  mndTransDrop(pTrans);
S
Shengliang 已提交
271
  return code;
S
Shengliang Guan 已提交
272 273
}

S
Shengliang Guan 已提交
274 275
static int32_t mndProcessCreateFuncReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
276 277 278 279 280 281 282
  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;
283
    goto _OVER;
S
Shengliang Guan 已提交
284
  }
S
Shengliang Guan 已提交
285

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

S
Shengliang Guan 已提交
288
  pFunc = mndAcquireFunc(pMnode, createReq.name);
S
Shengliang Guan 已提交
289
  if (pFunc != NULL) {
S
Shengliang Guan 已提交
290 291 292
    if (createReq.igExists) {
      mDebug("func:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
293
      goto _OVER;
S
Shengliang 已提交
294 295
    } else {
      terrno = TSDB_CODE_MND_FUNC_ALREADY_EXIST;
296
      goto _OVER;
S
Shengliang 已提交
297
    }
S
Shengliang 已提交
298
  } else if (terrno == TSDB_CODE_MND_FUNC_ALREADY_EXIST) {
299
    goto _OVER;
S
Shengliang Guan 已提交
300 301
  }

S
Shengliang Guan 已提交
302
  if (createReq.name[0] == 0) {
S
Shengliang Guan 已提交
303
    terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
304
    goto _OVER;
S
Shengliang Guan 已提交
305 306
  }

307
  if (createReq.pCode == NULL) {
S
Shengliang Guan 已提交
308
    terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
309
    goto _OVER;
S
Shengliang Guan 已提交
310 311
  }

S
Shengliang Guan 已提交
312
  if (createReq.pCode[0] == 0) {
S
Shengliang Guan 已提交
313
    terrno = TSDB_CODE_MND_INVALID_FUNC_CODE;
314
    goto _OVER;
S
Shengliang Guan 已提交
315 316
  }

S
Shengliang Guan 已提交
317
  if (createReq.bufSize <= 0 || createReq.bufSize > TSDB_FUNC_BUF_SIZE) {
S
Shengliang Guan 已提交
318
    terrno = TSDB_CODE_MND_INVALID_FUNC_BUFSIZE;
319
    goto _OVER;
S
Shengliang Guan 已提交
320 321
  }

S
Shengliang Guan 已提交
322 323 324
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
325
    goto _OVER;
S
Shengliang Guan 已提交
326 327
  }

S
Shengliang Guan 已提交
328
  if (mndCheckFuncAuth(pUser)) {
329
    goto _OVER;
S
Shengliang Guan 已提交
330 331 332 333 334
  }

  code = mndCreateFunc(pMnode, pReq, &createReq);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

335
_OVER:
S
Shengliang Guan 已提交
336 337
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("func:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
338 339
  }

S
Shengliang Guan 已提交
340 341
  mndReleaseFunc(pMnode, pFunc);
  mndReleaseUser(pMnode, pUser);
342
  tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
343 344

  return code;
S
Shengliang Guan 已提交
345 346
}

S
Shengliang Guan 已提交
347 348
static int32_t mndProcessDropFuncReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
349 350 351 352 353 354 355
  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;
356
    goto _OVER;
S
Shengliang Guan 已提交
357
  }
S
Shengliang Guan 已提交
358

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

S
Shengliang Guan 已提交
361
  if (dropReq.name[0] == 0) {
S
Shengliang Guan 已提交
362
    terrno = TSDB_CODE_MND_INVALID_FUNC_NAME;
363
    goto _OVER;
S
Shengliang Guan 已提交
364 365
  }

S
Shengliang Guan 已提交
366
  pFunc = mndAcquireFunc(pMnode, dropReq.name);
S
Shengliang Guan 已提交
367
  if (pFunc == NULL) {
S
Shengliang Guan 已提交
368 369 370
    if (dropReq.igNotExists) {
      mDebug("func:%s, not exist, ignore not exist is set", dropReq.name);
      code = 0;
371
      goto _OVER;
S
Shengliang 已提交
372 373
    } else {
      terrno = TSDB_CODE_MND_FUNC_NOT_EXIST;
374
      goto _OVER;
S
Shengliang 已提交
375
    }
S
Shengliang Guan 已提交
376 377
  }

S
Shengliang Guan 已提交
378 379 380
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
381
    goto _OVER;
S
Shengliang Guan 已提交
382 383
  }

S
Shengliang Guan 已提交
384
  if (mndCheckFuncAuth(pUser)) {
385
    goto _OVER;
S
Shengliang Guan 已提交
386 387 388 389
  }

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

391
_OVER:
S
Shengliang Guan 已提交
392 393
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("func:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
394 395
  }

S
Shengliang Guan 已提交
396 397 398 399
  mndReleaseFunc(pMnode, pFunc);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
400 401
}

S
Shengliang Guan 已提交
402 403
static int32_t mndProcessRetrieveFuncReq(SNodeMsg *pReq) {
  SMnode          *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
404 405 406 407 408 409 410 411
  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 已提交
412

S
Shengliang Guan 已提交
413
  if (retrieveReq.numOfFuncs <= 0 || retrieveReq.numOfFuncs > TSDB_FUNC_MAX_RETRIEVE) {
S
Shengliang 已提交
414
    terrno = TSDB_CODE_MND_INVALID_FUNC_RETRIEVE;
S
Shengliang Guan 已提交
415
    goto RETRIEVE_FUNC_OVER;
S
Shengliang 已提交
416
  }
S
Shengliang Guan 已提交
417

S
Shengliang Guan 已提交
418 419 420
  retrieveRsp.numOfFuncs = retrieveReq.numOfFuncs;
  retrieveRsp.pFuncInfos = taosArrayInit(retrieveReq.numOfFuncs, sizeof(SFuncInfo));
  if (retrieveRsp.pFuncInfos == NULL) {
S
Shengliang 已提交
421
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
422
    goto RETRIEVE_FUNC_OVER;
S
Shengliang 已提交
423 424
  }

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

S
Shengliang 已提交
428
    SFuncObj *pFunc = mndAcquireFunc(pMnode, funcName);
S
Shengliang Guan 已提交
429 430
    if (pFunc == NULL) {
      terrno = TSDB_CODE_MND_INVALID_FUNC;
S
Shengliang Guan 已提交
431
      goto RETRIEVE_FUNC_OVER;
S
Shengliang Guan 已提交
432 433
    }

S
Shengliang Guan 已提交
434 435 436 437 438 439 440 441 442 443
    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;
444
    funcInfo.pCode = taosMemoryCalloc(1, funcInfo.codeSize);
445
    if (funcInfo.pCode == NULL) {
446 447 448
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto RETRIEVE_FUNC_OVER;
    }
S
Shengliang Guan 已提交
449
    memcpy(funcInfo.pCode, pFunc->pCode, pFunc->codeSize);
450
    if (funcInfo.commentSize > 0) {
451
      funcInfo.pComment = taosMemoryCalloc(1, funcInfo.commentSize);
452 453 454 455 456 457
      if (funcInfo.pComment == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        goto RETRIEVE_FUNC_OVER;
      }
      memcpy(funcInfo.pComment, pFunc->pComment, pFunc->commentSize);
    }
S
Shengliang Guan 已提交
458
    taosArrayPush(retrieveRsp.pFuncInfos, &funcInfo);
S
Shengliang 已提交
459
    mndReleaseFunc(pMnode, pFunc);
S
Shengliang Guan 已提交
460 461
  }

S
Shengliang Guan 已提交
462 463 464 465 466 467 468 469 470
  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 已提交
471 472
  pReq->pRsp = pRsp;
  pReq->rspLen = contLen;
S
Shengliang Guan 已提交
473

S
Shengliang 已提交
474 475
  code = 0;

S
Shengliang Guan 已提交
476
RETRIEVE_FUNC_OVER:
477 478
  tFreeSRetrieveFuncReq(&retrieveReq);
  tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
479 480

  return code;
S
Shengliang Guan 已提交
481 482 483 484 485 486 487 488 489
}

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 已提交
490
    int32_t bytes = len > 0 ? (int32_t)(len - VARSTR_HEADER_SIZE) : len;
S
Shengliang Guan 已提交
491 492 493 494 495 496 497 498 499 500

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

501
static int32_t mndRetrieveFuncs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
502
  SMnode   *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
503 504 505 506 507 508 509 510 511 512 513 514
  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;

515 516 517
    char b1[tListLen(pFunc->name) + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(b1, pFunc->name, pShow->bytes[cols]);

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

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

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

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

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

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

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

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

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

545
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
546
    colDataAppend(pColInfo, numOfRows, (const char *)&pFunc->bufSize, false);
S
Shengliang Guan 已提交
547 548 549 550 551

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

552
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
553 554 555 556 557 558 559
  return numOfRows;
}

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