Apache Solr open-source search software
SOLR-17667: Simplify zombie logic in LBSolrClient (#3176)
Houston Putman3d6458b6a516
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6db704e3f75..319ad34a322 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -242,6 +242,8 @@ Other Changes* SOLR-17671: Replication and backup have their DirectoryFactory.DirContext so the directory they use is unwrappedwhen copying files. (Bruno Roustant, David Smiley)+* SOLR-17667: Simplify zombie server logic in LBSolrClient (Houston Putman)+================== 9.8.0 ==================New Features---------------------diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.javaindex 059e2592f77..33488a83d30 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.java@@ -237,7 +237,7 @@ public class LBHttp2SolrClient<C extends HttpSolrClientBase> extends LBSolrClienRetryListener listener) {rsp.rsp = result;if (isZombie) {- zombieServers.remove(endpoint.toString());+ reviveZombieServer(endpoint);}listener.onSuccess(rsp);}@@ -256,32 +256,32 @@ public class LBHttp2SolrClient<C extends HttpSolrClientBase> extends LBSolrClien// we retry on 404 or 403 or 503 or 500// unless it's an update - then we only retry on connect exceptionif (!isNonRetryable && RETRY_CODES.contains(e.code())) {- listener.onFailure((!isZombie) ? addZombie(endpoint, e) : e, true);+ listener.onFailure((!isZombie) ? makeServerAZombie(endpoint, e) : e, true);} else {// Server is alive but the request was likely malformed or invalidif (isZombie) {- zombieServers.remove(endpoint.toString());+ reviveZombieServer(endpoint);}listener.onFailure(e, false);}} catch (SocketException e) {if (!isNonRetryable || e instanceof ConnectException) {- listener.onFailure((!isZombie) ? addZombie(endpoint, e) : e, true);+ listener.onFailure((!isZombie) ? makeServerAZombie(endpoint, e) : e, true);} else {listener.onFailure(e, false);}} catch (SocketTimeoutException e) {if (!isNonRetryable) {- listener.onFailure((!isZombie) ? addZombie(endpoint, e) : e, true);+ listener.onFailure((!isZombie) ? makeServerAZombie(endpoint, e) : e, true);} else {listener.onFailure(e, false);}} catch (SolrServerException e) {Throwable rootCause = e.getRootCause();if (!isNonRetryable && rootCause instanceof IOException) {- listener.onFailure((!isZombie) ? addZombie(endpoint, e) : e, true);+ listener.onFailure((!isZombie) ? makeServerAZombie(endpoint, e) : e, true);} else if (isNonRetryable && rootCause instanceof ConnectException) {- listener.onFailure((!isZombie) ? addZombie(endpoint, e) : e, true);+ listener.onFailure((!isZombie) ? makeServerAZombie(endpoint, e) : e, true);} else {listener.onFailure(e, false);}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.javaindex 221cbd4bb7e..f78dc458841 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java@@ -191,7 +191,7 @@ public class LBHttpSolrClient extends LBSolrClient {}@Override- public String removeSolrServer(Endpoint server) {+ public synchronized String removeSolrServer(Endpoint server) {urlToClient.remove(server.toString());return super.removeSolrServer(server);}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.javaindex 80b8c1371be..0d0d539d97c 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java@@ -23,7 +23,6 @@ import java.io.IOException;import java.lang.invoke.MethodHandles;import java.lang.ref.WeakReference;import java.net.ConnectException;-import java.net.MalformedURLException;import java.net.SocketException;import java.net.SocketTimeoutException;import java.util.ArrayList;@@ -70,7 +69,7 @@ public abstract class LBSolrClient extends SolrClient {protected static final String UPDATE_LIVE_SERVER_MESSAGE = "Updated alive server list";- private static final String UPDATE_LIVE_SERVER_LOG = UPDATE_LIVE_SERVER_MESSAGE + ": {}";+ private static final String UPDATE_LIVE_SERVER_LOG = UPDATE_LIVE_SERVER_MESSAGE + " for {}: {}";// defaultsprotected static final Set<Integer> RETRY_CODES =@@ -80,8 +79,8 @@ public abstract class LBSolrClient extends SolrClient {// keys to the maps are currently of the form "http://localhost:8983/solr"// which should be equivalent to HttpSolrServer.getBaseURL()- private final Map<String, EndpointWrapper> aliveServers = new LinkedHashMap<>();- // access to aliveServers should be synchronized on itself+ // write-access to allServers or zombieServers should be within a synchronized method+ private final Map<String, EndpointWrapper> allServers = new LinkedHashMap<>();protected final Map<String, EndpointWrapper> zombieServers = new ConcurrentHashMap<>();@@ -204,11 +203,6 @@ public abstract class LBSolrClient extends SolrClient {protected static class EndpointWrapper {final Endpoint endpoint;- // "standard" endpoints are used by default. They normally live in the alive list- // and move to the zombie list when unavailable. When they become available again,- // they move back to the alive list.- boolean standard = true;-int failedPings = 0;EndpointWrapper(Endpoint endpoint) {@@ -412,7 +406,7 @@ public abstract class LBSolrClient extends SolrClient {if (!solrEndpoints.isEmpty()) {for (Endpoint s : solrEndpoints) {EndpointWrapper wrapper = createServerWrapper(s);- aliveServers.put(wrapper.getEndpoint().toString(), wrapper);+ allServers.put(wrapper.getEndpoint().toString(), wrapper);}updateAliveList();}@@ -420,16 +414,22 @@ public abstract class LBSolrClient extends SolrClient {}protected void updateAliveList() {- synchronized (aliveServers) {- aliveServerList = aliveServers.values().toArray(new EndpointWrapper[0]);+ synchronized (allServers) {+ aliveServerList =+ allServers.values().stream()+ .filter(server -> !zombieServers.containsKey(server.getEndpoint().getBaseUrl()))+ .toArray(EndpointWrapper[]::new);if (log.isDebugEnabled()) {- log.debug(UPDATE_LIVE_SERVER_LOG, Arrays.toString(aliveServerList));+ log.debug(UPDATE_LIVE_SERVER_LOG, this, Arrays.toString(aliveServerList));}}}- protected EndpointWrapper createServerWrapper(Endpoint baseUrl) {- return new EndpointWrapper(baseUrl);+ protected EndpointWrapper createServerWrapper(Endpoint endpoint) {+ if (allServers.containsKey(endpoint.toString())) {+ return allServers.get(endpoint.toString());+ }+ return new EndpointWrapper(endpoint);}public static String normalize(String server) {@@ -524,7 +524,7 @@ public abstract class LBSolrClient extends SolrClient {rsp.server = baseUrl.toString();rsp.rsp = doRequest(baseUrl, req.getRequest());if (isZombie) {- zombieServers.remove(baseUrl.toString());+ reviveZombieServer(baseUrl);}} catch (BaseHttpSolrClient.RemoteExecutionException e) {throw e;@@ -532,32 +532,32 @@ public abstract class LBSolrClient extends SolrClient {// we retry on 404 or 403 or 503 or 500// unless it's an update - then we only retry on connect exceptionif (!isNonRetryable && RETRY_CODES.contains(e.code())) {- ex = (!isZombie) ? addZombie(baseUrl, e) : e;+ ex = (!isZombie) ? makeServerAZombie(baseUrl, e) : e;} else {// Server is alive but the request was likely malformed or invalidif (isZombie) {- zombieServers.remove(baseUrl.toString());+ reviveZombieServer(baseUrl);}throw e;}} catch (SocketException e) {if (!isNonRetryable || e instanceof ConnectException) {- ex = (!isZombie) ? addZombie(baseUrl, e) : e;+ ex = (!isZombie) ? makeServerAZombie(baseUrl, e) : e;} else {throw e;}} catch (SocketTimeoutException e) {if (!isNonRetryable) {- ex = (!isZombie) ? addZombie(baseUrl, e) : e;+ ex = (!isZombie) ? makeServerAZombie(baseUrl, e) : e;} else {throw e;}} catch (SolrServerException e) {Throwable rootCause = e.getRootCause();if (!isNonRetryable && rootCause instanceof IOException) {- ex = (!isZombie) ? addZombie(baseUrl, e) : e;+ ex = (!isZombie) ? makeServerAZombie(baseUrl, e) : e;} else if (isNonRetryable && rootCause instanceof ConnectException) {- ex = (!isZombie) ? addZombie(baseUrl, e) : e;+ ex = (!isZombie) ? makeServerAZombie(baseUrl, e) : e;} else {throw e;}@@ -570,20 +570,13 @@ public abstract class LBSolrClient extends SolrClient {protected abstract SolrClient getClient(Endpoint endpoint);- protected Exception addZombie(Endpoint serverStr, Exception e) {- EndpointWrapper wrapper = createServerWrapper(serverStr);- wrapper.standard = false;- zombieServers.put(serverStr.toString(), wrapper);- startAliveCheckExecutor();- return e;- }-private void startAliveCheckExecutor() {// double-checked locking, but it's OK because we don't *do* anything with aliveCheckExecutor// if it's not null.if (aliveCheckExecutor == null) {synchronized (this) {if (aliveCheckExecutor == null) {+ log.debug("Starting aliveCheckExecutor for {}", this);aliveCheckExecutor =Executors.newSingleThreadScheduledExecutor(new SolrNamedThreadFactory("aliveCheckExecutor"));@@ -619,6 +612,7 @@ public abstract class LBSolrClient extends SolrClient {private void checkAZombieServer(EndpointWrapper zombieServer) {final Endpoint zombieEndpoint = zombieServer.getEndpoint();try {+ log.debug("Checking zombie server {} for {}", zombieServer, this);QueryRequest queryRequest = new QueryRequest(solrQuery);// First the one on the endpoint, then the default collectionfinal String effectiveCollection =@@ -634,19 +628,7 @@ public abstract class LBSolrClient extends SolrClient {if (resp.getStatus() == 0) {// server has come back up.- // make sure to remove from zombies before adding to the alive list to avoid a race- // condition- // where another thread could mark it down, move it back to zombie, and then we delete- // from zombie and lose it forever.- EndpointWrapper wrapper = zombieServers.remove(zombieServer.getEndpoint().toString());- if (wrapper != null) {- wrapper.failedPings = 0;- if (wrapper.standard) {- addToAlive(wrapper);- }- } else {- // something else already moved the server from zombie to alive- }+ reviveZombieServer(zombieEndpoint);}} catch (Exception e) {// Expected. The server is still down.@@ -654,37 +636,47 @@ public abstract class LBSolrClient extends SolrClient {// If the server doesn't belong in the standard set belonging to this load balancer// then simply drop it after a certain number of failed pings.- if (!zombieServer.standard && zombieServer.failedPings >= NONSTANDARD_PING_LIMIT) {- zombieServers.remove(zombieEndpoint.getUrl());+ if (!allServers.containsKey(zombieServer.toString())+ && zombieServer.failedPings >= NONSTANDARD_PING_LIMIT) {+ reviveZombieServer(zombieEndpoint);}}}- private EndpointWrapper removeFromAlive(String key) {- synchronized (aliveServers) {- EndpointWrapper wrapper = aliveServers.remove(key);- if (wrapper != null) updateAliveList();- return wrapper;+ protected synchronized void reviveZombieServer(Endpoint endpoint) {+ EndpointWrapper wrapper = zombieServers.remove(endpoint.toString());+ if (wrapper != null && allServers.containsKey(endpoint.toString())) {+ wrapper.failedPings = 0;+ updateAliveList();}}- private void addToAlive(EndpointWrapper wrapper) {- synchronized (aliveServers) {- EndpointWrapper prev = aliveServers.put(wrapper.getEndpoint().getBaseUrl(), wrapper);- // TODO: warn if there was a previous entry?+ protected synchronized void makeServerAZombie(EndpointWrapper wrapper) {+ if (zombieServers.putIfAbsent(wrapper.getEndpoint().toString(), wrapper) == null) {updateAliveList();}+ startAliveCheckExecutor();+ }++ protected void makeServerAZombie(Endpoint endpoint) {+ makeServerAZombie(createServerWrapper(endpoint));+ }++ protected Exception makeServerAZombie(Endpoint endpoint, Exception e) {+ makeServerAZombie(endpoint);+ return e;}- public void addSolrServer(Endpoint endpoint) throws MalformedURLException {- addToAlive(createServerWrapper(endpoint));+ public synchronized void addSolrServer(Endpoint endpoint) {+ EndpointWrapper prev = allServers.put(endpoint.toString(), createServerWrapper(endpoint));+ // TODO: warn if there was a previous entry?+ updateAliveList();}- public String removeSolrServer(Endpoint endpoint) {- // there is a small race condition here - if the server is in the process of being moved between- // lists, we could fail to remove it.- removeFromAlive(endpoint.getUrl());- zombieServers.remove(endpoint.getUrl());+ public synchronized String removeSolrServer(Endpoint endpoint) {+ allServers.remove(endpoint.toString());+ zombieServers.remove(endpoint.toString());+ updateAliveList();return null;}@@ -739,21 +731,21 @@ public abstract class LBSolrClient extends SolrClient {} catch (SolrServerException e) {if (e.getRootCause() instanceof IOException) {ex = e;- moveAliveToDead(wrapper);+ makeServerAZombie(wrapper);if (justFailed == null) {justFailed = new HashMap<>();}- justFailed.put(endpoint.getUrl(), wrapper);+ justFailed.put(endpoint.toString(), wrapper);} else {throw e;}} catch (IOException e) {ex = e;- moveAliveToDead(wrapper);+ makeServerAZombie(wrapper);if (justFailed == null) {justFailed = new HashMap<>();}- justFailed.put(endpoint.getUrl(), wrapper);+ justFailed.put(endpoint.toString(), wrapper);} catch (Exception e) {throw new SolrServerException(e);}@@ -767,21 +759,22 @@ public abstract class LBSolrClient extends SolrClient {break;}- if (wrapper.standard == false- || (justFailed != null && justFailed.containsKey(endpoint.getUrl()))) continue;+ // Do not try this server if it was just tried, or if it is not in this client's list of+ // servers+ if (!allServers.containsKey(wrapper.getEndpoint().toString())+ || (justFailed != null && justFailed.containsKey(wrapper.getEndpoint().toString())))+ continue;try {++numServersTried;final String effectiveCollection =endpoint.getCore() == null ? collection : endpoint.getCore();NamedList<Object> rsp =doRequest(getClient(endpoint), endpoint.getBaseUrl(), effectiveCollection, request);- // remove from zombie list *before* adding to the alive list to avoid a race that could lose- // a server- zombieServers.remove(endpoint.getUrl());- addToAlive(wrapper);+ reviveZombieServer(wrapper.getEndpoint());return rsp;} catch (SolrException e) {// Server is alive but the request was malformed or invalid+ reviveZombieServer(wrapper.getEndpoint());throw e;} catch (SolrServerException e) {if (e.getRootCause() instanceof IOException) {@@ -830,13 +823,6 @@ public abstract class LBSolrClient extends SolrClient {return aliveServerList[count % aliveServerList.length];}- private void moveAliveToDead(EndpointWrapper wrapper) {- wrapper = removeFromAlive(wrapper.getEndpoint().toString());- if (wrapper == null) return; // another thread already detected the failure and removed it- zombieServers.put(wrapper.getEndpoint().toString(), wrapper);- startAliveCheckExecutor();- }-@Overridepublic void close() {synchronized (this) {diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.javaindex 61504a052b8..9a825b9c196 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java@@ -38,6 +38,7 @@ import org.apache.solr.client.solrj.response.SolrResponseBase;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.embedded.JettyConfig;import org.apache.solr.embedded.JettySolrRunner;+import org.apache.solr.util.LogLevel;import org.apache.solr.util.LogListener;import org.junit.AfterClass;import org.junit.BeforeClass;@@ -49,6 +50,7 @@ import org.slf4j.LoggerFactory;** @since solr 1.4*/+@LogLevel("org.apache.solr.client.solrj.impl=DEBUG")public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());