InMemoryLeaseManager Class

  • java.lang.Object
    • ILeaseManager
      • com.microsoft.azure.eventprocessorhost.InMemoryLeaseManager

public class InMemoryLeaseManager implements ILeaseManager

Constructor Summary

Constructor Description
InMemoryLeaseManager()

Method Summary

Modifier and Type Method and Description
CompletableFuture<Boolean> acquireLease(CompleteLease lease)

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.

CompletableFuture<Void> createAllLeasesIfNotExists(List<String> partitionIds)

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.

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. If there is no stored lease for the given partition, that is treated as success.

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. 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.

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(HostContext hostContext)
CompletableFuture<Boolean> leaseStoreExists()

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.

void notifyOnSteal(String expectedOwner, String partitionId, Callable<?> notifier)
CompletableFuture<Void> releaseLease(CompleteLease lease)

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.

CompletableFuture<Boolean> renewLease(CompleteLease lease)

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.

void setLatency(long milliseconds)
CompletableFuture<Boolean> updateLease(CompleteLease lease)

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.

Constructor Details

InMemoryLeaseManager

public InMemoryLeaseManager()

Method Details

acquireLease

public CompletableFuture acquireLease(CompleteLease lease)

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.

Overrides:

InMemoryLeaseManager.acquireLease(CompleteLease lease)

Parameters:

lease - Lease info for the desired partition

Returns:

CompletableFuture { ->} true if the lease was acquired, false if not, completes exceptionally on error.

createAllLeasesIfNotExists

public CompletableFuture createAllLeasesIfNotExists(List partitionIds)

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.

Overrides:

InMemoryLeaseManager.createAllLeasesIfNotExists(List<String> partitionIds)

Parameters:

partitionIds - ids of partitions to create lease info for

Returns:

CompletableFuture { ->} null on success, completes exceptionally on error

createLeaseStoreIfNotExists

public CompletableFuture createLeaseStoreIfNotExists()

Create the lease store if it does not exist, do nothing if it does exist.

Overrides:

InMemoryLeaseManager.createLeaseStoreIfNotExists()

Returns:

CompletableFuture { ->} null on success, completes exceptionally on error.

deleteLease

public CompletableFuture deleteLease(CompleteLease lease)

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.

Overrides:

InMemoryLeaseManager.deleteLease(CompleteLease lease)

Parameters:

lease - the currently existing lease info for the partition

Returns:

CompletableFuture { ->} null on success, completes exceptionally on error.

deleteLeaseStore

public CompletableFuture deleteLeaseStore()

Deletes the lease store.

Overrides:

InMemoryLeaseManager.deleteLeaseStore()

Returns:

CompletableFuture { ->} null on success, completes exceptionally on error.

getAllLeases

public CompletableFuture> getAllLeases()

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.

Overrides:

InMemoryLeaseManager.getAllLeases()

Returns:

CompletableFuture { ->} list of BaseLease, completes exceptionally on error.

getLease

public CompletableFuture getLease(String partitionId)

Returns the lease info for the given partition..

Overrides:

InMemoryLeaseManager.getLease(String partitionId)

Parameters:

partitionId - Get the lease info for this partition.

Returns:

CompletableFuture { ->} Lease, completes exceptionally on error.

getLeaseDurationInMilliseconds

public 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.

Overrides:

InMemoryLeaseManager.getLeaseDurationInMilliseconds()

Returns:

Duration of a lease before it expires unless renewed, specified in milliseconds.

initialize

public void initialize(HostContext hostContext)

Parameters:

hostContext

leaseStoreExists

public CompletableFuture leaseStoreExists()

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.

Overrides:

InMemoryLeaseManager.leaseStoreExists()

Returns:

CompletableFuture { ->} true if it exists, false if not

notifyOnSteal

public void notifyOnSteal(String expectedOwner, String partitionId, Callable notifier)

Parameters:

expectedOwner
partitionId
notifier

releaseLease

public CompletableFuture releaseLease(CompleteLease lease)

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.

Overrides:

InMemoryLeaseManager.releaseLease(CompleteLease lease)

Parameters:

lease - Lease to be given up

Returns:

CompletableFuture { ->} null on success, completes exceptionally on error.

renewLease

public CompletableFuture renewLease(CompleteLease lease)

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.

Overrides:

InMemoryLeaseManager.renewLease(CompleteLease lease)

Parameters:

lease - Lease to be renewed

Returns:

true if the lease was renewed, false as described above, completes exceptionally on error.

setLatency

public void setLatency(long milliseconds)

Parameters:

milliseconds

updateLease

public CompletableFuture updateLease(CompleteLease lease)

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.

Overrides:

InMemoryLeaseManager.updateLease(CompleteLease lease)

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.

Applies to