Merge pull request #10978 from taosdata/szhou/3.0_udfd
first pull request of udfd. a lot of improvements shall follow this.
This commit is contained in:
commit
9f04cfe00e
|
@ -1,12 +1,55 @@
|
|||
aux_source_directory(src FUNCTION_SRC)
|
||||
list(REMOVE_ITEM FUNCTION_SRC src/udfd.c)
|
||||
add_library(function STATIC ${FUNCTION_SRC})
|
||||
target_include_directories(
|
||||
function
|
||||
PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/function"
|
||||
PUBLIC
|
||||
"${CMAKE_SOURCE_DIR}/include/libs/function"
|
||||
"${CMAKE_SOURCE_DIR}/contrib/libuv/include"
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc"
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
function
|
||||
PUBLIC uv_a
|
||||
PRIVATE os util common nodes
|
||||
)
|
||||
|
||||
add_executable(runUdf test/runUdf.c)
|
||||
target_include_directories(
|
||||
runUdf
|
||||
PUBLIC
|
||||
"${CMAKE_SOURCE_DIR}/include/libs/function"
|
||||
"${CMAKE_SOURCE_DIR}/contrib/libuv/include"
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc"
|
||||
)
|
||||
target_link_libraries(
|
||||
runUdf
|
||||
PUBLIC uv_a
|
||||
PRIVATE os util common nodes function
|
||||
)
|
||||
|
||||
add_library(udf1 MODULE test/udf1.c)
|
||||
target_include_directories(
|
||||
udf1
|
||||
PUBLIC
|
||||
"${CMAKE_SOURCE_DIR}/include/libs/function"
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc"
|
||||
)
|
||||
|
||||
#SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/build/bin)
|
||||
add_executable(udfd src/udfd.c)
|
||||
target_include_directories(
|
||||
udfd
|
||||
PUBLIC
|
||||
"${CMAKE_SOURCE_DIR}/include/libs/function"
|
||||
"${CMAKE_SOURCE_DIR}/contrib/libuv/include"
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc"
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
udfd
|
||||
PUBLIC uv_a
|
||||
PRIVATE os util common nodes function
|
||||
)
|
||||
|
||||
|
|
|
@ -20,68 +20,116 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "os.h"
|
||||
#include "taoserror.h"
|
||||
//======================================================================================
|
||||
//begin API to taosd and qworker
|
||||
/**
|
||||
* start udf dameon service
|
||||
* @return error code
|
||||
*/
|
||||
int32_t startUdfService();
|
||||
|
||||
/**
|
||||
* stop udf dameon service
|
||||
* @return error code
|
||||
*/
|
||||
int32_t stopUdfService();
|
||||
|
||||
enum {
|
||||
TSDB_UDF_FUNC_NORMAL = 0,
|
||||
TSDB_UDF_FUNC_INIT,
|
||||
TSDB_UDF_FUNC_FINALIZE,
|
||||
TSDB_UDF_FUNC_MERGE,
|
||||
TSDB_UDF_FUNC_DESTROY,
|
||||
TSDB_UDF_FUNC_MAX_NUM
|
||||
TSDB_UDF_TYPE_SCALAR = 0,
|
||||
TSDB_UDF_TYPE_AGGREGATE = 1
|
||||
};
|
||||
|
||||
typedef struct SUdfInit {
|
||||
int32_t maybe_null; /* 1 if function can return NULL */
|
||||
uint32_t decimals; /* for real functions */
|
||||
uint64_t length; /* For string functions */
|
||||
char* ptr; /* free pointer for function data */
|
||||
int32_t const_item; /* 0 if result is independent of arguments */
|
||||
|
||||
// script like lua/javascript
|
||||
void* script_ctx;
|
||||
void (*destroyCtxFunc)(void* script_ctx);
|
||||
} SUdfInit;
|
||||
enum {
|
||||
TSDB_UDF_SCRIPT_BIN_LIB = 0,
|
||||
TSDB_UDF_SCRIPT_LUA = 1,
|
||||
};
|
||||
|
||||
typedef struct SUdfInfo {
|
||||
int32_t functionId; // system assigned function id
|
||||
int32_t funcType; // scalar function or aggregate function
|
||||
char *udfName; // function name
|
||||
int32_t udfType; // scalar function or aggregate function
|
||||
int8_t scriptType;
|
||||
char *path;
|
||||
|
||||
int8_t resType; // result type
|
||||
int16_t resBytes; // result byte
|
||||
int32_t contLen; // content length
|
||||
int32_t bufSize; // interbuf size
|
||||
char* name; // function name
|
||||
void* handle; // handle loaded in mem
|
||||
void* funcs[TSDB_UDF_FUNC_MAX_NUM]; // function ptr
|
||||
int32_t bufSize; //interbuf size
|
||||
|
||||
// for script like lua/javascript only
|
||||
int isScript;
|
||||
void* pScriptCtx;
|
||||
|
||||
SUdfInit init;
|
||||
char* content;
|
||||
char* path;
|
||||
} SUdfInfo;
|
||||
|
||||
typedef void *UdfHandle;
|
||||
|
||||
/**
|
||||
* setup udf
|
||||
* @param udf, in
|
||||
* @param handle, out
|
||||
* @return error code
|
||||
*/
|
||||
int32_t setupUdf(SUdfInfo* udf, UdfHandle *handle);
|
||||
|
||||
|
||||
enum {
|
||||
TSDB_UDF_STEP_NORMAL = 0,
|
||||
TSDB_UDF_STEP_MERGE,
|
||||
TSDb_UDF_STEP_FINALIZE,
|
||||
TSDB_UDF_STEP_MAX_NUM
|
||||
};
|
||||
/**
|
||||
* call udf
|
||||
* @param handle udf handle
|
||||
* @param step
|
||||
* @param state
|
||||
* @param stateSize
|
||||
* @param input
|
||||
* @param newstate
|
||||
* @param newStateSize
|
||||
* @param output
|
||||
* @return error code
|
||||
*/
|
||||
|
||||
//TODO: must change the following after metadata flow and data flow between qworker and udfd is well defined
|
||||
typedef struct SUdfDataBlock {
|
||||
char* data;
|
||||
int32_t size;
|
||||
} SUdfDataBlock;
|
||||
|
||||
int32_t callUdf(UdfHandle handle, int8_t step, char *state, int32_t stateSize, SUdfDataBlock input, char **newstate,
|
||||
int32_t *newStateSize, SUdfDataBlock *output);
|
||||
|
||||
/**
|
||||
* tearn down udf
|
||||
* @param handle
|
||||
* @return
|
||||
*/
|
||||
int32_t teardownUdf(UdfHandle handle);
|
||||
|
||||
// end API to taosd and qworker
|
||||
//=============================================================================================================================
|
||||
// TODO: Must change
|
||||
// begin API to UDF writer.
|
||||
|
||||
// script
|
||||
|
||||
typedef int32_t (*scriptInitFunc)(void* pCtx);
|
||||
typedef void (*scriptNormalFunc)(void* pCtx, char* data, int16_t iType, int16_t iBytes, int32_t numOfRows,
|
||||
int64_t* ptList, int64_t key, char* dataOutput, char* tsOutput, int32_t* numOfOutput,
|
||||
int16_t oType, int16_t oBytes);
|
||||
typedef void (*scriptFinalizeFunc)(void* pCtx, int64_t key, char* dataOutput, int32_t* numOfOutput);
|
||||
typedef void (*scriptMergeFunc)(void* pCtx, char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput);
|
||||
typedef void (*scriptDestroyFunc)(void* pCtx);
|
||||
//typedef int32_t (*scriptInitFunc)(void* pCtx);
|
||||
//typedef void (*scriptNormalFunc)(void* pCtx, char* data, int16_t iType, int16_t iBytes, int32_t numOfRows,
|
||||
// int64_t* ptList, int64_t key, char* dataOutput, char* tsOutput, int32_t* numOfOutput,
|
||||
// int16_t oType, int16_t oBytes);
|
||||
//typedef void (*scriptFinalizeFunc)(void* pCtx, int64_t key, char* dataOutput, int32_t* numOfOutput);
|
||||
//typedef void (*scriptMergeFunc)(void* pCtx, char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput);
|
||||
//typedef void (*scriptDestroyFunc)(void* pCtx);
|
||||
|
||||
// dynamic lib
|
||||
typedef void (*udfNormalFunc)(char* data, int16_t itype, int16_t iBytes, int32_t numOfRows, int64_t* ts,
|
||||
char* dataOutput, char* interBuf, char* tsOutput, int32_t* numOfOutput, int16_t oType,
|
||||
int16_t oBytes, SUdfInit* buf);
|
||||
typedef int32_t (*udfInitFunc)(SUdfInit* data);
|
||||
typedef void (*udfFinalizeFunc)(char* dataOutput, char* interBuf, int32_t* numOfOutput, SUdfInit* buf);
|
||||
typedef void (*udfMergeFunc)(char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput, SUdfInit* buf);
|
||||
typedef void (*udfDestroyFunc)(SUdfInit* buf);
|
||||
typedef int32_t (*TUdfInitFunc)();
|
||||
typedef void (*TUdfDestroyFunc)();
|
||||
|
||||
typedef void (*TUdfFunc)(int8_t step,
|
||||
char *state, int32_t stateSize, SUdfDataBlock input,
|
||||
char **newstate, int32_t *newStateSize, SUdfDataBlock *output);
|
||||
|
||||
//typedef void (*udfMergeFunc)(char *data, int32_t numOfRows, char *dataOutput, int32_t* numOfOutput);
|
||||
//typedef void (*udfFinalizeFunc)(char* state, int32_t stateSize, SUdfDataBlock *output);
|
||||
|
||||
// end API to UDF writer
|
||||
//=======================================================================================================================
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TDENGINE_TUDF_INT_H
|
||||
#define TDENGINE_TUDF_INT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//TODO replaces them with fnDebug
|
||||
//#define debugPrint(...) taosPrintLog("Function", DEBUG_INFO, 135, __VA_ARGS__)
|
||||
#define debugPrint(...) {fprintf(stderr, __VA_ARGS__);fprintf(stderr, "\n");}
|
||||
enum {
|
||||
UDF_TASK_SETUP = 0,
|
||||
UDF_TASK_CALL = 1,
|
||||
UDF_TASK_TEARDOWN = 2
|
||||
|
||||
};
|
||||
|
||||
typedef struct SUdfSetupRequest {
|
||||
char udfName[16]; //
|
||||
int8_t scriptType; // 0:c, 1: lua, 2:js
|
||||
int8_t udfType; //udaf, udf
|
||||
int16_t pathSize;
|
||||
char *path;
|
||||
} SUdfSetupRequest;
|
||||
|
||||
typedef struct SUdfSetupResponse {
|
||||
int64_t udfHandle;
|
||||
} SUdfSetupResponse;
|
||||
|
||||
|
||||
typedef struct SUdfCallRequest {
|
||||
int64_t udfHandle;
|
||||
int8_t step;
|
||||
|
||||
int32_t inputBytes;
|
||||
char *input;
|
||||
|
||||
int32_t stateBytes;
|
||||
char *state;
|
||||
} SUdfCallRequest;
|
||||
|
||||
|
||||
typedef struct SUdfCallResponse {
|
||||
int32_t outputBytes;
|
||||
char *output;
|
||||
int32_t newStateBytes;
|
||||
char *newState;
|
||||
} SUdfCallResponse;
|
||||
|
||||
|
||||
typedef struct SUdfTeardownRequest {
|
||||
int64_t udfHandle;
|
||||
} SUdfTeardownRequest;
|
||||
|
||||
|
||||
typedef struct SUdfTeardownResponse {
|
||||
} SUdfTeardownResponse;
|
||||
|
||||
typedef struct SUdfRequest {
|
||||
int32_t msgLen;
|
||||
int64_t seqNum;
|
||||
|
||||
int8_t type;
|
||||
void *subReq;
|
||||
} SUdfRequest;
|
||||
|
||||
typedef struct SUdfResponse {
|
||||
int32_t msgLen;
|
||||
int64_t seqNum;
|
||||
|
||||
int8_t type;
|
||||
int32_t code;
|
||||
void *subRsp;
|
||||
} SUdfResponse;
|
||||
|
||||
int32_t decodeRequest(char *buf, int32_t bufLen, SUdfRequest **pRequest);
|
||||
int32_t encodeResponse(char **buf, int32_t *bufLen, SUdfResponse *response);
|
||||
int32_t encodeRequest(char **buf, int32_t *bufLen, SUdfRequest *request);
|
||||
int32_t decodeResponse(char *buf, int32_t bufLen, SUdfResponse **pResponse);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TDENGINE_TUDF_INT_H
|
|
@ -0,0 +1,110 @@
|
|||
//
|
||||
// Created by shenglian on 28/02/22.
|
||||
//
|
||||
|
||||
#ifndef UDF_UDF_H
|
||||
#define UDF_UDF_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#define DEBUG
|
||||
#ifdef DEBUG
|
||||
#define debugPrint(...) fprintf(__VA_ARGS__)
|
||||
#else
|
||||
#define debugPrint(...) /**/
|
||||
#endif
|
||||
|
||||
enum {
|
||||
UDF_TASK_SETUP = 0,
|
||||
UDF_TASK_CALL = 1,
|
||||
UDF_TASK_TEARDOWN = 2
|
||||
|
||||
};
|
||||
|
||||
typedef struct SSDataBlock{
|
||||
char *data;
|
||||
int32_t size;
|
||||
} SSDataBlock;
|
||||
|
||||
typedef struct SUdfInfo {
|
||||
char *udfName;
|
||||
char *path;
|
||||
} SUdfInfo;
|
||||
|
||||
typedef void *UdfHandle;
|
||||
|
||||
int32_t startUdfService();
|
||||
|
||||
int32_t stopUdfService();
|
||||
|
||||
//int32_t setupUdf(SUdfInfo *udf, int32_t numOfUdfs, UdfHandle *handles);
|
||||
|
||||
int32_t setupUdf(SUdfInfo* udf, UdfHandle* handle);
|
||||
|
||||
int32_t callUdf(UdfHandle handle, int8_t step, char *state, int32_t stateSize, SSDataBlock input, char **newstate,
|
||||
int32_t *newStateSize, SSDataBlock *output);
|
||||
|
||||
int32_t teardownUdf(UdfHandle handle);
|
||||
|
||||
typedef struct SUdfSetupRequest {
|
||||
char udfName[16]; //
|
||||
int8_t scriptType; // 0:c, 1: lua, 2:js
|
||||
int8_t udfType; //udaf, udf, udtf
|
||||
int16_t pathSize;
|
||||
char *path;
|
||||
} SUdfSetupRequest;
|
||||
|
||||
typedef struct SUdfSetupResponse {
|
||||
int64_t udfHandle;
|
||||
} SUdfSetupResponse;
|
||||
|
||||
|
||||
typedef struct SUdfCallRequest {
|
||||
int64_t udfHandle;
|
||||
int8_t step;
|
||||
|
||||
int32_t inputBytes;
|
||||
char *input;
|
||||
|
||||
int32_t stateBytes;
|
||||
char *state;
|
||||
} SUdfCallRequest;
|
||||
|
||||
|
||||
typedef struct SUdfCallResponse {
|
||||
int32_t outputBytes;
|
||||
char *output;
|
||||
int32_t newStateBytes;
|
||||
char *newState;
|
||||
} SUdfCallResponse;
|
||||
|
||||
|
||||
typedef struct SUdfTeardownRequest {
|
||||
int64_t udfHandle;
|
||||
} SUdfTeardownRequest;
|
||||
|
||||
|
||||
typedef struct SUdfTeardownResponse {
|
||||
} SUdfTeardownResponse;
|
||||
|
||||
typedef struct SUdfRequest {
|
||||
int32_t msgLen;
|
||||
int64_t seqNum;
|
||||
|
||||
int8_t type;
|
||||
void *subReq;
|
||||
} SUdfRequest;
|
||||
|
||||
typedef struct SUdfResponse {
|
||||
int32_t msgLen;
|
||||
int64_t seqNum;
|
||||
|
||||
int8_t type;
|
||||
int32_t code;
|
||||
void *subRsp;
|
||||
} SUdfResponse;
|
||||
|
||||
int32_t decodeRequest(char *buf, int32_t bufLen, SUdfRequest **pRequest);
|
||||
int32_t encodeResponse(char **buf, int32_t *bufLen, SUdfResponse *response);
|
||||
int32_t encodeRequest(char **buf, int32_t *bufLen, SUdfRequest *request);
|
||||
int32_t decodeResponse(char *buf, int32_t bufLen, SUdfResponse **pResponse);
|
||||
#endif //UDF_UDF_H
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,356 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "uv.h"
|
||||
#include "os.h"
|
||||
#include "tlog.h"
|
||||
|
||||
#include "tudf.h"
|
||||
#include "tudfInt.h"
|
||||
|
||||
|
||||
static uv_loop_t *loop;
|
||||
|
||||
typedef struct SUdfdUvConn {
|
||||
uv_stream_t *client;
|
||||
char *inputBuf;
|
||||
int32_t inputLen;
|
||||
int32_t inputCap;
|
||||
int32_t inputTotal;
|
||||
} SUdfdUvConn;
|
||||
|
||||
typedef struct SUvUdfWork {
|
||||
uv_stream_t *client;
|
||||
uv_buf_t input;
|
||||
uv_buf_t output;
|
||||
} SUvUdfWork;
|
||||
|
||||
typedef struct SUdf {
|
||||
int32_t refCount;
|
||||
|
||||
char name[16];
|
||||
int8_t type;
|
||||
|
||||
uv_lib_t lib;
|
||||
TUdfFunc normalFunc;
|
||||
} SUdf;
|
||||
|
||||
//TODO: low priority: change name onxxx to xxxCb, and udfc or udfd as prefix
|
||||
//TODO: add private udf structure.
|
||||
typedef struct SUdfHandle {
|
||||
SUdf *udf;
|
||||
} SUdfHandle;
|
||||
|
||||
|
||||
void udfdProcessRequest(uv_work_t *req) {
|
||||
SUvUdfWork *uvUdf = (SUvUdfWork *) (req->data);
|
||||
SUdfRequest *request = NULL;
|
||||
decodeRequest(uvUdf->input.base, uvUdf->input.len, &request);
|
||||
|
||||
switch (request->type) {
|
||||
case UDF_TASK_SETUP: {
|
||||
debugPrint("%s", "process setup request");
|
||||
SUdf *udf = malloc(sizeof(SUdf));
|
||||
udf->refCount = 0;
|
||||
SUdfSetupRequest *setup = request->subReq;
|
||||
strcpy(udf->name, setup->udfName);
|
||||
int err = uv_dlopen(setup->path, &udf->lib);
|
||||
if (err != 0) {
|
||||
debugPrint("can not load library %s. error: %s", setup->path, uv_strerror(err));
|
||||
//TODO set error
|
||||
}
|
||||
|
||||
char normalFuncName[32] = {0};
|
||||
strcpy(normalFuncName, setup->udfName);
|
||||
//TODO error,
|
||||
//TODO find all functions normal, init, destroy, normal, merge, finalize
|
||||
uv_dlsym(&udf->lib, normalFuncName, (void **) (&udf->normalFunc));
|
||||
|
||||
SUdfHandle *handle = malloc(sizeof(SUdfHandle));
|
||||
handle->udf = udf;
|
||||
udf->refCount++;
|
||||
//TODO: allocate private structure and call init function and set it to handle
|
||||
SUdfResponse *rsp = malloc(sizeof(SUdfResponse));
|
||||
rsp->seqNum = request->seqNum;
|
||||
rsp->type = request->type;
|
||||
rsp->code = 0;
|
||||
SUdfSetupResponse *subRsp = malloc(sizeof(SUdfSetupResponse));
|
||||
subRsp->udfHandle = (int64_t) (handle);
|
||||
rsp->subRsp = subRsp;
|
||||
char *buf;
|
||||
int32_t len;
|
||||
encodeResponse(&buf, &len, rsp);
|
||||
|
||||
uvUdf->output = uv_buf_init(buf, len);
|
||||
|
||||
free(rsp->subRsp);
|
||||
free(rsp);
|
||||
free(request->subReq);
|
||||
free(request);
|
||||
free(uvUdf->input.base);
|
||||
break;
|
||||
}
|
||||
|
||||
case UDF_TASK_CALL: {
|
||||
debugPrint("%s", "process call request");
|
||||
SUdfCallRequest *call = request->subReq;
|
||||
SUdfHandle *handle = (SUdfHandle *) (call->udfHandle);
|
||||
SUdf *udf = handle->udf;
|
||||
char *newState;
|
||||
int32_t newStateSize;
|
||||
SUdfDataBlock input = {.data = call->input, .size= call->inputBytes};
|
||||
SUdfDataBlock output;
|
||||
//TODO: call different functions according to the step
|
||||
udf->normalFunc(call->step, call->state, call->stateBytes, input, &newState, &newStateSize, &output);
|
||||
|
||||
SUdfResponse *rsp = malloc(sizeof(SUdfResponse));
|
||||
rsp->seqNum = request->seqNum;
|
||||
rsp->type = request->type;
|
||||
rsp->code = 0;
|
||||
SUdfCallResponse *subRsp = malloc(sizeof(SUdfCallResponse));
|
||||
subRsp->outputBytes = output.size;
|
||||
subRsp->output = output.data;
|
||||
subRsp->newStateBytes = newStateSize;
|
||||
subRsp->newState = newState;
|
||||
rsp->subRsp = subRsp;
|
||||
|
||||
char *buf;
|
||||
int32_t len;
|
||||
encodeResponse(&buf, &len, rsp);
|
||||
uvUdf->output = uv_buf_init(buf, len);
|
||||
|
||||
free(rsp->subRsp);
|
||||
free(rsp);
|
||||
free(newState);
|
||||
free(output.data);
|
||||
free(request->subReq);
|
||||
free(request);
|
||||
free(uvUdf->input.base);
|
||||
break;
|
||||
}
|
||||
case UDF_TASK_TEARDOWN: {
|
||||
debugPrint("%s", "process teardown request");
|
||||
|
||||
SUdfTeardownRequest *teardown = request->subReq;
|
||||
SUdfHandle *handle = (SUdfHandle *) (teardown->udfHandle);
|
||||
SUdf *udf = handle->udf;
|
||||
udf->refCount--;
|
||||
if (udf->refCount == 0) {
|
||||
uv_dlclose(&udf->lib);
|
||||
}
|
||||
free(udf);
|
||||
//TODO: call destroy and free udf private
|
||||
free(handle);
|
||||
|
||||
SUdfResponse *rsp = malloc(sizeof(SUdfResponse));
|
||||
rsp->seqNum = request->seqNum;
|
||||
rsp->type = request->type;
|
||||
rsp->code = 0;
|
||||
SUdfTeardownResponse *subRsp = malloc(sizeof(SUdfTeardownResponse));
|
||||
rsp->subRsp = subRsp;
|
||||
char *buf;
|
||||
int32_t len;
|
||||
encodeResponse(&buf, &len, rsp);
|
||||
uvUdf->output = uv_buf_init(buf, len);
|
||||
|
||||
free(rsp->subRsp);
|
||||
free(rsp);
|
||||
free(request->subReq);
|
||||
free(request);
|
||||
free(uvUdf->input.base);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void udfdOnWrite(uv_write_t *req, int status) {
|
||||
debugPrint("%s", "after writing to pipe");
|
||||
if (status < 0) {
|
||||
debugPrint("Write error %s", uv_err_name(status));
|
||||
}
|
||||
SUvUdfWork *work = (SUvUdfWork *) req->data;
|
||||
debugPrint("\tlength: %zu", work->output.len);
|
||||
free(work->output.base);
|
||||
free(work);
|
||||
free(req);
|
||||
}
|
||||
|
||||
|
||||
void udfdSendResponse(uv_work_t *work, int status) {
|
||||
debugPrint("%s", "send response");
|
||||
SUvUdfWork *udfWork = (SUvUdfWork *) (work->data);
|
||||
|
||||
uv_write_t *write_req = malloc(sizeof(uv_write_t));
|
||||
write_req->data = udfWork;
|
||||
uv_write(write_req, udfWork->client, &udfWork->output, 1, udfdOnWrite);
|
||||
|
||||
free(work);
|
||||
}
|
||||
|
||||
void udfdAllocBuffer(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *buf) {
|
||||
debugPrint("%s", "allocate buffer for read");
|
||||
SUdfdUvConn *ctx = handle->data;
|
||||
int32_t msgHeadSize = sizeof(int32_t) + sizeof(int64_t);
|
||||
if (ctx->inputCap == 0) {
|
||||
ctx->inputBuf = malloc(msgHeadSize);
|
||||
if (ctx->inputBuf) {
|
||||
ctx->inputLen = 0;
|
||||
ctx->inputCap = msgHeadSize;
|
||||
ctx->inputTotal = -1;
|
||||
|
||||
buf->base = ctx->inputBuf;
|
||||
buf->len = ctx->inputCap;
|
||||
} else {
|
||||
//TODO: log error
|
||||
buf->base = NULL;
|
||||
buf->len = 0;
|
||||
}
|
||||
} else {
|
||||
ctx->inputCap = ctx->inputTotal > ctx->inputCap ? ctx->inputTotal : ctx->inputCap;
|
||||
void *inputBuf = realloc(ctx->inputBuf, ctx->inputCap);
|
||||
if (inputBuf) {
|
||||
ctx->inputBuf = inputBuf;
|
||||
buf->base = ctx->inputBuf + ctx->inputLen;
|
||||
buf->len = ctx->inputCap - ctx->inputLen;
|
||||
} else {
|
||||
//TODO: log error
|
||||
buf->base = NULL;
|
||||
buf->len = 0;
|
||||
}
|
||||
}
|
||||
debugPrint("\tinput buf cap - len - total : %d - %d - %d", ctx->inputCap, ctx->inputLen, ctx->inputTotal);
|
||||
|
||||
}
|
||||
|
||||
bool isUdfdUvMsgComplete(SUdfdUvConn *pipe) {
|
||||
if (pipe->inputTotal == -1 && pipe->inputLen >= sizeof(int32_t)) {
|
||||
pipe->inputTotal = *(int32_t *) (pipe->inputBuf);
|
||||
}
|
||||
if (pipe->inputLen == pipe->inputCap && pipe->inputTotal == pipe->inputCap) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void udfdHandleRequest(SUdfdUvConn *conn) {
|
||||
uv_work_t *work = malloc(sizeof(uv_work_t));
|
||||
SUvUdfWork *udfWork = malloc(sizeof(SUvUdfWork));
|
||||
udfWork->client = conn->client;
|
||||
udfWork->input = uv_buf_init(conn->inputBuf, conn->inputLen);
|
||||
conn->inputBuf = NULL;
|
||||
conn->inputLen = 0;
|
||||
conn->inputCap = 0;
|
||||
conn->inputTotal = -1;
|
||||
work->data = udfWork;
|
||||
uv_queue_work(loop, work, udfdProcessRequest, udfdSendResponse);
|
||||
}
|
||||
|
||||
void udfdPipeCloseCb(uv_handle_t *pipe) {
|
||||
SUdfdUvConn *conn = pipe->data;
|
||||
free(conn->client);
|
||||
free(conn->inputBuf);
|
||||
free(conn);
|
||||
}
|
||||
|
||||
void udfdUvHandleError(SUdfdUvConn *conn) {
|
||||
uv_close((uv_handle_t *) conn->client, udfdPipeCloseCb);
|
||||
}
|
||||
|
||||
void udfdPipeRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
|
||||
debugPrint("%s, nread: %zd", "read from pipe", nread);
|
||||
|
||||
if (nread == 0) return;
|
||||
|
||||
SUdfdUvConn *conn = client->data;
|
||||
|
||||
if (nread > 0) {
|
||||
conn->inputLen += nread;
|
||||
if (isUdfdUvMsgComplete(conn)) {
|
||||
udfdHandleRequest(conn);
|
||||
} else {
|
||||
//log error or continue;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (nread < 0) {
|
||||
debugPrint("Read error %s", uv_err_name(nread));
|
||||
if (nread == UV_EOF) {
|
||||
//TODO check more when close
|
||||
} else {
|
||||
}
|
||||
udfdUvHandleError(conn);
|
||||
}
|
||||
}
|
||||
|
||||
void udfdOnNewConnection(uv_stream_t *server, int status) {
|
||||
debugPrint("%s", "on new connection");
|
||||
if (status < 0) {
|
||||
// TODO
|
||||
return;
|
||||
}
|
||||
|
||||
uv_pipe_t *client = (uv_pipe_t *) malloc(sizeof(uv_pipe_t));
|
||||
uv_pipe_init(loop, client, 0);
|
||||
if (uv_accept(server, (uv_stream_t *) client) == 0) {
|
||||
SUdfdUvConn *ctx = malloc(sizeof(SUdfdUvConn));
|
||||
ctx->client = (uv_stream_t *) client;
|
||||
ctx->inputBuf = 0;
|
||||
ctx->inputLen = 0;
|
||||
ctx->inputCap = 0;
|
||||
client->data = ctx;
|
||||
ctx->client = (uv_stream_t *) client;
|
||||
uv_read_start((uv_stream_t *) client, udfdAllocBuffer, udfdPipeRead);
|
||||
} else {
|
||||
uv_close((uv_handle_t *) client, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void removeListeningPipe(int sig) {
|
||||
uv_fs_t req;
|
||||
uv_fs_unlink(loop, &req, "udf.sock", NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main() {
|
||||
debugPrint("libuv version: %x", UV_VERSION_HEX);
|
||||
|
||||
loop = uv_default_loop();
|
||||
uv_fs_t req;
|
||||
uv_fs_unlink(loop, &req, "udf.sock", NULL);
|
||||
|
||||
uv_pipe_t server;
|
||||
uv_pipe_init(loop, &server, 0);
|
||||
|
||||
signal(SIGINT, removeListeningPipe);
|
||||
|
||||
int r;
|
||||
if ((r = uv_pipe_bind(&server, "udf.sock"))) {
|
||||
debugPrint("Bind error %s\n", uv_err_name(r));
|
||||
removeListeningPipe(0);
|
||||
return 1;
|
||||
}
|
||||
if ((r = uv_listen((uv_stream_t *) &server, 128, udfdOnNewConnection))) {
|
||||
debugPrint("Listen error %s", uv_err_name(r));
|
||||
return 2;
|
||||
}
|
||||
uv_run(loop, UV_RUN_DEFAULT);
|
||||
uv_loop_close(loop);
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "uv.h"
|
||||
#include "tudf.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
startUdfService();
|
||||
uv_sleep(1000);
|
||||
char path[256] = {0};
|
||||
size_t cwdSize = 256;
|
||||
int err = uv_cwd(path, &cwdSize);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "err cwd: %s\n", uv_strerror(err));
|
||||
return err;
|
||||
}
|
||||
fprintf(stdout, "current working directory:%s\n", path);
|
||||
strcat(path, "/libudf1.so");
|
||||
SUdfInfo udfInfo = {.udfName="udf1", .path=path};
|
||||
|
||||
UdfHandle handle;
|
||||
setupUdf(&udfInfo, &handle);
|
||||
|
||||
//char state[5000000] = "state";
|
||||
//char input[5000000] = "input";
|
||||
int dataSize = 500;
|
||||
int callCount = 2;
|
||||
if (argc > 1) dataSize = atoi(argv[1]);
|
||||
if (argc > 2) callCount = atoi(argv[2]);
|
||||
char *state = malloc(dataSize);
|
||||
char *input = malloc(dataSize);
|
||||
SUdfDataBlock blockInput = {.data = input, .size = dataSize};
|
||||
SUdfDataBlock blockOutput;
|
||||
char* newState;
|
||||
int32_t newStateSize;
|
||||
for (int l = 0; l < callCount; ++l) {
|
||||
callUdf(handle, 0, state, dataSize, blockInput, &newState, &newStateSize, &blockOutput);
|
||||
}
|
||||
free(state);
|
||||
free(input);
|
||||
teardownUdf(handle);
|
||||
|
||||
stopUdfService();
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "tudf.h"
|
||||
|
||||
void udf1(int8_t step, char *state, int32_t stateSize, SUdfDataBlock input,
|
||||
char **newState, int32_t *newStateSize, SUdfDataBlock *output) {
|
||||
fprintf(stdout, "%s, step:%d\n", "udf function called", step);
|
||||
char *newStateBuf = malloc(stateSize);
|
||||
memcpy(newStateBuf, state, stateSize);
|
||||
*newState = newStateBuf;
|
||||
*newStateSize = stateSize;
|
||||
|
||||
char *outputBuf = malloc(input.size);
|
||||
memcpy(outputBuf, input.data, input.size);
|
||||
output->data = outputBuf;
|
||||
output->size = input.size;
|
||||
return;
|
||||
}
|
Loading…
Reference in New Issue