/
leaseManager.ts
134 lines (132 loc) · 6.47 KB
/
leaseManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { CompleteLease } from "./completeLease";
import { BaseLease } from "./baseLease";
/**
* @interface LeaseManager
* If you wish to have EventProcessorHost store leases somewhere other than Azure Storage,
* you can write your own lease manager using this interface.
*
* The Azure Storage managers use the same storage for both lease and checkpoints, so both
* interfaces are implemented by the same class. You are free to do the same thing if you have
* a unified store for both types of data.
*
* This interface does not specify initialization methods because we have no way of knowing what
* information your implementation will require.
*/
export interface LeaseManager {
/**
* @property {number} leaseRenewInterval The sleep interval **in seconds** between scans.
*
* Allows a lease manager implementation to specify to PartitionManager how often it should
* scan leases and renew them. In order to redistribute leases in a timely fashion after a host
* ceases operating, we recommend a relatively short interval, such as ten seconds. Obviously it
* should be less than half of the lease length, to prevent accidental expiration.
*/
leaseRenewInterval: number;
/**
* @property {number} leaseDuration Duration of a lease **in seconds** before it expires
* unless renewed.
*/
leaseDuration: number;
/**
* Does the lease store exist?
* @returns {Promise<boolean>} Promise<boolean> `true` if it exists, `false` if it does not exist.
*/
leaseStoreExists(): Promise<boolean>;
/**
* Create the lease store if it doesn't exist. Do nothing if it does exist.
* @returns {Promise<void>} Promise<void> resolves with `undefined`; rejects with an `Error`.
*/
createLeaseStoreIfNotExists(): Promise<void>;
/**
* Delete lease store.
* @returns {Promise<void>} Promise<void> resolves with `undefined`; rejects with an `Error`.
*/
deleteLeaseStore(): Promise<void>;
/**
* Gets the lease info for the specified partition. Can return `undefined` if no lease has been
* created in the store for the specified partition.
* @param {string} partitionId Partition id to get the lease for.
* @returns {Promise<CompleteLease | undefined>} Promise<Lease | undefined>
*/
getLease(partitionId: string): Promise<CompleteLease | undefined>;
/**
* 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.
* @returns {Promise<BaseLease[]>} Promise<BaseLease[]>
*/
getAllLeases(): Promise<BaseLease[]>;
/**
* Create in the store the lease info for the given partition, if it does not exist. Do nothing
* if it does exist in the store already.
*
* @param {string[]} partitionIds ids of partitions to create lease info for
* @returns {Promise<void>} Promise<void> undefined on success, rejects on error.
*/
createAllLeasesIfNotExists(partitionIds: string[]): Promise<void>;
/**
* Delete the lease info for the given partition from the store. If there is no stored lease for
* the given partition, that is treated as success.
*
* @param {CompleteLease} lease Lease info for the desired partition as previously obtained from
* `getLease()`.
* @returns {Promise<void>} Promise<void> resolves with `undefined`; rejects with an `Error`.
*/
deleteLease(lease: CompleteLease): Promise<void>;
/**
* Acquire the lease on the desired partition for this EventProcessorHost.
*
* Note that it is legal to acquire a lease that is already owned by another host.
* 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 without errors 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.
*
* @param {CompleteLease} lease Lease info for the desired partition as previously obtained from
* `getLease()`.
* @returns {Promise<boolean>} Promise<boolean> `true` if acquired successfully; `false` otherwise.
*/
acquireLease(lease: CompleteLease): Promise<boolean>;
/**
* Renew a lease currently held by this host.
*
* 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.
*
* @param {CompleteLease} lease lease to be renewed.
* @returns {Promise<boolean>} Promise<boolean> `true` if renewed successfully; `false` otherwise.
*/
renewLease(lease: CompleteLease): Promise<boolean>;
/**
* Give up a lease currently held by this host.
*
* If the lease has been stolen, or expired, releasing it is unnecessary, and will fail if
* attempted.
*
* @param {CompleteLease} lease Lease info for the desired partition as previously obtained from
* `getLease()`.
* @returns {Promise<void>} Promise<void> resolves with `undefined`; rejects with an `Error`.
*/
releaseLease(lease: CompleteLease): Promise<void>;
/**
* 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.
*
* @param {CompleteLease} lease New lease information to be stored.
* @returns {Promise<boolean>} Promise<boolean> `true` if updated successfully; `false` otherwise.
*/
updateLease(lease: CompleteLease): Promise<boolean>;
}