Exemplo n.º 1
0
  public void handleView(View view) {
    Address oldCoord = coord;
    if (view.size() > 0) {
      coord = view.getMembers().iterator().next();
      is_coord = coord.equals(local_addr);
      if (log.isDebugEnabled())
        log.debug("local_addr=" + local_addr + ", coord=" + coord + ", is_coord=" + is_coord);
    }

    // If we got a new coordinator we have to send all the requests for
    // tasks and consumers again just incase they were missed when
    // coordinator went down
    // We are okay with duplicates since we don't add multiple times.
    // We also have a problem that if a task/consumer was picked up as the
    // consumer is changing we may have duplicates.  But this is technically
    // okay in that an extra consumer will reject and an extra task will just
    // be ran and return nowhere, but at least we won't lose data.
    if (oldCoord != coord) {
      for (Long requests : _requestId.values()) {
        sendToCoordinator(Type.RUN_REQUEST, requests, local_addr);
      }

      for (Long requests : _consumerId.keySet()) {
        sendToCoordinator(Type.CONSUMER_READY, requests, local_addr);
      }
    }

    if (num_backups > 0) {
      if (is_coord) {
        List<Address> new_backups = Util.pickNext(view.getMembers(), local_addr, num_backups);
        List<Address> new_members = null;
        synchronized (backups) {
          if (!backups.equals(new_backups)) {
            new_members = new ArrayList<Address>(new_backups);
            new_members.removeAll(backups);
            backups.clear();
            backups.addAll(new_backups);
          }
        }

        if (new_members != null && !new_members.isEmpty()) copyQueueTo(new_members);
      }
      // We keep what backups we have ourselves, so that when we become
      // the coordinator we don't update them again.  Technically we can
      // send multiple requests but don't if to prevent more message being
      // sent.
      else {
        List<Address> possiblebackups = Util.pickNext(view.getMembers(), coord, num_backups);

        boolean foundMyself = false;
        List<Address> myBackups = new ArrayList<Address>();
        for (Address backup : possiblebackups) {
          if (foundMyself) {
            myBackups.add(backup);
          } else if (backup.equals(local_addr)) {
            foundMyself = true;
          }
        }

        synchronized (backups) {
          backups.clear();
          backups.addAll(myBackups);
        }
      }
    }

    // Need to run this last so the backups are updated
    super.handleView(view);
  }