-
Notifications
You must be signed in to change notification settings - Fork 439
RATIS-2260. AtomicFileOutputStream should truncate temp file #1237
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
Conversation
szetszwo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adoroszlai , thanks a lot for working on this! Please see the comment inlined.
|
|
||
| public AtomicFileOutputStream(File outFile, File tmpFile) throws IOException { | ||
| super(FileUtils.newOutputStreamForceAtClose(tmpFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE)); | ||
| super(FileUtils.newOutputStreamForceAtClose(tmpFile, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's pass a createNew parameter. Usually, we don't care about overwriting tmp file since it is just tmp. On the other hand, we don't want to throw an exception when server starting because of a leftover tmp file. The leftover tmp file may be due to killing the previous run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we allow the file to exist, at least we should truncate it. Otherwise leftover content may be included in the final file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right -- we need TRUNCATE_EXISTING.
0: 0123456789
0: aaaaaa6789
0: bbb
static void main(String[] args) throws Exception {
final File f = new File("a.txt");
try(OutputStream out = newOutputStreamForceAtClose(f, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
out.write("0123456789".getBytes(StandardCharsets.UTF_8));
}
dump(f);
try(OutputStream out = newOutputStreamForceAtClose(f, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
out.write("aaaaaa".getBytes(StandardCharsets.UTF_8));
}
dump(f);
try(OutputStream out = newOutputStreamForceAtClose(f, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
out.write("bbb".getBytes(StandardCharsets.UTF_8));
}
dump(f);
}
static void dump(File f) throws Exception {
try(BufferedReader in = new BufferedReader(new InputStreamReader(
newInputStream(f.toPath()), StandardCharsets.UTF_8))) {
String line;
for(int i = 0; (line = in.readLine()) != null; i++) {
System.out.printf("%3d: %s%n", i, line);
}
}
}
szetszwo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 the change looks good.
|
Thanks @szetszwo for reviewing and merging this. |
What changes were proposed in this pull request?
AtomicFileOutputStreamshould reject overwriting existing temp file.https://issues.apache.org/jira/browse/RATIS-2260
How was this patch tested?
CI:
https://github.com/adoroszlai/ratis/actions/runs/13900762173