Search in sources :

Example 1 with LocalNodeMasterListener

use of org.elasticsearch.cluster.LocalNodeMasterListener in project elasticsearch by elastic.

the class ClusterServiceTests method testLocalNodeMasterListenerCallbacks.

public void testLocalNodeMasterListenerCallbacks() throws Exception {
    TimedClusterService timedClusterService = createTimedClusterService(false);
    AtomicBoolean isMaster = new AtomicBoolean();
    timedClusterService.addLocalNodeMasterListener(new LocalNodeMasterListener() {

        @Override
        public void onMaster() {
            isMaster.set(true);
        }

        @Override
        public void offMaster() {
            isMaster.set(false);
        }

        @Override
        public String executorName() {
            return ThreadPool.Names.SAME;
        }
    });
    ClusterState state = timedClusterService.state();
    DiscoveryNodes nodes = state.nodes();
    DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(nodes.getLocalNodeId());
    state = ClusterState.builder(state).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).nodes(nodesBuilder).build();
    setState(timedClusterService, state);
    assertThat(isMaster.get(), is(true));
    nodes = state.nodes();
    nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(null);
    state = ClusterState.builder(state).blocks(ClusterBlocks.builder().addGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_WRITES)).nodes(nodesBuilder).build();
    setState(timedClusterService, state);
    assertThat(isMaster.get(), is(false));
    nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(nodes.getLocalNodeId());
    state = ClusterState.builder(state).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).nodes(nodesBuilder).build();
    setState(timedClusterService, state);
    assertThat(isMaster.get(), is(true));
    timedClusterService.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.elasticsearch.cluster.ClusterState) LocalNodeMasterListener(org.elasticsearch.cluster.LocalNodeMasterListener) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 LocalNodeMasterListener (org.elasticsearch.cluster.LocalNodeMasterListener)1 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1