Browse Source

lease, leasehttp: add TTL() method

Fix https://github.com/coreos/etcd/issues/6595.
Gyu-Ho Lee 9 years ago
parent
commit
5adca4a720
3 changed files with 25 additions and 18 deletions
  1. 1 1
      lease/leasehttp/http.go
  2. 15 10
      lease/lessor.go
  3. 9 7
      lease/lessor_test.go

+ 1 - 1
lease/leasehttp/http.go

@@ -96,7 +96,7 @@ func (h *leaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 				Header:     &pb.ResponseHeader{},
 				Header:     &pb.ResponseHeader{},
 				ID:         lreq.LeaseTimeToLiveRequest.ID,
 				ID:         lreq.LeaseTimeToLiveRequest.ID,
 				TTL:        int64(l.Remaining().Seconds()),
 				TTL:        int64(l.Remaining().Seconds()),
-				GrantedTTL: l.TTL,
+				GrantedTTL: l.TTL(),
 			},
 			},
 		}
 		}
 		if lreq.LeaseTimeToLiveRequest.Keys {
 		if lreq.LeaseTimeToLiveRequest.Keys {

+ 15 - 10
lease/lessor.go

@@ -196,7 +196,7 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
 	// with longer TTL to reduce renew load.
 	// with longer TTL to reduce renew load.
 	l := &Lease{
 	l := &Lease{
 		ID:      id,
 		ID:      id,
-		TTL:     ttl,
+		ttl:     ttl,
 		itemSet: make(map[LeaseItem]struct{}),
 		itemSet: make(map[LeaseItem]struct{}),
 		revokec: make(chan struct{}),
 		revokec: make(chan struct{}),
 	}
 	}
@@ -208,8 +208,8 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
 		return nil, ErrLeaseExists
 		return nil, ErrLeaseExists
 	}
 	}
 
 
-	if l.TTL < le.minLeaseTTL {
-		l.TTL = le.minLeaseTTL
+	if l.ttl < le.minLeaseTTL {
+		l.ttl = le.minLeaseTTL
 	}
 	}
 
 
 	if le.isPrimary() {
 	if le.isPrimary() {
@@ -311,7 +311,7 @@ func (le *lessor) Renew(id LeaseID) (int64, error) {
 	}
 	}
 
 
 	l.refresh(0)
 	l.refresh(0)
-	return l.TTL, nil
+	return l.ttl, nil
 }
 }
 
 
 func (le *lessor) Lookup(id LeaseID) *Lease {
 func (le *lessor) Lookup(id LeaseID) *Lease {
@@ -470,7 +470,7 @@ func (le *lessor) initAndRecover() {
 		}
 		}
 		le.leaseMap[ID] = &Lease{
 		le.leaseMap[ID] = &Lease{
 			ID:  ID,
 			ID:  ID,
-			TTL: lpb.TTL,
+			ttl: lpb.TTL,
 			// itemSet will be filled in when recover key-value pairs
 			// itemSet will be filled in when recover key-value pairs
 			// set expiry to forever, refresh when promoted
 			// set expiry to forever, refresh when promoted
 			itemSet: make(map[LeaseItem]struct{}),
 			itemSet: make(map[LeaseItem]struct{}),
@@ -485,7 +485,7 @@ func (le *lessor) initAndRecover() {
 
 
 type Lease struct {
 type Lease struct {
 	ID  LeaseID
 	ID  LeaseID
-	TTL int64 // time to live in seconds
+	ttl int64 // time to live in seconds
 
 
 	itemSet map[LeaseItem]struct{}
 	itemSet map[LeaseItem]struct{}
 	// expiry time in unixnano
 	// expiry time in unixnano
@@ -493,14 +493,14 @@ type Lease struct {
 	revokec chan struct{}
 	revokec chan struct{}
 }
 }
 
 
-func (l Lease) expired() bool {
+func (l *Lease) expired() bool {
 	return l.Remaining() <= 0
 	return l.Remaining() <= 0
 }
 }
 
 
-func (l Lease) persistTo(b backend.Backend) {
+func (l *Lease) persistTo(b backend.Backend) {
 	key := int64ToBytes(int64(l.ID))
 	key := int64ToBytes(int64(l.ID))
 
 
-	lpb := leasepb.Lease{ID: int64(l.ID), TTL: int64(l.TTL)}
+	lpb := leasepb.Lease{ID: int64(l.ID), TTL: int64(l.ttl)}
 	val, err := lpb.Marshal()
 	val, err := lpb.Marshal()
 	if err != nil {
 	if err != nil {
 		panic("failed to marshal lease proto item")
 		panic("failed to marshal lease proto item")
@@ -511,9 +511,14 @@ func (l Lease) persistTo(b backend.Backend) {
 	b.BatchTx().Unlock()
 	b.BatchTx().Unlock()
 }
 }
 
 
+// TTL returns the TTL of the Lease.
+func (l *Lease) TTL() int64 {
+	return l.ttl
+}
+
 // refresh refreshes the expiry of the lease.
 // refresh refreshes the expiry of the lease.
 func (l *Lease) refresh(extend time.Duration) {
 func (l *Lease) refresh(extend time.Duration) {
-	l.expiry = time.Now().Add(extend + time.Second*time.Duration(l.TTL))
+	l.expiry = time.Now().Add(extend + time.Second*time.Duration(l.ttl))
 }
 }
 
 
 // forever sets the expiry of lease to be forever.
 // forever sets the expiry of lease to be forever.

+ 9 - 7
lease/lessor_test.go

@@ -140,13 +140,15 @@ func TestLessorRenew(t *testing.T) {
 	}
 	}
 
 
 	// manually change the ttl field
 	// manually change the ttl field
-	l.TTL = 10
+	le.mu.Lock()
+	l.ttl = 10
+	le.mu.Unlock()
 	ttl, err := le.Renew(l.ID)
 	ttl, err := le.Renew(l.ID)
 	if err != nil {
 	if err != nil {
 		t.Fatalf("failed to renew lease (%v)", err)
 		t.Fatalf("failed to renew lease (%v)", err)
 	}
 	}
-	if ttl != l.TTL {
-		t.Errorf("ttl = %d, want %d", ttl, l.TTL)
+	if ttl != l.ttl {
+		t.Errorf("ttl = %d, want %d", ttl, l.ttl)
 	}
 	}
 
 
 	l = le.Lookup(l.ID)
 	l = le.Lookup(l.ID)
@@ -211,13 +213,13 @@ func TestLessorRecover(t *testing.T) {
 	// Create a new lessor with the same backend
 	// Create a new lessor with the same backend
 	nle := newLessor(be, minLeaseTTL)
 	nle := newLessor(be, minLeaseTTL)
 	nl1 := nle.Lookup(l1.ID)
 	nl1 := nle.Lookup(l1.ID)
-	if nl1 == nil || nl1.TTL != l1.TTL {
-		t.Errorf("nl1 = %v, want nl1.TTL= %d", nl1.TTL, l1.TTL)
+	if nl1 == nil || nl1.ttl != l1.ttl {
+		t.Errorf("nl1 = %v, want nl1.ttl= %d", nl1.ttl, l1.ttl)
 	}
 	}
 
 
 	nl2 := nle.Lookup(l2.ID)
 	nl2 := nle.Lookup(l2.ID)
-	if nl2 == nil || nl2.TTL != l2.TTL {
-		t.Errorf("nl2 = %v, want nl2.TTL= %d", nl2.TTL, l2.TTL)
+	if nl2 == nil || nl2.ttl != l2.ttl {
+		t.Errorf("nl2 = %v, want nl2.ttl= %d", nl2.ttl, l2.ttl)
 	}
 	}
 }
 }