Skip to content
dahliamalkhi edited this page Jun 29, 2016 · 1 revision

The A-B-C of writing a Corfu example

Corfu applications must start by instantiating a CorfuRuntime object. This object connects with an existing Corfu deployment. The application can use the runtime services, such as logging to the Corfu log, building shared data-structures over the log, and so on.

There are three deployment scenarios. We will start with the easiest one, a test deployment. Later, we discuss how to deploy a full Corfu service within a single JVM, and finally, how to deploy a distributed Corfu service.

A test CorfuRuntime

To bring up a test environment, define a test class which extends AbstractViewTest as follows:

public class MySimpleTest extends AbstractViewTest {

  @Override
  public String getDefaultConfigurationString() {
    return getDefaultEndpoint();
  }

  CorfuRuntime getRuntimeAndConnect() {
    return getDefaultRuntime();
  }

}

A shared data-structure

Next, open a stream in the runtime. A stream has a unique name (a String), and an associated class-type of the object it backs. The first time the stream is opened by any application, the stream is created, and an object is instantiated with no initial value.

For example, to associate a SharedCounter class with a Corfu stream named "SimpleCounterExample", do as follows:

public SharedCounter getSharedCounter() {

  SharedCounter sharedCounter = runtime.getObjectsView().build()
     .setType(SharedCounter.class)
     .setStreamName("SimpleCounterExample")
     .open();
  return sharedCounter;
}

The SharedCounter class is the simplest shared data structure possible, which stores an integer value and has a set/get methods:

    public static class SharedCounter {
      Integer cnt;

      @Accessor
      public Integer getCnt() { return cnt; }

      @Mutator
      public void setCnt(int v) { cnt = v; }

  }

Simple as it is, make no mistake: it is not a trivial feat! In a few LOC, it is a strictly atomic shared counter, that distributed applications may manipulate.

Putting it together: Running a SimpleCounterExample test

We are now ready to define our first test. Inside SimpleCounterExample, the following code snippet could be a unit test or the main method. Within IntelliJ, you may run it by simply right-clicking the method, and hitting 'run':

// instantiate a test runtime
CorfuRuntime runtime = getRuntimeAndConnect();

// open a SharedCounter
SharedCounter cnt = getSharedCounter();

// set/get counter value
cnt.setCnt(44);
system.out.println("counter value: " +  cnt.getCnt());

Single JVM runtime deployment

The Corfu main class infrastructure.CorfuServer brings up an entire Corfu service within a single JVM. Invoking it with command line arguments -ms 9000 brings up an in memory Corfu service which listens on port 9000.

You will need to build a CorfuRuntime object that connects with the single JVM service. The CorfuRuntime constructor gets options as a Map<String, Object>. Do not create the map manually; use the Docopt to convert command-line arguments to an options map, as in (see http://docopt.org for more details):

finalString doc = 
    "--h --help\n"
   +"--c <corfu-config>       The configuration string for Corfu [default:localhost:9000]\n";

Map<String, Object> opts = new Docopt(doc).parse(args);
CorfuRuntime runtime = new CorfuRuntime((String)opts.get("-c"))
                    .connect();

Once you have a runtime, the rest of the SimpleCounterExample is the same.