Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transport error on delete session #56

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- Fix timeout error on create session
- Fix transport error on delete session

## v0.1.4
- Add exception throwing when results truncated
- lint: add line feed at file end
Expand Down
10 changes: 6 additions & 4 deletions src/Ydb.Sdk/src/Services/Table/CreateSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Ydb.Sdk.Services.Table;

public class CreateSessionSettings : OperationRequestSettings
{
public bool DeleteOnDispose = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the flag will false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when field DeleteSessionsOnDispose in class SessionPoolConfig is false

}

public class CreateSessionResponse : ResponseWithResultBase<CreateSessionResponse.ResultData>
Expand All @@ -24,7 +25,8 @@ internal ResultData(Session session)

public Session Session { get; }

internal static ResultData FromProto(CreateSessionResult resultProto, Driver driver, string endpoint)
internal static ResultData FromProto(CreateSessionResult resultProto, Driver driver, string endpoint,
bool settingsDeleteOnDispose)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The settingsDeleteOnDispose arg not used

{
var session = new Session(
driver: driver,
Expand Down Expand Up @@ -57,13 +59,13 @@ public async Task<CreateSessionResponse> CreateSession(CreateSessionSettings? se
request: request,
settings: settings);

CreateSessionResult? resultProto;
var status = UnpackOperation(response.Data.Operation, out resultProto);
var status = UnpackOperation(response.Data.Operation, out CreateSessionResult? resultProto);

CreateSessionResponse.ResultData? result = null;
if (status.IsSuccess && resultProto != null)
{
result = CreateSessionResponse.ResultData.FromProto(resultProto, Driver, response.UsedEndpoint);
result = CreateSessionResponse.ResultData.FromProto(resultProto, Driver, response.UsedEndpoint,
settings.DeleteOnDispose);
}

return new CreateSessionResponse(status, result);
Expand Down
21 changes: 14 additions & 7 deletions src/Ydb.Sdk/src/Services/Table/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public partial class Session : ClientBase, IDisposable
private readonly ILogger _logger;
private bool _disposed;

internal Session(Driver driver, SessionPool? sessionPool, string id, string? endpoint)
private readonly bool _deleteOnDispose;

internal Session(Driver driver, SessionPool? sessionPool, string id, string? endpoint, bool deleteOnDispose = true)
: base(driver)
{
_sessionPool = sessionPool;
_logger = Driver.LoggerFactory.CreateLogger<Session>();
Id = id;
Endpoint = endpoint;
_deleteOnDispose = deleteOnDispose;
}

public string Id { get; }
Expand Down Expand Up @@ -73,13 +76,17 @@ protected virtual void Dispose(bool disposing)
{
if (_sessionPool is null)
{
_logger.LogTrace($"Closing detached session on dispose: {Id}");

var client = new TableClient(Driver, new NoPool());
_ = client.DeleteSession(Id, new DeleteSessionSettings
if (_deleteOnDispose)
{
TransportTimeout = DeleteSessionTimeout
});
_logger.LogTrace($"Closing detached session on dispose: {Id}");

var client = new TableClient(Driver, new NoPool());
var task = client.DeleteSession(Id, new DeleteSessionSettings
{
TransportTimeout = DeleteSessionTimeout
});
task.Wait();
}
}
else
{
Expand Down
36 changes: 23 additions & 13 deletions src/Ydb.Sdk/src/Services/Table/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public SessionPoolConfig(
internal TimeSpan PeriodicCheckInterval { get; } = TimeSpan.FromSeconds(10);
internal TimeSpan KeepAliveTimeout { get; } = TimeSpan.FromSeconds(1);
internal TimeSpan CreateSessionTimeout { get; } = TimeSpan.FromSeconds(1);
internal bool DeleteSessionsOnDispose { get; } = true;
}

internal class GetSessionResponse : ResponseWithResultBase<Session>, IDisposable
Expand Down Expand Up @@ -90,8 +91,7 @@ public async Task<GetSessionResponse> GetSession()
{
var sessionId = _idleSessions.Pop();

SessionState? sessionState;
if (!_sessions.TryGetValue(sessionId, out sessionState))
if (!_sessions.TryGetValue(sessionId, out var sessionState))
{
continue;
}
Expand Down Expand Up @@ -120,7 +120,8 @@ public async Task<GetSessionResponse> GetSession()
var createSessionResponse = await _client.CreateSession(new CreateSessionSettings
{
TransportTimeout = _config.CreateSessionTimeout,
OperationTimeout = _config.CreateSessionTimeout
OperationTimeout = _config.CreateSessionTimeout,
DeleteOnDispose = _config.DeleteSessionsOnDispose
});

lock (_lock)
Expand All @@ -133,7 +134,8 @@ public async Task<GetSessionResponse> GetSession()
driver: _driver,
sessionPool: this,
id: createSessionResponse.Result.Session.Id,
endpoint: createSessionResponse.Result.Session.Endpoint);
endpoint: createSessionResponse.Result.Session.Endpoint,
deleteOnDispose: _config.DeleteSessionsOnDispose);

_sessions.Add(session.Id, new SessionState(session));

Expand All @@ -152,14 +154,14 @@ internal void ReturnSession(string id)
{
lock (_lock)
{
SessionState? oldSession;
if (_sessions.TryGetValue(id, out oldSession))
if (_sessions.TryGetValue(id, out var oldSession))
{
var session = new Session(
driver: _driver,
sessionPool: this,
id: id,
endpoint: oldSession.Session.Endpoint);
endpoint: oldSession.Session.Endpoint,
deleteOnDispose: _config.DeleteSessionsOnDispose);

_sessions[id] = new SessionState(session);
_idleSessions.Push(id);
Expand Down Expand Up @@ -275,14 +277,22 @@ private void Dispose(bool disposing)

if (disposing)
{
foreach (var state in _sessions.Values)
if (_config.DeleteSessionsOnDispose)
{
_logger.LogTrace($"Closing session on session pool dispose: {state.Session.Id}");

_ = _client.DeleteSession(state.Session.Id, new DeleteSessionSettings
var tasks = new Task[_sessions.Count];
var i = 0;
foreach (var state in _sessions.Values)
{
TransportTimeout = Session.DeleteSessionTimeout
});
_logger.LogTrace($"Closing session on session pool dispose: {state.Session.Id}");

var task = _client.DeleteSession(state.Session.Id, new DeleteSessionSettings
{
TransportTimeout = Session.DeleteSessionTimeout
});
tasks[i++] = task;
}

Task.WaitAll(tasks);
}
}

Expand Down