You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by to...@apache.org on 2014/04/24 11:22:46 UTC
svn commit: r1589646 -
/jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/server/RemoteSolrServerProvider.java
Author: tommaso
Date: Thu Apr 24 09:22:46 2014
New Revision: 1589646
URL: http://svn.apache.org/r1589646
Log:
OAK-1766 - multiple attempts in connecting to ZK in RemoteSSP and exception if failing
Modified:
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/server/RemoteSolrServerProvider.java
Modified: jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/server/RemoteSolrServerProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/server/RemoteSolrServerProvider.java?rev=1589646&r1=1589645&r2=1589646&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/server/RemoteSolrServerProvider.java (original)
+++ jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/server/RemoteSolrServerProvider.java Thu Apr 24 09:22:46 2014
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.oak.plugin
import java.io.File;
import java.io.IOException;
+
import org.apache.jackrabbit.oak.plugins.index.solr.configuration.RemoteSolrServerConfiguration;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
@@ -85,45 +86,68 @@ public class RemoteSolrServerProvider im
// try SolrCloud client
CloudSolrServer cloudSolrServer = new CloudSolrServer(remoteSolrServerConfiguration.getSolrZkHost());
cloudSolrServer.setZkConnectTimeout(100);
- cloudSolrServer.connect();
- cloudSolrServer.setDefaultCollection("collection1"); // workaround for first request when the needed collection may not exist
+ if (connectToZK(cloudSolrServer)) {
+ cloudSolrServer.setDefaultCollection("collection1"); // workaround for first request when the needed collection may not exist
- // create specified collection if it doesn't exists
- try {
- createCollectionIfNeeded(cloudSolrServer);
- } catch (Throwable t) {
- if (log.isWarnEnabled()) {
- log.warn("could not create the collection on {}", remoteSolrServerConfiguration.getSolrZkHost(), t);
+ // create specified collection if it doesn't exists
+ try {
+ createCollectionIfNeeded(cloudSolrServer);
+ } catch (Throwable t) {
+ if (log.isWarnEnabled()) {
+ log.warn("could not create the collection on {}", remoteSolrServerConfiguration.getSolrZkHost(), t);
+ }
+ }
+
+ cloudSolrServer.setDefaultCollection(remoteSolrServerConfiguration.getSolrCollection());
+
+ // SolrCloud may need some time to sync on collection creation (to spread it over the shards / replicas)
+ int i = 0;
+ while (i < 3) {
+ try {
+ SolrPingResponse ping = cloudSolrServer.ping();
+ if (ping != null && 0 == ping.getStatus()) {
+ return cloudSolrServer;
+ } else {
+ throw new IOException("the found SolrCloud server is not alive");
+ }
+ } catch (Exception e) {
+ // wait a bit
+ try {
+ if (log.isDebugEnabled()) {
+ log.debug("server is not alive yet, wait a bit", e);
+ }
+ Thread.sleep(3000);
+ } catch (InterruptedException e1) {
+ // do nothing
+ }
+ }
+ i++;
}
+ throw new IOException("the found SolrCloud server is not alive");
+ }
+ else {
+ throw new IOException("could not connect to Zookeeper hosted at " + remoteSolrServerConfiguration.getSolrZkHost());
}
- cloudSolrServer.setDefaultCollection(remoteSolrServerConfiguration.getSolrCollection());
+ }
- // SolrCloud may need some time to sync on collection creation (to spread it over the shards / replicas)
- int i = 0;
- while (i < 3) {
+ private boolean connectToZK(CloudSolrServer cloudSolrServer) {
+ boolean connected = false;
+ for (int i = 0; i < 3; i++) {
try {
- SolrPingResponse ping = cloudSolrServer.ping();
- if (ping != null && 0 == ping.getStatus()) {
- return cloudSolrServer;
- } else {
- throw new IOException("the found SolrCloud server is not alive");
- }
+ cloudSolrServer.connect();
+ connected = true;
+ break;
} catch (Exception e) {
- // wait a bit
+ log.warn("could not connect to ZK", e);
try {
- if (log.isDebugEnabled()) {
- log.debug("server is not alive yet, wait a bit", e);
- }
Thread.sleep(3000);
} catch (InterruptedException e1) {
// do nothing
}
}
- i++;
}
- throw new IOException("the found SolrCloud server is not alive");
-
+ return connected;
}
private void createCollectionIfNeeded(CloudSolrServer cloudSolrServer) throws SolrServerException, IOException {
@@ -142,10 +166,8 @@ public class RemoteSolrServerProvider im
ZkController.uploadConfigDir(zkClient, dir, solrCollection);
UpdateRequest req = new UpdateRequest("/admin/collections");
req.setParam("action", "CREATE");
- if (remoteSolrServerConfiguration != null) {
- req.setParam("numShards", String.valueOf(remoteSolrServerConfiguration.getSolrShardsNo()));
- req.setParam("replicationFactor", String.valueOf(remoteSolrServerConfiguration.getSolrReplicationFactor()));
- }
+ req.setParam("numShards", String.valueOf(remoteSolrServerConfiguration.getSolrShardsNo()));
+ req.setParam("replicationFactor", String.valueOf(remoteSolrServerConfiguration.getSolrReplicationFactor()));
req.setParam("collection.configName", solrCollection);
req.setParam("name", solrCollection);
cloudSolrServer.request(req);