Search in sources :

Example 81 with FileAlreadyExistsException

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

the class LocalizedResource method fetchUnzipToTemp.

@Override
public long fetchUnzipToTemp(ClientBlobStore store) throws IOException, KeyNotFoundException, AuthorizationException {
    String key = getKey();
    ReadableBlobMeta meta = store.getBlobMeta(key);
    if (!ServerUtils.canUserReadBlob(meta, user, conf)) {
        throw new WrappedAuthorizationException(user + " does not have READ access to " + key);
    }
    DownloadMeta downloadMeta = fetch(store, key, v -> {
        Path path = shouldUncompress ? tmpOutputLocation() : constructBlobWithVersionFileName(baseDir, getKey(), v);
        // we need to download to temp file and then unpack into the one requested
        Path parent = path.getParent();
        if (!Files.exists(parent)) {
            // There is a race here that we can still lose
            try {
                Files.createDirectories(parent);
            } catch (FileAlreadyExistsException e) {
            // Ignored
            } catch (IOException e) {
                LOG.error("Failed to create parent directory {}", parent, e);
                throw e;
            }
        }
        return path;
    }, FileOutputStream::new);
    Path finalLocation = downloadMeta.getDownloadPath();
    if (shouldUncompress) {
        Path downloadFile = finalLocation;
        finalLocation = constructBlobWithVersionFileName(baseDir, getKey(), downloadMeta.getVersion());
        ServerUtils.unpack(downloadFile.toFile(), finalLocation.toFile(), symLinksDisabled);
        LOG.debug("Uncompressed {} to: {}", downloadFile, finalLocation);
    }
    setBlobPermissions(conf, user, finalLocation);
    return downloadMeta.getVersion();
}
Also used : Path(java.nio.file.Path) WrappedAuthorizationException(org.apache.storm.utils.WrappedAuthorizationException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileOutputStream(java.io.FileOutputStream) ReadableBlobMeta(org.apache.storm.generated.ReadableBlobMeta) IOException(java.io.IOException)

Example 82 with FileAlreadyExistsException

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

the class ShardRecoveryManager method restoreFromBackup.

@VisibleForTesting
void restoreFromBackup(UUID shardUuid, long shardSize, OptionalLong shardXxhash64) {
    File storageFile = localFileSystem.get().pathToFile(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 (!isFileCorrupt(storageFile, shardSize, shardXxhash64)) {
            return;
        }
        stats.incrementCorruptLocalFile();
        quarantineFile(shardUuid, storageFile, "Local file is corrupt.");
    }
    // create a temporary file in the staging directory
    File stagingFile = temporarySuffix(localFileSystem.get().pathToFile(storageService.getStagingFile(shardUuid)));
    storageService.createParents(new Path(stagingFile.toURI()));
    // 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(new Path(storageFile.toURI()));
    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()) {
        stats.incrementShardRecoveryFailure();
        throw new PrestoException(RAPTOR_RECOVERY_ERROR, "File does not exist after recovery: " + shardUuid);
    }
    if (isFileCorrupt(storageFile, shardSize, shardXxhash64)) {
        stats.incrementShardRecoveryFailure();
        stats.incrementCorruptRecoveredFile();
        quarantineFile(shardUuid, storageFile, "Local file is corrupt after recovery.");
        throw new PrestoException(RAPTOR_BACKUP_CORRUPTION, "Backup is corrupt after read: " + shardUuid);
    }
    stats.incrementShardRecoverySuccess();
}
Also used : Path(org.apache.hadoop.fs.Path) 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 83 with FileAlreadyExistsException

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

the class HistoryServerArchiveFetcher method processArchive.

private void processArchive(String jobID, Path jobArchive) throws IOException {
    for (ArchivedJson archive : FsJobArchivist.getArchivedJsons(jobArchive)) {
        String path = archive.getPath();
        String json = archive.getJson();
        File target;
        if (path.equals(JobsOverviewHeaders.URL)) {
            target = new File(webOverviewDir, jobID + JSON_FILE_ENDING);
        } else if (path.equals("/joboverview")) {
            // legacy path
            LOG.debug("Migrating legacy archive {}", jobArchive);
            json = convertLegacyJobOverview(json);
            target = new File(webOverviewDir, jobID + JSON_FILE_ENDING);
        } else {
            // this implicitly writes into webJobDir
            target = new File(webDir, path + JSON_FILE_ENDING);
        }
        java.nio.file.Path parent = target.getParentFile().toPath();
        try {
            Files.createDirectories(parent);
        } catch (FileAlreadyExistsException ignored) {
        // there may be left-over directories from the previous
        // attempt
        }
        java.nio.file.Path targetPath = target.toPath();
        // We overwrite existing files since this may be another attempt
        // at fetching this archive.
        // Existing files may be incomplete/corrupt.
        Files.deleteIfExists(targetPath);
        Files.createFile(target.toPath());
        try (FileWriter fw = new FileWriter(target)) {
            fw.write(json);
            fw.flush();
        }
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileWriter(java.io.FileWriter) File(java.io.File)

Example 84 with FileAlreadyExistsException

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

the class DumpCommandIT method shouldGiveAClearErrorIfTheArchiveAlreadyExists.

@Test
void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws Exception {
    doThrow(new FileAlreadyExistsException("the-archive-path")).when(dumper).dump(any(), any(), any(), any(), any());
    CommandFailedException commandFailed = assertThrows(CommandFailedException.class, () -> execute("foo"));
    assertEquals("Archive already exists: the-archive-path", commandFailed.getMessage());
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CommandFailedException(org.neo4j.cli.CommandFailedException) Test(org.junit.jupiter.api.Test)

Example 85 with FileAlreadyExistsException

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

the class EphemeralFileSystemAbstraction method renameFile.

@Override
public void renameFile(Path from, Path 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.getParent())) {
        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)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)104 Path (java.nio.file.Path)49 IOException (java.io.IOException)44 File (java.io.File)24 NoSuchFileException (java.nio.file.NoSuchFileException)22 Test (org.junit.Test)15 FileNotFoundException (java.io.FileNotFoundException)10 FileSystemException (java.nio.file.FileSystemException)9 AccessDeniedException (java.nio.file.AccessDeniedException)8 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)8 NotDirectoryException (java.nio.file.NotDirectoryException)7 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)5 CopyOption (java.nio.file.CopyOption)5 FileSystem (java.nio.file.FileSystem)5 FileVisitResult (java.nio.file.FileVisitResult)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4