From 4b93f17b24a6107ed48aad44d94efa98e9c56369 Mon Sep 17 00:00:00 2001 From: Hendrik Muhs Date: Mon, 27 Apr 2020 17:16:23 +0200 Subject: [PATCH] [Transform] improve TransformRestTestCase robustness (#55786) handles/retries temporary SearchPhaseExecutionErrors fixes #54810 --- .../integration/TransformRestTestCase.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/test/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java b/x-pack/plugin/transform/qa/single-node-tests/src/test/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java index f357df2476cc..1f541c94b0fa 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/test/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/test/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java @@ -487,7 +487,7 @@ protected static String getTransformEndpoint() { } @SuppressWarnings("unchecked") - private void logAudits() throws IOException { + private void logAudits() throws Exception { logger.info("writing audit messages to the log"); Request searchRequest = new Request("GET", TransformInternalIndexConstants.AUDIT_INDEX + "/_search?ignore_unavailable=true"); searchRequest.setJsonEntity( @@ -501,23 +501,36 @@ private void logAudits() throws IOException { + " ] }" ); - refreshIndex(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN); - - Response searchResponse = client().performRequest(searchRequest); - Map searchResult = entityAsMap(searchResponse); - List> searchHits = (List>) XContentMapValues.extractValue("hits.hits", searchResult); - - for (Map hit : searchHits) { - Map source = (Map) XContentMapValues.extractValue("_source", hit); - String level = (String) source.getOrDefault("level", "info"); - logger.log( - Level.getLevel(level.toUpperCase(Locale.ROOT)), - "Transform audit: [{}] [{}] [{}] [{}]", - Instant.ofEpochMilli((long) source.getOrDefault("timestamp", 0)), - source.getOrDefault("transform_id", "n/a"), - source.getOrDefault("message", "n/a"), - source.getOrDefault("node_name", "n/a") - ); - } + assertBusy(() -> { + try { + refreshIndex(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN); + Response searchResponse = client().performRequest(searchRequest); + + Map searchResult = entityAsMap(searchResponse); + List> searchHits = (List>) XContentMapValues.extractValue( + "hits.hits", + searchResult + ); + + for (Map hit : searchHits) { + Map source = (Map) XContentMapValues.extractValue("_source", hit); + String level = (String) source.getOrDefault("level", "info"); + logger.log( + Level.getLevel(level.toUpperCase(Locale.ROOT)), + "Transform audit: [{}] [{}] [{}] [{}]", + Instant.ofEpochMilli((long) source.getOrDefault("timestamp", 0)), + source.getOrDefault("transform_id", "n/a"), + source.getOrDefault("message", "n/a"), + source.getOrDefault("node_name", "n/a") + ); + } + } catch (ResponseException e) { + // see gh#54810, wrap temporary 503's as assertion error for retry + if (e.getResponse().getStatusLine().getStatusCode() != 503) { + throw e; + } + throw new AssertionError("Failed to retrieve audit logs", e); + } + }, 5, TimeUnit.SECONDS); } }