Class ScheduledChore

java.lang.Object
org.apache.hadoop.hbase.ScheduledChore
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
BalancerChore, BrokenStoreFileCleaner, CatalogJanitor, CleanerChore, ClusterStatusChore, ClusterStatusPublisher, CompactedHFilesDischarger, ExecutorStatusChore, FileSystemUtilizationChore, HbckChore, HealthCheckChore, HeapMemoryManager.HeapMemoryTunerChore, HRegionServer.CompactionChecker, HRegionServer.PeriodicMemStoreFlusher, MobFileCleanerChore, MobFileCompactionChore, NamedQueueServiceChore, OldWALsDirSizeChore, QuotaCache.QuotaRefresherChore, QuotaObserverChore, RegionNormalizerChore, RegionSizeReportingChore, RegionsRecoveryChore, Replication.ReplicationStatisticsChore, ReplicationBarrierCleaner, ReplicationMarkerChore, ReplicationSinkServiceImpl.ReplicationStatisticsChore, RollingUpgradeChore, RSMobFileCleanerChore, ServerManager.FlushedSequenceIdFlusher, SnapshotCleanerChore, SnapshotQuotaObserverChore, SpaceQuotaRefresherChore, SplitLogManager.TimeoutMonitor, StorefileRefresherChore

@Private public abstract class ScheduledChore extends Object implements Runnable
ScheduledChore is a task performed on a period in hbase. ScheduledChores become active once scheduled with a ChoreService via ChoreService.scheduleChore(ScheduledChore). The chore is run in a ScheduledThreadPoolExecutor and competes with other ScheduledChores for access to the threads in the core thread pool. If an unhandled exception occurs, the chore cancellation is logged. Implementers should consider whether or not the Chore will be able to execute within the defined period. It is bad practice to define a ScheduledChore whose execution time exceeds its period since it will try to hog one of the threads in the ChoreService's thread pool.

Don't subclass ScheduledChore if the task relies on being woken up for something to do, such as an entry being added to a queue, etc.

  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
    • name

      private final String name
    • DEFAULT_TIME_UNIT

      private static final TimeUnit DEFAULT_TIME_UNIT
      Default values for scheduling parameters should they be excluded during construction
    • DEFAULT_INITIAL_DELAY

      private static final long DEFAULT_INITIAL_DELAY
      See Also:
    • period

      private final int period
      Scheduling parameters. Used by ChoreService when scheduling the chore to run periodically
    • timeUnit

      private final TimeUnit timeUnit
    • initialDelay

      private final long initialDelay
    • choreService

      Interface to the ChoreService that this ScheduledChore is scheduled with. null if the chore is not scheduled.
    • timeOfLastRun

      private long timeOfLastRun
      Variables that encapsulate the meaningful state information
    • timeOfThisRun

      private long timeOfThisRun
    • initialChoreComplete

      private boolean initialChoreComplete
    • stopper

      private final Stoppable stopper
      A means by which a ScheduledChore can be stopped. Once a chore recognizes that it has been stopped, it will cancel itself. This is particularly useful in the case where a single stopper instance is given to multiple chores. In such a case, a single Stoppable.stop(String) command can cause many chores to stop together.
  • Constructor Details

    • ScheduledChore

      @Private protected ScheduledChore()
      Construct a ScheduledChore

      This constructor is for test only. It allows us to create an object and to call chore() on it.

    • ScheduledChore

      public ScheduledChore(String name, Stoppable stopper, int period)
      Construct a ScheduledChore
      Parameters:
      name - Name assigned to Chore. Useful for identification amongst chores of the same type
      stopper - When Stoppable.isStopped() is true, this chore will cancel and cleanup
      period - Period in millis with which this Chore repeats execution when scheduled.
    • ScheduledChore

      public ScheduledChore(String name, Stoppable stopper, int period, long initialDelay)
      Construct a ScheduledChore
      Parameters:
      name - Name assigned to Chore. Useful for identification amongst chores of the same type
      stopper - When Stoppable.isStopped() is true, this chore will cancel and cleanup
      period - Period in millis with which this Chore repeats execution when scheduled.
      initialDelay - Delay before this Chore begins to execute once it has been scheduled. A value of 0 means the chore will begin to execute immediately. Negative delays are invalid and will be corrected to a value of 0.
    • ScheduledChore

      public ScheduledChore(String name, Stoppable stopper, int period, long initialDelay, TimeUnit unit)
      Construct a ScheduledChore
      Parameters:
      name - Name assigned to Chore. Useful for identification amongst chores of the same type
      stopper - When Stoppable.isStopped() is true, this chore will cancel and cleanup
      period - Period in Timeunit unit with which this Chore repeats execution when scheduled.
      initialDelay - Delay in Timeunit unit before this Chore begins to execute once it has been scheduled. A value of 0 means the chore will begin to execute immediately. Negative delays are invalid and will be corrected to a value of 0.
      unit - The unit that is used to measure period and initialDelay
  • Method Details

    • run

      public void run()
      Specified by:
      run in interface Runnable
    • updateTimeTrackingBeforeRun

      Update our time tracking members. Called at the start of an execution of this chore's run() method so that a correct decision can be made as to whether or not we missed the start time
    • onChoreMissedStartTime

      private void onChoreMissedStartTime()
      Notify the ChoreService that this chore has missed its start time. Allows the ChoreService to make the decision as to whether or not it would be worthwhile to increase the number of core pool threads
    • getTimeBetweenRuns

      Return how long in millis has it been since this chore last run. Useful for checking if the chore has missed its scheduled start time by too large of a margin
    • missedStartTime

      private boolean missedStartTime()
      Returns true when the time between runs exceeds the acceptable threshold
    • getMaximumAllowedTimeBetweenRuns

      Returns max allowed time in millis between runs.
    • isValidTime

      private boolean isValidTime(long time)
      Return true if time is earlier or equal to current time
    • triggerNow

      public boolean triggerNow()
      Returns false when the Chore is not currently scheduled with a ChoreService
    • setChoreService

    • cancel

      public void cancel()
    • cancel

      public void cancel(boolean mayInterruptIfRunning)
    • getName

      public String getName()
    • getStopper

    • getPeriod

      public int getPeriod()
      Returns period to execute chore in getTimeUnit() units
    • getInitialDelay

      public long getInitialDelay()
      Returns initial delay before executing chore in getTimeUnit() units
    • getTimeUnit

    • isInitialChoreComplete

      public boolean isInitialChoreComplete()
    • getChoreService

    • getTimeOfLastRun

    • getTimeOfThisRun

    • isScheduled

      public boolean isScheduled()
      Returns true when this Chore is scheduled with a ChoreService
    • choreForTesting

      @Private public void choreForTesting()
    • chore

      protected abstract void chore()
      The task to execute on each scheduled execution of the Chore
    • initialChore

      protected boolean initialChore()
      Override to run a task before we start looping.
      Returns:
      true if initial chore was successful
    • cleanup

      protected void cleanup()
      Override to run cleanup tasks when the Chore encounters an error and must stop running
    • shutdown

      public void shutdown()
      Call shutdown(boolean) with true.
      See Also:
    • shutdown

      public void shutdown(boolean mayInterruptIfRunning)
      Completely shutdown the ScheduleChore, which means we will call cleanup and you should not schedule it again.

      This is another path to cleanup the chore, comparing to stop the stopper instance passed in.

    • toString

      @Private public String toString()
      A summation of this chore in human readable format. Downstream users should not presume parsing of this string can relaibly be done between versions. Instead, they should rely on the public accessor methods to get the information they desire.
      Overrides:
      toString in class Object