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 <spearce@spearce.org>
This commit is contained in:
parent
24fccadeda
commit
e0e7fe531d
|
@ -51,6 +51,9 @@
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
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.FileLock;
|
||||||
import java.nio.channels.OverlappingFileLockException;
|
import java.nio.channels.OverlappingFileLockException;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
@ -92,6 +95,8 @@ public boolean accept(File dir, String name) {
|
||||||
|
|
||||||
private boolean needStatInformation;
|
private boolean needStatInformation;
|
||||||
|
|
||||||
|
private boolean fsync;
|
||||||
|
|
||||||
private long commitLastModified;
|
private long commitLastModified;
|
||||||
|
|
||||||
private final FS fs;
|
private final FS fs;
|
||||||
|
@ -191,10 +196,21 @@ public void copyCurrentContent() throws IOException {
|
||||||
try {
|
try {
|
||||||
final FileInputStream fis = new FileInputStream(ref);
|
final FileInputStream fis = new FileInputStream(ref);
|
||||||
try {
|
try {
|
||||||
final byte[] buf = new byte[2048];
|
if (fsync) {
|
||||||
int r;
|
FileChannel in = fis.getChannel();
|
||||||
while ((r = fis.read(buf)) >= 0)
|
long pos = 0;
|
||||||
os.write(buf, 0, r);
|
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 {
|
} finally {
|
||||||
fis.close();
|
fis.close();
|
||||||
}
|
}
|
||||||
|
@ -251,8 +267,15 @@ public void write(final ObjectId id) throws IOException {
|
||||||
public void write(final byte[] content) throws IOException {
|
public void write(final byte[] content) throws IOException {
|
||||||
requireLock();
|
requireLock();
|
||||||
try {
|
try {
|
||||||
os.write(content);
|
if (fsync) {
|
||||||
os.flush();
|
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();
|
fLck.release();
|
||||||
os.close();
|
os.close();
|
||||||
os = null;
|
os = null;
|
||||||
|
@ -279,34 +302,43 @@ public void write(final byte[] content) throws IOException {
|
||||||
*/
|
*/
|
||||||
public OutputStream getOutputStream() {
|
public OutputStream getOutputStream() {
|
||||||
requireLock();
|
requireLock();
|
||||||
|
|
||||||
|
final OutputStream out;
|
||||||
|
if (fsync)
|
||||||
|
out = Channels.newOutputStream(os.getChannel());
|
||||||
|
else
|
||||||
|
out = os;
|
||||||
|
|
||||||
return new OutputStream() {
|
return new OutputStream() {
|
||||||
@Override
|
@Override
|
||||||
public void write(final byte[] b, final int o, final int n)
|
public void write(final byte[] b, final int o, final int n)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
os.write(b, o, n);
|
out.write(b, o, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(final byte[] b) throws IOException {
|
public void write(final byte[] b) throws IOException {
|
||||||
os.write(b);
|
out.write(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(final int b) throws IOException {
|
public void write(final int b) throws IOException {
|
||||||
os.write(b);
|
out.write(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
os.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
try {
|
try {
|
||||||
os.flush();
|
out.flush();
|
||||||
|
if (fsync)
|
||||||
|
os.getChannel().force(true);
|
||||||
fLck.release();
|
fLck.release();
|
||||||
os.close();
|
out.close();
|
||||||
os = null;
|
os = null;
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
unlock();
|
unlock();
|
||||||
|
@ -339,6 +371,16 @@ public void setNeedStatInformation(final boolean on) {
|
||||||
needStatInformation = on;
|
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.
|
* Wait until the lock file information differs from the old file.
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -67,6 +67,8 @@
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -606,6 +608,7 @@ else if (log.isFile())
|
||||||
write = false;
|
write = false;
|
||||||
|
|
||||||
if (write) {
|
if (write) {
|
||||||
|
WriteConfig wc = getRepository().getConfig().get(WriteConfig.KEY);
|
||||||
FileOutputStream out;
|
FileOutputStream out;
|
||||||
try {
|
try {
|
||||||
out = new FileOutputStream(log, true);
|
out = new FileOutputStream(log, true);
|
||||||
|
@ -618,7 +621,15 @@ else if (log.isFile())
|
||||||
out = new FileOutputStream(log, true);
|
out = new FileOutputStream(log, true);
|
||||||
}
|
}
|
||||||
try {
|
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 {
|
} finally {
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
@ -757,6 +768,7 @@ private void commitPackedRefs(final LockFile lck, final RefList<Ref> refs,
|
||||||
@Override
|
@Override
|
||||||
protected void writeFile(String name, byte[] content)
|
protected void writeFile(String name, byte[] content)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
lck.setFSync(true);
|
||||||
lck.setNeedStatInformation(true);
|
lck.setNeedStatInformation(true);
|
||||||
try {
|
try {
|
||||||
lck.write(content);
|
lck.write(content);
|
||||||
|
|
|
@ -99,6 +99,10 @@ protected void unlock() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Result doUpdate(final Result status) throws IOException {
|
protected Result doUpdate(final Result status) throws IOException {
|
||||||
|
WriteConfig wc = database.getRepository().getConfig()
|
||||||
|
.get(WriteConfig.KEY);
|
||||||
|
|
||||||
|
lock.setFSync(wc.getFSyncRefFiles());
|
||||||
lock.setNeedStatInformation(true);
|
lock.setNeedStatInformation(true);
|
||||||
lock.write(getNewObjectId());
|
lock.write(getNewObjectId());
|
||||||
|
|
||||||
|
@ -143,6 +147,10 @@ protected Result doDelete(final Result status) throws IOException {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Result doLink(final String target) throws IOException {
|
protected Result doLink(final String target) throws IOException {
|
||||||
|
WriteConfig wc = database.getRepository().getConfig()
|
||||||
|
.get(WriteConfig.KEY);
|
||||||
|
|
||||||
|
lock.setFSync(wc.getFSyncRefFiles());
|
||||||
lock.setNeedStatInformation(true);
|
lock.setNeedStatInformation(true);
|
||||||
lock.write(encode(RefDirectory.SYMREF + target + '\n'));
|
lock.write(encode(RefDirectory.SYMREF + target + '\n'));
|
||||||
|
|
||||||
|
|
|
@ -59,9 +59,12 @@ public WriteConfig parse(final Config cfg) {
|
||||||
|
|
||||||
private final boolean fsyncObjectFiles;
|
private final boolean fsyncObjectFiles;
|
||||||
|
|
||||||
|
private final boolean fsyncRefFiles;
|
||||||
|
|
||||||
private WriteConfig(final Config rc) {
|
private WriteConfig(final Config rc) {
|
||||||
compression = rc.get(CoreConfig.KEY).getCompression();
|
compression = rc.get(CoreConfig.KEY).getCompression();
|
||||||
fsyncObjectFiles = rc.getBoolean("core", "fsyncobjectfiles", false);
|
fsyncObjectFiles = rc.getBoolean("core", "fsyncobjectfiles", false);
|
||||||
|
fsyncRefFiles = rc.getBoolean("core", "fsyncreffiles", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getCompression() {
|
int getCompression() {
|
||||||
|
@ -71,4 +74,8 @@ int getCompression() {
|
||||||
boolean getFSyncObjectFiles() {
|
boolean getFSyncObjectFiles() {
|
||||||
return fsyncObjectFiles;
|
return fsyncObjectFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean getFSyncRefFiles() {
|
||||||
|
return fsyncRefFiles;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue