-
Notifications
You must be signed in to change notification settings - Fork 401
feat: Spanner.V1 BatchWrite #15122
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
Closed
robertvoinescu-work
wants to merge
1
commit into
googleapis:main
from
robertvoinescu-work:users/robertvoinescu/spanner-batchwrite
Closed
feat: Spanner.V1 BatchWrite #15122
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...loud.Spanner.Data/Google.Cloud.Spanner.Data.IntegrationTests/V1/BatchWriteTableFixture.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using Google.Api.Gax.Grpc; | ||
| using Google.Cloud.Spanner.V1; | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Google.Cloud.Spanner.Data.IntegrationTests; | ||
|
|
||
| [CollectionDefinition(nameof(BatchWriteTableFixture))] | ||
| public class BatchWriteTableFixture : CommonDataTableFixture, ICollectionFixture<BatchWriteTableFixture>, IAsyncLifetime | ||
| { | ||
| private const int TimeoutSeconds = 60; | ||
| private SessionPoolManager _poolManager; | ||
| private SessionPool _pool; | ||
|
|
||
| /// <summary> | ||
| /// The name of the key column in the table. | ||
| /// </summary> | ||
| public readonly string KeyName = "Key"; | ||
|
|
||
| public BatchWriteTableFixture() : base("BatchWrite") | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void CreateTable() | ||
| { | ||
| ExecuteDdl($@"CREATE TABLE {this.TableName} ( | ||
| {this.KeyName} STRING(256), | ||
| ) PRIMARY KEY ({this.KeyName})"); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void PopulateTable(bool fresh) | ||
| { | ||
| // Do nothing. The BatchWriteTests don't need pre-populated data. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes the session pool for the fixture. | ||
| /// </summary> | ||
| public async Task InitializeAsync() | ||
| { | ||
| _poolManager = SessionPoolManager.Create(new SessionPoolOptions()); | ||
| _pool = await _poolManager.AcquireSessionPoolAsync(SpannerClientCreationOptions); | ||
| } | ||
|
|
||
| public Task<PooledSession> GetPooledSessionAsync(TransactionOptions transactionOptions = default) => | ||
| _pool.AcquireSessionAsync(DatabaseName, transactionOptions ?? new TransactionOptions(), CancellationToken.None); | ||
|
|
||
| /// <summary> | ||
| /// Gets the call settings used to interact with the database. | ||
| /// </summary> | ||
| public CallSettings GetCallSettings() | ||
| { | ||
| using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(TimeoutSeconds)); | ||
| using SpannerConnection connection = GetConnection(); | ||
| return connection.CreateCallSettings(settings => settings.BatchWriteSettings, TimeoutSeconds, cts.Token); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task DisposeAsync() | ||
| { | ||
| _poolManager.Release(_pool); | ||
| return Task.CompletedTask; | ||
| } | ||
| } |
167 changes: 167 additions & 0 deletions
167
...oogle.Cloud.Spanner.Data/Google.Cloud.Spanner.Data.IntegrationTests/V1/BatchWriteTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using Google.Cloud.Spanner.Data.IntegrationTests; | ||
| using Google.Protobuf.WellKnownTypes; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using System.Collections.Generic; | ||
| using System; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Google.Cloud.Spanner.V1.IntegrationTests; | ||
|
|
||
| [CommonTestDiagnostics] | ||
| [Collection(nameof(BatchWriteTableFixture))] | ||
| public class BatchWriteTests | ||
| { | ||
| private record BatchWriteResponseAnalysis(int successCount, int failureCount); | ||
|
|
||
| private readonly BatchWriteTableFixture _fixture; | ||
| private const int TimeoutSeconds = 60; | ||
|
|
||
| public BatchWriteTests(BatchWriteTableFixture fixture) => | ||
| _fixture = fixture; | ||
|
|
||
| [Fact] | ||
| public async Task BatchWrite_Success() | ||
| { | ||
| // Create two non-conflicting write mutations | ||
| PooledSession pooledSession = await _fixture.GetPooledSessionAsync(); | ||
| var mutationGroup = new BatchWriteRequest.Types.MutationGroup(); | ||
| mutationGroup.Mutations.AddRange([ | ||
| new Mutation | ||
| { | ||
| Insert = new Mutation.Types.Write | ||
| { | ||
| Table = _fixture.TableName, | ||
| Columns = { _fixture.KeyName }, | ||
| Values = { new ListValue { Values = { Value.ForString(Guid.NewGuid().ToString()) } } } | ||
| } | ||
| }, | ||
| new Mutation | ||
| { | ||
| Insert = new Mutation.Types.Write | ||
| { | ||
| Table = _fixture.TableName, | ||
| Columns = { _fixture.KeyName }, | ||
| Values = { new ListValue { Values = { Value.ForString(Guid.NewGuid().ToString()) } } } | ||
| } | ||
| } | ||
| ]); | ||
|
|
||
| BatchWriteRequest batchWriteRequest = new() | ||
| { | ||
| Session = pooledSession.Session.Name, | ||
| MutationGroups = { mutationGroup } | ||
| }; | ||
|
|
||
| IAsyncEnumerable<BatchWriteResponse> responseStream = pooledSession.BatchWriteAsync(batchWriteRequest, _fixture.GetCallSettings()); | ||
| BatchWriteResponseAnalysis responseAnalysis = await GetResponseAnalysis(responseStream); | ||
|
|
||
| // The single mutation group will succeed as one atomic unit | ||
| Assert.Equal(0, responseAnalysis.failureCount); | ||
| Assert.Equal(1, responseAnalysis.successCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BatchWrite_Failure_Conflict() | ||
| { | ||
| // Create a valid and a conflicting write mutation group. | ||
| PooledSession pooledSession = await _fixture.GetPooledSessionAsync(); | ||
| var mutation = new Mutation | ||
| { | ||
| Insert = new Mutation.Types.Write | ||
| { | ||
| Table = _fixture.TableName, | ||
| Columns = { _fixture.KeyName }, | ||
| Values = { new ListValue { Values = { Value.ForString("Conflict because matching write.") } } }, | ||
| } | ||
| }; | ||
|
|
||
| var mutationGroupNoConflict = new BatchWriteRequest.Types.MutationGroup(); | ||
| var mutationGroupConflict = new BatchWriteRequest.Types.MutationGroup(); | ||
| mutationGroupConflict.Mutations.AddRange([mutation, mutation]); | ||
| mutationGroupNoConflict.Mutations.AddRange([mutation]); | ||
|
|
||
| BatchWriteRequest batchWriteRequest = new() | ||
| { | ||
| Session = pooledSession.Session.Name, | ||
| MutationGroups = { mutationGroupConflict, mutationGroupNoConflict } | ||
| }; | ||
|
|
||
| // Send the batch write request and parse the response | ||
| IAsyncEnumerable<BatchWriteResponse> responseStream = pooledSession.BatchWriteAsync(batchWriteRequest, _fixture.GetCallSettings()); | ||
| BatchWriteResponseAnalysis responseAnalysis = await GetResponseAnalysis(responseStream); | ||
|
|
||
| // The mutation group with a conflict will result in one failure | ||
| Assert.Equal(1, responseAnalysis.failureCount); | ||
| // The mutation group without a conflict will result in one success | ||
| Assert.Equal(1, responseAnalysis.successCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BatchWrite_Failure_TransactionModeNotNone() | ||
| { | ||
| // Any transaction associated with the session will cause the batchwrite to throw an exception | ||
| var options = new TransactionOptions() | ||
| { | ||
| ReadWrite = new TransactionOptions.Types.ReadWrite() | ||
| }; | ||
| PooledSession pooledSession = await _fixture.GetPooledSessionAsync(options); | ||
|
|
||
| var mutationGroup = new BatchWriteRequest.Types.MutationGroup(); | ||
| mutationGroup.Mutations.AddRange([ | ||
| new Mutation | ||
| { | ||
| Insert = new Mutation.Types.Write | ||
| { | ||
| Table = _fixture.TableName, | ||
| Columns = { _fixture.KeyName }, | ||
| Values = { new ListValue { Values = { Value.ForString(Guid.NewGuid().ToString()) } } } | ||
| } | ||
| }]); | ||
|
|
||
| BatchWriteRequest batchWriteRequest = new() | ||
| { | ||
| Session = pooledSession.Session.Name, | ||
| MutationGroups = { mutationGroup } | ||
| }; | ||
|
|
||
| Assert.Throws<InvalidOperationException>(() => | ||
| pooledSession.BatchWriteAsync(batchWriteRequest, _fixture.GetCallSettings())); | ||
| } | ||
|
|
||
| private static async Task<BatchWriteResponseAnalysis> GetResponseAnalysis(IAsyncEnumerable<BatchWriteResponse> responseStream) | ||
| { | ||
| var successCount = 0; | ||
| var failureCount = 0; | ||
| string result = ""; | ||
| await foreach(var response in responseStream) | ||
| { | ||
| // STATUS CODE of 0 is a success | ||
| if (response.Status.Code == 0) | ||
| { | ||
| successCount += response.Indexes.Count; | ||
| } | ||
| else | ||
| { | ||
| failureCount += response.Indexes.Count; | ||
| } | ||
| result += JsonConvert.SerializeObject(response); | ||
| } | ||
|
|
||
| return new BatchWriteResponseAnalysis(successCount: successCount, failureCount: failureCount); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,4 +141,4 @@ async Task<TResult> ImplAsync() | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.