You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by st...@apache.org on 2015/04/16 13:08:23 UTC

svn commit: r1674042 - in /sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl: DiscoveryServiceImpl.java DiscoveryServiceMBean.java DiscoveryServiceMBeanImpl.java common/heartbeat/HeartbeatHandler.java

Author: stefanegli
Date: Thu Apr 16 11:08:22 2015
New Revision: 1674042

URL: http://svn.apache.org/r1674042
Log:
SLING-4628 : add a new "org.apache.sling: DiscoveryServiceImpl (discovery)" MBean which allows to "startNewVoting"

Added:
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBean.java   (with props)
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBeanImpl.java   (with props)
Modified:
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceImpl.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceImpl.java?rev=1674042&r1=1674041&r2=1674042&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceImpl.java (original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceImpl.java Thu Apr 16 11:08:22 2015
@@ -24,8 +24,10 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
+import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -61,7 +63,9 @@ import org.apache.sling.discovery.impl.t
 import org.apache.sling.discovery.impl.topology.announcement.AnnouncementRegistry;
 import org.apache.sling.discovery.impl.topology.connector.ConnectorRegistry;
 import org.apache.sling.settings.SlingSettingsService;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -130,11 +134,34 @@ public class DiscoveryServiceImpl implem
     /** whether or not there is a delayed event sending pending **/
     private boolean delayedEventPending = false;
 
+    private ServiceRegistration mbeanRegistration;
+
+    protected void registerMBean(BundleContext bundleContext) {
+        if (this.mbeanRegistration!=null) {
+            try{
+                if ( this.mbeanRegistration != null ) {
+                    this.mbeanRegistration.unregister();
+                    this.mbeanRegistration = null;
+                }
+            } catch(Exception e) {
+                logger.error("registerMBean: Error on unregister: "+e, e);
+            }
+        }
+        try {
+            final Dictionary<String, String> mbeanProps = new Hashtable<String, String>();
+            mbeanProps.put("jmx.objectname", "org.apache.sling:type=discovery,name=DiscoveryServiceImpl");
+
+            final DiscoveryServiceMBeanImpl mbean = new DiscoveryServiceMBeanImpl(heartbeatHandler);
+            this.mbeanRegistration = bundleContext.registerService(DiscoveryServiceMBeanImpl.class.getName(), mbean, mbeanProps);
+        } catch (Throwable t) {
+            logger.warn("registerMBean: Unable to register DiscoveryServiceImpl MBean", t);
+        }
+    }
     /**
      * Activate this service
      */
     @Activate
-    protected void activate() {
+    protected void activate(final BundleContext bundleContext) {
         logger.debug("DiscoveryServiceImpl activating...");
 
         if (settingsService == null) {
@@ -195,6 +222,8 @@ public class DiscoveryServiceImpl implem
                 }
             }
         }
+        
+        registerMBean(bundleContext);        
 
         logger.debug("DiscoveryServiceImpl activated.");
     }
@@ -227,6 +256,14 @@ public class DiscoveryServiceImpl implem
         synchronized (lock) {
             activated = false;
         }
+        try{
+            if ( this.mbeanRegistration != null ) {
+                this.mbeanRegistration.unregister();
+                this.mbeanRegistration = null;
+            }
+        } catch(Exception e) {
+            logger.error("deactivate: Error on unregister: "+e, e);
+        }
     }
 
     /**

Added: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBean.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBean.java?rev=1674042&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBean.java (added)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBean.java Thu Apr 16 11:08:22 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.sling.discovery.impl;
+
+/**
+ * MBean of the DiscoveryServiceImpl
+ */
+public interface DiscoveryServiceMBean {
+
+    /**
+     * Starts a new voting (based on the current set of active instances in the cluster).
+     */
+    public void startNewVoting();
+
+}

Propchange: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBeanImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBeanImpl.java?rev=1674042&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBeanImpl.java (added)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBeanImpl.java Thu Apr 16 11:08:22 2015
@@ -0,0 +1,48 @@
+/*
+ * 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.sling.discovery.impl;
+
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+
+import org.apache.sling.discovery.impl.common.heartbeat.HeartbeatHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * MBean implementation for the DiscoveryServiceImpl
+ */
+public class DiscoveryServiceMBeanImpl extends StandardMBean implements DiscoveryServiceMBean {
+    
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private final HeartbeatHandler heartbeatHandler;
+
+    DiscoveryServiceMBeanImpl(HeartbeatHandler heartbeatHandler) throws NotCompliantMBeanException {
+        super(DiscoveryServiceMBean.class);
+        this.heartbeatHandler = heartbeatHandler;
+    }
+
+    public void startNewVoting() {
+        logger.info("startNewVoting: JMX-triggered starting a new voting with the HeartbeatHandler.");
+        heartbeatHandler.startNewVoting();
+        logger.info("startNewVoting: new voting was started.");
+    }
+
+}

Propchange: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/DiscoveryServiceMBeanImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java?rev=1674042&r1=1674041&r2=1674042&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java (original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java Thu Apr 16 11:08:22 2015
@@ -493,10 +493,46 @@ public class HeartbeatHandler implements
         // the currently live instances.
 
         // initiate a new voting
+        doStartNewVoting(resourceResolver, liveInstances);
+    }
+
+    private void doStartNewVoting(final ResourceResolver resourceResolver,
+            final Set<String> liveInstances) throws PersistenceException {
         String votingId = nextVotingId;
         nextVotingId = UUID.randomUUID().toString();
 
         VotingView.newVoting(resourceResolver, config, votingId, slingId, liveInstances);
     }
 
+    /**
+     * Management function to trigger the otherwise algorithm-dependent
+     * start of a new voting.
+     * This can make sense when explicitly trying to force a leader
+     * change (which is otherwise not allowed by the discovery API)
+     */
+    public void startNewVoting() {
+        logger.info("startNewVoting: explicitly starting new voting...");
+        ResourceResolver resourceResolver = null;
+        try {
+            resourceResolver = getResourceResolver();
+            final Resource clusterNodesRes = ResourceHelper.getOrCreateResource(
+                    resourceResolver, config.getClusterInstancesPath());
+            final Set<String> liveInstances = ViewHelper.determineLiveInstances(
+                    clusterNodesRes, config);
+            doStartNewVoting(resourceResolver, liveInstances);
+            logger.info("startNewVoting: explicit new voting was started.");
+        } catch (LoginException e) {
+            logger.error("startNewVoting: could not log in administratively: " + e,
+                    e);
+        } catch (PersistenceException e) {
+            logger.error(
+                    "startNewVoting: encountered a persistence exception during view check: "
+                            + e, e);
+        } finally {
+            if (resourceResolver != null) {
+                resourceResolver.close();
+            }
+        }
+    }
+
 }