Class 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) 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.

    • 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 class TestCluster
      • 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 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 java.io.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:
        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 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 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 return null
      • close

        public void close()
        Description copied from class: TestCluster
        Closes the current cluster
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class TestCluster
      • 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 class TestCluster
        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 class TestCluster
        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 class TestCluster
        Throws:
        java.lang.Exception
      • clusterService

        public ClusterService clusterService​(@Nullable
                                             java.lang.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> 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 class TestCluster
      • 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 class TestCluster
      • 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 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()
      • clearDisruptionScheme

        public void clearDisruptionScheme()
      • clearDisruptionScheme

        public void clearDisruptionScheme​(boolean ensureHealthyCluster)
      • getClients

        public java.lang.Iterable<Client> getClients()
        Description copied from class: TestCluster
        Returns an Iterable over all clients in this test cluster
        Specified by:
        getClients in class TestCluster
      • 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 class TestCluster
      • 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 class TestCluster
        Throws:
        java.io.IOException