aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ClusterStateBundleTest.java
blob: d12541687f0147fc00a4a625ec0e16d2bcae065f (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
// 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 com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vdslib.state.State;
import org.junit.Test;

import java.util.Set;
import java.util.function.Function;

import static com.yahoo.vespa.clustercontroller.core.FeedBlockUtil.exhaustion;
import static com.yahoo.vespa.clustercontroller.core.FeedBlockUtil.setOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class ClusterStateBundleTest {

    private static ClusterState stateOf(String state) {
        return ClusterState.stateFromString(state);
    }

    private static AnnotatedClusterState annotatedStateOf(String state) {
        return AnnotatedClusterState.withoutAnnotations(stateOf(state));
    }

    private static ClusterStateBundle.Builder createTestBundleBuilder(boolean modifyDefaultSpace) {
        return ClusterStateBundle
                .builder(annotatedStateOf("distributor:2 storage:2"))
                .bucketSpaces("default", "global", "narnia")
                .stateDeriver((state, space) -> {
                    AnnotatedClusterState derived = state.clone();
                    if (space.equals("default") && modifyDefaultSpace) {
                        derived.getClusterState()
                                .setNodeState(Node.ofStorage(0), new NodeState(NodeType.STORAGE, State.DOWN));
                    } else if (space.equals("narnia")) {
                        derived.getClusterState()
                                .setNodeState(Node.ofDistributor(0), new NodeState(NodeType.DISTRIBUTOR, State.DOWN));
                    }
                    return derived;
                });
    }

    private static ClusterStateBundle createTestBundle(boolean modifyDefaultSpace) {
        return createTestBundleBuilder(modifyDefaultSpace).deriveAndBuild();
    }

    private static ClusterStateBundle createTestBundleWithFeedBlock(String description) {
        return createTestBundleBuilder(false)
                .feedBlock(ClusterStateBundle.FeedBlock.blockedWithDescription(description))
                .deriveAndBuild();
    }

    private static ClusterStateBundle createTestBundleWithFeedBlock(String description, Set<NodeResourceExhaustion> concreteExhaustions) {
        return createTestBundleBuilder(false)
                .feedBlock(ClusterStateBundle.FeedBlock.blockedWith(description, concreteExhaustions))
                .deriveAndBuild();
    }

    private static ClusterStateBundle createTestBundle() {
        return createTestBundle(true);
    }

    @Test
    public void builder_creates_baseline_state_and_derived_state_per_space() {
        ClusterStateBundle bundle = createTestBundle();
        assertThat(bundle.getBaselineClusterState(), equalTo(stateOf("distributor:2 storage:2")));
        assertThat(bundle.getDerivedBucketSpaceStates().size(), equalTo(3));
        assertThat(bundle.getDerivedBucketSpaceStates().get("default"), equalTo(annotatedStateOf("distributor:2 storage:2 .0.s:d")));
        assertThat(bundle.getDerivedBucketSpaceStates().get("global"), equalTo(annotatedStateOf("distributor:2 storage:2")));
        assertThat(bundle.getDerivedBucketSpaceStates().get("narnia"), equalTo(annotatedStateOf("distributor:2 .0.s:d storage:2")));
    }

    @Test
    public void version_clone_sets_version_for_all_spaces() {
        ClusterStateBundle bundle = createTestBundle().clonedWithVersionSet(123);
        assertThat(bundle.getBaselineClusterState(), equalTo(stateOf("version:123 distributor:2 storage:2")));
        assertThat(bundle.getDerivedBucketSpaceStates().size(), equalTo(3));
        assertThat(bundle.getDerivedBucketSpaceStates().get("default"), equalTo(annotatedStateOf("version:123 distributor:2 storage:2 .0.s:d")));
        assertThat(bundle.getDerivedBucketSpaceStates().get("global"), equalTo(annotatedStateOf("version:123 distributor:2 storage:2")));
        assertThat(bundle.getDerivedBucketSpaceStates().get("narnia"), equalTo(annotatedStateOf("version:123 distributor:2 .0.s:d storage:2")));
    }

    @Test
    public void same_bundle_instance_considered_similar() {
        ClusterStateBundle bundle = createTestBundle();
        assertTrue(bundle.similarTo(bundle));
    }

    @Test
    public void similarity_test_considers_all_bucket_spaces() {
        ClusterStateBundle bundle = createTestBundle(false);
        ClusterStateBundle unchangedBundle = createTestBundle(false);

        assertTrue(bundle.similarTo(unchangedBundle));
        assertTrue(unchangedBundle.similarTo(bundle));

        ClusterStateBundle changedBundle = createTestBundle(true);
        assertFalse(bundle.similarTo(changedBundle));
        assertFalse(changedBundle.similarTo(bundle));
    }

    @Test
    public void similarity_test_considers_cluster_feed_block_state() {
        var nonBlockingBundle = createTestBundle(false);
        var blockingBundle = createTestBundleWithFeedBlock("foo");
        var blockingBundleWithOtherDesc = createTestBundleWithFeedBlock("bar");

        assertFalse(nonBlockingBundle.similarTo(blockingBundle));
        assertFalse(blockingBundle.similarTo(nonBlockingBundle));
        assertTrue(blockingBundle.similarTo(blockingBundle));
        // We currently consider different descriptions with same blocking status to be similar
        assertTrue(blockingBundle.similarTo(blockingBundleWithOtherDesc));
    }

    @Test
    public void similarity_test_considers_cluster_feed_block_concrete_exhaustion_set() {
        var blockingBundleNoSet        = createTestBundleWithFeedBlock("foo");
        var blockingBundleWithSet      = createTestBundleWithFeedBlock("bar", setOf(exhaustion(1, "beer"), exhaustion(1, "wine")));
        var blockingBundleWithOtherSet = createTestBundleWithFeedBlock("bar", setOf(exhaustion(1, "beer"), exhaustion(1, "soda")));

        assertTrue(blockingBundleNoSet.similarTo(blockingBundleNoSet));
        assertTrue(blockingBundleWithSet.similarTo(blockingBundleWithSet));
        assertFalse(blockingBundleWithSet.similarTo(blockingBundleWithOtherSet));
        assertFalse(blockingBundleNoSet.similarTo(blockingBundleWithSet));
        assertFalse(blockingBundleNoSet.similarTo(blockingBundleWithOtherSet));
    }

    @Test
    public void feed_block_state_is_available() {
        var nonBlockingBundle = createTestBundle(false);
        var blockingBundle = createTestBundleWithFeedBlock("foo");

        assertFalse(nonBlockingBundle.clusterFeedIsBlocked());
        assertFalse(nonBlockingBundle.getFeedBlock().isPresent());

        assertTrue(blockingBundle.clusterFeedIsBlocked());
        var block = blockingBundle.getFeedBlock();
        assertTrue(block.isPresent());
        assertTrue(block.get().blockFeedInCluster());
        assertEquals(block.get().getDescription(), "foo");
    }

    @Test
    public void toString_without_bucket_space_states_prints_only_baseline_state() {
        ClusterStateBundle bundle = ClusterStateBundle.ofBaselineOnly(
                annotatedStateOf("distributor:2 storage:2"));
        assertThat(bundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2')"));
    }

    @Test
    public void toString_includes_all_bucket_space_states() {
        ClusterStateBundle bundle = createTestBundle();
        assertThat(bundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2', " +
                "default 'distributor:2 storage:2 .0.s:d', " +
                "global 'distributor:2 storage:2', " +
                "narnia 'distributor:2 .0.s:d storage:2')"));
    }

    @Test
    public void toString_with_feed_blocked_includes_description() {
        var blockingBundle = createTestBundleWithFeedBlock("bear sleeping on server rack");
        assertThat(blockingBundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2', " +
                "default 'distributor:2 storage:2', " +
                "global 'distributor:2 storage:2', " +
                "narnia 'distributor:2 .0.s:d storage:2', " +
                "feed blocked: 'bear sleeping on server rack')"));
    }

    @Test
    public void toString_without_derived_states_specifies_deferred_activation_iff_set() {
        var bundle = ClusterStateBundle.ofBaselineOnly(annotatedStateOf("distributor:2 storage:2"), null, true);
        assertThat(bundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2' (deferred activation))"));
    }

    @Test
    public void toString_without_derived_states_does_not_specify_deferred_activation_iff_not_set() {
        var bundle = ClusterStateBundle.ofBaselineOnly(annotatedStateOf("distributor:2 storage:2"), null, false);
        assertThat(bundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2')"));
    }

    @Test
    public void toString_with_derived_states_specifies_deferred_activation_iff_set() {
        var bundle = createTestBundleBuilder(true).deferredActivation(true).deriveAndBuild();
        assertThat(bundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2', " +
                "default 'distributor:2 storage:2 .0.s:d', " +
                "global 'distributor:2 storage:2', " +
                "narnia 'distributor:2 .0.s:d storage:2' (deferred activation))"));
    }

    @Test
    public void toString_with_derived_states_does_not_specify_deferred_activation_iff_not_set() {
        var bundle = createTestBundleBuilder(true).deferredActivation(false).deriveAndBuild();
        assertThat(bundle.toString(), equalTo("ClusterStateBundle('distributor:2 storage:2', " +
                "default 'distributor:2 storage:2 .0.s:d', " +
                "global 'distributor:2 storage:2', " +
                "narnia 'distributor:2 .0.s:d storage:2')"));
    }

    @Test
    public void deferred_activation_is_disabled_by_default() {
        ClusterStateBundle bundle = createTestBundle();
        assertFalse(bundle.deferredActivation());
    }

    @Test
    public void can_build_bundle_with_deferred_activation_enabled() {
        var bundle = createTestBundleBuilder(false).deferredActivation(true).deriveAndBuild();
        assertTrue(bundle.deferredActivation());
    }

    @Test
    public void can_build_bundle_with_deferred_activation_disabled() {
        var bundle = createTestBundleBuilder(false).deferredActivation(false).deriveAndBuild();
        assertFalse(bundle.deferredActivation());
    }

    @Test
    public void simple_bundle_without_derived_states_propagates_deferred_activation_flag() {
        var bundle = ClusterStateBundle
                .builder(annotatedStateOf("distributor:2 storage:2"))
                .deferredActivation(true) // defaults to false
                .deriveAndBuild();
        assertTrue(bundle.deferredActivation());
    }

    @Test
    public void cloning_preserves_false_deferred_activation_flag() {
        var bundle = createTestBundleBuilder(true).deferredActivation(false).deriveAndBuild();
        var derived = bundle.cloneWithMapper(Function.identity());
        assertEquals(bundle, derived);
    }

    @Test
    public void cloning_preserves_true_deferred_activation_flag() {
        var bundle = createTestBundleBuilder(true).deferredActivation(true).deriveAndBuild();
        var derived = bundle.cloneWithMapper(Function.identity());
        assertEquals(bundle, derived);
    }

    @Test
    public void cloning_preserves_feed_block_state() {
        var bundle = createTestBundleWithFeedBlock("foo");;
        var derived = bundle.cloneWithMapper(Function.identity());
        assertEquals(bundle, derived);
    }

}