コード例 #1
0
        public async Task <IAnalyticsResult <T> > QueryAsync <T>(IAnalyticsRequest queryRequest, CancellationToken token = default)
        {
            // try get Analytics node
            var analyticsUri = _serviceUriProvider.GetRandomAnalyticsUri();

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

            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);
                        }

                        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);
        }
コード例 #2
0
        public async Task <IAnalyticsResult <T> > QueryAsync <T>(string statement, AnalyticsOptions options)
        {
            using var rootSpan = RootSpan(OuterRequestSpans.ServiceSpan.AnalyticsQuery, options)
                                 .WithOperationId(options)
                                 .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}",
                             options.ClientContextIdValue, analyticsUri);

            using var encodingSpan = rootSpan.EncodingSpan();

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

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

                    if (options.PriorityValue != 0)
                    {
                        request.Headers.Add(AnalyticsPriorityHeaderName, new[] { options.PriorityValue.ToStringInvariant() });
                    }

                    encodingSpan.Dispose();
                    using var dispatchSpan = rootSpan.DispatchSpan(options);
                    using var httpClient   = CreateHttpClient(options.TimeoutValue);

                    var response = await httpClient.SendAsync(request, options.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(options.Token).ConfigureAwait(false);

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var context = new AnalyticsErrorContext
                        {
                            ClientContextId = options.ClientContextIdValue,
                            HttpStatus      = response.StatusCode,
                            Statement       = statement,
                            Parameters      = options.GetParametersAsJson(),
                            Errors          = result.Errors
                        };

                        if (result.ShouldRetry())
                        {
                            result.NoRetryException = CreateExceptionForError(result, context, true);
                            UpdateLastActivity();
                            return(result);
                        }

                        CouchbaseException?ex = CreateExceptionForError(result, context, false);
                        if (ex != null)
                        {
                            throw ex;
                        }
                    }
                }
                catch (OperationCanceledException e)
                {
                    //treat as an orphaned response
                    rootSpan.LogOrphaned();

                    var context = new AnalyticsErrorContext
                    {
                        ClientContextId = options.ClientContextIdValue,
                        Statement       = statement,
                        Parameters      = options.GetParametersAsJson()
                    };

                    _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request timeout.");
                    if (options.ReadonlyValue)
                    {
                        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)
                {
                    //treat as an orphaned response
                    rootSpan.LogOrphaned();

                    var context = new AnalyticsErrorContext
                    {
                        ClientContextId = options.ClientContextIdValue,
                        Statement       = statement,
                        Parameters      = options.GetParametersAsJson()
                    };

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

            UpdateLastActivity();
            return(result);
        }