-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Replace InMemoryPersistedState with GatewayMetaState in CoordinatorTests #36897
Merged
andrershov
merged 10 commits into
elastic:master
from
andrershov:zen2_replace_inmemorystate
Jan 15, 2019
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ca00f0f
Replace InMemoryPersistedState with GatewayMetaState in CoordinatorTests
7a3604a
Fix formatting
a63cc94
GatewayMetaStateUT -> MockGatewayMetaState
d5774de
Use GatewayMetaState rarely
6f75d6e
Merge branch 'master' into zen2_replace_inmemorystate
c96ec62
fail -> AssertionError
288314e
Fix format
d76029a
Merge branch 'master' into zen2_replace_inmemorystate
40b303c
Merge branch 'master' into zen2_replace_inmemorystate
937ded8
Merge branch 'master' into zen2_replace_inmemorystate
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -50,12 +50,15 @@ | |||||
import org.elasticsearch.common.unit.TimeValue; | ||||||
import org.elasticsearch.discovery.zen.PublishClusterStateStats; | ||||||
import org.elasticsearch.discovery.zen.UnicastHostsProvider.HostsResolver; | ||||||
import org.elasticsearch.env.NodeEnvironment; | ||||||
import org.elasticsearch.gateway.MockGatewayMetaState; | ||||||
import org.elasticsearch.indices.cluster.FakeThreadPoolMasterService; | ||||||
import org.elasticsearch.test.ESTestCase; | ||||||
import org.elasticsearch.test.disruption.DisruptableMockTransport; | ||||||
import org.elasticsearch.test.disruption.DisruptableMockTransport.ConnectionStatus; | ||||||
import org.elasticsearch.transport.TransportService; | ||||||
import org.hamcrest.Matcher; | ||||||
import org.junit.After; | ||||||
import org.junit.Before; | ||||||
|
||||||
import java.io.IOException; | ||||||
|
@@ -123,6 +126,16 @@ | |||||
|
||||||
public class CoordinatorTests extends ESTestCase { | ||||||
|
||||||
private final List<NodeEnvironment> nodeEnvironments = new ArrayList<>(); | ||||||
|
||||||
@After | ||||||
public void closeNodeEnvironmentsAfterEachTest() { | ||||||
for (NodeEnvironment nodeEnvironment : nodeEnvironments) { | ||||||
nodeEnvironment.close(); | ||||||
} | ||||||
nodeEnvironments.clear(); | ||||||
} | ||||||
|
||||||
@Before | ||||||
public void resetPortCounterBeforeEachTest() { | ||||||
resetPortCounter(); | ||||||
|
@@ -1358,35 +1371,50 @@ ClusterNode getAnyNodePreferringLeaders() { | |||||
return getAnyNode(); | ||||||
} | ||||||
|
||||||
class MockPersistedState extends InMemoryPersistedState { | ||||||
MockPersistedState(long term, ClusterState acceptedState) { | ||||||
super(term, acceptedState); | ||||||
class MockPersistedState implements PersistedState { | ||||||
private final PersistedState delegate; | ||||||
|
||||||
MockPersistedState(Settings settings, DiscoveryNode localNode) throws IOException { | ||||||
if (rarely()) { | ||||||
NodeEnvironment nodeEnvironment = newNodeEnvironment(); | ||||||
nodeEnvironments.add(nodeEnvironment); | ||||||
delegate = new MockGatewayMetaState(settings, nodeEnvironment, xContentRegistry(), localNode) | ||||||
.getPersistedState(settings, null); | ||||||
} else { | ||||||
delegate = new InMemoryPersistedState(0L, | ||||||
clusterState(0L, 0L, localNode, VotingConfiguration.EMPTY_CONFIG, VotingConfiguration.EMPTY_CONFIG, 0L)); | ||||||
} | ||||||
} | ||||||
|
||||||
private void possiblyFail(String description) { | ||||||
if (disruptStorage && rarely()) { | ||||||
// TODO revisit this when we've decided how PersistedState should throw exceptions | ||||||
logger.trace("simulating IO exception [{}]", description); | ||||||
if (randomBoolean()) { | ||||||
throw new UncheckedIOException(new IOException("simulated IO exception [" + description + ']')); | ||||||
} else { | ||||||
throw new CoordinationStateRejectedException("simulated IO exception [" + description + ']'); | ||||||
} | ||||||
// In the real-life IOError might be thrown, for example if state fsync fails. | ||||||
// This will require node restart and we're not emulating it here. | ||||||
throw new UncheckedIOException(new IOException("simulated IO exception [" + description + ']')); | ||||||
} | ||||||
} | ||||||
|
||||||
@Override | ||||||
public long getCurrentTerm() { | ||||||
return delegate.getCurrentTerm(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public ClusterState getLastAcceptedState() { | ||||||
return delegate.getLastAcceptedState(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public void setCurrentTerm(long currentTerm) { | ||||||
possiblyFail("before writing term of " + currentTerm); | ||||||
super.setCurrentTerm(currentTerm); | ||||||
// TODO possiblyFail() here if that's a failure mode of the storage layer | ||||||
delegate.setCurrentTerm(currentTerm); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public void setLastAcceptedState(ClusterState clusterState) { | ||||||
possiblyFail("before writing last-accepted state of term=" + clusterState.term() + ", version=" + clusterState.version()); | ||||||
super.setLastAcceptedState(clusterState); | ||||||
// TODO possiblyFail() here if that's a failure mode of the storage layer | ||||||
delegate.setLastAcceptedState(clusterState); | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -1396,7 +1424,7 @@ class ClusterNode { | |||||
private final int nodeIndex; | ||||||
private Coordinator coordinator; | ||||||
private DiscoveryNode localNode; | ||||||
private final PersistedState persistedState; | ||||||
private PersistedState persistedState; | ||||||
private FakeClusterApplier clusterApplier; | ||||||
private AckedFakeThreadPoolMasterService masterService; | ||||||
private TransportService transportService; | ||||||
|
@@ -1406,8 +1434,6 @@ class ClusterNode { | |||||
ClusterNode(int nodeIndex, boolean masterEligible) { | ||||||
this.nodeIndex = nodeIndex; | ||||||
localNode = createDiscoveryNode(masterEligible); | ||||||
persistedState = new MockPersistedState(0L, | ||||||
clusterState(0L, 0L, localNode, VotingConfiguration.EMPTY_CONFIG, VotingConfiguration.EMPTY_CONFIG, 0L)); | ||||||
onNode(localNode, this::setUp).run(); | ||||||
} | ||||||
|
||||||
|
@@ -1480,6 +1506,12 @@ protected void onBlackholedDuringSend(long requestId, String action, DiscoveryNo | |||||
Cluster.this::provideUnicastHosts, clusterApplier, Randomness.get()); | ||||||
masterService.setClusterStatePublisher(coordinator); | ||||||
|
||||||
try { | ||||||
persistedState = new MockPersistedState(settings, localNode); | ||||||
} catch (IOException e) { | ||||||
fail("Unable to create MockPersistedState"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we would like to see the caught exception here:
Suggested change
|
||||||
} | ||||||
|
||||||
transportService.start(); | ||||||
transportService.acceptIncomingRequests(); | ||||||
masterService.start(); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
server/src/test/java/org/elasticsearch/gateway/MockGatewayMetaState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.gateway; | ||
|
||
import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.env.NodeEnvironment; | ||
import org.elasticsearch.indices.IndicesService; | ||
import org.elasticsearch.plugins.MetaDataUpgrader; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* {@link GatewayMetaState} constructor accepts a lot of arguments. | ||
* It's not always easy / convenient to construct these dependencies. | ||
* This class constructor takes far fewer dependencies and constructs usable {@link GatewayMetaState} with 2 restrictions: | ||
* no metadata upgrade will be performed and no cluster state updaters will be run. This is sufficient for most of the tests. | ||
* Metadata upgrade is tested in {@link GatewayMetaStateTests} and different {@link ClusterStateUpdaters} in | ||
* {@link ClusterStateUpdatersTests}. | ||
*/ | ||
public class MockGatewayMetaState extends GatewayMetaState { | ||
private final DiscoveryNode localNode; | ||
|
||
public MockGatewayMetaState(Settings settings, NodeEnvironment nodeEnvironment, | ||
NamedXContentRegistry xContentRegistry, DiscoveryNode localNode) throws IOException { | ||
super(settings, nodeEnvironment, new MetaStateService(nodeEnvironment, xContentRegistry), | ||
mock(MetaDataIndexUpgradeService.class), mock(MetaDataUpgrader.class), | ||
mock(TransportService.class), mock(ClusterService.class), | ||
mock(IndicesService.class)); | ||
this.localNode = localNode; | ||
} | ||
|
||
@Override | ||
protected void upgradeMetaData(MetaDataIndexUpgradeService metaDataIndexUpgradeService, MetaDataUpgrader metaDataUpgrader) { | ||
// MetaData upgrade is tested in GatewayMetaStateTests, we override this method to NOP to make mocking easier | ||
} | ||
|
||
@Override | ||
public void applyClusterStateUpdaters() { | ||
// Just set localNode here, not to mess with ClusterService and IndicesService mocking | ||
previousClusterState = ClusterStateUpdaters.setLocalNode(previousClusterState, localNode); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: too much indent here?