Exemplo n.º 1
0
 public void PublishConfig(BucketConfig bucketConfig)
 {
     _logger.LogDebug(LoggingEvents.ConfigEvent, JsonConvert.SerializeObject(bucketConfig));
     _configHandler.Publish(bucketConfig);
 }
        private Task StartBackgroundTask()
        {
            // Ensure that we don't flow the ExecutionContext into the long running task below
            using var flowControl = ExecutionContext.SuppressFlow();

            return(Task.Run(async() =>
            {
                var delayMs = InitialDelayMs;

                var bucket = _bucket as BucketBase;

                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        var nodes = bucket?.Nodes.ToList().Shuffle();
                        while (nodes != null && nodes.Any())
                        {
                            try
                            {
                                var node = nodes.First();
                                nodes?.Remove(node);

                                _logger.LogDebug("HTTP Streaming with node {node}", node.EndPoint.Host);

                                var streamingUri = new UriBuilder()
                                {
                                    Scheme =
                                        _clusterOptions.EffectiveEnableTls ? Uri.UriSchemeHttps : Uri.UriSchemeHttp,
                                    Host = node.ManagementUri.Host,
                                    Port = node.ManagementUri.Port,
                                    Path = _streamingUriPath
                                };

                                using var httpClient = _httpClientFactory.Create();
                                httpClient.Timeout = Timeout.InfiniteTimeSpan;

                                var response = await httpClient.GetAsync(streamingUri.Uri,
                                                                         HttpCompletionOption.ResponseHeadersRead,
                                                                         _cancellationTokenSource.Token).ConfigureAwait(false);

                                response.EnsureSuccessStatusCode();

                                using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
                                if (stream.CanTimeout)
                                {
                                    //the stream itself can timeout if CanTimeout is true on a platform
                                    stream.ReadTimeout = Timeout.Infinite;
                                }

                                using var reader = new StreamReader(stream, Encoding.UTF8, false);

                                string?config;
                                while (!_cancellationTokenSource.IsCancellationRequested &&
                                       (config = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
                                {
                                    if (config != string.Empty)
                                    {
                                        _logger.LogDebug(LoggingEvents.ConfigEvent, config);
                                        config = config.Replace("$HOST", node.EndPoint.Host);
                                        var bucketConfig = JsonSerializer.Deserialize(config,
                                                                                      InternalSerializationContext.Default.BucketConfig) !;
                                        _configHandler.Publish(bucketConfig);
                                    }

                                    // on success, reset the exponential delay
                                    delayMs = InitialDelayMs;
                                }
                            }
                            catch (Exception e)
                            {
                                _logger.LogError(e, "HTTP Streaming error.");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "HTTP Streaming error. (outer loop)");
                    }

                    // if we exited the inner loop, then all servers failed and we need to start over.
                    // however, we don't want to create a failstorm in the logs if the failure is 100%
                    // try again, but with an exponential delay of up to 10s.
                    await Task.Delay(delayMs).ConfigureAwait(false);
                    delayMs = Math.Min(delayMs * 10, MaxDelayMs);
                }
            }, _cancellationTokenSource.Token));
        }
Exemplo n.º 3
0
 public void PublishConfig(BucketConfig bucketConfig)
 {
     _configHandler.Publish(bucketConfig);
 }
        private Task StartBackgroundTask()
        {
            return(Task.Run(async() =>
            {
                _httpClient.Timeout = Timeout.InfiniteTimeSpan;
                var delayMs = InitialDelayMs;

                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        var servers = _clusterOptions.ConnectionStringValue?.GetBootstrapEndpoints().ToList().Shuffle();

                        while (servers.Any())
                        {
                            try
                            {
                                var server = servers.First();
                                servers?.Remove(server);

                                var streamingUri = new UriBuilder()
                                {
                                    Scheme =
                                        _clusterOptions.EffectiveEnableTls ? Uri.UriSchemeHttps : Uri.UriSchemeHttp,
                                    Host = server.Host,
                                    Port = _clusterOptions.BootstrapHttpPort,
                                    Path = _streamingUriPath
                                };

                                var response = await _httpClient.GetAsync(streamingUri.Uri,
                                                                          HttpCompletionOption.ResponseHeadersRead,
                                                                          _cancellationTokenSource.Token).ConfigureAwait(false);

                                response.EnsureSuccessStatusCode();

                                using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
                                if (stream.CanTimeout)
                                {
                                    //the stream itself can timeout if CanTimeout is true on a platform
                                    stream.ReadTimeout = Timeout.Infinite;
                                }

                                using var reader = new StreamReader(stream, Encoding.UTF8, false);

                                string?config;
                                while (!_cancellationTokenSource.IsCancellationRequested &&
                                       (config = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
                                {
                                    if (config != string.Empty)
                                    {
                                        _logger.LogDebug(LoggingEvents.ConfigEvent, config);
                                        config = config.Replace("$HOST", server.Host);
                                        var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(config);
                                        _configHandler.Publish(bucketConfig);
                                    }

                                    // on success, reset the exponential delay
                                    delayMs = InitialDelayMs;
                                }
                            }
                            catch (Exception e)
                            {
                                _logger.LogError(e, "HTTP Streaming error.");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "HTTP Streaming error. (outer loop)");
                    }

                    // if we exited the inner loop, then all servers failed and we need to start over.
                    // however, we don't want to create a failstorm in the logs if the failure is 100%
                    // try again, but with an exponential delay of up to 10s.
                    await Task.Delay(delayMs).ConfigureAwait(false);
                    delayMs = Math.Min(delayMs * 10, MaxDelayMs);
                }
            }, _cancellationTokenSource.Token));
        }
        public void StartListening()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(HttpStreamingConfigListener));
            }

            if (Started)
            {
                return;
            }

            Started = true;

            var streamingUriPath = "/pools/default/bs/" + _bucketName;

            Task.Run(async() =>
            {
                _httpClient.Timeout = Timeout.InfiniteTimeSpan;

                var servers = _clusterOptions.ConnectionStringValue !.GetBootstrapEndpoints().ToList().Shuffle();
                while (servers.Any())
                {
                    try
                    {
                        var server = servers.First();
                        servers.Remove(server);

                        var streamingUri = new UriBuilder()
                        {
                            Scheme = _clusterOptions.EffectiveEnableTls ? Uri.UriSchemeHttps : Uri.UriSchemeHttp,
                            Host   = server.Host,
                            Port   = _clusterOptions.BootstrapHttpPort,
                            Path   = streamingUriPath
                        };

                        var response = await _httpClient.GetAsync(streamingUri.Uri,
                                                                  HttpCompletionOption.ResponseHeadersRead,
                                                                  _cancellationTokenSource.Token).ConfigureAwait(false);

                        response.EnsureSuccessStatusCode();

                        using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
                        if (stream.CanTimeout)
                        {
                            //the stream itself can timeout if CanTimeout is true on a platform
                            stream.ReadTimeout = Timeout.Infinite;
                        }

                        using var reader = new StreamReader(stream, Encoding.UTF8, false);

                        string?config;
                        while (!_cancellationTokenSource.IsCancellationRequested &&
                               (config = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
                        {
                            if (config != string.Empty)
                            {
                                _logger.LogDebug(LoggingEvents.ConfigEvent, config);
                                config           = config.Replace("$HOST", server.Host);
                                var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(config);
                                _configHandler.Publish(bucketConfig);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "HTTP Streaming error.");
                    }
                }
            }, _cancellationTokenSource.Token);
        }