Java Code Examples for com.netflix.loadbalancer.Server#setReadyToServe()

The following examples show how to use com.netflix.loadbalancer.Server#setReadyToServe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConsulServerList.java    From dropwizard-consul with Apache License 2.0 6 votes vote down vote up
/**
 * Build a {@link Server} instance from a Consul {@link ServiceHealth} instance. If the service
 * has an address defined, use that as the server host, otherwise default to using the node
 * address.
 *
 * @param service Consul service health record
 * @return Ribbon Server instance
 */
private Server buildServer(final ServiceHealth service) {
  @Nullable final String scheme = service.getService().getMeta().get("scheme");
  final int port = service.getService().getPort();

  final String address;
  if (!Strings.isNullOrEmpty(service.getService().getAddress())) {
    address = service.getService().getAddress();
  } else {
    address = service.getNode().getAddress();
  }

  final Server server = new Server(scheme, address, port);
  server.setZone(service.getNode().getDatacenter().orElse(Server.UNKNOWN_ZONE));
  server.setReadyToServe(true);

  return server;
}
 
Example 2
Source File: PrimeConnections.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Prime connections, blocking until configured percentage (default is 100%) of target servers are primed 
 * or max time is reached.
 * 
 * @see CommonClientConfigKey#MinPrimeConnectionsRatio
 * @see CommonClientConfigKey#MaxTotalTimeToPrimeConnections
 * 
 */
public void primeConnections(List<Server> servers) {
    if (servers == null || servers.size() == 0) {
        logger.debug("No server to prime");
        return;
    }
    for (Server server: servers) {
        server.setReadyToServe(false);
    }
    int totalCount = (int) (servers.size() * primeRatio); 
    final CountDownLatch latch = new CountDownLatch(totalCount);
    final AtomicInteger successCount = new AtomicInteger(0);
    final AtomicInteger failureCount= new AtomicInteger(0);
    primeConnectionsAsync(servers, new PrimeConnectionListener()  {            
        @Override
        public void primeCompleted(Server s, Throwable lastException) {
            if (lastException == null) {
                successCount.incrementAndGet();
                s.setReadyToServe(true);
            } else {
                failureCount.incrementAndGet();
            }
            latch.countDown();
        }
    }); 
            
    Stopwatch stopWatch = initialPrimeTimer.start();
    try {
        latch.await(maxTotalTimeToPrimeConnections, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logger.error("Priming connection interrupted", e);
    } finally {
        stopWatch.stop();
    }

    stats = new PrimeConnectionEndStats(totalCount, successCount.get(), failureCount.get(), stopWatch.getDuration(TimeUnit.MILLISECONDS));

    printStats(stats);
}
 
Example 3
Source File: PrimeConnections.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Prime servers asynchronously.
 * 
 * @param servers
 * @param listener
 */
public List<Future<Boolean>> primeConnectionsAsync(final List<Server> servers, final PrimeConnectionListener listener) {
    if (servers == null) {
        return Collections.emptyList();
    }
    List<Server> allServers = new ArrayList<Server>();
    allServers.addAll(servers);
    if (allServers.size() == 0){
        logger.debug("RestClient:" + name + ". No nodes/servers to prime connections");
        return Collections.emptyList();
    }        

    logger.info("Priming Connections for RestClient:" + name
            + ", numServers:" + allServers.size());
    List<Future<Boolean>> ftList = new ArrayList<Future<Boolean>>();
    for (Server s : allServers) {
        // prevent the server to be used by load balancer
        // will be set to true when priming is done
        s.setReadyToServe(false);
        if (aSync) {
            Future<Boolean> ftC = null;
            try {
                ftC = makeConnectionASync(s, listener);
                ftList.add(ftC);
            }
            catch (RejectedExecutionException ree) {
                logger.error("executor submit failed", ree);
            }
            catch (Exception e) {
                logger.error("general error", e);
                // It does not really matter if there was an exception,
                // the goal here is to attempt "priming/opening" the route
                // in ec2 .. actual http results do not matter
            }
        } else {
            connectToServer(s, listener);
        }
    }   
    return ftList;
}