/** * Create a FileOutputStream using * {@link FileOutputStream#FileOutputStream(File, boolean)}. * * Wraps the created output stream to intercept write calls * before delegating to the wrapped stream. * * @param volume target volume. null if unavailable. * @param f File object. * @return FileOutputStream to the given file object. * @throws FileNotFoundException */ public FileOutputStream getFileOutputStream( @Nullable FsVolumeSpi volume, File f) throws FileNotFoundException { return getFileOutputStream(volume, f, false); }
@Override public OutputStream createRestartMetaStream() throws IOException { File blockFile = getBlockFile(); File restartMeta = new File(blockFile.getParent() + File.pathSeparator + "." + blockFile.getName() + ".restart"); if (!getFileIoProvider().deleteWithExistsCheck(getVolume(), restartMeta)) { DataNode.LOG.warn("Failed to delete restart meta file: " + restartMeta.getPath()); } return getFileIoProvider().getFileOutputStream(getVolume(), restartMeta); }
@Override public OutputStream getDataOutputStream(boolean append) throws IOException { return getFileIoProvider().getFileOutputStream( getVolume(), getBlockFile(), append); }
@Override public void save() throws IOException { state.lastSavedMs = Time.now(); boolean success = false; try (BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(fileIoProvider.getFileOutputStream( FsVolumeImpl.this, getTempSaveFile()), "UTF-8"))) { WRITER.writeValue(writer, state); success = true; } finally { if (!success) { fileIoProvider.delete(FsVolumeImpl.this, getTempSaveFile()); } } fileIoProvider.move(FsVolumeImpl.this, getTempSaveFile().toPath(), getSaveFile().toPath(), StandardCopyOption.ATOMIC_MOVE); if (LOG.isTraceEnabled()) { LOG.trace("save({}, {}): saved {}", storageID, bpid, WRITER.writeValueAsString(state)); } }
private void saveReplicas(BlockListAsLongs blocksListToPersist) { if (blocksListToPersist == null || blocksListToPersist.getNumberOfBlocks()== 0) { return; } final File tmpFile = new File(currentDir, REPLICA_CACHE_FILE + ".tmp"); final File replicaCacheFile = new File(currentDir, REPLICA_CACHE_FILE); if (!fileIoProvider.deleteWithExistsCheck(volume, tmpFile) || !fileIoProvider.deleteWithExistsCheck(volume, replicaCacheFile)) { return; } FileOutputStream out = null; try { out = fileIoProvider.getFileOutputStream(volume, tmpFile); blocksListToPersist.writeTo(out); out.close(); // Renaming the tmp file to replicas fileIoProvider.moveFile(volume, tmpFile, replicaCacheFile); } catch (Exception e) { // If write failed, the volume might be bad. Since the cache file is // not critical, log the error, delete both the files (tmp and cache) // and continue. LOG.warn("Failed to write replicas to cache ", e); fileIoProvider.deleteWithExistsCheck(volume, replicaCacheFile); } finally { IOUtils.closeStream(out); fileIoProvider.deleteWithExistsCheck(volume, tmpFile); } }
FileOutputStream crcOut = null; try { blockOut = fileIoProvider.getFileOutputStream( getVolume(), new RandomAccessFile(blockFile, "rw").getFD()); crcOut = fileIoProvider.getFileOutputStream(getVolume(), metaRAF.getFD()); if (!isCreate) { blockOut.getChannel().position(blockDiskSize);
try (FileInputStream in = fileIoProvider.getFileInputStream( getVolume(), file)) { try (FileOutputStream out = fileIoProvider.getFileOutputStream( getVolume(), tmpFile)) { IOUtils.copyBytes(in, out, 16 * 1024);