Apache Lucene open-source search software
LUCENE-5600: HttpReplicator does not properly handle server failures
Shai Erera9edd501d0028
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 4156a53fdef..d569750c86d 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -86,6 +86,11 @@ Optimizations* LUCENE-5599: HttpReplicator did not properly delegate bulk read() to wrappedInputStream. (Christoph Kaser via Shai Erera)++Bug fixes++* LUCENE-5600: HttpClientBase did not properly consume a connection if a server+ error occurred. (Christoph Kaser via Shai Erera)======================= Lucene 4.8.0 =======================diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.javaindex 4c8d1c86740..24d35a1fd75 100644--- a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java@@ -37,6 +37,7 @@ import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.HttpConnectionParams;import org.apache.http.util.EntityUtils;import org.apache.lucene.store.AlreadyClosedException;+import org.apache.lucene.util.IOUtils;/*** Base class for Http clients.@@ -124,7 +125,11 @@ public abstract class HttpClientBase implements Closeable {protected void verifyStatus(HttpResponse response) throws IOException {StatusLine statusLine = response.getStatusLine();if (statusLine.getStatusCode() != HttpStatus.SC_OK) {- throwKnownError(response, statusLine);+ try {+ throwKnownError(response, statusLine);+ } finally {+ EntityUtils.consumeQuietly(response.getEntity());+ }}}@@ -132,27 +137,20 @@ public abstract class HttpClientBase implements Closeable {ObjectInputStream in = null;try {in = new ObjectInputStream(response.getEntity().getContent());- } catch (Exception e) {+ } catch (Throwable t) {// the response stream is not an exception - could be an error in servlet.init().- throw new RuntimeException("Uknown error: " + statusLine);+ throw new RuntimeException("Unknown error: " + statusLine, t);}Throwable t;try {t = (Throwable) in.readObject();- } catch (Exception e) {- //not likely- throw new RuntimeException("Failed to read exception object: " + statusLine, e);+ } catch (Throwable th) {+ throw new RuntimeException("Failed to read exception object: " + statusLine, th);} finally {in.close();}- if (t instanceof IOException) {- throw (IOException) t;- }- if (t instanceof RuntimeException) {- throw (RuntimeException) t;- }- throw new RuntimeException("unknown exception "+statusLine,t);+ IOUtils.reThrow(t);}/**@@ -216,7 +214,7 @@ public abstract class HttpClientBase implements Closeable {}@Overridepublic void close() throws IOException {- super.close();+ in.close();consume(-1);}@Override@@ -232,7 +230,7 @@ public abstract class HttpClientBase implements Closeable {return res;}private void consume(int minusOne) {- if (!consumed && minusOne==-1) {+ if (!consumed && minusOne == -1) {try {EntityUtils.consume(entity);} catch (Exception e) {@@ -266,27 +264,23 @@ public abstract class HttpClientBase implements Closeable {* release the response at exit, depending on <code>consume</code> parameter.*/protected <T> T doAction(HttpResponse response, boolean consume, Callable<T> call) throws IOException {- IOException error = null;+ Throwable th = null;try {return call.call();- } catch (IOException e) {- error = e;- } catch (Exception e) {- error = new IOException(e);+ } catch (Throwable t) {+ th = t;} finally {try {verifyStatus(response);} finally {if (consume) {- try {- EntityUtils.consume(response.getEntity());- } catch (Exception e) {- // ignoring on purpose- }+ EntityUtils.consumeQuietly(response.getEntity());}}}- throw error; // should not get here+ assert th != null; // extra safety - if we get here, it means the callable failed+ IOUtils.reThrow(th);+ return null; // silly, if we're here, IOUtils.reThrow always throws an exception}@Overridediff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.javaindex 7df96977457..4d44e7bd8b7 100644--- a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java@@ -77,7 +77,7 @@ public class HttpReplicator extends HttpClientBase implements Replicator {return doAction(response, false, new Callable<InputStream>() {@Overridepublic InputStream call() throws Exception {- return responseInputStream(response,true);+ return responseInputStream(response, true);}});}diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.javaindex 551fe5246a4..ca7cbc4a52d 100644--- a/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java@@ -21,6 +21,7 @@ import java.io.File;import java.io.IOException;import java.util.Collections;+import org.apache.http.impl.conn.BasicClientConnectionManager;import org.apache.lucene.document.Document;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;@@ -35,7 +36,6 @@ import org.apache.lucene.replicator.Replicator;import org.apache.lucene.replicator.ReplicatorTestCase;import org.apache.lucene.store.Directory;import org.apache.lucene.util.IOUtils;-import org.apache.lucene.util.TestUtil;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.servlet.ServletHandler;import org.eclipse.jetty.servlet.ServletHolder;@@ -52,11 +52,13 @@ public class HttpReplicatorTest extends ReplicatorTestCase {private int port;private String host;private Directory serverIndexDir, handlerIndexDir;+ private ReplicationServlet replicationServlet;private void startServer() throws Exception {ServletHandler replicationHandler = new ServletHandler();ReplicationService service = new ReplicationService(Collections.singletonMap("s1", serverReplicator));- ServletHolder servlet = new ServletHolder(new ReplicationServlet(service));+ replicationServlet = new ReplicationServlet(service);+ ServletHolder servlet = new ServletHolder(replicationServlet);replicationHandler.addServletWithMapping(servlet, ReplicationService.REPLICATION_CONTEXT + "/*");server = newHttpServer(replicationHandler);port = serverPort(server);@@ -120,6 +122,38 @@ public class HttpReplicatorTest extends ReplicatorTestCase {client.updateNow();reopenReader();assertEquals(2, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16));++ client.close();}+ @Test+ public void testServerErrors() throws Exception {+ // tests the behaviour of the client when the server sends an error+ // must use BasicClientConnectionManager to test whether the client is closed correctly+ BasicClientConnectionManager conMgr = new BasicClientConnectionManager();+ Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", conMgr);+ ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null),+ new PerSessionDirectoryFactory(clientWorkDir));++ try {+ publishRevision(5);++ try {+ replicationServlet.setRespondWithError(true);+ client.updateNow();+ fail("expected exception");+ } catch (Throwable t) {+ // expected+ }++ replicationServlet.setRespondWithError(false);+ client.updateNow(); // now it should work+ reopenReader();+ assertEquals(5, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16));++ client.close();+ } finally {+ replicationServlet.setRespondWithError(false);+ }+ }}diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/http/ReplicationServlet.java b/lucene/replicator/src/test/org/apache/lucene/replicator/http/ReplicationServlet.javaindex 797d35dfdc9..11b34a71286 100644--- a/lucene/replicator/src/test/org/apache/lucene/replicator/http/ReplicationServlet.java+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/http/ReplicationServlet.java@@ -27,15 +27,23 @@ import javax.servlet.http.HttpServletResponse;public class ReplicationServlet extends HttpServlet {private final ReplicationService service;+ private boolean respondWithError = false;public ReplicationServlet(ReplicationService service) {- super();this.service = service;}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {- service.perform(req, resp);+ if (respondWithError) {+ resp.sendError(500, "Fake error");+ } else {+ service.perform(req, resp);+ }+ }++ public void setRespondWithError(boolean respondWithError) {+ this.respondWithError = respondWithError;}}