Exemple #1
0
    def start(self):
        """Start consumer."""
        # Step 1. Create our consumer ID.
        path = '/consumers/%s/ids/%s' % (self.group_id, self.consumer_id)
        data = json.dumps(self.subscribed)
        self.znode = self.kazoo.create(path,
                                       value=data,
                                       ephemeral=True,
                                       makepath=True)

        # Step 2: Start monitoring for consumers of this group.
        consumer_path = '/consumers/%s/ids' % (self.group_id, )
        rebalance = Rebalancer(self)
        zkmonitor(self.kazoo,
                  consumer_path,
                  into=self.clients,
                  watch=rebalance)

        # Step 3: Start monitoring for brokers.
        broker_path = '/brokers/ids'
        zkmonitor(self.kazoo,
                  broker_path,
                  into=self.brokers,
                  watch=rebalance,
                  factory=broker.broker_factory)

        # Step 4: Start the global rebalance greenlet.
        self.rebalance_greenlet = gevent.spawn(self._rebalance)
 def test_zkmonitor_uses_factory(self):
     """Check that the factory function is used on data."""
     factory = Mock(return_value=67)
     monitor.zkmonitor(self.client, self.path, self.data, factory=factory)
     child_path = os.path.join(self.path, "child1")
     self.client.create(child_path, "73")
     keep_trying(lambda: factory.assert_called_once_with("73"))
     self.assertEquals(self.data["child1"], 67)
 def test_zkmonitor_uses_factory(self):
     """Check that the factory function is used on data."""
     factory = Mock(return_value=67)
     monitor.zkmonitor(self.client, self.path, self.data, factory=factory)
     child_path = os.path.join(self.path, "child1")
     self.client.create(child_path, "73")
     keep_trying(lambda: factory.assert_called_once_with("73"))
     self.assertEquals(self.data["child1"], 67)
 def test_zkmonitor_runs_watch(self):
     """Verify that the given watch callback is called on changes."""
     watch = Mock()
     monitor.zkmonitor(self.client, self.path, self.data, watch=watch)
     child_path = os.path.join(self.path, "child1")
     self.client.create(child_path, '{"some": "data"}')
     self.client.set(os.path.join(self.path, "child1"),
                     '{"some": "potato"}')
     keep_trying(watch.assert_called_once_with)
 def test_zkmonitor_runs_watch(self):
     """Verify that the given watch callback is called on changes."""
     watch = Mock()
     monitor.zkmonitor(self.client, self.path, self.data, watch=watch)
     child_path = os.path.join(self.path, "child1")
     self.client.create(child_path, '{"some": "data"}')
     self.client.set(os.path.join(self.path, "child1"),
                     '{"some": "potato"}')
     keep_trying(watch.assert_called_once_with)
Exemple #6
0
 def start(self, callback):
     """Start consuming the topic."""
     self.callback = callback
     self.consumer._add_topic(self.topic_name, self)
     partitions_path = '/brokers/topics/%s' % (self.topic_name,)
     zkmonitor(self.kazoo, partitions_path,
               into=self.partitions,
               watch=Rebalancer(self),
               factory=int)
     self.rebalance_greenlet = gevent.spawn(self._rebalance)
Exemple #7
0
    def start(self):
        """Start the producer."""
        broker_path = '/brokers/ids'
        zkmonitor(self.kazoo,
                  broker_path,
                  into=self.brokers,
                  factory=broker.broker_factory)

        topic_path = '/brokers/topics/%s' % (self.topic_name)
        zkmonitor(self.kazoo, topic_path, into=self.topic_parts, factory=int)
 def test_zkmonitor_notices_children(self):
     """Check that new child nodes are noticed."""
     monitor.zkmonitor(self.client, self.path, self.data)
     self.client.create(os.path.join(self.path, "child1"),
                        '{"data": "foo"}')
     self.client.create(os.path.join(self.path, "child2"),
                        '{"data": "bar"}')
     keep_trying(lambda: self.assertEquals(self.data.get("child2"),
                                           dict(data="bar")))
     keep_trying(lambda: self.assertEquals(self.data.get("child1"),
                                           dict(data="foo")))
Exemple #9
0
    def start(self):
        """Start the producer."""
        broker_path = '/brokers/ids'
        zkmonitor(self.kazoo, broker_path,
                  into=self.brokers,
                  factory=broker.broker_factory)

        topic_path = '/brokers/topics/%s' % (self.topic_name)
        zkmonitor(self.kazoo, topic_path,
                  into=self.topic_parts,
                  factory=int)
Exemple #10
0
 def start(self, callback):
     """Start consuming the topic."""
     self.callback = callback
     self.consumer._add_topic(self.topic_name, self)
     partitions_path = '/brokers/topics/%s' % (self.topic_name, )
     zkmonitor(self.kazoo,
               partitions_path,
               into=self.partitions,
               watch=Rebalancer(self),
               factory=int)
     self.rebalance_greenlet = gevent.spawn(self._rebalance)
Exemple #11
0
 def test_zkmonitor_notices_children(self):
     """Check that new child nodes are noticed."""
     monitor.zkmonitor(self.client, self.path, self.data)
     self.client.create(os.path.join(self.path, "child1"),
                        '{"data": "foo"}')
     self.client.create(os.path.join(self.path, "child2"),
                        '{"data": "bar"}')
     keep_trying(lambda: self.assertEquals(self.data.get("child2"),
                                           dict(data="bar")))
     keep_trying(lambda: self.assertEquals(self.data.get("child1"),
                                           dict(data="foo")))
Exemple #12
0
    def start(self):
        """Start consumer."""
        # Step 1. Create our consumer ID.
        path = '/consumers/%s/ids/%s' % (self.group_id, self.consumer_id)
        data = json.dumps(self.subscribed)
        self.znode = self.kazoo.create(path, value=data, ephemeral=True,
                                       makepath=True)

        # Step 2: Start monitoring for consumers of this group.
        consumer_path = '/consumers/%s/ids' % (self.group_id,)
        rebalance = Rebalancer(self)
        zkmonitor(self.kazoo, consumer_path,
                  into=self.clients,
                  watch=rebalance)

        # Step 3: Start monitoring for brokers.
        broker_path = '/brokers/ids'
        zkmonitor(self.kazoo, broker_path,
                  into=self.brokers,
                  watch=rebalance,
                  factory=broker.broker_factory)

        # Step 4: Start the global rebalance greenlet.
        self.rebalance_greenlet = gevent.spawn(self._rebalance)
 def test_zkmonitor_creates_path(self):
     """Check that the monitor creates the base node if needed."""
     self.assertFalse(self.client.exists(self.path))
     monitor.zkmonitor(self.client, self.path, {})
     self.assertTrue(self.client.exists(self.path))
Exemple #14
0
 def test_zkmonitor_creates_path(self):
     """Check that the monitor creates the base node if needed."""
     self.assertFalse(self.client.exists(self.path))
     monitor.zkmonitor(self.client, self.path, {})
     self.assertTrue(self.client.exists(self.path))