Showing posts with label OSGi. Show all posts
Showing posts with label OSGi. Show all posts

Friday, April 26, 2013

Multiple Profiles and Shared Bundles With Eclipse P2 : Case Study


WSO2 Carbon is an OSGi based server framework. Number of WSO2 middleware products use Carbon as their base platform. Carbon make use of Eclipse Equinox as its OSGi framework implementation and use Eclipse P2 as its provisioning framework.

Problem Description

Some of the Carbon products has their own deployment patterns during actual production deployments. However to give a smooth evaluation experience the same product is available as a 'ready to go' all in one zip distribution. For an example, WSO2 Business Activity Monitor(BAM) allows enterprises to batch process their collected enterprise data and present it by means of human readable graphs/etc. A real production deployment includes, three main parts.

Figure 1



















1. Receiver component responsible for receiving the events
2. Storage and Analyzer will analyze the stored data in batch mode.
3. presentation component will present the processed data by means of dashboard elements.

Even though these components are distinct by their functionality and by their deployment, they are all part of an one product. The first experience of the middleware developer should be seamless, means Business Activity monitor product should include all these components. However having all the components in the product resolves to a larger memory footprint during the run-time. During an actual deployment users have to deploy three seperate BAM instances even though they are interested only in one functionality of the product at a given time - they can't deploy only the receiver bit of BAM.

All of WSO2's Carbon based middleware products are available in the cloud as a multitenanted PAAS offering. This resolves to a atleast one product instance from each of the product types (Enterprise Service Bus, Application Server,etc). If we take per product distribution size to be ~100MB, then ten product instances will resolve in to 1GB. However we want to make our cloud offering available to users as a downloadable archive. This will enable them to easily download/setup their own private PAAS and evalute it - means total distribution size matters.

Solution 1 : Handling multiple profiles

Figure 2




















There is one important aspect in the above described BAM scenario. All the different deployment entities can share the same configuration. Being differnt component of BAM server, the configuration requirement of the three different components are more or less the same. If we can selectively activate some of these functionalities using a switch mechanism, that would do the trick. The Eclipse P2 has a concept called profiles. Once you provision your OSGi application using P2, the application get assigned a profile. During the server startup, only the bundles that were provisioned under a particular profile get started. We created seperate profiles for each BAM component, namely receiver, analyzer and dashboard. During the server start-up user can select the profile by means of a system property. The default profile contains functionality of all of the BAM components just like good old days. The notion of profile is a logical partition that works with server provisioning framework. Each of these profiles share the same set of bundles in a shared repository, hence there is no increase in the distribution size.

Solution 2 : Shared bundle pool during run-time

Figure3


The solution for the second problem, can be addressed using P2 profiles and shared bundle pools as well. However there is a suttle difference in the approach. Unlike the BAM use case, each of these different products have their own configuration area and data/persistance locations. We can tackle this requirement by completely removing the bundle pool location from the product distribution and placing it outside of all products. Each product will have their own configuration/distribution area and all the products will point to the same bundle location, hence distribution size will be much much less. The use case demands runtime isolation (running two or more products parallely using the same bundle location) and we can successfully achieve the same using P2 profiles and bundle pooling.

Pitfalls

P2 bundle pooling functionality coupled with roaming (the ability to move your provisioned application from your original location - relies on relative paths) seems to be broken. However with the help from P2 dev-list we figured out a workaround.

Images
http://cdn3.iconfinder.com/data/icons/humano2/128x128/mimetypes/application-x-jar.png
wso2 BAM product presentation slides


Sunday, February 10, 2013

Patterns in OSGi programming : White Board Pattern

A common solution for a recurring problem can be named as a pattern. Patterns are everywhere. More often than not, we adopt patterns for computer science from our day to day life experiences. White board pattern is one such common pattern. The pattern allows you to decouple two parties from each other (consumer/provider). I came across this pattern for the first time during my final year project. We worked on a distributed Tuple space implementation and end user-apps used white-board pattern to publish/consume data-strings to/from the tuple space.

The pattern is analogous to a notice board. When ever one wants to publish something to the world/audience, he/she goes and stick the note on the notice board. The interested party can pick up the note. This makes provider and consumer decoupled from each other.


White-board in OSGi ?
In OSGi programming model we do have notice board like mechanism. The OSGi service registry provided by the runtime can act as the whiteboard for publishers/consumers. In OSGi jargon we called them services. We can publish and lookup services that resides in OSGi service registry.





Why not Observer pattern to consume services ?
One common way to consume OSGi services is to use Observer pattern. Here we acquire the service using an API look-up from the service registry and register our implementation against the exposed service.

Sample use-case
Think about a web-app that runs inside an OSGi environment. One of the bundle publishes the standard http.service (http service is the link between servlet transports and the OSGi environment) to the environment and service consuming bundles should hookup their servlets to the published http.service.

Here bundle foo has a servlet implementations that has to be registered under context /foo and likewise bundle bar wants to register a servelet under context /bar. Http.Service allows you to register a servlet using the API method,

registerServlet(String context, Servlet servlet); // example method signature.

Hooking in to the Http.Service using Observer pattern
When using observer pattern, the foo and bar bundles become observers. The individually lookup for http.service from the OSGi service registry and registers their respective servlets against preferred contexts using http.service API.










As depicted above, each bundle has to acquire the http.service through service lookup and register their servlets against http.service.

The same using white board pattern














In this scenario, instead bundles looking up for http.service, they themselves publish their servlet implementation with OSGi service registry. Then who does the http.service and servlet hooking up part ?. There we have another bundle to take care of that. It looks up both http.service implementations and servlet implementations from the OSGi service registry and hook them up together.

Benefits
  • The consumers are no longer depend on the service providers interface. Instead they just have to adhere to servlet.api interface. 
  • The change in the http.service API will affect only the service binding bundle implementation. It will not affect the consumers.
  • The life-cycle of the consumer bundle does not get affected by the http.service. That is, it can survive in a OSGi runtime where http.service is not available.
We use the above pattern during servlet registration process in the WSO2 Carbon servers. Recently while doing some code hacking, I saw the same pattern in pax-logging code. They use it to hookup hookup layouts, appenders and filters to the logging engine.




Sunday, February 3, 2013

Demystifying OSGi : Java Colombo Meetup

Java Colombo is the largest Java developer group in Sri Lanka. Recently, we (me and my collegue Sameera) did a introductory session on OSGi during one of their meetups. It was very well received. Below are the presentation slides we used during the meetup.



Me presenting at the meetup. :) The photo credit for below photo goes to Isuru Perera.

Saturday, August 11, 2012

WSO2 Carbon 4.0 Architecture - My Notes

With the 4.0 release of WSO2 Carbon  we have changed the internal architecture of the platform in a significant way. In the pre Carbon 4 era, WSO2 carbon used to get deployed as a web-app on apache-tomcat servlet container under the hood. The hight level architecture looked like,



 As shown in the above diagram Carbon framework get deployed as just another web-app in Apache tomcat server. Hence, in the in the Carbon 3.2.0 architecture there were two Carbon environments,

  1. The OSGi environment - the area where coloured in light blue
  2. The Non OSGi environment - rest of the area
The requests were routed to the OSGi environment using the bridge-servlet mechanism.(read the equinox getting started guides for more details)

However with this approach there was a noticeable disadvantage:

JAR duplication -  Carbon based products like WSO2 Applicaton Server allows users to deploy web-applications. To share some of the core APIs with the third-party web-apps we had to move them-out/duplicate from the Carbon OSGi environment. (Eg: Carbon Context API).


The Solution : Make Tomcat a bundle and move it in to the OSGi environment





In the new architecture we run our application within a pure OSGi environment. There is no bridging between environments. One of the reasons that blocked us from moving to this clear architectural model was the unavailability of tomcat as a bundle. We created a OSGi bundle version of embedded tomcat, which is somewhat similar to existing OSGified jetty servlet container.


Some of the pros of the above approach includes:

1. There is no jar duplication. Hence almost all the carbon related jars resides in $CARBON_HOME/repository/components/plugins directory
2.Now the all the Carbon libs are visible to the third-party web-apps. This is one of reasons why we are going to support native support for CXF web-apps.
3. Now carbon can be deployed in any OSGi framework/platform with minimal set of modifications - its a set of bundles.


However a techy user still can deploy Carbon as a web-app in another servlet container like Apache-tomcat standalone server, JBoss, IBM WAS, etc. :)
The documentation will follow.