Search in sources :

Example 1 with EndpointStatus

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());
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 2 with EndpointStatus

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()));
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) Endpoint(com.twitter.thrift.Endpoint) InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 3 with EndpointStatus

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());
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) Endpoint(com.twitter.thrift.Endpoint) ServiceInstance(com.twitter.thrift.ServiceInstance) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 4 with EndpointStatus

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);
        }
    });
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) JoinException(com.twitter.common.zookeeper.Group.JoinException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Leader(com.twitter.common.zookeeper.Candidate.Leader)

Example 5 with 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);
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) TearDown(com.google.common.testing.TearDown) ExceptionalCommand(com.twitter.common.base.ExceptionalCommand) LeadershipListener(com.twitter.common.zookeeper.SingletonService.LeadershipListener) Before(org.junit.Before)

Aggregations

EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)8 Test (org.junit.Test)6 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)5 ServiceInstance (com.twitter.thrift.ServiceInstance)3 Endpoint (com.twitter.thrift.Endpoint)2 InetSocketAddress (java.net.InetSocketAddress)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TearDown (com.google.common.testing.TearDown)1 Command (com.twitter.common.base.Command)1 ExceptionalCommand (com.twitter.common.base.ExceptionalCommand)1 Amount (com.twitter.common.quantity.Amount)1 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)1 TTransportConnection (com.twitter.common.thrift.TTransportConnection)1 MockTSocket (com.twitter.common.thrift.testing.MockTSocket)1 Leader (com.twitter.common.zookeeper.Candidate.Leader)1 JoinException (com.twitter.common.zookeeper.Group.JoinException)1 ServerSet (com.twitter.common.zookeeper.ServerSet)1 LeadershipListener (com.twitter.common.zookeeper.SingletonService.LeadershipListener)1 Map (java.util.Map)1 Set (java.util.Set)1