Scheduler Architecture

This section discusses the Scheduler's architecture, and describes:

对scheduler体系结构文档的译注


Figure 26-3 illustrates how jobs are handled by the database.

Figure 26-3 Scheduler Components


Description of the illustration admin062.gif

The Job Table

The job table(作业表,作业队列) is a container for all the jobs, (所有作业的容器)with one table per database.一个数据库有一个作业队列) The job作业表存储描述作业的信息如作业的属主和登陆级别,这些信息可以从*_SCHEDULER_JOBS视图查看 table stores information for all jobs such as the owner name or the level of logging. You can find this information in the *_SCHEDULER_JOBS views.

Jobs are database objects, and can therefore accumulate and take up too much space. To avoid this, job objects are automatically dropped by default after completion.(默认情况下,作业完成后,自动删除作业对象,可以由auto_drop属性设置) This behavior is controlled by the auto_drop job attribute.

See "How to View Scheduler Information" for the available job views and administration.

The Job Coordinator

The job coordinator(作业调度器或协调器) is a background process (cjqNNN) that is automatically started when jobs must be run, or windows must be opened. It is automatically brought down after a sustained period of Scheduler inactivity. The job coordinator:

  • Controls and spawns the job slaves(控制与产生job slaves(作业执行器)

  • Queries the job table查询作业队列

  • Picks up jobs from the job table on a regular basis and places them in a memory cache. This improves performance by avoiding going to the disk从作业队列中选择作业到cache内存中

  • Takes jobs from the memory cache and passes them to job slaves for execution从内存cache中取作业,并传给job slaves,等待执行

  • Cleans up the job slave pool when slaves are no longer needed(作业执行完成后,清除job slave pool

  • Goes to sleep when no jobs are scheduled(作业队列没有作业时,转入睡眠)

  • Wakes up when a new job is about to be executed or a job was created using the CREATE_JOB procedure当创建新的作业或要执行新的作业,唤醒作业调度器)

  • Upon database startup after an abnormal database shutdown, recovers any jobs that were running.(数据库异常关闭后,重起时,恢复关闭时的任何作业)

You do not need to set when the job coordinator checks the job table; the system chooses the time frame automatically.

One job coordinator is used per instance. This is also the case in RAC environments.

See Also:

"How to View Scheduler Information" for job coordinator administration and "Using the Scheduler in Real Application Clusters Environments" for RAC information

How Jobs Execute

When a job is picked for processing, the job slave:

  1. Gathers all the metadata needed to run the job.(收集作业运行需要的所有元数据,如程序的参数、权限) As an example, arguments of the program and privilege information.

  2. Starts a database session as the owner of the job, starts a transaction, and then starts executing the job.启动数据库一个会话(该作业的属主),启动事务,执行作业)

  3. Once the job is complete, the slave commits and ends the transaction.作业完成后,提交该事务)

  4. Closes the session.(关闭会话)

Job Slaves

Job slaves actually execute the jobs you submit. They are awakened by the job coordinator when it is time for a job to be executed. They gather metadata to run the job from the job table.(执行作业时,job slave由作业调度器唤醒)

When a job is done, the slaves:作业执行器完成一个作业后

  • Reschedule the job if required重新计划作业)

  • Update the state in the job table to reflect whether the job has completed or is scheduled to run again更新作业队列的状态

  • Insert an entry into the job log table(向作业日志表中插入日志项)

  • Update the run count, and if necessary, failure count and retry count(更新运行次数,失败次数,重试次数)更新各种计数器

  • Clean up(清除)

  • Look for new work (if none, they go to sleep)睡觉

The Scheduler dynamically sizes the slave pool as required.(计划器动态调整job slave pool的大小,即slave的数量)

Using the Scheduler in Real Application Clusters Environments

In a Real Application Clusters (RAC) environment, the Scheduler uses one job table for each database and one job coordinator for each instance. The job coordinators communicate with each other to keep information current. The Scheduler attempts to balance the load of the jobs of a job class across all available instances when the job class has no service affinity, or across the instances assigned to a particular service when the job class does have service affinity.

Figure 26-4 illustrates a typical RAC architecture, with each instance's job coordinator exchanging information with the others.

Figure 26-4 RAC Architecture and the Scheduler


Description of the illustration admin067.gif

Service Affinity when Using the Scheduler

The Scheduler enables you to specify the database service under which a job should be run (service affinity). This ensures better availability than instance affinity because it guarantees that other nodes can be dynamically assigned to the service if an instance goes down. Instance affinity does not have this capability, so, when an instance goes down, none of the jobs with an affinity to that instance will be able to run until the instance comes back up. Figure 26-5 illustrates a typical example of how services and instances could be used.

Figure 26-5 Service Affinity and the Scheduler


Description of the illustration admin068.gif

In Figure 26-5, you could change the properties of the services and the Scheduler will automatically recognize the change.

Each job class can specify a database service. If a service is not specified, the job class belongs to an internal service that is guaranteed to be mapped to every running instance.

调度例行任务

利用新的任务调度程序解放 DBA 的时间。

例行任务是单调和令人厌烦的。如果你以手动方式执行这些例行任务,则你不得不紧张地一次又一次地重复执行相同的任务,并且还存在一种风险,就是某一天你可能会忽略、错过其一个步骤,或者犯其它错误。Oracle数据库10g新的内置任务调度程序为你提供了调度例行任务的强大功能。利用这一调度程序,你可以规定要完成的工作、指定什么时候完成该工作,并监测该工作的完成情况,以便能纠正任何问题。你甚至可以控制分配给您所调度的任务的数据库资源和优先级,以确保首先完成最重要的工作,而不会对其它系统的活动产生不可抵抗的影响。

体系结构

你可以通过 DBMS_SCHEDULER 包或者通过Oracle企业管理器10g的数据库控制功能来访问这一新的调度程序。1给出了与任务的创建和执行具有最直接关系的调度程序组件。一项任务一个程序和一个调度表结合在一起。该程序定义将要运行的内容。例如,一个程序可以是一个 PL/SQL 、一个存储过程,或者一个操作系统脚本。而调度表则定义什么时候运行该程序。对于一个一次性任务,调度表中仅包含一个起始时间。对于一个重复性任务,你可以指定一个起始时间,一个重复运行调度表,还可以根据需要指定结束时间。每次运行一项任务都被看作是一个任务实例。在开始使用该程序调度程序时必须掌握的三个核心组件就是任务、调度表和程序。

1所示,任务类将调度系统与资源管理系统连接起来,使你可以控制如何将数据库资源分配给正在运行的任务。利用任务类将一个任务下达到资源使用者组,该组是一个共享 CPU 时间、并行操作和其它资源分配的会话期组。然后,你可以利用资源计划来控制将这些资源分配给这个使用者组或其它使用者组。2 显示出调度程序窗口如何控制不同资源计划在何时被激活。2 还显示一个窗口组,它将类似的窗口组合成一个单一的实体。窗口和窗口组使你可以在很好地控制如何将数据库资源分配给不同的任务类。

1:调度程序核心组件

2:调度程序资源管理

开始

要创建并运行任务,你需要至少具有CREATE JOB 系统权限。如果你是一位 DBA,那么你将通过授予DBA 角色的SCHEDULER_ADMIN角色而具有 CREATE JOB权限和所有其它调度程序权限。你可以将 CREATE JOB 权限授予数据库用户,允许他们在其自己的模式中创建和运行他们自己的任务。例如,下面的语句将 CREATE JOB 授予用户 gennick

GRANT CREATE JOB TO gennick;

与你应用DBMS_JOB时的情况不同,你不需要设置一个初始化参数,以启动一个任务协调程序后台进程。如果你使用该新的调度程序创建任务,则你的实例会在需要时自动启动一个任务协调程序后台进程。

如果你计划使用资源计划和使用者组来控制数据库资源的分配,则将你实例的RESOURCE_LIMIT 参数设置为TRUE。可以通过 ALTER SYSTEM完成该项设置,如下所示:

ALTER SYSTEM SET RESOURCE_LIMIT = TRUE;

最后,如果您将CREATE JOB 权限授予了一个非DBA用户,而且你希望该用户能够使用数据库控制的图形用户界面来调度任务,那么你还需要授予该用户 SELECT ANY DICTIONARY 系统权限。

你还可以使用 DBMS_SCHEDULER 包。通过SQL*Plus 调用DBMS_SCHEDULER使你完全可以通过命令行访问该调度程序。DBMS_SCHEDULER 还提供了将调度程序功能内嵌于用户应用程序中的能力。

创建你的第一个任务

要创建一个重复性任务,你首先需要定义程序和调度表。假定你需要在每隔一天的下午6:00加载一个外部表。首先创建一个调度程序,它提供一个 PL/SQL 块以执行该加载。代码清单1中的PL/SQL 块发出一个对DBMS_SCHEDULER.CREATE_PROGRAM的调用,作为程序操作所提供的 PL/SQL 块包含有一个简单的INSERT...SELECT FROM 语句,用于从该外部表中载入新的客户。

接着,创建一个调度表,根据该调度表运行该程序。代码清单2 给出一个对DBMS_SCHEDULER.CREATE_SCHEDULE的调用,用于创建我的 PERIODICLOADS调度表。注释和调度表名称参数相当简单。注释可以是提醒你为什么要创建该调度表的任意文本。schedule_ name 参数指明该调度表的所有者和名称。与程序和任务相类似,调度程序也是模式对象,它们的名称必须符合与表、索引等相同的命名规则。

start_date为一个TIMESTAMP WITH TIME ZONE 值,它指明该调度表什么时候被激活。此调度表在(美国东部时间)2004322日星期一开始时被激活。

此调度程序引入了一个功能强大的日历语法,你可以用此语法调度重复性任务。此语法具有三个组件: 频度、间隔和时刻。你还有一个选项:基于一个PL/SQL表达式定义重复间隔(如果你对一个任务定义了一个内嵌调度程序),但是我相信你可能找到一种日历语法,它能满足除最不常用的调度表以外的所有调度表的需要。以下语句是我的PERIODICLOADS 调度表中repeat_interval参数中的日历语法:

FREQ=DAILY;INTERVAL=2;BYHOUR=18;
BYMINUTE=0;BYSECOND=0

FREQ=DAILY 根据PERIODICLOADS 调度表规定,任何任务相继运行之间的重复时间间隔以天或者24小时为单位计算。INTERVAL=2 指明单位的数目,在此例中为2天。三个 BY 数值-BYHOUR=18, BYMINUTE=0, and BYSECOND=0-指明任务运行的时刻,以24小时制表示。代码清单2中的PERIODICLOADS 调度表将给出以下任务运行时间:

  • 第一次运行是在2004322日星期一,下午6:00
  • 第二次运行是在2004322日星期一的两天之后,即324
请使用浏览器的分享功能分享到微信等