Search in sources :

Example 1 with ClientCnxn

use of org.apache.zookeeper.ClientCnxn in project lucene-solr by apache.

the class SolrZooKeeper method closeCnxn.

public void closeCnxn() {
    final Thread t = new Thread() {

        @Override
        public void run() {
            try {
                AccessController.doPrivileged((PrivilegedAction<Void>) this::closeZookeeperChannel);
            } finally {
                spawnedThreads.remove(this);
            }
        }

        @SuppressForbidden(reason = "Hack for Zookeper needs access to private methods.")
        private Void closeZookeeperChannel() {
            final ClientCnxn cnxn = getConnection();
            synchronized (cnxn) {
                try {
                    final Field sendThreadFld = cnxn.getClass().getDeclaredField("sendThread");
                    sendThreadFld.setAccessible(true);
                    Object sendThread = sendThreadFld.get(cnxn);
                    if (sendThread != null) {
                        Method method = sendThread.getClass().getDeclaredMethod("testableCloseSocket");
                        method.setAccessible(true);
                        try {
                            method.invoke(sendThread);
                        } catch (InvocationTargetException e) {
                        // is fine
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException("Closing Zookeeper send channel failed.", e);
                }
            }
            // Void
            return null;
        }
    };
    spawnedThreads.add(t);
    t.start();
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Method(java.lang.reflect.Method) ClientCnxn(org.apache.zookeeper.ClientCnxn) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ClientCnxn

use of org.apache.zookeeper.ClientCnxn in project otter by alibaba.

the class ZooKeeperClientTest method testClient.

@Test
public void testClient() {
    ZkClientx zk = ZooKeeperClient.getInstance();
    // 强制获取zk中的地址信息
    final ZooKeeper zkp = ((ZooKeeperx) zk.getConnection()).getZookeeper();
    ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zkp);
    HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
    List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
    want.number(serverAddrs.size()).isEqualTo(3);
    String s1 = serverAddrs.get(0).getAddress().getHostAddress() + ":" + serverAddrs.get(0).getPort();
    want.string(s1).isEqualTo(cluster1);
    Stat stat = new Stat();
    try {
        zkp.getChildren("/otter/channel/304/388", false, stat);
        System.out.println(stat.getCversion());
    } catch (KeeperException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (InterruptedException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    // 测试下session timeout
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {

        public void run() {
            try {
                zkp.getChildren("/", false);
            } catch (KeeperException e1) {
                want.fail();
            } catch (InterruptedException e1) {
                want.fail();
            }
            int sessionTimeout = zkp.getSessionTimeout();
            long sessionId = zkp.getSessionId();
            byte[] passwd = zkp.getSessionPasswd();
            try {
                ZooKeeper newZk = new ZooKeeper(cluster1, sessionTimeout, new Watcher() {

                    public void process(WatchedEvent event) {
                    // do nothing
                    }
                }, sessionId, passwd);
                // 用老的sessionId连接上去,进行一次close操作后,让原先正在使用的出现SESSION_EXPIRED
                newZk.close();
            } catch (IOException e) {
                want.fail();
            } catch (InterruptedException e) {
                want.fail();
            }
            latch.countDown();
        }
    }.start();
    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }
    zk.getChildren("/");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) ZkClientx(com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx) ClientCnxn(org.apache.zookeeper.ClientCnxn) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) ZooKeeperx(com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.arbitrate.BaseOtterTest)

Example 3 with ClientCnxn

use of org.apache.zookeeper.ClientCnxn in project canal by alibaba.

the class ZooKeeperx method configMutliCluster.

// ===============================
public void configMutliCluster(ZooKeeper zk) {
    if (_serversList.size() == 1) {
        return;
    }
    String cluster1 = _serversList.get(0);
    try {
        if (_serversList.size() > 1) {
            // 强制的声明accessible
            ReflectionUtils.makeAccessible(clientCnxnField);
            ReflectionUtils.makeAccessible(hostProviderField);
            ReflectionUtils.makeAccessible(serverAddressesField);
            // 添加第二组集群列表
            for (int i = 1; i < _serversList.size(); i++) {
                String cluster = _serversList.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) ZkException(org.I0Itec.zkclient.exception.ZkException) IOException(java.io.IOException)

Example 4 with ClientCnxn

use of org.apache.zookeeper.ClientCnxn 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)

Aggregations

IOException (java.io.IOException)4 ClientCnxn (org.apache.zookeeper.ClientCnxn)4 InetSocketAddress (java.net.InetSocketAddress)3 List (java.util.List)3 HostProvider (org.apache.zookeeper.client.HostProvider)3 StaticHostProvider (org.apache.zookeeper.client.StaticHostProvider)3 ZkException (org.I0Itec.zkclient.exception.ZkException)2 KeeperException (org.apache.zookeeper.KeeperException)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 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 PrivilegedAction (java.security.PrivilegedAction)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