[#31] Client: fix for session
Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
parent
195854a45b
commit
2e56c13946
10 changed files with 247 additions and 146 deletions
|
@ -12,7 +12,7 @@ public class ClientKey(ECDsa key)
|
|||
|
||||
internal ByteString PublicKeyProto { get; } = ByteString.CopyFrom(key.PublicKey());
|
||||
|
||||
internal string PublicKey { get; } = key.PublicKey().ToString();
|
||||
internal string PublicKey { get; } = Base58.Encode(key.PublicKey());
|
||||
|
||||
internal FrostFsOwner Owner { get; } = new FrostFsOwner(key.PublicKey().PublicKeyToAddress());
|
||||
}
|
||||
|
|
|
@ -447,7 +447,7 @@ public class FrostFSClient : IFrostFSClient
|
|||
{
|
||||
var invoker = CreateInvoker();
|
||||
|
||||
ObjectServiceClient = ObjectServiceClient ?? (
|
||||
ObjectServiceClient ??= (
|
||||
invoker != null
|
||||
? new ObjectServiceClient(invoker)
|
||||
: new ObjectServiceClient(ClientCtx.Channel));
|
||||
|
|
|
@ -47,7 +47,8 @@ public struct FrostFsPlacementPolicy(bool unique,
|
|||
Filters = { },
|
||||
Selectors = { },
|
||||
Replicas = { },
|
||||
Unique = Unique
|
||||
Unique = Unique,
|
||||
ContainerBackupFactor = BackupFactor
|
||||
};
|
||||
|
||||
foreach (var replica in Replicas)
|
||||
|
|
|
@ -45,9 +45,10 @@ public class FrostFsObject
|
|||
/// <value>Reader for received data</value>
|
||||
public IObjectReader? ObjectReader { get; set; }
|
||||
|
||||
internal byte[] SingleObjectPayload
|
||||
public byte[] SingleObjectPayload
|
||||
{
|
||||
get { return bytes ?? []; }
|
||||
set { bytes = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using System;
|
||||
|
||||
using System.Diagnostics;
|
||||
using FrostFS.Refs;
|
||||
|
||||
using FrostFS.SDK.Client;
|
||||
|
@ -36,7 +36,9 @@ public class FrostFsSessionToken
|
|||
get
|
||||
{
|
||||
if (_id == Guid.Empty)
|
||||
{
|
||||
_id = ProtoId.ToUuid();
|
||||
}
|
||||
|
||||
return _id;
|
||||
}
|
||||
|
@ -47,7 +49,9 @@ public class FrostFsSessionToken
|
|||
get
|
||||
{
|
||||
if (_sessionKey.IsEmpty)
|
||||
{
|
||||
_sessionKey = ProtoSessionKey.Memory;
|
||||
}
|
||||
|
||||
return _sessionKey;
|
||||
}
|
||||
|
@ -69,12 +73,15 @@ public class FrostFsSessionToken
|
|||
sessionToken.Body.Container = new() { Verb = verb };
|
||||
|
||||
if (containerId != null)
|
||||
{
|
||||
sessionToken.Body.Container.ContainerId = containerId;
|
||||
}
|
||||
else
|
||||
{
|
||||
sessionToken.Body.Container.Wildcard = true;
|
||||
}
|
||||
|
||||
sessionToken.Body.SessionKey = key.PublicKeyProto;
|
||||
|
||||
sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body);
|
||||
|
||||
return sessionToken;
|
||||
|
@ -100,7 +107,9 @@ public class FrostFsSessionToken
|
|||
ObjectSessionContext.Types.Target target = new() { Container = address.ContainerId };
|
||||
|
||||
if (address.ObjectId != null)
|
||||
{
|
||||
target.Objects.Add(address.ObjectId);
|
||||
}
|
||||
|
||||
sessionToken.Body.Object = new()
|
||||
{
|
||||
|
@ -108,8 +117,6 @@ public class FrostFsSessionToken
|
|||
Verb = verb
|
||||
};
|
||||
|
||||
sessionToken.Body.SessionKey = key.PublicKeyProto;
|
||||
|
||||
sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body);
|
||||
|
||||
return sessionToken;
|
||||
|
|
|
@ -32,6 +32,8 @@ namespace FrostFS.SDK.Client
|
|||
}
|
||||
};
|
||||
|
||||
chunkRequest.AddMetaHeader(args.XHeaders);
|
||||
|
||||
chunkRequest.Sign(this.ctx.Key.ECDsaKey);
|
||||
|
||||
await streamer.Write(chunkRequest).ConfigureAwait(false);
|
||||
|
|
|
@ -10,7 +10,6 @@ using FrostFS.Refs;
|
|||
using FrostFS.SDK.Client;
|
||||
using FrostFS.SDK.Client.Interfaces;
|
||||
using FrostFS.SDK.Client.Mappers.GRPC;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
using FrostFS.Session;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
@ -278,7 +277,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
var sessionToken = args.SessionToken ?? await GetDefaultSession(args, ctx).ConfigureAwait(false);
|
||||
|
||||
var protoToken = sessionToken.CreateObjectTokenContext(
|
||||
new Address { ContainerId = grpcObject.Header.ContainerId, ObjectId = grpcObject.ObjectId },
|
||||
new Address { ContainerId = grpcObject.Header.ContainerId },
|
||||
ObjectSessionContext.Types.Verb.Put,
|
||||
ClientContext.Key);
|
||||
|
||||
|
@ -297,7 +296,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
{
|
||||
var chunkSize = args.MaxChunkLength;
|
||||
Stream payload = args.Payload ?? throw new ArgumentNullException(nameof(args), "Stream parameter is null");
|
||||
|
||||
|
||||
var call = client.Patch(null, ctx.GetDeadline(), ctx.CancellationToken);
|
||||
|
||||
byte[]? chunkBuffer = null;
|
||||
|
@ -306,31 +305,15 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
// common
|
||||
chunkBuffer = ArrayPool<byte>.Shared.Rent(chunkSize);
|
||||
|
||||
bool isFirstChunk = true;
|
||||
ulong currentPos = args.Range.Offset;
|
||||
|
||||
var address = new Address
|
||||
{
|
||||
ObjectId = args.Address.ObjectId,
|
||||
ContainerId = args.Address.ContainerId
|
||||
};
|
||||
|
||||
var sessionToken = args.SessionToken ?? await GetDefaultSession(args, ctx).ConfigureAwait(false);
|
||||
|
||||
var protoToken = sessionToken.CreateObjectTokenContext(
|
||||
address,
|
||||
ObjectSessionContext.Types.Verb.Patch,
|
||||
ClientContext.Key);
|
||||
|
||||
var request = new PatchRequest()
|
||||
{
|
||||
Body = new()
|
||||
{
|
||||
Address = address,
|
||||
ReplaceAttributes = args.ReplaceAttributes,
|
||||
}
|
||||
};
|
||||
|
||||
bool isFirstChunk = true;
|
||||
ulong currentPos = args.Range.Offset;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var bytesCount = await payload.ReadAsync(chunkBuffer, 0, chunkSize, ctx.CancellationToken).ConfigureAwait(false);
|
||||
|
@ -340,41 +323,62 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
break;
|
||||
}
|
||||
|
||||
if (isFirstChunk && args.NewAttributes != null && args.NewAttributes.Length > 0)
|
||||
var request = new PatchRequest()
|
||||
{
|
||||
foreach (var attr in args.NewAttributes)
|
||||
Body = new()
|
||||
{
|
||||
request.Body.NewAttributes.Add(attr.ToMessage());
|
||||
Address = address,
|
||||
Patch = new PatchRequest.Types.Body.Types.Patch
|
||||
{
|
||||
Chunk = ByteString.CopyFrom(chunkBuffer, 0, bytesCount),
|
||||
SourceRange = new Object.Range { Offset = currentPos, Length = (ulong)bytesCount }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request.Body.Patch = new PatchRequest.Types.Body.Types.Patch
|
||||
{
|
||||
Chunk = ByteString.CopyFrom(chunkBuffer, 0, bytesCount),
|
||||
SourceRange = new Object.Range { Offset = currentPos, Length = (ulong)bytesCount }
|
||||
};
|
||||
|
||||
currentPos += (ulong)bytesCount;
|
||||
if (isFirstChunk)
|
||||
{
|
||||
var sessionToken = args.SessionToken ?? await GetDefaultSession(args, ctx).ConfigureAwait(false);
|
||||
|
||||
request.AddMetaHeader(args.XHeaders, protoToken);
|
||||
var protoToken = sessionToken.CreateObjectTokenContext(
|
||||
address,
|
||||
ObjectSessionContext.Types.Verb.Patch,
|
||||
ClientContext.Key);
|
||||
|
||||
request.AddMetaHeader(args.XHeaders, protoToken);
|
||||
|
||||
if (args.NewAttributes != null && args.NewAttributes.Length > 0)
|
||||
{
|
||||
foreach (var attr in args.NewAttributes)
|
||||
{
|
||||
request.Body.NewAttributes.Add(attr.ToMessage());
|
||||
request.Body.ReplaceAttributes = args.ReplaceAttributes;
|
||||
}
|
||||
}
|
||||
|
||||
isFirstChunk = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
request.AddMetaHeader(args.XHeaders);
|
||||
}
|
||||
|
||||
request.Sign(ClientContext.Key.ECDsaKey);
|
||||
|
||||
await call.RequestStream.WriteAsync(request).ConfigureAwait(false);
|
||||
|
||||
isFirstChunk = false;
|
||||
currentPos += (ulong)bytesCount;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await call.RequestStream.CompleteAsync().ConfigureAwait(false);
|
||||
|
||||
if (chunkBuffer != null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(chunkBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
await call.RequestStream.CompleteAsync().ConfigureAwait(false);
|
||||
var response = await call.ResponseAsync.ConfigureAwait(false);
|
||||
|
||||
Verifier.CheckResponse(response);
|
||||
|
@ -513,7 +517,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
{
|
||||
// send chunks limited to default or user's settings
|
||||
var bufferSize = objectLimitSize > 0 ?
|
||||
(int)Math.Min(objectLimitSize - sentBytes, chunkSize)
|
||||
Math.Min(objectLimitSize - sentBytes, chunkSize)
|
||||
: chunkSize;
|
||||
|
||||
var bytesCount = await payload.ReadAsync(chunkBuffer, 0, bufferSize, ctx.CancellationToken).ConfigureAwait(false);
|
||||
|
@ -531,6 +535,8 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
}
|
||||
};
|
||||
|
||||
chunkRequest.AddMetaHeader(args.XHeaders);
|
||||
|
||||
chunkRequest.Sign(ClientContext.Key.ECDsaKey);
|
||||
|
||||
await stream.Write(chunkRequest).ConfigureAwait(false);
|
||||
|
@ -571,8 +577,6 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
ObjectTools.SetSplitValues(grpcHeader, header.Split, ClientContext.Owner, ClientContext.Version, ClientContext.Key);
|
||||
}
|
||||
|
||||
var oid = new ObjectID { Value = grpcHeader.Sha256() };
|
||||
|
||||
var initRequest = new PutRequest
|
||||
{
|
||||
Body = new PutRequest.Types.Body
|
||||
|
@ -587,7 +591,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
|
|||
var sessionToken = args.SessionToken ?? await GetDefaultSession(args, ctx).ConfigureAwait(false);
|
||||
|
||||
var protoToken = sessionToken.CreateObjectTokenContext(
|
||||
new Address { ContainerId = grpcHeader.ContainerId, ObjectId = oid },
|
||||
new Address { ContainerId = grpcHeader.ContainerId },
|
||||
ObjectSessionContext.Types.Verb.Put,
|
||||
ClientContext.Key);
|
||||
|
||||
|
|
|
@ -18,7 +18,10 @@ public static class RequestConstructor
|
|||
if (request.MetaHeader is not null)
|
||||
return;
|
||||
|
||||
request.MetaHeader = MetaHeader.Default().ToMessage();
|
||||
var metaHeader = MetaHeader.Default();
|
||||
metaHeader.Ttl = 2;
|
||||
|
||||
request.MetaHeader = metaHeader.ToMessage();
|
||||
|
||||
if (sessionToken != null)
|
||||
request.MetaHeader.SessionToken = sessionToken;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue