aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ClusterStateHistoryEntry.java
blob: be89617fe79fe5be4c49d6b9359cce63c80269db (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vdslib.state.ClusterState;

import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * Represents the absolute cluster states (baseline + derived) for a given version
 * as well as the relative diffs (baseline + derived) since the previous version.
 */
public class ClusterStateHistoryEntry {

    public final static String BASELINE = "-";

    private final long time;
    private final Map<String, String> states = new TreeMap<>();
    private final Map<String, String> diffs = new TreeMap<>();

    ClusterStateHistoryEntry(ClusterStateBundle state, long time) {
        this.time = time;
        populateStateStrings(state);
        // No diffs for the first entry in the history.
    }

    ClusterStateHistoryEntry(ClusterStateBundle state, ClusterStateBundle prevState, long time) {
        this.time = time;
        populateStateStrings(state);
        populateDiffStrings(state, prevState);
    }

    private void populateStateStrings(ClusterStateBundle state) {
        states.put(BASELINE, state.getBaselineClusterState().toString());
        var derivedStates = state.getDerivedBucketSpaceStates().entrySet().stream()
                .collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> entry.getValue().getClusterState().toString()));
        states.putAll(derivedStates);
    }

    private void populateDiffStrings(ClusterStateBundle state, ClusterStateBundle prevState) {
        // Yes, it's correct to get the diff by doing old.getHtmlDifference(new) rather than the other way around.
        diffs.put(BASELINE, prevState.getBaselineClusterState().getHtmlDifference(state.getBaselineClusterState()));
        var spaceDiffs = state.getDerivedBucketSpaceStates().entrySet().stream()
                .collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> derivedStateOf(prevState, entry.getKey()).getHtmlDifference(entry.getValue().getClusterState())));
        diffs.putAll(spaceDiffs);
    }

    public static ClusterStateHistoryEntry makeFirstEntry(ClusterStateBundle state, long time) {
        return new ClusterStateHistoryEntry(state, time);
    }

    public static ClusterStateHistoryEntry makeSuccessor(ClusterStateBundle state, ClusterStateBundle prevState, long time) {
        return new ClusterStateHistoryEntry(state, prevState, time);
    }

    private static ClusterState derivedStateOf(ClusterStateBundle state, String space) {
        return state.getDerivedBucketSpaceStates().getOrDefault(space, AnnotatedClusterState.emptyState()).getClusterState();
    }

    public Map<String, String> getRawStates() {
        return states;
    }

    public String getStateString(String space) {
        return states.getOrDefault(space, "");
    }

    public String getDiffString(String space) {
        return diffs.getOrDefault(space, "");
    }

    public long time() {
        return time;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ClusterStateHistoryEntry that = (ClusterStateHistoryEntry) o;
        return time == that.time &&
                Objects.equals(states, that.states) &&
                Objects.equals(diffs, that.diffs);
    }

    @Override
    public int hashCode() {
        return Objects.hash(time, states, diffs);
    }

    // String representation only used for test expectation failures and debugging output.
    // Actual status page history entry rendering emits formatted date/time.
    public String toString() {
        return String.format("state '%s' at time %d", getStateString(BASELINE), time);
    }

}