use of org.apache.lucene.mockfile.WindowsFS in project lucene-solr by apache.
the class TestWindowsFS method testOpenDeleteConcurrently.
public void testOpenDeleteConcurrently() throws IOException, Exception {
final Path dir = wrap(createTempDir());
final Path file = dir.resolve("thefile");
final CyclicBarrier barrier = new CyclicBarrier(2);
final AtomicBoolean stopped = new AtomicBoolean(false);
Thread t = new Thread() {
@Override
public void run() {
try {
barrier.await();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
while (stopped.get() == false) {
try {
if (random().nextBoolean()) {
Files.delete(file);
} else if (random().nextBoolean()) {
Files.deleteIfExists(file);
} else {
Path target = file.resolveSibling("other");
Files.move(file, target);
Files.delete(target);
}
} catch (IOException ex) {
// continue
}
}
}
};
t.start();
barrier.await();
try {
final int iters = 10 + random().nextInt(100);
for (int i = 0; i < iters; i++) {
boolean opened = false;
try (OutputStream stream = Files.newOutputStream(file)) {
opened = true;
stream.write(0);
// just create
} catch (FileNotFoundException | NoSuchFileException ex) {
assertEquals("File handle leaked - file is closed but still registered", 0, ((WindowsFS) dir.getFileSystem().provider()).openFiles.size());
assertFalse("caught FNF on close", opened);
}
assertEquals("File handle leaked - file is closed but still registered", 0, ((WindowsFS) dir.getFileSystem().provider()).openFiles.size());
Files.deleteIfExists(file);
}
} finally {
stopped.set(true);
t.join();
}
}
use of org.apache.lucene.mockfile.WindowsFS in project lucene-solr by apache.
the class TestUtil method hasWindowsFS.
public static boolean hasWindowsFS(Directory dir) {
dir = FilterDirectory.unwrap(dir);
if (dir instanceof FSDirectory) {
Path path = ((FSDirectory) dir).getDirectory();
FileSystem fs = path.getFileSystem();
while (fs instanceof FilterFileSystem) {
FilterFileSystem ffs = (FilterFileSystem) fs;
if (ffs.getParent() instanceof WindowsFS) {
return true;
}
fs = ffs.getDelegate();
}
}
return false;
}
use of org.apache.lucene.mockfile.WindowsFS in project lucene-solr by apache.
the class TestUtil method hasWindowsFS.
public static boolean hasWindowsFS(Path path) {
FileSystem fs = path.getFileSystem();
while (fs instanceof FilterFileSystem) {
FilterFileSystem ffs = (FilterFileSystem) fs;
if (ffs.getParent() instanceof WindowsFS) {
return true;
}
fs = ffs.getDelegate();
}
return false;
}
use of org.apache.lucene.mockfile.WindowsFS in project lucene-solr by apache.
the class TestIndexWriter method testDeleteUnusedFiles.
public void testDeleteUnusedFiles() throws Exception {
assumeFalse("test relies on exact filenames", Codec.getDefault() instanceof SimpleTextCodec);
assumeWorkingMMapOnWindows();
for (int iter = 0; iter < 2; iter++) {
// relies on windows semantics
Path path = createTempDir();
FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
Path indexPath = new FilterPath(path, fs);
// NOTE: on Unix, we cannot use MMapDir, because WindowsFS doesn't see/think it keeps file handles open. Yet, on Windows, we MUST use
// MMapDir because the windows OS will in fact prevent file deletion for us, and fails otherwise:
FSDirectory dir;
if (Constants.WINDOWS) {
dir = new MMapDirectory(indexPath);
} else {
dir = new NIOFSDirectory(indexPath);
}
MergePolicy mergePolicy = newLogMergePolicy(true);
// This test expects all of its segments to be in CFS
mergePolicy.setNoCFSRatio(1.0);
mergePolicy.setMaxCFSSegmentSizeMB(Double.POSITIVE_INFINITY);
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(mergePolicy).setUseCompoundFile(true));
Document doc = new Document();
doc.add(newTextField("field", "go", Field.Store.NO));
w.addDocument(doc);
DirectoryReader r;
if (iter == 0) {
// use NRT
r = w.getReader();
} else {
// don't use NRT
w.commit();
r = DirectoryReader.open(dir);
}
assertTrue(Files.exists(indexPath.resolve("_0.cfs")));
assertTrue(Files.exists(indexPath.resolve("_0.cfe")));
assertTrue(Files.exists(indexPath.resolve("_0.si")));
if (iter == 1) {
// we run a full commit so there should be a segments file etc.
assertTrue(Files.exists(indexPath.resolve("segments_1")));
} else {
// this is an NRT reopen - no segments files yet
assertFalse(Files.exists(indexPath.resolve("segments_1")));
}
w.addDocument(doc);
w.forceMerge(1);
if (iter == 1) {
w.commit();
}
IndexReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
assertTrue(r != r2);
// NOTE: here we rely on "Windows" behavior, ie, even
// though IW wanted to delete _0.cfs since it was
// merged away, because we have a reader open
// against this file, it should still be here:
assertTrue(Files.exists(indexPath.resolve("_0.cfs")));
// forceMerge created this
//assertTrue(files.contains("_2.cfs"));
w.deleteUnusedFiles();
// r still holds this file open
assertTrue(Files.exists(indexPath.resolve("_0.cfs")));
//assertTrue(files.contains("_2.cfs"));
r.close();
if (iter == 0) {
// on closing NRT reader, it calls writer.deleteUnusedFiles
assertFalse(Files.exists(indexPath.resolve("_0.cfs")));
} else {
// now FSDir can remove it
dir.deletePendingFiles();
assertFalse(Files.exists(indexPath.resolve("_0.cfs")));
}
w.close();
r2.close();
dir.close();
}
}
use of org.apache.lucene.mockfile.WindowsFS in project lucene-solr by apache.
the class TestIndexWriter method testWithPendingDeletions.
public void testWithPendingDeletions() throws Exception {
// irony: currently we don't emulate windows well enough to work on windows!
assumeFalse("windows is not supported", Constants.WINDOWS);
Path path = createTempDir();
// Use WindowsFS to prevent open files from being deleted:
FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
Path root = new FilterPath(path, fs);
// MMapDirectory doesn't work because it closes its file handles after mapping!
try (FSDirectory dir = new SimpleFSDirectory(root)) {
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w = new IndexWriter(dir, iwc);
w.commit();
IndexInput in = dir.openInput("segments_1", IOContext.DEFAULT);
w.addDocument(new Document());
w.close();
assertTrue(dir.checkPendingDeletions());
// make sure we get NFSF if we try to delete and already-pending-delete file:
expectThrows(NoSuchFileException.class, () -> {
dir.deleteFile("segments_1");
});
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
});
assertTrue(expected.getMessage().contains("still has pending deleted files; cannot initialize IndexWriter"));
in.close();
}
}
Aggregations