Class TestingCluster

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class TestingCluster
    extends Object
    implements Closeable
    manages an internally running ensemble of ZooKeeper servers. FOR TESTING PURPOSES ONLY
    • Constructor Detail

      • TestingCluster

        public TestingCluster​(int instanceQty)
        Creates an ensemble comprised of n servers. Each server will use a temp directory and random ports
        Parameters:
        instanceQty - number of servers to create in the ensemble
      • TestingCluster

        public TestingCluster​(InstanceSpec... specs)
        Creates an ensemble using the given server specs
        Parameters:
        specs - the server specs
      • TestingCluster

        public TestingCluster​(Collection<InstanceSpec> specs)
        Creates an ensemble using the given server specs
        Parameters:
        specs - the server specs
      • TestingCluster

        public TestingCluster​(Map<InstanceSpec,​Collection<InstanceSpec>> specs)
        Creates an ensemble using the given server specs
        Parameters:
        specs - map of an instance spec to its set of quorum instances. Allows simulation of an ensemble with instances having different config peers
    • Method Detail

      • getInstances

        public Collection<InstanceSpec> getInstances()
        Returns the set of servers in the ensemble
        Returns:
        set of servers
      • getConnectString

        public String getConnectString()
        Returns the connection string to pass to the ZooKeeper constructor
        Returns:
        connection string
      • start

        public void start()
                   throws Exception
        Start the ensemble. The cluster must be started before use.
        Throws:
        Exception - errors
      • stop

        public void stop()
                  throws IOException
        Shutdown the ensemble WITHOUT freeing resources, etc.
        Throws:
        IOException
      • close

        public void close()
                   throws IOException
        Shutdown the ensemble, free resources, etc. If temp directories were used, they are deleted. You should call this in a finally block.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - errors
      • killServer

        public boolean killServer​(InstanceSpec instance)
                           throws Exception
        Kills the given server. This simulates the server unexpectedly crashing
        Parameters:
        instance - server to kill
        Returns:
        true if the instance was found
        Throws:
        Exception - errors
      • restartServer

        public boolean restartServer​(InstanceSpec instance)
                              throws Exception
        Restart the given server of the cluster
        Parameters:
        instance - server instance
        Returns:
        true of the server was found
        Throws:
        Exception - errors
      • findConnectionInstance

        public InstanceSpec findConnectionInstance​(org.apache.zookeeper.ZooKeeper client)
                                            throws Exception
        Given a ZooKeeper instance, returns which server it is connected to
        Parameters:
        client - ZK instance
        Returns:
        the server
        Throws:
        Exception - errors