blob: dc06ac52077b2e1e0517ddbea26bdac67c08e543 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package etcd3
18
19import (
20 "context"
21 "sync"
22 "time"
23
24 "github.com/coreos/etcd/clientv3"
25)
26
27// leaseManager is used to manage leases requested from etcd. If a new write
28// needs a lease that has similar expiration time to the previous one, the old
29// lease will be reused to reduce the overhead of etcd, since lease operations
30// are expensive. In the implementation, we only store one previous lease,
31// since all the events have the same ttl.
32type leaseManager struct {
33 client *clientv3.Client // etcd client used to grant leases
34 leaseMu sync.Mutex
35 prevLeaseID clientv3.LeaseID
36 prevLeaseExpirationTime time.Time
37 // The period of time in seconds and percent of TTL that each lease is
38 // reused. The minimum of them is used to avoid unreasonably large
39 // numbers. We use var instead of const for testing purposes.
40 leaseReuseDurationSeconds int64
41 leaseReuseDurationPercent float64
42}
43
44// newDefaultLeaseManager creates a new lease manager using default setting.
45func newDefaultLeaseManager(client *clientv3.Client) *leaseManager {
46 return newLeaseManager(client, 60, 0.05)
47}
48
49// newLeaseManager creates a new lease manager with the number of buffered
50// leases, lease reuse duration in seconds and percentage. The percentage
51// value x means x*100%.
52func newLeaseManager(client *clientv3.Client, leaseReuseDurationSeconds int64, leaseReuseDurationPercent float64) *leaseManager {
53 return &leaseManager{
54 client: client,
55 leaseReuseDurationSeconds: leaseReuseDurationSeconds,
56 leaseReuseDurationPercent: leaseReuseDurationPercent,
57 }
58}
59
60// setLeaseReuseDurationSeconds is used for testing purpose. It is used to
61// reduce the extra lease duration to avoid unnecessary timeout in testing.
62func (l *leaseManager) setLeaseReuseDurationSeconds(duration int64) {
63 l.leaseMu.Lock()
64 defer l.leaseMu.Unlock()
65 l.leaseReuseDurationSeconds = duration
66}
67
68// GetLease returns a lease based on requested ttl: if the cached previous
69// lease can be reused, reuse it; otherwise request a new one from etcd.
70func (l *leaseManager) GetLease(ctx context.Context, ttl int64) (clientv3.LeaseID, error) {
71 now := time.Now()
72 l.leaseMu.Lock()
73 defer l.leaseMu.Unlock()
74 // check if previous lease can be reused
75 reuseDurationSeconds := l.getReuseDurationSecondsLocked(ttl)
76 valid := now.Add(time.Duration(ttl) * time.Second).Before(l.prevLeaseExpirationTime)
77 sufficient := now.Add(time.Duration(ttl+reuseDurationSeconds) * time.Second).After(l.prevLeaseExpirationTime)
78 if valid && sufficient {
79 return l.prevLeaseID, nil
80 }
81 // request a lease with a little extra ttl from etcd
82 ttl += reuseDurationSeconds
83 lcr, err := l.client.Lease.Grant(ctx, ttl)
84 if err != nil {
85 return clientv3.LeaseID(0), err
86 }
87 // cache the new lease id
88 l.prevLeaseID = lcr.ID
89 l.prevLeaseExpirationTime = now.Add(time.Duration(ttl) * time.Second)
90 return lcr.ID, nil
91}
92
93// getReuseDurationSecondsLocked returns the reusable duration in seconds
94// based on the configuration. Lock has to be acquired before calling this
95// function.
96func (l *leaseManager) getReuseDurationSecondsLocked(ttl int64) int64 {
97 reuseDurationSeconds := int64(l.leaseReuseDurationPercent * float64(ttl))
98 if reuseDurationSeconds > l.leaseReuseDurationSeconds {
99 reuseDurationSeconds = l.leaseReuseDurationSeconds
100 }
101 return reuseDurationSeconds
102}