diff --git a/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java index d6c0db846b5..7d490be4af5 100644 --- a/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -19,6 +19,10 @@ package org.elasticsearch.env; +import java.io.UncheckedIOException; +import java.util.Iterator; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -486,12 +490,27 @@ public final class NodeEnvironment implements Closeable { } private static boolean assertPathsDoNotExist(final Path[] paths) { - Set existingPaths = new HashSet<>(); - for (Path path : paths) { - if (FileSystemUtils.exists(path)) { - existingPaths.add(path); - } - } + Set existingPaths = Stream.of(paths) + .filter(FileSystemUtils::exists) + .filter(leftOver -> { + // Relaxed assertion for the special case where only the empty state directory exists after deleting + // the shard directory because it was created again as a result of a metadata read action concurrently. + try (DirectoryStream children = Files.newDirectoryStream(leftOver)) { + Iterator iter = children.iterator(); + if (iter.hasNext() == false) { + return true; + } + Path maybeState = iter.next(); + if (iter.hasNext() || maybeState.equals(leftOver.resolve(MetaDataStateFormat.STATE_DIR_NAME)) == false) { + return true; + } + try (DirectoryStream stateChildren = Files.newDirectoryStream(maybeState)) { + return stateChildren.iterator().hasNext(); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }).collect(Collectors.toSet()); assert existingPaths.size() == 0 : "Paths exist that should have been deleted: " + existingPaths; return existingPaths.size() == 0; } diff --git a/server/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java b/server/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java index 324b32fa6a9..c24ba2f8612 100644 --- a/server/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java +++ b/server/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java @@ -246,7 +246,6 @@ public class IndexRecoveryIT extends ESIntegTestCase { validateIndexRecoveryState(nodeBRecoveryState.getIndex()); } - @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/32686") @TestLogging( "_root:DEBUG," + "org.elasticsearch.cluster.service:TRACE,"