Next, we'll learn how to implement a Spectator. Typically, a spectator needs to react to changes within the distributed system. Examples: a client that needs to know where to send a request, a topic consumer in a consumer group. The spectator is automatically informed of changes in the external state of the cluster, but it does not have to add any code to keep track of other components in the system.
Same as for a Participant, The Helix agent is the common component that connects each system component with the controller.
It requires the following parameters:
After the Helix manager instance is created, only thing that needs to be registered is the listener. When the ExternalView changes, the listener is notified.
A spectator observes the cluster and is notified when the state of the system changes. Helix consolidates the state of entire cluster in one Znode called ExternalView. Helix provides a default implementation RoutingTableProvider that caches the cluster state and updates it when there is a change in the cluster.
manager = HelixManagerFactory.getZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, zkConnectString); manager.connect(); RoutingTableProvider routingTableProvider = new RoutingTableProvider(); manager.addExternalViewChangeListener(routingTableProvider);
In the following code snippet, the application sends the request to a valid instance by interrogating the external view. Suppose the desired resource for this request is in the partition myDB_1.
## instances = routingTableProvider.getInstances(, "PARTITION_NAME", "PARTITION_STATE"); instances = routingTableProvider.getInstances("myDB", "myDB_1", "ONLINE"); //////////////////////////////////////////////////////////////////////////////////////////////// // Application-specific code to send a request to one of the instances // //////////////////////////////////////////////////////////////////////////////////////////////// theInstance = instances.get(0); // should choose an instance and throw an exception if none are available result = theInstance.sendRequest(yourApplicationRequest, responseObject);
When the external view changes, the application needs to react by sending requests to a different instance.