use of com.twitter.common.zookeeper.Group.JoinException in project commons by twitter.
the class CandidateImpl method offerLeadership.
@Override
public Supplier<Boolean> offerLeadership(final Leader leader) throws JoinException, WatchException, InterruptedException {
final Membership membership = group.join(dataSupplier, new Command() {
@Override
public void execute() {
leader.onDefeated();
}
});
final AtomicBoolean elected = new AtomicBoolean(false);
final AtomicBoolean abdicated = new AtomicBoolean(false);
group.watch(new GroupChangeListener() {
@Override
public void onGroupChange(Iterable<String> memberIds) {
boolean noCandidates = Iterables.isEmpty(memberIds);
String memberId = membership.getMemberId();
if (noCandidates) {
LOG.warning("All candidates have temporarily left the group: " + group);
} else if (!Iterables.contains(memberIds, memberId)) {
LOG.severe(String.format("Current member ID %s is not a candidate for leader, current voting: %s", memberId, memberIds));
} else {
boolean electedLeader = memberId.equals(getLeader(memberIds));
boolean previouslyElected = elected.getAndSet(electedLeader);
if (!previouslyElected && electedLeader) {
LOG.info(String.format("Candidate %s is now leader of group: %s", membership.getMemberPath(), memberIds));
leader.onElected(new ExceptionalCommand<JoinException>() {
@Override
public void execute() throws JoinException {
membership.cancel();
abdicated.set(true);
}
});
} else if (!electedLeader) {
if (previouslyElected) {
leader.onDefeated();
}
LOG.info(String.format("Candidate %s waiting for the next leader election, current voting: %s", membership.getMemberPath(), memberIds));
}
}
}
});
return new Supplier<Boolean>() {
@Override
public Boolean get() {
return !abdicated.get() && elected.get();
}
};
}
use of com.twitter.common.zookeeper.Group.JoinException in project commons by twitter.
the class SingletonService method lead.
/**
* Attempts to lead the singleton service.
*
* @param endpoint The primary endpoint to register as a leader candidate in the service.
* @param additionalEndpoints Additional endpoints that are available on the host.
* @param listener Handler to call when the candidate is elected or defeated.
* @throws Group.WatchException If there was a problem watching the ZooKeeper group.
* @throws Group.JoinException If there was a problem joining the ZooKeeper group.
* @throws InterruptedException If the thread watching/joining the group was interrupted.
*/
public void lead(final InetSocketAddress endpoint, final Map<String, InetSocketAddress> additionalEndpoints, final LeadershipListener listener) throws Group.WatchException, Group.JoinException, InterruptedException {
Preconditions.checkNotNull(listener);
candidate.offerLeadership(new Leader() {
private EndpointStatus endpointStatus = null;
@Override
public void onElected(final ExceptionalCommand<JoinException> abdicate) {
listener.onLeading(new LeaderControl() {
EndpointStatus endpointStatus = null;
final AtomicBoolean left = new AtomicBoolean(false);
// Methods are synchronized to prevent simultaneous invocations.
@Override
public synchronized void advertise() throws JoinException, InterruptedException {
Preconditions.checkState(!left.get(), "Cannot advertise after leaving.");
Preconditions.checkState(endpointStatus == null, "Cannot advertise more than once.");
endpointStatus = serverSet.join(endpoint, additionalEndpoints);
}
@Override
public synchronized void leave() throws UpdateException, JoinException {
Preconditions.checkState(left.compareAndSet(false, true), "Cannot leave more than once.");
if (endpointStatus != null) {
endpointStatus.leave();
}
abdicate.execute();
}
});
}
@Override
public void onDefeated() {
listener.onDefeated(endpointStatus);
}
});
}
use of com.twitter.common.zookeeper.Group.JoinException in project commons by twitter.
the class SingletonServiceTest method testLeadJoinFailure.
@Test
public void testLeadJoinFailure() throws Exception {
Capture<Leader> leaderCapture = new Capture<Leader>();
expect(candidate.offerLeadership(capture(leaderCapture))).andReturn(null);
Capture<LeaderControl> controlCapture = createCapture();
listener.onLeading(capture(controlCapture));
expectJoin().andThrow(new Group.JoinException("Injected join failure.", new Exception()));
abdicate.execute();
control.replay();
newLeader("foo", leaderCapture);
try {
controlCapture.getValue().advertise();
fail("Join should have failed.");
} catch (JoinException e) {
// Expected.
}
controlCapture.getValue().leave();
}
use of com.twitter.common.zookeeper.Group.JoinException in project commons by twitter.
the class PartitionerTest method join.
private Partition join(Partitioner partitioner) throws JoinException, InterruptedException {
final Partition partition = partitioner.join();
addTearDown(new TearDown() {
@Override
public void tearDown() throws JoinException {
partition.cancel();
}
});
return partition;
}
Aggregations