171
171
172
172
import java .io .IOException ;
173
173
import java .nio .ByteBuffer ;
174
+ import java .nio .file .DirectoryNotEmptyException ;
175
+ import java .nio .file .FileVisitResult ;
174
176
import java .nio .file .Files ;
175
177
import java .nio .file .Path ;
178
+ import java .nio .file .SimpleFileVisitor ;
179
+ import java .nio .file .attribute .BasicFileAttributes ;
176
180
import java .util .Collection ;
177
181
import java .util .Collections ;
178
182
import java .util .Comparator ;
@@ -538,6 +542,7 @@ private void assertNoStaleRepositoryData() throws IOException {
538
542
repos = reposDir .filter (s -> s .getFileName ().toString ().startsWith ("extra" ) == false ).collect (Collectors .toList ());
539
543
}
540
544
for (Path repoRoot : repos ) {
545
+ cleanupEmptyTrees (repoRoot );
541
546
final Path latestIndexGenBlob = repoRoot .resolve ("index.latest" );
542
547
assertTrue ("Could not find index.latest blob for repo at [" + repoRoot + ']' , Files .exists (latestIndexGenBlob ));
543
548
final long latestGen = ByteBuffer .wrap (Files .readAllBytes (latestIndexGenBlob )).getLong (0 );
@@ -553,6 +558,35 @@ private void assertNoStaleRepositoryData() throws IOException {
553
558
}
554
559
}
555
560
561
+ // Lucene's mock file system randomly generates empty `extra0` files that break the deletion of blob-store directories.
562
+ // We clean those up here before checking a blob-store for stale files in this test.
563
+ private void cleanupEmptyTrees (Path repoPath ) {
564
+ try {
565
+ Files .walkFileTree (repoPath , new SimpleFileVisitor <>() {
566
+
567
+ @ Override
568
+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
569
+ if (file .getFileName ().toString ().startsWith ("extra" )) {
570
+ Files .delete (file );
571
+ }
572
+ return FileVisitResult .CONTINUE ;
573
+ }
574
+
575
+ @ Override
576
+ public FileVisitResult postVisitDirectory (Path dir , IOException exc ) throws IOException {
577
+ try {
578
+ Files .delete (dir );
579
+ } catch (DirectoryNotEmptyException e ) {
580
+ // We're only interested in deleting empty trees here, just ignore directories with content
581
+ }
582
+ return FileVisitResult .CONTINUE ;
583
+ }
584
+ });
585
+ } catch (IOException e ) {
586
+ throw new AssertionError (e );
587
+ }
588
+ }
589
+
556
590
private static void assertIndexGenerations (Path repoRoot , long latestGen ) throws IOException {
557
591
try (Stream <Path > repoRootBlobs = Files .list (repoRoot )) {
558
592
final long [] indexGenerations = repoRootBlobs .filter (p -> p .getFileName ().toString ().startsWith ("index-" ))
0 commit comments