You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2011/01/30 18:29:57 UTC
svn commit: r1065327 [3/3] - in /lucene/dev/trunk: ./
lucene/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/
lucene/src/java/org/apache/lucene/queryParser/
lucene/src/java/org/apache/lucene/search/spans/
lucene/src/test/org/apache/lucene...
Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkControllerTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkControllerTest.java?rev=1065327&r1=1065326&r2=1065327&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkControllerTest.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkControllerTest.java Sun Jan 30 17:29:55 2011
@@ -1,225 +1,225 @@
-package org.apache.solr.cloud;
-
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. The ASF
- * licenses this file to You under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Map;
-
-import org.apache.solr.SolrTestCaseJ4;
-import org.apache.solr.common.cloud.CloudState;
-import org.apache.solr.common.cloud.Slice;
-import org.apache.solr.common.cloud.SolrZkClient;
-import org.apache.solr.common.cloud.ZkNodeProps;
-import org.apache.solr.common.cloud.ZkStateReader;
-import org.apache.solr.core.SolrConfig;
-import org.apache.zookeeper.CreateMode;
-import org.apache.zookeeper.KeeperException;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class ZkControllerTest extends SolrTestCaseJ4 {
-
- private static final String TEST_NODE_NAME = "test_node_name";
-
- private static final String URL3 = "http://localhost:3133/solr/core1";
-
- private static final String URL2 = "http://localhost:3123/solr/core1";
-
- private static final String SHARD3 = "localhost:3123_solr_core3";
-
- private static final String SHARD2 = "localhost:3123_solr_core2";
-
- private static final String SHARD1 = "localhost:3123_solr_core1";
-
- private static final String COLLECTION_NAME = "collection1";
-
- static final int TIMEOUT = 10000;
-
- private static final String URL1 = "http://localhost:3133/solr/core0";
-
- private static final boolean DEBUG = false;
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- initCore();
- }
-
- @Test
- public void testReadShards() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
- ZkTestServer server = null;
- SolrZkClient zkClient = null;
- ZkController zkController = null;
- try {
- server = new ZkTestServer(zkDir);
- server.run();
- AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
- AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
-
- zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
- String shardsPath = "/collections/collection1/shards/shardid1";
- zkClient.makePath(shardsPath);
-
- addShardToZk(zkClient, shardsPath, SHARD1, URL1);
- addShardToZk(zkClient, shardsPath, SHARD2, URL2);
- addShardToZk(zkClient, shardsPath, SHARD3, URL3);
-
- if (DEBUG) {
- zkClient.printLayoutToStdOut();
- }
-
- zkController = new ZkController(server.getZkAddress(),
- TIMEOUT, 1000, "localhost", "8983", "solr");
-
- zkController.getZkStateReader().updateCloudState(true);
- CloudState cloudInfo = zkController.getCloudState();
- Map<String,Slice> slices = cloudInfo.getSlices("collection1");
- assertNotNull(slices);
-
- for (Slice slice : slices.values()) {
- Map<String,ZkNodeProps> shards = slice.getShards();
- if (DEBUG) {
- for (String shardName : shards.keySet()) {
- ZkNodeProps props = shards.get(shardName);
- System.out.println("shard:" + shardName);
- System.out.println("props:" + props.toString());
- }
- }
- assertNotNull(shards.get(SHARD1));
- assertNotNull(shards.get(SHARD2));
- assertNotNull(shards.get(SHARD3));
-
- ZkNodeProps props = shards.get(SHARD1);
- assertEquals(URL1, props.get(ZkStateReader.URL_PROP));
- assertEquals(TEST_NODE_NAME, props.get(ZkStateReader.NODE_NAME));
-
- props = shards.get(SHARD2);
- assertEquals(URL2, props.get(ZkStateReader.URL_PROP));
- assertEquals(TEST_NODE_NAME, props.get(ZkStateReader.NODE_NAME));
-
- props = shards.get(SHARD3);
- assertEquals(URL3, props.get(ZkStateReader.URL_PROP));
- assertEquals(TEST_NODE_NAME, props.get(ZkStateReader.NODE_NAME));
-
- }
-
- } finally {
- if (zkClient != null) {
- zkClient.close();
- }
- if (zkController != null) {
- zkController.close();
- }
- if (server != null) {
- server.shutdown();
- }
- }
- }
-
- @Test
- public void testReadConfigName() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
-
- ZkTestServer server = new ZkTestServer(zkDir);
- try {
- server.run();
-
- AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
-
- SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
- String actualConfigName = "firstConfig";
-
- zkClient.makePath(ZkController.CONFIGS_ZKNODE + "/" + actualConfigName);
-
- ZkNodeProps props = new ZkNodeProps();
- props.put("configName", actualConfigName);
- zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/" + COLLECTION_NAME , props.store(), CreateMode.PERSISTENT);
-
- if (DEBUG) {
- zkClient.printLayoutToStdOut();
- }
- zkClient.close();
- ZkController zkController = new ZkController(server.getZkAddress(), TIMEOUT, TIMEOUT,
- "localhost", "8983", "/solr");
- try {
- String configName = zkController.readConfigName(COLLECTION_NAME);
- assertEquals(configName, actualConfigName);
- } finally {
- zkController.close();
- }
- } finally {
-
- server.shutdown();
- }
-
- }
-
- @Test
- public void testUploadToCloud() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
-
- ZkTestServer server = new ZkTestServer(zkDir);
- ZkController zkController = null;
- try {
- server.run();
-
- AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
-
- zkController = new ZkController(server.getZkAddress(),
- TIMEOUT, 1000, "localhost", "8983", "/solr");
-
- zkController.uploadToZK(getFile("solr/conf"),
- ZkController.CONFIGS_ZKNODE + "/config1");
-
- if (DEBUG) {
- zkController.printLayoutToStdOut();
- }
-
- } finally {
- if (zkController != null) {
- zkController.close();
- }
- server.shutdown();
- }
-
- }
-
- private void addShardToZk(SolrZkClient zkClient, String shardsPath,
- String zkNodeName, String url) throws IOException,
- KeeperException, InterruptedException {
-
- ZkNodeProps props = new ZkNodeProps();
- props.put(ZkStateReader.URL_PROP, url);
- props.put(ZkStateReader.NODE_NAME, TEST_NODE_NAME);
- byte[] bytes = props.store();
-
- zkClient
- .create(shardsPath + "/" + zkNodeName, bytes, CreateMode.PERSISTENT);
- }
-
- @Override
- public void tearDown() throws Exception {
- SolrConfig.severeErrors.clear();
- super.tearDown();
- }
-}
+package org.apache.solr.cloud;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.cloud.CloudState;
+import org.apache.solr.common.cloud.Slice;
+import org.apache.solr.common.cloud.SolrZkClient;
+import org.apache.solr.common.cloud.ZkNodeProps;
+import org.apache.solr.common.cloud.ZkStateReader;
+import org.apache.solr.core.SolrConfig;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class ZkControllerTest extends SolrTestCaseJ4 {
+
+ private static final String TEST_NODE_NAME = "test_node_name";
+
+ private static final String URL3 = "http://localhost:3133/solr/core1";
+
+ private static final String URL2 = "http://localhost:3123/solr/core1";
+
+ private static final String SHARD3 = "localhost:3123_solr_core3";
+
+ private static final String SHARD2 = "localhost:3123_solr_core2";
+
+ private static final String SHARD1 = "localhost:3123_solr_core1";
+
+ private static final String COLLECTION_NAME = "collection1";
+
+ static final int TIMEOUT = 10000;
+
+ private static final String URL1 = "http://localhost:3133/solr/core0";
+
+ private static final boolean DEBUG = false;
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ initCore();
+ }
+
+ @Test
+ public void testReadShards() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+ ZkTestServer server = null;
+ SolrZkClient zkClient = null;
+ ZkController zkController = null;
+ try {
+ server = new ZkTestServer(zkDir);
+ server.run();
+ AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+
+ zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
+ String shardsPath = "/collections/collection1/shards/shardid1";
+ zkClient.makePath(shardsPath);
+
+ addShardToZk(zkClient, shardsPath, SHARD1, URL1);
+ addShardToZk(zkClient, shardsPath, SHARD2, URL2);
+ addShardToZk(zkClient, shardsPath, SHARD3, URL3);
+
+ if (DEBUG) {
+ zkClient.printLayoutToStdOut();
+ }
+
+ zkController = new ZkController(server.getZkAddress(),
+ TIMEOUT, 1000, "localhost", "8983", "solr");
+
+ zkController.getZkStateReader().updateCloudState(true);
+ CloudState cloudInfo = zkController.getCloudState();
+ Map<String,Slice> slices = cloudInfo.getSlices("collection1");
+ assertNotNull(slices);
+
+ for (Slice slice : slices.values()) {
+ Map<String,ZkNodeProps> shards = slice.getShards();
+ if (DEBUG) {
+ for (String shardName : shards.keySet()) {
+ ZkNodeProps props = shards.get(shardName);
+ System.out.println("shard:" + shardName);
+ System.out.println("props:" + props.toString());
+ }
+ }
+ assertNotNull(shards.get(SHARD1));
+ assertNotNull(shards.get(SHARD2));
+ assertNotNull(shards.get(SHARD3));
+
+ ZkNodeProps props = shards.get(SHARD1);
+ assertEquals(URL1, props.get(ZkStateReader.URL_PROP));
+ assertEquals(TEST_NODE_NAME, props.get(ZkStateReader.NODE_NAME));
+
+ props = shards.get(SHARD2);
+ assertEquals(URL2, props.get(ZkStateReader.URL_PROP));
+ assertEquals(TEST_NODE_NAME, props.get(ZkStateReader.NODE_NAME));
+
+ props = shards.get(SHARD3);
+ assertEquals(URL3, props.get(ZkStateReader.URL_PROP));
+ assertEquals(TEST_NODE_NAME, props.get(ZkStateReader.NODE_NAME));
+
+ }
+
+ } finally {
+ if (zkClient != null) {
+ zkClient.close();
+ }
+ if (zkController != null) {
+ zkController.close();
+ }
+ if (server != null) {
+ server.shutdown();
+ }
+ }
+ }
+
+ @Test
+ public void testReadConfigName() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+
+ ZkTestServer server = new ZkTestServer(zkDir);
+ try {
+ server.run();
+
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+
+ SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
+ String actualConfigName = "firstConfig";
+
+ zkClient.makePath(ZkController.CONFIGS_ZKNODE + "/" + actualConfigName);
+
+ ZkNodeProps props = new ZkNodeProps();
+ props.put("configName", actualConfigName);
+ zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE + "/" + COLLECTION_NAME , props.store(), CreateMode.PERSISTENT);
+
+ if (DEBUG) {
+ zkClient.printLayoutToStdOut();
+ }
+ zkClient.close();
+ ZkController zkController = new ZkController(server.getZkAddress(), TIMEOUT, TIMEOUT,
+ "localhost", "8983", "/solr");
+ try {
+ String configName = zkController.readConfigName(COLLECTION_NAME);
+ assertEquals(configName, actualConfigName);
+ } finally {
+ zkController.close();
+ }
+ } finally {
+
+ server.shutdown();
+ }
+
+ }
+
+ @Test
+ public void testUploadToCloud() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+
+ ZkTestServer server = new ZkTestServer(zkDir);
+ ZkController zkController = null;
+ try {
+ server.run();
+
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+
+ zkController = new ZkController(server.getZkAddress(),
+ TIMEOUT, 1000, "localhost", "8983", "/solr");
+
+ zkController.uploadToZK(getFile("solr/conf"),
+ ZkController.CONFIGS_ZKNODE + "/config1");
+
+ if (DEBUG) {
+ zkController.printLayoutToStdOut();
+ }
+
+ } finally {
+ if (zkController != null) {
+ zkController.close();
+ }
+ server.shutdown();
+ }
+
+ }
+
+ private void addShardToZk(SolrZkClient zkClient, String shardsPath,
+ String zkNodeName, String url) throws IOException,
+ KeeperException, InterruptedException {
+
+ ZkNodeProps props = new ZkNodeProps();
+ props.put(ZkStateReader.URL_PROP, url);
+ props.put(ZkStateReader.NODE_NAME, TEST_NODE_NAME);
+ byte[] bytes = props.store();
+
+ zkClient
+ .create(shardsPath + "/" + zkNodeName, bytes, CreateMode.PERSISTENT);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ SolrConfig.severeErrors.clear();
+ super.tearDown();
+ }
+}
Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkNodePropsTest.java?rev=1065327&r1=1065326&r2=1065327&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkNodePropsTest.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkNodePropsTest.java Sun Jan 30 17:29:55 2011
@@ -1,49 +1,49 @@
-package org.apache.solr.cloud;
-
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. The ASF
- * licenses this file to You under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-import java.io.IOException;
-
-import org.apache.solr.SolrTestCaseJ4;
-import org.apache.solr.common.cloud.ZkNodeProps;
-import org.junit.Test;
-
-
-public class ZkNodePropsTest extends SolrTestCaseJ4 {
- @Test
- public void testBasic() throws IOException {
-
- ZkNodeProps props = new ZkNodeProps();
- props.put("prop1", "value1");
- props.put("prop2", "value2");
- props.put("prop3", "value3");
- props.put("prop4", "value4");
- props.put("prop5", "value5");
- props.put("prop6", "value6");
- byte[] bytes = props.store();
-
- ZkNodeProps props2 = new ZkNodeProps();
- props2.load(bytes);
- assertEquals("value1", props2.get("prop1"));
- assertEquals("value2", props2.get("prop2"));
- assertEquals("value3", props2.get("prop3"));
- assertEquals("value4", props2.get("prop4"));
- assertEquals("value5", props2.get("prop5"));
- assertEquals("value6", props2.get("prop6"));
- }
-}
+package org.apache.solr.cloud;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import java.io.IOException;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.cloud.ZkNodeProps;
+import org.junit.Test;
+
+
+public class ZkNodePropsTest extends SolrTestCaseJ4 {
+ @Test
+ public void testBasic() throws IOException {
+
+ ZkNodeProps props = new ZkNodeProps();
+ props.put("prop1", "value1");
+ props.put("prop2", "value2");
+ props.put("prop3", "value3");
+ props.put("prop4", "value4");
+ props.put("prop5", "value5");
+ props.put("prop6", "value6");
+ byte[] bytes = props.store();
+
+ ZkNodeProps props2 = new ZkNodeProps();
+ props2.load(bytes);
+ assertEquals("value1", props2.get("prop1"));
+ assertEquals("value2", props2.get("prop2"));
+ assertEquals("value3", props2.get("prop3"));
+ assertEquals("value4", props2.get("prop4"));
+ assertEquals("value5", props2.get("prop5"));
+ assertEquals("value6", props2.get("prop6"));
+ }
+}
Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkSolrClientTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkSolrClientTest.java?rev=1065327&r1=1065326&r2=1065327&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkSolrClientTest.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkSolrClientTest.java Sun Jan 30 17:29:55 2011
@@ -1,241 +1,241 @@
-package org.apache.solr.cloud;
-
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. The ASF
- * licenses this file to You under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-import java.io.File;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import junit.framework.TestCase;
-
-import org.apache.solr.common.cloud.SolrZkClient;
-import org.apache.solr.core.SolrConfig;
-import org.apache.solr.util.AbstractSolrTestCase;
-import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.Watcher;
-
-public class ZkSolrClientTest extends AbstractSolrTestCase {
- private static final boolean DEBUG = false;
-
- public void testConnect() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
- ZkTestServer server = null;
-
- server = new ZkTestServer(zkDir);
- server.run();
-
- SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), 100);
-
- zkClient.close();
- server.shutdown();
- }
-
- public void testMakeRootNode() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
- ZkTestServer server = null;
-
- server = new ZkTestServer(zkDir);
- server.run();
-
- AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
-
- SolrZkClient zkClient = new SolrZkClient(server.getZkHost(),
- AbstractZkTestCase.TIMEOUT);
-
- assertTrue(zkClient.exists("/solr"));
-
- zkClient.close();
- server.shutdown();
- }
-
- public void testReconnect() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
- ZkTestServer server = null;
- SolrZkClient zkClient = null;
- try {
- server = new ZkTestServer(zkDir);
- server.run();
-
- AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
-
- zkClient = new SolrZkClient(server.getZkAddress(), AbstractZkTestCase.TIMEOUT);
- String shardsPath = "/collections/collection1/shards";
- zkClient.makePath(shardsPath);
-
- zkClient.makePath("collections/collection1");
- int zkServerPort = server.getPort();
- // this tests disconnect state
- server.shutdown();
-
- Thread.sleep(80);
-
-
- try {
- zkClient.makePath("collections/collection2");
- TestCase.fail("Server should be down here");
- } catch (KeeperException.ConnectionLossException e) {
-
- }
-
- // bring server back up
- server = new ZkTestServer(zkDir, zkServerPort);
- server.run();
-
- // TODO: can we do better?
- // wait for reconnect
- Thread.sleep(600);
-
- try {
- zkClient.makePath("collections/collection3");
- } catch (KeeperException.ConnectionLossException e) {
- Thread.sleep(5000); // try again in a bit
- zkClient.makePath("collections/collection3");
- }
-
- if (DEBUG) {
- zkClient.printLayoutToStdOut();
- }
-
- assertNotNull(zkClient.exists("/collections/collection3", null));
- assertNotNull(zkClient.exists("/collections/collection1", null));
-
- // simulate session expiration
-
- // one option
- long sessionId = zkClient.getSolrZooKeeper().getSessionId();
- server.expire(sessionId);
-
- // another option
- //zkClient.getSolrZooKeeper().getConnection().disconnect();
-
- // this tests expired state
-
- Thread.sleep(1000); // pause for reconnect
-
- for (int i = 0; i < 8; i++) {
- try {
- zkClient.makePath("collections/collection4");
- break;
- } catch (KeeperException.SessionExpiredException e) {
-
- } catch (KeeperException.ConnectionLossException e) {
-
- }
- Thread.sleep(1000 * i);
- }
-
- if (DEBUG) {
- zkClient.printLayoutToStdOut();
- }
-
- assertNotNull("Node does not exist, but it should", zkClient.exists("/collections/collection4", null));
-
- } finally {
-
- if (zkClient != null) {
- zkClient.close();
- }
- if (server != null) {
- server.shutdown();
- }
- }
- }
-
- public void testWatchChildren() throws Exception {
- String zkDir = dataDir.getAbsolutePath() + File.separator
- + "zookeeper/server1/data";
-
- final AtomicInteger cnt = new AtomicInteger();
- ZkTestServer server = new ZkTestServer(zkDir);
- server.run();
- Thread.sleep(400);
- AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
- final SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), AbstractZkTestCase.TIMEOUT);
- try {
- zkClient.makePath("/collections");
-
- zkClient.getChildren("/collections", new Watcher() {
-
- public void process(WatchedEvent event) {
- if (DEBUG) {
- System.out.println("children changed");
- }
- cnt.incrementAndGet();
- // remake watch
- try {
- zkClient.getChildren("/collections", this);
- } catch (KeeperException e) {
- throw new RuntimeException(e);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- });
-
- zkClient.makePath("/collections/collection99/shards");
-
- zkClient.makePath("collections/collection99/config=collection1");
-
- zkClient.makePath("collections/collection99/config=collection3");
-
- zkClient.makePath("/collections/collection97/shards");
-
- if (DEBUG) {
- zkClient.printLayoutToStdOut();
- }
-
- // pause for the watches to fire
- Thread.sleep(700);
-
- if (cnt.intValue() < 2) {
- Thread.sleep(4000); // wait a bit more
- }
-
- assertEquals(2, cnt.intValue());
-
- } finally {
-
- if (zkClient != null) {
- zkClient.close();
- }
- if (server != null) {
- server.shutdown();
- }
- }
- }
-
- @Override
- public String getSchemaFile() {
- return null;
- }
-
- @Override
- public String getSolrConfigFile() {
- return null;
- }
-
- @Override
- public void tearDown() throws Exception {
- SolrConfig.severeErrors.clear();
- super.tearDown();
- }
-
-}
+package org.apache.solr.cloud;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.TestCase;
+
+import org.apache.solr.common.cloud.SolrZkClient;
+import org.apache.solr.core.SolrConfig;
+import org.apache.solr.util.AbstractSolrTestCase;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+
+public class ZkSolrClientTest extends AbstractSolrTestCase {
+ private static final boolean DEBUG = false;
+
+ public void testConnect() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+ ZkTestServer server = null;
+
+ server = new ZkTestServer(zkDir);
+ server.run();
+
+ SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), 100);
+
+ zkClient.close();
+ server.shutdown();
+ }
+
+ public void testMakeRootNode() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+ ZkTestServer server = null;
+
+ server = new ZkTestServer(zkDir);
+ server.run();
+
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+
+ SolrZkClient zkClient = new SolrZkClient(server.getZkHost(),
+ AbstractZkTestCase.TIMEOUT);
+
+ assertTrue(zkClient.exists("/solr"));
+
+ zkClient.close();
+ server.shutdown();
+ }
+
+ public void testReconnect() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+ ZkTestServer server = null;
+ SolrZkClient zkClient = null;
+ try {
+ server = new ZkTestServer(zkDir);
+ server.run();
+
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+
+ zkClient = new SolrZkClient(server.getZkAddress(), AbstractZkTestCase.TIMEOUT);
+ String shardsPath = "/collections/collection1/shards";
+ zkClient.makePath(shardsPath);
+
+ zkClient.makePath("collections/collection1");
+ int zkServerPort = server.getPort();
+ // this tests disconnect state
+ server.shutdown();
+
+ Thread.sleep(80);
+
+
+ try {
+ zkClient.makePath("collections/collection2");
+ TestCase.fail("Server should be down here");
+ } catch (KeeperException.ConnectionLossException e) {
+
+ }
+
+ // bring server back up
+ server = new ZkTestServer(zkDir, zkServerPort);
+ server.run();
+
+ // TODO: can we do better?
+ // wait for reconnect
+ Thread.sleep(600);
+
+ try {
+ zkClient.makePath("collections/collection3");
+ } catch (KeeperException.ConnectionLossException e) {
+ Thread.sleep(5000); // try again in a bit
+ zkClient.makePath("collections/collection3");
+ }
+
+ if (DEBUG) {
+ zkClient.printLayoutToStdOut();
+ }
+
+ assertNotNull(zkClient.exists("/collections/collection3", null));
+ assertNotNull(zkClient.exists("/collections/collection1", null));
+
+ // simulate session expiration
+
+ // one option
+ long sessionId = zkClient.getSolrZooKeeper().getSessionId();
+ server.expire(sessionId);
+
+ // another option
+ //zkClient.getSolrZooKeeper().getConnection().disconnect();
+
+ // this tests expired state
+
+ Thread.sleep(1000); // pause for reconnect
+
+ for (int i = 0; i < 8; i++) {
+ try {
+ zkClient.makePath("collections/collection4");
+ break;
+ } catch (KeeperException.SessionExpiredException e) {
+
+ } catch (KeeperException.ConnectionLossException e) {
+
+ }
+ Thread.sleep(1000 * i);
+ }
+
+ if (DEBUG) {
+ zkClient.printLayoutToStdOut();
+ }
+
+ assertNotNull("Node does not exist, but it should", zkClient.exists("/collections/collection4", null));
+
+ } finally {
+
+ if (zkClient != null) {
+ zkClient.close();
+ }
+ if (server != null) {
+ server.shutdown();
+ }
+ }
+ }
+
+ public void testWatchChildren() throws Exception {
+ String zkDir = dataDir.getAbsolutePath() + File.separator
+ + "zookeeper/server1/data";
+
+ final AtomicInteger cnt = new AtomicInteger();
+ ZkTestServer server = new ZkTestServer(zkDir);
+ server.run();
+ Thread.sleep(400);
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+ final SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), AbstractZkTestCase.TIMEOUT);
+ try {
+ zkClient.makePath("/collections");
+
+ zkClient.getChildren("/collections", new Watcher() {
+
+ public void process(WatchedEvent event) {
+ if (DEBUG) {
+ System.out.println("children changed");
+ }
+ cnt.incrementAndGet();
+ // remake watch
+ try {
+ zkClient.getChildren("/collections", this);
+ } catch (KeeperException e) {
+ throw new RuntimeException(e);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+
+ zkClient.makePath("/collections/collection99/shards");
+
+ zkClient.makePath("collections/collection99/config=collection1");
+
+ zkClient.makePath("collections/collection99/config=collection3");
+
+ zkClient.makePath("/collections/collection97/shards");
+
+ if (DEBUG) {
+ zkClient.printLayoutToStdOut();
+ }
+
+ // pause for the watches to fire
+ Thread.sleep(700);
+
+ if (cnt.intValue() < 2) {
+ Thread.sleep(4000); // wait a bit more
+ }
+
+ assertEquals(2, cnt.intValue());
+
+ } finally {
+
+ if (zkClient != null) {
+ zkClient.close();
+ }
+ if (server != null) {
+ server.shutdown();
+ }
+ }
+ }
+
+ @Override
+ public String getSchemaFile() {
+ return null;
+ }
+
+ @Override
+ public String getSolrConfigFile() {
+ return null;
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ SolrConfig.severeErrors.clear();
+ super.tearDown();
+ }
+
+}
Modified: lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkTestServer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkTestServer.java?rev=1065327&r1=1065326&r2=1065327&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkTestServer.java (original)
+++ lucene/dev/trunk/solr/src/test/org/apache/solr/cloud/ZkTestServer.java Sun Jan 30 17:29:55 2011
@@ -1,319 +1,319 @@
-package org.apache.solr.cloud;
-
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. The ASF
- * licenses this file to You under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.management.JMException;
-
-import org.apache.solr.SolrTestCaseJ4;
-import org.apache.zookeeper.jmx.ManagedUtil;
-import org.apache.zookeeper.server.NIOServerCnxn;
-import org.apache.zookeeper.server.ServerConfig;
-import org.apache.zookeeper.server.ZooKeeperServer;
-import org.apache.zookeeper.server.SessionTracker.Session;
-import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
-import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
-
-public class ZkTestServer {
-
- protected final ZKServerMain zkServer = new ZKServerMain();
-
- private String zkDir;
-
- private int clientPort;
-
- private Thread zooThread;
-
- class ZKServerMain {
-
- private NIOServerCnxn.Factory cnxnFactory;
- private ZooKeeperServer zooKeeperServer;
-
- protected void initializeAndRun(String[] args) throws ConfigException,
- IOException {
- try {
- ManagedUtil.registerLog4jMBeans();
- } catch (JMException e) {
-
- }
-
- ServerConfig config = new ServerConfig();
- if (args.length == 1) {
- config.parse(args[0]);
- } else {
- config.parse(args);
- }
-
- runFromConfig(config);
- }
-
- /**
- * Run from a ServerConfig.
- *
- * @param config ServerConfig to use.
- * @throws IOException
- */
- public void runFromConfig(ServerConfig config) throws IOException {
- try {
- // Note that this thread isn't going to be doing anything else,
- // so rather than spawning another thread, we will just call
- // run() in this thread.
- // create a file logger url from the command line args
- zooKeeperServer = new ZooKeeperServer();
-
- FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config
- .getDataLogDir()), new File(config.getDataDir()));
- zooKeeperServer.setTxnLogFactory(ftxn);
- zooKeeperServer.setTickTime(config.getTickTime());
- cnxnFactory = new NIOServerCnxn.Factory(config.getClientPortAddress(), config
- .getMaxClientCnxns());
- cnxnFactory.startup(zooKeeperServer);
- cnxnFactory.join();
- if (zooKeeperServer.isRunning()) {
- zooKeeperServer.shutdown();
- }
- } catch (InterruptedException e) {
- }
- }
-
- /**
- * Shutdown the serving instance
- * @throws IOException
- */
- protected void shutdown() throws IOException {
- zooKeeperServer.shutdown();
- zooKeeperServer.getZKDatabase().close();
- waitForServerDown(getZkHost() + ":" + getPort(), 5000);
- cnxnFactory.shutdown();
- }
-
- public int getLocalPort() {
- if (cnxnFactory == null) {
- throw new IllegalStateException("A port has not yet been selected");
- }
- int port = cnxnFactory.getLocalPort();
- if (port == 0) {
- throw new IllegalStateException("A port has not yet been selected");
- }
- return port;
- }
- }
-
- public ZkTestServer(String zkDir) {
- this.zkDir = zkDir;
- }
-
- public ZkTestServer(String zkDir, int port) {
- this.zkDir = zkDir;
- this.clientPort = port;
- }
-
- public String getZkHost() {
- return "127.0.0.1:" + zkServer.getLocalPort();
- }
-
- public String getZkAddress() {
- return "127.0.0.1:" + zkServer.getLocalPort() + "/solr";
- }
-
- public int getPort() {
- return zkServer.getLocalPort();
- }
-
- public void expire(final long sessionId) {
- zkServer.zooKeeperServer.expire(new Session() {
- @Override
- public long getSessionId() {
- return sessionId;
- }
- @Override
- public int getTimeout() {
- return 4000;
- }});
- }
-
- public void run() throws InterruptedException {
- // we don't call super.setUp
- zooThread = new Thread() {
-
- @Override
- public void run() {
- ServerConfig config = new ServerConfig() {
-
- {
- setClientPort(ZkTestServer.this.clientPort);
- this.dataDir = zkDir;
- this.dataLogDir = zkDir;
- this.tickTime = 1500;
- }
-
- public void setClientPort(int clientPort) {
- if (clientPortAddress != null) {
- try {
- this.clientPortAddress = new InetSocketAddress(
- InetAddress.getByName(clientPortAddress.getHostName()), clientPort);
- } catch (UnknownHostException e) {
- throw new RuntimeException(e);
- }
- } else {
- this.clientPortAddress = new InetSocketAddress(clientPort);
- }
- }
- };
-
- try {
- zkServer.runFromConfig(config);
- } catch (Throwable e) {
- throw new RuntimeException(e);
- }
- }
- };
-
- zooThread.setDaemon(true);
- zooThread.start();
-
- int cnt = 0;
- int port = -1;
- try {
- port = getPort();
- } catch(IllegalStateException e) {
-
- }
- while (port < 1) {
- Thread.sleep(100);
- try {
- port = getPort();
- } catch(IllegalStateException e) {
-
- }
- if (cnt == 40) {
- throw new RuntimeException("Could not get the port for ZooKeeper server");
- }
- cnt++;
- }
- }
-
- @SuppressWarnings("deprecation")
- public void shutdown() throws IOException {
- SolrTestCaseJ4.ignoreException("java.nio.channels.ClosedChannelException");
- // TODO: this can log an exception while trying to unregister a JMX MBean
- try {
- zkServer.shutdown();
- } finally {
- SolrTestCaseJ4.resetExceptionIgnores();
- }
- }
-
-
- public static boolean waitForServerDown(String hp, long timeout) {
- long start = System.currentTimeMillis();
- while (true) {
- try {
- HostPort hpobj = parseHostPortList(hp).get(0);
- send4LetterWord(hpobj.host, hpobj.port, "stat");
- } catch (IOException e) {
- return true;
- }
-
- if (System.currentTimeMillis() > start + timeout) {
- break;
- }
- try {
- Thread.sleep(250);
- } catch (InterruptedException e) {
- // ignore
- }
- }
- return false;
- }
-
- public static class HostPort {
- String host;
- int port;
-
- HostPort(String host, int port) {
- this.host = host;
- this.port = port;
- }
- }
-
- /**
- * Send the 4letterword
- * @param host the destination host
- * @param port the destination port
- * @param cmd the 4letterword
- * @return
- * @throws IOException
- */
- public static String send4LetterWord(String host, int port, String cmd)
- throws IOException
- {
-
- Socket sock = new Socket(host, port);
- BufferedReader reader = null;
- try {
- OutputStream outstream = sock.getOutputStream();
- outstream.write(cmd.getBytes());
- outstream.flush();
- // this replicates NC - close the output stream before reading
- sock.shutdownOutput();
-
- reader =
- new BufferedReader(
- new InputStreamReader(sock.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String line;
- while((line = reader.readLine()) != null) {
- sb.append(line + "\n");
- }
- return sb.toString();
- } finally {
- sock.close();
- if (reader != null) {
- reader.close();
- }
- }
- }
-
- public static List<HostPort> parseHostPortList(String hplist) {
- ArrayList<HostPort> alist = new ArrayList<HostPort>();
- for (String hp : hplist.split(",")) {
- int idx = hp.lastIndexOf(':');
- String host = hp.substring(0, idx);
- int port;
- try {
- port = Integer.parseInt(hp.substring(idx + 1));
- } catch (RuntimeException e) {
- throw new RuntimeException("Problem parsing " + hp + e.toString());
- }
- alist.add(new HostPort(host, port));
- }
- return alist;
- }
-}
+package org.apache.solr.cloud;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.management.JMException;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.zookeeper.jmx.ManagedUtil;
+import org.apache.zookeeper.server.NIOServerCnxn;
+import org.apache.zookeeper.server.ServerConfig;
+import org.apache.zookeeper.server.ZooKeeperServer;
+import org.apache.zookeeper.server.SessionTracker.Session;
+import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
+import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
+
+public class ZkTestServer {
+
+ protected final ZKServerMain zkServer = new ZKServerMain();
+
+ private String zkDir;
+
+ private int clientPort;
+
+ private Thread zooThread;
+
+ class ZKServerMain {
+
+ private NIOServerCnxn.Factory cnxnFactory;
+ private ZooKeeperServer zooKeeperServer;
+
+ protected void initializeAndRun(String[] args) throws ConfigException,
+ IOException {
+ try {
+ ManagedUtil.registerLog4jMBeans();
+ } catch (JMException e) {
+
+ }
+
+ ServerConfig config = new ServerConfig();
+ if (args.length == 1) {
+ config.parse(args[0]);
+ } else {
+ config.parse(args);
+ }
+
+ runFromConfig(config);
+ }
+
+ /**
+ * Run from a ServerConfig.
+ *
+ * @param config ServerConfig to use.
+ * @throws IOException
+ */
+ public void runFromConfig(ServerConfig config) throws IOException {
+ try {
+ // Note that this thread isn't going to be doing anything else,
+ // so rather than spawning another thread, we will just call
+ // run() in this thread.
+ // create a file logger url from the command line args
+ zooKeeperServer = new ZooKeeperServer();
+
+ FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config
+ .getDataLogDir()), new File(config.getDataDir()));
+ zooKeeperServer.setTxnLogFactory(ftxn);
+ zooKeeperServer.setTickTime(config.getTickTime());
+ cnxnFactory = new NIOServerCnxn.Factory(config.getClientPortAddress(), config
+ .getMaxClientCnxns());
+ cnxnFactory.startup(zooKeeperServer);
+ cnxnFactory.join();
+ if (zooKeeperServer.isRunning()) {
+ zooKeeperServer.shutdown();
+ }
+ } catch (InterruptedException e) {
+ }
+ }
+
+ /**
+ * Shutdown the serving instance
+ * @throws IOException
+ */
+ protected void shutdown() throws IOException {
+ zooKeeperServer.shutdown();
+ zooKeeperServer.getZKDatabase().close();
+ waitForServerDown(getZkHost() + ":" + getPort(), 5000);
+ cnxnFactory.shutdown();
+ }
+
+ public int getLocalPort() {
+ if (cnxnFactory == null) {
+ throw new IllegalStateException("A port has not yet been selected");
+ }
+ int port = cnxnFactory.getLocalPort();
+ if (port == 0) {
+ throw new IllegalStateException("A port has not yet been selected");
+ }
+ return port;
+ }
+ }
+
+ public ZkTestServer(String zkDir) {
+ this.zkDir = zkDir;
+ }
+
+ public ZkTestServer(String zkDir, int port) {
+ this.zkDir = zkDir;
+ this.clientPort = port;
+ }
+
+ public String getZkHost() {
+ return "127.0.0.1:" + zkServer.getLocalPort();
+ }
+
+ public String getZkAddress() {
+ return "127.0.0.1:" + zkServer.getLocalPort() + "/solr";
+ }
+
+ public int getPort() {
+ return zkServer.getLocalPort();
+ }
+
+ public void expire(final long sessionId) {
+ zkServer.zooKeeperServer.expire(new Session() {
+ @Override
+ public long getSessionId() {
+ return sessionId;
+ }
+ @Override
+ public int getTimeout() {
+ return 4000;
+ }});
+ }
+
+ public void run() throws InterruptedException {
+ // we don't call super.setUp
+ zooThread = new Thread() {
+
+ @Override
+ public void run() {
+ ServerConfig config = new ServerConfig() {
+
+ {
+ setClientPort(ZkTestServer.this.clientPort);
+ this.dataDir = zkDir;
+ this.dataLogDir = zkDir;
+ this.tickTime = 1500;
+ }
+
+ public void setClientPort(int clientPort) {
+ if (clientPortAddress != null) {
+ try {
+ this.clientPortAddress = new InetSocketAddress(
+ InetAddress.getByName(clientPortAddress.getHostName()), clientPort);
+ } catch (UnknownHostException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ this.clientPortAddress = new InetSocketAddress(clientPort);
+ }
+ }
+ };
+
+ try {
+ zkServer.runFromConfig(config);
+ } catch (Throwable e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ zooThread.setDaemon(true);
+ zooThread.start();
+
+ int cnt = 0;
+ int port = -1;
+ try {
+ port = getPort();
+ } catch(IllegalStateException e) {
+
+ }
+ while (port < 1) {
+ Thread.sleep(100);
+ try {
+ port = getPort();
+ } catch(IllegalStateException e) {
+
+ }
+ if (cnt == 40) {
+ throw new RuntimeException("Could not get the port for ZooKeeper server");
+ }
+ cnt++;
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ public void shutdown() throws IOException {
+ SolrTestCaseJ4.ignoreException("java.nio.channels.ClosedChannelException");
+ // TODO: this can log an exception while trying to unregister a JMX MBean
+ try {
+ zkServer.shutdown();
+ } finally {
+ SolrTestCaseJ4.resetExceptionIgnores();
+ }
+ }
+
+
+ public static boolean waitForServerDown(String hp, long timeout) {
+ long start = System.currentTimeMillis();
+ while (true) {
+ try {
+ HostPort hpobj = parseHostPortList(hp).get(0);
+ send4LetterWord(hpobj.host, hpobj.port, "stat");
+ } catch (IOException e) {
+ return true;
+ }
+
+ if (System.currentTimeMillis() > start + timeout) {
+ break;
+ }
+ try {
+ Thread.sleep(250);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }
+ return false;
+ }
+
+ public static class HostPort {
+ String host;
+ int port;
+
+ HostPort(String host, int port) {
+ this.host = host;
+ this.port = port;
+ }
+ }
+
+ /**
+ * Send the 4letterword
+ * @param host the destination host
+ * @param port the destination port
+ * @param cmd the 4letterword
+ * @return
+ * @throws IOException
+ */
+ public static String send4LetterWord(String host, int port, String cmd)
+ throws IOException
+ {
+
+ Socket sock = new Socket(host, port);
+ BufferedReader reader = null;
+ try {
+ OutputStream outstream = sock.getOutputStream();
+ outstream.write(cmd.getBytes());
+ outstream.flush();
+ // this replicates NC - close the output stream before reading
+ sock.shutdownOutput();
+
+ reader =
+ new BufferedReader(
+ new InputStreamReader(sock.getInputStream()));
+ StringBuilder sb = new StringBuilder();
+ String line;
+ while((line = reader.readLine()) != null) {
+ sb.append(line + "\n");
+ }
+ return sb.toString();
+ } finally {
+ sock.close();
+ if (reader != null) {
+ reader.close();
+ }
+ }
+ }
+
+ public static List<HostPort> parseHostPortList(String hplist) {
+ ArrayList<HostPort> alist = new ArrayList<HostPort>();
+ for (String hp : hplist.split(",")) {
+ int idx = hp.lastIndexOf(':');
+ String host = hp.substring(0, idx);
+ int port;
+ try {
+ port = Integer.parseInt(hp.substring(idx + 1));
+ } catch (RuntimeException e) {
+ throw new RuntimeException("Problem parsing " + hp + e.toString());
+ }
+ alist.add(new HostPort(host, port));
+ }
+ return alist;
+ }
+}