Repository: brooklyn-server Updated Branches: refs/heads/master 25312b16a -> 06c9f621c
Sync file system on temp file before moving it. See https://issues.apache.org/jira/browse/BROOKLYN-526 Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/9fe0179a Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/9fe0179a Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/9fe0179a Branch: refs/heads/master Commit: 9fe0179a5fb04b7578c94684ecc5a6342ed79c06 Parents: d961de3 Author: Geoff Macartney <geoff.macart...@cloudsoftcorp.com> Authored: Fri Aug 4 12:09:34 2017 +0100 Committer: Geoff Macartney <geoff.macart...@cloudsoftcorp.com> Committed: Fri Aug 18 14:42:04 2017 +0100 ---------------------------------------------------------------------- .../persist/FileBasedStoreObjectAccessor.java | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9fe0179a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/FileBasedStoreObjectAccessor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/FileBasedStoreObjectAccessor.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/FileBasedStoreObjectAccessor.java index a8d7cf1..6a3de10 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/FileBasedStoreObjectAccessor.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/FileBasedStoreObjectAccessor.java @@ -19,8 +19,10 @@ package org.apache.brooklyn.core.mgmt.persist; import java.io.File; +import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.IOException; +import java.io.SyncFailedException; import java.util.Date; import org.apache.brooklyn.util.exceptions.Exceptions; @@ -87,7 +89,9 @@ public class FileBasedStoreObjectAccessor implements PersistenceObjectStore.Stor public void put(ByteSource bytes) { try { FileUtil.setFilePermissionsTo600(tmpFile); - Streams.copyClose(bytes.openStream(), new FileOutputStream(tmpFile)); + final FileOutputStream tempStream = new FileOutputStream(tmpFile); + Streams.copyClose(bytes.openStream(), tempStream); + syncFileSystem(tempStream.getFD()); FileBasedObjectStore.moveFile(tmpFile, file); } catch (IOException e) { throw Exceptions.propagateAnnotated("Problem writing data to file "+file+" (via temporary file "+tmpFile+")", e); @@ -96,6 +100,25 @@ public class FileBasedStoreObjectAccessor implements PersistenceObjectStore.Stor } } + private void syncFileSystem(final FileDescriptor fd) throws SyncFailedException { + // Simple retry a number of times; avoids Repeater to avoid complications of timeouts and separate threads + int maxTries = 3; + + SyncFailedException sfe = null; + for (int c = 0 ; c < maxTries ; c++) { + try { + fd.sync(); + sfe = null; + break; + } catch (SyncFailedException e) { + sfe = e; + } + } + if (sfe != null) { + throw sfe; + } + } + // TODO Should this write to the temporary file? Otherwise we'll risk getting a partial view of the write. @Override public void append(String val) {