Class InMemoryLeaseManager
- java.lang.Object
-
- com.microsoft.azure.eventprocessorhost.InMemoryLeaseManager
-
- All Implemented Interfaces:
ILeaseManager
public class InMemoryLeaseManager extends Object implements ILeaseManager
An ILeaseManager implementation based on an in-memory store. THIS CLASS IS PROVIDED AS A CONVENIENCE FOR TESTING ONLY. All data stored via this class is in memory only and not persisted in any way. In addition, it is only visible within the same process: multiple instances of EventProcessorHost in the same process will share the same in-memory store and leases created by one will be visible to the others, but that is not true across processes. With an ordinary store, there is a clear and distinct line between the values that are persisted and the values that are live in memory. With an in-memory store, that line gets blurry. If we accidentally hand out a reference to the in-store object, then the calling code is operating on the "persisted" values without going through the manager and behavior will be very different. Hence, the implementation takes pains to distinguish between references to "live" and "persisted" checkpoints. To use this class, create a new instance and pass it to the EventProcessorHost constructor that takes ILeaseManager as an argument. After the EventProcessorHost instance is constructed, be sure to call initialize() on this object before starting processing with EventProcessorHost.registerEventProcessor() or EventProcessorHost.registerEventProcessorFactory().
-
-
Constructor Summary
Constructors Constructor Description InMemoryLeaseManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Boolean>
acquireLease(CompleteLease lease)
Acquire the lease on the desired partition for this EventProcessorHost.CompletableFuture<Void>
createAllLeasesIfNotExists(List<String> partitionIds)
Create in the store a lease for each of the given partitions, if it does not exist.CompletableFuture<Void>
createLeaseStoreIfNotExists()
Create the lease store if it does not exist, do nothing if it does exist.CompletableFuture<Void>
deleteLease(CompleteLease lease)
Delete the lease info for a partition from the store.CompletableFuture<Void>
deleteLeaseStore()
Deletes the lease store.CompletableFuture<List<BaseLease>>
getAllLeases()
Returns lightweight BaseLease for all leases, which includes name of owning host and whether lease is expired.CompletableFuture<CompleteLease>
getLease(String partitionId)
Returns the lease info for the given partition..int
getLeaseDurationInMilliseconds()
The lease duration is mostly internal to the lease manager implementation but may be needed by other parts of the event processor host.void
initialize(com.microsoft.azure.eventprocessorhost.HostContext hostContext)
CompletableFuture<Boolean>
leaseStoreExists()
Does the lease store exist?void
notifyOnSteal(String expectedOwner, String partitionId, Callable<?> notifier)
CompletableFuture<Void>
releaseLease(CompleteLease lease)
Give up a lease currently held by this host.CompletableFuture<Boolean>
renewLease(CompleteLease lease)
Renew a lease currently held by this host instance.void
setLatency(long milliseconds)
CompletableFuture<Boolean>
updateLease(CompleteLease lease)
Update the store with the information in the provided lease.
-
-
-
Method Detail
-
initialize
public void initialize(com.microsoft.azure.eventprocessorhost.HostContext hostContext)
-
setLatency
public void setLatency(long milliseconds)
-
getLeaseDurationInMilliseconds
public int getLeaseDurationInMilliseconds()
Description copied from interface:ILeaseManager
The lease duration is mostly internal to the lease manager implementation but may be needed by other parts of the event processor host.- Specified by:
getLeaseDurationInMilliseconds
in interfaceILeaseManager
- Returns:
- Duration of a lease before it expires unless renewed, specified in milliseconds.
-
leaseStoreExists
public CompletableFuture<Boolean> leaseStoreExists()
Description copied from interface:ILeaseManager
Does the lease store exist?The returned CompletableFuture completes with true if the checkpoint store exists or false if it does not. It completes exceptionally on error.
- Specified by:
leaseStoreExists
in interfaceILeaseManager
- Returns:
- CompletableFuture -> true if it exists, false if not
-
createLeaseStoreIfNotExists
public CompletableFuture<Void> createLeaseStoreIfNotExists()
Description copied from interface:ILeaseManager
Create the lease store if it does not exist, do nothing if it does exist.- Specified by:
createLeaseStoreIfNotExists
in interfaceILeaseManager
- Returns:
- CompletableFuture -> null on success, completes exceptionally on error.
-
deleteLeaseStore
public CompletableFuture<Void> deleteLeaseStore()
Description copied from interface:ILeaseManager
Deletes the lease store.- Specified by:
deleteLeaseStore
in interfaceILeaseManager
- Returns:
- CompletableFuture -> null on success, completes exceptionally on error.
-
getLease
public CompletableFuture<CompleteLease> getLease(String partitionId)
Description copied from interface:ILeaseManager
Returns the lease info for the given partition..- Specified by:
getLease
in interfaceILeaseManager
- Parameters:
partitionId
- Get the lease info for this partition.- Returns:
- CompletableFuture -> Lease, completes exceptionally on error.
-
getAllLeases
public CompletableFuture<List<BaseLease>> getAllLeases()
Description copied from interface:ILeaseManager
Returns lightweight BaseLease for all leases, which includes name of owning host and whether lease is expired. An implementation is free to return CompleteLease or its own class derived from CompleteLease, but it is important that getAllLeases run as fast as possible. If it is faster to obtain only the information required for a BaseLease, we heavily recommend doing that.- Specified by:
getAllLeases
in interfaceILeaseManager
- Returns:
- CompletableFuture -> list of BaseLease, completes exceptionally on error.
-
createAllLeasesIfNotExists
public CompletableFuture<Void> createAllLeasesIfNotExists(List<String> partitionIds)
Description copied from interface:ILeaseManager
Create in the store a lease for each of the given partitions, if it does not exist. Do nothing for any lease which exists in the store already.- Specified by:
createAllLeasesIfNotExists
in interfaceILeaseManager
- Parameters:
partitionIds
- ids of partitions to create lease info for- Returns:
- CompletableFuture -> null on success, completes exceptionally on error
-
deleteLease
public CompletableFuture<Void> deleteLease(CompleteLease lease)
Description copied from interface:ILeaseManager
Delete the lease info for a partition from the store. If there is no stored lease for the given partition, that is treated as success.- Specified by:
deleteLease
in interfaceILeaseManager
- Parameters:
lease
- the currently existing lease info for the partition- Returns:
- CompletableFuture -> null on success, completes exceptionally on error.
-
acquireLease
public CompletableFuture<Boolean> acquireLease(CompleteLease lease)
Description copied from interface:ILeaseManager
Acquire the lease on the desired partition for this EventProcessorHost.Note that it is legal to acquire a lease that is currently owned by another host, which is called "stealing". Lease-stealing is how partitions are redistributed when additional hosts are started.
The existing Azure Storage implementation can experience races between two host instances attempting to acquire or steal the lease at the same time. To avoid situations where two host instances both believe that they own the lease, acquisition can fail non-exceptionally by returning false and should do so when there is any doubt -- the worst that can happen is that no host instance owns the lease for a short time. This is qualitatively different from, for example, the underlying store throwing an access exception, which is an error and should complete exceptionally.
- Specified by:
acquireLease
in interfaceILeaseManager
- Parameters:
lease
- Lease info for the desired partition- Returns:
- CompletableFuture -> true if the lease was acquired, false if not, completes exceptionally on error.
-
notifyOnSteal
public void notifyOnSteal(String expectedOwner, String partitionId, Callable<?> notifier)
-
renewLease
public CompletableFuture<Boolean> renewLease(CompleteLease lease)
Description copied from interface:ILeaseManager
Renew a lease currently held by this host instance.If the lease has been taken by another host instance (either stolen or after expiration) or explicitly released, renewLease must return false. With the Azure Storage-based implementation, it IS possible to renew an expired lease that has not been taken by another host, so your implementation can allow that or not, whichever is convenient. If it does not, renewLease should return false.
- Specified by:
renewLease
in interfaceILeaseManager
- Parameters:
lease
- Lease to be renewed- Returns:
- true if the lease was renewed, false as described above, completes exceptionally on error.
-
releaseLease
public CompletableFuture<Void> releaseLease(CompleteLease lease)
Description copied from interface:ILeaseManager
Give up a lease currently held by this host.If the lease has expired or been taken by another host, releasing it is unnecessary but will succeed since the intent has been fulfilled.
- Specified by:
releaseLease
in interfaceILeaseManager
- Parameters:
lease
- Lease to be given up- Returns:
- CompletableFuture -> null on success, completes exceptionally on error.
-
updateLease
public CompletableFuture<Boolean> updateLease(CompleteLease lease)
Description copied from interface:ILeaseManager
Update the store with the information in the provided lease.It is necessary to currently hold a lease in order to update it. If the lease has been stolen, or expired, or released, it cannot be updated. Lease manager implementations should renew the lease before performing the update to avoid lease expiration during the process.
- Specified by:
updateLease
in interfaceILeaseManager
- Parameters:
lease
- New lease info to be stored- Returns:
- true if the update was successful, false if lease was lost and could not be updated, completes exceptionally on error.
-
-