TaskScheduler.java revision ca669ae54f86dbeea277280690584d9f591c7571
325N/A * The contents of this file are subject to the terms of the 325N/A * Common Development and Distribution License, Version 1.0 only 325N/A * (the "License"). You may not use this file except in compliance 325N/A * See the License for the specific language governing permissions 325N/A * and limitations under the License. 325N/A * When distributing Covered Code, include this CDDL HEADER in each 325N/A * If applicable, add the following below this CDDL HEADER, with the 325N/A * fields enclosed by brackets "[]" replaced with your own identifying 325N/A * Portions Copyright [yyyy] [name of copyright owner] 325N/A * Copyright 2006-2010 Sun Microsystems, Inc. 325N/A * Portions Copyright 2013-2015 ForgeRock AS 325N/A * This class defines a task scheduler for the Directory Server that will 325N/A * control the execution of scheduled tasks and other administrative functions 325N/A * that need to occur on a regular basis. 325N/A * The fully-qualified name of this class. 325N/A "org.opends.server.backends.task.TaskScheduler";
325N/A * The maximum length of time in milliseconds to sleep between iterations 325N/A * through the scheduler loop. 325N/A // Indicates whether the scheduler is currently running. 325N/A // Indicates whether a request has been received to stop the scheduler. 325N/A // The entry that serves as the immediate parent for recurring tasks. 325N/A // The entry that serves as the immediate parent for scheduled tasks. 325N/A // The top-level entry at the root of the task tree. 325N/A // The set of recurring tasks defined in the server. 325N/A // The set of tasks associated with this scheduler. 325N/A // The set of worker threads that are actively busy processing tasks. 325N/A // The thread ID for the next task thread to be created; 325N/A // The set of worker threads that may be used to process tasks. 325N/A // The lock used to provide threadsafe access to the scheduler. 325N/A // The task backend with which this scheduler is associated. 325N/A // The thread being used to actually run the scheduler. 325N/A // The set of recently-completed tasks that need to be retained. 325N/A // The set of tasks that have been scheduled but not yet arrived. 325N/A // The set of tasks that are currently running. 325N/A * Creates a new task scheduler that will be used to ensure that tasks are 325N/A * invoked at the appropriate times. 325N/A * @param taskBackend The task backend with which this scheduler is 325N/A * @throws InitializationException If a problem occurs while initializing 325N/A * the scheduler from the backing file. 325N/A super(
"Task Scheduler Thread");
325N/A // This task might have been already scheduled from before 325N/A // and thus got initialized from backing file, otherwise 325N/A // log error and continue. 325N/A * Adds a recurring task to the scheduler, optionally scheduling the first 325N/A * iteration for processing. 325N/A * @param recurringTask The recurring task to add to the scheduler. 325N/A * @param scheduleIteration Indicates whether to schedule an iteration of 325N/A * @throws DirectoryException If a problem occurs while trying to add the 325N/A * recurring task (e.g., there's already another 325N/A * recurring task defined with the same ID). 325N/A // If there is an existing task with the same id 325N/A // and it is in completed state, take its place. 325N/A * Removes the recurring task with the given ID. 325N/A * @param recurringTaskID The ID of the recurring task to remove. 325N/A * @return The recurring task that was removed, or <CODE>null</CODE> if there 325N/A * was no such recurring task. 325N/A * @throws DirectoryException If there is currently a pending or running 325N/A * iteration of the associated recurring task. 325N/A // Find any existing task iterations and try to cancel them. 325N/A // Remove any completed task iterations. 325N/A * Schedules the provided task for execution. If the scheduler is active and 325N/A * the start time has arrived, then the task will begin execution immediately. 325N/A * Otherwise, it will be placed in the pending queue to be started at the 325N/A * @param task The task to be scheduled. 325N/A * @param writeState Indicates whether the current state information for 325N/A * the scheduler should be persisted to disk once the 325N/A * @throws DirectoryException If a problem occurs while trying to schedule 325N/A * the task (e.g., there's already another task 325N/A * defined with the same ID). 325N/A * Attempts to cancel the task with the given task ID. This will only cancel 325N/A * the task if it has not yet started running. If it has started, then it 325N/A * will not be interrupted. 325N/A * @param taskID The task ID of the task to cancel. 325N/A * @return The requested task, which may or may not have actually been 325N/A * cancelled (the task state should make it possible to determine 325N/A * whether it was cancelled), or <CODE>null</CODE> if there is no 325N/A * Removes the specified pending task. It will be completely removed rather 325N/A * than moving it to the set of completed tasks. 325N/A * @param taskID The task ID of the pending task to remove. 325N/A * @return The task that was removed. 325N/A * @throws DirectoryException If the requested task is not in the pending 325N/A * Removes the specified completed task. 325N/A * @param taskID The task ID of the completed task to remove. 325N/A * @return The task that was removed. 325N/A * @throws DirectoryException If the requested task could not be found. 325N/A * Indicates that processing has completed on the provided task thread and 325N/A * that it is now available for processing other tasks. The thread may be 325N/A * immediately used for processing another task if appropriate. 325N/A * @param taskThread The thread that has completed processing on its 325N/A * previously-assigned task. 325N/A * @param completedTask The task for which processing has been completed. 325N/A * @param taskState The task state for this completed task. 325N/A * @return <CODE>true</CODE> if the thread should continue running and 325N/A * wait for the next task to process, or <CODE>false</CODE> if it 325N/A * should exit immediately. 325N/A // See if the task is part of a recurring task. 325N/A // If so, then schedule the next iteration. 325N/A * Check if a given task is a recurring task iteration and re-schedule it. 325N/A * @param completedTask The task for which processing has been completed. 325N/A * @param calendar The calendar date and time to schedule from. // If there is an existing task with the same id // and it is in completed state, take its place. // This task might have been already scheduled from before // and thus got initialized from backing file, otherwise // log error and continue. * Adds the provided task to the set of completed tasks associated with the * scheduler. It will be automatically removed after the appropriate * retention time has elapsed. * @param completedTask The task for which processing has completed. // The scheduler lock is reentrant, so even if we already hold it, we can // If the task never ran set its completion // time here explicitly so that it can be // correctly evaluated for retention later. * Stops the scheduler so that it will not start any scheduled tasks. It will * not attempt to interrupt any tasks that are already running. Note that * once the scheduler has been stopped, it cannot be restarted and it will be * necessary to restart the task backend to start a new scheduler instance. * Attempts to interrupt any tasks that are actively running. This will not * make any attempt to stop the scheduler. * @param interruptState The state that should be assigned to the tasks if * they are successfully interrupted. * @param interruptReason A message indicating the reason that the tasks * @param waitForStop Indicates whether this method should wait until * all active tasks have stopped before returning. // Grab a copy of the running threads so that we can operate on them without // Iterate through all the task threads and request that they stop // If we should actually wait for all the task threads to stop, then do so. * Operates in a loop, launching tasks at the appropriate time and performing * any necessary periodic cleanup. // If there are any pending tasks that need to be started, then do so // If we're waiting for the start time to arrive, then see if that // will come before the next sleep time is up. // Recurring task iteration has to spawn the next one // even if the current iteration has been canceled. // If we're waiting for the start time to arrive, // then see if that will come before the next // Clean up any completed tasks that have been around long enough. // If anything changed, then make sure that the on-disk state gets * Determines whether the specified task should start running. This is based * on the start time, the set of dependencies, and whether or not the * scheduler is active. Note that the caller to this method must hold the * @param task The task for which to make the determination. * @return The task state that should be used for the task. It should be * RUNNING if the task should be started, or some other state if not. // If the task has finished we don't want to restart it // Reset task state if recurring. * Populates the scheduler with information read from the task backing file. * If no backing file is found, then create a new one. The caller must * already hold the scheduler lock or otherwise ensure that this is a * @throws InitializationException If a fatal error occurs while attempting * to perform the initialization. * Creates a new task backing file that contains only the basic structure but * no scheduled or recurring task entries. The caller must already hold the * scheduler lock or otherwise ensure that this is a threadsafe operation. * @throws InitializationException If a problem occurs while attempting to * create the backing file. // First, write a header to the top of the file to indicate that it should // not be manually edited. // Next, create the required hierarchical entries and add them to the // Close the file and we're done. * Writes state information about all tasks and recurring tasks to disk. // First, write a header to the top of the file to indicate that it should // not be manually edited. // Next, write the structural entries to the top of the LDIF. // Iterate through all the recurring tasks and write them to LDIF. // Iterate through all the scheduled tasks and write them to LDIF. // See if there is a ".save" file. If so, then delete it. // If there is an existing backing file, then rename it to ".save". // Rename the ".tmp" file into place. * Retrieves the total number of entries in the task backend. * @return The total number of entries in the task backend. * Retrieves the number of scheduled tasks in the task backend. * @return The total number of entries in the task backend. * Retrieves the number of recurring tasks in the task backend. * @return The total number of entries in the task backend. * Retrieves the task backend with which this scheduler is associated. * @return The task backend with which this scheduler is associated. * Retrieves the root entry that is the common ancestor for all entries in the * @return The root entry that is the common ancestor for all entries in the * Retrieves the entry that is the immediate parent for all scheduled task * entries in the task backend. * @return The entry that is the immediate parent for all scheduled task * entries in the task backend. * Retrieves the entry that is the immediate parent for all recurring task * entries in the task backend. * @return The entry that is the immediate parent for all recurring task * entries in the task backend. * Retrieves the scheduled task with the given task ID. * @param taskID The task ID for the scheduled task to retrieve. * @return The requested scheduled task, or <CODE>null</CODE> if there is no * Retrieves the scheduled task created from the specified entry. * @param taskEntryDN The DN of the task configuration entry associated * with the task to retrieve. * @return The requested scheduled task, or <CODE>null</CODE> if there is no * Indicates whether the current thread already holds a lock on the scheduler. * @return {@code true} if the current thread holds the scheduler lock, or * Attempts to acquire a write lock on the specified entry, trying as many * times as necessary until the lock has been acquired. * @param entryDN The DN of the entry for which to acquire the write lock. * @return The write lock that has been acquired for the entry. * Attempts to acquire a read lock on the specified entry, trying up to five * @param entryDN The DN of the entry for which to acquire the read lock. * @return The read lock that has been acquired for the entry. * @throws DirectoryException If the read lock cannot be acquired. * Releases the lock held on the specified entry. * @param entryDN The DN of the entry for which the lock is held. * @param lock The lock held on the entry. * Retrieves the scheduled task entry with the provided DN. The caller should * hold a read lock on the target entry. * @param scheduledTaskEntryDN The entry DN that indicates which scheduled * task entry to retrieve. * @return The scheduled task entry with the provided DN, or * <CODE>null</CODE> if no scheduled task has the provided DN. * Compares the filter in the provided search operation against each of the * task entries, returning any that match. Note that only the search filter * will be used -- the base and scope will be ignored, so the caller must * ensure that they are correct for scheduled tasks. * @param searchOperation The search operation to use when performing the * @return <CODE>true</CODE> if processing should continue on the search * operation, or <CODE>false</CODE> if it should not for some reason * (e.g., a size or time limit was reached). * @throws DirectoryException If a problem occurs while processing the * search operation against the scheduled tasks. * Retrieves the recurring task with the given recurring task ID. * @param recurringTaskID The recurring task ID for the recurring task to * @return The requested recurring task, or <CODE>null</CODE> if there is no * Retrieves the recurring task with the given recurring task ID. * @param recurringTaskEntryDN The recurring task ID for the recurring task * @return The requested recurring task, or <CODE>null</CODE> if there is no * Retrieves the recurring task entry with the provided DN. The caller should * hold a read lock on the target entry. * @param recurringTaskEntryDN The entry DN that indicates which recurring * task entry to retrieve. * @return The recurring task entry with the provided DN, or * <CODE>null</CODE> if no recurring task has the provided DN. * Compares the filter in the provided search operation against each of the * recurring task entries, returning any that match. Note that only the * search filter will be used -- the base and scope will be ignored, so the * caller must ensure that they are correct for recurring tasks. * @param searchOperation The search operation to use when performing the * @return <CODE>true</CODE> if processing should continue on the search * operation, or <CODE>false</CODE> if it should not for some reason * (e.g., a size or time limit was reached). * @throws DirectoryException If a problem occurs while processing the * search operation against the recurring tasks. * Decodes the contents of the provided entry as a scheduled task. The * resulting task will not actually be scheduled for processing. * @param entry The entry to decode as a scheduled task. * @param operation The operation used to create this task in the server, or * {@code null} if the operation is not available. * @return The scheduled task decoded from the provided entry. * @throws DirectoryException If the provided entry cannot be decoded as a // Get the name of the class that implements the task logic. // Try to load the specified class. // Instantiate the class as a task. // Perform the necessary internal and external initialization for the task. // Avoid task specific initialization for completed tasks. * Decodes the contents of the provided entry as a recurring task. The * resulting recurring task will not actually be added to the scheduler. * @param entry The entry to decode as a recurring task. * @return The recurring task decoded from the provided entry. * @throws DirectoryException If the provided entry cannot be decoded as a * Retrieves the DN of the configuration entry with which this alert generator * @return The DN of the configuration entry with which this alert generator * Retrieves the fully-qualified name of the Java class for this alert * generator implementation. * @return The fully-qualified name of the Java class for this alert * generator implementation. * Retrieves information about the set of alerts that this generator may * produce. The map returned should be between the notification type for a * particular notification and the human-readable description for that * notification. This alert generator must not generate any alerts with types * that are not contained in this list. * @return Information about the set of alerts that this generator may