From cfa3f365d6e526a10aa80004c7291cf1c89fbb0e Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Tue, 9 Nov 2010 19:12:24 -0800 Subject: [PATCH 1/5] Simplify LockFile write(ObjectId) case The ObjectId (for a ref) can be easily reformatted into a temporary byte[] and then passed off to write(byte[]), removing the duplicated code that existed in both write methods. Change-Id: I09740658e070d5f22682333a2e0d325fd1c4a6cb Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/storage/file/LockFile.java | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java index e8bc3e2cf..1a4952ad0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java @@ -44,7 +44,6 @@ package org.eclipse.jgit.storage.file; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -229,26 +228,10 @@ public class LockFile { * before throwing the underlying exception to the caller. */ public void write(final ObjectId id) throws IOException { - requireLock(); - try { - final BufferedOutputStream b; - b = new BufferedOutputStream(os, Constants.OBJECT_ID_STRING_LENGTH + 1); - id.copyTo(b); - b.write('\n'); - b.flush(); - fLck.release(); - b.close(); - os = null; - } catch (IOException ioe) { - unlock(); - throw ioe; - } catch (RuntimeException ioe) { - unlock(); - throw ioe; - } catch (Error ioe) { - unlock(); - throw ioe; - } + byte[] buf = new byte[Constants.OBJECT_ID_STRING_LENGTH + 1]; + id.copyTo(buf, 0); + buf[Constants.OBJECT_ID_STRING_LENGTH] = '\n'; + write(buf); } /** From 24fccadeda0a323d9b438dafab33f44491bb15d9 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Tue, 9 Nov 2010 18:58:36 -0800 Subject: [PATCH 2/5] Support core.fsyncObjectFiles option Some repositories may be on really unstable filesystems, but still want to have good reliability when objects are written to disk. If core.fsyncObjectFiles is set to true, request the JVM to ensure the data is written before returning success to the caller of insert. The option defaults to false because it should be useless on any filesystem that orders writes and metadata, such as ext3 mounted with data=ordered (or data=journal). But it may be useful on some systems (especially HFS+) where file content may flush to the disk independently of filesystem structure changes. Because FileChannel.force(boolean) only claims to ensure data is written if it was written using the write(ByteBuffer) method of FileChannel, redirect all writes when using fsyncObjectFiles to go through the FileChannel interface instead of through the older style OutputStream interface. This may not be necessary on all JVMs, but its more portable to follow the definition than the common behavior. Change-Id: I57f6b6bb7e403c07fbae989dbf3758eaf5edbc78 Signed-off-by: Shawn O. Pearce --- .../storage/file/ObjectDirectoryInserter.java | 22 ++++-- .../jgit/storage/file/WriteConfig.java | 74 +++++++++++++++++++ 2 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryInserter.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryInserter.java index 16cb8aa35..d922bebe8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryInserter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryInserter.java @@ -52,6 +52,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.channels.Channels; import java.security.DigestOutputStream; import java.security.MessageDigest; import java.util.zip.Deflater; @@ -60,7 +61,6 @@ import java.util.zip.DeflaterOutputStream; import org.eclipse.jgit.errors.ObjectWritingException; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.CoreConfig; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; @@ -68,13 +68,13 @@ import org.eclipse.jgit.lib.ObjectInserter; class ObjectDirectoryInserter extends ObjectInserter { private final FileObjectDatabase db; - private final Config config; + private final WriteConfig config; private Deflater deflate; ObjectDirectoryInserter(final FileObjectDatabase dest, final Config cfg) { db = dest; - config = cfg; + config = cfg.get(WriteConfig.KEY); } @Override @@ -121,9 +121,13 @@ class ObjectDirectoryInserter extends ObjectInserter { boolean delete = true; File tmp = newTempFile(); try { - DigestOutputStream dOut = new DigestOutputStream( - compress(new FileOutputStream(tmp)), md); + FileOutputStream fOut = new FileOutputStream(tmp); try { + OutputStream out = fOut; + if (config.getFSyncObjectFiles()) + out = Channels.newOutputStream(fOut.getChannel()); + DeflaterOutputStream cOut = compress(out); + DigestOutputStream dOut = new DigestOutputStream(cOut, md); writeHeader(dOut, type, len); final byte[] buf = buffer(); @@ -134,8 +138,12 @@ class ObjectDirectoryInserter extends ObjectInserter { dOut.write(buf, 0, n); len -= n; } + dOut.flush(); + cOut.finish(); } finally { - dOut.close(); + if (config.getFSyncObjectFiles()) + fOut.getChannel().force(true); + fOut.close(); } delete = false; @@ -160,7 +168,7 @@ class ObjectDirectoryInserter extends ObjectInserter { DeflaterOutputStream compress(final OutputStream out) { if (deflate == null) - deflate = new Deflater(config.get(CoreConfig.KEY).getCompression()); + deflate = new Deflater(config.getCompression()); else deflate.reset(); return new DeflaterOutputStream(out, deflate); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java new file mode 100644 index 000000000..1f28d8b75 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.storage.file; + +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.lib.Config.SectionParser; +import org.eclipse.jgit.lib.CoreConfig; + +class WriteConfig { + /** Key for {@link Config#get(SectionParser)}. */ + static final Config.SectionParser KEY = new SectionParser() { + public WriteConfig parse(final Config cfg) { + return new WriteConfig(cfg); + } + }; + + private final int compression; + + private final boolean fsyncObjectFiles; + + private WriteConfig(final Config rc) { + compression = rc.get(CoreConfig.KEY).getCompression(); + fsyncObjectFiles = rc.getBoolean("core", "fsyncobjectfiles", false); + } + + int getCompression() { + return compression; + } + + boolean getFSyncObjectFiles() { + return fsyncObjectFiles; + } +} From e0e7fe531d08676ce13308bf2bfda809d866ed01 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 10 Nov 2010 17:24:16 -0800 Subject: [PATCH 3/5] Support core.fsyncRefFiles option If core.fsyncRefFiles is set to true, fsync is used whenever a reference file is updated, ensuring the file contents are also written to disk. This can help to prevent empty ref files after a system crash when using a filesystem such as HFS+ where data writes may be delayed. Change-Id: Ie508a974da50f63b0409c38afe68772322dc19f1 Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/storage/file/LockFile.java | 66 +++++++++++++++---- .../jgit/storage/file/RefDirectory.java | 14 +++- .../jgit/storage/file/RefDirectoryUpdate.java | 8 +++ .../jgit/storage/file/WriteConfig.java | 7 ++ 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java index 1a4952ad0..a794ec330 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java @@ -51,6 +51,9 @@ import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; import java.text.MessageFormat; @@ -92,6 +95,8 @@ public class LockFile { private boolean needStatInformation; + private boolean fsync; + private long commitLastModified; private final FS fs; @@ -191,10 +196,21 @@ public class LockFile { try { final FileInputStream fis = new FileInputStream(ref); try { - final byte[] buf = new byte[2048]; - int r; - while ((r = fis.read(buf)) >= 0) - os.write(buf, 0, r); + if (fsync) { + FileChannel in = fis.getChannel(); + long pos = 0; + long cnt = in.size(); + while (0 < cnt) { + long r = os.getChannel().transferFrom(in, pos, cnt); + pos += r; + cnt -= r; + } + } else { + final byte[] buf = new byte[2048]; + int r; + while ((r = fis.read(buf)) >= 0) + os.write(buf, 0, r); + } } finally { fis.close(); } @@ -251,8 +267,15 @@ public class LockFile { public void write(final byte[] content) throws IOException { requireLock(); try { - os.write(content); - os.flush(); + if (fsync) { + FileChannel fc = os.getChannel(); + ByteBuffer buf = ByteBuffer.wrap(content); + while (0 < buf.remaining()) + fc.write(buf); + fc.force(true); + } else { + os.write(content); + } fLck.release(); os.close(); os = null; @@ -279,34 +302,43 @@ public class LockFile { */ public OutputStream getOutputStream() { requireLock(); + + final OutputStream out; + if (fsync) + out = Channels.newOutputStream(os.getChannel()); + else + out = os; + return new OutputStream() { @Override public void write(final byte[] b, final int o, final int n) throws IOException { - os.write(b, o, n); + out.write(b, o, n); } @Override public void write(final byte[] b) throws IOException { - os.write(b); + out.write(b); } @Override public void write(final int b) throws IOException { - os.write(b); + out.write(b); } @Override public void flush() throws IOException { - os.flush(); + out.flush(); } @Override public void close() throws IOException { try { - os.flush(); + out.flush(); + if (fsync) + os.getChannel().force(true); fLck.release(); - os.close(); + out.close(); os = null; } catch (IOException ioe) { unlock(); @@ -339,6 +371,16 @@ public class LockFile { needStatInformation = on; } + /** + * Request that {@link #commit()} force dirty data to the drive. + * + * @param on + * true if dirty data should be forced to the drive. + */ + public void setFSync(final boolean on) { + fsync = on; + } + /** * Wait until the lock file information differs from the old file. *

diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java index 96c8361ad..2af7ca3e6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java @@ -67,6 +67,8 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; import java.text.MessageFormat; import java.util.Arrays; import java.util.LinkedList; @@ -606,6 +608,7 @@ public class RefDirectory extends RefDatabase { write = false; if (write) { + WriteConfig wc = getRepository().getConfig().get(WriteConfig.KEY); FileOutputStream out; try { out = new FileOutputStream(log, true); @@ -618,7 +621,15 @@ public class RefDirectory extends RefDatabase { out = new FileOutputStream(log, true); } try { - out.write(rec); + if (wc.getFSyncRefFiles()) { + FileChannel fc = out.getChannel(); + ByteBuffer buf = ByteBuffer.wrap(rec); + while (0 < buf.remaining()) + fc.write(buf); + fc.force(true); + } else { + out.write(rec); + } } finally { out.close(); } @@ -757,6 +768,7 @@ public class RefDirectory extends RefDatabase { @Override protected void writeFile(String name, byte[] content) throws IOException { + lck.setFSync(true); lck.setNeedStatInformation(true); try { lck.write(content); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectoryUpdate.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectoryUpdate.java index a9f054837..109960df2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectoryUpdate.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectoryUpdate.java @@ -99,6 +99,10 @@ class RefDirectoryUpdate extends RefUpdate { @Override protected Result doUpdate(final Result status) throws IOException { + WriteConfig wc = database.getRepository().getConfig() + .get(WriteConfig.KEY); + + lock.setFSync(wc.getFSyncRefFiles()); lock.setNeedStatInformation(true); lock.write(getNewObjectId()); @@ -143,6 +147,10 @@ class RefDirectoryUpdate extends RefUpdate { @Override protected Result doLink(final String target) throws IOException { + WriteConfig wc = database.getRepository().getConfig() + .get(WriteConfig.KEY); + + lock.setFSync(wc.getFSyncRefFiles()); lock.setNeedStatInformation(true); lock.write(encode(RefDirectory.SYMREF + target + '\n')); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java index 1f28d8b75..fd467a555 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WriteConfig.java @@ -59,9 +59,12 @@ class WriteConfig { private final boolean fsyncObjectFiles; + private final boolean fsyncRefFiles; + private WriteConfig(final Config rc) { compression = rc.get(CoreConfig.KEY).getCompression(); fsyncObjectFiles = rc.getBoolean("core", "fsyncobjectfiles", false); + fsyncRefFiles = rc.getBoolean("core", "fsyncreffiles", false); } int getCompression() { @@ -71,4 +74,8 @@ class WriteConfig { boolean getFSyncObjectFiles() { return fsyncObjectFiles; } + + boolean getFSyncRefFiles() { + return fsyncRefFiles; + } } From ed5fe8af9ae6a4513f886aed004214f84924eae6 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 10 Nov 2010 17:28:14 -0800 Subject: [PATCH 4/5] Remove unnecessary region locking from LockFile The lock file protocol relies on the atomic creation of a standardized name in the parent directory of the file being updated. Since the creation is atomic, at most one thread in any process can succeed on this creation, and all others will fail. While the lock file exists, that file is private to the thread that is writing it, and no others will attempt to read or modify the file. Consequently the use of the region level locks around the file are unnecessary, and may actually reduce performance when using NFS, SMB, or some other sort of remote filesystem that supports locking. Change-Id: Ice312b6fb4fdf9d36c734c3624c6d0537903913b Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/storage/file/LockFile.java | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java index a794ec330..fc464cefe 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java @@ -54,8 +54,6 @@ import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; -import java.nio.channels.FileLock; -import java.nio.channels.OverlappingFileLockException; import java.text.MessageFormat; import org.eclipse.jgit.JGitText; @@ -87,8 +85,6 @@ public class LockFile { private final File lck; - private FileLock fLck; - private boolean haveLck; private FileOutputStream os; @@ -131,23 +127,6 @@ public class LockFile { haveLck = true; try { os = new FileOutputStream(lck); - try { - fLck = os.getChannel().tryLock(); - if (fLck == null) - throw new OverlappingFileLockException(); - } catch (OverlappingFileLockException ofle) { - // We cannot use unlock() here as this file is not - // held by us, but we thought we created it. We must - // not delete it, as it belongs to some other process. - // - haveLck = false; - try { - os.close(); - } catch (IOException ioe) { - // Fail by returning haveLck = false. - } - os = null; - } } catch (IOException ioe) { unlock(); throw ioe; @@ -276,7 +255,6 @@ public class LockFile { } else { os.write(content); } - fLck.release(); os.close(); os = null; } catch (IOException ioe) { @@ -337,7 +315,6 @@ public class LockFile { out.flush(); if (fsync) os.getChannel().force(true); - fLck.release(); out.close(); os = null; } catch (IOException ioe) { @@ -472,14 +449,6 @@ public class LockFile { */ public void unlock() { if (os != null) { - if (fLck != null) { - try { - fLck.release(); - } catch (IOException ioe) { - // Huh? - } - fLck = null; - } try { os.close(); } catch (IOException ioe) { From 2f6e79307de23e1920fb525ac91e3974baa4fc9c Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 10 Nov 2010 17:30:43 -0800 Subject: [PATCH 5/5] Remove unnecessary flush calls from LockFile Change-Id: I144af9db4714acabd796880be73bd50d84b92efe Signed-off-by: Shawn O. Pearce --- .../src/org/eclipse/jgit/storage/file/LockFile.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java index fc464cefe..6199f4c45 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java @@ -304,15 +304,9 @@ public class LockFile { out.write(b); } - @Override - public void flush() throws IOException { - out.flush(); - } - @Override public void close() throws IOException { try { - out.flush(); if (fsync) os.getChannel().force(true); out.close();