|
19 | 19 | package org.elasticsearch.cluster;
|
20 | 20 |
|
21 | 21 | import org.elasticsearch.Version;
|
| 22 | +import org.elasticsearch.cluster.ClusterState.VotingConfiguration; |
22 | 23 | import org.elasticsearch.cluster.node.DiscoveryNode;
|
23 | 24 | import org.elasticsearch.cluster.node.DiscoveryNodes;
|
| 25 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
24 | 26 | import org.elasticsearch.common.settings.Settings;
|
| 27 | +import org.elasticsearch.common.util.set.Sets; |
25 | 28 | import org.elasticsearch.test.ESTestCase;
|
| 29 | +import org.elasticsearch.test.EqualsHashCodeTestUtils; |
| 30 | + |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.HashSet; |
| 33 | +import java.util.Set; |
26 | 34 |
|
27 | 35 | import static java.util.Collections.emptyMap;
|
28 | 36 | import static java.util.Collections.emptySet;
|
@@ -55,6 +63,70 @@ public void testSupersedes() {
|
55 | 63 |
|
56 | 64 | // state from the same master compare by version
|
57 | 65 | assertThat(withMaster1a.supersedes(withMaster1b), equalTo(withMaster1a.version() > withMaster1b.version()));
|
| 66 | + } |
| 67 | + |
| 68 | + public void testVotingConfiguration() { |
| 69 | + VotingConfiguration config0 = new VotingConfiguration(Sets.newHashSet()); |
| 70 | + assertThat(config0, equalTo(VotingConfiguration.EMPTY_CONFIG)); |
| 71 | + assertThat(config0.getNodeIds(), equalTo(Sets.newHashSet())); |
| 72 | + assertThat(config0.isEmpty(), equalTo(true)); |
| 73 | + assertThat(config0.hasQuorum(Sets.newHashSet()), equalTo(false)); |
| 74 | + assertThat(config0.hasQuorum(Sets.newHashSet("id1")), equalTo(false)); |
| 75 | + |
| 76 | + VotingConfiguration config1 = new VotingConfiguration(Sets.newHashSet("id1")); |
| 77 | + assertThat(config1.getNodeIds(), equalTo(Sets.newHashSet("id1"))); |
| 78 | + assertThat(config1.isEmpty(), equalTo(false)); |
| 79 | + assertThat(config1.hasQuorum(Sets.newHashSet("id1")), equalTo(true)); |
| 80 | + assertThat(config1.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true)); |
| 81 | + assertThat(config1.hasQuorum(Sets.newHashSet("id2")), equalTo(false)); |
| 82 | + assertThat(config1.hasQuorum(Sets.newHashSet()), equalTo(false)); |
| 83 | + |
| 84 | + VotingConfiguration config2 = new VotingConfiguration(Sets.newHashSet("id1", "id2")); |
| 85 | + assertThat(config2.getNodeIds(), equalTo(Sets.newHashSet("id1", "id2"))); |
| 86 | + assertThat(config2.isEmpty(), equalTo(false)); |
| 87 | + assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true)); |
| 88 | + assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id2", "id3")), equalTo(true)); |
| 89 | + assertThat(config2.hasQuorum(Sets.newHashSet("id1")), equalTo(false)); |
| 90 | + assertThat(config2.hasQuorum(Sets.newHashSet("id2")), equalTo(false)); |
| 91 | + assertThat(config2.hasQuorum(Sets.newHashSet("id3")), equalTo(false)); |
| 92 | + assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id3")), equalTo(false)); |
| 93 | + assertThat(config2.hasQuorum(Sets.newHashSet()), equalTo(false)); |
| 94 | + |
| 95 | + VotingConfiguration config3 = new VotingConfiguration(Sets.newHashSet("id1", "id2", "id3")); |
| 96 | + assertThat(config3.getNodeIds(), equalTo(Sets.newHashSet("id1", "id2", "id3"))); |
| 97 | + assertThat(config3.isEmpty(), equalTo(false)); |
| 98 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true)); |
| 99 | + assertThat(config3.hasQuorum(Sets.newHashSet("id2", "id3")), equalTo(true)); |
| 100 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id3")), equalTo(true)); |
| 101 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2", "id3")), equalTo(true)); |
| 102 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2", "id4")), equalTo(true)); |
| 103 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1")), equalTo(false)); |
| 104 | + assertThat(config3.hasQuorum(Sets.newHashSet("id2")), equalTo(false)); |
| 105 | + assertThat(config3.hasQuorum(Sets.newHashSet("id3")), equalTo(false)); |
| 106 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id4")), equalTo(false)); |
| 107 | + assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id4", "id5")), equalTo(false)); |
| 108 | + assertThat(config3.hasQuorum(Sets.newHashSet()), equalTo(false)); |
| 109 | + } |
58 | 110 |
|
| 111 | + public void testVotingConfigurationSerializationEqualsHashCode() { |
| 112 | + VotingConfiguration initialConfig = new VotingConfiguration( |
| 113 | + Sets.newHashSet(generateRandomStringArray(randomInt(10), 20, false))); |
| 114 | + EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialConfig, |
| 115 | + orig -> ESTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), VotingConfiguration::new), |
| 116 | + cfg -> { |
| 117 | + Set<String> newNodeIds = new HashSet<>(cfg.getNodeIds()); |
| 118 | + if (cfg.isEmpty() == false && randomBoolean()) { |
| 119 | + // remove random element |
| 120 | + newNodeIds.remove(randomFrom(cfg.getNodeIds())); |
| 121 | + } else if (cfg.isEmpty() == false && randomBoolean()) { |
| 122 | + // change random element |
| 123 | + newNodeIds.remove(randomFrom(cfg.getNodeIds())); |
| 124 | + newNodeIds.add(randomAlphaOfLength(20)); |
| 125 | + } else { |
| 126 | + // add random element |
| 127 | + newNodeIds.add(randomAlphaOfLength(20)); |
| 128 | + } |
| 129 | + return new VotingConfiguration(newNodeIds); |
| 130 | + }); |
59 | 131 | }
|
60 | 132 | }
|
0 commit comments