private async Task <RequestCopyFileResponse> RequestCopyFileAsync(RequestCopyFileRequest request, CancellationToken cancellationToken) { OperationStarted(); DateTime startTime = DateTime.UtcNow; Context cacheContext = new Context(new Guid(request.TraceId), Logger); ContentHash hash = request.ContentHash.ToContentHash((HashType)request.HashType); // Iterate through all known stores, looking for content in each. // In most of our configurations there is just one store anyway, // and doing this means both we can callers don't have // to deal with cache roots and drive letters. if (_contentStoreByCacheName.Values.OfType <ICopyRequestHandler>().FirstOrDefault() is ICopyRequestHandler handler) { var result = await handler.HandleCopyFileRequestAsync(cacheContext, hash); if (result.Succeeded) { return(new RequestCopyFileResponse { Header = ResponseHeader.Success(startTime) }); } return(new RequestCopyFileResponse { Header = ResponseHeader.Failure(startTime, result.ErrorMessage) }); } return(new RequestCopyFileResponse { Header = ResponseHeader.Failure(startTime, $"No stores implement {nameof(ICopyRequestHandler)}.") }); }
/// <summary> /// Implements a request copy file request /// </summary> private async Task <RequestCopyFileResponse> RequestCopyFileAsync(RequestCopyFileRequest request, CancellationToken cancellationToken) { OperationStarted(); var startTime = DateTime.UtcNow; var context = new OperationContext(new Context(new Guid(request.TraceId), _logger), cancellationToken); var sessionResult = await _sessionHandler.CreateSessionAsync(context, Guid.NewGuid().ToString(), cacheName : null, ImplicitPin.None, Capabilities.ContentOnly); if (!sessionResult.Succeeded) { return(new RequestCopyFileResponse { Header = ResponseHeader.Failure(startTime, sessionResult.ErrorMessage) }); } var session = _sessionHandler.GetSession(sessionResult.Value.sessionId); var pinResult = await session.PinAsync(context, request.ContentHash.ToContentHash((HashType)request.HashType), cancellationToken); await _sessionHandler.ReleaseSessionAsync(context, sessionResult.Value.sessionId); if (pinResult.Succeeded) { return(new RequestCopyFileResponse { Header = ResponseHeader.Success(startTime) }); } return(new RequestCopyFileResponse { Header = ResponseHeader.Failure(startTime, pinResult.ErrorMessage) }); }
/// <summary> /// Requests host to copy a file from another source machine. /// </summary> public async Task <BoolResult> RequestCopyFileAsync(OperationContext context, ContentHash hash) { try { var request = new RequestCopyFileRequest { TraceId = context.TracingContext.Id.ToString(), ContentHash = hash.ToByteString(), HashType = (int)hash.HashType }; var response = await _client.RequestCopyFileAsync(request, cancellationToken : context.Token); return(response.Header.Succeeded ? BoolResult.Success : new BoolResult(response.Header.ErrorMessage)); } catch (RpcException r) { return(new BoolResult(r)); } }
private Task <RequestCopyFileResponse> RequestCopyFileAsync(RequestCopyFileRequest request, CancellationToken cancellationToken) { ContentHash hash = request.ContentHash.ToContentHash((HashType)request.HashType); return(RunFuncNoSessionAsync( request.TraceId, async(context) => { // Iterate through all known stores, looking for content in each. // In most of our configurations there is just one store anyway, // and doing this means both we can callers don't have // to deal with cache roots and drive letters. if (_contentStoreByCacheName.Values.OfType <ICopyRequestHandler>().FirstOrDefault() is ICopyRequestHandler handler) { var result = await handler.HandleCopyFileRequestAsync(context.OperationContext, hash, context.Token); if (result.Succeeded) { return new RequestCopyFileResponse { Header = ResponseHeader.Success(context.StartTime) }; } return new RequestCopyFileResponse { Header = ResponseHeader.Failure(context.StartTime, result.ErrorMessage) }; } return new RequestCopyFileResponse { Header = ResponseHeader.Failure(context.StartTime, $"No stores implement {nameof(ICopyRequestHandler)}.") }; }, (context, errorMessage) => new RequestCopyFileResponse { Header = ResponseHeader.Failure(context.StartTime, errorMessage) }, cancellationToken)); }
/// <inheritdoc /> public override Task <RequestCopyFileResponse> RequestCopyFile(RequestCopyFileRequest request, ServerCallContext context) => _contentServer.RequestCopyFileAsync(request, context.CancellationToken);