public void markServerDown(String id) {
    boolean triggered = false;

    id = Server.normalizeId(id);
    if (id == null) {
      return;
    }

    Lock writeLock = upServerLock.writeLock();
    try {
      writeLock.lock();

      final List<Server> changedServers = new ArrayList<Server>();

      for (Server svr : upServerList) {
        if (svr.isAlive() && (svr.getId().equals(id))) {
          triggered = true;
          svr.setAlive(false);
          changedServers.add(svr);
        }
      }

      if (triggered) {
        logger.error("LoadBalancer:  markServerDown called on [" + id + "]");
        notifyServerStatusChangeListener(changedServers);
      }
    } finally {
      try {
        writeLock.unlock();
      } catch (Exception e) { // NOPMD
      }
    }
  }
  @Override
  public void markServerDown(Server server) {
    if (server == null) {
      return;
    }

    if (!server.isAlive()) {
      return;
    }

    logger.error("LoadBalancer:  markServerDown called on [" + server.getId() + "]");
    server.setAlive(false);
    // forceQuickPing();

    notifyServerStatusChangeListener(singleton(server));
  }
Пример #3
0
  public Server choose(ILoadBalancer lb, Object key) {
    if (lb == null) {
      log.warn("no load balancer");
      return null;
    }
    Server server = null;
    int index = 0;

    int count = 0;
    while (server == null && count++ < 10) {
      List<Server> upList = lb.getServerList(true);
      List<Server> allList = lb.getServerList(false);
      int upCount = upList.size();
      int serverCount = allList.size();

      if ((upCount == 0) || (serverCount == 0)) {
        log.warn("No up servers available from load balancer: " + lb);
        return null;
      }

      index = nextIndexAI.incrementAndGet() % serverCount;
      server = allList.get(index);

      if (server == null) {
        /* Transient. */
        Thread.yield();
        continue;
      }

      if (server.isAlive() && (server.isReadyToServe())) {
        return (server);
      }

      // Next.
      server = null;
    }

    if (count >= 10) {
      log.warn("No available alive servers after 10 tries from load balancer: " + lb);
    }
    return server;
  }
    public void runPinger() {
      if (pingInProgress.get()) {
        return;
      } else {
        pingInProgress.set(true);
      }

      Object[] allServers = null;
      boolean[] results = null;

      Lock allLock = null;
      Lock upLock = null;

      try {
        allLock = allServerLock.readLock();
        allLock.lock();
        allServers = allServerList.toArray();
        allLock.unlock();

        int numCandidates = allServers.length;
        results = new boolean[numCandidates];

        if (logger.isDebugEnabled()) {
          logger.debug(
              "LoadBalancer:  PingTask executing [" + numCandidates + "] servers configured");
        }

        for (int i = 0; i < numCandidates; i++) {
          results[i] = false;
          try {
            if (ping != null) {
              results[i] = ping.isAlive((Server) allServers[i]);
            }
          } catch (Throwable t) {
            logger.error("Exception while pinging Server:" + allServers[i], t);
          }
        }

        final List<Server> newUpList = new ArrayList<Server>();
        final List<Server> changedServers = new ArrayList<Server>();

        for (int i = 0; i < numCandidates; i++) {
          boolean isAlive = results[i];
          Server svr = (Server) allServers[i];
          boolean oldIsAlive = svr.isAlive();

          svr.setAlive(isAlive);

          if (oldIsAlive != isAlive) {
            changedServers.add(svr);
            if (logger.isDebugEnabled()) {
              logger.debug(
                  "LoadBalancer:  Server ["
                      + svr.getId()
                      + "] status changed to "
                      + (isAlive ? "ALIVE" : "DEAD"));
            }
          }

          if (isAlive) {
            newUpList.add(svr);
          }
        }
        upLock = upServerLock.writeLock();
        upLock.lock();
        upServerList = newUpList;
        upLock.unlock();

        notifyServerStatusChangeListener(changedServers);

      } catch (Throwable t) {
        logger.error("Throwable caught while running the Pinger-" + name, t);
      } finally {
        pingInProgress.set(false);
      }
    }