Repository navigation
AutoProcessor - Load and start bundles in parallel - #236
pskowronek wants to merge 1 commit into
Conversation
|
Any chance for feedback on this one? |
|
@pskowronek interesting concept. Did you encounter this while creating this PR? |
No, I didn't encounter this. We're using this patch in https://github.com/mucommander/mucommander project since around 6 months, and so far no deadlocks were reported. |
Ok, that's good. I might try it as well in our product and see what happens. I think we had the issue 1 out of 10 startups, and have a script that i can test it with. |
|
Without having reviewed the full PR, I think it would make sense to at least make this behavior optional and thus configurable, to not break any existing applications. But let me first try it out in my application to see if i run into any issues. |
Yes, it should be configurable.
Superb! Thanks. |
|
Could you please run this against the OSGi TCKs Spec: |
|
Good point @stbischof. |
|
This all happens in main Project. The command line launcher. So this may make it easier. But the startlevel chapters should be respected. Did you read them carefully? The bnd Launcher also does start bundle in parallel. Maybe @pkriens can tell us why. |
|
@pskowronek i checked how we ran parallel startup earlier, and it was based on code shared in https://felix.apache.org/documentation/subprojects/apache-felix-dependency-manager/reference/thread-model.html. How does this PR work differently than via the dependency manager as outlined above? I assume this PR just starts the OSGi bundles in parallel, where the above starts the services in these bundles in parallel, correct? |
|
@pskowronek i tried it out and compared startup times against the "regular" build. I'm also running our script to detect startup issues, will let you know the outcome. |
|
I have the results; startup did fail at least once with the following exception: Seems like there can be a race condition with the dependency manager, see the stacktrace. |
Can you tell me how many bundles the test app has, and what was the processor you were testing on? |
|
I don't have the exact count at hand, but in the order of 30-40 bundles i think. |
ok, what type of processor the test was run on - the number of (HT) cores might matter. In muCommander we have around 180 bundles , and since this is desktop app, it cpu have 4 (8 HT) cores at minimum. |
Hi Paul, yes, you are correct, when DM is used in concurrent mode, then services are all started and registered concurrently, so concurrency is achieved at the service level. now, about the exception you reported:
after many attempts, I confirm that I also reproduced the problem. So, from the stacktrace, we have a NPE in this location, meaning that in the previous line (here), I cannot comment on this for the moment. |
|
It it correct that all your bubdles use onlY bundleactivators and no declarative services? |
Yes |
I don't know what causes this, but maybe adding a while loop on the bundleContext being non null with a thread.sleep(100) would resolve this intermittent timing issue? Our startup times greatly improved by starting services in parallel, but the intermittent NPE cause us to not use it in production. |
|
Hi @paulrutter , Well... it only took a little while to get back to this one 😅 I'm not sure anyone is still waiting for it at this point, but just in case: there is now a PR that should fix the Dependency Manager side of the parallel startup problem. 🎉 See #598, I will test it a bit more this week, then if you want to review it and even better test it before I merge ? (if you are able to test, please do not test it using this PR 236, because in my opinion, the patch should be reworked and moved from the AutoProcessor to the Framework start level layer, maybe I would like to look into this a bit later this week). thank you. |
Great! Yes, still interested to give it a go. Will let you know when i have results! |
Motivation: ServiceRegistryCacheManager initialized itself from a static initializer, which called start() on the DM bundle in order to get its BundleContext. When a user bundle is started before the DM bundle, the framework may not start the DM bundle synchronously, so getBundleContext() returned null. The static initializer then failed with a NullPointerException, and ServiceRegistryCacheManager became unusable for every DM bundle (NoClassDefFoundError: Could not initialize class ...). This can happen when threads that start bundles in parallel are launched while the framework is still starting and raising its start levels: a user bundle may then be started before the DM bundle, which is still waiting to be started by the framework start level thread. Until now, DM in parallel mode had only been tested after the FrameworkStarted event, so the problem was never seen. Modification: - The cache is now initialized by the DM bundle Activator, with its own BundleContext, and the static initializer is removed: Activator.start() calls ServiceRegistryCacheManager.init(context), which creates the configured filter indices once. - DependencyManager now calls getCache(), which locates the DM bundle with FrameworkUtil and starts it transiently if it is not active. If another thread is already starting it, the framework blocks the caller until the DM Activator has completed, so the cache is ready when the DependencyManager is created. While the framework is raising its start level, it may refuse a transient start (START_TRANSIENT_ERROR): the DM bundle is then started with a persistent start, which lets the framework start it now if it can, else later (the caller then does not use the cache). - The service registry cache is only an optimization: getCache() never throws. If the DM bundle can't be started, the DependencyManager is created without cache, so that bundles can still start. The failure is logged only once, at ERROR level with the DependencyManager Logger. - A DM bundle stopped on purpose is not restarted by getCache(): the Activator stop() calls ServiceRegistryCacheManager.stopped(), which sets a flag, reset by init() at the next start. getCache() then returns null, so no user bundle restarts a stopped DM bundle. - getCache() calls made back on the DM Activator thread while the Activator starts (for example from a ServiceFactory creating a DependencyManager, called by one of the Activator trackers) return null at once, instead of trying to start a bundle which is already starting, and consuming the "log once" flag. - A stop/start of the DM bundle without refresh is not supported (the components created before the stop keep their interceptors on the old cache): the supported way is a refresh, which creates a new class loader, so the static state of ServiceRegistryCacheManager starts from scratch. Tests: - FELIX6874_ParallelStartTest starts 100 user bundles and the DM bundle from a pool of threads, on an embedded Felix framework, with and without a filter index (the Felix framework is added as a test dependency). It fails with the previous code and passes with this fix. It also covers: a stopped DM bundle which is not restarted, the re-entrant getCache() call from the Activator thread, and the START_TRANSIENT_ERROR fallback to a persistent start. By default, the scenario is repeated 20 times (-Dfelix6874.runs=200 for stress runs). - FELIX6874_DependencyManagerRefreshTest (new modules index.itest.refresh.tests and index.itest.refresh.bundle) refreshes the DM bundle with an index configured by the system property, and checks that a service dependency works before and after the refresh, including for a service registered after the refresh, and that the update of the service properties is propagated. Result: Starting bundles in parallel no longer breaks ServiceRegistryCacheManager. Ref #236 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A PoC of loading and starting bundles in parallel in AutoProcessor.
This was tried in the following project https://github.com/mucommander/mucommander that is using AutoProcessor to load bundles. For muCommander this gave 2-3 times faster bundle loading (from 2-3s to ~400-800ms on my macbook 2012) - see here mucommander/mucommander@ab376b9 (PR in muCommander: https://github.com/mucommacommits/ab376b984257e79a563a0fa49cc128af9c501e9f).
Can you please review and assess whether the concept could be incorporated into Felix, whether it is safe etc.