From 9a139bfe31ccdc5d6740c9c2dc11a66cce979f19 Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 12 Jun 2018 14:08:13 +0200 Subject: [PATCH] REST hl client: adjust wait_for_active_shards param in cluster health The default wait_for_active_shards is NONE for cluster health, which differs from all the other API in master, hence we need to make sure to set the parameter whenever it differs from NONE (0). The test around this also had a bug, which is why this was not originally uncovered. Relates to #29331 --- .../client/RequestConverters.java | 8 +++++-- .../client/RequestConvertersTests.java | 23 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index e2b61dc41deb0..93bf6a1a19881 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -721,7 +721,7 @@ static Request clusterHealth(ClusterHealthRequest healthRequest) { .withWaitForStatus(healthRequest.waitForStatus()) .withWaitForNoRelocatingShards(healthRequest.waitForNoRelocatingShards()) .withWaitForNoInitializingShards(healthRequest.waitForNoInitializingShards()) - .withWaitForActiveShards(healthRequest.waitForActiveShards()) + .withWaitForActiveShards(healthRequest.waitForActiveShards(), ActiveShardCount.NONE) .withWaitForNodes(healthRequest.waitForNodes()) .withWaitForEvents(healthRequest.waitForEvents()) .withTimeout(healthRequest.timeout()) @@ -1047,7 +1047,11 @@ Params withVersionType(VersionType versionType) { } Params withWaitForActiveShards(ActiveShardCount activeShardCount) { - if (activeShardCount != null && activeShardCount != ActiveShardCount.DEFAULT) { + return withWaitForActiveShards(activeShardCount, ActiveShardCount.DEFAULT); + } + + Params withWaitForActiveShards(ActiveShardCount activeShardCount, ActiveShardCount defaultActiveShardCount) { + if (activeShardCount != null && activeShardCount != defaultActiveShardCount) { return putParam("wait_for_active_shards", activeShardCount.toString().toLowerCase(Locale.ROOT)); } return this; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 1e03e55f61f13..5bc9bac96dcd7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -1562,7 +1562,7 @@ public void testClusterHealth() { default: throw new UnsupportedOperationException(); } - setRandomWaitForActiveShards(healthRequest::waitForActiveShards, expectedParams, "0"); + setRandomWaitForActiveShards(healthRequest::waitForActiveShards, ActiveShardCount.NONE, expectedParams); if (randomBoolean()) { ClusterHealthRequest.Level level = randomFrom(ClusterHealthRequest.Level.values()); healthRequest.level(level); @@ -2187,23 +2187,24 @@ private static void setRandomMasterTimeout(MasterNodeRequest request, Map setter, Map expectedParams) { - setRandomWaitForActiveShards(setter, expectedParams, null); + setRandomWaitForActiveShards(setter, ActiveShardCount.DEFAULT, expectedParams); } - private static void setRandomWaitForActiveShards(Consumer setter,Map expectedParams, - String defaultValue) { + private static void setRandomWaitForActiveShards(Consumer setter, ActiveShardCount defaultActiveShardCount, + Map expectedParams) { if (randomBoolean()) { + int waitForActiveShardsInt = randomIntBetween(-1, 5); String waitForActiveShardsString; - int waitForActiveShards = randomIntBetween(-1, 5); - if (waitForActiveShards == -1) { + if (waitForActiveShardsInt == -1) { waitForActiveShardsString = "all"; } else { - waitForActiveShardsString = String.valueOf(waitForActiveShards); + waitForActiveShardsString = String.valueOf(waitForActiveShardsInt); + } + ActiveShardCount activeShardCount = ActiveShardCount.parseString(waitForActiveShardsString); + setter.accept(activeShardCount); + if (defaultActiveShardCount.equals(activeShardCount) == false) { + expectedParams.put("wait_for_active_shards", waitForActiveShardsString); } - setter.accept(ActiveShardCount.parseString(waitForActiveShardsString)); - expectedParams.put("wait_for_active_shards", waitForActiveShardsString); - } else if (defaultValue != null) { - expectedParams.put("wait_for_active_shards", defaultValue); } }