incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1557176 - in /sling/trunk/bundles/extensions/healthcheck/core/src: main/java/org/apache/sling/hc/core/impl/executor/ main/java/org/apache/sling/hc/jmx/impl/ test/java/org/apache/sling/hc/jmx/impl/
Date Fri, 10 Jan 2014 16:14:17 GMT
Author: cziegeler
Date: Fri Jan 10 16:14:16 2014
New Revision: 1557176

URL: http://svn.apache.org/r1557176
Log:
SLING-3278 : Provide a HealthCheckExecutor service. Use executor for jmx mbean execution

Added:
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
  (with props)
Modified:
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java
    sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanTest.java

Added: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java?rev=1557176&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
(added)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
Fri Jan 10 16:14:16 2014
@@ -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 SF 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.hc.core.impl.executor;
+
+import org.apache.sling.hc.api.execution.HealthCheckExecutionResult;
+import org.apache.sling.hc.api.execution.HealthCheckExecutor;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Internal service used by the JMX stuff
+ *
+ */
+public interface ExtendedHealthCheckExecutor extends HealthCheckExecutor {
+
+    HealthCheckExecutionResult execute(ServiceReference ref);
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java?rev=1557176&r1=1557175&r2=1557176&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
(original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
Fri Jan 10 16:14:16 2014
@@ -36,6 +36,7 @@ import org.apache.commons.lang.time.Stop
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
@@ -57,11 +58,11 @@ import org.slf4j.LoggerFactory;
  * Runs health checks for a given list of tags in parallel.
  *
  */
-@Service(value = HealthCheckExecutor.class)
+@Service(value = {HealthCheckExecutor.class, ExtendedHealthCheckExecutor.class})
 @Component(label = "Apache Sling Health Check Executor",
         description = "Runs health checks for a given list of tags in parallel.",
         metatype = true, immediate = true)
-public class HealthCheckExecutorImpl implements HealthCheckExecutor {
+public class HealthCheckExecutorImpl implements ExtendedHealthCheckExecutor {
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -106,10 +107,15 @@ public class HealthCheckExecutorImpl imp
     protected final void activate(final Map<String, Object> properties, final BundleContext
bundleContext) {
         this.bundleContext = bundleContext;
 
-        ModifiableThreadPoolConfig hcThreadPoolConfig = new ModifiableThreadPoolConfig();
+        final ModifiableThreadPoolConfig hcThreadPoolConfig = new ModifiableThreadPoolConfig();
         hcThreadPoolConfig.setMaxPoolSize(25);
         hcThreadPool = threadPoolManager.create(hcThreadPoolConfig, "Health Check Thread
Pool");
 
+        this.modified(properties);
+    }
+
+    @Modified
+    protected final void modified(final Map<String, Object> properties) {
         this.timeoutInMs = PropertiesUtil.toLong(properties.get(PROP_TIMEOUT_MS), TIMEOUT_DEFAULT_MS);
 
         if ( this.timeoutInMs <= 0L) {
@@ -145,32 +151,51 @@ public class HealthCheckExecutorImpl imp
         try {
             final ServiceReference[] healthCheckReferences = filter.getTaggedHealthCheckServiceReferences(tags);
 
-            final StopWatch stopWatch = new StopWatch();
-            stopWatch.start();
+            return this.execute(healthCheckReferences);
+        } finally {
+            filter.dispose();
+        }
+    }
 
-            final List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
-            final List<HealthCheckMetadata> healthCheckDescriptors = getHealthCheckDescriptors(healthCheckReferences);
+    /**
+     * @see org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor#execute(org.osgi.framework.ServiceReference)
+     */
+    @Override
+    public HealthCheckExecutionResult execute(final ServiceReference ref) {
+        final List<HealthCheckExecutionResult> result = this.execute(new ServiceReference[]
{ref});
+        if ( result.size() > 0 ) {
+            return result.get(0);
+        }
+        return null;
+    }
 
-            createResultsForDescriptors(healthCheckDescriptors, results);
+    /**
+     * Execute a set of health checks
+     */
+    private List<HealthCheckExecutionResult> execute(final ServiceReference[] healthCheckReferences)
{
+        final StopWatch stopWatch = new StopWatch();
+        stopWatch.start();
+
+        final List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
+        final List<HealthCheckMetadata> healthCheckDescriptors = getHealthCheckDescriptors(healthCheckReferences);
+
+        createResultsForDescriptors(healthCheckDescriptors, results);
+
+        stopWatch.stop();
+        if ( logger.isDebugEnabled() ) {
+            logger.debug("Time consumed for all checks: {}", msHumanReadable(stopWatch.getTime()));
+        }
+        // sort result
+        Collections.sort(results, new Comparator<HealthCheckExecutionResult>() {
 
-            stopWatch.stop();
-            if ( logger.isDebugEnabled() ) {
-                logger.debug("Time consumed for all checks: {}", msHumanReadable(stopWatch.getTime()));
+            @Override
+            public int compare(final HealthCheckExecutionResult arg0,
+                    final HealthCheckExecutionResult arg1) {
+                return ((ExecutionResult)arg0).compareTo((ExecutionResult)arg1);
             }
-            // sort result
-            Collections.sort(results, new Comparator<HealthCheckExecutionResult>()
{
 
-                @Override
-                public int compare(final HealthCheckExecutionResult arg0,
-                        final HealthCheckExecutionResult arg1) {
-                    return ((ExecutionResult)arg0).compareTo((ExecutionResult)arg1);
-                }
-
-            });
-            return results;
-        } finally {
-            filter.dispose();
-        }
+        });
+        return results;
     }
 
     private void createResultsForDescriptors(final List<HealthCheckMetadata> healthCheckDescriptors,

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java?rev=1557176&r1=1557175&r2=1557176&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java
(original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java
Fri Jan 10 16:14:16 2014
@@ -45,15 +45,13 @@ import javax.management.openmbean.Tabula
 import org.apache.sling.hc.api.HealthCheck;
 import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.api.ResultLog;
+import org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 
 /** A {@link DynamicMBean} used to execute a {@link HealthCheck} service */
 public class HealthCheckMBean implements DynamicMBean {
 
-    /** A HC result is cached for this time (ms) */
-    private static final long RESULT_TTL = 1500;
-
     public static final String HC_OK_ATTRIBUTE_NAME = "ok";
     public static final String HC_STATUS_ATTRIBUTE_NAME = "status";
     public static final String HC_LOG_ATTRIBUTE_NAME = "log";
@@ -69,7 +67,10 @@ public class HealthCheckMBean implements
     public static final String JMX_DOMAIN = "org.apache.sling.healthcheck";
 
     /** The health check service to call. */
-    private final HealthCheck healthCheck;
+    private final ServiceReference healthCheckRef;
+
+    /** The executor service. */
+    private final ExtendedHealthCheckExecutor executor;
 
     /** The mbean info. */
     private final MBeanInfo mbeanInfo;
@@ -77,10 +78,6 @@ public class HealthCheckMBean implements
     /** The default attributes. */
     private final Map<String, Object> defaultAttributes;
 
-    private long healthCheckInvocationTime;
-
-    private Result healthCheckResult;
-
     static {
         try {
             // Define the log row and table types
@@ -98,8 +95,9 @@ public class HealthCheckMBean implements
         }
     }
 
-    public HealthCheckMBean(final ServiceReference ref, final HealthCheck hc) {
-        this.healthCheck = hc;
+    public HealthCheckMBean(final ServiceReference ref, final ExtendedHealthCheckExecutor
executor) {
+        this.healthCheckRef = ref;
+        this.executor = executor;
         this.mbeanInfo = this.createMBeanInfo(ref);
         this.defaultAttributes = this.createDefaultAttributes(ref);
     }
@@ -246,16 +244,10 @@ public class HealthCheckMBean implements
 
     @Override
     public String toString() {
-        return "HealthCheckMBean [healthCheck=" + healthCheck + "]";
+        return "HealthCheckMBean [healthCheck=" + this.healthCheckRef + "]";
     }
 
     private Result getHealthCheckResult() {
-        synchronized ( this ) {
-            if ( this.healthCheckResult == null || this.healthCheckInvocationTime < System.currentTimeMillis()
) {
-                this.healthCheckResult = this.healthCheck.execute();
-                this.healthCheckInvocationTime = System.currentTimeMillis() + RESULT_TTL;
-            }
-            return this.healthCheckResult;
-        }
+        return this.executor.execute(this.healthCheckRef).getHealthCheckResult();
     }
 }
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java?rev=1557176&r1=1557175&r2=1557176&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java
(original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java
Fri Jan 10 16:14:16 2014
@@ -30,7 +30,9 @@ import javax.management.DynamicMBean;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
 import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
@@ -53,6 +55,9 @@ public class HealthCheckMBeanCreator {
 
     private ServiceTracker hcTracker;
 
+    @Reference
+    private ExtendedHealthCheckExecutor executor;
+
     @Activate
     protected void activate(final BundleContext btx) {
         this.hcTracker = new ServiceTracker(btx, HealthCheck.class.getName(), null) {
@@ -98,7 +103,7 @@ public class HealthCheckMBeanCreator {
      * @return The registered mbean or <code>null</code>
      */
     private synchronized Object registerHCMBean(final BundleContext bundleContext, final
ServiceReference reference) {
-        final Registration reg = Registration.getRegistration(bundleContext, reference);
+        final Registration reg = Registration.getRegistration(this.executor, reference);
         if ( reg != null ) {
             this.registeredServices.put(reference, reg);
 
@@ -151,15 +156,12 @@ public class HealthCheckMBeanCreator {
             this.mbean = mbean;
         }
 
-        public static Registration getRegistration(final BundleContext bundleContext, final
ServiceReference ref) {
+        public static Registration getRegistration(final ExtendedHealthCheckExecutor executor,
final ServiceReference ref) {
             final Object nameObj = ref.getProperty(HealthCheck.MBEAN_NAME);
             if ( nameObj != null ) {
-                final HealthCheck service = (HealthCheck) bundleContext.getService(ref);
-                if ( service != null ) {
-                    final HealthCheckMBean mbean = new HealthCheckMBean(ref, service);
+                final HealthCheckMBean mbean = new HealthCheckMBean(ref, executor);
 
-                    return new Registration(nameObj.toString().replace(',', '.'), mbean);
-                }
+                return new Registration(nameObj.toString().replace(',', '.'), mbean);
             }
             return null;
         }

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanTest.java?rev=1557176&r1=1557175&r2=1557176&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanTest.java
(original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanTest.java
Fri Jan 10 16:14:16 2014
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.fail;
 
 import java.lang.management.ManagementFactory;
+import java.util.Date;
+import java.util.List;
 
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
@@ -28,6 +30,9 @@ import javax.management.ObjectName;
 import org.apache.sling.hc.api.HealthCheck;
 import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.api.ResultLog;
+import org.apache.sling.hc.api.execution.HealthCheckExecutionResult;
+import org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor;
+import org.apache.sling.hc.util.HealthCheckMetadata;
 import org.apache.sling.hc.util.SimpleConstraintChecker;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
@@ -97,7 +102,44 @@ public class HealthCheckMBeanTest {
                 return 0;
             }
         };
-        final HealthCheckMBean mbean = new HealthCheckMBean(ref, testHealthCheck);
+        final HealthCheckMBean mbean = new HealthCheckMBean(ref, new ExtendedHealthCheckExecutor()
{
+
+            @Override
+            public List<HealthCheckExecutionResult> execute(String... tags) {
+                return null;
+            }
+
+            @Override
+            public HealthCheckExecutionResult execute(ServiceReference ref) {
+                // TODO Auto-generated method stub
+                return new HealthCheckExecutionResult() {
+
+                    @Override
+                    public Result getHealthCheckResult() {
+                        // TODO Auto-generated method stub
+                        return testHealthCheck.execute();
+                    }
+
+                    @Override
+                    public HealthCheckMetadata getHealthCheckMetadata() {
+                        // TODO Auto-generated method stub
+                        return null;
+                    }
+
+                    @Override
+                    public Date getFinishedAt() {
+                        // TODO Auto-generated method stub
+                        return null;
+                    }
+
+                    @Override
+                    public long getElapsedTimeInMs() {
+                        // TODO Auto-generated method stub
+                        return 0;
+                    }
+                };
+            }
+        });
         final ObjectName name = new ObjectName(OBJECT_NAME);
         jmxServer.registerMBean(mbean, name);
         try {



Mime
View raw message