aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/NodeStateChangeCheckerTest.java
blob: 199a23f49bab09c750d3ec2588d0849f73653f21 (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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
// Copyright Vespa.ai. 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.log.LogSetup;
import com.yahoo.vdslib.distribution.ConfiguredNode;
import com.yahoo.vdslib.distribution.Distribution;
import com.yahoo.vdslib.state.ClusterState;
import com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.State;
import com.yahoo.vespa.clustercontroller.core.hostinfo.HostInfo;
import com.yahoo.vespa.config.content.StorDistributionConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList;
import java.util.List;

import static com.yahoo.vdslib.state.NodeType.DISTRIBUTOR;
import static com.yahoo.vdslib.state.NodeType.STORAGE;
import static com.yahoo.vdslib.state.State.DOWN;
import static com.yahoo.vdslib.state.State.INITIALIZING;
import static com.yahoo.vdslib.state.State.MAINTENANCE;
import static com.yahoo.vdslib.state.State.UP;
import static com.yahoo.vespa.clustercontroller.core.NodeStateChangeChecker.Result;
import static com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest.Condition.FORCE;
import static com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest.Condition.SAFE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class NodeStateChangeCheckerTest {

    private static final int requiredRedundancy = 4;
    private static final int currentClusterStateVersion = 2;

    private static final Node nodeDistributor = new Node(DISTRIBUTOR, 1);
    private static final Node nodeStorage = new Node(STORAGE, 1);

    private static final NodeState UP_NODE_STATE = new NodeState(STORAGE, UP);
    private static final NodeState MAINTENANCE_NODE_STATE = createNodeState(MAINTENANCE, "Orchestrator");
    private static final NodeState DOWN_NODE_STATE = createNodeState(DOWN, "RetireEarlyExpirer");

    private static NodeState createNodeState(State state, String description) {
        return new NodeState(STORAGE, state).setDescription(description);
    }

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

    private static ClusterState defaultAllUpClusterState() {
        return defaultAllUpClusterState(4);
    }

    private static ClusterState defaultAllUpClusterState(int nodeCount) {
        return clusterState(String.format("version:%d distributor:%d storage:%d",
                                          currentClusterStateVersion,
                                          nodeCount ,
                                          nodeCount));
    }

    private NodeStateChangeChecker createChangeChecker(ContentCluster cluster) {
        return new NodeStateChangeChecker(cluster, false);
    }

    private ContentCluster createCluster(int nodeCount, int maxNumberOfGroupsAllowedToBeDown) {
        return createCluster(nodeCount, 1, maxNumberOfGroupsAllowedToBeDown);
    }

    private ContentCluster createCluster(int nodeCount, int groupCount, int maxNumberOfGroupsAllowedToBeDown) {
        List<ConfiguredNode> nodes = createNodes(nodeCount);
        Distribution distribution = new Distribution(createDistributionConfig(nodeCount, groupCount));
        return new ContentCluster("Clustername", nodes, distribution, maxNumberOfGroupsAllowedToBeDown);
    }

    private String createDistributorHostInfo(int replicationfactor1, int replicationfactor2, int replicationfactor3) {
        return "{\n" +
                "    \"cluster-state-version\": 2,\n" +
                "    \"distributor\": {\n" +
                "        \"storage-nodes\": [\n" +
                "            {\n" +
                "                \"node-index\": 0,\n" +
                "                \"min-current-replication-factor\": " + replicationfactor1 + "\n" +
                "            },\n" +
                "            {\n" +
                "                \"node-index\": 1,\n" +
                "                \"min-current-replication-factor\": " + replicationfactor2 + "\n" +
                "            },\n" +
                "            {\n" +
                "                \"node-index\": 2,\n" +
                "                \"min-current-replication-factor\": " + replicationfactor3 + "\n" +
                "            },\n" +
                "            {\n" +
                "                \"node-index\": 3\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}\n";
    }

    private void markAllNodesAsReportingStateUp(ContentCluster cluster) {
        final ClusterInfo clusterInfo = cluster.clusterInfo();
        final int configuredNodeCount = cluster.clusterInfo().getConfiguredNodes().size();
        for (int i = 0; i < configuredNodeCount; i++) {
            clusterInfo.getDistributorNodeInfo(i).setReportedState(new NodeState(DISTRIBUTOR, UP), 0);
            clusterInfo.getDistributorNodeInfo(i).setHostInfo(HostInfo.createHostInfo(createDistributorHostInfo(4, 5, 6)));
            clusterInfo.getStorageNodeInfo(i).setReportedState(new NodeState(STORAGE, UP), 0);
        }
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeWithForce(int maxNumberOfGroupsAllowedToBeDown) {
        var nodeStateChangeChecker = createChangeChecker(createCluster(1, maxNumberOfGroupsAllowedToBeDown));
        NodeState newState = new NodeState(STORAGE, INITIALIZING);
        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeDistributor, defaultAllUpClusterState(), FORCE,
                UP_NODE_STATE, newState);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testDeniedInMoratorium(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        var nodeStateChangeChecker = new NodeStateChangeChecker(cluster, true);
        Result result = nodeStateChangeChecker.evaluateTransition(
                new Node(STORAGE, 10), defaultAllUpClusterState(), SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Master cluster controller is bootstrapping and in moratorium", result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testUnknownStorageNode(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        var nodeStateChangeChecker = createChangeChecker(cluster);
        Result result = nodeStateChangeChecker.evaluateTransition(
                new Node(STORAGE, 10), defaultAllUpClusterState(), SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Unknown node storage.10", result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSafeMaintenanceDisallowedWhenOtherStorageNodeInFlatClusterIsSuspended(int maxNumberOfGroupsAllowedToBeDown) {
        // Nodes 0-3, storage node 0 being in maintenance with "Orchestrator" description.
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        setStorageNodeWantedStateToMaintenance(cluster, 0);
        var nodeStateChangeChecker = createChangeChecker(cluster);
        ClusterState clusterStateWith0InMaintenance = clusterState(String.format(
                "version:%d distributor:4 storage:4 .0.s:m",
                currentClusterStateVersion));

        Result result = nodeStateChangeChecker.evaluateTransition(
                new Node(STORAGE, 1), clusterStateWith0InMaintenance,
                SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("At most one node can have a wanted state when #groups = 1: Other storage node 0 has wanted state Maintenance",
                result.reason());
    }

    @Test
    void testMaintenanceAllowedFor2Of4Groups() {
        int maxNumberOfGroupsAllowedToBeDown = 2;
        // 4 groups with 1 node in each group
        var cluster = createCluster(4, 4, maxNumberOfGroupsAllowedToBeDown);
        setAllNodesUp(cluster, HostInfo.createHostInfo(createDistributorHostInfo(4, 5, 6)));
        var nodeStateChangeChecker = createChangeChecker(cluster);

        // All nodes up, set a storage node in group 0 to maintenance
        {
            int nodeIndex = 0;
            checkSettingToMaintenanceIsAllowed(nodeIndex, nodeStateChangeChecker, defaultAllUpClusterState());
            setStorageNodeWantedStateToMaintenance(cluster, nodeIndex);
        }

        // Node in group 0 in maintenance, set storage node in group 1 to maintenance
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:4 .0.s:d storage:4 .0.s:m", currentClusterStateVersion));
            int nodeIndex = 1;
            checkSettingToMaintenanceIsAllowed(nodeIndex, nodeStateChangeChecker, clusterState);
            setStorageNodeWantedStateToMaintenance(cluster, nodeIndex);
        }

        // Nodes in group 0 and 1 in maintenance, try to set storage node in group 2 to maintenance while storage node 2 is down, should fail
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:4 storage:4 .0.s:m .1.s:m .2.s:d", currentClusterStateVersion));
            int nodeIndex = 2;
            cluster.clusterInfo().getStorageNodeInfo(nodeIndex).setReportedState(new NodeState(STORAGE, DOWN), 0);
            Node node = new Node(STORAGE, nodeIndex);
            Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed(), result.toString());
            assertFalse(result.isAlreadySet());
            assertEquals("At most 2 groups can have wanted state: [0, 1, 2]", result.reason());
        }

        // Nodes in group 0 and 1 in maintenance, try to set storage node in group 2 to maintenance, should fail
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:4 storage:4 .0.s:m .1.s:m", currentClusterStateVersion));
            int nodeIndex = 2;
            Node node = new Node(STORAGE, nodeIndex);
            Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed(), result.toString());
            assertFalse(result.isAlreadySet());
            assertEquals("At most 2 groups can have wanted state: [0, 1]", result.reason());
        }

    }

    @Test
    void testMaintenanceAllowedFor2Of4Groups8Nodes() {
        int maxNumberOfGroupsAllowedToBeDown = 2;
        // 4 groups with 2 nodes in each group
        var cluster = createCluster(8, 4, maxNumberOfGroupsAllowedToBeDown);
        setAllNodesUp(cluster, HostInfo.createHostInfo(createDistributorHostInfo(4, 5, 6)));
        var nodeStateChangeChecker = createChangeChecker(cluster);

        // All nodes up, set a storage node in group 0 to maintenance
        {
            ClusterState clusterState = defaultAllUpClusterState(8);
            int nodeIndex = 0;
            checkSettingToMaintenanceIsAllowed(nodeIndex, nodeStateChangeChecker, clusterState);
            setStorageNodeWantedStateToMaintenance(cluster, nodeIndex);
        }

        // 1 Node in group 0 in maintenance, try to set node 1 in group 0 to maintenance
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 .0.s:d storage:8 .0.s:m", currentClusterStateVersion));
            int nodeIndex = 1;
            checkSettingToMaintenanceIsAllowed(nodeIndex, nodeStateChangeChecker, clusterState);
            setStorageNodeWantedStateToMaintenance(cluster, nodeIndex);
        }

        // 2 nodes in group 0 in maintenance, try to set storage node 2 in group 1 to maintenance
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 storage:8 .0.s:m .1.s:m", currentClusterStateVersion));
            int nodeIndex = 2;
            checkSettingToMaintenanceIsAllowed(nodeIndex, nodeStateChangeChecker, clusterState);
            setStorageNodeWantedStateToMaintenance(cluster, nodeIndex);
        }

        // 2 nodes in group 0 and 1 in group 1 in maintenance, try to set storage node 4 in group 2 to maintenance, should fail (different group)
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 storage:8 .0.s:m .1.s:m .2.s:m", currentClusterStateVersion));
            int nodeIndex = 4;
            Node node = new Node(STORAGE, nodeIndex);
            Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed(), result.toString());
            assertFalse(result.isAlreadySet());
            assertEquals("At most 2 groups can have wanted state: [0, 1]", result.reason());
        }

        // 2 nodes in group 0 and 1 in group 1 in maintenance, try to set storage node 3 in group 1 to maintenance
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 storage:8 .0.s:m .1.s:m .2.s:m", currentClusterStateVersion));
            int nodeIndex = 3;
            checkSettingToMaintenanceIsAllowed(nodeIndex, nodeStateChangeChecker, clusterState);
            setStorageNodeWantedStateToMaintenance(cluster, nodeIndex);
        }

        // 2 nodes in group 0 and 2 nodes in group 1 in maintenance, try to set storage node 4 in group 2 to maintenance, should fail
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 storage:8 .0.s:m .1.s:m .2.s:m .3.s:m", currentClusterStateVersion));
            int nodeIndex = 4;
            Node node = new Node(STORAGE, nodeIndex);
            Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed(), result.toString());
            assertFalse(result.isAlreadySet());
            assertEquals("At most 2 groups can have wanted state: [0, 1]", result.reason());
        }

        // 2 nodes in group 0 in maintenance, storage node 3 in group 1 is in maintenance with another description
        // (set in maintenance by operator), try to set storage node 2 in group 1 to maintenance, should be allowed
        {
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 storage:8 .0.s:m .1.s:m .3.s:m", currentClusterStateVersion));
            setStorageNodeWantedState(cluster, 3, MAINTENANCE, "Maintenance, set by operator");  // Set to another description
            setStorageNodeWantedState(cluster, 2, UP, ""); // Set back to UP, want to set this to maintenance again
            int nodeIndex = 2;
            Node node = new Node(STORAGE, nodeIndex);
            Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertTrue(result.allowed(), result.toString());
            assertFalse(result.isAlreadySet());
        }

        // 2 nodes in group 0 up again but buckets not in sync and 2 nodes in group 1 in maintenance,
        // try to set storage node 4 in group 2 to maintenance
        {
            setStorageNodeWantedState(cluster, 0, MAINTENANCE, "Orchestrator");
            setStorageNodeWantedState(cluster, 1, MAINTENANCE, "Orchestrator");
            setStorageNodeWantedState(cluster, 2, UP, ""); // Set up again
            setStorageNodeWantedState(cluster, 3, UP, ""); // Set up again
            ClusterState clusterState = clusterState(String.format("version:%d distributor:8 storage:8 .0.s:m .1.s:m", currentClusterStateVersion));

            // Set bucket in sync to 1 for node 2 in group 1
            var distributorHostInfo = createDistributorHostInfo(1, 2, 1);
            cluster.clusterInfo().getDistributorNodeInfo(0).setHostInfo(HostInfo.createHostInfo(distributorHostInfo));
            cluster.clusterInfo().getDistributorNodeInfo(1).setHostInfo(HostInfo.createHostInfo(distributorHostInfo));
            cluster.clusterInfo().getDistributorNodeInfo(2).setHostInfo(HostInfo.createHostInfo(distributorHostInfo));

            int nodeIndex = 2;
            Node node = new Node(STORAGE, nodeIndex);
            Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed(), result.toString());
            assertFalse(result.isAlreadySet());
            assertEquals("Distributor 0 says storage node 0 has buckets with redundancy as low as 1, but we require at least 4", result.reason());
        }

    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSafeMaintenanceDisallowedWhenOtherDistributorInFlatClusterIsSuspended(int maxNumberOfGroupsAllowedToBeDown) {
        // Nodes 0-3, distributor 0 being down with "Orchestrator" description.
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        setDistributorNodeWantedState(cluster, 0, DOWN, "Orchestrator");
        var nodeStateChangeChecker = createChangeChecker(cluster);
        ClusterState clusterStateWith0InMaintenance = clusterState(String.format(
                "version:%d distributor:4 .0.s:d storage:4",
                currentClusterStateVersion));

        Result result = nodeStateChangeChecker.evaluateTransition(
                new Node(STORAGE, 1), clusterStateWith0InMaintenance,
                SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("At most one node can have a wanted state when #groups = 1: Other distributor 0 has wanted state Down",
                result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSafeMaintenanceDisallowedWhenDistributorInGroupIsDown(int maxNumberOfGroupsAllowedToBeDown) {
        // Nodes 0-3, distributor 0 being in maintenance with "Orchestrator" description.
        // 2 groups: nodes 0-1 is group 0, 2-3 is group 1.
        ContentCluster cluster = createCluster(4, 2, maxNumberOfGroupsAllowedToBeDown);
        setDistributorNodeWantedState(cluster, 0, DOWN, "Orchestrator");
        var nodeStateChangeChecker = new NodeStateChangeChecker(cluster, false);
        ClusterState clusterStateWith0InMaintenance = clusterState(String.format(
                "version:%d distributor:4 .0.s:d storage:4",
                currentClusterStateVersion));

        {
            // Denied for node 2 in group 1, since distributor 0 in group 0 is down
            Result result = nodeStateChangeChecker.evaluateTransition(
                    new Node(STORAGE, 2), clusterStateWith0InMaintenance,
                    SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed());
            assertFalse(result.isAlreadySet());
            if (maxNumberOfGroupsAllowedToBeDown >= 1)
                assertEquals("Wanted state already set for another node in groups: [0]", result.reason());
            else
                assertEquals("At most one group can have wanted state: Other distributor 0 in group 0 has wanted state Down", result.reason());
        }

        {
            // Even node 1 of group 0 is not permitted, as node 0 is not considered
            // suspended since only the distributor has been set down.
            Result result = nodeStateChangeChecker.evaluateTransition(
                    new Node(STORAGE, 1), clusterStateWith0InMaintenance,
                    SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            if (maxNumberOfGroupsAllowedToBeDown >= 1) {
                assertFalse(result.allowed(), result.reason());
                assertEquals("Wanted state already set for another node in groups: [0]", result.reason());
            } else {
                assertFalse(result.allowed(), result.reason());
                assertEquals("Another distributor wants state DOWN: 0", result.reason());
            }
        }
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSafeMaintenanceWhenOtherStorageNodeInGroupIsSuspended(int maxNumberOfGroupsAllowedToBeDown) {
        // Nodes 0-3, storage node 0 being in maintenance with "Orchestrator" description.
        // 2 groups: nodes 0-1 is group 0, 2-3 is group 1.
        ContentCluster cluster = createCluster(4, 2, maxNumberOfGroupsAllowedToBeDown);
        setStorageNodeWantedState(cluster, 0, MAINTENANCE, "Orchestrator");
        var nodeStateChangeChecker = new NodeStateChangeChecker(cluster, false);
        ClusterState clusterStateWith0InMaintenance = clusterState(String.format(
                "version:%d distributor:4 storage:4 .0.s:m",
                currentClusterStateVersion));

        {
            // Denied for node 2 in group 1, since node 0 in group 0 is in maintenance
            Result result = nodeStateChangeChecker.evaluateTransition(
                    new Node(STORAGE, 2), clusterStateWith0InMaintenance,
                    SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertFalse(result.allowed());
            assertFalse(result.isAlreadySet());
            if (maxNumberOfGroupsAllowedToBeDown >= 1)
                assertEquals("At most 1 groups can have wanted state: [0]", result.reason());
            else
                assertEquals("At most one group can have wanted state: Other storage node 0 in group 0 has wanted state Maintenance",
                             result.reason());
        }

        {
            // Permitted for node 1 in group 0, since node 0 is already in maintenance with
            // description Orchestrator, and it is in the same group
            Result result = nodeStateChangeChecker.evaluateTransition(
                    new Node(STORAGE, 1), clusterStateWith0InMaintenance,
                    SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
            assertTrue(result.allowed(), result.reason());
            assertFalse(result.isAlreadySet());
        }
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSafeSetStateDistributors(int maxNumberOfGroupsAllowedToBeDown) {
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(createCluster(1, 1, maxNumberOfGroupsAllowedToBeDown));
        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeDistributor, defaultAllUpClusterState(), SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertTrue(result.reason().contains("Safe-set of node state is only supported for storage nodes"));
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeSafeMissingStorage(int maxNumberOfGroupsAllowedToBeDown) {
        // Create a content cluster with 4 nodes, and storage node with index 3 down.
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        setAllNodesUp(cluster, HostInfo.createHostInfo(createDistributorHostInfo(4, 5, 6)));
        cluster.clusterInfo().getStorageNodeInfo(3).setReportedState(new NodeState(STORAGE, DOWN), 0);
        ClusterState clusterStateWith3Down = clusterState(String.format(
                "version:%d distributor:4 storage:4 .3.s:d",
                currentClusterStateVersion));

        // We should then be denied setting storage node 1 safely to maintenance.
        var nodeStateChangeChecker = createChangeChecker(cluster);
        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, clusterStateWith3Down, SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Another storage node has state DOWN: 3", result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeStorageSafeYes(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = transitionToMaintenanceWithNoStorageNodesDown(createCluster(4, 1, maxNumberOfGroupsAllowedToBeDown), defaultAllUpClusterState());
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSetUpFailsIfReportedIsDown(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);
        // Not setting nodes up -> all are down

        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, defaultAllUpClusterState(), SAFE,
                MAINTENANCE_NODE_STATE, UP_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    // A node may be reported as Up but have a generated state of Down if it's part of
    // nodes taken down implicitly due to a group having too low node availability.
    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSetUpSucceedsIfReportedIsUpButGeneratedIsDown(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);

        markAllNodesAsReportingStateUp(cluster);

        ClusterState stateWithNodeDown = clusterState(String.format(
                "version:%d distributor:4 storage:4 .%d.s:d",
                currentClusterStateVersion, nodeStorage.getIndex()));

        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, stateWithNodeDown, SAFE,
                MAINTENANCE_NODE_STATE, UP_NODE_STATE);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanSetUpEvenIfOldWantedStateIsDown(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);
        setAllNodesUp(cluster, HostInfo.createHostInfo(createDistributorHostInfo(4, 3, 6)));

        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, defaultAllUpClusterState(), SAFE,
                new NodeState(STORAGE, DOWN), UP_NODE_STATE);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeStorageSafeNo(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);
        setAllNodesUp(cluster, HostInfo.createHostInfo(createDistributorHostInfo(4, 3, 6)));

        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, defaultAllUpClusterState(), SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Distributor 0 says storage node 1 has buckets with redundancy as low as 3, but we require at least 4",
                result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeIfMissingMinReplicationFactor(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);
        setAllNodesUp(cluster, HostInfo.createHostInfo(createDistributorHostInfo(4, 3, 6)));

        Result result = nodeStateChangeChecker.evaluateTransition(
                new Node(STORAGE, 3), defaultAllUpClusterState(), SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeIfStorageNodeMissingFromNodeInfo(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);
        String hostInfo = "{\n" +
                "    \"cluster-state-version\": 2,\n" +
                "    \"distributor\": {\n" +
                "        \"storage-nodes\": [\n" +
                "            {\n" +
                "                \"node-index\": 0,\n" +
                "                \"min-current-replication-factor\": " + requiredRedundancy + "\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}\n";
        setAllNodesUp(cluster, HostInfo.createHostInfo(hostInfo));

        Result result = nodeStateChangeChecker.evaluateTransition(
                new Node(STORAGE, 1), defaultAllUpClusterState(), SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testMissingDistributorState(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);
        cluster.clusterInfo().getStorageNodeInfo(1).setReportedState(new NodeState(STORAGE, UP), 0);

        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, defaultAllUpClusterState(), SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Distributor node 0 has not reported any cluster state version yet.", result.reason());
    }

    private Result transitionToSameState(State state, String oldDescription, String newDescription, int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);

        NodeState currentNodeState = createNodeState(state, oldDescription);
        NodeState newNodeState = createNodeState(state, newDescription);
        return nodeStateChangeChecker.evaluateTransition(
                nodeStorage, defaultAllUpClusterState(), SAFE,
                currentNodeState, newNodeState);
    }

    private Result transitionToSameState(String oldDescription, String newDescription, int maxNumberOfGroupsAllowedToBeDown) {
        return transitionToSameState(MAINTENANCE, oldDescription, newDescription, maxNumberOfGroupsAllowedToBeDown);
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSettingUpWhenUpCausesAlreadySet(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = transitionToSameState(UP, "foo", "bar", maxNumberOfGroupsAllowedToBeDown);
        assertTrue(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testSettingAlreadySetState(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = transitionToSameState("foo", "foo", maxNumberOfGroupsAllowedToBeDown);
        assertFalse(result.allowed());
        assertTrue(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testDifferentDescriptionImpliesDenied(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = transitionToSameState("foo", "bar", maxNumberOfGroupsAllowedToBeDown);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    private Result transitionToMaintenanceWithOneStorageNodeDown(ContentCluster cluster, ClusterState clusterState) {
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);

        for (int x = 0; x < cluster.clusterInfo().getConfiguredNodes().size(); x++) {
            cluster.clusterInfo().getDistributorNodeInfo(x).setReportedState(new NodeState(DISTRIBUTOR, UP), 0);
            cluster.clusterInfo().getDistributorNodeInfo(x).setHostInfo(HostInfo.createHostInfo(createDistributorHostInfo(4, 5, 6)));
            cluster.clusterInfo().getStorageNodeInfo(x).setReportedState(new NodeState(STORAGE, UP), 0);
        }

        return nodeStateChangeChecker.evaluateTransition(
                nodeStorage, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
    }

    private void setAllNodesUp(ContentCluster cluster, HostInfo distributorHostInfo) {
        for (int x = 0; x < cluster.clusterInfo().getConfiguredNodes().size(); x++) {
            State state = UP;
            cluster.clusterInfo().getDistributorNodeInfo(x).setReportedState(new NodeState(DISTRIBUTOR, state), 0);
            cluster.clusterInfo().getDistributorNodeInfo(x).setHostInfo(distributorHostInfo);
            cluster.clusterInfo().getStorageNodeInfo(x).setReportedState(new NodeState(STORAGE, state), 0);
        }
    }

    private Result transitionToMaintenanceWithNoStorageNodesDown(ContentCluster cluster, ClusterState clusterState) {
        return transitionToMaintenanceWithOneStorageNodeDown(cluster, clusterState);
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeWhenAllUp(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = transitionToMaintenanceWithNoStorageNodesDown(createCluster(4, maxNumberOfGroupsAllowedToBeDown), defaultAllUpClusterState());
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeWhenAllUpOrRetired(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = transitionToMaintenanceWithNoStorageNodesDown(createCluster(4, maxNumberOfGroupsAllowedToBeDown), defaultAllUpClusterState());
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCanUpgradeWhenStorageIsDown(int maxNumberOfGroupsAllowedToBeDown) {
        ClusterState clusterState = defaultAllUpClusterState();
        var storageNodeIndex = nodeStorage.getIndex();

        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeState downNodeState = new NodeState(STORAGE, DOWN);
        cluster.clusterInfo().getStorageNodeInfo(storageNodeIndex).setReportedState(downNodeState, 4 /* time */);
        clusterState.setNodeState(new Node(STORAGE, storageNodeIndex), downNodeState);

        Result result = transitionToMaintenanceWithOneStorageNodeDown(cluster, clusterState);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testCannotUpgradeWhenOtherStorageIsDown(int maxNumberOfGroupsAllowedToBeDown) {
        int otherIndex = 2;
        // If this fails, just set otherIndex to some other valid index.
        assertNotEquals(nodeStorage.getIndex(), otherIndex);

        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        ClusterState clusterState = defaultAllUpClusterState();
        NodeState downNodeState = new NodeState(STORAGE, DOWN);
        cluster.clusterInfo().getStorageNodeInfo(otherIndex).setReportedState(downNodeState, 4 /* time */);
        clusterState.setNodeState(new Node(STORAGE, otherIndex), downNodeState);

        Result result = transitionToMaintenanceWithOneStorageNodeDown(cluster, clusterState);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertTrue(result.reason().contains("Another storage node has state DOWN: 2"));
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testNodeRatioRequirementConsidersGeneratedNodeStates(int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);

        markAllNodesAsReportingStateUp(cluster);

        // Both minRatioOfStorageNodesUp and minStorageNodesUp imply that a single node being
        // in state Down should halt the upgrade. This must also take into account the generated
        // state, not just the reported state. In this case, all nodes are reported as being Up
        // but one node has a generated state of Down.
        ClusterState stateWithNodeDown = clusterState(String.format(
                "version:%d distributor:4 storage:4 .3.s:d",
                currentClusterStateVersion));

        Result result = nodeStateChangeChecker.evaluateTransition(
                nodeStorage, stateWithNodeDown, SAFE,
                UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testDownDisallowedByNonRetiredState(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = evaluateDownTransition(
                defaultAllUpClusterState(),
                UP,
                currentClusterStateVersion,
                0,
                maxNumberOfGroupsAllowedToBeDown);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Only retired nodes are allowed to be set to DOWN in safe mode - is Up", result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testDownDisallowedByBuckets(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = evaluateDownTransition(
                retiredClusterStateSuffix(),
                UP,
                currentClusterStateVersion,
                1,
                maxNumberOfGroupsAllowedToBeDown);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("The storage node manages 1 buckets", result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testDownDisallowedByReportedState(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = evaluateDownTransition(
                retiredClusterStateSuffix(),
                INITIALIZING,
                currentClusterStateVersion,
                0,
                maxNumberOfGroupsAllowedToBeDown);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Reported state (Initializing) is not UP, so no bucket data is available", result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testDownDisallowedByVersionMismatch(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = evaluateDownTransition(
                retiredClusterStateSuffix(),
                UP,
                currentClusterStateVersion - 1,
                0,
                maxNumberOfGroupsAllowedToBeDown);
        assertFalse(result.allowed());
        assertFalse(result.isAlreadySet());
        assertEquals("Cluster controller at version 2 got info for storage node 1 at a different version 1",
                result.reason());
    }

    @ParameterizedTest
    @ValueSource(ints = {-1, 1})
    void testAllowedToSetDown(int maxNumberOfGroupsAllowedToBeDown) {
        Result result = evaluateDownTransition(
                retiredClusterStateSuffix(),
                UP,
                currentClusterStateVersion,
                0,
                maxNumberOfGroupsAllowedToBeDown);
        assertTrue(result.allowed());
        assertFalse(result.isAlreadySet());
    }

    private Result evaluateDownTransition(ClusterState clusterState,
                                          State reportedState,
                                          int hostInfoClusterStateVersion,
                                          int lastAlldisksBuckets,
                                          int maxNumberOfGroupsAllowedToBeDown) {
        ContentCluster cluster = createCluster(4, maxNumberOfGroupsAllowedToBeDown);
        NodeStateChangeChecker nodeStateChangeChecker = createChangeChecker(cluster);

        StorageNodeInfo nodeInfo = cluster.clusterInfo().getStorageNodeInfo(nodeStorage.getIndex());
        nodeInfo.setReportedState(new NodeState(STORAGE, reportedState), 0);
        nodeInfo.setHostInfo(createHostInfoWithMetrics(hostInfoClusterStateVersion, lastAlldisksBuckets));

        return nodeStateChangeChecker.evaluateTransition(
                nodeStorage, clusterState, SAFE,
                UP_NODE_STATE, DOWN_NODE_STATE);
    }

    private ClusterState retiredClusterStateSuffix() {
        return clusterState(String.format("version:%d distributor:4 storage:4 .%d.s:r",
                currentClusterStateVersion,
                nodeStorage.getIndex()));
    }

    private static HostInfo createHostInfoWithMetrics(int clusterStateVersion, int lastAlldisksBuckets) {
        return HostInfo.createHostInfo(String.format("{\n" +
                        "  \"metrics\":\n" +
                        "  {\n" +
                        "    \"snapshot\":\n" +
                        "    {\n" +
                        "      \"from\":1494940706,\n" +
                        "      \"to\":1494940766\n" +
                        "    },\n" +
                        "    \"values\":\n" +
                        "    [\n" +
                        "      {\n" +
                        "        \"name\":\"vds.datastored.alldisks.buckets\",\n" +
                        "        \"description\":\"buckets managed\",\n" +
                        "        \"values\":\n" +
                        "        {\n" +
                        "          \"average\":262144.0,\n" +
                        "          \"count\":1,\n" +
                        "          \"rate\":0.016666,\n" +
                        "          \"min\":262144,\n" +
                        "          \"max\":262144,\n" +
                        "          \"last\":%d\n" +
                        "        },\n" +
                        "        \"dimensions\":\n" +
                        "        {\n" +
                        "        }\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\":\"vds.datastored.alldisks.docs\",\n" +
                        "        \"description\":\"documents stored\",\n" +
                        "        \"values\":\n" +
                        "        {\n" +
                        "          \"average\":154689587.0,\n" +
                        "          \"count\":1,\n" +
                        "          \"rate\":0.016666,\n" +
                        "          \"min\":154689587,\n" +
                        "          \"max\":154689587,\n" +
                        "          \"last\":154689587\n" +
                        "        },\n" +
                        "        \"dimensions\":\n" +
                        "        {\n" +
                        "        }\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\":\"vds.datastored.bucket_space.buckets_total\",\n" +
                        "        \"description\":\"Total number buckets present in the bucket space (ready + not ready)\",\n" +
                        "        \"values\":\n" +
                        "        {\n" +
                        "          \"average\":0.0,\n" +
                        "          \"sum\":0.0,\n" +
                        "          \"count\":1,\n" +
                        "          \"rate\":0.016666,\n" +
                        "          \"min\":0,\n" +
                        "          \"max\":0,\n" +
                        "          \"last\":0\n" +
                        "        },\n" +
                        "        \"dimensions\":\n" +
                        "        {\n" +
                        "          \"bucketSpace\":\"global\"\n" +
                        "        }\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\":\"vds.datastored.bucket_space.buckets_total\",\n" +
                        "        \"description\":\"Total number buckets present in the bucket space (ready + not ready)\",\n" +
                        "        \"values\":\n" +
                        "        {\n" +
                        "          \"average\":129.0,\n" +
                        "          \"sum\":129.0,\n" +
                        "          \"count\":1,\n" +
                        "          \"rate\":0.016666,\n" +
                        "          \"min\":129,\n" +
                        "          \"max\":129,\n" +
                        "          \"last\":%d\n" +
                        "        },\n" +
                        "        \"dimensions\":\n" +
                        "        {\n" +
                        "          \"bucketSpace\":\"default\"\n" +
                        "        }\n" +
                        "      }\n" +
                        "    ]\n" +
                        "  },\n" +
                        "  \"cluster-state-version\":%d\n" +
                        "}",
                lastAlldisksBuckets, lastAlldisksBuckets, clusterStateVersion));
    }

    private List<ConfiguredNode> createNodes(int count) {
        List<ConfiguredNode> nodes = new ArrayList<>();
        for (int i = 0; i < count; i++)
            nodes.add(new ConfiguredNode(i, false));
        return nodes;
    }

    private StorDistributionConfig createDistributionConfig(int nodes) {
        var configBuilder = new StorDistributionConfig.Builder()
                .ready_copies(requiredRedundancy)
                .redundancy(requiredRedundancy)
                .initial_redundancy(requiredRedundancy);

        var groupBuilder = new StorDistributionConfig.Group.Builder()
                                    .index("invalid")
                                    .name("invalid")
                                    .capacity(nodes);
        int nodeIndex = 0;
        for (int j = 0; j < nodes; ++j, ++nodeIndex) {
            groupBuilder.nodes(new StorDistributionConfig.Group.Nodes.Builder()
                                       .index(nodeIndex));
        }
        configBuilder.group(groupBuilder);

        return configBuilder.build();
    }

    // When more than 1 group
    private StorDistributionConfig createDistributionConfig(int nodes, int groups) {
        if (groups == 1) return createDistributionConfig(nodes);

        if (nodes % groups != 0)
            throw new IllegalArgumentException("Cannot have " + groups + " groups with an odd number of nodes: " + nodes);

        int nodesPerGroup = nodes / groups;

        var configBuilder = new StorDistributionConfig.Builder()
                .active_per_leaf_group(true)
                .ready_copies(groups)
                .redundancy(groups)
                .initial_redundancy(groups);

        configBuilder.group(new StorDistributionConfig.Group.Builder()
                                    .index("invalid")
                                    .name("invalid")
                                    .capacity(nodes)
                                    .partitions("1|*"));

        int nodeIndex = 0;
        for (int i = 0; i < groups; ++i) {
            var groupBuilder = new StorDistributionConfig.Group.Builder()
                    .index(String.valueOf(i))
                    .name(String.valueOf(i))
                    .capacity(nodesPerGroup)
                    .partitions("");
            for (int j = 0; j < nodesPerGroup; ++j, ++nodeIndex) {
                groupBuilder.nodes(new StorDistributionConfig.Group.Nodes.Builder()
                                           .index(nodeIndex));
            }
            configBuilder.group(groupBuilder);
        }
        return configBuilder.build();
    }

    private void checkSettingToMaintenanceIsAllowed(int nodeIndex, NodeStateChangeChecker nodeStateChangeChecker, ClusterState clusterState) {
        Node node = new Node(STORAGE, nodeIndex);
        Result result = nodeStateChangeChecker.evaluateTransition(node, clusterState, SAFE, UP_NODE_STATE, MAINTENANCE_NODE_STATE);
        assertTrue(result.allowed(), result.toString());
        assertFalse(result.isAlreadySet());
        assertEquals("Preconditions fulfilled and new state different", result.reason());
    }

    private void setStorageNodeWantedStateToMaintenance(ContentCluster cluster, int nodeIndex) {
        setStorageNodeWantedState(cluster, nodeIndex, MAINTENANCE, "Orchestrator");
    }

    private void setStorageNodeWantedState(ContentCluster cluster, int nodeIndex, State state, String description) {
        NodeState nodeState = new NodeState(STORAGE, state);
        cluster.clusterInfo().getStorageNodeInfo(nodeIndex).setWantedState(nodeState.setDescription(description));
    }

    private void setDistributorNodeWantedState(ContentCluster cluster, int nodeIndex, State state, String description) {
        NodeState nodeState = new NodeState(DISTRIBUTOR, state);
        cluster.clusterInfo().getDistributorNodeInfo(nodeIndex).setWantedState(nodeState.setDescription(description));
    }

}