aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/DeploymentInfoMaintainer.java
blob: b2b06cf281fba741d9232a77803d03b057d8af8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.Instance;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.ConfigServerException;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeRepository;
import com.yahoo.yolean.Exceptions;

import java.time.Duration;
import java.util.Collection;
import java.util.Map;

/**
 * This pulls application deployment information from the node repo on all config servers,
 * and stores it in memory in controller.applications().deploymentInfo().
 *
 * @author bratseth
 */
public class DeploymentInfoMaintainer extends ControllerMaintainer {

    private final NodeRepository nodeRepository;

    public DeploymentInfoMaintainer(Controller controller, Duration duration, Double successFactorBaseline) {
        super(controller, duration, successFactorBaseline);
        this.nodeRepository = controller.serviceRegistry().configServer().nodeRepository();
    }

    @Override
    protected double maintain() {
        int attempts = 0;
        int failures = 0;
        outer:
        for (var application : controller().applications().idList()) {
            for (var instance : controller().applications().getApplication(application).map(Application::instances).orElse(Map.of()).values()) {
                for (var deployment : instanceDeployments(instance)) {
                    if (shuttingDown()) break outer;
                    attempts++;
                    if ( ! updateDeploymentInfo(deployment))
                        failures++;
                }
            }
        }
        return asSuccessFactorDeviation(attempts, failures);
    }

    private Collection<DeploymentId> instanceDeployments(Instance instance) {
        return instance.deployments().keySet().stream()
                       .filter(zoneId -> ! zoneId.environment().isTest())
                       .map(zoneId -> new DeploymentId(instance.id(), zoneId))
                       .toList();
    }

    private boolean updateDeploymentInfo(DeploymentId id) {
        try {
            controller().applications().deploymentInfo().put(id, nodeRepository.getApplication(id.zoneId(), id.applicationId()));
            return true;
        }
        catch (ConfigServerException e) {
            log.info("Could not retrieve deployment info for " + id + ": " + Exceptions.toMessageString(e));
            return false;
        }
    }

}