@InterfaceAudience.Private public class LeaseManager extends Thread
These external clients hold resources in the server class. Those resources need to be released if the external client fails to send a heartbeat after some interval of time passes.
The Leases class is a general reusable class for this kind of pattern. An instance of the Leases class will create a thread to do its dirty work. You should close() the instance if you want to clean up the thread properly.
NOTE: This class extends Thread rather than Chore because the sleep time can be interrupted when there is something to do, rather than the Chore sleep time which is invariant.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
LeaseManager.Lease
This class tracks a single Lease.
|
static class |
LeaseManager.LeaseStillHeldException
Thrown if we are asked to create a lease but lease on passed name already exists.
|
Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
private int |
leaseCheckFrequency |
private Map<String,LeaseManager.Lease> |
leases |
private static org.slf4j.Logger |
LOG |
private static int |
MIN_WAIT_TIME |
private boolean |
stopRequested |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Constructor and Description |
---|
LeaseManager(int leaseCheckFrequency)
Creates a lease manager.
|
Modifier and Type | Method and Description |
---|---|
void |
addLease(LeaseManager.Lease lease)
Inserts lease.
|
void |
cancelLease(String leaseName)
Client explicitly cancels a lease.
|
void |
close()
Shut down this Leases instance.
|
void |
closeAfterLeasesExpire()
Shuts down this lease instance when all outstanding leases expire.
|
LeaseManager.Lease |
createLease(String leaseName,
int leaseTimeoutPeriod,
LeaseListener listener)
Create a lease and insert it to the map of leases.
|
(package private) LeaseManager.Lease |
removeLease(String leaseName)
Remove named lease.
|
void |
renewLease(String leaseName)
Renew a lease
|
void |
run() |
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
private static final org.slf4j.Logger LOG
private static final int MIN_WAIT_TIME
private final Map<String,LeaseManager.Lease> leases
private final int leaseCheckFrequency
private volatile boolean stopRequested
public LeaseManager(int leaseCheckFrequency)
leaseCheckFrequency
- - how often the lease should be checked (milliseconds)public void closeAfterLeasesExpire()
close()
but
rather than violently end all leases, waits first on extant leases to finish. Use this method
if the lease holders could lose data, leak locks, etc. Presumes client has shutdown allocation
of new leases.public void close()
public LeaseManager.Lease createLease(String leaseName, int leaseTimeoutPeriod, LeaseListener listener) throws LeaseManager.LeaseStillHeldException
leaseName
- name of the leaseleaseTimeoutPeriod
- length of the lease in millisecondslistener
- listener that will process lease expirationsLeaseManager.LeaseStillHeldException
public void addLease(LeaseManager.Lease lease) throws LeaseManager.LeaseStillHeldException
public void renewLease(String leaseName) throws LeaseException
leaseName
- name of the leaseLeaseException
public void cancelLease(String leaseName) throws LeaseException
leaseName
- name of leaseLeaseException
LeaseManager.Lease removeLease(String leaseName) throws LeaseException
leaseName
- name of leaseLeaseException
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.