Exemplo n.º 1
0
    def test_switch_leader(self):
        producer = Producer(self.client)
        topic = self.topic
        try:
            for index in range(1, 3):
                # cause the client to establish connections to all the brokers
                log.debug("Pass: %d. Sending 10 random messages", index)
                yield self._send_random_messages(producer, topic, 10)

                # Ensure that the follower is in sync
                log.debug("Ensuring topic/partition is replicated.")
                part_meta = self.client.partition_meta[TopicAndPartition(
                    self.topic, 0)]
                # Ensure the all the replicas are in-sync before proceeding
                while len(part_meta.isr) != 2:  # pragma: no cover
                    log.debug("Waiting for Kafka replica to become synced")
                    if len(part_meta.replicas) != 2:
                        log.error(
                            "Kafka replica 'disappeared'!"
                            "Partitition Meta: %r", part_meta)
                    yield async_delay(1.0)
                    yield self.client.load_metadata_for_topics(self.topic)
                    part_meta = self.client.partition_meta[TopicAndPartition(
                        self.topic, 0)]

                # kill leader for partition 0
                log.debug("Killing leader of partition 0")
                broker, kill_time = self._kill_leader(topic, 0)

                log.debug("Sending 1 more message: 'part 1'")
                yield producer.send_messages(topic, msgs=['part 1'])
                log.debug("Sending 1 more message: 'part 2'")
                yield producer.send_messages(topic, msgs=['part 2'])

                # send to new leader
                log.debug("Sending 10 more messages")
                yield self._send_random_messages(producer, topic, 10)

                # Make sure the ZK ephemeral time (~6 seconds) has elapsed
                wait_time = (kill_time + 6.5) - time.time()
                if wait_time > 0:
                    log.debug("Waiting: %4.2f for ZK timeout", wait_time)
                    yield async_delay(wait_time)
                # restart the kafka broker
                log.debug("Restarting the broker")
                broker.restart()

                # count number of messages
                log.debug("Getting message count")
                count = yield self._count_messages(topic)
                self.assertEqual(count, 22 * index)
        finally:
            log.debug("Stopping the producer")
            yield producer.stop()
            log.debug("Producer stopped")

        log.debug("Test complete.")
Exemplo n.º 2
0
 def topic_info():
     if topic_name in client.topic_partitions:
         return "Topic {} exists. Partition metadata: {}".format(
             topic_name, pformat([client.partition_meta[TopicAndPartition(topic_name, part)]
                                  for part in client.topic_partitions[topic_name]]),
         )
     else:
         return "No metadata for topic {} found.".format(topic_name)
Exemplo n.º 3
0
 def _kill_leader(self, topic, partition):
     leader = self.client.topics_to_brokers[TopicAndPartition(
         topic, partition)]
     broker = self.kafka_brokers[leader.node_id]
     broker.stop()
     return (broker, time.time())