原文地址:https://lefred.be/content/extending-mysql-using-the-component-infrastructure-part-3-component-services/ 作者:lefred 发布时间:2022年1月10日
复制
这是 运用基础组件扩展MySQL 系列文章的第三篇:
Extending MySQL using the Component Infrastructure – part 1
Extending MySQL using the Component Infrastructure – part 2: building the server
Extending MySQL using the Component Infrastructure – part 3: component services
Extending MySQL using the Component Infrastructure – part 4: error logging
Extending MySQL using the Component Infrastructure – part 5: privileges
Extending MySQL using the Component Infrastructure – part 6: functions
Extending MySQL using the Component Infrastructure – part 7: messages to users
现在开始编写我们的新组件\o/
在MySQL服务的源目录中(见第2部分),我们将为组件创建一个新目录:
[fred@imac ~workspace/mysql-server/BIN-DEBUG]$ cd .. [fred@imac ~workspace/mysql-server]$ mkdir components/viruscan [fred@imac ~workspace/mysql-server]$ cd components/viruscan
复制
再创建三个文件
CMakeLists.txt scan.h scan.cc
复制
我们的组件自然将使用基础组件中的一些服务。
文档页面中列出了所需服务 Component Services Inventory.
我们还需要其他组件,比如日志
我们需要的服务头文件是:
- component_implementation(用于每个组件实现都必须包含的宏)
- log_builtin(将消息记录在错误日志和performance_schema.error_log表中)
- mysql_current_thread_reader(获取一些用户上下文)
- dynamic_privileges and security_context
- udf_registration (用于创建UDFs)
- status_variable_registration (用于创建新的状态变量)
扩展MySQL组件世界的第一步
我们现在将创建一个可以下载的组件。它不会做任何事情,只是可以被安装或者删除。
让我们从最小的scan.h开始:
/* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is also distributed with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have included with MySQL. 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. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #define LOG_COMPONENT_TAG "viruscan" #include <mysql/components/component_implementation.h>
复制
如你所见,没什么特别的,版权说明头部、然后为我们组件定义了一个标签:LOG_COMPONENT_TAG。
最后,包含实现我们组件的头文件。
接下来创建我们的组件代码(目前仍然是最小的),scan.cc:
/* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is also distributed with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have included with MySQL. 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. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #define LOG_COMPONENT_TAG "viruscan" #define NO_SIGNATURE_CHANGE 0 #define SIGNATURE_CHANGE 1 #include <components/viruscan/scan.h> static mysql_service_status_t viruscan_service_init() { mysql_service_status_t result = 0; return result; } static mysql_service_status_t viruscan_service_deinit() { mysql_service_status_t result = 0; return result; } BEGIN_COMPONENT_PROVIDES(viruscan_service) END_COMPONENT_PROVIDES(); BEGIN_COMPONENT_REQUIRES(viruscan_service) END_COMPONENT_REQUIRES(); /* A list of metadata to describe the Component. */ BEGIN_COMPONENT_METADATA(viruscan_service) METADATA("mysql.author", "Oracle Corporation"), METADATA("mysql.license", "GPL"), METADATA("mysql.dev", "lefred"), END_COMPONENT_METADATA(); /* Declaration of the Component. */ DECLARE_COMPONENT(viruscan_service, "mysql:viruscan_service") viruscan_service_init, viruscan_service_deinit END_DECLARE_COMPONENT(); /* Defines list of Components contained in this library. Note that for now we assume that library will have exactly one Component. */ DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(viruscan_service) END_DECLARE_LIBRARY_COMPONENTS
复制
包含我们前面创建的头部(scan.h),然后创建两个在安装或者卸载组件时调用的函数。
如果组件提供或者需要服务,我们需要在以下部分添加。现在它们是空的,我们稍后将使用它们:
之后,我们还为组件添加了一些元数据。
最后,我们将声明组件,引用之前创建的安装 (init)或者卸载(deinit)时被调用的函数。
库可以包含多个组件,而我们的库只包含一个。
创建我们的组件
在创建组件之前,我们仍然需要一个包含以下内容的文件CMakeLists.txt:
DISABLE_MISSING_PROFILE_WARNING() MYSQL_ADD_COMPONENT(viruscan scan.cc MODULE_ONLY TEST_ONLY )
复制
目前,我们对分析测试组件不感兴趣。一旦我们需要将组件与ClamAV链接起来,我们还将更新这个文件。
现在我们可以再次运行cmake(与第2部分相同的命令):
[fred@imac ~workspace/mysql-server]$ cd BIN-DEBUG [fred@imac ~workspace/mysql-server/BIN-DEBUG]$ cmake .. -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=../downloads
复制
现在可以只创建我们的组件:
[fred@imac ~workspace/mysql-server/BIN-DEBUG]$ make component_viruscan
复制
完美!
测试我们的组件
现在可以测试我们的组件了,我们再次使用mtr运行MySQL:
$ cd mysql-test $ perl mtr --mem --start
复制
在另一个终端上(我通常把它专用于MySQL脚本测试),我们可以尝试安装和卸载我们新构建的组件:
太棒了!
结论
我们已经创建了第一个组件,这是展扩展MySQL服务的第一步,祝贺你!
在下一篇文章,将会扩展我们组件的功能以便它能使用。
下一步我们将使用日志组件服务将日志消息记录到错误日志中。
保持关注,像往常一样享受和扩展MySQL的代码!
关注我
关注@lefred
通过电子邮件订阅博客
请输入您的电子邮件地址以订阅本博客,并通过电子邮件接收新文章的通知。