Author: markrmiller
Date: Fri Jan 1 17:27:23 2010
New Revision: 895037
URL: http://svn.apache.org/viewvc?rev=895037&view=rev
Log:
Add a ZooKeeperConnection class to handle universal volatile access to
ZooKeeper - for reconnections.
Added:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperConnection.java
Removed:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/CountdownWatcher.java
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ReconnectionHandler.java
Modified:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperController.java
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperReader.java
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperWriter.java
lucene/solr/branches/cloud/src/webapp/web/admin/zookeeper.jsp
Added:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperConnection.java
URL:
http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperConnection.java?rev=895037&view=auto
==============================================================================
---
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperConnection.java
(added)
+++
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperConnection.java
Fri Jan 1 17:27:23 2010
@@ -0,0 +1,177 @@
+package org.apache.solr.cloud;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.Watcher.Event.KeeperState;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.data.Stat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ZooKeeperConnection {
+ private static final int CONNECT_TIMEOUT = 5000;
+
+ protected static final Logger log = LoggerFactory
+ .getLogger(ZooKeeperConnection.class);
+
+ private String zooKeeperHost;
+
+ private int zkClientTimeout;
+
+ boolean connected = false;
+
+ private CountdownWatcher cw = new CountdownWatcher("ZooKeeperConnection
Watcher");
+
+ private volatile ZooKeeper keeper;
+
+ public ZooKeeperConnection(String zooKeeperHost, int zkClientTimeout) {
+ this.zooKeeperHost = zooKeeperHost;
+ this.zkClientTimeout = zkClientTimeout;
+ }
+
+ public void connect() throws InterruptedException, TimeoutException,
+ IOException {
+ // nocommit
+ log.info("Connecting to ZooKeeper...");
+
+ keeper = new ZooKeeper(zooKeeperHost, zkClientTimeout, cw);
+ cw.waitForConnected(CONNECT_TIMEOUT);
+
+ // nocommit
+ log.info("Connected");
+ }
+
+ public boolean connected() {
+ return keeper.getState() == ZooKeeper.States.CONNECTED;
+ }
+
+ class CountdownWatcher implements Watcher {
+
+ private final String name;
+ private CountDownLatch clientConnected;
+ private KeeperState state;
+ private boolean connected;
+
+ public CountdownWatcher(String name) {
+ this.name = name;
+ reset();
+ }
+
+ private synchronized void reset() {
+ clientConnected = new CountDownLatch(1);
+ state = KeeperState.Disconnected;
+ connected = false;
+ }
+
+ public synchronized void process(WatchedEvent event) {
+ if (log.isInfoEnabled()) {
+ log.info("Watcher " + name + " got event " + event);
+ }
+
+ state = event.getState();
+ if (state == KeeperState.SyncConnected) {
+ connected = true;
+ clientConnected.countDown();
+ } else if (state == KeeperState.Expired) {
+ connected = false;
+ log.info("Attempting to reconnect to ZooKeeper...");
+ boolean connected = true;
+
+ // nocommit : close old ZooKeeper client?
+
+ try {
+ connect();
+ } catch (InterruptedException e) {
+ // Restore the interrupted status
+ Thread.currentThread().interrupt();
+ connected = false;
+ } catch (TimeoutException e) {
+ connected = false;
+ } catch (IOException e) {
+ // nocommit
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ log.info("Connected:" + connected);
+ // nocommit: start reconnect attempts
+ } else if (state == KeeperState.Disconnected) {
+ connected = false;
+ // nocommit: start reconnect attempts
+ } else {
+ connected = false;
+ }
+ notifyAll();
+ }
+
+ public synchronized boolean isConnected() {
+ return connected;
+ }
+
+ public synchronized KeeperState state() {
+ return state;
+ }
+
+ public synchronized void waitForConnected(long timeout)
+ throws InterruptedException, TimeoutException {
+ long expire = System.currentTimeMillis() + timeout;
+ long left = timeout;
+ while (!connected && left > 0) {
+ wait(left);
+ left = expire - System.currentTimeMillis();
+ }
+ if (!connected) {
+ throw new TimeoutException("Did not connect");
+ }
+ }
+
+ public synchronized void waitForDisconnected(long timeout)
+ throws InterruptedException, TimeoutException {
+ long expire = System.currentTimeMillis() + timeout;
+ long left = timeout;
+ while (connected && left > 0) {
+ wait(left);
+ left = expire - System.currentTimeMillis();
+ }
+ if (connected) {
+ throw new TimeoutException("Did not disconnect");
+ }
+ }
+ }
+
+ public Stat exists(final String path, Watcher watcher)
+ throws KeeperException, InterruptedException {
+ return keeper.exists(path, watcher);
+ }
+
+ public String create(final String path, byte data[], List<ACL> acl,
+ CreateMode createMode) throws KeeperException, InterruptedException {
+ return keeper.create(path, data, acl, createMode);
+ }
+
+ public List<String> getChildren(final String path, Watcher watcher)
+ throws KeeperException, InterruptedException {
+ return keeper.getChildren(path, watcher);
+ }
+
+ public byte[] getData(final String path, Watcher watcher, Stat stat)
+ throws KeeperException, InterruptedException {
+ return keeper.getData(path, watcher, stat);
+ }
+
+ public Stat setData(final String path, byte data[], int version)
+ throws KeeperException, InterruptedException {
+ return keeper.setData(path, data, version);
+ }
+
+ public void close() throws InterruptedException {
+ keeper.close();
+ }
+}
Modified:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperController.java
URL:
http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperController.java?rev=895037&r1=895036&r2=895037&view=diff
==============================================================================
---
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperController.java
(original)
+++
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperController.java
Fri Jan 1 17:27:23 2010
@@ -33,7 +33,6 @@
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
-import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -97,39 +96,14 @@
private final static Pattern URL_PREFIX = Pattern.compile("(https?://).*");
- private final ReconnectionHandler RECONNECTION_HANDLER = new
ReconnectionHandler() {
-
- @Override
- public boolean handleReconnect() throws IOException {
- // nocommit : reconnection experimentation
- log.info("Attempting to reconnect to ZooKeeper...");
- boolean connected = true;
- CountdownWatcher countdownWatcher = new CountdownWatcher(
- "ZooKeeperController", RECONNECTION_HANDLER);
- // nocommit : close old ZooKeeper client?
- keeper = new ZooKeeper(zooKeeperHost, zkClientTimeout, countdownWatcher);
- try {
- countdownWatcher.waitForConnected(5000);
- } catch (InterruptedException e) {
- // Restore the interrupted status
- Thread.currentThread().interrupt();
- connected = false;
- } catch (TimeoutException e) {
- connected = false;
- }
- log.info("Connected:" + connected);
- return connected;
- }
- };
-
private static Logger log = LoggerFactory
.getLogger(ZooKeeperController.class);
// nocommit : consider reconnects more closely
- private volatile ZooKeeper keeper;
+ private volatile ZooKeeperConnection keeperConnection;
- ZooKeeper getKeeper() {
- return keeper;
+ ZooKeeperConnection getKeeper() {
+ return keeperConnection;
}
private ZooKeeperReader zkReader;
@@ -151,8 +125,6 @@
private String configName;
- private int zkClientTimeout;
-
private String zooKeeperHostName;
@@ -172,8 +144,8 @@
this.zooKeeperHost = zooKeeperHost;
this.hostPort = hostPort;
this.hostContext = hostContext;
- this.zkClientTimeout = zkClientTimeout;
-
+ keeperConnection = new ZooKeeperConnection(zooKeeperHost, zkClientTimeout);
+
shardsZkPath = COLLECTIONS_ZKNODE + collectionName + SHARDS_ZKNODE;
init();
@@ -182,14 +154,11 @@
private void init() {
try {
-
- CountdownWatcher countdownWatcher = new CountdownWatcher(
- "ZooKeeperController", RECONNECTION_HANDLER);
- keeper = new ZooKeeper(zooKeeperHost, zkClientTimeout, countdownWatcher);
- countdownWatcher.waitForConnected(5000);
-
- zkReader = new ZooKeeperReader(keeper);
- zkWriter = new ZooKeeperWriter(keeper);
+ keeperConnection.connect();
+
+ // nocommit : consider losing these and having everything on
ZooKeeperConnection
+ zkReader = new ZooKeeperReader(keeperConnection);
+ zkWriter = new ZooKeeperWriter(keeperConnection);
configName = zkReader.readConfigName(collectionName);
@@ -265,7 +234,7 @@
*/
public void close() {
try {
- keeper.close();
+ keeperConnection.close();
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
Modified:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperReader.java
URL:
http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperReader.java?rev=895037&r1=895036&r2=895037&view=diff
==============================================================================
---
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperReader.java
(original)
+++
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperReader.java
Fri Jan 1 17:27:23 2010
@@ -58,16 +58,16 @@
private static final String COLLECTIONS_ZKNODE = "/collections/";
- private ZooKeeper keeper;
+ private ZooKeeperConnection keeperConnection;
private boolean closeKeeper;
+
/**
- * @param zooKeeper
+ * @param keeperConnection
*/
- ZooKeeperReader(ZooKeeper zooKeeper) {
- this.keeper = zooKeeper;
- // this.configName = readConfigName(collection);
+ ZooKeeperReader(ZooKeeperConnection keeperConnection) {
+ this.keeperConnection = keeperConnection;
}
/**
@@ -83,15 +83,9 @@
ZooKeeperReader(String zooKeeperHost, int zkClientTimeout)
throws IOException, InterruptedException, TimeoutException {
closeKeeper = true;
- CountdownWatcher countdownWatcher = new CountdownWatcher(
- "ZooKeeperController", new ReconnectionHandler() {
- @Override
- public boolean handleReconnect() throws IOException {
- return false;
- }
- });
- keeper = new ZooKeeper(zooKeeperHost, zkClientTimeout, countdownWatcher);
- countdownWatcher.waitForConnected(5000);
+
+ keeperConnection = new ZooKeeperConnection(zooKeeperHost, zkClientTimeout);
+ keeperConnection.connect();
}
/**
@@ -103,7 +97,7 @@
public boolean exists(String path) {
Object exists = null;
try {
- exists = keeper.exists(path, null);
+ exists = keeperConnection.exists(path, null);
} catch (KeeperException e) {
log.error("ZooKeeper Exception", e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
@@ -116,7 +110,7 @@
public void close() throws InterruptedException {
if (closeKeeper) {
- keeper.close();
+ keeperConnection.close();
}
}
@@ -149,7 +143,7 @@
}
public byte[] getConfigFileData(String zkConfigName, String fileName) throws
KeeperException, InterruptedException {
- return keeper.getData(CONFIGS_ZKNODE + zkConfigName, null, null);
+ return keeperConnection.getData(CONFIGS_ZKNODE + zkConfigName, null, null);
}
/**
@@ -169,7 +163,7 @@
if (log.isInfoEnabled()) {
log.info("Reading " + fileName + " from zookeeper at " + configPath);
}
- bytes = keeper.getData(configPath, false, null);
+ bytes = keeperConnection.getData(configPath, null, null);
return bytes;
}
@@ -206,8 +200,8 @@
*/
public void printLayout(String path, int indent, StringBuilder string)
throws KeeperException, InterruptedException {
- byte[] data = keeper.getData(path, null, null);
- List<String> children = keeper.getChildren(path, false);
+ byte[] data = keeperConnection.getData(path, null, null);
+ List<String> children = keeperConnection.getChildren(path, null);
StringBuilder dent = new StringBuilder();
for (int i = 0; i < indent; i++) {
dent.append(" ");
@@ -258,7 +252,7 @@
if (log.isInfoEnabled()) {
log.info("Load collection config from:" + path);
}
- List<String> children = keeper.getChildren(path, null);
+ List<String> children = keeperConnection.getChildren(path, null);
for (String node : children) {
// nocommit
System.out.println("check child:" + node);
@@ -297,10 +291,10 @@
throw new IllegalStateException("Cannot find zk node that should exist:"
+ path);
}
- List<String> nodes = keeper.getChildren(path, null);
+ List<String> nodes = keeperConnection.getChildren(path, null);
for (String zkNodeName : nodes) {
- byte[] data = keeper.getData(path + "/" + zkNodeName, null, null);
+ byte[] data = keeperConnection.getData(path + "/" + zkNodeName, null,
null);
Properties props = new Properties();
props.load(new ByteArrayInputStream(data));
@@ -340,11 +334,11 @@
* @throws InterruptedException
*/
public Stat stat(String path) throws KeeperException, InterruptedException {
- return keeper.exists(path, null);
+ return keeperConnection.exists(path, null);
}
public boolean configFileExists(String configName, String fileName) throws
KeeperException, InterruptedException {
- Stat stat = keeper.exists(CONFIGS_ZKNODE + configName, null);
+ Stat stat = keeperConnection.exists(CONFIGS_ZKNODE + configName, null);
return stat != null;
}
Modified:
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperWriter.java
URL:
http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperWriter.java?rev=895037&r1=895036&r2=895037&view=diff
==============================================================================
---
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperWriter.java
(original)
+++
lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZooKeeperWriter.java
Fri Jan 1 17:27:23 2010
@@ -36,7 +36,7 @@
public class ZooKeeperWriter {
private static Logger log = LoggerFactory.getLogger(ZooKeeperWriter.class);
- private ZooKeeper keeper;
+ private ZooKeeperConnection keeperConnection;
private boolean closeKeeper;
@@ -52,21 +52,17 @@
ZooKeeperWriter(String zooKeeperHost, int zkClientTimeout)
throws IOException, InterruptedException, TimeoutException {
closeKeeper = true;
- CountdownWatcher countdownWatcher = new
CountdownWatcher("ZooKeeperWriter", new ReconnectionHandler() {
- @Override
- public boolean handleReconnect() throws IOException {
- return false;
- }
- });
- keeper = new ZooKeeper(zooKeeperHost, zkClientTimeout, countdownWatcher);
- countdownWatcher.waitForConnected(5000);
+
+ keeperConnection = new ZooKeeperConnection(zooKeeperHost, zkClientTimeout);
+ keeperConnection.connect();
}
+
/**
- * @param keeper
+ * @param keeperConnection
*/
- ZooKeeperWriter(ZooKeeper keeper) {
- this.keeper = keeper;
+ ZooKeeperWriter(ZooKeeperConnection keeperConnection) {
+ this.keeperConnection = keeperConnection;
}
/**
@@ -78,7 +74,7 @@
*/
public void close() throws InterruptedException {
if (closeKeeper) {
- keeper.close();
+ keeperConnection.close();
}
}
@@ -94,10 +90,10 @@
public String makeEphemeralSeqPath(String path, byte[] data,
Watcher watcher) throws KeeperException, InterruptedException {
- String zkPath = keeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,
+ String zkPath = keeperConnection.create(path, data,
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
- keeper.exists(zkPath, watcher);
+ keeperConnection.exists(zkPath, watcher);
return zkPath;
}
@@ -176,21 +172,21 @@
String pathPiece = paths[i];
sbPath.append("/" + pathPiece);
String currentPath = sbPath.toString();
- Object exists = keeper.exists(currentPath, watcher);
+ Object exists = keeperConnection.exists(currentPath, watcher);
if (exists == null) {
CreateMode mode = CreateMode.PERSISTENT;
if (i == paths.length - 1) {
mode = createMode;
bytes = data;
}
- keeper.create(currentPath, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
+ keeperConnection.create(currentPath, bytes,
ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
// set new watch
- keeper.exists(currentPath, watcher);
+ keeperConnection.exists(currentPath, watcher);
} else if (i == paths.length - 1) {
// nocommit: version ?
- keeper.setData(currentPath, data, -1);
+ keeperConnection.setData(currentPath, data, -1);
// set new watch
- keeper.exists(currentPath, watcher);
+ keeperConnection.exists(currentPath, watcher);
}
}
}
@@ -220,11 +216,11 @@
makePath(path);
- Object exists = keeper.exists(path, null);
+ Object exists = keeperConnection.exists(path, null);
if (exists != null) {
- keeper.setData(path, data, -1);
+ keeperConnection.setData(path, data, -1);
} else {
- keeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,
+ keeperConnection.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
}
Modified: lucene/solr/branches/cloud/src/webapp/web/admin/zookeeper.jsp
URL:
http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/webapp/web/admin/zookeeper.jsp?rev=895037&r1=895036&r2=895037&view=diff
==============================================================================
--- lucene/solr/branches/cloud/src/webapp/web/admin/zookeeper.jsp (original)
+++ lucene/solr/branches/cloud/src/webapp/web/admin/zookeeper.jsp Fri Jan 1
17:27:23 2010
@@ -15,8 +15,7 @@
See the License for the specific language governing permissions and
limitations under the License.
--%>
-<%@ page
import="javax.servlet.jsp.JspWriter,java.io.IOException,org.apache.zookeeper.*,org.apache.zookeeper.data.Stat,org.apache.solr.core.*,org.apache.solr.cloud.*,org.apache.solr.common.util.*
- "%>
+<%@ page
import="javax.servlet.jsp.JspWriter,java.io.IOException,org.apache.zookeeper.*,org.apache.zookeeper.data.Stat,org.apache.solr.core.*,org.apache.solr.cloud.*,org.apache.solr.common.util.*,java.util.concurrent.TimeoutException"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.net.URLEncoder"%>
@@ -29,24 +28,29 @@
<%
String path = request.getParameter("path");
String addr = request.getParameter("addr");
- if (addr!=null && addr.length()==0) addr=null;
+ if (addr != null && addr.length() == 0)
+ addr = null;
String detailS = request.getParameter("detail");
- boolean detail = detailS!=null && detailS.equals("true");
+ boolean detail = detailS != null && detailS.equals("true");
ZKPrinter printer = new ZKPrinter(out, core, addr);
printer.detail = detail;
- String tryAddr = printer.keeperAddr!=null ? printer.keeperAddr :
"localhost:2181";
+ String tryAddr = printer.keeperAddr != null ? printer.keeperAddr
+ : "localhost:2181";
%>
<form method="GET" action="zookeeper.jsp" accept-charset="UTF-8">
<table>
<tr>
<td>
- <strong> <% XML.escapeCharData(printer.keeper==null ? "Disconnected" :
("Connected to zookeeper "+printer.keeperAddr), out); %> </strong>
+ <strong> <%
+ XML.escapeCharData(printer.keeperConnection == null ? "Disconnected"
+ : ("Connected to zookeeper " + printer.keeperAddr), out);
+ %> </strong>
</td>
<td>
<strong>Connect to different zookeeper</strong>
- <input class="std" name="addr" type="text" value="<%
XML.escapeCharData(tryAddr, out); %>">
+ <input class="std" name="addr" type="text"
value="<%XML.escapeCharData(tryAddr, out);%>">
</td>
<td>
<input class="stdbutton" type="submit" value="CONNECT">
@@ -63,89 +67,89 @@
</body>
</html>
-<%!
+<%!static class ZKPrinter {
+ static boolean FULLPATH_DEFAULT = true;
+ boolean fullpath = FULLPATH_DEFAULT;
- static class ZKPrinter {
- static boolean FULLPATH_DEFAULT=true;
- boolean fullpath=FULLPATH_DEFAULT;
- boolean detail=false;
- String addr; // the address passed to us
- String keeperAddr; // the address we're connected to
+ boolean detail = false;
+
+ String addr; // the address passed to us
+
+ String keeperAddr; // the address we're connected to
+
+ ZooKeeperConnection keeperConnection;
- ZooKeeper keeper;
JspWriter out;
+
int level;
+
int maxData = 60;
private boolean levelchange;
- public ZKPrinter(JspWriter out, SolrCore core, String addr) throws
IOException {
+ public ZKPrinter(JspWriter out, SolrCore core, String addr)
+ throws IOException {
this.out = out;
this.addr = addr;
- // ZooKeeperController controller =
core.getCoreDescriptor().getCoreContainer().getZooKeeperController();
- ZooKeeperController controller = null;
-
- if (controller == null) {
- keeperAddr = addr;
- if (addr == null) {
- out.println("Zookeeper is not configured for this Solr Core. Please
try connecting to an alternate zookeeper address.");
- return;
- }
-
- try {
- keeper = new ZooKeeper(addr, 10000, null);
- } catch (IOException e) {
- out.println("Could not connect to zookeeper at " + addr);
- return;
- }
+ keeperAddr = addr;
+ if (addr == null) {
+ out
+ .println("Zookeeper is not configured for this Solr Core. Please
try connecting to an alternate zookeeper address.");
+ return;
+ }
+
+ keeperConnection = new ZooKeeperConnection(addr, 10000);
- try {
- Thread.sleep(2000); // temporary hack to wait until connected
- } catch (InterruptedException e) {
- exception(e);
- }
+ try {
+ keeperConnection.connect();
+ } catch (TimeoutException e) {
+ out.println("Could not connect to zookeeper at " + addr);
+ return;
+ } catch (InterruptedException e) {
+ // Restore the interrupted status
+ Thread.currentThread().interrupt();
+ }
- if (keeper.getState() != ZooKeeper.States.CONNECTED) {
- out.println("Could not connect to zookeeper at " + addr);
- keeper = null;
- return;
- }
- } else {
- keeper = controller.getZooKeeper();
- keeperAddr = controller.getZooKeeperHost();
+ if (!keeperConnection.connected()) {
+ out.println("Could not connect to zookeeper at " + addr);
+ keeperConnection = null;
+ return;
}
}
-
// main entry point
void print(String path) throws IOException {
- if (keeper==null) return;
+ if (keeperConnection == null)
+ return;
out.print("<table>");
out.print("<tr><td>");
out.print("[");
- url("ROOT","/",false);
+ url("ROOT", "/", false);
out.print("]");
// normalize path
- if (path==null) path="/";
+ if (path == null)
+ path = "/";
else {
path.trim();
- if (path.length()==0) path="/";
+ if (path.length() == 0)
+ path = "/";
}
- if (path.endsWith("/") && path.length()>1) {
- path = path.substring(0, path.length()-1);
+ if (path.endsWith("/") && path.length() > 1) {
+ path = path.substring(0, path.length() - 1);
}
int idx = path.lastIndexOf('/');
String parent = idx >= 0 ? path.substring(0, idx) : path;
- if (parent.length()==0) parent="/";
+ if (parent.length() == 0)
+ parent = "/";
out.print(" [");
- url("PARENT",parent,detail);
+ url("PARENT", parent, detail);
out.print("]");
out.print("</td></tr>");
@@ -162,8 +166,6 @@
out.print("</table>");
}
-
-
void exception(Exception e) {
try {
out.println(e.toString());
@@ -180,40 +182,43 @@
}
}
-
void up() throws IOException {
level++;
- if (!fullpath) out.println("<BLOCKQUOTE>");
- levelchange=true;
+ if (!fullpath)
+ out.println("<BLOCKQUOTE>");
+ levelchange = true;
}
void down() throws IOException {
level--;
- if (!fullpath) out.println("</BLOCKQUOTE>");
- levelchange=true;
+ if (!fullpath)
+ out.println("</BLOCKQUOTE>");
+ levelchange = true;
}
void indent() throws IOException {
// if we are using blockquote and just changed indent levels, don't
output a break
- if (fullpath || !levelchange) out.println("<br>");
+ if (fullpath || !levelchange)
+ out.println("<br>");
levelchange = false;
// if fullpath, no indent is needed
// if not, we are currently using blockquote which the browser
// will take care of indenting.
}
-
// collapse all whitespace to a single space or escaped newline
String compress(String str) {
StringBuilder sb = new StringBuilder();
- for (int i=0; i<str.length(); i++) {
+ for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
boolean whitespace = false;
boolean newline = false;
while (Character.isWhitespace(ch)) {
whitespace = true;
- if (ch=='\n') newline=true;
- if (++i >=str.length()) return sb.toString();
+ if (ch == '\n')
+ newline = true;
+ if (++i >= str.length())
+ return sb.toString();
ch = str.charAt(i);
}
@@ -226,7 +231,8 @@
// TODO: handle non-printable chars
sb.append(ch);
- if (sb.length() >= maxData) return sb.toString() + "...";
+ if (sb.length() >= maxData)
+ return sb.toString() + "...";
}
return sb.toString();
}
@@ -236,17 +242,17 @@
out.print("<a href=\"zookeeper.jsp?");
if (path != null) {
out.print("path=");
- out.print(URLEncoder.encode(path,"UTF-8"));
+ out.print(URLEncoder.encode(path, "UTF-8"));
}
if (detail) {
- out.print("&detail="+detail);
+ out.print("&detail=" + detail);
}
if (fullpath != FULLPATH_DEFAULT) {
- out.print("&fullpath="+fullpath);
+ out.print("&fullpath=" + fullpath);
}
if (addr != null) {
out.print("&addr=");
- out.print(URLEncoder.encode(addr,"UTF-8"));
+ out.print(URLEncoder.encode(addr, "UTF-8"));
}
out.print("\">");
@@ -258,8 +264,6 @@
}
}
-
-
void printTree(String path) throws IOException {
indent();
@@ -269,7 +273,7 @@
String label = path;
if (!fullpath) {
int idx = path.lastIndexOf('/');
- label = idx > 0 ? path.substring(idx+1) : path;
+ label = idx > 0 ? path.substring(idx + 1) : path;
}
url(label, path, true);
@@ -278,7 +282,7 @@
Stat stat = new Stat();
try {
- byte[] data = keeper.getData(path, null, stat);
+ byte[] data = keeperConnection.getData(path, null, stat);
out.print("v=" + stat.getVersion());
if (stat.getNumChildren() != 0) {
@@ -295,18 +299,19 @@
} catch (UnsupportedEncodingException e) {
// not UTF8
StringBuilder sb = new StringBuilder("BIN(");
- sb.append("len="+data.length);
+ sb.append("len=" + data.length);
sb.append("hex=");
- int limit = Math.min(data.length, maxData/2);
- for (int i=0; i<limit; i++) {
+ int limit = Math.min(data.length, maxData / 2);
+ for (int i = 0; i < limit; i++) {
byte b = data[i];
- sb.append(StrUtils.HEX_DIGITS[(b>>4)&0xf]);
- sb.append(StrUtils.HEX_DIGITS[b&0xf]);
+ sb.append(StrUtils.HEX_DIGITS[(b >> 4) & 0xf]);
+ sb.append(StrUtils.HEX_DIGITS[b & 0xf]);
}
- if (limit != data.length) sb.append("...");
+ if (limit != data.length)
+ sb.append("...");
sb.append(")");
str = sb.toString();
- out.print(" d="+str);
+ out.print(" d=" + str);
}
}
@@ -314,8 +319,7 @@
} catch (IllegalArgumentException e) {
// path doesn't exist (must have been removed)
out.println("(path gone)");
- }
- catch (KeeperException e) {
+ } catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
@@ -323,11 +327,12 @@
out.println("]");
- if (stat.getNumChildren() <= 0) return;
+ if (stat.getNumChildren() <= 0)
+ return;
List<String> children = null;
try {
- children = keeper.getChildren(path, null);
+ children = keeperConnection.getChildren(path, null);
} catch (KeeperException e) {
exception(e);
return;
@@ -340,7 +345,7 @@
up();
for (String child : children) {
- String childPath = path + (path.endsWith("/")?"":"/") + child;
+ String childPath = path + (path.endsWith("/") ? "" : "/") + child;
printTree(childPath);
}
down();
@@ -350,24 +355,35 @@
try {
Stat stat = new Stat();
- byte[] data = keeper.getData(path, null, stat);
+ byte[] data = keeperConnection.getData(path, null, stat);
out.print("<h2>");
xmlescape(path);
out.print("</h2>");
up();
- indent(); out.print("version=" + stat.getVersion());
- indent(); out.print("aversion=" + stat.getAversion());
- indent(); out.print("cversion=" + stat.getCversion());
- indent(); out.print("ctime=" + stat.getCtime());
- indent(); out.print("mtime=" + stat.getMtime());
- indent(); out.print("czxid=" + stat.getCzxid());
- indent(); out.print("mzxid=" + stat.getMzxid());
- indent(); out.print("pzxid=" + stat.getPzxid());
- indent(); out.print("numChildren=" + stat.getNumChildren());
- indent(); out.print("ephemeralOwner=" + stat.getEphemeralOwner());
- indent(); out.print("dataLength=" + stat.getDataLength());
+ indent();
+ out.print("version=" + stat.getVersion());
+ indent();
+ out.print("aversion=" + stat.getAversion());
+ indent();
+ out.print("cversion=" + stat.getCversion());
+ indent();
+ out.print("ctime=" + stat.getCtime());
+ indent();
+ out.print("mtime=" + stat.getMtime());
+ indent();
+ out.print("czxid=" + stat.getCzxid());
+ indent();
+ out.print("mzxid=" + stat.getMzxid());
+ indent();
+ out.print("pzxid=" + stat.getPzxid());
+ indent();
+ out.print("numChildren=" + stat.getNumChildren());
+ indent();
+ out.print("ephemeralOwner=" + stat.getEphemeralOwner());
+ indent();
+ out.print("dataLength=" + stat.getDataLength());
if (data != null) {
boolean isBinary = false;
@@ -376,21 +392,22 @@
str = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
// not UTF8
- StringBuilder sb = new StringBuilder(data.length*2);
- for (int i=0; i<data.length; i++) {
+ StringBuilder sb = new StringBuilder(data.length * 2);
+ for (int i = 0; i < data.length; i++) {
byte b = data[i];
- sb.append(StrUtils.HEX_DIGITS[(b>>4)&0xf]);
- sb.append(StrUtils.HEX_DIGITS[b&0xf]);
- if ((i&0x3f)==0x3f) sb.append("\n");
+ sb.append(StrUtils.HEX_DIGITS[(b >> 4) & 0xf]);
+ sb.append(StrUtils.HEX_DIGITS[b & 0xf]);
+ if ((i & 0x3f) == 0x3f)
+ sb.append("\n");
}
str = sb.toString();
}
int nLines = 1;
int lineLen = 0;
- int maxLineLen = 10; // the minimum
- for (int i=0; i<str.length(); i++) {
- if (str.charAt(i)=='\n') {
+ int maxLineLen = 10; // the minimum
+ for (int i = 0; i < str.length(); i++) {
+ if (str.charAt(i) == '\n') {
nLines++;
maxLineLen = Math.max(maxLineLen, lineLen);
lineLen = 0;
@@ -401,10 +418,11 @@
indent();
out.println("<form method='post' action=''>");
- out.println("<textarea class='big' wrap='off' readonly rows='" +
Math.min(20,nLines)
-// + "' cols='" + Math.min(80, maxLineLen+1)
-// + "' cols='" + (maxLineLen+1)
- + "' name='data'>");
+ out.println("<textarea class='big' wrap='off' readonly rows='"
+ + Math.min(20, nLines)
+ // + "' cols='" + Math.min(80, maxLineLen+1)
+ // + "' cols='" + (maxLineLen+1)
+ + "' name='data'>");
xmlescape(str);
@@ -420,5 +438,4 @@
exception(e);
}
}
- }
-%>
\ No newline at end of file
+ }%>
\ No newline at end of file