Skip to content

Commit a429aea

Browse files
authored
fix(spanner): transaction_tag should be set on BeginTransactionRequest (#13463)
When using multiplexed sessions, the transaction_tag should also be set on the BeginTransactionRequest.
1 parent 21c9dbf commit a429aea

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

spanner/client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6319,6 +6319,26 @@ func TestClient_ReadWriteTransactionWithTag_AbortedOnce(t *testing.T) {
63196319
if g != w {
63206320
t.Fatalf("mutations count mismatch\nGot: %v\nWant: %v", g, w)
63216321
}
6322+
// Verify that the BeginTransaction requests also contain the transaction tag.
6323+
// This is required when using multiplexed sessions.
6324+
for _, index := range []int{1, 3, 5} {
6325+
beginReq, ok := requests[index].(*sppb.BeginTransactionRequest)
6326+
if !ok {
6327+
t.Fatalf("%d is not a BeginTransactionRequest", index)
6328+
}
6329+
if beginReq.RequestOptions == nil {
6330+
t.Fatalf("%d has no RequestOptions", index)
6331+
}
6332+
var want string
6333+
if index < 5 {
6334+
want = "test-tag1"
6335+
} else {
6336+
want = "test-tag2"
6337+
}
6338+
if g, w := beginReq.RequestOptions.TransactionTag, want; g != w {
6339+
t.Fatalf("%d: transaction tag mismatch\n Got: %v\nWant: %v", index, g, w)
6340+
}
6341+
}
63226342
}
63236343

63246344
func TestClient_ReadWriteTransactionWithTag_SessionNotFound(t *testing.T) {

spanner/transaction.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,8 +1730,7 @@ func beginTransaction(ctx context.Context, opts transactionBeginOptions) (transa
17301730
if opts.multiplexEnabled {
17311731
readWriteOptions.MultiplexedSessionPreviousTransactionId = opts.previousTx
17321732
}
1733-
1734-
res, err := opts.client.BeginTransaction(ctx, &sppb.BeginTransactionRequest{
1733+
request := &sppb.BeginTransactionRequest{
17351734
Session: opts.sessionID,
17361735
Options: &sppb.TransactionOptions{
17371736
Mode: &sppb.TransactionOptions_ReadWrite_{
@@ -1741,7 +1740,13 @@ func beginTransaction(ctx context.Context, opts transactionBeginOptions) (transa
17411740
IsolationLevel: opts.txOptions.IsolationLevel,
17421741
},
17431742
MutationKey: opts.mutation,
1744-
})
1743+
}
1744+
// When using multiplexed sessions, the BeginTransaction request must include the transaction tag (if any).
1745+
if opts.multiplexEnabled && opts.txOptions.TransactionTag != "" {
1746+
request.RequestOptions = &sppb.RequestOptions{TransactionTag: opts.txOptions.TransactionTag}
1747+
}
1748+
1749+
res, err := opts.client.BeginTransaction(ctx, request)
17451750
if err != nil {
17461751
return nil, nil, err
17471752
}

spanner/transaction_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,27 @@ func TestApply_RetryOnAbort(t *testing.T) {
206206
Insert("Accounts", []string{"AccountId"}, []interface{}{int64(1)}),
207207
}
208208

209-
if _, e := client.Apply(ctx, ms); e != nil {
209+
if _, e := client.Apply(ctx, ms, TransactionTag("my_tag")); e != nil {
210210
t.Fatalf("ReadWriteTransaction retry on abort, got %v, want nil.", e)
211211
}
212212

213-
if _, err := shouldHaveReceived(server.TestSpanner, []interface{}{
213+
if reqs, err := shouldHaveReceived(server.TestSpanner, []interface{}{
214214
&sppb.BatchCreateSessionsRequest{},
215215
&sppb.BeginTransactionRequest{},
216216
&sppb.CommitRequest{}, // First commit fails.
217217
&sppb.BeginTransactionRequest{},
218218
&sppb.CommitRequest{}, // Second commit succeeds.
219219
}); err != nil {
220220
t.Fatal(err)
221+
} else {
222+
if g, w := reqs[1].(*sppb.BeginTransactionRequest).RequestOptions.TransactionTag, "my_tag"; g != w {
223+
t.Fatalf("transaction tag mismatch\n Got: %v\nWant: %v", g, w)
224+
}
225+
if g, w := reqs[3].(*sppb.BeginTransactionRequest).RequestOptions.TransactionTag, "my_tag"; g != w {
226+
t.Fatalf("transaction tag mismatch\n Got: %v\nWant: %v", g, w)
227+
}
221228
}
229+
222230
}
223231

224232
// Tests that SessionNotFound errors are retried.

0 commit comments

Comments
 (0)