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();
}
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("/");
}
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);
}
}
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);
}
}
Aggregations