Пример #1
0
        public void TestTopicMetadataRequest()
        {
            // create topic
            var topic = "test";
            AdminUtils.CreateTopic(this.ZkClient, topic, 1, 1, new Dictionary<string, string>());

            // create a topic metadata request
            var topicMetadataRequest = new TopicMetadataRequest(new List<string> { topic }, 0);

            var serializedMetadataRequest = ByteBuffer.Allocate(topicMetadataRequest.SizeInBytes + 2);
            topicMetadataRequest.WriteTo(serializedMetadataRequest);
            serializedMetadataRequest.Rewind();
            var deserializedMetadataRequest = TopicMetadataRequest.ReadFrom(serializedMetadataRequest);

            Assert.Equal(topicMetadataRequest, deserializedMetadataRequest);
        }
Пример #2
0
        /// <summary>
        /// Used by the producer to send a metadata request since it has access to the ProducerConfig
        /// </summary>
        /// <param name="topics">The topics for which the metadata needs to be fetched</param>
        /// <param name="brokers">The brokers in the cluster as configured on the client</param>
        /// <param name="producerConfig">The producer's config</param>
        /// <param name="correlationId">topic metadata response</param>
        /// <returns></returns>
        public static TopicMetadataResponse FetchTopicMetadata(
            ISet<string> topics, IList<Broker> brokers, ProducerConfig producerConfig, int correlationId)
        {
            var fetchMetaDataSucceeded = false;
            var i = 0;
            var topicMetadataRequest = new TopicMetadataRequest(
                TopicMetadataRequest.CurrentVersion, correlationId, producerConfig.ClientId, topics.ToList());

            TopicMetadataResponse topicMetadataResponse = null;
            Exception t = null;

            // shuffle the list of brokers before sending metadata requests so that most requests don't get routed to the same broker
            var shuffledBrokers = brokers.Shuffle();
            while (i < shuffledBrokers.Count() && !fetchMetaDataSucceeded)
            {
                var producer = ProducerPool.CreateSyncProducer(producerConfig, shuffledBrokers[i]);
                Logger.InfoFormat("Fetching metadata from broker {0} with correlation id {1} for {2} topic(s) {3}", shuffledBrokers[i], correlationId, topics.Count, string.Join(",", topics));
                try
                {
                    topicMetadataResponse = producer.Send(topicMetadataRequest);
                    fetchMetaDataSucceeded = true;
                }
                catch (Exception e)
                {
                    Logger.Warn(string.Format("Fetching topic metadata with correlation id {0} for topic [{1}] from broker [{2}] failed", correlationId, topics, shuffledBrokers[i]), e);
                    t = e;
                }
                finally
                {
                    i++;
                    producer.Dispose();
                }
            }

            if (!fetchMetaDataSucceeded)
            {
                throw new KafkaException(
                    string.Format(
                        "fetching topic metadata for topics [{0}] from broker [{1}] failed", string.Join(",", topics), string.Join(", ", shuffledBrokers)),
                    t);
            }

            Logger.DebugFormat("Successfully fetched metadata for {0} topic(s) {1}", topics.Count(), string.Join(",",  topics));
            return topicMetadataResponse;
        }
Пример #3
0
 public TopicMetadataResponse Send(TopicMetadataRequest request)
 {
     var response = this.DoSend(request);
     return TopicMetadataResponse.ReadFrom(response.Buffer);
 }
Пример #4
0
 protected bool Equals(TopicMetadataRequest other)
 {
     return this.VersionId == other.VersionId && this.CorrelationId == other.CorrelationId
            && string.Equals(this.ClientId, other.ClientId) && this.Topics.SequenceEqual(other.Topics);
 }
 protected bool Equals(TopicMetadataRequest other)
 {
     return(this.VersionId == other.VersionId && this.CorrelationId == other.CorrelationId &&
            string.Equals(this.ClientId, other.ClientId) && this.Topics.SequenceEqual(other.Topics));
 }
Пример #6
0
 public RequestResponseSerializationTest()
 {
     this.producerRequest = SerializationTestUtils.CreateTestProducerRequest();
     this.producerResponse = SerializationTestUtils.CreateTestProducerResponse();
     this.fetchRequest = SerializationTestUtils.CreateTestFetchRequest();
     this.offsetRequest = SerializationTestUtils.CreateTestOffsetRequest();
     this.offsetResponse = SerializationTestUtils.CreateTestOffsetResponse();
     this.topicMetadataRequest = SerializationTestUtils.CreateTestTopicMetadataRequest();
     this.topicMetadataResponse = SerializationTestUtils.CreateTestTopicMetadataResponse();
 }