summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ClusterInfoMaintainer.java
blob: cf0600f87bd6b705398e2b5f6bf4a0ce9f03214c (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2017 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.ClusterSpec;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeList;
import com.yahoo.vespa.hosted.controller.application.ApplicationList;
import com.yahoo.vespa.hosted.controller.application.ClusterInfo;
import com.yahoo.vespa.hosted.controller.application.Deployment;

import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Maintain info about hardware, hostnames and cluster specifications.
 * <p>
 * This is used to calculate cost metrics for the application api.
 *
 * @author smorgrav
 */
public class ClusterInfoMaintainer extends Maintainer {

    private static final Logger log = Logger.getLogger(ClusterInfoMaintainer.class.getName());
    
    private final Controller controller;

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

    private static String clusterid(NodeList.Node node) {
        return node.membership.clusterId;
    }

    private Map<ClusterSpec.Id, ClusterInfo> getClusterInfo(NodeList nodes, ZoneId zone) {
        Map<ClusterSpec.Id, ClusterInfo> infoMap = new HashMap<>();

        // Group nodes by clusterid
        Map<String, List<NodeList.Node>> clusters = nodes.nodes.stream()
                .filter(node -> node.membership != null)
                .collect(Collectors.groupingBy(ClusterInfoMaintainer::clusterid));

        // For each cluster - get info
        for (String id : clusters.keySet()) {
            List<NodeList.Node> clusterNodes = clusters.get(id);

            // Assume they are all equal and use first node as a representative for the cluster
            NodeList.Node node = clusterNodes.get(0);

            // Extract flavor info
            double cpu = 0;
            double mem = 0;
            double disk = 0;
            // TODO: This code was never run. Reenable when flavours are available from a FlavorRegistry or something, or remove.
            /*if (zone.nodeFlavors().isPresent()) {
                Optional<Flavor> flavorOptional = zone.nodeFlavors().get().getFlavor(node.flavor);
                if ((flavorOptional.isPresent())) {
                    Flavor flavor = flavorOptional.get();
                    cpu = flavor.getMinCpuCores();
                    mem = flavor.getMinMainMemoryAvailableGb();
                    disk = flavor.getMinMainMemoryAvailableGb();
                }
            }*/

            // Add to map
            List<String> hostnames = clusterNodes.stream().map(node1 -> node1.hostname).collect(Collectors.toList());
            ClusterInfo inf = new ClusterInfo(node.flavor, node.cost, cpu, mem, disk,
                                              ClusterSpec.Type.from(node.membership.clusterType), hostnames);
            infoMap.put(new ClusterSpec.Id(id), inf);
        }

        return infoMap;
    }

    @Override
    protected void maintain() {
        for (Application application : ApplicationList.from(controller().applications().asList()).notPullRequest().asList()) {
            for (Deployment deployment : application.deployments().values()) {
                DeploymentId deploymentId = new DeploymentId(application.id(), deployment.zone());
                try {
                    NodeList nodes = controller().applications().configserverClient().getNodeList(deploymentId);
                    Map<ClusterSpec.Id, ClusterInfo> clusterInfo = getClusterInfo(nodes, deployment.zone());
                    controller().applications().lockIfPresent(application.id(), lockedApplication ->
                        controller.applications().store(lockedApplication.withClusterInfo(deployment.zone(), clusterInfo)));
                }
                catch (IOException | IllegalArgumentException e) {
                    log.log(Level.WARNING, "Failing getting cluster info of for " + deploymentId, e);
                }
            }
        }
    }

}