Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Ydb.Sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Disable Discovery mode: skip discovery step and client balancing and use connection to start endpoint ([#420](https://github.com/ydb-platform/ydb-dotnet-sdk/issues/420)).

## v0.17.0

- Shutdown channels which are removed from the EndpointPool after discovery calls.
Expand Down
66 changes: 42 additions & 24 deletions src/Ydb.Sdk/src/Ado/YdbConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Ydb.Sdk.Auth;
using Ydb.Sdk.Transport;

namespace Ydb.Sdk.Ado;

Expand Down Expand Up @@ -32,6 +34,7 @@ private void InitDefaultValues()
_enableMultipleHttp2Connections = false;
_maxSendMessageSize = GrpcDefaultSettings.MaxSendMessageSize;
_maxReceiveMessageSize = GrpcDefaultSettings.MaxReceiveMessageSize;
_disableDiscovery = false;
}

public string Host
Expand Down Expand Up @@ -213,6 +216,18 @@ public int MaxReceiveMessageSize

private int _maxReceiveMessageSize;

public bool DisableDiscovery
{
get => _disableDiscovery;
set
{
_disableDiscovery = value;
SaveValue(nameof(DisableDiscovery), value);
}
}

private bool _disableDiscovery;

public ILoggerFactory? LoggerFactory { get; init; }

public ICredentialsProvider? CredentialsProvider { get; init; }
Expand Down Expand Up @@ -257,33 +272,34 @@ public override object this[string keyword]

private string Endpoint => $"{(UseTls ? "grpcs" : "grpc")}://{Host}:{Port}";

internal Task<Driver> BuildDriver()
internal async Task<IDriver> BuildDriver()
{
var cert = RootCertificate != null ? X509Certificate.CreateFromCertFile(RootCertificate) : null;
var driverConfig = new DriverConfig(
endpoint: Endpoint,
database: Database,
credentials: CredentialsProvider,
customServerCertificate: cert,
customServerCertificates: ServerCertificates
)
{
KeepAlivePingDelay = KeepAlivePingDelay == 0
? Timeout.InfiniteTimeSpan
: TimeSpan.FromSeconds(KeepAlivePingDelay),
KeepAlivePingTimeout = KeepAlivePingTimeout == 0
? Timeout.InfiniteTimeSpan
: TimeSpan.FromSeconds(KeepAlivePingTimeout),
User = User,
Password = Password,
EnableMultipleHttp2Connections = EnableMultipleHttp2Connections,
MaxSendMessageSize = MaxSendMessageSize,
MaxReceiveMessageSize = MaxReceiveMessageSize
};
var loggerFactory = LoggerFactory ?? NullLoggerFactory.Instance;

return Driver.CreateInitialized(
new DriverConfig(
endpoint: Endpoint,
database: Database,
credentials: CredentialsProvider,
customServerCertificate: cert,
customServerCertificates: ServerCertificates
)
{
KeepAlivePingDelay = KeepAlivePingDelay == 0
? Timeout.InfiniteTimeSpan
: TimeSpan.FromSeconds(KeepAlivePingDelay),
KeepAlivePingTimeout = KeepAlivePingTimeout == 0
? Timeout.InfiniteTimeSpan
: TimeSpan.FromSeconds(KeepAlivePingTimeout),
User = User,
Password = Password,
EnableMultipleHttp2Connections = EnableMultipleHttp2Connections,
MaxSendMessageSize = MaxSendMessageSize,
MaxReceiveMessageSize = MaxReceiveMessageSize
},
LoggerFactory
);
return DisableDiscovery
? new DirectGrpcChannelDriver(driverConfig, loggerFactory)
: await Driver.CreateInitialized(driverConfig, loggerFactory);
}

public override void Clear()
Expand Down Expand Up @@ -369,6 +385,8 @@ static YdbConnectionOption()
AddOption(new YdbConnectionOption<int>(IntExtractor, (builder, maxReceiveMessageSize) =>
builder.MaxReceiveMessageSize = maxReceiveMessageSize),
"MaxReceiveMessageSize", "Max Receive Message Size");
AddOption(new YdbConnectionOption<bool>(BoolExtractor, (builder, disableDiscovery) =>
builder.DisableDiscovery = disableDiscovery), "DisableDiscovery", "Disable Discovery");
}

private static void AddOption(YdbConnectionOption option, params string[] keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private async Task<LoginResponse> Login()

try
{
await using var transport = new AuthGrpcChannelDriver(_config, _grpcChannelFactory, _loggerFactory);
await using var transport = new DirectGrpcChannelDriver(_config, _grpcChannelFactory, _loggerFactory);

var response = await transport.UnaryCall(
method: AuthService.LoginMethod,
Expand Down
8 changes: 4 additions & 4 deletions src/Ydb.Sdk/src/Services/Query/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ internal sealed class SessionPool : SessionPool<Session>, IAsyncDisposable
TransportTimeout = TimeSpan.FromMinutes(2)
};

private readonly Driver _driver;
private readonly IDriver _driver;
private readonly bool _disposingDriver;
private readonly ILogger<Session> _loggerSession;

internal SessionPool(Driver driver, int? maxSessionPool = null, bool disposingDriver = false)
internal SessionPool(IDriver driver, int? maxSessionPool = null, bool disposingDriver = false)
: base(driver.LoggerFactory.CreateLogger<SessionPool>(), maxSessionPool)
{
_driver = driver;
Expand Down Expand Up @@ -120,10 +120,10 @@ protected override async Task<Session> CreateSession()

internal class Session : SessionBase<Session>
{
internal Driver Driver { get; }
internal IDriver Driver { get; }

internal Session(
Driver driver,
IDriver driver,
SessionPool<Session> sessionPool,
string sessionId,
long nodeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace Ydb.Sdk.Transport;

internal class AuthGrpcChannelDriver : BaseDriver
public class DirectGrpcChannelDriver : BaseDriver
{
private readonly GrpcChannel _channel;

public AuthGrpcChannelDriver(
internal DirectGrpcChannelDriver(
DriverConfig driverConfig,
GrpcChannelFactory grpcChannelFactory,
ILoggerFactory loggerFactory
Expand All @@ -19,11 +19,16 @@ ILoggerFactory loggerFactory
endpoint: driverConfig.Endpoint,
database: driverConfig.Database,
customServerCertificates: driverConfig.CustomServerCertificates
), loggerFactory, loggerFactory.CreateLogger<AuthGrpcChannelDriver>())
), loggerFactory, loggerFactory.CreateLogger<DirectGrpcChannelDriver>())
{
_channel = grpcChannelFactory.CreateChannel(Config.Endpoint);
}

public DirectGrpcChannelDriver(DriverConfig driverConfig, ILoggerFactory loggerFactory) :
this(driverConfig, new GrpcChannelFactory(loggerFactory, driverConfig), loggerFactory)
{
}

protected override (string, GrpcChannel) GetChannel(long nodeId) => (Config.Endpoint, _channel);

protected override void OnRpcError(string endpoint, RpcException e)
Expand Down
5 changes: 4 additions & 1 deletion src/Ydb.Sdk/tests/Ado/YdbConnectionStringBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void InitDefaultValues_WhenEmptyConstructorInvoke_ReturnDefaultConnection
Assert.False(ydbConnectionStringBuilder.EnableMultipleHttp2Connections);
Assert.Equal(64 * 1024 * 1024, ydbConnectionStringBuilder.MaxSendMessageSize);
Assert.Equal(64 * 1024 * 1024, ydbConnectionStringBuilder.MaxReceiveMessageSize);
Assert.False(ydbConnectionStringBuilder.DisableDiscovery);
}

[Fact]
Expand All @@ -41,7 +42,8 @@ public void InitConnectionStringBuilder_WhenExpectedKeys_ReturnUpdatedConnection
new YdbConnectionStringBuilder("Host=server;Port=2135;Database=/my/path;User=Kirill;UseTls=true;" +
"KeepAlivePingDelay=30;KeepAlivePingTimeout=60;" +
"EnableMultipleHttp2Connections=true;" +
"MaxSendMessageSize=1000000;MaxReceiveMessageSize=1000000");
"MaxSendMessageSize=1000000;MaxReceiveMessageSize=1000000" +
"DisableDiscovery=true");

Assert.Equal(2135, connectionString.Port);
Assert.Equal("server", connectionString.Host);
Expand All @@ -58,6 +60,7 @@ public void InitConnectionStringBuilder_WhenExpectedKeys_ReturnUpdatedConnection
"KeepAlivePingDelay=30;KeepAlivePingTimeout=60;" +
"EnableMultipleHttp2Connections=True;" +
"MaxSendMessageSize=1000000;MaxReceiveMessageSize=1000000", connectionString.ConnectionString);
Assert.True(connectionString.DisableDiscovery);
}

[Fact]
Expand Down
10 changes: 10 additions & 0 deletions src/Ydb.Sdk/tests/Ado/YdbConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ INSERT INTO {tableName}
await ydbCommand.ExecuteNonQueryAsync();
}

[Fact]
public async Task DisableDiscovery_WhenPropertyIsTrue_SimpleWorking()
{
await using var connection = CreateConnection();
connection.ConnectionString += ";DisableDiscovery=true";
await connection.OpenAsync();
Assert.True((bool)(await new YdbCommand(connection) { CommandText = "SELECT TRUE;" }.ExecuteScalarAsync())!);
await YdbConnection.ClearPool(connection);
}

private List<Task> GenerateTasks() => Enumerable.Range(0, 100).Select(async i =>
{
await using var connection = await CreateOpenConnectionAsync();
Expand Down
Loading