[#26] All: Remove V2 from naming
Rename project, namespaces and class names Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
parent
c406df1a78
commit
766f61a5f7
219 changed files with 219 additions and 974 deletions
157
src/FrostFS.SDK.Client/Pool/ClientWrapper.cs
Normal file
157
src/FrostFS.SDK.Client/Pool/ClientWrapper.cs
Normal file
|
@ -0,0 +1,157 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
namespace FrostFS.SDK.Client;
|
||||
|
||||
// clientWrapper is used by default, alternative implementations are intended for testing purposes only.
|
||||
public class ClientWrapper : ClientStatusMonitor
|
||||
{
|
||||
private readonly object _lock = new();
|
||||
|
||||
private SessionCache sessionCache;
|
||||
|
||||
|
||||
internal ClientWrapper(WrapperPrm wrapperPrm, Pool pool) : base(wrapperPrm.Logger, wrapperPrm.Address)
|
||||
{
|
||||
WrapperPrm = wrapperPrm;
|
||||
ErrorThreshold = wrapperPrm.ErrorThreshold;
|
||||
|
||||
sessionCache = pool.SessionCache;
|
||||
Client = new FrostFSClient(WrapperPrm, sessionCache);
|
||||
}
|
||||
|
||||
internal FrostFSClient? Client { get; private set; }
|
||||
|
||||
internal WrapperPrm WrapperPrm { get; }
|
||||
|
||||
internal FrostFSClient? GetClient()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (IsHealthy())
|
||||
{
|
||||
return Client;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// dial establishes a connection to the server from the FrostFS network.
|
||||
// Returns an error describing failure reason. If failed, the client
|
||||
// SHOULD NOT be used.
|
||||
internal async Task Dial(CallContext ctx)
|
||||
{
|
||||
var client = GetClient() ?? throw new FrostFsInvalidObjectException("pool client unhealthy");
|
||||
|
||||
await client.Dial(ctx).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
internal void HandleError(Exception ex)
|
||||
{
|
||||
if (ex is FrostFsResponseException responseException && responseException.Status != null)
|
||||
{
|
||||
switch (responseException.Status.Code)
|
||||
{
|
||||
case FrostFsStatusCode.Internal:
|
||||
case FrostFsStatusCode.WrongMagicNumber:
|
||||
case FrostFsStatusCode.SignatureVerificationFailure:
|
||||
case FrostFsStatusCode.NodeUnderMaintenance:
|
||||
IncErrorRate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
IncErrorRate();
|
||||
}
|
||||
|
||||
private async Task ScheduleGracefulClose()
|
||||
{
|
||||
if (Client == null)
|
||||
return;
|
||||
|
||||
await Task.Delay((int)WrapperPrm.GracefulCloseOnSwitchTimeout).ConfigureAwait(false);
|
||||
|
||||
Client.Close();
|
||||
}
|
||||
|
||||
// restartIfUnhealthy checks healthy status of client and recreate it if status is unhealthy.
|
||||
// Indicating if status was changed by this function call and returns error that caused unhealthy status.
|
||||
internal async Task<bool> RestartIfUnhealthy(CallContext ctx)
|
||||
{
|
||||
bool wasHealthy;
|
||||
|
||||
try
|
||||
{
|
||||
var prmNodeInfo = new PrmNodeInfo(ctx);
|
||||
var response = await Client!.GetNodeInfoAsync(prmNodeInfo).ConfigureAwait(false);
|
||||
return false;
|
||||
}
|
||||
catch (RpcException)
|
||||
{
|
||||
wasHealthy = true;
|
||||
}
|
||||
|
||||
// if connection is dialed before, to avoid routine/connection leak,
|
||||
// pool has to close it and then initialize once again.
|
||||
if (IsDialed())
|
||||
{
|
||||
await ScheduleGracefulClose().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
FrostFSClient? client = null;
|
||||
|
||||
try
|
||||
{
|
||||
client = new(WrapperPrm, sessionCache);
|
||||
|
||||
var dialCtx = new CallContext
|
||||
{
|
||||
Timeout = TimeSpan.FromTicks((long)WrapperPrm.DialTimeout),
|
||||
CancellationToken = ctx.CancellationToken
|
||||
};
|
||||
|
||||
var error = await client.Dial(ctx).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
SetUnhealthyOnDial();
|
||||
return wasHealthy;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
Client = client;
|
||||
}
|
||||
|
||||
client = null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
client?.Dispose();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var prmNodeInfo = new PrmNodeInfo(ctx);
|
||||
var res = await Client.GetNodeInfoAsync(prmNodeInfo).ConfigureAwait(false);
|
||||
}
|
||||
catch (FrostFsException)
|
||||
{
|
||||
SetUnhealthy();
|
||||
return wasHealthy;
|
||||
}
|
||||
|
||||
SetHealthy();
|
||||
return !wasHealthy;
|
||||
}
|
||||
|
||||
internal void IncRequests(ulong elapsed, MethodIndex method)
|
||||
{
|
||||
var methodStat = Methods[(int)method];
|
||||
|
||||
methodStat.IncRequests(elapsed);
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue