Permalink
Please sign in to comment.
Browse files
Added ServiceDisruptionScheme(s) and testAckedIndexing
This commit adds the notion of ServiceDisruptionScheme allowing for introducing disruptions in our test cluster. This abstraction as used in a couple of wrappers around the functionality offered by MockTransportService to simulate various network partions. There is also one implementation for causing a node to be slow in processing cluster state updates. This new mechnaism is integrated into existing tests DiscoveryWithNetworkFailuresTests. A new test called testAckedIndexing is added to verify retrieval of documents whose indexing was acked during various disruptions. Closes #6505
- Loading branch information...
Showing
with
1,156 additions
and 167 deletions.
- +6 −11 src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java
- +4 −0 src/main/java/org/elasticsearch/transport/TransportService.java
- +257 −140 src/test/java/org/elasticsearch/discovery/DiscoveryWithNetworkFailuresTests.java
- +0 −1 src/test/java/org/elasticsearch/recovery/RecoveryWhileUnderLoadTests.java
- +2 −3 src/test/java/org/elasticsearch/test/BackgroundIndexer.java
- +21 −11 src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java
- +64 −0 src/test/java/org/elasticsearch/test/InternalTestCluster.java
- +1 −0 src/test/java/org/elasticsearch/test/TestCluster.java
- +88 −0 src/test/java/org/elasticsearch/test/disruption/NetworkDelaysPartition.java
- +53 −0 src/test/java/org/elasticsearch/test/disruption/NetworkDisconnectPartition.java
- +199 −0 src/test/java/org/elasticsearch/test/disruption/NetworkPartition.java
- +52 −0 src/test/java/org/elasticsearch/test/disruption/NetworkUnresponsivePartition.java
- +60 −0 src/test/java/org/elasticsearch/test/disruption/NoOpDisruptionScheme.java
- +40 −0 src/test/java/org/elasticsearch/test/disruption/ServiceDisruptionScheme.java
- +84 −0 src/test/java/org/elasticsearch/test/disruption/SingleNodeDisruption.java
- +130 −0 src/test/java/org/elasticsearch/test/disruption/SlowClusterStateProcessing.java
- +95 −1 src/test/java/org/elasticsearch/test/transport/MockTransportService.java
17
src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java
4
src/main/java/org/elasticsearch/transport/TransportService.java
397
src/test/java/org/elasticsearch/discovery/DiscoveryWithNetworkFailuresTests.java
1
src/test/java/org/elasticsearch/recovery/RecoveryWhileUnderLoadTests.java
5
src/test/java/org/elasticsearch/test/BackgroundIndexer.java
32
src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java
64
src/test/java/org/elasticsearch/test/InternalTestCluster.java
1
src/test/java/org/elasticsearch/test/TestCluster.java
88
src/test/java/org/elasticsearch/test/disruption/NetworkDelaysPartition.java
| @@ -0,0 +1,88 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| +import org.elasticsearch.common.unit.TimeValue; | ||
| +import org.elasticsearch.test.transport.MockTransportService; | ||
| + | ||
| +import java.util.Random; | ||
| +import java.util.Set; | ||
| + | ||
| +public class NetworkDelaysPartition extends NetworkPartition { | ||
| + | ||
| + static long DEFAULT_DELAY_MIN = 10000; | ||
| + static long DEFAULT_DELAY_MAX = 90000; | ||
| + | ||
| + | ||
| + final long delayMin; | ||
| + final long delayMax; | ||
| + | ||
| + TimeValue duration; | ||
| + | ||
| + public NetworkDelaysPartition(Random random) { | ||
| + this(random, DEFAULT_DELAY_MIN, DEFAULT_DELAY_MAX); | ||
| + } | ||
| + | ||
| + public NetworkDelaysPartition(Random random, long delayMin, long delayMax) { | ||
| + super(random); | ||
| + this.delayMin = delayMin; | ||
| + this.delayMax = delayMax; | ||
| + } | ||
| + | ||
| + public NetworkDelaysPartition(String node1, String node2, Random random) { | ||
| + this(node1, node2, DEFAULT_DELAY_MIN, DEFAULT_DELAY_MAX, random); | ||
| + } | ||
| + | ||
| + public NetworkDelaysPartition(String node1, String node2, long delayMin, long delayMax, Random random) { | ||
| + super(node1, node2, random); | ||
| + this.delayMin = delayMin; | ||
| + this.delayMax = delayMax; | ||
| + } | ||
| + | ||
| + public NetworkDelaysPartition(Set<String> nodesSideOne, Set<String> nodesSideTwo, Random random) { | ||
| + this(nodesSideOne, nodesSideTwo, DEFAULT_DELAY_MIN, DEFAULT_DELAY_MAX, random); | ||
| + } | ||
| + | ||
| + public NetworkDelaysPartition(Set<String> nodesSideOne, Set<String> nodesSideTwo, long delayMin, long delayMax, Random random) { | ||
| + super(nodesSideOne, nodesSideTwo, random); | ||
| + this.delayMin = delayMin; | ||
| + this.delayMax = delayMax; | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void startDisrupting() { | ||
| + duration = new TimeValue(delayMin + random.nextInt((int) (delayMax - delayMin))); | ||
| + super.startDisrupting(); | ||
| + } | ||
| + | ||
| + @Override | ||
| + void applyDisruption(DiscoveryNode node1, MockTransportService transportService1, | ||
| + DiscoveryNode node2, MockTransportService transportService2) { | ||
| + transportService1.addUnresponsiveRule(node1, duration); | ||
| + transportService1.addUnresponsiveRule(node2, duration); | ||
| + } | ||
| + | ||
| + @Override | ||
| + protected String getPartitionDescription() { | ||
| + return "network delays for [" + duration + "]"; | ||
| + } | ||
| + | ||
| +} |
53
src/test/java/org/elasticsearch/test/disruption/NetworkDisconnectPartition.java
| @@ -0,0 +1,53 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| +import org.elasticsearch.test.transport.MockTransportService; | ||
| + | ||
| +import java.util.Random; | ||
| +import java.util.Set; | ||
| + | ||
| +public class NetworkDisconnectPartition extends NetworkPartition { | ||
| + | ||
| + | ||
| + public NetworkDisconnectPartition(Random random) { | ||
| + super(random); | ||
| + } | ||
| + | ||
| + public NetworkDisconnectPartition(String node1, String node2, Random random) { | ||
| + super(node1, node2, random); | ||
| + } | ||
| + | ||
| + public NetworkDisconnectPartition(Set<String> nodesSideOne, Set<String> nodesSideTwo, Random random) { | ||
| + super(nodesSideOne, nodesSideTwo, random); | ||
| + } | ||
| + | ||
| + @Override | ||
| + protected String getPartitionDescription() { | ||
| + return "disconnected"; | ||
| + } | ||
| + | ||
| + @Override | ||
| + void applyDisruption(DiscoveryNode node1, MockTransportService transportService1, | ||
| + DiscoveryNode node2, MockTransportService transportService2) { | ||
| + transportService1.addFailToSendNoConnectRule(node2); | ||
| + transportService2.addFailToSendNoConnectRule(node1); | ||
| + } | ||
| +} |
199
src/test/java/org/elasticsearch/test/disruption/NetworkPartition.java
| @@ -0,0 +1,199 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import com.google.common.collect.ImmutableList; | ||
| +import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| +import org.elasticsearch.common.logging.ESLogger; | ||
| +import org.elasticsearch.common.logging.Loggers; | ||
| +import org.elasticsearch.discovery.Discovery; | ||
| +import org.elasticsearch.test.InternalTestCluster; | ||
| +import org.elasticsearch.test.TestCluster; | ||
| +import org.elasticsearch.test.transport.MockTransportService; | ||
| +import org.elasticsearch.transport.TransportService; | ||
| + | ||
| +import java.util.HashSet; | ||
| +import java.util.List; | ||
| +import java.util.Random; | ||
| +import java.util.Set; | ||
| + | ||
| +public abstract class NetworkPartition implements ServiceDisruptionScheme { | ||
| + | ||
| + protected final ESLogger logger = Loggers.getLogger(getClass()); | ||
| + | ||
| + final Set<String> nodesSideOne; | ||
| + final Set<String> nodesSideTwo; | ||
| + volatile boolean autoExpand; | ||
| + protected final Random random; | ||
| + protected volatile InternalTestCluster cluster; | ||
| + | ||
| + | ||
| + public NetworkPartition(Random random) { | ||
| + this.random = new Random(random.nextLong()); | ||
| + nodesSideOne = new HashSet<>(); | ||
| + nodesSideTwo = new HashSet<>(); | ||
| + autoExpand = true; | ||
| + } | ||
| + | ||
| + public NetworkPartition(String node1, String node2, Random random) { | ||
| + this(random); | ||
| + nodesSideOne.add(node1); | ||
| + nodesSideTwo.add(node2); | ||
| + autoExpand = false; | ||
| + } | ||
| + | ||
| + public NetworkPartition(Set<String> nodesSideOne, Set<String> nodesSideTwo, Random random) { | ||
| + this(random); | ||
| + this.nodesSideOne.addAll(nodesSideOne); | ||
| + this.nodesSideTwo.addAll(nodesSideTwo); | ||
| + autoExpand = false; | ||
| + } | ||
| + | ||
| + | ||
| + public List<String> getNodesSideOne() { | ||
| + return ImmutableList.copyOf(nodesSideOne); | ||
| + } | ||
| + | ||
| + public List<String> getNodesSideTwo() { | ||
| + return ImmutableList.copyOf(nodesSideTwo); | ||
| + } | ||
| + | ||
| + public List<String> getMjaoritySide() { | ||
| + if (nodesSideOne.size() >= nodesSideTwo.size()) { | ||
| + return getNodesSideOne(); | ||
| + } else { | ||
| + return getNodesSideTwo(); | ||
| + } | ||
| + } | ||
| + | ||
| + public List<String> getMinoritySide() { | ||
| + if (nodesSideOne.size() >= nodesSideTwo.size()) { | ||
| + return getNodesSideTwo(); | ||
| + } else { | ||
| + return getNodesSideOne(); | ||
| + } | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void applyToCluster(InternalTestCluster cluster) { | ||
| + this.cluster = cluster; | ||
| + if (autoExpand) { | ||
| + for (String node : cluster.getNodeNames()) { | ||
| + applyToNode(node, cluster); | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void removeFromCluster(InternalTestCluster cluster) { | ||
| + stopDisrupting(); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void applyToNode(String node, InternalTestCluster cluster) { | ||
| + if (!autoExpand || nodesSideOne.contains(node) || nodesSideTwo.contains(node)) { | ||
| + return; | ||
| + } | ||
| + if (nodesSideOne.isEmpty()) { | ||
| + nodesSideOne.add(node); | ||
| + } else if (nodesSideTwo.isEmpty()) { | ||
| + nodesSideTwo.add(node); | ||
| + } else if (random.nextBoolean()) { | ||
| + nodesSideOne.add(node); | ||
| + } else { | ||
| + nodesSideTwo.add(node); | ||
| + } | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void removeFromNode(String node, InternalTestCluster cluster) { | ||
| + MockTransportService transportService = (MockTransportService) cluster.getInstance(TransportService.class, node); | ||
| + DiscoveryNode discoveryNode = discoveryNode(node); | ||
| + Set<String> otherSideNodes; | ||
| + if (nodesSideOne.contains(node)) { | ||
| + otherSideNodes = nodesSideTwo; | ||
| + } else if (nodesSideTwo.contains(node)) { | ||
| + otherSideNodes = nodesSideOne; | ||
| + } else { | ||
| + return; | ||
| + } | ||
| + for (String node2 : otherSideNodes) { | ||
| + MockTransportService transportService2 = (MockTransportService) cluster.getInstance(TransportService.class, node2); | ||
| + DiscoveryNode discoveryNode2 = discoveryNode(node2); | ||
| + removeDisruption(discoveryNode, transportService, discoveryNode2, transportService2); | ||
| + } | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void testClusterClosed() { | ||
| + | ||
| + } | ||
| + | ||
| + protected abstract String getPartitionDescription(); | ||
| + | ||
| + | ||
| + protected DiscoveryNode discoveryNode(String node) { | ||
| + return cluster.getInstance(Discovery.class, node).localNode(); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void startDisrupting() { | ||
| + if (nodesSideOne.size() == 0 || nodesSideTwo.size() == 0) { | ||
| + return; | ||
| + } | ||
| + logger.info("nodes {} will be partitioned from {}. partition type [{}]", nodesSideOne, nodesSideTwo, getPartitionDescription()); | ||
| + for (String node1 : nodesSideOne) { | ||
| + MockTransportService transportService1 = (MockTransportService) cluster.getInstance(TransportService.class, node1); | ||
| + DiscoveryNode discoveryNode1 = discoveryNode(node1); | ||
| + for (String node2 : nodesSideTwo) { | ||
| + DiscoveryNode discoveryNode2 = discoveryNode(node2); | ||
| + MockTransportService transportService2 = (MockTransportService) cluster.getInstance(TransportService.class, node2); | ||
| + applyDisruption(discoveryNode1, transportService1, discoveryNode2, transportService2); | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + | ||
| + @Override | ||
| + public void stopDisrupting() { | ||
| + if (nodesSideOne.size() == 0 || nodesSideTwo.size() == 0) { | ||
| + return; | ||
| + } | ||
| + logger.info("restoring partition between nodes {} & nodes {}", nodesSideOne, nodesSideTwo); | ||
| + for (String node1 : nodesSideOne) { | ||
| + MockTransportService transportService1 = (MockTransportService) cluster.getInstance(TransportService.class, node1); | ||
| + DiscoveryNode discoveryNode1 = discoveryNode(node1); | ||
| + for (String node2 : nodesSideTwo) { | ||
| + DiscoveryNode discoveryNode2 = discoveryNode(node2); | ||
| + MockTransportService transportService2 = (MockTransportService) cluster.getInstance(TransportService.class, node2); | ||
| + removeDisruption(discoveryNode1, transportService1, discoveryNode2, transportService2); | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + abstract void applyDisruption(DiscoveryNode node1, MockTransportService transportService1, | ||
| + DiscoveryNode node2, MockTransportService transportService2); | ||
| + | ||
| + | ||
| + protected void removeDisruption(DiscoveryNode node1, MockTransportService transportService1, | ||
| + DiscoveryNode node2, MockTransportService transportService2) { | ||
| + transportService1.clearRule(node2); | ||
| + transportService2.clearRule(node1); | ||
| + } | ||
| +} |
52
src/test/java/org/elasticsearch/test/disruption/NetworkUnresponsivePartition.java
| @@ -0,0 +1,52 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| +import org.elasticsearch.test.transport.MockTransportService; | ||
| + | ||
| +import java.util.Random; | ||
| +import java.util.Set; | ||
| + | ||
| +public class NetworkUnresponsivePartition extends NetworkPartition { | ||
| + | ||
| + public NetworkUnresponsivePartition(Random random) { | ||
| + super(random); | ||
| + } | ||
| + | ||
| + public NetworkUnresponsivePartition(String node1, String node2, Random random) { | ||
| + super(node1, node2, random); | ||
| + } | ||
| + | ||
| + public NetworkUnresponsivePartition(Set<String> nodesSideOne, Set<String> nodesSideTwo, Random random) { | ||
| + super(nodesSideOne, nodesSideTwo, random); | ||
| + } | ||
| + | ||
| + @Override | ||
| + protected String getPartitionDescription() { | ||
| + return "unresponsive"; | ||
| + } | ||
| + | ||
| + @Override | ||
| + void applyDisruption(DiscoveryNode node1, MockTransportService transportService1, | ||
| + DiscoveryNode node2, MockTransportService transportService2) { | ||
| + transportService1.addUnresponsiveRule(node2); | ||
| + transportService2.addUnresponsiveRule(node1); | ||
| + } | ||
| +} |
60
src/test/java/org/elasticsearch/test/disruption/NoOpDisruptionScheme.java
| @@ -0,0 +1,60 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.test.InternalTestCluster; | ||
| + | ||
| +public class NoOpDisruptionScheme implements ServiceDisruptionScheme { | ||
| + | ||
| + @Override | ||
| + public void applyToCluster(InternalTestCluster cluster) { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void removeFromCluster(InternalTestCluster cluster) { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void applyToNode(String node, InternalTestCluster cluster) { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void removeFromNode(String node, InternalTestCluster cluster) { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void startDisrupting() { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void stopDisrupting() { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void testClusterClosed() { | ||
| + | ||
| + } | ||
| +} |
40
src/test/java/org/elasticsearch/test/disruption/ServiceDisruptionScheme.java
| @@ -0,0 +1,40 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.test.InternalTestCluster; | ||
| +import org.elasticsearch.test.TestCluster; | ||
| + | ||
| +public interface ServiceDisruptionScheme { | ||
| + | ||
| + public void applyToCluster(InternalTestCluster cluster); | ||
| + | ||
| + public void removeFromCluster(InternalTestCluster cluster); | ||
| + | ||
| + public void applyToNode(String node, InternalTestCluster cluster); | ||
| + | ||
| + public void removeFromNode(String node, InternalTestCluster cluster); | ||
| + | ||
| + public void startDisrupting(); | ||
| + | ||
| + public void stopDisrupting(); | ||
| + | ||
| + public void testClusterClosed(); | ||
| + | ||
| +} |
84
src/test/java/org/elasticsearch/test/disruption/SingleNodeDisruption.java
| @@ -0,0 +1,84 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.common.logging.ESLogger; | ||
| +import org.elasticsearch.common.logging.Loggers; | ||
| +import org.elasticsearch.test.InternalTestCluster; | ||
| +import org.elasticsearch.test.TestCluster; | ||
| + | ||
| +import java.util.Random; | ||
| + | ||
| +public abstract class SingleNodeDisruption implements ServiceDisruptionScheme { | ||
| + | ||
| + protected final ESLogger logger = Loggers.getLogger(getClass()); | ||
| + | ||
| + protected volatile String disruptedNode; | ||
| + protected volatile TestCluster cluster; | ||
| + protected final Random random; | ||
| + | ||
| + | ||
| + public SingleNodeDisruption(String disruptedNode, Random random) { | ||
| + this(random); | ||
| + this.disruptedNode = disruptedNode; | ||
| + } | ||
| + | ||
| + public SingleNodeDisruption(Random random) { | ||
| + this.random = new Random(random.nextLong()); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void applyToCluster(InternalTestCluster cluster) { | ||
| + this.cluster = cluster; | ||
| + if (disruptedNode == null) { | ||
| + String[] nodes = cluster.getNodeNames(); | ||
| + disruptedNode = nodes[random.nextInt(nodes.length)]; | ||
| + } | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void removeFromCluster(InternalTestCluster cluster) { | ||
| + if (disruptedNode != null) { | ||
| + removeFromNode(disruptedNode, cluster); | ||
| + } | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void applyToNode(String node, InternalTestCluster cluster) { | ||
| + | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void removeFromNode(String node, InternalTestCluster cluster) { | ||
| + if (disruptedNode == null) { | ||
| + return; | ||
| + } | ||
| + if (!node.equals(disruptedNode)) { | ||
| + return; | ||
| + } | ||
| + stopDisrupting(); | ||
| + disruptedNode = null; | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void testClusterClosed() { | ||
| + disruptedNode = null; | ||
| + } | ||
| + | ||
| +} |
130
src/test/java/org/elasticsearch/test/disruption/SlowClusterStateProcessing.java
| @@ -0,0 +1,130 @@ | ||
| +/* | ||
| + * 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.test.disruption; | ||
| + | ||
| +import org.elasticsearch.cluster.ClusterService; | ||
| +import org.elasticsearch.cluster.ClusterState; | ||
| +import org.elasticsearch.cluster.ClusterStateNonMasterUpdateTask; | ||
| +import org.elasticsearch.common.Priority; | ||
| +import org.elasticsearch.common.unit.TimeValue; | ||
| + | ||
| +import java.util.Random; | ||
| + | ||
| +public class SlowClusterStateProcessing extends SingleNodeDisruption { | ||
| + | ||
| + volatile boolean disrupting; | ||
| + volatile Thread worker; | ||
| + | ||
| + final long intervalBetweenDelaysMin; | ||
| + final long intervalBetweenDelaysMax; | ||
| + final long delayDurationMin; | ||
| + final long delayDurationMax; | ||
| + | ||
| + | ||
| + public SlowClusterStateProcessing(Random random) { | ||
| + this(null, random); | ||
| + } | ||
| + | ||
| + public SlowClusterStateProcessing(String disruptedNode, Random random) { | ||
| + this(disruptedNode, random, 100, 200, 300, 20000); | ||
| + } | ||
| + | ||
| + public SlowClusterStateProcessing(String disruptedNode, Random random, long intervalBetweenDelaysMin, | ||
| + long intervalBetweenDelaysMax, long delayDurationMin, long delayDurationMax) { | ||
| + this(random, intervalBetweenDelaysMin, intervalBetweenDelaysMax, delayDurationMin, delayDurationMax); | ||
| + this.disruptedNode = disruptedNode; | ||
| + } | ||
| + | ||
| + public SlowClusterStateProcessing(Random random, | ||
| + long intervalBetweenDelaysMin, long intervalBetweenDelaysMax, long delayDurationMin, | ||
| + long delayDurationMax) { | ||
| + super(random); | ||
| + this.intervalBetweenDelaysMin = intervalBetweenDelaysMin; | ||
| + this.intervalBetweenDelaysMax = intervalBetweenDelaysMax; | ||
| + this.delayDurationMin = delayDurationMin; | ||
| + this.delayDurationMax = delayDurationMax; | ||
| + } | ||
| + | ||
| + | ||
| + @Override | ||
| + public void startDisrupting() { | ||
| + disrupting = true; | ||
| + worker = new Thread(new BackgroundWorker()); | ||
| + worker.setDaemon(true); | ||
| + worker.start(); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void stopDisrupting() { | ||
| + disrupting = false; | ||
| + try { | ||
| + worker.join(2 * (intervalBetweenDelaysMax + delayDurationMax)); | ||
| + } catch (InterruptedException e) { | ||
| + logger.info("background thread failed to stop"); | ||
| + } | ||
| + worker = null; | ||
| + } | ||
| + | ||
| + | ||
| + private synchronized boolean interruptClusterStateProcessing(final TimeValue duration) { | ||
| + if (disruptedNode == null) { | ||
| + return false; | ||
| + } | ||
| + logger.info("delaying cluster state updates on node [{}] for [{}]", disruptedNode, duration); | ||
| + ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptedNode); | ||
| + clusterService.submitStateUpdateTask("service_disruption_delay", Priority.IMMEDIATE, new ClusterStateNonMasterUpdateTask() { | ||
| + | ||
| + @Override | ||
| + public ClusterState execute(ClusterState currentState) throws Exception { | ||
| + Thread.sleep(duration.millis()); | ||
| + return currentState; | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void onFailure(String source, Throwable t) { | ||
| + | ||
| + } | ||
| + }); | ||
| + return true; | ||
| + } | ||
| + | ||
| + class BackgroundWorker implements Runnable { | ||
| + | ||
| + @Override | ||
| + public void run() { | ||
| + while (disrupting) { | ||
| + try { | ||
| + TimeValue duration = new TimeValue(delayDurationMin + random.nextInt((int) (delayDurationMax - delayDurationMin))); | ||
| + if (!interruptClusterStateProcessing(duration)) { | ||
| + continue; | ||
| + } | ||
| + Thread.sleep(duration.millis()); | ||
| + | ||
| + if (disruptedNode == null) { | ||
| + return; | ||
| + } | ||
| + | ||
| + } catch (Exception e) { | ||
| + logger.error("error in background worker", e); | ||
| + } | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| +} |
96
src/test/java/org/elasticsearch/test/transport/MockTransportService.java
0 comments on commit
ef75932