Search in sources :

Example 1 with Leader

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);
        }
    });
}
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 2 with Leader

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();
    }
}
Also used : DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) Leader(com.twitter.common.zookeeper.Candidate.Leader) InetSocketAddress(java.net.InetSocketAddress) LeaderControl(com.twitter.common.zookeeper.SingletonService.LeaderControl) Capture(org.easymock.Capture) EasyMockTest.createCapture(com.twitter.common.testing.easymock.EasyMockTest.createCapture) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 3 with Leader

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();
}
Also used : JoinException(com.twitter.common.zookeeper.Group.JoinException) JoinException(com.twitter.common.zookeeper.Group.JoinException) DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) Leader(com.twitter.common.zookeeper.Candidate.Leader) LeaderControl(com.twitter.common.zookeeper.SingletonService.LeaderControl) Capture(org.easymock.Capture) EasyMockTest.createCapture(com.twitter.common.testing.easymock.EasyMockTest.createCapture) JoinException(com.twitter.common.zookeeper.Group.JoinException) IOException(java.io.IOException) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Aggregations

Leader (com.twitter.common.zookeeper.Candidate.Leader)3 EasyMockTest.createCapture (com.twitter.common.testing.easymock.EasyMockTest.createCapture)2 JoinException (com.twitter.common.zookeeper.Group.JoinException)2 DefeatOnDisconnectLeader (com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader)2 LeaderControl (com.twitter.common.zookeeper.SingletonService.LeaderControl)2 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)2 Capture (org.easymock.Capture)2 Test (org.junit.Test)2 EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1