Terracotta Toolkit – Part 4 (Clustered Barrier)

Recently I had a need to coordinate test drivers across JVM's, so that they all fire the load at the same time. There were many options, needed flexibility in terms that each had its own warm-up time and other dependencies. Terracotta's Clustered Barrier was a natural choice for this. Reason's were simple, I had already seen it working and it was simple enough to integrate it within application.

What you need to run this sample
1. Terracotta Release Download here
2. Include terracotta-toolkit-1.1-runtime-2.1.0.jar in classpath. You can find the jar in terracotta_install/common folder

The image below describes the scenario

Scenario

Lets see how we use the Toolkit barrier to achieve this

public class ClusteredBarrierSample {
    static final String BARRIER_NAME = "RUN_BARRIER";

    Barrier barrier;

    // Handle to the toolkit
    ClusteringToolkit clusterToolkit = null;

    public void initializeToolKit(String serverAdd, int barrierParties) {
        clusterToolkit = new TerracottaClient(serverAdd).getToolkit();
        barrier = clusterToolkit.getBarrier(BARRIER_NAME, barrierParties);
    }

    /**
     * waits till all the parties have joined and once all drivers are ready
     * fires the test
     */
    public void fireTest() {
        try {
            barrier.await();
            // fire my network drivers
            System.out.println("firing the driver");
        } catch (Exception e) {
            e.printStackTrace();
       }
    }

    public static void main(String[] args) {
        ClusteredBarrierSample barrierSample = new ClusteredBarrierSample();
        barrierSample.initializeToolKit(args[0], Integer.parseInt(args[1]));
        barrierSample.fireTest();
    }
}

The implementation is straight simple and in 3 steps
1. Intialize Terracotta Client
2. Get the Barrier, with number of parties to wait for
3. Call await() on the barrier

Using it was pretty simple and took less than 5 minutes to have the requirements met 🙂

Next Steps:

Refer Terracotta Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.