use of com.twitter.common.zookeeper.ServerSet.EndpointStatus in project commons by twitter.
the class ServerSetImplTest method testMembershipChanges.
@Test
public void testMembershipChanges() throws Exception {
ServerSetImpl client = createServerSet();
client.watch(serverSetMonitor);
assertChangeFiredEmpty();
ServerSetImpl server = createServerSet();
EndpointStatus foo = join(server, "foo");
assertChangeFired("foo");
expireSession(client.getZkClient());
EndpointStatus bar = join(server, "bar");
// We should've auto re-monitored membership, but not been notifed of "foo" since this was not a
// change, just "foo", "bar" since this was an addition.
assertChangeFired("foo", "bar");
foo.leave();
assertChangeFired("bar");
EndpointStatus baz = join(server, "baz");
assertChangeFired("bar", "baz");
baz.leave();
assertChangeFired("bar");
bar.leave();
assertChangeFiredEmpty();
assertTrue(serverSetBuffer.isEmpty());
}
use of com.twitter.common.zookeeper.ServerSet.EndpointStatus in project commons by twitter.
the class ServerSetImplTest method testOrdering.
@Test
public void testOrdering() throws Exception {
ServerSetImpl client = createServerSet();
client.watch(serverSetMonitor);
assertChangeFiredEmpty();
Map<String, InetSocketAddress> server1Ports = makePortMap("http-admin1", 8080);
Map<String, InetSocketAddress> server2Ports = makePortMap("http-admin2", 8081);
Map<String, InetSocketAddress> server3Ports = makePortMap("http-admin3", 8082);
ServerSetImpl server1 = createServerSet();
ServerSetImpl server2 = createServerSet();
ServerSetImpl server3 = createServerSet();
ServiceInstance instance1 = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of("http-admin1", new Endpoint("foo", 8080)), Status.ALIVE).setShard(0);
ServiceInstance instance2 = new ServiceInstance(new Endpoint("foo", 1001), ImmutableMap.of("http-admin2", new Endpoint("foo", 8081)), Status.ALIVE).setShard(1);
ServiceInstance instance3 = new ServiceInstance(new Endpoint("foo", 1002), ImmutableMap.of("http-admin3", new Endpoint("foo", 8082)), Status.ALIVE).setShard(2);
server1.join(InetSocketAddress.createUnresolved("foo", 1000), server1Ports, 0);
assertEquals(ImmutableList.of(instance1), ImmutableList.copyOf(serverSetBuffer.take()));
EndpointStatus status2 = server2.join(InetSocketAddress.createUnresolved("foo", 1001), server2Ports, 1);
assertEquals(ImmutableList.of(instance1, instance2), ImmutableList.copyOf(serverSetBuffer.take()));
server3.join(InetSocketAddress.createUnresolved("foo", 1002), server3Ports, 2);
assertEquals(ImmutableList.of(instance1, instance2, instance3), ImmutableList.copyOf(serverSetBuffer.take()));
status2.leave();
assertEquals(ImmutableList.of(instance1, instance3), ImmutableList.copyOf(serverSetBuffer.take()));
}
use of com.twitter.common.zookeeper.ServerSet.EndpointStatus in project commons by twitter.
the class ServerSetImplTest method testLifecycle.
@Test
public void testLifecycle() throws Exception {
ServerSetImpl client = createServerSet();
client.watch(serverSetMonitor);
assertChangeFiredEmpty();
ServerSetImpl server = createServerSet();
EndpointStatus status = server.join(InetSocketAddress.createUnresolved("foo", 1234), makePortMap("http-admin", 8080), 0);
ServiceInstance serviceInstance = new ServiceInstance(new Endpoint("foo", 1234), ImmutableMap.of("http-admin", new Endpoint("foo", 8080)), Status.ALIVE).setShard(0);
assertChangeFired(serviceInstance);
status.leave();
assertChangeFiredEmpty();
assertTrue(serverSetBuffer.isEmpty());
}
use of com.twitter.common.zookeeper.ServerSet.EndpointStatus 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.ServerSet.EndpointStatus in project commons by twitter.
the class SingletonServiceTest method mySetUp.
@Before
@SuppressWarnings("unchecked")
public void mySetUp() throws IOException {
control = createControl();
addTearDown(new TearDown() {
@Override
public void tearDown() {
control.verify();
}
});
listener = control.createMock(SingletonService.LeadershipListener.class);
serverSet = control.createMock(ServerSet.class);
candidate = control.createMock(Candidate.class);
endpointStatus = control.createMock(ServerSet.EndpointStatus.class);
abdicate = control.createMock(ExceptionalCommand.class);
service = new SingletonService(serverSet, candidate);
}
Aggregations