|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.transport.rest5_client.low_level; |
| 21 | + |
| 22 | +import co.elastic.clients.transport.rest5_client.SafeResponseConsumer; |
| 23 | +import com.sun.net.httpserver.HttpServer; |
| 24 | +import org.apache.hc.core5.http.ContentType; |
| 25 | +import org.apache.hc.core5.http.HttpHost; |
| 26 | +import org.apache.hc.core5.http.HttpResponse; |
| 27 | +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; |
| 28 | +import org.apache.hc.core5.http.message.BasicClassicHttpResponse; |
| 29 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 30 | +import org.junit.jupiter.api.AfterAll; |
| 31 | +import org.junit.jupiter.api.Assertions; |
| 32 | +import org.junit.jupiter.api.BeforeAll; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import java.net.InetAddress; |
| 36 | +import java.net.InetSocketAddress; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | + |
| 39 | +public class SafeResponseConsumerTest { |
| 40 | + |
| 41 | + static HttpServer Server; |
| 42 | + static HttpHost ESHost; |
| 43 | + |
| 44 | + // A consumer factory that throws an Error, to simulate the effect of an OOME |
| 45 | + static HttpAsyncResponseConsumerFactory FailingConsumerFactory = |
| 46 | + () -> new BasicAsyncResponseConsumer(new BufferedByteConsumer(100 * 1024 * 1024)) { |
| 47 | + @Override |
| 48 | + public void informationResponse(HttpResponse response, HttpContext context) { |
| 49 | + super.informationResponse(response, context); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected BasicClassicHttpResponse buildResult(HttpResponse response, ByteArrayEntity entity, |
| 54 | + ContentType contentType) { |
| 55 | + super.buildResult(response, entity, contentType); |
| 56 | + throw new Error("Error in buildResult"); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + @BeforeAll |
| 61 | + public static void setup() throws Exception { |
| 62 | + Server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); |
| 63 | + Server.start(); |
| 64 | + |
| 65 | + Server.createContext("/", exchange -> { |
| 66 | + String path = exchange.getRequestURI().getPath(); |
| 67 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 68 | + exchange.getResponseHeaders().set("X-Elastic-Product", "Elasticsearch"); |
| 69 | + |
| 70 | + if (path.equals("/")) { |
| 71 | + byte[] bytes = Info.getBytes(StandardCharsets.UTF_8); |
| 72 | + exchange.sendResponseHeaders(200, bytes.length); |
| 73 | + exchange.getResponseBody().write(bytes); |
| 74 | + exchange.close(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + exchange.sendResponseHeaders(404, -1); |
| 79 | + exchange.close(); |
| 80 | + }); |
| 81 | + |
| 82 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 83 | + try { |
| 84 | + Server.stop(1); |
| 85 | + } catch (Exception e) { |
| 86 | + // Ignore |
| 87 | + } |
| 88 | + })); |
| 89 | + |
| 90 | + ESHost = new HttpHost(Server.getAddress().getAddress(), Server.getAddress().getPort()); |
| 91 | + } |
| 92 | + |
| 93 | + @AfterAll |
| 94 | + public static void tearDown() { |
| 95 | + Server.stop(0); |
| 96 | + } |
| 97 | + |
| 98 | + // testReactorDeath cannot be tested, as the io reactor thread gets stuck and the test never completes |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testReactorSurvival() throws Exception { |
| 102 | + |
| 103 | + // Request options that will simulate an OOME and wrapped in the safe consumer that will |
| 104 | + // avoid the reactor's death |
| 105 | + RequestOptions.Builder protectedFailingOptionsBuilder = RequestOptions.DEFAULT.toBuilder(); |
| 106 | + protectedFailingOptionsBuilder.setHttpAsyncResponseConsumerFactory(() -> |
| 107 | + new SafeResponseConsumer<>(FailingConsumerFactory.createHttpAsyncResponseConsumer()) |
| 108 | + ); |
| 109 | + RequestOptions protectedFailingOptions = protectedFailingOptionsBuilder.build(); |
| 110 | + |
| 111 | + Rest5Client restClient = Rest5Client.builder(ESHost).build(); |
| 112 | + |
| 113 | + // First request, to warm things up. |
| 114 | + // An "indice exists" request, that has no response body |
| 115 | + Request existsReq = new Request("HEAD", "/index-name"); |
| 116 | + restClient.performRequest(existsReq); |
| 117 | + |
| 118 | + try { |
| 119 | + Request infoReq = new Request("GET", "/"); |
| 120 | + infoReq.setOptions(protectedFailingOptions); |
| 121 | + |
| 122 | + restClient.performRequest(infoReq); |
| 123 | + Assertions.fail("First request should not succeed"); |
| 124 | + } catch (Exception t) { |
| 125 | + System.err.println("Request 1 error"); |
| 126 | + } |
| 127 | + { |
| 128 | + // 2nd request with no specific options |
| 129 | + Request infoReq = new Request("GET", "/"); |
| 130 | + |
| 131 | + Response resp = restClient.performRequest(infoReq); |
| 132 | + Assertions.assertEquals(200, resp.getStatusCode()); |
| 133 | + } |
| 134 | + { |
| 135 | + // final request to make sure that the reactor isn't closed |
| 136 | + restClient.performRequest(existsReq); |
| 137 | + } |
| 138 | + restClient.close(); |
| 139 | + } |
| 140 | + |
| 141 | + private static final String Info = "{\n" + |
| 142 | + " \"cluster_name\": \"foo\",\n" + |
| 143 | + " \"cluster_uuid\": \"bar\",\n" + |
| 144 | + " \"version\": {\n" + |
| 145 | + " \"build_date\": \"2022-01-28T08:36:04.875279988Z\",\n" + |
| 146 | + " \"minimum_wire_compatibility_version\": \"6.8.0\",\n" + |
| 147 | + " \"build_hash\": \"bee86328705acaa9a6daede7140defd4d9ec56bd\",\n" + |
| 148 | + " \"number\": \"7.17.0\",\n" + |
| 149 | + " \"lucene_version\": \"8.11.1\",\n" + |
| 150 | + " \"minimum_index_compatibility_version\": \"6.0.0-beta1\",\n" + |
| 151 | + " \"build_flavor\": \"default\",\n" + |
| 152 | + " \"build_snapshot\": false,\n" + |
| 153 | + " \"build_type\": \"docker\"\n" + |
| 154 | + " },\n" + |
| 155 | + " \"name\": \"instance-0000000000\",\n" + |
| 156 | + " \"tagline\": \"You Know, for Search\"\n" + |
| 157 | + "}"; |
| 158 | +} |
0 commit comments