Search in sources :

Example 1 with JoinException

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();
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JoinException(com.twitter.common.zookeeper.Group.JoinException) ExceptionalCommand(com.twitter.common.base.ExceptionalCommand) Command(com.twitter.common.base.Command) GroupChangeListener(com.twitter.common.zookeeper.Group.GroupChangeListener) Membership(com.twitter.common.zookeeper.Group.Membership) Supplier(com.google.common.base.Supplier)

Example 2 with JoinException

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);
        }
    });
}
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 3 with JoinException

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();
}
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)

Example 4 with JoinException

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;
}
Also used : Partition(com.twitter.common.zookeeper.Partitioner.Partition) TearDown(com.google.common.testing.TearDown) JoinException(com.twitter.common.zookeeper.Group.JoinException)

Aggregations

JoinException (com.twitter.common.zookeeper.Group.JoinException)4 Leader (com.twitter.common.zookeeper.Candidate.Leader)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Supplier (com.google.common.base.Supplier)1 TearDown (com.google.common.testing.TearDown)1 Command (com.twitter.common.base.Command)1 ExceptionalCommand (com.twitter.common.base.ExceptionalCommand)1 EasyMockTest.createCapture (com.twitter.common.testing.easymock.EasyMockTest.createCapture)1 GroupChangeListener (com.twitter.common.zookeeper.Group.GroupChangeListener)1 Membership (com.twitter.common.zookeeper.Group.Membership)1 Partition (com.twitter.common.zookeeper.Partitioner.Partition)1 EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)1 DefeatOnDisconnectLeader (com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader)1 LeaderControl (com.twitter.common.zookeeper.SingletonService.LeaderControl)1 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)1 IOException (java.io.IOException)1 Capture (org.easymock.Capture)1 Test (org.junit.Test)1