Home » Docs

Deployment Strategies

Implementing custom update policies

last updated: April 19th, 2012

Introduction

This article describes how ACE deploys new deployment packages to targets and how this strategy can be adapted to support more sophisticated scenario's.
The remainder of this article assumes the reader has basic knowledge of the principles behind ACE, and has sufficient programming skills. For this article, the latest code of ACE (0.8.1-SNAPSHOT, rev.1326140) was used.

Provisioning

Apache ACE is all about provisioning software artifacts to targets. Software artifacts, or simply, artifacts, can be compiled code, configuration files, or any other artifact needed for a software system to operate. Provisioned artifacts are bundled in so-called "deployment packages", that is comprised of the differences between the target's current artifacts and the to-be-deployed artifacts. To distinguish between deployment packages, each one is given its own version. The increment of versions is done automatically by ACE when changes are committed to its repository.
When a new deployment package is available, it is not automatically sent to the target as the communication between target and management server (ACE) is unidirectional and initiated by the target. A target has to periodically check whether new deployment packages are available, and if so, do something with it.

Understanding ACE's deployment strategy

Upon startup, the management agent, which represents a target, schedules several tasks that periodically synchronize the local state with that of the management server. Two of these tasks relate to the handling of deployment packages: DeploymentCheckTask and DeploymentUpdateTask. Figure 1 shows the relationship between the various components.

Figure 1: class diagram
Figure 1: deployment strategy class diagram.

The DeploymentCheckTask is responsible for periodically checking the management server for newer versions of deployment packages and if found emits an event. The DeploymentUpdateTask also periodically checks the management server, and if it finds a newer version, automatically downloads and installs it.

Figure 2: sequence diagram
Figure 2: deployment strategy sequence diagram.

Figure 2 shows a more detailed sequence diagram of the deployment update strategy in ACE. The scheduler in the management agent calls the run()-method of the DeploymentUpdateTask once every N seconds. This method in its turn makes several calls to the DeploymentService, which acts as a facade for other services in ACE. The DeploymentService is queried for the highest local and remote versions. If the remote version is newer than the local version, a deployment package containing the changes between the local and the remote version is installed. Note that this installation is done in a best effort strategy: if it fails, all changes are rolled back to what they were. As a consequence, if the installation fails one time, it probably will fail the next time as well. 

Tweaking the deployment strategy

By default, the management agent allows the interval in which the update task is run to be customized. To change this interval, the management agent should be started with the extra system property "-Dsyncinterval=1500". The value of this property is interpreted as the interval time in milliseconds. Note that this property also changes the interval of other tasks as well, so setting it to a lower interval could potentially raise lots of traffic to your management server.

Another thing you can customize is whether or not the update task should run at all. This is done by starting the management agent with the system property "-Dorg.apache.ace.deployment.task=disabled". While this option is not really useful out-of-the-box (the target is not be able to obtain any version at all) it opens up a possibility for providing custom deployment strategies.

Implementing a custom deployment strategy

To implement our own deployment strategy, we need to create a bundle that provides the management agent with our update task. We will start with an update task that simply prints something to the console each time it is run and expand it later on. As can be seen in figure 1, a task is anything that implements the Runnable interface, so our update task needs to do as well. Furthermore, we will use the DependencyManager component from Felix to provide us with the right dependent services, the most important one being the DeploymentService.
The (stub) code for our update task looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package net.luminis.ace.updatetask;

import org.apache.ace.deployment.service.DeploymentService;
import org.osgi.service.log.LogService;

public class MyUpdateTask implements Runnable {
    // injected by DependencyManager
    private DeploymentService m_service; 
    private LogService m_logService;

    public void run() {
        System.out.println("Hello from MyUpdateTask: " + new java.util.Date());
    }
}

The bundle activator that registers our update task as service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package net.luminis.ace.updatetask;

import java.util.Properties;

import org.apache.ace.deployment.service.DeploymentService;
import org.apache.ace.scheduler.constants.SchedulerConstants;
import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;

public class Activator extends DependencyActivatorBase {
  public void init(BundleContext context, DependencyManager manager) throws Exception {
    Properties props = new Properties();
    props.put(SchedulerConstants.SCHEDULER_NAME_KEY, MyUpdateTask.class.getName());
    props.put(SchedulerConstants.SCHEDULER_DESCRIPTION_KEY, "My own deployment update service.");
    props.put(SchedulerConstants.SCHEDULER_RECIPE, "5000");

    manager.add(createComponent()
      .setInterface(Runnable.class.getName(), props)
      .setImplementation(new MyUpdateTask())
      .add(createServiceDependency()
        .setService(DeploymentService.class)
        .setRequired(true)
      )
      .add(createServiceDependency()
        .setService(LogService.class)
        .setRequired(false)
      )
    );
  }

  public void destroy(BundleContext context, DependencyManager manager) throws Exception {
    // Nothing
  }
}

The activator isn't that different from any other one, the only interesting part is the service properties that we register along with our update task. The three properties are used by the ACE's scheduler to determine that our service is actually a scheduled task. With the "recipe" key, the scheduler is told what interval (in milliseconds) is to be used to execute our task. In our case the recipe is set to 5000, causing our task to be run once every five seconds.
To complete the whole cycle, we need some build scripting. For this, we use BndTools:

Bundle-Name: My Update Task
Bundle-Version: 0.0.1
Bundle-SymbolicName: net.luminis.ace.updatetask
Bundle-Activator: net.luminis.ace.updatetask.Activator
Private-Package: net.luminis.ace.updatetask
Import-Package: *
-buildpath: org.apache.ace.deployment.task.base;version=0.8,\
     org.apache.ace.scheduler.api;version=0.8,\
     org.apache.felix.dependencymanager;version=3.0,\
     osgi.core;version=4.2.0,\
     osgi.cmpn;version=4.2.0

To let the management agent to find and use our custom update task bundle, we need to add the JAR-file to its class path, and start the management agent with the following options:

[localhost:~/]$ java -Dorg.apache.ace.deployment.task=disabled \
 -cp org.apache.ace.launcher-0.8.1-SNAPSHOT.jar:net.luminis.ace.updatetask-1.0.0.jar \
 org.apache.ace.launcher.Main \
 bundle=net.luminis.ace.updatetask.Activator \
 identification=myTarget \
 discovery=http://localhost:8080

Note that besides adding our bundle to the class path, we also have added the bundle argument to tell the management agent to include our bundle activator in its startup.
If everything went well, one of the first few lines printed out on the console should be something like:

Adding additional bundle activator: net.luminis.ace.updatetask.Activator
Not starting activator org.apache.ace.deployment.task.
...

After startup, our update task should print a message stating its existence to the console every five seconds.

With the boiler plating in place, its time to make our update task a little more interesting. We change the code of our update task to the following (for brevity, only the run() method is shown):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void run() {
    try {
        Version local = m_service.getHighestLocalVersion();
        Version remote = m_service.getHighestRemoteVersion();

        if ((remote != null) && ((local == null) || (remote.compareTo(local) > 0))) {
            // Ask user whether this update should proceed...
            System.out.println("Update available! Upgrade from " + local + " to " + remote + " [Y/N]?");

            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String result = reader.readLine();

            if ("y".equalsIgnoreCase(result)) {
                // Ask the deployer service to install this update...
                m_service.installVersion(remote, local);

                m_logService.log(LogService.LOG_INFO, "Update installed!");
            }
        }
    } catch (IOException e) {
        m_logService.log(LogService.LOG_WARNING, "Update failed!", e);
    } catch (Exception e) {
        m_logService.log(LogService.LOG_WARNING, "Update failed!", e);
}

This new implementation first asks the DeploymentService what the current local (= version current running on management agent) and remote (= version available in the management server) versions are. If the remote version is newer/higher than the local version, than we ask the user for permission to install the update. When given, the DeploymentService is requested to upgrade from the local version to the remote version.
If you would run this code, you'll notice that if the user doesn't respond within the task's interval, a new task is started, causing an ever increasing number of tasks to be waiting for user input in case an update is available. Currently, due to the current implementation of ACE's scheduler, there is no way of disabling this behavior (although it is not really difficult to resolve this problem locally in your task).

Taking it a step further…

So far, we've reused the DeploymentService facade from ACE to tweak the update process of the management agent a bit. However, there still some magic going on in the installation of newer versions (all the logic behind DeploymentService#installVersion).  Let's explore this method a bit more into detail. The installVersion method roughly (some details are left out for brevity) has the following implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// injected by Felix' dependency manager
volatile Deployment m_deployer;
volatile EventAdmin m_eventAdmin;
volatile Identification m_identification;
// denotes the host + port where ACE is listening, e.g. http://192.168.1.1:8080/
final String m_host;

/**
 * @see org.apache.ace.deployment.service.impl.DeploymentServiceImpl#installVersion
*/
public void installVersion(Version highestRemoteVersion, Version highestLocalVersion) throws Exception {
    InputStream inputStream = null;

    try {
        String version = highestRemoteVersion.toString();
        URL baseURL = new URL(m_host, "deployment/" + m_identification.getID() + "/versions/");
        if (highestLocalVersion != null) {
            version += "?current=" + highestLocalVersion.toString();
        }
        URL dataURL = new URL(baseURL, version);
        inputStream = dataURL.openStream();

        // Post event for audit log
        String topic = "org/apache/ace/deployment/INSTALL";
        m_eventAdmin.postEvent(createEvent(topic, version, dataURL));

        m_deployer.install(inputStream);
    }
    finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            }
            catch (Exception ex) {
                // Not much we can do.
            }
        }
    }
}

Basically, the method builds up an URL to access the deployment service of ACE. Through this URL, the deployment-service creates a deployment package containing the changed artifacts between the given local and remote version. The InputStream containing this deployment package is given to the Deployment service (a facade to the standard DeploymentAdmin service) to be installed. Note that if the installation of the deployment package fails, an exception is thrown. As mentioned earlier, the installation of deployment packages is done in a "all or nothing" strategy, meaning that if it fails, all changes are reverted. For more details on this, you can read the DeploymentAdmin specification (see [2], chapter 114).
Aside the actual installation of the deployment package, an event is also posted to keep track of this installation. This event is picked up by the AuditLog service of ACE to track all changes made to the management agent (one can argue whether this shouldn't be a responsibility of the Deployment facade). 

Now we have seen how the installation of deployment packages is implemented in ACE, we can even tweak that process a bit. For example, we could first download the deployment package entirely to a temporary location, and install it from there. Or, as we have access to the deployment package, we could also provide the user additional information about its contents, perhaps showing a change log or a summary of its contents, before installing it.

References

  1. ACE subversion;
  2. OSGi 4.2 compendium specification.