From 152dcdba07c55c0ddec62fd290dee4b2e600fa0a Mon Sep 17 00:00:00 2001 From: fanmin shi Date: Mon, 21 Nov 2016 16:04:05 -0800 Subject: [PATCH] lease: use monotimer for lease lease uses monotimer to calculate its expiration. In this way, changing system time won't affect in lease expiration. FIX #6700 --- build | 4 ++-- lease/lessor.go | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/build b/build index b1fc8d43b97e..fe0267936fce 100755 --- a/build +++ b/build @@ -35,8 +35,8 @@ etcd_build() { if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi toggle_failpoints # Static compilation is useful when etcd is run in a container - CGO_ENABLED=0 go build $GO_BUILD_FLAGS -installsuffix cgo -ldflags "$GO_LDFLAGS" -o ${out}/etcd ${REPO_PATH}/cmd/etcd || return - CGO_ENABLED=0 go build $GO_BUILD_FLAGS -installsuffix cgo -ldflags "$GO_LDFLAGS" -o ${out}/etcdctl ${REPO_PATH}/cmd/etcdctl || return + go build $GO_BUILD_FLAGS -installsuffix cgo -ldflags "$GO_LDFLAGS" -o ${out}/etcd ${REPO_PATH}/cmd/etcd || return + go build $GO_BUILD_FLAGS -installsuffix cgo -ldflags "$GO_LDFLAGS" -o ${out}/etcdctl ${REPO_PATH}/cmd/etcdctl || return } etcd_setup_gopath() { diff --git a/lease/lessor.go b/lease/lessor.go index 45ed881b6734..126b4b031e63 100644 --- a/lease/lessor.go +++ b/lease/lessor.go @@ -22,6 +22,7 @@ import ( "sync" "time" + "github.com/ScaleFT/monotime" "github.com/coreos/etcd/lease/leasepb" "github.com/coreos/etcd/mvcc/backend" ) @@ -33,9 +34,9 @@ const ( var ( leaseBucketName = []byte("lease") - // do not use maxInt64 since it can overflow time which will add - // the offset of unix time (1970yr to seconds). - forever = time.Unix(math.MaxInt64>>1, 0) + + monoTimer = monotime.New() + forever = time.Duration(1>>63 - 1) ErrNotPrimary = errors.New("not a primary lessor") ErrLeaseNotFound = errors.New("lease not found") @@ -504,8 +505,8 @@ type Lease struct { ttl int64 // time to live in seconds itemSet map[LeaseItem]struct{} - // expiry time in unixnano - expiry time.Time + // expiry is time when lease should expire + expiry time.Duration revokec chan struct{} } @@ -534,7 +535,7 @@ func (l *Lease) TTL() int64 { // refresh refreshes the expiry of the lease. func (l *Lease) refresh(extend time.Duration) { - l.expiry = time.Now().Add(extend + time.Second*time.Duration(l.ttl)) + l.expiry = extend + monoTimer.Elapsed() + time.Duration(l.ttl)*time.Second } // forever sets the expiry of lease to be forever. @@ -551,7 +552,7 @@ func (l *Lease) Keys() []string { // Remaining returns the remaining time of the lease. func (l *Lease) Remaining() time.Duration { - return l.expiry.Sub(time.Now()) + return l.expiry - monoTimer.Elapsed() } type LeaseItem struct {