Search in sources :

Example 1 with ZookeeperCacheLoader

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

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;
    ZookeeperCacheLoader zkLoader = new ZookeeperCacheLoader(new DiscoveryZooKeeperClientFactoryImpl(), "");
    List<String> brokers = Lists.newArrayList("broker-1:15000", "broker-2:15000", "broker-3:15000");
    // 1. create znode for each broker
    brokers.stream().forEach(b -> {
        try {
            zkLoader.getLocalZkCache().getZooKeeper().create(LOADBALANCE_BROKERS_ROOT + "/" + b, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException | InterruptedException e) {
            fail("failed while creating broker znodes");
        }
    });
    // wait for 100 msec: to get cache updated
    Thread.sleep(100);
    // 2. get available brokers from ZookeeperCacheLoader
    List<LoadReport> list = zkLoader.getAvailableBrokers();
    // 3. verify retrieved broker list
    Assert.assertTrue(brokers.containsAll(list));
    // 4.a add new broker
    zkLoader.getLocalZkCache().getZooKeeper().create(LOADBALANCE_BROKERS_ROOT + "/" + "broker-4:15000", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    brokers.add("broker-4:15000");
    // 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
    Assert.assertTrue(brokers.containsAll(list));
}
Also used : ZookeeperCacheLoader(com.yahoo.pulsar.discovery.service.web.ZookeeperCacheLoader) DiscoveryZooKeeperClientFactoryImpl(com.yahoo.pulsar.discovery.service.web.BaseZKStarterTest.DiscoveryZooKeeperClientFactoryImpl) LoadReport(com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 2 with ZookeeperCacheLoader

use of com.yahoo.pulsar.discovery.service.web.ZookeeperCacheLoader 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

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