aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ClusterStateBundle.java
blob: 7062f67830b9b8f126e9ec291f4ed01f58f94291 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// 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.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
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 FeedBlock feedBlock;
    private final boolean deferredActivation;

    /**
     * Feed blocking status of the entire cluster that will be communicated to the nodes
     * as part of the cluster state bundle. If not present, or if blockFeedInCluster is
     * false, feed is not automatically blocked.
     *
     * Note that feed blocking only applies to client feed, not to feed generated by internal
     * maintenance operations such as merging.
     *
     * Immutable, so may be safely passed around.
     */
    public static class FeedBlock {
        private final boolean blockFeedInCluster;
        private final String description;
        private final Set<NodeResourceExhaustion> concreteExhaustions;

        public FeedBlock(boolean blockFeedInCluster, String description) {
            this.blockFeedInCluster = blockFeedInCluster;
            this.description = description;
            this.concreteExhaustions = Collections.emptySet();
        }

        public FeedBlock(boolean blockFeedInCluster, String description,
                         Set<NodeResourceExhaustion> concreteExhaustions)
        {
            this.blockFeedInCluster = blockFeedInCluster;
            this.description = description;
            this.concreteExhaustions = concreteExhaustions;
        }

        public static FeedBlock blockedWithDescription(String desc) {
            return new FeedBlock(true, desc);
        }

        public static FeedBlock blockedWith(String description, Set<NodeResourceExhaustion> concreteExhaustions) {
            return new FeedBlock(true, description, concreteExhaustions);
        }

        public boolean blockFeedInCluster() {
            return blockFeedInCluster;
        }

        public String getDescription() {
            return description;
        }

        public Set<NodeResourceExhaustion> getConcreteExhaustions() {
            return concreteExhaustions;
        }

        public boolean similarTo(FeedBlock other) {
            // We check everything _but_ the description, as that includes current usage
            // as floating point and we don't care about reporting changes in that. We do
            // however care about reporting changes to the actual set of exhaustions.
            return (blockFeedInCluster == other.blockFeedInCluster &&
                    Objects.equals(concreteExhaustions, other.concreteExhaustions));
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            FeedBlock feedBlock = (FeedBlock) o;
            return blockFeedInCluster == feedBlock.blockFeedInCluster &&
                    Objects.equals(description, feedBlock.description) &&
                    Objects.equals(concreteExhaustions, feedBlock.concreteExhaustions);
        }

        @Override
        public int hashCode() {
            return Objects.hash(blockFeedInCluster, description, concreteExhaustions);
        }
    }

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

        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 Builder feedBlock(FeedBlock fb) {
            this.feedBlock = fb;
            return this;
        }

        public ClusterStateBundle deriveAndBuild() {
            if ((stateDeriver == null || bucketSpaces == null || bucketSpaces.isEmpty()) && explicitDerivedStates == null) {
                return ClusterStateBundle.ofBaselineOnly(baselineState, feedBlock, 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, feedBlock, deferredActivation);
        }
    }

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

    private ClusterStateBundle(AnnotatedClusterState baselineState,
                               Map<String, AnnotatedClusterState> derivedBucketSpaceStates,
                               FeedBlock feedBlock,
                               boolean deferredActivation) {
        this.baselineState = baselineState;
        this.derivedBucketSpaceStates = Collections.unmodifiableMap(derivedBucketSpaceStates);
        this.feedBlock = feedBlock;
        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,
                                        FeedBlock feedBlock,
                                        boolean deferredActivation) {
        return new ClusterStateBundle(baselineState, derivedBucketSpaceStates, feedBlock, deferredActivation);
    }

    public static ClusterStateBundle ofBaselineOnly(AnnotatedClusterState baselineState,
                                                    FeedBlock feedBlock,
                                                    boolean deferredActivation) {
        return new ClusterStateBundle(baselineState, Collections.emptyMap(), feedBlock, 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, feedBlock, 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;
        }
        if (clusterFeedIsBlocked() != other.clusterFeedIsBlocked()) {
            return false;
        }
        if (clusterFeedIsBlocked() && !feedBlock.similarTo(other.feedBlock)) {
            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();
    }

    public Optional<FeedBlock> getFeedBlock() {
        return Optional.ofNullable(feedBlock);
    }

    public FeedBlock getFeedBlockOrNull() {
        return feedBlock;
    }

    public boolean clusterFeedIsBlocked() {
        return (feedBlock != null && feedBlock.blockFeedInCluster());
    }

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

    @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) &&
                Objects.equals(feedBlock, that.feedBlock));
    }

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