在聚合 UDF 执行期间丢失与 MySQL 服务器的连接

Lost connection to MySQL server during aggregate UDF execution

我用 C++ 创建了一个 MySQL UDF,并将其上传到 MySQL 的插件目录中。 我能够在 MySQL 中安装 UDF,但是当我尝试调用它时,MySQL 服务器停止了。 我正在使用 visual studio 2019 在 windows 10 操作系统上创建共享库(windows 中的 .dll)。

我在 MySQL C++ UDF 中从 java 调用一个方法,我正在使用 graalVM 从 C++ 函数调用 java 方法。

我用我的 java 代码创建了一个共享库,并在我的 C++ 代码中使用它们来访问我的 java 方法。

MySQL UDF code:DemoAgg.cpp

#ifdef STANDARD

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#endif
#include <mysql.h>
#include <iostream>
#include <ctype.h>
#include <demolibmysqlagg.h>

extern "C" bool test_agg_init(UDF_INIT * initid, UDF_ARGS * args, char* message)
{
    long long* i = new long long; 
    *i = 0;                    
    initid->ptr = (char*)i;

    std::cout << "in init func" << std::endl;

    if (args->arg_count != 1)
    {
        strcpy_s(message, 50, "testagg() requires one arguments");
        return 1;
    }

    if (args->arg_type[0] != INT_RESULT)
    {
        strcpy_s(message, 50, "testagg() requires one integer");
        return 1;
    }
    return 0;
}

extern "C" void test_agg_deinit(UDF_INIT * initid)
{
    std::cout << "in deinit func" << std::endl;
    delete (long long*)initid->ptr;
}

extern "C" void test_agg_clear(UDF_INIT* initid, char* is_null, char* error)
{
    std::cout << "in clear func" << std::endl;
    *((long long*)initid->ptr) = 0;
}

extern "C" void test_agg_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error)
{
    graal_isolate_t* isolate = NULL;
    graal_isolatethread_t* thread = NULL;

    std::cout << "in add func" << std::endl;

    long long int_val;
    int_val = *((long long*)initid->ptr);

    long long int_val2;
    int_val2 = *((long long*)args->args[0]);

    long long sum;
    sum = aggadd(thread, int_val, int_val2);     //my java method which i call from this program
    std::cout << sum;

    *((long long*)initid->ptr) = sum;
}

extern "C" long long test_agg(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error)
{
    std::cout <<"in main func"<<std::endl;
    return *((long long*)initid->ptr);
}

我的模块定义File:DemoAgg.def

LIBRARY testagg

EXPORTS
         test_agg_init
         test_agg_deinit
         test_agg_clear
         test_agg_add
         test_agg

我的java代码:

package testmysql;

import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.c.function.CEntryPoint;

public class demo_mysqlagg {

    @CEntryPoint (name = "aggadd")
    public static int aggadd(IsolateThread thread, int x, int y) {
            return x+y;
    }
}

构建我的 C++ 项目后,我得到一个 dll 文件,我将其复制到 MySQL 的插件目录。

我使用以下查询安装 UDF:

create aggregate function test_agg returns integer soname 'testagg.dll';

我使用以下查询调用我的 UDF:

select test_agg(c1) from demo_agg;

其中,

demo_agg is a table with only one column.

c1 is the column name of type integer.

但是当我执行上面的查询时,弹出如下错误:

18:52:53    select test_agg(c1) from demo_agg LIMIT 0, 1000 Error Code: 2013. Lost connection to MySQL server during query  0.063 sec

我的错误日志:

2020-09-29T14:41:24.584058Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2020-09-29T14:41:24.587014Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.21) starting as process 48276
2020-09-29T14:41:24.653757Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-09-29T14:41:28.123852Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-09-29T14:41:28.958692Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060
2020-09-29T14:41:30.249252Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-09-29T14:41:30.250713Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-09-29T14:41:30.653518Z 0 [System] [MY-010931] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: ready for connections. Version: '8.0.21'  socket: ''  port: 3306  MySQL Community Server - GPL.
in init func
in clear func
in add func
2020-09-29T14:42:49.045501Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2020-09-29T14:42:49.053090Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.21) starting as process 188880
2020-09-29T14:42:49.102031Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-09-29T14:42:53.150956Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-09-29T14:42:53.914379Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060
2020-09-29T14:42:54.728381Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
2020-09-29T14:42:54.737801Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
2020-09-29T14:42:55.464369Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-09-29T14:42:55.465965Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-09-29T14:42:56.133327Z 0 [System] [MY-010931] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: ready for connections. Version: '8.0.21'  socket: ''  port: 3306  MySQL Community Server - GPL.

我放了一些cout语句来知道我在哪个函数中。

有人可以帮我吗? 我在这里做什么? 任何形式的帮助或建议都会有很大的帮助。

问题在于:

graal_isolate_t* isolate = NULL;
graal_isolatethread_t* thread = NULL;

它们为空,这就是它无法调用 java 方法并且连接超时的原因。

如果我这样初始化它们:

if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
        fprintf(stderr, "initialization error\n");
        return;
    }

那么它工作正常。