aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ChangeManagementAssessor.java
blob: 5be20f9a99467f5531f015806d7c41f20433d993 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright Yahoo. 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.config.provision.NodeType;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.Node;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeFilter;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeRepository;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author smorgrav
 */
public class ChangeManagementAssessor {

    private final NodeRepository nodeRepository;

    public ChangeManagementAssessor(NodeRepository nodeRepository) {
        this.nodeRepository = nodeRepository;
    }

    public Assessment assessment(List<String> impactedHostnames, ZoneId zone) {
        return assessmentInner(impactedHostnames, nodeRepository.list(zone, NodeFilter.all()), zone);
    }

    Assessment assessmentInner(List<String> impactedHostnames, List<Node> allNodes, ZoneId zone) {
        List<String> impactedParentHosts = toParentHosts(impactedHostnames, allNodes);
        // Group impacted application nodes by parent host
        Map<Node, List<Node>> prParentHost = allNodes.stream()
                .filter(node -> node.state() == Node.State.active) //TODO look at more states?
                .filter(node -> impactedParentHosts.contains(node.parentHostname().map(HostName::value).orElse("")))
                .collect(Collectors.groupingBy(node ->
                    allNodes.stream()
                            .filter(parent -> parent.hostname().equals(node.parentHostname().get()))
                            .findFirst().orElseThrow()
                ));

        // Group nodes pr cluster
        Map<Cluster, List<Node>> prCluster = prParentHost.values()
                .stream()
                .flatMap(Collection::stream)
                .collect(Collectors.groupingBy(ChangeManagementAssessor::clusterKey));

        var tenantHosts = prParentHost.keySet().stream()
                .filter(node -> node.type() == NodeType.host)
                .map(node -> node.hostname())
                .toList();

        boolean allHostsReplacable = tenantHosts.isEmpty() || nodeRepository.isReplaceable(zone, tenantHosts);

        // Report assessment pr cluster
        var clusterAssessments = prCluster.entrySet().stream().map((entry) -> {
            Cluster cluster = entry.getKey();
            List<Node> nodes = entry.getValue();

            long[] totalStats = clusterStats(cluster, allNodes);
            long[] impactedStats = clusterStats(cluster, nodes);

            ClusterAssessment assessment = new ClusterAssessment();
            assessment.app = cluster.getApp();
            assessment.zone = zone.value();
            assessment.cluster = cluster.getClusterType() + ":" + cluster.getClusterId();
            assessment.clusterSize = totalStats[0];
            assessment.clusterImpact = impactedStats[0];
            assessment.groupsTotal = totalStats[1];
            assessment.groupsImpact = impactedStats[1];


            // TODO check upgrade policy
            assessment.upgradePolicy = "na";
            // TODO do some heuristic on suggestion action
            assessment.suggestedAction = allHostsReplacable ? "Retire all hosts" : "nothing";
            // TODO do some heuristic on impact
            assessment.impact = getImpact(cluster, impactedStats, totalStats);

            return assessment;
        }).toList();

        var hostAssessments = prParentHost.entrySet().stream().map((entry) -> {
            HostAssessment hostAssessment = new HostAssessment();
            hostAssessment.hostName = entry.getKey().hostname().value();
            hostAssessment.switchName = entry.getKey().switchHostname().orElse(null);
            hostAssessment.numberOfChildren = entry.getValue().size();

            //TODO: Some better heuristic for what's considered problematic
            hostAssessment.numberOfProblematicChildren = (int) entry.getValue().stream()
                    .mapToInt(node -> prCluster.get(clusterKey(node)).size())
                    .filter(i -> i > 1)
                    .count();

            return hostAssessment;
        }).toList();

        return new Assessment(clusterAssessments, hostAssessments);
    }

    private List<String> toParentHosts(List<String> impactedHostnames, List<Node> allNodes) {
        return impactedHostnames.stream()
                .flatMap(hostname ->
                    allNodes.stream()
                            .filter(node -> List.of(NodeType.config, NodeType.proxy, NodeType.host).contains(node.type()))
                            .filter(node -> hostname.equals(node.hostname().value()) || hostname.equals(node.parentHostname().map(HostName::value).orElse("")))
                            .map(node -> {
                                if (node.type() == NodeType.host)
                                    return node.hostname().value();
                                return node.parentHostname().get().value();
                            }).findFirst().stream()
                )
                .toList();
    }

    private static Cluster clusterKey(Node node) {
        if (node.owner().isEmpty())
            return Cluster.EMPTY;
        String appId = node.owner().get().serializedForm();
        return new Cluster(node.clusterType(), node.clusterId(), appId, node.type());
    }

    private static long[] clusterStats(Cluster cluster, List<Node> containerNodes) {
        List<Node> clusterNodes = containerNodes.stream().filter(node -> cluster.equals(clusterKey(node))).toList();
        long groups = clusterNodes.stream().map(Node::group).distinct().count();
        return new long[] { clusterNodes.size(), groups};
    }

    private String getImpact(Cluster cluster, long[] impactedStats, long[] totalStats) {
        switch (cluster.getNodeType()) {
            case tenant:
                return getTenantImpact(cluster, impactedStats, totalStats);
            case proxy:
                return getProxyImpact(impactedStats[0], totalStats[0]);
            case config:
                return getConfigServerImpact(impactedStats[0]);
            default:
                return "Unkown impact";
        }
    }

    private String getTenantImpact(Cluster cluster, long[] impactedStats, long[] totalStats) {
        switch (cluster.getClusterType()) {
            case container:
                return getContainerImpact(impactedStats[0], totalStats[0]);
            case content:
            case combined:
                return getContentImpact(totalStats[1] > 1, impactedStats[0], impactedStats[1]);
            default:
                return "Unknown impact";
        }
    }

    private String getProxyImpact(long impactedNodes, long totalNodes) {
        int impact = (int) (100.0 * impactedNodes / totalNodes);
        return impact + "% of routing nodes impacted. Consider reprovisioning if too many";
    }

    private String getConfigServerImpact(long impactedNodes) {
        if (impactedNodes == 1) {
            return "Acceptable impact";
        }
        return "Large impact. Consider reprovisioning one or more config servers";
    }

    private String getContainerImpact(long impactedNodes, long totalNodes) {
        if ((double) impactedNodes / totalNodes  <= 0.1) {
            return "Impact not larger than upgrade policy";
        }
        return "Impact larger than upgrade policy";
    }

    private String getContentImpact(boolean isGrouped, long impactedNodes, long impactedGroups) {
        if ((isGrouped && impactedGroups == 1) || impactedNodes == 1)
            return "Impact not larger than upgrade policy";
        return "Impact larger than upgrade policy";
    }


    public static class Assessment {
        List<ClusterAssessment> clusterAssessments;
        List<HostAssessment> hostAssessments;

        Assessment(List<ClusterAssessment> clusterAssessments, List<HostAssessment> hostAssessments) {
            this.clusterAssessments = clusterAssessments;
            this.hostAssessments = hostAssessments;
        }

        public List<ClusterAssessment> getClusterAssessments() {
            return clusterAssessments;
        }

        public List<HostAssessment> getHostAssessments() {
            return hostAssessments;
        }
    }

    public static class ClusterAssessment {
        public String app;
        public String zone;
        public String cluster;
        public long clusterImpact;
        public long clusterSize;
        public long groupsImpact;
        public long groupsTotal;
        public String upgradePolicy;
        public String suggestedAction;
        public String impact;
    }

    public static class HostAssessment {
        public String hostName;
        public String switchName;
        public int numberOfChildren;
        public int numberOfProblematicChildren;
    }

    private static class Cluster {
        private Node.ClusterType clusterType;
        private String clusterId;
        private String app;
        private NodeType nodeType;

        public final static Cluster EMPTY = new Cluster(Node.ClusterType.unknown, "na", "na", NodeType.tenant);

        public Cluster(Node.ClusterType clusterType, String clusterId, String app, NodeType nodeType) {
            this.clusterType = clusterType;
            this.clusterId = clusterId;
            this.app = app;
            this.nodeType = nodeType;
        }

        public Node.ClusterType getClusterType() {
            return clusterType;
        }

        public String getClusterId() {
            return clusterId;
        }

        public String getApp() {
            return app;
        }

        public NodeType getNodeType() {
            return nodeType;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Cluster cluster = (Cluster) o;
            return Objects.equals(clusterType, cluster.clusterType) &&
                    Objects.equals(clusterId, cluster.clusterId) &&
                    Objects.equals(app, cluster.app);
        }

        @Override
        public int hashCode() {
            return Objects.hash(clusterType, clusterId, app);
        }
    }

}