Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRESOLVER-372] Rework the FileUtils collocated temp file #364

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Objects.requireNonNull;

Expand All @@ -33,6 +39,10 @@
* @since 1.9.0
*/
public final class FileUtils {
// Logic borrowed from Commons-Lang3: we really need only this, to decide do we fsync on directories or not
private static final boolean IS_WINDOWS =
System.getProperty("os.name", "unknown").startsWith("Windows");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have plexus utils code for this, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and SystemUtils in commons-lang

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if external library is not used I would not like add next dependency for one simple function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if external library is not used I would not like add next dependency for one simple function

With this, I agree, but if we use it anyway, I wouldn't write the code myself.

private FileUtils() {
// hide constructor
}
Expand All @@ -52,7 +62,10 @@ public interface TempFile extends Closeable {
*/
public interface CollocatedTempFile extends TempFile {
/**
* Atomically moves temp file to target file it is collocated with.
* Upon close, atomically moves temp file to target file it is collocated with overwriting target (if exists).
* Invocation of this method merely signals that caller ultimately wants temp file to replace the target
* file, but when this method returns, the move operation did not yet happen, it will happen when this
* instance is closed.
*/
void move() throws IOException;
}
Expand Down Expand Up @@ -98,23 +111,79 @@ public static CollocatedTempFile newTempFile(Path file) throws IOException {
Path tempFile = parent.resolve(file.getFileName() + "."
+ Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp");
return new CollocatedTempFile() {
private final AtomicBoolean wantsMove = new AtomicBoolean(false);

@Override
public Path getPath() {
return tempFile;
}

@Override
public void move() throws IOException {
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE);
public void move() {
wantsMove.set(true);
}

@Override
public void close() throws IOException {
if (wantsMove.get() && Files.isReadable(tempFile)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not safe. The CollocatedTempFile should provide a way to create an OutputStream so that it can be closed before the CollocatedTempFile. This is needed so that buffered streams are fully written to disk before moving the file.
See

try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()));
FileUtils.CollocatedTempFile tempTarget = FileUtils.newTempFile(target.toPath());
OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempTarget.getPath()))) {
long result = copy(out, in, listener);
tempTarget.move();
return result;
}

The code is using a BufferedOutputStream so that it may not be completely written to the disk. I'm not sure if there's any guarantee on the ordering of the close() methods in the try/finally block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so order is specified as being the reversed order of declaration, which makes sense of course, so this should work because the BufferedOutputStream will be closed just before the CollocatedTempFile.

if (IS_WINDOWS) {
copy(tempFile, file);
} else {
fsyncFile(tempFile);
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE);
fsyncParent(tempFile);
}
}
Files.deleteIfExists(tempFile);
}
};
}

/**
* On Windows we use pre-NIO2 way to copy files, as for some reason it works. Beat me why.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beat, why?

*/
private static void copy(Path source, Path target) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
byte[] array = buffer.array();
try (InputStream is = Files.newInputStream(source);
OutputStream os = Files.newOutputStream(target)) {
while (true) {
int bytes = is.read(array);
if (bytes < 0) {
break;
}
os.write(array, 0, bytes);
}
}
}

/**
* Performs fsync: makes sure no OS "dirty buffers" exist for given file.
*
* @param target Path that must not be {@code null}, must exist as plain file.
*/
private static void fsyncFile(Path target) throws IOException {
try (FileChannel file = FileChannel.open(target, StandardOpenOption.WRITE)) {
file.force(true);
}
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Performs directory fsync: not usable on Windows, but some other OSes may also throw, hence thrown IO exception
* is just ignored.
*
* @param target Path that must not be {@code null}, must exist as plain file, and must have parent.
*/
private static void fsyncParent(Path target) throws IOException {
try (FileChannel parent = FileChannel.open(target.getParent(), StandardOpenOption.READ)) {
try {
parent.force(true);
} catch (IOException e) {
// ignore
}
}
}

/**
* A file writer, that accepts a {@link Path} to write some content to. Note: the file denoted by path may exist,
* hence implementation have to ensure it is able to achieve its goal ("replace existing" option or equivalent
Expand Down