protected override async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest, CancellationToken cancellationToken) { var queryResult = new StreamingQueryResult <T>(); FailureCountingUri baseUri; if (!TryGetQueryUri(out baseUri)) { Log.Error(ExceptionUtil.EmptyUriTryingSubmitN1qlQuery); ProcessError(new InvalidOperationException(ExceptionUtil.EmptyUriTryingSubmitN1QlQuery), queryResult); return(queryResult); } ApplyCredentials(queryRequest); string body; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.RequestEncoding).Start()) { body = queryRequest.GetFormValuesAsJson(); } using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json)) { try { var requestMessage = new HttpRequestMessage(HttpMethod.Post, baseUri) { Content = content }; Log.Trace("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); HttpResponseMessage response; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.DispatchToServer).Start()) { response = await HttpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ContinueOnAnyContext(); } var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext(); { queryResult = new StreamingQueryResult <T> { ResponseStream = stream, HttpStatusCode = response.StatusCode, Success = response.StatusCode == HttpStatusCode.OK, QueryTimer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfiguration.EnableQueryTiming), DecodeSpan = ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.ResponseDecoding).Start() }; Log.Trace("Received query cid{0}: {1}", queryRequest.CurrentContextId, queryResult.HttpStatusCode); } baseUri.ClearFailed(); } catch (OperationCanceledException e) { var operationContext = OperationContext.CreateQueryContext(queryRequest.CurrentContextId, Context.BucketName, baseUri?.Authority); if (queryRequest is QueryRequest request) { operationContext.TimeoutMicroseconds = request.TimeoutValue; } Log.Info(operationContext.ToString()); ProcessError(e, queryResult); } catch (HttpRequestException e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } UpdateLastActivity(); return(queryResult); }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <param name="cancellationToken">Token which can cancel the query.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> protected virtual async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest, CancellationToken cancellationToken) { var queryResult = new QueryResult <T>(); Log.Debug("Gettting Query Uri cid{0}", queryRequest.CurrentContextId); if (!TryGetQueryUri(out var baseUri)) { Log.Error(ExceptionUtil.EmptyUriTryingSubmitN1qlQuery); ProcessError(new InvalidOperationException(ExceptionUtil.EmptyUriTryingSubmitN1QlQuery), queryResult); return(queryResult); } Log.Debug("Applying creds cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ApplyCredentials(queryRequest); Log.Debug("Removing brackets cid{0}: {1}", queryRequest.CurrentContextId, baseUri); if (Log.IsDebugEnabled) { //need to remove the brackets or else string.format will fail in Log.Debug var req = queryRequest.ToString(); Log.Debug(req.Replace("{", "").Replace("}", "")); } Log.Debug("Buildspan cid{0}: {1}", queryRequest.CurrentContextId, baseUri); string body; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.RequestEncoding).StartActive()) { body = queryRequest.GetFormValuesAsJson(); } Log.Debug("Getting content cid{0}: {1}", queryRequest.CurrentContextId, baseUri); using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json)) { try { using (var timer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfiguration.EnableQueryTiming)) { Log.Debug("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); HttpResponseMessage response; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.DispatchToServer).StartActive()) { response = await HttpClient.PostAsync(baseUri, content, cancellationToken).ContinueOnAnyContext(); } Log.Debug("Handling response cid{0}: {1}", queryRequest.CurrentContextId, baseUri); using (var scope = ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.ResponseDecoding).StartActive()) using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { Log.Debug("Mapping cid{0}: {1}", queryRequest.CurrentContextId, baseUri); queryResult = GetDataMapper(queryRequest).Map <QueryResultData <T> >(stream).ToQueryResult(); queryResult.Success = queryResult.Status == QueryStatus.Success; queryResult.HttpStatusCode = response.StatusCode; Log.Trace("Received query cid{0}: {1}", queryResult.ClientContextId, queryResult.ToString()); timer.ClusterElapsedTime = queryResult.Metrics.ElaspedTime; scope.Span.SetPeerLatencyTag(queryResult.Metrics.ElaspedTime); } } baseUri.ClearFailed(); } catch (OperationCanceledException e) { var operationContext = OperationContext.CreateQueryContext(queryRequest.CurrentContextId, Context.BucketName, baseUri?.Authority); if (queryRequest is QueryRequest request) { operationContext.TimeoutMicroseconds = request.TimeoutValue; } Log.Info(operationContext.ToString()); ProcessError(e, queryResult); } catch (HttpRequestException e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } UpdateLastActivity(); return(queryResult); }