use of com.twitter.common.zookeeper.Candidate.Leader 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.Candidate.Leader in project commons by twitter.
the class SingletonServiceTest method testLeadMulti.
@Test
public void testLeadMulti() throws Exception {
List<Capture<Leader>> leaderCaptures = Lists.newArrayList();
List<Capture<LeaderControl>> leaderControlCaptures = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
Capture<Leader> leaderCapture = new Capture<Leader>();
leaderCaptures.add(leaderCapture);
Capture<LeaderControl> controlCapture = createCapture();
leaderControlCaptures.add(controlCapture);
expect(candidate.offerLeadership(capture(leaderCapture))).andReturn(null);
listener.onLeading(capture(controlCapture));
InetSocketAddress primary = InetSocketAddress.createUnresolved("foo" + i, PORT_A);
Map<String, InetSocketAddress> aux = ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo" + i, PORT_B));
expect(serverSet.join(primary, aux)).andReturn(endpointStatus);
endpointStatus.leave();
abdicate.execute();
}
control.replay();
for (int i = 0; i < 5; i++) {
final String leaderName = "foo" + i;
newLeader(leaderName, leaderCaptures.get(i));
leaderControlCaptures.get(i).getValue().advertise();
leaderControlCaptures.get(i).getValue().leave();
}
}
use of com.twitter.common.zookeeper.Candidate.Leader 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();
}
Aggregations