curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jordan Zimmerman <jor...@jordanzimmerman.com>
Subject Re: Zookeeper Leadership election with curator
Date Thu, 15 Jan 2015 22:38:41 GMT
A few things:

* You don’t need to create a new Curator instance for each Leader. One Curator instance
can be used for an application.
* Calling client.getZookeeperClient().blockUntilConnectedOrTimedOut(); is unnecessary
* Calling isLeader() immediately after starting the latch may not work. It takes time for
the leader to get selected. LeaderLatch has a listener if you want to get notified. Alternatively,
you can use LeaderSelector.

Hope this helps.

-Jordan



On January 15, 2015 at 5:25:26 PM, Check Peck (comptechgeeky@gmail.com) wrote:

I am using Zookeeper for the leadership election process. I have three nodes zookeeper ensemble.

We have a node like this "/test/msn" on which we are doing the leadership election and it
works fine and we have application servers running it in production and using "/test/msn"
node for leadership. And this program is always running.

Now we created another node like this "/testpnl" in the same zookeeper servers. And we have
another set of machines which is trying to do the leadership election on that node and whenever
I run my program in production from one servers, it always returns back that the node is not
leader? And I can see only one node inside "/testpnl" and it is the same node from which I
am running my program?

I am using Curator 2.7.0 and the zookeeper version is 3.4.5 Is there anything wrong with my
setup or zookeeper has some bug which I am not aware?

Is anything

    public class ZookeeperLeaderTester {

        private ZookeeperClient zookClient;

        private static final String ZOOK_PRODUCTION = "host1:2181,host2:2181,host3:2181";
        private static final String LEADERSHIP_NODE = "/testpnl";

        private static class Holder {
            static final ZookeeperLeaderTester INSTANCE = new ZookeeperLeaderTester();
        }

        public static ZookeeperLeaderTester getInstance() {
            return Holder.INSTANCE;
        }

        private ZookeeperLeaderTester() {
            try {
                String hostname = getHostName();

                zookClient = new ZookeeperClient(nodes, LEADERSHIP_NODE, hostname);
                zookClient.start();

            } catch (Exception ex) {
                System.out.println (ex);
                System.exit(1);
            }
        }

        public ZookeeperClient getZookClient() {
            return zookClient;
        }
    }
   
And here is my ZookeeperClient code -

    public class ZookeeperClient {

        private CuratorFramework client;
        private String latchPath;
        private String id;
        private LeaderLatch leaderLatch;

        public ZookeeperClient(String connString, String latchPath, String id) {
            client = CuratorFrameworkFactory.newClient(connString, new ExponentialBackoffRetry(1000,
Integer.MAX_VALUE));
            this.id = id;
            this.latchPath = latchPath;
        }

        public void start() throws Exception {
            client.start();
            client.getZookeeperClient().blockUntilConnectedOrTimedOut();
            leaderLatch = new LeaderLatch(client, latchPath, id);
            leaderLatch.start();
        }

        public boolean isLeader() {
            return leaderLatch.hasLeadership();
        }

        public Participant currentLeader() throws Exception {
            return leaderLatch.getLeader();
        }

        public void close() throws IOException {
            leaderLatch.close();
            client.close();
        }

        public CuratorFramework getClient() {
            return client;
        }

        public String getLatchPath() {
            return latchPath;
        }

        public String getId() {
            return id;
        }

        public LeaderLatch getLeaderLatch() {
            return leaderLatch;
        }
    }
   
And this is the way I am using it -

    ZookeeperLeaderTester zookClient = ZookeeperLeaderTester.getInstance().getZookClient();
    if (zookClient.isLeader()) {
        // then do stuff
    }

Mime
View raw message