You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2015/10/13 16:17:33 UTC

svn commit: r1708403 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarWriter.java

Author: mduerig
Date: Tue Oct 13 14:17:33 2015
New Revision: 1708403

URL: http://svn.apache.org/viewvc?rev=1708403&view=rev
Log:
OAK-3330: FileStore lock contention with concurrent writers
Avoid fat lock when reading segment by reading from the file channel instead of the random access file

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarWriter.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarWriter.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarWriter.java?rev=1708403&r1=1708402&r2=1708403&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarWriter.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarWriter.java Tue Oct 13 14:17:33 2015
@@ -35,6 +35,7 @@ import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -123,6 +124,8 @@ class TarWriter implements Closeable {
      */
     private RandomAccessFile access = null;
 
+    private FileChannel channel = null;
+
     /**
      * Flag to indicate a closed writer. Accessing a closed writer is illegal.
      * Should only be accessed from synchronized code.
@@ -177,15 +180,18 @@ class TarWriter implements Closeable {
      * @param lsb the least significant bits of the segment id
      * @return the byte buffer, or null if not in this file
      */
-    synchronized ByteBuffer readEntry(long msb, long lsb) throws IOException {
+    ByteBuffer readEntry(long msb, long lsb) throws IOException {
         checkState(!closed);
-        TarEntry entry = index.get(new UUID(msb, lsb));
+        
+        TarEntry entry;
+        synchronized (this) {
+            entry = index.get(new UUID(msb, lsb));
+        }
         if (entry != null) {
-            checkState(access != null); // implied by entry != null
+            checkState(channel != null); // implied by entry != null
             ByteBuffer data = ByteBuffer.allocate(entry.size());
-            access.seek(entry.offset());
-            access.readFully(data.array());
-            access.seek(access.length());
+            channel.read(data, entry.offset());
+            data.rewind();
             return data;
         } else {
             return null;
@@ -214,6 +220,7 @@ class TarWriter implements Closeable {
         checkState(!closed);
         if (access == null) {
             access = new RandomAccessFile(file, "rw");
+            channel = access.getChannel();
         }
 
         access.write(header);