aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/AnnotatedClusterState.java
blob: e0ddf16ab5f044c4d504952ba4ff5d23a23140a0 (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
// Copyright Vespa.ai. 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 com.yahoo.vdslib.state.Node;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;


public class AnnotatedClusterState implements Cloneable {

    private final ClusterState clusterState;
    private final Map<Node, NodeStateReason> nodeStateReasons;
    private final Optional<ClusterStateReason> clusterStateReason;

    public static class Builder {
        private ClusterState clusterState = ClusterState.emptyState();
        private Optional<ClusterStateReason> clusterReason = Optional.empty();
        private final Map<Node, NodeStateReason> nodeStateReasons = new HashMap<>();

        public Builder clusterState(String stateStr) {
            clusterState = ClusterState.stateFromString(stateStr);
            return this;
        }

        public Builder clusterReason(ClusterStateReason reason) {
            clusterReason = Optional.of(reason);
            return this;
        }

        public Builder storageNodeReason(int nodeIndex, NodeStateReason reason) {
            nodeStateReasons.put(Node.ofStorage(nodeIndex), reason);
            return this;
        }

        AnnotatedClusterState build() {
            return new AnnotatedClusterState(clusterState, clusterReason, nodeStateReasons);
        }
    }

    public AnnotatedClusterState(ClusterState clusterState,
                                 Optional<ClusterStateReason> clusterStateReason,
                                 Map<Node, NodeStateReason> nodeStateReasons) {
        this.clusterState = Objects.requireNonNull(clusterState, "Cluster state cannot be null");
        this.clusterStateReason = Objects.requireNonNull(clusterStateReason, "Cluster state reason cannot be null");
        this.nodeStateReasons = Objects.requireNonNull(nodeStateReasons, "Node state reasons cannot be null");
    }

    public static AnnotatedClusterState emptyState() {
        return new AnnotatedClusterState(ClusterState.emptyState(), Optional.empty(), emptyNodeStateReasons());
    }

    public static AnnotatedClusterState withoutAnnotations(ClusterState state) {
        return new AnnotatedClusterState(state, Optional.empty(), emptyNodeStateReasons());
    }

    static Map<Node, NodeStateReason> emptyNodeStateReasons() {
        return Collections.emptyMap();
    }

    public ClusterState getClusterState() {
        return clusterState;
    }

    public Map<Node, NodeStateReason> getNodeStateReasons() {
        return Collections.unmodifiableMap(nodeStateReasons);
    }

    public Optional<ClusterStateReason> getClusterStateReason() {
        return clusterStateReason;
    }

    public AnnotatedClusterState clone() {
        return cloneWithClusterState(clusterState.clone());
    }

    public AnnotatedClusterState cloneWithClusterState(ClusterState newClusterState) {
        return new AnnotatedClusterState(newClusterState,
                getClusterStateReason(),
                getNodeStateReasons());
    }

    @Override
    public String toString() {
        return clusterState.toString();
    }

    public String toString(boolean verbose) {
        return clusterState.toString(verbose);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AnnotatedClusterState that = (AnnotatedClusterState) o;
        return Objects.equals(clusterState, that.clusterState) &&
                Objects.equals(nodeStateReasons, that.nodeStateReasons) &&
                Objects.equals(clusterStateReason, that.clusterStateReason);
    }

    @Override
    public int hashCode() {
        return Objects.hash(clusterState, nodeStateReasons, clusterStateReason);
    }

}