Search in sources :

Example 1 with EurekaHttpClient

use of com.netflix.discovery.shared.transport.EurekaHttpClient in project eureka by Netflix.

the class RetryableEurekaHttpClient method execute.

@Override
protected <R> EurekaHttpResponse<R> execute(RequestExecutor<R> requestExecutor) {
    List<EurekaEndpoint> candidateHosts = null;
    int endpointIdx = 0;
    for (int retry = 0; retry < numberOfRetries; retry++) {
        EurekaHttpClient currentHttpClient = delegate.get();
        EurekaEndpoint currentEndpoint = null;
        if (currentHttpClient == null) {
            if (candidateHosts == null) {
                candidateHosts = getHostCandidates();
                if (candidateHosts.isEmpty()) {
                    throw new TransportException("There is no known eureka server; cluster server list is empty");
                }
            }
            if (endpointIdx >= candidateHosts.size()) {
                throw new TransportException("Cannot execute request on any known server");
            }
            currentEndpoint = candidateHosts.get(endpointIdx++);
            currentHttpClient = clientFactory.newClient(currentEndpoint);
        }
        try {
            EurekaHttpResponse<R> response = requestExecutor.execute(currentHttpClient);
            if (serverStatusEvaluator.accept(response.getStatusCode(), requestExecutor.getRequestType())) {
                delegate.set(currentHttpClient);
                if (retry > 0) {
                    logger.info("Request execution succeeded on retry #{}", retry);
                }
                return response;
            }
            logger.warn("Request execution failure with status code {}; retrying on another server if available", response.getStatusCode());
        } catch (Exception e) {
            // just log message as the underlying client should log the stacktrace
            logger.warn("Request execution failed with message: {}", e.getMessage());
        }
        // Connection error or 5xx from the server that must be retried on another server
        delegate.compareAndSet(currentHttpClient, null);
        if (currentEndpoint != null) {
            quarantineSet.add(currentEndpoint);
        }
    }
    throw new TransportException("Retry limit reached; giving up on completing the request");
}
Also used : EurekaHttpClient(com.netflix.discovery.shared.transport.EurekaHttpClient) TransportException(com.netflix.discovery.shared.transport.TransportException) EurekaEndpoint(com.netflix.discovery.shared.resolver.EurekaEndpoint) TransportException(com.netflix.discovery.shared.transport.TransportException) EurekaEndpoint(com.netflix.discovery.shared.resolver.EurekaEndpoint)

Example 2 with EurekaHttpClient

use of com.netflix.discovery.shared.transport.EurekaHttpClient in project eureka by Netflix.

the class SessionedEurekaHttpClient method execute.

@Override
protected <R> EurekaHttpResponse<R> execute(RequestExecutor<R> requestExecutor) {
    long now = System.currentTimeMillis();
    long delay = now - lastReconnectTimeStamp;
    if (delay >= currentSessionDurationMs) {
        logger.debug("Ending a session and starting anew");
        lastReconnectTimeStamp = now;
        currentSessionDurationMs = randomizeSessionDuration(sessionDurationMs);
        TransportUtils.shutdown(eurekaHttpClientRef.getAndSet(null));
    }
    EurekaHttpClient eurekaHttpClient = eurekaHttpClientRef.get();
    if (eurekaHttpClient == null) {
        eurekaHttpClient = TransportUtils.getOrSetAnotherClient(eurekaHttpClientRef, clientFactory.newClient());
    }
    return requestExecutor.execute(eurekaHttpClient);
}
Also used : EurekaHttpClient(com.netflix.discovery.shared.transport.EurekaHttpClient)

Example 3 with EurekaHttpClient

use of com.netflix.discovery.shared.transport.EurekaHttpClient in project eureka by Netflix.

the class EurekaHttpResolver method getClusterEndpoints.

@Override
public List<AwsEndpoint> getClusterEndpoints() {
    List<AwsEndpoint> result = new ArrayList<>();
    EurekaHttpClient client = null;
    try {
        client = clientFactory.newClient();
        EurekaHttpResponse<Applications> response = client.getVip(vipAddress);
        if (validResponse(response)) {
            Applications applications = response.getEntity();
            if (applications != null) {
                // filter out non-UP instances
                applications.shuffleInstances(true);
                List<InstanceInfo> validInstanceInfos = applications.getInstancesByVirtualHostName(vipAddress);
                for (InstanceInfo instanceInfo : validInstanceInfos) {
                    AwsEndpoint endpoint = ResolverUtils.instanceInfoToEndpoint(clientConfig, transportConfig, instanceInfo);
                    if (endpoint != null) {
                        result.add(endpoint);
                    }
                }
                logger.debug("Retrieved endpoint list {}", result);
                return result;
            }
        }
    } catch (Exception e) {
        logger.error("Error contacting server for endpoints with vipAddress:{}", vipAddress, e);
    } finally {
        if (client != null) {
            client.shutdown();
        }
    }
    logger.info("Returning empty endpoint list");
    return Collections.emptyList();
}
Also used : Applications(com.netflix.discovery.shared.Applications) EurekaHttpClient(com.netflix.discovery.shared.transport.EurekaHttpClient) RetryableEurekaHttpClient(com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 4 with EurekaHttpClient

use of com.netflix.discovery.shared.transport.EurekaHttpClient in project eureka by Netflix.

the class DiscoveryClient method scheduleServerEndpointTask.

private void scheduleServerEndpointTask(EurekaTransport eurekaTransport, AbstractDiscoveryClientOptionalArgs args) {
    Collection<?> additionalFilters = args == null ? Collections.emptyList() : args.additionalFilters;
    EurekaJerseyClient providedJerseyClient = args == null ? null : args.eurekaJerseyClient;
    TransportClientFactories argsTransportClientFactories = null;
    if (args != null && args.getTransportClientFactories() != null) {
        argsTransportClientFactories = args.getTransportClientFactories();
    }
    // Ignore the raw types warnings since the client filter interface changed between jersey 1/2
    @SuppressWarnings("rawtypes") TransportClientFactories transportClientFactories = argsTransportClientFactories == null ? new Jersey1TransportClientFactories() : argsTransportClientFactories;
    // If the transport factory was not supplied with args, assume they are using jersey 1 for passivity
    eurekaTransport.transportClientFactory = providedJerseyClient == null ? transportClientFactories.newTransportClientFactory(clientConfig, additionalFilters, applicationInfoManager.getInfo()) : transportClientFactories.newTransportClientFactory(additionalFilters, providedJerseyClient);
    ApplicationsResolver.ApplicationsSource applicationsSource = new ApplicationsResolver.ApplicationsSource() {

        @Override
        public Applications getApplications(int stalenessThreshold, TimeUnit timeUnit) {
            long thresholdInMs = TimeUnit.MILLISECONDS.convert(stalenessThreshold, timeUnit);
            long delay = getLastSuccessfulRegistryFetchTimePeriod();
            if (delay > thresholdInMs) {
                logger.info("Local registry is too stale for local lookup. Threshold:{}, actual:{}", thresholdInMs, delay);
                return null;
            } else {
                return localRegionApps.get();
            }
        }
    };
    eurekaTransport.bootstrapResolver = EurekaHttpClients.newBootstrapResolver(clientConfig, transportConfig, eurekaTransport.transportClientFactory, applicationInfoManager.getInfo(), applicationsSource);
    if (clientConfig.shouldRegisterWithEureka()) {
        EurekaHttpClientFactory newRegistrationClientFactory = null;
        EurekaHttpClient newRegistrationClient = null;
        try {
            newRegistrationClientFactory = EurekaHttpClients.registrationClientFactory(eurekaTransport.bootstrapResolver, eurekaTransport.transportClientFactory, transportConfig);
            newRegistrationClient = newRegistrationClientFactory.newClient();
        } catch (Exception e) {
            logger.warn("Transport initialization failure", e);
        }
        eurekaTransport.registrationClientFactory = newRegistrationClientFactory;
        eurekaTransport.registrationClient = newRegistrationClient;
    }
    // Configure new transport layer (candidate for injecting in the future)
    if (clientConfig.shouldFetchRegistry()) {
        EurekaHttpClientFactory newQueryClientFactory = null;
        EurekaHttpClient newQueryClient = null;
        try {
            newQueryClientFactory = EurekaHttpClients.queryClientFactory(eurekaTransport.bootstrapResolver, eurekaTransport.transportClientFactory, clientConfig, transportConfig, applicationInfoManager.getInfo(), applicationsSource);
            newQueryClient = newQueryClientFactory.newClient();
        } catch (Exception e) {
            logger.warn("Transport initialization failure", e);
        }
        eurekaTransport.queryClientFactory = newQueryClientFactory;
        eurekaTransport.queryClient = newQueryClient;
    }
}
Also used : ApplicationsResolver(com.netflix.discovery.shared.resolver.aws.ApplicationsResolver) EurekaHttpClientFactory(com.netflix.discovery.shared.transport.EurekaHttpClientFactory) EurekaJerseyClient(com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient) Jersey1TransportClientFactories(com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories) EurekaHttpClient(com.netflix.discovery.shared.transport.EurekaHttpClient) Jersey1TransportClientFactories(com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories) TransportClientFactories(com.netflix.discovery.shared.transport.jersey.TransportClientFactories) TimeUnit(java.util.concurrent.TimeUnit)

Example 5 with EurekaHttpClient

use of com.netflix.discovery.shared.transport.EurekaHttpClient in project eureka by Netflix.

the class RedirectingEurekaHttpClient method execute.

@Override
protected <R> EurekaHttpResponse<R> execute(RequestExecutor<R> requestExecutor) {
    EurekaHttpClient currentEurekaClient = delegateRef.get();
    if (currentEurekaClient == null) {
        AtomicReference<EurekaHttpClient> currentEurekaClientRef = new AtomicReference<>(factory.newClient(serviceEndpoint));
        try {
            EurekaHttpResponse<R> response = executeOnNewServer(requestExecutor, currentEurekaClientRef);
            TransportUtils.shutdown(delegateRef.getAndSet(currentEurekaClientRef.get()));
            return response;
        } catch (Exception e) {
            logger.error("Request execution error", e);
            TransportUtils.shutdown(currentEurekaClientRef.get());
            throw e;
        }
    } else {
        try {
            return requestExecutor.execute(currentEurekaClient);
        } catch (Exception e) {
            logger.error("Request execution error", e);
            delegateRef.compareAndSet(currentEurekaClient, null);
            currentEurekaClient.shutdown();
            throw e;
        }
    }
}
Also used : EurekaHttpClient(com.netflix.discovery.shared.transport.EurekaHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportException(com.netflix.discovery.shared.transport.TransportException)

Aggregations

EurekaHttpClient (com.netflix.discovery.shared.transport.EurekaHttpClient)5 TransportException (com.netflix.discovery.shared.transport.TransportException)2 InstanceInfo (com.netflix.appinfo.InstanceInfo)1 Applications (com.netflix.discovery.shared.Applications)1 EurekaEndpoint (com.netflix.discovery.shared.resolver.EurekaEndpoint)1 ApplicationsResolver (com.netflix.discovery.shared.resolver.aws.ApplicationsResolver)1 EurekaHttpClientFactory (com.netflix.discovery.shared.transport.EurekaHttpClientFactory)1 RetryableEurekaHttpClient (com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient)1 EurekaJerseyClient (com.netflix.discovery.shared.transport.jersey.EurekaJerseyClient)1 Jersey1TransportClientFactories (com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories)1 TransportClientFactories (com.netflix.discovery.shared.transport.jersey.TransportClientFactories)1 ArrayList (java.util.ArrayList)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1