Search in sources :

Example 1 with ZooKeeperClient

use of com.twitter.common.zookeeper.ZooKeeperClient in project commons by twitter.

the class ZooKeeperTestServer method createClient.

/**
   * Returns a new authenticated zookeeper client connected to the in-process zookeeper server with
   * a custom {@code sessionTimeout} and a custom {@code chrootPath}.
   */
public final ZooKeeperClient createClient(Amount<Integer, Time> sessionTimeout, Credentials credentials, Optional<String> chrootPath) {
    final ZooKeeperClient client = new ZooKeeperClient(sessionTimeout, credentials, chrootPath, Arrays.asList(InetSocketAddress.createUnresolved("127.0.0.1", port)));
    shutdownRegistry.addAction(new ExceptionalCommand<InterruptedException>() {

        @Override
        public void execute() {
            client.close();
        }
    });
    return client;
}
Also used : ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient)

Example 2 with ZooKeeperClient

use of com.twitter.common.zookeeper.ZooKeeperClient in project commons by twitter.

the class ServerSetImplTest method testUnwatchOnException.

@Test
public void testUnwatchOnException() throws Exception {
    IMocksControl control = createControl();
    ZooKeeperClient zkClient = control.createMock(ZooKeeperClient.class);
    Watcher onExpirationWatcher = control.createMock(Watcher.class);
    expect(zkClient.registerExpirationHandler(anyObject(Command.class))).andReturn(onExpirationWatcher);
    expect(zkClient.get()).andThrow(new InterruptedException());
    expect(zkClient.unregister(onExpirationWatcher)).andReturn(true);
    control.replay();
    Group group = new Group(zkClient, ZooDefs.Ids.OPEN_ACL_UNSAFE, "/blabla");
    ServerSetImpl serverset = new ServerSetImpl(zkClient, group);
    try {
        serverset.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {

            @Override
            public void onChange(ImmutableSet<ServiceInstance> hostSet) {
            }
        });
        fail("Expected MonitorException");
    } catch (DynamicHostSet.MonitorException e) {
    // expected
    }
    control.verify();
}
Also used : Group(com.twitter.common.zookeeper.Group) Watcher(org.apache.zookeeper.Watcher) ServiceInstance(com.twitter.thrift.ServiceInstance) DynamicHostSet(com.twitter.common.net.pool.DynamicHostSet) IMocksControl(org.easymock.IMocksControl) ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient) Command(com.twitter.common.base.Command) Override(java.lang.Override) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 3 with ZooKeeperClient

use of com.twitter.common.zookeeper.ZooKeeperClient in project commons by twitter.

the class AngryBirdZooKeeperTest method testEndpointExpiration.

@Test(expected = KeeperException.class)
public void testEndpointExpiration() throws Exception {
    // Create some sequential nodes.
    ZooKeeperClient zkClient1 = createZKClient("1@1234:81".getBytes());
    ZooKeeperClient zkClient2 = createZKClient("2@1234:82".getBytes());
    ZooKeeperClient zkClient3 = createZKClient("3@1235:81".getBytes());
    // Expire the leader's session.
    zkServer.expireEndpoint("1234", 81);
    // Make sure non-matching sessions are not expired.
    zkClient2.get().exists(zkPaths.get(zkClient2), null);
    zkClient3.get().exists(zkPaths.get(zkClient3), null);
    // Check that the matching session has expired.
    zkClient1.get().exists(zkPaths.get(zkClient1), null);
    fail("Matching session has not expired!");
}
Also used : ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient) Test(org.junit.Test)

Example 4 with ZooKeeperClient

use of com.twitter.common.zookeeper.ZooKeeperClient in project commons by twitter.

the class AngryBirdZooKeeperTest method testFollowerExpirationById.

@Test
public void testFollowerExpirationById() throws Exception {
    // Create some sequential nodes.
    ZooKeeperClient zkClient1 = createZKClient("1@1234:81".getBytes());
    ZooKeeperClient zkClient2 = createZKClient("2@1234:82".getBytes());
    ZooKeeperClient zkClient3 = createZKClient("3@1235:81".getBytes());
    ZooKeeperClient zkClient4 = createZKClient("4@1235:81".getBytes());
    // Expire the 3rd follower's session.
    zkServer.expireFollower(ROOT_PATH, Optional.of("3"));
    // Make sure the expected sessions non expired.
    zkClient1.get().exists(zkPaths.get(zkClient1), null);
    zkClient2.get().exists(zkPaths.get(zkClient2), null);
    zkClient4.get().exists(zkPaths.get(zkClient4), null);
    boolean failed = false;
    // Check that only one of the followers's sessions has expired.
    try {
        zkClient3.get().exists(zkPaths.get(zkClient3), null);
    } catch (KeeperException e) {
        failed = true;
    }
    assertTrue(failed);
}
Also used : ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 5 with ZooKeeperClient

use of com.twitter.common.zookeeper.ZooKeeperClient in project distributedlog by twitter.

the class DLZkServerSet method of.

public static DLZkServerSet of(URI uri, int zkSessionTimeoutMs) {
    // Create zookeeper and server set
    String zkPath = uri.getPath() + "/" + ZNODE_WRITE_PROXY;
    Iterable<InetSocketAddress> zkAddresses = getZkAddresses(uri);
    ZooKeeperClient zkClient = new ZooKeeperClient(Amount.of(zkSessionTimeoutMs, Time.MILLISECONDS), zkAddresses);
    ServerSet serverSet = ServerSets.create(zkClient, ZooDefs.Ids.OPEN_ACL_UNSAFE, zkPath);
    return new DLZkServerSet(zkClient, serverSet);
}
Also used : ServerSet(com.twitter.common.zookeeper.ServerSet) ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

ZooKeeperClient (com.twitter.common.zookeeper.ZooKeeperClient)9 Test (org.junit.Test)5 KeeperException (org.apache.zookeeper.KeeperException)2 Command (com.twitter.common.base.Command)1 DynamicHostSet (com.twitter.common.net.pool.DynamicHostSet)1 Group (com.twitter.common.zookeeper.Group)1 ServerSet (com.twitter.common.zookeeper.ServerSet)1 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)1 ServiceInstance (com.twitter.thrift.ServiceInstance)1 Override (java.lang.Override)1 InetSocketAddress (java.net.InetSocketAddress)1 Watcher (org.apache.zookeeper.Watcher)1 IMocksControl (org.easymock.IMocksControl)1