You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2014/08/26 12:59:41 UTC
svn commit: r1620563 - in
/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm: ./
src/org/apache/felix/dm/ src/org/apache/felix/dm/impl/
Author: pderop
Date: Tue Aug 26 10:59:41 2014
New Revision: 1620563
URL: http://svn.apache.org/r1620563
Log:
Finalized support for parallel mode: the threadpool, as suggested by Xander, should not be provided by user activators.
As a proposed solution, the threadpool is now tracked from the OSGi service registry.
And when a component is added to a DependencyManager, then a threadpool is used if there is one available from the registry.
So, a management agent bundle can simply register a java.util.Executor threadpool and must be started first (using a start level service).
If start level service can't be used, then a "org.apache.felix.dependencymanager.parallel=true" system property can be configured
and in this case, DependencyManager will wait for a threadpool before activating any DM components (all components
will be cached until an available threadpool is registered in the service registry).
Added:
felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/Activator.java
felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/ComponentScheduler.java
Modified:
felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/bnd.bnd
felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/DependencyManager.java
felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/DispatchExecutor.java
Modified: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/bnd.bnd
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/bnd.bnd?rev=1620563&r1=1620562&r2=1620563&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/bnd.bnd (original)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/bnd.bnd Tue Aug 26 10:59:41 2014
@@ -11,3 +11,4 @@ Export-Package: \
org.apache.felix.dm.tracker,\
org.apache.felix.dm.context
Import-Package: !org.junit,!org.mockito.*,*
+Bundle-Activator: org.apache.felix.dm.impl.Activator
\ No newline at end of file
Modified: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/DependencyManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/DependencyManager.java?rev=1620563&r1=1620562&r2=1620563&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/DependencyManager.java (original)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/DependencyManager.java Tue Aug 26 10:59:41 2014
@@ -33,6 +33,7 @@ import org.apache.felix.dm.impl.AspectSe
import org.apache.felix.dm.impl.BundleAdapterImpl;
import org.apache.felix.dm.impl.BundleDependencyImpl;
import org.apache.felix.dm.impl.ComponentImpl;
+import org.apache.felix.dm.impl.ComponentScheduler;
import org.apache.felix.dm.impl.ConfigurationDependencyImpl;
import org.apache.felix.dm.impl.FactoryConfigurationAdapterImpl;
import org.apache.felix.dm.impl.Logger;
@@ -61,13 +62,24 @@ import org.osgi.framework.FrameworkUtil;
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
public class DependencyManager {
+ /**
+ * A management agent can register a threadpool in the registry using a "target" property with the following value
+ */
+ public static final String THREADPOOL = "org.apache.felix.dependencymanager";
+
+ /**
+ * The DependencyManager Activator will wait for a threadpool before creating any DM components if the following
+ * OSGi system property is set to true.
+ */
+ public final static String PARALLEL = "org.apache.felix.dependencymanager.parallel";
+
public static final String ASPECT = "org.apache.felix.dependencymanager.aspect";
public static final String SERVICEREGISTRY_CACHE_INDICES = "org.apache.felix.dependencymanager.filterindex";
public static final String METHOD_CACHE_SIZE = "org.apache.felix.dependencymanager.methodcache";
+
private final BundleContext m_context;
private final Logger m_logger;
private final List<Component> m_components = new CopyOnWriteArrayList<>();
- private volatile Executor m_threadPool;
// service registry cache
private static ServiceRegistryCache m_serviceRegistryCache;
@@ -125,15 +137,6 @@ public class DependencyManager {
}
/**
- * Sets a threadpool to this dependency manager. All added/removed components will then be handled
- * in parallel, using the provided threadpool.
- */
- public DependencyManager setThreadPool(Executor threadPool) {
- m_threadPool = threadPool;
- return this;
- }
-
- /**
* Returns the list of currently created dependency managers.
* @return the list of currently created dependency managers
*/
@@ -175,10 +178,9 @@ public class DependencyManager {
*/
public void add(Component c) {
m_components.add(c);
- if (m_threadPool != null) {
- ((ComponentContext) c).setThreadPool(m_threadPool);
- }
- ((ComponentContext) c).start();
+ // Adding the component is delegated to the ComponentScheduler, which will possibly use a threadpool
+ // in order to add the component in parallel.
+ ComponentScheduler.instance().add(c);
}
/**
@@ -187,9 +189,9 @@ public class DependencyManager {
*
* @param service the service to remove
*/
- public void remove(Component service) {
- ((ComponentContext) service).stop();
- m_components.remove(service);
+ public void remove(Component c) {
+ ComponentScheduler.instance().remove(c);
+ m_components.remove(c);
}
/**
Added: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/Activator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/Activator.java?rev=1620563&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/Activator.java (added)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/Activator.java Tue Aug 26 10:59:41 2014
@@ -0,0 +1,62 @@
+/*
+ * 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.felix.dm.impl;
+
+import java.util.concurrent.Executor;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+
+/**
+ * DependencyManager Activator. We are using this activator in order to use a threadpool, which can be
+ * optionally provided by any management agent bundle.
+ * The management agent can just register a <code>java.util.Executor</code> service in the osgi registry
+ * using the "target=org.apache.felix.dependencymanager" system property.
+ *
+ * There are two ways to ensure that all DM components are handled in parallel using the threadpool:
+ *
+ * 1- the management agent bundle can simply be started before any bundles, using the start-level service.
+ * 2- if the start-level service can't be used, then you can configure the org.apache.felix.dependendencymanager.parallel
+ * system property, in order to ask DM to wait for the threadpool, before creating any DM components.
+ *
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class Activator extends DependencyActivatorBase {
+ @Override
+ public void init(BundleContext context, DependencyManager manager) throws Exception {
+ boolean waitForThreadPool = Boolean.valueOf(context.getProperty(DependencyManager.PARALLEL));
+ Component c = createComponent().setImplementation(ComponentScheduler.instance());
+
+ if (waitForThreadPool) {
+ c.add(createTemporalServiceDependency(10000)
+ .setService(Executor.class, "(target=" + DependencyManager.THREADPOOL + ")")
+ .setRequired(true)
+ .setAutoConfig("m_threadPool"));
+ } else {
+ c.add(createServiceDependency()
+ .setService(Executor.class, "(target=" + DependencyManager.THREADPOOL + ")")
+ .setRequired(false)
+ .setAutoConfig("m_threadPool")
+ .setDefaultImplementation(ComponentScheduler.NullExecutor.class));
+ }
+ manager.add(c);
+ }
+}
Added: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/ComponentScheduler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/ComponentScheduler.java?rev=1620563&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/ComponentScheduler.java (added)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/ComponentScheduler.java Tue Aug 26 10:59:41 2014
@@ -0,0 +1,97 @@
+package org.apache.felix.dm.impl;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.Executor;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.context.ComponentContext;
+
+/**
+ * Components addition/removal made through the DependencyManagerAPI are delegated to this
+ * scheduler, which will use a threadpool optionally provided by any management bundle.
+ * The management bundle can register a threadpool (java.util.Executor) in the osgi registry
+ * using the "target=org.apache.felix.dependencymanager" service property.
+ *
+ * If the "org.apache.felix.dependencymanager.parallel" OSGi service property is set to true,
+ * then the scheduler will wait for a threadpool before creating any DM components (that is:
+ * added components will be cached until threadpool comes up from the OSGi service registry.
+ *
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class ComponentScheduler {
+ private final static ComponentScheduler m_instance = new ComponentScheduler();
+ private volatile Executor m_threadPool;
+ private final Executor m_serial = new SerialExecutor(null);
+ private boolean m_started;
+ private Set<Component> m_pending = new HashSet<>();
+ public final static class NullExecutor implements Executor {
+ @Override
+ public void execute(Runnable command) {
+ }
+ };
+
+ public static ComponentScheduler instance() {
+ return m_instance;
+ }
+
+ public void start() {
+ m_serial.execute(new Runnable() {
+ @Override
+ public void run() {
+ m_started = true;
+ for (Component c : m_pending) {
+ doAdd(c);
+ }
+ m_pending.clear();
+ }
+ });
+ }
+
+ public void add(final Component c) {
+ if (c.getComponentDeclaration().getName().equals(ComponentScheduler.class.getName())) {
+ ((ComponentContext) c).start();
+ } else {
+ m_serial.execute(new Runnable() {
+ @Override
+ public void run() {
+ if (!m_started) {
+ m_pending.add(c);
+ }
+ else {
+ doAdd(c);
+ }
+ }
+ });
+ }
+ }
+
+ public void remove(final Component c) {
+ if (c.getComponentDeclaration().getName().equals(ComponentScheduler.class.getName())) {
+ ((ComponentContext) c).stop();
+ } else {
+ m_serial.execute(new Runnable() {
+ @Override
+ public void run() {
+ if (!m_started) {
+ m_pending.remove(c);
+ }
+ else {
+ doRemove(c);
+ }
+ }
+ });
+ }
+ }
+
+ private void doAdd(Component c) {
+ if (! (m_threadPool instanceof NullExecutor)) {
+ ((ComponentContext) c).setThreadPool(m_threadPool);
+ }
+ ((ComponentContext) c).start();
+ }
+
+ private void doRemove(Component c) {
+ ((ComponentContext) c).stop();
+ }
+}
Modified: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/DispatchExecutor.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/DispatchExecutor.java?rev=1620563&r1=1620562&r2=1620563&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/DispatchExecutor.java (original)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dm/src/org/apache/felix/dm/impl/DispatchExecutor.java Tue Aug 26 10:59:41 2014
@@ -7,7 +7,7 @@ import java.util.concurrent.atomic.Atomi
import org.osgi.service.log.LogService;
/**
- * A parallel DispatchExecutor, similar to the SerialExecutor, except that several DispatchQueues can be executed
+ * A parallel DispatchExecutor, similar to the SerialExecutor, except that several DispatchExecutor can be executed
* in parallel inside a shared thread pool. When one thread schedules tasks in DispatchQueue Q1, Q2, then Q1/Q2
* queues are dispatched and executed concurrently in a shared thread pool. However, each queue will execute tasks
* scheduled in it serially, in FIFO order.