2020package co .elastic .clients .transport ;
2121
2222import co .elastic .clients .elasticsearch .ElasticsearchClient ;
23+ import co .elastic .clients .elasticsearch .cat .IndicesResponse ;
2324import co .elastic .clients .json .jackson .JacksonJsonpMapper ;
2425import co .elastic .clients .transport .http .RepeatableBodyResponse ;
2526import co .elastic .clients .transport .rest_client .RestClientOptions ;
2627import co .elastic .clients .transport .rest_client .RestClientTransport ;
28+ import co .elastic .clients .transport .rest_client .RetryRestClientHttpClient ;
2729import co .elastic .clients .util .BinaryData ;
2830import com .sun .net .httpserver .HttpServer ;
2931import org .apache .http .HttpHost ;
3032import org .elasticsearch .client .RequestOptions ;
3133import org .elasticsearch .client .Response ;
34+ import org .elasticsearch .client .ResponseException ;
3235import org .elasticsearch .client .RestClient ;
3336import org .junit .jupiter .api .Assertions ;
3437import org .junit .jupiter .api .Test ;
3942import java .net .InetAddress ;
4043import java .net .InetSocketAddress ;
4144import java .nio .charset .StandardCharsets ;
45+ import java .util .Arrays ;
4246import java .util .Collections ;
47+ import java .util .concurrent .atomic .AtomicInteger ;
4348
4449import static co .elastic .clients .util .ContentType .APPLICATION_JSON ;
4550
@@ -122,7 +127,8 @@ public void testOriginalJsonBodyRetrievalException() throws Exception {
122127 assertNotEquals (RepeatableBodyResponse .class , ex .response ().getClass ());
123128
124129 // setting transport option
125- RestClientOptions options = new RestClientOptions (RequestOptions .DEFAULT , true ,BackoffPolicy .noBackoff ());
130+ RestClientOptions options = new RestClientOptions (RequestOptions .DEFAULT , true ,
131+ BackoffPolicy .noBackoff ());
126132
127133 ElasticsearchTransport transport = new RestClientTransport (
128134 restClient , new JacksonJsonpMapper (), options );
@@ -139,17 +145,82 @@ public void testOriginalJsonBodyRetrievalException() throws Exception {
139145 assertEquals (200 , ex .statusCode ());
140146 assertEquals (RepeatableBodyResponse .class , ex .response ().getClass ());
141147
142- try (RepeatableBodyResponse repeatableResponse = (RepeatableBodyResponse ) ex .response ()){
148+ try (RepeatableBodyResponse repeatableResponse = (RepeatableBodyResponse ) ex .response ()) {
143149 BinaryData body = repeatableResponse .body ();
144- StringBuilder sb = new StringBuilder ();
145- BufferedReader br = new BufferedReader (new InputStreamReader (body .asInputStream ()));
146- String read ;
147-
148- while ((read = br .readLine ()) != null ) {
149- sb .append (read );
150- }
151- br .close ();
152- assertEquals ("definitely not json" ,sb .toString ());
150+ StringBuilder sb = new StringBuilder ();
151+ BufferedReader br = new BufferedReader (new InputStreamReader (body .asInputStream ()));
152+ String read ;
153+
154+ while ((read = br .readLine ()) != null ) {
155+ sb .append (read );
156+ }
157+ br .close ();
158+ assertEquals ("definitely not json" , sb .toString ());
153159 }
154160 }
161+
162+
163+ @ Test
164+ public void testRetryClientSync () throws Exception {
165+ HttpServer httpServer = HttpServer .create (new InetSocketAddress (InetAddress .getLoopbackAddress (),
166+ 0 ), 0 );
167+
168+ // server will return success after 7 retries
169+ AtomicInteger errorCounter = new AtomicInteger ();
170+
171+ httpServer .createContext ("/_cat/indices" , exchange -> {
172+ exchange .getResponseHeaders ().put ("Content-Type" , Collections .singletonList (APPLICATION_JSON ));
173+ exchange .getResponseHeaders ().put ("X-Elastic-Product" , Collections .singletonList ("Elasticsearch"
174+ ));
175+ if (errorCounter .get ()>6 ){
176+ exchange .sendResponseHeaders (200 , 0 );
177+ OutputStream out = exchange .getResponseBody ();
178+ String jsonRes = " [{\n " +
179+ " \" health\" : \" green\" ,\n " +
180+ " \" status\" : \" open\" ,\n " +
181+ " \" index\" : \" test\" ,\n " +
182+ " \" uuid\" : \" 3iSkOlZAQVq2ir1hOtaVlw\" ,\n " +
183+ " \" pri\" : \" 1\" ,\n " +
184+ " \" rep\" : \" 1\" ,\n " +
185+ " \" docs.count\" : \" 5\" ,\n " +
186+ " \" docs.deleted\" : \" 0\" ,\n " +
187+ " \" store.size\" : \" 8.8kb\" ,\n " +
188+ " \" pri.store.size\" : \" 4.4kb\" ,\n " +
189+ " \" dataset.size\" : \" 4.4kb\" \n " +
190+ " }]" ;
191+ out .write (jsonRes .getBytes (StandardCharsets .UTF_8 ));
192+ out .close ();
193+ }
194+ else {
195+ exchange .sendResponseHeaders (503 , 0 );
196+ OutputStream out = exchange .getResponseBody ();
197+ out .write ("{}" .getBytes (StandardCharsets .UTF_8 ));
198+ out .close ();
199+ errorCounter .incrementAndGet ();
200+ }
201+ });
202+
203+ httpServer .start ();
204+ InetSocketAddress address = httpServer .getAddress ();
205+
206+ RestClient restClient = RestClient
207+ .builder (new HttpHost (address .getHostString (), address .getPort (), "http" ))
208+ .build ();
209+
210+ // setting transport option
211+ RestClientOptions options = new RestClientOptions (RequestOptions .DEFAULT , false ,
212+ BackoffPolicy .constantBackoff (50L ,8 ));
213+
214+ ElasticsearchTransport transport = new RestClientTransport (
215+ restClient , new JacksonJsonpMapper (), options );
216+
217+ ElasticsearchClient esClient = new ElasticsearchClient (transport );
218+
219+ IndicesResponse res = esClient .cat ().indices ();
220+
221+ httpServer .stop (0 );
222+
223+ assertTrue (errorCounter .get () == 7 );
224+ assertEquals ("test" , res .valueBody ().get (0 ).index ());
225+ }
155226}
0 commit comments