Browse Source

ConnectTransportException returns retryable BAD_GATEWAY (#118681) (#119146)

ConnectTransportException and its subclasses previous translated to a
INTERNAL_SERVER_ERROR HTTP 500 code. We are changing it to 502
BAD_GATEWAY so that users may choose to retry it on connectivity
issues.

Related ES-10214
Closes #118320

(cherry picked from commit 517abe4ffdceb3c7ca771207fe20b0c813181810)

Co-authored-by: Dianna Hohensee <dianna.hohensee@elastic.co>
Pawan Kartik 9 months ago
parent
commit
e777dbaa7c

+ 6 - 0
docs/changelog/118681.yaml

@@ -0,0 +1,6 @@
+pr: 118681
+summary: '`ConnectTransportException` returns retryable BAD_GATEWAY'
+area: Network
+type: enhancement
+issues:
+ - 118320

+ 13 - 0
server/src/main/java/org/elasticsearch/transport/ConnectTransportException.java

@@ -13,6 +13,7 @@ import org.elasticsearch.TransportVersions;
 import org.elasticsearch.cluster.node.DiscoveryNode;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
+import org.elasticsearch.rest.RestStatus;
 
 import java.io.IOException;
 
@@ -41,6 +42,18 @@ public class ConnectTransportException extends ActionTransportException {
         }
     }
 
+    /**
+     * The ES REST API is a gateway to a single or multiple clusters. If there is an error connecting to other servers, then we should
+     * return a 502 BAD_GATEWAY status code instead of the parent class' 500 INTERNAL_SERVER_ERROR. Clients tend to retry on a 502 but not
+     * on a 500, and retrying may help on a connection error.
+     *
+     * @return a {@link RestStatus#BAD_GATEWAY} code
+     */
+    @Override
+    public final RestStatus status() {
+        return RestStatus.BAD_GATEWAY;
+    }
+
     @Override
     protected void writeTo(StreamOutput out, Writer<Throwable> nestedExceptionsWriter) throws IOException {
         super.writeTo(out, nestedExceptionsWriter);

+ 1 - 0
server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java

@@ -410,6 +410,7 @@ public class ExceptionSerializationTests extends ESTestCase {
         ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
         assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
         assertThat(ex.getCause(), instanceOf(NullPointerException.class));
+        assertEquals(RestStatus.BAD_GATEWAY, ex.status());
     }
 
     public void testSearchPhaseExecutionException() throws IOException {