Search in sources :

Example 21 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project jdk8u_jdk by JetBrains.

the class FileUtils method deleteFileWithRetry0.

private static void deleteFileWithRetry0(Path path) throws IOException, InterruptedException {
    int times = 0;
    IOException ioe = null;
    while (true) {
        try {
            Files.delete(path);
            while (Files.exists(path)) {
                times++;
                if (times > MAX_RETRY_DELETE_TIMES)
                    throw new IOException("File still exists after " + times + " waits.");
                Thread.sleep(RETRY_DELETE_MILLIS);
            }
            break;
        } catch (NoSuchFileException | DirectoryNotEmptyException x) {
            throw x;
        } catch (IOException x) {
            // Backoff/retry in case another process is accessing the file
            times++;
            if (ioe == null)
                ioe = x;
            else
                ioe.addSuppressed(x);
            if (times > MAX_RETRY_DELETE_TIMES)
                throw ioe;
            Thread.sleep(RETRY_DELETE_MILLIS);
        }
    }
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException)

Example 22 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class SolrCore method getNewIndexDir.

/**
   * Returns the indexdir as given in index.properties. If index.properties exists in dataDir and
   * there is a property <i>index</i> available and it points to a valid directory
   * in dataDir that is returned Else dataDir/index is returned. Only called for creating new indexSearchers
   * and indexwriters. Use the getIndexDir() method to know the active index directory
   *
   * @return the indexdir as given in index.properties
   */
public String getNewIndexDir() {
    String result = dataDir + "index/";
    Properties p = new Properties();
    Directory dir = null;
    try {
        dir = getDirectoryFactory().get(getDataDir(), DirContext.META_DATA, getSolrConfig().indexConfig.lockType);
        IndexInput input;
        try {
            input = dir.openInput(IndexFetcher.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, StandardCharsets.UTF_8));
                String s = p.getProperty("index");
                if (s != null && s.trim().length() > 0) {
                    result = dataDir + s;
                }
            } catch (Exception e) {
                log.error("Unable to load " + IndexFetcher.INDEX_PROPERTIES, e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e) {
        SolrException.log(log, "", e);
    } finally {
        if (dir != null) {
            try {
                getDirectoryFactory().release(dir);
            } catch (IOException e) {
                SolrException.log(log, "", e);
            }
        }
    }
    if (!result.equals(lastNewIndexDir)) {
        log.debug("New index directory detected: old=" + lastNewIndexDir + " new=" + result);
    }
    lastNewIndexDir = result;
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexInput(org.apache.lucene.store.IndexInput) IOException(java.io.IOException) Properties(java.util.Properties) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException) Directory(org.apache.lucene.store.Directory)

Example 23 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class IndexFetcher method compareFile.

protected static CompareResult compareFile(Directory indexDir, String filename, Long backupIndexFileLen, Long backupIndexFileChecksum) {
    CompareResult compareResult = new CompareResult();
    try {
        try (final IndexInput indexInput = indexDir.openInput(filename, IOContext.READONCE)) {
            long indexFileLen = indexInput.length();
            long indexFileChecksum = 0;
            if (backupIndexFileChecksum != null) {
                try {
                    indexFileChecksum = CodecUtil.retrieveChecksum(indexInput);
                    compareResult.checkSummed = true;
                } catch (Exception e) {
                    LOG.warn("Could not retrieve checksum from file.", e);
                }
            }
            if (!compareResult.checkSummed) {
                if (indexFileLen == backupIndexFileLen) {
                    compareResult.equal = true;
                    return compareResult;
                } else {
                    LOG.info("File {} did not match. expected length is {} and actual length is {}", filename, backupIndexFileLen, indexFileLen);
                    compareResult.equal = false;
                    return compareResult;
                }
            }
            if (indexFileLen == backupIndexFileLen && indexFileChecksum == backupIndexFileChecksum) {
                compareResult.equal = true;
                return compareResult;
            } else {
                LOG.warn("File {} did not match. expected checksum is {} and actual is checksum {}. " + "expected length is {} and actual length is {}", filename, backupIndexFileChecksum, indexFileChecksum, backupIndexFileLen, indexFileLen);
                compareResult.equal = false;
                return compareResult;
            }
        }
    } catch (NoSuchFileException | FileNotFoundException e) {
        compareResult.equal = false;
        return compareResult;
    } catch (IOException e) {
        LOG.error("Could not read file " + filename + ". Downloading it again", e);
        compareResult.equal = false;
        return compareResult;
    }
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) IndexInput(org.apache.lucene.store.IndexInput) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 24 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class BaseDirectoryTestCase method testFsyncDoesntCreateNewFiles.

// this test backdoors the directory via the filesystem. so it must actually use the filesystem
// TODO: somehow change this test to 
public void testFsyncDoesntCreateNewFiles() throws Exception {
    Path path = createTempDir("nocreate");
    Directory fsdir = getDirectory(path);
    // if it's using two FSdirs and so on
    if (fsdir instanceof FSDirectory == false) {
        fsdir.close();
        assumeTrue("test only works for FSDirectory subclasses", false);
    }
    // write a file
    IndexOutput out = fsdir.createOutput("afile", newIOContext(random()));
    out.writeString("boo");
    out.close();
    // delete it
    Files.delete(path.resolve("afile"));
    int fileCount = fsdir.listAll().length;
    // fsync it
    try {
        fsdir.sync(Collections.singleton("afile"));
        fail("didn't get expected exception, instead fsync created new files: " + Arrays.asList(fsdir.listAll()));
    } catch (FileNotFoundException | NoSuchFileException expected) {
    // ok
    }
    // no new files created
    assertEquals(fileCount, fsdir.listAll().length);
    fsdir.close();
}
Also used : Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 25 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class BaseDirectoryTestCase method testNoDir.

// LUCENE-3382 -- make sure we get exception if the directory really does not exist.
public void testNoDir() throws Throwable {
    Path tempDir = createTempDir("doesnotexist");
    IOUtils.rm(tempDir);
    Directory dir = getDirectory(tempDir);
    try {
        DirectoryReader.open(dir);
        fail("did not hit expected exception");
    } catch (NoSuchFileException | IndexNotFoundException nsde) {
    // expected
    }
    dir.close();
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) IndexNotFoundException(org.apache.lucene.index.IndexNotFoundException)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)255 IOException (java.io.IOException)103 Path (java.nio.file.Path)103 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)30 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)20 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 ArrayList (java.util.ArrayList)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 HashMap (java.util.HashMap)13 Map (java.util.Map)12 ByteBuffer (java.nio.ByteBuffer)11 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11