Skip to content

Commit dcc2428

Browse files
authored
Add: hint "session-balancer" (#462)
* Add: hint "session-balancer" * Added DisableServerBalancer: support for server-side load balancing of sessions. * Added server balancer disabling check in Query/SessionPool
1 parent 0ebd3e1 commit dcc2428

File tree

10 files changed

+62
-12
lines changed

10 files changed

+62
-12
lines changed

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Added DisableServerBalancer option to ADO.NET session creation; default false.
2+
13
## v0.20.1
24

35
- Fixed bug ADO.NET: `YdbSchema.SchemaObjects` and `Ydb.DescribeTable`methods are public for `EntityFrameworkCore.Ydb`.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ private void InitDefaultValues()
3838
_maxReceiveMessageSize = GrpcDefaultSettings.MaxReceiveMessageSize;
3939
_disableDiscovery = GrpcDefaultSettings.DisableDiscovery;
4040
_createSessionTimeout = SessionPoolDefaultSettings.CreateSessionTimeoutSeconds;
41+
_disableServerBalancer = false;
4142
}
4243

4344
public string Host
@@ -236,6 +237,18 @@ public int MaxReceiveMessageSize
236237

237238
private int _maxReceiveMessageSize;
238239

240+
public bool DisableServerBalancer
241+
{
242+
get => _disableServerBalancer;
243+
set
244+
{
245+
_disableServerBalancer = value;
246+
SaveValue(nameof(DisableServerBalancer), value);
247+
}
248+
}
249+
250+
private bool _disableServerBalancer;
251+
239252
public bool DisableDiscovery
240253
{
241254
get => _disableDiscovery;
@@ -433,6 +446,9 @@ static YdbConnectionOption()
433446
AddOption(new YdbConnectionOption<int>(IntExtractor,
434447
(builder, createSessionTimeout) => builder.CreateSessionTimeout = createSessionTimeout),
435448
"CreateSessionTimeout", "Create Session Timeout");
449+
AddOption(new YdbConnectionOption<bool>(BoolExtractor, (builder, disableServerBalancer) =>
450+
builder.DisableServerBalancer = disableServerBalancer),
451+
"DisableServerBalancer", "Disable Server Balancer");
436452
}
437453

438454
private static void AddOption(YdbConnectionOption option, params string[] keys)

src/Ydb.Sdk/src/GrpcRequestSettings.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections.Immutable;
2-
using Google.Protobuf.WellKnownTypes;
1+
using Google.Protobuf.WellKnownTypes;
32
using Ydb.Operations;
43

54
namespace Ydb.Sdk;
@@ -8,9 +7,9 @@ public class GrpcRequestSettings
87
{
98
public string TraceId { get; set; } = string.Empty;
109
public TimeSpan TransportTimeout { get; set; } = TimeSpan.Zero;
11-
public ImmutableArray<string> CustomClientHeaders { get; } = new();
1210
public CancellationToken CancellationToken = default;
1311

12+
internal List<string> ClientCapabilities { get; } = new();
1413
internal long NodeId { get; set; }
1514
internal Action<Grpc.Core.Metadata> TrailersHandler { get; set; } = _ => { };
1615
}

src/Ydb.Sdk/src/IDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ protected async ValueTask<CallOptions> GetCallOptions(GrpcRequestSettings settin
182182
meta.Add(Metadata.RpcTraceIdHeader, settings.TraceId);
183183
}
184184

185+
foreach (var clientCapabilitiesHeader in settings.ClientCapabilities)
186+
{
187+
meta.Add(Metadata.RpcClientCapabilitiesHeader, clientCapabilitiesHeader);
188+
}
189+
185190
var options = new CallOptions(headers: meta, cancellationToken: settings.CancellationToken);
186191

187192
if (settings.TransportTimeout != TimeSpan.Zero)

src/Ydb.Sdk/src/Metadata.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ internal static class Metadata
88
public const string RpcTraceIdHeader = "x-ydb-trace-id";
99
public const string RpcSdkInfoHeader = "x-ydb-sdk-build-info";
1010
public const string RpcServerHintsHeader = "x-ydb-server-hints";
11+
public const string RpcClientCapabilitiesHeader = "x-ydb-client-capabilities";
1112

1213
public const string GracefulShutdownHint = "session-close";
1314
}

src/Ydb.Sdk/src/Pool/SessionPool.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ internal abstract class SessionPool<TSession> where TSession : SessionBase<TSess
1212
private readonly int _createSessionTimeoutMs;
1313
private readonly int _size;
1414

15+
protected readonly SessionPoolConfig Config;
1516
protected readonly ILogger<SessionPool<TSession>> Logger;
1617

1718
private volatile int _waitingCount;
1819
private volatile bool _disposed;
1920

20-
protected SessionPool(ILogger<SessionPool<TSession>> logger, SessionPoolConfig sessionPoolConfig)
21+
protected SessionPool(ILogger<SessionPool<TSession>> logger, SessionPoolConfig config)
2122
{
2223
Logger = logger;
23-
_size = sessionPoolConfig.MaxSessionPool;
24-
_createSessionTimeoutMs = sessionPoolConfig.CreateSessionTimeout * 1000;
24+
Config = config;
25+
_size = config.MaxSessionPool;
26+
_createSessionTimeoutMs = config.CreateSessionTimeout * 1000;
2527
_semaphore = new SemaphoreSlim(_size);
2628
}
2729

@@ -253,12 +255,15 @@ internal TS MakeGrpcRequestSettings<TS>(TS settings) where TS : GrpcRequestSetti
253255
internal record SessionPoolConfig(
254256
int MaxSessionPool = SessionPoolDefaultSettings.MaxSessionPool,
255257
int CreateSessionTimeout = SessionPoolDefaultSettings.CreateSessionTimeoutSeconds,
256-
bool DisposeDriver = false
258+
bool DisposeDriver = false,
259+
bool DisableServerBalancer = SessionPoolDefaultSettings.DisableServerBalancer
257260
);
258261

259262
internal static class SessionPoolDefaultSettings
260263
{
261264
internal const int MaxSessionPool = 100;
262265

263266
internal const int CreateSessionTimeoutSeconds = 5;
267+
268+
internal const bool DisableServerBalancer = false;
264269
}

src/Ydb.Sdk/src/Services/Query/SessionPool.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ protected override async Task<Session> CreateSession(
3535
CancellationToken cancellationToken = default
3636
)
3737
{
38+
var requestSettings = new GrpcRequestSettings
39+
{
40+
CancellationToken = cancellationToken
41+
};
42+
43+
if (!Config.DisableServerBalancer)
44+
{
45+
requestSettings.ClientCapabilities.Add("session-balancer");
46+
}
47+
3848
var response = await _driver.UnaryCall(
3949
QueryService.CreateSessionMethod,
4050
CreateSessionRequest,
41-
new GrpcRequestSettings { CancellationToken = cancellationToken }
51+
requestSettings
4252
);
4353

4454
Status.FromProto(response.Status, response.Issues).EnsureSuccess();

src/Ydb.Sdk/src/Services/Sessions/SessionPoolBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Logging;
22
using Ydb.Sdk.Client;
3+
using Ydb.Sdk.Pool;
34

45
namespace Ydb.Sdk.Services.Sessions;
56

@@ -16,6 +17,8 @@ public SessionPoolConfig(uint? sizeLimit = null)
1617
public TimeSpan PeriodicCheckInterval { get; set; } = TimeSpan.FromSeconds(10);
1718
public TimeSpan KeepAliveTimeout { get; set; } = TimeSpan.FromSeconds(1);
1819
public TimeSpan CreateSessionTimeout { get; set; } = TimeSpan.FromSeconds(1);
20+
21+
public bool DisableServerBalancer { get; set; } = SessionPoolDefaultSettings.DisableServerBalancer;
1922
}
2023

2124
public class GetSessionResponse<TSession> : ResponseWithResultBase<TSession>, IDisposable where TSession : SessionBase

src/Ydb.Sdk/src/Services/Table/SessionPool.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ public SessionPool(Driver driver, SessionPoolConfig config) :
2323

2424
private protected override async Task<GetSessionResponse> CreateSession()
2525
{
26-
var createSessionResponse = await _tableClient.CreateSession(new CreateSessionSettings
26+
var createSessionSettings = new CreateSessionSettings
2727
{
2828
TransportTimeout = Config.CreateSessionTimeout,
2929
OperationTimeout = Config.CreateSessionTimeout
30-
});
30+
};
31+
32+
if (!Config.DisableServerBalancer)
33+
{
34+
createSessionSettings.ClientCapabilities.Add("session-balancer");
35+
}
36+
37+
var createSessionResponse = await _tableClient.CreateSession(createSessionSettings);
3138

3239
lock (Lock)
3340
{

src/Ydb.Sdk/tests/Ado/YdbConnectionStringBuilderTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void InitDefaultValues_WhenEmptyConstructorInvoke_ReturnDefaultConnection
2424
Assert.Equal(64 * 1024 * 1024, ydbConnectionStringBuilder.MaxSendMessageSize);
2525
Assert.Equal(64 * 1024 * 1024, ydbConnectionStringBuilder.MaxReceiveMessageSize);
2626
Assert.False(ydbConnectionStringBuilder.DisableDiscovery);
27+
Assert.False(ydbConnectionStringBuilder.DisableServerBalancer);
2728
Assert.Equal(5, ydbConnectionStringBuilder.CreateSessionTimeout);
2829
}
2930

@@ -45,7 +46,7 @@ public void InitConnectionStringBuilder_WhenExpectedKeys_ReturnUpdatedConnection
4546
"ConnectTimeout=30;KeepAlivePingDelay=30;KeepAlivePingTimeout=60;" +
4647
"EnableMultipleHttp2Connections=true;CreateSessionTimeout=30;" +
4748
"MaxSendMessageSize=1000000;MaxReceiveMessageSize=1000000;" +
48-
"DisableDiscovery=true"
49+
"DisableDiscovery=true;DisableServerBalancer=true"
4950
);
5051

5152
Assert.Equal(2135, connectionString.Port);
@@ -64,8 +65,9 @@ public void InitConnectionStringBuilder_WhenExpectedKeys_ReturnUpdatedConnection
6465
"ConnectTimeout=30;KeepAlivePingDelay=30;KeepAlivePingTimeout=60;" +
6566
"EnableMultipleHttp2Connections=True;CreateSessionTimeout=30;" +
6667
"MaxSendMessageSize=1000000;MaxReceiveMessageSize=1000000;" +
67-
"DisableDiscovery=True", connectionString.ConnectionString);
68+
"DisableDiscovery=True;DisableServerBalancer=True", connectionString.ConnectionString);
6869
Assert.True(connectionString.DisableDiscovery);
70+
Assert.True(connectionString.DisableServerBalancer);
6971
Assert.Equal(30, connectionString.CreateSessionTimeout);
7072
}
7173

0 commit comments

Comments
 (0)