Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-8578: Successful or not, requests are not always fully consumed by Solrj clients and we count on HttpClient or the JVM.

markrmillera8bc427aac85
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 17272110c7d..860e127d8ef 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -513,6 +513,9 @@ Bug Fixes
* SOLR-8551: Make collection deletion more robust. (Mark Miller)
+* SOLR-8578: Successful or not, requests are not always fully consumed by Solrj clients and we
+ count on HttpClient or the JVM. (Mark Miller)
+
Optimizations
----------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java
index 2762a9a4ea4..5bec96a27f5 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java
@@ -23,7 +23,6 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
-import org.apache.http.util.EntityUtils;
import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
@@ -41,6 +40,7 @@ import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.IOUtils;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SolrjNamedThreadFactory;
+import org.apache.solr.common.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
@@ -314,7 +314,7 @@ public class ConcurrentUpdateSolrClient extends SolrClient {
} finally {
try {
if (response != null) {
- EntityUtils.consume(response.getEntity());
+ Utils.consumeFully(response.getEntity());
}
} catch (Exception e) {
log.error("Error consuming and closing http response stream.", e);
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
index fccac9d1c63..df99560ffd0 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
@@ -56,6 +56,7 @@ import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SolrjNamedThreadFactory;
+import org.apache.solr.common.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
@@ -590,11 +591,7 @@ public class HttpSolrClient extends SolrClient {
"IOException occured when talking to server at: " + getBaseURL(), e);
} finally {
if (shouldClose) {
- try {
- EntityUtils.consume(entity);
- } catch (IOException e) {
- log.error("Error consuming and closing http response stream.", e);
- }
+ Utils.consumeFully(entity);
}
}
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
index c807dbefcc4..03b8b2c7656 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
@@ -17,7 +17,9 @@
package org.apache.solr.common.util;
import java.io.IOException;
+import java.io.InputStream;
import java.io.StringReader;
+import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
@@ -31,16 +33,22 @@ import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.http.HttpEntity;
+import org.apache.http.util.EntityUtils;
import org.apache.solr.common.SolrException;
import org.noggit.CharArr;
import org.noggit.JSONParser;
import org.noggit.JSONWriter;
import org.noggit.ObjectBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;
public class Utils {
+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
public static Map getDeepCopy(Map map, int maxDepth) {
return getDeepCopy(map, maxDepth, true);
}
@@ -161,6 +169,39 @@ public class Utils {
return false;
}
+
+ /**
+ * If the passed entity has content, make sure it is fully
+ * read and closed.
+ *
+ * @param entity to consume or null
+ */
+ public static void consumeFully(HttpEntity entity) {
+ if (entity != null) {
+ try {
+ // make sure the stream is full read
+ readFully(entity.getContent());
+ } catch (UnsupportedOperationException e) {
+ // nothing to do then
+ } catch (IOException e) {
+ // quiet
+ } finally {
+ // close the stream
+ EntityUtils.consumeQuietly(entity);
+ }
+ }
+ }
+
+ /**
+ * Make sure the InputStream is fully read.
+ *
+ * @param is to read
+ * @throws IOException on problem with IO
+ */
+ private static void readFully(InputStream is) throws IOException {
+ is.skip(is.available());
+ while (is.read() != -1) {}
+ }
public static final Pattern ARRAY_ELEMENT_INDEX = Pattern
.compile("(\\S*?)\\[(\\d+)\\]");