Merge pull request #3719 from hashicorp/issue-3670

Sheds monotonic time info so tombstone GC bins work properly.
This commit is contained in:
James Phillips 2017-11-29 10:37:10 -08:00 committed by GitHub
commit 83095d6178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -135,7 +135,9 @@ func (t *TombstoneGC) PendingExpiration() bool {
// granularity that is set. This allows us to bin expirations and avoid a ton // granularity that is set. This allows us to bin expirations and avoid a ton
// of timers. // of timers.
func (t *TombstoneGC) nextExpires() time.Time { func (t *TombstoneGC) nextExpires() time.Time {
expires := time.Now().Add(t.ttl) // The Round(0) call here is to shed the monotonic time so that we
// can safely use these as map keys. See #3670 for more details.
expires := time.Now().Add(t.ttl).Round(0)
remain := expires.UnixNano() % int64(t.granularity) remain := expires.UnixNano() % int64(t.granularity)
adj := expires.Add(t.granularity - time.Duration(remain)) adj := expires.Add(t.granularity - time.Duration(remain))
return adj return adj

View File

@ -1,7 +1,6 @@
package state package state
import ( import (
"os"
"testing" "testing"
"time" "time"
) )
@ -24,10 +23,6 @@ func TestTombstoneGC_invalid(t *testing.T) {
} }
func TestTombstoneGC(t *testing.T) { func TestTombstoneGC(t *testing.T) {
if os.Getenv("TRAVIS") == "true" {
t.Skip("GC test is flaky on travis-ci (see #3670)")
}
ttl := 20 * time.Millisecond ttl := 20 * time.Millisecond
gran := 5 * time.Millisecond gran := 5 * time.Millisecond
gc, err := NewTombstoneGC(ttl, gran) gc, err := NewTombstoneGC(ttl, gran)
@ -65,6 +60,14 @@ func TestTombstoneGC(t *testing.T) {
gc.Hint(120) gc.Hint(120)
gc.Hint(125) gc.Hint(125)
// Check that we only have a single bin (this cross-checks #3670).
gc.Lock()
bins := len(gc.expires)
gc.Unlock()
if got, want := bins, 1; got != want {
t.Fatalf("got %d want %d", got, want)
}
if !gc.PendingExpiration() { if !gc.PendingExpiration() {
t.Fatalf("should be pending") t.Fatalf("should be pending")
} }