示例#1
0
        @Override
        public void handle(CoordinatorMessage event) {
          System.err.println(
              "[ELECTION::"
                  + self.getPeerId()
                  + "] I got a coordinator message from "
                  + event.getPeerSource().getPeerId()
                  + " the leader is "
                  + event.getLeader().getPeerId()
                  + "!");
          CancelTimeout ct = new CancelTimeout(timeoutId);
          trigger(ct, timerPort);

          leader = event.getLeader();
          electionGroup = event.getElectionGroup();
          electing = false;

          if (self.getPeerId().compareTo(leader.getPeerId()) != 0) {
            ScheduleTimeout heartbeatTimeout = new ScheduleTimeout(JRConfig.HEARTBEAT_TIMEOUT);
            heartbeatTimeout.setTimeoutEvent(new HeartbeatTimeout(heartbeatTimeout));
            trigger(heartbeatTimeout, timerPort);
          }

          Stats.reportElectionMessages();
          Stats.reportLeader(leader);
        }
示例#2
0
  /**
   * Starts an election.
   *
   * <p>The election group is specified and a ThinkLeaderMessage is sent to every peer in that group
   * so they can start the bully algorithm.
   */
  private void startLeaderElection() {
    ArrayList<PeerAddress> initialElectionGroup = new ArrayList<PeerAddress>(tmanPartners);
    initialElectionGroup.add(self);
    System.err.println(
        "[ELECTION::" + self.getPeerId() + "] The election group is " + initialElectionGroup);
    for (PeerAddress peer : tmanPartners) {
      trigger(new ThinkLeaderMessage(self, peer, initialElectionGroup), networkPort);
    }

    Stats.clearElectionMessages();
    Stats.registerElectionMessages(tmanPartners.size());
  }
示例#3
0
        @Override
        public void handle(ThinkLeaderMessage event) {
          if (!electing) {
            electing = true;
            leader = null;
            ArrayList<PeerAddress> electionGroup = event.getElectionGroup();
            System.err.println(
                "[ELECTION::" + self.getPeerId() + "] I got a THINK_LEADER message!");
            if (self.getPeerId().equals(minimumUtility(electionGroup))) {
              System.err.println(
                  "[ELECTION::"
                      + self.getPeerId()
                      + "] I am eligible to start the election! ("
                      + minimumUtility(electionGroup)
                      + ")");
              for (PeerAddress peer : electionGroup) {
                if (!self.getPeerId().equals(peer.getPeerId())) {
                  trigger(new ElectionMessage(self, peer, electionGroup), networkPort);
                }
              }
              Stats.registerElectionMessages(electionGroup.size());

              ScheduleTimeout st = new ScheduleTimeout(JRConfig.BULLY_TIMEOUT);
              st.setTimeoutEvent(new ElectionTimeout(st, electionGroup));
              timeoutId = st.getTimeoutEvent().getTimeoutId();
              trigger(st, timerPort);
            }
          }
        }
示例#4
0
        @Override
        public void handle(LeaderDeadMessage event) {
          electing = true;
          if (tmanPartners.contains(leader)) {
            tmanPartners.remove(leader);
          }
          leader = null;

          CancelTimeout ct = new CancelTimeout(heartbeatTimeoutId);
          trigger(ct, timerPort);

          ArrayList<PeerAddress> electionGroup = event.getElectionGroup();
          System.err.println("[ELECTION::" + self.getPeerId() + "] I got a LEADER_DEAD message!");
          if (self.getPeerId().equals(minimumUtility(electionGroup))) {
            System.err.println(
                "[ELECTION::"
                    + self.getPeerId()
                    + "] I am eligible to start the election! ("
                    + minimumUtility(electionGroup)
                    + ")");
            for (PeerAddress peer : electionGroup) {
              if (!self.getPeerId().equals(peer.getPeerId())) {
                trigger(new ElectionMessage(self, peer, electionGroup), networkPort);
              }
            }
            Stats.registerElectionMessages(electionGroup.size());

            ScheduleTimeout st = new ScheduleTimeout(JRConfig.BULLY_TIMEOUT);
            st.setTimeoutEvent(new ElectionTimeout(st, electionGroup));
            timeoutId = st.getTimeoutEvent().getTimeoutId();
            trigger(st, timerPort);
          }
        }
示例#5
0
 @Override
 public void handle(ElectionTimeout event) {
   if (electing) {
     System.err.println("[ELECTION::" + self.getPeerId() + "] I got an election timeout!");
     ArrayList<PeerAddress> electionGroup = event.getElectionGroup();
     for (PeerAddress peer : electionGroup) {
       trigger(new CoordinatorMessage(self, peer, electionGroup), networkPort);
     }
     Stats.registerElectionMessages(electionGroup.size());
   }
 }
示例#6
0
        @Override
        public void handle(ElectionMessage event) {
          if (electing) {
            boolean sentElectionMessage = false;
            System.err.println(
                "[ELECTION::"
                    + self.getPeerId()
                    + "] I got an election message from "
                    + event.getPeerSource().getPeerId()
                    + "!");
            BigInteger sender = event.getPeerSource().getPeerId();
            if (sender.compareTo(self.getPeerId()) == -1) {
              trigger(
                  new OKMessage(self, event.getPeerSource(), event.getElectionGroup()),
                  networkPort);
              Stats.registerElectionMessage();
              ArrayList<PeerAddress> electionGroup = event.getElectionGroup();
              for (PeerAddress peer : electionGroup) {
                if (self.getPeerId().compareTo(peer.getPeerId()) == -1) {
                  sentElectionMessage = true;
                  trigger(new ElectionMessage(self, peer, electionGroup), networkPort);
                  Stats.registerElectionMessage();

                  ScheduleTimeout st = new ScheduleTimeout(JRConfig.BULLY_TIMEOUT);
                  st.setTimeoutEvent(new ElectionTimeout(st, electionGroup));
                  timeoutId = st.getTimeoutEvent().getTimeoutId();
                  trigger(st, timerPort);
                }
              }

              if (!sentElectionMessage) {
                for (PeerAddress peer : electionGroup) {
                  trigger(new CoordinatorMessage(self, peer, electionGroup), networkPort);
                }
                Stats.registerElectionMessages(electionGroup.size());
              }
            }
          }
        }
示例#7
0
        @Override
        public void handle(CoordinatorTimeout event) {
          if (electing) {
            System.err.println("[ELECTION::" + self.getPeerId() + "] I got a coordinator timeout!");
            ArrayList<PeerAddress> electionGroup = event.getElectionGroup();
            for (PeerAddress peer : electionGroup) {
              if (self.getPeerId().compareTo(peer.getPeerId()) == -1) {
                trigger(new ElectionMessage(self, peer, electionGroup), networkPort);
                Stats.registerElectionMessage();

                ScheduleTimeout st = new ScheduleTimeout(JRConfig.BULLY_TIMEOUT);
                st.setTimeoutEvent(new ElectionTimeout(st, electionGroup));
                timeoutId = st.getTimeoutEvent().getTimeoutId();
                trigger(st, timerPort);
              }
            }
          }
        }