Search in sources :

Example 1 with ExceptionalCommand

use of com.twitter.common.base.ExceptionalCommand 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)

Aggregations

Supplier (com.google.common.base.Supplier)1 Command (com.twitter.common.base.Command)1 ExceptionalCommand (com.twitter.common.base.ExceptionalCommand)1 GroupChangeListener (com.twitter.common.zookeeper.Group.GroupChangeListener)1 JoinException (com.twitter.common.zookeeper.Group.JoinException)1 Membership (com.twitter.common.zookeeper.Group.Membership)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1