Author: mduerig
Date: Wed Feb 25 22:01:47 2015
New Revision: 1662315

URL: http://svn.apache.org/r1662315
Log:
OAK-2552: Implement MBean monitoring garbage collection
Introduce GCMonitor and track in FileStore

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/gc/
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/gc/GCMonitor.java
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java?rev=1662315&r1=1662314&r2=1662315&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
 Wed Feb 25 22:01:47 2015
@@ -55,11 +55,12 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.blob.datastore.SharedDataStoreUtils;
 import 
org.apache.jackrabbit.oak.plugins.blob.datastore.SharedDataStoreUtils.SharedStoreRecordType;
 import org.apache.jackrabbit.oak.plugins.identifier.ClusterRepositoryInfo;
-import 
org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategyMBean;
 import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy;
 import 
org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.CleanupType;
+import 
org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategyMBean;
 import 
org.apache.jackrabbit.oak.plugins.segment.compaction.DefaultCompactionStrategyMBean;
 import org.apache.jackrabbit.oak.plugins.segment.file.FileStore;
+import org.apache.jackrabbit.oak.plugins.segment.file.FileStore.Builder;
 import org.apache.jackrabbit.oak.spi.blob.BlobStore;
 import org.apache.jackrabbit.oak.spi.blob.GarbageCollectableBlobStore;
 import org.apache.jackrabbit.oak.spi.commit.Observable;
@@ -236,14 +237,17 @@ public class SegmentNodeStoreService ext
             }
         };
 
-        boolean memoryMapping = "64".equals(mode);
-        int cacheSize = Integer.parseInt(size);
+        OsgiWhiteboard whiteboard = new 
OsgiWhiteboard(context.getBundleContext());
+        Builder storeBuilder = FileStore.newFileStore(new File(directory))
+                .withCacheSize(Integer.parseInt(size))
+                .withMemoryMapping("64".equals(mode))
+                .withWhiteBoard(whiteboard);
         if (customBlobStore) {
             log.info("Initializing SegmentNodeStore with BlobStore [{}]", 
blobStore);
-            store = new FileStore(blobStore, new File(directory), cacheSize,
-                    memoryMapping).setCompactionStrategy(compactionStrategy);
+            store = storeBuilder.withBlobStore(blobStore).create()
+                    .setCompactionStrategy(compactionStrategy);
         } else {
-            store = new FileStore(new File(directory), cacheSize, 
memoryMapping)
+            store = storeBuilder.create()
                     .setCompactionStrategy(compactionStrategy);
         }
 
@@ -251,7 +255,6 @@ public class SegmentNodeStoreService ext
         observerTracker = new ObserverTracker(delegate);
         observerTracker.start(context.getBundleContext());
 
-        OsgiWhiteboard whiteboard = new 
OsgiWhiteboard(context.getBundleContext());
         executor = new WhiteboardExecutor();
         executor.start(whiteboard);
 

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=1662315&r1=1662314&r2=1662315&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
 Wed Feb 25 22:01:47 2015
@@ -70,8 +70,11 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.segment.SegmentWriter;
 import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy;
 import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.apache.jackrabbit.oak.spi.gc.GCMonitor;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.whiteboard.AbstractServiceTracker;
+import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -158,6 +161,7 @@ public class FileStore implements Segmen
      * Version of the segment storage format.
      */
     private final SegmentVersion version = SegmentVersion.V_11;
+    private final GCMonitorTracker gcMonitor = new GCMonitorTracker();
 
     /**
      * Create a new instance of a {@link Builder} for a file store.
@@ -179,6 +183,7 @@ public class FileStore implements Segmen
         private int maxFileSize = 256;
         private int cacheSize;   // 0 -> DEFAULT_MEMORY_CACHE_SIZE
         private boolean memoryMapping;
+        private Whiteboard whiteboard;
 
         private Builder(File directory) {
             this.directory = directory;
@@ -250,6 +255,17 @@ public class FileStore implements Segmen
         }
 
         /**
+         * {@link Whiteboard} used to track {@link GCMonitor} instances.
+         * @param whiteboard
+         * @return this instance
+         */
+        @Nonnull
+        public Builder withWhiteBoard(@Nonnull Whiteboard whiteboard) {
+            this.whiteboard = checkNotNull(whiteboard);
+            return this;
+        }
+
+        /**
          * Create a new {@link FileStore} instance with the settings specified 
in this
          * builder. If none of the {@code with} methods have been called 
before calling
          * this method, a file store with the following default settings is 
returned:
@@ -259,6 +275,7 @@ public class FileStore implements Segmen
          * <li>max file size: 256MB</li>
          * <li>cache size: 256MB</li>
          * <li>memory mapping: on for 64 bit JVMs off otherwise</li>
+         * <li>whiteboard: none. No {@link GCMonitor} tracking</li>
          * </ul>
          *
          * @return a new file store instance
@@ -267,14 +284,14 @@ public class FileStore implements Segmen
         @Nonnull
         public FileStore create() throws IOException {
             return new FileStore(
-                    blobStore, directory, root, maxFileSize, cacheSize, 
memoryMapping);
+                    blobStore, directory, root, maxFileSize, cacheSize, 
memoryMapping, whiteboard);
         }
     }
 
     @Deprecated
     public FileStore(BlobStore blobStore, File directory, int maxFileSizeMB, 
boolean memoryMapping)
             throws IOException {
-        this(blobStore, directory, EMPTY_NODE, maxFileSizeMB, 0, 
memoryMapping);
+        this(blobStore, directory, EMPTY_NODE, maxFileSizeMB, 0, 
memoryMapping, null);
     }
 
     @Deprecated
@@ -292,18 +309,24 @@ public class FileStore implements Segmen
     @Deprecated
     public FileStore(File directory, int maxFileSizeMB, int cacheSizeMB,
             boolean memoryMapping) throws IOException {
-        this(null, directory, EMPTY_NODE, maxFileSizeMB, cacheSizeMB, 
memoryMapping);
+        this(null, directory, EMPTY_NODE, maxFileSizeMB, cacheSizeMB, 
memoryMapping, null);
     }
 
     @Deprecated
     FileStore(File directory, NodeState initial, int maxFileSize) throws 
IOException {
-        this(null, directory, initial, maxFileSize, -1, 
MEMORY_MAPPING_DEFAULT);
+        this(null, directory, initial, maxFileSize, -1, 
MEMORY_MAPPING_DEFAULT, null);
     }
 
     @Deprecated
     public FileStore(
             BlobStore blobStore, final File directory, NodeState initial, int 
maxFileSizeMB,
-            int cacheSizeMB, boolean memoryMapping)
+            int cacheSizeMB, boolean memoryMapping) throws IOException {
+        this(blobStore, directory, initial, maxFileSizeMB, cacheSizeMB, 
memoryMapping, null);
+    }
+
+    private FileStore(
+            BlobStore blobStore, final File directory, NodeState initial, int 
maxFileSizeMB,
+            int cacheSizeMB, boolean memoryMapping, Whiteboard whiteboard)
             throws IOException {
         checkNotNull(directory).mkdirs();
         if (cacheSizeMB < 0) {
@@ -393,6 +416,9 @@ public class FileStore implements Segmen
                     }
                 });
 
+        if (whiteboard != null) {
+            gcMonitor.start(whiteboard);
+        }
         log.info("TarMK opened: {} (mmap={})", directory, memoryMapping);
     }
 
@@ -407,7 +433,7 @@ public class FileStore implements Segmen
         }
         long needed = delta * compactionStrategy.getMemoryThreshold();
         if (needed >= avail) {
-            log.info(
+            gcMonitor.skipped(
                     "Not enough available memory {}, needed {}, last merge 
delta {}, so skipping compaction for now",
                     humanReadableByteCount(avail),
                     humanReadableByteCount(needed),
@@ -425,7 +451,7 @@ public class FileStore implements Segmen
         CompactionGainEstimate estimate = estimateCompactionGain();
         long gain = estimate.estimateCompactionGain();
         if (gain >= 10) {
-            log.info(
+            gcMonitor.info(
                     "Estimated compaction in {}, gain is {}% ({}/{}) or 
({}/{}), so running compaction",
                     watch, gain, estimate.getReachableSize(),
                     estimate.getTotalSize(),
@@ -435,10 +461,10 @@ public class FileStore implements Segmen
                 compact();
                 compacted = true;
             } else {
-                log.info("TarMK compaction paused");
+                gcMonitor.skipped("TarMK compaction paused");
             }
         } else {
-            log.info(
+            gcMonitor.skipped(
                     "Estimated compaction in {}, gain is {}% ({}/{}) or 
({}/{}), so skipping compaction for now",
                     watch, gain, estimate.getReachableSize(),
                     estimate.getTotalSize(),
@@ -607,7 +633,7 @@ public class FileStore implements Segmen
     public synchronized void cleanup() throws IOException {
         Stopwatch watch = Stopwatch.createStarted();
         long initialSize = size();
-        log.info("TarMK revision cleanup started. Current repository size {}",
+        gcMonitor.info("TarMK revision cleanup started. Current repository 
size {}",
                 humanReadableByteCount(initialSize));
 
         // Suggest to the JVM that now would be a good time
@@ -633,13 +659,14 @@ public class FileStore implements Segmen
                     list.add(cleaned);
                 }
                 File file = reader.close();
-                log.info("TarMK revision cleanup reclaiming {}", 
file.getName());
+                gcMonitor.info("TarMK revision cleanup reclaiming {}", 
file.getName());
                 toBeRemoved.addLast(file);
             }
         }
         readers = list;
         long finalSize = size();
-        log.info("TarMK revision cleanup completed in {}. Post cleanup size is 
{} " +
+        gcMonitor.cleaned(initialSize - finalSize, finalSize);
+        gcMonitor.info("TarMK revision cleanup completed in {}. Post cleanup 
size is {} " +
                 "and space reclaimed {}", watch,
                 humanReadableByteCount(finalSize),
                 humanReadableByteCount(initialSize - finalSize));
@@ -653,7 +680,7 @@ public class FileStore implements Segmen
     public void compact() {
         checkArgument(!compactionStrategy.equals(NO_COMPACTION),
                 "You must set a compactionStrategy before calling compact");
-        log.info("TarMK compaction running, strategy={}", compactionStrategy);
+        gcMonitor.info("TarMK compaction running, strategy={}", 
compactionStrategy);
 
         long start = System.currentTimeMillis();
         SegmentWriter writer = new SegmentWriter(this, tracker, getVersion());
@@ -662,7 +689,7 @@ public class FileStore implements Segmen
         long existing = before.getChildNode(SegmentNodeStore.CHECKPOINTS)
                 .getChildNodeCount(Long.MAX_VALUE);
         if (existing > 1) {
-            log.warn(
+            gcMonitor.warn(
                     "TarMK compaction found {} checkpoints, you might need to 
run checkpoint cleanup",
                     existing);
         }
@@ -679,10 +706,10 @@ public class FileStore implements Segmen
                 after = compactor.compact(after, head);
                 setHead = new SetHead(head, after, compactor);
             }
-            log.info("TarMK compaction completed in {}ms",
+            gcMonitor.info("TarMK compaction completed in {}ms",
                     System.currentTimeMillis() - start);
         } catch (Exception e) {
-            log.error("Error while running TarMK compaction", e);
+            gcMonitor.error("Error while running TarMK compaction", e);
         }
     }
 
@@ -726,6 +753,7 @@ public class FileStore implements Segmen
         // threads before acquiring the synchronization lock
         compactionThread.close();
         flushThread.close();
+        gcMonitor.stop();
 
         synchronized (this) {
             try {
@@ -998,6 +1026,7 @@ public class FileStore implements Segmen
                 // content. TODO: There should be a cleaner way to do this.
                 tracker.getWriter().dropCache();
                 tracker.getWriter().flush();
+                gcMonitor.compacted();
                 tracker.clearSegmentIdTables(compactionStrategy);
                 return true;
             } else {
@@ -1009,4 +1038,49 @@ public class FileStore implements Segmen
     public SegmentVersion getVersion() {
         return version;
     }
+
+    private static class GCMonitorTracker extends 
AbstractServiceTracker<GCMonitor> {
+        public GCMonitorTracker() {
+            super(GCMonitor.class);
+        }
+
+        void info(String message, Object... arguments) {
+            log.info(message, arguments);
+            for (GCMonitor gcMonitor : getServices()) {
+                gcMonitor.info(message, arguments);
+            }
+        }
+
+        void warn(String message, Object... arguments) {
+            log.warn(message, arguments);
+            for (GCMonitor gcMonitor : getServices()) {
+                gcMonitor.warn(message, arguments);
+            }
+        }
+
+        void error(String message, Exception e) {
+            for (GCMonitor gcMonitor : getServices()) {
+                gcMonitor.error(message, e);
+            }
+        }
+
+        void skipped(String message, Object... arguments) {
+            log.info(message, arguments);
+            for (GCMonitor gcMonitor : getServices()) {
+                gcMonitor.skipped(message, arguments);
+            }
+        }
+
+        void compacted() {
+            for (GCMonitor gcMonitor : getServices()) {
+                gcMonitor.compacted();
+            }
+        }
+
+        void cleaned(long reclaimedSize, long currentSize) {
+            for (GCMonitor gcMonitor : getServices()) {
+                gcMonitor.cleaned(reclaimedSize, currentSize);
+            }
+        }
+    }
 }

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/gc/GCMonitor.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/gc/GCMonitor.java?rev=1662315&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/gc/GCMonitor.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/gc/GCMonitor.java
 Wed Feb 25 22:01:47 2015
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.spi.gc;
+
+import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
+
+/**
+ * {@code GCMonitor} instance are used to monitor garbage collection.
+ * Instances of {@code GCMonitor} are registered to the {@link Whiteboard}
+ * to receive notifications regarding garbage collection.
+ */
+public interface GCMonitor {
+
+    /**
+     * Log a message at the INFO level according to the specified format
+     * and arguments.
+     * <p/>
+     * <p>This form avoids superfluous string concatenation when the logger
+     * is disabled for the INFO level. However, this variant incurs the hidden
+     * (and relatively small) cost of creating an <code>Object[]</code> before 
invoking the method,
+     * even if this logger is disabled for INFO. The variants taking
+     * {@link #info(String, Object) one} and {@link #info(String, Object, 
Object) two}
+     * arguments exist solely in order to avoid this hidden cost.</p>
+     *
+     * @param format    the format string
+     * @param arguments a list of 3 or more arguments
+     */
+
+    /**
+     * Informal notification on the progress of garbage collection.
+     * @param message  The message with {} place holders for the {@code 
arguments}
+     * @param arguments
+     */
+    void info(String message, Object[] arguments);
+
+    /**
+     * Warning about a condition that might have advert effects on the overall
+     * garbage collection process but does not prevent the process from 
running.
+     * @param message  The message with {} place holders for the {@code 
arguments}
+     * @param arguments
+     */
+    void warn(String message, Object[] arguments);
+
+    /**
+     * An error caused the garbage collection process to terminate prematurely.
+     * @param message
+     * @param exception
+     */
+    void error(String message, Exception exception);
+
+    /**
+     * A garbage collection cycle is skipped for a specific {@code reason}.
+     * @param reason  The reason with {} place holders for the {@code 
arguments}
+     * @param arguments
+     */
+    void skipped(String reason, Object[] arguments);
+
+    /**
+     * The compaction phase of the garbage collection process terminated 
successfully.
+     */
+    void compacted();
+
+    /**
+     * The cleanup phase of the garbage collection process terminated 
successfully.
+     * @param reclaimedSize  number of bytes reclaimed
+     * @param currentSize    number of bytes after garbage collection
+     */
+    void cleaned(long reclaimedSize, long currentSize);
+}


Reply via email to