Search in sources :

Example 6 with Stopwatch

use of com.netflix.servo.monitor.Stopwatch in project eureka by Netflix.

the class MetricsCollectingEurekaHttpClient method execute.

@Override
protected <R> EurekaHttpResponse<R> execute(RequestExecutor<R> requestExecutor) {
    EurekaHttpClientRequestMetrics requestMetrics = metricsByRequestType.get(requestExecutor.getRequestType());
    Stopwatch stopwatch = requestMetrics.latencyTimer.start();
    try {
        EurekaHttpResponse<R> httpResponse = requestExecutor.execute(delegate);
        requestMetrics.countersByStatus.get(mappedStatus(httpResponse)).increment();
        return httpResponse;
    } catch (Exception e) {
        requestMetrics.connectionErrors.increment();
        exceptionsMetric.count(e);
        throw e;
    } finally {
        stopwatch.stop();
    }
}
Also used : Stopwatch(com.netflix.servo.monitor.Stopwatch)

Example 7 with Stopwatch

use of com.netflix.servo.monitor.Stopwatch in project eureka by Netflix.

the class RemoteRegionRegistry method fetchRegistry.

/**
     * Fetch the registry information from the remote region.
     * @return true, if the fetch was successful, false otherwise.
     */
private boolean fetchRegistry() {
    boolean success;
    Stopwatch tracer = fetchRegistryTimer.start();
    try {
        // If the delta is disabled or if it is the first time, get all applications
        if (serverConfig.shouldDisableDeltaForRemoteRegions() || (getApplications() == null) || (getApplications().getRegisteredApplications().size() == 0)) {
            logger.info("Disable delta property : {}", serverConfig.shouldDisableDeltaForRemoteRegions());
            logger.info("Application is null : {}", getApplications() == null);
            logger.info("Registered Applications size is zero : {}", getApplications().getRegisteredApplications().isEmpty());
            success = storeFullRegistry();
        } else {
            success = fetchAndStoreDelta();
        }
        logTotalInstances();
    } catch (Throwable e) {
        logger.error("Unable to fetch registry information from the remote registry " + this.remoteRegionURL.toString(), e);
        return false;
    } finally {
        if (tracer != null) {
            tracer.stop();
        }
    }
    return success;
}
Also used : Stopwatch(com.netflix.servo.monitor.Stopwatch)

Example 8 with Stopwatch

use of com.netflix.servo.monitor.Stopwatch in project ribbon by Netflix.

the class LoadBalancerCommand method submit.

/**
     * Create an {@link Observable} that once subscribed execute network call asynchronously with a server chosen by load balancer.
     * If there are any errors that are indicated as retriable by the {@link RetryHandler}, they will be consumed internally by the
     * function and will not be observed by the {@link Observer} subscribed to the returned {@link Observable}. If number of retries has
     * exceeds the maximal allowed, a final error will be emitted by the returned {@link Observable}. Otherwise, the first successful
     * result during execution and retries will be emitted.
     */
public Observable<T> submit(final ServerOperation<T> operation) {
    final ExecutionInfoContext context = new ExecutionInfoContext();
    if (listenerInvoker != null) {
        try {
            listenerInvoker.onExecutionStart();
        } catch (AbortExecutionException e) {
            return Observable.error(e);
        }
    }
    final int maxRetrysSame = retryHandler.getMaxRetriesOnSameServer();
    final int maxRetrysNext = retryHandler.getMaxRetriesOnNextServer();
    // Use the load balancer
    Observable<T> o = (server == null ? selectServer() : Observable.just(server)).concatMap(new Func1<Server, Observable<T>>() {

        @Override
        public // Called for each server being selected
        Observable<T> call(Server server) {
            context.setServer(server);
            final ServerStats stats = loadBalancerContext.getServerStats(server);
            // Called for each attempt and retry
            Observable<T> o = Observable.just(server).concatMap(new Func1<Server, Observable<T>>() {

                @Override
                public Observable<T> call(final Server server) {
                    context.incAttemptCount();
                    loadBalancerContext.noteOpenConnection(stats);
                    if (listenerInvoker != null) {
                        try {
                            listenerInvoker.onStartWithServer(context.toExecutionInfo());
                        } catch (AbortExecutionException e) {
                            return Observable.error(e);
                        }
                    }
                    final Stopwatch tracer = loadBalancerContext.getExecuteTracer().start();
                    return operation.call(server).doOnEach(new Observer<T>() {

                        private T entity;

                        @Override
                        public void onCompleted() {
                            recordStats(tracer, stats, entity, null);
                        // TODO: What to do if onNext or onError are never called?
                        }

                        @Override
                        public void onError(Throwable e) {
                            recordStats(tracer, stats, null, e);
                            logger.debug("Got error {} when executed on server {}", e, server);
                            if (listenerInvoker != null) {
                                listenerInvoker.onExceptionWithServer(e, context.toExecutionInfo());
                            }
                        }

                        @Override
                        public void onNext(T entity) {
                            this.entity = entity;
                            if (listenerInvoker != null) {
                                listenerInvoker.onExecutionSuccess(entity, context.toExecutionInfo());
                            }
                        }

                        private void recordStats(Stopwatch tracer, ServerStats stats, Object entity, Throwable exception) {
                            tracer.stop();
                            loadBalancerContext.noteRequestCompletion(stats, entity, exception, tracer.getDuration(TimeUnit.MILLISECONDS), retryHandler);
                        }
                    });
                }
            });
            if (maxRetrysSame > 0)
                o = o.retry(retryPolicy(maxRetrysSame, true));
            return o;
        }
    });
    if (maxRetrysNext > 0 && server == null)
        o = o.retry(retryPolicy(maxRetrysNext, false));
    return o.onErrorResumeNext(new Func1<Throwable, Observable<T>>() {

        @Override
        public Observable<T> call(Throwable e) {
            if (context.getAttemptCount() > 0) {
                if (maxRetrysNext > 0 && context.getServerAttemptCount() == (maxRetrysNext + 1)) {
                    e = new ClientException(ClientException.ErrorType.NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED, "Number of retries on next server exceeded max " + maxRetrysNext + " retries, while making a call for: " + context.getServer(), e);
                } else if (maxRetrysSame > 0 && context.getAttemptCount() == (maxRetrysSame + 1)) {
                    e = new ClientException(ClientException.ErrorType.NUMBEROF_RETRIES_EXEEDED, "Number of retries exceeded max " + maxRetrysSame + " retries, while making a call for: " + context.getServer(), e);
                }
            }
            if (listenerInvoker != null) {
                listenerInvoker.onExecutionFailed(e, context.toFinalExecutionInfo());
            }
            return Observable.error(e);
        }
    });
}
Also used : Server(com.netflix.loadbalancer.Server) Stopwatch(com.netflix.servo.monitor.Stopwatch) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) Observable(rx.Observable) ServerStats(com.netflix.loadbalancer.ServerStats) ClientException(com.netflix.client.ClientException) Func1(rx.functions.Func1)

Example 9 with Stopwatch

use of com.netflix.servo.monitor.Stopwatch in project ribbon by Netflix.

the class PrimeConnections method primeConnections.

/**
     * 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);
}
Also used : Server(com.netflix.loadbalancer.Server) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stopwatch(com.netflix.servo.monitor.Stopwatch) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Stopwatch (com.netflix.servo.monitor.Stopwatch)9 Server (com.netflix.loadbalancer.Server)2 ClientException (com.netflix.client.ClientException)1 Applications (com.netflix.discovery.shared.Applications)1 ServerStats (com.netflix.loadbalancer.ServerStats)1 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1