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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.versions;
import com.google.common.collect.ImmutableMap;
import com.yahoo.component.Version;
import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.zone.ZoneApi;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeFilter;
import com.yahoo.vespa.hosted.controller.application.SystemApplication;
import com.yahoo.vespa.hosted.controller.maintenance.OsUpgrader;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Information about OS versions in this system.
*
* @author mpolden
*/
public class OsVersionStatus {
public static final OsVersionStatus empty = new OsVersionStatus(ImmutableMap.of());
private final Map<OsVersion, List<NodeVersion>> versions;
/** Public for serialization purpose only. Use {@link OsVersionStatus#compute(Controller)} for an up-to-date status */
public OsVersionStatus(Map<OsVersion, List<NodeVersion>> versions) {
this.versions = ImmutableMap.copyOf(Objects.requireNonNull(versions, "versions must be non-null"));
}
/** All known OS versions and their nodes */
public Map<OsVersion, List<NodeVersion>> versions() {
return versions;
}
/** Returns nodes eligible for OS upgrades that exist in given cloud */
public List<NodeVersion> nodesIn(CloudName cloud) {
return versions.entrySet().stream()
.filter(entry -> entry.getKey().cloud().equals(cloud))
.map(Map.Entry::getValue)
.findFirst()
.orElseGet(List::of);
}
/** Returns versions that exist in given cloud */
public Set<Version> versionsIn(CloudName cloud) {
return versions.keySet().stream()
.filter(osVersion -> osVersion.cloud().equals(cloud))
.map(OsVersion::version)
.collect(Collectors.toUnmodifiableSet());
}
/** Compute the current OS versions in this system. This is expensive and should be called infrequently */
public static OsVersionStatus compute(Controller controller) {
Map<OsVersion, List<NodeVersion>> osVersions = new HashMap<>();
controller.osVersionTargets().forEach(target -> osVersions.put(target.osVersion(), new ArrayList<>()));
for (var application : SystemApplication.all()) {
for (var zone : zonesToUpgrade(controller)) {
if (!application.shouldUpgradeOs()) continue;
Version targetOsVersion = controller.serviceRegistry().configServer().nodeRepository()
.targetVersionsOf(zone.getVirtualId())
.osVersion(application.nodeType())
.orElse(Version.emptyVersion);
for (var node : controller.serviceRegistry().configServer().nodeRepository().list(zone.getVirtualId(), NodeFilter.all().applications(application.id()))) {
if (!OsUpgrader.canUpgrade(node)) continue;
Optional<Instant> suspendedAt = node.suspendedSince();
NodeVersion nodeVersion = new NodeVersion(node.hostname(), zone.getVirtualId(), node.currentOsVersion(),
targetOsVersion, suspendedAt);
OsVersion osVersion = new OsVersion(nodeVersion.currentVersion(), zone.getCloudName());
osVersions.computeIfAbsent(osVersion, (k) -> new ArrayList<>())
.add(nodeVersion);
}
}
}
return new OsVersionStatus(osVersions);
}
private static List<ZoneApi> zonesToUpgrade(Controller controller) {
return controller.zoneRegistry().osUpgradePolicies().stream()
.flatMap(upgradePolicy -> upgradePolicy.steps().stream())
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableList());
}
}
|