Test framework fall cleaning (#33423)

Wraps all lines in our test framework at 140 characters because that is
our standard line length and removes all of the checkstyle suppressions
for the test framework.

Drops most of `ModuleTestCase` because it isn't used and we're moving
away from using guice in the way that it wants to test anyway. Also
switches a few classes that extend it but don't use it to extend
`ESTestCase` instead.
This commit is contained in:
Nik Everett 2018-09-19 14:34:02 -04:00 committed by GitHub
parent 6ec12bef0d
commit 3ede13a454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 254 additions and 331 deletions

View File

@ -701,22 +701,4 @@
<suppress files="plugins[/\\]repository-s3[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]s3[/\\]MockDefaultS3OutputStream.java" checks="LineLength" /> <suppress files="plugins[/\\]repository-s3[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]s3[/\\]MockDefaultS3OutputStream.java" checks="LineLength" />
<suppress files="plugins[/\\]repository-s3[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]s3[/\\]TestAmazonS3.java" checks="LineLength" /> <suppress files="plugins[/\\]repository-s3[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]s3[/\\]TestAmazonS3.java" checks="LineLength" />
<suppress files="plugins[/\\]store-smb[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]index[/\\]store[/\\]SmbDirectoryWrapper.java" checks="LineLength" /> <suppress files="plugins[/\\]store-smb[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]index[/\\]store[/\\]SmbDirectoryWrapper.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]MockInternalClusterInfoService.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]routing[/\\]TestShardRouting.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]inject[/\\]ModuleTestCase.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]script[/\\]NativeSignificanceScoreScriptWithParams.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]BackgroundIndexer.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]CorruptionUtils.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]ESIntegTestCase.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]IndexSettingsModule.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]InternalTestCluster.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]MockIndexEventListener.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]discovery[/\\]ClusterDiscoveryConfiguration.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]disruption[/\\]IntermittentLongGCDisruption.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]disruption[/\\]SlowClusterStateProcessing.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]engine[/\\]AssertingSearcher.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]engine[/\\]MockEngineSupport.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]hamcrest[/\\]ElasticsearchAssertions.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]store[/\\]MockFSDirectoryService.java" checks="LineLength" />
<suppress files="test[/\\]framework[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]test[/\\]store[/\\]MockFSIndexStore.java" checks="LineLength" />
</suppressions> </suppressions>

View File

@ -22,7 +22,6 @@ package org.elasticsearch.common.network;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Table; import org.elasticsearch.common.Table;
import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.ModuleTestCase;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress; import org.elasticsearch.common.transport.BoundTransportAddress;
@ -39,6 +38,7 @@ import org.elasticsearch.plugins.NetworkPlugin;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.cat.AbstractCatAction; import org.elasticsearch.rest.action.cat.AbstractCatAction;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.Transport; import org.elasticsearch.transport.Transport;
@ -56,7 +56,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier; import java.util.function.Supplier;
public class NetworkModuleTests extends ModuleTestCase { public class NetworkModuleTests extends ESTestCase {
private ThreadPool threadPool; private ThreadPool threadPool;
@Override @Override

View File

@ -20,7 +20,6 @@ package org.elasticsearch.search;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.util.CharsRefBuilder; import org.apache.lucene.util.CharsRefBuilder;
import org.elasticsearch.common.inject.ModuleTestCase;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -72,6 +71,7 @@ import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.SuggestionSearchContext; import org.elasticsearch.search.suggest.SuggestionSearchContext;
import org.elasticsearch.search.suggest.term.TermSuggestion; import org.elasticsearch.search.suggest.term.TermSuggestion;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -89,7 +89,7 @@ import static java.util.stream.Collectors.toSet;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
public class SearchModuleTests extends ModuleTestCase { public class SearchModuleTests extends ESTestCase {
public void testDoubleRegister() { public void testDoubleRegister() {
SearchPlugin registersDupeHighlighter = new SearchPlugin() { SearchPlugin registersDupeHighlighter = new SearchPlugin() {

View File

@ -62,7 +62,8 @@ public class MockInternalClusterInfoService extends InternalClusterInfoService {
usage.getTotalBytes(), usage.getFreeBytes(), usage.getFreeBytes()); usage.getTotalBytes(), usage.getFreeBytes(), usage.getFreeBytes());
paths[0] = path; paths[0] = path;
FsInfo fsInfo = new FsInfo(System.currentTimeMillis(), null, paths); FsInfo fsInfo = new FsInfo(System.currentTimeMillis(), null, paths);
return new NodeStats(new DiscoveryNode(nodeName, ESTestCase.buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT), return new NodeStats(
new DiscoveryNode(nodeName, ESTestCase.buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT),
System.currentTimeMillis(), System.currentTimeMillis(),
null, null, null, null, null, null, null, null, null, null,
fsInfo, fsInfo,
@ -107,7 +108,8 @@ public class MockInternalClusterInfoService extends InternalClusterInfoService {
@Override @Override
public ClusterInfo getClusterInfo() { public ClusterInfo getClusterInfo() {
ClusterInfo clusterInfo = super.getClusterInfo(); ClusterInfo clusterInfo = super.getClusterInfo();
return new DevNullClusterInfo(clusterInfo.getNodeLeastAvailableDiskUsages(), clusterInfo.getNodeMostAvailableDiskUsages(), clusterInfo.shardSizes); return new DevNullClusterInfo(clusterInfo.getNodeLeastAvailableDiskUsages(),
clusterInfo.getNodeMostAvailableDiskUsages(), clusterInfo.shardSizes);
} }
/** /**

View File

@ -40,39 +40,52 @@ public class TestShardRouting {
} }
public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, boolean primary, ShardRoutingState state) { public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, boolean primary, ShardRoutingState state) {
return new ShardRouting(shardId, currentNodeId, null, primary, state, buildRecoveryTarget(primary, state), buildUnassignedInfo(state), buildAllocationId(state), -1); return new ShardRouting(shardId, currentNodeId, null, primary, state, buildRecoveryTarget(primary, state),
buildUnassignedInfo(state), buildAllocationId(state), -1);
} }
public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, boolean primary, ShardRoutingState state, RecoverySource recoverySource) { public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, boolean primary,
return new ShardRouting(shardId, currentNodeId, null, primary, state, recoverySource, buildUnassignedInfo(state), buildAllocationId(state), -1); ShardRoutingState state, RecoverySource recoverySource) {
return new ShardRouting(shardId, currentNodeId, null, primary, state, recoverySource,
buildUnassignedInfo(state), buildAllocationId(state), -1);
} }
public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId, String relocatingNodeId, boolean primary, ShardRoutingState state) { public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId, String relocatingNodeId,
return newShardRouting(new ShardId(index, IndexMetaData.INDEX_UUID_NA_VALUE, shardId), currentNodeId, relocatingNodeId, primary, state); boolean primary, ShardRoutingState state) {
return newShardRouting(new ShardId(index, IndexMetaData.INDEX_UUID_NA_VALUE, shardId), currentNodeId,
relocatingNodeId, primary, state);
} }
public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, String relocatingNodeId, boolean primary, ShardRoutingState state) { public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, String relocatingNodeId,
return new ShardRouting(shardId, currentNodeId, relocatingNodeId, primary, state, buildRecoveryTarget(primary, state), buildUnassignedInfo(state), buildAllocationId(state), -1); boolean primary, ShardRoutingState state) {
return new ShardRouting(shardId, currentNodeId, relocatingNodeId, primary, state,
buildRecoveryTarget(primary, state), buildUnassignedInfo(state), buildAllocationId(state), -1);
} }
public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId, String relocatingNodeId, boolean primary, ShardRoutingState state, AllocationId allocationId) { public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId,
return newShardRouting(new ShardId(index, IndexMetaData.INDEX_UUID_NA_VALUE, shardId), currentNodeId, relocatingNodeId, primary, state, allocationId); String relocatingNodeId, boolean primary, ShardRoutingState state, AllocationId allocationId) {
return newShardRouting(new ShardId(index, IndexMetaData.INDEX_UUID_NA_VALUE, shardId), currentNodeId,
relocatingNodeId, primary, state, allocationId);
} }
public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, String relocatingNodeId, boolean primary, ShardRoutingState state, AllocationId allocationId) { public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, String relocatingNodeId, boolean primary,
return new ShardRouting(shardId, currentNodeId, relocatingNodeId, primary, state, buildRecoveryTarget(primary, state), buildUnassignedInfo(state), allocationId, -1); ShardRoutingState state, AllocationId allocationId) {
return new ShardRouting(shardId, currentNodeId, relocatingNodeId, primary, state,
buildRecoveryTarget(primary, state), buildUnassignedInfo(state), allocationId, -1);
} }
public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId, public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId,
String relocatingNodeId, boolean primary, ShardRoutingState state, String relocatingNodeId, boolean primary, ShardRoutingState state,
UnassignedInfo unassignedInfo) { UnassignedInfo unassignedInfo) {
return newShardRouting(new ShardId(index, IndexMetaData.INDEX_UUID_NA_VALUE, shardId), currentNodeId, relocatingNodeId, primary, state, unassignedInfo); return newShardRouting(new ShardId(index, IndexMetaData.INDEX_UUID_NA_VALUE, shardId), currentNodeId, relocatingNodeId,
primary, state, unassignedInfo);
} }
public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId,
String relocatingNodeId, boolean primary, ShardRoutingState state, String relocatingNodeId, boolean primary, ShardRoutingState state,
UnassignedInfo unassignedInfo) { UnassignedInfo unassignedInfo) {
return new ShardRouting(shardId, currentNodeId, relocatingNodeId, primary, state, buildRecoveryTarget(primary, state), unassignedInfo, buildAllocationId(state), -1); return new ShardRouting(shardId, currentNodeId, relocatingNodeId, primary, state, buildRecoveryTarget(primary, state),
unassignedInfo, buildAllocationId(state), -1);
} }
public static ShardRouting relocate(ShardRouting shardRouting, String relocatingNodeId, long expectedShardSize) { public static ShardRouting relocate(ShardRouting shardRouting, String relocatingNodeId, long expectedShardSize) {

View File

@ -21,150 +21,17 @@ package org.elasticsearch.common.inject;
import org.elasticsearch.common.inject.spi.Element; import org.elasticsearch.common.inject.spi.Element;
import org.elasticsearch.common.inject.spi.Elements; import org.elasticsearch.common.inject.spi.Elements;
import org.elasticsearch.common.inject.spi.InstanceBinding; import org.elasticsearch.common.inject.spi.InstanceBinding;
import org.elasticsearch.common.inject.spi.LinkedKeyBinding;
import org.elasticsearch.common.inject.spi.ProviderInstanceBinding; import org.elasticsearch.common.inject.spi.ProviderInstanceBinding;
import org.elasticsearch.common.inject.spi.ProviderLookup;
import org.elasticsearch.common.inject.spi.UntargettedBinding;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
* Base testcase for testing {@link Module} implementations. * Base testcase for testing {@link Module} implementations.
*/ */
public abstract class ModuleTestCase extends ESTestCase { public abstract class ModuleTestCase extends ESTestCase {
/** Configures the module and asserts "clazz" is bound to "to". */
public void assertBinding(Module module, Class<?> to, Class<?> clazz) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding<?> binding = (LinkedKeyBinding<?>) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
assertSame(clazz, binding.getLinkedKey().getTypeLiteral().getType());
return;
}
} else if (element instanceof UntargettedBinding) {
UntargettedBinding<?> binding = (UntargettedBinding<?>) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
assertSame(clazz, to);
return;
}
}
}
StringBuilder s = new StringBuilder();
for (Element element : elements) {
s.append(element).append("\n");
}
fail("Did not find any binding to " + to.getName() + ". Found these bindings:\n" + s);
}
/** Configures the module and asserts "clazz" is not bound to anything. */
public void assertNotBound(Module module, Class<?> clazz) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding<?> binding = (LinkedKeyBinding<?>) element;
if (clazz.equals(binding.getKey().getTypeLiteral().getType())) {
fail("Found binding for " + clazz.getName() + " to " + binding.getKey().getTypeLiteral().getType().getTypeName());
}
} else if (element instanceof UntargettedBinding) {
UntargettedBinding<?> binding = (UntargettedBinding<?>) element;
if (clazz.equals(binding.getKey().getTypeLiteral().getType())) {
fail("Found binding for " + clazz.getName());
}
}
}
}
/**
* Attempts to configure the module, and asserts an {@link IllegalArgumentException} is
* caught, containing the given messages
*/
public void assertBindingFailure(Module module, String... msgs) {
try {
List<Element> elements = Elements.getElements(module);
StringBuilder s = new StringBuilder();
for (Element element : elements) {
s.append(element).append("\n");
}
fail("Expected exception from configuring module. Found these bindings:\n" + s);
} catch (IllegalArgumentException e) {
for (String msg : msgs) {
assertTrue(e.getMessage() + " didn't contain: " + msg, e.getMessage().contains(msg));
}
}
}
/**
* Configures the module and checks a Map&lt;String, Class&gt; of the "to" class
* is bound to "theClass".
*/
public void assertMapMultiBinding(Module module, Class<?> to, Class<?> theClass) {
List<Element> elements = Elements.getElements(module);
Set<Type> bindings = new HashSet<>();
boolean providerFound = false;
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding<?> binding = (LinkedKeyBinding<?>) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
bindings.add(binding.getLinkedKey().getTypeLiteral().getType());
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) element;
String setType = binding.getKey().getTypeLiteral().getType().toString();
if (setType.equals("java.util.Map<java.lang.String, " + to.getName() + ">")) {
providerFound = true;
}
}
}
if (bindings.contains(theClass) == false) {
fail("Expected to find " + theClass.getName() + " as binding to " + to.getName() + ", found these classes:\n" + bindings);
}
assertTrue("Did not find provider for map of " + to.getName(), providerFound);
}
/**
* Configures the module and checks a Set of the "to" class
* is bound to "classes". There may be more classes bound
* to "to" than just "classes".
*/
public void assertSetMultiBinding(Module module, Class<?> to, Class<?>... classes) {
List<Element> elements = Elements.getElements(module);
Set<Type> bindings = new HashSet<>();
boolean providerFound = false;
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding<?> binding = (LinkedKeyBinding<?>) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
bindings.add(binding.getLinkedKey().getTypeLiteral().getType());
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) element;
String setType = binding.getKey().getTypeLiteral().getType().toString();
if (setType.equals("java.util.Set<" + to.getName() + ">")) {
providerFound = true;
}
}
}
for (Class<?> clazz : classes) {
if (bindings.contains(clazz) == false) {
fail("Expected to find " + clazz.getName() + " as set binding to " + to.getName() + ", found these classes:\n" + bindings);
}
}
assertTrue("Did not find provider for set of " + to.getName(), providerFound);
}
/** /**
* Configures the module, and ensures an instance is bound to the "to" class, and the * Configures the module, and ensures an instance is bound to the "to" class, and the
* provided tester returns true on the instance. * provided tester returns true on the instance.
@ -173,33 +40,12 @@ public abstract class ModuleTestCase extends ESTestCase {
assertInstanceBindingWithAnnotation(module, to, tester, null); assertInstanceBindingWithAnnotation(module, to, tester, null);
} }
/**
* Configures the module, and returns an instance bound to the "to" class.
*/
public static <T> T bindAndGetInstance(Module module, Class<T> to) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof InstanceBinding) {
InstanceBinding<?> binding = (InstanceBinding<?>) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
return to.cast(binding.getInstance());
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
return to.cast(binding.getProviderInstance().get());
}
}
}
fail("can't get instance for class " + to);
return null; // won't happen ;)
}
/** /**
* Like {@link #assertInstanceBinding(Module, Class, Predicate)}, but filters the * Like {@link #assertInstanceBinding(Module, Class, Predicate)}, but filters the
* classes checked by the given annotation. * classes checked by the given annotation.
*/ */
public <T> void assertInstanceBindingWithAnnotation(Module module, Class<T> to, Predicate<T> tester, Class<? extends Annotation> annotation) { private <T> void assertInstanceBindingWithAnnotation(Module module, Class<T> to,
Predicate<T> tester, Class<? extends Annotation> annotation) {
List<Element> elements = Elements.getElements(module); List<Element> elements = Elements.getElements(module);
for (Element element : elements) { for (Element element : elements) {
if (element instanceof InstanceBinding) { if (element instanceof InstanceBinding) {
@ -224,39 +70,4 @@ public abstract class ModuleTestCase extends ESTestCase {
} }
fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s); fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s);
} }
/**
* Configures the module, and ensures a map exists between the "keyType" and "valueType",
* and that all of the "expected" values are bound.
*/
@SuppressWarnings("unchecked")
public <K, V> void assertMapInstanceBinding(Module module, Class<K> keyType, Class<V> valueType, Map<K, V> expected) throws Exception {
// this method is insane because java type erasure makes it incredibly difficult...
Map<K, Key<?>> keys = new HashMap<>();
Map<Key<?>, V> values = new HashMap<>();
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
if (element instanceof InstanceBinding) {
InstanceBinding<?> binding = (InstanceBinding<?>) element;
if (binding.getKey().getRawType().equals(valueType)) {
values.put(binding.getKey(), (V) binding.getInstance());
} else if (binding.getInstance() instanceof Map.Entry) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) binding.getInstance();
Object key = entry.getKey();
Object providerValue = entry.getValue();
if (key.getClass().equals(keyType) && providerValue instanceof ProviderLookup.ProviderImpl) {
ProviderLookup.ProviderImpl<?> provider = (ProviderLookup.ProviderImpl<?>) providerValue;
keys.put((K) key, provider.getKey());
}
}
}
}
for (Map.Entry<K, V> entry : expected.entrySet()) {
Key<?> valueKey = keys.get(entry.getKey());
assertNotNull("Could not find binding for key [" + entry.getKey() + "], found these keys:\n" + keys.keySet(), valueKey);
V value = values.get(valueKey);
assertNotNull("Could not find value for instance key [" + valueKey + "], found these bindings:\n" + elements);
assertEquals(entry.getValue(), value);
}
}
} }

View File

@ -142,7 +142,8 @@ public class BackgroundIndexer implements AutoCloseable {
if (batch) { if (batch) {
int batchSize = threadRandom.nextInt(20) + 1; int batchSize = threadRandom.nextInt(20) + 1;
if (hasBudget.get()) { if (hasBudget.get()) {
batchSize = Math.max(Math.min(batchSize, availableBudget.availablePermits()), 1);// always try to get at least one // always try to get at least one
batchSize = Math.max(Math.min(batchSize, availableBudget.availablePermits()), 1);
if (!availableBudget.tryAcquire(batchSize, 250, TimeUnit.MILLISECONDS)) { if (!availableBudget.tryAcquire(batchSize, 250, TimeUnit.MILLISECONDS)) {
// time out -> check if we have to stop. // time out -> check if we have to stop.
continue; continue;
@ -155,7 +156,8 @@ public class BackgroundIndexer implements AutoCloseable {
if (useAutoGeneratedIDs) { if (useAutoGeneratedIDs) {
bulkRequest.add(client.prepareIndex(index, type).setSource(generateSource(id, threadRandom))); bulkRequest.add(client.prepareIndex(index, type).setSource(generateSource(id, threadRandom)));
} else { } else {
bulkRequest.add(client.prepareIndex(index, type, Long.toString(id)).setSource(generateSource(id, threadRandom))); bulkRequest.add(client.prepareIndex(index, type, Long.toString(id))
.setSource(generateSource(id, threadRandom)));
} }
} }
BulkResponse bulkResponse = bulkRequest.get(); BulkResponse bulkResponse = bulkRequest.get();
@ -165,7 +167,8 @@ public class BackgroundIndexer implements AutoCloseable {
assert add : "ID: " + bulkItemResponse.getId() + " already used"; assert add : "ID: " + bulkItemResponse.getId() + " already used";
} else { } else {
throw new ElasticsearchException("bulk request failure, id: [" throw new ElasticsearchException("bulk request failure, id: ["
+ bulkItemResponse.getFailure().getId() + "] message: " + bulkItemResponse.getFailure().getMessage()); + bulkItemResponse.getFailure().getId() + "] message: "
+ bulkItemResponse.getFailure().getMessage());
} }
} }
@ -177,11 +180,13 @@ public class BackgroundIndexer implements AutoCloseable {
} }
id = idGenerator.incrementAndGet(); id = idGenerator.incrementAndGet();
if (useAutoGeneratedIDs) { if (useAutoGeneratedIDs) {
IndexResponse indexResponse = client.prepareIndex(index, type).setSource(generateSource(id, threadRandom)).get(); IndexResponse indexResponse = client.prepareIndex(index, type)
.setSource(generateSource(id, threadRandom)).get();
boolean add = ids.add(indexResponse.getId()); boolean add = ids.add(indexResponse.getId());
assert add : "ID: " + indexResponse.getId() + " already used"; assert add : "ID: " + indexResponse.getId() + " already used";
} else { } else {
IndexResponse indexResponse = client.prepareIndex(index, type, Long.toString(id)).setSource(generateSource(id, threadRandom)).get(); IndexResponse indexResponse = client.prepareIndex(index, type, Long.toString(id))
.setSource(generateSource(id, threadRandom)).get();
boolean add = ids.add(indexResponse.getId()); boolean add = ids.add(indexResponse.getId());
assert add : "ID: " + indexResponse.getId() + " already used"; assert add : "ID: " + indexResponse.getId() + " already used";
} }

View File

@ -93,7 +93,8 @@ public final class CorruptionUtils {
// rewrite // rewrite
raf.position(filePointer); raf.position(filePointer);
raf.write(bb); raf.write(bb);
logger.info("Corrupting file -- flipping at position {} from {} to {} file: {}", filePointer, Integer.toHexString(oldValue), Integer.toHexString(newValue), fileToCorrupt.getFileName()); logger.info("Corrupting file -- flipping at position {} from {} to {} file: {}", filePointer,
Integer.toHexString(oldValue), Integer.toHexString(newValue), fileToCorrupt.getFileName());
} }
long checksumAfterCorruption; long checksumAfterCorruption;
long actualChecksumAfterCorruption; long actualChecksumAfterCorruption;
@ -109,7 +110,8 @@ public final class CorruptionUtils {
msg.append("before: [").append(checksumBeforeCorruption).append("] "); msg.append("before: [").append(checksumBeforeCorruption).append("] ");
msg.append("after: [").append(checksumAfterCorruption).append("] "); msg.append("after: [").append(checksumAfterCorruption).append("] ");
msg.append("checksum value after corruption: ").append(actualChecksumAfterCorruption).append("] "); msg.append("checksum value after corruption: ").append(actualChecksumAfterCorruption).append("] ");
msg.append("file: ").append(fileToCorrupt.getFileName()).append(" length: ").append(dir.fileLength(fileToCorrupt.getFileName().toString())); msg.append("file: ").append(fileToCorrupt.getFileName()).append(" length: ");
msg.append(dir.fileLength(fileToCorrupt.getFileName().toString()));
logger.info("Checksum {}", msg); logger.info("Checksum {}", msg);
assumeTrue("Checksum collision - " + msg.toString(), assumeTrue("Checksum collision - " + msg.toString(),
checksumAfterCorruption != checksumBeforeCorruption // collision checksumAfterCorruption != checksumBeforeCorruption // collision

View File

@ -264,7 +264,8 @@ import static org.hamcrest.Matchers.startsWith;
* <p> * <p>
* This class supports the following system properties (passed with -Dkey=value to the application) * This class supports the following system properties (passed with -Dkey=value to the application)
* <ul> * <ul>
* <li>-D{@value #TESTS_CLIENT_RATIO} - a double value in the interval [0..1] which defines the ration between node and transport clients used</li> * <li>-D{@value #TESTS_CLIENT_RATIO} - a double value in the interval [0..1] which defines the ration between node
* and transport clients used</li>
* <li>-D{@value #TESTS_ENABLE_MOCK_MODULES} - a boolean value to enable or disable mock modules. This is * <li>-D{@value #TESTS_ENABLE_MOCK_MODULES} - a boolean value to enable or disable mock modules. This is
* useful to test the system without asserting modules that to make sure they don't hide any bugs in production.</li> * useful to test the system without asserting modules that to make sure they don't hide any bugs in production.</li>
* <li> - a random seed used to initialize the index random context. * <li> - a random seed used to initialize the index random context.
@ -470,7 +471,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
if (randomBoolean()) { if (randomBoolean()) {
// keep this low so we don't stall tests // keep this low so we don't stall tests
builder.put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 1, 15) + "ms"); builder.put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(),
RandomNumbers.randomIntBetween(random, 1, 15) + "ms");
} }
return builder; return builder;
@ -495,17 +497,21 @@ public abstract class ESIntegTestCase extends ESTestCase {
private static Settings.Builder setRandomIndexTranslogSettings(Random random, Settings.Builder builder) { private static Settings.Builder setRandomIndexTranslogSettings(Random random, Settings.Builder builder) {
if (random.nextBoolean()) { if (random.nextBoolean()) {
builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 300), ByteSizeUnit.MB)); builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(),
new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 300), ByteSizeUnit.MB));
} }
if (random.nextBoolean()) { if (random.nextBoolean()) {
builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.PB)); // just don't flush builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(),
new ByteSizeValue(1, ByteSizeUnit.PB)); // just don't flush
} }
if (random.nextBoolean()) { if (random.nextBoolean()) {
builder.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), RandomPicks.randomFrom(random, Translog.Durability.values())); builder.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(),
RandomPicks.randomFrom(random, Translog.Durability.values()));
} }
if (random.nextBoolean()) { if (random.nextBoolean()) {
builder.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 100, 5000), TimeUnit.MILLISECONDS); builder.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(),
RandomNumbers.randomIntBetween(random, 100, 5000), TimeUnit.MILLISECONDS);
} }
return builder; return builder;
@ -871,7 +877,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
if (fieldName.indexOf('.') != -1) { if (fieldName.indexOf('.') != -1) {
fieldName = fieldName.replace(".", ".properties."); fieldName = fieldName.replace(".", ".properties.");
} }
assertThat("field " + fieldName + " doesn't exists in mapping " + mappingMetaData.source().string(), XContentMapValues.extractValue(fieldName, mappingProperties), notNullValue()); assertThat("field " + fieldName + " doesn't exists in mapping " + mappingMetaData.source().string(),
XContentMapValues.extractValue(fieldName, mappingProperties), notNullValue());
} }
} }
@ -1036,7 +1043,7 @@ public abstract class ESIntegTestCase extends ESTestCase {
* @param numDocs number of documents to wait for * @param numDocs number of documents to wait for
* @param maxWaitTime if not progress have been made during this time, fail the test * @param maxWaitTime if not progress have been made during this time, fail the test
* @param maxWaitTimeUnit the unit in which maxWaitTime is specified * @param maxWaitTimeUnit the unit in which maxWaitTime is specified
* @param indexer a {@link org.elasticsearch.test.BackgroundIndexer}. If supplied it will be first checked for documents indexed. * @param indexer If supplied it will be first checked for documents indexed.
* This saves on unneeded searches. * This saves on unneeded searches.
* @return the actual number of docs seen. * @return the actual number of docs seen.
*/ */
@ -1050,7 +1057,7 @@ public abstract class ESIntegTestCase extends ESTestCase {
} }
if (lastKnownCount.get() >= numDocs) { if (lastKnownCount.get() >= numDocs) {
try { try {
long count = client().prepareSearch().setSize(0).setQuery(matchAllQuery()).execute().actionGet().getHits().getTotalHits(); long count = client().prepareSearch().setSize(0).setQuery(matchAllQuery()).get().getHits().getTotalHits();
if (count == lastKnownCount.get()) { if (count == lastKnownCount.get()) {
// no progress - try to refresh for the next time // no progress - try to refresh for the next time
client().admin().indices().prepareRefresh().get(); client().admin().indices().prepareRefresh().get();
@ -1426,7 +1433,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
indexRandom(forceRefresh, Arrays.asList(builders)); indexRandom(forceRefresh, Arrays.asList(builders));
} }
public void indexRandom(boolean forceRefresh, boolean dummyDocuments, IndexRequestBuilder... builders) throws InterruptedException, ExecutionException { public void indexRandom(boolean forceRefresh, boolean dummyDocuments, IndexRequestBuilder... builders)
throws InterruptedException, ExecutionException {
indexRandom(forceRefresh, dummyDocuments, Arrays.asList(builders)); indexRandom(forceRefresh, dummyDocuments, Arrays.asList(builders));
} }
@ -1437,9 +1445,11 @@ public abstract class ESIntegTestCase extends ESTestCase {
* segment or if only one document is in a segment etc. This method prevents issues like this by randomizing the index * segment or if only one document is in a segment etc. This method prevents issues like this by randomizing the index
* layout. * layout.
* *
* @param forceRefresh if {@code true} all involved indices are refreshed once the documents are indexed. Additionally if {@code true} * @param forceRefresh if {@code true} all involved indices are refreshed
* some empty dummy documents are may be randomly inserted into the document list and deleted once all documents are indexed. * once the documents are indexed. Additionally if {@code true} some
* This is useful to produce deleted documents on the server side. * empty dummy documents are may be randomly inserted into the document
* list and deleted once all documents are indexed. This is useful to
* produce deleted documents on the server side.
* @param builders the documents to index. * @param builders the documents to index.
* @see #indexRandom(boolean, boolean, java.util.List) * @see #indexRandom(boolean, boolean, java.util.List)
*/ */
@ -1459,7 +1469,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
* all documents are indexed. This is useful to produce deleted documents on the server side. * all documents are indexed. This is useful to produce deleted documents on the server side.
* @param builders the documents to index. * @param builders the documents to index.
*/ */
public void indexRandom(boolean forceRefresh, boolean dummyDocuments, List<IndexRequestBuilder> builders) throws InterruptedException, ExecutionException { public void indexRandom(boolean forceRefresh, boolean dummyDocuments, List<IndexRequestBuilder> builders)
throws InterruptedException, ExecutionException {
indexRandom(forceRefresh, dummyDocuments, true, builders); indexRandom(forceRefresh, dummyDocuments, true, builders);
} }
@ -1476,7 +1487,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
* @param maybeFlush if {@code true} this method may randomly execute full flushes after index operations. * @param maybeFlush if {@code true} this method may randomly execute full flushes after index operations.
* @param builders the documents to index. * @param builders the documents to index.
*/ */
public void indexRandom(boolean forceRefresh, boolean dummyDocuments, boolean maybeFlush, List<IndexRequestBuilder> builders) throws InterruptedException, ExecutionException { public void indexRandom(boolean forceRefresh, boolean dummyDocuments, boolean maybeFlush, List<IndexRequestBuilder> builders)
throws InterruptedException, ExecutionException {
Random random = random(); Random random = random();
Map<String, Set<String>> indicesAndTypes = new HashMap<>(); Map<String, Set<String>> indicesAndTypes = new HashMap<>();
for (IndexRequestBuilder builder : builders) { for (IndexRequestBuilder builder : builders) {
@ -1490,7 +1502,9 @@ public abstract class ESIntegTestCase extends ESTestCase {
final int numBogusDocs = scaledRandomIntBetween(1, builders.size() * 2); final int numBogusDocs = scaledRandomIntBetween(1, builders.size() * 2);
final int unicodeLen = between(1, 10); final int unicodeLen = between(1, 10);
for (int i = 0; i < numBogusDocs; i++) { for (int i = 0; i < numBogusDocs; i++) {
String id = "bogus_doc_" + randomRealisticUnicodeOfLength(unicodeLen) + Integer.toString(dummmyDocIdGenerator.incrementAndGet()); String id = "bogus_doc_"
+ randomRealisticUnicodeOfLength(unicodeLen)
+ Integer.toString(dummmyDocIdGenerator.incrementAndGet());
Map.Entry<String, Set<String>> indexAndTypes = RandomPicks.randomFrom(random, indicesAndTypes.entrySet()); Map.Entry<String, Set<String>> indexAndTypes = RandomPicks.randomFrom(random, indicesAndTypes.entrySet());
String index = indexAndTypes.getKey(); String index = indexAndTypes.getKey();
String type = RandomPicks.randomFrom(random, indexAndTypes.getValue()); String type = RandomPicks.randomFrom(random, indexAndTypes.getValue());
@ -1508,7 +1522,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
if (frequently()) { if (frequently()) {
logger.info("Index [{}] docs async: [{}] bulk: [{}]", builders.size(), true, false); logger.info("Index [{}] docs async: [{}] bulk: [{}]", builders.size(), true, false);
for (IndexRequestBuilder indexRequestBuilder : builders) { for (IndexRequestBuilder indexRequestBuilder : builders) {
indexRequestBuilder.execute(new PayloadLatchedActionListener<IndexResponse, IndexRequestBuilder>(indexRequestBuilder, newLatch(inFlightAsyncOperations), errors)); indexRequestBuilder.execute(
new PayloadLatchedActionListener<>(indexRequestBuilder, newLatch(inFlightAsyncOperations), errors));
postIndexAsyncActions(indices, inFlightAsyncOperations, maybeFlush); postIndexAsyncActions(indices, inFlightAsyncOperations, maybeFlush);
} }
} else { } else {
@ -1552,7 +1567,9 @@ public abstract class ESIntegTestCase extends ESTestCase {
} }
} }
if (forceRefresh) { if (forceRefresh) {
assertNoFailures(client().admin().indices().prepareRefresh(indices).setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().get()); assertNoFailures(client().admin().indices().prepareRefresh(indices)
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
.get());
} }
} }
@ -1586,7 +1603,8 @@ public abstract class ESIntegTestCase extends ESTestCase {
/** /**
* Maybe refresh, force merge, or flush then always make sure there aren't too many in flight async operations. * Maybe refresh, force merge, or flush then always make sure there aren't too many in flight async operations.
*/ */
private void postIndexAsyncActions(String[] indices, List<CountDownLatch> inFlightAsyncOperations, boolean maybeFlush) throws InterruptedException { private void postIndexAsyncActions(String[] indices, List<CountDownLatch> inFlightAsyncOperations, boolean maybeFlush)
throws InterruptedException {
if (rarely()) { if (rarely()) {
if (rarely()) { if (rarely()) {
client().admin().indices().prepareRefresh(indices).setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute( client().admin().indices().prepareRefresh(indices).setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute(
@ -1600,8 +1618,11 @@ public abstract class ESIntegTestCase extends ESTestCase {
new LatchedActionListener<>(newLatch(inFlightAsyncOperations))); new LatchedActionListener<>(newLatch(inFlightAsyncOperations)));
} }
} else if (rarely()) { } else if (rarely()) {
client().admin().indices().prepareForceMerge(indices).setIndicesOptions(IndicesOptions.lenientExpandOpen()).setMaxNumSegments(between(1, 10)).setFlush(maybeFlush && randomBoolean()).execute( client().admin().indices().prepareForceMerge(indices)
new LatchedActionListener<>(newLatch(inFlightAsyncOperations))); .setIndicesOptions(IndicesOptions.lenientExpandOpen())
.setMaxNumSegments(between(1, 10))
.setFlush(maybeFlush && randomBoolean())
.execute(new LatchedActionListener<>(newLatch(inFlightAsyncOperations)));
} }
} }
while (inFlightAsyncOperations.size() > MAX_IN_FLIGHT_ASYNC_INDEXES) { while (inFlightAsyncOperations.size() > MAX_IN_FLIGHT_ASYNC_INDEXES) {
@ -1774,12 +1795,14 @@ public abstract class ESIntegTestCase extends ESTestCase {
private int getMinNumDataNodes() { private int getMinNumDataNodes() {
ClusterScope annotation = getAnnotation(this.getClass(), ClusterScope.class); ClusterScope annotation = getAnnotation(this.getClass(), ClusterScope.class);
return annotation == null || annotation.minNumDataNodes() == -1 ? InternalTestCluster.DEFAULT_MIN_NUM_DATA_NODES : annotation.minNumDataNodes(); return annotation == null || annotation.minNumDataNodes() == -1
? InternalTestCluster.DEFAULT_MIN_NUM_DATA_NODES : annotation.minNumDataNodes();
} }
private int getMaxNumDataNodes() { private int getMaxNumDataNodes() {
ClusterScope annotation = getAnnotation(this.getClass(), ClusterScope.class); ClusterScope annotation = getAnnotation(this.getClass(), ClusterScope.class);
return annotation == null || annotation.maxNumDataNodes() == -1 ? InternalTestCluster.DEFAULT_MAX_NUM_DATA_NODES : annotation.maxNumDataNodes(); return annotation == null || annotation.maxNumDataNodes() == -1
? InternalTestCluster.DEFAULT_MAX_NUM_DATA_NODES : annotation.maxNumDataNodes();
} }
private int getNumClientNodes() { private int getNumClientNodes() {

View File

@ -48,7 +48,8 @@ public class IndexSettingsModule extends AbstractModule {
} }
public static IndexSettings newIndexSettings(String index, Settings settings, Setting<?>... setting) { public static IndexSettings newIndexSettings(String index, Settings settings, Setting<?>... setting) {
return newIndexSettings(new Index(index, settings.get(IndexMetaData.SETTING_INDEX_UUID, IndexMetaData.INDEX_UUID_NA_VALUE)), settings, setting); return newIndexSettings(new Index(index, settings.get(IndexMetaData.SETTING_INDEX_UUID, IndexMetaData.INDEX_UUID_NA_VALUE)),
settings, setting);
} }
public static IndexSettings newIndexSettings(Index index, Settings settings, Setting<?>... setting) { public static IndexSettings newIndexSettings(Index index, Settings settings, Setting<?>... setting) {

View File

@ -134,6 +134,8 @@ import java.util.stream.Stream;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY; import static org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY;
import static org.apache.lucene.util.LuceneTestCase.rarely; import static org.apache.lucene.util.LuceneTestCase.rarely;
import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.elasticsearch.discovery.DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING; import static org.elasticsearch.discovery.DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING;
import static org.elasticsearch.discovery.zen.ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING; import static org.elasticsearch.discovery.zen.ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING;
import static org.elasticsearch.discovery.zen.FileBasedUnicastHostsProvider.UNICAST_HOSTS_FILE; import static org.elasticsearch.discovery.zen.FileBasedUnicastHostsProvider.UNICAST_HOSTS_FILE;
@ -301,7 +303,8 @@ public final class InternalTestCluster extends TestCluster {
this.numSharedDedicatedMasterNodes = 0; this.numSharedDedicatedMasterNodes = 0;
} }
if (numClientNodes < 0) { if (numClientNodes < 0) {
this.numSharedCoordOnlyNodes = RandomNumbers.randomIntBetween(random, DEFAULT_MIN_NUM_CLIENT_NODES, DEFAULT_MAX_NUM_CLIENT_NODES); this.numSharedCoordOnlyNodes = RandomNumbers.randomIntBetween(random,
DEFAULT_MIN_NUM_CLIENT_NODES, DEFAULT_MAX_NUM_CLIENT_NODES);
} else { } else {
this.numSharedCoordOnlyNodes = numClientNodes; this.numSharedCoordOnlyNodes = numClientNodes;
} }
@ -359,16 +362,22 @@ public final class InternalTestCluster extends TestCluster {
builder.put(ScriptService.SCRIPT_MAX_COMPILATIONS_RATE.getKey(), "1000/1m"); builder.put(ScriptService.SCRIPT_MAX_COMPILATIONS_RATE.getKey(), "1000/1m");
builder.put(OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING.getKey(), random.nextBoolean()); builder.put(OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING.getKey(), random.nextBoolean());
if (TEST_NIGHTLY) { if (TEST_NIGHTLY) {
builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_INCOMING_RECOVERIES_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 5, 10)); builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_INCOMING_RECOVERIES_SETTING.getKey(),
builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 5, 10)); RandomNumbers.randomIntBetween(random, 5, 10));
builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(),
RandomNumbers.randomIntBetween(random, 5, 10));
} else if (random.nextInt(100) <= 90) { } else if (random.nextInt(100) <= 90) {
builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_INCOMING_RECOVERIES_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 2, 5)); builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_INCOMING_RECOVERIES_SETTING.getKey(),
builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 2, 5)); RandomNumbers.randomIntBetween(random, 2, 5));
builder.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(),
RandomNumbers.randomIntBetween(random, 2, 5));
} }
// always reduce this - it can make tests really slow // always reduce this - it can make tests really slow
builder.put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_STATE_SYNC_SETTING.getKey(), TimeValue.timeValueMillis(RandomNumbers.randomIntBetween(random, 20, 50))); builder.put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_STATE_SYNC_SETTING.getKey(), TimeValue.timeValueMillis(
RandomNumbers.randomIntBetween(random, 20, 50)));
defaultSettings = builder.build(); defaultSettings = builder.build();
executor = EsExecutors.newScaling("internal_test_cluster_executor", 0, Integer.MAX_VALUE, 0, TimeUnit.SECONDS, EsExecutors.daemonThreadFactory("test_" + clusterName), new ThreadContext(Settings.EMPTY)); executor = EsExecutors.newScaling("internal_test_cluster_executor", 0, Integer.MAX_VALUE, 0, TimeUnit.SECONDS,
EsExecutors.daemonThreadFactory("test_" + clusterName), new ThreadContext(Settings.EMPTY));
} }
@Override @Override
@ -391,7 +400,9 @@ public final class InternalTestCluster extends TestCluster {
Settings settings = nodeConfigurationSource.nodeSettings(nodeOrdinal); Settings settings = nodeConfigurationSource.nodeSettings(nodeOrdinal);
if (settings != null) { if (settings != null) {
if (settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) != null) { if (settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) != null) {
throw new IllegalStateException("Tests must not set a '" + ClusterName.CLUSTER_NAME_SETTING.getKey() + "' as a node setting set '" + ClusterName.CLUSTER_NAME_SETTING.getKey() + "': [" + settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) + "]"); throw new IllegalStateException("Tests must not set a '" + ClusterName.CLUSTER_NAME_SETTING.getKey()
+ "' as a node setting set '" + ClusterName.CLUSTER_NAME_SETTING.getKey() + "': ["
+ settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) + "]");
} }
builder.put(settings); builder.put(settings);
} }
@ -416,12 +427,12 @@ public final class InternalTestCluster extends TestCluster {
builder.put("cache.recycler.page.type", RandomPicks.randomFrom(random, PageCacheRecycler.Type.values())); builder.put("cache.recycler.page.type", RandomPicks.randomFrom(random, PageCacheRecycler.Type.values()));
} }
if (random.nextInt(10) == 0) { // 10% of the nodes have a very frequent check interval if (random.nextInt(10) == 0) { // 10% of the nodes have a very frequent check interval
builder.put(SearchService.KEEPALIVE_INTERVAL_SETTING.getKey(), TimeValue.timeValueMillis(10 + random.nextInt(2000)).getStringRep()); builder.put(SearchService.KEEPALIVE_INTERVAL_SETTING.getKey(), timeValueMillis(10 + random.nextInt(2000)).getStringRep());
} else if (random.nextInt(10) != 0) { // 90% of the time - 10% of the time we don't set anything } else if (random.nextInt(10) != 0) { // 90% of the time - 10% of the time we don't set anything
builder.put(SearchService.KEEPALIVE_INTERVAL_SETTING.getKey(), TimeValue.timeValueSeconds(10 + random.nextInt(5 * 60)).getStringRep()); builder.put(SearchService.KEEPALIVE_INTERVAL_SETTING.getKey(), timeValueSeconds(10 + random.nextInt(5 * 60)).getStringRep());
} }
if (random.nextBoolean()) { // sometimes set a if (random.nextBoolean()) { // sometimes set a
builder.put(SearchService.DEFAULT_KEEPALIVE_SETTING.getKey(), TimeValue.timeValueSeconds(100 + random.nextInt(5 * 60)).getStringRep()); builder.put(SearchService.DEFAULT_KEEPALIVE_SETTING.getKey(), timeValueSeconds(100 + random.nextInt(5 * 60)).getStringRep());
} }
builder.put(EsExecutors.PROCESSORS_SETTING.getKey(), 1 + random.nextInt(3)); builder.put(EsExecutors.PROCESSORS_SETTING.getKey(), 1 + random.nextInt(3));
@ -439,7 +450,8 @@ public final class InternalTestCluster extends TestCluster {
} }
if (random.nextBoolean()) { if (random.nextBoolean()) {
builder.put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(), new TimeValue(RandomNumbers.randomIntBetween(random, 10, 30), TimeUnit.SECONDS)); builder.put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(),
timeValueSeconds(RandomNumbers.randomIntBetween(random, 10, 30)).getStringRep());
} }
// turning on the real memory circuit breaker leads to spurious test failures. As have no full control over heap usage, we // turning on the real memory circuit breaker leads to spurious test failures. As have no full control over heap usage, we
@ -453,9 +465,11 @@ public final class InternalTestCluster extends TestCluster {
if (random.nextBoolean()) { if (random.nextBoolean()) {
if (random.nextInt(10) == 0) { // do something crazy slow here if (random.nextInt(10) == 0) { // do something crazy slow here
builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 10), ByteSizeUnit.MB)); builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(),
new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 10), ByteSizeUnit.MB));
} else { } else {
builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), new ByteSizeValue(RandomNumbers.randomIntBetween(random, 10, 200), ByteSizeUnit.MB)); builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(),
new ByteSizeValue(RandomNumbers.randomIntBetween(random, 10, 200), ByteSizeUnit.MB));
} }
} }
@ -467,7 +481,8 @@ public final class InternalTestCluster extends TestCluster {
builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 0, 2000)); builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 0, 2000));
} }
if (random.nextBoolean()) { if (random.nextBoolean()) {
builder.put(ScriptService.SCRIPT_CACHE_EXPIRE_SETTING.getKey(), TimeValue.timeValueMillis(RandomNumbers.randomIntBetween(random, 750, 10000000)).getStringRep()); builder.put(ScriptService.SCRIPT_CACHE_EXPIRE_SETTING.getKey(),
timeValueMillis(RandomNumbers.randomIntBetween(random, 750, 10000000)).getStringRep());
} }
return builder.build(); return builder.build();
@ -546,8 +561,10 @@ public final class InternalTestCluster extends TestCluster {
return; return;
} }
// prevent killing the master if possible and client nodes // prevent killing the master if possible and client nodes
final Stream<NodeAndClient> collection = final Stream<NodeAndClient> collection = n == 0
n == 0 ? nodes.values().stream() : nodes.values().stream().filter(new DataNodePredicate().and(new MasterNodePredicate(getMasterName()).negate())); ? nodes.values().stream()
: nodes.values().stream()
.filter(new DataNodePredicate().and(new MasterNodePredicate(getMasterName()).negate()));
final Iterator<NodeAndClient> values = collection.iterator(); final Iterator<NodeAndClient> values = collection.iterator();
logger.info("changing cluster size from {} data nodes to {}", size, n); logger.info("changing cluster size from {} data nodes to {}", size, n);
@ -785,7 +802,8 @@ public final class InternalTestCluster extends TestCluster {
*/ */
public synchronized Client client(final Predicate<Settings> filterPredicate) { public synchronized Client client(final Predicate<Settings> filterPredicate) {
ensureOpen(); ensureOpen();
final NodeAndClient randomNodeAndClient = getRandomNodeAndClient(nodeAndClient -> filterPredicate.test(nodeAndClient.node.settings())); final NodeAndClient randomNodeAndClient =
getRandomNodeAndClient(nodeAndClient -> filterPredicate.test(nodeAndClient.node.settings()));
if (randomNodeAndClient != null) { if (randomNodeAndClient != null) {
return randomNodeAndClient.client(random); return randomNodeAndClient.client(random);
} }
@ -877,10 +895,12 @@ public final class InternalTestCluster extends TestCluster {
private Client getOrBuildTransportClient() { private Client getOrBuildTransportClient() {
if (transportClient == null) { if (transportClient == null) {
/* no sniff client for now - doesn't work will all tests since it might throw NoNodeAvailableException if nodes are shut down. /* don't sniff client for now - doesn't work will all tests
* we first need support of transportClientRatio as annotations or so * since it might throw NoNodeAvailableException if nodes are
*/ * shut down. we first need support of transportClientRatio
transportClient = new TransportClientFactory(false, nodeConfigurationSource.transportClientSettings(), baseDir, nodeConfigurationSource.transportClientPlugins()).client(node, clusterName); * as annotations or so */
transportClient = new TransportClientFactory(false, nodeConfigurationSource.transportClientSettings(),
baseDir, nodeConfigurationSource.transportClientPlugins()).client(node, clusterName);
} }
return clientWrapper.apply(transportClient); return clientWrapper.apply(transportClient);
} }
@ -952,8 +972,13 @@ public final class InternalTestCluster extends TestCluster {
} }
private void createNewNode(final Settings newSettings, final Runnable onTransportServiceStarted) { private void createNewNode(final Settings newSettings, final Runnable onTransportServiceStarted) {
final long newIdSeed = NodeEnvironment.NODE_ID_SEED_SETTING.get(node.settings()) + 1; // use a new seed to make sure we have new node id // use a new seed to make sure we have new node id
Settings finalSettings = Settings.builder().put(node.originalSettings()).put(newSettings).put(NodeEnvironment.NODE_ID_SEED_SETTING.getKey(), newIdSeed).build(); final long newIdSeed = NodeEnvironment.NODE_ID_SEED_SETTING.get(node.settings()) + 1;
Settings finalSettings = Settings.builder()
.put(node.originalSettings())
.put(newSettings)
.put(NodeEnvironment.NODE_ID_SEED_SETTING.getKey(), newIdSeed)
.build();
if (DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.exists(finalSettings) == false) { if (DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.exists(finalSettings) == false) {
throw new IllegalStateException(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey() + throw new IllegalStateException(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey() +
" is not configured after restart of [" + name + "]"); " is not configured after restart of [" + name + "]");
@ -1042,10 +1067,12 @@ public final class InternalTestCluster extends TestCluster {
if (nodes.size() > 0 && autoManageMinMasterNodes) { if (nodes.size() > 0 && autoManageMinMasterNodes) {
updateMinMasterNodes(getMasterNodesCount()); updateMinMasterNodes(getMasterNodesCount());
} }
logger.debug("Cluster hasn't changed - moving out - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize); logger.debug("Cluster hasn't changed - moving out - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]",
nodes.keySet(), nextNodeId.get(), newSize);
return; return;
} }
logger.debug("Cluster is NOT consistent - restarting shared nodes - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize); logger.debug("Cluster is NOT consistent - restarting shared nodes - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]",
nodes.keySet(), nextNodeId.get(), newSize);
// trash all nodes with id >= sharedNodesSeeds.length - they are non shared // trash all nodes with id >= sharedNodesSeeds.length - they are non shared
final List<NodeAndClient> toClose = new ArrayList<>(); final List<NodeAndClient> toClose = new ArrayList<>();
@ -1104,7 +1131,8 @@ public final class InternalTestCluster extends TestCluster {
if (newSize > 0) { if (newSize > 0) {
validateClusterFormed(); validateClusterFormed();
} }
logger.debug("Cluster is consistent again - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize); logger.debug("Cluster is consistent again - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]",
nodes.keySet(), nextNodeId.get(), newSize);
} }
/** ensure a cluster is formed with all published nodes. */ /** ensure a cluster is formed with all published nodes. */
@ -1800,7 +1828,11 @@ public final class InternalTestCluster extends TestCluster {
} }
public synchronized List<String> startMasterOnlyNodes(int numNodes, Settings settings) { public synchronized List<String> startMasterOnlyNodes(int numNodes, Settings settings) {
Settings settings1 = Settings.builder().put(settings).put(Node.NODE_MASTER_SETTING.getKey(), true).put(Node.NODE_DATA_SETTING.getKey(), false).build(); Settings settings1 = Settings.builder()
.put(settings)
.put(Node.NODE_MASTER_SETTING.getKey(), true)
.put(Node.NODE_DATA_SETTING.getKey(), false)
.build();
return startNodes(numNodes, settings1); return startNodes(numNodes, settings1);
} }
@ -1809,7 +1841,11 @@ public final class InternalTestCluster extends TestCluster {
} }
public synchronized List<String> startDataOnlyNodes(int numNodes, Settings settings) { public synchronized List<String> startDataOnlyNodes(int numNodes, Settings settings) {
Settings settings1 = Settings.builder().put(settings).put(Node.NODE_MASTER_SETTING.getKey(), false).put(Node.NODE_DATA_SETTING.getKey(), true).build(); Settings settings1 = Settings.builder()
.put(settings)
.put(Node.NODE_MASTER_SETTING.getKey(), false)
.put(Node.NODE_DATA_SETTING.getKey(), true)
.build();
return startNodes(numNodes, settings1); return startNodes(numNodes, settings1);
} }
@ -1850,7 +1886,11 @@ public final class InternalTestCluster extends TestCluster {
} }
public synchronized String startMasterOnlyNode(Settings settings) { public synchronized String startMasterOnlyNode(Settings settings) {
Settings settings1 = Settings.builder().put(settings).put(Node.NODE_MASTER_SETTING.getKey(), true).put(Node.NODE_DATA_SETTING.getKey(), false).build(); Settings settings1 = Settings.builder()
.put(settings)
.put(Node.NODE_MASTER_SETTING.getKey(), true)
.put(Node.NODE_DATA_SETTING.getKey(), false)
.build();
return startNode(settings1); return startNode(settings1);
} }
@ -1858,7 +1898,11 @@ public final class InternalTestCluster extends TestCluster {
return startDataOnlyNode(Settings.EMPTY); return startDataOnlyNode(Settings.EMPTY);
} }
public synchronized String startDataOnlyNode(Settings settings) { public synchronized String startDataOnlyNode(Settings settings) {
Settings settings1 = Settings.builder().put(settings).put(Node.NODE_MASTER_SETTING.getKey(), false).put(Node.NODE_DATA_SETTING.getKey(), true).build(); Settings settings1 = Settings.builder()
.put(settings)
.put(Node.NODE_MASTER_SETTING.getKey(), false)
.put(Node.NODE_DATA_SETTING.getKey(), true)
.build();
return startNode(settings1); return startNode(settings1);
} }
@ -1933,7 +1977,8 @@ public final class InternalTestCluster extends TestCluster {
return filterNodes(nodes, new DataOrMasterNodePredicate()); return filterNodes(nodes, new DataOrMasterNodePredicate());
} }
private synchronized Collection<NodeAndClient> filterNodes(Map<String, InternalTestCluster.NodeAndClient> map, Predicate<NodeAndClient> predicate) { private synchronized Collection<NodeAndClient> filterNodes(Map<String, InternalTestCluster.NodeAndClient> map,
Predicate<NodeAndClient> predicate) {
return map return map
.values() .values()
.stream() .stream()
@ -1999,11 +2044,14 @@ public final class InternalTestCluster extends TestCluster {
ClusterService clusterService = getInstanceFromNode(ClusterService.class, node); ClusterService clusterService = getInstanceFromNode(ClusterService.class, node);
IndexService indexService = indicesService.indexService(index); IndexService indexService = indicesService.indexService(index);
if (indexService != null) { if (indexService != null) {
assertThat(indexService.getIndexSettings().getSettings().getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1), greaterThan(shard)); assertThat(indexService.getIndexSettings().getSettings().getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1),
greaterThan(shard));
OperationRouting operationRouting = clusterService.operationRouting(); OperationRouting operationRouting = clusterService.operationRouting();
while (true) { while (true) {
String routing = RandomStrings.randomAsciiOfLength(random, 10); String routing = RandomStrings.randomAsciiOfLength(random, 10);
final int targetShard = operationRouting.indexShards(clusterService.state(), index.getName(), null, routing).shardId().getId(); final int targetShard = operationRouting
.indexShards(clusterService.state(), index.getName(), null, routing)
.shardId().getId();
if (shard == targetShard) { if (shard == targetShard) {
return routing; return routing;
} }
@ -2111,7 +2159,8 @@ public final class InternalTestCluster extends TestCluster {
// network request, because a network request can increment one // network request, because a network request can increment one
// of the breakers // of the breakers
for (NodeAndClient nodeAndClient : nodes.values()) { for (NodeAndClient nodeAndClient : nodes.values()) {
final IndicesFieldDataCache fdCache = getInstanceFromNode(IndicesService.class, nodeAndClient.node).getIndicesFieldDataCache(); final IndicesFieldDataCache fdCache =
getInstanceFromNode(IndicesService.class, nodeAndClient.node).getIndicesFieldDataCache();
// Clean up the cache, ensuring that entries' listeners have been called // Clean up the cache, ensuring that entries' listeners have been called
fdCache.getCache().refresh(); fdCache.getCache().refresh();
@ -2144,9 +2193,12 @@ public final class InternalTestCluster extends TestCluster {
CommonStatsFlags flags = new CommonStatsFlags(Flag.FieldData, Flag.QueryCache, Flag.Segments); CommonStatsFlags flags = new CommonStatsFlags(Flag.FieldData, Flag.QueryCache, Flag.Segments);
NodeStats stats = nodeService.stats(flags, NodeStats stats = nodeService.stats(flags,
false, false, false, false, false, false, false, false, false, false, false, false); false, false, false, false, false, false, false, false, false, false, false, false);
assertThat("Fielddata size must be 0 on node: " + stats.getNode(), stats.getIndices().getFieldData().getMemorySizeInBytes(), equalTo(0L)); assertThat("Fielddata size must be 0 on node: " + stats.getNode(),
assertThat("Query cache size must be 0 on node: " + stats.getNode(), stats.getIndices().getQueryCache().getMemorySizeInBytes(), equalTo(0L)); stats.getIndices().getFieldData().getMemorySizeInBytes(), equalTo(0L));
assertThat("FixedBitSet cache size must be 0 on node: " + stats.getNode(), stats.getIndices().getSegments().getBitsetMemoryInBytes(), equalTo(0L)); assertThat("Query cache size must be 0 on node: " + stats.getNode(),
stats.getIndices().getQueryCache().getMemorySizeInBytes(), equalTo(0L));
assertThat("FixedBitSet cache size must be 0 on node: " + stats.getNode(),
stats.getIndices().getSegments().getBitsetMemoryInBytes(), equalTo(0L));
} }
} }
} }

View File

@ -41,8 +41,9 @@ import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* This is a testing plugin that registers a generic {@link org.elasticsearch.test.MockIndexEventListener.TestEventListener} as a node level service as well as a listener * This is a testing plugin that registers a generic
* on every index. Tests can access it like this: * {@link MockIndexEventListener.TestEventListener} as a node level service
* as well as a listener on every index. Tests can access it like this:
* <pre> * <pre>
* TestEventListener listener = internalCluster().getInstance(MockIndexEventListener.TestEventListener.class, node1); * TestEventListener listener = internalCluster().getInstance(MockIndexEventListener.TestEventListener.class, node1);
* listener.setNewDelegate(new IndexEventListener() { * listener.setNewDelegate(new IndexEventListener() {
@ -109,7 +110,8 @@ public final class MockIndexEventListener {
} }
@Override @Override
public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState, IndexShardState currentState, @Nullable String reason) { public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState,
IndexShardState currentState, @Nullable String reason) {
delegate.indexShardStateChanged(indexShard, previousState, currentState, reason); delegate.indexShardStateChanged(indexShard, previousState, currentState, reason);
} }

View File

@ -131,7 +131,8 @@ public class ClusterDiscoveryConfiguration extends NodeConfigurationSource {
String[] unicastHosts = new String[unicastHostOrdinals.length]; String[] unicastHosts = new String[unicastHostOrdinals.length];
if (nodeOrdinal >= unicastHostPorts.length) { if (nodeOrdinal >= unicastHostPorts.length) {
throw new ElasticsearchException("nodeOrdinal [" + nodeOrdinal + "] is greater than the number unicast ports [" + unicastHostPorts.length + "]"); throw new ElasticsearchException("nodeOrdinal [" + nodeOrdinal + "] is greater than the number unicast ports ["
+ unicastHostPorts.length + "]");
} else { } else {
// we need to pin the node port & host so we'd know where to point things // we need to pin the node port & host so we'd know where to point things
builder.put(TcpTransport.PORT.getKey(), unicastHostPorts[nodeOrdinal]); builder.put(TcpTransport.PORT.getKey(), unicastHostPorts[nodeOrdinal]);
@ -174,7 +175,8 @@ public class ClusterDiscoveryConfiguration extends NodeConfigurationSource {
} }
if (!foundPortInRange) { if (!foundPortInRange) {
throw new ElasticsearchException("could not find enough open ports in range [" + basePort + "-" + maxPort + "]. required [" + unicastHostPorts.length + "] ports"); throw new ElasticsearchException("could not find enough open ports in range [" + basePort + "-" + maxPort
+ "]. required [" + unicastHostPorts.length + "] ports");
} }
} }
return unicastHostPorts; return unicastHostPorts;

View File

@ -97,7 +97,8 @@ public class IntermittentLongGCDisruption extends LongGCDisruption {
TimeValue duration = new TimeValue(delayDurationMin + random.nextInt((int) (delayDurationMax - delayDurationMin))); TimeValue duration = new TimeValue(delayDurationMin + random.nextInt((int) (delayDurationMax - delayDurationMin)));
simulateLongGC(duration); simulateLongGC(duration);
duration = new TimeValue(intervalBetweenDelaysMin + random.nextInt((int) (intervalBetweenDelaysMax - intervalBetweenDelaysMin))); duration = new TimeValue(intervalBetweenDelaysMin
+ random.nextInt((int) (intervalBetweenDelaysMax - intervalBetweenDelaysMin)));
if (disrupting) { if (disrupting) {
Thread.sleep(duration.millis()); Thread.sleep(duration.millis());
} }

View File

@ -150,7 +150,8 @@ public class SlowClusterStateProcessing extends SingleNodeDisruption {
continue; continue;
} }
if (intervalBetweenDelaysMax > 0) { if (intervalBetweenDelaysMax > 0) {
duration = new TimeValue(intervalBetweenDelaysMin + random.nextInt((int) (intervalBetweenDelaysMax - intervalBetweenDelaysMin))); duration = new TimeValue(intervalBetweenDelaysMin
+ random.nextInt((int) (intervalBetweenDelaysMax - intervalBetweenDelaysMin)));
if (disrupting && disruptedNode != null) { if (disrupting && disruptedNode != null) {
Thread.sleep(duration.millis()); Thread.sleep(duration.millis());
} }

View File

@ -48,7 +48,8 @@ class AssertingSearcher extends Engine.Searcher {
this.logger = logger; this.logger = logger;
this.shardId = shardId; this.shardId = shardId;
initialRefCount = wrappedSearcher.reader().getRefCount(); initialRefCount = wrappedSearcher.reader().getRefCount();
assert initialRefCount > 0 : "IndexReader#getRefCount() was [" + initialRefCount + "] expected a value > [0] - reader is already closed"; assert initialRefCount > 0 :
"IndexReader#getRefCount() was [" + initialRefCount + "] expected a value > [0] - reader is already closed";
} }
@Override @Override
@ -62,9 +63,13 @@ class AssertingSearcher extends Engine.Searcher {
if (closed.compareAndSet(false, true)) { if (closed.compareAndSet(false, true)) {
firstReleaseStack = new RuntimeException(); firstReleaseStack = new RuntimeException();
final int refCount = wrappedSearcher.reader().getRefCount(); final int refCount = wrappedSearcher.reader().getRefCount();
// this assert seems to be paranoid but given LUCENE-5362 we better add some assertions here to make sure we catch any potential /*
// problems. * this assert seems to be paranoid but given LUCENE-5362 we
assert refCount > 0 : "IndexReader#getRefCount() was [" + refCount + "] expected a value > [0] - reader is already closed. Initial refCount was: [" + initialRefCount + "]"; * better add some assertions here to make sure we catch any
* potential problems.
*/
assert refCount > 0 : "IndexReader#getRefCount() was [" + refCount + "] expected a value > [0] - reader is already "
+ " closed. Initial refCount was: [" + initialRefCount + "]";
try { try {
wrappedSearcher.close(); wrappedSearcher.close();
} catch (RuntimeException ex) { } catch (RuntimeException ex) {

View File

@ -123,7 +123,12 @@ public final class MockEngineSupport {
* the first call and treats subsequent calls as if the engine passed is already closed. * the first call and treats subsequent calls as if the engine passed is already closed.
*/ */
public CloseAction flushOrClose(CloseAction originalAction) throws IOException { public CloseAction flushOrClose(CloseAction originalAction) throws IOException {
if (closing.compareAndSet(false, true)) { // only do the random thing if we are the first call to this since super.flushOnClose() calls #close() again and then we might end up with a stackoverflow. /*
* only do the random thing if we are the first call to this since
* super.flushOnClose() calls #close() again and then we might end
* up with a stackoverflow.
*/
if (closing.compareAndSet(false, true)) {
if (mockContext.random.nextBoolean()) { if (mockContext.random.nextBoolean()) {
return CloseAction.FLUSH_AND_CLOSE; return CloseAction.FLUSH_AND_CLOSE;
} else { } else {
@ -186,9 +191,14 @@ public final class MockEngineSupport {
public Engine.Searcher wrapSearcher(String source, Engine.Searcher engineSearcher) { public Engine.Searcher wrapSearcher(String source, Engine.Searcher engineSearcher) {
final AssertingIndexSearcher assertingIndexSearcher = newSearcher(engineSearcher); final AssertingIndexSearcher assertingIndexSearcher = newSearcher(engineSearcher);
assertingIndexSearcher.setSimilarity(engineSearcher.searcher().getSimilarity()); assertingIndexSearcher.setSimilarity(engineSearcher.searcher().getSimilarity());
// pass the original searcher to the super.newSearcher() method to make sure this is the searcher that will /*
// be released later on. If we wrap an index reader here must not pass the wrapped version to the manager * pass the original searcher to the super.newSearcher() method to
// on release otherwise the reader will be closed too early. - good news, stuff will fail all over the place if we don't get this right here * make sure this is the searcher that will be released later on.
* If we wrap an index reader here must not pass the wrapped version
* to the manager on release otherwise the reader will be closed too
* early. - good news, stuff will fail all over the place if we don't
* get this right here
*/
AssertingSearcher assertingSearcher = new AssertingSearcher(assertingIndexSearcher, engineSearcher, shardId, logger) { AssertingSearcher assertingSearcher = new AssertingSearcher(assertingIndexSearcher, engineSearcher, shardId, logger) {
@Override @Override
public void close() { public void close() {

View File

@ -58,7 +58,6 @@ import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.test.NotEqualMessageBuilder; import org.elasticsearch.test.NotEqualMessageBuilder;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -137,10 +136,13 @@ public class ElasticsearchAssertions {
* *
* */ * */
public static void assertBlocked(BroadcastResponse replicatedBroadcastResponse) { public static void assertBlocked(BroadcastResponse replicatedBroadcastResponse) {
assertThat("all shard requests should have failed", replicatedBroadcastResponse.getFailedShards(), Matchers.equalTo(replicatedBroadcastResponse.getTotalShards())); assertThat("all shard requests should have failed",
replicatedBroadcastResponse.getFailedShards(), equalTo(replicatedBroadcastResponse.getTotalShards()));
for (DefaultShardOperationFailedException exception : replicatedBroadcastResponse.getShardFailures()) { for (DefaultShardOperationFailedException exception : replicatedBroadcastResponse.getShardFailures()) {
ClusterBlockException clusterBlockException = (ClusterBlockException) ExceptionsHelper.unwrap(exception.getCause(), ClusterBlockException.class); ClusterBlockException clusterBlockException =
assertNotNull("expected the cause of failure to be a ClusterBlockException but got " + exception.getCause().getMessage(), clusterBlockException); (ClusterBlockException) ExceptionsHelper.unwrap(exception.getCause(), ClusterBlockException.class);
assertNotNull("expected the cause of failure to be a ClusterBlockException but got " + exception.getCause().getMessage(),
clusterBlockException);
assertThat(clusterBlockException.blocks().size(), greaterThan(0)); assertThat(clusterBlockException.blocks().size(), greaterThan(0));
assertThat(clusterBlockException.status(), CoreMatchers.equalTo(RestStatus.FORBIDDEN)); assertThat(clusterBlockException.status(), CoreMatchers.equalTo(RestStatus.FORBIDDEN));
} }
@ -204,8 +206,9 @@ public class ElasticsearchAssertions {
Set<String> idsSet = new HashSet<>(Arrays.asList(ids)); Set<String> idsSet = new HashSet<>(Arrays.asList(ids));
for (SearchHit hit : searchResponse.getHits()) { for (SearchHit hit : searchResponse.getHits()) {
assertThat("id [" + hit.getId() + "] was found in search results but wasn't expected (type [" + hit.getType() + "], index [" + hit.getIndex() + "])" assertThat(
+ shardStatus, idsSet.remove(hit.getId()), "id [" + hit.getId() + "] was found in search results but wasn't expected (type [" + hit.getType()
+ "], index [" + hit.getIndex() + "])" + shardStatus, idsSet.remove(hit.getId()),
equalTo(true)); equalTo(true));
} }
assertThat("Some expected ids were not found in search results: " + Arrays.toString(idsSet.toArray(new String[idsSet.size()])) + "." assertThat("Some expected ids were not found in search results: " + Arrays.toString(idsSet.toArray(new String[idsSet.size()])) + "."
@ -233,12 +236,14 @@ public class ElasticsearchAssertions {
public static void assertHitCount(SearchResponse countResponse, long expectedHitCount) { public static void assertHitCount(SearchResponse countResponse, long expectedHitCount) {
if (countResponse.getHits().getTotalHits() != expectedHitCount) { if (countResponse.getHits().getTotalHits() != expectedHitCount) {
fail("Count is " + countResponse.getHits().getTotalHits() + " but " + expectedHitCount + " was expected. " + formatShardStatus(countResponse)); fail("Count is " + countResponse.getHits().getTotalHits() + " but " + expectedHitCount
+ " was expected. " + formatShardStatus(countResponse));
} }
} }
public static void assertExists(GetResponse response) { public static void assertExists(GetResponse response) {
String message = String.format(Locale.ROOT, "Expected %s/%s/%s to exist, but does not", response.getIndex(), response.getType(), response.getId()); String message = String.format(Locale.ROOT, "Expected %s/%s/%s to exist, but does not",
response.getIndex(), response.getType(), response.getId());
assertThat(message, response.isExists(), is(true)); assertThat(message, response.isExists(), is(true));
} }
@ -326,7 +331,8 @@ public class ElasticsearchAssertions {
assertHighlight(resp, hit, field, fragment, greaterThan(fragment), matcher); assertHighlight(resp, hit, field, fragment, greaterThan(fragment), matcher);
} }
public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, int totalFragments, Matcher<String> matcher) { public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment,
int totalFragments, Matcher<String> matcher) {
assertHighlight(resp, hit, field, fragment, equalTo(totalFragments), matcher); assertHighlight(resp, hit, field, fragment, equalTo(totalFragments), matcher);
} }
@ -338,13 +344,15 @@ public class ElasticsearchAssertions {
assertHighlight(hit, field, fragment, equalTo(totalFragments), matcher); assertHighlight(hit, field, fragment, equalTo(totalFragments), matcher);
} }
private static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, Matcher<Integer> fragmentsMatcher, Matcher<String> matcher) { private static void assertHighlight(SearchResponse resp, int hit, String field, int fragment,
Matcher<Integer> fragmentsMatcher, Matcher<String> matcher) {
assertNoFailures(resp); assertNoFailures(resp);
assertThat("not enough hits", resp.getHits().getHits().length, greaterThan(hit)); assertThat("not enough hits", resp.getHits().getHits().length, greaterThan(hit));
assertHighlight(resp.getHits().getHits()[hit], field, fragment, fragmentsMatcher, matcher); assertHighlight(resp.getHits().getHits()[hit], field, fragment, fragmentsMatcher, matcher);
} }
private static void assertHighlight(SearchHit hit, String field, int fragment, Matcher<Integer> fragmentsMatcher, Matcher<String> matcher) { private static void assertHighlight(SearchHit hit, String field, int fragment,
Matcher<Integer> fragmentsMatcher, Matcher<String> matcher) {
assertThat(hit.getHighlightFields(), hasKey(field)); assertThat(hit.getHighlightFields(), hasKey(field));
assertThat(hit.getHighlightFields().get(field).fragments().length, fragmentsMatcher); assertThat(hit.getHighlightFields().get(field).fragments().length, fragmentsMatcher);
assertThat(hit.getHighlightFields().get(field).fragments()[fragment].string(), matcher); assertThat(hit.getHighlightFields().get(field).fragments()[fragment].string(), matcher);
@ -488,7 +496,7 @@ public class ElasticsearchAssertions {
} }
/** /**
* Run the request from a given builder and check that it throws an exception of the right type, with a given {@link org.elasticsearch.rest.RestStatus} * Run the request from a given builder and check that it throws an exception of the right type, with a given {@link RestStatus}
*/ */
public static <E extends Throwable> void assertThrows(ActionRequestBuilder<?, ?> builder, Class<E> exceptionClass, RestStatus status) { public static <E extends Throwable> void assertThrows(ActionRequestBuilder<?, ?> builder, Class<E> exceptionClass, RestStatus status) {
assertThrows(builder.execute(), exceptionClass, status); assertThrows(builder.execute(), exceptionClass, status);
@ -511,7 +519,7 @@ public class ElasticsearchAssertions {
} }
/** /**
* Run future.actionGet() and check that it throws an exception of the right type, with a given {@link org.elasticsearch.rest.RestStatus} * Run future.actionGet() and check that it throws an exception of the right type, with a given {@link RestStatus}
*/ */
public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass, RestStatus status) { public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass, RestStatus status) {
assertThrows(future, exceptionClass, status, null); assertThrows(future, exceptionClass, status, null);
@ -533,7 +541,8 @@ public class ElasticsearchAssertions {
* @param status {@link org.elasticsearch.rest.RestStatus} to check for. Can be null to disable the check * @param status {@link org.elasticsearch.rest.RestStatus} to check for. Can be null to disable the check
* @param extraInfo extra information to add to the failure message. Can be null. * @param extraInfo extra information to add to the failure message. Can be null.
*/ */
public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass, @Nullable RestStatus status, @Nullable String extraInfo) { public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass,
@Nullable RestStatus status, @Nullable String extraInfo) {
boolean fail = false; boolean fail = false;
extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": "; extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": ";
extraInfo += "expected a " + exceptionClass + " exception to be thrown"; extraInfo += "expected a " + exceptionClass + " exception to be thrown";

View File

@ -63,11 +63,11 @@ public class MockFSDirectoryService extends FsDirectoryService {
public static final Setting<Double> RANDOM_IO_EXCEPTION_RATE_SETTING = public static final Setting<Double> RANDOM_IO_EXCEPTION_RATE_SETTING =
Setting.doubleSetting("index.store.mock.random.io_exception_rate", 0.0d, 0.0d, Property.IndexScope, Property.NodeScope); Setting.doubleSetting("index.store.mock.random.io_exception_rate", 0.0d, 0.0d, Property.IndexScope, Property.NodeScope);
public static final Setting<Boolean> RANDOM_PREVENT_DOUBLE_WRITE_SETTING = public static final Setting<Boolean> RANDOM_PREVENT_DOUBLE_WRITE_SETTING =
Setting.boolSetting("index.store.mock.random.prevent_double_write", true, Property.IndexScope, Property.NodeScope);// true is default in MDW Setting.boolSetting("index.store.mock.random.prevent_double_write", true, Property.IndexScope, Property.NodeScope);
public static final Setting<Boolean> RANDOM_NO_DELETE_OPEN_FILE_SETTING = public static final Setting<Boolean> RANDOM_NO_DELETE_OPEN_FILE_SETTING =
Setting.boolSetting("index.store.mock.random.no_delete_open_file", true, Property.IndexScope, Property.NodeScope);// true is default in MDW Setting.boolSetting("index.store.mock.random.no_delete_open_file", true, Property.IndexScope, Property.NodeScope);
public static final Setting<Boolean> CRASH_INDEX_SETTING = public static final Setting<Boolean> CRASH_INDEX_SETTING =
Setting.boolSetting("index.store.mock.random.crash_index", true, Property.IndexScope, Property.NodeScope);// true is default in MDW Setting.boolSetting("index.store.mock.random.crash_index", true, Property.IndexScope, Property.NodeScope);
private final FsDirectoryService delegateService; private final FsDirectoryService delegateService;
private final Random random; private final Random random;
@ -88,7 +88,8 @@ public class MockFSDirectoryService extends FsDirectoryService {
randomIOExceptionRate = RANDOM_IO_EXCEPTION_RATE_SETTING.get(indexSettings); randomIOExceptionRate = RANDOM_IO_EXCEPTION_RATE_SETTING.get(indexSettings);
randomIOExceptionRateOnOpen = RANDOM_IO_EXCEPTION_RATE_ON_OPEN_SETTING.get(indexSettings); randomIOExceptionRateOnOpen = RANDOM_IO_EXCEPTION_RATE_ON_OPEN_SETTING.get(indexSettings);
preventDoubleWrite = RANDOM_PREVENT_DOUBLE_WRITE_SETTING.get(indexSettings); preventDoubleWrite = RANDOM_PREVENT_DOUBLE_WRITE_SETTING.get(indexSettings);
noDeleteOpenFile = RANDOM_NO_DELETE_OPEN_FILE_SETTING.exists(indexSettings) ? RANDOM_NO_DELETE_OPEN_FILE_SETTING.get(indexSettings) : random.nextBoolean(); noDeleteOpenFile = RANDOM_NO_DELETE_OPEN_FILE_SETTING.exists(indexSettings) ?
RANDOM_NO_DELETE_OPEN_FILE_SETTING.get(indexSettings) : random.nextBoolean();
random.nextInt(shardId.getId() + 1); // some randomness per shard random.nextInt(shardId.getId() + 1); // some randomness per shard
throttle = MockDirectoryWrapper.Throttling.NEVER; throttle = MockDirectoryWrapper.Throttling.NEVER;
crashIndex = CRASH_INDEX_SETTING.get(indexSettings); crashIndex = CRASH_INDEX_SETTING.get(indexSettings);

View File

@ -108,7 +108,8 @@ public class MockFSIndexStore extends IndexStore {
} }
@Override @Override
public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState, IndexShardState currentState, @Nullable String reason) { public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState,
IndexShardState currentState, @Nullable String reason) {
if (currentState == IndexShardState.CLOSED && validCheckIndexStates.contains(previousState)) { if (currentState == IndexShardState.CLOSED && validCheckIndexStates.contains(previousState)) {
shardSet.put(indexShard, Boolean.TRUE); shardSet.put(indexShard, Boolean.TRUE);
} }