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

taosdata / TDengine / #3633

11 Mar 2025 12:59PM UTC coverage: 0.0% (-60.7%) from 60.719%
#3633

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

0 of 280412 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 275582 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/source/libs/function/src/tudf.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
#include "uv.h"
16

17
#include "os.h"
18

19
#include "builtinsimpl.h"
20
#include "fnLog.h"
21
#include "functionMgt.h"
22
#include "querynodes.h"
23
#include "tarray.h"
24
#include "tdatablock.h"
25
#include "tglobal.h"
26
#include "tudf.h"
27
#include "tudfInt.h"
28

29
#ifdef _TD_DARWIN_64
30
#include <mach-o/dyld.h>
31
#endif
32

33
typedef struct SUdfdData {
34
  bool         startCalled;
35
  bool         needCleanUp;
36
  uv_loop_t    loop;
37
  uv_thread_t  thread;
38
  uv_barrier_t barrier;
39
  uv_process_t process;
40
#ifdef WINDOWS
41
  HANDLE jobHandle;
42
#endif
43
  int32_t    spawnErr;
44
  uv_pipe_t  ctrlPipe;
45
  uv_async_t stopAsync;
46
  int32_t    stopCalled;
47

48
  int32_t dnodeId;
49
} SUdfdData;
50

51
SUdfdData udfdGlobal = {0};
52

53
int32_t udfStartUdfd(int32_t startDnodeId);
54
void    udfStopUdfd();
55

56
extern char **environ;
57

58
static int32_t udfSpawnUdfd(SUdfdData *pData);
59
void           udfUdfdExit(uv_process_t *process, int64_t exitStatus, int32_t termSignal);
60
static void    udfUdfdCloseWalkCb(uv_handle_t *handle, void *arg);
61
static void    udfUdfdStopAsyncCb(uv_async_t *async);
62
static void    udfWatchUdfd(void *args);
63

64
void udfUdfdExit(uv_process_t *process, int64_t exitStatus, int32_t termSignal) {
×
65
  TAOS_UDF_CHECK_PTR_RVOID(process);
×
66
  fnInfo("taosudf process exited with status %" PRId64 ", signal %d", exitStatus, termSignal);
×
67
  SUdfdData *pData = process->data;
×
68
  if(pData == NULL) {
×
69
    fnError("taosudf process data is NULL");
×
70
    return;
×
71
  }
72
  if (exitStatus == 0 && termSignal == 0 || atomic_load_32(&pData->stopCalled)) {
×
73
    fnInfo("taosudf process exit due to SIGINT or dnode-mgmt called stop");
×
74
  } else {
75
    fnInfo("taosudf process restart");
×
76
    int32_t code = udfSpawnUdfd(pData);
×
77
    if (code != 0) {
×
78
      fnError("taosudf process restart failed with code:%d", code);
×
79
    }
80
  }
81
}
82

83
static int32_t udfSpawnUdfd(SUdfdData *pData) {
×
84
  fnInfo("start to init taosudf");
×
85
  TAOS_UDF_CHECK_PTR_RCODE(pData);
×
86

87
  int32_t              err = 0;
×
88
  uv_process_options_t options = {0};
×
89

90
  char path[PATH_MAX] = {0};
×
91
  if (tsProcPath == NULL) {
×
92
    path[0] = '.';
×
93
#ifdef WINDOWS
94
    GetModuleFileName(NULL, path, PATH_MAX);
95
    TAOS_DIRNAME(path);
96
#elif defined(_TD_DARWIN_64)
97
    uint32_t pathSize = sizeof(path);
98
    _NSGetExecutablePath(path, &pathSize);
99
    TAOS_DIRNAME(path);
100
#endif
101
  } else {
102
    TAOS_STRNCPY(path, tsProcPath, PATH_MAX);
×
103
    TAOS_DIRNAME(path);
×
104
  }
105
#ifdef WINDOWS
106
  if (strlen(path) == 0) {
107
    TAOS_STRCAT(path, "C:\\TDengine");
108
  }
109
  TAOS_STRCAT(path, "\\taosudf.exe");
110
#else
111
  if (strlen(path) == 0) {
×
112
    TAOS_STRCAT(path, "/usr/bin");
×
113
  }
114
  TAOS_STRCAT(path, "/taosudf");
×
115
#endif
116
  char *argsUdfd[] = {path, "-c", configDir, NULL};
×
117
  options.args = argsUdfd;
×
118
  options.file = path;
×
119

120
  options.exit_cb = udfUdfdExit;
×
121

122
  TAOS_UV_LIB_ERROR_RET(uv_pipe_init(&pData->loop, &pData->ctrlPipe, 1));
×
123

124
  uv_stdio_container_t child_stdio[3];
125
  child_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
×
126
  child_stdio[0].data.stream = (uv_stream_t *)&pData->ctrlPipe;
×
127
  child_stdio[1].flags = UV_IGNORE;
×
128
  child_stdio[2].flags = UV_INHERIT_FD;
×
129
  child_stdio[2].data.fd = 2;
×
130
  options.stdio_count = 3;
×
131
  options.stdio = child_stdio;
×
132

133
  options.flags = UV_PROCESS_DETACHED;
×
134

135
  char dnodeIdEnvItem[32] = {0};
×
136
  char thrdPoolSizeEnvItem[32] = {0};
×
137
  snprintf(dnodeIdEnvItem, 32, "%s=%d", "DNODE_ID", pData->dnodeId);
×
138

139
  float   numCpuCores = 4;
×
140
  int32_t code = taosGetCpuCores(&numCpuCores, false);
×
141
  if (code != 0) {
×
142
    fnError("failed to get cpu cores, code:0x%x", code);
×
143
  }
144
  numCpuCores = TMAX(numCpuCores, 2);
×
145
  snprintf(thrdPoolSizeEnvItem, 32, "%s=%d", "UV_THREADPOOL_SIZE", (int32_t)numCpuCores * 2);
×
146

147
  char    pathTaosdLdLib[512] = {0};
×
148
  size_t  taosdLdLibPathLen = sizeof(pathTaosdLdLib);
×
149
  int32_t ret = uv_os_getenv("LD_LIBRARY_PATH", pathTaosdLdLib, &taosdLdLibPathLen);
×
150
  if (ret != UV_ENOBUFS) {
×
151
    taosdLdLibPathLen = strlen(pathTaosdLdLib);
×
152
  }
153

154
  char   udfdPathLdLib[1024] = {0};
×
155
  size_t udfdLdLibPathLen = strlen(tsUdfdLdLibPath);
×
156
  tstrncpy(udfdPathLdLib, tsUdfdLdLibPath, sizeof(udfdPathLdLib));
×
157

158
  udfdPathLdLib[udfdLdLibPathLen] = ':';
×
159
  tstrncpy(udfdPathLdLib + udfdLdLibPathLen + 1, pathTaosdLdLib, sizeof(udfdPathLdLib) - udfdLdLibPathLen - 1);
×
160
  if (udfdLdLibPathLen + taosdLdLibPathLen < 1024) {
×
161
    fnInfo("[UDFD]taosudf LD_LIBRARY_PATH: %s", udfdPathLdLib);
×
162
  } else {
163
    fnError("[UDFD]can not set correct taosudf LD_LIBRARY_PATH");
×
164
  }
165
  char ldLibPathEnvItem[1024 + 32] = {0};
×
166
  snprintf(ldLibPathEnvItem, 1024 + 32, "%s=%s", "LD_LIBRARY_PATH", udfdPathLdLib);
×
167

168
  char *taosFqdnEnvItem = NULL;
×
169
  char *taosFqdn = getenv("TAOS_FQDN");
×
170
  if (taosFqdn != NULL) {
×
171
    int32_t subLen = strlen(taosFqdn);
×
172
    int32_t len = strlen("TAOS_FQDN=") + subLen + 1;
×
173
    taosFqdnEnvItem = taosMemoryMalloc(len);
×
174
    if (taosFqdnEnvItem != NULL) {
×
175
      tstrncpy(taosFqdnEnvItem, "TAOS_FQDN=", len);
×
176
      TAOS_STRNCAT(taosFqdnEnvItem, taosFqdn, subLen);
×
177
      fnInfo("[UDFD]Succsess to set TAOS_FQDN:%s", taosFqdn);
×
178
    } else {
179
      fnError("[UDFD]Failed to allocate memory for TAOS_FQDN");
×
180
      return terrno;
×
181
    }
182
  }
183

184
  char *envUdfd[] = {dnodeIdEnvItem, thrdPoolSizeEnvItem, ldLibPathEnvItem, taosFqdnEnvItem, NULL};
×
185

186
  char **envUdfdWithPEnv = NULL;
×
187
  if (environ != NULL) {
×
188
    int32_t lenEnvUdfd = ARRAY_SIZE(envUdfd);
×
189
    int32_t numEnviron = 0;
×
190
    while (environ[numEnviron] != NULL) {
×
191
      numEnviron++;
×
192
    }
193

194
    envUdfdWithPEnv = (char **)taosMemoryCalloc(numEnviron + lenEnvUdfd, sizeof(char *));
×
195
    if (envUdfdWithPEnv == NULL) {
×
196
      err = TSDB_CODE_OUT_OF_MEMORY;
×
197
      goto _OVER;
×
198
    }
199

200
    for (int32_t i = 0; i < numEnviron; i++) {
×
201
      int32_t len = strlen(environ[i]) + 1;
×
202
      envUdfdWithPEnv[i] = (char *)taosMemoryCalloc(len, 1);
×
203
      if (envUdfdWithPEnv[i] == NULL) {
×
204
        err = TSDB_CODE_OUT_OF_MEMORY;
×
205
        goto _OVER;
×
206
      }
207

208
      tstrncpy(envUdfdWithPEnv[i], environ[i], len);
×
209
    }
210

211
    for (int32_t i = 0; i < lenEnvUdfd; i++) {
×
212
      if (envUdfd[i] != NULL) {
×
213
        int32_t len = strlen(envUdfd[i]) + 1;
×
214
        envUdfdWithPEnv[numEnviron + i] = (char *)taosMemoryCalloc(len, 1);
×
215
        if (envUdfdWithPEnv[numEnviron + i] == NULL) {
×
216
          err = TSDB_CODE_OUT_OF_MEMORY;
×
217
          goto _OVER;
×
218
        }
219

220
        tstrncpy(envUdfdWithPEnv[numEnviron + i], envUdfd[i], len);
×
221
      }
222
    }
223
    envUdfdWithPEnv[numEnviron + lenEnvUdfd - 1] = NULL;
×
224

225
    options.env = envUdfdWithPEnv;
×
226
  } else {
227
    options.env = envUdfd;
×
228
  }
229

230
  err = uv_spawn(&pData->loop, &pData->process, &options);
×
231
  pData->process.data = (void *)pData;
×
232

233
#ifdef WINDOWS
234
  // End taosudf.exe by Job.
235
  if (pData->jobHandle != NULL) CloseHandle(pData->jobHandle);
236
  pData->jobHandle = CreateJobObject(NULL, NULL);
237
  bool add_job_ok = AssignProcessToJobObject(pData->jobHandle, pData->process.process_handle);
238
  if (!add_job_ok) {
239
    fnError("Assign taosudf to job failed.");
240
  } else {
241
    JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info;
242
    memset(&limit_info, 0x0, sizeof(limit_info));
243
    limit_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
244
    bool set_auto_kill_ok =
245
        SetInformationJobObject(pData->jobHandle, JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info));
246
    if (!set_auto_kill_ok) {
247
      fnError("Set job auto kill taosudf failed.");
248
    }
249
  }
250
#endif
251

252
  if (err != 0) {
×
253
    fnError("can not spawn taosudf. path: %s, error: %s", path, uv_strerror(err));
×
254
  } else {
255
    fnInfo("taosudf is initialized");
×
256
  }
257

258
_OVER:
×
259
  if (taosFqdnEnvItem) {
×
260
    taosMemoryFree(taosFqdnEnvItem);
×
261
  }
262

263
  if (envUdfdWithPEnv != NULL) {
×
264
    int32_t i = 0;
×
265
    while (envUdfdWithPEnv[i] != NULL) {
×
266
      taosMemoryFree(envUdfdWithPEnv[i]);
×
267
      i++;
×
268
    }
269
    taosMemoryFree(envUdfdWithPEnv);
×
270
  }
271

272
  return err;
×
273
}
274

275
static void udfUdfdCloseWalkCb(uv_handle_t *handle, void *arg) {
×
276
  TAOS_UDF_CHECK_PTR_RVOID(handle);
×
277
  if (!uv_is_closing(handle)) {
×
278
    uv_close(handle, NULL);
×
279
  }
280
}
281

282
static void udfUdfdStopAsyncCb(uv_async_t *async) {
×
283
  TAOS_UDF_CHECK_PTR_RVOID(async);
×
284
  SUdfdData *pData = async->data;
×
285
  uv_stop(&pData->loop);
×
286
}
287

288
static void udfWatchUdfd(void *args) {
×
289
  TAOS_UDF_CHECK_PTR_RVOID(args);
×
290
  SUdfdData *pData = args;
×
291
  TAOS_UV_CHECK_ERRNO(uv_loop_init(&pData->loop));
×
292
  TAOS_UV_CHECK_ERRNO(uv_async_init(&pData->loop, &pData->stopAsync, udfUdfdStopAsyncCb));
×
293
  pData->stopAsync.data = pData;
×
294
  TAOS_UV_CHECK_ERRNO(udfSpawnUdfd(pData));
×
295
  atomic_store_32(&pData->spawnErr, 0);
×
296
  (void)uv_barrier_wait(&pData->barrier);
×
297
  int32_t num = uv_run(&pData->loop, UV_RUN_DEFAULT);
×
298
  fnInfo("taosudf loop exit with %d active handles, line:%d", num, __LINE__);
×
299

300
  uv_walk(&pData->loop, udfUdfdCloseWalkCb, NULL);
×
301
  num = uv_run(&pData->loop, UV_RUN_DEFAULT);
×
302
  fnInfo("taosudf loop exit with %d active handles, line:%d", num, __LINE__);
×
303
  if (uv_loop_close(&pData->loop) != 0) {
×
304
    fnError("taosudf loop close failed, lino:%d", __LINE__);
×
305
  }
306
  return;
×
307

308
_exit:
×
309
  if (terrno != 0) {
×
310
    (void)uv_barrier_wait(&pData->barrier);
×
311
    atomic_store_32(&pData->spawnErr, terrno);
×
312
    if (uv_loop_close(&pData->loop) != 0) {
×
313
      fnError("taosudf loop close failed, lino:%d", __LINE__);
×
314
    }
315
    fnError("taosudf thread exit with code:%d lino:%d", terrno, terrln);
×
316
    terrno = TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
317
  }
318
  return;
×
319
}
320

321
int32_t udfStartUdfd(int32_t startDnodeId) {
×
322
  int32_t code = 0, lino = 0;
×
323
  if (!tsStartUdfd) {
×
324
    fnInfo("start taosudf is disabled.") return 0;
×
325
  }
326
  SUdfdData *pData = &udfdGlobal;
×
327
  if (pData->startCalled) {
×
328
    fnInfo("dnode start taosudf already called");
×
329
    return 0;
×
330
  }
331
  pData->startCalled = true;
×
332
  char dnodeId[8] = {0};
×
333
  snprintf(dnodeId, sizeof(dnodeId), "%d", startDnodeId);
×
334
  TAOS_CHECK_GOTO(uv_os_setenv("DNODE_ID", dnodeId), &lino, _exit);
×
335
  pData->dnodeId = startDnodeId;
×
336

337
  TAOS_CHECK_GOTO(uv_barrier_init(&pData->barrier, 2), &lino, _exit);
×
338
  TAOS_CHECK_GOTO(uv_thread_create(&pData->thread, udfWatchUdfd, pData), &lino, _exit);
×
339
  (void)uv_barrier_wait(&pData->barrier);
×
340
  int32_t err = atomic_load_32(&pData->spawnErr);
×
341
  if (err != 0) {
×
342
    uv_barrier_destroy(&pData->barrier);
×
343
    if (uv_async_send(&pData->stopAsync) != 0) {
×
344
      fnError("start taosudf: failed to send stop async");
×
345
    }
346
    if (uv_thread_join(&pData->thread) != 0) {
×
347
      fnError("start taosudf: failed to join taosudf thread");
×
348
    }
349
    pData->needCleanUp = false;
×
350
    fnInfo("taosudf is cleaned up after spawn err");
×
351
    TAOS_CHECK_GOTO(err, &lino, _exit);
×
352
  } else {
353
    pData->needCleanUp = true;
×
354
  }
355
_exit:
×
356
  if (code != 0) {
×
357
    fnError("taosudf start failed with code:%d, lino:%d", code, lino);
×
358
  }
359
  return code;
×
360
}
361

362
void udfStopUdfd() {
×
363
  SUdfdData *pData = &udfdGlobal;
×
364
  fnInfo("taosudf start to stop, need cleanup:%d, spawn err:%d", pData->needCleanUp, pData->spawnErr);
×
365
  if (!pData->needCleanUp || atomic_load_32(&pData->stopCalled)) {
×
366
    return;
×
367
  }
368
  atomic_store_32(&pData->stopCalled, 1);
×
369
  pData->needCleanUp = false;
×
370
  uv_barrier_destroy(&pData->barrier);
×
371
  if (uv_async_send(&pData->stopAsync) != 0) {
×
372
    fnError("stop taosudf: failed to send stop async");
×
373
  }
374
  if (uv_thread_join(&pData->thread) != 0) {
×
375
    fnError("stop taosudf: failed to join taosudf thread");
×
376
  }
377

378
#ifdef WINDOWS
379
  if (pData->jobHandle != NULL) CloseHandle(pData->jobHandle);
380
#endif
381
  fnInfo("taosudf is cleaned up");
×
382
  return;
×
383
}
384

385
/*
386
int32_t udfGetUdfdPid(int32_t* pUdfdPid) {
387
  SUdfdData *pData = &udfdGlobal;
388
  if (pData->spawnErr) {
389
    return pData->spawnErr;
390
  }
391
  uv_pid_t pid = uv_process_get_pid(&pData->process);
392
  if (pUdfdPid) {
393
    *pUdfdPid = (int32_t)pid;
394
  }
395
  return TSDB_CODE_SUCCESS;
396
}
397
*/
398

399
//==============================================================================================
400
/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
401
 * The QUEUE is copied from queue.h under libuv
402
 * */
403

404
typedef void *QUEUE[2];
405

406
/* Private macros. */
407
#define QUEUE_NEXT(q)      (*(QUEUE **)&((*(q))[0]))
408
#define QUEUE_PREV(q)      (*(QUEUE **)&((*(q))[1]))
409
#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
410
#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))
411

412
/* Public macros. */
413
#define QUEUE_DATA(ptr, type, field) ((type *)((char *)(ptr) - offsetof(type, field)))
414

415
/* Important note: mutating the list while QUEUE_FOREACH is
416
 * iterating over its elements results in undefined behavior.
417
 */
418
#define QUEUE_FOREACH(q, h) for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q))
419

420
#define QUEUE_EMPTY(q) ((const QUEUE *)(q) == (const QUEUE *)QUEUE_NEXT(q))
421

422
#define QUEUE_HEAD(q) (QUEUE_NEXT(q))
423

424
#define QUEUE_INIT(q)    \
425
  do {                   \
426
    QUEUE_NEXT(q) = (q); \
427
    QUEUE_PREV(q) = (q); \
428
  } while (0)
429

430
#define QUEUE_ADD(h, n)                 \
431
  do {                                  \
432
    QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \
433
    QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \
434
    QUEUE_PREV(h) = QUEUE_PREV(n);      \
435
    QUEUE_PREV_NEXT(h) = (h);           \
436
  } while (0)
437

438
#define QUEUE_SPLIT(h, q, n)       \
439
  do {                             \
440
    QUEUE_PREV(n) = QUEUE_PREV(h); \
441
    QUEUE_PREV_NEXT(n) = (n);      \
442
    QUEUE_NEXT(n) = (q);           \
443
    QUEUE_PREV(h) = QUEUE_PREV(q); \
444
    QUEUE_PREV_NEXT(h) = (h);      \
445
    QUEUE_PREV(q) = (n);           \
446
  } while (0)
447

448
#define QUEUE_MOVE(h, n)        \
449
  do {                          \
450
    if (QUEUE_EMPTY(h))         \
451
      QUEUE_INIT(n);            \
452
    else {                      \
453
      QUEUE *q = QUEUE_HEAD(h); \
454
      QUEUE_SPLIT(h, q, n);     \
455
    }                           \
456
  } while (0)
457

458
#define QUEUE_INSERT_HEAD(h, q)    \
459
  do {                             \
460
    QUEUE_NEXT(q) = QUEUE_NEXT(h); \
461
    QUEUE_PREV(q) = (h);           \
462
    QUEUE_NEXT_PREV(q) = (q);      \
463
    QUEUE_NEXT(h) = (q);           \
464
  } while (0)
465

466
#define QUEUE_INSERT_TAIL(h, q)    \
467
  do {                             \
468
    QUEUE_NEXT(q) = (h);           \
469
    QUEUE_PREV(q) = QUEUE_PREV(h); \
470
    QUEUE_PREV_NEXT(q) = (q);      \
471
    QUEUE_PREV(h) = (q);           \
472
  } while (0)
473

474
#define QUEUE_REMOVE(q)                 \
475
  do {                                  \
476
    QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \
477
    QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \
478
  } while (0)
479

480
enum { UV_TASK_CONNECT = 0, UV_TASK_REQ_RSP = 1, UV_TASK_DISCONNECT = 2 };
481

482
int64_t gUdfTaskSeqNum = 0;
483
typedef struct SUdfcFuncStub {
484
  char           udfName[TSDB_FUNC_NAME_LEN + 1];
485
  UdfcFuncHandle handle;
486
  int32_t        refCount;
487
  int64_t        createTime;
488
} SUdfcFuncStub;
489

490
typedef struct SUdfcProxy {
491
  char         udfdPipeName[PATH_MAX + UDF_LISTEN_PIPE_NAME_LEN + 2];
492
  uv_barrier_t initBarrier;
493

494
  uv_loop_t   uvLoop;
495
  uv_thread_t loopThread;
496
  uv_async_t  loopTaskAync;
497

498
  uv_async_t loopStopAsync;
499

500
  uv_mutex_t taskQueueMutex;
501
  int8_t     udfcState;
502
  QUEUE      taskQueue;
503
  QUEUE      uvProcTaskQueue;
504

505
  uv_mutex_t udfStubsMutex;
506
  SArray    *udfStubs;         // SUdfcFuncStub
507
  SArray    *expiredUdfStubs;  // SUdfcFuncStub
508

509
  uv_mutex_t udfcUvMutex;
510
  int8_t     initialized;
511
} SUdfcProxy;
512

513
SUdfcProxy gUdfcProxy = {0};
514

515
typedef struct SUdfcUvSession {
516
  SUdfcProxy *udfc;
517
  int64_t     severHandle;
518
  uv_pipe_t  *udfUvPipe;
519

520
  int8_t  outputType;
521
  int32_t bytes;
522
  int32_t bufSize;
523

524
  char udfName[TSDB_FUNC_NAME_LEN + 1];
525
} SUdfcUvSession;
526

527
typedef struct SClientUvTaskNode {
528
  SUdfcProxy *udfc;
529
  int8_t      type;
530
  int32_t     errCode;
531

532
  uv_pipe_t *pipe;
533

534
  int64_t  seqNum;
535
  uv_buf_t reqBuf;
536

537
  uv_sem_t taskSem;
538
  uv_buf_t rspBuf;
539

540
  QUEUE recvTaskQueue;
541
  QUEUE procTaskQueue;
542
  QUEUE connTaskQueue;
543
} SClientUvTaskNode;
544

545
typedef struct SClientUdfTask {
546
  int8_t type;
547

548
  SUdfcUvSession *session;
549

550
  union {
551
    struct {
552
      SUdfSetupRequest  req;
553
      SUdfSetupResponse rsp;
554
    } _setup;
555
    struct {
556
      SUdfCallRequest  req;
557
      SUdfCallResponse rsp;
558
    } _call;
559
    struct {
560
      SUdfTeardownRequest  req;
561
      SUdfTeardownResponse rsp;
562
    } _teardown;
563
  };
564

565
} SClientUdfTask;
566

567
typedef struct SClientConnBuf {
568
  char   *buf;
569
  int32_t len;
570
  int32_t cap;
571
  int32_t total;
572
} SClientConnBuf;
573

574
typedef struct SClientUvConn {
575
  uv_pipe_t      *pipe;
576
  QUEUE           taskQueue;
577
  SClientConnBuf  readBuf;
578
  SUdfcUvSession *session;
579
} SClientUvConn;
580

581
enum {
582
  UDFC_STATE_INITAL = 0,  // initial state
583
  UDFC_STATE_STARTNG,     // starting after udfcOpen
584
  UDFC_STATE_READY,       // started and begin to receive quests
585
  UDFC_STATE_STOPPING,    // stopping after udfcClose
586
};
587

588
void    getUdfdPipeName(char *pipeName, int32_t size);
589
int32_t encodeUdfSetupRequest(void **buf, const SUdfSetupRequest *setup);
590
void   *decodeUdfSetupRequest(const void *buf, SUdfSetupRequest *request);
591
int32_t encodeUdfInterBuf(void **buf, const SUdfInterBuf *state);
592
void   *decodeUdfInterBuf(const void *buf, SUdfInterBuf *state);
593
int32_t encodeUdfCallRequest(void **buf, const SUdfCallRequest *call);
594
void   *decodeUdfCallRequest(const void *buf, SUdfCallRequest *call);
595
int32_t encodeUdfTeardownRequest(void **buf, const SUdfTeardownRequest *teardown);
596
void   *decodeUdfTeardownRequest(const void *buf, SUdfTeardownRequest *teardown);
597
int32_t encodeUdfRequest(void **buf, const SUdfRequest *request);
598
void   *decodeUdfRequest(const void *buf, SUdfRequest *request);
599
int32_t encodeUdfSetupResponse(void **buf, const SUdfSetupResponse *setupRsp);
600
void   *decodeUdfSetupResponse(const void *buf, SUdfSetupResponse *setupRsp);
601
int32_t encodeUdfCallResponse(void **buf, const SUdfCallResponse *callRsp);
602
void   *decodeUdfCallResponse(const void *buf, SUdfCallResponse *callRsp);
603
int32_t encodeUdfTeardownResponse(void **buf, const SUdfTeardownResponse *teardownRsp);
604
void   *decodeUdfTeardownResponse(const void *buf, SUdfTeardownResponse *teardownResponse);
605
int32_t encodeUdfResponse(void **buf, const SUdfResponse *rsp);
606
void   *decodeUdfResponse(const void *buf, SUdfResponse *rsp);
607
void    freeUdfColumnData(SUdfColumnData *data, SUdfColumnMeta *meta);
608
void    freeUdfColumn(SUdfColumn *col);
609
void    freeUdfDataDataBlock(SUdfDataBlock *block);
610
void    freeUdfInterBuf(SUdfInterBuf *buf);
611
int32_t convertDataBlockToUdfDataBlock(SSDataBlock *block, SUdfDataBlock *udfBlock);
612
int32_t convertUdfColumnToDataBlock(SUdfColumn *udfCol, SSDataBlock *block);
613
int32_t convertScalarParamToDataBlock(SScalarParam *input, int32_t numOfCols, SSDataBlock *output);
614
int32_t convertDataBlockToScalarParm(SSDataBlock *input, SScalarParam *output);
615

616
void getUdfdPipeName(char *pipeName, int32_t size) {
×
617
  char    dnodeId[8] = {0};
×
618
  size_t  dnodeIdSize = sizeof(dnodeId);
×
619
  int32_t err = uv_os_getenv(UDF_DNODE_ID_ENV_NAME, dnodeId, &dnodeIdSize);
×
620
  if (err != 0) {
×
621
    fnError("failed to get dnodeId from env since %s", uv_err_name(err));
×
622
    dnodeId[0] = '1';
×
623
  }
624
#ifdef _WIN32
625
  snprintf(pipeName, size, "%s.%x.%s", UDF_LISTEN_PIPE_NAME_PREFIX, MurmurHash3_32(tsDataDir, strlen(tsDataDir)),
626
           dnodeId);
627
#else
628
  snprintf(pipeName, size, "%s/%s%s", tsDataDir, UDF_LISTEN_PIPE_NAME_PREFIX, dnodeId);
×
629
#endif
630
  fnInfo("get dnodeId:%s from env, pipe path:%s", dnodeId, pipeName);
×
631
}
×
632

633
int32_t encodeUdfSetupRequest(void **buf, const SUdfSetupRequest *setup) {
×
634
  int32_t len = 0;
×
635
  len += taosEncodeBinary(buf, setup->udfName, TSDB_FUNC_NAME_LEN);
×
636
  return len;
×
637
}
638

639
void *decodeUdfSetupRequest(const void *buf, SUdfSetupRequest *request) {
×
640
  buf = taosDecodeBinaryTo(buf, request->udfName, TSDB_FUNC_NAME_LEN);
×
641
  return (void *)buf;
×
642
}
643

644
int32_t encodeUdfInterBuf(void **buf, const SUdfInterBuf *state) {
×
645
  int32_t len = 0;
×
646
  len += taosEncodeFixedI8(buf, state->numOfResult);
×
647
  len += taosEncodeFixedI32(buf, state->bufLen);
×
648
  len += taosEncodeBinary(buf, state->buf, state->bufLen);
×
649
  return len;
×
650
}
651

652
void *decodeUdfInterBuf(const void *buf, SUdfInterBuf *state) {
×
653
  buf = taosDecodeFixedI8(buf, &state->numOfResult);
×
654
  buf = taosDecodeFixedI32(buf, &state->bufLen);
×
655
  buf = taosDecodeBinary(buf, (void **)&state->buf, state->bufLen);
×
656
  return (void *)buf;
×
657
}
658

659
int32_t encodeUdfCallRequest(void **buf, const SUdfCallRequest *call) {
×
660
  int32_t len = 0;
×
661
  len += taosEncodeFixedI64(buf, call->udfHandle);
×
662
  len += taosEncodeFixedI8(buf, call->callType);
×
663
  if (call->callType == TSDB_UDF_CALL_SCALA_PROC) {
×
664
    len += tEncodeDataBlock(buf, &call->block);
×
665
  } else if (call->callType == TSDB_UDF_CALL_AGG_INIT) {
×
666
    len += taosEncodeFixedI8(buf, call->initFirst);
×
667
  } else if (call->callType == TSDB_UDF_CALL_AGG_PROC) {
×
668
    len += tEncodeDataBlock(buf, &call->block);
×
669
    len += encodeUdfInterBuf(buf, &call->interBuf);
×
670
  } else if (call->callType == TSDB_UDF_CALL_AGG_MERGE) {
×
671
    // len += encodeUdfInterBuf(buf, &call->interBuf);
672
    // len += encodeUdfInterBuf(buf, &call->interBuf2);
673
  } else if (call->callType == TSDB_UDF_CALL_AGG_FIN) {
×
674
    len += encodeUdfInterBuf(buf, &call->interBuf);
×
675
  }
676
  return len;
×
677
}
678

679
void *decodeUdfCallRequest(const void *buf, SUdfCallRequest *call) {
×
680
  buf = taosDecodeFixedI64(buf, &call->udfHandle);
×
681
  buf = taosDecodeFixedI8(buf, &call->callType);
×
682
  switch (call->callType) {
×
683
    case TSDB_UDF_CALL_SCALA_PROC:
×
684
      buf = tDecodeDataBlock(buf, &call->block);
×
685
      break;
×
686
    case TSDB_UDF_CALL_AGG_INIT:
×
687
      buf = taosDecodeFixedI8(buf, &call->initFirst);
×
688
      break;
×
689
    case TSDB_UDF_CALL_AGG_PROC:
×
690
      buf = tDecodeDataBlock(buf, &call->block);
×
691
      buf = decodeUdfInterBuf(buf, &call->interBuf);
×
692
      break;
×
693
    // case TSDB_UDF_CALL_AGG_MERGE:
694
    //   buf = decodeUdfInterBuf(buf, &call->interBuf);
695
    //   buf = decodeUdfInterBuf(buf, &call->interBuf2);
696
    //   break;
697
    case TSDB_UDF_CALL_AGG_FIN:
×
698
      buf = decodeUdfInterBuf(buf, &call->interBuf);
×
699
      break;
×
700
  }
701
  return (void *)buf;
×
702
}
703

704
int32_t encodeUdfTeardownRequest(void **buf, const SUdfTeardownRequest *teardown) {
×
705
  int32_t len = 0;
×
706
  len += taosEncodeFixedI64(buf, teardown->udfHandle);
×
707
  return len;
×
708
}
709

710
void *decodeUdfTeardownRequest(const void *buf, SUdfTeardownRequest *teardown) {
×
711
  buf = taosDecodeFixedI64(buf, &teardown->udfHandle);
×
712
  return (void *)buf;
×
713
}
714

715
int32_t encodeUdfRequest(void **buf, const SUdfRequest *request) {
×
716
  int32_t len = 0;
×
717
  if (buf == NULL) {
×
718
    len += sizeof(request->msgLen);
×
719
  } else {
720
    *(int32_t *)(*buf) = request->msgLen;
×
721
    *buf = POINTER_SHIFT(*buf, sizeof(request->msgLen));
×
722
  }
723
  len += taosEncodeFixedI64(buf, request->seqNum);
×
724
  len += taosEncodeFixedI8(buf, request->type);
×
725
  if (request->type == UDF_TASK_SETUP) {
×
726
    len += encodeUdfSetupRequest(buf, &request->setup);
×
727
  } else if (request->type == UDF_TASK_CALL) {
×
728
    len += encodeUdfCallRequest(buf, &request->call);
×
729
  } else if (request->type == UDF_TASK_TEARDOWN) {
×
730
    len += encodeUdfTeardownRequest(buf, &request->teardown);
×
731
  }
732
  return len;
×
733
}
734

735
void *decodeUdfRequest(const void *buf, SUdfRequest *request) {
×
736
  request->msgLen = *(int32_t *)(buf);
×
737
  buf = POINTER_SHIFT(buf, sizeof(request->msgLen));
×
738

739
  buf = taosDecodeFixedI64(buf, &request->seqNum);
×
740
  buf = taosDecodeFixedI8(buf, &request->type);
×
741

742
  if (request->type == UDF_TASK_SETUP) {
×
743
    buf = decodeUdfSetupRequest(buf, &request->setup);
×
744
  } else if (request->type == UDF_TASK_CALL) {
×
745
    buf = decodeUdfCallRequest(buf, &request->call);
×
746
  } else if (request->type == UDF_TASK_TEARDOWN) {
×
747
    buf = decodeUdfTeardownRequest(buf, &request->teardown);
×
748
  }
749
  return (void *)buf;
×
750
}
751

752
int32_t encodeUdfSetupResponse(void **buf, const SUdfSetupResponse *setupRsp) {
×
753
  int32_t len = 0;
×
754
  len += taosEncodeFixedI64(buf, setupRsp->udfHandle);
×
755
  len += taosEncodeFixedI8(buf, setupRsp->outputType);
×
756
  len += taosEncodeFixedI32(buf, setupRsp->bytes);
×
757
  len += taosEncodeFixedI32(buf, setupRsp->bufSize);
×
758
  return len;
×
759
}
760

761
void *decodeUdfSetupResponse(const void *buf, SUdfSetupResponse *setupRsp) {
×
762
  buf = taosDecodeFixedI64(buf, &setupRsp->udfHandle);
×
763
  buf = taosDecodeFixedI8(buf, &setupRsp->outputType);
×
764
  buf = taosDecodeFixedI32(buf, &setupRsp->bytes);
×
765
  buf = taosDecodeFixedI32(buf, &setupRsp->bufSize);
×
766
  return (void *)buf;
×
767
}
768

769
int32_t encodeUdfCallResponse(void **buf, const SUdfCallResponse *callRsp) {
×
770
  int32_t len = 0;
×
771
  len += taosEncodeFixedI8(buf, callRsp->callType);
×
772
  switch (callRsp->callType) {
×
773
    case TSDB_UDF_CALL_SCALA_PROC:
×
774
      len += tEncodeDataBlock(buf, &callRsp->resultData);
×
775
      break;
×
776
    case TSDB_UDF_CALL_AGG_INIT:
×
777
      len += encodeUdfInterBuf(buf, &callRsp->resultBuf);
×
778
      break;
×
779
    case TSDB_UDF_CALL_AGG_PROC:
×
780
      len += encodeUdfInterBuf(buf, &callRsp->resultBuf);
×
781
      break;
×
782
    // case TSDB_UDF_CALL_AGG_MERGE:
783
    //   len += encodeUdfInterBuf(buf, &callRsp->resultBuf);
784
    //   break;
785
    case TSDB_UDF_CALL_AGG_FIN:
×
786
      len += encodeUdfInterBuf(buf, &callRsp->resultBuf);
×
787
      break;
×
788
  }
789
  return len;
×
790
}
791

792
void *decodeUdfCallResponse(const void *buf, SUdfCallResponse *callRsp) {
×
793
  buf = taosDecodeFixedI8(buf, &callRsp->callType);
×
794
  switch (callRsp->callType) {
×
795
    case TSDB_UDF_CALL_SCALA_PROC:
×
796
      buf = tDecodeDataBlock(buf, &callRsp->resultData);
×
797
      break;
×
798
    case TSDB_UDF_CALL_AGG_INIT:
×
799
      buf = decodeUdfInterBuf(buf, &callRsp->resultBuf);
×
800
      break;
×
801
    case TSDB_UDF_CALL_AGG_PROC:
×
802
      buf = decodeUdfInterBuf(buf, &callRsp->resultBuf);
×
803
      break;
×
804
    // case TSDB_UDF_CALL_AGG_MERGE:
805
    //   buf = decodeUdfInterBuf(buf, &callRsp->resultBuf);
806
    //   break;
807
    case TSDB_UDF_CALL_AGG_FIN:
×
808
      buf = decodeUdfInterBuf(buf, &callRsp->resultBuf);
×
809
      break;
×
810
  }
811
  return (void *)buf;
×
812
}
813

814
int32_t encodeUdfTeardownResponse(void **buf, const SUdfTeardownResponse *teardownRsp) { return 0; }
×
815

816
void *decodeUdfTeardownResponse(const void *buf, SUdfTeardownResponse *teardownResponse) { return (void *)buf; }
×
817

818
int32_t encodeUdfResponse(void **buf, const SUdfResponse *rsp) {
×
819
  int32_t len = 0;
×
820
  len += sizeof(rsp->msgLen);
×
821
  if (buf != NULL) {
×
822
    *(int32_t *)(*buf) = rsp->msgLen;
×
823
    *buf = POINTER_SHIFT(*buf, sizeof(rsp->msgLen));
×
824
  }
825

826
  len += sizeof(rsp->seqNum);
×
827
  if (buf != NULL) {
×
828
    *(int64_t *)(*buf) = rsp->seqNum;
×
829
    *buf = POINTER_SHIFT(*buf, sizeof(rsp->seqNum));
×
830
  }
831

832
  len += taosEncodeFixedI64(buf, rsp->seqNum);
×
833
  len += taosEncodeFixedI8(buf, rsp->type);
×
834
  len += taosEncodeFixedI32(buf, rsp->code);
×
835

836
  switch (rsp->type) {
×
837
    case UDF_TASK_SETUP:
×
838
      len += encodeUdfSetupResponse(buf, &rsp->setupRsp);
×
839
      break;
×
840
    case UDF_TASK_CALL:
×
841
      len += encodeUdfCallResponse(buf, &rsp->callRsp);
×
842
      break;
×
843
    case UDF_TASK_TEARDOWN:
×
844
      len += encodeUdfTeardownResponse(buf, &rsp->teardownRsp);
×
845
      break;
×
846
    default:
×
847
      fnError("encode udf response, invalid udf response type %d", rsp->type);
×
848
      break;
×
849
  }
850
  return len;
×
851
}
852

853
void *decodeUdfResponse(const void *buf, SUdfResponse *rsp) {
×
854
  rsp->msgLen = *(int32_t *)(buf);
×
855
  buf = POINTER_SHIFT(buf, sizeof(rsp->msgLen));
×
856
  rsp->seqNum = *(int64_t *)(buf);
×
857
  buf = POINTER_SHIFT(buf, sizeof(rsp->seqNum));
×
858
  buf = taosDecodeFixedI64(buf, &rsp->seqNum);
×
859
  buf = taosDecodeFixedI8(buf, &rsp->type);
×
860
  buf = taosDecodeFixedI32(buf, &rsp->code);
×
861

862
  switch (rsp->type) {
×
863
    case UDF_TASK_SETUP:
×
864
      buf = decodeUdfSetupResponse(buf, &rsp->setupRsp);
×
865
      break;
×
866
    case UDF_TASK_CALL:
×
867
      buf = decodeUdfCallResponse(buf, &rsp->callRsp);
×
868
      break;
×
869
    case UDF_TASK_TEARDOWN:
×
870
      buf = decodeUdfTeardownResponse(buf, &rsp->teardownRsp);
×
871
      break;
×
872
    default:
×
873
      rsp->code = TSDB_CODE_UDF_INTERNAL_ERROR;
×
874
      fnError("decode udf response, invalid udf response type %d", rsp->type);
×
875
      break;
×
876
  }
877
  if (buf == NULL) {
×
878
    rsp->code = terrno;
×
879
    fnError("decode udf response failed, code:0x%x", rsp->code);
×
880
  }
881
  return (void *)buf;
×
882
}
883

884
void freeUdfColumnData(SUdfColumnData *data, SUdfColumnMeta *meta) {
×
885
  TAOS_UDF_CHECK_PTR_RVOID(data, meta);
×
886
  if (IS_VAR_DATA_TYPE(meta->type)) {
×
887
    taosMemoryFree(data->varLenCol.varOffsets);
×
888
    data->varLenCol.varOffsets = NULL;
×
889
    taosMemoryFree(data->varLenCol.payload);
×
890
    data->varLenCol.payload = NULL;
×
891
  } else {
892
    taosMemoryFree(data->fixLenCol.nullBitmap);
×
893
    data->fixLenCol.nullBitmap = NULL;
×
894
    taosMemoryFree(data->fixLenCol.data);
×
895
    data->fixLenCol.data = NULL;
×
896
  }
897
}
898

899
void freeUdfColumn(SUdfColumn *col) {
×
900
  TAOS_UDF_CHECK_PTR_RVOID(col);
×
901
  freeUdfColumnData(&col->colData, &col->colMeta);
×
902
}
903

904
void freeUdfDataDataBlock(SUdfDataBlock *block) {
×
905
  TAOS_UDF_CHECK_PTR_RVOID(block);
×
906
  for (int32_t i = 0; i < block->numOfCols; ++i) {
×
907
    freeUdfColumn(block->udfCols[i]);
×
908
    taosMemoryFree(block->udfCols[i]);
×
909
    block->udfCols[i] = NULL;
×
910
  }
911
  taosMemoryFree(block->udfCols);
×
912
  block->udfCols = NULL;
×
913
}
914

915
void freeUdfInterBuf(SUdfInterBuf *buf) {
×
916
  TAOS_UDF_CHECK_PTR_RVOID(buf);
×
917
  taosMemoryFree(buf->buf);
×
918
  buf->buf = NULL;
×
919
}
920

921
int32_t convertDataBlockToUdfDataBlock(SSDataBlock *block, SUdfDataBlock *udfBlock) {
×
922
  TAOS_UDF_CHECK_PTR_RCODE(block, udfBlock);
×
923
  int32_t code = blockDataCheck(block);
×
924
  if (code != TSDB_CODE_SUCCESS) {
×
925
    return code;
×
926
  }
927
  udfBlock->numOfRows = block->info.rows;
×
928
  udfBlock->numOfCols = taosArrayGetSize(block->pDataBlock);
×
929
  udfBlock->udfCols = taosMemoryCalloc(taosArrayGetSize(block->pDataBlock), sizeof(SUdfColumn *));
×
930
  if ((udfBlock->udfCols) == NULL) {
×
931
    return terrno;
×
932
  }
933
  for (int32_t i = 0; i < udfBlock->numOfCols; ++i) {
×
934
    udfBlock->udfCols[i] = taosMemoryCalloc(1, sizeof(SUdfColumn));
×
935
    if (udfBlock->udfCols[i] == NULL) {
×
936
      return terrno;
×
937
    }
938
    SColumnInfoData *col = (SColumnInfoData *)taosArrayGet(block->pDataBlock, i);
×
939
    SUdfColumn      *udfCol = udfBlock->udfCols[i];
×
940
    udfCol->colMeta.type = col->info.type;
×
941
    udfCol->colMeta.bytes = col->info.bytes;
×
942
    udfCol->colMeta.scale = col->info.scale;
×
943
    udfCol->colMeta.precision = col->info.precision;
×
944
    udfCol->colData.numOfRows = udfBlock->numOfRows;
×
945
    udfCol->hasNull = col->hasNull;
×
946
    if (IS_VAR_DATA_TYPE(udfCol->colMeta.type)) {
×
947
      udfCol->colData.varLenCol.varOffsetsLen = sizeof(int32_t) * udfBlock->numOfRows;
×
948
      udfCol->colData.varLenCol.varOffsets = taosMemoryMalloc(udfCol->colData.varLenCol.varOffsetsLen);
×
949
      if (udfCol->colData.varLenCol.varOffsets == NULL) {
×
950
        return terrno;
×
951
      }
952
      memcpy(udfCol->colData.varLenCol.varOffsets, col->varmeta.offset, udfCol->colData.varLenCol.varOffsetsLen);
×
953
      udfCol->colData.varLenCol.payloadLen = colDataGetLength(col, udfBlock->numOfRows);
×
954
      udfCol->colData.varLenCol.payload = taosMemoryMalloc(udfCol->colData.varLenCol.payloadLen);
×
955
      if (udfCol->colData.varLenCol.payload == NULL) {
×
956
        return terrno;
×
957
      }
958
      if (col->reassigned) {
×
959
        for (int32_t row = 0; row < udfCol->colData.numOfRows; ++row) {
×
960
          char   *pColData = col->pData + col->varmeta.offset[row];
×
961
          int32_t colSize = 0;
×
962
          if (col->info.type == TSDB_DATA_TYPE_JSON) {
×
963
            colSize = getJsonValueLen(pColData);
×
964
          } else {
965
            colSize = varDataTLen(pColData);
×
966
          }
967
          memcpy(udfCol->colData.varLenCol.payload, pColData, colSize);
×
968
          udfCol->colData.varLenCol.payload += colSize;
×
969
        }
970
      } else {
971
        memcpy(udfCol->colData.varLenCol.payload, col->pData, udfCol->colData.varLenCol.payloadLen);
×
972
      }
973
    } else {
974
      udfCol->colData.fixLenCol.nullBitmapLen = BitmapLen(udfCol->colData.numOfRows);
×
975
      int32_t bitmapLen = udfCol->colData.fixLenCol.nullBitmapLen;
×
976
      udfCol->colData.fixLenCol.nullBitmap = taosMemoryMalloc(udfCol->colData.fixLenCol.nullBitmapLen);
×
977
      if (udfCol->colData.fixLenCol.nullBitmap == NULL) {
×
978
        return terrno;
×
979
      }
980
      char *bitmap = udfCol->colData.fixLenCol.nullBitmap;
×
981
      memcpy(bitmap, col->nullbitmap, bitmapLen);
×
982
      udfCol->colData.fixLenCol.dataLen = colDataGetLength(col, udfBlock->numOfRows);
×
983
      int32_t dataLen = udfCol->colData.fixLenCol.dataLen;
×
984
      udfCol->colData.fixLenCol.data = taosMemoryMalloc(udfCol->colData.fixLenCol.dataLen);
×
985
      if (NULL == udfCol->colData.fixLenCol.data) {
×
986
        return terrno;
×
987
      }
988
      char *data = udfCol->colData.fixLenCol.data;
×
989
      memcpy(data, col->pData, dataLen);
×
990
    }
991
  }
992
  return TSDB_CODE_SUCCESS;
×
993
}
994

995
int32_t convertUdfColumnToDataBlock(SUdfColumn *udfCol, SSDataBlock *block) {
×
996
  TAOS_UDF_CHECK_PTR_RCODE(udfCol, block);
×
997
  int32_t         code = 0, lino = 0;
×
998
  SUdfColumnMeta *meta = &udfCol->colMeta;
×
999

1000
  SColumnInfoData colInfoData = createColumnInfoData(meta->type, meta->bytes, 1);
×
1001
  code = blockDataAppendColInfo(block, &colInfoData);
×
1002
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1003

1004
  code = blockDataEnsureCapacity(block, udfCol->colData.numOfRows);
×
1005
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1006

1007
  SColumnInfoData *col = NULL;
×
1008
  code = bdGetColumnInfoData(block, 0, &col);
×
1009
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1010

1011
  for (int32_t i = 0; i < udfCol->colData.numOfRows; ++i) {
×
1012
    if (udfColDataIsNull(udfCol, i)) {
×
1013
      colDataSetNULL(col, i);
×
1014
    } else {
1015
      char *data = udfColDataGetData(udfCol, i);
×
1016
      code = colDataSetVal(col, i, data, false);
×
1017
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
1018
    }
1019
  }
1020
  block->info.rows = udfCol->colData.numOfRows;
×
1021

1022
  code = blockDataCheck(block);
×
1023
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1024
_exit:
×
1025
  if (code != 0) {
×
1026
    fnError("failed to convert udf column to data block, code:%d, line:%d", code, lino);
×
1027
  }
1028
  return TSDB_CODE_SUCCESS;
×
1029
}
1030

1031
int32_t convertScalarParamToDataBlock(SScalarParam *input, int32_t numOfCols, SSDataBlock *output) {
×
1032
  TAOS_UDF_CHECK_PTR_RCODE(input, output);
×
1033
  int32_t code = 0, lino = 0;
×
1034
  int32_t numOfRows = 0;
×
1035
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1036
    numOfRows = (input[i].numOfRows > numOfRows) ? input[i].numOfRows : numOfRows;
×
1037
  }
1038

1039
  // create the basic block info structure
1040
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1041
    SColumnInfoData *pInfo = input[i].columnData;
×
1042
    SColumnInfoData  d = {0};
×
1043
    d.info = pInfo->info;
×
1044

1045
    TAOS_CHECK_GOTO(blockDataAppendColInfo(output, &d), &lino, _exit);
×
1046
  }
1047

1048
  TAOS_CHECK_GOTO(blockDataEnsureCapacity(output, numOfRows), &lino, _exit);
×
1049

1050
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1051
    SColumnInfoData *pDest = taosArrayGet(output->pDataBlock, i);
×
1052

1053
    SColumnInfoData *pColInfoData = input[i].columnData;
×
1054
    TAOS_CHECK_GOTO(colDataAssign(pDest, pColInfoData, input[i].numOfRows, &output->info), &lino, _exit);
×
1055

1056
    if (input[i].numOfRows < numOfRows) {
×
1057
      int32_t startRow = input[i].numOfRows;
×
1058
      int32_t expandRows = numOfRows - startRow;
×
1059
      bool    isNull = colDataIsNull_s(pColInfoData, (input + i)->numOfRows - 1);
×
1060
      if (isNull) {
×
1061
        colDataSetNNULL(pDest, startRow, expandRows);
×
1062
      } else {
1063
        char *src = colDataGetData(pColInfoData, (input + i)->numOfRows - 1);
×
1064
        for (int32_t j = 0; j < expandRows; ++j) {
×
1065
          TAOS_CHECK_GOTO(colDataSetVal(pDest, startRow + j, src, false), &lino, _exit);
×
1066
        }
1067
      }
1068
    }
1069
  }
1070

1071
  output->info.rows = numOfRows;
×
1072
_exit:
×
1073
  if (code != 0) {
×
1074
    fnError("failed to convert scalar param to data block, code:%d, line:%d", code, lino);
×
1075
  }
1076
  return code;
×
1077
}
1078

1079
int32_t convertDataBlockToScalarParm(SSDataBlock *input, SScalarParam *output) {
×
1080
  TAOS_UDF_CHECK_PTR_RCODE(input, output);
×
1081
  if (taosArrayGetSize(input->pDataBlock) != 1) {
×
1082
    fnError("scalar function only support one column");
×
1083
    return 0;
×
1084
  }
1085
  output->numOfRows = input->info.rows;
×
1086

1087
  output->columnData = taosMemoryMalloc(sizeof(SColumnInfoData));
×
1088
  if (output->columnData == NULL) {
×
1089
    return terrno;
×
1090
  }
1091
  memcpy(output->columnData, taosArrayGet(input->pDataBlock, 0), sizeof(SColumnInfoData));
×
1092
  output->colAlloced = true;
×
1093

1094
  return 0;
×
1095
}
1096

1097
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
1098
// memory layout |---SUdfAggRes----|-----final result-----|---inter result----|
1099
typedef struct SUdfAggRes {
1100
  int8_t  finalResNum;
1101
  int8_t  interResNum;
1102
  int32_t interResBufLen;
1103
  char   *finalResBuf;
1104
  char   *interResBuf;
1105
} SUdfAggRes;
1106

1107
void    onUdfcPipeClose(uv_handle_t *handle);
1108
int32_t udfcGetUdfTaskResultFromUvTask(SClientUdfTask *task, SClientUvTaskNode *uvTask);
1109
void    udfcAllocateBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf);
1110
bool    isUdfcUvMsgComplete(SClientConnBuf *connBuf);
1111
void    udfcUvHandleRsp(SClientUvConn *conn);
1112
void    udfcUvHandleError(SClientUvConn *conn);
1113
void    onUdfcPipeRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf);
1114
void    onUdfcPipeWrite(uv_write_t *write, int32_t status);
1115
void    onUdfcPipeConnect(uv_connect_t *connect, int32_t status);
1116
int32_t udfcInitializeUvTask(SClientUdfTask *task, int8_t uvTaskType, SClientUvTaskNode *uvTask);
1117
int32_t udfcQueueUvTask(SClientUvTaskNode *uvTask);
1118
int32_t udfcStartUvTask(SClientUvTaskNode *uvTask);
1119
void    udfcAsyncTaskCb(uv_async_t *async);
1120
void    cleanUpUvTasks(SUdfcProxy *udfc);
1121
void    udfStopAsyncCb(uv_async_t *async);
1122
void    constructUdfService(void *argsThread);
1123
int32_t udfcRunUdfUvTask(SClientUdfTask *task, int8_t uvTaskType);
1124
int32_t doSetupUdf(char udfName[], UdfcFuncHandle *funcHandle);
1125
int32_t compareUdfcFuncSub(const void *elem1, const void *elem2);
1126
int32_t doTeardownUdf(UdfcFuncHandle handle);
1127

1128
int32_t callUdf(UdfcFuncHandle handle, int8_t callType, SSDataBlock *input, SUdfInterBuf *state, SUdfInterBuf *state2,
1129
                SSDataBlock *output, SUdfInterBuf *newState);
1130
int32_t doCallUdfAggInit(UdfcFuncHandle handle, SUdfInterBuf *interBuf);
1131
int32_t doCallUdfAggProcess(UdfcFuncHandle handle, SSDataBlock *block, SUdfInterBuf *state, SUdfInterBuf *newState);
1132
// udf todo:  aggmerge
1133
// int32_t doCallUdfAggMerge(UdfcFuncHandle handle, SUdfInterBuf *interBuf1, SUdfInterBuf *interBuf2,
1134
//                           SUdfInterBuf *resultBuf);
1135
int32_t doCallUdfAggFinalize(UdfcFuncHandle handle, SUdfInterBuf *interBuf, SUdfInterBuf *resultData);
1136
int32_t doCallUdfScalarFunc(UdfcFuncHandle handle, SScalarParam *input, int32_t numOfCols, SScalarParam *output);
1137
int32_t callUdfScalarFunc(char *udfName, SScalarParam *input, int32_t numOfCols, SScalarParam *output);
1138

1139
int32_t udfcOpen();
1140
int32_t udfcClose();
1141

1142
int32_t acquireUdfFuncHandle(char *udfName, UdfcFuncHandle *pHandle);
1143
void    releaseUdfFuncHandle(char *udfName, UdfcFuncHandle handle);
1144
int32_t cleanUpUdfs();
1145

1146
bool    udfAggGetEnv(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv);
1147
int32_t udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo);
1148
int32_t udfAggProcess(struct SqlFunctionCtx *pCtx);
1149
int32_t udfAggFinalize(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock);
1150

1151
void    cleanupNotExpiredUdfs();
1152
void    cleanupExpiredUdfs();
1153
int32_t compareUdfcFuncSub(const void *elem1, const void *elem2) {
×
1154
  SUdfcFuncStub *stub1 = (SUdfcFuncStub *)elem1;
×
1155
  SUdfcFuncStub *stub2 = (SUdfcFuncStub *)elem2;
×
1156
  return strcmp(stub1->udfName, stub2->udfName);
×
1157
}
1158

1159
int32_t acquireUdfFuncHandle(char *udfName, UdfcFuncHandle *pHandle) {
×
1160
  TAOS_UDF_CHECK_PTR_RCODE(udfName, pHandle);
×
1161
  int32_t code = 0, line = 0;
×
1162
  uv_mutex_lock(&gUdfcProxy.udfStubsMutex);
×
1163
  SUdfcFuncStub key = {0};
×
1164
  tstrncpy(key.udfName, udfName, TSDB_FUNC_NAME_LEN);
×
1165
  int32_t stubIndex = taosArraySearchIdx(gUdfcProxy.udfStubs, &key, compareUdfcFuncSub, TD_EQ);
×
1166
  if (stubIndex != -1) {
×
1167
    SUdfcFuncStub *foundStub = taosArrayGet(gUdfcProxy.udfStubs, stubIndex);
×
1168
    UdfcFuncHandle handle = foundStub->handle;
×
1169
    int64_t        currUs = taosGetTimestampUs();
×
1170
    bool           expired = (currUs - foundStub->createTime) >= 10 * 1000 * 1000;
×
1171
    if (!expired) {
×
1172
      if (handle != NULL && ((SUdfcUvSession *)handle)->udfUvPipe != NULL) {
×
1173
        *pHandle = foundStub->handle;
×
1174
        ++foundStub->refCount;
×
1175
        uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1176
        return 0;
×
1177
      } else {
1178
        fnInfo("udf invalid handle for %s, refCount: %d, create time: %" PRId64 ". remove it from cache", udfName,
×
1179
               foundStub->refCount, foundStub->createTime);
1180
        taosArrayRemove(gUdfcProxy.udfStubs, stubIndex);
×
1181
      }
1182
    } else {
1183
      fnDebug("udf handle expired for %s, will setup udf. move it to expired list", udfName);
×
1184
      if (taosArrayPush(gUdfcProxy.expiredUdfStubs, foundStub) == NULL) {
×
1185
        fnError("acquireUdfFuncHandle: failed to push udf stub to array");
×
1186
      } else {
1187
        taosArrayRemove(gUdfcProxy.udfStubs, stubIndex);
×
1188
        taosArraySort(gUdfcProxy.expiredUdfStubs, compareUdfcFuncSub);
×
1189
      }
1190
    }
1191
  }
1192
  uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1193
  *pHandle = NULL;
×
1194
  code = doSetupUdf(udfName, pHandle);
×
1195
  if (code == TSDB_CODE_SUCCESS) {
×
1196
    SUdfcFuncStub stub = {0};
×
1197
    tstrncpy(stub.udfName, udfName, TSDB_FUNC_NAME_LEN);
×
1198
    stub.handle = *pHandle;
×
1199
    ++stub.refCount;
×
1200
    stub.createTime = taosGetTimestampUs();
×
1201
    uv_mutex_lock(&gUdfcProxy.udfStubsMutex);
×
1202
    if (taosArrayPush(gUdfcProxy.udfStubs, &stub) == NULL) {
×
1203
      fnError("acquireUdfFuncHandle: failed to push udf stub to array");
×
1204
      uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1205
      goto _exit;
×
1206
    } else {
1207
      taosArraySort(gUdfcProxy.udfStubs, compareUdfcFuncSub);
×
1208
    }
1209
    uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1210
  } else {
1211
    *pHandle = NULL;
×
1212
  }
1213

1214
_exit:
×
1215
  return code;
×
1216
}
1217

1218
void releaseUdfFuncHandle(char *udfName, UdfcFuncHandle handle) {
×
1219
  TAOS_UDF_CHECK_PTR_RVOID(udfName);
×
1220
  uv_mutex_lock(&gUdfcProxy.udfStubsMutex);
×
1221
  SUdfcFuncStub key = {0};
×
1222
  tstrncpy(key.udfName, udfName, TSDB_FUNC_NAME_LEN);
×
1223
  SUdfcFuncStub *foundStub = taosArraySearch(gUdfcProxy.udfStubs, &key, compareUdfcFuncSub, TD_EQ);
×
1224
  SUdfcFuncStub *expiredStub = taosArraySearch(gUdfcProxy.expiredUdfStubs, &key, compareUdfcFuncSub, TD_EQ);
×
1225
  if (!foundStub && !expiredStub) {
×
1226
    uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1227
    return;
×
1228
  }
1229
  if (foundStub != NULL && foundStub->handle == handle && foundStub->refCount > 0) {
×
1230
    --foundStub->refCount;
×
1231
  }
1232
  if (expiredStub != NULL && expiredStub->handle == handle && expiredStub->refCount > 0) {
×
1233
    --expiredStub->refCount;
×
1234
  }
1235
  uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1236
}
1237

1238
void cleanupExpiredUdfs() {
×
1239
  int32_t i = 0;
×
1240
  SArray *expiredUdfStubs = taosArrayInit(16, sizeof(SUdfcFuncStub));
×
1241
  if (expiredUdfStubs == NULL) {
×
1242
    fnError("cleanupExpiredUdfs: failed to init array");
×
1243
    return;
×
1244
  }
1245
  while (i < taosArrayGetSize(gUdfcProxy.expiredUdfStubs)) {
×
1246
    SUdfcFuncStub *stub = taosArrayGet(gUdfcProxy.expiredUdfStubs, i);
×
1247
    if (stub->refCount == 0) {
×
1248
      fnInfo("tear down udf. expired. udf name: %s, handle: %p, ref count: %d", stub->udfName, stub->handle,
×
1249
             stub->refCount);
1250
      (void)doTeardownUdf(stub->handle);
×
1251
    } else {
1252
      fnInfo("udf still in use. expired. udf name: %s, ref count: %d, create time: %" PRId64 ", handle: %p",
×
1253
             stub->udfName, stub->refCount, stub->createTime, stub->handle);
1254
      UdfcFuncHandle handle = stub->handle;
×
1255
      if (handle != NULL && ((SUdfcUvSession *)handle)->udfUvPipe != NULL) {
×
1256
        if (taosArrayPush(expiredUdfStubs, stub) == NULL) {
×
1257
          fnError("cleanupExpiredUdfs: failed to push udf stub to array");
×
1258
        }
1259
      } else {
1260
        fnInfo("udf invalid handle for %s, expired. refCount: %d, create time: %" PRId64 ". remove it from cache",
×
1261
               stub->udfName, stub->refCount, stub->createTime);
1262
      }
1263
    }
1264
    ++i;
×
1265
  }
1266
  taosArrayDestroy(gUdfcProxy.expiredUdfStubs);
×
1267
  gUdfcProxy.expiredUdfStubs = expiredUdfStubs;
×
1268
}
1269

1270
void cleanupNotExpiredUdfs() {
×
1271
  SArray *udfStubs = taosArrayInit(16, sizeof(SUdfcFuncStub));
×
1272
  if (udfStubs == NULL) {
×
1273
    fnError("cleanupNotExpiredUdfs: failed to init array");
×
1274
    return;
×
1275
  }
1276
  int32_t i = 0;
×
1277
  while (i < taosArrayGetSize(gUdfcProxy.udfStubs)) {
×
1278
    SUdfcFuncStub *stub = taosArrayGet(gUdfcProxy.udfStubs, i);
×
1279
    if (stub->refCount == 0) {
×
1280
      fnInfo("tear down udf. udf name: %s, handle: %p, ref count: %d", stub->udfName, stub->handle, stub->refCount);
×
1281
      (void)doTeardownUdf(stub->handle);
×
1282
    } else {
1283
      fnInfo("udf still in use. udf name: %s, ref count: %d, create time: %" PRId64 ", handle: %p", stub->udfName,
×
1284
             stub->refCount, stub->createTime, stub->handle);
1285
      UdfcFuncHandle handle = stub->handle;
×
1286
      if (handle != NULL && ((SUdfcUvSession *)handle)->udfUvPipe != NULL) {
×
1287
        if (taosArrayPush(udfStubs, stub) == NULL) {
×
1288
          fnError("cleanupNotExpiredUdfs: failed to push udf stub to array");
×
1289
        }
1290
      } else {
1291
        fnInfo("udf invalid handle for %s, refCount: %d, create time: %" PRId64 ". remove it from cache", stub->udfName,
×
1292
               stub->refCount, stub->createTime);
1293
      }
1294
    }
1295
    ++i;
×
1296
  }
1297
  taosArrayDestroy(gUdfcProxy.udfStubs);
×
1298
  gUdfcProxy.udfStubs = udfStubs;
×
1299
}
1300

1301
int32_t cleanUpUdfs() {
×
1302
  int8_t initialized = atomic_load_8(&gUdfcProxy.initialized);
×
1303
  if (!initialized) {
×
1304
    return TSDB_CODE_SUCCESS;
×
1305
  }
1306

1307
  uv_mutex_lock(&gUdfcProxy.udfStubsMutex);
×
1308
  if ((gUdfcProxy.udfStubs == NULL || taosArrayGetSize(gUdfcProxy.udfStubs) == 0) &&
×
1309
      (gUdfcProxy.expiredUdfStubs == NULL || taosArrayGetSize(gUdfcProxy.expiredUdfStubs) == 0)) {
×
1310
    uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1311
    return TSDB_CODE_SUCCESS;
×
1312
  }
1313

1314
  cleanupNotExpiredUdfs();
×
1315
  cleanupExpiredUdfs();
×
1316

1317
  uv_mutex_unlock(&gUdfcProxy.udfStubsMutex);
×
1318
  return 0;
×
1319
}
1320

1321
int32_t callUdfScalarFunc(char *udfName, SScalarParam *input, int32_t numOfCols, SScalarParam *output) {
×
1322
  TAOS_UDF_CHECK_PTR_RCODE(udfName, input, output);
×
1323
  UdfcFuncHandle handle = NULL;
×
1324
  int32_t        code = acquireUdfFuncHandle(udfName, &handle);
×
1325
  if (code != 0) {
×
1326
    return code;
×
1327
  }
1328

1329
  SUdfcUvSession *session = handle;
×
1330
  code = doCallUdfScalarFunc(handle, input, numOfCols, output);
×
1331
  if (code != TSDB_CODE_SUCCESS) {
×
1332
    fnError("udfc scalar function execution failure");
×
1333
    releaseUdfFuncHandle(udfName, handle);
×
1334
    return code;
×
1335
  }
1336

1337
  if (output->columnData == NULL) {
×
1338
    fnError("udfc scalar function calculate error. no column data");
×
1339
    code = TSDB_CODE_UDF_INVALID_OUTPUT_TYPE;
×
1340
  } else {
1341
    if (session->outputType != output->columnData->info.type || session->bytes != output->columnData->info.bytes) {
×
1342
      fnError("udfc scalar function calculate error. type mismatch. session type: %d(%d), output type: %d(%d)",
×
1343
              session->outputType, session->bytes, output->columnData->info.type, output->columnData->info.bytes);
1344
      code = TSDB_CODE_UDF_INVALID_OUTPUT_TYPE;
×
1345
    }
1346
  }
1347
  releaseUdfFuncHandle(udfName, handle);
×
1348
  return code;
×
1349
}
1350

1351
bool udfAggGetEnv(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv) {
×
1352
  if (pFunc == NULL || pEnv == NULL) {
×
1353
    fnError("udfAggGetEnv: invalid input lint: %d", __LINE__);
×
1354
    return false;
×
1355
  }
1356
  if (fmIsScalarFunc(pFunc->funcId)) {
×
1357
    return false;
×
1358
  }
1359
  pEnv->calcMemSize = sizeof(SUdfAggRes) + pFunc->node.resType.bytes + pFunc->udfBufSize;
×
1360
  return true;
×
1361
}
1362

1363
int32_t udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo) {
×
1364
  TAOS_UDF_CHECK_PTR_RCODE(pCtx, pResultCellInfo);
×
1365
  if (pResultCellInfo->initialized) {
×
1366
    return TSDB_CODE_SUCCESS;
×
1367
  }
1368
  if (functionSetup(pCtx, pResultCellInfo) != TSDB_CODE_SUCCESS) {
×
1369
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1370
  }
1371
  UdfcFuncHandle handle;
1372
  int32_t        udfCode = 0;
×
1373
  if ((udfCode = acquireUdfFuncHandle((char *)pCtx->udfName, &handle)) != 0) {
×
1374
    fnError("udfAggInit error. step doSetupUdf. udf code: %d", udfCode);
×
1375
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1376
  }
1377
  SUdfcUvSession *session = (SUdfcUvSession *)handle;
×
1378
  SUdfAggRes     *udfRes = (SUdfAggRes *)GET_ROWCELL_INTERBUF(pResultCellInfo);
×
1379
  int32_t         envSize = sizeof(SUdfAggRes) + session->bytes + session->bufSize;
×
1380
  memset(udfRes, 0, envSize);
×
1381

1382
  udfRes->finalResBuf = (char *)udfRes + sizeof(SUdfAggRes);
×
1383
  udfRes->interResBuf = (char *)udfRes + sizeof(SUdfAggRes) + session->bytes;
×
1384

1385
  SUdfInterBuf buf = {0};
×
1386
  if ((udfCode = doCallUdfAggInit(handle, &buf)) != 0) {
×
1387
    fnError("udfAggInit error. step doCallUdfAggInit. udf code: %d", udfCode);
×
1388
    releaseUdfFuncHandle(pCtx->udfName, handle);
×
1389
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1390
  }
1391
  if (buf.bufLen <= session->bufSize) {
×
1392
    memcpy(udfRes->interResBuf, buf.buf, buf.bufLen);
×
1393
    udfRes->interResBufLen = buf.bufLen;
×
1394
    udfRes->interResNum = buf.numOfResult;
×
1395
  } else {
1396
    fnError("udfc inter buf size %d is greater than function bufSize %d", buf.bufLen, session->bufSize);
×
1397
    releaseUdfFuncHandle(pCtx->udfName, handle);
×
1398
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1399
  }
1400
  releaseUdfFuncHandle(pCtx->udfName, handle);
×
1401
  freeUdfInterBuf(&buf);
×
1402
  return TSDB_CODE_SUCCESS;
×
1403
}
1404

1405
int32_t udfAggProcess(struct SqlFunctionCtx *pCtx) {
×
1406
  TAOS_UDF_CHECK_PTR_RCODE(pCtx);
×
1407
  int32_t        udfCode = 0;
×
1408
  UdfcFuncHandle handle = 0;
×
1409
  if ((udfCode = acquireUdfFuncHandle((char *)pCtx->udfName, &handle)) != 0) {
×
1410
    fnError("udfAggProcess  error. step acquireUdfFuncHandle. udf code: %d", udfCode);
×
1411
    return udfCode;
×
1412
  }
1413

1414
  SUdfcUvSession *session = handle;
×
1415
  SUdfAggRes     *udfRes = (SUdfAggRes *)GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1416
  udfRes->finalResBuf = (char *)udfRes + sizeof(SUdfAggRes);
×
1417
  udfRes->interResBuf = (char *)udfRes + sizeof(SUdfAggRes) + session->bytes;
×
1418

1419
  SInputColumnInfoData *pInput = &pCtx->input;
×
1420
  int32_t               numOfCols = pInput->numOfInputCols;
×
1421
  int32_t               start = pInput->startRowIndex;
×
1422
  int32_t               numOfRows = pInput->numOfRows;
×
1423
  SSDataBlock          *pTempBlock = NULL;
×
1424
  int32_t               code = createDataBlock(&pTempBlock);
×
1425

1426
  if (code) {
×
1427
    return code;
×
1428
  }
1429

1430
  pTempBlock->info.rows = pInput->totalRows;
×
1431
  pTempBlock->info.id.uid = pInput->uid;
×
1432
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1433
    if ((udfCode = blockDataAppendColInfo(pTempBlock, pInput->pData[i])) != 0) {
×
1434
      fnError("udfAggProcess error. step blockDataAppendColInfo. udf code: %d", udfCode);
×
1435
      blockDataDestroy(pTempBlock);
×
1436
      return udfCode;
×
1437
    }
1438
  }
1439

1440
  SSDataBlock *inputBlock = NULL;
×
1441
  code = blockDataExtractBlock(pTempBlock, start, numOfRows, &inputBlock);
×
1442
  if (code) {
×
1443
    return code;
×
1444
  }
1445

1446
  SUdfInterBuf state = {
×
1447
      .buf = udfRes->interResBuf, .bufLen = udfRes->interResBufLen, .numOfResult = udfRes->interResNum};
×
1448
  SUdfInterBuf newState = {0};
×
1449

1450
  udfCode = doCallUdfAggProcess(session, inputBlock, &state, &newState);
×
1451
  if (udfCode != 0) {
×
1452
    fnError("udfAggProcess error. code: %d", udfCode);
×
1453
    newState.numOfResult = 0;
×
1454
  } else {
1455
    if (newState.bufLen <= session->bufSize) {
×
1456
      memcpy(udfRes->interResBuf, newState.buf, newState.bufLen);
×
1457
      udfRes->interResBufLen = newState.bufLen;
×
1458
      udfRes->interResNum = newState.numOfResult;
×
1459
    } else {
1460
      fnError("udfc inter buf size %d is greater than function bufSize %d", newState.bufLen, session->bufSize);
×
1461
      udfCode = TSDB_CODE_UDF_INVALID_BUFSIZE;
×
1462
    }
1463
  }
1464

1465
  GET_RES_INFO(pCtx)->numOfRes = udfRes->interResNum;
×
1466

1467
  blockDataDestroy(inputBlock);
×
1468

1469
  taosArrayDestroy(pTempBlock->pDataBlock);
×
1470
  taosMemoryFree(pTempBlock);
×
1471

1472
  releaseUdfFuncHandle(pCtx->udfName, handle);
×
1473
  freeUdfInterBuf(&newState);
×
1474
  return udfCode;
×
1475
}
1476

1477
int32_t udfAggFinalize(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock) {
×
1478
  TAOS_UDF_CHECK_PTR_RCODE(pCtx, pBlock);
×
1479
  int32_t        udfCode = 0;
×
1480
  UdfcFuncHandle handle = 0;
×
1481
  if ((udfCode = acquireUdfFuncHandle((char *)pCtx->udfName, &handle)) != 0) {
×
1482
    fnError("udfAggProcess  error. step acquireUdfFuncHandle. udf code: %d", udfCode);
×
1483
    return udfCode;
×
1484
  }
1485

1486
  SUdfcUvSession *session = handle;
×
1487
  SUdfAggRes     *udfRes = (SUdfAggRes *)GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1488
  udfRes->finalResBuf = (char *)udfRes + sizeof(SUdfAggRes);
×
1489
  udfRes->interResBuf = (char *)udfRes + sizeof(SUdfAggRes) + session->bytes;
×
1490

1491
  SUdfInterBuf resultBuf = {0};
×
1492
  SUdfInterBuf state = {
×
1493
      .buf = udfRes->interResBuf, .bufLen = udfRes->interResBufLen, .numOfResult = udfRes->interResNum};
×
1494
  int32_t udfCallCode = 0;
×
1495
  udfCallCode = doCallUdfAggFinalize(session, &state, &resultBuf);
×
1496
  if (udfCallCode != 0) {
×
1497
    fnError("udfAggFinalize error. doCallUdfAggFinalize step. udf code:%d", udfCallCode);
×
1498
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1499
  } else {
1500
    if (resultBuf.numOfResult == 0) {
×
1501
      udfRes->finalResNum = 0;
×
1502
      GET_RES_INFO(pCtx)->numOfRes = 0;
×
1503
    } else {
1504
      if (resultBuf.bufLen <= session->bytes) {
×
1505
        memcpy(udfRes->finalResBuf, resultBuf.buf, resultBuf.bufLen);
×
1506
        udfRes->finalResNum = resultBuf.numOfResult;
×
1507
        GET_RES_INFO(pCtx)->numOfRes = udfRes->finalResNum;
×
1508
      } else {
1509
        fnError("udfc inter buf size %d is greater than function output size %d", resultBuf.bufLen, session->bytes);
×
1510
        GET_RES_INFO(pCtx)->numOfRes = 0;
×
1511
        udfCallCode = TSDB_CODE_UDF_INVALID_OUTPUT_TYPE;
×
1512
      }
1513
    }
1514
  }
1515

1516
  freeUdfInterBuf(&resultBuf);
×
1517

1518
  int32_t numOfResults = functionFinalizeWithResultBuf(pCtx, pBlock, udfRes->finalResBuf);
×
1519
  releaseUdfFuncHandle(pCtx->udfName, handle);
×
1520
  return udfCallCode == 0 ? numOfResults : udfCallCode;
×
1521
}
1522

1523
void onUdfcPipeClose(uv_handle_t *handle) {
×
1524
  SClientUvConn *conn = handle->data;
×
1525
  if (!QUEUE_EMPTY(&conn->taskQueue)) {
×
1526
    QUEUE             *h = QUEUE_HEAD(&conn->taskQueue);
×
1527
    SClientUvTaskNode *task = QUEUE_DATA(h, SClientUvTaskNode, connTaskQueue);
×
1528
    task->errCode = 0;
×
1529
    QUEUE_REMOVE(&task->procTaskQueue);
×
1530
    uv_sem_post(&task->taskSem);
×
1531
  }
1532
  uv_mutex_lock(&gUdfcProxy.udfcUvMutex);
×
1533
  if (conn->session != NULL) {
×
1534
    conn->session->udfUvPipe = NULL;
×
1535
  }
1536
  uv_mutex_unlock(&gUdfcProxy.udfcUvMutex);
×
1537
  taosMemoryFree(conn->readBuf.buf);
×
1538
  taosMemoryFree(conn);
×
1539
  taosMemoryFree((uv_pipe_t *)handle);
×
1540
}
×
1541

1542
int32_t udfcGetUdfTaskResultFromUvTask(SClientUdfTask *task, SClientUvTaskNode *uvTask) {
×
1543
  int32_t code = 0;
×
1544
  fnDebug("udfc get uv task result. task: %p, uvTask: %p", task, uvTask);
×
1545
  if (uvTask->type == UV_TASK_REQ_RSP) {
×
1546
    if (uvTask->rspBuf.base != NULL) {
×
1547
      SUdfResponse rsp = {0};
×
1548
      void        *buf = decodeUdfResponse(uvTask->rspBuf.base, &rsp);
×
1549
      code = rsp.code;
×
1550
      if (code != 0) {
×
1551
        fnError("udfc get udf task result failure. code: %d", code);
×
1552
      }
1553

1554
      switch (task->type) {
×
1555
        case UDF_TASK_SETUP: {
×
1556
          task->_setup.rsp = rsp.setupRsp;
×
1557
          break;
×
1558
        }
1559
        case UDF_TASK_CALL: {
×
1560
          task->_call.rsp = rsp.callRsp;
×
1561
          break;
×
1562
        }
1563
        case UDF_TASK_TEARDOWN: {
×
1564
          task->_teardown.rsp = rsp.teardownRsp;
1565
          break;
×
1566
        }
1567
        default: {
×
1568
          break;
×
1569
        }
1570
      }
1571

1572
      // TODO: the call buffer is setup and freed by udf invocation
1573
      taosMemoryFreeClear(uvTask->rspBuf.base);
×
1574
    } else {
1575
      code = uvTask->errCode;
×
1576
      if (code != 0) {
×
1577
        fnError("udfc get udf task result failure. code: %d, line:%d", code, __LINE__);
×
1578
      }
1579
    }
1580
  } else if (uvTask->type == UV_TASK_CONNECT) {
×
1581
    code = uvTask->errCode;
×
1582
    if (code != 0) {
×
1583
      fnError("udfc get udf task result failure. code: %d, line:%d", code, __LINE__);
×
1584
    }
1585
  } else if (uvTask->type == UV_TASK_DISCONNECT) {
×
1586
    code = uvTask->errCode;
×
1587
    if (code != 0) {
×
1588
      fnError("udfc get udf task result failure. code: %d, line:%d", code, __LINE__);
×
1589
    }
1590
  }
1591
  return code;
×
1592
}
1593

1594
void udfcAllocateBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf) {
×
1595
  SClientUvConn  *conn = handle->data;
×
1596
  SClientConnBuf *connBuf = &conn->readBuf;
×
1597

1598
  int32_t msgHeadSize = sizeof(int32_t) + sizeof(int64_t);
×
1599
  if (connBuf->cap == 0) {
×
1600
    connBuf->buf = taosMemoryMalloc(msgHeadSize);
×
1601
    if (connBuf->buf) {
×
1602
      connBuf->len = 0;
×
1603
      connBuf->cap = msgHeadSize;
×
1604
      connBuf->total = -1;
×
1605

1606
      buf->base = connBuf->buf;
×
1607
      buf->len = connBuf->cap;
×
1608
    } else {
1609
      fnError("udfc allocate buffer failure. size: %d", msgHeadSize);
×
1610
      buf->base = NULL;
×
1611
      buf->len = 0;
×
1612
    }
1613
  } else if (connBuf->total == -1 && connBuf->len < msgHeadSize) {
×
1614
    buf->base = connBuf->buf + connBuf->len;
×
1615
    buf->len = msgHeadSize - connBuf->len;
×
1616
  } else {
1617
    connBuf->cap = connBuf->total > connBuf->cap ? connBuf->total : connBuf->cap;
×
1618
    void *resultBuf = taosMemoryRealloc(connBuf->buf, connBuf->cap);
×
1619
    if (resultBuf) {
×
1620
      connBuf->buf = resultBuf;
×
1621
      buf->base = connBuf->buf + connBuf->len;
×
1622
      buf->len = connBuf->cap - connBuf->len;
×
1623
    } else {
1624
      fnError("udfc re-allocate buffer failure. size: %d", connBuf->cap);
×
1625
      buf->base = NULL;
×
1626
      buf->len = 0;
×
1627
    }
1628
  }
1629

1630
  fnDebug("udfc uv alloc buffer: cap - len - total : %d - %d - %d", connBuf->cap, connBuf->len, connBuf->total);
×
1631
}
×
1632

1633
bool isUdfcUvMsgComplete(SClientConnBuf *connBuf) {
×
1634
  if (connBuf->total == -1 && connBuf->len >= sizeof(int32_t)) {
×
1635
    connBuf->total = *(int32_t *)(connBuf->buf);
×
1636
  }
1637
  if (connBuf->len == connBuf->cap && connBuf->total == connBuf->cap) {
×
1638
    fnDebug("udfc complete message is received, now handle it");
×
1639
    return true;
×
1640
  }
1641
  return false;
×
1642
}
1643

1644
void udfcUvHandleRsp(SClientUvConn *conn) {
×
1645
  SClientConnBuf *connBuf = &conn->readBuf;
×
1646
  int64_t         seqNum = *(int64_t *)(connBuf->buf + sizeof(int32_t));  // msglen then seqnum
×
1647

1648
  if (QUEUE_EMPTY(&conn->taskQueue)) {
×
1649
    fnError("udfc no task waiting on connection. response seqnum:%" PRId64, seqNum);
×
1650
    return;
×
1651
  }
1652
  bool               found = false;
×
1653
  SClientUvTaskNode *taskFound = NULL;
×
1654
  QUEUE             *h = QUEUE_NEXT(&conn->taskQueue);
×
1655
  SClientUvTaskNode *task = QUEUE_DATA(h, SClientUvTaskNode, connTaskQueue);
×
1656

1657
  while (h != &conn->taskQueue) {
×
1658
    fnDebug("udfc handle response iterate through queue. uvTask:%" PRId64 "-%p", task->seqNum, task);
×
1659
    if (task->seqNum == seqNum) {
×
1660
      if (found == false) {
×
1661
        found = true;
×
1662
        taskFound = task;
×
1663
      } else {
1664
        fnError("udfc more than one task waiting for the same response");
×
1665
        continue;
×
1666
      }
1667
    }
1668
    h = QUEUE_NEXT(h);
×
1669
    task = QUEUE_DATA(h, SClientUvTaskNode, connTaskQueue);
×
1670
  }
1671

1672
  if (taskFound) {
×
1673
    taskFound->rspBuf = uv_buf_init(connBuf->buf, connBuf->len);
×
1674
    QUEUE_REMOVE(&taskFound->connTaskQueue);
×
1675
    QUEUE_REMOVE(&taskFound->procTaskQueue);
×
1676
    uv_sem_post(&taskFound->taskSem);
×
1677
  } else {
1678
    fnError("no task is waiting for the response.");
×
1679
  }
1680
  connBuf->buf = NULL;
×
1681
  connBuf->total = -1;
×
1682
  connBuf->len = 0;
×
1683
  connBuf->cap = 0;
×
1684
}
1685

1686
void udfcUvHandleError(SClientUvConn *conn) {
×
1687
  fnDebug("handle error on conn: %p, pipe: %p", conn, conn->pipe);
×
1688
  while (!QUEUE_EMPTY(&conn->taskQueue)) {
×
1689
    QUEUE             *h = QUEUE_HEAD(&conn->taskQueue);
×
1690
    SClientUvTaskNode *task = QUEUE_DATA(h, SClientUvTaskNode, connTaskQueue);
×
1691
    task->errCode = TSDB_CODE_UDF_PIPE_READ_ERR;
×
1692
    QUEUE_REMOVE(&task->connTaskQueue);
×
1693
    QUEUE_REMOVE(&task->procTaskQueue);
×
1694
    uv_sem_post(&task->taskSem);
×
1695
  }
1696
  if (!uv_is_closing((uv_handle_t *)conn->pipe)) {
×
1697
    uv_close((uv_handle_t *)conn->pipe, onUdfcPipeClose);
×
1698
  }
1699
}
×
1700

1701
void onUdfcPipeRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
×
1702
  fnDebug("udfc client %p, client read from pipe. nread: %zd", client, nread);
×
1703
  if (nread == 0) return;
×
1704

1705
  SClientUvConn  *conn = client->data;
×
1706
  SClientConnBuf *connBuf = &conn->readBuf;
×
1707
  if (nread > 0) {
×
1708
    connBuf->len += nread;
×
1709
    if (isUdfcUvMsgComplete(connBuf)) {
×
1710
      udfcUvHandleRsp(conn);
×
1711
    }
1712
  }
1713
  if (nread < 0) {
×
1714
    fnError("udfc client pipe %p read error: %zd(%s).", client, nread, uv_strerror(nread));
×
1715
    if (nread == UV_EOF) {
×
1716
      fnError("\tudfc client pipe %p closed", client);
×
1717
    }
1718
    udfcUvHandleError(conn);
×
1719
  }
1720
}
1721

1722
void onUdfcPipeWrite(uv_write_t *write, int32_t status) {
×
1723
  SClientUvConn *conn = write->data;
×
1724
  if (status < 0) {
×
1725
    fnError("udfc client connection %p write failed. status: %d(%s)", conn, status, uv_strerror(status));
×
1726
    udfcUvHandleError(conn);
×
1727
  } else {
1728
    fnDebug("udfc client connection %p write succeed", conn);
×
1729
  }
1730
  taosMemoryFree(write);
×
1731
}
×
1732

1733
void onUdfcPipeConnect(uv_connect_t *connect, int32_t status) {
×
1734
  SClientUvTaskNode *uvTask = connect->data;
×
1735
  if (status != 0) {
×
1736
    fnError("client connect error, task seq: %" PRId64 ", code:%s", uvTask->seqNum, uv_strerror(status));
×
1737
  }
1738
  uvTask->errCode = status;
×
1739

1740
  int32_t code = uv_read_start((uv_stream_t *)uvTask->pipe, udfcAllocateBuffer, onUdfcPipeRead);
×
1741
  if (code != 0) {
×
1742
    fnError("udfc client connection %p read start failed. code: %d(%s)", uvTask->pipe, code, uv_strerror(code));
×
1743
    uvTask->errCode = code;
×
1744
  }
1745
  taosMemoryFree(connect);
×
1746
  QUEUE_REMOVE(&uvTask->procTaskQueue);
×
1747
  uv_sem_post(&uvTask->taskSem);
×
1748
}
×
1749

1750
int32_t udfcInitializeUvTask(SClientUdfTask *task, int8_t uvTaskType, SClientUvTaskNode *uvTask) {
×
1751
  uvTask->type = uvTaskType;
×
1752
  uvTask->udfc = task->session->udfc;
×
1753

1754
  if (uvTaskType == UV_TASK_CONNECT) {
×
1755
  } else if (uvTaskType == UV_TASK_REQ_RSP) {
×
1756
    uvTask->pipe = task->session->udfUvPipe;
×
1757
    SUdfRequest request;
1758
    request.type = task->type;
×
1759
    request.seqNum = atomic_fetch_add_64(&gUdfTaskSeqNum, 1);
×
1760

1761
    if (task->type == UDF_TASK_SETUP) {
×
1762
      request.setup = task->_setup.req;
×
1763
      request.type = UDF_TASK_SETUP;
×
1764
    } else if (task->type == UDF_TASK_CALL) {
×
1765
      request.call = task->_call.req;
×
1766
      request.type = UDF_TASK_CALL;
×
1767
    } else if (task->type == UDF_TASK_TEARDOWN) {
×
1768
      request.teardown = task->_teardown.req;
×
1769
      request.type = UDF_TASK_TEARDOWN;
×
1770
    } else {
1771
      fnError("udfc create uv task, invalid task type : %d", task->type);
×
1772
    }
1773
    int32_t bufLen = encodeUdfRequest(NULL, &request);
×
1774
    if (bufLen <= 0) {
×
1775
      fnError("udfc create uv task, encode request failed. size: %d", bufLen);
×
1776
      return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1777
    }
1778
    request.msgLen = bufLen;
×
1779
    void *bufBegin = taosMemoryMalloc(bufLen);
×
1780
    if (bufBegin == NULL) {
×
1781
      fnError("udfc create uv task, malloc buffer failed. size: %d", bufLen);
×
1782
      return terrno;
×
1783
    }
1784
    void *buf = bufBegin;
×
1785
    if (encodeUdfRequest(&buf, &request) <= 0) {
×
1786
      fnError("udfc create uv task, encode request failed. size: %d", bufLen);
×
1787
      taosMemoryFree(bufBegin);
×
1788
      return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1789
    }
1790

1791
    uvTask->reqBuf = uv_buf_init(bufBegin, bufLen);
×
1792
    uvTask->seqNum = request.seqNum;
×
1793
  } else if (uvTaskType == UV_TASK_DISCONNECT) {
×
1794
    uvTask->pipe = task->session->udfUvPipe;
×
1795
  }
1796
  if (uv_sem_init(&uvTask->taskSem, 0) != 0) {
×
1797
    if (uvTaskType == UV_TASK_REQ_RSP) {
×
1798
      taosMemoryFreeClear(uvTask->reqBuf.base);
×
1799
    }
1800
    fnError("udfc create uv task, init semaphore failed.");
×
1801
    return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1802
  }
1803

1804
  return 0;
×
1805
}
1806

1807
int32_t udfcQueueUvTask(SClientUvTaskNode *uvTask) {
×
1808
  fnDebug("queue uv task to event loop, uvTask: %d-%p", uvTask->type, uvTask);
×
1809
  SUdfcProxy *udfc = uvTask->udfc;
×
1810
  uv_mutex_lock(&udfc->taskQueueMutex);
×
1811
  QUEUE_INSERT_TAIL(&udfc->taskQueue, &uvTask->recvTaskQueue);
×
1812
  uv_mutex_unlock(&udfc->taskQueueMutex);
×
1813
  int32_t code = uv_async_send(&udfc->loopTaskAync);
×
1814
  if (code != 0) {
×
1815
    fnError("udfc queue uv task to event loop failed. code:%s", uv_strerror(code));
×
1816
    return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1817
  }
1818

1819
  uv_sem_wait(&uvTask->taskSem);
×
1820
  fnDebug("udfc uvTask finished. uvTask:%" PRId64 "-%d-%p", uvTask->seqNum, uvTask->type, uvTask);
×
1821
  uv_sem_destroy(&uvTask->taskSem);
×
1822

1823
  return 0;
×
1824
}
1825

1826
int32_t udfcStartUvTask(SClientUvTaskNode *uvTask) {
×
1827
  fnDebug("event loop start uv task. uvTask: %" PRId64 "-%d-%p", uvTask->seqNum, uvTask->type, uvTask);
×
1828
  int32_t code = 0;
×
1829

1830
  switch (uvTask->type) {
×
1831
    case UV_TASK_CONNECT: {
×
1832
      uv_pipe_t *pipe = taosMemoryMalloc(sizeof(uv_pipe_t));
×
1833
      if (pipe == NULL) {
×
1834
        fnError("udfc event loop start connect task malloc pipe failed.");
×
1835
        return terrno;
×
1836
      }
1837
      if (uv_pipe_init(&uvTask->udfc->uvLoop, pipe, 0) != 0) {
×
1838
        fnError("udfc event loop start connect task uv_pipe_init failed.");
×
1839
        taosMemoryFree(pipe);
×
1840
        return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
1841
      }
1842
      uvTask->pipe = pipe;
×
1843

1844
      SClientUvConn *conn = taosMemoryCalloc(1, sizeof(SClientUvConn));
×
1845
      if (conn == NULL) {
×
1846
        fnError("udfc event loop start connect task malloc conn failed.");
×
1847
        taosMemoryFree(pipe);
×
1848
        return terrno;
×
1849
      }
1850
      conn->pipe = pipe;
×
1851
      conn->readBuf.len = 0;
×
1852
      conn->readBuf.cap = 0;
×
1853
      conn->readBuf.buf = 0;
×
1854
      conn->readBuf.total = -1;
×
1855
      QUEUE_INIT(&conn->taskQueue);
×
1856

1857
      pipe->data = conn;
×
1858

1859
      uv_connect_t *connReq = taosMemoryMalloc(sizeof(uv_connect_t));
×
1860
      if (connReq == NULL) {
×
1861
        fnError("udfc event loop start connect task malloc connReq failed.");
×
1862
        taosMemoryFree(pipe);
×
1863
        taosMemoryFree(conn);
×
1864
        return terrno;
×
1865
      }
1866
      connReq->data = uvTask;
×
1867
      uv_pipe_connect(connReq, pipe, uvTask->udfc->udfdPipeName, onUdfcPipeConnect);
×
1868
      code = 0;
×
1869
      break;
×
1870
    }
1871
    case UV_TASK_REQ_RSP: {
×
1872
      uv_pipe_t *pipe = uvTask->pipe;
×
1873
      if (pipe == NULL) {
×
1874
        code = TSDB_CODE_UDF_PIPE_NOT_EXIST;
×
1875
      } else {
1876
        uv_write_t *write = taosMemoryMalloc(sizeof(uv_write_t));
×
1877
        if (write == NULL) {
×
1878
          fnError("udfc event loop start req_rsp task malloc write failed.");
×
1879
          return terrno;
×
1880
        }
1881
        write->data = pipe->data;
×
1882
        QUEUE *connTaskQueue = &((SClientUvConn *)pipe->data)->taskQueue;
×
1883
        QUEUE_INSERT_TAIL(connTaskQueue, &uvTask->connTaskQueue);
×
1884
        int32_t err = uv_write(write, (uv_stream_t *)pipe, &uvTask->reqBuf, 1, onUdfcPipeWrite);
×
1885
        if (err != 0) {
×
1886
          taosMemoryFree(write);
×
1887
          fnError("udfc event loop start req_rsp task uv_write failed. uvtask: %p, code:%s", uvTask, uv_strerror(err));
×
1888
        }
1889
        code = err;
×
1890
      }
1891
      break;
×
1892
    }
1893
    case UV_TASK_DISCONNECT: {
×
1894
      uv_pipe_t *pipe = uvTask->pipe;
×
1895
      if (pipe == NULL) {
×
1896
        code = TSDB_CODE_UDF_PIPE_NOT_EXIST;
×
1897
      } else {
1898
        SClientUvConn *conn = pipe->data;
×
1899
        QUEUE_INSERT_TAIL(&conn->taskQueue, &uvTask->connTaskQueue);
×
1900
        if (!uv_is_closing((uv_handle_t *)uvTask->pipe)) {
×
1901
          uv_close((uv_handle_t *)uvTask->pipe, onUdfcPipeClose);
×
1902
        }
1903
        code = 0;
×
1904
      }
1905
      break;
×
1906
    }
1907
    default: {
×
1908
      fnError("udfc event loop unknown task type.") break;
×
1909
    }
1910
  }
1911

1912
  return code;
×
1913
}
1914

1915
void udfcAsyncTaskCb(uv_async_t *async) {
×
1916
  SUdfcProxy *udfc = async->data;
×
1917
  QUEUE       wq;
1918

1919
  uv_mutex_lock(&udfc->taskQueueMutex);
×
1920
  QUEUE_MOVE(&udfc->taskQueue, &wq);
×
1921
  uv_mutex_unlock(&udfc->taskQueueMutex);
×
1922

1923
  while (!QUEUE_EMPTY(&wq)) {
×
1924
    QUEUE *h = QUEUE_HEAD(&wq);
×
1925
    QUEUE_REMOVE(h);
×
1926
    SClientUvTaskNode *task = QUEUE_DATA(h, SClientUvTaskNode, recvTaskQueue);
×
1927
    int32_t            code = udfcStartUvTask(task);
×
1928
    if (code == 0) {
×
1929
      QUEUE_INSERT_TAIL(&udfc->uvProcTaskQueue, &task->procTaskQueue);
×
1930
    } else {
1931
      task->errCode = code;
×
1932
      uv_sem_post(&task->taskSem);
×
1933
    }
1934
  }
1935
}
×
1936

1937
void cleanUpUvTasks(SUdfcProxy *udfc) {
×
1938
  fnDebug("clean up uv tasks") QUEUE wq;
×
1939

1940
  uv_mutex_lock(&udfc->taskQueueMutex);
×
1941
  QUEUE_MOVE(&udfc->taskQueue, &wq);
×
1942
  uv_mutex_unlock(&udfc->taskQueueMutex);
×
1943

1944
  while (!QUEUE_EMPTY(&wq)) {
×
1945
    QUEUE *h = QUEUE_HEAD(&wq);
×
1946
    QUEUE_REMOVE(h);
×
1947
    SClientUvTaskNode *task = QUEUE_DATA(h, SClientUvTaskNode, recvTaskQueue);
×
1948
    if (udfc->udfcState == UDFC_STATE_STOPPING) {
×
1949
      task->errCode = TSDB_CODE_UDF_STOPPING;
×
1950
    }
1951
    uv_sem_post(&task->taskSem);
×
1952
  }
1953

1954
  while (!QUEUE_EMPTY(&udfc->uvProcTaskQueue)) {
×
1955
    QUEUE *h = QUEUE_HEAD(&udfc->uvProcTaskQueue);
×
1956
    QUEUE_REMOVE(h);
×
1957
    SClientUvTaskNode *task = QUEUE_DATA(h, SClientUvTaskNode, procTaskQueue);
×
1958
    if (udfc->udfcState == UDFC_STATE_STOPPING) {
×
1959
      task->errCode = TSDB_CODE_UDF_STOPPING;
×
1960
    }
1961
    uv_sem_post(&task->taskSem);
×
1962
  }
1963
}
×
1964

1965
void udfStopAsyncCb(uv_async_t *async) {
×
1966
  SUdfcProxy *udfc = async->data;
×
1967
  cleanUpUvTasks(udfc);
×
1968
  if (udfc->udfcState == UDFC_STATE_STOPPING) {
×
1969
    uv_stop(&udfc->uvLoop);
×
1970
  }
1971
}
×
1972

1973
void constructUdfService(void *argsThread) {
×
1974
  int32_t     code = 0, lino = 0;
×
1975
  SUdfcProxy *udfc = (SUdfcProxy *)argsThread;
×
1976
  code = uv_loop_init(&udfc->uvLoop);
×
1977
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1978

1979
  code = uv_async_init(&udfc->uvLoop, &udfc->loopTaskAync, udfcAsyncTaskCb);
×
1980
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1981
  udfc->loopTaskAync.data = udfc;
×
1982
  code = uv_async_init(&udfc->uvLoop, &udfc->loopStopAsync, udfStopAsyncCb);
×
1983
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1984
  udfc->loopStopAsync.data = udfc;
×
1985
  code = uv_mutex_init(&udfc->taskQueueMutex);
×
1986
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1987
  QUEUE_INIT(&udfc->taskQueue);
×
1988
  QUEUE_INIT(&udfc->uvProcTaskQueue);
×
1989
  (void)uv_barrier_wait(&udfc->initBarrier);
×
1990
  // TODO return value of uv_run
1991
  int32_t num = uv_run(&udfc->uvLoop, UV_RUN_DEFAULT);
×
1992
  fnInfo("udfc uv loop exit. active handle num: %d", num);
×
1993
  (void)uv_loop_close(&udfc->uvLoop);
×
1994

1995
  uv_walk(&udfc->uvLoop, udfUdfdCloseWalkCb, NULL);
×
1996
  num = uv_run(&udfc->uvLoop, UV_RUN_DEFAULT);
×
1997
  fnInfo("udfc uv loop exit. active handle num: %d", num);
×
1998

1999
  (void)uv_loop_close(&udfc->uvLoop);
×
2000
_exit:
×
2001
  if (code != 0) {
×
2002
    fnError("udfc construct error. code: %d, line: %d", code, lino);
×
2003
  }
2004
  fnInfo("udfc construct finished");
×
2005
}
×
2006

2007
int32_t udfcOpen() {
×
2008
  int32_t code = 0, lino = 0;
×
2009
  int8_t  old = atomic_val_compare_exchange_8(&gUdfcProxy.initialized, 0, 1);
×
2010
  if (old == 1) {
×
2011
    return 0;
×
2012
  }
2013
  SUdfcProxy *proxy = &gUdfcProxy;
×
2014
  getUdfdPipeName(proxy->udfdPipeName, sizeof(proxy->udfdPipeName));
×
2015
  proxy->udfcState = UDFC_STATE_STARTNG;
×
2016
  code = uv_barrier_init(&proxy->initBarrier, 2);
×
2017
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2018
  code = uv_thread_create(&proxy->loopThread, constructUdfService, proxy);
×
2019
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2020
  atomic_store_8(&proxy->udfcState, UDFC_STATE_READY);
×
2021
  proxy->udfcState = UDFC_STATE_READY;
×
2022
  (void)uv_barrier_wait(&proxy->initBarrier);
×
2023
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2024
  code = uv_mutex_init(&proxy->udfStubsMutex);
×
2025
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2026
  proxy->udfStubs = taosArrayInit(8, sizeof(SUdfcFuncStub));
×
2027
  if (proxy->udfStubs == NULL) {
×
2028
    fnError("udfc init failed. udfStubs: %p", proxy->udfStubs);
×
2029
    return -1;
×
2030
  }
2031
  proxy->expiredUdfStubs = taosArrayInit(8, sizeof(SUdfcFuncStub));
×
2032
  if (proxy->expiredUdfStubs == NULL) {
×
2033
    taosArrayDestroy(proxy->udfStubs);
×
2034
    fnError("udfc init failed. expiredUdfStubs: %p", proxy->expiredUdfStubs);
×
2035
    return -1;
×
2036
  }
2037
  code = uv_mutex_init(&proxy->udfcUvMutex);
×
2038
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2039
_exit:
×
2040
  if (code != 0) {
×
2041
    fnError("udfc open error. code: %d, line: %d", code, lino);
×
2042
    return TSDB_CODE_UDF_UV_EXEC_FAILURE;
×
2043
  }
2044
  fnInfo("udfc initialized");
×
2045
  return 0;
×
2046
}
2047

2048
int32_t udfcClose() {
×
2049
  int8_t old = atomic_val_compare_exchange_8(&gUdfcProxy.initialized, 1, 0);
×
2050
  if (old == 0) {
×
2051
    return 0;
×
2052
  }
2053

2054
  SUdfcProxy *udfc = &gUdfcProxy;
×
2055
  udfc->udfcState = UDFC_STATE_STOPPING;
×
2056
  if (uv_async_send(&udfc->loopStopAsync) != 0) {
×
2057
    fnError("udfc close error to send stop async");
×
2058
  }
2059
  if (uv_thread_join(&udfc->loopThread) != 0) {
×
2060
    fnError("udfc close errir to join loop thread");
×
2061
  }
2062
  uv_mutex_destroy(&udfc->taskQueueMutex);
×
2063
  uv_barrier_destroy(&udfc->initBarrier);
×
2064
  taosArrayDestroy(udfc->expiredUdfStubs);
×
2065
  taosArrayDestroy(udfc->udfStubs);
×
2066
  uv_mutex_destroy(&udfc->udfStubsMutex);
×
2067
  uv_mutex_destroy(&udfc->udfcUvMutex);
×
2068
  udfc->udfcState = UDFC_STATE_INITAL;
×
2069
  fnInfo("udfc is cleaned up");
×
2070
  return 0;
×
2071
}
2072

2073
int32_t udfcRunUdfUvTask(SClientUdfTask *task, int8_t uvTaskType) {
×
2074
  int32_t            code = 0, lino = 0;
×
2075
  SClientUvTaskNode *uvTask = taosMemoryCalloc(1, sizeof(SClientUvTaskNode));
×
2076
  if (uvTask == NULL) {
×
2077
    fnError("udfc client task: %p failed to allocate memory for uvTask", task);
×
2078
    return terrno;
×
2079
  }
2080
  fnDebug("udfc client task: %p created uvTask: %p. pipe: %p", task, uvTask, task->session->udfUvPipe);
×
2081

2082
  code = udfcInitializeUvTask(task, uvTaskType, uvTask);
×
2083
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2084
  code = udfcQueueUvTask(uvTask);
×
2085
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2086
  code = udfcGetUdfTaskResultFromUvTask(task, uvTask);
×
2087
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2088
  if (uvTaskType == UV_TASK_CONNECT) {
×
2089
    task->session->udfUvPipe = uvTask->pipe;
×
2090
    SClientUvConn *conn = uvTask->pipe->data;
×
2091
    conn->session = task->session;
×
2092
  }
2093

2094
_exit:
×
2095
  if (code != 0) {
×
2096
    fnError("udfc run udf uv task failure. task: %p, uvTask: %p, err: %d, line: %d", task, uvTask, code, lino);
×
2097
  }
2098
  taosMemoryFree(uvTask->reqBuf.base);
×
2099
  uvTask->reqBuf.base = NULL;
×
2100
  taosMemoryFree(uvTask);
×
2101
  uvTask = NULL;
×
2102
  return code;
×
2103
}
2104

2105
int32_t doSetupUdf(char udfName[], UdfcFuncHandle *funcHandle) {
×
2106
  int32_t         code = TSDB_CODE_SUCCESS, lino = 0;
×
2107
  SClientUdfTask *task = taosMemoryCalloc(1, sizeof(SClientUdfTask));
×
2108
  if (task == NULL) {
×
2109
    fnError("doSetupUdf, failed to allocate memory for task");
×
2110
    return terrno;
×
2111
  }
2112
  task->session = taosMemoryCalloc(1, sizeof(SUdfcUvSession));
×
2113
  if (task->session == NULL) {
×
2114
    fnError("doSetupUdf, failed to allocate memory for session");
×
2115
    taosMemoryFree(task);
×
2116
    return terrno;
×
2117
  }
2118
  task->session->udfc = &gUdfcProxy;
×
2119
  task->type = UDF_TASK_SETUP;
×
2120

2121
  SUdfSetupRequest *req = &task->_setup.req;
×
2122
  tstrncpy(req->udfName, udfName, TSDB_FUNC_NAME_LEN);
×
2123

2124
  code = udfcRunUdfUvTask(task, UV_TASK_CONNECT);
×
2125
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2126

2127
  code = udfcRunUdfUvTask(task, UV_TASK_REQ_RSP);
×
2128
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2129

2130
  SUdfSetupResponse *rsp = &task->_setup.rsp;
×
2131
  task->session->severHandle = rsp->udfHandle;
×
2132
  task->session->outputType = rsp->outputType;
×
2133
  task->session->bytes = rsp->bytes;
×
2134
  task->session->bufSize = rsp->bufSize;
×
2135
  tstrncpy(task->session->udfName, udfName, TSDB_FUNC_NAME_LEN);
×
2136
  fnInfo("successfully setup udf func handle. udfName: %s, handle: %p", udfName, task->session);
×
2137
  *funcHandle = task->session;
×
2138
  taosMemoryFree(task);
×
2139
  return 0;
×
2140

2141
_exit:
×
2142
  if (code != 0) {
×
2143
    fnError("failed to setup udf. udfname: %s, err: %d line:%d", udfName, code, lino);
×
2144
  }
2145
  taosMemoryFree(task->session);
×
2146
  taosMemoryFree(task);
×
2147
  return code;
×
2148
}
2149

2150
int32_t callUdf(UdfcFuncHandle handle, int8_t callType, SSDataBlock *input, SUdfInterBuf *state, SUdfInterBuf *state2,
×
2151
                SSDataBlock *output, SUdfInterBuf *newState) {
2152
  fnDebug("udfc call udf. callType: %d, funcHandle: %p", callType, handle);
×
2153
  SUdfcUvSession *session = (SUdfcUvSession *)handle;
×
2154
  if (session->udfUvPipe == NULL) {
×
2155
    fnError("No pipe to taosudf");
×
2156
    return TSDB_CODE_UDF_PIPE_NOT_EXIST;
×
2157
  }
2158
  SClientUdfTask *task = taosMemoryCalloc(1, sizeof(SClientUdfTask));
×
2159
  if (task == NULL) {
×
2160
    fnError("udfc call udf. failed to allocate memory for task");
×
2161
    return terrno;
×
2162
  }
2163
  task->session = (SUdfcUvSession *)handle;
×
2164
  task->type = UDF_TASK_CALL;
×
2165

2166
  SUdfCallRequest *req = &task->_call.req;
×
2167
  req->udfHandle = task->session->severHandle;
×
2168
  req->callType = callType;
×
2169

2170
  switch (callType) {
×
2171
    case TSDB_UDF_CALL_AGG_INIT: {
×
2172
      req->initFirst = 1;
×
2173
      break;
×
2174
    }
2175
    case TSDB_UDF_CALL_AGG_PROC: {
×
2176
      req->block = *input;
×
2177
      req->interBuf = *state;
×
2178
      break;
×
2179
    }
2180
    // case TSDB_UDF_CALL_AGG_MERGE: {
2181
    //   req->interBuf = *state;
2182
    //   req->interBuf2 = *state2;
2183
    //   break;
2184
    // }
2185
    case TSDB_UDF_CALL_AGG_FIN: {
×
2186
      req->interBuf = *state;
×
2187
      break;
×
2188
    }
2189
    case TSDB_UDF_CALL_SCALA_PROC: {
×
2190
      req->block = *input;
×
2191
      break;
×
2192
    }
2193
  }
2194

2195
  int32_t code = udfcRunUdfUvTask(task, UV_TASK_REQ_RSP);
×
2196
  if (code != 0) {
×
2197
    fnError("call udf failure. udfcRunUdfUvTask err: %d", code);
×
2198
  } else {
2199
    SUdfCallResponse *rsp = &task->_call.rsp;
×
2200
    switch (callType) {
×
2201
      case TSDB_UDF_CALL_AGG_INIT: {
×
2202
        *newState = rsp->resultBuf;
×
2203
        break;
×
2204
      }
2205
      case TSDB_UDF_CALL_AGG_PROC: {
×
2206
        *newState = rsp->resultBuf;
×
2207
        break;
×
2208
      }
2209
      // case TSDB_UDF_CALL_AGG_MERGE: {
2210
      //   *newState = rsp->resultBuf;
2211
      //   break;
2212
      // }
2213
      case TSDB_UDF_CALL_AGG_FIN: {
×
2214
        *newState = rsp->resultBuf;
×
2215
        break;
×
2216
      }
2217
      case TSDB_UDF_CALL_SCALA_PROC: {
×
2218
        *output = rsp->resultData;
×
2219
        break;
×
2220
      }
2221
    }
2222
  }
2223
  taosMemoryFree(task);
×
2224
  return code;
×
2225
}
2226

2227
int32_t doCallUdfAggInit(UdfcFuncHandle handle, SUdfInterBuf *interBuf) {
×
2228
  int8_t callType = TSDB_UDF_CALL_AGG_INIT;
×
2229

2230
  int32_t err = callUdf(handle, callType, NULL, NULL, NULL, NULL, interBuf);
×
2231

2232
  return err;
×
2233
}
2234

2235
// input: block, state
2236
// output: interbuf,
2237
int32_t doCallUdfAggProcess(UdfcFuncHandle handle, SSDataBlock *block, SUdfInterBuf *state, SUdfInterBuf *newState) {
×
2238
  int8_t  callType = TSDB_UDF_CALL_AGG_PROC;
×
2239
  int32_t err = callUdf(handle, callType, block, state, NULL, NULL, newState);
×
2240
  return err;
×
2241
}
2242

2243
// input: interbuf1, interbuf2
2244
// output: resultBuf
2245
// udf todo:  aggmerge
2246
// int32_t doCallUdfAggMerge(UdfcFuncHandle handle, SUdfInterBuf *interBuf1, SUdfInterBuf *interBuf2,
2247
//                           SUdfInterBuf *resultBuf) {
2248
//   int8_t  callType = TSDB_UDF_CALL_AGG_MERGE;
2249
//   int32_t err = callUdf(handle, callType, NULL, interBuf1, interBuf2, NULL, resultBuf);
2250
//   return err;
2251
// }
2252

2253
// input: interBuf
2254
// output: resultData
2255
int32_t doCallUdfAggFinalize(UdfcFuncHandle handle, SUdfInterBuf *interBuf, SUdfInterBuf *resultData) {
×
2256
  int8_t  callType = TSDB_UDF_CALL_AGG_FIN;
×
2257
  int32_t err = callUdf(handle, callType, NULL, interBuf, NULL, NULL, resultData);
×
2258
  return err;
×
2259
}
2260

2261
int32_t doCallUdfScalarFunc(UdfcFuncHandle handle, SScalarParam *input, int32_t numOfCols, SScalarParam *output) {
×
2262
  int8_t      callType = TSDB_UDF_CALL_SCALA_PROC;
×
2263
  SSDataBlock inputBlock = {0};
×
2264
  int32_t     code = convertScalarParamToDataBlock(input, numOfCols, &inputBlock);
×
2265
  if (code != 0) {
×
2266
    fnError("doCallUdfScalarFunc, convertScalarParamToDataBlock failed. code: %d", code);
×
2267
    return code;
×
2268
  }
2269
  SSDataBlock resultBlock = {0};
×
2270
  int32_t     err = callUdf(handle, callType, &inputBlock, NULL, NULL, &resultBlock, NULL);
×
2271
  if (err == 0) {
×
2272
    err = convertDataBlockToScalarParm(&resultBlock, output);
×
2273
    taosArrayDestroy(resultBlock.pDataBlock);
×
2274
  }
2275

2276
  blockDataFreeRes(&inputBlock);
×
2277
  return err;
×
2278
}
2279

2280
int32_t doTeardownUdf(UdfcFuncHandle handle) {
×
2281
  int32_t         code = TSDB_CODE_SUCCESS, lino = 0;
×
2282
  SUdfcUvSession *session = (SUdfcUvSession *)handle;
×
2283

2284
  if (session->udfUvPipe == NULL) {
×
2285
    fnError("tear down udf. pipe to taosudf does not exist. udf name: %s", session->udfName);
×
2286
    taosMemoryFree(session);
×
2287
    return TSDB_CODE_UDF_PIPE_NOT_EXIST;
×
2288
  }
2289

2290
  SClientUdfTask *task = taosMemoryCalloc(1, sizeof(SClientUdfTask));
×
2291
  if (task == NULL) {
×
2292
    fnError("doTeardownUdf, failed to allocate memory for task");
×
2293
    taosMemoryFree(session);
×
2294
    return terrno;
×
2295
  }
2296
  task->session = session;
×
2297
  task->type = UDF_TASK_TEARDOWN;
×
2298

2299
  SUdfTeardownRequest *req = &task->_teardown.req;
×
2300
  req->udfHandle = task->session->severHandle;
×
2301

2302
  code = udfcRunUdfUvTask(task, UV_TASK_REQ_RSP);
×
2303
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2304

2305
  code = udfcRunUdfUvTask(task, UV_TASK_DISCONNECT);
×
2306
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
2307

2308
  fnInfo("tear down udf. udf name: %s, udf func handle: %p", session->udfName, handle);
×
2309
  // TODO: synchronization refactor between libuv event loop and request thread
2310
  uv_mutex_lock(&gUdfcProxy.udfcUvMutex);
×
2311
  if (session->udfUvPipe != NULL && session->udfUvPipe->data != NULL) {
×
2312
    SClientUvConn *conn = session->udfUvPipe->data;
×
2313
    conn->session = NULL;
×
2314
  }
2315
  uv_mutex_unlock(&gUdfcProxy.udfcUvMutex);
×
2316

2317
_exit:
×
2318
  if (code != 0) {
×
2319
    fnError("failed to teardown udf. udf name: %s, err: %d, line: %d", session->udfName, code, lino);
×
2320
  }
2321
  taosMemoryFree(session);
×
2322
  taosMemoryFree(task);
×
2323

2324
  return code;
×
2325
}
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