Skip to content

Commit

Permalink
Prevent the staleness of an optimized revision from exceeding the gc …
Browse files Browse the repository at this point in the history
…window
  • Loading branch information
josephschorr committed Feb 7, 2024
1 parent 5d0eb90 commit b656f36
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/datastore/revisions/remoteclock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ type RemoteClockRevisions struct {

// NewRemoteClockRevisions returns a RemoteClockRevisions for the given configuration
func NewRemoteClockRevisions(gcWindow, maxRevisionStaleness, followerReadDelay, quantization time.Duration) *RemoteClockRevisions {
// Ensure the max revision staleness never exceeds the GC window.
if maxRevisionStaleness > gcWindow {
log.Warn().
Dur("maxRevisionStaleness", maxRevisionStaleness).
Dur("gcWindow", gcWindow).
Msg("the configured maximum revision staleness exceeds the configured gc window, so capping to gcWindow")
maxRevisionStaleness = gcWindow - 1
}

revisions := &RemoteClockRevisions{
CachedOptimizedRevisions: NewCachedOptimizedRevisions(
maxRevisionStaleness,
Expand Down
39 changes: 39 additions & 0 deletions internal/datastore/revisions/remoteclock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,42 @@ func TestRemoteClockCheckRevisions(t *testing.T) {
})
}
}

func TestRemoteClockStalenessBeyondGC(t *testing.T) {
// Set a GC window of 1 hour.
gcWindow := 1 * time.Hour

// Set a max revision staleness of 100 hours, well in excess of the GC window.
maxRevisionStaleness := 100 * time.Hour

rcr := NewRemoteClockRevisions(gcWindow, maxRevisionStaleness, 0, 0)

remoteClock := clock.NewMock()
rcr.clockFn = remoteClock
rcr.SetNowFunc(func(ctx context.Context) (datastore.Revision, error) {
log.Debug().Stringer("now", remoteClock.Now()).Msg("current remote time")
return NewForTime(remoteClock.Now()), nil
})

// Set the current time to 1.
currentTime := int64(1)
remoteClock.Set(time.Unix(currentTime, 0))

// Call optimized revision.
optimized, err := rcr.OptimizedRevision(context.Background())
require.NoError(t, err)

// Ensure the optimized revision is not past the GC window.
err = rcr.CheckRevision(context.Background(), optimized)
require.NoError(t, err)

// Set the current time to 100001 to ensure the optimized revision is past the GC window.
remoteClock.Set(time.Unix(100001, 0))

newOptimized, err := rcr.OptimizedRevision(context.Background())
require.NoError(t, err)

// Ensure the new optimized revision is not past the GC window.
err = rcr.CheckRevision(context.Background(), newOptimized)
require.NoError(t, err)
}

0 comments on commit b656f36

Please sign in to comment.