Esempio n. 1
0
        private static byte[] EncodeRequest(IRequestContext context, MetadataRequest request)
        {
            using (var message = EncodeHeader(context, request)) {
                message.Write(request.Topics, true);

                return(message.ToBytes());
            }
        }
 /// <inheritdoc />
 public bool Equals(MetadataRequest other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(topics.HasEqualElementsInOrder(other.topics));
 }
Esempio n. 3
0
        /// <summary>
        /// Given a collection of server connections, query for the topic metadata.
        /// </summary>
        /// <param name="router">The router which provides the route and metadata.</param>
        /// <param name="request">Metadata request to make</param>
        /// <param name="cancellationToken"></param>
        /// <remarks>
        /// Used by <see cref="Router"/> internally. Broken out for better testability, but not intended to be used separately.
        /// </remarks>
        /// <returns>MetadataResponse validated to be complete.</returns>
        internal static async Task <MetadataResponse> GetMetadataAsync(this IRouter router, MetadataRequest request, CancellationToken cancellationToken)
        {
            return(await router.Configuration.RefreshRetry.TryAsync(
                       async (retryAttempt, elapsed) => {
                var connections = router.Connections.ToList();
                var connection = connections[retryAttempt % connections.Count];
                var response = await connection.SendAsync(request, cancellationToken).ConfigureAwait(false);
                if (response == null)
                {
                    return new RetryAttempt <MetadataResponse>(null);
                }

                var results = response.brokers
                              .Select(ValidateServer)
                              .Union(response.topic_metadata.Select(ValidateTopic))
                              .Where(r => !r.IsValid.GetValueOrDefault())
                              .ToList();

                var exceptions = results.Select(r => r.ToException(connection.Endpoint)).Where(e => e != null).ToList();
                if (exceptions.Count == 1)
                {
                    throw exceptions.Single();
                }
                if (exceptions.Count > 1)
                {
                    throw new AggregateException(exceptions);
                }

                if (results.Count == 0)
                {
                    return new RetryAttempt <MetadataResponse>(response);
                }
                foreach (var result in results.Where(r => !string.IsNullOrEmpty(r.Message)))
                {
                    router.Log.Warn(() => LogEvent.Create(result.Message));
                }

                return new RetryAttempt <MetadataResponse>(response, false);
            },
                       (ex, retryAttempt, retryDelay) => router.Log.Warn(() => LogEvent.Create(ex, $"Failed metadata request on attempt {retryAttempt}: Will retry in {retryDelay}")),
                       null, // return the failed response above, resulting in the final response
                       cancellationToken).ConfigureAwait(false));
        }