/** * It's possible an Oracle has gone into a bad state. Upon the leader being changed, we want to * update our state */ @Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) { Participant participant = leaderSelector.getLeader(); synchronized (this) { if (isLeader(participant)) { currentLeader = leaderSelector.getLeader(); } else { currentLeader = null; } } } }
@Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { try { if (isConnected() && (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED))) { synchronized (this) { Participant participant = leaderSelector.getLeader(); if (isLeader(participant) && !leaderSelector.hasLeadership()) { // in case current instance becomes leader, we want to know who came before it. currentLeader = participant; } } } } catch (InterruptedException e) { log.warn("Oracle leadership watcher has been interrupted unexpectedly"); } }
public List<String> getParticipants() { try { List<Participant> participants = new ArrayList<Participant>(LEADER_SELECTOR.getParticipants()); List<String> results = new ArrayList<String>(); for (Participant part : participants) { results.add(part.getId()); } return results; } catch (Exception e) { LOG.error(e.getMessage(), e); return null; } }
/** Test starting and stopping a single instance of LeaderService. */ @Test public void testLifeCycle() throws Exception { ServiceTriggers triggers = new ServiceTriggers(); LeaderService leader = newLeaderService(1, TimeUnit.HOURS, supply(triggers.listenTo(new NopService()))); assertEquals("test-id", leader.getId()); assertFalse(leader.hasLeadership()); // Start trying to obtain leadership leader.startAsync(); assertTrue(triggers.getRunning().firedWithin(1, TimeUnit.MINUTES)); assertTrue(leader.isRunning()); assertTrue(leader.hasLeadership()); assertEquals(new Participant("test-id", true), leader.getLeader()); assertEquals(Collections.singletonList(new Participant("test-id", true)), leader.getParticipants()); assertFalse(triggers.getTerminated().hasFired()); assertTrue(leader.getCurrentDelegateService().get().isRunning()); // Start watching ZooKeeper directly for changes WatchTrigger childrenTrigger = WatchTrigger.childrenTrigger(); _curator.getChildren().usingWatcher(childrenTrigger).forPath(PATH); // Stop trying to obtain leadership leader.stopAsync(); assertTrue(triggers.getTerminated().firedWithin(1, TimeUnit.SECONDS)); assertFalse(leader.isRunning()); assertFalse(leader.getCurrentDelegateService().isPresent()); // Wait for stopped state to reflect in ZooKeeper then poll ZooKeeper for leadership participants state assertTrue(childrenTrigger.firedWithin(1, TimeUnit.SECONDS)); assertFalse(leader.hasLeadership()); assertTrue(_curator.getChildren().forPath(PATH).isEmpty()); assertEquals(new Participant("", false), leader.getLeader()); assertEquals(Collections.<Participant>emptyList(), leader.getParticipants()); }
public Participant currentLeader() throws Exception{ return leaderLatch.getLeader(); }
public Participant currentLeader() throws Exception { return leaderLatch.getLeader(); }
private boolean isLeader(Participant participant) { return participant != null && participant.isLeader(); }
/** * @return The set of current participants in the leader selection. * <p> * <B>NOTE</B> - this method polls the ZooKeeper server. Therefore it may return a value that does not match * {@link #hasLeadership()} as hasLeadership returns a cached value. */ public Collection<Participant> getParticipants() throws Exception { return _latch.getParticipants(); }
/** * @return The id for the current leader. If for some reason there is no current leader, a dummy participant * is returned. * <p> * <B>NOTE</B> - this method polls the ZooKeeper server. Therefore it may return a value that does not match * {@link #hasLeadership()} as hasLeadership returns a cached value. */ public Participant getLeader() throws Exception { return _latch.getLeader(); }