Skip to content

Commit

Permalink
lease: remove 'promote' field (to be squashed)
Browse files Browse the repository at this point in the history
Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Jun 15, 2017
1 parent a926cff commit 505cbbf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
22 changes: 6 additions & 16 deletions lease/lessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ type lessor struct {
stopC chan struct{}
// doneC is a channel whose closure indicates that the lessor is stopped.
doneC chan struct{}

// 'true' when lessor just started promoting after recovery
promoting bool
}

func NewLessor(b backend.Backend, minLeaseTTL int64) Lessor {
Expand All @@ -164,10 +161,9 @@ func newLessor(b backend.Backend, minLeaseTTL int64) *lessor {
b: b,
minLeaseTTL: minLeaseTTL,
// expiredC is a small buffered chan to avoid unnecessary blocking.
expiredC: make(chan []*Lease, 16),
stopC: make(chan struct{}),
doneC: make(chan struct{}),
promoting: true,
expiredC: make(chan []*Lease, 16),
stopC: make(chan struct{}),
doneC: make(chan struct{}),
}
l.initAndRecover()

Expand Down Expand Up @@ -328,19 +324,13 @@ func (le *lessor) Promote(extend time.Duration) {
defer le.mu.Unlock()

le.demotec = make(chan struct{})
promoting := le.promoting

// refresh the expiries of all leases.
for _, l := range le.leaseMap {
ext := extend
if promoting {
// randomize expiry with 士10%, otherwise leases of same TTL
// will expire all at the same time,
ext += computeRandomDelta(l.ttl)
}
l.refresh(ext)
// randomize expiry with 士10%, otherwise leases of same TTL
// will expire all at the same time,
l.refresh(extend + computeRandomDelta(l.ttl))
}
le.promoting = false
}

func computeRandomDelta(seconds int64) time.Duration {
Expand Down
13 changes: 1 addition & 12 deletions lease/lessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestLessorRenewRandomize(t *testing.T) {

now := monotime.Now()

// first extend after recovery should randomize expiries
// extend after recovery should randomize expiries
le.Promote(0)

for _, l := range le.leaseMap {
Expand All @@ -244,17 +244,6 @@ func TestLessorRenewRandomize(t *testing.T) {
t.Fatalf("expected randomized expiry, got %d seconds (ttl: 3600)", leftSeconds)
}
}

// second extend should not be randomized
le.Promote(0)

for _, l := range le.leaseMap {
leftSeconds := uint64(float64(l.expiry-now) * float64(1e-9))
pc := (float64(leftSeconds-3600) / float64(3600)) * 100
if pc > .5 || pc < -.5 { // should be close to original ttl 3600
t.Fatalf("expected 3600-sec left, got %d", leftSeconds)
}
}
}

func TestLessorDetach(t *testing.T) {
Expand Down

0 comments on commit 505cbbf

Please sign in to comment.