pderop commented on PR #598:
URL: https://github.com/apache/felix-dev/pull/598#issuecomment-7156094881
Thanks for the review! Commit b9f7be4744. addresses your first comment: a DM
bundle stopped by a management agent is no longer restarted by `getCache()`,
and a start failure is logged only once. I'll address the other comments in
follow-up commits.
One edge case is deliberately not handled: a stop which completes between
the checks of `getCache()` and the moment `Bundle.start()` gets the bundle
lock. For example:
1. the start level thread is activating the DM bundle (`STARTING`, it holds
the bundle lock);
2. a user bundle calls `getCache()`, reads `STARTING` and `m_stopped ==
false`, calls `start(START_TRANSIENT)` and waits for the bundle lock;
3. a management agent calls `stop()`, which also waits for the lock;
4. the activation completes, and the management agent gets the lock first:
the DM bundle is stopped (`m_stopped == true`, `RESOLVED`);
5. the user bundle gets the lock, finds the DM bundle `RESOLVED`, and
activates it again.
I thought about closing it with a `ThreadLocal`: the final decision is taken
in `init()`, which runs in our `Activator.start()` while the framework holds
the bundle lock (so a stop is either complete or not started), and the
`ThreadLocal` tells `init()` whether the activation comes from `getCache()`
(refused if the DM bundle has been stopped) or from the framework or a
management agent (accepted). This works because the framework calls the
activator in the thread which calls `Bundle.start()` (both Felix and Equinox
do):
```java
/** Set while getCache() starts the DependencyManager bundle (see init()). */
private static final ThreadLocal<Boolean> m_startedByGetCache = new
ThreadLocal<>();
public static ServiceRegistryCache getCache(Logger logger) {
try {
Bundle dm =
FrameworkUtil.getBundle(ServiceRegistryCacheManager.class);
if (dm != null) {
int state = dm.getState();
if (state == Bundle.STOPPING || m_stopped) {
return null; // shortcut: init() takes the final decision
}
if (state != Bundle.ACTIVE) {
m_startedByGetCache.set(Boolean.TRUE);
try {
startDependencyManager(dm);
} finally {
m_startedByGetCache.remove();
}
}
}
} catch (Exception e) {
if (m_stopped) {
return null; // init() refused to restart the stopped
DependencyManager bundle: not an error
}
if (m_startFailureLogged.compareAndSet(false, true)) {
logger.log(Logger.LOG_ERROR,
"Could not start the DependencyManager bundle, the service
registry cache won't be used", e);
}
return null;
}
return m_cache;
}
public static void init(BundleContext context) {
// called by our Activator start(), while the framework holds the bundle
lock: a stop is either complete or not started
if (m_stopped && m_startedByGetCache.get() != null) {
throw new IllegalStateException("The DependencyManager bundle has
been stopped, a user bundle must not restart it");
}
m_stopped = false;
...
}
```
When `init()` throws, the framework cancels the activation (the bundle goes
back to `RESOLVED`), and `getCache()` returns `null`.
I didn't apply it, to keep the code simple: it adds a `ThreadLocal`, an
exception thrown on purpose, and a hidden coupling between `init()` and
`getCache()`, for an edge case that requires a management agent to stop the DM
bundle exactly while it is being activated. Its effect is also limited: the DM
bundle is only restarted transiently, so the persistent stop is still honored
at the next framework start. The window is documented in the javadoc of
`getCache()`. If you think it's worth closing, I can add it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]