Rama Krishna. G
Rama Krishna. G

Reputation: 536

Curator - How to know current node is leader or not

Just curious to know whether there is any API to know the a particular node is leader using CuratorFramework class in Curator Framework. I am using LeaderLatch but not working even the Node is leader (Elected by Zookeeper framework).

NOTE: 3 nodes are configured in cluster set up.

Zookeeper framework will take care of electing a node. I need to know the selected Curator client is pointing to leader or not.

String zkConnString = "172.18.54.211:2181";

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client1 = CuratorFrameworkFactory.newClient(zkConnString, retryPolicy);
client1.start();

 LeaderLatch leaderLatch = new LeaderLatch(client1, "/FirstNode");
 try {
    leaderLatch.start();

} catch (Exception e1) {
    e1.printStackTrace();
}

System.out.println("has leader ship ? "+leaderLatch.hasLeadership());

The output is

has leader ship ? false

The Use case is as follows. Micro Services are involved in the use case. enter image description here

If the LEADER corresponding Micro Service went down then need to select another leader. Each Micro service can be treated as a Z-NODE. If a Z-NODE is deleted a NODEREMOVED event will be triggered and listened.

The LEADER should not be Micro Service down node.

Upvotes: 1

Views: 3160

Answers (3)

Lei Chi
Lei Chi

Reputation: 269

I believe you need to add listener to LeadLatch by following code:

    leaderLatch.addListener(new LeaderLatchListener {
      override def isLeader(): Unit = {
        println(s"I am the lead $i")
      }

      override def notLeader(): Unit = {
        println(s"i am not the leader any more $i")
      }
    })

Upvotes: 0

M. Prokhorov
M. Prokhorov

Reputation: 3993

According to documentation, leader election is asyncrhonous. This means that after you have started leader election, leader information is not necessarily available right away.

You should wait for leader election to be completed first.

Unfortunately, I wasn't able to find clean way of obtaining information about election completion using classes in org/apache/curator/framework/recipes/leader package: the API only provides means of checking leadership change.

The closest query I'm able to find is this (using Java8):

new LeaderSelector(clientFramework, path, new NoOpLeaderSelectorListener())
    .getParticipants().stream()
    .filter(Participant::isLeader)
    .map(Participant::getId)
    .findAny()
    .ifPresent(<... do something with leader node Id ...>);

But I am still unclear whether this will yield correct results with more certainty than original variant.

What it will do is:

  1. Query current nodes from the Framework using interprocess communication
  2. Sort nodes using configured Sorter
  3. Mark first node in line after sorting as Leader node for the purposes of that query

there is also a somewhat equivalent query:

new LeaderSelector(clientFramework, path, new NoOpLeaderSelectorListener())
  .getLeader()

Differences between first and second is if there are no nodes, then dummy participant is object is returned, whereas first query relatively transparently returns Optional.empty() if no nodes are there.

Upvotes: 0

Se ven
Se ven

Reputation: 405

I think the state of LeaderLatch(first, second) has changed when the third instance start.

leaderLatch.hasLeadership()

should be performed after enough instances(3th) started.

or registry a callback method when this happend.

implements interface LeaderLatchListener and override isLeader() or notLeader() is a good idea.

Upvotes: 2

Related Questions