summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ClusterStateBundle.java
blob: fc06fef5b303309d9d9faa96cd4c42eaab25f5aa (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2018 Yahoo Holdings. 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.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * A cluster state bundle is a wrapper around the baseline ("source of truth") cluster
 * state and any bucket space specific states that may be derived from it.
 *
 * The baseline state represents the generated state of the _nodes_ in the cluster,
 * while the per-space states represent possible transformations that make sense in
 * the context of that particular bucket space. The most prominent example is
 * transforming nodes in the default bucket space into maintenance mode if they have
 * merges pending in the global space.
 *
 * The baseline state is identical to the legacy, global cluster state that the
 * cluster controller has historically produced as its only output.
 *
 * The bundle also contains an additional "deferred activation" flag which tells
 * the recipient if the cluster state transition should complete immediately or
 * await an explicit activation RPC from the cluster controller.
 */
public class ClusterStateBundle {

    private final AnnotatedClusterState baselineState;
    private final Map<String, AnnotatedClusterState> derivedBucketSpaceStates;
    private final boolean deferredActivation;

    public static class Builder {
        private final AnnotatedClusterState baselineState;
        private Map<String, AnnotatedClusterState> explicitDerivedStates;
        private ClusterStateDeriver stateDeriver;
        private Set<String> bucketSpaces;
        private boolean deferredActivation = false;

        public Builder(AnnotatedClusterState baselineState) {
            this.baselineState = baselineState;
        }

        public Builder stateDeriver(ClusterStateDeriver stateDeriver) {
            this.stateDeriver = stateDeriver;
            return this;
        }

        public Builder bucketSpaces(Set<String> bucketSpaces) {
            if (this.explicitDerivedStates != null) {
                throw new IllegalStateException("Cannot set bucket spaces on Builder that already " +
                                                "has explicit derived states set");
            }
            this.bucketSpaces = bucketSpaces;
            return this;
        }

        public Builder bucketSpaces(String... bucketSpaces) {
            return bucketSpaces(new TreeSet<>(Arrays.asList(bucketSpaces)));
        }

        public Builder explicitDerivedStates(Map<String, AnnotatedClusterState> derivedStates) {
            if (this.bucketSpaces != null || this.stateDeriver != null) {
                throw new IllegalStateException("Cannot set explicitly derived states on Builder " +
                                                "that already has bucket spaces or deriver set");
            }
            this.explicitDerivedStates = derivedStates;
            return this;
        }

        public Builder deferredActivation(boolean deferred) {
            this.deferredActivation = deferred;
            return this;
        }

        public ClusterStateBundle deriveAndBuild() {
            if ((stateDeriver == null || bucketSpaces == null || bucketSpaces.isEmpty()) && explicitDerivedStates == null) {
                return ClusterStateBundle.ofBaselineOnly(baselineState, deferredActivation);
            }
            Map<String, AnnotatedClusterState> derived;
            if (explicitDerivedStates != null) {
                derived = explicitDerivedStates;
            } else {
                derived = bucketSpaces.stream()
                        .collect(Collectors.toMap(
                                Function.identity(),
                                s -> stateDeriver.derivedFrom(baselineState, s)));
            }
            return new ClusterStateBundle(baselineState, derived, deferredActivation);
        }
    }

    private ClusterStateBundle(AnnotatedClusterState baselineState, Map<String, AnnotatedClusterState> derivedBucketSpaceStates) {
        this(baselineState, derivedBucketSpaceStates, false);
    }

    private ClusterStateBundle(AnnotatedClusterState baselineState, Map<String,
                               AnnotatedClusterState> derivedBucketSpaceStates,
                               boolean deferredActivation) {
        this.baselineState = baselineState;
        this.derivedBucketSpaceStates = Collections.unmodifiableMap(derivedBucketSpaceStates);
        this.deferredActivation = deferredActivation;
    }

    public static Builder builder(AnnotatedClusterState baselineState) {
        return new Builder(baselineState);
    }

    public static ClusterStateBundle of(AnnotatedClusterState baselineState, Map<String, AnnotatedClusterState> derivedBucketSpaceStates) {
        return new ClusterStateBundle(baselineState, derivedBucketSpaceStates);
    }

    public static ClusterStateBundle of(AnnotatedClusterState baselineState,
                                        Map<String, AnnotatedClusterState> derivedBucketSpaceStates,
                                        boolean deferredActivation) {
        return new ClusterStateBundle(baselineState, derivedBucketSpaceStates, deferredActivation);
    }

    public static ClusterStateBundle ofBaselineOnly(AnnotatedClusterState baselineState, boolean deferredActivation) {
        return new ClusterStateBundle(baselineState, Collections.emptyMap(), deferredActivation);
    }

    public static ClusterStateBundle ofBaselineOnly(AnnotatedClusterState baselineState) {
        return new ClusterStateBundle(baselineState, Collections.emptyMap());
    }

    public static ClusterStateBundle empty() {
        return ofBaselineOnly(AnnotatedClusterState.emptyState());
    }

    public AnnotatedClusterState getBaselineAnnotatedState() {
        return baselineState;
    }

    public ClusterState getBaselineClusterState() {
        return baselineState.getClusterState();
    }

    public Map<String, AnnotatedClusterState> getDerivedBucketSpaceStates() {
        return derivedBucketSpaceStates;
    }

    public boolean deferredActivation() { return this.deferredActivation; }

    public ClusterStateBundle cloneWithMapper(Function<ClusterState, ClusterState> mapper) {
        AnnotatedClusterState clonedBaseline = baselineState.cloneWithClusterState(
                mapper.apply(baselineState.getClusterState().clone()));
        Map<String, AnnotatedClusterState> clonedDerived = derivedBucketSpaceStates.entrySet().stream()
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().cloneWithClusterState(
                        mapper.apply(e.getValue().getClusterState().clone()))));
        return new ClusterStateBundle(clonedBaseline, clonedDerived, deferredActivation);
    }

    public ClusterStateBundle clonedWithVersionSet(int version) {
        return cloneWithMapper(state -> {
            state.setVersion(version);
            return state;
        });
    }

    public boolean similarTo(ClusterStateBundle other) {
        if (!baselineState.getClusterState().similarToIgnoringInitProgress(other.baselineState.getClusterState())) {
            return false;
        }
        // FIXME we currently treat mismatching bucket space sets as unchanged to avoid breaking some tests
        return derivedBucketSpaceStates.entrySet().stream()
                .allMatch(entry -> other.derivedBucketSpaceStates.getOrDefault(entry.getKey(), entry.getValue())
                        .getClusterState().similarToIgnoringInitProgress(entry.getValue().getClusterState()));
    }

    public int getVersion() {
        return baselineState.getClusterState().getVersion();
    }

    @Override
    public String toString() {
        if (derivedBucketSpaceStates.isEmpty()) {
            return String.format("ClusterStateBundle('%s'%s)", baselineState,
                    deferredActivation ? " (deferred activation)" : "");
        }
        Map<String, AnnotatedClusterState> orderedStates = new TreeMap<>(derivedBucketSpaceStates);
        return String.format("ClusterStateBundle('%s', %s%s)", baselineState, orderedStates.entrySet().stream()
                .map(e -> String.format("%s '%s'", e.getKey(), e.getValue()))
                .collect(Collectors.joining(", ")),
                deferredActivation ? " (deferred activation)" : "");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ClusterStateBundle that = (ClusterStateBundle) o;
        return deferredActivation == that.deferredActivation &&
                Objects.equals(baselineState, that.baselineState) &&
                Objects.equals(derivedBucketSpaceStates, that.derivedBucketSpaceStates);
    }

    @Override
    public int hashCode() {
        return Objects.hash(baselineState, derivedBucketSpaceStates, deferredActivation);
    }
}