Apache Solr open-source search software
SOLR-17394: Check response status in IndexFetcher (#2621)
Jason Gerlowskif33fc60febf5
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex cdedd6d51a3..9c4cd6e2603 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -254,6 +254,8 @@ Bug Fixes* SOLR-17337: Display all custom distributed stages in debug output. (Torsten Bøgh Köster, Christine Poerschke)+* SOLR-17394: Detect and handle non-200 HTTP status codes for requests made by IndexFetcher (Jason Gerlowski)+Dependency Upgrades---------------------(No changes)diff --git a/solr/core/src/java/org/apache/solr/handler/IndexFetcher.java b/solr/core/src/java/org/apache/solr/handler/IndexFetcher.javaindex 662a94943c9..d439f7196cd 100644--- a/solr/core/src/java/org/apache/solr/handler/IndexFetcher.java+++ b/solr/core/src/java/org/apache/solr/handler/IndexFetcher.java@@ -71,6 +71,7 @@ import java.util.HashSet;import java.util.List;import java.util.Locale;import java.util.Map;+import java.util.Optional;import java.util.Properties;import java.util.Set;import java.util.concurrent.CountDownLatch;@@ -1988,16 +1989,38 @@ public class IndexFetcher {req.setBasePath(leaderBaseUrl);if (useExternalCompression) req.addHeader("Accept-Encoding", "gzip");response = solrClient.request(req, leaderCoreName);+ final var responseStatus = (Integer) response.get("responseStatus");is = (InputStream) response.get("stream");++ if (responseStatus != 200) {+ final var errorMsg =+ String.format(+ Locale.ROOT,+ "Unexpected status code [%d] when downloading file [%s].",+ responseStatus,+ fileName);+ closeStreamAndThrowIOE(is, errorMsg, Optional.empty());+ }+if (useInternalCompression) {is = new InflaterInputStream(is);}return new FastInputStream(is);} catch (Exception e) {- // close stream on error- IOUtils.closeQuietly(is);- throw new IOException("Could not download file '" + fileName + "'", e);+ closeStreamAndThrowIOE(is, "Could not download file '" + fileName + "'", Optional.of(e));+ }++ // Unreachable b/c closeStreamAndThrowIOE will always throw+ return null;+ }++ private void closeStreamAndThrowIOE(+ InputStream is, String exceptionMessage, Optional<Exception> e) throws IOException {+ IOUtils.closeQuietly(is);+ if (e.isPresent()) {+ throw new IOException(exceptionMessage, e.get());}+ throw new IOException(exceptionMessage);}}