Skip to content

Commit

Permalink
Don't try to sync directories on Windows (issue #7).
Browse files Browse the repository at this point in the history
  • Loading branch information
archiecobbs committed Jul 11, 2016
1 parent 7eff8e8 commit 287c94c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Version Next

- Don't try to sync directories on Windows (issue #7)
- Fixed bug in the Raft follower algorithm when probing is enabled.
- Added new CLI command `raft-fallback-force-standalone'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -160,7 +161,7 @@ public class AtomicArrayKVStore extends AbstractKVStore implements AtomicKVStore
private File valsFile;
private File modsFile;
private FileOutputStream modsFileOutput;
private FileChannel directoryChannel;
private FileChannel directoryChannel; // not used on Windows
private long modsFileLength;
private long modsFileSyncPoint;
private ByteBuffer indx;
Expand Down Expand Up @@ -350,7 +351,12 @@ public Thread newThread(Runnable action) {
throw new ArrayKVException("file `" + this.directory + "' is not a directory");

// Get directory channel we can fsync()
this.directoryChannel = FileChannel.open(this.directory.toPath());
try {
this.directoryChannel = FileChannel.open(this.directory.toPath());
} catch (IOException e) {
if (!this.isWindows())
throw e;
}

// Open and lock the lock file
this.lockFile = new File(this.directory, LOCK_FILE_NAME);
Expand Down Expand Up @@ -396,14 +402,16 @@ public Thread newThread(Runnable action) {
keysOutput.getChannel().force(false);
indxOutput.getChannel().force(false);
}
this.directoryChannel.force(false);
if (this.directoryChannel != null)
this.directoryChannel.force(false);

// Create generation file
try (FileOutputStream output = new FileOutputStream(this.generationFile)) {
new PrintStream(output, true).println(0);
output.getChannel().force(false);
}
this.directoryChannel.force(false);
if (this.directoryChannel != null)
this.directoryChannel.force(false);
}

// Read current generation number
Expand Down Expand Up @@ -1179,7 +1187,8 @@ private void compact(final Compaction compaction) throws IOException {
assert newModsFile.exists();

// Sync directory
this.directoryChannel.force(false);
if (this.directoryChannel != null)
this.directoryChannel.force(false);

// We're done creating files
success = true;
Expand Down Expand Up @@ -1285,10 +1294,12 @@ private void compact(final Compaction compaction) throws IOException {
this.mods = new MutableView(this.kvstore, null, this.mods.getWrites());

// Sync directory prior to deleting files
try {
this.directoryChannel.force(false);
} catch (IOException e) {
this.log.error("error syncing directory " + this.directory + " (ignoring)", e);
if (this.directoryChannel != null) {
try {
this.directoryChannel.force(false);
} catch (IOException e) {
this.log.error("error syncing directory " + this.directory + " (ignoring)", e);
}
}

// Delete old files
Expand Down Expand Up @@ -1355,6 +1366,10 @@ private void compact(final Compaction compaction) throws IOException {
}
}

private boolean isWindows() {
return System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH).indexOf("win") != -1;
}

// Compaction

private class Compaction implements Runnable {
Expand Down

0 comments on commit 287c94c

Please sign in to comment.