All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opower.zookeeper.test.MiniZooKeeper Maven / Gradle / Ivy

The newest version!
package com.opower.zookeeper.test;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

/**
 * Encapsulates configuration for a single ZooKeeper server that will be run in the same JVM as calling code.
 * Construct using the {@link com.opower.zookeeper.test.MiniZooKeeper.Builder} provided by {@link #newBuilder()} or use
 * one of the {@code newDefaultInstance*} convenience factory methods.
 *
 * @author eric.chang
 */
public class MiniZooKeeper {
    private final String localhost;
    private final Optional port;
    private final int maxClientConnections;
    private final int tickTime;

    private MiniZooKeeper(String localhost, Optional port, int maxClientConnections, int tickTime) {
        this.localhost = Preconditions.checkNotNull(localhost, "localhost cannot be null");
        this.port = Preconditions.checkNotNull(port, "port cannot be null");
        this.maxClientConnections = Preconditions.checkNotNull(maxClientConnections, "maxClientConnections cannot be null");
        this.tickTime = Preconditions.checkNotNull(tickTime, "tickTime cannot be null");
    }

    /**
     * @return the hostname for localhost
     */
    String getLocalhost() {
        return this.localhost;
    }

    /**
     * @return the port on which this zookeeper instance should run.  Absent value indicates the port should be assigned
     * dynamically.
     */
    Optional getPort() {
        return this.port;
    }

    /**
     * @return the max client connections for this server
     */
    int getMaxClientConnections() {
        return this.maxClientConnections;
    }

    /**
     * @return the tick time in ms for this server
     */
    int getTickTime() {
        return this.tickTime;
    }

    /**
     * Static factory method that returns a MiniZooKeeper with default settings (see {@link MiniZooKeeper.Builder} constants)
     * and a dynamically assigned port.
     */
    public static MiniZooKeeper newDefaultInstance() {
        return newBuilder().build();
    }

    /**
     * Static factory method that returns a MiniZooKeeper with default settings (see {@link MiniZooKeeper.Builder} constants)
     * with the specified port.
     *
     * @param port port on which the ZooKeeper server should run.
     */
    public static MiniZooKeeper newDefaultInstanceWithPort(int port) {
        return newBuilder().setPort(port).build();
    }

    /**
     * @return a new Builder to create a MiniZooKeeper instance
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     * Builder for MiniZooKeeper instances
     */
    public static class Builder {
        // using ipv4 ip address for consistent client/server binding to ip address (avoids resolution of "localhost" to ipv6)
        public static final String DEFAULT_LOCALHOST = "127.0.0.1";
        public static final int DEFAULT_MAX_CLIENT_CXNS = 1024;
        public static final int DEFAULT_TICK_TIME_MS = 500;

        private String localhost = DEFAULT_LOCALHOST;
        private Optional port = Optional.absent();
        private int maxClientConnections = DEFAULT_MAX_CLIENT_CXNS;
        private int tickTime = DEFAULT_TICK_TIME_MS;

        private Builder() {
        }

        /**
         * Set localhost hostname (e.g. "localhost", "127.0.0.1").  Defaults to {@link #DEFAULT_LOCALHOST}
         *
         * @param localhost on which to run ZooKeeper
         */
        public Builder setLocalhost(String localhost) {
            this.localhost = Preconditions.checkNotNull(localhost, "localhost cannot be null");
            return this;
        }

        /**
         * Set the ports on which the server should run.  If left unspecified, an available port is assigned dynamically.
         *
         * @param port on which to run ZooKeeper
         */
        public Builder setPort(int port) {
            Preconditions.checkState(port > 0 && port < Math.pow(2, 16), String.format("invalid port: %d", port));
            this.port = Optional.of(port);
            return this;
        }

        /**
         * Set the max number of client connections accepted the server.  Defaults to {@link #DEFAULT_MAX_CLIENT_CXNS}.
         *
         * @param maxClientConnections max number of client connections
         */
        public Builder setMaxClientConnections(int maxClientConnections) {
            Preconditions.checkState(maxClientConnections > 0, "maxClientConnections must be > 0");
            this.maxClientConnections = maxClientConnections;
            return this;
        }

        /**
         * Set the tick time.  Defaults to {@link #DEFAULT_TICK_TIME_MS}.
         *
         * @param tickTime tick time in ms
         */
        public Builder setTickTime(int tickTime) {
            Preconditions.checkState(tickTime > 0, "tickTime must be > 0");
            this.tickTime = tickTime;
            return this;
        }

        /**
         * @return a MiniZooKeeper
         */
        public MiniZooKeeper build() {
            return new MiniZooKeeper(this.localhost, this.port, this.maxClientConnections, this.tickTime);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy