Compare commits
No commits in common. "bff8d6786767a8ece329676427a26004abb4685c" and "d1271df207e4811f26902820e0f2099185b4b18b" have entirely different histories.
bff8d67867
...
d1271df207
103 changed files with 667 additions and 3261 deletions
23
src/FrostFS.SDK.ClientV2/Cache.cs
Normal file
23
src/FrostFS.SDK.ClientV2/Cache.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2
|
||||
{
|
||||
internal static class Cache
|
||||
{
|
||||
private static readonly IMemoryCache _ownersCache = new MemoryCache(new MemoryCacheOptions
|
||||
{
|
||||
// TODO: get from options?
|
||||
SizeLimit = 256
|
||||
});
|
||||
|
||||
private static readonly IMemoryCache _containersCache = new MemoryCache(new MemoryCacheOptions
|
||||
{
|
||||
// TODO: get from options?
|
||||
SizeLimit = 1024
|
||||
});
|
||||
|
||||
internal static IMemoryCache Owners => _ownersCache;
|
||||
|
||||
internal static IMemoryCache Containers => _containersCache;
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal static class Caches
|
||||
{
|
||||
private static readonly IMemoryCache _ownersCache = new MemoryCache(new MemoryCacheOptions
|
||||
{
|
||||
// TODO: get from options?
|
||||
SizeLimit = 256
|
||||
});
|
||||
|
||||
private static readonly IMemoryCache _containersCache = new MemoryCache(new MemoryCacheOptions
|
||||
{
|
||||
// TODO: get from options?
|
||||
SizeLimit = 1024
|
||||
});
|
||||
|
||||
internal static IMemoryCache Owners => _ownersCache;
|
||||
|
||||
internal static IMemoryCache Containers => _containersCache;
|
||||
}
|
|
@ -4,11 +4,12 @@ using FrostFS.SDK.Cryptography;
|
|||
|
||||
using Google.Protobuf;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2
|
||||
{
|
||||
public class ClientKey(ECDsa key)
|
||||
{
|
||||
internal ECDsa ECDsaKey { get; } = key;
|
||||
|
||||
internal ByteString PublicKeyProto { get; } = ByteString.CopyFrom(key.PublicKey());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class FrostFsInvalidObjectException : FrostFsException
|
||||
{
|
||||
public FrostFsInvalidObjectException()
|
||||
{
|
||||
}
|
||||
|
||||
public FrostFsInvalidObjectException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public FrostFsInvalidObjectException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class FrostFsResponseException : FrostFsException
|
||||
{
|
||||
public FrostFsResponseStatus? Status { get; private set; }
|
||||
|
||||
public FrostFsResponseException()
|
||||
{
|
||||
}
|
||||
|
||||
public FrostFsResponseException(FrostFsResponseStatus status)
|
||||
{
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public FrostFsResponseException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public FrostFsResponseException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class FrostFsStreamException : FrostFsException
|
||||
{
|
||||
public FrostFsStreamException()
|
||||
{
|
||||
}
|
||||
|
||||
public FrostFsStreamException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public FrostFsStreamException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class InvalidObjectException : Exception
|
||||
{
|
||||
public InvalidObjectException()
|
||||
{
|
||||
}
|
||||
|
||||
public InvalidObjectException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public InvalidObjectException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
25
src/FrostFS.SDK.ClientV2/Exceptions/ResponseException.cs
Normal file
25
src/FrostFS.SDK.ClientV2/Exceptions/ResponseException.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class ResponseException : Exception
|
||||
{
|
||||
public FrostFsResponseStatus? Status { get; private set; }
|
||||
|
||||
public ResponseException()
|
||||
{
|
||||
}
|
||||
|
||||
public ResponseException(FrostFsResponseStatus status)
|
||||
{
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public ResponseException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public ResponseException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class SessionExpiredException : FrostFsException
|
||||
{
|
||||
public SessionExpiredException()
|
||||
{
|
||||
}
|
||||
|
||||
public SessionExpiredException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public SessionExpiredException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class SessionNotFoundException : FrostFsException
|
||||
{
|
||||
public SessionNotFoundException()
|
||||
{
|
||||
}
|
||||
|
||||
public SessionNotFoundException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public SessionNotFoundException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -22,10 +22,10 @@
|
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
||||
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
|
||||
<PackageReference Include="System.Runtime.Caching" Version="8.0.1" />
|
||||
<PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -6,12 +6,10 @@ using System.Threading.Tasks;
|
|||
using Frostfs.V2.Ape;
|
||||
using Frostfs.V2.Apemanager;
|
||||
|
||||
using FrostFS.Accounting;
|
||||
using FrostFS.Container;
|
||||
using FrostFS.Netmap;
|
||||
using FrostFS.Object;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.ClientV2.Services;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
using FrostFS.Session;
|
||||
|
||||
|
@ -37,9 +35,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
internal ObjectService.ObjectServiceClient? ObjectServiceClient { get; set; }
|
||||
|
||||
internal AccountingService.AccountingServiceClient? AccountingServiceClient { get; set; }
|
||||
|
||||
internal ClientContext ClientCtx { get; set; }
|
||||
internal ClientEnvironment ClientCtx { get; set; }
|
||||
|
||||
public static IFrostFSClient GetInstance(IOptions<ClientSettings> clientOptions, GrpcChannelOptions? channelOptions = null)
|
||||
{
|
||||
|
@ -93,7 +89,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
var ecdsaKey = settings.Value.Key.LoadWif();
|
||||
FrostFsOwner.FromKey(ecdsaKey);
|
||||
|
||||
ClientCtx = new ClientContext(
|
||||
ClientCtx = new ClientEnvironment(
|
||||
client: this,
|
||||
key: ecdsaKey,
|
||||
owner: FrostFsOwner.FromKey(ecdsaKey),
|
||||
|
@ -108,13 +104,13 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
private FrostFSClient(IOptions<ClientSettings> options, GrpcChannelOptions? channelOptions)
|
||||
{
|
||||
var clientSettings = (options?.Value) ?? throw new ArgumentNullException(nameof(options), "Options value must be initialized");
|
||||
var clientSettings = (options?.Value) ?? throw new ArgumentException("Options must be initialized");
|
||||
|
||||
clientSettings.Validate();
|
||||
|
||||
var channel = InitGrpcChannel(clientSettings.Host, channelOptions);
|
||||
|
||||
ClientCtx = new ClientContext(
|
||||
ClientCtx = new ClientEnvironment(
|
||||
this,
|
||||
key: null,
|
||||
owner: null,
|
||||
|
@ -127,7 +123,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
private FrostFSClient(IOptions<SingleOwnerClientSettings> options, GrpcChannelOptions? channelOptions)
|
||||
{
|
||||
var clientSettings = (options?.Value) ?? throw new ArgumentNullException(nameof(options), "Options value must be initialized");
|
||||
var clientSettings = (options?.Value) ?? throw new ArgumentException("Options must be initialized");
|
||||
|
||||
clientSettings.Validate();
|
||||
|
||||
|
@ -135,7 +131,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
var channel = InitGrpcChannel(clientSettings.Host, channelOptions);
|
||||
|
||||
ClientCtx = new ClientContext(
|
||||
ClientCtx = new ClientEnvironment(
|
||||
this,
|
||||
key: ecdsaKey,
|
||||
owner: FrostFsOwner.FromKey(ecdsaKey),
|
||||
|
@ -146,19 +142,6 @@ public class FrostFSClient : IFrostFSClient
|
|||
// CheckFrostFsVersionSupport(new Context { Timeout = TimeSpan.FromSeconds(20) });
|
||||
}
|
||||
|
||||
internal FrostFSClient(WrapperPrm prm, SessionCache cache)
|
||||
{
|
||||
ClientCtx = new ClientContext(
|
||||
client: this,
|
||||
key: prm.Key,
|
||||
owner: FrostFsOwner.FromKey(prm.Key!),
|
||||
channel: InitGrpcChannel(prm.Address, null), //prm.GrpcChannelOptions),
|
||||
version: new FrostFsVersion(2, 13))
|
||||
{
|
||||
SessionCache = cache
|
||||
};
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
|
@ -329,9 +312,9 @@ public class FrostFSClient : IFrostFSClient
|
|||
throw new ArgumentNullException(nameof(args));
|
||||
|
||||
var session = await CreateSessionInternalAsync(args).ConfigureAwait(false);
|
||||
|
||||
var token = session.Serialize();
|
||||
return new FrostFsSessionToken(token, session.Body.Id.ToUuid());
|
||||
|
||||
return new FrostFsSessionToken(token);
|
||||
}
|
||||
|
||||
internal Task<SessionToken> CreateSessionInternalAsync(PrmSessionCreate args)
|
||||
|
@ -344,18 +327,8 @@ public class FrostFSClient : IFrostFSClient
|
|||
}
|
||||
#endregion
|
||||
|
||||
#region Accounting Implementation
|
||||
public async Task<Accounting.Decimal> GetBalanceAsync(PrmBalance? args)
|
||||
{
|
||||
args ??= new PrmBalance();
|
||||
|
||||
var service = GetAccouningService(args);
|
||||
return await service.GetBallance(args).ConfigureAwait(false);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ToolsImplementation
|
||||
public FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, CallContext ctx)
|
||||
public FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, Context ctx)
|
||||
{
|
||||
if (header == null)
|
||||
throw new ArgumentNullException(nameof(header));
|
||||
|
@ -364,12 +337,12 @@ public class FrostFSClient : IFrostFSClient
|
|||
}
|
||||
#endregion
|
||||
|
||||
private async void CheckFrostFsVersionSupport(CallContext? ctx = default)
|
||||
private async void CheckFrostFsVersionSupport(Context? ctx = default)
|
||||
{
|
||||
var args = new PrmNodeInfo(ctx);
|
||||
var args = new PrmNodeInfo { Context = ctx };
|
||||
|
||||
if (ctx?.Version == null)
|
||||
throw new ArgumentNullException(nameof(ctx), "Version must be initialized");
|
||||
throw new InvalidObjectException(nameof(ctx.Version));
|
||||
|
||||
var service = GetNetmapService(args);
|
||||
var localNodeInfo = await service.GetLocalNodeInfoAsync(args).ConfigureAwait(false);
|
||||
|
@ -381,16 +354,18 @@ public class FrostFSClient : IFrostFSClient
|
|||
}
|
||||
}
|
||||
|
||||
private CallInvoker? SetupClientContext(IContext ctx)
|
||||
private CallInvoker? SetupEnvironment(IContext ctx)
|
||||
{
|
||||
if (isDisposed)
|
||||
throw new FrostFsInvalidObjectException("Client is disposed.");
|
||||
throw new InvalidObjectException("Client is disposed.");
|
||||
|
||||
if (ctx.Context!.Key == null)
|
||||
ctx.Context ??= new Context();
|
||||
|
||||
if (ctx.Context.Key == null)
|
||||
{
|
||||
if (ClientCtx.Key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(ctx), "Key is not initialized.");
|
||||
throw new InvalidObjectException("Key is not initialized.");
|
||||
}
|
||||
|
||||
ctx.Context.Key = ClientCtx.Key.ECDsaKey;
|
||||
|
@ -405,23 +380,24 @@ public class FrostFSClient : IFrostFSClient
|
|||
{
|
||||
if (ClientCtx.Version == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(ctx), "Version is not initialized.");
|
||||
throw new InvalidObjectException("Version is not initialized.");
|
||||
}
|
||||
|
||||
ctx.Context.Version = ClientCtx.Version;
|
||||
}
|
||||
|
||||
CallInvoker? callInvoker = null;
|
||||
|
||||
if (ctx.Context.Interceptors != null && ctx.Context.Interceptors.Count > 0)
|
||||
{
|
||||
foreach (var interceptor in ctx.Context.Interceptors)
|
||||
{
|
||||
callInvoker = AddInvoker(callInvoker, interceptor);
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.Context.Callback != null)
|
||||
callInvoker = AddInvoker(callInvoker, new MetricsInterceptor(ctx.Context.Callback));
|
||||
|
||||
if (ctx.Context.PoolErrorHandler != null)
|
||||
callInvoker = AddInvoker(callInvoker, new ErrorInterceptor(ctx.Context.PoolErrorHandler));
|
||||
|
||||
return callInvoker;
|
||||
|
||||
CallInvoker AddInvoker(CallInvoker? callInvoker, Interceptor interceptor)
|
||||
|
@ -429,7 +405,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
if (callInvoker == null)
|
||||
callInvoker = ClientCtx.Channel.Intercept(interceptor);
|
||||
else
|
||||
callInvoker = callInvoker.Intercept(interceptor);
|
||||
callInvoker.Intercept(interceptor);
|
||||
|
||||
return callInvoker;
|
||||
}
|
||||
|
@ -437,7 +413,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
private NetmapServiceProvider GetNetmapService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var callInvoker = SetupEnvironment(ctx);
|
||||
var client = NetmapServiceClient ?? (callInvoker != null
|
||||
? new NetmapService.NetmapServiceClient(callInvoker)
|
||||
: new NetmapService.NetmapServiceClient(ClientCtx.Channel));
|
||||
|
@ -447,7 +423,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
private SessionServiceProvider GetSessionService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var callInvoker = SetupEnvironment(ctx);
|
||||
var client = SessionServiceClient ?? (callInvoker != null
|
||||
? new SessionService.SessionServiceClient(callInvoker)
|
||||
: new SessionService.SessionServiceClient(ClientCtx.Channel));
|
||||
|
@ -457,7 +433,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
private ApeManagerServiceProvider GetApeManagerService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var callInvoker = SetupEnvironment(ctx);
|
||||
var client = ApeManagerServiceClient ?? (callInvoker != null
|
||||
? new APEManagerService.APEManagerServiceClient(callInvoker)
|
||||
: new APEManagerService.APEManagerServiceClient(ClientCtx.Channel));
|
||||
|
@ -465,19 +441,9 @@ public class FrostFSClient : IFrostFSClient
|
|||
return new ApeManagerServiceProvider(client, ClientCtx);
|
||||
}
|
||||
|
||||
private AccountingServiceProvider GetAccouningService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var client = AccountingServiceClient ?? (callInvoker != null
|
||||
? new AccountingService.AccountingServiceClient(callInvoker)
|
||||
: new AccountingService.AccountingServiceClient(ClientCtx.Channel));
|
||||
|
||||
return new AccountingServiceProvider(client, ClientCtx);
|
||||
}
|
||||
|
||||
private ContainerServiceProvider GetContainerService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var callInvoker = SetupEnvironment(ctx);
|
||||
var client = ContainerServiceClient ?? (callInvoker != null
|
||||
? new ContainerService.ContainerServiceClient(callInvoker)
|
||||
: new ContainerService.ContainerServiceClient(ClientCtx.Channel));
|
||||
|
@ -487,7 +453,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
|
||||
private ObjectServiceProvider GetObjectService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var callInvoker = SetupEnvironment(ctx);
|
||||
var client = ObjectServiceClient ?? (callInvoker != null
|
||||
? new ObjectService.ObjectServiceClient(callInvoker)
|
||||
: new ObjectService.ObjectServiceClient(ClientCtx.Channel));
|
||||
|
@ -495,16 +461,6 @@ public class FrostFSClient : IFrostFSClient
|
|||
return new ObjectServiceProvider(client, ClientCtx);
|
||||
}
|
||||
|
||||
private AccountingServiceProvider GetAccountService(IContext ctx)
|
||||
{
|
||||
var callInvoker = SetupClientContext(ctx);
|
||||
var client = AccountingServiceClient ?? (callInvoker != null
|
||||
? new AccountingService.AccountingServiceClient(callInvoker)
|
||||
: new AccountingService.AccountingServiceClient(ClientCtx.Channel));
|
||||
|
||||
return new AccountingServiceProvider(client, ClientCtx);
|
||||
}
|
||||
|
||||
private static GrpcChannel InitGrpcChannel(string host, GrpcChannelOptions? channelOptions)
|
||||
{
|
||||
try
|
||||
|
@ -524,24 +480,4 @@ public class FrostFSClient : IFrostFSClient
|
|||
throw new ArgumentException($"Host '{host}' has invalid format. Error: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> Dial(CallContext ctx)
|
||||
{
|
||||
var prm = new PrmBalance(ctx);
|
||||
|
||||
var service = GetAccouningService(prm);
|
||||
_ = await service.GetBallance(prm).ConfigureAwait(false);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool RestartIfUnhealthy(CallContext ctx)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
// This file is used by Code Analysis to maintain SuppressMessage
|
||||
// attributes that are applied to this project.
|
||||
// Project-level suppressions either have no target or are given
|
||||
// a specific target and scoped to a namespace, type, member, etc.
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[assembly: SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "<Pending>", Scope = "member", Target = "~M:FrostFS.SDK.ClientV2.Sampler.Next~System.Int32")]
|
|
@ -1,68 +0,0 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Grpc.Core;
|
||||
using Grpc.Core.Interceptors;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods",
|
||||
Justification = "parameters are provided by GRPC infrastructure")]
|
||||
public class ErrorInterceptor(Action<Exception> handler) : Interceptor
|
||||
{
|
||||
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
ClientInterceptorContext<TRequest, TResponse> context,
|
||||
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
|
||||
{
|
||||
var call = continuation(request, context);
|
||||
|
||||
return new AsyncUnaryCall<TResponse>(
|
||||
HandleUnaryResponse(call),
|
||||
call.ResponseHeadersAsync,
|
||||
call.GetStatus,
|
||||
call.GetTrailers,
|
||||
call.Dispose);
|
||||
}
|
||||
|
||||
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(
|
||||
ClientInterceptorContext<TRequest, TResponse> context,
|
||||
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
|
||||
{
|
||||
var call = continuation(context);
|
||||
|
||||
return new AsyncClientStreamingCall<TRequest, TResponse>(
|
||||
call.RequestStream,
|
||||
HandleStreamResponse(call),
|
||||
call.ResponseHeadersAsync,
|
||||
call.GetStatus,
|
||||
call.GetTrailers,
|
||||
call.Dispose);
|
||||
}
|
||||
|
||||
private async Task<TResponse> HandleUnaryResponse<TResponse>(AsyncUnaryCall<TResponse> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
handler(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<TResponse> HandleStreamResponse<TRequest, TResponse>(AsyncClientStreamingCall<TRequest, TResponse> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
handler(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,8 +7,6 @@ using Grpc.Core.Interceptors;
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods",
|
||||
Justification = "parameters are provided by GRPC infrastructure")]
|
||||
public class MetricsInterceptor(Action<CallStatistics> callback) : Interceptor
|
||||
{
|
||||
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
|
||||
|
@ -16,6 +14,11 @@ public class MetricsInterceptor(Action<CallStatistics> callback) : Interceptor
|
|||
ClientInterceptorContext<TRequest, TResponse> context,
|
||||
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
|
||||
{
|
||||
if (continuation is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(continuation));
|
||||
}
|
||||
|
||||
var call = continuation(request, context);
|
||||
|
||||
return new AsyncUnaryCall<TResponse>(
|
||||
|
@ -30,6 +33,9 @@ public class MetricsInterceptor(Action<CallStatistics> callback) : Interceptor
|
|||
ClientInterceptorContext<TRequest, TResponse> context,
|
||||
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
|
||||
{
|
||||
if (continuation is null)
|
||||
throw new ArgumentNullException(nameof(continuation));
|
||||
|
||||
var call = continuation(context);
|
||||
|
||||
return new AsyncClientStreamingCall<TRequest, TResponse>(
|
||||
|
@ -46,7 +52,7 @@ public class MetricsInterceptor(Action<CallStatistics> callback) : Interceptor
|
|||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
var response = await call;
|
||||
var response = await call.ResponseAsync.ConfigureAwait(false);
|
||||
|
||||
watch.Stop();
|
||||
|
||||
|
@ -62,7 +68,7 @@ public class MetricsInterceptor(Action<CallStatistics> callback) : Interceptor
|
|||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
var response = await call;
|
||||
var response = await call.ResponseAsync.ConfigureAwait(false);
|
||||
|
||||
watch.Stop();
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ public interface IFrostFSClient : IDisposable
|
|||
Task<FrostFsSessionToken> CreateSessionAsync(PrmSessionCreate args);
|
||||
#endregion
|
||||
|
||||
#region ApeManager
|
||||
#region ApeMAnager
|
||||
Task<byte[]> AddChainAsync(PrmApeChainAdd args);
|
||||
|
||||
Task RemoveChainAsync(PrmApeChainRemove args);
|
||||
|
@ -51,15 +51,7 @@ public interface IFrostFSClient : IDisposable
|
|||
IAsyncEnumerable<FrostFsObjectId> SearchObjectsAsync(PrmObjectSearch args);
|
||||
#endregion
|
||||
|
||||
#region Account
|
||||
Task<Accounting.Decimal> GetBalanceAsync(PrmBalance? args = null);
|
||||
#endregion
|
||||
|
||||
#region Tools
|
||||
FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, CallContext ctx);
|
||||
FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, Context ctx);
|
||||
#endregion
|
||||
|
||||
public Task<string?> Dial(CallContext ctx);
|
||||
|
||||
public void Close();
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal static partial class FrostFsMessages
|
||||
{
|
||||
[LoggerMessage(100,
|
||||
LogLevel.Warning,
|
||||
"Failed to create frostfs session token for client. Address {address}, {error}",
|
||||
EventName = nameof(SessionCreationError))]
|
||||
internal static partial void SessionCreationError(ILogger logger, string address, string error);
|
||||
|
||||
[LoggerMessage(101,
|
||||
LogLevel.Warning,
|
||||
"Error threshold reached. Address {address}, threshold {threshold}",
|
||||
EventName = nameof(ErrorЕhresholdReached))]
|
||||
internal static partial void ErrorЕhresholdReached(ILogger logger, string address, uint threshold);
|
||||
|
||||
[LoggerMessage(102,
|
||||
LogLevel.Warning,
|
||||
"Health has changed: {address} healthy {healthy}, reason {error}",
|
||||
EventName = nameof(HealthChanged))]
|
||||
internal static partial void HealthChanged(ILogger logger, string address, bool healthy, string error);
|
||||
}
|
|
@ -24,14 +24,14 @@ public static class ContainerIdMapper
|
|||
|
||||
var containerId = model.GetValue() ?? throw new ArgumentNullException(nameof(model));
|
||||
|
||||
if (!Caches.Containers.TryGetValue(containerId, out ContainerID? message))
|
||||
if (!Cache.Containers.TryGetValue(containerId, out ContainerID? message))
|
||||
{
|
||||
message = new ContainerID
|
||||
{
|
||||
Value = ByteString.CopyFrom(Base58.Decode(containerId))
|
||||
};
|
||||
|
||||
Caches.Containers.Set(containerId, message, _oneHourExpiration);
|
||||
Cache.Containers.Set(containerId, message, _oneHourExpiration);
|
||||
}
|
||||
|
||||
return message!;
|
||||
|
|
|
@ -22,14 +22,14 @@ public static class OwnerIdMapper
|
|||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
|
||||
if (!Caches.Owners.TryGetValue(model, out OwnerID? message))
|
||||
if (!Cache.Owners.TryGetValue(model, out OwnerID? message))
|
||||
{
|
||||
message = new OwnerID
|
||||
{
|
||||
Value = ByteString.CopyFrom(model.ToHash())
|
||||
};
|
||||
|
||||
Caches.Owners.Set(model, message, _oneHourExpiration);
|
||||
Cache.Owners.Set(model, message, _oneHourExpiration);
|
||||
}
|
||||
|
||||
return message!;
|
||||
|
@ -42,11 +42,11 @@ public static class OwnerIdMapper
|
|||
throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
||||
if (!Caches.Owners.TryGetValue(message, out FrostFsOwner? model))
|
||||
if (!Cache.Owners.TryGetValue(message, out FrostFsOwner? model))
|
||||
{
|
||||
model = new FrostFsOwner(Base58.Encode(message.Value.ToByteArray()));
|
||||
|
||||
Caches.Owners.Set(message, model, _oneHourExpiration);
|
||||
Cache.Owners.Set(message, model, _oneHourExpiration);
|
||||
}
|
||||
|
||||
return model!;
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
using Frostfs.V2.Ape;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2
|
||||
{
|
||||
public struct FrostFsChainTarget(FrostFsTargetType type, string name) : IEquatable<FrostFsChainTarget>
|
||||
{
|
||||
private ChainTarget? chainTarget;
|
||||
|
@ -60,3 +60,4 @@ public struct FrostFsChainTarget(FrostFsTargetType type, string name) : IEquatab
|
|||
return Type == other.Type && Name.Equals(other.Name, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
using Google.Protobuf;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2
|
||||
{
|
||||
public struct FrostFsChain(byte[] raw) : System.IEquatable<FrostFsChain>
|
||||
{
|
||||
private ByteString? grpcRaw;
|
||||
|
@ -39,3 +39,4 @@ public struct FrostFsChain(byte[] raw) : System.IEquatable<FrostFsChain>
|
|||
return Raw == other.Raw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2
|
||||
{
|
||||
public enum FrostFsTargetType
|
||||
{
|
||||
Undefined = 0,
|
||||
|
@ -8,3 +8,4 @@ public enum FrostFsTargetType
|
|||
User,
|
||||
Group
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ public class ClientSettings
|
|||
{
|
||||
var errors = CheckFields();
|
||||
if (errors != null)
|
||||
ThrowSettingsException(errors);
|
||||
ThrowException(errors);
|
||||
}
|
||||
|
||||
protected Collection<string>? CheckFields()
|
||||
|
@ -29,7 +29,7 @@ public class ClientSettings
|
|||
return null;
|
||||
}
|
||||
|
||||
protected static void ThrowSettingsException(Collection<string> errors)
|
||||
protected static void ThrowException(Collection<string> errors)
|
||||
{
|
||||
if (errors is null)
|
||||
{
|
||||
|
@ -55,7 +55,7 @@ public class SingleOwnerClientSettings : ClientSettings
|
|||
{
|
||||
var errors = CheckFields();
|
||||
if (errors != null)
|
||||
ThrowSettingsException(errors);
|
||||
ThrowException(errors);
|
||||
}
|
||||
|
||||
protected new Collection<string>? CheckFields()
|
||||
|
|
|
@ -31,7 +31,7 @@ public class FrostFsContainerId
|
|||
return this.modelId;
|
||||
}
|
||||
|
||||
throw new FrostFsInvalidObjectException();
|
||||
throw new InvalidObjectException();
|
||||
}
|
||||
|
||||
internal ContainerID ContainerID
|
||||
|
@ -47,7 +47,7 @@ public class FrostFsContainerId
|
|||
return this.containerID;
|
||||
}
|
||||
|
||||
throw new FrostFsInvalidObjectException();
|
||||
throw new InvalidObjectException();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ public class FrostFsContainerInfo
|
|||
{
|
||||
if (PlacementPolicy == null)
|
||||
{
|
||||
throw new ArgumentNullException("PlacementPolicy is null");
|
||||
throw new InvalidObjectException("PlacementPolicy is null");
|
||||
}
|
||||
|
||||
this.container = new Container.Container()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
|
||||
namespace FrostFS.SDK;
|
||||
|
||||
|
@ -67,7 +67,7 @@ public class FrostFsObject
|
|||
public void SetParent(FrostFsObjectHeader largeObjectHeader)
|
||||
{
|
||||
if (Header?.Split == null)
|
||||
throw new ArgumentNullException(nameof(largeObjectHeader), "Split value must not be null");
|
||||
throw new InvalidObjectException("The object is not initialized properly");
|
||||
|
||||
Header.Split.ParentHeader = largeObjectHeader;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK;
|
||||
|
||||
public class FrostFsSessionToken(byte[] token, Guid id)
|
||||
public class FrostFsSessionToken(byte[] token)
|
||||
{
|
||||
public Guid Id { get; private set; } = id;
|
||||
public byte[] Token { get; private set; } = token;
|
||||
}
|
||||
|
|
|
@ -11,11 +11,11 @@ using Grpc.Core.Interceptors;
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class CallContext()
|
||||
public class Context()
|
||||
{
|
||||
private ByteString? publicKeyCache;
|
||||
private ReadOnlyCollection<Interceptor>? interceptors;
|
||||
|
||||
internal Action<Exception>? PoolErrorHandler { get; set; }
|
||||
private ByteString? publicKeyCache;
|
||||
|
||||
public ECDsa? Key { get; set; }
|
||||
|
||||
|
@ -31,7 +31,11 @@ public class CallContext()
|
|||
|
||||
public Action<CallStatistics>? Callback { get; set; }
|
||||
|
||||
public Collection<Interceptor> Interceptors { get; } = [];
|
||||
public ReadOnlyCollection<Interceptor>? Interceptors
|
||||
{
|
||||
get { return this.interceptors; }
|
||||
set { this.interceptors = value; }
|
||||
}
|
||||
|
||||
public ByteString? GetPublicKeyCache()
|
||||
{
|
|
@ -7,5 +7,5 @@ public interface IContext
|
|||
/// callbacks, interceptors.
|
||||
/// </summary>
|
||||
/// <value>Additional parameters for calling the method</value>
|
||||
CallContext? Context { get; }
|
||||
Context? Context { get; set; }
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmApeChainList(FrostFsChainTarget target, CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmApeChainList(FrostFsChainTarget target) : PrmBase
|
||||
{
|
||||
public FrostFsChainTarget Target { get; } = target;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmApeChainRemove(FrostFsChainTarget target, FrostFsChain chain, CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmApeChainRemove(FrostFsChainTarget target, FrostFsChain chain) : PrmBase
|
||||
{
|
||||
public FrostFsChainTarget Target { get; } = target;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmApeChainAdd(FrostFsChainTarget target, FrostFsChain chain, CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmApeChainAdd(FrostFsChainTarget target, FrostFsChain chain) : PrmBase
|
||||
{
|
||||
public FrostFsChainTarget Target { get; } = target;
|
||||
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmBalance(CallContext? ctx = null) : PrmBase(ctx)
|
||||
{
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class PrmBase(CallContext? ctx, NameValueCollection? xheaders = null) : IContext
|
||||
public class PrmBase(NameValueCollection? xheaders = null) : IContext
|
||||
{
|
||||
/// <summary>
|
||||
/// FrostFS request X-Headers
|
||||
|
@ -10,5 +10,5 @@ public class PrmBase(CallContext? ctx, NameValueCollection? xheaders = null) : I
|
|||
public NameValueCollection XHeaders { get; } = xheaders ?? [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public CallContext Context { get; } = ctx ?? new CallContext();
|
||||
public Context? Context { get; set; }
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmContainerCreate(FrostFsContainerInfo container, CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmContainerCreate(FrostFsContainerInfo container) : PrmBase, ISessionToken
|
||||
{
|
||||
public FrostFsContainerInfo Container { get; set; } = container;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmContainerDelete(FrostFsContainerId containerId, CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmContainerDelete(FrostFsContainerId containerId) : PrmBase, ISessionToken
|
||||
{
|
||||
public FrostFsContainerId ContainerId { get; set; } = containerId;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmContainerGet(FrostFsContainerId container, CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmContainerGet(FrostFsContainerId container) : PrmBase
|
||||
{
|
||||
public FrostFsContainerId Container { get; set; } = container;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmContainerGetAll(CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmContainerGetAll() : PrmBase()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmNetmapSnapshot(CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmNetmapSnapshot() : PrmBase
|
||||
{
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmNetworkSettings(CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmNetworkSettings() : PrmBase
|
||||
{
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmNodeInfo(CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmNodeInfo() : PrmBase
|
||||
{
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmObjectDelete(FrostFsContainerId containerId, FrostFsObjectId objectId, CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmObjectDelete(FrostFsContainerId containerId, FrostFsObjectId objectId) : PrmBase, ISessionToken
|
||||
{
|
||||
public FrostFsContainerId ContainerId { get; set; } = containerId;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmObjectGet(FrostFsContainerId containerId, FrostFsObjectId objectId, CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmObjectGet(FrostFsContainerId containerId, FrostFsObjectId objectId) : PrmBase, ISessionToken
|
||||
{
|
||||
public FrostFsContainerId ContainerId { get; set; } = containerId;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmObjectHeadGet(FrostFsContainerId containerId, FrostFsObjectId objectId, CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmObjectHeadGet(FrostFsContainerId containerId, FrostFsObjectId objectId) : PrmBase, ISessionToken
|
||||
{
|
||||
public FrostFsContainerId ContainerId { get; set; } = containerId;
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ using System.IO;
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmObjectPut(CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmObjectPut : PrmBase, ISessionToken
|
||||
{
|
||||
/// <summary>
|
||||
/// Need to provide values like <c>ContainerId</c> and <c>ObjectType</c> to create and object.
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmObjectSearch(FrostFsContainerId containerId, CallContext? ctx = null, params IObjectFilter[] filters) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmObjectSearch(FrostFsContainerId containerId, params IObjectFilter[] filters) : PrmBase, ISessionToken
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines container for the search
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmSessionCreate(ulong expiration, CallContext? ctx = null) : PrmBase(ctx)
|
||||
public sealed class PrmSessionCreate(ulong expiration) : PrmBase
|
||||
{
|
||||
public ulong Expiration { get; set; } = expiration;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class PrmSingleObjectPut(FrostFsObject frostFsObject, CallContext? ctx = null) : PrmBase(ctx), ISessionToken
|
||||
public sealed class PrmSingleObjectPut(FrostFsObject frostFsObject) : PrmBase, ISessionToken
|
||||
{
|
||||
public FrostFsObject FrostFsObject { get; set; } = frostFsObject;
|
||||
|
||||
|
|
|
@ -1,163 +0,0 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// clientStatusMonitor count error rate and other statistics for connection.
|
||||
public class ClientStatusMonitor : IClientStatus
|
||||
{
|
||||
private static readonly MethodIndex[] MethodIndexes =
|
||||
[
|
||||
MethodIndex.methodBalanceGet,
|
||||
MethodIndex.methodContainerPut,
|
||||
MethodIndex.methodContainerGet,
|
||||
MethodIndex.methodContainerList,
|
||||
MethodIndex.methodContainerDelete,
|
||||
MethodIndex.methodEndpointInfo,
|
||||
MethodIndex.methodNetworkInfo,
|
||||
MethodIndex.methodNetMapSnapshot,
|
||||
MethodIndex.methodObjectPut,
|
||||
MethodIndex.methodObjectDelete,
|
||||
MethodIndex.methodObjectGet,
|
||||
MethodIndex.methodObjectHead,
|
||||
MethodIndex.methodObjectRange,
|
||||
MethodIndex.methodObjectPatch,
|
||||
MethodIndex.methodSessionCreate,
|
||||
MethodIndex.methodAPEManagerAddChain,
|
||||
MethodIndex.methodAPEManagerRemoveChain,
|
||||
MethodIndex.methodAPEManagerListChains
|
||||
];
|
||||
|
||||
public static string GetMethodName(MethodIndex index)
|
||||
{
|
||||
return index switch
|
||||
{
|
||||
MethodIndex.methodBalanceGet => "BalanceGet",
|
||||
MethodIndex.methodContainerPut => "ContainerPut",
|
||||
MethodIndex.methodContainerGet => "ContainerGet",
|
||||
MethodIndex.methodContainerList => "ContainerList",
|
||||
MethodIndex.methodContainerDelete => "ContainerDelete",
|
||||
MethodIndex.methodEndpointInfo => "EndpointInfo",
|
||||
MethodIndex.methodNetworkInfo => "NetworkInfo",
|
||||
MethodIndex.methodNetMapSnapshot => "NetMapSnapshot",
|
||||
MethodIndex.methodObjectPut => "ObjectPut",
|
||||
MethodIndex.methodObjectDelete => "ObjectDelete",
|
||||
MethodIndex.methodObjectGet => "ObjectGet",
|
||||
MethodIndex.methodObjectHead => "ObjectHead",
|
||||
MethodIndex.methodObjectRange => "ObjectRange",
|
||||
MethodIndex.methodObjectPatch => "ObjectPatch",
|
||||
MethodIndex.methodSessionCreate => "SessionCreate",
|
||||
MethodIndex.methodAPEManagerAddChain => "APEManagerAddChain",
|
||||
MethodIndex.methodAPEManagerRemoveChain => "APEManagerRemoveChain",
|
||||
MethodIndex.methodAPEManagerListChains => "APEManagerListChains",
|
||||
_ => throw new ArgumentException("Unknown method", nameof(index)),
|
||||
};
|
||||
}
|
||||
|
||||
private readonly object _lock = new();
|
||||
|
||||
private readonly ILogger? logger;
|
||||
private int healthy;
|
||||
|
||||
public ClientStatusMonitor(ILogger? logger, string address)
|
||||
{
|
||||
this.logger = logger;
|
||||
healthy = (int)HealthyStatus.Healthy;
|
||||
|
||||
Address = address;
|
||||
Methods = new MethodStatus[MethodIndexes.Length];
|
||||
|
||||
for (int i = 0; i < MethodIndexes.Length; i++)
|
||||
{
|
||||
Methods[i] = new MethodStatus(GetMethodName(MethodIndexes[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public string Address { get; }
|
||||
|
||||
internal uint ErrorThreshold { get; set; }
|
||||
|
||||
public uint CurrentErrorCount { get; set; }
|
||||
|
||||
public ulong OverallErrorCount { get; set; }
|
||||
|
||||
public MethodStatus[] Methods { get; private set; }
|
||||
|
||||
public bool IsHealthy()
|
||||
{
|
||||
var res = Interlocked.CompareExchange(ref healthy, -1, -1) == (int)HealthyStatus.Healthy;
|
||||
return res;
|
||||
}
|
||||
|
||||
public bool IsDialed()
|
||||
{
|
||||
return Interlocked.CompareExchange(ref healthy, -1, -1) != (int)HealthyStatus.UnhealthyOnDial;
|
||||
}
|
||||
|
||||
public void SetHealthy()
|
||||
{
|
||||
Interlocked.Exchange(ref healthy, (int)HealthyStatus.Healthy);
|
||||
}
|
||||
public void SetUnhealthy()
|
||||
{
|
||||
Interlocked.Exchange(ref healthy, (int)HealthyStatus.UnhealthyOnRequest);
|
||||
}
|
||||
|
||||
public void SetUnhealthyOnDial()
|
||||
{
|
||||
Interlocked.Exchange(ref healthy, (int)HealthyStatus.UnhealthyOnDial);
|
||||
}
|
||||
|
||||
public void IncErrorRate()
|
||||
{
|
||||
bool thresholdReached;
|
||||
lock (_lock)
|
||||
{
|
||||
CurrentErrorCount++;
|
||||
OverallErrorCount++;
|
||||
|
||||
thresholdReached = CurrentErrorCount >= ErrorThreshold;
|
||||
|
||||
if (thresholdReached)
|
||||
{
|
||||
SetUnhealthy();
|
||||
CurrentErrorCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (thresholdReached && logger != null)
|
||||
{
|
||||
FrostFsMessages.ErrorЕhresholdReached(logger, Address, ErrorThreshold);
|
||||
}
|
||||
}
|
||||
|
||||
public uint GetCurrentErrorRate()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return CurrentErrorCount;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong GetOverallErrorRate()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return OverallErrorCount;
|
||||
}
|
||||
}
|
||||
|
||||
public StatusSnapshot[] MethodsStatus()
|
||||
{
|
||||
var result = new StatusSnapshot[Methods.Length];
|
||||
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
result[i] = Methods[i].Snapshot!;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,143 +0,0 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
#pragma warning disable CA2000 // Dispose objects before losing scope: will be disposed manually
|
||||
FrostFSClient client = new(WrapperPrm, sessionCache);
|
||||
#pragma warning restore CA2000
|
||||
|
||||
//TODO: set additioanl params
|
||||
var error = await client.Dial(ctx).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
SetUnhealthyOnDial();
|
||||
return wasHealthy;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
Client = client;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// values for healthy status of clientStatusMonitor.
|
||||
public enum HealthyStatus
|
||||
{
|
||||
// statusUnhealthyOnDial is set when dialing to the endpoint is failed,
|
||||
// so there is no connection to the endpoint, and pool should not close it
|
||||
// before re-establishing connection once again.
|
||||
UnhealthyOnDial,
|
||||
|
||||
// statusUnhealthyOnRequest is set when communication after dialing to the
|
||||
// endpoint is failed due to immediate or accumulated errors, connection is
|
||||
// available and pool should close it before re-establishing connection once again.
|
||||
UnhealthyOnRequest,
|
||||
|
||||
// statusHealthy is set when connection is ready to be used by the pool.
|
||||
Healthy
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public interface IClientStatus
|
||||
{
|
||||
// isHealthy checks if the connection can handle requests.
|
||||
bool IsHealthy();
|
||||
|
||||
// isDialed checks if the connection was created.
|
||||
bool IsDialed();
|
||||
|
||||
// setUnhealthy marks client as unhealthy.
|
||||
void SetUnhealthy();
|
||||
|
||||
// address return address of endpoint.
|
||||
string Address { get; }
|
||||
|
||||
// currentErrorRate returns current errors rate.
|
||||
// After specific threshold connection is considered as unhealthy.
|
||||
// Pool.startRebalance routine can make this connection healthy again.
|
||||
uint GetCurrentErrorRate();
|
||||
|
||||
// overallErrorRate returns the number of all happened errors.
|
||||
ulong GetOverallErrorRate();
|
||||
|
||||
// methodsStatus returns statistic for all used methods.
|
||||
StatusSnapshot[] MethodsStatus();
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Grpc.Net.Client;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// InitParameters contains values used to initialize connection Pool.
|
||||
public class InitParameters
|
||||
{
|
||||
public ECDsa? Key { get; set; }
|
||||
|
||||
public ulong NodeDialTimeout { get; set; }
|
||||
|
||||
public ulong NodeStreamTimeout { get; set; }
|
||||
|
||||
public ulong HealthcheckTimeout { get; set; }
|
||||
|
||||
public ulong ClientRebalanceInterval { get; set; }
|
||||
|
||||
public ulong SessionExpirationDuration { get; set; }
|
||||
|
||||
public uint ErrorThreshold { get; set; }
|
||||
|
||||
public NodeParam[]? NodeParams { get; set; }
|
||||
|
||||
public GrpcChannelOptions[]? DialOptions { get; set; }
|
||||
|
||||
public Func<string, ClientWrapper>? ClientBuilder { get; set; }
|
||||
|
||||
public ulong GracefulCloseOnSwitchTimeout { get; set; }
|
||||
|
||||
public ILogger? Logger { get; set; }
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
using FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class InnerPool
|
||||
{
|
||||
private readonly object _lock = new();
|
||||
|
||||
internal InnerPool(Sampler sampler, ClientWrapper[] clients)
|
||||
{
|
||||
Clients = clients;
|
||||
Sampler = sampler;
|
||||
}
|
||||
|
||||
internal Sampler Sampler { get; set; }
|
||||
|
||||
internal ClientWrapper[] Clients { get; }
|
||||
|
||||
internal ClientWrapper? Connection()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (Clients.Length == 1)
|
||||
{
|
||||
var client = Clients[0];
|
||||
if (client.IsHealthy())
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var attempts = 3 * Clients.Length;
|
||||
|
||||
for (int i = 0; i < attempts; i++)
|
||||
{
|
||||
int index = Sampler.Next();
|
||||
|
||||
if (Clients[index].IsHealthy())
|
||||
{
|
||||
return Clients[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public enum MethodIndex
|
||||
{
|
||||
methodBalanceGet,
|
||||
methodContainerPut,
|
||||
methodContainerGet,
|
||||
methodContainerList,
|
||||
methodContainerDelete,
|
||||
methodEndpointInfo,
|
||||
methodNetworkInfo,
|
||||
methodNetMapSnapshot,
|
||||
methodObjectPut,
|
||||
methodObjectDelete,
|
||||
methodObjectGet,
|
||||
methodObjectHead,
|
||||
methodObjectRange,
|
||||
methodObjectPatch,
|
||||
methodSessionCreate,
|
||||
methodAPEManagerAddChain,
|
||||
methodAPEManagerRemoveChain,
|
||||
methodAPEManagerListChains,
|
||||
methodLast
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class MethodStatus(string name)
|
||||
{
|
||||
private readonly object _lock = new();
|
||||
|
||||
public string Name { get; } = name;
|
||||
|
||||
public StatusSnapshot Snapshot { get; set; } = new StatusSnapshot();
|
||||
|
||||
internal void IncRequests(ulong elapsed)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
Snapshot.AllTime += elapsed;
|
||||
Snapshot.AllRequests++;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// NodeParam groups parameters of remote node.
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1815:Override equals and operator equals on value types", Justification = "<Pending>")]
|
||||
public readonly struct NodeParam(int priority, string address, float weight)
|
||||
{
|
||||
public int Priority { get; } = priority;
|
||||
|
||||
public string Address { get; } = address;
|
||||
|
||||
public float Weight { get; } = weight;
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class NodeStatistic
|
||||
{
|
||||
public string? Address { get; internal set; }
|
||||
|
||||
public StatusSnapshot[]? Methods { get; internal set; }
|
||||
|
||||
public ulong OverallErrors { get; internal set; }
|
||||
|
||||
public uint CurrentErrors { get; internal set; }
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class NodesParam(int priority)
|
||||
{
|
||||
public int Priority { get; } = priority;
|
||||
|
||||
public Collection<string> Addresses { get; } = [];
|
||||
|
||||
public Collection<double> Weights { get; } = [];
|
||||
}
|
|
@ -1,776 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Frostfs.V2.Ape;
|
||||
|
||||
using FrostFS.Refs;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.ClientV2.Mappers.GRPC;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public partial class Pool : IFrostFSClient
|
||||
{
|
||||
const int defaultSessionTokenExpirationDuration = 100; // in epochs
|
||||
|
||||
const int defaultErrorThreshold = 100;
|
||||
|
||||
const int defaultGracefulCloseOnSwitchTimeout = 10; //Seconds;
|
||||
const int defaultRebalanceInterval = 15; //Seconds;
|
||||
const int defaultHealthcheckTimeout = 4; //Seconds;
|
||||
const int defaultDialTimeout = 5; //Seconds;
|
||||
const int defaultStreamTimeout = 10; //Seconds;
|
||||
|
||||
private readonly object _lock = new();
|
||||
|
||||
private InnerPool[]? InnerPools { get; set; }
|
||||
|
||||
private ECDsa Key { get; set; }
|
||||
|
||||
private string PublicKey { get; }
|
||||
|
||||
private OwnerID? _ownerId;
|
||||
private FrostFsOwner? _owner;
|
||||
|
||||
private FrostFsOwner Owner
|
||||
{
|
||||
get
|
||||
{
|
||||
_owner ??= new FrostFsOwner(Key.PublicKey().PublicKeyToAddress());
|
||||
return _owner;
|
||||
}
|
||||
}
|
||||
|
||||
private OwnerID OwnerId
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_ownerId == null)
|
||||
{
|
||||
_owner = new FrostFsOwner(Key.PublicKey().PublicKeyToAddress());
|
||||
_ownerId = _owner.ToMessage();
|
||||
}
|
||||
return _ownerId;
|
||||
}
|
||||
}
|
||||
|
||||
internal CancellationTokenSource CancellationTokenSource { get; } = new CancellationTokenSource();
|
||||
|
||||
internal SessionCache SessionCache { get; set; }
|
||||
|
||||
private ulong SessionTokenDuration { get; set; }
|
||||
|
||||
private RebalanceParameters RebalanceParams { get; set; }
|
||||
|
||||
private Func<string, ClientWrapper> ClientBuilder;
|
||||
|
||||
private bool disposedValue;
|
||||
|
||||
private ILogger? logger { get; set; }
|
||||
|
||||
private ulong MaxObjectSize { get; set; }
|
||||
|
||||
public IClientStatus? ClientStatus { get; }
|
||||
|
||||
// NewPool creates connection pool using parameters.
|
||||
public Pool(InitParameters options)
|
||||
{
|
||||
if (options is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
if (options.Key == null)
|
||||
{
|
||||
throw new ArgumentException($"Missed required parameter {nameof(options.Key)}");
|
||||
}
|
||||
|
||||
var nodesParams = AdjustNodeParams(options.NodeParams);
|
||||
|
||||
var cache = new SessionCache(options.SessionExpirationDuration);
|
||||
|
||||
FillDefaultInitParams(options, this);
|
||||
|
||||
Key = options.Key;
|
||||
PublicKey = $"{Key.PublicKey()}";
|
||||
|
||||
SessionCache = cache;
|
||||
logger = options.Logger;
|
||||
SessionTokenDuration = options.SessionExpirationDuration;
|
||||
|
||||
RebalanceParams = new RebalanceParameters(
|
||||
nodesParams.ToArray(),
|
||||
options.HealthcheckTimeout,
|
||||
options.ClientRebalanceInterval,
|
||||
options.SessionExpirationDuration);
|
||||
|
||||
ClientBuilder = options.ClientBuilder!;
|
||||
}
|
||||
|
||||
private void SetupContext(CallContext ctx)
|
||||
{
|
||||
if (ctx == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(ctx));
|
||||
}
|
||||
|
||||
ctx.Key ??= Key;
|
||||
}
|
||||
|
||||
// Dial establishes a connection to the servers from the FrostFS network.
|
||||
// It also starts a routine that checks the health of the nodes and
|
||||
// updates the weights of the nodes for balancing.
|
||||
// Returns an error describing failure reason.
|
||||
//
|
||||
// If failed, the Pool SHOULD NOT be used.
|
||||
//
|
||||
// See also InitParameters.SetClientRebalanceInterval.
|
||||
public async Task<string?> Dial(CallContext ctx)
|
||||
{
|
||||
SetupContext(ctx);
|
||||
|
||||
var inner = new InnerPool[RebalanceParams.NodesParams.Length];
|
||||
|
||||
bool atLeastOneHealthy = false;
|
||||
int i = 0;
|
||||
foreach (var nodeParams in RebalanceParams.NodesParams)
|
||||
{
|
||||
var clients = new ClientWrapper[nodeParams.Weights.Count];
|
||||
|
||||
for (int j = 0; j < nodeParams.Addresses.Count; j++)
|
||||
{
|
||||
ClientWrapper? client = null;
|
||||
bool dialed = false;
|
||||
try
|
||||
{
|
||||
client = clients[j] = ClientBuilder(nodeParams.Addresses[j]);
|
||||
|
||||
await client.Dial(ctx).ConfigureAwait(false);
|
||||
dialed = true;
|
||||
|
||||
var token = await InitSessionForDuration(ctx, client, RebalanceParams.SessionExpirationDuration, Key, false)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var key = FormCacheKey(nodeParams.Addresses[j], Key.PrivateKey().ToString());
|
||||
_ = SessionCache.Cache[key] = token;
|
||||
|
||||
atLeastOneHealthy = true;
|
||||
}
|
||||
catch (RpcException ex)
|
||||
{
|
||||
if (!dialed)
|
||||
client!.SetUnhealthyOnDial();
|
||||
else
|
||||
client!.SetUnhealthy();
|
||||
|
||||
if (logger != null)
|
||||
{
|
||||
FrostFsMessages.SessionCreationError(logger, client!.WrapperPrm.Address, ex.Message);
|
||||
}
|
||||
}
|
||||
catch (FrostFsInvalidObjectException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var sampler = new Sampler(nodeParams.Weights.ToArray());
|
||||
|
||||
inner[i] = new InnerPool(sampler, clients);
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!atLeastOneHealthy)
|
||||
return "At least one node must be healthy";
|
||||
|
||||
InnerPools = inner;
|
||||
|
||||
var res = await GetNetworkSettingsAsync(new PrmNetworkSettings(ctx)).ConfigureAwait(false);
|
||||
|
||||
MaxObjectSize = res.MaxObjectSize;
|
||||
|
||||
StartRebalance(ctx);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static IEnumerable<NodesParam> AdjustNodeParams(NodeParam[]? nodeParams)
|
||||
{
|
||||
if (nodeParams == null || nodeParams.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("No FrostFS peers configured");
|
||||
}
|
||||
|
||||
Dictionary<int, NodesParam> nodesParamsDict = new(nodeParams.Length);
|
||||
foreach (var nodeParam in nodeParams)
|
||||
{
|
||||
if (!nodesParamsDict.TryGetValue(nodeParam.Priority, out var nodes))
|
||||
{
|
||||
nodes = new NodesParam(nodeParam.Priority);
|
||||
nodesParamsDict[nodeParam.Priority] = nodes;
|
||||
}
|
||||
|
||||
nodes.Addresses.Add(nodeParam.Address);
|
||||
nodes.Weights.Add(nodeParam.Weight);
|
||||
}
|
||||
|
||||
var nodesParams = new List<NodesParam>(nodesParamsDict.Count);
|
||||
|
||||
foreach (var key in nodesParamsDict.Keys)
|
||||
{
|
||||
var nodes = nodesParamsDict[key];
|
||||
var newWeights = AdjustWeights([.. nodes.Weights]);
|
||||
nodes.Weights.Clear();
|
||||
foreach (var weight in newWeights)
|
||||
{
|
||||
nodes.Weights.Add(weight);
|
||||
}
|
||||
|
||||
nodesParams.Add(nodes);
|
||||
}
|
||||
|
||||
return nodesParams.OrderBy(n => n.Priority);
|
||||
}
|
||||
|
||||
private static double[] AdjustWeights(double[] weights)
|
||||
{
|
||||
var adjusted = new double[weights.Length];
|
||||
|
||||
var sum = weights.Sum();
|
||||
|
||||
if (sum > 0)
|
||||
{
|
||||
for (int i = 0; i < weights.Length; i++)
|
||||
{
|
||||
adjusted[i] = weights[i] / sum;
|
||||
}
|
||||
}
|
||||
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
private static void FillDefaultInitParams(InitParameters parameters, Pool pool)
|
||||
{
|
||||
if (parameters.SessionExpirationDuration == 0)
|
||||
parameters.SessionExpirationDuration = defaultSessionTokenExpirationDuration;
|
||||
|
||||
if (parameters.ErrorThreshold == 0)
|
||||
parameters.ErrorThreshold = defaultErrorThreshold;
|
||||
|
||||
if (parameters.ClientRebalanceInterval <= 0)
|
||||
parameters.ClientRebalanceInterval = defaultRebalanceInterval;
|
||||
|
||||
if (parameters.GracefulCloseOnSwitchTimeout <= 0)
|
||||
parameters.GracefulCloseOnSwitchTimeout = defaultGracefulCloseOnSwitchTimeout;
|
||||
|
||||
if (parameters.HealthcheckTimeout <= 0)
|
||||
parameters.HealthcheckTimeout = defaultHealthcheckTimeout;
|
||||
|
||||
if (parameters.NodeDialTimeout <= 0)
|
||||
parameters.NodeDialTimeout = defaultDialTimeout;
|
||||
|
||||
if (parameters.NodeStreamTimeout <= 0)
|
||||
parameters.NodeStreamTimeout = defaultStreamTimeout;
|
||||
|
||||
if (parameters.SessionExpirationDuration == 0)
|
||||
parameters.SessionExpirationDuration = defaultSessionTokenExpirationDuration;
|
||||
|
||||
parameters.ClientBuilder ??= new Func<string, ClientWrapper>((address) =>
|
||||
{
|
||||
var wrapperPrm = new WrapperPrm
|
||||
{
|
||||
Address = address,
|
||||
Key = parameters.Key,
|
||||
Logger = parameters.Logger,
|
||||
DialTimeout = parameters.NodeDialTimeout,
|
||||
StreamTimeout = parameters.NodeStreamTimeout,
|
||||
ErrorThreshold = parameters.ErrorThreshold,
|
||||
GracefulCloseOnSwitchTimeout = parameters.GracefulCloseOnSwitchTimeout
|
||||
};
|
||||
|
||||
return new ClientWrapper(wrapperPrm, pool);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private ClientWrapper Connection()
|
||||
{
|
||||
foreach (var pool in InnerPools!)
|
||||
{
|
||||
var client = pool.Connection();
|
||||
if (client != null)
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FrostFsException("Cannot find alive client");
|
||||
}
|
||||
|
||||
private static async Task<FrostFsSessionToken?> InitSessionForDuration(CallContext ctx, ClientWrapper cw, ulong duration, ECDsa key, bool clientCut)
|
||||
{
|
||||
var client = cw.Client;
|
||||
var networkInfo = await client!.GetNetworkSettingsAsync(new PrmNetworkSettings(ctx)).ConfigureAwait(false);
|
||||
|
||||
var epoch = networkInfo.Epoch;
|
||||
|
||||
ulong exp = ulong.MaxValue - epoch < duration
|
||||
? ulong.MaxValue
|
||||
: epoch + duration;
|
||||
|
||||
var prmSessionCreate = new PrmSessionCreate(exp, ctx);
|
||||
|
||||
return await client.CreateSessionAsync(prmSessionCreate).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
internal static string FormCacheKey(string address, string key)
|
||||
{
|
||||
return $"{address}{key}";
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
CancellationTokenSource.Cancel();
|
||||
|
||||
if (InnerPools != null)
|
||||
{
|
||||
// close all clients
|
||||
foreach (var innerPool in InnerPools)
|
||||
foreach (var client in innerPool.Clients)
|
||||
if (client.IsDialed())
|
||||
client.Client?.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// startRebalance runs loop to monitor connection healthy status.
|
||||
internal void StartRebalance(CallContext ctx)
|
||||
{
|
||||
var buffers = new double[RebalanceParams.NodesParams.Length][];
|
||||
|
||||
for (int i = 0; i < RebalanceParams.NodesParams.Length; i++)
|
||||
{
|
||||
var parameters = RebalanceParams.NodesParams[i];
|
||||
buffers[i] = new double[parameters.Weights.Count];
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay((int)RebalanceParams.ClientRebalanceInterval).ConfigureAwait(false);
|
||||
UpdateNodesHealth(ctx, buffers);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateNodesHealth(CallContext ctx, double[][] buffers)
|
||||
{
|
||||
var tasks = new Task[InnerPools!.Length];
|
||||
|
||||
for (int i = 0; i < InnerPools.Length; i++)
|
||||
{
|
||||
var bufferWeights = buffers[i];
|
||||
|
||||
tasks[i] = Task.Run(() => UpdateInnerNodesHealth(ctx, i, bufferWeights));
|
||||
}
|
||||
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
private async ValueTask UpdateInnerNodesHealth(CallContext ctx, int poolIndex, double[] bufferWeights)
|
||||
{
|
||||
if (poolIndex > InnerPools!.Length - 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var pool = InnerPools[poolIndex];
|
||||
|
||||
var options = RebalanceParams;
|
||||
|
||||
int healthyChanged = 0;
|
||||
|
||||
var tasks = new Task[pool.Clients.Length];
|
||||
|
||||
for (int j = 0; j < pool.Clients.Length; j++)
|
||||
{
|
||||
var client = pool.Clients[j];
|
||||
var healthy = false;
|
||||
string? error = null;
|
||||
var changed = false;
|
||||
|
||||
try
|
||||
{
|
||||
// check timeout settings
|
||||
changed = await client!.RestartIfUnhealthy(ctx).ConfigureAwait(false);
|
||||
healthy = true;
|
||||
bufferWeights[j] = options.NodesParams[poolIndex].Weights[j];
|
||||
}
|
||||
// TODO: specify
|
||||
catch (FrostFsException e)
|
||||
{
|
||||
error = e.Message;
|
||||
bufferWeights[j] = 0;
|
||||
|
||||
SessionCache.DeleteByPrefix(client.Address);
|
||||
}
|
||||
|
||||
if (changed)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
if (logger != null)
|
||||
{
|
||||
FrostFsMessages.HealthChanged(logger, client.Address, healthy, error!);
|
||||
}
|
||||
|
||||
Interlocked.Exchange(ref healthyChanged, 1);
|
||||
}
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
||||
if (Interlocked.CompareExchange(ref healthyChanged, -1, -1) == 1)
|
||||
{
|
||||
var probabilities = AdjustWeights(bufferWeights);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
pool.Sampler = new Sampler(probabilities);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO: remove
|
||||
private bool CheckSessionTokenErr(Exception error, string address)
|
||||
{
|
||||
if (error == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (error is SessionNotFoundException || error is SessionExpiredException)
|
||||
{
|
||||
this.SessionCache.DeleteByPrefix(address);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Statistic Statistic()
|
||||
{
|
||||
if (InnerPools == null)
|
||||
{
|
||||
throw new FrostFsInvalidObjectException(nameof(Pool));
|
||||
}
|
||||
|
||||
var statistics = new Statistic();
|
||||
|
||||
foreach (var inner in InnerPools)
|
||||
{
|
||||
int valueIndex = 0;
|
||||
var nodes = new string[inner.Clients.Length];
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (var client in inner.Clients)
|
||||
{
|
||||
if (client.IsHealthy())
|
||||
{
|
||||
nodes[valueIndex] = client.Address;
|
||||
}
|
||||
|
||||
var node = new NodeStatistic
|
||||
{
|
||||
Address = client.Address,
|
||||
Methods = client.MethodsStatus(),
|
||||
OverallErrors = client.GetOverallErrorRate(),
|
||||
CurrentErrors = client.GetCurrentErrorRate()
|
||||
};
|
||||
|
||||
statistics.Nodes.Add(node);
|
||||
|
||||
valueIndex++;
|
||||
|
||||
statistics.OverallErrors += node.OverallErrors;
|
||||
}
|
||||
|
||||
if (statistics.CurrentNodes == null || statistics.CurrentNodes.Length == 0)
|
||||
{
|
||||
statistics.CurrentNodes = nodes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
public async Task<FrostFsNetmapSnapshot> GetNetmapSnapshotAsync(PrmNetmapSnapshot? args = null)
|
||||
{
|
||||
var client = Connection();
|
||||
|
||||
args ??= new();
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetNetmapSnapshotAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsNodeInfo> GetNodeInfoAsync(PrmNodeInfo? args = null)
|
||||
{
|
||||
var client = Connection();
|
||||
|
||||
args ??= new();
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetNodeInfoAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<NetworkSettings> GetNetworkSettingsAsync(PrmNetworkSettings? args = null)
|
||||
{
|
||||
var client = Connection();
|
||||
|
||||
args ??= new();
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetNetworkSettingsAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsSessionToken> CreateSessionAsync(PrmSessionCreate args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.CreateSessionAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<byte[]> AddChainAsync(PrmApeChainAdd args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.AddChainAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task RemoveChainAsync(PrmApeChainRemove args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
await client.Client!.RemoveChainAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<Chain[]> ListChainAsync(PrmApeChainList args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.ListChainAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsContainerInfo> GetContainerAsync(PrmContainerGet args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetContainerAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<FrostFsContainerId> ListContainersAsync(PrmContainerGetAll? args = null)
|
||||
{
|
||||
var client = Connection();
|
||||
|
||||
args ??= new();
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return client.Client!.ListContainersAsync(args);
|
||||
}
|
||||
|
||||
public async Task<FrostFsContainerId> CreateContainerAsync(PrmContainerCreate args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.CreateContainerAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task DeleteContainerAsync(PrmContainerDelete args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
await client.Client!.DeleteContainerAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsObjectHeader> GetObjectHeadAsync(PrmObjectHeadGet args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetObjectHeadAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsObject> GetObjectAsync(PrmObjectGet args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetObjectAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsObjectId> PutObjectAsync(PrmObjectPut args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.PutObjectAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<FrostFsObjectId> PutSingleObjectAsync(PrmSingleObjectPut args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.PutSingleObjectAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task DeleteObjectAsync(PrmObjectDelete args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
await client.Client!.DeleteObjectAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<FrostFsObjectId> SearchObjectsAsync(PrmObjectSearch args)
|
||||
{
|
||||
if (args is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args));
|
||||
}
|
||||
|
||||
var client = Connection();
|
||||
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return client.Client!.SearchObjectsAsync(args);
|
||||
}
|
||||
|
||||
public async Task<Accounting.Decimal> GetBalanceAsync(PrmBalance? args)
|
||||
{
|
||||
var client = Connection();
|
||||
|
||||
args ??= new();
|
||||
args.Context.PoolErrorHandler = client.HandleError;
|
||||
|
||||
return await client.Client!.GetBalanceAsync(args).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, CallContext ctx)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class RebalanceParameters(
|
||||
NodesParam[] nodesParams,
|
||||
ulong nodeRequestTimeout,
|
||||
ulong clientRebalanceInterval,
|
||||
ulong sessionExpirationDuration)
|
||||
{
|
||||
public NodesParam[] NodesParams { get; set; } = nodesParams;
|
||||
|
||||
public ulong NodeRequestTimeout { get; set; } = nodeRequestTimeout;
|
||||
|
||||
public ulong ClientRebalanceInterval { get; set; } = clientRebalanceInterval;
|
||||
|
||||
public ulong SessionExpirationDuration { get; set; } = sessionExpirationDuration;
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// RequestInfo groups info about pool request.
|
||||
struct RequestInfo
|
||||
{
|
||||
public string Address { get; set; }
|
||||
|
||||
public MethodIndex MethodIndex { get; set; }
|
||||
|
||||
public TimeSpan Elapsed { get; set; }
|
||||
}
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class Sampler
|
||||
{
|
||||
private readonly object _lock = new();
|
||||
|
||||
private Random random = new();
|
||||
|
||||
internal double[] Probabilities { get; set; }
|
||||
internal int[] Alias { get; set; }
|
||||
|
||||
internal Sampler(double[] probabilities)
|
||||
{
|
||||
var small = new WorkList();
|
||||
var large = new WorkList();
|
||||
|
||||
var n = probabilities.Length;
|
||||
|
||||
// sampler.randomGenerator = rand.New(source)
|
||||
Probabilities = new double[n];
|
||||
Alias = new int[n];
|
||||
|
||||
// Compute scaled probabilities.
|
||||
var p = new double[n];
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
p[i] = probabilities[i] * n;
|
||||
if (p[i] < 1)
|
||||
small.Add(i);
|
||||
else
|
||||
large.Add(i);
|
||||
}
|
||||
|
||||
while (small.Length > 0 && large.Length > 0)
|
||||
{
|
||||
var l = small.Remove();
|
||||
var g = large.Remove();
|
||||
|
||||
Probabilities[l] = p[l];
|
||||
Alias[l] = g;
|
||||
|
||||
p[g] = p[g] + p[l] - 1;
|
||||
|
||||
if (p[g] < 1)
|
||||
small.Add(g);
|
||||
else
|
||||
large.Add(g);
|
||||
}
|
||||
|
||||
while (large.Length > 0)
|
||||
{
|
||||
var g = large.Remove();
|
||||
Probabilities[g] = 1;
|
||||
}
|
||||
|
||||
while (small.Length > 0)
|
||||
{
|
||||
var l = small.Remove();
|
||||
probabilities[l] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
internal int Next()
|
||||
{
|
||||
var n = Alias.Length;
|
||||
|
||||
int i;
|
||||
double f;
|
||||
lock (_lock)
|
||||
{
|
||||
i = random.Next(0, n - 1);
|
||||
f = random.NextDouble();
|
||||
}
|
||||
|
||||
if (f < Probabilities[i])
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
||||
return Alias[i];
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal struct SessionCache(ulong sessionExpirationDuration)
|
||||
{
|
||||
internal Hashtable Cache { get; } = [];
|
||||
|
||||
internal ulong CurrentEpoch { get; set; }
|
||||
|
||||
internal ulong TokenDuration { get; set; } = sessionExpirationDuration;
|
||||
|
||||
internal void DeleteByPrefix(string prefix)
|
||||
{
|
||||
foreach (var key in Cache.Keys)
|
||||
{
|
||||
if (((string)key).StartsWith(prefix, StringComparison.Ordinal))
|
||||
{
|
||||
Cache.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public sealed class Statistic
|
||||
{
|
||||
public ulong OverallErrors { get; internal set; }
|
||||
|
||||
public Collection<NodeStatistic> Nodes { get; } = [];
|
||||
|
||||
public string[]? CurrentNodes { get; internal set; }
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class StatusSnapshot()
|
||||
{
|
||||
public ulong AllTime { get; internal set; }
|
||||
|
||||
public ulong AllRequests { get; internal set; }
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class WorkList
|
||||
{
|
||||
private readonly List<int> elements = [];
|
||||
|
||||
internal int Length
|
||||
{
|
||||
get { return elements.Count; }
|
||||
}
|
||||
|
||||
internal void Add(int element)
|
||||
{
|
||||
elements.Add(element);
|
||||
}
|
||||
|
||||
internal int Remove()
|
||||
{
|
||||
int last = elements.LastOrDefault();
|
||||
elements.RemoveAt(elements.Count - 1);
|
||||
return last;
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Grpc.Net.Client;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
// wrapperPrm is params to create clientWrapper.
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1815:Override equals and operator equals on value types", Justification = "<Pending>")]
|
||||
public struct WrapperPrm
|
||||
{
|
||||
internal ILogger? Logger { get; set; }
|
||||
|
||||
internal string Address { get; set; }
|
||||
|
||||
internal ECDsa? Key { get; set; }
|
||||
|
||||
internal ulong DialTimeout { get; set; }
|
||||
|
||||
internal ulong StreamTimeout { get; set; }
|
||||
|
||||
internal uint ErrorThreshold { get; set; }
|
||||
|
||||
internal Action ResponseInfoCallback { get; set; }
|
||||
|
||||
internal Action PoolRequestInfoCallback { get; set; }
|
||||
|
||||
internal GrpcChannelOptions GrpcChannelOptions { get; set; }
|
||||
|
||||
internal ulong GracefulCloseOnSwitchTimeout { get; set; }
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
using FrostFS.Accounting;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class AccountingServiceProvider : ContextAccessor
|
||||
{
|
||||
private readonly AccountingService.AccountingServiceClient? _accountingServiceClient;
|
||||
|
||||
internal AccountingServiceProvider(
|
||||
AccountingService.AccountingServiceClient? accountingServiceClient,
|
||||
ClientContext context)
|
||||
: base(context)
|
||||
{
|
||||
_accountingServiceClient = accountingServiceClient;
|
||||
}
|
||||
|
||||
internal async Task<Decimal> GetBallance(PrmBalance args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
|
||||
BalanceRequest request = new()
|
||||
{
|
||||
Body = new()
|
||||
{
|
||||
OwnerId = ctx.OwnerId!.OwnerID
|
||||
}
|
||||
};
|
||||
|
||||
request.AddMetaHeader(args.XHeaders);
|
||||
request.Sign(ctx.Key!);
|
||||
|
||||
var response = await _accountingServiceClient!.BalanceAsync(request, null, ctx.Deadline, ctx.CancellationToken);
|
||||
|
||||
Verifier.CheckResponse(response);
|
||||
|
||||
return response.Body.Balance;
|
||||
}
|
||||
}
|
|
@ -12,28 +12,20 @@ using FrostFS.Session;
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class ContainerServiceProvider(ContainerService.ContainerServiceClient service, ClientContext clientCtx) : ContextAccessor(clientCtx), ISessionProvider
|
||||
internal sealed class ContainerServiceProvider(ContainerService.ContainerServiceClient service, ClientEnvironment context) : ContextAccessor(context), ISessionProvider
|
||||
{
|
||||
private SessionProvider? sessions;
|
||||
readonly SessionProvider sessions = new(context);
|
||||
|
||||
public async ValueTask<SessionToken> GetOrCreateSession(ISessionToken args, CallContext ctx)
|
||||
public async ValueTask<SessionToken> GetOrCreateSession(ISessionToken args, Context ctx)
|
||||
{
|
||||
sessions ??= new(ClientContext);
|
||||
|
||||
if (ClientContext.SessionCache.Cache != null &&
|
||||
ClientContext.SessionCache.Cache.ContainsKey(ClientContext.SessionCacheKey))
|
||||
{
|
||||
return (SessionToken)ClientContext.SessionCache.Cache[ClientContext.SessionCacheKey];
|
||||
}
|
||||
|
||||
return await sessions.GetOrCreateSession(args, ctx).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
internal async Task<FrostFsContainerInfo> GetContainerAsync(PrmContainerGet args)
|
||||
{
|
||||
GetRequest request = GetContainerRequest(args.Container.ContainerID, args.XHeaders, args.Context);
|
||||
GetRequest request = GetContainerRequest(args.Container.ContainerID, args.XHeaders, args.Context!);
|
||||
|
||||
var response = await service.GetAsync(request, null, args.Context.Deadline, args.Context.CancellationToken);
|
||||
var response = await service.GetAsync(request, null, args.Context!.Deadline, args.Context.CancellationToken);
|
||||
|
||||
Verifier.CheckResponse(response);
|
||||
|
||||
|
@ -43,13 +35,13 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService
|
|||
internal async IAsyncEnumerable<FrostFsContainerId> ListContainersAsync(PrmContainerGetAll args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.OwnerId ??= ClientContext.Owner;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.OwnerId ??= Context.Owner;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
if (ctx.OwnerId == null)
|
||||
throw new ArgumentException(nameof(ctx.OwnerId));
|
||||
throw new InvalidObjectException(nameof(ctx.OwnerId));
|
||||
|
||||
var request = new ListRequest
|
||||
{
|
||||
|
@ -82,11 +74,11 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService
|
|||
grpcContainer.Version ??= ctx.Version?.ToMessage();
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
if (grpcContainer.OwnerId == null)
|
||||
throw new ArgumentException(nameof(grpcContainer.OwnerId));
|
||||
throw new InvalidObjectException(nameof(grpcContainer.OwnerId));
|
||||
if (grpcContainer.Version == null)
|
||||
throw new ArgumentException(nameof(grpcContainer.Version));
|
||||
throw new InvalidObjectException(nameof(grpcContainer.Version));
|
||||
|
||||
var request = new PutRequest
|
||||
{
|
||||
|
@ -122,7 +114,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService
|
|||
{
|
||||
var ctx = args.Context!;
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new DeleteRequest
|
||||
{
|
||||
|
@ -155,10 +147,10 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService
|
|||
Verifier.CheckResponse(response);
|
||||
}
|
||||
|
||||
private static GetRequest GetContainerRequest(ContainerID id, NameValueCollection? xHeaders, CallContext ctx)
|
||||
private static GetRequest GetContainerRequest(ContainerID id, NameValueCollection? xHeaders, Context ctx)
|
||||
{
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(ctx), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new GetRequest
|
||||
{
|
||||
|
@ -180,7 +172,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService
|
|||
Removed
|
||||
}
|
||||
|
||||
private async Task WaitForContainer(WaitExpects expect, ContainerID id, PrmWait? waitParams, CallContext ctx)
|
||||
private async Task WaitForContainer(WaitExpects expect, ContainerID id, PrmWait? waitParams, Context ctx)
|
||||
{
|
||||
var request = GetContainerRequest(id, null, ctx);
|
||||
|
||||
|
@ -215,7 +207,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService
|
|||
|
||||
await Task.Delay(waitParams.PollInterval).ConfigureAwait(false);
|
||||
}
|
||||
catch (FrostFsResponseException ex)
|
||||
catch (ResponseException ex)
|
||||
{
|
||||
if (DateTime.UtcNow >= deadLine)
|
||||
throw new TimeoutException();
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
@ -13,33 +12,27 @@ internal sealed class NetmapServiceProvider : ContextAccessor
|
|||
{
|
||||
private readonly NetmapService.NetmapServiceClient netmapServiceClient;
|
||||
|
||||
internal NetmapServiceProvider(NetmapService.NetmapServiceClient netmapServiceClient, ClientContext context)
|
||||
internal NetmapServiceProvider(NetmapService.NetmapServiceClient netmapServiceClient, ClientEnvironment context)
|
||||
: base(context)
|
||||
{
|
||||
this.netmapServiceClient = netmapServiceClient;
|
||||
}
|
||||
|
||||
internal async Task<NetworkSettings> GetNetworkSettingsAsync(CallContext ctx)
|
||||
internal async Task<NetworkSettings> GetNetworkSettingsAsync(Context ctx)
|
||||
{
|
||||
if (ClientContext.NetworkSettings != null)
|
||||
return ClientContext.NetworkSettings;
|
||||
if (Context.NetworkSettings != null)
|
||||
return Context.NetworkSettings;
|
||||
|
||||
var response = await GetNetworkInfoAsync(ctx).ConfigureAwait(false);
|
||||
var info = await GetNetworkInfoAsync(ctx).ConfigureAwait(false);
|
||||
|
||||
var settings = new NetworkSettings();
|
||||
|
||||
var info = response.Body.NetworkInfo;
|
||||
|
||||
settings.Epoch = info.CurrentEpoch;
|
||||
settings.MagicNumber = info.MagicNumber;
|
||||
settings.MsPerBlock = info.MsPerBlock;
|
||||
|
||||
foreach (var param in info.NetworkConfig.Parameters)
|
||||
foreach (var param in info.Body.NetworkInfo.NetworkConfig.Parameters)
|
||||
{
|
||||
SetNetworksParam(param, settings);
|
||||
}
|
||||
|
||||
ClientContext.NetworkSettings = settings;
|
||||
Context.NetworkSettings = settings;
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
@ -47,10 +40,10 @@ internal sealed class NetmapServiceProvider : ContextAccessor
|
|||
internal async Task<FrostFsNodeInfo> GetLocalNodeInfoAsync(PrmNodeInfo args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new LocalNodeInfoRequest
|
||||
{
|
||||
|
@ -67,12 +60,12 @@ internal sealed class NetmapServiceProvider : ContextAccessor
|
|||
return response.Body.ToModel();
|
||||
}
|
||||
|
||||
internal async Task<NetworkInfoResponse> GetNetworkInfoAsync(CallContext ctx)
|
||||
internal async Task<NetworkInfoResponse> GetNetworkInfoAsync(Context ctx)
|
||||
{
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(ctx), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new NetworkInfoRequest();
|
||||
|
||||
|
@ -90,10 +83,10 @@ internal sealed class NetmapServiceProvider : ContextAccessor
|
|||
internal async Task<FrostFsNetmapSnapshot> GetNetmapSnapshotAsync(PrmNetmapSnapshot args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new NetmapSnapshotRequest();
|
||||
|
||||
|
|
|
@ -15,32 +15,30 @@ using Google.Protobuf;
|
|||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient client, ClientContext clientCtx)
|
||||
: ContextAccessor(clientCtx), ISessionProvider
|
||||
internal sealed class ObjectServiceProvider : ContextAccessor, ISessionProvider
|
||||
{
|
||||
private SessionProvider? sessions;
|
||||
private readonly ObjectService.ObjectServiceClient client = client;
|
||||
private readonly SessionProvider sessions;
|
||||
private ObjectService.ObjectServiceClient client;
|
||||
|
||||
public async ValueTask<SessionToken> GetOrCreateSession(ISessionToken args, CallContext ctx)
|
||||
internal ObjectServiceProvider(ObjectService.ObjectServiceClient client, ClientEnvironment env)
|
||||
: base(env)
|
||||
{
|
||||
sessions ??= new(ClientContext);
|
||||
|
||||
if (ClientContext.SessionCache.Cache != null &&
|
||||
ClientContext.SessionCache.Cache.ContainsKey(ClientContext.SessionCacheKey))
|
||||
{
|
||||
return (SessionToken)ClientContext.SessionCache.Cache[ClientContext.SessionCacheKey];
|
||||
this.sessions = new(Context);
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async ValueTask<SessionToken> GetOrCreateSession(ISessionToken args, Context ctx)
|
||||
{
|
||||
return await sessions.GetOrCreateSession(args, ctx).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
internal async Task<FrostFsObjectHeader> GetObjectHeadAsync(PrmObjectHeadGet args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new HeadRequest
|
||||
{
|
||||
|
@ -76,10 +74,10 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
{
|
||||
var ctx = args.Context!;
|
||||
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new GetRequest
|
||||
{
|
||||
|
@ -110,10 +108,10 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
internal async Task DeleteObjectAsync(PrmObjectDelete args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new DeleteRequest
|
||||
{
|
||||
|
@ -147,7 +145,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
var ctx = args.Context!;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var request = new SearchRequest
|
||||
{
|
||||
|
@ -185,10 +183,10 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
throw new ArgumentNullException(nameof(args));
|
||||
|
||||
if (args.Header == null)
|
||||
throw new ArgumentNullException(nameof(args), "Header is null");
|
||||
throw new ArgumentException(nameof(args.Header));
|
||||
|
||||
if (args.Payload == null)
|
||||
throw new ArgumentNullException(nameof(args), "Payload is null");
|
||||
throw new ArgumentException(nameof(args.Payload));
|
||||
|
||||
if (args.ClientCut)
|
||||
return await PutClientCutObject(args).ConfigureAwait(false);
|
||||
|
@ -208,7 +206,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
var ctx = args.Context!;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var grpcObject = ObjectTools.CreateObject(args.FrostFsObject, ctx);
|
||||
|
||||
|
@ -240,7 +238,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
var ctx = args.Context!;
|
||||
|
||||
var tokenRaw = await GetOrCreateSession(args, ctx).ConfigureAwait(false);
|
||||
var token = new FrostFsSessionToken(tokenRaw.Serialize(), tokenRaw.Body.Id.ToUuid());
|
||||
var token = new FrostFsSessionToken(tokenRaw.Serialize());
|
||||
|
||||
args.SessionToken = token;
|
||||
|
||||
|
@ -256,7 +254,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
|
||||
if (args.MaxObjectSizeCache == 0)
|
||||
{
|
||||
var networkSettings = await ClientContext.Client.GetNetworkSettingsAsync(new PrmNetworkSettings(ctx))
|
||||
var networkSettings = await Context.Client.GetNetworkSettingsAsync(new PrmNetworkSettings() { Context = ctx })
|
||||
.ConfigureAwait(false);
|
||||
|
||||
args.MaxObjectSizeCache = (int)networkSettings.MaxObjectSize;
|
||||
|
@ -308,7 +306,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
|
||||
var linkObject = new FrostFsLinkObject(header.ContainerId, split!.SplitId, largeObjectHeader, sentObjectIds);
|
||||
|
||||
_ = await PutSingleObjectAsync(new PrmSingleObjectPut(linkObject, args.Context)).ConfigureAwait(false);
|
||||
_ = await PutSingleObjectAsync(new PrmSingleObjectPut(linkObject) { Context = args.Context }).ConfigureAwait(false);
|
||||
|
||||
var parentHeader = args.Header.GetHeader();
|
||||
|
||||
|
@ -333,7 +331,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
{
|
||||
var ctx = args.Context!;
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
var payload = args.Payload!;
|
||||
|
||||
|
@ -354,7 +352,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
}
|
||||
else
|
||||
{
|
||||
chunkBuffer = ClientContext.GetArrayPool(Constants.ObjectChunkSize).Rent(chunkSize);
|
||||
chunkBuffer = Context.GetArrayPool(Constants.ObjectChunkSize).Rent(chunkSize);
|
||||
isRentBuffer = true;
|
||||
}
|
||||
|
||||
|
@ -406,12 +404,12 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
}
|
||||
}
|
||||
|
||||
private async Task<ObjectStreamer> GetUploadStream(PrmObjectPut args, CallContext ctx)
|
||||
private async Task<ObjectStreamer> GetUploadStream(PrmObjectPut args, Context ctx)
|
||||
{
|
||||
var header = args.Header!;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
header.OwnerId ??= ctx.OwnerId;
|
||||
header.Version ??= ctx.Version;
|
||||
|
@ -451,7 +449,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
return await PutObjectInit(initRequest, ctx).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task<FrostFsObject> GetObject(GetRequest request, CallContext ctx)
|
||||
private async Task<FrostFsObject> GetObject(GetRequest request, Context ctx)
|
||||
{
|
||||
var reader = GetObjectInit(request, ctx);
|
||||
|
||||
|
@ -463,7 +461,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
return modelObject;
|
||||
}
|
||||
|
||||
private ObjectReader GetObjectInit(GetRequest initRequest, CallContext ctx)
|
||||
private ObjectReader GetObjectInit(GetRequest initRequest, Context ctx)
|
||||
{
|
||||
if (initRequest is null)
|
||||
throw new ArgumentNullException(nameof(initRequest));
|
||||
|
@ -473,7 +471,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
return new ObjectReader(call);
|
||||
}
|
||||
|
||||
private async Task<ObjectStreamer> PutObjectInit(PutRequest initRequest, CallContext ctx)
|
||||
private async Task<ObjectStreamer> PutObjectInit(PutRequest initRequest, Context ctx)
|
||||
{
|
||||
if (initRequest is null)
|
||||
{
|
||||
|
@ -487,7 +485,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
return new ObjectStreamer(call);
|
||||
}
|
||||
|
||||
private async IAsyncEnumerable<ObjectID> SearchObjects(SearchRequest request, CallContext ctx)
|
||||
private async IAsyncEnumerable<ObjectID> SearchObjects(SearchRequest request, Context ctx)
|
||||
{
|
||||
using var stream = GetSearchReader(request, ctx);
|
||||
|
||||
|
@ -505,7 +503,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
}
|
||||
}
|
||||
|
||||
private SearchReader GetSearchReader(SearchRequest initRequest, CallContext ctx)
|
||||
private SearchReader GetSearchReader(SearchRequest initRequest, Context ctx)
|
||||
{
|
||||
if (initRequest is null)
|
||||
{
|
||||
|
|
|
@ -10,7 +10,7 @@ internal sealed class SessionServiceProvider : ContextAccessor
|
|||
{
|
||||
private readonly SessionService.SessionServiceClient? _sessionServiceClient;
|
||||
|
||||
internal SessionServiceProvider(SessionService.SessionServiceClient? sessionServiceClient, ClientContext context)
|
||||
internal SessionServiceProvider(SessionService.SessionServiceClient? sessionServiceClient, ClientEnvironment context)
|
||||
: base(context)
|
||||
{
|
||||
_sessionServiceClient = sessionServiceClient;
|
||||
|
@ -20,7 +20,7 @@ internal sealed class SessionServiceProvider : ContextAccessor
|
|||
{
|
||||
var ctx = args.Context!;
|
||||
|
||||
ctx.OwnerId ??= ClientContext.Owner;
|
||||
ctx.OwnerId ??= Context.Owner;
|
||||
|
||||
var request = new CreateRequest
|
||||
{
|
||||
|
@ -37,7 +37,7 @@ internal sealed class SessionServiceProvider : ContextAccessor
|
|||
return await CreateSession(request, args.Context!).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
internal async Task<SessionToken> CreateSession(CreateRequest request, CallContext ctx)
|
||||
internal async Task<SessionToken> CreateSession(CreateRequest request, Context ctx)
|
||||
{
|
||||
var response = await _sessionServiceClient!.CreateAsync(request, null, ctx.Deadline, ctx.CancellationToken);
|
||||
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Frostfs.V2.Ape;
|
||||
using Frostfs.V2.Apemanager;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2.Services;
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal sealed class ApeManagerServiceProvider : ContextAccessor
|
||||
{
|
||||
private readonly APEManagerService.APEManagerServiceClient? _apeManagerServiceClient;
|
||||
|
||||
internal ApeManagerServiceProvider(APEManagerService.APEManagerServiceClient? apeManagerServiceClient, ClientContext context)
|
||||
internal ApeManagerServiceProvider(APEManagerService.APEManagerServiceClient? apeManagerServiceClient, ClientEnvironment context)
|
||||
: base(context)
|
||||
{
|
||||
_apeManagerServiceClient = apeManagerServiceClient;
|
||||
|
@ -19,10 +18,10 @@ internal sealed class ApeManagerServiceProvider : ContextAccessor
|
|||
internal async Task<byte[]> AddChainAsync(PrmApeChainAdd args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
AddChainRequest request = new()
|
||||
{
|
||||
|
@ -46,10 +45,10 @@ internal sealed class ApeManagerServiceProvider : ContextAccessor
|
|||
internal async Task RemoveChainAsync(PrmApeChainRemove args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
RemoveChainRequest request = new()
|
||||
{
|
||||
|
@ -71,10 +70,10 @@ internal sealed class ApeManagerServiceProvider : ContextAccessor
|
|||
internal async Task<Chain[]> ListChainAsync(PrmApeChainList args)
|
||||
{
|
||||
var ctx = args.Context!;
|
||||
ctx.Key ??= ClientContext.Key?.ECDsaKey;
|
||||
ctx.Key ??= Context.Key?.ECDsaKey;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new ArgumentNullException(nameof(args), "Key is null");
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
ListChainsRequest request = new()
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
internal class ContextAccessor(ClientContext context)
|
||||
internal class ContextAccessor(ClientEnvironment context)
|
||||
{
|
||||
protected ClientContext ClientContext { get; set; } = context;
|
||||
protected ClientEnvironment Context { get; set; } = context;
|
||||
}
|
||||
|
|
|
@ -4,16 +4,16 @@ namespace FrostFS.SDK.ClientV2;
|
|||
|
||||
internal interface ISessionProvider
|
||||
{
|
||||
ValueTask<Session.SessionToken> GetOrCreateSession(ISessionToken args, CallContext ctx);
|
||||
ValueTask<Session.SessionToken> GetOrCreateSession(ISessionToken args, Context ctx);
|
||||
}
|
||||
|
||||
internal sealed class SessionProvider(ClientContext envCtx)
|
||||
internal sealed class SessionProvider(ClientEnvironment env)
|
||||
{
|
||||
public async ValueTask<Session.SessionToken> GetOrCreateSession(ISessionToken args, CallContext ctx)
|
||||
public async ValueTask<Session.SessionToken> GetOrCreateSession(ISessionToken args, Context ctx)
|
||||
{
|
||||
if (args.SessionToken is null)
|
||||
{
|
||||
return await envCtx.Client.CreateSessionInternalAsync(new PrmSessionCreate(uint.MaxValue, ctx))
|
||||
return await env.Client.CreateSessionInternalAsync(new PrmSessionCreate(uint.MaxValue) { Context = ctx })
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,21 +2,16 @@ using System;
|
|||
using System.Buffers;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Grpc.Net.Client;
|
||||
|
||||
namespace FrostFS.SDK.ClientV2;
|
||||
|
||||
public class ClientContext(FrostFSClient client, ECDsa? key, FrostFsOwner? owner, GrpcChannel channel, FrostFsVersion version) : IDisposable
|
||||
public class ClientEnvironment(FrostFSClient client, ECDsa? key, FrostFsOwner? owner, GrpcChannel channel, FrostFsVersion version) : IDisposable
|
||||
{
|
||||
private ArrayPool<byte>? _arrayPool;
|
||||
private string? sessionKey;
|
||||
|
||||
internal FrostFsOwner? Owner { get; } = owner;
|
||||
|
||||
internal string? Address { get; } = channel.Target;
|
||||
|
||||
internal GrpcChannel Channel { get; private set; } = channel;
|
||||
|
||||
internal FrostFsVersion Version { get; } = version;
|
||||
|
@ -27,21 +22,6 @@ public class ClientContext(FrostFSClient client, ECDsa? key, FrostFsOwner? owner
|
|||
|
||||
internal ClientKey? Key { get; } = key != null ? new ClientKey(key) : null;
|
||||
|
||||
internal SessionCache SessionCache { get; set; }
|
||||
|
||||
internal string? SessionCacheKey
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sessionKey == null && Key != null && Address != null)
|
||||
{
|
||||
sessionKey = Pool.FormCacheKey(Address, Key.ECDsaKey.PrivateKey().ToString());
|
||||
}
|
||||
|
||||
return sessionKey;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Custom pool is used for predefined sizes of buffers like grpc chunk
|
||||
/// </summary>
|
|
@ -4,10 +4,6 @@ namespace FrostFS.SDK.ClientV2;
|
|||
|
||||
public class NetworkSettings
|
||||
{
|
||||
public ulong Epoch { get; internal set; }
|
||||
public ulong MagicNumber { get; internal set; }
|
||||
public long MsPerBlock { get; internal set; }
|
||||
|
||||
public ulong AuditFee { get; internal set; }
|
||||
public ulong BasicIncomeRate { get; internal set; }
|
||||
public ulong ContainerFee { get; internal set; }
|
||||
|
|
|
@ -17,13 +17,13 @@ public sealed class ObjectReader(AsyncServerStreamingCall<GetResponse> call) : I
|
|||
internal async Task<Object.Object> ReadHeader()
|
||||
{
|
||||
if (!await Call.ResponseStream.MoveNext().ConfigureAwait(false))
|
||||
throw new FrostFsStreamException("unexpected end of stream");
|
||||
throw new InvalidOperationException("unexpected end of stream");
|
||||
|
||||
var response = Call.ResponseStream.Current;
|
||||
Verifier.CheckResponse(response);
|
||||
|
||||
if (response.Body.ObjectPartCase != GetResponse.Types.Body.ObjectPartOneofCase.Init)
|
||||
throw new FrostFsStreamException("unexpected message type");
|
||||
throw new InvalidOperationException("unexpected message type");
|
||||
|
||||
return new Object.Object
|
||||
{
|
||||
|
@ -41,7 +41,7 @@ public sealed class ObjectReader(AsyncServerStreamingCall<GetResponse> call) : I
|
|||
Verifier.CheckResponse(response);
|
||||
|
||||
if (response.Body.ObjectPartCase != GetResponse.Types.Body.ObjectPartOneofCase.Chunk)
|
||||
throw new FrostFsStreamException("unexpected message type");
|
||||
throw new InvalidOperationException("unexpected message type");
|
||||
|
||||
return response.Body.Chunk.Memory;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace FrostFS.SDK.ClientV2;
|
|||
|
||||
internal static class ObjectTools
|
||||
{
|
||||
internal static FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, CallContext ctx)
|
||||
internal static FrostFsObjectId CalculateObjectId(FrostFsObjectHeader header, Context ctx)
|
||||
{
|
||||
var grpcHeader = CreateHeader(header, [], ctx);
|
||||
|
||||
|
@ -21,7 +21,7 @@ internal static class ObjectTools
|
|||
return new ObjectID { Value = grpcHeader.Sha256() }.ToModel();
|
||||
}
|
||||
|
||||
internal static Object.Object CreateObject(FrostFsObject @object, CallContext ctx)
|
||||
internal static Object.Object CreateObject(FrostFsObject @object, Context ctx)
|
||||
{
|
||||
@object.Header.OwnerId ??= ctx.OwnerId;
|
||||
@object.Header.Version ??= ctx.Version;
|
||||
|
@ -53,13 +53,13 @@ internal static class ObjectTools
|
|||
return obj;
|
||||
}
|
||||
|
||||
internal static void SetSplitValues(Header grpcHeader, FrostFsSplit split, CallContext ctx)
|
||||
internal static void SetSplitValues(Header grpcHeader, FrostFsSplit split, Context ctx)
|
||||
{
|
||||
if (split == null)
|
||||
return;
|
||||
|
||||
if (ctx.Key == null)
|
||||
throw new FrostFsInvalidObjectException(nameof(ctx.Key));
|
||||
throw new InvalidObjectException(nameof(ctx.Key));
|
||||
|
||||
grpcHeader.Split = new Header.Types.Split
|
||||
{
|
||||
|
@ -85,7 +85,7 @@ internal static class ObjectTools
|
|||
grpcHeader.Split.Previous = split.Previous?.ToMessage();
|
||||
}
|
||||
|
||||
internal static Header CreateHeader(FrostFsObjectHeader header, byte[]? payload, CallContext ctx)
|
||||
internal static Header CreateHeader(FrostFsObjectHeader header, byte[]? payload, Context ctx)
|
||||
{
|
||||
header.OwnerId ??= ctx.OwnerId;
|
||||
header.Version ??= ctx.Version;
|
||||
|
|
|
@ -76,11 +76,6 @@ public static class RequestSigner
|
|||
|
||||
public static byte[] SignData(this ECDsa key, byte[] data)
|
||||
{
|
||||
if (key is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
var hash = new byte[65];
|
||||
hash[0] = 0x04;
|
||||
|
||||
|
|
|
@ -122,7 +122,7 @@ public static class Verifier
|
|||
var status = resp.MetaHeader.Status.ToModel();
|
||||
|
||||
if (status != null && !status.IsSuccess)
|
||||
throw new FrostFsResponseException(status);
|
||||
throw new ResponseException(status);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -137,6 +137,6 @@ public static class Verifier
|
|||
}
|
||||
|
||||
if (!request.Verify())
|
||||
throw new FrostFsResponseException($"invalid response, type={request.GetType()}");
|
||||
throw new FormatException($"invalid response, type={request.GetType()}");
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
using FrostFS.SDK.ProtosV2.Interfaces;
|
||||
using FrostFS.Session;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
namespace FrostFS.Accounting;
|
||||
|
||||
public partial class BalanceRequest : IRequest
|
||||
{
|
||||
IMetaHeader IVerifiableMessage.GetMetaHeader()
|
||||
{
|
||||
return MetaHeader;
|
||||
}
|
||||
|
||||
IVerificationHeader IVerifiableMessage.GetVerificationHeader()
|
||||
{
|
||||
return VerifyHeader;
|
||||
}
|
||||
|
||||
void IVerifiableMessage.SetMetaHeader(IMetaHeader metaHeader)
|
||||
{
|
||||
MetaHeader = (RequestMetaHeader)metaHeader;
|
||||
}
|
||||
|
||||
void IVerifiableMessage.SetVerificationHeader(IVerificationHeader verificationHeader)
|
||||
{
|
||||
VerifyHeader = (RequestVerificationHeader)verificationHeader;
|
||||
}
|
||||
|
||||
public IMessage GetBody()
|
||||
{
|
||||
return Body;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class BalanceResponse : IResponse
|
||||
{
|
||||
IMetaHeader IVerifiableMessage.GetMetaHeader()
|
||||
{
|
||||
return MetaHeader;
|
||||
}
|
||||
|
||||
IVerificationHeader IVerifiableMessage.GetVerificationHeader()
|
||||
{
|
||||
return VerifyHeader;
|
||||
}
|
||||
|
||||
void IVerifiableMessage.SetMetaHeader(IMetaHeader metaHeader)
|
||||
{
|
||||
MetaHeader = (ResponseMetaHeader)metaHeader;
|
||||
}
|
||||
|
||||
void IVerifiableMessage.SetVerificationHeader(IVerificationHeader verificationHeader)
|
||||
{
|
||||
VerifyHeader = (ResponseVerificationHeader)verificationHeader;
|
||||
}
|
||||
|
||||
public IMessage GetBody()
|
||||
{
|
||||
return Body;
|
||||
}
|
||||
}
|
|
@ -1,14 +1,49 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.ClientV2.Mappers.GRPC;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
|
||||
public abstract class ContainerTestsBase
|
||||
{
|
||||
protected readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
protected ContainerMocker Mocker { get; set; }
|
||||
|
||||
protected ContainerTestsBase()
|
||||
{
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
Mocker = new ContainerMocker(this.key)
|
||||
{
|
||||
PlacementPolicy = new FrostFsPlacementPolicy(true, new FrostFsReplica(1)),
|
||||
Version = new FrostFsVersion(2, 13),
|
||||
ContainerGuid = Guid.NewGuid()
|
||||
};
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return ClientV2.FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
new NetworkMocker(this.key).GetMock().Object,
|
||||
new SessionMocker(this.key).GetMock().Object,
|
||||
Mocker.GetMock().Object,
|
||||
new ObjectMocker(this.key).GetMock().Object);
|
||||
}
|
||||
}
|
||||
|
||||
public class ContainerTest : ContainerTestsBase
|
||||
{
|
||||
[Fact]
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
public abstract class ContainerTestsBase
|
||||
{
|
||||
internal readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
protected ContainerMocker Mocker { get; set; }
|
||||
|
||||
protected ContainerTestsBase()
|
||||
{
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
Mocker = new ContainerMocker(this.key)
|
||||
{
|
||||
PlacementPolicy = new FrostFsPlacementPolicy(true, new FrostFsReplica(1)),
|
||||
Version = new FrostFsVersion(2, 13),
|
||||
ContainerGuid = Guid.NewGuid()
|
||||
};
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return ClientV2.FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
new NetworkMocker(this.key).GetMock().Object,
|
||||
new SessionMocker(this.key).GetMock().Object,
|
||||
Mocker.GetMock().Object,
|
||||
new ObjectMocker(this.key).GetMock().Object);
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
// This file is used by Code Analysis to maintain SuppressMessage
|
||||
// attributes that are applied to this project.
|
||||
// Project-level suppressions either have no target or are given
|
||||
// a specific target and scoped to a namespace, type, member, etc.
|
||||
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
using Grpc.Core;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Grpc.Core;
|
||||
using Grpc.Core.Interceptors;
|
||||
|
||||
namespace FrostFS.SDK.SmokeTests;
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods",
|
||||
Justification = "parameters are provided by GRPC infrastructure")]
|
||||
public class CallbackInterceptor(Action<string>? callback = null) : Interceptor
|
||||
public class MetricsInterceptor() : Interceptor
|
||||
{
|
||||
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
|
@ -22,11 +22,17 @@ public class CallbackInterceptor(Action<string>? callback = null) : Interceptor
|
|||
call.Dispose);
|
||||
}
|
||||
|
||||
private async Task<TResponse> HandleUnaryResponse<TResponse>(AsyncUnaryCall<TResponse> call)
|
||||
private static async Task<TResponse> HandleUnaryResponse<TResponse>(AsyncUnaryCall<TResponse> call)
|
||||
{
|
||||
var response = await call;
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
callback?.Invoke($"elapsed");
|
||||
var response = await call.ResponseAsync;
|
||||
|
||||
watch.Stop();
|
||||
|
||||
// Do something with call info
|
||||
// var elapsed = watch.ElapsedTicks * 1_000_000/Stopwatch.Frequency;
|
||||
|
||||
return response;
|
||||
}
|
|
@ -26,11 +26,8 @@ public class AsyncStreamReaderMock(string key, FrostFsObjectHeader objectHeader)
|
|||
OwnerId = objectHeader.OwnerId!.ToMessage()
|
||||
};
|
||||
|
||||
if (objectHeader.Attributes != null)
|
||||
{
|
||||
foreach (var attr in objectHeader.Attributes)
|
||||
header.Attributes.Add(attr.ToMessage());
|
||||
}
|
||||
|
||||
var response = new GetResponse
|
||||
{
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
using System.Collections.ObjectModel;
|
||||
|
||||
using FrostFS.SDK.ProtosV2.Interfaces;
|
||||
|
||||
using Grpc.Core;
|
||||
|
@ -8,15 +6,13 @@ namespace FrostFS.SDK.Tests;
|
|||
|
||||
public class ClientStreamWriter : IClientStreamWriter<IRequest>
|
||||
{
|
||||
private WriteOptions? _options;
|
||||
|
||||
public Collection<IRequest> Messages { get; } = [];
|
||||
public List<IRequest> Messages { get; set; } = [];
|
||||
public bool CompletedTask { get; private set; }
|
||||
|
||||
public WriteOptions? WriteOptions
|
||||
{
|
||||
get => _options;
|
||||
set => _options = value;
|
||||
get => throw new NotImplementedException();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task CompleteAsync()
|
||||
|
|
|
@ -25,10 +25,7 @@ public abstract class ServiceBase(string key)
|
|||
public static FrostFsVersion DefaultVersion { get; } = new(2, 13);
|
||||
public static FrostFsPlacementPolicy DefaultPlacementPolicy { get; } = new FrostFsPlacementPolicy(true, new FrostFsReplica(1));
|
||||
|
||||
#pragma warning disable CA2227 // this is specific object, should be treated as is
|
||||
public Metadata? Metadata { get; set; }
|
||||
#pragma warning restore CA2227
|
||||
|
||||
public Metadata Metadata { get; protected set; }
|
||||
public DateTime? DateTime { get; protected set; }
|
||||
|
||||
public CancellationToken CancellationToken { get; protected set; }
|
||||
|
@ -38,8 +35,6 @@ public abstract class ServiceBase(string key)
|
|||
|
||||
protected ResponseVerificationHeader GetResponseVerificationHeader(IResponse response)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(response);
|
||||
|
||||
var verifyHeader = new ResponseVerificationHeader
|
||||
{
|
||||
MetaSignature = new Refs.Signature
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
using System.Collections.ObjectModel;
|
||||
|
||||
using FrostFS.Container;
|
||||
using FrostFS.Refs;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
|
@ -43,7 +41,7 @@ public class ContainerMocker(string key) : ContainerServiceBase(key)
|
|||
putResponse.VerifyHeader = GetResponseVerificationHeader(putResponse);
|
||||
|
||||
var metadata = new Metadata();
|
||||
using var putContainerResponse = new AsyncUnaryCall<PutResponse>(
|
||||
var putContainerResponse = new AsyncUnaryCall<PutResponse>(
|
||||
Task.FromResult(putResponse),
|
||||
Task.FromResult(metadata),
|
||||
() => new Grpc.Core.Status(StatusCode.OK, string.Empty),
|
||||
|
@ -182,7 +180,7 @@ public class ContainerMocker(string key) : ContainerServiceBase(key)
|
|||
|
||||
public bool ReturnContainerRemoved { get; set; }
|
||||
|
||||
public Collection<byte[]> ContainerIds { get; } = [];
|
||||
public List<byte[]> ContainerIds { get; set; } = [];
|
||||
|
||||
public Collection<RequestData<DeleteRequest>> Requests { get; } = [];
|
||||
public List<RequestData<DeleteRequest>> Requests { get; set; } = [];
|
||||
}
|
||||
|
|
|
@ -25,17 +25,18 @@ public class NetworkMocker(string key) : ServiceBase(key)
|
|||
"MaintenanceModeAllowed"
|
||||
];
|
||||
|
||||
public Dictionary<string, byte[]> Parameters { get; } = [];
|
||||
public Dictionary<string, byte[]>? Parameters { get; set; }
|
||||
|
||||
public LocalNodeInfoResponse? NodeInfoResponse { get; set; }
|
||||
public LocalNodeInfoResponse NodeInfoResponse { get; set; }
|
||||
|
||||
public LocalNodeInfoRequest? LocalNodeInfoRequest { get; set; }
|
||||
public LocalNodeInfoRequest LocalNodeInfoRequest { get; set; }
|
||||
|
||||
public NetworkInfoRequest? NetworkInfoRequest { get; set; }
|
||||
public NetworkInfoRequest NetworkInfoRequest { get; set; }
|
||||
|
||||
public NetmapSnapshotResponse? NetmapSnapshotResponse { get; set; }
|
||||
public NetmapSnapshotResponse NetmapSnapshotResponse { get; set; }
|
||||
|
||||
public NetmapSnapshotRequest NetmapSnapshotRequest { get; set; }
|
||||
|
||||
public NetmapSnapshotRequest? NetmapSnapshotRequest { get; set; }
|
||||
|
||||
public Mock<NetmapService.NetmapServiceClient> GetMock()
|
||||
{
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.Object;
|
||||
|
@ -93,7 +92,7 @@ public class ObjectMocker(string key) : ObjectServiceBase(key)
|
|||
|
||||
}
|
||||
|
||||
if (ResultObjectIds != null && ResultObjectIds.Count > 0)
|
||||
if (ResultObjectIds != null)
|
||||
{
|
||||
PutResponse putResponse = new()
|
||||
{
|
||||
|
@ -198,14 +197,14 @@ public class ObjectMocker(string key) : ObjectServiceBase(key)
|
|||
|
||||
public Header? HeadResponse { get; set; }
|
||||
|
||||
public Collection<byte[]>? ResultObjectIds { get; } = [];
|
||||
public List<byte[]>? ResultObjectIds { get; set; }
|
||||
|
||||
public ClientStreamWriter? ClientStreamWriter { get; private set; } = new();
|
||||
|
||||
public Collection<PutSingleRequest> PutSingleRequests { get; private set; } = [];
|
||||
public List<PutSingleRequest> PutSingleRequests { get; private set; } = [];
|
||||
|
||||
public Collection<DeleteRequest> DeleteRequests { get; private set; } = [];
|
||||
public List<DeleteRequest> DeleteRequests { get; private set; } = [];
|
||||
|
||||
public Collection<HeadRequest> HeadRequests { get; private set; } = [];
|
||||
public List<HeadRequest> HeadRequests { get; private set; } = [];
|
||||
}
|
||||
|
||||
|
|
|
@ -8,14 +8,13 @@ using Moq;
|
|||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "No secure purpose")]
|
||||
public class SessionMocker(string key) : ServiceBase(key)
|
||||
{
|
||||
public byte[]? SessionId { get; set; }
|
||||
|
||||
public byte[]? SessionKey { get; set; }
|
||||
|
||||
public CreateRequest? CreateSessionRequest { get; private set; }
|
||||
public CreateRequest CreateSessionRequest { get; private set; }
|
||||
|
||||
public Mock<SessionService.SessionServiceClient> GetMock()
|
||||
{
|
||||
|
@ -25,7 +24,7 @@ public class SessionMocker(string key) : ServiceBase(key)
|
|||
|
||||
if (SessionId == null)
|
||||
{
|
||||
SessionId = new byte[16];
|
||||
SessionId = new byte[32];
|
||||
rand.NextBytes(SessionId);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,55 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.Netmap;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
|
||||
public abstract class NetworkTestsBase
|
||||
{
|
||||
protected readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
|
||||
protected FrostFsVersion Version { get; set; } = new FrostFsVersion(2, 13);
|
||||
|
||||
protected ECDsa ECDsaKey { get; set; }
|
||||
protected FrostFsOwner OwnerId { get; set; }
|
||||
protected NetworkMocker Mocker { get; set; }
|
||||
|
||||
protected NetworkTestsBase()
|
||||
{
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
ECDsaKey = key.LoadWif();
|
||||
OwnerId = FrostFsOwner.FromKey(ECDsaKey);
|
||||
|
||||
Mocker = new NetworkMocker(this.key);
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return ClientV2.FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
Mocker.GetMock().Object,
|
||||
new SessionMocker(this.key).GetMock().Object,
|
||||
new ContainerMocker(this.key).GetMock().Object,
|
||||
new ObjectMocker(this.key).GetMock().Object);
|
||||
}
|
||||
}
|
||||
|
||||
public class NetworkTest : NetworkTestsBase
|
||||
{
|
||||
[Theory]
|
||||
|
@ -15,29 +57,35 @@ public class NetworkTest : NetworkTestsBase
|
|||
[InlineData(true)]
|
||||
public async void NetworkSettingsTest(bool useContext)
|
||||
{
|
||||
Mocker.Parameters.Add("AuditFee", [1]);
|
||||
Mocker.Parameters.Add("BasicIncomeRate", [2]);
|
||||
Mocker.Parameters.Add("ContainerFee", [3]);
|
||||
Mocker.Parameters.Add("ContainerAliasFee", [4]);
|
||||
Mocker.Parameters.Add("EpochDuration", [5]);
|
||||
Mocker.Parameters.Add("InnerRingCandidateFee", [6]);
|
||||
Mocker.Parameters.Add("MaxECDataCount", [7]);
|
||||
Mocker.Parameters.Add("MaxECParityCount", [8]);
|
||||
Mocker.Parameters.Add("MaxObjectSize", [9]);
|
||||
Mocker.Parameters.Add("WithdrawFee", [10]);
|
||||
Mocker.Parameters.Add("HomomorphicHashingDisabled", [1]);
|
||||
Mocker.Parameters.Add("MaintenanceModeAllowed", [1]);
|
||||
Mocker.Parameters = new Dictionary<string, byte[]>
|
||||
{
|
||||
{ "AuditFee", [1] },
|
||||
{ "BasicIncomeRate", [2] },
|
||||
{ "ContainerFee", [3] },
|
||||
{ "ContainerAliasFee", [4] },
|
||||
{ "EpochDuration", [5] },
|
||||
{ "InnerRingCandidateFee", [6] },
|
||||
{ "MaxECDataCount", [7] },
|
||||
{ "MaxECParityCount", [8] },
|
||||
{ "MaxObjectSize", [9] },
|
||||
{ "WithdrawFee", [10] },
|
||||
{ "HomomorphicHashingDisabled", [1] },
|
||||
{ "MaintenanceModeAllowed", [1] },
|
||||
};
|
||||
|
||||
var param = useContext ?
|
||||
new PrmNetworkSettings(new CallContext
|
||||
var param = new PrmNetworkSettings();
|
||||
|
||||
if (useContext)
|
||||
{
|
||||
param.Context = new Context
|
||||
{
|
||||
CancellationToken = Mocker.CancellationTokenSource.Token,
|
||||
Timeout = TimeSpan.FromSeconds(20),
|
||||
OwnerId = OwnerId,
|
||||
Key = ECDsaKey,
|
||||
Version = Version
|
||||
})
|
||||
: new PrmNetworkSettings();
|
||||
};
|
||||
}
|
||||
|
||||
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
|
||||
|
||||
|
@ -71,7 +119,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
}
|
||||
else
|
||||
{
|
||||
Assert.NotNull(Mocker.NetworkInfoRequest);
|
||||
Assert.Empty(Mocker.NetworkInfoRequest.MetaHeader.XHeaders);
|
||||
Assert.Null(Mocker.DateTime);
|
||||
}
|
||||
|
@ -80,7 +127,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
|
||||
public async void NetmapSnapshotTest(bool useContext)
|
||||
{
|
||||
var body = new NetmapSnapshotResponse.Types.Body
|
||||
|
@ -113,11 +159,12 @@ public class NetworkTest : NetworkTestsBase
|
|||
|
||||
Mocker.NetmapSnapshotResponse = new NetmapSnapshotResponse { Body = body };
|
||||
|
||||
PrmNetmapSnapshot param;
|
||||
var param = new PrmNetmapSnapshot();
|
||||
|
||||
if (useContext)
|
||||
{
|
||||
var ctx = new CallContext
|
||||
param.XHeaders.Add("headerKey1", "headerValue1");
|
||||
param.Context = new Context
|
||||
{
|
||||
CancellationToken = Mocker.CancellationTokenSource.Token,
|
||||
Timeout = TimeSpan.FromSeconds(20),
|
||||
|
@ -125,14 +172,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
Key = ECDsaKey,
|
||||
Version = Version
|
||||
};
|
||||
|
||||
param = new(ctx);
|
||||
param.XHeaders.Add("headerKey1", "headerValue1");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
param = new();
|
||||
}
|
||||
|
||||
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
|
||||
|
@ -171,7 +210,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
|
||||
if (useContext)
|
||||
{
|
||||
Assert.NotNull(Mocker.NetmapSnapshotRequest);
|
||||
Assert.Single(Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders);
|
||||
Assert.Equal(param.XHeaders.Keys[0], Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders.First().Key);
|
||||
Assert.Equal(param.XHeaders[param.XHeaders.Keys[0]], Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders.First().Value);
|
||||
|
@ -184,7 +222,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
}
|
||||
else
|
||||
{
|
||||
Assert.NotNull(Mocker.NetmapSnapshotRequest);
|
||||
Assert.Empty(Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders);
|
||||
Assert.Null(Mocker.DateTime);
|
||||
}
|
||||
|
@ -212,11 +249,12 @@ public class NetworkTest : NetworkTestsBase
|
|||
|
||||
Mocker.NodeInfoResponse = new LocalNodeInfoResponse { Body = body };
|
||||
|
||||
PrmNodeInfo param;
|
||||
var param = new PrmNodeInfo();
|
||||
|
||||
if (useContext)
|
||||
{
|
||||
var ctx = new CallContext
|
||||
param.XHeaders.Add("headerKey1", "headerValue1");
|
||||
param.Context = new Context
|
||||
{
|
||||
CancellationToken = Mocker.CancellationTokenSource.Token,
|
||||
Timeout = TimeSpan.FromSeconds(20),
|
||||
|
@ -224,14 +262,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
Key = ECDsaKey,
|
||||
Version = Version
|
||||
};
|
||||
|
||||
param = new(ctx);
|
||||
param.XHeaders.Add("headerKey1", "headerValue1");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
param = new();
|
||||
}
|
||||
|
||||
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
|
||||
|
@ -252,7 +282,6 @@ public class NetworkTest : NetworkTestsBase
|
|||
Assert.Equal("value1", result.Attributes["key1"]);
|
||||
Assert.Equal("value2", result.Attributes["key2"]);
|
||||
|
||||
Assert.NotNull(Mocker.LocalNodeInfoRequest);
|
||||
if (useContext)
|
||||
{
|
||||
Assert.Single(Mocker.LocalNodeInfoRequest.MetaHeader.XHeaders);
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
|
||||
public abstract class NetworkTestsBase
|
||||
{
|
||||
internal readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
|
||||
protected FrostFsVersion Version { get; set; } = new FrostFsVersion(2, 13);
|
||||
|
||||
protected ECDsa ECDsaKey { get; set; }
|
||||
protected FrostFsOwner OwnerId { get; set; }
|
||||
protected NetworkMocker Mocker { get; set; }
|
||||
|
||||
protected NetworkTestsBase()
|
||||
{
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
ECDsaKey = key.LoadWif();
|
||||
OwnerId = FrostFsOwner.FromKey(ECDsaKey);
|
||||
|
||||
Mocker = new NetworkMocker(this.key);
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return ClientV2.FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
Mocker.GetMock().Object,
|
||||
new SessionMocker(this.key).GetMock().Object,
|
||||
new ContainerMocker(this.key).GetMock().Object,
|
||||
new ObjectMocker(this.key).GetMock().Object);
|
||||
}
|
||||
}
|
|
@ -1,19 +1,71 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
using FrostFS.Refs;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.ClientV2.Mappers.GRPC;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
|
||||
[SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "No secure purpose")]
|
||||
public abstract class ObjectTestsBase
|
||||
{
|
||||
protected static readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
protected FrostFsContainerId ContainerId { get; set; }
|
||||
|
||||
protected NetworkMocker NetworkMocker { get; set; } = new NetworkMocker(key);
|
||||
protected SessionMocker SessionMocker { get; set; } = new SessionMocker(key);
|
||||
protected ContainerMocker ContainerMocker { get; set; } = new ContainerMocker(key);
|
||||
protected ObjectMocker Mocker { get; set; }
|
||||
|
||||
protected ObjectTestsBase()
|
||||
{
|
||||
var ecdsaKey = key.LoadWif();
|
||||
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
Mocker = new ObjectMocker(key)
|
||||
{
|
||||
PlacementPolicy = new FrostFsPlacementPolicy(true, new FrostFsReplica(1)),
|
||||
Version = new FrostFsVersion(2, 13),
|
||||
ContainerGuid = Guid.NewGuid()
|
||||
};
|
||||
|
||||
ContainerId = new FrostFsContainerId(Base58.Encode(Mocker.ContainerGuid.ToBytes()));
|
||||
|
||||
Mocker.ObjectHeader = new(
|
||||
ContainerId,
|
||||
FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("k", "v")],
|
||||
null,
|
||||
FrostFsOwner.FromKey(ecdsaKey),
|
||||
new FrostFsVersion(2, 13));
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
NetworkMocker.GetMock().Object,
|
||||
SessionMocker.GetMock().Object,
|
||||
ContainerMocker.GetMock().Object,
|
||||
Mocker.GetMock().Object);
|
||||
}
|
||||
}
|
||||
|
||||
public class ObjectTest : ObjectTestsBase
|
||||
{
|
||||
[Fact]
|
||||
|
@ -23,7 +75,7 @@ public class ObjectTest : ObjectTestsBase
|
|||
|
||||
var ecdsaKey = key.LoadWif();
|
||||
|
||||
var ctx = new CallContext
|
||||
var ctx = new Context
|
||||
{
|
||||
Key = ecdsaKey,
|
||||
OwnerId = FrostFsOwner.FromKey(ecdsaKey),
|
||||
|
@ -32,25 +84,22 @@ public class ObjectTest : ObjectTestsBase
|
|||
|
||||
var objectId = client.CalculateObjectId(Mocker.ObjectHeader!, ctx);
|
||||
|
||||
var result = await client.GetObjectAsync(new PrmObjectGet(ContainerId, objectId, ctx));
|
||||
var result = await client.GetObjectAsync(new PrmObjectGet(ContainerId, objectId) { Context = ctx });
|
||||
|
||||
Assert.NotNull(result);
|
||||
|
||||
Assert.NotNull(Mocker.ObjectHeader);
|
||||
|
||||
Assert.Equal(Mocker.ObjectHeader.ContainerId.GetValue(), result.Header.ContainerId.GetValue());
|
||||
Assert.Equal(Mocker.ObjectHeader.OwnerId!.Value, result.Header.OwnerId!.Value);
|
||||
Assert.Equal(Mocker.ObjectHeader!.ContainerId.GetValue(), result.Header.ContainerId.GetValue());
|
||||
Assert.Equal(Mocker.ObjectHeader!.OwnerId!.Value, result.Header.OwnerId!.Value);
|
||||
Assert.Equal(Mocker.ObjectHeader.PayloadLength, result.Header.PayloadLength);
|
||||
Assert.NotNull(result.Header.Attributes);
|
||||
Assert.Single(result.Header.Attributes);
|
||||
Assert.Equal(Mocker.ObjectHeader.Attributes![0].Key, result.Header.Attributes[0].Key);
|
||||
Assert.Equal(Mocker.ObjectHeader.Attributes![0].Value, result.Header.Attributes[0].Value);
|
||||
Assert.Equal(Mocker.ObjectHeader.Attributes[0].Key, result.Header.Attributes[0].Key);
|
||||
Assert.Equal(Mocker.ObjectHeader.Attributes[0].Value, result.Header.Attributes[0].Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void PutObjectTest()
|
||||
{
|
||||
Mocker.ResultObjectIds!.Add(SHA256.HashData([]));
|
||||
Mocker.ResultObjectIds = new([SHA256.HashData([])]);
|
||||
|
||||
Random rnd = new();
|
||||
var bytes = new byte[1024];
|
||||
|
@ -85,7 +134,7 @@ public class ObjectTest : ObjectTestsBase
|
|||
[Fact]
|
||||
public async void ClientCutTest()
|
||||
{
|
||||
NetworkMocker.Parameters.Add("MaxObjectSize", [0x0, 0xa]);
|
||||
NetworkMocker.Parameters = new Dictionary<string, byte[]>() { { "MaxObjectSize", [0x0, 0xa] } };
|
||||
|
||||
var blockSize = 2560;
|
||||
byte[] bytes = File.ReadAllBytes(@".\..\..\..\TestData\cat.jpg");
|
||||
|
@ -101,19 +150,17 @@ public class ObjectTest : ObjectTestsBase
|
|||
|
||||
Random rnd = new();
|
||||
|
||||
Collection<byte[]> objIds = new([new byte[32], new byte[32], new byte[32]]);
|
||||
List<byte[]> objIds = new([new byte[32], new byte[32], new byte[32]]);
|
||||
rnd.NextBytes(objIds.ElementAt(0));
|
||||
rnd.NextBytes(objIds.ElementAt(1));
|
||||
rnd.NextBytes(objIds.ElementAt(2));
|
||||
|
||||
foreach (var objId in objIds)
|
||||
Mocker.ResultObjectIds!.Add(objId);
|
||||
Mocker.ResultObjectIds = objIds;
|
||||
|
||||
var result = await GetClient().PutObjectAsync(param);
|
||||
|
||||
var singleObjects = Mocker.PutSingleRequests.ToArray();
|
||||
|
||||
Assert.NotNull(Mocker.ClientStreamWriter?.Messages);
|
||||
var streamObjects = Mocker.ClientStreamWriter.Messages.ToArray();
|
||||
|
||||
Assert.Single(singleObjects);
|
||||
|
@ -215,7 +262,6 @@ public class ObjectTest : ObjectTestsBase
|
|||
|
||||
Assert.Equal(FrostFsObjectType.Regular, response.ObjectType);
|
||||
|
||||
Assert.NotNull(response.Attributes);
|
||||
Assert.Single(response.Attributes);
|
||||
|
||||
Assert.Equal(Mocker.HeadResponse.Attributes[0].Key, response.Attributes.First().Key);
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
public abstract class ObjectTestsBase
|
||||
{
|
||||
protected static readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
protected FrostFsContainerId ContainerId { get; set; }
|
||||
|
||||
protected NetworkMocker NetworkMocker { get; set; } = new NetworkMocker(key);
|
||||
protected SessionMocker SessionMocker { get; set; } = new SessionMocker(key);
|
||||
protected ContainerMocker ContainerMocker { get; set; } = new ContainerMocker(key);
|
||||
protected ObjectMocker Mocker { get; set; }
|
||||
|
||||
protected ObjectTestsBase()
|
||||
{
|
||||
var ecdsaKey = key.LoadWif();
|
||||
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
Mocker = new ObjectMocker(key)
|
||||
{
|
||||
PlacementPolicy = new FrostFsPlacementPolicy(true, new FrostFsReplica(1)),
|
||||
Version = new FrostFsVersion(2, 13),
|
||||
ContainerGuid = Guid.NewGuid()
|
||||
};
|
||||
|
||||
ContainerId = new FrostFsContainerId(Base58.Encode(Mocker.ContainerGuid.ToBytes()));
|
||||
|
||||
Mocker.ObjectHeader = new(
|
||||
ContainerId,
|
||||
FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("k", "v")],
|
||||
null,
|
||||
FrostFsOwner.FromKey(ecdsaKey),
|
||||
new FrostFsVersion(2, 13));
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
NetworkMocker.GetMock().Object,
|
||||
SessionMocker.GetMock().Object,
|
||||
ContainerMocker.GetMock().Object,
|
||||
Mocker.GetMock().Object);
|
||||
}
|
||||
}
|
|
@ -1,612 +0,0 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using static FrostFS.Session.SessionToken.Types.Body;
|
||||
|
||||
namespace FrostFS.SDK.SmokeTests;
|
||||
|
||||
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
|
||||
[SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "No secure purpose")]
|
||||
public class PoolSmokeTests : SmokeTestsBase
|
||||
{
|
||||
private static readonly PrmWait lightWait = new(100, 1);
|
||||
|
||||
private InitParameters GetDefaultParams()
|
||||
{
|
||||
return new InitParameters
|
||||
{
|
||||
Key = keyString.LoadWif(),
|
||||
|
||||
NodeParams = [new(1, this.url, 100.0f)],
|
||||
ClientBuilder = null,
|
||||
GracefulCloseOnSwitchTimeout = 30_000_000,
|
||||
Logger = null
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NetworkMapTest()
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext());
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
var result = await pool.GetNetmapSnapshotAsync(default);
|
||||
|
||||
Assert.True(result.Epoch > 0);
|
||||
Assert.Single(result.NodeInfoCollection);
|
||||
|
||||
var item = result.NodeInfoCollection[0];
|
||||
Assert.Equal(2, item.Version.Major);
|
||||
Assert.Equal(13, item.Version.Minor);
|
||||
Assert.Equal(NodeState.Online, item.State);
|
||||
Assert.True(item.PublicKey.Length > 0);
|
||||
Assert.Single(item.Addresses);
|
||||
Assert.Equal(9, item.Attributes.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NodeInfoTest()
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext());
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
var result = await pool.GetNodeInfoAsync();
|
||||
|
||||
Assert.Equal(2, result.Version.Major);
|
||||
Assert.Equal(13, result.Version.Minor);
|
||||
Assert.Equal(NodeState.Online, result.State);
|
||||
Assert.Equal(33, result.PublicKey.Length);
|
||||
Assert.Single(result.Addresses);
|
||||
Assert.Equal(9, result.Attributes.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NodeInfoStatisticsTwoNodesTest()
|
||||
{
|
||||
var options = new InitParameters
|
||||
{
|
||||
Key = keyString.LoadWif(),
|
||||
NodeParams = [
|
||||
new(1, this.url, 100.0f),
|
||||
new(2, this.url.Replace('0', '1'), 100.0f)
|
||||
],
|
||||
ClientBuilder = null,
|
||||
GracefulCloseOnSwitchTimeout = 30_000_000,
|
||||
Logger = null
|
||||
};
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var callbackText = string.Empty;
|
||||
|
||||
var ctx = new CallContext
|
||||
{
|
||||
Callback = (cs) => callbackText = $"{cs.MethodName} took {cs.ElapsedMicroSeconds} microseconds"
|
||||
};
|
||||
|
||||
var error = await pool.Dial(ctx).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
using var client = FrostFSClient.GetSingleOwnerInstance(GetSingleOwnerOptions(this.keyString, this.url));
|
||||
|
||||
var result = await client.GetNodeInfoAsync();
|
||||
|
||||
var statistics = pool.Statistic();
|
||||
|
||||
Assert.False(string.IsNullOrEmpty(callbackText));
|
||||
Assert.Contains(" took ", callbackText, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void NodeInfoStatisticsTest()
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var callbackText = string.Empty;
|
||||
|
||||
var ctx = new CallContext
|
||||
{
|
||||
Callback = (cs) => callbackText = $"{cs.MethodName} took {cs.ElapsedMicroSeconds} microseconds"
|
||||
};
|
||||
|
||||
var error = await pool.Dial(ctx).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
using var client = FrostFSClient.GetSingleOwnerInstance(GetSingleOwnerOptions(this.keyString, this.url));
|
||||
|
||||
var result = await client.GetNodeInfoAsync();
|
||||
|
||||
Assert.False(string.IsNullOrEmpty(callbackText));
|
||||
Assert.Contains(" took ", callbackText, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetSessionTest()
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext()).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
var prm = new PrmSessionCreate(100);
|
||||
|
||||
var token = await pool.CreateSessionAsync(prm).ConfigureAwait(true);
|
||||
|
||||
var session = new Session.SessionToken().Deserialize(token.Token);
|
||||
|
||||
var ownerHash = Base58.Decode(OwnerId!.Value);
|
||||
|
||||
Assert.NotNull(session);
|
||||
Assert.Null(session.Body.Container);
|
||||
Assert.Null(session.Body.Object);
|
||||
Assert.Equal(16, session.Body.Id.Length);
|
||||
Assert.Equal(100ul, session.Body.Lifetime.Exp);
|
||||
Assert.Equal(ownerHash, session.Body.OwnerId.Value);
|
||||
Assert.Equal(33, session.Body.SessionKey.Length);
|
||||
Assert.Equal(ContextOneofCase.None, session.Body.ContextCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void CreateObjectWithSessionToken()
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext()).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
var token = await pool.CreateSessionAsync(new PrmSessionCreate(int.MaxValue));
|
||||
|
||||
var createContainerParam = new PrmContainerCreate(
|
||||
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))));
|
||||
|
||||
createContainerParam.XHeaders.Add("key1", "value1");
|
||||
|
||||
var containerId = await pool.CreateContainerAsync(createContainerParam);
|
||||
|
||||
var bytes = GetRandomBytes(1024);
|
||||
|
||||
var param = new PrmObjectPut
|
||||
{
|
||||
Header = new FrostFsObjectHeader(
|
||||
containerId: containerId,
|
||||
type: FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("fileName", "test")]),
|
||||
Payload = new MemoryStream(bytes),
|
||||
ClientCut = false,
|
||||
SessionToken = token
|
||||
};
|
||||
|
||||
var objectId = await pool.PutObjectAsync(param).ConfigureAwait(true);
|
||||
|
||||
var @object = await pool.GetObjectAsync(new PrmObjectGet(containerId, objectId));
|
||||
|
||||
var downloadedBytes = new byte[@object.Header.PayloadLength];
|
||||
MemoryStream ms = new(downloadedBytes);
|
||||
|
||||
ReadOnlyMemory<byte>? chunk = null;
|
||||
while ((chunk = await @object.ObjectReader!.ReadChunk()) != null)
|
||||
{
|
||||
ms.Write(chunk.Value.Span);
|
||||
}
|
||||
|
||||
Assert.Equal(SHA256.HashData(bytes), SHA256.HashData(downloadedBytes));
|
||||
|
||||
await Cleanup(pool);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void FilterTest()
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext()).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
var createContainerParam = new PrmContainerCreate(
|
||||
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))))
|
||||
{
|
||||
WaitParams = lightWait
|
||||
};
|
||||
|
||||
var containerId = await pool.CreateContainerAsync(createContainerParam);
|
||||
|
||||
var bytes = new byte[] { 1, 2, 3 };
|
||||
|
||||
var ParentHeader = new FrostFsObjectHeader(
|
||||
containerId: containerId,
|
||||
type: FrostFsObjectType.Regular)
|
||||
{
|
||||
PayloadLength = 3
|
||||
};
|
||||
|
||||
var param = new PrmObjectPut
|
||||
{
|
||||
Header = new FrostFsObjectHeader(
|
||||
containerId: containerId,
|
||||
type: FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("fileName", "test")],
|
||||
new FrostFsSplit()),
|
||||
Payload = new MemoryStream(bytes),
|
||||
ClientCut = false
|
||||
};
|
||||
|
||||
var objectId = await pool.PutObjectAsync(param);
|
||||
|
||||
var head = await pool.GetObjectHeadAsync(new PrmObjectHeadGet(containerId, objectId));
|
||||
|
||||
var ecdsaKey = this.keyString.LoadWif();
|
||||
|
||||
var networkInfo = await pool.GetNetmapSnapshotAsync();
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByContainerId(FrostFsMatchType.Equals, containerId));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByOwnerId(FrostFsMatchType.Equals, FrostFsOwner.FromKey(ecdsaKey)));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterBySplitId(FrostFsMatchType.Equals, param.Header.Split!.SplitId));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByAttributePair(FrostFsMatchType.Equals, "fileName", "test"));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByObjectId(FrostFsMatchType.Equals, objectId));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByVersion(FrostFsMatchType.Equals, networkInfo.NodeInfoCollection[0].Version));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByEpoch(FrostFsMatchType.Equals, networkInfo.Epoch));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByPayloadLength(FrostFsMatchType.Equals, 3));
|
||||
|
||||
var checkSum = CheckSum.CreateCheckSum(bytes);
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByPayloadHash(FrostFsMatchType.Equals, checkSum));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByPhysicallyStored());
|
||||
}
|
||||
|
||||
private static async Task CheckFilter(Pool pool, FrostFsContainerId containerId, IObjectFilter filter)
|
||||
{
|
||||
var resultObjectsCount = 0;
|
||||
|
||||
PrmObjectSearch searchParam = new(containerId) { Filters = [filter] };
|
||||
|
||||
await foreach (var objId in pool.SearchObjectsAsync(searchParam))
|
||||
{
|
||||
resultObjectsCount++;
|
||||
var objHeader = await pool.GetObjectHeadAsync(new PrmObjectHeadGet(containerId, objId));
|
||||
}
|
||||
|
||||
Assert.True(0 < resultObjectsCount, $"Filter for {filter.Key} doesn't work");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(3 * 1024 * 1024)] // exactly one chunk size - 3MB
|
||||
[InlineData(6 * 1024 * 1024 + 100)]
|
||||
public async void SimpleScenarioTest(int objectSize)
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext()).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
bool callbackInvoked = false;
|
||||
var ctx = new CallContext
|
||||
{
|
||||
// Timeout = TimeSpan.FromSeconds(20),
|
||||
Callback = new((CallStatistics cs) =>
|
||||
{
|
||||
callbackInvoked = true;
|
||||
Assert.True(cs.ElapsedMicroSeconds > 0);
|
||||
})
|
||||
};
|
||||
|
||||
var createContainerParam = new PrmContainerCreate(
|
||||
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1)), [new("testKey", "testValue")]), ctx);
|
||||
|
||||
var createdContainer = await pool.CreateContainerAsync(createContainerParam);
|
||||
|
||||
var container = await pool.GetContainerAsync(new PrmContainerGet(createdContainer));
|
||||
Assert.NotNull(container);
|
||||
Assert.True(callbackInvoked);
|
||||
|
||||
var bytes = GetRandomBytes(objectSize);
|
||||
|
||||
var ctx1 = new CallContext
|
||||
{
|
||||
Callback = new((CallStatistics cs) => Assert.True(cs.ElapsedMicroSeconds > 0))
|
||||
};
|
||||
|
||||
var param = new PrmObjectPut(ctx1)
|
||||
{
|
||||
Header = new FrostFsObjectHeader(
|
||||
containerId: createdContainer,
|
||||
type: FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("fileName", "test")]),
|
||||
Payload = new MemoryStream(bytes),
|
||||
ClientCut = false
|
||||
};
|
||||
|
||||
var objectId = await pool.PutObjectAsync(param);
|
||||
|
||||
var filter = new FilterByAttributePair(FrostFsMatchType.Equals, "fileName", "test");
|
||||
|
||||
bool hasObject = false;
|
||||
await foreach (var objId in pool.SearchObjectsAsync(new PrmObjectSearch(createdContainer) { Filters = [filter] }))
|
||||
{
|
||||
hasObject = true;
|
||||
|
||||
var objHeader = await pool.GetObjectHeadAsync(new PrmObjectHeadGet(createdContainer, objectId));
|
||||
Assert.Equal((ulong)bytes.Length, objHeader.PayloadLength);
|
||||
Assert.NotNull(objHeader.Attributes);
|
||||
Assert.Single(objHeader.Attributes!);
|
||||
Assert.Equal("fileName", objHeader.Attributes!.First().Key);
|
||||
Assert.Equal("test", objHeader.Attributes!.First().Value);
|
||||
}
|
||||
|
||||
Assert.True(hasObject);
|
||||
|
||||
var @object = await pool.GetObjectAsync(new PrmObjectGet(createdContainer, objectId));
|
||||
|
||||
var downloadedBytes = new byte[@object.Header.PayloadLength];
|
||||
MemoryStream ms = new(downloadedBytes);
|
||||
|
||||
ReadOnlyMemory<byte>? chunk = null;
|
||||
while ((chunk = await @object.ObjectReader!.ReadChunk()) != null)
|
||||
{
|
||||
ms.Write(chunk.Value.Span);
|
||||
}
|
||||
|
||||
Assert.Equal(SHA256.HashData(bytes), SHA256.HashData(downloadedBytes));
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
await foreach (var _ in pool.ListContainersAsync())
|
||||
{
|
||||
Assert.Fail("Containers exist");
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(3 * 1024 * 1024)] // exactly one chunk size - 3MB
|
||||
[InlineData(6 * 1024 * 1024 + 100)]
|
||||
public async void SimpleScenarioWithSessionTest(int objectSize)
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext()).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
var token = await pool.CreateSessionAsync(new PrmSessionCreate(int.MaxValue));
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
var ctx = new CallContext
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(20),
|
||||
Callback = new((CallStatistics cs) => Assert.True(cs.ElapsedMicroSeconds > 0))
|
||||
};
|
||||
|
||||
var createContainerParam = new PrmContainerCreate(
|
||||
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))), ctx);
|
||||
|
||||
var container = await pool.CreateContainerAsync(createContainerParam);
|
||||
|
||||
var containerInfo = await pool.GetContainerAsync(new PrmContainerGet(container, ctx));
|
||||
Assert.NotNull(containerInfo);
|
||||
|
||||
var bytes = GetRandomBytes(objectSize);
|
||||
|
||||
var param = new PrmObjectPut(new CallContext { Callback = new((CallStatistics cs) => Assert.True(cs.ElapsedMicroSeconds > 0)) })
|
||||
{
|
||||
Header = new FrostFsObjectHeader(
|
||||
containerId: container,
|
||||
type: FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("fileName", "test")]),
|
||||
Payload = new MemoryStream(bytes),
|
||||
ClientCut = false,
|
||||
SessionToken = token
|
||||
};
|
||||
|
||||
var objectId = await pool.PutObjectAsync(param);
|
||||
|
||||
var filter = new FilterByAttributePair(FrostFsMatchType.Equals, "fileName", "test");
|
||||
|
||||
bool hasObject = false;
|
||||
await foreach (var objId in pool.SearchObjectsAsync(new PrmObjectSearch(container) { Filters = [filter], SessionToken = token }))
|
||||
{
|
||||
hasObject = true;
|
||||
|
||||
var objHeader = await pool.GetObjectHeadAsync(new PrmObjectHeadGet(container, objectId) { SessionToken = token });
|
||||
Assert.Equal((ulong)bytes.Length, objHeader.PayloadLength);
|
||||
Assert.NotNull(objHeader.Attributes);
|
||||
Assert.Single(objHeader.Attributes);
|
||||
Assert.Equal("fileName", objHeader.Attributes.First().Key);
|
||||
Assert.Equal("test", objHeader.Attributes.First().Value);
|
||||
}
|
||||
|
||||
Assert.True(hasObject);
|
||||
|
||||
var @object = await pool.GetObjectAsync(new PrmObjectGet(container, objectId) { SessionToken = token });
|
||||
|
||||
var downloadedBytes = new byte[@object.Header.PayloadLength];
|
||||
MemoryStream ms = new(downloadedBytes);
|
||||
|
||||
ReadOnlyMemory<byte>? chunk = null;
|
||||
while ((chunk = await @object.ObjectReader!.ReadChunk()) != null)
|
||||
{
|
||||
ms.Write(chunk.Value.Span);
|
||||
}
|
||||
|
||||
Assert.Equal(SHA256.HashData(bytes), SHA256.HashData(downloadedBytes));
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
await foreach (var _ in pool.ListContainersAsync())
|
||||
{
|
||||
Assert.Fail("Containers exist");
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(64 * 1024 * 1024)] // exactly 1 block size - 64MB
|
||||
[InlineData(64 * 1024 * 1024 - 1)]
|
||||
[InlineData(64 * 1024 * 1024 + 1)]
|
||||
[InlineData(2 * 64 * 1024 * 1024 + 256)]
|
||||
[InlineData(200)]
|
||||
public async void ClientCutScenarioTest(int objectSize)
|
||||
{
|
||||
var options = GetDefaultParams();
|
||||
|
||||
using var pool = new Pool(options);
|
||||
|
||||
var error = await pool.Dial(new CallContext()).ConfigureAwait(true);
|
||||
|
||||
Assert.Null(error);
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
var createContainerParam = new PrmContainerCreate(new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))))
|
||||
{
|
||||
WaitParams = lightWait
|
||||
};
|
||||
|
||||
var containerId = await pool.CreateContainerAsync(createContainerParam);
|
||||
|
||||
var ctx = new CallContext
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(10),
|
||||
};
|
||||
|
||||
ctx.Interceptors.Add(new CallbackInterceptor());
|
||||
|
||||
var container = await pool.GetContainerAsync(new PrmContainerGet(containerId, ctx));
|
||||
|
||||
Assert.NotNull(container);
|
||||
|
||||
byte[] bytes = GetRandomBytes(objectSize);
|
||||
|
||||
var param = new PrmObjectPut
|
||||
{
|
||||
Header = new FrostFsObjectHeader(
|
||||
containerId: containerId,
|
||||
type: FrostFsObjectType.Regular,
|
||||
[new FrostFsAttributePair("fileName", "test")]),
|
||||
Payload = new MemoryStream(bytes),
|
||||
ClientCut = true
|
||||
};
|
||||
|
||||
var objectId = await pool.PutObjectAsync(param);
|
||||
|
||||
var filter = new FilterByAttributePair(FrostFsMatchType.Equals, "fileName", "test");
|
||||
|
||||
bool hasObject = false;
|
||||
await foreach (var objId in pool.SearchObjectsAsync(new PrmObjectSearch(containerId, null, filter)))
|
||||
{
|
||||
hasObject = true;
|
||||
|
||||
var objHeader = await pool.GetObjectHeadAsync(new PrmObjectHeadGet(containerId, objectId));
|
||||
Assert.Equal((ulong)bytes.Length, objHeader.PayloadLength);
|
||||
Assert.NotNull(objHeader.Attributes);
|
||||
Assert.Single(objHeader.Attributes);
|
||||
Assert.Equal("fileName", objHeader.Attributes[0].Key);
|
||||
Assert.Equal("test", objHeader.Attributes[0].Value);
|
||||
}
|
||||
|
||||
Assert.True(hasObject);
|
||||
|
||||
var @object = await pool.GetObjectAsync(new PrmObjectGet(containerId, objectId));
|
||||
|
||||
var downloadedBytes = new byte[@object.Header.PayloadLength];
|
||||
MemoryStream ms = new(downloadedBytes);
|
||||
|
||||
ReadOnlyMemory<byte>? chunk = null;
|
||||
while ((chunk = await @object.ObjectReader!.ReadChunk()) != null)
|
||||
{
|
||||
ms.Write(chunk.Value.Span);
|
||||
}
|
||||
|
||||
Assert.Equal(SHA256.HashData(bytes), SHA256.HashData(downloadedBytes));
|
||||
|
||||
await CheckFilter(pool, containerId, new FilterByRootObject());
|
||||
|
||||
await Cleanup(pool);
|
||||
|
||||
await foreach (var cid in pool.ListContainersAsync())
|
||||
{
|
||||
Assert.Fail($"Container {cid.GetValue()} exist");
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] GetRandomBytes(int size)
|
||||
{
|
||||
Random rnd = new();
|
||||
var bytes = new byte[size];
|
||||
rnd.NextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static IOptions<SingleOwnerClientSettings> GetSingleOwnerOptions(string key, string url)
|
||||
{
|
||||
return Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = url
|
||||
});
|
||||
}
|
||||
|
||||
private static IOptions<ClientSettings> GetOptions(string url)
|
||||
{
|
||||
return Options.Create(new ClientSettings
|
||||
{
|
||||
Host = url
|
||||
});
|
||||
}
|
||||
|
||||
static async Task Cleanup(Pool pool)
|
||||
{
|
||||
await foreach (var cid in pool.ListContainersAsync())
|
||||
{
|
||||
await pool.DeleteContainerAsync(new PrmContainerDelete(cid) { WaitParams = lightWait }).ConfigureAwait(true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,56 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.ClientV2.Interfaces;
|
||||
using FrostFS.SDK.ClientV2.Mappers.GRPC;
|
||||
using FrostFS.SDK.ClientV2;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FrostFS.SDK.Tests;
|
||||
|
||||
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
|
||||
public abstract class SessionTestsBase
|
||||
{
|
||||
protected readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
||||
|
||||
protected IOptions<SingleOwnerClientSettings> Settings { get; set; }
|
||||
|
||||
|
||||
protected ECDsa ECDsaKey { get; set; }
|
||||
protected FrostFsOwner OwnerId { get; set; }
|
||||
protected SessionMocker Mocker { get; set; }
|
||||
|
||||
protected SessionTestsBase()
|
||||
{
|
||||
Settings = Options.Create(new SingleOwnerClientSettings
|
||||
{
|
||||
Key = key,
|
||||
Host = "http://localhost:8080"
|
||||
});
|
||||
|
||||
ECDsaKey = key.LoadWif();
|
||||
OwnerId = FrostFsOwner.FromKey(ECDsaKey);
|
||||
|
||||
Mocker = new SessionMocker(this.key)
|
||||
{
|
||||
PlacementPolicy = new FrostFsPlacementPolicy(true, new FrostFsReplica(1)),
|
||||
Version = new FrostFsVersion(2, 13)
|
||||
};
|
||||
}
|
||||
|
||||
protected IFrostFSClient GetClient()
|
||||
{
|
||||
return ClientV2.FrostFSClient.GetTestInstance(
|
||||
Settings,
|
||||
null,
|
||||
new NetworkMocker(this.key).GetMock().Object,
|
||||
Mocker.GetMock().Object,
|
||||
new ContainerMocker(this.key).GetMock().Object,
|
||||
new ObjectMocker(this.key).GetMock().Object);
|
||||
}
|
||||
}
|
||||
|
||||
public class SessionTest : SessionTestsBase
|
||||
{
|
||||
[Theory]
|
||||
|
@ -14,11 +59,12 @@ public class SessionTest : SessionTestsBase
|
|||
public async void CreateSessionTest(bool useContext)
|
||||
{
|
||||
var exp = 100u;
|
||||
PrmSessionCreate param;
|
||||
var param = new PrmSessionCreate(exp);
|
||||
|
||||
if (useContext)
|
||||
{
|
||||
var ctx = new CallContext
|
||||
param.XHeaders.Add("headerKey1", "headerValue1");
|
||||
param.Context = new Context
|
||||
{
|
||||
CancellationToken = Mocker.CancellationTokenSource.Token,
|
||||
Timeout = TimeSpan.FromSeconds(20),
|
||||
|
@ -26,14 +72,6 @@ public class SessionTest : SessionTestsBase
|
|||
Key = ECDsaKey,
|
||||
Version = Mocker.Version
|
||||
};
|
||||
|
||||
param = new PrmSessionCreate(exp, ctx);
|
||||
|
||||
param.XHeaders.Add("headerKey1", "headerValue1");
|
||||
}
|
||||
else
|
||||
{
|
||||
param = new PrmSessionCreate(exp);
|
||||
}
|
||||
|
||||
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
|
||||
|
@ -63,6 +101,7 @@ public class SessionTest : SessionTestsBase
|
|||
Assert.NotNull(Mocker.CreateSessionRequest.MetaHeader);
|
||||
Assert.Equal(Mocker.Version.ToMessage(), Mocker.CreateSessionRequest.MetaHeader.Version);
|
||||
|
||||
|
||||
Assert.Null(Mocker.Metadata);
|
||||
|
||||
if (useContext)
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue