Author: stefanegli
Date: Mon Sep 14 14:44:53 2015
New Revision: 1702963

URL: http://svn.apache.org/r1702963
Log:
OAK-3400 : avoid using org.osgi in ClusterNodeInfo by refactoring the lease 
failure handling into an explicit, separate interface that can be set with the 
DocumentMK.Builder and will be passed on by DocumentNodeStore to 
ClusterNodeInfo. If ClusterNodeInfo subsequently has a LeaseFailureHandler upon 
hitting a lease failure, it will use it, besides throwing exceptions - 
otherwise it will only throw exceptions

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/LeaseFailureHandler.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ClusterNodeInfo.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ClusterNodeInfo.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ClusterNodeInfo.java?rev=1702963&r1=1702962&r2=1702963&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ClusterNodeInfo.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ClusterNodeInfo.java
 Mon Sep 14 14:44:53 2015
@@ -30,9 +30,6 @@ import java.util.UUID;
 import org.apache.jackrabbit.oak.commons.StringUtils;
 import org.apache.jackrabbit.oak.stats.Clock;
 import org.apache.jackrabbit.oak.util.OakVersion;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
-import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -271,8 +268,8 @@ public class ClusterNodeInfo {
      */
     private boolean newEntry;
 
-    /** OAK-3397 : this context is used to stop the oak-core bundle in case of 
lease failure **/
-    private ComponentContext cc;
+    /** OAK-3397 / OAK-3400 : the LeaseFailureHandler is the one that actually 
stops the oak-core bundle (or does something else if necessary) **/
+    private LeaseFailureHandler leaseFailureHandler;
 
     private ClusterNodeInfo(int id, DocumentStore store, String machineId, 
String instanceId, ClusterNodeState state,
             RecoverLockState revRecoveryLock, Long leaseEnd, boolean newEntry) 
{
@@ -431,7 +428,7 @@ public class ClusterNodeInfo {
     }
 
     public void performLeaseCheck() {
-        if (leaseCheckDisabled || !renewed || (cc==null)) {
+        if (leaseCheckDisabled || !renewed) {
             // if leaseCheckDisabled is set we never do the check, so return 
fast
 
             // the 'renewed' flag indicates if this instance *ever* renewed 
the lease after startup
@@ -494,38 +491,24 @@ public class ClusterNodeInfo {
         LOG.error(restarterErrorMsg);
         
         // actual stopping should be done in a separate thread, so:
-        final Runnable r = new Runnable() {
-
-            @Override
-            public void run() {
-                handleLeaseCheckFailed();
-            }
-        };
-        final Thread th = new Thread(r, "FailedLeaseCheck-Thread");
-        th.setDaemon(true);
-        th.start();
+        if (leaseFailureHandler!=null) {
+            final Runnable r = new Runnable() {
+    
+                @Override
+                public void run() {
+                    if (leaseFailureHandler!=null) {
+                        leaseFailureHandler.handleLeaseFailure();
+                    }
+                }
+            };
+            final Thread th = new Thread(r, "LeaseFailureHandler-Thread");
+            th.setDaemon(true);
+            th.start();
+        }
 
         throw new AssertionError(restarterErrorMsg);
     }
 
-    private void handleLeaseCheckFailed() {
-        try {
-            // plan A: try stopping oak-core
-            LOG.error("handleLeaseCheckFailed: stopping oak-core...");
-            Bundle bundle = cc.getBundleContext().getBundle();
-            bundle.stop();
-            LOG.error("handleLeaseCheckFailed: stopped oak-core.");
-            // plan A worked, perfect!
-        } catch (BundleException e) {
-            LOG.error("handleLeaseCheckFailed: exception while stopping 
oak-core: "+e, e);
-            // plan B: stop only DocumentNodeStoreService (to stop the 
background threads)
-            LOG.error("handleLeaseCheckFailed: stopping 
DocumentNodeStoreService...");
-            cc.disableComponent(DocumentNodeStoreService.class.getName());
-            LOG.error("handleLeaseCheckFailed: stopped 
DocumentNodeStoreService");
-            // plan B succeeded.
-        }
-    }
-
     /**
      * Renew the cluster id lease. This method needs to be called once in a 
while,
      * to ensure the same cluster id is not re-used by a different instance.
@@ -577,10 +560,10 @@ public class ClusterNodeInfo {
         return leaseTime;
     }
 
-    public void setComponentContext(ComponentContext cc) {
-        this.cc = cc;
+    public void setLeaseFailureHandler(LeaseFailureHandler 
leaseFailureHandler) {
+        this.leaseFailureHandler = leaseFailureHandler;
     }
-
+    
     public void dispose() {
         synchronized(this) {
             if (leaseCheckFailed) {

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java?rev=1702963&r1=1702962&r2=1702963&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
 Mon Sep 14 14:44:53 2015
@@ -493,6 +493,7 @@ public class DocumentMK {
         private Executor executor;
         private String persistentCacheURI = DEFAULT_PERSISTENT_CACHE_URI;
         private PersistentCache persistentCache;
+        private LeaseFailureHandler leaseFailureHandler;
 
         public Builder() {
         }
@@ -614,6 +615,15 @@ public class DocumentMK {
             return leaseCheck;
         }
 
+        public Builder setLeaseFailureHandler(LeaseFailureHandler 
leaseFailureHandler) {
+            this.leaseFailureHandler = leaseFailureHandler;
+            return this;
+        }
+        
+        public LeaseFailureHandler getLeaseFailureHandler() {
+            return leaseFailureHandler;
+        }
+        
         /**
          * Set the document store to use. By default an in-memory store is 
used.
          *

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1702963&r1=1702962&r2=1702963&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
 Mon Sep 14 14:44:53 2015
@@ -439,6 +439,9 @@ public final class DocumentNodeStore
         }
         if (builder.getLeaseCheck()) {
             s = new LeaseCheckDocumentStoreWrapper(s, clusterNodeInfo);
+            if (clusterNodeInfo!=null) {
+                
clusterNodeInfo.setLeaseFailureHandler(builder.getLeaseFailureHandler());
+            }
         }
         this.store = s;
         this.clusterId = cid;

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java?rev=1702963&r1=1702962&r2=1702963&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java
 Mon Sep 14 14:44:53 2015
@@ -78,6 +78,8 @@ import org.apache.jackrabbit.oak.spi.whi
 import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
 import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardExecutor;
 import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
@@ -360,7 +362,28 @@ public class DocumentNodeStoreService {
                         diffCachePercentage).
                 setCacheSegmentCount(cacheSegmentCount).
                 setCacheStackMoveDistance(cacheStackMoveDistance).
-                setLeaseCheck(true /* OAK-2739: enabled by default */);
+                setLeaseCheck(true /* OAK-2739: enabled by default */).
+                setLeaseFailureHandler(new LeaseFailureHandler() {
+                    
+                    @Override
+                    public void handleLeaseFailure() {
+                        try {
+                            // plan A: try stopping oak-core
+                            log.error("handleLeaseFailure: stopping 
oak-core...");
+                            Bundle bundle = 
context.getBundleContext().getBundle();
+                            bundle.stop();
+                            log.error("handleLeaseFailure: stopped oak-core.");
+                            // plan A worked, perfect!
+                        } catch (BundleException e) {
+                            log.error("handleLeaseFailure: exception while 
stopping oak-core: "+e, e);
+                            // plan B: stop only DocumentNodeStoreService (to 
stop the background threads)
+                            log.error("handleLeaseFailure: stopping 
DocumentNodeStoreService...");
+                            
context.disableComponent(DocumentNodeStoreService.class.getName());
+                            log.error("handleLeaseFailure: stopped 
DocumentNodeStoreService");
+                            // plan B succeeded.
+                        }
+                    }
+                });
 
         if (persistentCache != null && persistentCache.length() > 0) {
             mkBuilder.setPersistentCache(persistentCache);
@@ -433,13 +456,6 @@ public class DocumentNodeStoreService {
 
         observerTracker.start(context.getBundleContext());
 
-        ClusterNodeInfo clusterInfo = mk.getNodeStore().getClusterInfo();
-        if (clusterInfo!=null) {
-            clusterInfo.setComponentContext(context);
-        } else {
-            log.warn("registerNodeStore: no ClusterNodeInfo available, 
lease-check will be disabled!");
-        }
-        
         DocumentStore ds = mk.getDocumentStore();
 
         // OAK-2682: time difference detection applied at startup with a 
default

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/LeaseFailureHandler.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/LeaseFailureHandler.java?rev=1702963&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/LeaseFailureHandler.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/LeaseFailureHandler.java
 Mon Sep 14 14:44:53 2015
@@ -0,0 +1,43 @@
+/*
+ * 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.plugins.document;
+
+/**
+ * A LeaseFailureHandler can be provided to the DocumentMK.Builder 
+ * and will be passed on to the ClusterNodeInfo for use upon
+ * lease failure.
+ * <p>
+ * When ClusterNodeInfo does not have such a LeaseFailureHandler, 
+ * the only thing it does is fail every subsequent access with
+ * an exception - but it doesn't do fancy things like stopping
+ * the oak-core bundle etc. Such an operation must be provided
+ * in a LeaseFailureHandler.
+ */
+public interface LeaseFailureHandler {
+
+    /**
+     * Invoked by ClusterNodeInfo when it detects a lease
+     * failure and has started preventing any further access
+     * to the DocumentStore by throwing exceptions - what's
+     * now left is any further actions that should be taken
+     * such as eg stopping the oak-core bundle. This part
+     * however is optional from the ClusterNodeInfo's pov
+     * and must be done by here in this LeaseFailureHandler.
+     */
+    public void handleLeaseFailure();
+    
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/LeaseFailureHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to