HDDS-1621. writeData in ChunkUtils should not use AsynchronousFileChannel. Contributed by Supratim Deka (#917)

This commit is contained in:
supratimdeka 2019-06-06 18:53:37 +05:30 committed by bshashikant
parent 829848ba2e
commit 9fded678ff

View File

@ -43,6 +43,7 @@
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel; import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -84,23 +85,20 @@ public static void writeData(File chunkFile, ChunkInfo chunkInfo,
throw new StorageContainerException(err, INVALID_WRITE_SIZE); throw new StorageContainerException(err, INVALID_WRITE_SIZE);
} }
AsynchronousFileChannel file = null; FileChannel file = null;
FileLock lock = null; FileLock lock = null;
try { try {
long writeTimeStart = Time.monotonicNow(); long writeTimeStart = Time.monotonicNow();
file = sync ?
AsynchronousFileChannel.open(chunkFile.toPath(), // skip SYNC and DSYNC to reduce contention on file.lock
StandardOpenOption.CREATE, file = FileChannel.open(chunkFile.toPath(),
StandardOpenOption.WRITE,
StandardOpenOption.SPARSE,
StandardOpenOption.SYNC) :
AsynchronousFileChannel.open(chunkFile.toPath(),
StandardOpenOption.CREATE, StandardOpenOption.CREATE,
StandardOpenOption.WRITE, StandardOpenOption.WRITE,
StandardOpenOption.SPARSE); StandardOpenOption.SPARSE);
lock = file.lock().get();
int size = file.write(data, chunkInfo.getOffset()).get(); lock = file.lock();
int size = file.write(data, chunkInfo.getOffset());
// Increment volumeIO stats here. // Increment volumeIO stats here.
volumeIOStats.incWriteTime(Time.monotonicNow() - writeTimeStart); volumeIOStats.incWriteTime(Time.monotonicNow() - writeTimeStart);
volumeIOStats.incWriteOpCount(); volumeIOStats.incWriteOpCount();
@ -128,6 +126,10 @@ public static void writeData(File chunkFile, ChunkInfo chunkInfo,
} }
if (file != null) { if (file != null) {
try { try {
if (sync) {
// ensure data and metadata is persisted. Outside the lock
file.force(true);
}
file.close(); file.close();
} catch (IOException e) { } catch (IOException e) {
throw new StorageContainerException("Error closing chunk file", throw new StorageContainerException("Error closing chunk file",