Search in sources :

Example 1 with AtomicMoveNotSupportedException

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

the class NodeEnvironment method ensureAtomicMoveSupported.

/**
     * This method tries to write an empty file and moves it using an atomic move operation.
     * This method throws an {@link IllegalStateException} if this operation is
     * not supported by the filesystem. This test is executed on each of the data directories.
     * This method cleans up all files even in the case of an error.
     */
public void ensureAtomicMoveSupported() throws IOException {
    final NodePath[] nodePaths = nodePaths();
    for (NodePath nodePath : nodePaths) {
        assert Files.isDirectory(nodePath.path) : nodePath.path + " is not a directory";
        final Path src = nodePath.path.resolve("__es__.tmp");
        final Path target = nodePath.path.resolve("__es__.final");
        try {
            Files.createFile(src);
            Files.move(src, target, StandardCopyOption.ATOMIC_MOVE);
        } catch (AtomicMoveNotSupportedException ex) {
            throw new IllegalStateException("atomic_move is not supported by the filesystem on path [" + nodePath.path + "] atomic_move is required for elasticsearch to work correctly.", ex);
        } finally {
            try {
                Files.deleteIfExists(src);
            } finally {
                Files.deleteIfExists(target);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ShardPath(org.elasticsearch.index.shard.ShardPath) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 2 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException 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 3 with AtomicMoveNotSupportedException

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

the class StandardDirectoryFactory method move.

/**
   * Override for more efficient moves.
   * 
   * Intended for use with replication - use
   * carefully - some Directory wrappers will
   * cache files for example.
   * 
   * You should first {@link Directory#sync(java.util.Collection)} any file that will be 
   * moved or avoid cached files through settings.
   * 
   * @throws IOException
   *           If there is a low-level I/O error.
   */
@Override
public void move(Directory fromDir, Directory toDir, String fileName, IOContext ioContext) throws IOException {
    Directory baseFromDir = getBaseDir(fromDir);
    Directory baseToDir = getBaseDir(toDir);
    if (baseFromDir instanceof FSDirectory && baseToDir instanceof FSDirectory) {
        Path path1 = ((FSDirectory) baseFromDir).getDirectory().toAbsolutePath();
        Path path2 = ((FSDirectory) baseToDir).getDirectory().toAbsolutePath();
        try {
            Files.move(path1.resolve(fileName), path2.resolve(fileName), StandardCopyOption.ATOMIC_MOVE);
        } catch (AtomicMoveNotSupportedException e) {
            Files.move(path1.resolve(fileName), path2.resolve(fileName));
        }
        return;
    }
    super.move(fromDir, toDir, fileName, ioContext);
}
Also used : Path(java.nio.file.Path) FSDirectory(org.apache.lucene.store.FSDirectory) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 4 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project graal by oracle.

the class IOHelper method copy.

static void copy(final Path source, final Path target, final FileSystem sourceFileSystem, final FileSystem targetFileSystem, CopyOption... options) throws IOException {
    if (source.equals(target)) {
        return;
    }
    final Path sourceReal = sourceFileSystem.toRealPath(source, LinkOption.NOFOLLOW_LINKS);
    final Path targetReal = targetFileSystem.toRealPath(target, LinkOption.NOFOLLOW_LINKS);
    if (sourceReal.equals(targetReal)) {
        return;
    }
    final Set<LinkOption> linkOptions = new HashSet<>();
    final Set<StandardCopyOption> copyOptions = EnumSet.noneOf(StandardCopyOption.class);
    for (CopyOption option : options) {
        if (option instanceof StandardCopyOption) {
            copyOptions.add((StandardCopyOption) option);
        } else if (option instanceof LinkOption) {
            linkOptions.add((LinkOption) option);
        }
    }
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.getFileName().toString(), target.getFileName().toString(), "Atomic move not supported");
    }
    final Map<String, Object> sourceAttributes = sourceFileSystem.readAttributes(sourceReal, "basic:isSymbolicLink,isDirectory,lastModifiedTime,lastAccessTime,creationTime", linkOptions.toArray(new LinkOption[linkOptions.size()]));
    if ((Boolean) sourceAttributes.getOrDefault("isSymbolicLink", false)) {
        throw new IOException("Copying of symbolic links is not supported.");
    }
    if (copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
        try {
            targetFileSystem.delete(targetReal);
        } catch (NoSuchFileException notFound) {
        // Does not exist - nothing to delete
        }
    } else {
        boolean exists;
        try {
            targetFileSystem.checkAccess(targetReal, EnumSet.noneOf(AccessMode.class));
            exists = true;
        } catch (IOException ioe) {
            exists = false;
        }
        if (exists) {
            throw new FileAlreadyExistsException(target.toString());
        }
    }
    if ((Boolean) sourceAttributes.getOrDefault("isDirectory", false)) {
        targetFileSystem.createDirectory(targetReal);
    } else {
        final Set<StandardOpenOption> readOptions = EnumSet.of(StandardOpenOption.READ);
        final Set<StandardOpenOption> writeOptions = EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
        try (final SeekableByteChannel sourceChannel = sourceFileSystem.newByteChannel(sourceReal, readOptions);
            final SeekableByteChannel targetChannel = targetFileSystem.newByteChannel(targetReal, writeOptions)) {
            final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 16);
            while (sourceChannel.read(buffer) != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    targetChannel.write(buffer);
                }
                buffer.clear();
            }
        }
    }
    if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
        String[] basicMutableAttributes = { "lastModifiedTime", "lastAccessTime", "creationTime" };
        try {
            for (String key : basicMutableAttributes) {
                final Object value = sourceAttributes.get(key);
                if (value != null) {
                    targetFileSystem.setAttribute(targetReal, key, value);
                }
            }
        } catch (Throwable rootCause) {
            try {
                targetFileSystem.delete(targetReal);
            } catch (Throwable suppressed) {
                rootCause.addSuppressed(suppressed);
            }
            throw rootCause;
        }
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) LinkOption(java.nio.file.LinkOption) StandardOpenOption(java.nio.file.StandardOpenOption) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SeekableByteChannel(java.nio.channels.SeekableByteChannel) StandardCopyOption(java.nio.file.StandardCopyOption) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) AccessMode(java.nio.file.AccessMode) HashSet(java.util.HashSet)

Example 5 with AtomicMoveNotSupportedException

use of java.nio.file.AtomicMoveNotSupportedException in project j2objc by google.

the class AtomicMoveNotSupportedExceptionTest method test_constructor$String$String$String.

public void test_constructor$String$String$String() {
    AtomicMoveNotSupportedException exception = new AtomicMoveNotSupportedException("source", "target", "reason");
    assertEquals("source", exception.getFile());
    assertEquals("target", exception.getOtherFile());
    assertEquals("reason", exception.getReason());
    assertTrue(exception instanceof FileSystemException);
}
Also used : FileSystemException(java.nio.file.FileSystemException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Aggregations

AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)21 Path (java.nio.file.Path)11 IOException (java.io.IOException)9 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)7 NoSuchFileException (java.nio.file.NoSuchFileException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 FileSystemException (java.nio.file.FileSystemException)5 AccessDeniedException (java.nio.file.AccessDeniedException)4 CopyOption (java.nio.file.CopyOption)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 NotDirectoryException (java.nio.file.NotDirectoryException)4 StandardCopyOption (java.nio.file.StandardCopyOption)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)3 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)3