def add_brokers_from_zk(cluster, zk): for b in zk.get_children("/brokers/ids"): broker_data, bstat = zk.get("/brokers/ids/{0}".format(b)) cluster.add_broker(Broker.create_from_json(int(b), broker_data)) if cluster.num_brokers() == 0: raise ZookeeperException( "The cluster specified does not have any brokers")
def test_broker_create_from_json_extended(self): jsonstr = '{"jmx_port":-1,"timestamp":"1466985807242","endpoints":["PLAINTEXT://10.0.0.10:9092"],"host":"10.0.0.10","version":3,"port":9092}' broker2 = Broker.create_from_json(1, jsonstr) assert broker2.jmx_port == -1 assert broker2.timestamp == "1466985807242" assert broker2.endpoints == ["PLAINTEXT://10.0.0.10:9092"] assert broker2.version == 3 assert broker2.port == 9092
def create_from_zookeeper(cls, zkconnect): log.info("Connecting to zookeeper {0}".format(zkconnect)) try: zk = KazooClient(zkconnect) zk.start() except Exception as e: raise ZookeeperException("Cannot connect to Zookeeper: {0}".format(e)) # Get broker list cluster = cls() for b in zk.get_children("/brokers/ids"): broker_data, bstat = zk.get("/brokers/ids/{0}".format(b)) cluster.add_broker(Broker.create_from_json(int(b), broker_data)) if cluster.num_brokers() == 0: raise ZookeeperException("The cluster specified does not have any brokers") # Get current partition state log.info("Getting partition list from Zookeeper") for topic in zk.get_children("/brokers/topics"): zdata, zstat = zk.get("/brokers/topics/{0}".format(topic)) zj = json.loads(zdata) newtopic = Topic(topic, len(zj['partitions'])) for partition in zj['partitions']: for i, replica in enumerate(zj['partitions'][partition]): if replica not in cluster.brokers: # Hit a replica that's not in the ID list (which means it's dead) # We'll add it, but trying to get sizes will fail as we don't have a hostname cluster.add_broker(Broker(replica, None)) newtopic.partitions[int(partition)].add_replica(cluster.brokers[replica], i) cluster.add_topic(newtopic) if cluster.num_topics() == 0: raise ZookeeperException("The cluster specified does not have any topics") log.info("Closing connection to zookeeper") zk.stop() zk.close() return cluster
def test_broker_create_from_json_bad_jmx_port(self): jsonstr = '{"timestamp":"1466985807242","endpoints":["PLAINTEXT://10.0.0.10:9092"],"host":"10.0.0.10","version":3,"port":9092}' broker2 = Broker.create_from_json(1, jsonstr) assert broker2.hostname == '10.0.0.10'
def add_brokers_from_zk(cluster, zk): for b in zk.get_children("/brokers/ids"): broker_data, bstat = zk.get("/brokers/ids/{0}".format(b)) cluster.add_broker(Broker.create_from_json(int(b), broker_data)) if cluster.num_brokers() == 0: raise ZookeeperException("The cluster specified does not have any brokers")