• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3824

01 Apr 2025 12:26PM UTC coverage: 34.064% (-0.001%) from 34.065%
#3824

push

travis-ci

happyguoxy
test:alter gcda dir

148483 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222466 of 489445 relevant lines covered (45.45%)

762427.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#ifdef USE_UDF
17
// clang-format off
18
#include "uv.h"
19
#include "os.h"
20
#include "fnLog.h"
21
#include "thash.h"
22

23
#include "tudf.h"
24
#include "tudfInt.h"
25
#include "version.h"
26

27
#include "tdatablock.h"
28
#include "tdataformat.h"
29
#include "tglobal.h"
30
#include "tmsg.h"
31
#include "trpc.h"
32
#include "tmisce.h"
33
#include "tversion.h"
34
// clang-format on
35

36
#define UDFD_MAX_SCRIPT_PLUGINS 64
37
#define UDFD_MAX_SCRIPT_TYPE    1
38
#define UDFD_MAX_PLUGIN_FUNCS   9
39

40
typedef struct SUdfCPluginCtx {
41
  uv_lib_t lib;
42

43
  TUdfScalarProcFunc scalarProcFunc;
44

45
  TUdfAggStartFunc   aggStartFunc;
46
  TUdfAggProcessFunc aggProcFunc;
47
  TUdfAggFinishFunc  aggFinishFunc;
48
  TUdfAggMergeFunc   aggMergeFunc;
49

50
  TUdfInitFunc    initFunc;
51
  TUdfDestroyFunc destroyFunc;
52
} SUdfCPluginCtx;
53

54
int32_t udfdCPluginOpen(SScriptUdfEnvItem *items, int numItems) { return 0; }
3✔
55

56
int32_t udfdCPluginClose() { return 0; }
3✔
57

58
int32_t udfdCPluginUdfInitLoadInitDestoryFuncs(SUdfCPluginCtx *udfCtx, const char *udfName) {
138✔
59
  TAOS_UDF_CHECK_PTR_RCODE(udfCtx, udfName);
414!
60
  char  initFuncName[TSDB_FUNC_NAME_LEN + 6] = {0};
138✔
61
  char *initSuffix = "_init";
138✔
62
  snprintf(initFuncName, sizeof(initFuncName), "%s%s", udfName, initSuffix);
138✔
63
  TAOS_CHECK_RETURN(uv_dlsym(&udfCtx->lib, initFuncName, (void **)(&udfCtx->initFunc)));
138✔
64

65
  char  destroyFuncName[TSDB_FUNC_NAME_LEN + 9] = {0};
130✔
66
  char *destroySuffix = "_destroy";
130✔
67
  snprintf(destroyFuncName, sizeof(destroyFuncName), "%s%s", udfName, destroySuffix);
130✔
68
  TAOS_CHECK_RETURN(uv_dlsym(&udfCtx->lib, destroyFuncName, (void **)(&udfCtx->destroyFunc)));
130!
69
  return 0;
130✔
70
}
71

72
int32_t udfdCPluginUdfInitLoadAggFuncs(SUdfCPluginCtx *udfCtx, const char *udfName) {
97✔
73
  TAOS_UDF_CHECK_PTR_RCODE(udfCtx, udfName);
291!
74
  char processFuncName[TSDB_FUNC_NAME_LEN] = {0};
97✔
75
  snprintf(processFuncName, sizeof(processFuncName), "%s", udfName); 
97✔
76
  TAOS_CHECK_RETURN(uv_dlsym(&udfCtx->lib, processFuncName, (void **)(&udfCtx->aggProcFunc)));
97!
77

78
  char  startFuncName[TSDB_FUNC_NAME_LEN + 7] = {0};
97✔
79
  char *startSuffix = "_start";
97✔
80
  snprintf(startFuncName, sizeof(startFuncName), "%s%s", processFuncName, startSuffix);
97✔
81
  TAOS_CHECK_RETURN(uv_dlsym(&udfCtx->lib, startFuncName, (void **)(&udfCtx->aggStartFunc)));
97!
82

83
  char  finishFuncName[TSDB_FUNC_NAME_LEN + 8] = {0};
97✔
84
  char *finishSuffix = "_finish";
97✔
85
  snprintf(finishFuncName, sizeof(finishFuncName), "%s%s", processFuncName, finishSuffix);
97✔
86
  TAOS_CHECK_RETURN(uv_dlsym(&udfCtx->lib, finishFuncName, (void **)(&udfCtx->aggFinishFunc)));
97!
87

88
  char  mergeFuncName[TSDB_FUNC_NAME_LEN + 7] = {0};
97✔
89
  char *mergeSuffix = "_merge";
97✔
90
  snprintf(mergeFuncName, sizeof(mergeFuncName), "%s%s", processFuncName, mergeSuffix);
97✔
91
  int ret = uv_dlsym(&udfCtx->lib, mergeFuncName, (void **)(&udfCtx->aggMergeFunc));
97✔
92
  if (ret != 0) {
97!
93
    fnInfo("uv_dlsym function %s. error: %s", mergeFuncName, uv_strerror(ret));
97!
94
  }
95
  return 0;
97✔
96
}
97

98
int32_t udfdCPluginUdfInit(SScriptUdfInfo *udf, void **pUdfCtx) {
138✔
99
  TAOS_UDF_CHECK_PTR_RCODE(udf, pUdfCtx);
414!
100
  int32_t         err = 0;
138✔
101
  SUdfCPluginCtx *udfCtx = taosMemoryCalloc(1, sizeof(SUdfCPluginCtx));
138!
102
  if (NULL == udfCtx) {
138!
103
    return terrno;
×
104
  }
105
  err = uv_dlopen(udf->path, &udfCtx->lib);
138✔
106
  if (err != 0) {
138!
107
    fnError("can not load library %s. error: %s", udf->path, uv_strerror(err));
×
108
    taosMemoryFree(udfCtx);
×
109
    return TSDB_CODE_UDF_LOAD_UDF_FAILURE;
×
110
  }
111
  const char *udfName = udf->name;
138✔
112

113
  err = udfdCPluginUdfInitLoadInitDestoryFuncs(udfCtx, udfName);
138✔
114
  if (err != 0) {
138✔
115
    fnError("can not load init/destroy functions. error: %d", err);
8!
116
    err = TSDB_CODE_UDF_LOAD_UDF_FAILURE;
8✔
117
    goto _exit;
8✔
118
  }
119

120
  if (udf->funcType == UDF_FUNC_TYPE_SCALAR) {
130✔
121
    char processFuncName[TSDB_FUNC_NAME_LEN] = {0};
33✔
122
    snprintf(processFuncName, sizeof(processFuncName), "%s", udfName);
33✔
123
    if (uv_dlsym(&udfCtx->lib, processFuncName, (void **)(&udfCtx->scalarProcFunc)) != 0) {
33!
124
      fnError("can not load library function %s. error: %s", processFuncName, uv_strerror(err));
×
125
      err = TSDB_CODE_UDF_LOAD_UDF_FAILURE;
×
126
      goto _exit;
×
127
    }
128
  } else if (udf->funcType == UDF_FUNC_TYPE_AGG) {
97!
129
    err = udfdCPluginUdfInitLoadAggFuncs(udfCtx, udfName);
97✔
130
    if (err != 0) {
97!
131
      fnError("can not load aggregation functions. error: %d", err);
×
132
      err = TSDB_CODE_UDF_LOAD_UDF_FAILURE;
×
133
      goto _exit;
×
134
    }
135
  }
136

137
  if (udfCtx->initFunc) {
130!
138
    err = (udfCtx->initFunc)();
130✔
139
    if (err != 0) {
130!
140
      fnError("udf init function failed. error: %d", err);
×
141
      goto _exit;
×
142
    }
143
  }
144
  *pUdfCtx = udfCtx;
130✔
145
  return 0;
130✔
146
_exit:
8✔
147
  uv_dlclose(&udfCtx->lib);
8✔
148
  taosMemoryFree(udfCtx);
8!
149
  return err;
8✔
150
}
151

152
int32_t udfdCPluginUdfDestroy(void *udfCtx) {
128✔
153
  TAOS_UDF_CHECK_PTR_RCODE(udfCtx);
256!
154
  SUdfCPluginCtx *ctx = udfCtx;
128✔
155
  int32_t         code = 0;
128✔
156
  if (ctx->destroyFunc) {
128!
157
    code = (ctx->destroyFunc)();
128✔
158
  }
159
  uv_dlclose(&ctx->lib);
128✔
160
  taosMemoryFree(ctx);
128!
161
  return code;
128✔
162
}
163

164
int32_t udfdCPluginUdfScalarProc(SUdfDataBlock *block, SUdfColumn *resultCol, void *udfCtx) {
202✔
165
  TAOS_UDF_CHECK_PTR_RCODE(block, resultCol, udfCtx);
808!
166
  SUdfCPluginCtx *ctx = udfCtx;
202✔
167
  if (ctx->scalarProcFunc) {
202!
168
    return ctx->scalarProcFunc(block, resultCol);
202✔
169
  } else {
170
    fnError("taosudf c plugin scalar proc not implemented");
×
171
    return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
172
  }
173
}
174

175
int32_t udfdCPluginUdfAggStart(SUdfInterBuf *buf, void *udfCtx) {
235✔
176
  TAOS_UDF_CHECK_PTR_RCODE(buf, udfCtx);
705!
177
  SUdfCPluginCtx *ctx = udfCtx;
235✔
178
  if (ctx->aggStartFunc) {
235✔
179
    return ctx->aggStartFunc(buf);
226✔
180
  } else {
181
    fnError("taosudf c plugin aggregation start not implemented");
9!
182
    return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
9✔
183
  }
184
  return 0;
185
}
186

187
int32_t udfdCPluginUdfAggProc(SUdfDataBlock *block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf, void *udfCtx) {
472✔
188
  TAOS_UDF_CHECK_PTR_RCODE(block, interBuf, newInterBuf, udfCtx);
2,360!
189
  SUdfCPluginCtx *ctx = udfCtx;
472✔
190
  if (ctx->aggProcFunc) {
472!
191
    return ctx->aggProcFunc(block, interBuf, newInterBuf);
472✔
192
  } else {
193
    fnError("taosudf c plugin aggregation process not implemented");
×
194
    return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
195
  }
196
}
197

198
// int32_t udfdCPluginUdfAggMerge(SUdfInterBuf *inputBuf1, SUdfInterBuf *inputBuf2, SUdfInterBuf *outputBuf,
199
//                                void *udfCtx) {
200
//   TAOS_UDF_CHECK_PTR_RCODE(inputBuf1, inputBuf2, outputBuf, udfCtx);
201
//   SUdfCPluginCtx *ctx = udfCtx;
202
//   if (ctx->aggMergeFunc) {
203
//     return ctx->aggMergeFunc(inputBuf1, inputBuf2, outputBuf);
204
//   } else {
205
//     fnError("taosudf c plugin aggregation merge not implemented");
206
//     return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
207
//   }
208
// }
209

210
int32_t udfdCPluginUdfAggFinish(SUdfInterBuf *buf, SUdfInterBuf *resultData, void *udfCtx) {
207✔
211
  TAOS_UDF_CHECK_PTR_RCODE(buf, resultData, udfCtx);
828!
212
  SUdfCPluginCtx *ctx = udfCtx;
207✔
213
  if (ctx->aggFinishFunc) {
207!
214
    return ctx->aggFinishFunc(buf, resultData);
207✔
215
  } else {
216
    fnError("taosudf c plugin aggregation finish not implemented");
×
217
    return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
218
  }
219
  return 0;
220
}
221

222
// for c, the function pointer are filled directly and libloaded = true;
223
// for others, dlopen/dlsym to find function pointers
224
typedef struct SUdfScriptPlugin {
225
  int8_t scriptType;
226

227
  char     libPath[PATH_MAX];
228
  bool     libLoaded;
229
  uv_lib_t lib;
230

231
  TScriptUdfScalarProcFunc udfScalarProcFunc;
232
  TScriptUdfAggStartFunc   udfAggStartFunc;
233
  TScriptUdfAggProcessFunc udfAggProcFunc;
234
  TScriptUdfAggMergeFunc   udfAggMergeFunc;
235
  TScriptUdfAggFinishFunc  udfAggFinishFunc;
236

237
  TScriptUdfInitFunc    udfInitFunc;
238
  TScriptUdfDestoryFunc udfDestroyFunc;
239

240
  TScriptOpenFunc  openFunc;
241
  TScriptCloseFunc closeFunc;
242
} SUdfScriptPlugin;
243

244
typedef struct SUdfdContext {
245
  uv_loop_t  *loop;
246
  uv_pipe_t   ctrlPipe;
247
  uv_signal_t intrSignal;
248
  char        listenPipeName[PATH_MAX + UDF_LISTEN_PIPE_NAME_LEN + 2];
249
  uv_pipe_t   listeningPipe;
250

251
  void     *clientRpc;
252
  SCorEpSet mgmtEp;
253

254
  uv_mutex_t udfsMutex;
255
  SHashObj  *udfsHash;
256

257
  uv_mutex_t        scriptPluginsMutex;
258
  SUdfScriptPlugin *scriptPlugins[UDFD_MAX_SCRIPT_PLUGINS];
259

260
  SArray *residentFuncs;
261

262
  char udfDataDir[PATH_MAX];
263
  bool printVersion;
264
} SUdfdContext;
265

266
SUdfdContext global;
267

268
struct SUdfdUvConn;
269
struct SUvUdfWork;
270

271
typedef struct SUdfdUvConn {
272
  uv_stream_t *client;
273
  char        *inputBuf;
274
  int32_t      inputLen;
275
  int32_t      inputCap;
276
  int32_t      inputTotal;
277

278
  struct SUvUdfWork *pWorkList;  // head of work list
279
} SUdfdUvConn;
280

281
typedef struct SUvUdfWork {
282
  SUdfdUvConn *conn;
283
  uv_buf_t     input;
284
  uv_buf_t     output;
285

286
  struct SUvUdfWork *pWorkNext;
287
} SUvUdfWork;
288

289
typedef enum { UDF_STATE_INIT = 0, UDF_STATE_LOADING, UDF_STATE_READY } EUdfState;
290

291
typedef struct SUdf {
292
  char    name[TSDB_FUNC_NAME_LEN + 1];
293
  int32_t version;
294
  int64_t createdTime;
295

296
  int8_t  funcType;
297
  int8_t  scriptType;
298
  int8_t  outputType;
299
  int32_t outputLen;
300
  int32_t bufSize;
301

302
  char path[PATH_MAX];
303

304
  int32_t    refCount;
305
  EUdfState  state;
306
  uv_mutex_t lock;
307
  uv_cond_t  condReady;
308
  bool       resident;
309

310
  SUdfScriptPlugin *scriptPlugin;
311
  void             *scriptUdfCtx;
312

313
  int64_t lastFetchTime;  // last fetch time in milliseconds
314
  bool    expired;
315
} SUdf;
316

317
typedef struct SUdfcFuncHandle {
318
  SUdf *udf;
319
} SUdfcFuncHandle;
320

321
typedef enum EUdfdRpcReqRspType {
322
  UDFD_RPC_MNODE_CONNECT = 0,
323
  UDFD_RPC_RETRIVE_FUNC,
324
} EUdfdRpcReqRspType;
325

326
typedef struct SUdfdRpcSendRecvInfo {
327
  EUdfdRpcReqRspType rpcType;
328
  int32_t            code;
329
  void              *param;
330
  uv_sem_t           resultSem;
331
} SUdfdRpcSendRecvInfo;
332

333
static void    udfdProcessRpcRsp(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
334
static int32_t udfdFillUdfInfoFromMNode(void *clientRpc, char *udfName, SUdf *udf);
335
static int32_t udfdConnectToMnode();
336
static bool    udfdRpcRfp(int32_t code, tmsg_t msgType);
337
static int     initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet);
338
static int32_t udfdOpenClientRpc();
339
static void    udfdCloseClientRpc();
340

341
static void udfdProcessSetupRequest(SUvUdfWork *uvUdf, SUdfRequest *request);
342
static void udfdProcessCallRequest(SUvUdfWork *uvUdf, SUdfRequest *request);
343
static void udfdProcessTeardownRequest(SUvUdfWork *uvUdf, SUdfRequest *request);
344
static void udfdProcessRequest(uv_work_t *req);
345
static void udfdOnWrite(uv_write_t *req, int status);
346
static void udfdSendResponse(uv_work_t *work, int status);
347
static void udfdAllocBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf);
348
static bool isUdfdUvMsgComplete(SUdfdUvConn *pipe);
349
static void udfdHandleRequest(SUdfdUvConn *conn);
350
static void udfdPipeCloseCb(uv_handle_t *pipe);
351
static void udfdUvHandleError(SUdfdUvConn *conn) { uv_close((uv_handle_t *)conn->client, udfdPipeCloseCb); }
311✔
352
static void udfdPipeRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf);
353
static void udfdOnNewConnection(uv_stream_t *server, int status);
354

355
static void    udfdIntrSignalHandler(uv_signal_t *handle, int signum);
356
static void    removeListeningPipe();
357

358
static void    udfdPrintVersion();
359
static int32_t udfdParseArgs(int32_t argc, char *argv[]);
360
static int32_t udfdInitLog();
361

362
static void    udfdCtrlAllocBufCb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
363
static void    udfdCtrlReadCb(uv_stream_t *q, ssize_t nread, const uv_buf_t *buf);
364
static int32_t udfdUvInit();
365
static void    udfdCloseWalkCb(uv_handle_t *handle, void *arg);
366
static void    udfdRun();
367
static void    udfdConnectMnodeThreadFunc(void *args);
368

369
int32_t udfdNewUdf(SUdf **pUdf,  const char *udfName);
370
void  udfdGetFuncBodyPath(const SUdf *udf, char *path);
371

372
int32_t udfdInitializeCPlugin(SUdfScriptPlugin *plugin) {
3✔
373
  TAOS_UDF_CHECK_PTR_RCODE(plugin);
6!
374
  plugin->scriptType = TSDB_FUNC_SCRIPT_BIN_LIB;
3✔
375
  plugin->openFunc = udfdCPluginOpen;
3✔
376
  plugin->closeFunc = udfdCPluginClose;
3✔
377
  plugin->udfInitFunc = udfdCPluginUdfInit;
3✔
378
  plugin->udfDestroyFunc = udfdCPluginUdfDestroy;
3✔
379
  plugin->udfScalarProcFunc = udfdCPluginUdfScalarProc;
3✔
380
  plugin->udfAggStartFunc = udfdCPluginUdfAggStart;
3✔
381
  plugin->udfAggProcFunc = udfdCPluginUdfAggProc;
3✔
382
  // plugin->udfAggMergeFunc = udfdCPluginUdfAggMerge;
383
  plugin->udfAggFinishFunc = udfdCPluginUdfAggFinish;
3✔
384

385
  SScriptUdfEnvItem items[1] = {{"LD_LIBRARY_PATH", tsUdfdLdLibPath}};
3✔
386
  int32_t           err = plugin->openFunc(items, 1);
3✔
387
  if (err != 0) return err;
3!
388
  return 0;
3✔
389
}
390

391
int32_t udfdLoadSharedLib(char *libPath, uv_lib_t *pLib, const char *funcName[], void **func[], int numOfFuncs) {
2✔
392
  TAOS_UDF_CHECK_PTR_RCODE(libPath, pLib, funcName, func);
10!
393
  int err = uv_dlopen(libPath, pLib);
2✔
394
  if (err != 0) {
2!
395
    fnError("can not load library %s. error: %s", libPath, uv_strerror(err));
×
396
    return TSDB_CODE_UDF_LOAD_UDF_FAILURE;
×
397
  }
398

399
  for (int i = 0; i < numOfFuncs; ++i) {
20✔
400
    err = uv_dlsym(pLib, funcName[i], func[i]);
18✔
401
    if (err != 0) {
18!
402
      fnError("load library function failed. lib %s function %s", libPath, funcName[i]);
×
403
    }
404
  }
405
  return 0;
2✔
406
}
407

408
int32_t udfdInitializePythonPlugin(SUdfScriptPlugin *plugin) {
2✔
409
  TAOS_UDF_CHECK_PTR_RCODE(plugin);
4!
410
  plugin->scriptType = TSDB_FUNC_SCRIPT_PYTHON;
2✔
411
  // todo: windows support
412
  snprintf(plugin->libPath, PATH_MAX, "%s", "libtaospyudf.so");
2✔
413
  plugin->libLoaded = false;
2✔
414
  const char *funcName[UDFD_MAX_PLUGIN_FUNCS] = {"pyOpen",         "pyClose",         "pyUdfInit",
2✔
415
                                                 "pyUdfDestroy",   "pyUdfScalarProc", "pyUdfAggStart",
416
                                                 "pyUdfAggFinish", "pyUdfAggProc",    "pyUdfAggMerge"};
417
  void      **funcs[UDFD_MAX_PLUGIN_FUNCS] = {
2✔
418
           (void **)&plugin->openFunc,         (void **)&plugin->closeFunc,         (void **)&plugin->udfInitFunc,
2✔
419
           (void **)&plugin->udfDestroyFunc,   (void **)&plugin->udfScalarProcFunc, (void **)&plugin->udfAggStartFunc,
2✔
420
           (void **)&plugin->udfAggFinishFunc, (void **)&plugin->udfAggProcFunc,    (void **)&plugin->udfAggMergeFunc};
2✔
421
  int32_t err = udfdLoadSharedLib(plugin->libPath, &plugin->lib, funcName, funcs, UDFD_MAX_PLUGIN_FUNCS);
2✔
422
  if (err != 0) {
2!
423
    fnError("can not load python plugin. lib path %s", plugin->libPath);
×
424
    return err;
×
425
  }
426

427
  if (plugin->openFunc) {
2!
428
    int16_t lenPythonPath =
2✔
429
        strlen(tsUdfdLdLibPath) + strlen(global.udfDataDir) + 1 + 1;  // global.udfDataDir:tsUdfdLdLibPath
2✔
430
    char *pythonPath = taosMemoryMalloc(lenPythonPath);
2!
431
    if(pythonPath == NULL) {
2!
432
      uv_dlclose(&plugin->lib);
×
433
      return terrno;
×
434
    }
435
#ifdef WINDOWS
436
    snprintf(pythonPath, lenPythonPath, "%s;%s", global.udfDataDir, tsUdfdLdLibPath);
437
#else
438
    snprintf(pythonPath, lenPythonPath, "%s:%s", global.udfDataDir, tsUdfdLdLibPath);
2✔
439
#endif
440
    SScriptUdfEnvItem items[] = {{"PYTHONPATH", pythonPath}, {"LOGDIR", tsLogDir}};
2✔
441
    err = plugin->openFunc(items, 2);
2✔
442
    taosMemoryFree(pythonPath);
2!
443
  }
444
  if (err != 0) {
2!
445
    fnError("udf script python plugin open func failed. error: %d", err);
×
446
    uv_dlclose(&plugin->lib);
×
447
    return err;
×
448
  }
449
  plugin->libLoaded = true;
2✔
450

451
  return 0;
2✔
452
}
453

454
void udfdDeinitCPlugin(SUdfScriptPlugin *plugin) {
3✔
455
  TAOS_UDF_CHECK_PTR_RVOID(plugin);
6!
456
  if (plugin->closeFunc) {
3!
457
    if (plugin->closeFunc() != 0) {
3!
458
      fnError("udf script c plugin close func failed.line:%d", __LINE__);
×
459
    }
460
  }
461
  plugin->openFunc = NULL;
3✔
462
  plugin->closeFunc = NULL;
3✔
463
  plugin->udfInitFunc = NULL;
3✔
464
  plugin->udfDestroyFunc = NULL;
3✔
465
  plugin->udfScalarProcFunc = NULL;
3✔
466
  plugin->udfAggStartFunc = NULL;
3✔
467
  plugin->udfAggProcFunc = NULL;
3✔
468
  plugin->udfAggMergeFunc = NULL;
3✔
469
  plugin->udfAggFinishFunc = NULL;
3✔
470
  return;
3✔
471
}
472

473
void udfdDeinitPythonPlugin(SUdfScriptPlugin *plugin) {
2✔
474
  TAOS_UDF_CHECK_PTR_RVOID(plugin);
4!
475
  if (plugin->closeFunc) {
2!
476
    if (plugin->closeFunc() != 0) {
2!
477
      fnError("udf script python plugin close func failed.line:%d", __LINE__);
×
478
    }
479
  }
480
  uv_dlclose(&plugin->lib);
2✔
481
  if (plugin->libLoaded) {
2!
482
    plugin->libLoaded = false;
2✔
483
  }
484
  plugin->openFunc = NULL;
2✔
485
  plugin->closeFunc = NULL;
2✔
486
  plugin->udfInitFunc = NULL;
2✔
487
  plugin->udfDestroyFunc = NULL;
2✔
488
  plugin->udfScalarProcFunc = NULL;
2✔
489
  plugin->udfAggStartFunc = NULL;
2✔
490
  plugin->udfAggProcFunc = NULL;
2✔
491
  plugin->udfAggMergeFunc = NULL;
2✔
492
  plugin->udfAggFinishFunc = NULL;
2✔
493
}
494

495
int32_t udfdInitScriptPlugin(int8_t scriptType) {
5✔
496
  SUdfScriptPlugin *plugin = taosMemoryCalloc(1, sizeof(SUdfScriptPlugin));
5!
497
  if (plugin == NULL) {
5!
498
    return terrno;
×
499
  }
500
  int32_t err = 0;
5✔
501
  switch (scriptType) {
5!
502
    case TSDB_FUNC_SCRIPT_BIN_LIB:
3✔
503
      err = udfdInitializeCPlugin(plugin);
3✔
504
      if (err != 0) {
3!
505
        fnError("udf script c plugin init failed. error: %d", err);
×
506
        taosMemoryFree(plugin);
×
507
        return err;
×
508
      }
509
      break;
3✔
510
    case TSDB_FUNC_SCRIPT_PYTHON: {
2✔
511
      err = udfdInitializePythonPlugin(plugin);
2✔
512
      if (err != 0) {
2!
513
        fnError("udf script python plugin init failed. error: %d", err);
×
514
        taosMemoryFree(plugin);
×
515
        return err;
×
516
      }
517
      break;
2✔
518
    }
519
    default:
×
520
      fnError("udf script type %d not supported", scriptType);
×
521
      taosMemoryFree(plugin);
×
522
      return TSDB_CODE_UDF_SCRIPT_NOT_SUPPORTED;
×
523
  }
524

525
  global.scriptPlugins[scriptType] = plugin;
5✔
526
  return TSDB_CODE_SUCCESS;
5✔
527
}
528

529
void udfdDeinitScriptPlugins() {
70✔
530
  SUdfScriptPlugin *plugin = NULL;
70✔
531
  plugin = global.scriptPlugins[TSDB_FUNC_SCRIPT_PYTHON];
70✔
532
  if (plugin != NULL) {
70✔
533
    udfdDeinitPythonPlugin(plugin);
2✔
534
    taosMemoryFree(plugin);
2!
535
    global.scriptPlugins[TSDB_FUNC_SCRIPT_PYTHON] = NULL;
2✔
536
  }
537

538
  plugin = global.scriptPlugins[TSDB_FUNC_SCRIPT_BIN_LIB];
70✔
539
  if (plugin != NULL) {
70✔
540
    udfdDeinitCPlugin(plugin);
3✔
541
    taosMemoryFree(plugin);
3!
542
    global.scriptPlugins[TSDB_FUNC_SCRIPT_BIN_LIB] = NULL;
3✔
543
  }
544
  return;
70✔
545
}
546

547
void udfdProcessRequest(uv_work_t *req) {
3,031✔
548
  TAOS_UDF_CHECK_PTR_RVOID(req);
6,062!
549
  SUvUdfWork *uvUdf = (SUvUdfWork *)(req->data);
3,031✔
550
  if (uvUdf == NULL) {
3,031!
551
    fnError("udf work is NULL");
×
552
    return;
×
553
  }
554
  SUdfRequest request = {0};
3,031✔
555
  if(decodeUdfRequest(uvUdf->input.base, &request) == NULL)
3,031!
556
  {
557
    taosMemoryFreeClear(uvUdf->input.base);
×
558
    fnError("udf request decode failed");
×
559
    return;
×
560
  }
561

562
  switch (request.type) {
3,031!
563
    case UDF_TASK_SETUP: {
311✔
564
      udfdProcessSetupRequest(uvUdf, &request);
311✔
565
      break;
311✔
566
    }
567

568
    case UDF_TASK_CALL: {
2,447✔
569
      udfdProcessCallRequest(uvUdf, &request);
2,447✔
570
      break;
2,447✔
571
    }
572
    case UDF_TASK_TEARDOWN: {
273✔
573
      udfdProcessTeardownRequest(uvUdf, &request);
273✔
574
      break;
273✔
575
    }
576
    default: {
×
577
      break;
×
578
    }
579
  }
580
}
581

582
static void convertUdf2UdfInfo(SUdf *udf, SScriptUdfInfo *udfInfo) {
264✔
583
  udfInfo->bufSize = udf->bufSize;
264✔
584
  if (udf->funcType == TSDB_FUNC_TYPE_AGGREGATE) {
264✔
585
    udfInfo->funcType = UDF_FUNC_TYPE_AGG;
169✔
586
  } else if (udf->funcType == TSDB_FUNC_TYPE_SCALAR) {
95!
587
    udfInfo->funcType = UDF_FUNC_TYPE_SCALAR;
95✔
588
  }
589
  udfInfo->name = udf->name;
264✔
590
  udfInfo->version = udf->version;
264✔
591
  udfInfo->createdTime = udf->createdTime;
264✔
592
  udfInfo->outputLen = udf->outputLen;
264✔
593
  udfInfo->outputType = udf->outputType;
264✔
594
  udfInfo->path = udf->path;
264✔
595
  udfInfo->scriptType = udf->scriptType;
264✔
596
}
264✔
597

598
static int32_t udfdInitUdf(char *udfName, SUdf *udf) {
264✔
599
  TAOS_UDF_CHECK_PTR_RCODE(udfName, udf);
792!
600
  int32_t err = 0;
264✔
601
  err = udfdFillUdfInfoFromMNode(global.clientRpc, udfName, udf);
264✔
602
  if (err != 0) {
264!
603
    fnError("can not retrieve udf from mnode. udf name %s", udfName);
×
604
    return TSDB_CODE_UDF_LOAD_UDF_FAILURE;
×
605
  }
606
  if (udf->scriptType > UDFD_MAX_SCRIPT_TYPE) {
264!
607
    fnError("udf name %s script type %d not supported", udfName, udf->scriptType);
×
608
    return TSDB_CODE_UDF_SCRIPT_NOT_SUPPORTED;
×
609
  }
610

611
  uv_mutex_lock(&global.scriptPluginsMutex);
264✔
612
  SUdfScriptPlugin *scriptPlugin = global.scriptPlugins[udf->scriptType];
264✔
613
  if (scriptPlugin == NULL) {
264✔
614
    err = udfdInitScriptPlugin(udf->scriptType);
5✔
615
    if (err != 0) {
5!
616
      fnError("udf name %s init script plugin failed. error %d", udfName, err);
×
617
      uv_mutex_unlock(&global.scriptPluginsMutex);
×
618
      return err;
×
619
    }
620
  }
621
  uv_mutex_unlock(&global.scriptPluginsMutex);
264✔
622
  udf->scriptPlugin = global.scriptPlugins[udf->scriptType];
264✔
623

624
  SScriptUdfInfo info = {0};
264✔
625
  convertUdf2UdfInfo(udf, &info);
264✔
626
  err = udf->scriptPlugin->udfInitFunc(&info, &udf->scriptUdfCtx);
264✔
627
  if (err != 0) {
264✔
628
    fnError("udf name %s init failed. error %d", udfName, err);
8!
629
    return err;
8✔
630
  }
631

632
  fnInfo("udf init succeeded. name %s type %d context %p", udf->name, udf->scriptType, (void *)udf->scriptUdfCtx);
256!
633
  return 0;
256✔
634
}
635

636
int32_t udfdNewUdf(SUdf **pUdf, const char *udfName) {
258✔
637
  TAOS_UDF_CHECK_PTR_RCODE(pUdf, udfName);
774!
638
  SUdf *udfNew = taosMemoryCalloc(1, sizeof(SUdf));
258!
639
  if (NULL == udfNew) {
258!
640
    return terrno;
×
641
  }
642
  udfNew->refCount = 1;
258✔
643
  udfNew->lastFetchTime = taosGetTimestampMs();
258✔
644
  tstrncpy(udfNew->name, udfName, TSDB_FUNC_NAME_LEN);
258✔
645

646
  udfNew->state = UDF_STATE_INIT;
258✔
647
  if (uv_mutex_init(&udfNew->lock) != 0) return TSDB_CODE_UDF_UV_EXEC_FAILURE;
258!
648
  if (uv_cond_init(&udfNew->condReady) != 0) return TSDB_CODE_UDF_UV_EXEC_FAILURE;
258!
649

650
  udfNew->resident = false;
258✔
651
  udfNew->expired = false;
258✔
652
  for (int32_t i = 0; i < taosArrayGetSize(global.residentFuncs); ++i) {
258!
653
    char *funcName = taosArrayGet(global.residentFuncs, i);
×
654
    if (strcmp(udfName, funcName) == 0) {
×
655
      udfNew->resident = true;
×
656
      break;
×
657
    }
658
  }
659
  *pUdf =  udfNew;
258✔
660
  return 0;
258✔
661
}
662

663
void udfdFreeUdf(void *pData) {
×
664
  SUdf *pSudf = (SUdf *)pData;
×
665
  if (pSudf == NULL) {
×
666
    return;
×
667
  }
668

669
  if (pSudf->scriptPlugin != NULL) {
×
670
    if(pSudf->scriptPlugin->udfDestroyFunc(pSudf->scriptUdfCtx) != 0) {
×
671
      fnError("udfdFreeUdf: taosudf destroy udf %s failed", pSudf->name);
×
672
    }
673
  }
674

675
  uv_mutex_destroy(&pSudf->lock);
×
676
  uv_cond_destroy(&pSudf->condReady);
×
677
  taosMemoryFree(pSudf);
×
678
}
679

680
int32_t udfdGetOrCreateUdf(SUdf **ppUdf, const char *udfName) {
311✔
681
  TAOS_UDF_CHECK_PTR_RCODE(ppUdf, udfName);
933!
682
  uv_mutex_lock(&global.udfsMutex);
311✔
683
  SUdf  **pUdfHash = taosHashGet(global.udfsHash, udfName, strlen(udfName));
311✔
684
  int64_t currTime = taosGetTimestampMs();
311✔
685
  bool    expired = false;
311✔
686
  if (pUdfHash) {
311✔
687
    expired = currTime - (*pUdfHash)->lastFetchTime > 10 * 1000;  // 10s
54✔
688
    if (!expired) {
54✔
689
      ++(*pUdfHash)->refCount;
53✔
690
      *ppUdf = *pUdfHash;
53✔
691
      uv_mutex_unlock(&global.udfsMutex);
53✔
692
      fnInfo("taosudf reuse existing udf. udf  %s udf version %d, udf created time %" PRIx64, (*ppUdf)->name, (*ppUdf)->version,
53!
693
             (*ppUdf)->createdTime);
694
      return 0;
53✔
695
    } else {
696
      (*pUdfHash)->expired = true;
1✔
697
      fnInfo("taosudf expired, check for new version. existing udf %s udf version %d, udf created time %" PRIx64,
1!
698
             (*pUdfHash)->name, (*pUdfHash)->version, (*pUdfHash)->createdTime);
699
      if(taosHashRemove(global.udfsHash, udfName, strlen(udfName)) != 0) {
1!
700
        fnError("udfdGetOrCreateUdf: taosudf remove udf %s failed", udfName);
×
701
      }
702
    }
703
  }
704

705
  int32_t code = udfdNewUdf(ppUdf, udfName);
258✔
706
  if(code != 0) {
258!
707
    uv_mutex_unlock(&global.udfsMutex);
×
708
    return code;
×
709
  }
710

711
  if ((code = taosHashPut(global.udfsHash, udfName, strlen(udfName), ppUdf, POINTER_BYTES)) != 0) {
258!
712
    uv_mutex_unlock(&global.udfsMutex);
×
713
    return code;
×
714
  }
715
  uv_mutex_unlock(&global.udfsMutex);
258✔
716

717
  return 0;
258✔
718
}
719

720
void udfdProcessSetupRequest(SUvUdfWork *uvUdf, SUdfRequest *request) {
311✔
721
  TAOS_UDF_CHECK_PTR_RVOID(uvUdf, request);
933!
722
  // TODO: tracable id from client. connect, setup, call, teardown
723
  fnInfo("setup request. seq num: %" PRId64 ", udf name: %s", request->seqNum, request->setup.udfName);
311!
724

725
  SUdfSetupRequest *setup = &request->setup;
311✔
726
  int32_t           code = TSDB_CODE_SUCCESS;
311✔
727
  SUdf *udf = NULL;
311✔
728

729
  code = udfdGetOrCreateUdf(&udf, setup->udfName);
311✔
730
  if(code != 0) {
311!
731
    fnError("udfdGetOrCreateUdf failed. udf name %s", setup->udfName);
×
732
    goto _send;
×
733
  }
734
  uv_mutex_lock(&udf->lock);
311✔
735
  if (udf->state == UDF_STATE_INIT) {
311✔
736
    udf->state = UDF_STATE_LOADING;
264✔
737
    code = udfdInitUdf(setup->udfName, udf);
264✔
738
    if (code == 0) {
264✔
739
      udf->state = UDF_STATE_READY;
256✔
740
    } else {
741
      udf->state = UDF_STATE_INIT;
8✔
742
    }
743
    uv_cond_broadcast(&udf->condReady);
264✔
744
    uv_mutex_unlock(&udf->lock);
264✔
745
  } else {
746
    while (udf->state == UDF_STATE_LOADING) {
47!
747
      uv_cond_wait(&udf->condReady, &udf->lock);
×
748
    }
749
    uv_mutex_unlock(&udf->lock);
47✔
750
  }
751
  SUdfcFuncHandle *handle = taosMemoryMalloc(sizeof(SUdfcFuncHandle));
311!
752
  if(handle == NULL) {
311!
753
    fnError("udfdProcessSetupRequest: malloc failed.");
×
754
    code = terrno;
×
755
  }
756
  handle->udf = udf;
311✔
757

758
_send:
311✔
759
  ;
760
  SUdfResponse rsp;
761
  rsp.seqNum = request->seqNum;
311✔
762
  rsp.type = request->type;
311✔
763
  rsp.code = (code != 0) ? TSDB_CODE_UDF_FUNC_EXEC_FAILURE : 0;
311✔
764
  rsp.setupRsp.udfHandle = (int64_t)(handle);
311✔
765
  rsp.setupRsp.outputType = udf->outputType;
311✔
766
  rsp.setupRsp.bytes = udf->outputLen;
311✔
767
  rsp.setupRsp.bufSize = udf->bufSize;
311✔
768

769
  int32_t len = encodeUdfResponse(NULL, &rsp);
311✔
770
  if(len < 0) {
311!
771
    fnError("udfdProcessSetupRequest: encode udf response failed. len %d", len);
×
772
    return;
×
773
  }
774
  rsp.msgLen = len;
311✔
775
  void *bufBegin = taosMemoryMalloc(len);
311!
776
  if(bufBegin == NULL) {
311!
777
    fnError("udfdProcessSetupRequest: malloc failed. len %d", len);
×
778
    return;
×
779
  }
780
  void *buf = bufBegin;
311✔
781
  if(encodeUdfResponse(&buf, &rsp) < 0) {
311!
782
    fnError("udfdProcessSetupRequest: encode udf response failed. len %d", len);
×
783
    taosMemoryFree(bufBegin);
×
784
    return;
×
785
  }
786
  
787
  uvUdf->output = uv_buf_init(bufBegin, len);
311✔
788

789
  taosMemoryFreeClear(uvUdf->input.base);
311!
790
  return;
311✔
791
}
792

793
static int32_t checkUDFScalaResult(SSDataBlock *block, SUdfColumn *output) {
580✔
794
  if (tsSafetyCheckLevel == TSDB_SAFETY_CHECK_LEVELL_NEVER) {
580!
795
    return TSDB_CODE_SUCCESS;
×
796
  }
797
  if (output->colData.numOfRows != block->info.rows) {
580!
798
    fnError("udf scala result num of rows %d not equal to input rows %" PRId64, output->colData.numOfRows, block->info.rows);
×
799
    return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
800
  }
801

802
  if (tsSafetyCheckLevel == TSDB_SAFETY_CHECK_LEVELL_BYROW) {
580✔
803
    for (int32_t i = 0; i < output->colData.numOfRows; ++i) {
712,590✔
804
      if (!udfColDataIsNull(output, i)) {
712,248✔
805
        if (IS_VAR_DATA_TYPE(output->colMeta.type)) {
12,099!
806
          TAOS_UDF_CHECK_CONDITION(output->colData.varLenCol.payload != NULL, TSDB_CODE_UDF_FUNC_EXEC_FAILURE);
11,880!
807
          TAOS_UDF_CHECK_CONDITION(output->colData.varLenCol.varOffsets[i] >= 0 &&
11,880!
808
                                       output->colData.varLenCol.varOffsets[i] < output->colData.varLenCol.payloadLen,
809
                                   TSDB_CODE_UDF_FUNC_EXEC_FAILURE);
810
        } else {
811
          TAOS_UDF_CHECK_CONDITION(
219!
812
              output->colMeta.bytes * output->colData.numOfRows <= output->colData.fixLenCol.dataLen,
813
              TSDB_CODE_UDF_FUNC_EXEC_FAILURE);
814
          break;
219✔
815
        }
816
      }
817
    }
818
  }
819

820
  return TSDB_CODE_SUCCESS;
580✔
821
}
822

823
static int32_t checkUDFAggResult(SSDataBlock *block, SUdfInterBuf *output) {
1,205✔
824
  if (tsSafetyCheckLevel == TSDB_SAFETY_CHECK_LEVELL_NEVER) {
1,205!
825
    return TSDB_CODE_SUCCESS;
×
826
  }
827
  if (output->numOfResult != 1 && output->numOfResult != 0) {
1,205!
828
    fnError("udf agg result num of rows %d not equal to 1", output->numOfResult);
×
829
    return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
830
  }
831
  TAOS_UDF_CHECK_CONDITION(output->buf != NULL, TSDB_CODE_UDF_FUNC_EXEC_FAILURE);
1,205!
832
  TAOS_UDF_CHECK_CONDITION(output->bufLen > 0, TSDB_CODE_UDF_FUNC_EXEC_FAILURE);
1,205!
833
  return TSDB_CODE_SUCCESS;
1,205✔
834
}
835

836
void udfdProcessCallRequest(SUvUdfWork *uvUdf, SUdfRequest *request) {
2,447✔
837
  TAOS_UDF_CHECK_PTR_RVOID(uvUdf, request);
7,341!
838
  SUdfCallRequest *call = &request->call;
2,447✔
839
  fnDebug("call request. call type %d, handle: %" PRIx64 ", seq num %" PRId64, call->callType, call->udfHandle,
2,447✔
840
          request->seqNum);
841
  SUdfcFuncHandle  *handle = (SUdfcFuncHandle *)(call->udfHandle);
2,447✔
842
  SUdf             *udf = handle->udf;
2,447✔
843
  SUdfResponse      response = {0};
2,447✔
844
  SUdfResponse     *rsp = &response;
2,447✔
845
  SUdfCallResponse *subRsp = &rsp->callRsp;
2,447✔
846

847
  int32_t code = TSDB_CODE_SUCCESS;
2,447✔
848
  switch (call->callType) {
2,447!
849
    case TSDB_UDF_CALL_SCALA_PROC: {
596✔
850
      SUdfColumn output = {0};
596✔
851
      output.colMeta.bytes = udf->outputLen;
596✔
852
      output.colMeta.type = udf->outputType;
596✔
853
      output.colMeta.precision = 0;
596✔
854
      output.colMeta.scale = 0;
596✔
855
      if (udfColEnsureCapacity(&output, call->block.info.rows) == TSDB_CODE_SUCCESS) {
1,192!
856
        SUdfDataBlock input = {0};
596✔
857
        code = convertDataBlockToUdfDataBlock(&call->block, &input);
596✔
858
        if (code == TSDB_CODE_SUCCESS) code = udf->scriptPlugin->udfScalarProcFunc(&input, &output, udf->scriptUdfCtx);
596!
859
        freeUdfDataDataBlock(&input);
596✔
860
        if (code == TSDB_CODE_SUCCESS) code = checkUDFScalaResult(&call->block, &output);
596✔
861
        if (code == TSDB_CODE_SUCCESS) code = convertUdfColumnToDataBlock(&output, &response.callRsp.resultData);
596✔
862
      }
863
      freeUdfColumn(&output);
596✔
864
      break;
596✔
865
    }
866
    case TSDB_UDF_CALL_AGG_INIT: {
339✔
867
      SUdfInterBuf outBuf = {.buf = taosMemoryMalloc(udf->bufSize), .bufLen = udf->bufSize, .numOfResult = 0};
339!
868
      if (outBuf.buf != NULL) {
339!
869
        code = udf->scriptPlugin->udfAggStartFunc(&outBuf, udf->scriptUdfCtx);
339✔
870
      } else {
871
        code = terrno;
×
872
      }
873
      subRsp->resultBuf = outBuf;
339✔
874
      break;
339✔
875
    }
876
    case TSDB_UDF_CALL_AGG_PROC: {
1,205✔
877
      SUdfDataBlock input = {0};
1,205✔
878
      if (convertDataBlockToUdfDataBlock(&call->block, &input) == TSDB_CODE_SUCCESS) {
1,205!
879
        SUdfInterBuf outBuf = {.buf = taosMemoryMalloc(udf->bufSize), .bufLen = udf->bufSize, .numOfResult = 0};
1,205!
880
        if (outBuf.buf != NULL) {
1,205!
881
          code = udf->scriptPlugin->udfAggProcFunc(&input, &call->interBuf, &outBuf, udf->scriptUdfCtx);
1,205✔
882
          freeUdfInterBuf(&call->interBuf);
1,205✔
883
          if (code == TSDB_CODE_SUCCESS) code = checkUDFAggResult(&call->block, &outBuf);
1,205!
884
          subRsp->resultBuf = outBuf;
1,205✔
885
        } else {
886
          code = terrno;
×
887
        }
888
      }
889
      freeUdfDataDataBlock(&input);
1,205✔
890

891
      break;
1,205✔
892
    }
893
    // case TSDB_UDF_CALL_AGG_MERGE: {
894
    //   SUdfInterBuf outBuf = {.buf = taosMemoryMalloc(udf->bufSize), .bufLen = udf->bufSize, .numOfResult = 0};
895
    //   if (outBuf.buf != NULL) {
896
    //     code = udf->scriptPlugin->udfAggMergeFunc(&call->interBuf, &call->interBuf2, &outBuf, udf->scriptUdfCtx);
897
    //     freeUdfInterBuf(&call->interBuf);
898
    //     freeUdfInterBuf(&call->interBuf2);
899
    //     subRsp->resultBuf = outBuf;
900
    //   } else {
901
    //     code = terrno;
902
    //   }
903
    // 
904
    //   break;
905
    // }
906
    case TSDB_UDF_CALL_AGG_FIN: {
307✔
907
      SUdfInterBuf outBuf = {.buf = taosMemoryMalloc(udf->bufSize), .bufLen = udf->bufSize, .numOfResult = 0};
307!
908
      if (outBuf.buf != NULL) {
307!
909
        code = udf->scriptPlugin->udfAggFinishFunc(&call->interBuf, &outBuf, udf->scriptUdfCtx);
307✔
910
        freeUdfInterBuf(&call->interBuf);
307✔
911
        subRsp->resultBuf = outBuf;
307✔
912
      } else {
913
        code = terrno;
×
914
      }
915

916
      break;
307✔
917
    }
918
    default:
×
919
      break;
×
920
  }
921

922
  rsp->seqNum = request->seqNum;
2,447✔
923
  rsp->type = request->type;
2,447✔
924
  rsp->code = (code != 0) ? TSDB_CODE_UDF_FUNC_EXEC_FAILURE : 0;
2,447✔
925
  subRsp->callType = call->callType;
2,447✔
926

927
  int32_t len = encodeUdfResponse(NULL, rsp);
2,447✔
928
  if(len < 0) {
2,446!
929
    fnError("udfdProcessCallRequest: encode udf response failed. len %d", len);
×
930
    goto _exit;
×
931
  }
932
  rsp->msgLen = len;
2,446✔
933
  void *bufBegin = taosMemoryMalloc(len);
2,446!
934
  if (bufBegin == NULL) {
2,447!
935
    fnError("udfdProcessCallRequest: malloc failed. len %d", len);
×
936
    goto _exit;
×
937
  }
938
  void *buf = bufBegin;
2,447✔
939
  if(encodeUdfResponse(&buf, rsp) < 0) {
2,447!
940
    fnError("udfdProcessCallRequest: encode udf response failed. len %d", len);
×
941
    taosMemoryFree(bufBegin);
×
942
    goto _exit;
×
943
  }
944

945
  uvUdf->output = uv_buf_init(bufBegin, len);
2,447✔
946

947
_exit:
2,447✔
948
  switch (call->callType) {
2,447!
949
    case TSDB_UDF_CALL_SCALA_PROC: {
596✔
950
      blockDataFreeRes(&call->block);
596✔
951
      blockDataFreeRes(&subRsp->resultData);
596✔
952
      break;
596✔
953
    }
954
    case TSDB_UDF_CALL_AGG_INIT: {
339✔
955
      freeUdfInterBuf(&subRsp->resultBuf);
339✔
956
      break;
339✔
957
    }
958
    case TSDB_UDF_CALL_AGG_PROC: {
1,205✔
959
      blockDataFreeRes(&call->block);
1,205✔
960
      freeUdfInterBuf(&subRsp->resultBuf);
1,205✔
961
      break;
1,205✔
962
    }
963
    // case TSDB_UDF_CALL_AGG_MERGE: {
964
    //   freeUdfInterBuf(&subRsp->resultBuf);
965
    //   break;
966
    // }
967
    case TSDB_UDF_CALL_AGG_FIN: {
307✔
968
      freeUdfInterBuf(&subRsp->resultBuf);
307✔
969
      break;
307✔
970
    }
971
    default:
×
972
      break;
×
973
  }
974

975
  taosMemoryFreeClear(uvUdf->input.base);
2,447!
976
  return;
2,447✔
977
}
978

979
void udfdProcessTeardownRequest(SUvUdfWork *uvUdf, SUdfRequest *request) {
273✔
980
  TAOS_UDF_CHECK_PTR_RVOID(uvUdf, request);
819!
981
  SUdfTeardownRequest *teardown = &request->teardown;
273✔
982
  fnInfo("teardown. seq number: %" PRId64 ", handle:%" PRIx64, request->seqNum, teardown->udfHandle);
273!
983
  SUdfcFuncHandle *handle = (SUdfcFuncHandle *)(teardown->udfHandle);
273✔
984
  SUdf            *udf = handle->udf;
273✔
985
  bool             unloadUdf = false;
273✔
986
  int32_t          code = TSDB_CODE_SUCCESS;
273✔
987

988
  uv_mutex_lock(&global.udfsMutex);
273✔
989
  udf->refCount--;
273✔
990
  if (udf->refCount == 0 && (!udf->resident || udf->expired)) {
273!
991
    unloadUdf = true;
226✔
992
    code = taosHashRemove(global.udfsHash, udf->name, strlen(udf->name));
226✔
993
    if (code != 0) {
226!
994
      fnError("udf name %s remove from hash failed, err:%0x %s", udf->name, code, tstrerror(code));
×
995
      uv_mutex_unlock(&global.udfsMutex);
×
996
      goto _send;
×
997
    }
998
  }
999
  uv_mutex_unlock(&global.udfsMutex);
273✔
1000
  if (unloadUdf) {
273✔
1001
    fnInfo("udf teardown. udf name: %s type %d: context %p", udf->name, udf->scriptType, (void *)(udf->scriptUdfCtx));
226!
1002
    uv_cond_destroy(&udf->condReady);
226✔
1003
    uv_mutex_destroy(&udf->lock);
226✔
1004
    code = udf->scriptPlugin->udfDestroyFunc(udf->scriptUdfCtx);
226✔
1005
    fnDebug("taosudf destroy function returns %d", code);
226✔
1006
    taosMemoryFree(udf);
226!
1007
  }
1008

1009
_send:
47✔
1010
  taosMemoryFree(handle);
273!
1011
  SUdfResponse  response = {0};
273✔
1012
  SUdfResponse *rsp = &response;
273✔
1013
  rsp->seqNum = request->seqNum;
273✔
1014
  rsp->type = request->type;
273✔
1015
  rsp->code = code;
273✔
1016
  int32_t len = encodeUdfResponse(NULL, rsp);
273✔
1017
  if (len < 0) {
273!
1018
    fnError("udfdProcessTeardownRequest: encode udf response failed. len %d", len);
×
1019
    return;
×
1020
  }
1021
  rsp->msgLen = len;
273✔
1022
  void *bufBegin = taosMemoryMalloc(len);
273!
1023
  if(bufBegin == NULL) {
273!
1024
    fnError("udfdProcessTeardownRequest: malloc failed. len %d", len);
×
1025
    return;
×
1026
  }
1027
  void *buf = bufBegin;
273✔
1028
  if (encodeUdfResponse(&buf, rsp) < 0) {
273!
1029
    fnError("udfdProcessTeardownRequest: encode udf response failed. len %d", len);
×
1030
    taosMemoryFree(bufBegin);
×
1031
    return;
×
1032
  }
1033
  uvUdf->output = uv_buf_init(bufBegin, len);
273✔
1034

1035
  taosMemoryFree(uvUdf->input.base);
273!
1036
  return;
273✔
1037
}
1038

1039
void udfdGetFuncBodyPath(const SUdf *udf, char *path) {
264✔
1040
  TAOS_UDF_CHECK_PTR_RVOID(udf, path);
792!
1041
  if (udf->scriptType == TSDB_FUNC_SCRIPT_BIN_LIB) {
264✔
1042
#ifdef WINDOWS
1043
    snprintf(path, PATH_MAX, "%s%s_%d_%" PRIx64 ".dll", global.udfDataDir, udf->name, udf->version, udf->createdTime);
1044
#else
1045
    snprintf(path, PATH_MAX, "%s/lib%s_%d_%" PRIx64 ".so", global.udfDataDir, udf->name, udf->version,
138✔
1046
             udf->createdTime);
1047
#endif
1048
  } else if (udf->scriptType == TSDB_FUNC_SCRIPT_PYTHON) {
126!
1049
#ifdef WINDOWS
1050
    snprintf(path, PATH_MAX, "%s%s_%d_%" PRIx64 ".py", global.udfDataDir, udf->name, udf->version, udf->createdTime);
1051
#else
1052
    snprintf(path, PATH_MAX, "%s/%s_%d_%" PRIx64 ".py", global.udfDataDir, udf->name, udf->version, udf->createdTime);
126✔
1053
#endif
1054
  } else {
1055
#ifdef WINDOWS
1056
    snprintf(path, PATH_MAX, "%s%s_%d_%" PRIx64, global.udfDataDir, udf->name, udf->version, udf->createdTime);
1057
#else
1058
    snprintf(path, PATH_MAX, "%s/lib%s_%d_%" PRIx64, global.udfDataDir, udf->name, udf->version, udf->createdTime);
×
1059
#endif
1060
  }
1061
}
1062

1063
int32_t udfdSaveFuncBodyToFile(SFuncInfo *pFuncInfo, SUdf *udf) {
264✔
1064
  TAOS_UDF_CHECK_PTR_RCODE(pFuncInfo, udf);
792!
1065
  if (!osDataSpaceAvailable()) {
264!
1066
    terrno = TSDB_CODE_NO_DISKSPACE;
×
1067
    fnError("taosudf create shared library failed since %s", terrstr());
×
1068
    return terrno;
×
1069
  }
1070

1071
  char path[PATH_MAX] = {0};
264✔
1072
  udfdGetFuncBodyPath(udf, path);
264✔
1073
  bool fileExist = !(taosStatFile(path, NULL, NULL, NULL) < 0);
264✔
1074
  if (fileExist) {
264✔
1075
    tstrncpy(udf->path, path, PATH_MAX);
196✔
1076
    fnInfo("taosudf func body file. reuse existing file %s", path);
196!
1077
    return TSDB_CODE_SUCCESS;
196✔
1078
  }
1079

1080
  TdFilePtr file = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC);
68✔
1081
  if (file == NULL) {
68!
1082
    fnError("taosudf write udf shared library: %s failed, error: %d %s", path, ERRNO, strerror(ERRNO));
×
1083
    return TSDB_CODE_FILE_CORRUPTED;
×
1084
  }
1085
  int64_t count = taosWriteFile(file, pFuncInfo->pCode, pFuncInfo->codeSize);
68✔
1086
  if (count != pFuncInfo->codeSize) {
68!
1087
    fnError("taosudf write udf shared library failed");
×
1088
    return TSDB_CODE_FILE_CORRUPTED;
×
1089
  }
1090
  if(taosCloseFile(&file) != 0) {
68!
1091
    fnError("udfdSaveFuncBodyToFile, taosudf close file failed");
×
1092
    return TSDB_CODE_FILE_CORRUPTED;
×
1093
  }
1094

1095
  tstrncpy(udf->path, path, PATH_MAX);
68✔
1096
  return TSDB_CODE_SUCCESS;
68✔
1097
}
1098

1099
void udfdProcessRpcRsp(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
264✔
1100
  TAOS_UDF_CHECK_PTR_RVOID(parent, pMsg);
792!
1101
  SUdfdRpcSendRecvInfo *msgInfo = (SUdfdRpcSendRecvInfo *)pMsg->info.ahandle;
264✔
1102

1103
  if (pEpSet) {
264!
1104
    if (!isEpsetEqual(&global.mgmtEp.epSet, pEpSet)) {
×
1105
      updateEpSet_s(&global.mgmtEp, pEpSet);
×
1106
    }
1107
  }
1108

1109
  if (pMsg->code != TSDB_CODE_SUCCESS) {
264!
1110
    fnError("taosudf rpc error, code:%s", tstrerror(pMsg->code));
×
1111
    msgInfo->code = pMsg->code;
×
1112
    goto _return;
×
1113
  }
1114

1115
  if (msgInfo->rpcType == UDFD_RPC_MNODE_CONNECT) {
264!
1116
    SConnectRsp connectRsp = {0};
×
1117
    if(tDeserializeSConnectRsp(pMsg->pCont, pMsg->contLen, &connectRsp) < 0){
×
1118
      fnError("taosudf deserialize connect response failed");
×
1119
      goto _return;
×
1120
    }
1121

1122
    int32_t now = taosGetTimestampSec();
×
1123
    int32_t delta = abs(now - connectRsp.svrTimestamp);
×
1124
    if (delta > 900) {
×
1125
      msgInfo->code = TSDB_CODE_TIME_UNSYNCED;
×
1126
      goto _return;
×
1127
    }
1128

1129
    if (connectRsp.epSet.numOfEps == 0) {
×
1130
      msgInfo->code = TSDB_CODE_APP_ERROR;
×
1131
      goto _return;
×
1132
    }
1133

1134
    if (connectRsp.dnodeNum > 1 && !isEpsetEqual(&global.mgmtEp.epSet, &connectRsp.epSet)) {
×
1135
      updateEpSet_s(&global.mgmtEp, &connectRsp.epSet);
×
1136
    }
1137
    msgInfo->code = 0;
×
1138
  } else if (msgInfo->rpcType == UDFD_RPC_RETRIVE_FUNC) {
264!
1139
    SRetrieveFuncRsp retrieveRsp = {0};
264✔
1140
    if(tDeserializeSRetrieveFuncRsp(pMsg->pCont, pMsg->contLen, &retrieveRsp) < 0){
264!
1141
      fnError("taosudf deserialize retrieve func response failed");
×
1142
      goto _return;
×
1143
    }
1144

1145
    SFuncInfo *pFuncInfo = (SFuncInfo *)taosArrayGet(retrieveRsp.pFuncInfos, 0);
264✔
1146
    SUdf      *udf = msgInfo->param;
264✔
1147
    udf->funcType = pFuncInfo->funcType;
264✔
1148
    udf->scriptType = pFuncInfo->scriptType;
264✔
1149
    udf->outputType = pFuncInfo->outputType;
264✔
1150
    udf->outputLen = pFuncInfo->outputLen;
264✔
1151
    udf->bufSize = pFuncInfo->bufSize;
264✔
1152

1153
    SFuncExtraInfo *pFuncExtraInfo = (SFuncExtraInfo *)taosArrayGet(retrieveRsp.pFuncExtraInfos, 0);
264✔
1154
    udf->version = pFuncExtraInfo->funcVersion;
264✔
1155
    udf->createdTime = pFuncExtraInfo->funcCreatedTime;
264✔
1156
    msgInfo->code = udfdSaveFuncBodyToFile(pFuncInfo, udf);
264✔
1157
    if (msgInfo->code != 0) {
264!
1158
      udf->lastFetchTime = 0;
×
1159
    }
1160
    tFreeSFuncInfo(pFuncInfo);
264✔
1161
    taosArrayDestroy(retrieveRsp.pFuncInfos);
264✔
1162
    taosArrayDestroy(retrieveRsp.pFuncExtraInfos);
264✔
1163
  }
1164

1165
_return:
×
1166
  rpcFreeCont(pMsg->pCont);
264✔
1167
  uv_sem_post(&msgInfo->resultSem);
264✔
1168
  return;
264✔
1169
}
1170

1171
int32_t udfdFillUdfInfoFromMNode(void *clientRpc, char *udfName, SUdf *udf) {
264✔
1172
  TAOS_UDF_CHECK_PTR_RCODE(clientRpc, udfName, udf);
1,056!
1173
  SRetrieveFuncReq retrieveReq = {0};
264✔
1174
  retrieveReq.numOfFuncs = 1;
264✔
1175
  retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
264✔
1176
  if(taosArrayPush(retrieveReq.pFuncNames, udfName) == NULL) {
528!
1177
    taosArrayDestroy(retrieveReq.pFuncNames);
×
1178
    return terrno;
×
1179
  }
1180

1181
  int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
264✔
1182
  if(contLen < 0) {
264!
1183
    taosArrayDestroy(retrieveReq.pFuncNames);
×
1184
    return terrno;
×
1185
  }
1186
  void   *pReq = rpcMallocCont(contLen);
264✔
1187
  if(tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq)  < 0) {
264!
1188
    taosArrayDestroy(retrieveReq.pFuncNames);
×
1189
    rpcFreeCont(pReq);
×
1190
    return terrno;
×
1191
  }
1192
  taosArrayDestroy(retrieveReq.pFuncNames);
264✔
1193

1194
  SUdfdRpcSendRecvInfo *msgInfo = taosMemoryCalloc(1, sizeof(SUdfdRpcSendRecvInfo));
264!
1195
  if(NULL == msgInfo) {
264!
1196
    return terrno;
×
1197
  }
1198
  msgInfo->rpcType = UDFD_RPC_RETRIVE_FUNC;
264✔
1199
  msgInfo->param = udf;
264✔
1200
  if(uv_sem_init(&msgInfo->resultSem, 0)  != 0) {
264!
1201
    taosMemoryFree(msgInfo);
×
1202
    return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1203
  }
1204

1205
  SRpcMsg rpcMsg = {0};
264✔
1206
  rpcMsg.pCont = pReq;
264✔
1207
  rpcMsg.contLen = contLen;
264✔
1208
  rpcMsg.msgType = TDMT_MND_RETRIEVE_FUNC;
264✔
1209
  rpcMsg.info.ahandle = msgInfo;
264✔
1210
  int32_t code = rpcSendRequest(clientRpc, &global.mgmtEp.epSet, &rpcMsg, NULL);
264✔
1211
  if (code == 0) {
264!
1212
    uv_sem_wait(&msgInfo->resultSem);
264✔
1213
    uv_sem_destroy(&msgInfo->resultSem);
264✔
1214
    code = msgInfo->code;
264✔
1215
  }
1216
  taosMemoryFree(msgInfo);
264!
1217
  return code;
264✔
1218
}
1219

1220
static bool udfdRpcRfp(int32_t code, tmsg_t msgType) {
×
1221
  if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_BROKEN_LINK || code == TSDB_CODE_SYN_NOT_LEADER ||
×
1222
      code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED || code == TSDB_CODE_SYN_RESTORING ||
×
1223
      code == TSDB_CODE_MNODE_NOT_FOUND || code == TSDB_CODE_APP_IS_STARTING || code == TSDB_CODE_APP_IS_STOPPING) {
×
1224
    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY || msgType == TDMT_SCH_FETCH ||
×
1225
        msgType == TDMT_SCH_MERGE_FETCH || msgType == TDMT_SCH_TASK_NOTIFY) {
×
1226
      return false;
×
1227
    }
1228
    return true;
×
1229
  } else {
1230
    return false;
×
1231
  }
1232
}
1233

1234
int initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet) {
70✔
1235
  pEpSet->version = 0;
70✔
1236

1237
  // init mnode ip set
1238
  SEpSet *mgmtEpSet = &(pEpSet->epSet);
70✔
1239
  mgmtEpSet->numOfEps = 0;
70✔
1240
  mgmtEpSet->inUse = 0;
70✔
1241

1242
  if (firstEp && firstEp[0] != 0) {
70!
1243
    if (strlen(firstEp) >= TSDB_EP_LEN) {
70!
1244
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1245
      return -1;
×
1246
    }
1247

1248
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[0]);
70✔
1249
    if (code != TSDB_CODE_SUCCESS) {
70!
1250
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1251
      return terrno;
×
1252
    }
1253

1254
    mgmtEpSet->numOfEps++;
70✔
1255
  }
1256

1257
  if (secondEp && secondEp[0] != 0) {
70!
1258
    if (strlen(secondEp) >= TSDB_EP_LEN) {
70!
1259
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1260
      return -1;
×
1261
    }
1262

1263
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
70✔
1264
    if (code != TSDB_CODE_SUCCESS) {
70!
1265
      fnError("invalid ep %s", secondEp);
×
1266
    } else {
1267
      mgmtEpSet->numOfEps++;
70✔
1268
    }
1269
  }
1270

1271
  if (mgmtEpSet->numOfEps == 0) {
70!
1272
    terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1273
    return -1;
×
1274
  }
1275

1276
  return 0;
70✔
1277
}
1278

1279
int32_t udfdOpenClientRpc() {
70✔
1280
  SRpcInit rpcInit = {0};
70✔
1281
  rpcInit.label = "UDFD";
70✔
1282
  rpcInit.numOfThreads = 1;
70✔
1283
  rpcInit.cfp = (RpcCfp)udfdProcessRpcRsp;
70✔
1284
  rpcInit.sessions = 1024;
70✔
1285
  rpcInit.connType = TAOS_CONN_CLIENT;
70✔
1286
  rpcInit.idleTime = tsShellActivityTimer * 1000;
70✔
1287
  rpcInit.user = TSDB_DEFAULT_USER;
70✔
1288
  rpcInit.parent = &global;
70✔
1289
  rpcInit.rfp = udfdRpcRfp;
70✔
1290
  rpcInit.compressSize = tsCompressMsgSize;
70✔
1291

1292
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
70✔
1293
  connLimitNum = TMAX(connLimitNum, 10);
70✔
1294
  connLimitNum = TMIN(connLimitNum, 500);
70✔
1295
  rpcInit.connLimitNum = connLimitNum;
70✔
1296
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
70✔
1297
  TAOS_CHECK_RETURN(taosVersionStrToInt(td_version, &rpcInit.compatibilityVer));
70!
1298
  global.clientRpc = rpcOpen(&rpcInit);
70✔
1299
  if (global.clientRpc == NULL) {
70!
1300
    fnError("failed to init dnode rpc client");
×
1301
    return terrno;
×
1302
  }
1303
  return 0;
70✔
1304
}
1305

1306
void udfdCloseClientRpc() {
70✔
1307
  fnInfo("taosudf begin closing rpc");
70!
1308
  rpcClose(global.clientRpc);
70✔
1309
  fnInfo("taosudf finish closing rpc");
70!
1310
}
70✔
1311

1312
void udfdOnWrite(uv_write_t *req, int status) {
3,031✔
1313
  TAOS_UDF_CHECK_PTR_RVOID(req);
6,062!
1314
  SUvUdfWork *work = (SUvUdfWork *)req->data;
3,031✔
1315
  if (status < 0) {
3,031!
1316
    fnError("taosudf send response error, length:%zu code:%s", work->output.len, uv_err_name(status));
×
1317
  }
1318
  // remove work from the connection work list
1319
  if (work->conn != NULL) {
3,031!
1320
    SUvUdfWork **ppWork;
1321
    for (ppWork = &work->conn->pWorkList; *ppWork && (*ppWork != work); ppWork = &((*ppWork)->pWorkNext)) {
3,168!
1322
    }
1323
    if (*ppWork == work) {
3,031!
1324
      *ppWork = work->pWorkNext;
3,031✔
1325
    } else {
1326
      fnError("work not in conn any more");
×
1327
    }
1328
  }
1329
  taosMemoryFree(work->output.base);
3,031!
1330
  taosMemoryFree(work);
3,031!
1331
  taosMemoryFree(req);
3,031!
1332
}
1333

1334
void udfdSendResponse(uv_work_t *work, int status) {
3,031✔
1335
  TAOS_UDF_CHECK_PTR_RVOID(work);
6,062!
1336
  SUvUdfWork *udfWork = (SUvUdfWork *)(work->data);
3,031✔
1337

1338
  if (udfWork->conn != NULL) {
3,031!
1339
    uv_write_t *write_req = taosMemoryMalloc(sizeof(uv_write_t));
3,031!
1340
    if(write_req == NULL) {
3,031!
1341
      fnError("taosudf send response error, malloc failed");
×
1342
      taosMemoryFree(work);
×
1343
      return;
×
1344
    }
1345
    write_req->data = udfWork;
3,031✔
1346
    int32_t code = uv_write(write_req, udfWork->conn->client, &udfWork->output, 1, udfdOnWrite);
3,031✔
1347
    if (code != 0) {
3,031!
1348
      fnError("taosudf send response error %s", uv_strerror(code));
×
1349
      taosMemoryFree(write_req);
×
1350
   }
1351
  }
1352
  taosMemoryFree(work);
3,031!
1353
}
1354

1355
void udfdAllocBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf) {
9,436✔
1356
  TAOS_UDF_CHECK_PTR_RVOID(handle, buf);
28,308!
1357
  SUdfdUvConn *ctx = handle->data;
9,436✔
1358
  int32_t      msgHeadSize = sizeof(int32_t) + sizeof(int64_t);
9,436✔
1359
  if (ctx->inputCap == 0) {
9,436✔
1360
    ctx->inputBuf = taosMemoryMalloc(msgHeadSize);
3,342!
1361
    if (ctx->inputBuf) {
3,342!
1362
      ctx->inputLen = 0;
3,342✔
1363
      ctx->inputCap = msgHeadSize;
3,342✔
1364
      ctx->inputTotal = -1;
3,342✔
1365

1366
      buf->base = ctx->inputBuf;
3,342✔
1367
      buf->len = ctx->inputCap;
3,342✔
1368
    } else {
1369
      fnError("taosudf can not allocate enough memory") buf->base = NULL;
×
1370
      buf->len = 0;
×
1371
    }
1372
  } else if (ctx->inputTotal == -1 && ctx->inputLen < msgHeadSize) {
6,094!
1373
    buf->base = ctx->inputBuf + ctx->inputLen;
3,010✔
1374
    buf->len = msgHeadSize - ctx->inputLen;
3,010✔
1375
  } else {
1376
    ctx->inputCap = ctx->inputTotal > ctx->inputCap ? ctx->inputTotal : ctx->inputCap;
3,084✔
1377
    void *inputBuf = taosMemoryRealloc(ctx->inputBuf, ctx->inputCap);
3,084!
1378
    if (inputBuf) {
3,084!
1379
      ctx->inputBuf = inputBuf;
3,084✔
1380
      buf->base = ctx->inputBuf + ctx->inputLen;
3,084✔
1381
      buf->len = ctx->inputCap - ctx->inputLen;
3,084✔
1382
    } else {
1383
      fnError("taosudf can not allocate enough memory") buf->base = NULL;
×
1384
      buf->len = 0;
×
1385
    }
1386
  }
1387
}
1388

1389
bool isUdfdUvMsgComplete(SUdfdUvConn *pipe) {
6,115✔
1390
  if (pipe == NULL) {
6,115!
1391
    fnError("taosudf pipe is NULL, LINE:%d", __LINE__);
×
1392
    return false;
×
1393
  }
1394
  if (pipe->inputTotal == -1 && pipe->inputLen >= sizeof(int32_t)) {
6,115!
1395
    pipe->inputTotal = *(int32_t *)(pipe->inputBuf);
3,031✔
1396
  }
1397
  if (pipe->inputLen == pipe->inputCap && pipe->inputTotal == pipe->inputCap) {
6,115✔
1398
    fnDebug("receive request complete. length %d", pipe->inputLen);
3,031✔
1399
    return true;
3,031✔
1400
  }
1401
  return false;
3,084✔
1402
}
1403

1404
void udfdHandleRequest(SUdfdUvConn *conn) {
3,031✔
1405
  TAOS_UDF_CHECK_PTR_RVOID(conn);
6,062!
1406
  char   *inputBuf = conn->inputBuf;
3,031✔
1407
  int32_t inputLen = conn->inputLen;
3,031✔
1408

1409
  uv_work_t  *work = taosMemoryMalloc(sizeof(uv_work_t));
3,031!
1410
  if(work == NULL) {
3,031!
1411
    fnError("taosudf malloc work failed");
×
1412
    return;
×
1413
  }
1414
  SUvUdfWork *udfWork = taosMemoryMalloc(sizeof(SUvUdfWork));
3,031!
1415
  if(udfWork == NULL) {
3,031!
1416
    fnError("taosudf malloc udf work failed");
×
1417
    taosMemoryFree(work);
×
1418
    return;
×
1419
  }
1420
  udfWork->conn = conn;
3,031✔
1421
  udfWork->pWorkNext = conn->pWorkList;
3,031✔
1422
  conn->pWorkList = udfWork;
3,031✔
1423
  udfWork->input = uv_buf_init(inputBuf, inputLen);
3,031✔
1424
  conn->inputBuf = NULL;
3,031✔
1425
  conn->inputLen = 0;
3,031✔
1426
  conn->inputCap = 0;
3,031✔
1427
  conn->inputTotal = -1;
3,031✔
1428
  work->data = udfWork;
3,031✔
1429
  if(uv_queue_work(global.loop, work, udfdProcessRequest, udfdSendResponse) != 0)
3,031!
1430
  {
1431
    fnError("taosudf queue work failed");
×
1432
    taosMemoryFree(work);
×
1433
    taosMemoryFree(udfWork);
×
1434
  }
1435
}
1436

1437
void udfdPipeCloseCb(uv_handle_t *pipe) {
311✔
1438
  TAOS_UDF_CHECK_PTR_RVOID(pipe);
622!
1439
  SUdfdUvConn *conn = pipe->data;
311✔
1440
  SUvUdfWork  *pWork = conn->pWorkList;
311✔
1441
  while (pWork != NULL) {
311!
1442
    pWork->conn = NULL;
×
1443
    pWork = pWork->pWorkNext;
×
1444
  }
1445

1446
  taosMemoryFree(conn->client);
311!
1447
  taosMemoryFree(conn->inputBuf);
311!
1448
  taosMemoryFree(conn);
311!
1449
}
1450

1451
void udfdPipeRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
9,436✔
1452
  TAOS_UDF_CHECK_PTR_RVOID(client, buf);
28,308!
1453
  fnDebug("taosudf read %zd bytes from client", nread);
9,436✔
1454
  if (nread == 0) return;
9,436✔
1455

1456
  SUdfdUvConn *conn = client->data;
6,426✔
1457

1458
  if (nread > 0) {
6,426✔
1459
    conn->inputLen += nread;
6,115✔
1460
    if (isUdfdUvMsgComplete(conn)) {
6,115✔
1461
      udfdHandleRequest(conn);
3,031✔
1462
    } else {
1463
      // log error or continue;
1464
    }
1465
    return;
6,115✔
1466
  }
1467

1468
  if (nread < 0) {
311!
1469
    if (nread == UV_EOF) {
311!
1470
      fnInfo("taosudf pipe read EOF");
311!
1471
    } else {
1472
      fnError("Receive error %s", uv_err_name(nread));
×
1473
    }
1474
    udfdUvHandleError(conn);
311✔
1475
  }
1476
}
1477

1478
void udfdOnNewConnection(uv_stream_t *server, int status) {
311✔
1479
  TAOS_UDF_CHECK_PTR_RVOID(server);
622!
1480
  if (status < 0) {
311!
1481
    fnError("taosudf new connection error, code:%s", uv_strerror(status));
×
1482
    return;
×
1483
  }
1484
  int32_t code = 0;
311✔
1485

1486
  uv_pipe_t *client = (uv_pipe_t *)taosMemoryMalloc(sizeof(uv_pipe_t));
311!
1487
  if(client == NULL) {
311!
1488
    fnError("taosudf pipe malloc failed");
×
1489
    return;
×
1490
  }
1491
  code = uv_pipe_init(global.loop, client, 0);
311✔
1492
  if (code) {
311!
1493
    fnError("taosudf pipe init error %s", uv_strerror(code));
×
1494
    taosMemoryFree(client);
×
1495
    return;
×
1496
  }
1497
  if (uv_accept(server, (uv_stream_t *)client) == 0) {
311!
1498
    SUdfdUvConn *ctx = taosMemoryMalloc(sizeof(SUdfdUvConn));
311!
1499
    if(ctx == NULL) {
311!
1500
      fnError("taosudf conn malloc failed");
×
1501
      goto _exit;
×
1502
    }
1503
    ctx->pWorkList = NULL;
311✔
1504
    ctx->client = (uv_stream_t *)client;
311✔
1505
    ctx->inputBuf = 0;
311✔
1506
    ctx->inputLen = 0;
311✔
1507
    ctx->inputCap = 0;
311✔
1508
    client->data = ctx;
311✔
1509
    ctx->client = (uv_stream_t *)client;
311✔
1510
    code = uv_read_start((uv_stream_t *)client, udfdAllocBuffer, udfdPipeRead);
311✔
1511
    if (code) {
311!
1512
      fnError("taosudf read start error %s", uv_strerror(code));
×
1513
      udfdUvHandleError(ctx);
×
1514
      taosMemoryFree(ctx);
×
1515
      taosMemoryFree(client);
×
1516
    }
1517
    return;
311✔
1518
  }
1519
_exit:
×
1520
    uv_close((uv_handle_t *)client, NULL);
×
1521
    taosMemoryFree(client);
×
1522
}
1523

1524
void udfdIntrSignalHandler(uv_signal_t *handle, int signum) {
×
1525
  TAOS_UDF_CHECK_PTR_RVOID(handle);
×
1526
  fnInfo("taosudf signal received: %d\n", signum);
×
1527
  uv_fs_t req;
1528
  int32_t code = uv_fs_unlink(global.loop, &req, global.listenPipeName, NULL);
×
1529
  if(code) {
×
1530
    fnError("remove listening pipe %s failed, reason:%s, lino:%d", global.listenPipeName, uv_strerror(code), __LINE__);
×
1531
  }
1532
  code = uv_signal_stop(handle);
×
1533
  if(code) {
×
1534
    fnError("stop signal handler failed, reason:%s", uv_strerror(code));
×
1535
  }
1536
  uv_stop(global.loop);
×
1537
}
1538

1539
static int32_t udfdParseArgs(int32_t argc, char *argv[]) {
70✔
1540
  for (int32_t i = 1; i < argc; ++i) {
140✔
1541
    if (strcmp(argv[i], "-c") == 0) {
70!
1542
      if (i < argc - 1) {
70!
1543
        if (strlen(argv[++i]) >= PATH_MAX) {
70!
1544
          (void)printf("config file path overflow");
×
1545
          return -1;
×
1546
        }
1547
        tstrncpy(configDir, argv[i], PATH_MAX);
70✔
1548
      } else {
1549
        (void)printf("'-c' requires a parameter, default is %s\n", configDir);
×
1550
        return -1;
×
1551
      }
1552
    } else if (strcmp(argv[i], "-V") == 0) {
×
1553
      global.printVersion = true;
×
1554
    } else {
1555
    }
1556
  }
1557

1558
  return 0;
70✔
1559
}
1560

1561
static void udfdPrintVersion() {
×
1562
  (void)printf("taosudf version: %s compatible_version: %s\n", td_version, td_compatible_version);
×
1563
  (void)printf("git: %s\n", td_gitinfo);
×
1564
  (void)printf("build: %s\n", td_buildinfo);
×
1565
}
×
1566

1567
static int32_t udfdInitLog() {
70✔
1568
  const char *logName = "udfdlog";
70✔
1569
  TAOS_CHECK_RETURN(taosInitLogOutput(&logName));
70!
1570
  return taosCreateLog(logName, 1, configDir, NULL, NULL, NULL, NULL, 0);
70✔
1571
}
1572

1573
void udfdCtrlAllocBufCb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
70✔
1574
  TAOS_UDF_CHECK_PTR_RVOID(buf);
140!
1575
  buf->base = taosMemoryMalloc(suggested_size);
70!
1576
  if (buf->base == NULL) {
70!
1577
    fnError("taosudf ctrl pipe alloc buffer failed");
×
1578
    return;
×
1579
  }
1580
  buf->len = suggested_size;
70✔
1581
}
1582

1583
void udfdCtrlReadCb(uv_stream_t *q, ssize_t nread, const uv_buf_t *buf) {
70✔
1584
  TAOS_UDF_CHECK_PTR_RVOID(q, buf);
210!
1585
  if (nread < 0) {
70!
1586
    fnError("taosudf ctrl pipe read error. %s", uv_err_name(nread));
70!
1587
    taosMemoryFree(buf->base);
70!
1588
    uv_close((uv_handle_t *)q, NULL);
70✔
1589
    uv_stop(global.loop);
70✔
1590
    return;
70✔
1591
  }
1592
  fnError("taosudf ctrl pipe read %zu bytes", nread);
×
1593
  taosMemoryFree(buf->base);
×
1594
}
1595

1596
static void removeListeningPipe() {
140✔
1597
  uv_fs_t req;
1598
  int     err = uv_fs_unlink(global.loop, &req, global.listenPipeName, NULL);
140✔
1599
  uv_fs_req_cleanup(&req);
140✔
1600
  if(err) {
140!
1601
    fnInfo("remove listening pipe %s : %s, lino:%d", global.listenPipeName, uv_strerror(err), __LINE__);
140!
1602
  }
1603
}
140✔
1604

1605
static int32_t udfdUvInit() {
70✔
1606
  TAOS_CHECK_RETURN(uv_loop_init(global.loop));
70!
1607

1608
  if (tsStartUdfd) {  // taosudf is started by taosd, which shall exit when taosd exit
70!
1609
    TAOS_CHECK_RETURN(uv_pipe_init(global.loop, &global.ctrlPipe, 1));
70!
1610
    TAOS_CHECK_RETURN(uv_pipe_open(&global.ctrlPipe, 0));
70!
1611
    TAOS_CHECK_RETURN(uv_read_start((uv_stream_t *)&global.ctrlPipe, udfdCtrlAllocBufCb, udfdCtrlReadCb));
70!
1612
  }
1613
  getUdfdPipeName(global.listenPipeName, sizeof(global.listenPipeName));
70✔
1614

1615
  removeListeningPipe();
70✔
1616

1617
  TAOS_CHECK_RETURN(uv_pipe_init(global.loop, &global.listeningPipe, 0));
70!
1618

1619
  TAOS_CHECK_RETURN(uv_signal_init(global.loop, &global.intrSignal));
70!
1620
  TAOS_CHECK_RETURN(uv_signal_start(&global.intrSignal, udfdIntrSignalHandler, SIGINT));
70!
1621

1622
  int r;
1623
  fnInfo("bind to pipe %s", global.listenPipeName);
70!
1624
  if ((r = uv_pipe_bind(&global.listeningPipe, global.listenPipeName))) {
70!
1625
    fnError("Bind error %s", uv_err_name(r));
×
1626
    removeListeningPipe();
×
1627
    return -2;
×
1628
  }
1629
  if ((r = uv_listen((uv_stream_t *)&global.listeningPipe, 128, udfdOnNewConnection))) {
70!
1630
    fnError("Listen error %s", uv_err_name(r));
×
1631
    removeListeningPipe();
×
1632
    return -3;
×
1633
  }
1634
  return 0;
70✔
1635
}
1636

1637
static void udfdCloseWalkCb(uv_handle_t *handle, void *arg) {
140✔
1638
  if (!uv_is_closing(handle)) {
140!
1639
    uv_close(handle, NULL);
140✔
1640
  }
1641
}
140✔
1642

1643
static int32_t udfdGlobalDataInit() {
70✔
1644
  uv_loop_t *loop = taosMemoryMalloc(sizeof(uv_loop_t));
70!
1645
  if (loop == NULL) {
70!
1646
    fnError("taosudf init uv loop failed, mem overflow");
×
1647
    return terrno;
×
1648
  }
1649
  global.loop = loop;
70✔
1650

1651
  if (uv_mutex_init(&global.scriptPluginsMutex) != 0) {
70!
1652
    fnError("taosudf init script plugins mutex failed");
×
1653
    return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1654
  }
1655

1656
  global.udfsHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
70✔
1657
  if (global.udfsHash == NULL) {
70!
1658
    return terrno;
×
1659
  }
1660
  // taosHashSetFreeFp(global.udfsHash, udfdFreeUdf);
1661

1662
  if (uv_mutex_init(&global.udfsMutex) != 0) {
70!
1663
    fnError("taosudf init udfs mutex failed");
×
1664
    return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1665
  }
1666

1667
  return 0;
70✔
1668
}
1669

1670
static void udfdGlobalDataDeinit() {
70✔
1671
  uv_mutex_destroy(&global.udfsMutex);
70✔
1672
  uv_mutex_destroy(&global.scriptPluginsMutex);
70✔
1673
  taosMemoryFreeClear(global.loop);
70!
1674
  fnInfo("taosudf global data deinit");
70!
1675
}
70✔
1676

1677
static void udfdRun() {
70✔
1678
  fnInfo("start taosudf event loop");
70!
1679
  int32_t code = uv_run(global.loop, UV_RUN_DEFAULT);
70✔
1680
  if(code != 0) {
70!
1681
    fnError("taosudf event loop still has active handles or requests.");
70!
1682
  }
1683
  fnInfo("taosudf event loop stopped.");
70!
1684

1685
  (void)uv_loop_close(global.loop);
70✔
1686

1687
  uv_walk(global.loop, udfdCloseWalkCb, NULL);
70✔
1688
  code = uv_run(global.loop, UV_RUN_DEFAULT);
70✔
1689
  if(code != 0) {
70!
1690
    fnError("taosudf event loop still has active handles or requests.");
×
1691
  }
1692
  (void)uv_loop_close(global.loop);
70✔
1693
}
70✔
1694

1695
int32_t udfdInitResidentFuncs() {
70✔
1696
  if (strlen(tsUdfdResFuncs) == 0) {
70!
1697
    return TSDB_CODE_SUCCESS;
70✔
1698
  }
1699

1700
  global.residentFuncs = taosArrayInit(2, TSDB_FUNC_NAME_LEN);
×
1701
  char *pSave = tsUdfdResFuncs;
×
1702
  char *token;
1703
  while ((token = strtok_r(pSave, ",", &pSave)) != NULL) {
×
1704
    char func[TSDB_FUNC_NAME_LEN + 1] = {0};
×
1705
    tstrncpy(func, token, TSDB_FUNC_NAME_LEN);
×
1706
    fnInfo("taosudf add resident function %s", func);
×
1707
    if(taosArrayPush(global.residentFuncs, func) == NULL)
×
1708
    {
1709
      taosArrayDestroy(global.residentFuncs);
×
1710
      return terrno;
×
1711
    }
1712
  }
1713

1714
  return TSDB_CODE_SUCCESS;
×
1715
}
1716

1717
void udfdDeinitResidentFuncs() {
70✔
1718
  for (int32_t i = 0; i < taosArrayGetSize(global.residentFuncs); ++i) {
70!
1719
    char  *funcName = taosArrayGet(global.residentFuncs, i);
×
1720
    SUdf **udfInHash = taosHashGet(global.udfsHash, funcName, strlen(funcName));
×
1721
    if (udfInHash) {
×
1722
      SUdf   *udf = *udfInHash;
×
1723
      int32_t code = 0;
×
1724
      if (udf->scriptPlugin->udfDestroyFunc) {
×
1725
        code = udf->scriptPlugin->udfDestroyFunc(udf->scriptUdfCtx);
×
1726
        fnDebug("taosudf %s destroy function returns %d", funcName, code);
×
1727
      }
1728
      if(taosHashRemove(global.udfsHash, funcName, strlen(funcName)) != 0)
×
1729
      {
1730
        fnError("taosudf remove resident function %s failed", funcName);
×
1731
      }
1732
      taosMemoryFree(udf);
×
1733
    }
1734
  }
1735
  taosHashCleanup(global.udfsHash);
70✔
1736
  taosArrayDestroy(global.residentFuncs);
70✔
1737
  fnInfo("taosudf resident functions are deinit");
70!
1738
}
70✔
1739

1740
int32_t udfdCreateUdfSourceDir() {
70✔
1741
  snprintf(global.udfDataDir, PATH_MAX, "%s/.udf", tsDataDir);
70✔
1742
  int32_t code = taosMkDir(global.udfDataDir);
70✔
1743
  if (code != TSDB_CODE_SUCCESS) {
70!
1744
    snprintf(global.udfDataDir, PATH_MAX, "%s/.udf", tsTempDir);
×
1745
    code = taosMkDir(global.udfDataDir);
×
1746
  }
1747
  fnInfo("taosudf create udf source directory %s. result: %s", global.udfDataDir, tstrerror(code));
70!
1748

1749
  return code;
70✔
1750
}
1751

1752
void udfdDestroyUdfSourceDir() {
70✔
1753
  fnInfo("destory udf source directory %s", global.udfDataDir);
70!
1754
  taosRemoveDir(global.udfDataDir);
70✔
1755
}
70✔
1756

1757
int main(int argc, char *argv[]) {
70✔
1758
  int  code = 0;
70✔
1759
  bool logInitialized = false;
70✔
1760
  bool cfgInitialized = false;
70✔
1761
  bool openClientRpcFinished = false;
70✔
1762
  bool residentFuncsInited = false;
70✔
1763
  bool udfSourceDirInited = false;
70✔
1764
  bool globalDataInited = false;
70✔
1765

1766
  if (!taosCheckSystemIsLittleEnd()) {
70!
1767
    (void)printf("failed to start since on non-little-end machines\n");
×
1768
    return -1;
×
1769
  }
1770

1771
  if (udfdParseArgs(argc, argv) != 0) {
70!
1772
    (void)printf("failed to start since parse args error\n");
×
1773
    return -1;
×
1774
  }
1775

1776
  if (global.printVersion) {
70!
1777
    udfdPrintVersion();
×
1778
    return 0;
×
1779
  }
1780

1781
  if (udfdInitLog() != 0) {
70!
1782
    // ignore create log failed, because this error no matter
1783
    (void)printf("failed to init taosudf log.");
×
1784
  } else {
1785
    logInitialized = true;  // log is initialized
70✔
1786
  }
1787

1788
  if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0) != 0) {
70!
1789
    fnError("failed to start since read config error");
×
1790
    code = -2;
×
1791
    goto _exit;
×
1792
  }
1793
  cfgInitialized = true;  // cfg is initialized
70✔
1794
  fnInfo("taosudf start with config file %s", configDir);
70!
1795

1796
  if (initEpSetFromCfg(tsFirst, tsSecond, &global.mgmtEp) != 0) {
70!
1797
    fnError("init ep set from cfg failed");
×
1798
    code = -3;
×
1799
    goto _exit;
×
1800
  }
1801
  fnInfo("taosudf start with mnode ep %s", global.mgmtEp.epSet.eps[0].fqdn);
70!
1802
  if (udfdOpenClientRpc() != 0) {
70!
1803
    fnError("open rpc connection to mnode failed");
×
1804
    code = -4;
×
1805
    goto _exit;
×
1806
  }
1807
  fnInfo("taosudf rpc client is opened");
70!
1808
  openClientRpcFinished = true;  // rpc is opened
70✔
1809

1810
  if (udfdCreateUdfSourceDir() != 0) {
70!
1811
    fnError("create udf source directory failed");
×
1812
    code = -5;
×
1813
    goto _exit;
×
1814
  }
1815
  udfSourceDirInited = true;  // udf source dir is created
70✔
1816
  fnInfo("taosudf udf source directory is created");
70!
1817

1818
  if (udfdGlobalDataInit() != 0) {
70!
1819
    fnError("init global data failed");
×
1820
    code = -6;
×
1821
    goto _exit;
×
1822
  }
1823
  globalDataInited = true;  // global data is inited
70✔
1824
  fnInfo("taosudf global data is inited");
70!
1825

1826
  if (udfdUvInit() != 0) {
70!
1827
    fnError("uv init failure");
×
1828
    code = -7;
×
1829
    goto _exit;
×
1830
  }
1831
  fnInfo("taosudf uv is inited");
70!
1832

1833
  if (udfdInitResidentFuncs() != 0) {
70!
1834
    fnError("init resident functions failed");
×
1835
    code = -8;
×
1836
    goto _exit;
×
1837
  }
1838
  residentFuncsInited = true;  // resident functions are inited
70✔
1839
  fnInfo("taosudf resident functions are inited");
70!
1840

1841
  udfdRun();
70✔
1842
  fnInfo("taosudf exit normally");
70!
1843

1844
  removeListeningPipe();
70✔
1845

1846
_exit:
70✔
1847
  if (residentFuncsInited) {
70!
1848
    udfdDeinitResidentFuncs();
70✔
1849
  }
1850
  udfdDeinitScriptPlugins();
70✔
1851
  if (globalDataInited) {
70!
1852
    udfdGlobalDataDeinit();
70✔
1853
  }
1854
  if (udfSourceDirInited) {
70!
1855
    udfdDestroyUdfSourceDir();
70✔
1856
  }
1857
  if (openClientRpcFinished) {
70!
1858
    udfdCloseClientRpc();
70✔
1859
  }
1860
  if (cfgInitialized) {
70!
1861
    taosCleanupCfg();
70✔
1862
  }
1863
  if (logInitialized) {
70!
1864
    taosCloseLog();
70✔
1865
  }
1866

1867
  return code;
70✔
1868
}
1869
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc