-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ambient transactions support commit delays
- Loading branch information
1 parent
af15aaf
commit f4b4208
Showing
6 changed files
with
323 additions
and
12 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...ogle.Cloud.Spanner.Data/Google.Cloud.Spanner.Data.Tests/AmbientTransactionOptionsTests.cs
This file contains 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,50 @@ | ||
// Copyright 2024 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 System; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Spanner.Data.Tests; | ||
public class AmbientTransactionOptionsTests | ||
{ | ||
[Fact] | ||
public void Default_Singleton() | ||
{ | ||
var one = AmbientTransactionOptions.Default; | ||
Assert.NotNull(one); | ||
var two = AmbientTransactionOptions.Default; | ||
Assert.Same(one, two); | ||
} | ||
|
||
[Fact] | ||
public void Default_Values() => | ||
Assert.Null(AmbientTransactionOptions.Default.CommitDelay); | ||
|
||
public static TheoryData<TimeSpan?> ValidCommitDelayValues => SpannerTransactionTests.ValidCommitDelayValues; | ||
|
||
[Theory, MemberData(nameof(ValidCommitDelayValues))] | ||
public void WithCommitDelay_Valid(TimeSpan? commitDelay) | ||
{ | ||
var custom = AmbientTransactionOptions.Default.WithCommitDelay(commitDelay); | ||
Assert.NotSame(AmbientTransactionOptions.Default, custom); | ||
Assert.Equal(commitDelay, custom.CommitDelay); | ||
} | ||
|
||
public static TheoryData<TimeSpan?> InvalidCommitDelayValues => SpannerTransactionTests.InvalidCommitDelayValues; | ||
|
||
[Theory, MemberData(nameof(InvalidCommitDelayValues))] | ||
public void WithCommitDelay_Invalid(TimeSpan? commitDelay) => | ||
Assert.Throws<ArgumentOutOfRangeException>(() => AmbientTransactionOptions.Default.WithCommitDelay(commitDelay)); | ||
|
||
} |
This file contains 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 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
48 changes: 48 additions & 0 deletions
48
apis/Google.Cloud.Spanner.Data/Google.Cloud.Spanner.Data/AmbientTransactionOptions.cs
This file contains 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,48 @@ | ||
// Copyright 2024 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 System; | ||
|
||
namespace Google.Cloud.Spanner.Data; | ||
|
||
/// <summary> | ||
/// Options for a <see cref="SpannerTransaction"/> that is enlisted in <see cref="System.Transactions.Transaction"/>. | ||
/// </summary> | ||
public sealed class AmbientTransactionOptions | ||
{ | ||
/// <summary> | ||
/// Default options for an ambient transaction. Using these options will result in a read-write transaction | ||
/// with no <see cref="CommitDelay"/> value set. | ||
/// </summary> | ||
public static AmbientTransactionOptions Default { get; } = new AmbientTransactionOptions(null); | ||
|
||
/// <summary> | ||
/// The maximum amount of time the commit may be delayed server side for batching with other commits. | ||
/// The bigger the delay, the better the throughput (QPS), but at the expense of commit latency. | ||
/// If set to <see cref="TimeSpan.Zero"/>, commit batching is disabled. | ||
/// May be null, in which case commits will continue to be batched as they had been before this configuration | ||
/// option was made available to Spanner API consumers. | ||
/// May be set to any value between <see cref="TimeSpan.Zero"/> and 500ms. | ||
/// </summary> | ||
public TimeSpan? CommitDelay { get; } | ||
|
||
private AmbientTransactionOptions(TimeSpan? commitDelay) => | ||
CommitDelay = SpannerTransaction.CheckCommitDelayRange(commitDelay); | ||
|
||
/// <summary> | ||
/// Returns a new set of options equal to these, except for the specified <paramref name="commitDelay"/>. | ||
/// </summary> | ||
public AmbientTransactionOptions WithCommitDelay(TimeSpan? commitDelay) => | ||
new AmbientTransactionOptions(commitDelay); | ||
} |
This file contains 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.