In this chapter, we'll learn how to implement a Participant, which is a primary functional component of a distributed system.

Start the Helix Participant

The Helix participant class is a common component that connects each participant with the controller.

It requires the following parameters:

  • clusterId: A logical ID to represent the group of nodes
  • participantId: A logical ID of the process creating the manager instance. Generally this is host:port.
  • zkConnectString: Connection string to Zookeeper. This is of the form host1:port1,host2:port2,host3:port3.

After the Helix participant instance is created, only thing that needs to be registered is the state model factory. The methods of the State Model will be called when controller sends transitions to the Participant. In this example, we’ll use the OnlineOffline factory. Other options include:

  • MasterSlaveStateModelFactory
  • LeaderStandbyStateModelFactory
  • BootstrapHandler
  • An application-defined state model factory
HelixConnection connection = new ZKHelixConnection(zkConnectString);
HelixParticipant participant = connection.createParticipant(clusterId, participantId);
StateMachineEngine stateMach = participant.getStateMachineEngine();

// create a stateModelFactory that returns a statemodel object for each partition.
HelixStateModelFactory<OnlineOfflineStateModel> stateModelFactory = new OnlineOfflineStateModelFactory();
stateMach.registerStateModelFactory(stateModelType, stateModelFactory);
participant.startAsync();

Helix doesn't know what it means to change from OFFLINE-->ONLINE or ONLINE-->OFFLINE. The following code snippet shows where you insert your system logic for these two state transitions.

public class OnlineOfflineStateModelFactory extends HelixStateModelFactory<OnlineOfflineStateModel> {
  @Override
  public OnlineOfflineStateModel createNewStateModel(PartitionId partitionId) {
    OnlineOfflineStateModel stateModel = new OnlineOfflineStateModel();
    return stateModel;
  }
}

@StateModelInfo(states = "{'OFFLINE','ONLINE'}", initialState = "OFFINE")
public static class OnlineOfflineStateModel extends StateModel {
  @Transition(from = "OFFLINE", to = "ONLINE")
  public void onBecomeOnlineFromOffline(Message message,
      NotificationContext context) {

    System.out.println("OnlineOfflineStateModel.onBecomeOnlineFromOffline()");

    ////////////////////////////////////////////////////////////////////////////////////////////////
    // Application logic to handle transition                                                     //
    // For example, you might start a service, run initialization, etc                            //
    ////////////////////////////////////////////////////////////////////////////////////////////////
  }

  @Transition(from = "ONLINE", to = "OFFLINE")
  public void onBecomeOfflineFromOnline(Message message,
      NotificationContext context) {
    System.out.println("OnlineOfflineStateModel.onBecomeOfflineFromOnline()");

    ////////////////////////////////////////////////////////////////////////////////////////////////
    // Application logic to handle transition                                                     //
    // For example, you might shutdown a service, log this event, or change monitoring settings   //
    ////////////////////////////////////////////////////////////////////////////////////////////////
  }
}

Back to top

Reflow Maven skin by Andrius Velykis.

Apache Helix, Apache, the Apache feather logo, and the Apache Helix project logos are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Privacy Policy