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 { diff --git a/lease/lessor_test.go b/lease/lessor_test.go index 4cc28a9db212..3ae38cfc7eeb 100644 --- a/lease/lessor_test.go +++ b/lease/lessor_test.go @@ -26,7 +26,10 @@ import ( "github.com/coreos/etcd/mvcc/backend" ) -const minLeaseTTL = int64(5) +const ( + minLeaseTTL = int64(5) + minLeaseTTLDuration = time.Duration(minLeaseTTL) * time.Second +) // TestLessorGrant ensures Lessor can grant wanted lease. // The granted lease should have a unique ID with a term @@ -48,8 +51,9 @@ func TestLessorGrant(t *testing.T) { if !reflect.DeepEqual(gl, l) { t.Errorf("lease = %v, want %v", gl, l) } - if l.expiry.Sub(time.Now()) < time.Duration(minLeaseTTL)*time.Second-time.Second { - t.Errorf("term = %v, want at least %v", l.expiry.Sub(time.Now()), time.Duration(minLeaseTTL)*time.Second-time.Second) + minLeaseTTLDura + if l.Remaining() < minLeaseTTLDuration-time.Second { + t.Errorf("term = %v, want at least %v", l.Remaining(), minLeaseTTLDuration-time.Second) } nl, err := le.Grant(1, 1) @@ -152,7 +156,7 @@ func TestLessorRenew(t *testing.T) { } l = le.Lookup(l.ID) - if l.expiry.Sub(time.Now()) < 9*time.Second { + if l.Remaining() < 9*time.Second { t.Errorf("failed to renew the lease") } }