From 13f35b7248e20d8983786b50e6d00a1cc4600af2 Mon Sep 17 00:00:00 2001 From: David Turner Date: Tue, 13 Jul 2021 16:25:26 +0100 Subject: [PATCH] Fix testPreferCopyWithHighestMatchingOperations (#75170) In #74081 this test failed with a `NoNodeAvailableException` within the `indexRandom()` call immediately after stopping a node. This could happen if the `node-left` event wasn't fully applied before calling `indexRandom()` with an empty list of docs but with `forceRefresh` set to true: since there's no docs, the replica wouldn't be marked as stale, so the final refresh would detect the missing node, failing its `assertNoFailures` wrapper. This commit avoids calling `indexRandom()` with no docs in this location. It also enhances `assertNoFailures` to report the details of each failure, rather than just the summary. Closes #74081 --- .../elasticsearch/gateway/ReplicaShardAllocatorIT.java | 4 +++- .../test/hamcrest/ElasticsearchAssertions.java | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/gateway/ReplicaShardAllocatorIT.java b/server/src/internalClusterTest/java/org/elasticsearch/gateway/ReplicaShardAllocatorIT.java index 2dac552cfdc51..8156ffbd70cc1 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/gateway/ReplicaShardAllocatorIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/gateway/ReplicaShardAllocatorIT.java @@ -263,8 +263,10 @@ public void testPreferCopyWithHighestMatchingOperations() throws Exception { String nodeWithHigherMatching = randomFrom(internalCluster().nodesInclude(indexName)); Settings nodeWithHigherMatchingSettings = internalCluster().dataPathSettings(nodeWithHigherMatching); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeWithHigherMatching)); - indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, between(0, 100)) + if (usually()) { + indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, between(1, 100)) .mapToObj(n -> client().prepareIndex(indexName, "_doc").setSource("f", "v")).collect(Collectors.toList())); + } assertAcked(client().admin().cluster().prepareUpdateSettings() .setPersistentSettings(Settings.builder().put("cluster.routing.allocation.enable", "primaries").build())); diff --git a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java index d9bfe3f5f0f19..82c59f9370f68 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java +++ b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java @@ -329,7 +329,15 @@ public static void assertFailures(SearchRequestBuilder searchRequestBuilder, Res } public static void assertNoFailures(BroadcastResponse response) { - assertThat("Unexpected ShardFailures: " + Arrays.toString(response.getShardFailures()), response.getFailedShards(), equalTo(0)); + if (response.getFailedShards() != 0) { + final AssertionError assertionError = new AssertionError("[" + response.getFailedShards() + "] shard failures"); + + for (DefaultShardOperationFailedException shardFailure : response.getShardFailures()) { + assertionError.addSuppressed(new ElasticsearchException(shardFailure.toString(), shardFailure.getCause())); + } + + throw assertionError; + } } public static void assertAllSuccessful(BroadcastResponse response) {