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();
}
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();
}
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();
}
}
}
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());
}
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));
}
Aggregations