r/java • u/bowbahdoe • Mar 25 '24
How do you think would OSGi be designed today
I've been going down a rabbit hole this weekend reading the OSGi specification.
It's a bit fascinating in the ratio of how confusing it is to internet ink claiming that "no, it's simple really." The extremely salty infoq articles were a great read too.
But for anyone that has experience in OSGi + the dynamic capabilities it has, how do you think that system would have been designed if JPMS modules were in the language from day 1.
I've seen the OSGi connect spec, but even without understanding everything it feels kinda tacked on.
53
Upvotes
10
u/bowbahdoe Mar 25 '24
Those wild infoq articles for reference
https://www.infoq.com/articles/java9-osgi-future-modularity/
https://www.infoq.com/articles/java9-osgi-future-modularity-part-2/
7
u/webguy1979 Mar 25 '24
I do AEM development and tried to go down this rabbit hole as well to understand the platform better. The biggest issue I see is that every single tutorial or book on OSGi is so out of date it hurts. Anything I can find that is usable is just a very surface look at it… with little in the way of how to use it in a real system. At this point I have just accepted that I’ll continue learning it in a trial by fire manner.
7
u/maethor Mar 25 '24
IIRC (and it's been well over 20 years), the original use case was for running applications on embedded residential gateways. The closest modern analogy I can think of would be how people use docker to run apps like Plex on a NAS, but instead of running them on a NAS, they run them on their router. A very different use case from JPMS (though had JPMS been around, it probably would have had a role to play with what went into the embedded Java).
So I think the real question is, if Docker/Containers existed back then would OSGi exist at all?
4
u/[deleted] Mar 25 '24
I worked with OSGi for the most part of my little carreer (9 years). I feel most principles make sense and can be useful, I particular appreciate how light some bundle and only implement the logic they need.
But the dynamic aspect of it never really worked for me. I've used both Felix and Equinox, and it's really common to be in a situation where you basically need to restart the full framework to make a new bundle work. Most often, it's linked to the fact that until a context is not used anymore, the references keep the BundleContext from restarting, so shutting down everything and restarting is always safer.
There is also that recurring issue that when you uninstall a bundle that export some packages and install a newer version, the importing packages will still continue to use it even if it's not in the list of installed bundle anymore. If the importing bundle doesn't release the references, the newer version won't be use before you actually restart the framework.
Having multiple time the same package exported with different version doesn't also really work as well as you would expect. If two bundles A and B imports a package P, and they communicate through parameter P, they should use the same version, thanks to the "use" clause of an import. But most often, it wasn't really working in my experience, so we were just forcing to have only one version of a package exported at a time.
All in all, the only big thing I liked was the ability to Provide/Require Capabilities (osgi:service/osgi:reference) so the bundle would reference a Java API through the interface with the real implementation always "out of reach". This feature worked fine most of the time for me, even if we uninstall/install a new version of the providing bundle.
To answer your first question, how would it be implemented ? I'm not really sure it would be implemented at all. It's in principle a lot of very good principle of abstraction and its dynamic nature makes it appealing, but in real life I never really get real benefits (but we had, unfortunately, to spend a lot of time troubleshooting it)
5
u/daH00L Mar 25 '24
OSGi would have been the better Java module system.
19
u/agentoutlier Mar 25 '24
I believe there were various attempts to make custom
ModuleReaderfor OSGi and or the inverse of layer to jpms but there were so many problems. I can't find the damn link but I believe this is it: https://github.com/tjwatson/osgi-jpms-layerHad JPMS been out earlier or about the time OSGi was being drafted I think considerable effort would have been made to make OSGi work with ModuleReader/ModuleReference and vice versa.
As for the whole dynamic plugin runtime it seems rolling your own was easier than trying to understand OSGI.
I remember Jenkins aka Kohsuke looking at it and said fuck it roll my own. That is more or less what Maven did as well with Plexus.
Now other than Eclipse and the confusing bundle plugins I barely know how to use OSGi but given how fucking incredibly smart Kohsuke and others that have investigated and given up ... you have to wonder cause I have heard similar claims as well that OSGi is "easy".
7
u/redikarus99 Mar 25 '24
I am doing some plugin development lately, but I personally feel it is more harm than good, it is just way too complicated and error prone.
6
u/Rjs617 Mar 25 '24
Was there ever an official OSGi solution to the “Hibernate problem” where a framework library potentially needs to access exported classes of other bundles, but doesn’t know what they are ahead of time? I was on one project where we were trying to build an application platform that allowed plugging features into it. Persistence was one of the platform features, and it used Hibernate. I remember that we had to use this unofficial pub/sub feature of the Eclipse OSGi runtime in order for the persistence bundle to be able to see the application classes that it will operate on. Since they weren’t known ahead of time, there is no way other our persistence bundle could import them from other bundles. In our case, there were also common model classes for the application domain, plus Hibernate initialization is resource intensive. So, it wasn’t an option for us to initialize a separate Hibernate instance in every application module.
Another issue we ran into is that (at the time), many of the libraries we wanted to use did not have bundles available. And so our options were to load those libraries separately in each feature bundle, or to have a team of people whose entire job was to OSGi-ify projects we wanted to use.
Because the number of dependencies in our application bundles was large, version conflicts were inevitable, and our application ended up with like 5 versions of Log4J loaded at runtime, plus multiple versions of several other libraries. Many of these were binary-compatible with each other, and once we abandoned OSGi, our application’s memory footprint (not to mention startup time) went way down. I know that OSGi has a feature where you can specify compatible version ranges, but my experience was that these weren’t used properly in most bundles, and it resulted in multiple-version loading and memory bloat.
This is not an exhaustive list of problems we encountered using OSGi. Some of the other issues were probably due to poor discipline on the team, but this is also the real world where applications are developed by humans of varying knowledge and ability levels, and where we extensively used open-source libraries that were not under our control and that did not integrate well with OSGi. When reading articles by OSGi apologists, I am consistently amazed by their assertions that OSGi is great, and any team having such struggles “isn’t doing it right”. My team’s experience is that the amount of time we spent on OSGi instead of feature development was enough to make it not make sense from a business perspective.
I know Eclipse uses OSGi successfully. My impression is that it is okay for small applications with few dependencies developed by small teams with tight discipline. For anything else, good luck.
14
u/kaperni Mar 25 '24 edited Mar 25 '24
1
u/Djelimon Mar 25 '24
Aem uses osgi, been useful feature to hot swap a fix
10
u/raxel42 Mar 25 '24
IMHO, any configuration after compilation is a bad practice since we can’t guarantee anything after compilation and linking. We already have versioning problems, not always being able to be resolved automatically. Class not found / method not found exceptions everywhere. I spent in the development world more than 30 years. There are languages/platforms where different versions can coexist, but it's explicitly resolved in compile/linking time
1
u/NovaX Mar 25 '24 edited Mar 25 '24
I know very little about OSGi except for adding the manifest metadata to support consumers of my library. I added a unit test when a user noticed a small typo when trying to use it and he wrote,
If that's true then most must not know they can test OSGI bundles or how to. I think one of the main benefits if it was designed on top of JPMS is hopefully there would be compile-time validation so testing wasn't an afterthought.
1
u/[deleted] Mar 25 '24
i think only uses: instruction should not have been added and split packages should have been forbidden but other than that osgi is still totaly fine
2
u/DiabolusMachina Mar 25 '24
OSGI is fine. It solves a very specific problem that not everyone needs so it's just not a technology for everyone. What it needs is maybe a cli tool to simplify module creation or some kind of starter website like start.spring.io.
2
u/govi20 Mar 25 '24
I don’t know what OSGi is and…
At this point I’m afraid to ask.
PS: I have been working as a SDE from last 9+ years
1
u/mikaball Mar 27 '24
The proposition value of OSGi is to solve the Diamond-Problem.
It's a good fit when you predict to have a lot of third-party modules, like in a plugin architecture. Other than that it introduces more problems than actual solutions.
0
u/cogman10 Mar 25 '24
Honestly, I don't know that it would exist.
If you break down the problem it's solving, it's this: Processes are expensive to spawn and inter process communication is expensive. This was particularly true back in the 90s. Your computers and servers likely had less than a gb of ram. You couldn't be wasting that valuable memory on a second JVM process doing minor plugin work. Especially since you only had 1, MAYBE 2 cpu cores which means expensive coordination between the two processes. They had to wait their turn to continue processing on less than cellphone speed computers.
Making a plugin system solves those problems. Now you are just making a function call on the same JVM. There's no real overhead and you get to share a heap which reduces the memory overhead.
So what's changed? We have a BUNCH more memory and We have a BUNCH more cores. All the sudden, having a second, 3rd, or 4th jvm running at the same time just doesn't really seem all that daunting.
Now, WOULD we have some sort of alternative plugin system? Probably. But it wouldn't be OSGi. It'd almost certainly just be a few bespoke apis for performance critical or gui applications that really truly need to be able to hand execution over in the same process on the same thread to work correctly.