Class InternalTestCluster

java.lang.Object
org.elasticsearch.test.TestCluster
org.elasticsearch.test.InternalTestCluster
All Implemented Interfaces:
Closeable, 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) 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.

  • 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:
    • BOOTSTRAP_MASTER_NODE_INDEX_AUTO

      public static final int BOOTSTRAP_MASTER_NODE_INDEX_AUTO
      Default value of bootstrapMasterNodeIndex, indicating that bootstrapping should happen automatically.
      See Also:
    • BOOTSTRAP_MASTER_NODE_INDEX_DONE

      public static final int BOOTSTRAP_MASTER_NODE_INDEX_DONE
      Sentinel value of bootstrapMasterNodeIndex, indicating that bootstrapMasterNodeIndex was set explicitly and has been used.
      See Also:
    • EMPTY_CALLBACK

      public static final InternalTestCluster.RestartCallback 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 Plugin>> mockPlugins, Function<Client,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 Plugin>> mockPlugins, Function<Client,Client> clientWrapper, boolean forbidPrivateIndexSettings, boolean forceSingleDataPath, boolean autoManageVotingExclusions)
  • Method Details

    • setBootstrapMasterNodeIndex

      public void setBootstrapMasterNodeIndex(int bootstrapMasterNodeIndex)
      Sets bootstrapMasterNodeIndex to the given value, see bootstrapMasterNodeWithSpecifiedIndex(List) for the description of how this field is used. It's only possible to change bootstrapMasterNodeIndex value if autoManageMasterNodes is false.
    • getClusterName

      public String getClusterName()
      Description copied from class: TestCluster
      Returns the cluster name
      Specified by:
      getClusterName in class TestCluster
    • getNodeNames

      public String[] getNodeNames()
    • getPlugins

      public Collection<Class<? extends Plugin>> getPlugins()
    • clusterName

      public static String clusterName(String prefix, long clusterSeed)
    • ensureAtLeastNumDataNodes

      public void ensureAtLeastNumDataNodes(int n)
      Ensures that at least n data nodes are present in the cluster. if more nodes than n are present this method will not stop any of the running nodes.
    • ensureAtMostNumDataNodes

      public void ensureAtMostNumDataNodes(int n) throws IOException
      Ensures that at most n are up and running. If less nodes that n are running this method will not start any additional nodes.
      Throws:
      IOException
    • client

      public Client client()
      Description copied from class: TestCluster
      Returns a client connected to any node in the cluster
      Specified by:
      client in class TestCluster
    • 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 String startCoordinatingOnlyNode(Settings settings)
    • client

      public Client client(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
    • close

      public void close() throws IOException
      Description copied from class: TestCluster
      Closes the current cluster
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class TestCluster
      Throws:
      IOException
    • beforeTest

      public void beforeTest(Random random) throws IOException, 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 class TestCluster
      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 class TestCluster
    • beforeIndexDeletion

      public void beforeIndexDeletion() throws Exception
      Description copied from class: TestCluster
      Assertions that should run before the cluster is wiped should be called in this method
      Overrides:
      beforeIndexDeletion in class TestCluster
      Throws:
      Exception
    • assertConsistentHistoryBetweenTranslogAndLuceneIndex

      public void assertConsistentHistoryBetweenTranslogAndLuceneIndex() throws IOException
      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

      public void assertNoInFlightDocsInEngine() throws Exception
      Throws:
      Exception
    • assertSeqNos

      public void assertSeqNos() throws Exception
      Throws:
      Exception
    • assertSameDocIdsOnShards

      public void assertSameDocIdsOnShards() throws Exception
      Asserts that all shards with the same shardId should have document Ids.
      Throws:
      Exception
    • wipePendingDataDirectories

      public void wipePendingDataDirectories()
    • clusterService

      public ClusterService clusterService()
      Returns a reference to a random node's ClusterService
    • clusterService

      public ClusterService clusterService(@Nullable String node)
      Returns a reference to a node's ClusterService. If the given node is null, a random node will be selected.
    • getInstances

      public <T> Iterable<T> getInstances(Class<T> clazz)
      Returns an Iterable to all instances for the given class >T< across all nodes in the cluster.
    • getDataNodeInstances

      public <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.
    • getCurrentMasterNodeInstance

      public <T> T getCurrentMasterNodeInstance(Class<T> clazz)
      Return the instance of the given class >T< from the elected master node. See also getAnyMasterNodeInstance(java.lang.Class<T>).
    • getDataOrMasterNodeInstances

      public <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.
    • getInstance

      public <T> T getInstance(Class<T> clazz, String nodeName)
      Returns:
      the instance of the given class from the node with provided nodeName
    • getInstance

      public <T> T getInstance(Class<T> clazz, DiscoveryNodeRole role)
      Returns:
      the instance of the given class from a random node with provided role
    • getDataNodeInstance

      public <T> T getDataNodeInstance(Class<T> clazz)
    • getAnyMasterNodeInstance

      public <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). See also getCurrentMasterNodeInstance(java.lang.Class<T>).
    • getInstance

      public <T> T getInstance(Class<T> clazz)
      Returns a reference to a random nodes instances of the given class >T<
    • dataPathSettings

      public Settings dataPathSettings(String node)
    • size

      public int size()
      Description copied from class: TestCluster
      Returns the number of nodes in the cluster.
      Specified by:
      size in class TestCluster
    • httpAddresses

      public 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 class TestCluster
    • stopRandomDataNode

      public boolean stopRandomDataNode() throws IOException
      Stops a random data node in the cluster. Returns true if a node was found to stop, false otherwise.
      Throws:
      IOException
    • stopNode

      public boolean stopNode(String nodeName) throws IOException
      Stops a specific node in the cluster. Returns true if the node was found to stop, false otherwise.
      Throws:
      IOException
    • stopCurrentMasterNode

      public void stopCurrentMasterNode() throws IOException
      Stops the current master node forcefully
      Throws:
      IOException
    • stopRandomNonMasterNode

      public void stopRandomNonMasterNode() throws IOException
      Stops any of the current nodes but not the master node.
      Throws:
      IOException
    • configPaths

      public Collection<Path> configPaths()
    • restartRandomDataNode

      public void restartRandomDataNode() throws Exception
      Restarts a random data node in the cluster
      Throws:
      Exception
    • restartRandomDataNode

      public void restartRandomDataNode(InternalTestCluster.RestartCallback callback) throws Exception
      Restarts a random data node in the cluster and calls the callback during restart.
      Throws:
      Exception
    • restartNode

      public void restartNode(String nodeName) throws Exception
      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

      public void fullRestart() throws Exception
      Restarts all nodes in the cluster. It first stops all nodes and then restarts all the nodes again.
      Throws:
      Exception
    • rollingRestart

      public void rollingRestart(InternalTestCluster.RestartCallback callback) throws Exception
      Restarts all nodes in a rolling restart fashion ie. only restarts on node a time.
      Throws:
      Exception
    • fullRestart

      public void fullRestart(InternalTestCluster.RestartCallback callback) throws Exception
      Restarts all nodes in the cluster. It first stops all nodes and then restarts all the nodes again.
      Throws:
      Exception
    • getMasterName

      public String getMasterName()
      Returns the name of the current master node in the cluster.
    • getMasterName

      public String getMasterName(@Nullable 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.
    • getRandomNodeName

      public String getRandomNodeName()
      Returns:
      the name of a random node in a cluster
    • getNodeNameThat

      public String getNodeNameThat(Predicate<Settings> predicate)
      Returns:
      the name of a random node in a cluster that match the predicate
    • nodesInclude

      public Set<String> nodesInclude(String index)
      Returns a set of nodes that have at least one shard of the given index.
    • startNode

      public String startNode()
      Starts a node with default settings and returns its name.
    • startNode

      public String startNode(Settings.Builder settings)
      Starts a node with the given settings builder and returns its name.
    • startNode

      public String startNode(Settings settings)
      Starts a node with the given settings and returns its name.
    • startNodes

      public List<String> startNodes(int numOfNodes)
      Starts multiple nodes with default settings and returns their names
    • startNodes

      public List<String> startNodes(int numOfNodes, Settings settings)
      Starts multiple nodes with the given settings and returns their names
    • startNodes

      public List<String> startNodes(Settings... extraSettings)
      Starts multiple nodes with the given settings and returns their names
    • startMasterOnlyNodes

      public List<String> startMasterOnlyNodes(int numNodes)
    • startMasterOnlyNodes

      public List<String> startMasterOnlyNodes(int numNodes, Settings settings)
    • startDataOnlyNodes

      public List<String> startDataOnlyNodes(int numNodes)
    • startDataOnlyNodes

      public List<String> startDataOnlyNodes(int numNodes, Settings settings)
    • startMasterOnlyNode

      public String startMasterOnlyNode()
    • startMasterOnlyNode

      public String startMasterOnlyNode(Settings settings)
    • startDataOnlyNode

      public String startDataOnlyNode()
    • startDataOnlyNode

      public String startDataOnlyNode(Settings settings)
    • closeNonSharedNodes

      public void closeNonSharedNodes(boolean wipeData) throws IOException
      Throws:
      IOException
    • numDataNodes

      public int numDataNodes()
      Description copied from class: TestCluster
      Returns the number of data nodes in the cluster.
      Specified by:
      numDataNodes in class TestCluster
    • 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 class TestCluster
    • numMasterNodes

      public int numMasterNodes()
    • setDisruptionScheme

      public void setDisruptionScheme(ServiceDisruptionScheme scheme)
    • clearDisruptionScheme

      public void clearDisruptionScheme()
    • clearDisruptionScheme

      public void clearDisruptionScheme(boolean ensureHealthyCluster)
    • getClients

      public Iterable<Client> getClients()
      Description copied from class: TestCluster
      Returns an Iterable over all clients in this test cluster
      Specified by:
      getClients in class TestCluster
    • getNamedWriteableRegistry

      public NamedWriteableRegistry getNamedWriteableRegistry()
      Description copied from class: TestCluster
      Returns this clusters NamedWriteableRegistry this is needed to deserialize binary content from this cluster that might include custom named writeables
      Specified by:
      getNamedWriteableRegistry in class TestCluster
    • 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 class TestCluster
    • assertAfterTest

      public void assertAfterTest() throws Exception
      Description copied from class: TestCluster
      This method checks all the things that need to be checked after each test
      Overrides:
      assertAfterTest in class TestCluster
      Throws:
      Exception
    • assertRequestsFinished

      public void assertRequestsFinished()