// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.config.provision.HostName;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.ApplicationController;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.MetricsService;
import com.yahoo.vespa.hosted.controller.application.ApplicationList;
import com.yahoo.vespa.hosted.controller.application.Deployment;
import com.yahoo.vespa.hosted.controller.application.DeploymentMetrics;
import com.yahoo.vespa.hosted.controller.application.RotationStatus;
import com.yahoo.yolean.Exceptions;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Retrieve deployment metrics such as QPS and document count from the metric service and
 * update applications with this info.
 *
 * @author smorgrav
 * @author mpolden
 */
public class DeploymentMetricsMaintainer extends Maintainer {

    private static final Logger log = Logger.getLogger(DeploymentMetricsMaintainer.class.getName());

    private static final int applicationsToUpdateInParallel = 10;

    private final ApplicationController applications;

    public DeploymentMetricsMaintainer(Controller controller, Duration duration, JobControl jobControl) {
        super(controller, duration, jobControl);
        this.applications = controller.applications();
    }

    @Override
    protected void maintain() {
        AtomicInteger failures = new AtomicInteger(0);
        AtomicInteger zeroQps = new AtomicInteger(0);
        AtomicReference<Exception> lastException = new AtomicReference<>(null);
        List<Application> applicationList = applications.asList();

        // Run parallel stream inside a custom ForkJoinPool so that we can control the number of threads used
        ForkJoinPool pool = new ForkJoinPool(applicationsToUpdateInParallel);
        pool.submit(() -> {
            applicationList.parallelStream().forEach(application -> {
                try {
                    applications.lockIfPresent(application.id(), locked ->
                            applications.store(locked.with(controller().metricsService().getApplicationMetrics(application.id()))));

                    applications.lockIfPresent(application.id(), locked ->
                            applications.store(locked.withRotationStatus(rotationStatus(application))));

                    for (Deployment deployment : application.deployments().values()) {
                        MetricsService.DeploymentMetrics deploymentMetrics = controller().metricsService()
                                                                                         .getDeploymentMetrics(application.id(), deployment.zone());
                        if (deploymentMetrics.queriesPerSecond() < 0.0001) {
                            zeroQps.incrementAndGet();
                        }

                        DeploymentMetrics newMetrics = new DeploymentMetrics(deploymentMetrics.queriesPerSecond(),
                                                                             deploymentMetrics.writesPerSecond(),
                                                                             deploymentMetrics.documentCount(),
                                                                             deploymentMetrics.queryLatencyMillis(),
                                                                             deploymentMetrics.writeLatencyMillis(),
                                                                             Optional.of(controller().clock().instant()));

                        applications.lockIfPresent(application.id(), locked ->
                                applications.store(locked.with(deployment.zone(), newMetrics)
                                                         .recordActivityAt(controller().clock().instant(), deployment.zone())));
                    }
                } catch (Exception e) {
                    failures.incrementAndGet();
                    lastException.set(e);
                }
            });
        });
        pool.shutdown();
        try {
            pool.awaitTermination(30, TimeUnit.MINUTES);
            log.log(Level.INFO, String.format("Number of application with 0 qps: %d/%d", zeroQps.get(), applicationList.size()));
            if (lastException.get() != null) {
                log.log(Level.WARNING, String.format("Failed to query metrics service for %d/%d applications. Last error: %s. Retrying in %s",
                                                     failures.get(),
                                                     applicationList.size(),
                                                     Exceptions.toMessageString(lastException.get()),
                                                     maintenanceInterval()));
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /** Get global rotation status for application */
    private Map<HostName, RotationStatus> rotationStatus(Application application) {
        return applications.rotationRepository().getRotation(application)
                           .map(rotation -> controller().metricsService().getRotationStatus(rotation.name()))
                           .map(rotationStatus -> {
                               Map<HostName, RotationStatus> result = new TreeMap<>();
                               rotationStatus.forEach((hostname, status) -> result.put(hostname, from(status)));
                               return result;
                           })
                           .orElseGet(Collections::emptyMap);
    }

    private static RotationStatus from(com.yahoo.vespa.hosted.controller.api.integration.routing.RotationStatus status) {
        switch (status) {
            case IN: return RotationStatus.in;
            case OUT: return RotationStatus.out;
            case UNKNOWN: return RotationStatus.unknown;
            default: throw new IllegalArgumentException("Unknown API value for rotation status: " + status);
        }
    }

}

@@ -460,78 +460,8 @@
 ce;%0A
-import com.yahoo.vespa.hosted.controller.application.ApplicationList;%0A
 impo
@@ -665,44 +665,8 @@
 tus;
-%0Aimport com.yahoo.yolean.Exceptions;
 %0A%0Aim
@@ -1854,62 +1854,8 @@
 0);%0A
-        AtomicInteger zeroQps = new AtomicInteger(0);%0A
     
@@ -3042,166 +3042,8 @@
 ());
-%0A                        if (deploymentMetrics.queriesPerSecond() %3C 0.0001) %7B%0A                            zeroQps.incrementAndGet();%0A                        %7D
 %0A%0A  
@@ -4339,138 +4339,8 @@
 S);%0A
-            log.log(Level.INFO, String.format(%22Number of application with 0 qps: %25d/%25d%22, zeroQps.get(), applicationList.size()));%0A
     
@@ -4416,15 +4416,12 @@
 vel.
-WARNING
+INFO
 , St
@@ -4493,24 +4493,8 @@
 ns. 
-Last error: %25s. 
 Retr
@@ -4503,16 +4503,30 @@
 ng in %25s
+. Last error: 
 %22,%0A     
@@ -4723,131 +4723,74 @@
     
-Exceptions.toMessageString(lastException.get()),%0A                                                     maintenanceInterval()
+maintenanceInterval()),%0A                        lastException.get(
 ));%0A

Print stacktrace of last exception. Remove logging of 0 qps deployments