Package org.elasticsearch.test
Class InternalTestCluster
java.lang.Object
org.elasticsearch.test.TestCluster
org.elasticsearch.test.InternalTestCluster
- All Implemented Interfaces:
Closeable
,AutoCloseable
InternalTestCluster manages a set of JVM private nodes and allows convenient access to them.
The cluster supports randomized configuration such that nodes started in the cluster will
automatically load asserting services tracking resources like file handles or open searchers.
The Cluster is bound to a test lifecycle where tests must call beforeTest(java.util.Random)
and
afterTest()
to initialize and reset the cluster in order to be more reproducible. The term "more" relates
to the async nature of Elasticsearch in combination with randomized testing. Once Threads and asynchronous calls
are involved reproducibility is very limited. This class should only be used through ESIntegTestCase
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
An abstract class that is called duringrollingRestart(InternalTestCluster.RestartCallback)
and / orfullRestart(InternalTestCluster.RestartCallback)
to execute actions at certain stages of the restart. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final InternalTestCluster.RestartCallback
Fields inherited from class org.elasticsearch.test.TestCluster
random
-
Constructor Summary
ConstructorsConstructorDescriptionInternalTestCluster
(long clusterSeed, Path baseDir, boolean randomlyAddDedicatedMasters, boolean autoManageMasterNodes, int minNumDataNodes, int maxNumDataNodes, String clusterName, NodeConfigurationSource nodeConfigurationSource, int numClientNodes, String nodePrefix, Collection<Class<? extends org.elasticsearch.plugins.Plugin>> mockPlugins, Function<org.elasticsearch.client.internal.Client, org.elasticsearch.client.internal.Client> clientWrapper) InternalTestCluster
(long clusterSeed, Path baseDir, boolean randomlyAddDedicatedMasters, boolean autoManageMasterNodes, int minNumDataNodes, int maxNumDataNodes, String clusterName, NodeConfigurationSource nodeConfigurationSource, int numClientNodes, String nodePrefix, Collection<Class<? extends org.elasticsearch.plugins.Plugin>> mockPlugins, Function<org.elasticsearch.client.internal.Client, org.elasticsearch.client.internal.Client> clientWrapper, boolean forbidPrivateIndexSettings, boolean forceSingleDataPath) -
Method Summary
Modifier and TypeMethodDescriptionvoid
This method should be executed during tear down, after each test (but after assertAfterTest)void
This method checks all the things that need to be checked after each testvoid
Asserts that the document history in Lucene index is consistent with Translog's on every index shard of the cluster.void
void
void
Asserts that all shards with the same shardId should have document Ids.void
void
Assertions that should run before the cluster is wiped should be called in this methodvoid
beforeTest
(Random random) This method should be executed before each test to reset the cluster to its initial state.void
void
clearDisruptionScheme
(boolean ensureHealthyCluster) org.elasticsearch.client.internal.Client
client()
Returns a client connected to any node in the clusterorg.elasticsearch.client.internal.Client
Returns a node client to a given node.void
close()
Closes the current clustervoid
closeNonSharedNodes
(boolean wipeData) static String
clusterName
(String prefix, long clusterSeed) org.elasticsearch.cluster.service.ClusterService
Returns a reference to a random node'sClusterService
org.elasticsearch.cluster.service.ClusterService
clusterService
(String node) Returns a reference to a node'sClusterService
.org.elasticsearch.client.internal.Client
Returns a client to a coordinating only nodeorg.elasticsearch.client.internal.Client
Returns a node client to a data node in the cluster.org.elasticsearch.common.settings.Settings
dataPathSettings
(String node) void
ensureAtLeastNumDataNodes
(int n) Ensures that at leastn
data nodes are present in the cluster.void
ensureAtMostNumDataNodes
(int n) Ensures that at mostn
are up and running.void
Ensures that any breaker statistics are reset to 0.void
Restarts all nodes in the cluster.void
Restarts all nodes in the cluster.<T> T
getAnyMasterNodeInstance
(Class<T> clazz) Return the instance of the given class >T< from any of the master-eligible nodes (not necessarily the elected master).Iterable<org.elasticsearch.client.internal.Client>
Returns anIterable
over all clients in this test clusterReturns the cluster name<T> T
getCurrentMasterNodeInstance
(Class<T> clazz) Return the instance of the given class >T< from the elected master node.<T> T
getDataNodeInstance
(Class<T> clazz) <T> Iterable<T>
getDataNodeInstances
(Class<T> clazz) Returns an Iterable to all instances for the given class >T< across all data nodes in the cluster.<T> Iterable<T>
getDataOrMasterNodeInstances
(Class<T> clazz) Returns an Iterable to all instances for the given class >T< across all data and master nodes in the cluster.org.elasticsearch.common.settings.Settings
<T> T
getInstance
(Class<T> clazz) Returns a reference to a random nodes instances of the given class >T<<T> T
getInstance
(Class<T> clazz, String node) Returns a reference to the given nodes instances of the given class >T<<T> Iterable<T>
getInstances
(Class<T> clazz) Returns an Iterable to all instances for the given class >T< across all nodes in the cluster.Returns the name of the current master node in the cluster.getMasterName
(String viaNode) Returns the name of the current master node in the cluster and executes the request via the node specified in the viaNode parameter.org.elasticsearch.common.io.stream.NamedWriteableRegistry
Returns this clustersNamedWriteableRegistry
this is needed to deserialize binary content from this cluster that might include custom named writeablesString[]
Collection<Class<? extends org.elasticsearch.plugins.Plugin>>
Returns the http addresses of the nodes within the cluster.org.elasticsearch.client.internal.Client
Returns a node client to the current master node.static Predicate<org.elasticsearch.common.settings.Settings>
nameFilter
(String... nodeNames) Returns a predicate that only accepts settings of nodes with one of the given names.nodesInclude
(String index) Returns a set of nodes that have at least one shard of the given index.org.elasticsearch.client.internal.Client
Returns a node client to random node but not the master.int
Returns the number of data and master eligible nodes in the cluster.int
Returns the number of data nodes in the cluster.int
void
restartNode
(String nodeName) Restarts a node.void
restartNode
(String nodeName, InternalTestCluster.RestartCallback callback) Restarts a node and calls the callback during restart.void
Restarts a random data node in the clustervoid
Restarts a random data node in the cluster and calls the callback during restart.void
Restarts all nodes in a rolling restart fashion ie.void
setBootstrapMasterNodeIndex
(int bootstrapMasterNodeIndex) SetsbootstrapMasterNodeIndex
to the given value, seebootstrapMasterNodeWithSpecifiedIndex(List)
for the description of how this field is used.void
int
size()
Returns the number of nodes in the cluster.org.elasticsearch.client.internal.Client
Returns a "smart" node client to a random node in the clusterstartCoordinatingOnlyNode
(org.elasticsearch.common.settings.Settings settings) startDataOnlyNode
(org.elasticsearch.common.settings.Settings settings) startDataOnlyNodes
(int numNodes) startDataOnlyNodes
(int numNodes, org.elasticsearch.common.settings.Settings settings) startMasterOnlyNode
(org.elasticsearch.common.settings.Settings settings) startMasterOnlyNodes
(int numNodes) startMasterOnlyNodes
(int numNodes, org.elasticsearch.common.settings.Settings settings) Starts a node with default settings and returns its name.startNode
(org.elasticsearch.common.settings.Settings settings) Starts a node with the given settings and returns its name.startNode
(org.elasticsearch.common.settings.Settings.Builder settings) Starts a node with the given settings builder and returns its name.startNodes
(int numOfNodes) Starts multiple nodes with default settings and returns their namesstartNodes
(int numOfNodes, org.elasticsearch.common.settings.Settings settings) Starts multiple nodes with the given settings and returns their namesstartNodes
(org.elasticsearch.common.settings.Settings... extraSettings) Starts multiple nodes with the given settings and returns their namesvoid
Stops the current master node forcefullyboolean
Stops a specific node in the cluster.boolean
Stops a random data node in the cluster.void
stopRandomNode
(Predicate<org.elasticsearch.common.settings.Settings> filter) Stops a random node in the cluster that applies to the given filter.void
Stops any of the current nodes but not the master node.void
ensure a cluster is formed with all published nodes.void
Methods inherited from class org.elasticsearch.test.TestCluster
seed, wipe, wipeAllTemplates, wipeIndices, wipeRepositories, wipeTemplates
-
Field Details
-
DEFAULT_LOW_NUM_MASTER_NODES
public static final int DEFAULT_LOW_NUM_MASTER_NODES- See Also:
-
DEFAULT_HIGH_NUM_MASTER_NODES
public static final int DEFAULT_HIGH_NUM_MASTER_NODES- See Also:
-
EMPTY_CALLBACK
-
-
Constructor Details
-
InternalTestCluster
public InternalTestCluster(long clusterSeed, Path baseDir, boolean randomlyAddDedicatedMasters, boolean autoManageMasterNodes, int minNumDataNodes, int maxNumDataNodes, String clusterName, NodeConfigurationSource nodeConfigurationSource, int numClientNodes, String nodePrefix, Collection<Class<? extends org.elasticsearch.plugins.Plugin>> mockPlugins, Function<org.elasticsearch.client.internal.Client, org.elasticsearch.client.internal.Client> clientWrapper) -
InternalTestCluster
public InternalTestCluster(long clusterSeed, Path baseDir, boolean randomlyAddDedicatedMasters, boolean autoManageMasterNodes, int minNumDataNodes, int maxNumDataNodes, String clusterName, NodeConfigurationSource nodeConfigurationSource, int numClientNodes, String nodePrefix, Collection<Class<? extends org.elasticsearch.plugins.Plugin>> mockPlugins, Function<org.elasticsearch.client.internal.Client, org.elasticsearch.client.internal.Client> clientWrapper, boolean forbidPrivateIndexSettings, boolean forceSingleDataPath)
-
-
Method Details
-
setBootstrapMasterNodeIndex
public void setBootstrapMasterNodeIndex(int bootstrapMasterNodeIndex) SetsbootstrapMasterNodeIndex
to the given value, seebootstrapMasterNodeWithSpecifiedIndex(List)
for the description of how this field is used. It's only possible to changebootstrapMasterNodeIndex
value if autoManageMasterNodes is false. -
getClusterName
Description copied from class:TestCluster
Returns the cluster name- Specified by:
getClusterName
in classTestCluster
-
getNodeNames
-
getPlugins
-
clusterName
-
ensureAtLeastNumDataNodes
public void ensureAtLeastNumDataNodes(int n) Ensures that at leastn
data nodes are present in the cluster. if more nodes thann
are present this method will not stop any of the running nodes. -
ensureAtMostNumDataNodes
Ensures that at mostn
are up and running. If less nodes thatn
are running this method will not start any additional nodes.- Throws:
IOException
-
client
public org.elasticsearch.client.internal.Client client()Description copied from class:TestCluster
Returns a client connected to any node in the cluster- Specified by:
client
in classTestCluster
-
dataNodeClient
public org.elasticsearch.client.internal.Client dataNodeClient()Returns a node client to a data node in the cluster. Note: use this with care tests should not rely on a certain nodes client. -
masterClient
public org.elasticsearch.client.internal.Client masterClient()Returns a node client to the current master node. Note: use this with care tests should not rely on a certain nodes client. -
nonMasterClient
public org.elasticsearch.client.internal.Client nonMasterClient()Returns a node client to random node but not the master. This method will fail if no non-master client is available. -
coordOnlyNodeClient
public org.elasticsearch.client.internal.Client coordOnlyNodeClient()Returns a client to a coordinating only node -
startCoordinatingOnlyNode
-
client
Returns a node client to a given node. -
smartClient
public org.elasticsearch.client.internal.Client smartClient()Returns a "smart" node client to a random node in the cluster -
close
Description copied from class:TestCluster
Closes the current cluster- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classTestCluster
- Throws:
IOException
-
beforeTest
Description copied from class:TestCluster
This method should be executed before each test to reset the cluster to its initial state.- Overrides:
beforeTest
in classTestCluster
- Throws:
IOException
InterruptedException
-
validateClusterFormed
public void validateClusterFormed()ensure a cluster is formed with all published nodes. -
afterTest
public void afterTest()Description copied from class:TestCluster
This method should be executed during tear down, after each test (but after assertAfterTest)- Specified by:
afterTest
in classTestCluster
-
beforeIndexDeletion
Description copied from class:TestCluster
Assertions that should run before the cluster is wiped should be called in this method- Overrides:
beforeIndexDeletion
in classTestCluster
- Throws:
Exception
-
assertConsistentHistoryBetweenTranslogAndLuceneIndex
Asserts that the document history in Lucene index is consistent with Translog's on every index shard of the cluster. This assertion might be expensive, thus we prefer not to execute on every test but only interesting tests.- Throws:
IOException
-
assertNoInFlightDocsInEngine
- Throws:
Exception
-
assertSeqNos
- Throws:
Exception
-
assertSameDocIdsOnShards
Asserts that all shards with the same shardId should have document Ids.- Throws:
Exception
-
wipePendingDataDirectories
public void wipePendingDataDirectories() -
clusterService
public org.elasticsearch.cluster.service.ClusterService clusterService()Returns a reference to a random node'sClusterService
-
clusterService
Returns a reference to a node'sClusterService
. If the given node is null, a random node will be selected. -
getInstances
Returns an Iterable to all instances for the given class >T< across all nodes in the cluster. -
getDataNodeInstances
Returns an Iterable to all instances for the given class >T< across all data nodes in the cluster. -
getCurrentMasterNodeInstance
Return the instance of the given class >T< from the elected master node. See alsogetAnyMasterNodeInstance(java.lang.Class<T>)
. -
getDataOrMasterNodeInstances
Returns an Iterable to all instances for the given class >T< across all data and master nodes in the cluster. -
getInstance
Returns a reference to the given nodes instances of the given class >T< -
getDataNodeInstance
-
getAnyMasterNodeInstance
Return the instance of the given class >T< from any of the master-eligible nodes (not necessarily the elected master). See alsogetCurrentMasterNodeInstance(java.lang.Class<T>)
. -
getInstance
Returns a reference to a random nodes instances of the given class >T< -
dataPathSettings
-
size
public int size()Description copied from class:TestCluster
Returns the number of nodes in the cluster.- Specified by:
size
in classTestCluster
-
httpAddresses
Description copied from class:TestCluster
Returns the http addresses of the nodes within the cluster. Can be used to run REST tests against the test cluster.- Specified by:
httpAddresses
in classTestCluster
-
stopRandomDataNode
Stops a random data node in the cluster. Returns true if a node was found to stop, false otherwise.- Throws:
IOException
-
stopNode
Stops a specific node in the cluster. Returns true if the node was found to stop, false otherwise.- Throws:
IOException
-
stopRandomNode
public void stopRandomNode(Predicate<org.elasticsearch.common.settings.Settings> filter) throws IOException Stops a random node in the cluster that applies to the given filter. Does nothing if none of the nodes match the filter.- Throws:
IOException
-
stopCurrentMasterNode
Stops the current master node forcefully- Throws:
IOException
-
stopRandomNonMasterNode
Stops any of the current nodes but not the master node.- Throws:
IOException
-
restartRandomDataNode
Restarts a random data node in the cluster- Throws:
Exception
-
restartRandomDataNode
Restarts a random data node in the cluster and calls the callback during restart.- Throws:
Exception
-
restartNode
Restarts a node.- Throws:
Exception
-
restartNode
public void restartNode(String nodeName, InternalTestCluster.RestartCallback callback) throws Exception Restarts a node and calls the callback during restart.- Throws:
Exception
-
fullRestart
Restarts all nodes in the cluster. It first stops all nodes and then restarts all the nodes again.- Throws:
Exception
-
rollingRestart
Restarts all nodes in a rolling restart fashion ie. only restarts on node a time.- Throws:
Exception
-
fullRestart
Restarts all nodes in the cluster. It first stops all nodes and then restarts all the nodes again.- Throws:
Exception
-
getMasterName
Returns the name of the current master node in the cluster. -
getMasterName
Returns the name of the current master node in the cluster and executes the request via the node specified in the viaNode parameter. If viaNode isn't specified a random node will be picked to the send the request to. -
nodesInclude
Returns a set of nodes that have at least one shard of the given index. -
startNode
Starts a node with default settings and returns its name. -
startNode
Starts a node with the given settings builder and returns its name. -
startNode
Starts a node with the given settings and returns its name. -
startNodes
Starts multiple nodes with default settings and returns their names -
startNodes
Starts multiple nodes with the given settings and returns their names -
startNodes
Starts multiple nodes with the given settings and returns their names -
startMasterOnlyNodes
-
startMasterOnlyNodes
-
startDataOnlyNodes
-
startDataOnlyNodes
-
startMasterOnlyNode
-
startMasterOnlyNode
-
startDataOnlyNode
-
startDataOnlyNode
-
numDataNodes
public int numDataNodes()Description copied from class:TestCluster
Returns the number of data nodes in the cluster.- Specified by:
numDataNodes
in classTestCluster
-
numDataAndMasterNodes
public int numDataAndMasterNodes()Description copied from class:TestCluster
Returns the number of data and master eligible nodes in the cluster.- Specified by:
numDataAndMasterNodes
in classTestCluster
-
numMasterNodes
public int numMasterNodes() -
setDisruptionScheme
-
clearDisruptionScheme
public void clearDisruptionScheme() -
clearDisruptionScheme
public void clearDisruptionScheme(boolean ensureHealthyCluster) -
getClients
Description copied from class:TestCluster
Returns anIterable
over all clients in this test cluster- Specified by:
getClients
in classTestCluster
-
getNamedWriteableRegistry
public org.elasticsearch.common.io.stream.NamedWriteableRegistry getNamedWriteableRegistry()Description copied from class:TestCluster
Returns this clustersNamedWriteableRegistry
this is needed to deserialize binary content from this cluster that might include custom named writeables- Specified by:
getNamedWriteableRegistry
in classTestCluster
-
nameFilter
Returns a predicate that only accepts settings of nodes with one of the given names. -
getDefaultSettings
public org.elasticsearch.common.settings.Settings getDefaultSettings() -
ensureEstimatedStats
public void ensureEstimatedStats()Description copied from class:TestCluster
Ensures that any breaker statistics are reset to 0. The implementation is specific to the test cluster, because the act of checking some breaker stats can increase them.- Specified by:
ensureEstimatedStats
in classTestCluster
-
assertAfterTest
Description copied from class:TestCluster
This method checks all the things that need to be checked after each test- Overrides:
assertAfterTest
in classTestCluster
- Throws:
Exception
-
assertRequestsFinished
public void assertRequestsFinished()
-