-
Notifications
You must be signed in to change notification settings - Fork 3k
/
etcd_lease.go
270 lines (220 loc) · 6.72 KB
/
etcd_lease.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package kvstore
import (
"context"
"errors"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
v3rpcErrors "go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
client "go.etcd.io/etcd/client/v3"
"github.com/cilium/cilium/pkg/lock"
)
// etcdLeaseClient represents the subset of the etcd client methods used to handle the leases lifecycle.
type etcdLeaseClient interface {
Grant(ctx context.Context, ttl int64) (*client.LeaseGrantResponse, error)
KeepAlive(ctx context.Context, id client.LeaseID) (<-chan *client.LeaseKeepAliveResponse, error)
Ctx() context.Context
}
type leaseInfo struct {
count uint32
cancel context.CancelFunc
}
// etcdLeaseManager manages the acquisition of the leases, and keeps track of
// which lease is attached to which etcd key.
type etcdLeaseManager struct {
client etcdLeaseClient
log *logrus.Entry
ttl time.Duration
limit uint32
expired func(key string)
mu lock.RWMutex
leases map[client.LeaseID]*leaseInfo
keys map[string]client.LeaseID
current client.LeaseID
acquiring chan struct{}
wg sync.WaitGroup
}
// newEtcdLeaseManager builds and returns a new lease manager instance.
func newEtcdLeaseManager(cl etcdLeaseClient, ttl time.Duration, limit uint32, expired func(key string), log *logrus.Entry) *etcdLeaseManager {
return &etcdLeaseManager{
client: cl,
log: log,
ttl: ttl,
limit: limit,
expired: expired,
current: client.NoLease,
leases: make(map[client.LeaseID]*leaseInfo),
keys: make(map[string]client.LeaseID),
}
}
// GetLeaseID returns a lease ID, and associates it to the given key. It leverages
// one of the already acquired leases if they are not already attached to too many
// keys, otherwise a new one is acquired.
func (elm *etcdLeaseManager) GetLeaseID(ctx context.Context, key string) (client.LeaseID, error) {
elm.mu.Lock()
// This key is already attached to a lease, hence just return it.
if leaseID := elm.keys[key]; leaseID != client.NoLease {
elm.mu.Unlock()
return leaseID, nil
}
// Return the current lease if it has not been used more than limit times
if info := elm.leases[elm.current]; info != nil && info.count < elm.limit {
info.count++
elm.keys[key] = elm.current
elm.mu.Unlock()
return elm.current, nil
}
// Otherwise, loop through the other known leases to see if any has been released
for lease, info := range elm.leases {
if info.count < elm.limit {
elm.current = lease
info.count++
elm.keys[key] = elm.current
elm.mu.Unlock()
return elm.current, nil
}
}
// If none is found, we need to acquire a new lease. acquiring is a channel
// used to detect whether we are already in the process of acquiring a new
// lease, to prevent multiple acquisitions in parallel.
acquiring := elm.acquiring
if acquiring == nil {
elm.acquiring = make(chan struct{})
}
// Unlock, so that we don't block other paraller operations (e.g., releases)
// while acquiring a new lease, since it might be a slow operation.
elm.mu.Unlock()
// Someone else is already acquiring a new lease. Wait until
// it completes, and then retry again.
if acquiring != nil {
select {
case <-acquiring:
return elm.GetLeaseID(ctx, key)
case <-ctx.Done():
return client.NoLease, ctx.Err()
}
}
// Otherwise, we can proceed to acquire a new lease.
leaseID, cancel, err := elm.newLease(ctx)
elm.mu.Lock()
// Signal that the acquisition process has completed.
close(elm.acquiring)
elm.acquiring = nil
if err != nil {
elm.mu.Unlock()
return client.NoLease, err
}
elm.current = leaseID
elm.leases[leaseID] = &leaseInfo{cancel: cancel}
elm.mu.Unlock()
return elm.GetLeaseID(ctx, key)
}
// Release decrements the counter of the lease attached to the given key.
func (elm *etcdLeaseManager) Release(key string) {
elm.mu.Lock()
defer elm.mu.Unlock()
elm.releaseUnlocked(key)
}
// ReleasePrefix decrements the counter of the leases attached to the keys
// starting with the given prefix.
func (elm *etcdLeaseManager) ReleasePrefix(prefix string) {
elm.mu.Lock()
defer elm.mu.Unlock()
for key, leaseID := range elm.keys {
if strings.HasPrefix(key, prefix) {
if info := elm.leases[leaseID]; info != nil && info.count > 0 {
info.count--
}
delete(elm.keys, key)
}
}
}
// KeyHasLease returns whether the given key is associated with the specified lease.
func (elm *etcdLeaseManager) KeyHasLease(key string, leaseID client.LeaseID) bool {
elm.mu.RLock()
defer elm.mu.RUnlock()
return elm.keys[key] == leaseID
}
// CancelIfExpired verifies whether the error reports that the given lease has
// expired, and in that case aborts the corresponding keepalive process.
func (elm *etcdLeaseManager) CancelIfExpired(err error, leaseID client.LeaseID) {
if errors.Is(err, v3rpcErrors.ErrLeaseNotFound) {
elm.mu.Lock()
if info := elm.leases[leaseID]; info != nil {
info.cancel()
}
elm.mu.Unlock()
}
}
// TotalLeases returns the number of managed leases.
func (elm *etcdLeaseManager) TotalLeases() uint32 {
elm.mu.RLock()
defer elm.mu.RUnlock()
return uint32(len(elm.leases))
}
// Wait waits until all child goroutines terminated.
func (elm *etcdLeaseManager) Wait() {
elm.wg.Wait()
}
func (elm *etcdLeaseManager) newLease(ctx context.Context) (client.LeaseID, context.CancelFunc, error) {
resp, err := elm.client.Grant(ctx, int64(elm.ttl.Seconds()))
if err != nil {
return client.NoLease, nil, err
}
leaseID := resp.ID
kctx, cancel := context.WithCancel(context.Background())
keepalive, err := elm.client.KeepAlive(kctx, leaseID)
if err != nil {
cancel()
return client.NoLease, nil, err
}
elm.wg.Add(1)
go elm.keepalive(kctx, leaseID, keepalive)
elm.log.WithFields(logrus.Fields{
"LeaseID": leaseID,
"TTL": elm.ttl,
}).Info("New lease successfully acquired")
return leaseID, cancel, nil
}
func (elm *etcdLeaseManager) keepalive(ctx context.Context, leaseID client.LeaseID,
keepalive <-chan *client.LeaseKeepAliveResponse) {
defer elm.wg.Done()
for range keepalive {
// Consume the keepalive messages until the channel is closed
}
select {
case <-elm.client.Ctx().Done():
// The context of the etcd client was closed
return
case <-ctx.Done():
default:
}
elm.log.WithField("LeaseID", leaseID).Warning("Lease expired")
elm.mu.Lock()
delete(elm.leases, leaseID)
var keys []string
for key, id := range elm.keys {
if id == leaseID {
keys = append(keys, key)
delete(elm.keys, key)
}
}
elm.mu.Unlock()
if elm.expired != nil {
for _, key := range keys {
elm.expired(key)
}
}
}
func (elm *etcdLeaseManager) releaseUnlocked(key string) {
leaseID := elm.keys[key]
if leaseID != client.NoLease {
if info := elm.leases[leaseID]; info != nil && info.count > 0 {
info.count--
}
delete(elm.keys, key)
}
}