Exemplo n.º 1
0
        private async Task <IEnumerable <Task <Indexed <PinResult> > > > PinBatchAsync(Context context, int baseIndex, IReadOnlyList <ContentHash> chunk)
        {
            StructResult <int> sessionResult = await _sessionState.GetIdAsync();

            if (!sessionResult.Succeeded)
            {
                PinResult pinResult = new PinResult(sessionResult);
                return(chunk.Select((ContentHash h) => pinResult).AsIndexed().AsTasks());
            }

            int sessionId = sessionResult.Data;

            var pinResults     = new List <Indexed <PinResult> >();
            var bulkPinRequest = new PinBulkRequest {
                Header = new RequestHeader(context.Id, sessionId)
            };

            foreach (var contentHash in chunk)
            {
                bulkPinRequest.Hashes.Add(
                    new ContentHashAndHashTypeData {
                    HashType = (int)contentHash.HashType, ContentHash = contentHash.ToByteString()
                });
            }

            DateTime        startTime = DateTime.UtcNow;
            PinBulkResponse underlyingBulkPinResponse = await RunClientActionAndThrowIfFailedAsync(
                context,
                async() => await _client.PinBulkAsync(bulkPinRequest));

            long ticksWaited = underlyingBulkPinResponse.Header.Values.First().ServerReceiptTimeUtcTicks - startTime.Ticks;

            _tracer.TrackClientWaitForServerTicks(ticksWaited);

            foreach (var response in underlyingBulkPinResponse.Header)
            {
                await ResetOnUnknownSessionAsync(context, response.Value, sessionId);

                pinResults.Add(UnpackPinResult(response.Value).WithIndex(response.Key + baseIndex));
            }

            _tracer.LogPinResults(context, pinResults.Select(r => chunk[r.Index - baseIndex]).ToList(), pinResults.Select(r => r.Item).ToList());

            return(pinResults.AsTasks());
        }
Exemplo n.º 2
0
        private async Task <IEnumerable <Task <Indexed <PinResult> > > > PinBatchAsync(Context context, int baseIndex, IReadOnlyList <ContentHash> chunk)
        {
            // This operation is quite different from others because there is no single header response.
            // So instead of using a common functionality we have handle this case separately.
            var sessionContext = await CreateSessionContextAsync(context);

            if (!sessionContext)
            {
                PinResult pinResult = new PinResult(sessionContext);
                return(chunk.Select((ContentHash h) => pinResult).AsIndexed().AsTasks());
            }

            int sessionId = sessionContext.Value.SessionId;

            var pinResults     = new List <Indexed <PinResult> >();
            var bulkPinRequest = new PinBulkRequest {
                Header = new RequestHeader(context.TraceId, sessionId)
            };

            foreach (var contentHash in chunk)
            {
                bulkPinRequest.Hashes.Add(
                    new ContentHashAndHashTypeData {
                    HashType = (int)contentHash.HashType, ContentHash = contentHash.ToByteString()
                });
            }

            PinBulkResponse underlyingBulkPinResponse = await SendGrpcRequestAndThrowIfFailedAsync(
                sessionContext.Value,
                async() => await Client.PinBulkAsync(bulkPinRequest),
                throwFailures : false);

            var info = underlyingBulkPinResponse.Info.Count == 0 ? null : underlyingBulkPinResponse.Info;

            foreach (var response in underlyingBulkPinResponse.Header)
            {
                await ResetOnUnknownSessionAsync(context, response.Value, sessionId);

                pinResults.Add(UnpackPinResult(response.Value, info?[response.Key]).WithIndex(response.Key + baseIndex));
            }

            ServiceClientTracer.LogPinResults(context, pinResults.Select(r => chunk[r.Index - baseIndex]).ToList(), pinResults.Select(r => r.Item).ToList());

            return(pinResults.AsTasks());
        }