Beispiel #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);

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

                    using (var response = await request.Content.ReadAsStreamAsync().ContinueOnAnyContext())
                    {
                        result                = DataMapper.Map <AnalyticsResultData <T> >(response).ToQueryResult();
                        result.Success        = result.Status == QueryStatus.Success;
                        result.HttpStatusCode = request.StatusCode;
                        Log.Trace("Received analytics query cid{0}: {1}", result.ClientContextId, result.ToString());
                    }
                    baseUri.ClearFailed();
                }
                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);
        }
        /// <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)
        {
            // try get Analytics node
            if (!ClusterOptions.GlobalNodes.TryGetRandom(x => x.HasAnalytics(), out var node))
            {
                //const string noNodeAvailableMessage = "Unable to locate analytics node to submit query to.";
                //Logger.LogError(noNodeAvailableMessage);
                throw new ServiceNotAvailableException(ServiceType.Analytics);
            }

            var result = new AnalyticsResult <T>();

            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, node.AnalyticsUri)
                    {
                        Content = content
                    };

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

                    response = await HttpClient.SendAsync(request, token).ConfigureAwait(false);

                    //}

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

                        //scope.Span.SetPeerLatencyTag(result.Metrics.ElaspedTime);
                    }
                    //uri.ClearFailed();analytu
                }
                catch (OperationCanceledException e)
                {
                    //var operationContext = OperationContext.CreateAnalyticsContext(queryRequest.CurrentContextId, Context.BucketName, uri?.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);
                    //uri.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);
                        //Log.Error(e);
                        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);
        }
Beispiel #3
0
        public async Task <IAnalyticsResult <T> > QueryAsync <T>(IAnalyticsRequest queryRequest, CancellationToken token = default)
        {
            // try get Analytics node
            var analyticsUri = _serviceUriProvider.GetRandomAnalyticsUri();
            AnalyticsResultBase <T> result;
            var body = queryRequest.GetFormValuesAsJson();

            using (var content = new StringContent(body, Encoding.UTF8, MediaType.Json))
            {
                try
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, analyticsUri)
                    {
                        Content = content
                    };

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

                    var response = await HttpClient.SendAsync(request, token).ConfigureAwait(false);

                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    if (_typeSerializer is IStreamingTypeDeserializer streamingTypeDeserializer)
                    {
                        result = new StreamingAnalyticsResult <T>(stream, streamingTypeDeserializer)
                        {
                            HttpStatusCode = response.StatusCode
                        };
                    }
                    else
                    {
                        result = new BlockAnalyticsResult <T>(stream, _typeSerializer)
                        {
                            HttpStatusCode = response.StatusCode
                        };
                    }

                    await result.InitializeAsync(token).ConfigureAwait(false);

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        if (result.ShouldRetry())
                        {
                            UpdateLastActivity();
                            return(result);
                        }

                        if (result.LinkNotFound())
                        {
                            throw new LinkNotFoundException();
                        }
                        if (result.DataverseExists())
                        {
                            throw new DataverseExistsException();
                        }
                        if (result.DatasetExists())
                        {
                            throw new DatasetExistsException();
                        }
                        if (result.DataverseNotFound())
                        {
                            throw new DataverseNotFoundException();
                        }
                        if (result.DataSetNotFound())
                        {
                            throw new DatasetNotFoundException();
                        }
                        if (result.JobQueueFull())
                        {
                            throw new JobQueueFullException();
                        }
                        if (result.CompilationFailure())
                        {
                            throw new CompilationFailureException();
                        }
                        if (result.InternalServerFailure())
                        {
                            throw new InternalServerFailureException();
                        }
                        if (result.AuthenticationFailure())
                        {
                            throw new AuthenticationFailureException();
                        }
                        if (result.TemporaryFailure())
                        {
                            throw new TemporaryFailureException();
                        }
                        if (result.ParsingFailure())
                        {
                            throw new ParsingFailureException();
                        }
                        if (result.IndexNotFound())
                        {
                            throw new IndexNotFoundException();
                        }
                        if (result.IndexExists())
                        {
                            throw new IndexExistsException();
                        }
                    }
                }
                catch (OperationCanceledException e)
                {
                    _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request timeout.");
                    if (queryRequest.ReadOnly)
                    {
                        throw new UnambiguousTimeoutException("The query was timed out via the Token.", e);
                    }

                    throw new AmbiguousTimeoutException("The query was timed out via the Token.", e);
                }
                catch (HttpRequestException e)
                {
                    _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request cancelled.");
                    throw new RequestCanceledException("The query was canceled.", e);
                }
            }

            UpdateLastActivity();
            return(result);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public async Task <IAnalyticsResult <T> > QueryAsync <T>(IAnalyticsRequest queryRequest, CancellationToken token = default)
        {
            using var rootSpan = _tracer.RootSpan(RequestTracing.ServiceIdentifier.Analytics, OperationNames.AnalyticsQuery)
                                 .WithTag(CouchbaseTags.OperationId, queryRequest.ClientContextId ?? Guid.NewGuid().ToString())
                                 .WithLocalAddress();

            // try get Analytics node
            var analyticsUri = _serviceUriProvider.GetRandomAnalyticsUri();

            rootSpan.WithRemoteAddress(analyticsUri);

            _logger.LogDebug("Sending analytics query with a context id {contextId} to server {searchUri}",
                             queryRequest.ClientContextId, analyticsUri);

            using var encodingSpan = rootSpan.StartPayloadEncoding();

            AnalyticsResultBase <T> result;
            var body = queryRequest.GetFormValuesAsJson();

            using (var content = new StringContent(body, Encoding.UTF8, MediaType.Json))
            {
                try
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, analyticsUri)
                    {
                        Content = content
                    };

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

                    encodingSpan.Dispose();
                    using var dispatchSpan = rootSpan.StartDispatch();
                    var response = await HttpClient.SendAsync(request, token).ConfigureAwait(false);

                    dispatchSpan.Dispose();
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    if (_typeSerializer is IStreamingTypeDeserializer streamingTypeDeserializer)
                    {
                        result = new StreamingAnalyticsResult <T>(stream, streamingTypeDeserializer)
                        {
                            HttpStatusCode = response.StatusCode
                        };
                    }
                    else
                    {
                        result = new BlockAnalyticsResult <T>(stream, _typeSerializer)
                        {
                            HttpStatusCode = response.StatusCode
                        };
                    }

                    await result.InitializeAsync(token).ConfigureAwait(false);

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        if (result.ShouldRetry())
                        {
                            UpdateLastActivity();
                            return(result);
                        }

                        var context = new AnalyticsErrorContext
                        {
                            ClientContextId = queryRequest.ClientContextId,
                            HttpStatus      = response.StatusCode,
                            Statement       = queryRequest.Statement,
                            Parameters      = queryRequest.GetParametersAsJson(),
                            Errors          = result.Errors
                        };

                        if (result.LinkNotFound())
                        {
                            throw new LinkNotFoundException(context);
                        }
                        if (result.DataverseExists())
                        {
                            throw new DataverseExistsException(context);
                        }
                        if (result.DatasetExists())
                        {
                            throw new DatasetExistsException();
                        }
                        if (result.DataverseNotFound())
                        {
                            throw new DataverseNotFoundException(context);
                        }
                        if (result.DataSetNotFound())
                        {
                            throw new DatasetNotFoundException(context);
                        }
                        if (result.JobQueueFull())
                        {
                            throw new JobQueueFullException(context);
                        }
                        if (result.CompilationFailure())
                        {
                            throw new CompilationFailureException(context);
                        }
                        if (result.InternalServerFailure())
                        {
                            throw new InternalServerFailureException(context);
                        }
                        if (result.AuthenticationFailure())
                        {
                            throw new AuthenticationFailureException(context);
                        }
                        if (result.TemporaryFailure())
                        {
                            throw new TemporaryFailureException(context);
                        }
                        if (result.ParsingFailure())
                        {
                            throw new ParsingFailureException(context);
                        }
                        if (result.IndexNotFound())
                        {
                            throw new IndexNotFoundException(context);
                        }
                        if (result.IndexExists())
                        {
                            throw new IndexExistsException(context);
                        }
                    }
                }
                catch (OperationCanceledException e)
                {
                    var context = new AnalyticsErrorContext
                    {
                        ClientContextId = queryRequest.ClientContextId,
                        Statement       = queryRequest.Statement,
                        Parameters      = queryRequest.GetParametersAsJson()
                    };

                    _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request timeout.");
                    if (queryRequest.ReadOnly)
                    {
                        throw new UnambiguousTimeoutException("The query was timed out via the Token.", e)
                              {
                                  Context = context
                              };
                    }

                    throw new AmbiguousTimeoutException("The query was timed out via the Token.", e)
                          {
                              Context = context
                          };
                }
                catch (HttpRequestException e)
                {
                    var context = new AnalyticsErrorContext
                    {
                        ClientContextId = queryRequest.ClientContextId,
                        Statement       = queryRequest.Statement,
                        Parameters      = queryRequest.GetParametersAsJson()
                    };

                    _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request cancelled.");
                    throw new RequestCanceledException("The query was canceled.", e)
                          {
                              Context = context
                          };
                }
            }

            UpdateLastActivity();
            return(result);
        }