Search in sources :

Example 6 with HostProvider

use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testNextGoesRoundAndSleeps.

@Test
public void testNextGoesRoundAndSleeps() {
    byte size = 2;
    HostProvider hostProvider = getHostProvider(size);
    while (size > 0) {
        hostProvider.next(0);
        --size;
    }
    long start = Time.currentElapsedTime();
    hostProvider.next(1000);
    long stop = Time.currentElapsedTime();
    assertTrue(900 <= stop - start);
}
Also used : StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Test(org.junit.Test)

Example 7 with HostProvider

use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testUpdateMigrationGoesRound.

@Test
public void testUpdateMigrationGoesRound() throws UnknownHostException {
    HostProvider hostProvider = getHostProvider((byte) 4);
    // old list (just the ports): 1238, 1237, 1236, 1235
    Collection<InetSocketAddress> newList = new ArrayList<InetSocketAddress>(10);
    for (byte i = 12; i > 2; i--) {
        // 1246, 1245, 1244, 1243, 1242, 1241,
        // 1240, 1239, 1238, 1237
        newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
    }
    // servers from the old list that appear in the new list
    Collection<InetSocketAddress> oldStaying = new ArrayList<InetSocketAddress>(2);
    for (byte i = 4; i > 2; i--) {
        // 1238, 1237
        oldStaying.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
    }
    // servers in the new list that are not in the old list
    Collection<InetSocketAddress> newComing = new ArrayList<InetSocketAddress>(10);
    for (byte i = 12; i > 4; i--) {
        // 1246, 1245, 1244, 1243, 1242, 1241, 1240, 1139
        newComing.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
    }
    // Number of machines increases, my server is not in the new cluster
    // load on old servers must be decreased, so must connect to one of the
    // new servers
    // i.e., pNew = 1.
    boolean disconnectRequired = hostProvider.updateServerList(newList, new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, 1 }), 1235));
    assertTrue(disconnectRequired);
    // This means reconfigMode = true, and nextHostInReconfigMode will be
    // called from next
    // Since pNew = 1 we should first try the new servers
    ArrayList<InetSocketAddress> seen = new ArrayList<InetSocketAddress>();
    for (int i = 0; i < newComing.size(); i++) {
        InetSocketAddress addr = hostProvider.next(0);
        assertTrue(newComing.contains(addr));
        assertTrue(!seen.contains(addr));
        seen.add(addr);
    }
    // Next the old servers
    seen.clear();
    for (int i = 0; i < oldStaying.size(); i++) {
        InetSocketAddress addr = hostProvider.next(0);
        assertTrue(oldStaying.contains(addr));
        assertTrue(!seen.contains(addr));
        seen.add(addr);
    }
    // And now it goes back to normal next() so it should be everything
    // together like in testNextGoesRound()
    InetSocketAddress first = hostProvider.next(0);
    assertTrue(first != null);
    for (int i = 0; i < newList.size() - 1; i++) {
        hostProvider.next(0);
    }
    assertEquals(first, hostProvider.next(0));
    hostProvider.onConnected();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with HostProvider

use of org.apache.zookeeper.client.HostProvider in project canal by alibaba.

the class ZooKeeperx method configMutliCluster.

// ===============================
public void configMutliCluster(ZooKeeper zk) {
    if (_servers.size() == 1) {
        return;
    }
    String cluster1 = _servers.get(0);
    try {
        if (_servers.size() > 1) {
            // 强制的声明accessible
            ReflectionUtils.makeAccessible(clientCnxnField);
            ReflectionUtils.makeAccessible(hostProviderField);
            ReflectionUtils.makeAccessible(serverAddressesField);
            // 添加第二组集群列表
            for (int i = 1; i < _servers.size(); i++) {
                String cluster = _servers.get(i);
                // 强制获取zk中的地址信息
                ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zk);
                HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
                List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
                // 添加第二组集群列表
                serverAddrs.addAll(new ConnectStringParser(cluster).getServerAddresses());
            }
        }
    } catch (Exception e) {
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (InterruptedException ie) {
        // ignore interrupt
        }
        throw new ZkException("zookeeper_create_error, serveraddrs=" + cluster1, e);
    }
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser) ZkException(org.I0Itec.zkclient.exception.ZkException) InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) List(java.util.List) ClientCnxn(org.apache.zookeeper.ClientCnxn) KeeperException(org.apache.zookeeper.KeeperException) ZkException(org.I0Itec.zkclient.exception.ZkException) IOException(java.io.IOException)

Example 9 with HostProvider

use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.

the class ClientReconnectTest method testClientReconnect.

@Test
public void testClientReconnect() throws IOException, InterruptedException {
    HostProvider hostProvider = mock(HostProvider.class);
    when(hostProvider.size()).thenReturn(1);
    InetSocketAddress inaddr = new InetSocketAddress("127.0.0.1", 1111);
    when(hostProvider.next(anyLong())).thenReturn(inaddr);
    ZooKeeper zk = mock(ZooKeeper.class);
    when(zk.getClientConfig()).thenReturn(new ZKClientConfig());
    sc = SocketChannel.open();
    ClientCnxnSocketNIO nioCnxn = new MockCnxn();
    ClientWatchManager watcher = mock(ClientWatchManager.class);
    ClientCnxn clientCnxn = new ClientCnxn("tmp", hostProvider, 5000, zk, watcher, nioCnxn, false);
    clientCnxn.start();
    countDownLatch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(countDownLatch.getCount() == 0);
    clientCnxn.close();
}
Also used : ZKClientConfig(org.apache.zookeeper.client.ZKClientConfig) InetSocketAddress(java.net.InetSocketAddress) HostProvider(org.apache.zookeeper.client.HostProvider) Test(org.junit.Test)

Example 10 with HostProvider

use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testLiteralIPNoReverseNS.

@Test
public void testLiteralIPNoReverseNS() throws Exception {
    byte size = 30;
    HostProvider hostProvider = getHostProviderUnresolved(size);
    for (int i = 0; i < size; i++) {
        InetSocketAddress next = hostProvider.next(0);
        assertTrue(next instanceof InetSocketAddress);
        assertTrue(!next.isUnresolved());
        assertTrue(!next.toString().startsWith("/"));
        // Do NOT trigger the reverse name service lookup.
        String hostname = next.getHostString();
        // In this case, the hostname equals literal IP address.
        hostname.equals(next.getAddress().getHostAddress());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Test(org.junit.Test)

Aggregations

HostProvider (org.apache.zookeeper.client.HostProvider)14 StaticHostProvider (org.apache.zookeeper.client.StaticHostProvider)12 Test (org.junit.Test)11 InetSocketAddress (java.net.InetSocketAddress)10 IOException (java.io.IOException)3 List (java.util.List)3 ClientCnxn (org.apache.zookeeper.ClientCnxn)3 KeeperException (org.apache.zookeeper.KeeperException)3 ZkException (org.I0Itec.zkclient.exception.ZkException)2 ConnectStringParser (org.apache.zookeeper.client.ConnectStringParser)2 BaseOtterTest (com.alibaba.otter.shared.arbitrate.BaseOtterTest)1 ZkClientx (com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx)1 ZooKeeperx (com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Watcher (org.apache.zookeeper.Watcher)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 ZKClientConfig (org.apache.zookeeper.client.ZKClientConfig)1 Stat (org.apache.zookeeper.data.Stat)1