|
19 | 19 |
|
20 | 20 | package org.elasticsearch.env; |
21 | 21 |
|
| 22 | +import java.io.UncheckedIOException; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import java.util.stream.Stream; |
22 | 26 | import org.apache.logging.log4j.Logger; |
23 | 27 | import org.apache.logging.log4j.message.ParameterizedMessage; |
24 | 28 | import org.apache.lucene.index.IndexWriter; |
@@ -460,12 +464,27 @@ public void deleteShardDirectoryUnderLock(ShardLock lock, IndexSettings indexSet |
460 | 464 | } |
461 | 465 |
|
462 | 466 | private static boolean assertPathsDoNotExist(final Path[] paths) { |
463 | | - Set<Path> existingPaths = new HashSet<>(); |
464 | | - for (Path path : paths) { |
465 | | - if (FileSystemUtils.exists(paths)) { |
466 | | - existingPaths.add(path); |
467 | | - } |
468 | | - } |
| 467 | + Set<Path> existingPaths = Stream.of(paths) |
| 468 | + .filter(FileSystemUtils::exists) |
| 469 | + .filter(leftOver -> { |
| 470 | + // Relaxed assertion for the special case where only the empty state directory exists after deleting |
| 471 | + // the shard directory because it was created again as a result of a metadata read action concurrently. |
| 472 | + try (DirectoryStream<Path> children = Files.newDirectoryStream(leftOver)) { |
| 473 | + Iterator<Path> iter = children.iterator(); |
| 474 | + if (iter.hasNext() == false) { |
| 475 | + return true; |
| 476 | + } |
| 477 | + Path maybeState = iter.next(); |
| 478 | + if (iter.hasNext() || maybeState.equals(leftOver.resolve(MetaDataStateFormat.STATE_DIR_NAME)) == false) { |
| 479 | + return true; |
| 480 | + } |
| 481 | + try (DirectoryStream<Path> stateChildren = Files.newDirectoryStream(maybeState)) { |
| 482 | + return stateChildren.iterator().hasNext(); |
| 483 | + } |
| 484 | + } catch (IOException e) { |
| 485 | + throw new UncheckedIOException(e); |
| 486 | + } |
| 487 | + }).collect(Collectors.toSet()); |
469 | 488 | assert existingPaths.size() == 0 : "Paths exist that should have been deleted: " + existingPaths; |
470 | 489 | return existingPaths.size() == 0; |
471 | 490 | } |
|
0 commit comments