Search in sources :

Example 6 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project neo4j by neo4j.

the class DumpCommandTest method shouldGiveAClearErrorIfTheArchiveAlreadyExists.

@Test
public void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws Exception {
    doThrow(new FileAlreadyExistsException("the-archive-path")).when(dumper).dump(any(), any(), any());
    try {
        execute("foo.db");
        fail("expected exception");
    } catch (CommandFailed e) {
        assertThat(e.getMessage(), equalTo("archive already exists: the-archive-path"));
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CommandFailed(org.neo4j.commandline.admin.CommandFailed) Test(org.junit.Test)

Example 7 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project neo4j by neo4j.

the class DumperTest method shouldGiveAClearErrorIfTheArchiveAlreadyExists.

@Test
public void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws IOException {
    Path directory = testDirectory.directory("a-directory").toPath();
    Path archive = testDirectory.file("the-archive.dump").toPath();
    Files.write(archive, new byte[0]);
    try {
        new Dumper().dump(directory, archive, Predicates.alwaysFalse());
        fail("Expected an exception");
    } catch (FileAlreadyExistsException e) {
        assertEquals(archive.toString(), e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Test(org.junit.Test)

Example 8 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project neo4j by neo4j.

the class EphemeralFileSystemAbstraction method renameFile.

@Override
public void renameFile(File from, File to, CopyOption... copyOptions) throws IOException {
    from = canonicalFile(from);
    to = canonicalFile(to);
    if (!files.containsKey(from)) {
        throw new NoSuchFileException("'" + from + "' doesn't exist");
    }
    boolean replaceExisting = false;
    for (CopyOption copyOption : copyOptions) {
        replaceExisting |= copyOption == REPLACE_EXISTING;
    }
    if (files.containsKey(to) && !replaceExisting) {
        throw new FileAlreadyExistsException("'" + to + "' already exists");
    }
    if (!isDirectory(to.getParentFile())) {
        throw new NoSuchFileException("Target directory[" + to.getParent() + "] does not exists");
    }
    files.put(to, files.remove(from));
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CopyOption(java.nio.file.CopyOption) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 9 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project presto by prestodb.

the class ShardRecoveryManager method restoreFromBackup.

@VisibleForTesting
void restoreFromBackup(UUID shardUuid, OptionalLong shardSize) {
    File storageFile = storageService.getStorageFile(shardUuid);
    if (!backupStore.get().shardExists(shardUuid)) {
        stats.incrementShardRecoveryBackupNotFound();
        throw new PrestoException(RAPTOR_RECOVERY_ERROR, "No backup file found for shard: " + shardUuid);
    }
    if (storageFile.exists()) {
        if (!shardSize.isPresent() || (storageFile.length() == shardSize.getAsLong())) {
            return;
        }
        log.warn("Local shard file is corrupt. Deleting local file: %s", storageFile);
        storageFile.delete();
    }
    // create a temporary file in the staging directory
    File stagingFile = temporarySuffix(storageService.getStagingFile(shardUuid));
    storageService.createParents(stagingFile);
    // copy to temporary file
    log.info("Copying shard %s from backup...", shardUuid);
    long start = System.nanoTime();
    try {
        backupStore.get().restoreShard(shardUuid, stagingFile);
    } catch (PrestoException e) {
        stats.incrementShardRecoveryFailure();
        stagingFile.delete();
        throw e;
    }
    Duration duration = nanosSince(start);
    DataSize size = succinctBytes(stagingFile.length());
    DataSize rate = dataRate(size, duration).convertToMostSuccinctDataSize();
    stats.addShardRecoveryDataRate(rate, size, duration);
    log.info("Copied shard %s from backup in %s (%s at %s/s)", shardUuid, duration, size, rate);
    // move to final location
    storageService.createParents(storageFile);
    try {
        Files.move(stagingFile.toPath(), storageFile.toPath(), ATOMIC_MOVE);
    } catch (FileAlreadyExistsException e) {
    // someone else already created it (should not happen, but safe to ignore)
    } catch (IOException e) {
        stats.incrementShardRecoveryFailure();
        throw new PrestoException(RAPTOR_RECOVERY_ERROR, "Failed to move shard: " + shardUuid, e);
    } finally {
        stagingFile.delete();
    }
    if (!storageFile.exists() || (shardSize.isPresent() && (storageFile.length() != shardSize.getAsLong()))) {
        stats.incrementShardRecoveryFailure();
        log.info("Files do not match after recovery. Deleting local file: " + shardUuid);
        storageFile.delete();
        throw new PrestoException(RAPTOR_RECOVERY_ERROR, "File not recovered correctly: " + shardUuid);
    }
    stats.incrementShardRecoverySuccess();
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DataSize.succinctDataSize(io.airlift.units.DataSize.succinctDataSize) DataSize(io.airlift.units.DataSize) PrestoException(com.facebook.presto.spi.PrestoException) Duration(io.airlift.units.Duration) IOException(java.io.IOException) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project flink by apache.

the class LocalFileSystem method create.

@Override
public FSDataOutputStream create(final Path filePath, final WriteMode overwrite) throws IOException {
    checkNotNull(filePath, "filePath");
    if (exists(filePath) && overwrite == WriteMode.NO_OVERWRITE) {
        throw new FileAlreadyExistsException("File already exists: " + filePath);
    }
    final Path parent = filePath.getParent();
    if (parent != null && !mkdirs(parent)) {
        throw new IOException("Mkdirs failed to create " + parent);
    }
    final File file = pathToFile(filePath);
    return new LocalDataOutputStream(file);
}
Also used : Path(org.apache.flink.core.fs.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)38 IOException (java.io.IOException)15 Path (java.nio.file.Path)14 NoSuchFileException (java.nio.file.NoSuchFileException)9 Test (org.junit.Test)9 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)5 AccessDeniedException (java.nio.file.AccessDeniedException)5 FileSystem (java.nio.file.FileSystem)4 FileSystemException (java.nio.file.FileSystemException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4 VertxException (io.vertx.core.VertxException)3 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)3 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 FileSystemLoopException (java.nio.file.FileSystemLoopException)3 NotDirectoryException (java.nio.file.NotDirectoryException)3 ZipFile (java.util.zip.ZipFile)3 StorageException (com.google.cloud.storage.StorageException)2