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;
}
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启动完毕.");
}
}
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("退出选举 完毕");
}
}
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);
}
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());
}
Aggregations