Exemplo n.º 1
0
        /// <summary>
        /// Queries the asynchronous.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryRequest">The query request.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public async Task <IAnalyticsResult <T> > QueryAsync <T>(IAnalyticsRequest queryRequest, CancellationToken token)
        {
            var result = new AnalyticsResult <T>();

            FailureCountingUri baseUri;

            if (!TryGetUri(result, out baseUri))
            {
                return(result);
            }

            ApplyCredentials(queryRequest, ClientConfiguration);

            string body;

            using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.RequestEncoding).StartActive())
            {
                body = queryRequest.GetFormValuesAsJson();
            }

            using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json))
            {
                try
                {
                    Log.Trace("Sending analytics query cid{0}: {1}", queryRequest.CurrentContextId, baseUri);

                    HttpResponseMessage response;
                    using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.DispatchToServer).StartActive())
                    {
                        var request = new HttpRequestMessage(HttpMethod.Post, baseUri)
                        {
                            Content = content
                        };

                        if (queryRequest is AnalyticsRequest req && req.PriorityValue != 0)
                        {
                            request.Headers.Add(AnalyticsPriorityHeaderName, new[] { req.PriorityValue.ToString() });
                        }

                        response = await HttpClient.SendAsync(request, token).ContinueOnAnyContext();
                    }

                    using (var scope = ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.ResponseDecoding).StartActive())
                        using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext())
                        {
                            result                = DataMapper.Map <AnalyticsResultData <T> >(stream).ToQueryResult();
                            result.Success        = result.Status == QueryStatus.Success;
                            result.HttpStatusCode = response.StatusCode;
                            Log.Trace("Received analytics query cid{0}: {1}", result.ClientContextId, result.ToString());

                            scope.Span.SetPeerLatencyTag(result.Metrics.ElaspedTime);
                        }
                    baseUri.ClearFailed();
                }
                catch (OperationCanceledException e)
                {
                    var operationContext = OperationContext.CreateAnalyticsContext(queryRequest.CurrentContextId, Context.BucketName, baseUri?.Authority);
                    if (queryRequest is AnalyticsRequest request)
                    {
                        operationContext.TimeoutMicroseconds = request.TimeoutValue;
                    }

                    Log.Info(operationContext.ToString());
                    ProcessError(e, result);
                }
                catch (HttpRequestException e)
                {
                    Log.Info("Failed analytics query cid{0}: {1}", queryRequest.CurrentContextId, baseUri);
                    baseUri.IncrementFailed();
                    ProcessError(e, result);
                    Log.Error(e);
                }
                catch (AggregateException ae)
                {
                    ae.Flatten().Handle(e =>
                    {
                        Log.Info("Failed analytics query cid{0}: {1}", queryRequest.CurrentContextId, baseUri);
                        ProcessError(e, result);
                        return(true);
                    });
                }
                catch (Exception e)
                {
                    Log.Info("Failed analytics query cid{0}: {1}", queryRequest.CurrentContextId, baseUri);
                    Log.Info(e);
                    ProcessError(e, result);
                }
            }

            UpdateLastActivity();

            return(result);
        }