From 39757948fe74c930ebb0d5fe4026868f8953a9fd Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Thu, 20 Sep 2012 17:31:19 +0000 Subject: [PATCH] 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 --- .../solr/common/cloud/SolrZooKeeper.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZooKeeper.java b/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZooKeeper.java index 0b3c8f56c42..c4212ef09ea 100644 --- a/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZooKeeper.java +++ b/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZooKeeper.java @@ -18,11 +18,10 @@ package org.apache.solr.common.cloud; */ import java.io.IOException; -import java.nio.channels.SocketChannel; -import java.util.Iterator; +import java.lang.reflect.Field; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import org.apache.zookeeper.ClientCnxn; @@ -46,6 +45,16 @@ public class SolrZooKeeper extends ZooKeeper { 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 * for the given number of milliseconds. @@ -57,17 +66,12 @@ public class SolrZooKeeper extends ZooKeeper { try { synchronized (cnxn) { try { - // nocommit: reflect me, move me somewehre else as static method, - // something. i am only used by tests! the rest of this file is clean. - ((SocketChannel) cnxn.sendThread.sockKey.channel()).socket() - .close(); + getSendThreadChannel().close(); } catch (Exception e) { + throw new RuntimeException("Closing zookeper send channel failed.", e); } Thread.sleep(ms); } - - // Wait a long while to make sure we properly clean up these threads. - Thread.sleep(500000); } catch (InterruptedException e) {} } }; @@ -77,9 +81,8 @@ public class SolrZooKeeper extends ZooKeeper { @Override public synchronized void close() throws InterruptedException { - //clients.remove(this); for (Thread t : spawnedThreads) { - t.interrupt(); + if (t.isAlive()) t.interrupt(); } super.close(); }