use of org.apache.pulsar.discovery.service.web.ZookeeperCacheLoader.LOADBALANCE_BROKERS_ROOT in project incubator-pulsar by apache.
the class ZookeeperCacheLoaderTest method testZookeeperCacheLoader.
/**
* Create znode for available broker in ZooKeeper and updates it again to verify ZooKeeper cache update
*
* @throws InterruptedException
* @throws KeeperException
* @throws IOException
*/
@Test
public void testZookeeperCacheLoader() throws InterruptedException, KeeperException, Exception {
DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
@SuppressWarnings("resource") ZookeeperCacheLoader zkLoader = new ZookeeperCacheLoader(new DiscoveryZooKeeperClientFactoryImpl(), "", 30_000);
List<String> brokers = Lists.newArrayList("broker-1:15000", "broker-2:15000", "broker-3:15000");
for (int i = 0; i < brokers.size(); i++) {
try {
LoadManagerReport report = i % 2 == 0 ? getSimpleLoadManagerLoadReport(brokers.get(i)) : getModularLoadManagerLoadReport(brokers.get(i));
zkLoader.getLocalZkCache().getZooKeeper().create(LOADBALANCE_BROKERS_ROOT + "/" + brokers.get(i), ObjectMapperFactory.getThreadLocal().writeValueAsBytes(report), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (Exception e) {
fail("failed while creating broker znodes");
}
}
// strategically wait for cache to get sync
for (int i = 0; i < 5; i++) {
if (zkLoader.getAvailableBrokers().size() == 3 || i == 4) {
break;
}
Thread.sleep(1000);
}
// 2. get available brokers from ZookeeperCacheLoader
List<LoadManagerReport> list = zkLoader.getAvailableBrokers();
// 3. verify retrieved broker list
Set<String> cachedBrokers = list.stream().map(loadReport -> loadReport.getWebServiceUrl()).collect(Collectors.toSet());
Assert.assertEquals(list.size(), brokers.size());
Assert.assertTrue(brokers.containsAll(cachedBrokers));
// 4.a add new broker
final String newBroker = "broker-4:15000";
LoadManagerReport report = getSimpleLoadManagerLoadReport(newBroker);
zkLoader.getLocalZkCache().getZooKeeper().create(LOADBALANCE_BROKERS_ROOT + "/" + newBroker, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(report), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
brokers.add(newBroker);
// wait for 100 msec: to get cache updated
Thread.sleep(100);
// 4.b. get available brokers from ZookeeperCacheLoader
list = zkLoader.getAvailableBrokers();
// 4.c. verify retrieved broker list
cachedBrokers = list.stream().map(loadReport -> loadReport.getWebServiceUrl()).collect(Collectors.toSet());
Assert.assertEquals(list.size(), brokers.size());
Assert.assertTrue(brokers.containsAll(cachedBrokers));
}
Aggregations