In this chapter, we'll learn how to implement a Participant, which is a primary functional component of a distributed system.
The Helix manager is a common component that connects each system component with the controller.
It requires the following parameters:
After the Helix manager instance is created, the 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:
manager = HelixManagerFactory.getZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, zkConnectString); StateMachineEngine stateMach = manager.getStateMachineEngine(); //create a stateModelFactory that returns a statemodel object for each partition. stateModelFactory = new OnlineOfflineStateModelFactory(); stateMach.registerStateModelFactory(stateModelType, stateModelFactory); manager.connect();
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 StateModelFactory<StateModel> { @Override public StateModel createNewStateModel(String stateUnitKey) { 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 // //////////////////////////////////////////////////////////////////////////////////////////////// } } }