SOLR-3733: Fix crazy zookeeper test-only stuff by reflection.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/solr3733@1388122 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-09-20 17:31:19 +00:00
parent 8b6c905e7c
commit 39757948fe
1 changed files with 16 additions and 13 deletions

View File

@ -18,11 +18,10 @@ package org.apache.solr.common.cloud;
*/ */
import java.io.IOException; import java.io.IOException;
import java.nio.channels.SocketChannel; import java.lang.reflect.Field;
import java.util.Iterator; import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.zookeeper.ClientCnxn; import org.apache.zookeeper.ClientCnxn;
@ -46,6 +45,16 @@ public class SolrZooKeeper extends ZooKeeper {
return cnxn; return cnxn;
} }
SelectableChannel getSendThreadChannel() throws Exception {
final Field sendThreadFld = cnxn.getClass().getDeclaredField("sendThread");
sendThreadFld.setAccessible(true);
Object sendThread = sendThreadFld.get(cnxn);
final Field sockKeyFld = sendThread.getClass().getDeclaredField("sockKey");
sockKeyFld.setAccessible(true);
final SelectionKey sockKey = (SelectionKey) sockKeyFld.get(sendThread);
return sockKey.channel();
}
/** /**
* Cause this ZooKeeper object to stop receiving from the ZooKeeperServer * Cause this ZooKeeper object to stop receiving from the ZooKeeperServer
* for the given number of milliseconds. * for the given number of milliseconds.
@ -57,17 +66,12 @@ public class SolrZooKeeper extends ZooKeeper {
try { try {
synchronized (cnxn) { synchronized (cnxn) {
try { try {
// nocommit: reflect me, move me somewehre else as static method, getSendThreadChannel().close();
// something. i am only used by tests! the rest of this file is clean.
((SocketChannel) cnxn.sendThread.sockKey.channel()).socket()
.close();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Closing zookeper send channel failed.", e);
} }
Thread.sleep(ms); Thread.sleep(ms);
} }
// Wait a long while to make sure we properly clean up these threads.
Thread.sleep(500000);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
}; };
@ -77,9 +81,8 @@ public class SolrZooKeeper extends ZooKeeper {
@Override @Override
public synchronized void close() throws InterruptedException { public synchronized void close() throws InterruptedException {
//clients.remove(this);
for (Thread t : spawnedThreads) { for (Thread t : spawnedThreads) {
t.interrupt(); if (t.isAlive()) t.interrupt();
} }
super.close(); super.close();
} }