Merge pull request #9519 from gyuho/heap

lease: remove unnecessary O(log N) heap operation when nothing is expiry
This commit is contained in:
Gyuho Lee 2018-04-03 15:22:52 -07:00 committed by GitHub
commit d8dc29e5d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 16 deletions

View File

@ -14,8 +14,9 @@
package lease
// LeaseWithTime contains lease object with expire information.
type LeaseWithTime struct {
leaseId LeaseID
id LeaseID
expiration int64
index int
}

59
lease/lease_queue_test.go Normal file
View File

@ -0,0 +1,59 @@
// Copyright 2018 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lease
import (
"container/heap"
"testing"
"time"
)
func TestLeaseQueue(t *testing.T) {
le := &lessor{
leaseHeap: make(LeaseQueue, 0),
leaseMap: make(map[LeaseID]*Lease),
}
heap.Init(&le.leaseHeap)
// insert in reverse order of expiration time
for i := 50; i >= 1; i-- {
exp := time.Now().Add(time.Hour).UnixNano()
if i == 1 {
exp = time.Now().UnixNano()
}
le.leaseMap[LeaseID(i)] = &Lease{ID: LeaseID(i)}
heap.Push(&le.leaseHeap, &LeaseWithTime{id: LeaseID(i), expiration: exp})
}
// first element must be front
if le.leaseHeap[0].id != LeaseID(1) {
t.Fatalf("first item expected lease ID %d, got %d", LeaseID(1), le.leaseHeap[0].id)
}
l, ok, more := le.expireExists()
if l.ID != 1 {
t.Fatalf("first item expected lease ID %d, got %d", 1, l.ID)
}
if !ok {
t.Fatal("expect expiry lease exists")
}
if more {
t.Fatal("expect no more expiry lease")
}
if le.leaseHeap.Len() != 49 {
t.Fatalf("expected lease heap pop, got %d", le.leaseHeap.Len())
}
}

View File

@ -235,7 +235,7 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
}
le.leaseMap[id] = l
item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
heap.Push(&le.leaseHeap, item)
l.persistTo(le.b)
@ -319,7 +319,7 @@ func (le *lessor) Renew(id LeaseID) (int64, error) {
}
l.refresh(0)
item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
heap.Push(&le.leaseHeap, item)
return l.ttl, nil
}
@ -355,7 +355,7 @@ func (le *lessor) Promote(extend time.Duration) {
// refresh the expiries of all leases.
for _, l := range le.leaseMap {
l.refresh(extend)
item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
heap.Push(&le.leaseHeap, item)
}
@ -392,7 +392,7 @@ func (le *lessor) Promote(extend time.Duration) {
delay := time.Duration(rateDelay)
nextWindow = baseWindow + delay
l.refresh(delay + extend)
item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
heap.Push(&le.leaseHeap, item)
}
}
@ -521,28 +521,50 @@ func (le *lessor) runLoop() {
}
}
// expireExists returns true if expiry items exist.
// It pops only when expiry item exists.
// "next" is true, to indicate that it may exist in next attempt.
func (le *lessor) expireExists() (l *Lease, ok bool, next bool) {
if le.leaseHeap.Len() == 0 {
return nil, false, false
}
item := le.leaseHeap[0]
l = le.leaseMap[item.id]
if l == nil {
// lease has expired or been revoked
// no need to revoke (nothing is expiry)
heap.Pop(&le.leaseHeap) // O(log N)
return nil, false, true
}
if time.Now().UnixNano() < item.expiration {
// Candidate expirations are caught up, reinsert this item
// and no need to revoke (nothing is expiry)
return l, false, false
}
// if the lease is actually expired, add to the removal list. If it is not expired, we can ignore it because another entry will have been inserted into the heap
heap.Pop(&le.leaseHeap) // O(log N)
return l, true, false
}
// findExpiredLeases loops leases in the leaseMap until reaching expired limit
// and returns the expired leases that needed to be revoked.
func (le *lessor) findExpiredLeases(limit int) []*Lease {
leases := make([]*Lease, 0, 16)
for {
if le.leaseHeap.Len() == 0 {
l, ok, next := le.expireExists()
if !ok && !next {
break
}
item := heap.Pop(&le.leaseHeap).(*LeaseWithTime)
l := le.leaseMap[item.leaseId]
if l == nil {
// lease has expired or been revoked, continue
if !ok {
continue
}
if time.Now().UnixNano() < item.expiration {
// Candidate expirations are caught up, reinsert this item
heap.Push(&le.leaseHeap, item)
break
if next {
continue
}
// if the lease is actually expired, add to the removal list. If it is not expired, we can ignore it because another entry will have been inserted into the heap
if l.expired() {
leases = append(leases, l)