Package org.elasticsearch.test
Class InternalTestCluster
- java.lang.Object
-
- org.elasticsearch.test.TestCluster
-
- org.elasticsearch.test.InternalTestCluster
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public final class InternalTestCluster extends TestCluster
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, double)
andafterTest()
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 throughESIntegTestCase
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InternalTestCluster.RestartCallback
An abstract class that is called duringrollingRestart(InternalTestCluster.RestartCallback)
and / orfullRestart(InternalTestCluster.RestartCallback)
to execute actions at certain stages of the restart.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_HIGH_NUM_MASTER_NODES
static int
DEFAULT_LOW_NUM_MASTER_NODES
static InternalTestCluster.RestartCallback
EMPTY_CALLBACK
static java.lang.String
TRANSPORT_CLIENT_PREFIX
-
Fields inherited from class org.elasticsearch.test.TestCluster
random, transportClientRatio
-
-
Constructor Summary
Constructors Constructor Description InternalTestCluster(long clusterSeed, java.nio.file.Path baseDir, boolean randomlyAddDedicatedMasters, boolean autoManageMinMasterNodes, int minNumDataNodes, int maxNumDataNodes, java.lang.String clusterName, NodeConfigurationSource nodeConfigurationSource, int numClientNodes, boolean enableHttpPipelining, java.lang.String nodePrefix, java.util.Collection<java.lang.Class<? extends Plugin>> mockPlugins, java.util.function.Function<Client,Client> clientWrapper)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterTest()
This method should be executed during tear down, after each test (but after assertAfterTest)void
assertAfterTest()
This method checks all the things that need to be checked after each testvoid
beforeIndexDeletion()
Assertions that should run before the cluster is wiped should be called in this methodvoid
beforeTest(java.util.Random random, double transportClientRatio)
This method should be executed before each test to reset the cluster to its initial state.void
clearDisruptionScheme()
void
clearDisruptionScheme(boolean ensureHealthyCluster)
Client
client()
Returns a client connected to any node in the clusterClient
client(java.lang.String nodeName)
Returns a node client to a given node.Client
client(java.util.function.Predicate<Settings> filterPredicate)
Returns a random node that applies to the given predicate.void
close()
Closes the current clustervoid
closeNonSharedNodes(boolean wipeData)
static java.lang.String
clusterName(java.lang.String prefix, long clusterSeed)
ClusterService
clusterService()
Returns a reference to a random node'sClusterService
ClusterService
clusterService(java.lang.String node)
Returns a reference to a node'sClusterService
.Client
coordOnlyNodeClient()
Returns a client to a coordinating only nodeClient
dataNodeClient()
Returns a node client to a data node in the cluster.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
ensureEstimatedStats()
Ensures that any breaker statistics are reset to 0.void
fullRestart()
Restarts all nodes in the cluster.void
fullRestart(InternalTestCluster.RestartCallback callback)
Restarts all nodes in the cluster.boolean
getAutoManageMinMasterNode()
returns true if theElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING
setting is auto managed by this clusterjava.lang.Iterable<Client>
getClients()
Returns anIterable
over all clients in this test clusterjava.lang.String
getClusterName()
Returns the cluster name<T> T
getDataNodeInstance(java.lang.Class<T> clazz)
<T> java.lang.Iterable<T>
getDataNodeInstances(java.lang.Class<T> clazz)
Returns an Iterable to all instances for the given class >T< across all data nodes in the cluster.<T> java.lang.Iterable<T>
getDataOrMasterNodeInstances(java.lang.Class<T> clazz)
Returns an Iterable to all instances for the given class >T< across all data and master nodes in the cluster.Settings
getDefaultSettings()
<T> T
getInstance(java.lang.Class<T> clazz)
Returns a reference to a random nodes instances of the given class >T<<T> T
getInstance(java.lang.Class<T> clazz, java.lang.String node)
Returns a reference to the given nodes instances of the given class >T<<T> java.lang.Iterable<T>
getInstances(java.lang.Class<T> clazz)
Returns an Iterable to all instances for the given class >T< across all nodes in the cluster.java.lang.String
getMasterName()
Returns the name of the current master node in the cluster.java.lang.String
getMasterName(java.lang.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.NamedWriteableRegistry
getNamedWriteableRegistry()
Returns this clustersNamedWriteableRegistry
this is needed to deserialize binary content from this cluster that might include custom named writeablesjava.lang.String[]
getNodeNames()
java.util.Collection<java.lang.Class<? extends Plugin>>
getPlugins()
java.net.InetSocketAddress[]
httpAddresses()
Returns the http addresses of the nodes within the cluster.Client
masterClient()
Returns a node client to the current master node.static java.util.function.Predicate<Settings>
nameFilter(java.lang.String... nodeName)
Returns a predicate that only accepts settings of nodes with one of the given names.java.lang.String
nodePrefix()
Returns the common node name prefix for this test cluster.java.util.Set<java.lang.String>
nodesInclude(java.lang.String index)
Returns a set of nodes that have at least one shard of the given index.Client
nonMasterClient()
Returns a node client to random node but not the master.int
numDataAndMasterNodes()
Returns the number of data and master eligible nodes in the cluster.int
numDataNodes()
Returns the number of data nodes in the cluster.int
numMasterNodes()
void
restartNode(java.lang.String nodeName, InternalTestCluster.RestartCallback callback)
Restarts a node and calls the callback during restart.void
restartRandomDataNode()
Restarts a random data node in the clustervoid
restartRandomDataNode(InternalTestCluster.RestartCallback callback)
Restarts a random data node in the cluster and calls the callback during restart.void
restartRandomNode()
Restarts a random node in the clustervoid
restartRandomNode(InternalTestCluster.RestartCallback callback)
Restarts a random node in the cluster and calls the callback during restart.void
rollingRestart()
Restarts all nodes in a rolling restart fashion ie.void
rollingRestart(InternalTestCluster.RestartCallback callback)
Restarts all nodes in a rolling restart fashion ie.void
setDisruptionScheme(ServiceDisruptionScheme scheme)
int
size()
Returns the number of nodes in the cluster.Client
smartClient()
Returns a "smart" node client to a random node in the clusterjava.lang.String
startCoordinatingOnlyNode(Settings settings)
java.lang.String
startDataOnlyNode()
java.lang.String
startDataOnlyNode(Settings settings)
java.util.List<java.lang.String>
startDataOnlyNodes(int numNodes)
java.util.List<java.lang.String>
startDataOnlyNodes(int numNodes, Settings settings)
java.lang.String
startMasterOnlyNode()
java.lang.String
startMasterOnlyNode(Settings settings)
java.util.List<java.lang.String>
startMasterOnlyNodes(int numNodes)
java.util.List<java.lang.String>
startMasterOnlyNodes(int numNodes, Settings settings)
java.lang.String
startNode()
Starts a node with default settings and returns its name.java.lang.String
startNode(Settings settings)
Starts a node with the given settings and returns its name.java.lang.String
startNode(Settings.Builder settings)
Starts a node with the given settings builder and returns its name.java.util.List<java.lang.String>
startNodes(int numOfNodes)
Starts multiple nodes with default settings and returns their namesjava.util.List<java.lang.String>
startNodes(int numOfNodes, Settings settings)
Starts multiple nodes with the given settings and returns their namesjava.util.List<java.lang.String>
startNodes(Settings... settings)
Starts multiple nodes with the given settings and returns their namesvoid
stopCurrentMasterNode()
Stops the current master node forcefullyboolean
stopRandomDataNode()
Stops a random data node in the cluster.void
stopRandomNode(java.util.function.Predicate<Settings> filter)
Stops a random node in the cluster that applies to the given filter or non if the non of the nodes applies to the filter.void
stopRandomNonMasterNode()
Stops any of the current nodes but not the master node.Client
transportClient()
Returns a transport clientvoid
validateClusterFormed()
ensure a cluster is formed with all published nodes.void
validateClusterFormed(java.lang.String viaNode)
ensure a cluster is formed with all published nodes, but do so by using the client of the specified node-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from class org.elasticsearch.test.TestCluster
seed, wipe, wipeAllTemplates, wipeIndices, wipeRepositories, wipeTemplates
-
-
-
-
Field Detail
-
DEFAULT_LOW_NUM_MASTER_NODES
public static final int DEFAULT_LOW_NUM_MASTER_NODES
- See Also:
- Constant Field Values
-
DEFAULT_HIGH_NUM_MASTER_NODES
public static final int DEFAULT_HIGH_NUM_MASTER_NODES
- See Also:
- Constant Field Values
-
TRANSPORT_CLIENT_PREFIX
public static final java.lang.String TRANSPORT_CLIENT_PREFIX
- See Also:
- Constant Field Values
-
EMPTY_CALLBACK
public static final InternalTestCluster.RestartCallback EMPTY_CALLBACK
-
-
Constructor Detail
-
InternalTestCluster
public InternalTestCluster(long clusterSeed, java.nio.file.Path baseDir, boolean randomlyAddDedicatedMasters, boolean autoManageMinMasterNodes, int minNumDataNodes, int maxNumDataNodes, java.lang.String clusterName, NodeConfigurationSource nodeConfigurationSource, int numClientNodes, boolean enableHttpPipelining, java.lang.String nodePrefix, java.util.Collection<java.lang.Class<? extends Plugin>> mockPlugins, java.util.function.Function<Client,Client> clientWrapper)
-
-
Method Detail
-
getClusterName
public java.lang.String getClusterName()
Description copied from class:TestCluster
Returns the cluster name- Specified by:
getClusterName
in classTestCluster
-
getAutoManageMinMasterNode
public boolean getAutoManageMinMasterNode()
returns true if theElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING
setting is auto managed by this cluster
-
getNodeNames
public java.lang.String[] getNodeNames()
-
getPlugins
public java.util.Collection<java.lang.Class<? extends Plugin>> getPlugins()
-
clusterName
public static java.lang.String clusterName(java.lang.String prefix, long clusterSeed)
-
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
public void ensureAtMostNumDataNodes(int n) throws java.io.IOException
Ensures that at mostn
are up and running. If less nodes thatn
are running this method will not start any additional nodes.- Throws:
java.io.IOException
-
nodePrefix
public java.lang.String nodePrefix()
Returns the common node name prefix for this test cluster.
-
client
public Client client()
Description copied from class:TestCluster
Returns a client connected to any node in the cluster- Specified by:
client
in classTestCluster
-
dataNodeClient
public 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 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 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 Client coordOnlyNodeClient()
Returns a client to a coordinating only node
-
startCoordinatingOnlyNode
public java.lang.String startCoordinatingOnlyNode(Settings settings)
-
transportClient
public Client transportClient()
Returns a transport client
-
client
public Client client(java.lang.String nodeName)
Returns a node client to a given node.
-
smartClient
public Client smartClient()
Returns a "smart" node client to a random node in the cluster
-
client
public Client client(java.util.function.Predicate<Settings> filterPredicate)
Returns a random node that applies to the given predicate. The predicate can filter nodes based on the nodes settings. If all nodes are filtered out this method will returnnull
-
close
public void close()
Description copied from class:TestCluster
Closes the current cluster- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classTestCluster
-
beforeTest
public void beforeTest(java.util.Random random, double transportClientRatio) throws java.io.IOException, java.lang.InterruptedException
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:
java.io.IOException
java.lang.InterruptedException
-
validateClusterFormed
public void validateClusterFormed()
ensure a cluster is formed with all published nodes.
-
validateClusterFormed
public void validateClusterFormed(java.lang.String viaNode)
ensure a cluster is formed with all published nodes, but do so by using the client of the specified node
-
afterTest
public void afterTest() throws java.io.IOException
Description copied from class:TestCluster
This method should be executed during tear down, after each test (but after assertAfterTest)- Specified by:
afterTest
in classTestCluster
- Throws:
java.io.IOException
-
beforeIndexDeletion
public void beforeIndexDeletion() throws java.lang.Exception
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:
java.lang.Exception
-
clusterService
public ClusterService clusterService()
Returns a reference to a random node'sClusterService
-
clusterService
public ClusterService clusterService(@Nullable java.lang.String node)
Returns a reference to a node'sClusterService
. If the given node is null, a random node will be selected.
-
getInstances
public <T> java.lang.Iterable<T> getInstances(java.lang.Class<T> clazz)
Returns an Iterable to all instances for the given class >T< across all nodes in the cluster.
-
getDataNodeInstances
public <T> java.lang.Iterable<T> getDataNodeInstances(java.lang.Class<T> clazz)
Returns an Iterable to all instances for the given class >T< across all data nodes in the cluster.
-
getDataOrMasterNodeInstances
public <T> java.lang.Iterable<T> getDataOrMasterNodeInstances(java.lang.Class<T> clazz)
Returns an Iterable to all instances for the given class >T< across all data and master nodes in the cluster.
-
getInstance
public <T> T getInstance(java.lang.Class<T> clazz, java.lang.String node)
Returns a reference to the given nodes instances of the given class >T<
-
getDataNodeInstance
public <T> T getDataNodeInstance(java.lang.Class<T> clazz)
-
getInstance
public <T> T getInstance(java.lang.Class<T> clazz)
Returns a reference to a random nodes instances of the given class >T<
-
size
public int size()
Description copied from class:TestCluster
Returns the number of nodes in the cluster.- Specified by:
size
in classTestCluster
-
httpAddresses
public java.net.InetSocketAddress[] 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
public boolean stopRandomDataNode() throws java.io.IOException
Stops a random data node in the cluster. Returns true if a node was found to stop, false otherwise.- Throws:
java.io.IOException
-
stopRandomNode
public void stopRandomNode(java.util.function.Predicate<Settings> filter) throws java.io.IOException
Stops a random node in the cluster that applies to the given filter or non if the non of the nodes applies to the filter.- Throws:
java.io.IOException
-
stopCurrentMasterNode
public void stopCurrentMasterNode() throws java.io.IOException
Stops the current master node forcefully- Throws:
java.io.IOException
-
stopRandomNonMasterNode
public void stopRandomNonMasterNode() throws java.io.IOException
Stops any of the current nodes but not the master node.- Throws:
java.io.IOException
-
restartRandomNode
public void restartRandomNode() throws java.lang.Exception
Restarts a random node in the cluster- Throws:
java.lang.Exception
-
restartRandomNode
public void restartRandomNode(InternalTestCluster.RestartCallback callback) throws java.lang.Exception
Restarts a random node in the cluster and calls the callback during restart.- Throws:
java.lang.Exception
-
restartRandomDataNode
public void restartRandomDataNode() throws java.lang.Exception
Restarts a random data node in the cluster- Throws:
java.lang.Exception
-
restartRandomDataNode
public void restartRandomDataNode(InternalTestCluster.RestartCallback callback) throws java.lang.Exception
Restarts a random data node in the cluster and calls the callback during restart.- Throws:
java.lang.Exception
-
restartNode
public void restartNode(java.lang.String nodeName, InternalTestCluster.RestartCallback callback) throws java.lang.Exception
Restarts a node and calls the callback during restart.- Throws:
java.lang.Exception
-
fullRestart
public void fullRestart() throws java.lang.Exception
Restarts all nodes in the cluster. It first stops all nodes and then restarts all the nodes again.- Throws:
java.lang.Exception
-
rollingRestart
public void rollingRestart() throws java.lang.Exception
Restarts all nodes in a rolling restart fashion ie. only restarts on node a time.- Throws:
java.lang.Exception
-
rollingRestart
public void rollingRestart(InternalTestCluster.RestartCallback callback) throws java.lang.Exception
Restarts all nodes in a rolling restart fashion ie. only restarts on node a time.- Throws:
java.lang.Exception
-
fullRestart
public void fullRestart(InternalTestCluster.RestartCallback callback) throws java.lang.Exception
Restarts all nodes in the cluster. It first stops all nodes and then restarts all the nodes again.- Throws:
java.lang.Exception
-
getMasterName
public java.lang.String getMasterName()
Returns the name of the current master node in the cluster.
-
getMasterName
public java.lang.String getMasterName(@Nullable java.lang.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. If viaNode isn't specified a random node will be picked to the send the request to.
-
nodesInclude
public java.util.Set<java.lang.String> nodesInclude(java.lang.String index)
Returns a set of nodes that have at least one shard of the given index.
-
startNode
public java.lang.String startNode()
Starts a node with default settings and returns its name.
-
startNode
public java.lang.String startNode(Settings.Builder settings)
Starts a node with the given settings builder and returns its name.
-
startNode
public java.lang.String startNode(Settings settings)
Starts a node with the given settings and returns its name.
-
startNodes
public java.util.List<java.lang.String> startNodes(int numOfNodes)
Starts multiple nodes with default settings and returns their names
-
startNodes
public java.util.List<java.lang.String> startNodes(int numOfNodes, Settings settings)
Starts multiple nodes with the given settings and returns their names
-
startNodes
public java.util.List<java.lang.String> startNodes(Settings... settings)
Starts multiple nodes with the given settings and returns their names
-
startMasterOnlyNodes
public java.util.List<java.lang.String> startMasterOnlyNodes(int numNodes)
-
startMasterOnlyNodes
public java.util.List<java.lang.String> startMasterOnlyNodes(int numNodes, Settings settings)
-
startDataOnlyNodes
public java.util.List<java.lang.String> startDataOnlyNodes(int numNodes)
-
startDataOnlyNodes
public java.util.List<java.lang.String> startDataOnlyNodes(int numNodes, Settings settings)
-
startMasterOnlyNode
public java.lang.String startMasterOnlyNode()
-
startMasterOnlyNode
public java.lang.String startMasterOnlyNode(Settings settings)
-
startDataOnlyNode
public java.lang.String startDataOnlyNode()
-
startDataOnlyNode
public java.lang.String startDataOnlyNode(Settings settings)
-
closeNonSharedNodes
public void closeNonSharedNodes(boolean wipeData) throws java.io.IOException
- Throws:
java.io.IOException
-
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
public void setDisruptionScheme(ServiceDisruptionScheme scheme)
-
clearDisruptionScheme
public void clearDisruptionScheme()
-
clearDisruptionScheme
public void clearDisruptionScheme(boolean ensureHealthyCluster)
-
getClients
public java.lang.Iterable<Client> getClients()
Description copied from class:TestCluster
Returns anIterable
over all clients in this test cluster- Specified by:
getClients
in classTestCluster
-
getNamedWriteableRegistry
public 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
public static java.util.function.Predicate<Settings> nameFilter(java.lang.String... nodeName)
Returns a predicate that only accepts settings of nodes with one of the given names.
-
getDefaultSettings
public 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
public void assertAfterTest() throws java.io.IOException
Description copied from class:TestCluster
This method checks all the things that need to be checked after each test- Overrides:
assertAfterTest
in classTestCluster
- Throws:
java.io.IOException
-
-