Search in sources :

Example 1 with WindowsFS

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();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) RuntimeException(java.lang.RuntimeException) FileNotFoundException(java.io.FileNotFoundException) Exception(java.lang.Exception) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RuntimeException(java.lang.RuntimeException) WindowsFS(org.apache.lucene.mockfile.WindowsFS)

Example 2 with WindowsFS

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;
}
Also used : Path(java.nio.file.Path) WindowsFS(org.apache.lucene.mockfile.WindowsFS) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 3 with WindowsFS

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;
}
Also used : WindowsFS(org.apache.lucene.mockfile.WindowsFS) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem)

Example 4 with WindowsFS

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();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) FilterPath(org.apache.lucene.mockfile.FilterPath) SimpleTextCodec(org.apache.lucene.codecs.simpletext.SimpleTextCodec) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Document(org.apache.lucene.document.Document) MMapDirectory(org.apache.lucene.store.MMapDirectory) WindowsFS(org.apache.lucene.mockfile.WindowsFS) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FileSystem(java.nio.file.FileSystem)

Example 5 with WindowsFS

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();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) WindowsFS(org.apache.lucene.mockfile.WindowsFS) FilterPath(org.apache.lucene.mockfile.FilterPath) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FileSystem(java.nio.file.FileSystem) IndexInput(org.apache.lucene.store.IndexInput) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Document(org.apache.lucene.document.Document) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory)

Aggregations

WindowsFS (org.apache.lucene.mockfile.WindowsFS)6 FileSystem (java.nio.file.FileSystem)5 Path (java.nio.file.Path)4 FilterPath (org.apache.lucene.mockfile.FilterPath)3 FSDirectory (org.apache.lucene.store.FSDirectory)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 Document (org.apache.lucene.document.Document)2 FilterFileSystem (org.apache.lucene.mockfile.FilterFileSystem)2 NIOFSDirectory (org.apache.lucene.store.NIOFSDirectory)2 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Exception (java.lang.Exception)1 RuntimeException (java.lang.RuntimeException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1