scheduler 组件包含在 MySQL 企业版中,这是一个商业产品。要了解有关商业产品的更多信息,请参见 https://www.mysql.com/products/。The scheduler component is included in MySQL Enterprise Edition,
a commercial product. To learn more about commercial products,
see https://www.mysql.com/products/.
scheduler 组件提供了 mysql_scheduler 服务的实现,使应用程序、组件或插件能够每 N 秒配置、运行和取消配置任务。例如,audit_log 服务器插件在初始化时调用 scheduler 组件,并配置定期、循环地刷新其内存缓存(参见 启用审核日志刷新任务)。The scheduler component provides an
implementation of the mysql_scheduler service
that enables applications, components, or plugins to configure,
run, and unconfigure tasks every N
seconds. For example, the audit_log server
plugin calls the scheduler component at its
initialization and configures a regular, recurring flush of its
memory cache (see Enabling the Audit Log Flush Task).
目的:实现
component_scheduler.enabled系统变量,该变量控制调度器是否正在积极执行任务。在启动时,scheduler组件会注册performance_schema.component_scheduler_tasks表,该表列出当前调度的任务及每个任务的一些运行时数据。Purpose: Implements thecomponent_scheduler.enabledsystem variable that controls whether the scheduler is actively executing tasks. At startup, theschedulercomponent registers theperformance_schema.component_scheduler_taskstable, which lists the currently scheduled tasks and some runtime data about each one.统一资源名 (URN):
file://component_schedulerURN:file://component_scheduler
有关安装说明,请参阅 第 7.5.1 节,“安装和卸载组件”。For installation instructions, see Section 7.5.1, “Installing and Uninstalling Components”.
scheduler 组件通过以下元素实现该服务:The scheduler component implements the service
using these elements:
一个按下一次运行时间排序(升序)的已注册、非活动计划任务的优先队列。A priority queue of the registered, inactive scheduled tasks sorted by the next time to run (in ascending order).
一个已注册活动任务的列表。A list of the registered, active tasks.
一个后台线程:A background thread that:
如果没有任务或顶部任务需要更多时间才能运行,则进入睡眠。它会定期唤醒以检查是否到了结束时间。Sleeps if there are no tasks or if the top task needs more time to run. It wakes periodically to check whether it is time to end.
编译需要运行的任务列表,将它们从非活动队列移动,添加到活动队列,并分别执行每个任务。Compiles a list of the tasks that need to run, moves them from the inactive queue, adds them to the active queue, and executes each task individually.
在执行完任务列表后,将任务从活动列表中移除,添加回非活动列表,并计算它们下一次需要运行的时间。After executing the task list, removes the tasks from the active list, adds them to the inactive list, and calculates the next time they need to run.
当调用者调用 mysql_scheduler.create() 服务时,它会创建一个新的计划任务实例以添加到队列中,从而触发后台线程的信号量。该新任务的句柄会返回给调用者。调用代码应保留此句柄和对调度服务的引用,直到调用 mysql_scheduler.destroy() 服务之后。当调用者调用 destroy() 并传入它从 create() 收到的句柄时,服务会等待任务变为非活动状态(如果正在运行),然后将其从非活动队列中移除。When a caller invokes the
mysql_scheduler.create() service, it creates a
new scheduled task instance to add to the queue, which signals the
semaphore of the background thread. A handle to the new task is
returned to the caller. The calling code should keep this handle
and the service reference to the scheduling service until after
calling the mysql_scheduler.destroy() service.
When the caller invokes destroy() and passes in
the handle it received from create(), the
service waits for the task to become inactive (if running) and
then removes it from the inactive queue.
组件服务将每个应用程序提供的回调(函数指针)一次调用到同一个调度器线程中,按每个回调所需运行时间的升序顺序逐个调用。The component service calls each application-provided callback (function pointer) into the same scheduler thread, one at a time and in ascending order, based on the time each requires to run.
希望在应用程序、组件或插件中加入调度器队列功能的开发人员应查阅 MySQL 源代码分发包中的 mysql_scheduler.h 文件。Developers who wish to incorporate scheduler-queueing capabilities
into an application, component, or plugin should consult the
mysql_scheduler.h file in a MySQL source
distribution.