From f6dacc77bf28749aba2ee7d358dfd1a3a49880f3 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Wed, 20 May 2020 09:44:40 -0500 Subject: [PATCH 1/2] Handle exceptions when building _cat/indices response (#56993) --- .../test/cat.indices/10_basic.yml | 16 +++++++++ .../rest/action/cat/RestIndicesAction.java | 36 +++++++++++-------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml index 4da58c2ed4d0d..5ad29d9fdecf8 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml @@ -273,3 +273,19 @@ open \s+ foo\n open \s+ baz\n $/ + +--- +"Test cat indices with invalid health parameter": + + - do: + indices.create: + index: foo + body: + settings: + number_of_shards: "1" + number_of_replicas: "0" + + - do: + catch: bad_request + cat.indices: + health: "invalid-health-value" diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java index 5d5c792d24da4..6a8f2842f5f6a 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java @@ -216,21 +216,27 @@ private GroupedActionListener createGroupedListener(final RestRe return new GroupedActionListener<>(new ActionListener>() { @Override public void onResponse(final Collection responses) { - GetSettingsResponse settingsResponse = extractResponse(responses, GetSettingsResponse.class); - Map indicesSettings = StreamSupport.stream(settingsResponse.getIndexToSettings().spliterator(), false) - .collect(Collectors.toMap(cursor -> cursor.key, cursor -> cursor.value)); - - ClusterStateResponse stateResponse = extractResponse(responses, ClusterStateResponse.class); - Map indicesStates = StreamSupport.stream(stateResponse.getState().getMetaData().spliterator(), false) - .collect(Collectors.toMap(indexMetaData -> indexMetaData.getIndex().getName(), Function.identity())); - - ClusterHealthResponse healthResponse = extractResponse(responses, ClusterHealthResponse.class); - Map indicesHealths = healthResponse.getIndices(); - - IndicesStatsResponse statsResponse = extractResponse(responses, IndicesStatsResponse.class); - Map indicesStats = statsResponse.getIndices(); - - listener.onResponse(buildTable(request, indicesSettings, indicesHealths, indicesStats, indicesStates)); + try { + GetSettingsResponse settingsResponse = extractResponse(responses, GetSettingsResponse.class); + Map indicesSettings = StreamSupport.stream(settingsResponse.getIndexToSettings().spliterator(), false) + .collect(Collectors.toMap(cursor -> cursor.key, cursor -> cursor.value)); + + ClusterStateResponse stateResponse = extractResponse(responses, ClusterStateResponse.class); + Map indicesStates = + StreamSupport.stream(stateResponse.getState().getMetadata().spliterator(), false) + .collect(Collectors.toMap(indexMetadata -> indexMetadata.getIndex().getName(), Function.identity())); + + ClusterHealthResponse healthResponse = extractResponse(responses, ClusterHealthResponse.class); + Map indicesHealths = healthResponse.getIndices(); + + IndicesStatsResponse statsResponse = extractResponse(responses, IndicesStatsResponse.class); + Map indicesStats = statsResponse.getIndices(); + + Table responseTable = buildTable(request, indicesSettings, indicesHealths, indicesStats, indicesStates); + listener.onResponse(responseTable); + } catch (Exception e) { + onFailure(e); + } } @Override From 284dedcc58d3b1eeec609f7437411d8fca997080 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Tue, 26 May 2020 08:07:13 -0500 Subject: [PATCH 2/2] Catch exceptions when building response --- .../resources/rest-api-spec/test/cat.indices/10_basic.yml | 3 +++ .../org/elasticsearch/rest/action/cat/RestIndicesAction.java | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml index 5ad29d9fdecf8..7b62bd90052ac 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml @@ -276,6 +276,9 @@ --- "Test cat indices with invalid health parameter": + - skip: + version: " - 7.7.1" + reason: "fixed in 7.7.1+" - do: indices.create: diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java index 6a8f2842f5f6a..e4ee2c8a713d6 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java @@ -222,8 +222,8 @@ public void onResponse(final Collection responses) { .collect(Collectors.toMap(cursor -> cursor.key, cursor -> cursor.value)); ClusterStateResponse stateResponse = extractResponse(responses, ClusterStateResponse.class); - Map indicesStates = - StreamSupport.stream(stateResponse.getState().getMetadata().spliterator(), false) + Map indicesStates = + StreamSupport.stream(stateResponse.getState().getMetaData().spliterator(), false) .collect(Collectors.toMap(indexMetadata -> indexMetadata.getIndex().getName(), Function.identity())); ClusterHealthResponse healthResponse = extractResponse(responses, ClusterHealthResponse.class);