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
|
// Copyright Verizon Media. 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.Environment;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeRepository;
import com.yahoo.vespa.hosted.controller.api.integration.vcmr.ChangeRequest;
import com.yahoo.vespa.hosted.controller.api.integration.vcmr.ChangeRequestClient;
import com.yahoo.vespa.hosted.controller.api.integration.vcmr.ChangeRequestSource;
import com.yahoo.vespa.hosted.controller.api.integration.vcmr.VespaChangeRequest;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* @author olaa
*/
public class ChangeRequestMaintainer extends ControllerMaintainer {
private final Logger logger = Logger.getLogger(ChangeRequestMaintainer.class.getName());
private final ChangeRequestClient changeRequestClient;
private final CuratorDb curator;
private final NodeRepository nodeRepository;
public ChangeRequestMaintainer(Controller controller, Duration interval) {
super(controller, interval, null, SystemName.allOf(Predicate.not(SystemName::isPublic)));
this.changeRequestClient = controller.serviceRegistry().changeRequestClient();
this.curator = controller.curator();
this.nodeRepository = controller.serviceRegistry().configServer().nodeRepository();
}
@Override
protected double maintain() {
var currentChangeRequests = pruneOldChangeRequests();
var changeRequests = changeRequestClient.getChangeRequests(currentChangeRequests);
logger.fine(() -> "Found requests: " + changeRequests);
storeChangeRequests(changeRequests);
return 1.0;
}
private void storeChangeRequests(List<ChangeRequest> changeRequests) {
var existingChangeRequests = curator.readChangeRequests()
.stream()
.collect(Collectors.toMap(ChangeRequest::getId, Function.identity()));
var hostsByZone = hostsByZone();
// Create or update requests in curator
try (var lock = curator.lockChangeRequests()) {
changeRequests.forEach(changeRequest -> {
var optionalZone = inferZone(changeRequest, hostsByZone);
optionalZone.ifPresent(zone -> {
var vcmr = existingChangeRequests
.getOrDefault(changeRequest.getId(), new VespaChangeRequest(changeRequest, zone))
.withSource(changeRequest.getChangeRequestSource())
.withApproval(changeRequest.getApproval());
logger.fine(() -> "Storing " + vcmr);
curator.writeChangeRequest(vcmr);
});
});
}
}
// Deletes closed change requests older than 7 days, returns the current list of requests
private List<ChangeRequest> pruneOldChangeRequests() {
List<ChangeRequest> currentChangeRequests = new ArrayList<>();
try (var lock = curator.lockChangeRequests()) {
for (var changeRequest : curator.readChangeRequests()) {
if (shouldDeleteChangeRequest(changeRequest.getChangeRequestSource())) {
curator.deleteChangeRequest(changeRequest);
} else {
currentChangeRequests.add(changeRequest);
}
}
}
return currentChangeRequests;
}
private Map<ZoneId, List<String>> hostsByZone() {
return controller().zoneRegistry()
.zones()
.reachable()
.in(Environment.prod)
.ids()
.stream()
.collect(Collectors.toMap(
zone -> zone,
zone -> nodeRepository.list(zone, false)
.stream()
.map(node -> node.hostname().value())
.collect(Collectors.toList())
));
}
private Optional<ZoneId> inferZone(ChangeRequest changeRequest, Map<ZoneId, List<String>> hostsByZone) {
return hostsByZone.entrySet().stream()
.filter(entry -> !Collections.disjoint(entry.getValue(), changeRequest.getImpactedHosts()))
.map(Map.Entry::getKey)
.findFirst();
}
private boolean shouldDeleteChangeRequest(ChangeRequestSource source) {
return source.isClosed() &&
source.getPlannedStartTime()
.plus(Duration.ofDays(7))
.isBefore(ZonedDateTime.now());
}
}
|