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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.policy;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.applicationmodel.ServiceInstance;
import java.time.Duration;
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.Optional;
import java.util.stream.Collectors;
/**
* Information worth logging when/if suspending a host.
*
* @author hakon
*/
public class SuspensionReasons {
private final Map<HostName, List<String>> reasons = new HashMap<>();
public static SuspensionReasons nothingNoteworthy() { return new SuspensionReasons(); }
public static SuspensionReasons isDown(ServiceInstance service) {
return new SuspensionReasons().addReason(
service.hostName(),
service.descriptiveName() + " is down");
}
public static SuspensionReasons unknownStatus(ServiceInstance service) {
return new SuspensionReasons().addReason(
service.hostName(),
service.descriptiveName() + " has not yet been probed for health");
}
public static SuspensionReasons downSince(ServiceInstance service, Instant instant, Duration downDuration) {
return new SuspensionReasons().addReason(
service.hostName(),
service.descriptiveName() + " has been down since " +
// Round to whole second
Instant.ofEpochSecond(instant.getEpochSecond()).toString() +
" (" + downDuration.getSeconds() + " seconds)");
}
public SuspensionReasons() {}
/** An ordered list of all messages, typically useful for testing. */
public List<String> getMessagesInOrder() {
return reasons.values().stream().flatMap(Collection::stream).sorted().toList();
}
public SuspensionReasons mergeWith(SuspensionReasons that) {
for (var entry : that.reasons.entrySet()) {
for (var reason : entry.getValue()) {
addReason(entry.getKey(), reason);
}
}
return this;
}
/**
* Makes a log message, if there is anything worth logging about the decision to allow suspension,
* that can be directly fed to {@link java.util.logging.Logger#info(String) Logger.info(String)}.
*/
public Optional<String> makeLogMessage() {
if (reasons.isEmpty()) {
return Optional.empty();
}
return Optional.of(reasons.entrySet().stream()
.map(entry -> entry.getKey().s() + " suspended because " + String.join(", ", entry.getValue()))
.sorted()
.collect(Collectors.joining("; ")));
}
/** Package-private for testing. */
SuspensionReasons addReason(HostName hostname, String message) {
reasons.computeIfAbsent(hostname, h -> new ArrayList<>()).add(message);
return this;
}
@Override
public String toString() {
return makeLogMessage().orElse("");
}
}
|