Skip to content

Commit 5e156cc

Browse files
init commit
1 parent 8ece963 commit 5e156cc

File tree

13 files changed

+347
-105
lines changed

13 files changed

+347
-105
lines changed

src/Ydb.Sdk/src/Ado/RetryPolicy/YdbRetryPolicyConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public class YdbRetryPolicyConfig
1515
public int SlowCapBackoffMs { get; init; } = 5_000;
1616

1717
public bool EnableRetryIdempotence { get; init; } = false;
18+
19+
public override string ToString() => $"MaxAttempt={MaxAttempt};" +
20+
$"FastBackoffBaseMs={FastBackoffBaseMs};" +
21+
$"SlowBackoffBaseMs={SlowBackoffBaseMs};" +
22+
$"FastCapBackoffMs={FastCapBackoffMs};" +
23+
$"SlowCapBackoffMs={SlowCapBackoffMs};" +
24+
$"EnableRetryIdempotence={EnableRetryIdempotence}";
1825
}

src/Ydb.Sdk/src/Ado/RetryPolicy/YdbRetryPolicyExecutor.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace Ydb.Sdk.Ado.RetryPolicy;
22

3-
internal class YdbRetryPolicyExecutor
3+
internal sealed class YdbRetryPolicyExecutor
44
{
5-
private readonly YdbRetryPolicy _retryPolicy;
5+
private readonly IRetryPolicy _retryPolicy;
66

7-
public YdbRetryPolicyExecutor(YdbRetryPolicy retryPolicy)
7+
public YdbRetryPolicyExecutor(IRetryPolicy retryPolicy)
88
{
99
_retryPolicy = retryPolicy;
1010
}
@@ -25,15 +25,19 @@ public YdbRetryPolicyExecutor(YdbRetryPolicy retryPolicy)
2525
/// first time or after retrying transient failures). If the task fails with a non-transient error or
2626
/// the retry limit is reached, the returned task will become faulted and the exception must be observed.
2727
/// </returns>
28-
public virtual Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> operation,
29-
CancellationToken cancellationToken = default) => ExecuteImplementationAsync(operation, cancellationToken);
28+
public Task<TResult> ExecuteAsync<TResult>(
29+
Func<CancellationToken, Task<TResult>> operation,
30+
CancellationToken cancellationToken = default
31+
) => ExecuteImplementationAsync(operation, cancellationToken);
3032

31-
public async Task ExecuteAsync(Func<CancellationToken, Task> operation, CancellationToken cancellationToken = new())
32-
=> await ExecuteImplementationAsync(async ct =>
33-
{
34-
await operation(ct).ConfigureAwait(false);
35-
return 0;
36-
}, cancellationToken).ConfigureAwait(false);
33+
public async Task ExecuteAsync(
34+
Func<CancellationToken, Task> operation,
35+
CancellationToken cancellationToken = default
36+
) => await ExecuteImplementationAsync(async ct =>
37+
{
38+
await operation(ct).ConfigureAwait(false);
39+
return 0;
40+
}, cancellationToken).ConfigureAwait(false);
3741

3842
private async Task<TResult> ExecuteImplementationAsync<TResult>(
3943
Func<CancellationToken, Task<TResult>> operation,
@@ -44,7 +48,7 @@ CancellationToken cancellationToken
4448
while (true)
4549
{
4650
cancellationToken.ThrowIfCancellationRequested();
47-
51+
4852
try
4953
{
5054
return await operation(cancellationToken).ConfigureAwait(false);
@@ -54,7 +58,7 @@ CancellationToken cancellationToken
5458
var delay = _retryPolicy.GetNextDelay(e, attempt++);
5559
if (delay == null)
5660
throw;
57-
61+
5862
await Task.Delay(delay.Value, cancellationToken).ConfigureAwait(false);
5963
}
6064
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Ydb.Query;
2+
3+
namespace Ydb.Sdk.Ado.Transaction;
4+
5+
internal static class TransactionExtensions
6+
{
7+
private static readonly TransactionSettings SerializableRw = new()
8+
{ SerializableReadWrite = new SerializableModeSettings() };
9+
10+
private static readonly TransactionSettings SnapshotRo = new()
11+
{ SnapshotReadOnly = new SnapshotModeSettings() };
12+
13+
private static readonly TransactionSettings StaleRo = new()
14+
{ StaleReadOnly = new StaleModeSettings() };
15+
16+
private static readonly TransactionSettings OnlineRo = new()
17+
{ OnlineReadOnly = new OnlineModeSettings { AllowInconsistentReads = false } };
18+
19+
private static readonly TransactionSettings OnlineInconsistentRo = new()
20+
{ OnlineReadOnly = new OnlineModeSettings { AllowInconsistentReads = true } };
21+
22+
internal static TransactionSettings TransactionSettings(this TransactionMode mode) =>
23+
mode switch
24+
{
25+
TransactionMode.SerializableRw => SerializableRw,
26+
TransactionMode.SnapshotRo => SnapshotRo,
27+
TransactionMode.StaleRo => StaleRo,
28+
TransactionMode.OnlineRo => OnlineRo,
29+
TransactionMode.OnlineInconsistentRo => OnlineInconsistentRo,
30+
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null)
31+
};
32+
33+
internal static TransactionControl TransactionControl(this TransactionMode mode, bool commit = true) =>
34+
new() { BeginTx = mode.TransactionSettings(), CommitTx = commit };
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ReSharper disable once CheckNamespace
2+
namespace Ydb.Sdk.Ado;
3+
4+
public enum TransactionMode
5+
{
6+
SerializableRw,
7+
SnapshotRo,
8+
StaleRo,
9+
10+
OnlineRo,
11+
OnlineInconsistentRo
12+
}

src/Ydb.Sdk/src/Ado/YdbConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Diagnostics.CodeAnalysis;
44
using Ydb.Sdk.Ado.BulkUpsert;
55
using Ydb.Sdk.Ado.Session;
6-
using Ydb.Sdk.Services.Query;
6+
using Ydb.Sdk.Ado.Transaction;
77
using static System.Data.IsolationLevel;
88

99
namespace Ydb.Sdk.Ado;
@@ -59,14 +59,14 @@ protected override YdbTransaction BeginDbTransaction(IsolationLevel isolationLev
5959

6060
return BeginTransaction(isolationLevel switch
6161
{
62-
Serializable or Unspecified => TxMode.SerializableRw,
62+
Serializable or Unspecified => TransactionMode.SerializableRw,
6363
_ => throw new ArgumentException("Unsupported isolationLevel: " + isolationLevel)
6464
});
6565
}
6666

6767
public new YdbTransaction BeginTransaction(IsolationLevel isolationLevel) => BeginDbTransaction(isolationLevel);
6868

69-
public YdbTransaction BeginTransaction(TxMode txMode = TxMode.SerializableRw)
69+
public YdbTransaction BeginTransaction(TransactionMode transactionMode = TransactionMode.SerializableRw)
7070
{
7171
ThrowIfConnectionClosed();
7272

@@ -77,7 +77,7 @@ public YdbTransaction BeginTransaction(TxMode txMode = TxMode.SerializableRw)
7777
);
7878
}
7979

80-
CurrentTransaction = new YdbTransaction(this, txMode);
80+
CurrentTransaction = new YdbTransaction(this, transactionMode);
8181

8282
return CurrentTransaction;
8383
}

src/Ydb.Sdk/src/Ado/YdbConnectionStringBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Security.Cryptography.X509Certificates;
44
using Microsoft.Extensions.Logging;
55
using Microsoft.Extensions.Logging.Abstractions;
6+
using Ydb.Sdk.Ado.RetryPolicy;
67
using Ydb.Sdk.Auth;
78
using Ydb.Sdk.Transport;
89

@@ -320,6 +321,10 @@ public int CreateSessionTimeout
320321

321322
public X509Certificate2Collection? ServerCertificates { get; init; }
322323

324+
public IRetryPolicy RetryPolicy { get; init; } = YdbRetryPolicy.Default;
325+
326+
internal YdbRetryPolicyExecutor YdbRetryPolicyExecutor => new(RetryPolicy);
327+
323328
private void SaveValue(string propertyName, object? value)
324329
{
325330
if (value == null)

0 commit comments

Comments
 (0)