Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 23 additions & 26 deletions slo/src/TableService/SloTableContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public class SloTableContext : SloTableContext<TableClient>

protected override async Task Create(TableClient client, string createTableSql, int operationTimeout)
{
var response = await client.SessionExec(
async session => await session.ExecuteSchemeQuery(createTableSql,
new ExecuteSchemeQuerySettings { OperationTimeout = TimeSpan.FromSeconds(operationTimeout) }));
var response = await client.SessionExec(async session => await session.ExecuteSchemeQuery(createTableSql,
new ExecuteSchemeQuerySettings { OperationTimeout = TimeSpan.FromSeconds(operationTimeout) }));

response.Status.EnsureSuccess();
}
Expand All @@ -29,21 +28,20 @@ protected override async Task Create(TableClient client, string createTableSql,

var attempts = 0;

var response = await tableClient.SessionExec(
async session =>
var response = await tableClient.SessionExec(async session =>
{
attempts++;
var response = await session.ExecuteDataQuery(upsertSql, _txControl, parameters, querySettings);
if (response.Status.IsSuccess)
{
attempts++;
var response = await session.ExecuteDataQuery(upsertSql, _txControl, parameters, querySettings);
if (response.Status.IsSuccess)
{
return response;
}
return response;
}


errorsGauge?.WithLabels(response.Status.StatusCode.ToString(), "retried").Inc();
errorsGauge?.WithLabels(response.Status.StatusCode.ToString(), "retried").Inc();

return response;
});
return response;
});

return (attempts, response.Status.StatusCode);
}
Expand All @@ -56,22 +54,21 @@ protected override async Task Create(TableClient client, string createTableSql,

var attempts = 0;

var response = (ExecuteDataQueryResponse)await tableClient.SessionExec(
async session =>
var response = (ExecuteDataQueryResponse)await tableClient.SessionExec(async session =>
{
attempts++;
var response = await session.ExecuteDataQuery(selectSql, _txControl, parameters, querySettings);
if (response.Status.IsSuccess)
{
attempts++;
var response = await session.ExecuteDataQuery(selectSql, _txControl, parameters, querySettings);
if (response.Status.IsSuccess)
{
return response;
}
return response;
}

Logger.LogWarning("{}", response.Status.ToString());
Logger.LogWarning("{}", response.Status.ToString());

errorsGauge?.WithLabels(response.Status.StatusCode.StatusName(), "retried").Inc();
errorsGauge?.WithLabels(response.Status.StatusCode.StatusName(), "retried").Inc();

return response;
});
return response;
});

return (attempts, response.Status.StatusCode,
response.Status.IsSuccess ? response.Result.ResultSets[0].Rows[0][0].GetOptionalInt32() : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,16 @@ public override void InsertDataOperation_required_args_multiple_rows()
public override void InsertDataOperation_throws_for_unsupported_column_types()
=> Assert.Equal(
RelationalStrings.UnsupportedDataOperationStoreType("foo", "dbo.People.First Name"),
Assert.Throws<InvalidOperationException>(
() =>
Generate(
new InsertDataOperation
{
Table = "People",
Schema = "dbo",
Columns = ["First Name"],
ColumnTypes = ["foo"],
Values = new object?[,] { { null } }
})).Message);
Assert.Throws<InvalidOperationException>(() =>
Generate(
new InsertDataOperation
{
Table = "People",
Schema = "dbo",
Columns = ["First Name"],
ColumnTypes = ["foo"],
Values = new object?[,] { { null } }
})).Message);

public override void DeleteDataOperation_all_args()
{
Expand Down
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 @@
- Added `x-ydb-sdk-build-info` header to any RPC call.

## v0.16.0

- **Breaking Change**: `Ydb.Sdk.Yc.Auth` version <= 0.1.0 is not compatible with newer versions.
Expand Down
20 changes: 7 additions & 13 deletions src/Ydb.Sdk/src/Driver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Reflection;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.Extensions.Logging;
Expand All @@ -16,7 +15,6 @@ public sealed class Driver : BaseDriver
{
private const int AttemptDiscovery = 10;

private readonly string _sdkInfo;
private readonly GrpcChannelFactory _grpcChannelFactory;
private readonly EndpointPool _endpointPool;
private readonly ChannelPool<GrpcChannel> _channelPool;
Expand All @@ -35,10 +33,6 @@ public Driver(DriverConfig config, ILoggerFactory? loggerFactory = null)
_grpcChannelFactory
);

var version = Assembly.GetExecutingAssembly().GetName().Version;
var versionStr = version is null ? "unknown" : version.ToString(3);
_sdkInfo = $"ydb-dotnet-sdk/{versionStr}";

CredentialsProvider = Config.User != null
? new CachedCredentialsProvider(
new StaticCredentialsAuthClient(config, _grpcChannelFactory, LoggerFactory),
Expand Down Expand Up @@ -134,12 +128,10 @@ private async Task<Status> DiscoverEndpoints()
TransportTimeout = Config.EndpointDiscoveryTimeout
};

var options = await GetCallOptions(requestSettings);
options.Headers?.Add(Metadata.RpcSdkInfoHeader, _sdkInfo);

var response = await client.ListEndpointsAsync(
request: request,
options: options);
options: await GetCallOptions(requestSettings)
);

if (!response.Operation.Ready)
{
Expand Down Expand Up @@ -168,7 +160,8 @@ private async Task<Status> DiscoverEndpoints()

Logger.LogDebug(
"Successfully discovered endpoints: {EndpointsCount}, self location: {SelfLocation}, sdk info: {SdkInfo}",
resultProto.Endpoints.Count, resultProto.SelfLocation, _sdkInfo);
resultProto.Endpoints.Count, resultProto.SelfLocation, Config.SdkVersion
);

_endpointPool.Reset(resultProto.Endpoints
.Select(endpointSettings => new EndpointSettings(
Expand All @@ -189,15 +182,16 @@ private async Task PeriodicDiscovery()
try
{
await Task.Delay(Config.EndpointDiscoveryInterval);

_ = await DiscoverEndpoints();
}
catch (RpcException e)
{
Logger.LogWarning($"RPC error during endpoint discovery: {e.Status}");
Logger.LogWarning("RPC error during endpoint discovery: {Status}", e.Status);
}
catch (Exception e)
{
Logger.LogError($"Unexpected exception during session pool periodic check: {e}");
Logger.LogError(e, "Unexpected exception during session pool periodic check");
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/Ydb.Sdk/src/DriverConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography.X509Certificates;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using Ydb.Sdk.Auth;

namespace Ydb.Sdk;
Expand All @@ -21,6 +22,7 @@ public class DriverConfig
internal X509Certificate2Collection CustomServerCertificates { get; } = new();
internal TimeSpan EndpointDiscoveryInterval = TimeSpan.FromMinutes(1);
internal TimeSpan EndpointDiscoveryTimeout = TimeSpan.FromSeconds(10);
internal string SdkVersion { get; }

public DriverConfig(
string endpoint,
Expand All @@ -42,6 +44,10 @@ public DriverConfig(
{
CustomServerCertificates.AddRange(customServerCertificates);
}

var version = Assembly.GetExecutingAssembly().GetName().Version;
var versionStr = version is null ? "unknown" : version.ToString(3);
SdkVersion = $"ydb-dotnet-sdk/{versionStr}";
}

private static string FormatEndpoint(string endpoint)
Expand Down
3 changes: 2 additions & 1 deletion src/Ydb.Sdk/src/IDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ protected async ValueTask<CallOptions> GetCallOptions(GrpcRequestSettings settin
{
var meta = new Grpc.Core.Metadata
{
{ Metadata.RpcDatabaseHeader, Config.Database }
{ Metadata.RpcDatabaseHeader, Config.Database },
{ Metadata.RpcSdkInfoHeader, Config.SdkVersion }
};

if (CredentialsProvider != null)
Expand Down
4 changes: 2 additions & 2 deletions src/Ydb.Sdk/src/Services/Topic/Reader/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ public async Task CommitOffsetRange(OffsetsRange offsetsRange, long partitionSes
{
var tcsCommit = new TaskCompletionSource();

await using var register = _lifecycleReaderSessionCts.Token.Register(
() => tcsCommit.TrySetException(new ReaderException($"ReaderSession[{SessionId}] was deactivated"))
await using var register = _lifecycleReaderSessionCts.Token.Register(() =>
tcsCommit.TrySetException(new ReaderException($"ReaderSession[{SessionId}] was deactivated"))
);

var commitSending = new CommitSending(offsetsRange, tcsCommit);
Expand Down
45 changes: 22 additions & 23 deletions src/Ydb.Sdk/tests/Ado/YdbCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,18 @@ public void GetChars_WhenSelectText_MoveCharsToBuffer()
Assert.Equal($"dataOffset must be between 0 and {int.MaxValue}",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetChars(0, -1, null, 0, 6)).Message);
Assert.Equal($"dataOffset must be between 0 and {int.MaxValue}",
Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetChars(0, long.MaxValue, null, 0, 6)).Message);
Assert.Throws<IndexOutOfRangeException>(() =>
ydbDataReader.GetChars(0, long.MaxValue, null, 0, 6)).Message);

Assert.Equal("bufferOffset must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetChars(0, 0, bufferChars, -1, 6)).Message);
Assert.Equal("bufferOffset must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetChars(0, 0, bufferChars, -1, 6)).Message);
Assert.Equal("bufferOffset must be between 0 and 10",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetChars(0, 0, bufferChars, -1, 6)).Message);
Assert.Equal("bufferOffset must be between 0 and 10",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetChars(0, 0, bufferChars, -1, 6)).Message);

Assert.Equal("length must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetChars(0, 0, bufferChars, 3, -1)).Message);
Assert.Equal("bufferOffset must be between 0 and 5", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetChars(0, 0, bufferChars, 8, 5)).Message);
Assert.Equal("length must be between 0 and 10",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetChars(0, 0, bufferChars, 3, -1)).Message);
Assert.Equal("bufferOffset must be between 0 and 5",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetChars(0, 0, bufferChars, 8, 5)).Message);

Assert.Equal(6, ydbDataReader.GetChars(0, 0, bufferChars, 4, 6));
checkBuffer[4] = 'a';
Expand Down Expand Up @@ -330,19 +330,18 @@ public void GetBytes_WhenSelectBytes_MoveBytesToBuffer()
Assert.Equal(7, ydbDataReader.GetBytes(0, 4, null, 0, 6));
Assert.Equal($"dataOffset must be between 0 and {int.MaxValue}",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetBytes(0, -1, null, 0, 6)).Message);
Assert.Equal($"dataOffset must be between 0 and {int.MaxValue}",
Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetBytes(0, long.MaxValue, null, 0, 6)).Message);

Assert.Equal("bufferOffset must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetBytes(0, 0, bufferChars, -1, 6)).Message);
Assert.Equal("bufferOffset must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetBytes(0, 0, bufferChars, -1, 6)).Message);

Assert.Equal("length must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetBytes(0, 0, bufferChars, 3, -1)).Message);
Assert.Equal("bufferOffset must be between 0 and 5", Assert.Throws<IndexOutOfRangeException>(
() => ydbDataReader.GetBytes(0, 0, bufferChars, 8, 5)).Message);
Assert.Equal($"dataOffset must be between 0 and {int.MaxValue}", Assert.Throws<IndexOutOfRangeException>(() =>
ydbDataReader.GetBytes(0, long.MaxValue, null, 0, 6)).Message);

Assert.Equal("bufferOffset must be between 0 and 10",
Assert.Throws<IndexOutOfRangeException>(() => ydbDataReader.GetBytes(0, 0, bufferChars, -1, 6)).Message);
Assert.Equal("bufferOffset must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(() =>
ydbDataReader.GetBytes(0, 0, bufferChars, -1, 6)).Message);

Assert.Equal("length must be between 0 and 10", Assert.Throws<IndexOutOfRangeException>(() =>
ydbDataReader.GetBytes(0, 0, bufferChars, 3, -1)).Message);
Assert.Equal("bufferOffset must be between 0 and 5", Assert.Throws<IndexOutOfRangeException>(() =>
ydbDataReader.GetBytes(0, 0, bufferChars, 8, 5)).Message);

Assert.Equal(6, ydbDataReader.GetBytes(0, 0, bufferChars, 4, 6));
checkBuffer[4] = (byte)'a';
Expand Down
8 changes: 4 additions & 4 deletions src/Ydb.Sdk/tests/Ado/YdbConnectionStringBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void InitDefaultValues_WhenEmptyConstructorInvoke_ReturnDefaultConnection
[Fact]
public void InitConnectionStringBuilder_WhenUnexpectedKey_ThrowException()
{
Assert.Equal("Key doesn't support: unexpectedkey", Assert.Throws<ArgumentException>(
() => new YdbConnectionStringBuilder("UnexpectedKey=123;Port=2135;")).Message);
Assert.Equal("Key doesn't support: unexpectedkey", Assert.Throws<ArgumentException>(() =>
new YdbConnectionStringBuilder("UnexpectedKey=123;Port=2135;")).Message);

Assert.Equal("Key doesn't support: unexpectedkey", Assert.Throws<ArgumentException>(
() => new YdbConnectionStringBuilder { ConnectionString = "UnexpectedKey=123;Port=2135;" }).Message);
Assert.Equal("Key doesn't support: unexpectedkey", Assert.Throws<ArgumentException>(() =>
new YdbConnectionStringBuilder { ConnectionString = "UnexpectedKey=123;Port=2135;" }).Message);
}

[Fact]
Expand Down
5 changes: 2 additions & 3 deletions src/Ydb.Sdk/tests/Ado/YdbDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public async Task BasedIteration_WhenNotCallMethodRead_ThrowException()
public async Task CreateYdbDataReader_WhenAbortedStatus_ThrowException()
{
var statuses = new List<Status>();
Assert.Equal("Status: Aborted", (await Assert.ThrowsAsync<YdbException>(
() => YdbDataReader.CreateYdbDataReader(SingleEnumeratorFailed, statuses.Add)))
.Message);
Assert.Equal("Status: Aborted", (await Assert.ThrowsAsync<YdbException>(() =>
YdbDataReader.CreateYdbDataReader(SingleEnumeratorFailed, statuses.Add))).Message);
Assert.Single(statuses);
Assert.Equal(StatusCode.Aborted, statuses[0].StatusCode);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Ydb.Sdk/tests/Ado/YdbParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ public class TestDataGenerator : IEnumerable<object[]>
new object[] { new Data<DateTime?>(DbType.DateTime2, null, value => value.GetOptionalTimestamp()) },
new object[]
{
new Data<byte[]>(DbType.Binary, Encoding.ASCII.GetBytes("test str").ToArray(),
new Data<byte[]>(DbType.Binary, Encoding.ASCII.GetBytes("test str"),
value => value.GetString())
},
new object[]
{
new Data<byte[]?>(DbType.Binary, Encoding.ASCII.GetBytes("test str").ToArray(),
new Data<byte[]?>(DbType.Binary, Encoding.ASCII.GetBytes("test str"),
value => value.GetString(), true)
},
new object[] { new Data<byte[]?>(DbType.Binary, null, value => value.GetOptionalString()) },
Expand Down
8 changes: 4 additions & 4 deletions src/Ydb.Sdk/tests/Ado/YdbSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public async Task GetSchema_WhenTablesCollection_ReturnAllTables()
Assert.Equal(_table2, singleTable2.Rows[0]["table_name"].ToString());
Assert.Equal("TABLE", singleTable2.Rows[0]["table_type"].ToString());

await Assert.ThrowsAsync<YdbException>(
async () => await ydbConnection.GetSchemaAsync("Tables", new[] { "not_found", null })
await Assert.ThrowsAsync<YdbException>(async () =>
await ydbConnection.GetSchemaAsync("Tables", new[] { "not_found", null })
);
}

Expand Down Expand Up @@ -87,8 +87,8 @@ public async Task GetSchema_WhenTablesWithStatsCollection_ReturnAllTables()
Assert.NotNull(singleTable2.Rows[0]["modification_time"]);

// not found case
await Assert.ThrowsAsync<YdbException>(
async () => await ydbConnection.GetSchemaAsync("Tables", new[] { "not_found", null })
await Assert.ThrowsAsync<YdbException>(async () =>
await ydbConnection.GetSchemaAsync("Tables", new[] { "not_found", null })
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Ydb.Sdk/tests/Auth/StaticAuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ private async Task CheckAuth(string? passwordCreate, string? passwordAuth)
public async Task NoPasswordAuth() => await CheckAuth(null, null);

[Fact]
public async Task WrongPassword() => await Assert.ThrowsAsync<StatusUnsuccessfulException>(
async () => await CheckAuth("good_password", "wrong_password"));
public async Task WrongPassword() => await Assert.ThrowsAsync<StatusUnsuccessfulException>(async () =>
await CheckAuth("good_password", "wrong_password"));

[Fact]
public async Task NotExistAuth()
Expand All @@ -101,7 +101,7 @@ public async Task NotExistAuth()
database: "/local"
) { User = "notexists", Password = "nopass" };

await Assert.ThrowsAsync<StatusUnsuccessfulException>(
async () => await Driver.CreateInitialized(driverConfig, _loggerFactory));
await Assert.ThrowsAsync<StatusUnsuccessfulException>(async () =>
await Driver.CreateInitialized(driverConfig, _loggerFactory));
}
}
Loading
Loading