Search in sources :

Example 11 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project xian by happyyangyuan.

the class ZkLeaderElection method isLeader.

public static boolean isLeader() {
    LeaderSelector leaderSelector = singleton.leaderSelector;
    if (leaderSelector != null) {
        return leaderSelector.hasLeadership();
    }
    LOG.warn("当前节点根本就没有参与选举,你为何要检查它是否是leader呢?", new Throwable());
    return false;
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 12 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project xian by happyyangyuan.

the class ZkLeaderElection method start.

/**
 * 启动主节点选举
 */
public static void start() {
    synchronized (lock) {
        if (singleton != null)
            return;
        ZkLeaderElection leaderElection = new ZkLeaderElection();
        LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {

            public void takeLeadership(CuratorFramework client) throws InterruptedException {
                LOG.info("被提升为主节点!");
                try {
                    Thread.sleep(Long.MAX_VALUE);
                } catch (InterruptedException e) {
                    LOG.info("节点主动断开 or  主动让出主节点身份?反正此处是预期的打断!不需要打印堆栈");
                } finally {
                    LOG.info("主节点降级为普通节点!");
                }
            }
        };
        leaderElection.leaderSelector = new LeaderSelector(ZkConnection.client, ZkPathManager.getMyNodeBasePath(), listener);
        // not required, but this is behavior that you will probably expect
        leaderElection.leaderSelector.autoRequeue();
        leaderElection.leaderSelector.start();
        singleton = leaderElection;
        LOG.info("ZkLeaderElection启动完毕.");
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)

Example 13 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project xian by happyyangyuan.

the class ZkLeaderElection method stop.

/**
 * 如果参与了选举,那么退出主节点选举
 *
 * @deprecated curator的选举算法有问题,在最后一个唯一节点,同时也是主节点退出选举时,它抛出java.lang.InterruptedException。
 * 所以请直接依赖zk断开连接的方式退出节点选举,而不是调用本方法来退出选举
 */
public static void stop() {
    synchronized (lock) {
        if (singleton == null)
            return;
        LeaderSelector leaderSelector = singleton.leaderSelector;
        if (leaderSelector == null) {
            return;
        }
        LOG.info("节点退出zk选举");
        leaderSelector.close();
        singleton = null;
        LOG.info("退出选举 完毕");
    }
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 14 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project Mycat-Server by MyCATApache.

the class DistributedSequenceHandler method initializeZK.

public void initializeZK(String zkAddress) {
    this.client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
    this.client.start();
    try {
        if (client.checkExists().forPath(PATH.concat(INSTANCE_PATH)) == null) {
            client.create().creatingParentContainersIfNeeded().forPath(PATH.concat(INSTANCE_PATH));
        }
    } catch (Exception e) {
    // do nothing
    }
    this.leaderSelector = new LeaderSelector(client, PATH.concat(LEADER_PATH), this);
    this.leaderSelector.autoRequeue();
    this.leaderSelector.start();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                while (leaderSelector.getLeader() == null) {
                    Thread.currentThread().yield();
                }
                if (!leaderSelector.hasLeadership()) {
                    isLeader = false;
                    if (slavePath != null && client.checkExists().forPath(slavePath) != null) {
                        return;
                    }
                    slavePath = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(PATH.concat("/instance/node"), "ready".getBytes());
                    while ("ready".equals(new String(client.getData().forPath(slavePath)))) {
                        Thread.currentThread().yield();
                    }
                    instanceId = Long.parseLong(new String(client.getData().forPath(slavePath)));
                    ready = true;
                }
            } catch (Exception e) {
                LOGGER.warn("Caught exception while handling zk!", e);
            }
        }
    };
    timerExecutor.scheduleAtFixedRate(runnable, 1L, 10L, TimeUnit.SECONDS);
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) IOException(java.io.IOException) CancelLeadershipException(org.apache.curator.framework.recipes.leader.CancelLeadershipException)

Example 15 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project eventuate-local by eventuate-local.

the class MyLeaderSelectorListener method shouldElectLeader.

@Test
public void shouldElectLeader() throws InterruptedException {
    CuratorFramework client1 = EventTableChangesToAggregateTopicRelayConfiguration.makeStartedCuratorClient(eventuateLocalZookeperConfigurationProperties.getConnectionString());
    CuratorFramework client2 = EventTableChangesToAggregateTopicRelayConfiguration.makeStartedCuratorClient(eventuateLocalZookeperConfigurationProperties.getConnectionString());
    // String leaderPath = "/eventuatelocal/cdc/testleader";
    String leaderPath = "/foo";
    MyLeaderSelectorListener myLeaderSelectorListener1 = new MyLeaderSelectorListener("1");
    LeaderSelector leaderSelector1 = new LeaderSelector(client1, leaderPath, myLeaderSelectorListener1);
    leaderSelector1.start();
    MyLeaderSelectorListener myLeaderSelectorListener2 = new MyLeaderSelectorListener("2");
    LeaderSelector leaderSelector2 = new LeaderSelector(client2, leaderPath, myLeaderSelectorListener2);
    leaderSelector2.start();
    TimeUnit.SECONDS.sleep(5);
    assertEquals(1L, MyLeaderSelectorListener.leaderCounter.get());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

LeaderSelector (org.apache.curator.framework.recipes.leader.LeaderSelector)24 CuratorFramework (org.apache.curator.framework.CuratorFramework)7 LeaderSelectorListener (org.apache.curator.framework.recipes.leader.LeaderSelectorListener)6 IOException (java.io.IOException)5 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)4 CancelLeadershipException (org.apache.curator.framework.recipes.leader.CancelLeadershipException)3 LeaderSelectorListenerAdapter (org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)3 Test (org.junit.Test)3 Timing (org.apache.curator.test.Timing)2 KeeperException (org.apache.zookeeper.KeeperException)2 Test (org.testng.annotations.Test)2 BaseTest (com.baeldung.apache.curator.BaseTest)1 DrUtil (com.emc.storageos.coordinator.client.service.DrUtil)1 LeaderSelectorListenerImpl (com.emc.storageos.coordinator.client.service.impl.LeaderSelectorListenerImpl)1 ReaperLeaderSelectorListener (com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1