Friday, March 30, 2012

What happened to pre-release versions?

In RFC 175, we proposed to introduce two new things for Core Release 5. One was a change to the version syntax to support pre-release versions and the other was to introduce a VersionRange class to provide a common implementation of version range operations. When the Core Release 5 spec was completed, it included the VersionRange class but did not include pre-release version support. So, what happened?

Pre-release, aka. snapshot, versions seemed like a good idea when first proposed. From the RFC:
During development of a bundle (or package), there is a period of time where the bundle has not been declared final. That is, the bundle has a planned version number once final, but that version number is not practically consumed until the bundle has been declared final. However, during development of the bundle, it must have a version number. This version number must be larger than the version number of the previous final version of the bundle but less than the version number intended for the bundle once final.
There are several usage patterns for version numbers which have emerged to deal with this problem. For example, some use an odd/even version number for the minor version to differentiate between development versions and final (release) versions. Some also place the build timestamp in the qualifier to distinguish all built versions of a bundle, but there is no clear marking which is the final version so dependents cannot mandate a final version.
So we proposed a change to the version syntax to open up space between version numbers so that before the unqualified version (e.g. 1.2.3) there would be pre-release versions. So 1.2.3-snapshot would be a lower version number than 1.2.3. It would have a total ordering over all versions and be backwards compatible with existing version syntax.

1.2.3- < 1.2.3-x < 1.2.3 = 1.2.3. < 1.2.3.x

However, we also had to work properly with existing version range syntax. For example, is the version 1.2.3-snapshot included in the range [1.2.3,2.0.0)? We defined two rules for this.
  1. If a version range having an endpoint specified without a qualifier (e.g. [1.2.3,2.0.0)) would include a version with a release qualifier of the empty string (e.g. 1.2.3), then the version range must also include that version when identified as pre-release (e.g. 1.2.3-x).
  2. If a version range having an endpoint specified without a qualifier (e.g. [1.2.3,2.0.0)) would exclude a version with a release qualifier of the empty string (e.g. 2.0.0), then the version range must also exclude that version when identified as pre-release (e.g. 2.0.0-x).
All together we had a complete design and I was able to implement the changes to the Version and VersionRange classes and write compliance tests. We even implemented it in Equinox. So from a runtime point of view, things looked OK.

But the big concern come around interactions with existing tooling, management and provisioning systems. These systems would not understand a bundle having a pre-release version string. They would require a lot of changes to properly handle support the pre-release version syntax.

Furthermore, we also become concerned about the mental complexity of pre-release versions. In numerous discussions within CPEG and with peers, people would get confused over the ordering of versions and whether some version was included in some range. If we, the "experts", couldn't keep it straight in our minds, we might expect others to also have a hard time.

So in the end, given the mental complexity and the downstream impact to tools, repositories and management systems, CPEG decided that the benefit of the changes was not sufficient to justify the cost of the change. So we agreed, after some lengthy discussions to discard the pre-release version proposal.

BJ Hargrave

Friday, March 23, 2012

Surprising Services

Services are arguably what OSGi is about but at the same time they are also the least understood. When we set out to design OSGi our key requirement was to create an ad-hoc collaborative programming model. Instead of components that are centrally managed and/or are closely coupled to their siblings, we looked for a model of independent peers. To understand what I mean:


In a peer-to-peer model you need to be able to find out who your peers are, and how you can collaborate with them. In the agent or actor model the peer is identified and messages are exchanged based on peer identity. Peer dependencies suffers from the transitive dependency aggregation, quite soon almost all agents are transitively dependent on each other and there is no more component reuse. This is the big ball of mud problem. To address this problem we came up with the service model.  OSGi services combine package based contracts with an instance broker, to provide a managed conduit between peers. 

That said, then what is actual the value of the services for application developers?

Well, the surprising effect of services is that the contracts can often be significantly simplified over traditional APIs because in most Java APIs the collaborative parts are mixed with instance coupling techniques (hacks?)  (DocumentBuilderFactoryInitialContextFactoryBuilder anyone?) and administrative aspects.

It turns out that with services you rarely need to define contracts for these aspects since they are taken care of by the service model (factories, but much more), or in most cases can stay inside the component. Many administrative aspects can be handled by the implementation. Service contracts can be limited to strictly the collaborative parts. And size does matter, the smaller the contract, the easier it is to use.

For example, assume you need to use a persistent queue that is used by a set of workers. Active MQ, Amazon SQS, etc. have a significant number of API calls about maintaining the queues, setting the properties, and interacting with it. However, virtually all of those aspects can be defined in Configuration Admin, the only collaborative aspects are how does the worker get its task and how do you queue a task?

The by far simplest solution I know is to define a contract where only the worker registers a Worker service and a MessageQueue service:

  @Component(properties="queue=myqueue")
  public class MyWorker implements Worker<MyTask> {
      MessageQueue queue;


      public void work(MyTask task) { 
         ...
         queue.post( AnotherTask.class, another );
      }


      @Reference(target="(queue=myqueue)")
      void setQueue( MessageQueue queue ) {
         this.queue = queue;
      }
  }

This queuing contract is sufficient to allow a wide variety of different implementations. Implementing this with the Amazon Simple Queue Service is quite easy. A puny little bundle can look for the services, uses the queue service property to listen to queues and dispatch the messages. In this case, the web based AWS console can be used to manage the queues, no code required. A more comprehensive implementation can use Configuration Admin to manage queues, or it can create queues on demand. Implementing this on another persistent queue can be done quite different without requiring any change in the components that act as senders and workers.

If there is one rule about simplifying software that works consistently then it is hiding. Something that you could not see can't bug you. OSGi services are by far the most effective way to hide implementation details; minimizing what must be shared. Our industry is predicted to double the amount of code in the next 8 years, we better get our services on or this avalanche will bury us.

 Peter Kriens

Tuesday, March 20, 2012

Coordinator

Last year we introduced the Coordinator in the Compendium 4.3. Unfortunately, this release 4.3 was held up over some legal issues. However, it will soon be available, in the 4.3 Compendium as well as the Enterprise 5.0.

The Coordinator service is a bit my baby. When we started with OSGi almost 14 years ago one of the first things I proposed was to start with a transaction manager. I'd just read in 3days Transaction Processing from Gray & Reuters and was thrilled, that had been the best non-fiction book I ever read. Obviously the ACID properties were interesting, and very informative to see how they could be implemented, but the most exciting part was the coordination aspect of transactions. Transactions, as described in the seminal book, allowed different parties (the resource managers) to collaborate on a task without prior knowledge of each other. Resource managers when called could discover an ongoing transaction and join it. The transaction guaranteed them a callback before the task was finished. This of course is a dream in a component model like OSGi where you call many different parties of which most you have no knowledge of. Each called service could participate in the ongoing task and be informed when the task was about to be finished. When I proposed a transaction manager the guys around the table looked at me warily and further ignored me, transactions in an embedded environment?

We got very busy but I kept nagging and the rest kept looking if I was silly in the head. In 2004 I finally wrote RFC 98, a modest proposal for a transaction manager light. Of course I immediately ran into the situation that, even though few if any had used it, that there was an already existing Java Transaction API (JTA). Prosyst did some work on this since they saw some value but in the end it was moved to a full blown JTA service. This was not what I wanted because JTA is weird (from my perspective then) because it distinguishes too much between the Container people and the application people. OSGi is about peer-to-peer, containers are about control from above. Try to act like a resource manager with XA (which would give the coordination aspects), however, it looks like it was made difficult on purpose.

The it hit me, I always ran into the opposition because I used a name that too many people associated with heavy and complexity. Though a full blown distributed high performance robust transaction manager is to say the least a non-trivial beast I was mostly interested in the coordination aspects inside an OSGi framework, a significantly simpler animal. So choose to change the name! The Coordinator service was born!

The first RFC was a very simple thread based Coordinator service. When you're called in your service you can get the current Coordination (or you can start one). Each thread had its own current Coordination. Anybody could then join the Coordination by giving it a participant. The Coordination can either fail or succeed, after which all the participants are called back and informed of the outcome. Anybody that has access to the Coordination can fail it, the Coordination will also fail with time out if it is not terminated before a deadline.

So how would you use it? The following code shows how a Coordination is started:


  Coordinator  coordinator = ...
 
  Coordination c = coordinator.create("work",0);
  try {
    doWork(c);
  } catch( Throwable t ) {
    c.fail(t);
  } finally {
    c.end(); 
  }
This template is very robust. The Coordination is created with a name and a timeout. The work is then done in a try/catch/finally block. The catch block will fail the Coordination. Calling end on a failed Coordination will throw an exception so the exception does not get lost. A worker would do the following to participate in the Coordination:

 Coordinator coordinator = ... 
 void foo() {
   doPrepare();
   if ( !coordinator.addParticipant(this))
     doFinish();
 }
 
A worker can use the Coordination service to add itself as a participant. It is then guaranteed to get a call back when the current Coordination is terminated.

An example use case is batching a number of updates. Normally you can significantly optimize if you can delay communications by batching a number of updates. However, how you do you know when you had the last update so you can initiate the batch? If there is no Coordinator, the updates are done immediately, with a Coordinator they can be batched until the Coordination is terminated.


During the specification process a number of features were added: direct Coordinations (not thread based), a variable store per Coordination, and a reflective API.

I guess it will take some time before the Coordinator is really taken advantage of since the model is quite foreign to most developers. However, I am convinced that the service is really what OSGi is all about: coordinated collaborative components.

Peter Kriens

Thursday, March 15, 2012

Requirements and Capabilities

Last night late I finally finished the final documents for Release 5. Brand new Core and Enterprise specifications will await you. Well, after the Expert Group members have voted (one week) and the board has approved it (not sure) and the lawyers have had it long enough on/in their desk (another 60 days). However, we will be able to show you a draft that is almost identical during OSGi Devcon/EclipseCon if all goes well. You will be there, aren't you?

So what is the most important thing in this release? For me it is clear that the OSGi Requirement-Capability model is the winner of this release.

Late 2005 Richard S. Hall and I wrote RFC 112 OSGi Bundle Repository (OBR). In this RFC we developed a generic model for dependencies. This work was driven by my frustration. Though the Java language uses static typing, almost every other software safety aspect is more or less left to the deployer. Running an application that happens to have a Java 7 class will crash somewhere in the future when that class gets executed. Code that requires a certain library happily churns along until that moment the library is accessed. Code written for a high resolution display happily churns along on a small display while looking awful. We seem to be fanatic about types but completely blind for other dependencies!

Our industry did not have a way to describe these myriad of dependencies so the solution was a proliferation of profiles. Instead of having 100 dependencies you only had one. Unfortunately, that single dependency aggregated a thousand dependencies, 900 of which you could not care less about. Oh yeah, and it lacked one dependency you actually really, really, required. Profiles are the siren song of software, they lure us with a simple solution that is surprisingly wrong.

A much better solution is to only depend on what you really depend on and assemble what you need when you need it. If you only use package com.foo, why should you depend on an aggregation foobar that also contains so many other things you have no use for and drags in the kitchen sink (excuse me for mixing metaphors)?

Unfortunately, this model is severely hindered by our human frailties. We humans much prefer foobar over 25 package names even if it hurts (others?)  in the long run. However, that is only an argument if you actually have to handle these packages. Since you're already having the package names in your classes, why not use computers to handle these details? Obviously bndtools and other bnd users like maven show how easy this can be. However, there are many other types of dependencies that until now we could not express in a computer readable way.

This is where OBR comes into the picture. Richard and I developed a language to express capabilities and requirements. Capabilities have attributes and a requirement is a filter on the capability's attributes.

  Cap: osgi.extender=foo.bar; vendor=ACME
  Req: (osgi.extender=foo.bar)


A requirement and a capability can only match when they are in the same namespace. The syntax for requirements and capabilities is fixed by the OSGi, however, the namespace defines the semantics, what it really means to provide such a capability.  OSGi has always been about describing dependencies, think Import-Package and Require-Bundle. All these dependencies have now their own namespace that define the attributes, directives, and the runtime class loading rules.

Capabilities and requirements are declared by a resource. The resource is the artifact that needs to be installed in an environment and then resolved. A resource is resolved if all its mandatory requirements are satisfied by some capability in the environment. This looks remarkably close to a bundle, and that is not a coincidence. However, resources can be anything, from a certificate to a piece of hardware.

The generic model is quite useful in a wide variety of scenarios. A good example is the extender model. An extender reads some content of an active bundle and uses the information to provide a function on top of that bundle. Blueprint, Declarative Services (DS), the Service Loader Mediator, etc. are all extenders. Though the model works extremely well it has one great disadvantage: there is no managed dependency between the extendee (e.g. the bundle using DS) and extender (e.g. the bundle implementing DS).

For example, installing a DS bundle without installing a DS extender results in a very soothing but rather undesired level of inactivity: nothing happens. With the Require-Capability model we now have the possibility to prevent this peaceful state with the extender namespace.  The contract is that the extendee (the bundle that uses DS) requires the osgi.component extender capability and that any implementation of DS provides the osgi.component extender capability. If the DS extendee is installed it cannot resolve until there is also a matching implementation of the DS bundle installed.

It gets even better. Each bundle becomes completely self-describing with the Requirement-Capability model. This enables the holy grail of automatic assembly of components (well, ok, almost). From a resource it is now possible to find the minimum set of transitive dependencies automatically, even taking local capabilities into account.  For me the greatest advantage of this model is that it voids the need for "applications". Any bundle can be an "application" since any dependencies can now automatically be included.

In Release 5 the OSGi added a completely generic API that is used by a Resolver service and a Repository service. The Resolver service provides access to an OSGi aware resolver that can take resources and find the closure of a set of resources. The Repository service provides access to local or external repositories. The Service Loader Mediator and the Subsystems specifications also rely heavily on the generic Requirement-Capability model.

If you're interested in this model, I was asked to do the OBR presentation from David Savage during our OSGi BOF (Tuesday, Reston Suites B 19.00). If that is not sufficient motivation then be aware that you can also win a free OSGi in Action, OSGi in Depth, or  Java Application Architecture  book during the BOF.

After OSGi DevCon/EclipseCon my work for the OSGi Alliance will be over.  This was my last release. Though there is a certain amount of sadness to leave it behind I am happy that the Requirement-Capability model made it in this release since it is for me the most innovative piece since services. Though I will not be active in the Alliance I am pretty sure that my future work will include the Requirement-Capability model. Check it out! (After the lawyers have finished reading it.)

Peter Kriens


P.S. Of course there is also the free OSGi Cloud Workshop on Thurs 09.00 to 13.00 where we are looking for input on the future activities for the OSGi Alliance to work on in this area.  Places are limited, and we are nearly full, so if you want to join us for this be sure to sign up quickly.

Blog Archive