-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
More conservative write #8750
Closed
Closed
More conservative write #8750
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
242e799
Write to final file only of no exception during write happened
koppor 125e09e
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 69ba879
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 048729b
Add log on disk
koppor 2fb0657
Add writing to a local tmp file (instead of somewhere on a network dr…
koppor 1671496
Fix logger
koppor 977e2e2
Change file handling during writing
koppor 292de7a
Initial AtomicFileWriterTest
koppor 3e5f117
Add test case
koppor 902b52e
Revert "Add log on disk"
koppor a9172e6
Merge branch 'main' into more-conservative-save
koppor e948789
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 67e181c
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 84d09d2
Fix checkstyle
koppor 1c76e6d
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 36f4cf1
Remove "keepBackup" from AtomicFileOutputStream
koppor 2d21fe5
Add ADR-0026
koppor be4d336
Add links to ADR
koppor 75e0aed
Use more clear method
koppor 40157de
Fix typos
koppor 9054614
Add getUniqueFilePrefix(Path)
koppor 7bbf120
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 45d01ac
Fix checkstyle
koppor b8fc4f6
Fix org.jabref.logic.autosaveandbackup.BackupManager#performBackup to…
koppor ba392b3
Fix typos
koppor 4becafb
Move path determination method to FileUtil (will be reused at BackupM…
koppor a35a6de
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor ee078fd
Introduce BackupFileType
koppor bc7853c
Rename method
koppor d1dc0f4
Add test for getPathOfBackupFileAndCreateDirectory()
koppor 8a08df7
Start to implement multiple backups
koppor 34b9dd9
Merge remote-tracking branch 'origin/main' into more-conservative-save
koppor 60ad4d1
Fix AtomicFileOutputStreamTest
koppor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ions/0026-safe-file-writing-by-writing-to-a-temporary-file-in-another-filder.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
parent: Decision Records | ||
nav_order: 26 | ||
--- | ||
# Safe File Writing by Writing to a Temporary File in Another Folder | ||
|
||
## Context and Problem Statement | ||
|
||
JabRef needs to write to the .bib file. A .bib file of a user should never be damaged. JabRef needs to provide a way to do "safe" writing of a bib file. | ||
|
||
## Considered Options | ||
|
||
* Create a temporary file in a local folder and copy after successful write | ||
* Create temporary file next to bib file and atomically move it to the original file | ||
|
||
## Decision Outcome | ||
|
||
Chosen option: "Create a temporary file in a local folder and copy after successful write", because good usage of Dropbox outweighs potential recovery scenarios | ||
|
||
## Pros and Cons of the Options | ||
|
||
### Create a temporary file in a local folder and copy after successful write | ||
|
||
* Good, because Keeps directory of .bib file clean | ||
* Good, because Keeping file access rights is simple as the file content is replaced, not the file itself | ||
* Bad, because Error recovery is hard | ||
|
||
### Create temporary file next to bib file and atomically move it to the original file | ||
|
||
This implementation is available at [Marty Lamb's AtomicFileOutputStream](https://github.com/martylamb/atomicfileoutputstream/blob/master/src/main/java/com/martiansoftware/io/AtomicFileOutputStream.java) | ||
and [Apache Zookeepr's AtomicFileOutputStream](https://github.com/apache/zookeeper/blob/master/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java). | ||
|
||
* Good, because Atomic move is an all-or-nothing move | ||
* Bad, because Makes issues with Dropbox, OneDrive, ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/org/jabref/logic/exporter/AtomicFileOutputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.jabref.logic.util.BackupFileType; | ||
import org.jabref.logic.util.io.BackupFileUtil; | ||
import org.jabref.logic.util.io.FileUtil; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [reviewdog] <com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck> reported by reviewdog 🐶 |
||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class AtomicFileOutputStreamTest { | ||
|
||
/** | ||
* Tests whether a failing write to a file keeps the respective .sav file | ||
*/ | ||
@Test | ||
public void savFileExistsOnDiskFull() throws Exception { | ||
String fiftyChars = Strings.repeat("1234567890", 5); | ||
String fiveThousandChars = Strings.repeat("A", 5_000); | ||
|
||
FileSystem fileSystem = Jimfs.newFileSystem( | ||
Configuration.unix().toBuilder() | ||
.setMaxSize(1_000) | ||
.setBlockSize(1_000).build()); | ||
|
||
Path out = fileSystem.getPath("out.bib"); | ||
Files.writeString(out, fiftyChars); | ||
|
||
// Running out of disk space should occur | ||
assertThrows(IOException.class, () -> { | ||
AtomicFileOutputStream atomicFileOutputStream = new AtomicFileOutputStream(out); | ||
InputStream inputStream = new ByteArrayInputStream(fiveThousandChars.getBytes()); | ||
inputStream.transferTo(atomicFileOutputStream); | ||
atomicFileOutputStream.close(); | ||
}); | ||
|
||
// Written file still has the contents as before the error | ||
assertEquals(fiftyChars, Files.readString(out)); | ||
} | ||
|
||
@Test | ||
void tempAndBackupDifferentPaths() { | ||
Path testFile = Path.of("test.bib"); | ||
assertNotEquals(AtomicFileOutputStream.getPathOfTemporaryFile(testFile), BackupFileUtil.getPathForNewBackupFileAndCreateDirectory(testFile, BackupFileType.SAVE)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Then hard-exiting JabRef during a write operation may leave the file corrupt and kind of defeats the purpose of the atomic writer.
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.
Note to self: I'll create an ADR. - The intended implementation fixes #8887.