Search in sources :

Example 11 with HostProvider

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

the class StaticHostProviderTest method testOnConnectDoesNotReset.

@Test
public void testOnConnectDoesNotReset() {
    HostProvider hostProvider = getHostProvider((byte) 2);
    InetSocketAddress first = hostProvider.next(0);
    hostProvider.onConnected();
    InetSocketAddress second = hostProvider.next(0);
    assertNotSame(first, second);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Test(org.junit.Test)

Example 12 with HostProvider

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

the class StaticHostProviderTest method testUpdateLoadBalancing.

@Test
public void testUpdateLoadBalancing() throws UnknownHostException {
    // Start with 9 servers and 10000 clients
    boolean disconnectRequired;
    HostProvider[] hostProviderArray = new HostProvider[numClients];
    InetSocketAddress[] curHostForEachClient = new InetSocketAddress[numClients];
    int[] numClientsPerHost = new int[9];
    // initialization
    for (int i = 0; i < numClients; i++) {
        hostProviderArray[i] = getHostProvider((byte) 9);
        curHostForEachClient[i] = hostProviderArray[i].next(0);
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 9; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 9));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 9));
        // prepare for next test
        numClientsPerHost[i] = 0;
    }
    // remove host number 8 (the last one in a list of 9 hosts)
    Collection<InetSocketAddress> newList = getServerAddresses((byte) 8);
    for (int i = 0; i < numClients; i++) {
        disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 8; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 8));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 8));
        // prepare for next test
        numClientsPerHost[i] = 0;
    }
    assertTrue(numClientsPerHost[8] == 0);
    // remove hosts number 6 and 7 (the currently last two in the list)
    newList = getServerAddresses((byte) 6);
    for (int i = 0; i < numClients; i++) {
        disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 6; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 6));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 6));
        // prepare for next test
        numClientsPerHost[i] = 0;
    }
    assertTrue(numClientsPerHost[6] == 0);
    assertTrue(numClientsPerHost[7] == 0);
    assertTrue(numClientsPerHost[8] == 0);
    // remove host number 0 (the first one in the current list)
    // and add back hosts 6, 7 and 8
    newList = new ArrayList<InetSocketAddress>(8);
    for (byte i = 9; i > 1; i--) {
        newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
    }
    for (int i = 0; i < numClients; i++) {
        disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    assertTrue(numClientsPerHost[0] == 0);
    for (int i = 1; i < 9; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 8));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 8));
        // prepare for next test
        numClientsPerHost[i] = 0;
    }
    // add back host number 0
    newList = getServerAddresses((byte) 9);
    for (int i = 0; i < numClients; i++) {
        disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 9; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 9));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 9));
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Test(org.junit.Test)

Example 13 with HostProvider

use of org.apache.zookeeper.client.HostProvider in project otter 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 14 with HostProvider

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

the class StaticHostProviderTest method testUpdateClientMigrateOrNot.

@Test
public void testUpdateClientMigrateOrNot() throws UnknownHostException {
    // 10.10.10.4:1238, 10.10.10.3:1237, 10.10.10.2:1236, 10.10.10.1:1235
    HostProvider hostProvider = getHostProvider((byte) 4);
    // 10.10.10.3:1237, 10.10.10.2:1236, 10.10.10.1:1235
    Collection<InetSocketAddress> newList = getServerAddresses((byte) 3);
    InetSocketAddress myServer = new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, 3 }), 1237);
    // Number of machines becomes smaller, my server is in the new cluster
    boolean disconnectRequired = hostProvider.updateServerList(newList, myServer);
    assertTrue(!disconnectRequired);
    hostProvider.onConnected();
    // Number of machines stayed the same, my server is in the new cluster
    disconnectRequired = hostProvider.updateServerList(newList, myServer);
    assertTrue(!disconnectRequired);
    hostProvider.onConnected();
    // Number of machines became smaller, my server is not in the new
    // cluster
    // 10.10.10.2:1236, 10.10.10.1:1235
    newList = getServerAddresses((byte) 2);
    disconnectRequired = hostProvider.updateServerList(newList, myServer);
    assertTrue(disconnectRequired);
    hostProvider.onConnected();
    // Number of machines stayed the same, my server is not in the new
    // cluster
    disconnectRequired = hostProvider.updateServerList(newList, myServer);
    assertTrue(disconnectRequired);
    hostProvider.onConnected();
    // Number of machines increased, my server is not in the new cluster
    newList = new ArrayList<InetSocketAddress>(3);
    for (byte i = 4; i > 1; i--) {
        // 10.10.10.4:1238, 10.10.10.3:1237, 10.10.10.2:1236
        newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
    }
    myServer = new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, 1 }), 1235);
    disconnectRequired = hostProvider.updateServerList(newList, myServer);
    assertTrue(disconnectRequired);
    hostProvider.onConnected();
    // Number of machines increased, my server is in the new cluster
    // Here whether to move or not depends on the difference of cluster
    // sizes
    // With probability 1 - |old|/|new} the client disconnects
    // In the test below 1-9/10 = 1/10 chance of disconnecting
    HostProvider[] hostProviderArray = new HostProvider[numClients];
    newList = getServerAddresses((byte) 10);
    int numDisconnects = 0;
    for (int i = 0; i < numClients; i++) {
        hostProviderArray[i] = getHostProvider((byte) 9);
        disconnectRequired = hostProviderArray[i].updateServerList(newList, myServer);
        if (disconnectRequired)
            numDisconnects++;
    }
    hostProvider.onConnected();
    // should be numClients/10 in expectation, we test that its numClients/10 +- slackPercent
    assertTrue(numDisconnects < upperboundCPS(numClients, 10));
}
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