Search in sources :

Example 6 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.

the class BlobStoreRepository method getRepositoryData.

@Override
public RepositoryData getRepositoryData() {
    try {
        final long indexGen = latestIndexBlobId();
        final String snapshotsIndexBlobName = INDEX_FILE_PREFIX + Long.toString(indexGen);
        RepositoryData repositoryData;
        try (InputStream blob = snapshotsBlobContainer.readBlob(snapshotsIndexBlobName)) {
            BytesStreamOutput out = new BytesStreamOutput();
            Streams.copy(blob, out);
            // EMPTY is safe here because RepositoryData#fromXContent calls namedObject
            try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, out.bytes())) {
                repositoryData = RepositoryData.snapshotsFromXContent(parser, indexGen);
            } catch (NotXContentException e) {
                logger.warn("[{}] index blob is not valid x-content [{} bytes]", snapshotsIndexBlobName, out.bytes().length());
                throw e;
            }
        }
        // now load the incompatible snapshot ids, if they exist
        try (InputStream blob = snapshotsBlobContainer.readBlob(INCOMPATIBLE_SNAPSHOTS_BLOB)) {
            BytesStreamOutput out = new BytesStreamOutput();
            Streams.copy(blob, out);
            try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, out.bytes())) {
                repositoryData = repositoryData.incompatibleSnapshotsFromXContent(parser);
            }
        } catch (NoSuchFileException e) {
            logger.debug("[{}] Incompatible snapshots blob [{}] does not exist, the likely reason is that " + "there are no incompatible snapshots in the repository", metadata.name(), INCOMPATIBLE_SNAPSHOTS_BLOB);
        }
        return repositoryData;
    } catch (NoSuchFileException ex) {
        // repository doesn't have an index blob, its a new blank repo
        return RepositoryData.EMPTY;
    } catch (IOException ioe) {
        throw new RepositoryException(metadata.name(), "could not read repository data from index blob", ioe);
    }
}
Also used : NotXContentException(org.elasticsearch.common.compress.NotXContentException) RateLimitingInputStream(org.elasticsearch.index.snapshots.blobstore.RateLimitingInputStream) FilterInputStream(java.io.FilterInputStream) SlicedInputStream(org.elasticsearch.index.snapshots.blobstore.SlicedInputStream) InputStream(java.io.InputStream) NoSuchFileException(java.nio.file.NoSuchFileException) RepositoryException(org.elasticsearch.repositories.RepositoryException) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentParser(org.elasticsearch.common.xcontent.XContentParser) RepositoryData(org.elasticsearch.repositories.RepositoryData)

Example 7 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.

the class URLBlobStoreTests method testNoBlobFound.

public void testNoBlobFound() throws IOException {
    BlobContainer container = urlBlobStore.blobContainer(BlobPath.cleanPath().add("indices"));
    String incorrectBlobName = "incorrect_" + blobName;
    try (InputStream ignored = container.readBlob(incorrectBlobName)) {
        fail("Should have thrown NoSuchFileException exception");
        ignored.read();
    } catch (NoSuchFileException e) {
        assertEquals(String.format("[%s] blob not found", incorrectBlobName), e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 8 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.

the class FileBasedUnicastHostsProvider method buildDynamicNodes.

@Override
public List<DiscoveryNode> buildDynamicNodes() {
    List<String> hostsList;
    try (Stream<String> lines = Files.lines(unicastHostsFilePath)) {
        hostsList = // lines starting with `#` are comments
        lines.filter(line -> line.startsWith("#") == false).collect(Collectors.toList());
    } catch (FileNotFoundException | NoSuchFileException e) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("[discovery-file] Failed to find unicast hosts file [{}]", unicastHostsFilePath), e);
        hostsList = Collections.emptyList();
    } catch (IOException e) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("[discovery-file] Error reading unicast hosts file [{}]", unicastHostsFilePath), e);
        hostsList = Collections.emptyList();
    }
    final List<DiscoveryNode> discoNodes = new ArrayList<>();
    try {
        discoNodes.addAll(resolveHostsLists(executorService, logger, hostsList, 1, transportService, UNICAST_HOST_PREFIX, resolveTimeout));
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    logger.debug("[discovery-file] Using dynamic discovery nodes {}", discoNodes);
    return discoNodes;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 9 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.

the class StreamInput method readException.

public <T extends Exception> T readException() throws IOException {
    if (readBoolean()) {
        int key = readVInt();
        switch(key) {
            case 0:
                final int ord = readVInt();
                return (T) ElasticsearchException.readException(this, ord);
            case 1:
                String msg1 = readOptionalString();
                String resource1 = readOptionalString();
                return (T) readStackTrace(new CorruptIndexException(msg1, resource1, readException()), this);
            case 2:
                String resource2 = readOptionalString();
                int version2 = readInt();
                int minVersion2 = readInt();
                int maxVersion2 = readInt();
                return (T) readStackTrace(new IndexFormatTooNewException(resource2, version2, minVersion2, maxVersion2), this);
            case 3:
                String resource3 = readOptionalString();
                if (readBoolean()) {
                    int version3 = readInt();
                    int minVersion3 = readInt();
                    int maxVersion3 = readInt();
                    return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3, minVersion3, maxVersion3), this);
                } else {
                    String version3 = readOptionalString();
                    return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3), this);
                }
            case 4:
                return (T) readStackTrace(new NullPointerException(readOptionalString()), this);
            case 5:
                return (T) readStackTrace(new NumberFormatException(readOptionalString()), this);
            case 6:
                return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readException()), this);
            case 7:
                return (T) readStackTrace(new AlreadyClosedException(readOptionalString(), readException()), this);
            case 8:
                return (T) readStackTrace(new EOFException(readOptionalString()), this);
            case 9:
                return (T) readStackTrace(new SecurityException(readOptionalString(), readException()), this);
            case 10:
                return (T) readStackTrace(new StringIndexOutOfBoundsException(readOptionalString()), this);
            case 11:
                return (T) readStackTrace(new ArrayIndexOutOfBoundsException(readOptionalString()), this);
            case 12:
                return (T) readStackTrace(new FileNotFoundException(readOptionalString()), this);
            case 13:
                final int subclass = readVInt();
                final String file = readOptionalString();
                final String other = readOptionalString();
                final String reason = readOptionalString();
                // skip the msg - it's composed from file, other and reason
                readOptionalString();
                final Exception exception;
                switch(subclass) {
                    case 0:
                        exception = new NoSuchFileException(file, other, reason);
                        break;
                    case 1:
                        exception = new NotDirectoryException(file);
                        break;
                    case 2:
                        exception = new DirectoryNotEmptyException(file);
                        break;
                    case 3:
                        exception = new AtomicMoveNotSupportedException(file, other, reason);
                        break;
                    case 4:
                        exception = new FileAlreadyExistsException(file, other, reason);
                        break;
                    case 5:
                        exception = new AccessDeniedException(file, other, reason);
                        break;
                    case 6:
                        exception = new FileSystemLoopException(file);
                        break;
                    case 7:
                        exception = new FileSystemException(file, other, reason);
                        break;
                    default:
                        throw new IllegalStateException("unknown FileSystemException with index " + subclass);
                }
                return (T) readStackTrace(exception, this);
            case 14:
                return (T) readStackTrace(new IllegalStateException(readOptionalString(), readException()), this);
            case 15:
                return (T) readStackTrace(new LockObtainFailedException(readOptionalString(), readException()), this);
            case 16:
                return (T) readStackTrace(new InterruptedException(readOptionalString()), this);
            case 17:
                return (T) readStackTrace(new IOException(readOptionalString(), readException()), this);
            default:
                assert false : "no such exception for id: " + key;
        }
    }
    return null;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NotDirectoryException(java.nio.file.NotDirectoryException) FileSystemException(java.nio.file.FileSystemException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) GeoPoint(org.elasticsearch.common.geo.GeoPoint) ElasticsearchException(org.elasticsearch.ElasticsearchException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) FileSystemLoopException(java.nio.file.FileSystemLoopException) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) AccessDeniedException(java.nio.file.AccessDeniedException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 10 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project buck by facebook.

the class MoreFiles method concatenateFiles.

/**
   * Concatenates the contents of one or more files.
   *
   * @param dest The path to which the concatenated files' contents are written.
   * @param pathsToConcatenate The paths whose contents are concatenated to {@code dest}.
   *
   * @return {@code true} if any data was concatenated to {@code dest}, {@code false} otherwise.
   */
public static boolean concatenateFiles(Path dest, Iterable<Path> pathsToConcatenate) throws IOException {
    // Concatenate all the logs to a temp file, then atomically rename it to the
    // passed-in concatenatedPath if any log data was collected.
    String tempFilename = "." + dest.getFileName() + ".tmp." + UUID.randomUUID().toString();
    Path tempPath = dest.resolveSibling(tempFilename);
    try {
        long bytesCollected = 0;
        try (OutputStream os = Files.newOutputStream(tempPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
            for (Path path : pathsToConcatenate) {
                try (InputStream is = Files.newInputStream(path)) {
                    bytesCollected += ByteStreams.copy(is, os);
                } catch (NoSuchFileException e) {
                    continue;
                }
            }
        }
        if (bytesCollected > 0) {
            Files.move(tempPath, dest, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
            return true;
        } else {
            return false;
        }
    } finally {
        Files.deleteIfExists(tempPath);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) NoSuchFileException(java.nio.file.NoSuchFileException)

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