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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// 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 com.yahoo.vespa.clustercontroller.core.hostinfo.HostInfo;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class StateVersionTrackerTest {
private static AnnotatedClusterState stateWithoutAnnotations(String stateStr) {
return AnnotatedClusterState.withoutAnnotations(ClusterState.stateFromString(stateStr));
}
private static ClusterStateBundle stateBundleWithoutAnnotations(String stateStr) {
return ClusterStateBundle.ofBaselineOnly(stateWithoutAnnotations(stateStr));
}
private static StateVersionTracker createWithMockedMetrics() {
return new StateVersionTracker(1.0);
}
private static void updateAndPromote(final StateVersionTracker versionTracker,
final AnnotatedClusterState state,
final long timeMs)
{
versionTracker.updateLatestCandidateStateBundle(ClusterStateBundle.ofBaselineOnly(state));
versionTracker.promoteCandidateToVersionedState(timeMs);
}
@Test
void version_is_incremented_when_new_state_is_applied() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.setVersionRetrievedFromZooKeeper(100);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:2 storage:2"), 123);
assertEquals(101, versionTracker.getCurrentVersion());
assertEquals("version:101 distributor:2 storage:2", versionTracker.getVersionedClusterState().toString());
}
@Test
void version_is_1_upon_construction() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
assertEquals(1, versionTracker.getCurrentVersion());
}
@Test
void set_current_version_caps_lowest_version_to_1() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.setVersionRetrievedFromZooKeeper(0);
assertEquals(1, versionTracker.getCurrentVersion());
}
@Test
void new_version_from_zk_predicate_initially_false() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
assertFalse(versionTracker.hasReceivedNewVersionFromZooKeeper());
}
@Test
void new_version_from_zk_predicate_true_after_setting_zk_version() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.setVersionRetrievedFromZooKeeper(5);
assertTrue(versionTracker.hasReceivedNewVersionFromZooKeeper());
}
@Test
void new_version_from_zk_predicate_false_after_applying_higher_version() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.setVersionRetrievedFromZooKeeper(5);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:2 storage:2"), 123);
assertFalse(versionTracker.hasReceivedNewVersionFromZooKeeper());
}
@Test
void exposed_states_are_empty_upon_construction() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
assertTrue(versionTracker.getVersionedClusterState().toString().isEmpty());
assertTrue(versionTracker.getAnnotatedVersionedClusterState().getClusterState().toString().isEmpty());
}
@Test
void diff_from_initial_state_implies_changed_state() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.updateLatestCandidateStateBundle(stateBundleWithoutAnnotations("cluster:d"));
assertTrue(versionTracker.candidateChangedEnoughFromCurrentToWarrantPublish());
}
private static boolean stateChangedBetween(String fromState, String toState) {
final StateVersionTracker versionTracker = createWithMockedMetrics();
updateAndPromote(versionTracker, stateWithoutAnnotations(fromState), 123);
versionTracker.updateLatestCandidateStateBundle(stateBundleWithoutAnnotations(toState));
return versionTracker.candidateChangedEnoughFromCurrentToWarrantPublish();
}
@Test
void version_mismatch_not_counted_as_changed_state() {
assertFalse(stateChangedBetween("distributor:2 storage:2", "distributor:2 storage:2"));
}
@Test
void different_distributor_node_count_implies_changed_state() {
assertTrue(stateChangedBetween("distributor:2 storage:2", "distributor:3 storage:2"));
assertTrue(stateChangedBetween("distributor:3 storage:2", "distributor:2 storage:2"));
}
@Test
void different_storage_node_count_implies_changed_state() {
assertTrue(stateChangedBetween("distributor:2 storage:2", "distributor:2 storage:3"));
assertTrue(stateChangedBetween("distributor:2 storage:3", "distributor:2 storage:2"));
}
@Test
void different_distributor_node_state_implies_changed_state() {
assertTrue(stateChangedBetween("distributor:2 storage:2", "distributor:2 .0.s:d storage:2"));
assertTrue(stateChangedBetween("distributor:2 .0.s:d storage:2", "distributor:2 storage:2"));
}
@Test
void different_storage_node_state_implies_changed_state() {
assertTrue(stateChangedBetween("distributor:2 storage:2", "distributor:2 storage:2 .0.s:d"));
assertTrue(stateChangedBetween("distributor:2 storage:2 .0.s:d", "distributor:2 storage:2"));
}
@Test
void init_progress_change_not_counted_as_changed_state() {
assertFalse(stateChangedBetween("distributor:2 storage:2 .0.s:i .0.i:0.5",
"distributor:2 storage:2 .0.s:i .0.i:0.6"));
}
@Test
void lowest_observed_distribution_bit_is_initially_16() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
assertEquals(16, versionTracker.getLowestObservedDistributionBits());
}
@Test
void lowest_observed_distribution_bit_is_tracked_across_states() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
updateAndPromote(versionTracker, stateWithoutAnnotations("bits:15 distributor:2 storage:2"), 100);
assertEquals(15, versionTracker.getLowestObservedDistributionBits());
updateAndPromote(versionTracker, stateWithoutAnnotations("bits:17 distributor:2 storage:2"), 200);
assertEquals(15, versionTracker.getLowestObservedDistributionBits());
updateAndPromote(versionTracker, stateWithoutAnnotations("bits:14 distributor:2 storage:2"), 300);
assertEquals(14, versionTracker.getLowestObservedDistributionBits());
}
// For similarity purposes, only the cluster-wide bits matter, not the individual node state
// min used bits. The former is derived from the latter, but the latter is not visible in the
// published state (but _is_ visible in the internal ClusterState structures).
@Test
void per_node_min_bits_changes_are_not_considered_different() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
final AnnotatedClusterState stateWithMinBits = stateWithoutAnnotations("distributor:2 storage:2");
stateWithMinBits.getClusterState().setNodeState(
new Node(NodeType.STORAGE, 0),
new NodeState(NodeType.STORAGE, State.UP).setMinUsedBits(15));
updateAndPromote(versionTracker, stateWithMinBits, 123);
versionTracker.updateLatestCandidateStateBundle(stateBundleWithoutAnnotations("distributor:2 storage:2"));
assertFalse(versionTracker.candidateChangedEnoughFromCurrentToWarrantPublish());
}
@Test
void state_history_is_initially_empty() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
assertTrue(versionTracker.getClusterStateHistory().isEmpty());
}
private static ClusterStateHistoryEntry historyEntry(String state, long time) {
return ClusterStateHistoryEntry.makeFirstEntry(stateBundleWithoutAnnotations(state), time);
}
private static ClusterStateHistoryEntry historyEntry(String state, String prevState, long time) {
return ClusterStateHistoryEntry.makeSuccessor(stateBundleWithoutAnnotations(state),
stateBundleWithoutAnnotations(prevState), time);
}
@Test
void applying_state_adds_to_cluster_state_history() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:2 storage:2"), 100);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:3 storage:3"), 200);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:4 storage:4"), 300);
String s4 = "version:4 distributor:4 storage:4";
String s3 = "version:3 distributor:3 storage:3";
String s2 = "version:2 distributor:2 storage:2";
// Note: newest entry first
assertEquals(List.of(historyEntry(s4, s3, 300), historyEntry(s3, s2, 200), historyEntry(s2, 100)),
versionTracker.getClusterStateHistory());
}
@Test
void old_states_pruned_when_state_history_limit_reached() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.setMaxHistoryEntryCount(2);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:2 storage:2"), 100);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:3 storage:3"), 200);
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:4 storage:4"), 300);
String s5 = "version:5 distributor:5 storage:5";
String s4 = "version:4 distributor:4 storage:4";
String s3 = "version:3 distributor:3 storage:3";
String s2 = "version:2 distributor:2 storage:2";
assertEquals(List.of(historyEntry(s4, s3, 300), historyEntry(s3, s2, 200)),
versionTracker.getClusterStateHistory());
updateAndPromote(versionTracker, stateWithoutAnnotations("distributor:5 storage:5"), 400);
assertEquals(List.of(historyEntry(s5, s4, 400), historyEntry(s4, s3, 300)),
versionTracker.getClusterStateHistory());
}
@Test
void can_get_latest_non_published_candidate_state() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
AnnotatedClusterState candidate = stateWithoutAnnotations("distributor:2 storage:2");
versionTracker.updateLatestCandidateStateBundle(ClusterStateBundle.ofBaselineOnly(candidate));
assertEquals(candidate, versionTracker.getLatestCandidateState());
candidate = stateWithoutAnnotations("distributor:3 storage:3");
versionTracker.updateLatestCandidateStateBundle(ClusterStateBundle.ofBaselineOnly(candidate));
assertEquals(candidate, versionTracker.getLatestCandidateState());
}
private static ClusterState stateOf(String state) {
return ClusterState.stateFromString(state);
}
private static ClusterStateBundle baselineBundle(boolean alteredDefaultState) {
return ClusterStateBundle
.builder(AnnotatedClusterState.withoutAnnotations(stateOf("distributor:1 storage:1")))
.bucketSpaces("default")
.stateDeriver((state, space) -> {
AnnotatedClusterState derived = state.clone();
if (alteredDefaultState) {
derived.getClusterState().setNodeState(Node.ofStorage(0), new NodeState(NodeType.STORAGE, State.DOWN));
}
return derived;
})
.deriveAndBuild();
}
@Test
void version_change_check_takes_derived_states_into_account() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.updateLatestCandidateStateBundle(baselineBundle(false));
versionTracker.promoteCandidateToVersionedState(1234);
// Not marked changed with no changes across bucket spaces
versionTracker.updateLatestCandidateStateBundle(baselineBundle(false));
assertFalse(versionTracker.candidateChangedEnoughFromCurrentToWarrantPublish());
// Changing state in default space marks as sufficiently changed
versionTracker.updateLatestCandidateStateBundle(baselineBundle(true));
assertTrue(versionTracker.candidateChangedEnoughFromCurrentToWarrantPublish());
}
@Test
void buckets_pending_state_is_tracked_between_cluster_states() {
final StateVersionTracker tracker = createWithMockedMetrics();
final NodeInfo distributorNode = mock(DistributorNodeInfo.class);
when(distributorNode.isDistributor()).thenReturn(true);
assertFalse(tracker.bucketSpaceMergeCompletionStateHasChanged());
// Set baseline state
tracker.updateLatestCandidateStateBundle(ClusterStateBundle
.ofBaselineOnly(stateWithoutAnnotations("distributor:1 storage:1")));
tracker.promoteCandidateToVersionedState(1234);
assertFalse(tracker.bucketSpaceMergeCompletionStateHasChanged());
// Current node not in previous stats
tracker.handleUpdatedHostInfo(distributorNode, createHostInfo(0));
assertTrue(tracker.bucketSpaceMergeCompletionStateHasChanged());
// Sync aggregated stats
tracker.updateLatestCandidateStateBundle(ClusterStateBundle
.ofBaselineOnly(stateWithoutAnnotations("distributor:1 storage:1")));
// Give 'global' bucket space no buckets pending, which is the same as previous stats
tracker.handleUpdatedHostInfo(distributorNode, createHostInfo(0));
assertFalse(tracker.bucketSpaceMergeCompletionStateHasChanged());
// Give 'global' bucket space buckets pending, which is different from previous stats
tracker.handleUpdatedHostInfo(distributorNode, createHostInfo(1));
assertTrue(tracker.bucketSpaceMergeCompletionStateHasChanged());
tracker.updateLatestCandidateStateBundle(ClusterStateBundle
.ofBaselineOnly(stateWithoutAnnotations("distributor:1 storage:1")));
assertFalse(tracker.bucketSpaceMergeCompletionStateHasChanged());
}
@Test
void setting_zookeeper_retrieved_bundle_sets_current_versioned_state_and_resets_candidate_state() {
final StateVersionTracker versionTracker = createWithMockedMetrics();
versionTracker.setVersionRetrievedFromZooKeeper(100);
versionTracker.updateLatestCandidateStateBundle(
ClusterStateBundle.ofBaselineOnly(stateWithoutAnnotations("distributor:1 storage:1")));
versionTracker.promoteCandidateToVersionedState(12345);
ClusterStateBundle zkBundle = ClusterStateBundle.ofBaselineOnly(stateWithoutAnnotations("version:199 distributor:2 storage:2"));
versionTracker.setVersionRetrievedFromZooKeeper(200);
versionTracker.setClusterStateBundleRetrievedFromZooKeeper(zkBundle);
assertEquals(AnnotatedClusterState.emptyState(), versionTracker.getLatestCandidateState());
assertEquals(zkBundle, versionTracker.getVersionedClusterStateBundle());
assertEquals(200, versionTracker.getCurrentVersion()); // Not from bundle, but from explicitly stored version
}
private HostInfo createHostInfo(long bucketsPending) {
return HostInfo.createHostInfo(
"{\n" +
"\"cluster-state-version\": 2,\n" +
"\"distributor\": {\n" +
" \"storage-nodes\": [\n" +
" {\n" +
" \"node-index\": 0,\n" +
" \"bucket-spaces\": [\n" +
" {\n" +
" \"name\": \"global\"\n," +
" \"buckets\": {\n" +
" \"total\": 5,\n" +
" \"pending\": " + bucketsPending + "\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}\n" +
"}");
}
}
|