Apache Solr open-source search software
LUCENE-5543 remove/deprecate Directory.fileExists
Michael McCandlessef3973c58900
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 7fa9d952813..da6d0a2068c 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -172,6 +172,8 @@ API Changesat an arbitrary position in the analysis chain. Use ThaiTokenizer and HMMChineseTokenizerinstead. (Robert Muir)+* LUCENE-5543: Remove/deprecate Directory.fileExists (Mike McCandless)+Optimizations* LUCENE-5468: HunspellStemFilter uses 10 to 100x less RAM. It also loadsdiff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java b/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.javaindex db7d11f2469..a590a515657 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java@@ -584,23 +584,20 @@ final class IndexFileDeleter implements Closeable {}directory.deleteFile(fileName);} catch (IOException e) { // if delete fails- if (directory.fileExists(fileName)) {+ // Some operating systems (e.g. Windows) don't+ // permit a file to be deleted while it is opened+ // for read (e.g. by another process or thread). So+ // we assume that when a delete fails it is because+ // the file is open in another process, and queue+ // the file for subsequent deletion.- // Some operating systems (e.g. Windows) don't- // permit a file to be deleted while it is opened- // for read (e.g. by another process or thread). So- // we assume that when a delete fails it is because- // the file is open in another process, and queue- // the file for subsequent deletion.-- if (infoStream.isEnabled("IFD")) {- infoStream.message("IFD", "unable to remove file \"" + fileName + "\": " + e.toString() + "; Will re-try later.");- }- if (deletable == null) {- deletable = new ArrayList<>();- }- deletable.add(fileName); // add to deletable+ if (infoStream.isEnabled("IFD")) {+ infoStream.message("IFD", "unable to remove file \"" + fileName + "\": " + e.toString() + "; Will re-try later.");+ }+ if (deletable == null) {+ deletable = new ArrayList<>();}+ deletable.add(fileName); // add to deletable}}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex c1eae52e3a1..53a55ef1d4b 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -18,8 +18,11 @@ package org.apache.lucene.index;*/import java.io.Closeable;+import java.io.FileNotFoundException;import java.io.IOException;+import java.nio.file.NoSuchFileException;import java.util.ArrayList;+import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Date;@@ -29,8 +32,8 @@ import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Locale;-import java.util.Map;import java.util.Map.Entry;+import java.util.Map;import java.util.Queue;import java.util.Set;import java.util.concurrent.atomic.AtomicInteger;@@ -2785,7 +2788,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{continue;}- assert !directory.fileExists(newFileName): "file \"" + newFileName + "\" already exists; siFiles=" + siFiles;+ assert !slowFileExists(directory, newFileName): "file \"" + newFileName + "\" already exists; siFiles=" + siFiles;info.info.dir.copy(directory, file, newFileName, context);}@@ -2924,17 +2927,18 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{if (anySegmentsFlushed) {maybeMerge(MergeTrigger.FULL_FLUSH, UNBOUNDED_MAX_MERGE_SEGMENTS);}+ startCommit(toCommit);success = true;} finally {if (!success) {synchronized (this) {- deleter.decRef(filesToCommit);- filesToCommit = null;+ if (filesToCommit != null) {+ deleter.decRef(filesToCommit);+ filesToCommit = null;+ }}}}-- startCommit(toCommit);}}@@ -4309,7 +4313,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{Collection<String> files = toSync.files(directory, false);for(final String fileName: files) {- assert directory.fileExists(fileName): "file " + fileName + " does not exist";+ assert slowFileExists(directory, fileName): "file " + fileName + " does not exist; files=" + Arrays.toString(directory.listAll());// If this trips it means we are missing a call to// .checkpoint somewhere, because by the time we// are called, deleter should know about every@@ -4710,4 +4714,16 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{void process(IndexWriter writer, boolean triggerMerge, boolean clearBuffers) throws IOException;}+ /** Used only by asserts: returns true if the file exists+ * (can be opened), false if it cannot be opened, and+ * (unlike Java's File.exists) throws IOException if+ * there's some unexpected error. */+ private static boolean slowFileExists(Directory dir, String fileName) throws IOException {+ try {+ dir.openInput(fileName, IOContext.DEFAULT).close();+ return true;+ } catch (NoSuchFileException | FileNotFoundException e) {+ return false;+ }+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java b/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.javaindex ed56fcef209..6bc497bb224 100644--- a/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java@@ -206,8 +206,10 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {if (nextWriteGen > 0) {String lastSaveFile = SNAPSHOTS_PREFIX + (nextWriteGen-1);- if (dir.fileExists(lastSaveFile)) {+ try {dir.deleteFile(lastSaveFile);+ } catch (IOException ioe) {+ // OK: likely it didn't exist}}diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.javaindex c9931378c37..65709ed4267 100644--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java@@ -752,8 +752,14 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo"",gen-1);- final boolean prevExists;- prevExists = directory.fileExists(prevSegmentFileName);+ boolean prevExists;++ try {+ directory.openInput(prevSegmentFileName, IOContext.DEFAULT).close();+ prevExists = true;+ } catch (IOException ioe) {+ prevExists = false;+ }if (prevExists) {if (infoStream != null) {diff --git a/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java b/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.javaindex 0b1a56263ab..1532779b6c0 100644--- a/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java@@ -207,16 +207,6 @@ public final class CompoundFileDirectory extends BaseDirectory {return res;}- /** Returns true iff a file with the given name exists. */- @Override- public boolean fileExists(String name) {- ensureOpen();- if (this.writer != null) {- return writer.fileExists(name);- }- return entries.containsKey(IndexFileNames.stripSegmentName(name));- }-/** Not implemented* @throws UnsupportedOperationException always: not supported by CFS */@Overridediff --git a/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java b/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.javaindex f6da132208d..aefe2a46d74 100644--- a/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java+++ b/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java@@ -225,9 +225,6 @@ final class CompoundFileWriter implements Closeable{out = new DirectCFSIndexOutput(getOutput(), entry, false);} else {entry.dir = this.directory;- if (directory.fileExists(name)) {- throw new IllegalArgumentException("File " + name + " already exists");- }out = new DirectCFSIndexOutput(directory.createOutput(name, context), entry,true);}diff --git a/lucene/core/src/java/org/apache/lucene/store/Directory.java b/lucene/core/src/java/org/apache/lucene/store/Directory.javaindex 9bf531a9749..9af6ff04cbc 100644--- a/lucene/core/src/java/org/apache/lucene/store/Directory.java+++ b/lucene/core/src/java/org/apache/lucene/store/Directory.java@@ -53,10 +53,6 @@ public abstract class Directory implements Closeable {*/public abstract String[] listAll() throws IOException;- /** Returns true iff a file with the given name exists. */- public abstract boolean fileExists(String name)- throws IOException;-/** Removes an existing file in the directory. */public abstract void deleteFile(String name)throws IOException;diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.javaindex f03a90d55dd..262330decf6 100644--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java@@ -234,14 +234,6 @@ public abstract class FSDirectory extends BaseDirectory {return listAll(directory);}- /** Returns true iff a file with the given name exists. */- @Override- public boolean fileExists(String name) {- ensureOpen();- File file = new File(directory, name);- return file.exists();- }-/** Returns the length in bytes of a file in the directory. */@Overridepublic long fileLength(String name) throws IOException {diff --git a/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.javaindex bb7a257c5ad..2a7bab6b075 100644--- a/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java@@ -132,11 +132,6 @@ public class FileSwitchDirectory extends BaseDirectory {}}- @Override- public boolean fileExists(String name) throws IOException {- return getDirectory(name).fileExists(name);- }-@Overridepublic void deleteFile(String name) throws IOException {getDirectory(name).deleteFile(name);diff --git a/lucene/core/src/java/org/apache/lucene/store/FilterDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FilterDirectory.javaindex 30de69a6c3e..f3bb5a06158 100644--- a/lucene/core/src/java/org/apache/lucene/store/FilterDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/FilterDirectory.java@@ -48,11 +48,6 @@ public class FilterDirectory extends Directory {return in.listAll();}- @Override- public boolean fileExists(String name) throws IOException {- return in.fileExists(name);- }-@Overridepublic void deleteFile(String name) throws IOException {in.deleteFile(name);diff --git a/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.javaindex 3823d33265c..b36e496ba81 100644--- a/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java@@ -151,18 +151,12 @@ public class NRTCachingDirectory extends Directory {return cache.sizeInBytes();}- @Override- public synchronized boolean fileExists(String name) throws IOException {- return cache.fileExists(name) || delegate.fileExists(name);- }-@Overridepublic synchronized void deleteFile(String name) throws IOException {if (VERBOSE) {System.out.println("nrtdir.deleteFile name=" + name);}- if (cache.fileExists(name)) {- assert !delegate.fileExists(name): "name=" + name;+ if (cache.fileNameExists(name)) {cache.deleteFile(name);} else {delegate.deleteFile(name);@@ -171,7 +165,7 @@ public class NRTCachingDirectory extends Directory {@Overridepublic synchronized long fileLength(String name) throws IOException {- if (cache.fileExists(name)) {+ if (cache.fileNameExists(name)) {return cache.fileLength(name);} else {return delegate.fileLength(name);@@ -223,7 +217,7 @@ public class NRTCachingDirectory extends Directory {if (VERBOSE) {System.out.println("nrtdir.openInput name=" + name);}- if (cache.fileExists(name)) {+ if (cache.fileNameExists(name)) {if (VERBOSE) {System.out.println(" from cache");}@@ -239,7 +233,7 @@ public class NRTCachingDirectory extends Directory {if (VERBOSE) {System.out.println("nrtdir.openInput name=" + name);}- if (cache.fileExists(name)) {+ if (cache.fileNameExists(name)) {if (VERBOSE) {System.out.println(" from cache");}@@ -289,13 +283,10 @@ public class NRTCachingDirectory extends Directory {if (VERBOSE) {System.out.println("nrtdir.unCache name=" + fileName);}- if (!cache.fileExists(fileName)) {+ if (!cache.fileNameExists(fileName)) {// Another thread beat us...return;}- if (delegate.fileExists(fileName)) {- throw new IOException("cannot uncache file=\"" + fileName + "\": it was separately also created in the delegate directory");- }final IOContext context = IOContext.DEFAULT;final IndexOutput out = delegate.createOutput(fileName, context);IndexInput in = null;@@ -309,7 +300,7 @@ public class NRTCachingDirectory extends Directory {// Lock order: uncacheLock -> thissynchronized(this) {// Must sync here because other sync methods have- // if (cache.fileExists(name)) { ... } else { ... }:+ // if (cache.fileNameExists(name)) { ... } else { ... }:cache.deleteFile(fileName);}}diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.javaindex cac6224a432..5d602eb47de 100644--- a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java@@ -118,9 +118,7 @@ public class RAMDirectory extends BaseDirectory {return names.toArray(new String[names.size()]);}- /** Returns true iff the named file exists in this directory. */- @Override- public final boolean fileExists(String name) {+ public final boolean fileNameExists(String name) {ensureOpen();return fileMap.containsKey(name);}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java b/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.javaindex 6e8e65a236d..489d8927e0e 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java@@ -682,12 +682,6 @@ public class TestCompoundFile extends LuceneTestCasecreateSequenceFile(newDir, "d1", (byte) 0, 15);IndexOutput out = csw.createOutput("d.xyz", newIOContext(random()));out.writeInt(0);- try {- newDir.copy(csw, "d1", "d1", newIOContext(random()));- fail("file does already exist");- } catch (IllegalArgumentException e) {- //- }out.close();assertEquals(1, csw.listAll().length);assertEquals("d.xyz", csw.listAll()[0]);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCrashCausesCorruptIndex.java b/lucene/core/src/test/org/apache/lucene/index/TestCrashCausesCorruptIndex.javaindex 7ca4ef6cf2f..5cd1f7a4781 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestCrashCausesCorruptIndex.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestCrashCausesCorruptIndex.java@@ -33,7 +33,6 @@ import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;-import org.apache.lucene.util.TestUtil;public class TestCrashCausesCorruptIndex extends LuceneTestCase {@@ -83,7 +82,7 @@ public class TestCrashCausesCorruptIndex extends LuceneTestCase {}// writes segments_3indexWriter.close();- assertFalse(realDirectory.fileExists("segments_2"));+ assertFalse(slowFileExists(realDirectory, "segments_2"));crashAfterCreateOutput.close();}@@ -103,7 +102,7 @@ public class TestCrashCausesCorruptIndex extends LuceneTestCase {// however, to test the fix, the following lines should pass as well.indexWriter.addDocument(getDocument());indexWriter.close();- assertFalse(realDirectory.fileExists("segments_2"));+ assertFalse(slowFileExists(realDirectory, "segments_2"));realDirectory.close();}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.javaindex a3fa208af15..3c188bc11c7 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java@@ -121,10 +121,6 @@ public class TestFieldsReader extends LuceneTestCase {return fsDir.listAll();}@Override- public boolean fileExists(String name) throws IOException {- return fsDir.fileExists(name);- }- @Overridepublic void deleteFile(String name) throws IOException {fsDir.deleteFile(name);}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.javaindex 81c3461c380..e03d59af3b9 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.java@@ -116,8 +116,8 @@ public class TestIndexFileDeleter extends LuceneTestCase {// Create a bogus cfs file shadowing a non-cfs segment:// TODO: assert is bogus (relies upon codec-specific filenames)- assertTrue(dir.fileExists("_3.fdt") || dir.fileExists("_3.fld"));- assertTrue(!dir.fileExists("_3.cfs"));+ assertTrue(slowFileExists(dir, "_3.fdt") || slowFileExists(dir, "_3.fld"));+ assertTrue(!slowFileExists(dir, "_3.cfs"));copyFile(dir, "_1.cfs", "_3.cfs");String[] filesPre = dir.listAll();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.javaindex da4a0c7ccb7..b866b51d626 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -1133,6 +1133,9 @@ public class TestIndexWriter extends LuceneTestCase {}if (!failed) {+ if (VERBOSE) {+ System.out.println("TEST: now rollback");+ }// clear interrupt state:Thread.interrupted();if (w != null) {@@ -1161,7 +1164,12 @@ public class TestIndexWriter extends LuceneTestCase {}}try {- IOUtils.close(dir, adder);+ IOUtils.close(dir);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ try {+ IOUtils.close(adder);} catch (IOException e) {throw new RuntimeException(e);}@@ -1931,7 +1939,7 @@ public class TestIndexWriter extends LuceneTestCase {new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random()))).close();- assertTrue(dir.fileExists("myrandomfile"));+ assertTrue(slowFileExists(dir, "myrandomfile"));} finally {dir.close();}@@ -2009,14 +2017,14 @@ public class TestIndexWriter extends LuceneTestCase {new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random()))).close();- assertTrue(dir.fileExists("_a.frq"));+ assertTrue(slowFileExists(dir, "_a.frq"));IndexWriter iw = new IndexWriter(dir,newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));iw.addDocument(new Document());iw.close();- assertFalse(dir.fileExists("_a.frq"));+ assertFalse(slowFileExists(dir, "_a.frq"));} finally {dir.close();}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.javaindex 445c4af4431..dba31fc283d 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterDelete.java@@ -45,7 +45,6 @@ import org.apache.lucene.store.MockDirectoryWrapper;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;-import org.apache.lucene.util.TestUtil;public class TestIndexWriterDelete extends LuceneTestCase {@@ -727,7 +726,8 @@ public class TestIndexWriterDelete extends LuceneTestCase {boolean seen = false;StackTraceElement[] trace = new Exception().getStackTrace();for (int i = 0; i < trace.length; i++) {- if ("applyDeletesAndUpdates".equals(trace[i].getMethodName())) {+ if ("applyDeletesAndUpdates".equals(trace[i].getMethodName()) ||+ "slowFileExists".equals(trace[i].getMethodName())) {seen = true;break;}@@ -1042,7 +1042,7 @@ public class TestIndexWriterDelete extends LuceneTestCase {w.updateDocument(delTerm, doc);// Eventually segment 0 should get a del docs:// TODO: fix this test- if (dir.fileExists("_0_1.del") || dir.fileExists("_0_1.liv") ) {+ if (slowFileExists(dir, "_0_1.del") || slowFileExists(dir, "_0_1.liv") ) {if (VERBOSE) {System.out.println("TEST: deletes created @ count=" + count);}@@ -1088,7 +1088,7 @@ public class TestIndexWriterDelete extends LuceneTestCase {w.updateDocument(delTerm, doc);// Eventually segment 0 should get a del docs:// TODO: fix this test- if (dir.fileExists("_0_1.del") || dir.fileExists("_0_1.liv")) {+ if (slowFileExists(dir, "_0_1.del") || slowFileExists(dir, "_0_1.liv")) {break;}count++;@@ -1135,7 +1135,7 @@ public class TestIndexWriterDelete extends LuceneTestCase {w.updateDocument(new Term("id", ""+id), doc);docsInSegment.incrementAndGet();// TODO: fix this test- if (dir.fileExists("_0_1.del") || dir.fileExists("_0_1.liv")) {+ if (slowFileExists(dir, "_0_1.del") || slowFileExists(dir, "_0_1.liv")) {if (VERBOSE) {System.out.println("TEST: deletes created @ id=" + id);}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.javaindex d5cd7941e8a..ac3ff6eb45a 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.java@@ -83,6 +83,7 @@ public class TestIndexWriterOutOfFileDescriptors extends LuceneTestCase {}w.addDocument(docs.nextDoc());}+ dir.setRandomIOExceptionRateOnOpen(0.0);w.close();w = null;diff --git a/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java b/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.javaindex fc0df11d6df..21facbbe7c1 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java@@ -91,7 +91,7 @@ public class TestNeverDelete extends LuceneTestCase {allFiles.addAll(ic.getFileNames());// Make sure no old files were removedfor(String fileName : allFiles) {- assertTrue("file " + fileName + " does not exist", d.fileExists(fileName));+ assertTrue("file " + fileName + " does not exist", slowFileExists(d, fileName));}DirectoryReader r2 = DirectoryReader.openIfChanged(r);if (r2 != null) {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.javaindex 599a64de83f..b74cf91a608 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java@@ -51,7 +51,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {protected void checkSnapshotExists(Directory dir, IndexCommit c) throws Exception {String segFileName = c.getSegmentsFileName();- assertTrue("segments file not found in directory: " + segFileName, dir.fileExists(segFileName));+ assertTrue("segments file not found in directory: " + segFileName, slowFileExists(dir, segFileName));}protected void checkMaxDoc(IndexCommit commit, int expectedMaxDoc) throws Exception {@@ -338,7 +338,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {// but 'snapshot1' files will still exist (need to release snapshot before they can be deleted).String segFileName = snapshots.get(1).getSegmentsFileName();- assertTrue("snapshot files should exist in the directory: " + segFileName, dir.fileExists(segFileName));+ assertTrue("snapshot files should exist in the directory: " + segFileName, slowFileExists(dir, segFileName));dir.close();}@@ -360,7 +360,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {sdp.release(snapshots.get(0));writer.deleteUnusedFiles();writer.close();- assertFalse("segments file should not be found in dirctory: " + segFileName, dir.fileExists(segFileName));+ assertFalse("segments file should not be found in dirctory: " + segFileName, slowFileExists(dir, segFileName));dir.close();}@@ -409,7 +409,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {// commit.new IndexWriter(dir, getConfig(random(), null)).close();- assertFalse("snapshotted commit should not exist", dir.fileExists(s1.getSegmentsFileName()));+ assertFalse("snapshotted commit should not exist", slowFileExists(dir, s1.getSegmentsFileName()));dir.close();}}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java b/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.javaindex 28877c681ec..16763ffa445 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java@@ -500,7 +500,7 @@ public class TestControlledRealTimeReopenThread extends ThreadedIndexingAndSearcfor (String name : ic.getFileNames()) {//distribute, and backup//System.out.println(names);- assertTrue(dir.fileExists(name));+ assertTrue(slowFileExists(dir, name));}} catch (Exception e) {throw new RuntimeException(e);diff --git a/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java b/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.javaindex 5c99fa2b56a..4b90c7831b2 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java@@ -40,7 +40,6 @@ import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;-import org.apache.lucene.util.TestUtil;import org.apache.lucene.util.ArrayUtil;public class TestBufferedIndexInput extends LuceneTestCase {@@ -330,12 +329,6 @@ public class TestBufferedIndexInput extends LuceneTestCase {dir.deleteFile(name);}@Override- public boolean fileExists(String name)- throws IOException- {- return dir.fileExists(name);- }- @Overridepublic String[] listAll()throws IOException{diff --git a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.javaindex c5b13c88121..6f107b493b3 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java@@ -74,7 +74,7 @@ public class TestDirectory extends LuceneTestCase {//System.out.println("create:" + fileName);IndexOutput output = dir.createOutput(fileName, newIOContext(random()));output.close();- assertTrue(dir.fileExists(fileName));+ assertTrue(slowFileExists(dir, fileName));} catch (IOException e) {throw new RuntimeException(e);}@@ -160,7 +160,7 @@ public class TestDirectory extends LuceneTestCase {for (int j=0; j<dirs.length; j++) {FSDirectory d2 = dirs[j];d2.ensureOpen();- assertTrue(d2.fileExists(fname));+ assertTrue(slowFileExists(d2, fname));assertEquals(1 + largeBuffer.length, d2.fileLength(fname));// don't do read tests if unmapping is not supported!@@ -186,7 +186,7 @@ public class TestDirectory extends LuceneTestCase {for (int j=0; j<dirs.length; j++) {FSDirectory d2 = dirs[j];- assertFalse(d2.fileExists(fname));+ assertFalse(slowFileExists(d2, fname));}Lock lock = dir.makeLock(lockname);@@ -248,7 +248,7 @@ public class TestDirectory extends LuceneTestCase {String name = "file";try {dir.createOutput(name, newIOContext(random())).close();- assertTrue(dir.fileExists(name));+ assertTrue(slowFileExists(dir, name));assertTrue(Arrays.asList(dir.listAll()).contains(name));} finally {dir.close();@@ -275,7 +275,7 @@ public class TestDirectory extends LuceneTestCase {try {IndexOutput out = fsDir.createOutput("afile", newIOContext(random()));out.close();- assertTrue(fsDir.fileExists("afile"));+ assertTrue(slowFileExists(fsDir, "afile"));try {new SimpleFSDirectory(new File(path, "afile"), null);fail("did not hit expected exception");diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.javaindex 0d91b58d1e6..f7e6ecb8419 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java@@ -122,7 +122,7 @@ public class TestFileSwitchDirectory extends LuceneTestCase {String name = "file";try {dir.createOutput(name, newIOContext(random())).close();- assertTrue(dir.fileExists(name));+ assertTrue(slowFileExists(dir, name));assertTrue(Arrays.asList(dir.listAll()).contains(name));} finally {dir.close();@@ -136,12 +136,6 @@ public class TestFileSwitchDirectory extends LuceneTestCase {createSequenceFile(newDir, "d1", (byte) 0, 15);IndexOutput out = csw.createOutput("d.xyz", newIOContext(random()));out.writeInt(0);- try {- newDir.copy(csw, "d1", "d1", newIOContext(random()));- fail("file does already exist");- } catch (IllegalArgumentException e) {- //- }out.close();assertEquals(1, csw.listAll().length);assertEquals("d.xyz", csw.listAll()[0]);diff --git a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.javaindex 5102c7686bf..df4b6a47ab0 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java@@ -141,7 +141,7 @@ public class TestNRTCachingDirectory extends LuceneTestCase {String name = "file";try {dir.createOutput(name, newIOContext(random())).close();- assertTrue(dir.fileExists(name));+ assertTrue(slowFileExists(dir, name));assertTrue(Arrays.asList(dir.listAll()).contains(name));} finally {dir.close();@@ -155,12 +155,6 @@ public class TestNRTCachingDirectory extends LuceneTestCase {createSequenceFile(newDir, "d1", (byte) 0, 15);IndexOutput out = csw.createOutput("d.xyz", newIOContext(random()));out.writeInt(0);- try {- newDir.copy(csw, "d1", "d1", newIOContext(random()));- fail("file does already exist");- } catch (IllegalArgumentException e) {- //- }out.close();assertEquals(1, csw.listAll().length);assertEquals("d.xyz", csw.listAll()[0]);diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.java b/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.javaindex 299a6661488..86c4914e20f 100644--- a/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.java+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.java@@ -123,9 +123,7 @@ public class IndexReplicationHandler implements ReplicationHandler {public static void cleanupFilesOnFailure(Directory dir, List<String> files) {for (String file : files) {try {- if (dir.fileExists(file)) {- dir.deleteFile(file);- }+ dir.deleteFile(file);} catch (Throwable t) {// suppress any exception because if we're here, it means copy// failed, and we must cleanup after ourselves.@@ -194,9 +192,7 @@ public class IndexReplicationHandler implements ReplicationHandler {SegmentInfos.writeSegmentsGen(dir, SegmentInfos.generationFromSegmentsFileName(segmentsFile));} else {try {- if (dir.fileExists(IndexFileNames.SEGMENTS_GEN)) {- dir.deleteFile(IndexFileNames.SEGMENTS_GEN);- }+ dir.deleteFile(IndexFileNames.SEGMENTS_GEN);} catch (Throwable t) {// suppress any errors while deleting this file.}diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.javaindex cbb4d2c731b..bf69127f9aa 100644--- a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java@@ -82,8 +82,8 @@ public class IndexAndTaxonomyRevisionTest extends ReplicatorTestCase {Revision rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);// releasing that revision should not delete the filesrev1.release();- assertTrue(indexDir.fileExists(IndexFileNames.SEGMENTS + "_1"));- assertTrue(taxoDir.fileExists(IndexFileNames.SEGMENTS + "_1"));+ assertTrue(slowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1"));+ assertTrue(slowFileExists(taxoDir, IndexFileNames.SEGMENTS + "_1"));rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter); // create revision again, so the files are snapshottedindexWriter.addDocument(newDocument(taxoWriter));@@ -91,7 +91,7 @@ public class IndexAndTaxonomyRevisionTest extends ReplicatorTestCase {taxoWriter.commit();assertNotNull(new IndexAndTaxonomyRevision(indexWriter, taxoWriter));rev1.release(); // this release should trigger the delete of segments_1- assertFalse(indexDir.fileExists(IndexFileNames.SEGMENTS + "_1"));+ assertFalse(slowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1"));} finally {IOUtils.close(indexWriter, taxoWriter, taxoDir, indexDir);}diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.javaindex b6e66b828a2..62e4b79186f 100644--- a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java@@ -79,14 +79,14 @@ public class IndexRevisionTest extends ReplicatorTestCase {Revision rev1 = new IndexRevision(writer);// releasing that revision should not delete the filesrev1.release();- assertTrue(dir.fileExists(IndexFileNames.SEGMENTS + "_1"));+ assertTrue(slowFileExists(dir, IndexFileNames.SEGMENTS + "_1"));rev1 = new IndexRevision(writer); // create revision again, so the files are snapshottedwriter.addDocument(new Document());writer.commit();assertNotNull(new IndexRevision(writer));rev1.release(); // this release should trigger the delete of segments_1- assertFalse(dir.fileExists(IndexFileNames.SEGMENTS + "_1"));+ assertFalse(slowFileExists(dir, IndexFileNames.SEGMENTS + "_1"));} finally {IOUtils.close(writer, dir);}diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.javaindex ef20967c6a2..5b12374a5d5 100644--- a/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java@@ -187,11 +187,11 @@ public class LocalReplicatorTest extends ReplicatorTestCase {@Testpublic void testRevisionRelease() throws Exception {replicator.publish(createRevision(1));- assertTrue(sourceDir.fileExists(IndexFileNames.SEGMENTS + "_1"));+ assertTrue(slowFileExists(sourceDir, IndexFileNames.SEGMENTS + "_1"));replicator.publish(createRevision(2));// now the files of revision 1 can be deleted- assertTrue(sourceDir.fileExists(IndexFileNames.SEGMENTS + "_2"));- assertFalse("segments_1 should not be found in index directory after revision is released", sourceDir.fileExists(IndexFileNames.SEGMENTS + "_1"));+ assertTrue(slowFileExists(sourceDir, IndexFileNames.SEGMENTS + "_2"));+ assertFalse("segments_1 should not be found in index directory after revision is released", slowFileExists(sourceDir, IndexFileNames.SEGMENTS + "_1"));}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.javaindex 2f2acacb13e..f369e71dbd2 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java@@ -251,7 +251,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {String tempFileName;while (true) {tempFileName = ""+randomState.nextInt();- if (!in.fileExists(tempFileName)) {+ if (!LuceneTestCase.slowFileExists(in, tempFileName)) {break;}}@@ -549,7 +549,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {if (failOnOpenInput) {maybeThrowDeterministicException();}- if (!in.fileExists(name)) {+ if (!LuceneTestCase.slowFileExists(in, name)) {throw randomState.nextBoolean() ? new FileNotFoundException(name + " in dir=" + in) : new NoSuchFileException(name + " in dir=" + in);}@@ -864,12 +864,6 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {return in.listAll();}- @Override- public synchronized boolean fileExists(String name) throws IOException {- maybeYield();- return in.fileExists(name);- }-@Overridepublic synchronized long fileLength(String name) throws IOException {maybeYield();@@ -925,7 +919,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {public IndexInputSlicer createSlicer(final String name, IOContext context)throws IOException {maybeYield();- if (!in.fileExists(name)) {+ if (!LuceneTestCase.slowFileExists(in, name)) {throw randomState.nextBoolean() ? new FileNotFoundException(name) : new NoSuchFileException(name);}// cannot open a file for input if it's still open fordiff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex 153211a64c8..6891382ce93 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -18,6 +18,7 @@ package org.apache.lucene.util;*/import java.io.*;+import java.nio.file.NoSuchFileException;import java.lang.annotation.*;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;@@ -2063,4 +2064,17 @@ public abstract class LuceneTestCase extends Assert {assertEquals(info, left, right);}++ /** Returns true if the file exists (can be opened), false+ * if it cannot be opened, and (unlike Java's+ * File.exists) throws IOException if there's some+ * unexpected error. */+ public static boolean slowFileExists(Directory dir, String fileName) throws IOException {+ try {+ dir.openInput(fileName, IOContext.DEFAULT).close();+ return true;+ } catch (NoSuchFileException | FileNotFoundException e) {+ return false;+ }+ }}diff --git a/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/DirectoryFactory.javaindex 2c1aa578de3..d2229b5c51c 100644--- a/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java+++ b/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java@@ -19,7 +19,9 @@ package org.apache.solr.core;import java.io.Closeable;import java.io.File;+import java.io.FileNotFoundException;import java.io.IOException;+import java.nio.file.NoSuchFileException;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FlushInfo;@@ -202,11 +204,11 @@ public abstract class DirectoryFactory implements NamedListInitializedPlugin,}public static long sizeOf(Directory directory, String file) throws IOException {- if (!directory.fileExists(file)) {+ try {+ return directory.fileLength(file);+ } catch (FileNotFoundException | NoSuchFileException e) {return 0;}-- return directory.fileLength(file);}/**diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.javaindex b5e58b2cd35..5c0a8fee6f8 100644--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java@@ -18,14 +18,16 @@package org.apache.solr.core;import java.io.File;+import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Writer;import java.lang.reflect.Constructor;import java.net.URL;-import java.util.Arrays;+import java.nio.file.NoSuchFileException;import java.util.ArrayList;+import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Date;@@ -49,7 +51,6 @@ import java.util.concurrent.Future;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicLong;import java.util.concurrent.locks.ReentrantLock;-import javax.xml.parsers.ParserConfigurationException;import org.apache.commons.io.IOUtils;@@ -66,8 +67,8 @@ import org.apache.solr.cloud.CloudDescriptor;import org.apache.solr.common.SolrException;import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.Slice;-import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.CommonParams.EchoParamStyle;+import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.ExecutorUtil;import org.apache.solr.common.util.NamedList;@@ -77,6 +78,7 @@ import org.apache.solr.handler.SnapPuller;import org.apache.solr.handler.admin.ShowFileRequestHandler;import org.apache.solr.handler.component.AnalyticsComponent;import org.apache.solr.handler.component.DebugComponent;+import org.apache.solr.handler.component.ExpandComponent;import org.apache.solr.handler.component.FacetComponent;import org.apache.solr.handler.component.HighlightComponent;import org.apache.solr.handler.component.MoreLikeThisComponent;@@ -84,7 +86,6 @@ import org.apache.solr.handler.component.QueryComponent;import org.apache.solr.handler.component.RealTimeGetComponent;import org.apache.solr.handler.component.SearchComponent;import org.apache.solr.handler.component.StatsComponent;-import org.apache.solr.handler.component.ExpandComponent;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.BinaryResponseWriter;@@ -100,9 +101,9 @@ import org.apache.solr.response.SchemaXmlResponseWriter;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.response.XMLResponseWriter;import org.apache.solr.response.transform.TransformerFactory;+import org.apache.solr.rest.ManagedResourceStorage.StorageIO;import org.apache.solr.rest.ManagedResourceStorage;import org.apache.solr.rest.RestManager;-import org.apache.solr.rest.ManagedResourceStorage.StorageIO;import org.apache.solr.schema.FieldType;import org.apache.solr.schema.IndexSchema;import org.apache.solr.schema.IndexSchemaFactory;@@ -113,8 +114,8 @@ import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.search.ValueSourceParser;import org.apache.solr.update.DefaultSolrCoreState;import org.apache.solr.update.DirectUpdateHandler2;-import org.apache.solr.update.SolrCoreState;import org.apache.solr.update.SolrCoreState.IndexWriterCloser;+import org.apache.solr.update.SolrCoreState;import org.apache.solr.update.SolrIndexWriter;import org.apache.solr.update.UpdateHandler;import org.apache.solr.update.VersionInfo;@@ -263,9 +264,14 @@ public final class SolrCore implements SolrInfoMBean {Directory dir = null;try {dir = getDirectoryFactory().get(getDataDir(), DirContext.META_DATA, getSolrConfig().indexConfig.lockType);- if (dir.fileExists(SnapPuller.INDEX_PROPERTIES)){- final IndexInput input = dir.openInput(SnapPuller.INDEX_PROPERTIES, IOContext.DEFAULT);-+ IndexInput input;+ try {+ input = dir.openInput(SnapPuller.INDEX_PROPERTIES, IOContext.DEFAULT);+ } catch (FileNotFoundException | NoSuchFileException e) {+ input = null;+ }++ if (input != null) {final InputStream is = new PropertiesInputStream(input);try {p.load(new InputStreamReader(is, "UTF-8"));diff --git a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.javaindex a1238c5deb2..5da16a4a8ce 100644--- a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java@@ -18,6 +18,7 @@ package org.apache.solr.handler;import java.io.File;import java.io.FileInputStream;+import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;@@ -25,6 +26,7 @@ import java.io.OutputStream;import java.io.Writer;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;+import java.nio.file.NoSuchFileException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;@@ -43,19 +45,16 @@ import java.util.zip.Checksum;import java.util.zip.DeflaterOutputStream;import org.apache.commons.io.IOUtils;+import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexCommit;import org.apache.lucene.index.IndexDeletionPolicy;-import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;--import static org.apache.lucene.util.IOUtils.CHARSET_UTF_8;-import org.apache.solr.client.solrj.impl.BinaryResponseParser;-import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;+import org.apache.solr.common.SolrException;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.SolrParams;@@ -64,12 +63,12 @@ import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.common.util.StrUtils;import org.apache.solr.core.CloseHook;+import org.apache.solr.core.DirectoryFactory.DirContext;import org.apache.solr.core.DirectoryFactory;import org.apache.solr.core.IndexDeletionPolicyWrapper;import org.apache.solr.core.SolrCore;import org.apache.solr.core.SolrDeletionPolicy;import org.apache.solr.core.SolrEventListener;-import org.apache.solr.core.DirectoryFactory.DirContext;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.BinaryQueryResponseWriter;import org.apache.solr.response.SolrQueryResponse;@@ -82,6 +81,8 @@ import org.apache.solr.util.plugin.SolrCoreAware;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import static org.apache.lucene.util.IOUtils.CHARSET_UTF_8;+/*** <p> A Handler which provides a REST API for replication and serves replication requests from Slaves. <p/> </p>* <p>When running on the master, it provides the following commands <ol> <li>Get the current replicable index version@@ -811,11 +812,14 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAwtry {dir = core.getDirectoryFactory().get(core.getDataDir(),DirContext.META_DATA, core.getSolrConfig().indexConfig.lockType);- if (!dir.fileExists(SnapPuller.REPLICATION_PROPERTIES)) {+ IndexInput input;+ try {+ input = dir.openInput(+ SnapPuller.REPLICATION_PROPERTIES, IOContext.DEFAULT);+ } catch (FileNotFoundException | NoSuchFileException e) {return new Properties();}- final IndexInput input = dir.openInput(- SnapPuller.REPLICATION_PROPERTIES, IOContext.DEFAULT);+try {final InputStream is = new PropertiesInputStream(input);Properties props = new Properties();diff --git a/solr/core/src/java/org/apache/solr/handler/SnapPuller.java b/solr/core/src/java/org/apache/solr/handler/SnapPuller.javaindex e6b00ed2348..3b2caf07771 100644--- a/solr/core/src/java/org/apache/solr/handler/SnapPuller.java+++ b/solr/core/src/java/org/apache/solr/handler/SnapPuller.java@@ -16,28 +16,8 @@*/package org.apache.solr.handler;-import static org.apache.lucene.util.IOUtils.CHARSET_UTF_8;-import static org.apache.solr.handler.ReplicationHandler.ALIAS;-import static org.apache.solr.handler.ReplicationHandler.CHECKSUM;-import static org.apache.solr.handler.ReplicationHandler.CMD_DETAILS;-import static org.apache.solr.handler.ReplicationHandler.CMD_GET_FILE;-import static org.apache.solr.handler.ReplicationHandler.CMD_GET_FILE_LIST;-import static org.apache.solr.handler.ReplicationHandler.CMD_INDEX_VERSION;-import static org.apache.solr.handler.ReplicationHandler.COMMAND;-import static org.apache.solr.handler.ReplicationHandler.COMPRESSION;-import static org.apache.solr.handler.ReplicationHandler.CONF_FILES;-import static org.apache.solr.handler.ReplicationHandler.CONF_FILE_SHORT;-import static org.apache.solr.handler.ReplicationHandler.EXTERNAL;-import static org.apache.solr.handler.ReplicationHandler.FILE;-import static org.apache.solr.handler.ReplicationHandler.FILE_STREAM;-import static org.apache.solr.handler.ReplicationHandler.GENERATION;-import static org.apache.solr.handler.ReplicationHandler.INTERNAL;-import static org.apache.solr.handler.ReplicationHandler.MASTER_URL;-import static org.apache.solr.handler.ReplicationHandler.NAME;-import static org.apache.solr.handler.ReplicationHandler.OFFSET;-import static org.apache.solr.handler.ReplicationHandler.SIZE;-import java.io.File;+import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;@@ -46,6 +26,7 @@ import java.io.OutputStreamWriter;import java.io.Writer;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;+import java.nio.file.NoSuchFileException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Arrays;@@ -76,22 +57,23 @@ import org.apache.http.client.HttpClient;import org.apache.lucene.index.IndexCommit;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpClientUtil;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.request.QueryRequest;-import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;+import org.apache.solr.common.SolrException;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.ExecutorUtil;import org.apache.solr.common.util.FastInputStream;import org.apache.solr.common.util.NamedList;-import org.apache.solr.core.DirectoryFactory;import org.apache.solr.core.DirectoryFactory.DirContext;+import org.apache.solr.core.DirectoryFactory;import org.apache.solr.core.IndexDeletionPolicyWrapper;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.ReplicationHandler.FileInfo;@@ -108,6 +90,27 @@ import org.eclipse.jetty.util.log.Log;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import static org.apache.lucene.util.IOUtils.CHARSET_UTF_8;+import static org.apache.solr.handler.ReplicationHandler.ALIAS;+import static org.apache.solr.handler.ReplicationHandler.CHECKSUM;+import static org.apache.solr.handler.ReplicationHandler.CMD_DETAILS;+import static org.apache.solr.handler.ReplicationHandler.CMD_GET_FILE;+import static org.apache.solr.handler.ReplicationHandler.CMD_GET_FILE_LIST;+import static org.apache.solr.handler.ReplicationHandler.CMD_INDEX_VERSION;+import static org.apache.solr.handler.ReplicationHandler.COMMAND;+import static org.apache.solr.handler.ReplicationHandler.COMPRESSION;+import static org.apache.solr.handler.ReplicationHandler.CONF_FILES;+import static org.apache.solr.handler.ReplicationHandler.CONF_FILE_SHORT;+import static org.apache.solr.handler.ReplicationHandler.EXTERNAL;+import static org.apache.solr.handler.ReplicationHandler.FILE;+import static org.apache.solr.handler.ReplicationHandler.FILE_STREAM;+import static org.apache.solr.handler.ReplicationHandler.GENERATION;+import static org.apache.solr.handler.ReplicationHandler.INTERNAL;+import static org.apache.solr.handler.ReplicationHandler.MASTER_URL;+import static org.apache.solr.handler.ReplicationHandler.NAME;+import static org.apache.solr.handler.ReplicationHandler.OFFSET;+import static org.apache.solr.handler.ReplicationHandler.SIZE;+/*** <p/> Provides functionality of downloading changed index files as well as config files and a timer for scheduling fetches from the* master. </p>@@ -763,7 +766,7 @@ public class SnapPuller {LOG.debug("Download files to dir: " + Arrays.asList(indexDir.listAll()));}for (Map<String,Object> file : filesToDownload) {- if (!indexDir.fileExists((String) file.get(NAME))+ if (!slowFileExists(indexDir, (String) file.get(NAME))|| downloadCompleteIndex) {dirFileFetcher = new DirectoryFileFetcher(tmpIndexDir, file,(String) file.get(NAME), false, latestGeneration);@@ -777,6 +780,19 @@ public class SnapPuller {}}+ /** Returns true if the file exists (can be opened), false+ * if it cannot be opened, and (unlike Java's+ * File.exists) throws IOException if there's some+ * unexpected error. */+ private static boolean slowFileExists(Directory dir, String fileName) throws IOException {+ try {+ dir.openInput(fileName, IOContext.DEFAULT).close();+ return true;+ } catch (NoSuchFileException | FileNotFoundException e) {+ return false;+ }+ }+/*** All the files which are common between master and slave must have same size else we assume they are* not compatible (stale).@@ -786,7 +802,7 @@ public class SnapPuller {*/private boolean isIndexStale(Directory dir) throws IOException {for (Map<String, Object> file : filesToDownload) {- if (dir.fileExists((String) file.get(NAME))+ if (slowFileExists(dir, (String) file.get(NAME))&& dir.fileLength((String) file.get(NAME)) != (Long) file.get(SIZE)) {LOG.warn("File " + file.get(NAME) + " expected to be " + file.get(SIZE)+ " while it is " + dir.fileLength((String) file.get(NAME)));@@ -806,7 +822,7 @@ public class SnapPuller {LOG.debug("Moving file: {}", fname);boolean success = false;try {- if (indexDir.fileExists(fname)) {+ if (slowFileExists(indexDir, fname)) {LOG.info("Skipping move file - it already exists:" + fname);return true;}@@ -924,7 +940,7 @@ public class SnapPuller {Directory dir = null;try {dir = solrCore.getDirectoryFactory().get(solrCore.getDataDir(), DirContext.META_DATA, solrCore.getSolrConfig().indexConfig.lockType);- if (dir.fileExists(SnapPuller.INDEX_PROPERTIES)){+ if (slowFileExists(dir, SnapPuller.INDEX_PROPERTIES)){final IndexInput input = dir.openInput(SnapPuller.INDEX_PROPERTIES, DirectoryFactory.IOCONTEXT_NO_CACHE);final InputStream is = new PropertiesInputStream(input);diff --git a/solr/core/src/java/org/apache/solr/store/blockcache/BlockDirectory.java b/solr/core/src/java/org/apache/solr/store/blockcache/BlockDirectory.javaindex 028fd55aecb..62065f7ed30 100644--- a/solr/core/src/java/org/apache/solr/store/blockcache/BlockDirectory.java+++ b/solr/core/src/java/org/apache/solr/store/blockcache/BlockDirectory.java@@ -369,10 +369,6 @@ public class BlockDirectory extends Directory {directory.deleteFile(name);}- public boolean fileExists(String name) throws IOException {- return directory.fileExists(name);- }-public long fileLength(String name) throws IOException {return directory.fileLength(name);}diff --git a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.javaindex 468f56ceb45..78237087104 100644--- a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java+++ b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java@@ -143,11 +143,6 @@ public class HdfsDirectory extends BaseDirectory {getFileSystem().delete(path, false);}- @Override- public boolean fileExists(String name) throws IOException {- return getFileSystem().exists(new Path(hdfsDirPath, name));- }-@Overridepublic long fileLength(String name) throws IOException {return HdfsFileReader.getLength(getFileSystem(),diff --git a/solr/core/src/test/org/apache/solr/store/hdfs/HdfsDirectoryTest.java b/solr/core/src/test/org/apache/solr/store/hdfs/HdfsDirectoryTest.javaindex 34b2dc06f36..ee571d9a3ba 100644--- a/solr/core/src/test/org/apache/solr/store/hdfs/HdfsDirectoryTest.java+++ b/solr/core/src/test/org/apache/solr/store/hdfs/HdfsDirectoryTest.java@@ -118,10 +118,10 @@ public class HdfsDirectoryTest extends SolrTestCaseJ4 {assertEquals(12345, input1.readInt());input1.close();- assertFalse(directory.fileExists("testing.test.other"));- assertTrue(directory.fileExists("testing.test"));+ assertFalse(slowFileExists(directory, "testing.test.other"));+ assertTrue(slowFileExists(directory, "testing.test"));directory.deleteFile("testing.test");- assertFalse(directory.fileExists("testing.test"));+ assertFalse(slowFileExists(directory, "testing.test"));}@Test