Skip to content

Commit

Permalink
Merge pull request #1935 from ecordell/spanner-now-stale
Browse files Browse the repository at this point in the history
spanner: use stale reads for current_timestamp for optimized revision
  • Loading branch information
ecordell authored Jun 11, 2024
2 parents 2611fcb + 2b77bad commit 4434d80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/datastore/spanner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func RevisionQuantization(bucketSize time.Duration) Option {
}
}

// FollowerReadDelay is the time delay to apply to enable historial reads.
// FollowerReadDelay is the time delay to apply to enable historical reads.
//
// This value defaults to 0 seconds.
func FollowerReadDelay(delay time.Duration) Option {
Expand Down
29 changes: 14 additions & 15 deletions internal/datastore/spanner/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,27 @@ import (
"github.com/authzed/spicedb/pkg/datastore"
)

var ParseRevisionString = revisions.RevisionParser(revisions.Timestamp)
var (
ParseRevisionString = revisions.RevisionParser(revisions.Timestamp)
nowStmt = spanner.NewStatement("SELECT CURRENT_TIMESTAMP()")
)

func (sd *spannerDatastore) headRevisionInternal(ctx context.Context) (datastore.Revision, error) {
now, err := sd.now(ctx)
if err != nil {
func (sd *spannerDatastore) HeadRevision(ctx context.Context) (datastore.Revision, error) {
var timestamp time.Time
if err := sd.client.Single().Query(ctx, nowStmt).Do(func(r *spanner.Row) error {
return r.Columns(&timestamp)
}); err != nil {
return datastore.NoRevision, fmt.Errorf(errRevision, err)
}

return revisions.NewForTime(now), nil
return revisions.NewForTime(timestamp), nil
}

func (sd *spannerDatastore) HeadRevision(ctx context.Context) (datastore.Revision, error) {
return sd.headRevisionInternal(ctx)
}

func (sd *spannerDatastore) now(ctx context.Context) (time.Time, error) {
func (sd *spannerDatastore) staleHeadRevision(ctx context.Context) (datastore.Revision, error) {
var timestamp time.Time
if err := sd.client.Single().Query(ctx, spanner.NewStatement("SELECT CURRENT_TIMESTAMP()")).Do(func(r *spanner.Row) error {
if err := sd.client.Single().WithTimestampBound(spanner.ExactStaleness(sd.config.followerReadDelay)).Query(ctx, nowStmt).Do(func(r *spanner.Row) error {
return r.Columns(&timestamp)
}); err != nil {
return time.Time{}, err
return datastore.NoRevision, fmt.Errorf(errRevision, err)
}

return timestamp, nil
return revisions.NewForTime(timestamp), nil
}
6 changes: 5 additions & 1 deletion internal/datastore/spanner/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ func NewSpannerDatastore(ctx context.Context, database string, opts ...Option) (
cachedEstimatedBytesPerRelationshipLock: sync.RWMutex{},
tableSizesStatsTable: tableSizesStatsTable,
}
ds.RemoteClockRevisions.SetNowFunc(ds.headRevisionInternal)
// Optimized revision and revision checking use a stale read for the
// current timestamp.
// TODO: Still investigating whether a stale read can be used for
// HeadRevision for FullConsistency queries.
ds.RemoteClockRevisions.SetNowFunc(ds.staleHeadRevision)

return ds, nil
}
Expand Down
2 changes: 2 additions & 0 deletions tools/analyzers/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,8 @@ github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/sftp v1.13.1 h1:I2qBYMChEhIjOgazfJmV3/mZM256btk6wkCDRmW7JYs=
github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
Expand Down

0 comments on commit 4434d80

Please sign in to comment.