Search in sources :

Example 1 with DiscoveryServiceServlet

use of com.yahoo.pulsar.discovery.service.web.DiscoveryServiceServlet in project pulsar by yahoo.

the class DiscoveryServiceWebTest method testNextBroker.

@Test
public void testNextBroker() throws Exception {
    // 1. create znode for each broker
    List<String> brokers = Lists.newArrayList("broker-1", "broker-2", "broker-3");
    brokers.stream().forEach(broker -> {
        try {
            LoadReport report = new LoadReport(broker, null, null, null);
            String reportData = ObjectMapperFactory.getThreadLocal().writeValueAsString(report);
            ZkUtils.createFullPathOptimistic(mockZookKeeper, LOADBALANCE_BROKERS_ROOT + "/" + broker, reportData.getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        }
    });
    // 2. Setup discovery-zkcache
    DiscoveryServiceServlet discovery = new DiscoveryServiceServlet();
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Field zkCacheField = DiscoveryServiceServlet.class.getDeclaredField("zkCache");
    zkCacheField.setAccessible(true);
    ZookeeperCacheLoader zkCache = new ZookeeperCacheLoader(new DiscoveryZooKeeperClientFactoryImpl(), "zk-test-servers");
    zkCacheField.set(discovery, zkCache);
    // 3. verify nextBroker functionality : round-robin in broker list
    for (String broker : brokers) {
        assertEquals(broker, discovery.nextBroker().getWebServiceUrl());
    }
}
Also used : Field(java.lang.reflect.Field) ZookeeperCacheLoader(com.yahoo.pulsar.discovery.service.web.ZookeeperCacheLoader) LoadReport(com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport) DiscoveryServiceServlet(com.yahoo.pulsar.discovery.service.web.DiscoveryServiceServlet) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 LoadReport (com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport)1 DiscoveryServiceServlet (com.yahoo.pulsar.discovery.service.web.DiscoveryServiceServlet)1 ZookeeperCacheLoader (com.yahoo.pulsar.discovery.service.web.ZookeeperCacheLoader)1 Field (java.lang.reflect.Field)1 KeeperException (org.apache.zookeeper.KeeperException)1 Test (org.testng.annotations.Test)1