public class TaskCoordinator extends Object
acquirePermit(Task, long, long, TimeUnit) and
releasePermit(Permit) pair of methods.
The application controls the supply of permits available to the housekeeping
tasks via setAppPermitPercent(int), increasing the percentage of
permits it retains as it gets busy and decreasing the percentage when the
load diminishes.| Modifier and Type | Class and Description |
|---|---|
class |
TaskCoordinator.Permit
Permits acquired by Tasks are represented by Permit objects.
|
static interface |
TaskCoordinator.StatDefs
Defines stats associated with the TaskCoordinator.
|
static class |
TaskCoordinator.Task
Represents a task that can request permits.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_LEASE_CHECK_PERIOD_MS
The default period used to run the lease checking task.
|
protected Logger |
logger |
protected Timer |
timer |
| Constructor and Description |
|---|
TaskCoordinator(Logger logger,
Set<TaskCoordinator.Task> tasks)
The constructor used to create active coordinators.
|
TaskCoordinator(Logger logger,
Set<TaskCoordinator.Task> tasks,
boolean active)
The constructor used to initialize the set of cooperating tasks.
|
| Modifier and Type | Method and Description |
|---|---|
TaskCoordinator.Permit |
acquirePermit(TaskCoordinator.Task task,
long timeout,
long leaseInterval,
TimeUnit unit)
Returns a Permit.
|
void |
close()
Close the coordinator.
|
int |
getAppPermitPercent()
Returns the % of total permits current reserved by the application.
|
int |
getAppPermits()
Returns the current number of app permits reserved by the application.
|
int |
getAvailableRealPermits()
The number of real permits available for acquisition by housekeeping
tasks.
|
int |
getMaxPermits()
Returns the constant maximum number of real permits available to
housekeeping tasks.
|
int |
getOutstandingDeficitPermits()
Returns the number of currently outstanding deficit permits
|
int |
getOutstandingRealPermits()
Returns the number of currently outstanding real permits.
|
StatGroup |
getStats(StatsConfig statsConfig)
Returns the stats associated with the Task Coordinator.
|
protected String |
permitSummary()
Log information about current use of permits
|
void |
releasePermit(TaskCoordinator.Permit permit)
Releases the permit that was acquired by an earlier call to
acquirePermit(com.sleepycat.je.utilint.TaskCoordinator.Task, long, long, java.util.concurrent.TimeUnit). |
boolean |
setAppPermitPercent(int newAppPermitPercent)
Used to adjust the number of permits associated with the application.
|
void |
setLeaseCheckingPeriod(int periodMs)
Modifies the lease period.
|
protected final Timer timer
public static final int DEFAULT_LEASE_CHECK_PERIOD_MS
protected final Logger logger
public TaskCoordinator(Logger logger, Set<TaskCoordinator.Task> tasks, boolean active)
acquirePermit(com.sleepycat.je.utilint.TaskCoordinator.Task, long, long, java.util.concurrent.TimeUnit), thus effectively disabling the coordination
mechanism. Note that leases, if specified, are still tracked against
these permits. This is to ensure that the behavior of the code is
correct and no permits are being leaked.logger - the logger used for task coordination related loggingtasks - the set of cooperating tasksactive - if true the coordinator is activepublic TaskCoordinator(Logger logger, Set<TaskCoordinator.Task> tasks)
TaskCoordinator(Logger, Set, boolean)public void setLeaseCheckingPeriod(int periodMs)
periodMs - the new period; turns off lease checking if the period
is zeropublic int getMaxPermits()
public int getAppPermits()
public int getAppPermitPercent()
public int getAvailableRealPermits()
public int getOutstandingDeficitPermits()
public int getOutstandingRealPermits()
public TaskCoordinator.Permit acquirePermit(TaskCoordinator.Task task, long timeout, long leaseInterval, TimeUnit unit) throws InterruptedException
If a permit cannot be granted within the timeout period, a
deficit permit (TaskCoordinator.Permit.isDeficit() is granted. The requester can
choose to release the permit immediately or proceed with its task in any
case and release the permit later. Returning a deficit permit reduces
the supply of permits by one; the supply is restored when the deficit
permit is released.
A permit can optionally be associated with an extensible (via Permit#setLease) lease period. This mechanism is primarily intended to be a debugging aid to detect Permits which were acquired but not released. The permit is effectively released after it has expired by a background thread thus freeing up its resources. Such "potentially" leaked permits are logged as an aid to debugging. Attempts to access them after the lease has expired will result in an IllegalStateException.
task - the task acquiring the permit. It must be one of the
cooperating tasks, supplied when the coordinator was created.timeout - the amount of time to wait for the permits. If the value
is zero don't wait.leaseInterval - the max amount of time that a permit can be
retained after it is acquired before it must be released.unit - the time unit of the timeout and @code leaseInterval
argumentsInterruptedException - if the thread is interrupted. The request
to acquire the permit is abandoned in this case. The state of the
coordinator is not impacted by the interrupt and the application can
proceed to acquire other permits or close() the coordinator.releasePermit(Permit),
TaskCoordinator.Permit.setLease(long, java.util.concurrent.TimeUnit)public void releasePermit(TaskCoordinator.Permit permit)
acquirePermit(com.sleepycat.je.utilint.TaskCoordinator.Task, long, long, java.util.concurrent.TimeUnit). It's up to the application to ensure correct
pairing of an acquirePermit(com.sleepycat.je.utilint.TaskCoordinator.Task, long, long, java.util.concurrent.TimeUnit) and releasePermit(com.sleepycat.je.utilint.TaskCoordinator.Permit) pair.
An IllegalStateException is thrown if the lease associated with the
permit has expired or an attempt is made to release a permit that was
already released.permit - the permit being releasedacquirePermit(Task, long, long, TimeUnit)public boolean setAppPermitPercent(int newAppPermitPercent)
newAppPermitPercent - the percentage of permits to be reserved for
the application. The higher this percentage, the fewer the permits
available for use by the housekeeping tasks. It must be a number between
0 and 100public void close()
acquirePermit(com.sleepycat.je.utilint.TaskCoordinator.Task, long, long, java.util.concurrent.TimeUnit)
will result in the return of a DeficitPermit. It's up to the
individual tasks to check for application shutdown and do their own
individual cleanup.public StatGroup getStats(StatsConfig statsConfig)
protected String permitSummary()
Copyright © 2024. All rights reserved.