From 9fded678ffcda12b980ab8f01914168de38a8c73 Mon Sep 17 00:00:00 2001 From: supratimdeka <46919641+supratimdeka@users.noreply.github.com> Date: Thu, 6 Jun 2019 18:53:37 +0530 Subject: [PATCH] HDDS-1621. writeData in ChunkUtils should not use AsynchronousFileChannel. Contributed by Supratim Deka (#917) --- .../keyvalue/helpers/ChunkUtils.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/helpers/ChunkUtils.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/helpers/ChunkUtils.java index 2781bfacca..2993bbb81a 100644 --- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/helpers/ChunkUtils.java +++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/helpers/ChunkUtils.java @@ -43,6 +43,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousFileChannel; +import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.file.StandardOpenOption; import java.security.NoSuchAlgorithmException; @@ -84,23 +85,20 @@ public static void writeData(File chunkFile, ChunkInfo chunkInfo, throw new StorageContainerException(err, INVALID_WRITE_SIZE); } - AsynchronousFileChannel file = null; + FileChannel file = null; FileLock lock = null; try { long writeTimeStart = Time.monotonicNow(); - file = sync ? - AsynchronousFileChannel.open(chunkFile.toPath(), - StandardOpenOption.CREATE, - StandardOpenOption.WRITE, - StandardOpenOption.SPARSE, - StandardOpenOption.SYNC) : - AsynchronousFileChannel.open(chunkFile.toPath(), + + // skip SYNC and DSYNC to reduce contention on file.lock + file = FileChannel.open(chunkFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, 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. volumeIOStats.incWriteTime(Time.monotonicNow() - writeTimeStart); volumeIOStats.incWriteOpCount(); @@ -128,6 +126,10 @@ public static void writeData(File chunkFile, ChunkInfo chunkInfo, } if (file != null) { try { + if (sync) { + // ensure data and metadata is persisted. Outside the lock + file.force(true); + } file.close(); } catch (IOException e) { throw new StorageContainerException("Error closing chunk file",