jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1547200 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment: AbstractStore.java file/FileStore.java file/TarFile.java memory/MemoryStore.java mongo/MongoStore.java
Date Mon, 02 Dec 2013 21:36:09 GMT
Author: jukka
Date: Mon Dec  2 21:36:09 2013
New Revision: 1547200

URL: http://svn.apache.org/r1547200
Log:
OAK-593: Segment-based MK

Avoid caching bulk segments in memory

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarFile.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java?rev=1547200&r1=1547199&r2=1547200&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/AbstractStore.java
Mon Dec  2 21:36:09 2013
@@ -17,11 +17,15 @@
 package org.apache.jackrabbit.oak.plugins.segment;
 
 import static com.google.common.collect.Sets.newHashSet;
+import static org.apache.jackrabbit.oak.plugins.segment.SegmentIdFactory.isBulkSegmentId;
+import static org.apache.jackrabbit.oak.plugins.segment.SegmentIdFactory.isDataSegmentId;
 
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.Callable;
 
+import javax.annotation.Nonnull;
+
 import org.apache.jackrabbit.oak.cache.CacheLIRS;
 
 import com.google.common.cache.Cache;
@@ -53,7 +57,8 @@ public abstract class AbstractStore impl
         }
     }
 
-    protected abstract Segment loadSegment(UUID id) throws Exception;
+    @Nonnull
+    protected abstract Segment loadSegment(UUID id);
 
     @Override
     public SegmentWriter getWriter() {
@@ -62,30 +67,24 @@ public abstract class AbstractStore impl
 
     @Override
     public Segment readSegment(UUID id) {
-        Segment segment = null;
-        if (segments != null) {
-            segment = segments.getIfPresent(id);
-            if (segment != null) {
-                return segment;
-            }
+        if (isBulkSegmentId(id)) {
+            return loadSegment(id);
         }
 
-        segment = getWriter().getCurrentSegment(id);
+        Segment segment = getWriter().getCurrentSegment(id);
         if (segment != null) {
             return segment;
         }
 
         if (segments == null) {
-            // no caching, just load the segment directly
-            try {
-                return loadSegment(id);
-            } catch (Exception e) {
-                throw new IllegalStateException("Failed to load segment " + id, e);
-            }
+            // no in-memory cache, load the segment directly
+            return loadSegment(id);
         }
 
         synchronized (this) {
+            // check if the segment is already cached
             segment = segments.getIfPresent(id);
+            // ... or currently being loaded
             while (segment == null && currentlyLoading.contains(id)) {
                 try {
                     wait(); // for another thread to load the segment
@@ -95,29 +94,23 @@ public abstract class AbstractStore impl
                 segment = segments.getIfPresent(id);
             }
             if (segment != null) {
+                // found the segment in the cache
                 return segment;
             }
+            // not yet cached, so start let others know that we're loading it
             currentlyLoading.add(id);
         }
 
         try {
             segment = loadSegment(id);
-            if (segment == null) {
-                throw new IllegalStateException("Unable to find segment " + id);
-            }
-        } catch (Exception e) {
-            throw new IllegalStateException("Failed to load segment " + id, e);
+            segments.put(id, segment);
+            return segment;
         } finally {
             synchronized (this) {
-                if (segment != null) {
-                    segments.put(id, segment);
-                }
                 currentlyLoading.remove(id);
                 notifyAll();
             }
         }
-
-        return segment;
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java?rev=1547200&r1=1547199&r2=1547200&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
Mon Dec  2 21:36:09 2013
@@ -33,6 +33,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.annotation.Nonnull;
+
 import org.apache.jackrabbit.oak.plugins.segment.AbstractStore;
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
 import org.apache.jackrabbit.oak.plugins.segment.RecordId;
@@ -161,20 +163,32 @@ public class FileStore extends AbstractS
         return journal;
     }
 
-    @Override
-    protected Segment loadSegment(UUID id) throws Exception {
+    @Override @Nonnull
+    protected Segment loadSegment(UUID id) {
         for (TarFile file : dataFiles) {
-            ByteBuffer buffer = file.readEntry(id);
-            if (buffer != null) {
-                return new Segment(FileStore.this, id, buffer);
+            try {
+                ByteBuffer buffer = file.readEntry(id);
+                if (buffer != null) {
+                    return new Segment(FileStore.this, id, buffer);
+                }
+            } catch (IOException e) {
+                throw new RuntimeException(
+                        "Failed to access data file " + file, e);
             }
         }
+
         for (TarFile file : bulkFiles) {
-            ByteBuffer buffer = file.readEntry(id);
-            if (buffer != null) {
-                return new Segment(FileStore.this, id, buffer);
+            try {
+                ByteBuffer buffer = file.readEntry(id);
+                if (buffer != null) {
+                    return new Segment(FileStore.this, id, buffer);
+                }
+            } catch (IOException e) {
+                throw new RuntimeException(
+                        "Failed to access bulk file " + file, e);
             }
         }
+
         throw new IllegalStateException("Segment " + id + " not found");
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarFile.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarFile.java?rev=1547200&r1=1547199&r2=1547200&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarFile.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/TarFile.java
Mon Dec  2 21:36:09 2013
@@ -47,7 +47,9 @@ class TarFile {
 
     }
 
-    private final FileAccess file;
+    private final File file;
+
+    private final FileAccess access;
 
     private int position = 0;
 
@@ -61,16 +63,17 @@ class TarFile {
         checkState(len <= Integer.MAX_VALUE);
         this.maxFileSize = Math.max((int) len, maxFileSize);
 
+        this.file = file;
         if (memoryMapping) {
-            this.file = new MappedAccess(file, this.maxFileSize);
+            this.access = new MappedAccess(file, this.maxFileSize);
         } else {
-            this.file = new RandomAccess(file);
+            this.access = new RandomAccess(file);
         }
 
         this.position = 0;
         while (position + BLOCK_SIZE <= len) {
             // read the tar header block
-            ByteBuffer buffer = this.file.read(position, BLOCK_SIZE);
+            ByteBuffer buffer = this.access.read(position, BLOCK_SIZE);
             String name = readString(buffer, 100);
             buffer.position(124);
             int size = readNumber(buffer, 12);
@@ -98,7 +101,7 @@ class TarFile {
     ByteBuffer readEntry(UUID id) throws IOException {
         Location location = entries.get(id);
         if (location != null) {
-            return file.read(location.offset, location.size);
+            return access.read(location.offset, location.size);
         } else {
             return null;
         }
@@ -160,16 +163,16 @@ class TarFile {
                 header, 148, 6);
         header[154] = 0;
 
-        file.write(position, header, 0, BLOCK_SIZE);
+        access.write(position, header, 0, BLOCK_SIZE);
         position += BLOCK_SIZE;
 
-        file.write(position, b, offset, size);
+        access.write(position, b, offset, size);
         entries.put(id, new Location(position, size));
         position += size;
 
         int padding = BLOCK_SIZE - position % BLOCK_SIZE;
         if (padding < BLOCK_SIZE) {
-            file.write(position, ZERO_BYTES, 0, padding);
+            access.write(position, ZERO_BYTES, 0, padding);
             position += padding;
         }
 
@@ -177,8 +180,8 @@ class TarFile {
     }
 
     void close() throws IOException {
-        file.flush();
-        file.close();
+        access.flush();
+        access.close();
     }
 
     private static String readString(ByteBuffer buffer, int fieldSize) {
@@ -206,4 +209,11 @@ class TarFile {
         return number;
     }
 
+    //------------------------------------------------------------< Object >--
+
+    @Override
+    public String toString() {
+        return file.toString();
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java?rev=1547200&r1=1547199&r2=1547200&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
Mon Dec  2 21:36:09 2013
@@ -23,6 +23,8 @@ import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentMap;
 
+import javax.annotation.Nonnull;
+
 import org.apache.jackrabbit.oak.plugins.segment.AbstractStore;
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
 import org.apache.jackrabbit.oak.plugins.segment.Segment;
@@ -63,7 +65,7 @@ public class MemoryStore extends Abstrac
         return journal;
     }
 
-    @Override
+    @Override @Nonnull
     protected Segment loadSegment(UUID id) {
         Segment segment = segments.get(id);
         if (segment != null) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java?rev=1547200&r1=1547199&r2=1547200&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
Mon Dec  2 21:36:09 2013
@@ -26,6 +26,8 @@ import java.nio.ByteBuffer;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.annotation.Nonnull;
+
 import org.apache.jackrabbit.oak.plugins.segment.AbstractStore;
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
 import org.apache.jackrabbit.oak.plugins.segment.Segment;
@@ -89,7 +91,7 @@ public class MongoStore extends Abstract
         segments.insert(segment, concern);
     }
 
-    @Override
+    @Override @Nonnull
     protected Segment loadSegment(UUID segmentId) {
         DBObject id = new BasicDBObject("_id", segmentId.toString());
         DBObject fields = new BasicDBObject(of("data", 1));



Mime
View raw message