Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2018 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package etcd3 |
| 18 | |
| 19 | import ( |
| 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. |
| 32 | type 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. |
| 45 | func 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%. |
| 52 | func 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. |
| 62 | func (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. |
| 70 | func (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. |
| 96 | func (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 | } |