Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimistic locking for records #320

Merged
merged 3 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
800 changes: 405 additions & 395 deletions api/open_saves.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions api/open_saves.proto
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ message Record {
// big and does not fit in the properties. This will not be indexed or
// queryable. The current size limit is 32KiB.
string opaque_string = 11;

// Signature is a server-generated unique UUID that is updated each time
// the server updates the record. The server returns the current signature
// for read requests. The client may optionally populate this field and send
// update requests, and the server will check the value against the latest value
// and abort the request if they don't match.
bytes signature = 12;
}

// Performance optimization hints for the server.
Expand Down
46 changes: 25 additions & 21 deletions api/open_saves_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ require (
google.golang.org/api v0.48.0
google.golang.org/genproto v0.0.0-20210614182748-5b3b54cad159 // indirect
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
Expand Down
18 changes: 15 additions & 3 deletions internal/app/server/open_saves.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func (s *openSavesServer) CreateStore(ctx context.Context, req *pb.CreateStoreRe
}

func (s *openSavesServer) CreateRecord(ctx context.Context, req *pb.CreateRecordRequest) (*pb.Record, error) {
record := record.FromProto(req.GetStoreKey(), req.GetRecord())
record, err := record.FromProto(req.GetStoreKey(), req.GetRecord())
if err != nil {
log.Errorf("Invalid record proto for store (%s), record (%s): %v", req.GetStoreKey(), req.GetRecord().GetKey(), err)
return nil, status.Errorf(codes.InvalidArgument, "Invalid record proto: %v", err)
}
newRecord, err := s.metaDB.InsertRecord(ctx, req.StoreKey, record)
if err != nil {
log.Warnf("CreateRecord failed for store (%s), record (%s): %v",
Expand Down Expand Up @@ -177,9 +181,17 @@ func (s *openSavesServer) GetRecord(ctx context.Context, req *pb.GetRecordReques
}

func (s *openSavesServer) UpdateRecord(ctx context.Context, req *pb.UpdateRecordRequest) (*pb.Record, error) {
updateTo := record.FromProto(req.GetStoreKey(), req.GetRecord())
updateTo, err := record.FromProto(req.GetStoreKey(), req.GetRecord())
if err != nil {
log.Errorf("Invalid proto for store (%s), record (%s): %v", req.GetStoreKey(), req.GetRecord().GetKey(), err)
return nil, status.Errorf(codes.InvalidArgument, "Invalid record proto: %v", err)
}
newRecord, err := s.metaDB.UpdateRecord(ctx, req.GetStoreKey(), updateTo.Key,
func(r *record.Record) (*record.Record, error) {
if updateTo.Timestamps.Signature != uuid.Nil && r.Timestamps.Signature != updateTo.Timestamps.Signature {
return nil, status.Errorf(codes.Aborted, "Signature mismatch: expected (%v), actual (%v)",
updateTo.Timestamps.Signature.String(), r.Timestamps.Signature.String())
}
r.OwnerID = updateTo.OwnerID
r.Properties = updateTo.Properties
r.Tags = updateTo.Tags
Expand All @@ -191,7 +203,7 @@ func (s *openSavesServer) UpdateRecord(ctx context.Context, req *pb.UpdateRecord
if err != nil {
log.Warnf("UpdateRecord failed for store(%s), record (%s): %v",
req.GetStoreKey(), req.GetRecord().GetKey(), err)
return nil, status.Convert(err).Err()
return nil, err
}

// Update cache store.
Expand Down
Loading