feat(spanner): add option for LastStatement in transaction#11638
Merged
feat(spanner): add option for LastStatement in transaction#11638
Conversation
Adds an option to indicate that a statement is the last statement in a transaction. This allows Spanner to skip some validations during the execution of the statement, and instead rely on the validations during the Commit. This option will also be used by the database/sql driver for statements that are executed directly on a connection instead of in a transaction. This change also fixes a number of test cases so that the test time is reduced from 2 minutes to approx 5 seconds.
35e6ca5 to
69abc5f
Compare
olavloite
commented
Feb 22, 2025
|
|
||
| func (s *inMemSpannerServer) simulateExecutionTime(method string, req interface{}) (interface{}, error) { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() |
Contributor
Author
There was a problem hiding this comment.
Keeping this lock here prevented the server from being shut down if there was a request still waiting for an execution time to finish (e.g. if an execution time of 1 minute had been set for an RPC, then keeping this lock would prevent the tests from stopping the server for 1 minute, which again would mean that the test would take 1 minute).
rahul2393
approved these changes
Mar 3, 2025
olavloite
commented
Mar 3, 2025
Comment on lines
+1351
to
+1371
| if _, err := client.ReadWriteTransaction(context.Background(), func(ctx context.Context, tx *ReadWriteTransaction) error { | ||
| if _, err := tx.Update(context.Background(), NewStatement(UpdateBarSetFoo)); err != nil { | ||
| return err | ||
| } | ||
| if _, err := tx.UpdateWithOptions(context.Background(), NewStatement(UpdateBarSetFoo), QueryOptions{LastStatement: true}); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }); err != nil { | ||
| t.Fatalf("transaction failed: %v", err) | ||
| } | ||
| requests := drainRequestsFromServer(server.TestSpanner) | ||
| executeRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) | ||
| if g, w := len(executeRequests), 2; g != w { | ||
| t.Fatalf("num requests mismatch\n Got: %v\nWant: %v", g, w) | ||
| } | ||
| for i := 0; i < len(executeRequests); i++ { | ||
| if g, w := executeRequests[i].(*sppb.ExecuteSqlRequest).LastStatement, i == len(executeRequests)-1; g != w { | ||
| t.Fatalf("%d: last statement mismatch\n Got: %v\nWant: %v", i, g, w) | ||
| } | ||
| } |
Contributor
Author
There was a problem hiding this comment.
This test shows how to use the LastStatement option in the client library. Setting the LastStatement: true option causes the client library to include the option in the request, which again allows Spanner to optimize the transaction execution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an option to indicate that a statement is the last statement in a transaction. This allows Spanner to skip some validations during the execution of the statement, and instead rely on the validations during the Commit.
This option will also be used by the database/sql driver for statements that are executed directly on a connection instead of in a transaction.
This change also fixes a number of test cases so that the test time is reduced from 2 minutes to approx 5 seconds.