Skip to content

Add term and config to cluster state #32100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ task verifyVersions {
* the enabled state of every bwc task. It should be set back to true
* after the backport of the backcompat code is complete.
*/
final boolean bwc_tests_enabled = true
final String bwc_tests_disabled_issue = "" /* place a PR link here when committing bwc changes */
final boolean bwc_tests_enabled = false
final String bwc_tests_disabled_issue = "https://github.com/elastic/elasticsearch/issues/32006" // BWC handled at a later time
if (bwc_tests_enabled == false) {
if (bwc_tests_disabled_issue.isEmpty()) {
throw new GradleException("bwc_tests_disabled_issue must be set when bwc_tests_enabled == false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ protected void masterOperation(final ClusterStateRequest request, final ClusterS
ClusterState currentState = clusterService.state();
logger.trace("Serving cluster state request using version {}", currentState.version());
ClusterState.Builder builder = ClusterState.builder(currentState.getClusterName());
builder.term(currentState.term());
builder.version(currentState.version());
builder.stateUUID(currentState.stateUUID());
builder.lastCommittedConfiguration(currentState.getLastCommittedConfiguration());
builder.lastAcceptedConfiguration(currentState.getLastAcceptedConfiguration());
if (request.nodes()) {
builder.nodes(currentState.nodes());
}
Expand Down
179 changes: 173 additions & 6 deletions server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ public void testToXContent() throws IOException {
" \"acknowledged\" : true,\n" +
" \"state\" : {\n" +
" \"cluster_uuid\" : \"_na_\",\n" +
" \"term\" : 0,\n" +
" \"version\" : 0,\n" +
" \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" +
" \"last_committed_config\" : [ ],\n" +
" \"last_accepted_config\" : [ ],\n" +
" \"master_node\" : \"node0\",\n" +
" \"blocks\" : { },\n" +
" \"nodes\" : {\n" +
Expand Down Expand Up @@ -138,8 +141,11 @@ public void testToXContent() throws IOException {
" \"acknowledged\" : true,\n" +
" \"state\" : {\n" +
" \"cluster_uuid\" : \"_na_\",\n" +
" \"term\" : 0,\n" +
" \"version\" : 0,\n" +
" \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" +
" \"last_committed_config\" : [ ],\n" +
" \"last_accepted_config\" : [ ],\n" +
" \"master_node\" : \"node0\"\n" +
" },\n" +
" \"explanations\" : [\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testClusterStateDiffSerialization() throws Exception {
if (i > 0) {
clusterState = builder.build();
}
switch (randomInt(4)) {
switch (randomInt(5)) {
case 0:
builder = randomNodes(clusterState);
break;
Expand All @@ -113,11 +113,14 @@ public void testClusterStateDiffSerialization() throws Exception {
case 4:
builder = randomMetaDataChanges(clusterState);
break;
case 5:
builder = randomVotingConfiguration(clusterState);
break;
default:
throw new IllegalArgumentException("Shouldn't be here");
}
}
clusterState = builder.incrementVersion().build();
clusterState = builder.incrementVersion().term(randomLong()).build();

if (randomIntBetween(0, 10) < 1) {
// Update cluster state via full serialization from time to time
Expand All @@ -141,7 +144,10 @@ public void testClusterStateDiffSerialization() throws Exception {
try {
// Check non-diffable elements
assertThat(clusterStateFromDiffs.version(), equalTo(clusterState.version()));
assertThat(clusterStateFromDiffs.term(), equalTo(clusterState.term()));
assertThat(clusterStateFromDiffs.stateUUID(), equalTo(clusterState.stateUUID()));
assertThat(clusterStateFromDiffs.getLastAcceptedConfiguration(), equalTo(clusterState.getLastAcceptedConfiguration()));
assertThat(clusterStateFromDiffs.getLastCommittedConfiguration(), equalTo(clusterState.getLastCommittedConfiguration()));

// Check nodes
assertThat(clusterStateFromDiffs.nodes().getNodes(), equalTo(clusterState.nodes().getNodes()));
Expand Down Expand Up @@ -190,6 +196,20 @@ public void testClusterStateDiffSerialization() throws Exception {

}

private ClusterState.Builder randomVotingConfiguration(ClusterState clusterState) {
ClusterState.Builder builder = ClusterState.builder(clusterState);
if (randomBoolean()) {
builder.lastCommittedConfiguration(
new ClusterState.VotingConfiguration(Sets.newHashSet(generateRandomStringArray(10, 10, false))));
}
if (randomBoolean()) {
builder.lastAcceptedConfiguration(
new ClusterState.VotingConfiguration(Sets.newHashSet(generateRandomStringArray(10, 10, false))));
}

return builder;
}

/**
* Randomly updates nodes in the cluster state
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@
package org.elasticsearch.cluster;

import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterState.VotingConfiguration;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
Expand Down Expand Up @@ -55,6 +63,70 @@ public void testSupersedes() {

// state from the same master compare by version
assertThat(withMaster1a.supersedes(withMaster1b), equalTo(withMaster1a.version() > withMaster1b.version()));
}

public void testVotingConfiguration() {
VotingConfiguration config0 = new VotingConfiguration(Sets.newHashSet());
assertThat(config0, equalTo(VotingConfiguration.EMPTY_CONFIG));
assertThat(config0.getNodeIds(), equalTo(Sets.newHashSet()));
assertThat(config0.isEmpty(), equalTo(true));
assertThat(config0.hasQuorum(Sets.newHashSet()), equalTo(false));
assertThat(config0.hasQuorum(Sets.newHashSet("id1")), equalTo(false));

VotingConfiguration config1 = new VotingConfiguration(Sets.newHashSet("id1"));
assertThat(config1.getNodeIds(), equalTo(Sets.newHashSet("id1")));
assertThat(config1.isEmpty(), equalTo(false));
assertThat(config1.hasQuorum(Sets.newHashSet("id1")), equalTo(true));
assertThat(config1.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true));
assertThat(config1.hasQuorum(Sets.newHashSet("id2")), equalTo(false));
assertThat(config1.hasQuorum(Sets.newHashSet()), equalTo(false));

VotingConfiguration config2 = new VotingConfiguration(Sets.newHashSet("id1", "id2"));
assertThat(config2.getNodeIds(), equalTo(Sets.newHashSet("id1", "id2")));
assertThat(config2.isEmpty(), equalTo(false));
assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true));
assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id2", "id3")), equalTo(true));
assertThat(config2.hasQuorum(Sets.newHashSet("id1")), equalTo(false));
assertThat(config2.hasQuorum(Sets.newHashSet("id2")), equalTo(false));
assertThat(config2.hasQuorum(Sets.newHashSet("id3")), equalTo(false));
assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id3")), equalTo(false));
assertThat(config2.hasQuorum(Sets.newHashSet()), equalTo(false));

VotingConfiguration config3 = new VotingConfiguration(Sets.newHashSet("id1", "id2", "id3"));
assertThat(config3.getNodeIds(), equalTo(Sets.newHashSet("id1", "id2", "id3")));
assertThat(config3.isEmpty(), equalTo(false));
assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true));
assertThat(config3.hasQuorum(Sets.newHashSet("id2", "id3")), equalTo(true));
assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id3")), equalTo(true));
assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2", "id3")), equalTo(true));
assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2", "id4")), equalTo(true));
assertThat(config3.hasQuorum(Sets.newHashSet("id1")), equalTo(false));
assertThat(config3.hasQuorum(Sets.newHashSet("id2")), equalTo(false));
assertThat(config3.hasQuorum(Sets.newHashSet("id3")), equalTo(false));
assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id4")), equalTo(false));
assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id4", "id5")), equalTo(false));
assertThat(config3.hasQuorum(Sets.newHashSet()), equalTo(false));
}

public void testVotingConfigurationSerializationEqualsHashCode() {
VotingConfiguration initialConfig = new VotingConfiguration(
Sets.newHashSet(generateRandomStringArray(randomInt(10), 20, false)));
EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialConfig,
orig -> ESTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), VotingConfiguration::new),
cfg -> {
Set<String> newNodeIds = new HashSet<>(cfg.getNodeIds());
if (cfg.isEmpty() == false && randomBoolean()) {
// remove random element
newNodeIds.remove(randomFrom(cfg.getNodeIds()));
} else if (cfg.isEmpty() == false && randomBoolean()) {
// change random element
newNodeIds.remove(randomFrom(cfg.getNodeIds()));
newNodeIds.add(randomAlphaOfLength(20));
} else {
// add random element
newNodeIds.add(randomAlphaOfLength(20));
}
return new VotingConfiguration(newNodeIds);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ public void clusterChanged(ClusterChangedEvent event) {
clusterState = ClusterState.builder(clusterState).blocks(ClusterBlocks.builder()
.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK)).incrementVersion().build();

ClusterState unserializableClusterState = new ClusterState(clusterState.version(), clusterState.stateUUID(), clusterState) {
ClusterState unserializableClusterState = new ClusterState(clusterState.term(), clusterState.version(), clusterState.stateUUID(),
clusterState) {
@Override
public Diff<ClusterState> diff(ClusterState previousState) {
return new Diff<ClusterState>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,11 @@ public void testToXContent() throws IOException {
+ "\"nodes_hash\":1314980060,"
+ "\"status\":\"green\","
+ "\"cluster_uuid\":\"_cluster\","
+ "\"term\":0,"
+ "\"version\":12,"
+ "\"state_uuid\":\"_state_uuid\","
+ "\"last_committed_config\":[],"
+ "\"last_accepted_config\":[],"
+ "\"master_node\":\"_node\","
+ "\"nodes\":{"
+ "\"_node_id\":{"
Expand Down