コード例 #1
0
        /// <summary>
        /// Given a collection of server connections, query for the topic metadata.
        /// </summary>
        /// <param name="connections">The server connections to query.  Will cycle through the collection, starting at zero until a response is received.</param>
        /// <param name="topics">The collection of topics to get metadata for.</param>
        /// <returns>MetadataResponse validated to be complete.</returns>
        public MetadataResponse Get(IKafkaConnection[] connections, IEnumerable <string> topics)
        {
            var request = new MetadataRequest {
                Topics = topics.ToList()
            };

            if (request.Topics.Count <= 0)
            {
                return(null);
            }

            var performRetry = false;
            var retryAttempt = 0;
            MetadataResponse metadataResponse = null;

            do
            {
                performRetry     = false;
                metadataResponse = GetMetadataResponse(connections, request);
                if (metadataResponse == null)
                {
                    return(null);
                }

                foreach (var validation in ValidateResponse(metadataResponse))
                {
                    switch (validation.Status)
                    {
                    case ValidationResult.Retry:
                        performRetry = true;
                        _log.WarnFormat(validation.Message);
                        break;

                    case ValidationResult.Error:
                        throw validation.Exception;
                    }
                }

                BackoffOnRetry(++retryAttempt, performRetry);
            } while (_interrupted == false && performRetry);

            return(metadataResponse);
        }
コード例 #2
0
        private MetadataResponse GetMetadataResponse(IKafkaConnection[] connections, MetadataRequest request)
        {
            //try each default broker until we find one that is available
            foreach (var conn in connections)
            {
                try
                {
                    var response = conn.SendAsync(request).Result;
                    if (response != null && response.Count > 0)
                    {
                        return(response.FirstOrDefault());
                    }
                }
                catch (Exception ex)
                {
                    _log.WarnFormat("Failed to contact Kafka server={0}.  Trying next default server.  Exception={1}", conn.Endpoint, ex);
                }
            }

            throw new ServerUnreachableException(
                      "Unable to query for metadata from any of the default Kafka servers.  At least one provided server must be available.  Server list: {0}",
                      string.Join(", ", connections.Select(x => x.ToString())));
        }