Skip to content

Commit bbe2d77

Browse files
committedJul 17, 2018
lease: Add LessorConfig and wire zap logger into Lessor
1 parent 75ac18c commit bbe2d77

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed
 

‎clientv3/snapshot/v3_snapshot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (s *v3Manager) saveDB() error {
373373
be := backend.NewDefaultBackend(dbpath)
374374

375375
// a lessor never timeouts leases
376-
lessor := lease.NewLessor(be, math.MaxInt64)
376+
lessor := lease.NewLessor(s.lg, be, lease.LessorConfig{MinLeaseTTL: math.MaxInt64})
377377

378378
mvs := mvcc.NewStore(s.lg, be, lessor, (*initIndex)(&commit))
379379
txn := mvs.Write()

‎etcdserver/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func NewServer(cfg ServerConfig) (srv *EtcdServer, err error) {
519519

520520
// always recover lessor before kv. When we recover the mvcc.KV it will reattach keys to its leases.
521521
// If we recover mvcc.KV first, it will attach the keys to the wrong lessor before it recovers.
522-
srv.lessor = lease.NewLessor(srv.be, int64(math.Ceil(minTTL.Seconds())))
522+
srv.lessor = lease.NewLessor(srv.getLogger(), srv.be, lease.LessorConfig{MinLeaseTTL: int64(math.Ceil(minTTL.Seconds()))})
523523
srv.kv = mvcc.New(srv.getLogger(), srv.be, srv.lessor, &srv.consistIndex)
524524
if beExist {
525525
kvindex := srv.kv.ConsistentIndex()

‎lease/leasehttp/http_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ import (
2424

2525
"github.com/coreos/etcd/lease"
2626
"github.com/coreos/etcd/mvcc/backend"
27+
"go.uber.org/zap"
2728
)
2829

2930
func TestRenewHTTP(t *testing.T) {
31+
lg := zap.NewNop()
3032
be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
3133
defer os.Remove(tmpPath)
3234
defer be.Close()
3335

34-
le := lease.NewLessor(be, int64(5))
36+
le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)})
3537
le.Promote(time.Second)
3638
l, err := le.Grant(1, int64(5))
3739
if err != nil {
@@ -51,11 +53,12 @@ func TestRenewHTTP(t *testing.T) {
5153
}
5254

5355
func TestTimeToLiveHTTP(t *testing.T) {
56+
lg := zap.NewNop()
5457
be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
5558
defer os.Remove(tmpPath)
5659
defer be.Close()
5760

58-
le := lease.NewLessor(be, int64(5))
61+
le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)})
5962
le.Promote(time.Second)
6063
l, err := le.Grant(1, int64(5))
6164
if err != nil {
@@ -92,11 +95,12 @@ func TestTimeToLiveHTTPTimeout(t *testing.T) {
9295
}
9396

9497
func testApplyTimeout(t *testing.T, f func(*lease.Lease, string) error) {
98+
lg := zap.NewNop()
9599
be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
96100
defer os.Remove(tmpPath)
97101
defer be.Close()
98102

99-
le := lease.NewLessor(be, int64(5))
103+
le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)})
100104
le.Promote(time.Second)
101105
l, err := le.Grant(1, int64(5))
102106
if err != nil {

‎lease/lessor.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/coreos/etcd/lease/leasepb"
2727
"github.com/coreos/etcd/mvcc/backend"
28+
"go.uber.org/zap"
2829
)
2930

3031
// NoLease is a special LeaseID representing the absence of a lease.
@@ -144,23 +145,30 @@ type lessor struct {
144145
stopC chan struct{}
145146
// doneC is a channel whose closure indicates that the lessor is stopped.
146147
doneC chan struct{}
148+
149+
lg *zap.Logger
150+
}
151+
152+
type LessorConfig struct {
153+
MinLeaseTTL int64
147154
}
148155

149-
func NewLessor(b backend.Backend, minLeaseTTL int64) Lessor {
150-
return newLessor(b, minLeaseTTL)
156+
func NewLessor(lg *zap.Logger, b backend.Backend, cfg LessorConfig) Lessor {
157+
return newLessor(lg, b, cfg)
151158
}
152159

153-
func newLessor(b backend.Backend, minLeaseTTL int64) *lessor {
160+
func newLessor(lg *zap.Logger, b backend.Backend, cfg LessorConfig) *lessor {
154161
l := &lessor{
155162
leaseMap: make(map[LeaseID]*Lease),
156163
itemMap: make(map[LeaseItem]LeaseID),
157164
leaseHeap: make(LeaseQueue, 0),
158165
b: b,
159-
minLeaseTTL: minLeaseTTL,
166+
minLeaseTTL: cfg.MinLeaseTTL,
160167
// expiredC is a small buffered chan to avoid unnecessary blocking.
161168
expiredC: make(chan []*Lease, 16),
162169
stopC: make(chan struct{}),
163170
doneC: make(chan struct{}),
171+
lg: lg,
164172
}
165173
l.initAndRecover()
166174

‎lease/lessor_bench_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/coreos/etcd/mvcc/backend"
22+
"go.uber.org/zap"
2223
)
2324

2425
func BenchmarkLessorFindExpired1(b *testing.B) { benchmarkLessorFindExpired(1, b) }
@@ -54,8 +55,9 @@ func BenchmarkLessorRevoke100000(b *testing.B) { benchmarkLessorRevoke(100000,
5455
func BenchmarkLessorRevoke1000000(b *testing.B) { benchmarkLessorRevoke(1000000, b) }
5556

5657
func benchmarkLessorFindExpired(size int, b *testing.B) {
58+
lg := zap.NewNop()
5759
be, tmpPath := backend.NewDefaultTmpBackend()
58-
le := newLessor(be, minLeaseTTL)
60+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
5961
defer le.Stop()
6062
defer cleanup(be, tmpPath)
6163
le.Promote(0)
@@ -71,8 +73,9 @@ func benchmarkLessorFindExpired(size int, b *testing.B) {
7173
}
7274

7375
func benchmarkLessorGrant(size int, b *testing.B) {
76+
lg := zap.NewNop()
7477
be, tmpPath := backend.NewDefaultTmpBackend()
75-
le := newLessor(be, minLeaseTTL)
78+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
7679
defer le.Stop()
7780
defer cleanup(be, tmpPath)
7881
for i := 0; i < size; i++ {
@@ -85,8 +88,9 @@ func benchmarkLessorGrant(size int, b *testing.B) {
8588
}
8689

8790
func benchmarkLessorRevoke(size int, b *testing.B) {
91+
lg := zap.NewNop()
8892
be, tmpPath := backend.NewDefaultTmpBackend()
89-
le := newLessor(be, minLeaseTTL)
93+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
9094
defer le.Stop()
9195
defer cleanup(be, tmpPath)
9296
for i := 0; i < size; i++ {
@@ -102,8 +106,9 @@ func benchmarkLessorRevoke(size int, b *testing.B) {
102106
}
103107

104108
func benchmarkLessorRenew(size int, b *testing.B) {
109+
lg := zap.NewNop()
105110
be, tmpPath := backend.NewDefaultTmpBackend()
106-
le := newLessor(be, minLeaseTTL)
111+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
107112
defer le.Stop()
108113
defer cleanup(be, tmpPath)
109114
for i := 0; i < size; i++ {

‎lease/lessor_test.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"github.com/coreos/etcd/mvcc/backend"
29+
"go.uber.org/zap"
2930
)
3031

3132
const (
@@ -37,11 +38,12 @@ const (
3738
// The granted lease should have a unique ID with a term
3839
// that is greater than minLeaseTTL.
3940
func TestLessorGrant(t *testing.T) {
41+
lg := zap.NewNop()
4042
dir, be := NewTestBackend(t)
4143
defer os.RemoveAll(dir)
4244
defer be.Close()
4345

44-
le := newLessor(be, minLeaseTTL)
46+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
4547
defer le.Stop()
4648
le.Promote(0)
4749

@@ -98,11 +100,12 @@ func TestLessorGrant(t *testing.T) {
98100
// TestLeaseConcurrentKeys ensures Lease.Keys method calls are guarded
99101
// from concurrent map writes on 'itemSet'.
100102
func TestLeaseConcurrentKeys(t *testing.T) {
103+
lg := zap.NewNop()
101104
dir, be := NewTestBackend(t)
102105
defer os.RemoveAll(dir)
103106
defer be.Close()
104107

105-
le := newLessor(be, minLeaseTTL)
108+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
106109
defer le.Stop()
107110
le.SetRangeDeleter(func() TxnDelete { return newFakeDeleter(be) })
108111

@@ -146,11 +149,12 @@ func TestLeaseConcurrentKeys(t *testing.T) {
146149
// the backend.
147150
// The revoked lease cannot be got from Lessor again.
148151
func TestLessorRevoke(t *testing.T) {
152+
lg := zap.NewNop()
149153
dir, be := NewTestBackend(t)
150154
defer os.RemoveAll(dir)
151155
defer be.Close()
152156

153-
le := newLessor(be, minLeaseTTL)
157+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
154158
defer le.Stop()
155159
var fd *fakeDeleter
156160
le.SetRangeDeleter(func() TxnDelete {
@@ -198,11 +202,12 @@ func TestLessorRevoke(t *testing.T) {
198202

199203
// TestLessorRenew ensures Lessor can renew an existing lease.
200204
func TestLessorRenew(t *testing.T) {
205+
lg := zap.NewNop()
201206
dir, be := NewTestBackend(t)
202207
defer be.Close()
203208
defer os.RemoveAll(dir)
204209

205-
le := newLessor(be, minLeaseTTL)
210+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
206211
defer le.Stop()
207212
le.Promote(0)
208213

@@ -234,12 +239,13 @@ func TestLessorRenew(t *testing.T) {
234239
func TestLessorRenewExtendPileup(t *testing.T) {
235240
oldRevokeRate := leaseRevokeRate
236241
defer func() { leaseRevokeRate = oldRevokeRate }()
242+
lg := zap.NewNop()
237243
leaseRevokeRate = 10
238244

239245
dir, be := NewTestBackend(t)
240246
defer os.RemoveAll(dir)
241247

242-
le := newLessor(be, minLeaseTTL)
248+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
243249
ttl := int64(10)
244250
for i := 1; i <= leaseRevokeRate*10; i++ {
245251
if _, err := le.Grant(LeaseID(2*i), ttl); err != nil {
@@ -258,7 +264,7 @@ func TestLessorRenewExtendPileup(t *testing.T) {
258264
bcfg.Path = filepath.Join(dir, "be")
259265
be = backend.New(bcfg)
260266
defer be.Close()
261-
le = newLessor(be, minLeaseTTL)
267+
le = newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
262268
defer le.Stop()
263269

264270
// extend after recovery should extend expiration on lease pile-up
@@ -283,11 +289,12 @@ func TestLessorRenewExtendPileup(t *testing.T) {
283289
}
284290

285291
func TestLessorDetach(t *testing.T) {
292+
lg := zap.NewNop()
286293
dir, be := NewTestBackend(t)
287294
defer os.RemoveAll(dir)
288295
defer be.Close()
289296

290-
le := newLessor(be, minLeaseTTL)
297+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
291298
defer le.Stop()
292299
le.SetRangeDeleter(func() TxnDelete { return newFakeDeleter(be) })
293300

@@ -323,11 +330,12 @@ func TestLessorDetach(t *testing.T) {
323330
// TestLessorRecover ensures Lessor recovers leases from
324331
// persist backend.
325332
func TestLessorRecover(t *testing.T) {
333+
lg := zap.NewNop()
326334
dir, be := NewTestBackend(t)
327335
defer os.RemoveAll(dir)
328336
defer be.Close()
329337

330-
le := newLessor(be, minLeaseTTL)
338+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
331339
defer le.Stop()
332340
l1, err1 := le.Grant(1, 10)
333341
l2, err2 := le.Grant(2, 20)
@@ -336,7 +344,7 @@ func TestLessorRecover(t *testing.T) {
336344
}
337345

338346
// Create a new lessor with the same backend
339-
nle := newLessor(be, minLeaseTTL)
347+
nle := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
340348
defer nle.Stop()
341349
nl1 := nle.Lookup(l1.ID)
342350
if nl1 == nil || nl1.ttl != l1.ttl {
@@ -350,13 +358,14 @@ func TestLessorRecover(t *testing.T) {
350358
}
351359

352360
func TestLessorExpire(t *testing.T) {
361+
lg := zap.NewNop()
353362
dir, be := NewTestBackend(t)
354363
defer os.RemoveAll(dir)
355364
defer be.Close()
356365

357366
testMinTTL := int64(1)
358367

359-
le := newLessor(be, testMinTTL)
368+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: testMinTTL})
360369
defer le.Stop()
361370

362371
le.Promote(1 * time.Second)
@@ -402,13 +411,14 @@ func TestLessorExpire(t *testing.T) {
402411
}
403412

404413
func TestLessorExpireAndDemote(t *testing.T) {
414+
lg := zap.NewNop()
405415
dir, be := NewTestBackend(t)
406416
defer os.RemoveAll(dir)
407417
defer be.Close()
408418

409419
testMinTTL := int64(1)
410420

411-
le := newLessor(be, testMinTTL)
421+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: testMinTTL})
412422
defer le.Stop()
413423

414424
le.Promote(1 * time.Second)
@@ -452,11 +462,12 @@ func TestLessorExpireAndDemote(t *testing.T) {
452462
}
453463

454464
func TestLessorMaxTTL(t *testing.T) {
465+
lg := zap.NewNop()
455466
dir, be := NewTestBackend(t)
456467
defer os.RemoveAll(dir)
457468
defer be.Close()
458469

459-
le := newLessor(be, minLeaseTTL)
470+
le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
460471
defer le.Stop()
461472

462473
_, err := le.Grant(1, MaxLeaseTTL+1)

0 commit comments

Comments
 (0)
Please sign in to comment.