Skip to content

Commit

Permalink
Merge #37165
Browse files Browse the repository at this point in the history
37165: raftentry: deflake TestConcurrentUpdates r=ajwerner a=ajwerner

I spent way too long looking at this concerned that it was a correctness
bug before I remembered that the gauges are updated with a snapshot of the
cache state but the writes are not totally ordered and thus can become out of sync
in the face of concurrent updates. Later updates will update to later values. This
isn't a correctness problem, just an artifact of the design of the metrics which the
test relied on. This PR adds logic to sync the gauges before verifying their state
which deflakes the test when run under stress.

Fixes #37111.

Release note: None

Co-authored-by: Andrew Werner <[email protected]>
  • Loading branch information
craig[bot] and ajwerner committed Apr 27, 2019
2 parents f42659d + 9026b0e commit dff4132
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/storage/raftentry/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func TestEntryCache(t *testing.T) {

func verifyMetrics(t *testing.T, c *Cache, expectedEntries, expectedBytes int64) {
t.Helper()
// NB: Cache gauges are updated asynchronously. In the face of concurrent
// updates gauges may not reflect the current cache state. Force
// synchrononization of the metrics before using them to validate cache state.
c.syncGauges()
if got := c.Metrics().Size.Value(); got != expectedEntries {
t.Errorf("expected cache to have %d entries, got %d", expectedEntries, got)
}
Expand All @@ -157,6 +161,10 @@ func verifyMetrics(t *testing.T, c *Cache, expectedEntries, expectedBytes int64)
}
}

func (c *Cache) syncGauges() {
c.updateGauges(c.addBytes(0), c.addEntries(0))
}

func TestIgnoredAdd(t *testing.T) {
defer leaktest.AfterTest(t)()
rangeID := roachpb.RangeID(1)
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/raftentry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (

// Metrics is the set of metrics for the raft entry cache.
type Metrics struct {
// NB: the values in the gauges are updated asynchronously and may hold stale
// values in the face of concurrent updates.
Size *metric.Gauge
Bytes *metric.Gauge
Accesses *metric.Counter
Expand Down

0 comments on commit dff4132

Please sign in to comment.