summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ClusterStateBundleTest.java
blob: db9c8b4dd7389fb72399b12087c23c80947f6ad7 (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
// 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.*;
import org.junit.Test;

import java.util.function.Function;

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 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 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' (deferred activation))"));
    }

    @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' (deferred activation))"));
    }

    @Test
    public void toString_without_derived_states_specifies_deferred_activation_iff_set() {
        var bundle = ClusterStateBundle.ofBaselineOnly(annotatedStateOf("distributor:2 storage:2"), 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"), 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_enabled_by_default() {
        ClusterStateBundle bundle = createTestBundle();
        assertTrue(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(false) // defaults to true
                .deriveAndBuild();
        assertFalse(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);
    }

}