public async Task <IViewResult <TKey, TValue> > ExecuteAsync <TKey, TValue>(IViewQuery query) { using var rootSpan = _tracer.RootSpan(RequestTracing.ServiceIdentifier.View, OperationNames.ViewQuery) .WithTag(CouchbaseTags.Service, RequestTracing.ServiceIdentifier.View) .WithLocalAddress(); using var encodingSpan = rootSpan.StartPayloadEncoding(); var uri = query.RawUri(); rootSpan.WithRemoteAddress(uri); ViewResultBase <TKey, TValue> viewResult; var body = query.CreateRequestBody(); try { _logger.LogDebug("Sending view request to: {uri}", _redactor.SystemData(uri)); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); encodingSpan.Dispose(); using var dispatchSpan = rootSpan.StartDispatch(); var response = await HttpClient.PostAsync(uri, content).ConfigureAwait(false); dispatchSpan.Dispose(); var serializer = query.Serializer ?? _serializer; if (response.IsSuccessStatusCode) { if (serializer is IStreamingTypeDeserializer streamingTypeDeserializer) { viewResult = new StreamingViewResult <TKey, TValue>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), streamingTypeDeserializer ); } else { viewResult = new BlockViewResult <TKey, TValue>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), serializer ); } await viewResult.InitializeAsync().ConfigureAwait(false); } else { if (serializer is IStreamingTypeDeserializer streamingTypeDeserializer) { viewResult = new StreamingViewResult <TKey, TValue>( response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false), streamingTypeDeserializer ); } else { viewResult = new BlockViewResult <TKey, TValue>( response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false), serializer ); } await viewResult.InitializeAsync().ConfigureAwait(false); if (viewResult.ShouldRetry()) { UpdateLastActivity(); return(viewResult); } if (viewResult.ViewNotFound()) { throw new ViewNotFoundException(uri.ToString()) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = response.StatusCode, Errors = viewResult.Message } }; } } } catch (OperationCanceledException e) { _logger.LogDebug(LoggingEvents.ViewEvent, e, "View request timeout."); throw new AmbiguousTimeoutException("The view query was timed out via the Token.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } catch (HttpRequestException e) { _logger.LogDebug(LoggingEvents.QueryEvent, e, "View request cancelled."); throw new RequestCanceledException("The view query was canceled.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } UpdateLastActivity(); return(viewResult); }
public async Task <IViewResult <TKey, TValue> > ExecuteAsync <TKey, TValue>(IViewQuery query) { using var rootSpan = RootSpan(OuterRequestSpans.ServiceSpan.ViewQuery, query); rootSpan.WithLocalAddress() .WithOperation(query); var uri = query.RawUri(); rootSpan.WithRemoteAddress(uri); using var encodingSpan = rootSpan.EncodingSpan(); ViewResultBase <TKey, TValue> viewResult; var body = query.CreateRequestBody(); try { _logger.LogDebug("Sending view request to: {uri}", _redactor.SystemData(uri)); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); encodingSpan.Dispose(); using var dispatchSpan = rootSpan.DispatchSpan(query); using var httpClient = CreateHttpClient(); // set timeout to infinite so we can stream results without the connection // closing part way through httpClient.Timeout = Timeout.InfiniteTimeSpan; var response = await httpClient.PostAsync(uri, content).ConfigureAwait(false); dispatchSpan.Dispose(); var serializer = query.Serializer ?? _serializer; if (response.IsSuccessStatusCode) { if (serializer is IStreamingTypeDeserializer streamingTypeDeserializer) { viewResult = new StreamingViewResult <TKey, TValue>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), streamingTypeDeserializer ); } else { viewResult = new BlockViewResult <TKey, TValue>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), serializer ); } await viewResult.InitializeAsync().ConfigureAwait(false); } else { if (serializer is IStreamingTypeDeserializer streamingTypeDeserializer) { viewResult = new StreamingViewResult <TKey, TValue>( response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false), streamingTypeDeserializer ); } else { viewResult = new BlockViewResult <TKey, TValue>( response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false), serializer ); } await viewResult.InitializeAsync().ConfigureAwait(false); if (viewResult.ShouldRetry()) { viewResult.NoRetryException = new CouchbaseException() { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = response.StatusCode, Errors = viewResult.Message } }; UpdateLastActivity(); return(viewResult); } if (viewResult.ViewNotFound()) { throw new ViewNotFoundException("The queried view is not found on the server.") { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = response.StatusCode, Errors = viewResult.Message } }; } } } catch (OperationCanceledException e) { //treat as an orphaned response rootSpan.LogOrphaned(); _logger.LogDebug(LoggingEvents.ViewEvent, e, "View request timeout."); throw new AmbiguousTimeoutException("The view query was timed out via the Token.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } catch (HttpRequestException e) { //treat as an orphaned response rootSpan.LogOrphaned(); _logger.LogDebug(LoggingEvents.QueryEvent, e, "View request cancelled."); throw new RequestCanceledException("The view query was canceled.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } UpdateLastActivity(); return(viewResult); }