Java Code Examples for org.apache.zookeeper.server.NIOServerCnxnFactory#configure()

The following examples show how to use org.apache.zookeeper.server.NIOServerCnxnFactory#configure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CCEmbeddedZookeeper.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public CCEmbeddedZookeeper() {
  int tickTime = 500;
  try {
    File snapshotDir = CCKafkaTestUtils.newTempDir();
    File logDir = CCKafkaTestUtils.newTempDir();
    _zk = new ZooKeeperServer(snapshotDir, logDir, tickTime);
    _cnxnFactory = new NIOServerCnxnFactory();
    InetAddress localHost = InetAddress.getLocalHost();
    _hostAddress = localHost.getHostAddress();
    InetSocketAddress bindAddress = new InetSocketAddress(localHost, 0);
    _cnxnFactory.configure(bindAddress, 0);
    _cnxnFactory.startup(_zk);
    _port = _zk.getClientPort();
  } catch (Exception e) {
    throw new IllegalStateException(e);
  }
  //sanity check
  if (_zk.getClientPort() != _port) {
    throw new IllegalStateException();
  }
}
 
Example 2
Source File: EmbeddedZookeeper.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
public EmbeddedZookeeper(int port, Path baseDir) throws Exception {
    this.port = port;

    zookeeperBaseDir = baseDir;

    zkServer = new ZooKeeperServer();
    File dataDir = zookeeperBaseDir.resolve("log").toFile();
    File snapDir = zookeeperBaseDir.resolve("data").toFile();
    FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDir, snapDir);
    zkServer.setTxnLogFactory(ftxn);
    zkServer.setTickTime(1000);
    connectionFactory = new NIOServerCnxnFactory() {
        @Override
        protected void configureSaslLogin() throws IOException {
            // do nothing
        }
    };
    connectionFactory.configure(new InetSocketAddress("localhost", port), 0);
}
 
Example 3
Source File: TestCurrentInprogress.java    From big-c with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setupZooKeeper() throws Exception {
  LOG.info("Starting ZK server");
  zkTmpDir = File.createTempFile("zookeeper", "test");
  zkTmpDir.delete();
  zkTmpDir.mkdir();
  try {
    zks = new ZooKeeperServer(zkTmpDir, zkTmpDir, ZooKeeperDefaultPort);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(new InetSocketAddress(ZooKeeperDefaultPort), 10);
    serverFactory.startup(zks);
  } catch (Exception e) {
    LOG.error("Exception while instantiating ZooKeeper", e);
  }
  boolean b = LocalBookKeeper.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT);
  LOG.debug("ZooKeeper server up: " + b);
}
 
Example 4
Source File: TestBookKeeperConfiguration.java    From big-c with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setupZooKeeper() throws Exception {
  // create a ZooKeeper server(dataDir, dataLogDir, port)
  LOG.info("Starting ZK server");
  ZkTmpDir = File.createTempFile("zookeeper", "test");
  ZkTmpDir.delete();
  ZkTmpDir.mkdir();

  try {
    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperDefaultPort);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(new InetSocketAddress(ZooKeeperDefaultPort), 10);
    serverFactory.startup(zks);
  } catch (Exception e) {
    LOG.error("Exception while instantiating ZooKeeper", e);
  }

  boolean b = LocalBookKeeper.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT);
  LOG.debug("ZooKeeper server up: " + b);
}
 
Example 5
Source File: EmbeddedZooKeeper.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Override
public void before() throws IOException {
    snapshotDir = new File("target/test-classes/zk-snapshot");
    snapshotDir.mkdirs();
    logDir = new File("target/test-classes/zk-log");
    logDir.mkdirs();

    try {
        zooKeeperServer = new ZooKeeperServer(snapshotDir, logDir, tickTime);
        cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(new InetSocketAddress("localhost", port), 1024);
        cnxnFactory.startup(zooKeeperServer);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
 
Example 6
Source File: ZooKeeperTestServer.java    From vespa with Apache License 2.0 6 votes vote down vote up
private ZooKeeperTestServer(int port) throws IOException {
    zooKeeperDir = getTempDir();
    delete(zooKeeperDir);
    if (!zooKeeperDir.mkdir()) {
        throw new IllegalStateException("Failed to create directory " + zooKeeperDir);
    }
    zooKeeperDir.deleteOnExit();
    server = new ZooKeeperServer(zooKeeperDir, zooKeeperDir, (int)tickTime.toMillis());
    final int maxcc = 10000; // max number of connections from the same client
    factory = new NIOServerCnxnFactory();
    factory.configure(new InetSocketAddress(port), maxcc); // Use any port
    try{
        factory.startup(server);
    } catch (InterruptedException e) {
        throw (RuntimeException) new IllegalStateException("Interrupted during test startup: ").initCause(e);
    }
}
 
Example 7
Source File: ZKServerFactoryBean.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
public void afterPropertiesSet() throws Exception {
    if (purge) {
        deleteFilesInDir(getDataLogDir());
        deleteFilesInDir(getDataDir());
    }
    FileTxnSnapLog ftxn = new FileTxnSnapLog(getDataLogDir(), getDataDir());
    zooKeeperServer.setTxnLogFactory(ftxn);
    zooKeeperServer.setTickTime(getTickTime());
    zooKeeperServer.setMinSessionTimeout(getMinSessionTimeout());
    zooKeeperServer.setMaxSessionTimeout(getMaxSessionTimeout());
    connectionFactory = new NIOServerCnxnFactory() {
        @Override
        protected void configureSaslLogin() throws IOException {
            // do nothing
        }
    };
    connectionFactory.configure(getClientPortAddress(), getMaxClientConnections());
    connectionFactory.startup(zooKeeperServer);
}
 
Example 8
Source File: ZooKeeperTestServer.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Starts zookeeper up on an ephemeral port.
 */
public void startNetwork() throws IOException, InterruptedException {
  zooKeeperServer =
      new ZooKeeperServer(
          new FileTxnSnapLog(dataDir, snapDir),
          new BasicDataTreeBuilder()) {

        // TODO(John Sirois): Introduce a builder to configure the in-process server if and when
        // some folks need JMX for in-process tests.
        @Override protected void registerJMX() {
          // noop
        }
      };

  connectionFactory = new NIOServerCnxnFactory();
  connectionFactory.configure(
      new InetSocketAddress(port),
      60 /* Semi-arbitrary, max 60 connections is the default used by NIOServerCnxnFactory */);
  connectionFactory.startup(zooKeeperServer);
  port = zooKeeperServer.getClientPort();
}
 
Example 9
Source File: EmbeddedZkServer.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public EmbeddedZkServer(int port) throws Exception {
    snapshotDir = Files.createTempDirectory("zookeeper-snapshot-");
    logDir = Files.createTempDirectory("zookeeper-log-");
    int tickTime = 500;
    zookeeper = new ZooKeeperServer(snapshotDir.toFile(), logDir.toFile(), tickTime);
    factory = new NIOServerCnxnFactory();
    InetSocketAddress addr = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), port);
    LOGGER.info("Starting Zookeeper at " + addr);
    factory.configure(addr, 0);
    factory.startup(zookeeper);
}
 
Example 10
Source File: ZookeeperServer.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
public ZookeeperServer(File root) throws IOException, InterruptedException {
    zkServer = new ZooKeeperServer();

    File dataDir = new File(root, "log");
    File snapDir = new File(root, "data");
    FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDir, snapDir);

    zkServer.setTxnLogFactory(ftxn);
    zkServer.setTickTime(1000);

    connectionFactory = new NIOServerCnxnFactory();
    connectionFactory.configure(new InetSocketAddress("localhost", SocketUtils.findAvailableTcpPort()), 0);
    connectionFactory.startup(zkServer);
}
 
Example 11
Source File: ZookeeperServer.java    From ingestion with Apache License 2.0 5 votes vote down vote up
/**
 * Set embedded zookeeper up and spawn it in a new thread.
 * 
 * @throws TTransportException
 * @throws IOException
 * @throws InterruptedException
 */
public void start() throws Exception {

    File dir = Files.createTempDir();
    String dirPath = dir.getAbsolutePath();
    System.out.println("Storing ZooKeeper files in " + dirPath);

    ZooKeeperServer server = new ZooKeeperServer(dir, dir, TICK_TIME);
    standaloneServerFactory = new NIOServerCnxnFactory();
    standaloneServerFactory.configure(new InetSocketAddress(CLIENT_PORT), NUM_CONNECTIONS);

    standaloneServerFactory.startup(server); // start the server.

    TimeUnit.SECONDS.sleep(WAIT_SECONDS);
}
 
Example 12
Source File: ZookeeperTestServer.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public void connect() throws IOException, InterruptedException {
    zkServer = new ZooKeeperServer(new FileTxnSnapLog(dataDir, logDir), new ZooKeeperServer.BasicDataTreeBuilder());
    connectionFactory = new NIOServerCnxnFactory();
    connectionFactory.configure(new InetSocketAddress(port), 10);
    connectionFactory.startup(zkServer);
    port = zkServer.getClientPort();
}
 
Example 13
Source File: HBaseTestHelper.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private static void startZooKeeperServer() throws IOException, InterruptedException
{
  String zooLocation = System.getProperty("java.io.tmpdir");
  File zooFile = new File(zooLocation, "zookeeper-malhartest");
  ZooKeeperServer zooKeeper = new ZooKeeperServer(zooFile, zooFile, 2000);

  NIOServerCnxnFactory serverFactory = new NIOServerCnxnFactory();
  serverFactory.configure(new InetSocketAddress(2181),10);
  serverFactory.startup(zooKeeper);
}
 
Example 14
Source File: ZooKeeperUtil.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void startServer(String path) throws Exception {
    LOG.debug("Running ZK server");
    // ServerStats.registerAsConcrete();
    ClientBase.setupTestEnv();
    ZkTmpDir = File.createTempFile("zookeeper", "test");
    ZkTmpDir.delete();
    ZkTmpDir.mkdir();

    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperServer.DEFAULT_TICK_TIME);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(zkaddr, 100);
    serverFactory.startup(zks);

    zooKeeperPort = serverFactory.getLocalPort();
    connectString = "localhost:" + zooKeeperPort;
    boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
    LOG.debug("Server up: " + b);

    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    zkc = ZooKeeperClient.newBuilder().connectString(getZooKeeperConnectString()).build();
    if (path != "") {
        zkc.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    // initialize the zk client with values
    zkc.create(path + "/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.create(path +"/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example 15
Source File: LocalBookkeeperEnsemble.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void runZookeeper(int maxCC) throws IOException {
    // create a ZooKeeper server(dataDir, dataLogDir, port)
    LOG.info("Starting ZK server");
    // ServerStats.registerAsConcrete();
    // ClientBase.setupTestEnv();

    File zkDataDir = isNotBlank(zkDataDirName) ? Files.createDirectories(Paths.get(zkDataDirName)).toFile()
            : Files.createTempDirectory("zktest").toFile();

    if (this.clearOldData) {
        cleanDirectory(zkDataDir);
    }

    try {
        // Allow all commands on ZK control port
        System.setProperty("zookeeper.4lw.commands.whitelist", "*");
        zks = new ZooKeeperServer(zkDataDir, zkDataDir, ZooKeeperServer.DEFAULT_TICK_TIME);

        serverFactory = new NIOServerCnxnFactory();
        serverFactory.configure(new InetSocketAddress(zkPort), maxCC);
        serverFactory.startup(zks);
    } catch (Exception e) {
        LOG.error("Exception while instantiating ZooKeeper", e);

        if (serverFactory != null) {
            serverFactory.shutdown();
        }
        throw new IOException(e);
    }

    this.zkPort = serverFactory.getLocalPort();
    this.HOSTPORT = "127.0.0.1:" + zkPort;

    boolean b = waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT);

    LOG.info("ZooKeeper server up: {}", b);
    LOG.debug("Local ZK started (port: {}, data_directory: {})", zkPort, zkDataDir.getAbsolutePath());
}
 
Example 16
Source File: ZooKeeperClientAspectJTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void start() throws IOException {
    try {
        zks = new ZooKeeperServer(zkTmpDir, zkTmpDir, ZooKeeperServer.DEFAULT_TICK_TIME);
        zks.setMaxSessionTimeout(20000);
        serverFactory = new NIOServerCnxnFactory();
        serverFactory.configure(new InetSocketAddress(zkPort), 1000);
        serverFactory.startup(zks);
    } catch (Exception e) {
        log.error("Exception while instantiating ZooKeeper", e);
    }

    LocalBookkeeperEnsemble.waitForServerUp(hostPort, 30000);
    log.info("ZooKeeper started at {}", hostPort);
}
 
Example 17
Source File: ZookeeperTestService.java    From hudi with Apache License 2.0 5 votes vote down vote up
public ZooKeeperServer start() throws IOException, InterruptedException {
  Objects.requireNonNull(workDir, "The localBaseFsLocation must be set before starting cluster.");

  setupTestEnv();
  stop();

  File dir = new File(workDir, "zookeeper").getAbsoluteFile();
  recreateDir(dir, clean);
  int tickTimeToUse;
  if (this.tickTime > 0) {
    tickTimeToUse = this.tickTime;
  } else {
    tickTimeToUse = TICK_TIME;
  }
  this.zooKeeperServer = new ZooKeeperServer(dir, dir, tickTimeToUse);
  standaloneServerFactory = new NIOServerCnxnFactory();

  // NOTE: Changed from the original, where InetSocketAddress was
  // originally created to bind to the wildcard IP, we now configure it.
  LOG.info("Zookeeper force binding to: " + this.bindIP);
  standaloneServerFactory.configure(new InetSocketAddress(bindIP, clientPort), 1000);

  // Start up this ZK server
  standaloneServerFactory.startup(zooKeeperServer);

  String serverHostname;
  if (bindIP.equals("0.0.0.0")) {
    serverHostname = "localhost";
  } else {
    serverHostname = bindIP;
  }
  if (!waitForServerUp(serverHostname, clientPort, CONNECTION_TIMEOUT)) {
    throw new IOException("Waiting for startup of standalone server");
  }

  started = true;
  LOG.info("Zookeeper Minicluster service started on client port: " + clientPort);
  return zooKeeperServer;
}
 
Example 18
Source File: KafkaEmbeddedRule.java    From devicehive-java-server with Apache License 2.0 5 votes vote down vote up
public EmbeddedZookeeperInternal(int port) throws IOException, InterruptedException {
    this.port = port;
    logDir = TestUtils.tempDir();
    snapshotDir = TestUtils.tempDir();
    zooKeeperServer = new ZooKeeperServer(snapshotDir, logDir, 500);
    nioServerCnxnFactory = new NIOServerCnxnFactory();
    InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", port);
    nioServerCnxnFactory.configure(inetSocketAddress, 0);
    nioServerCnxnFactory.startup(zooKeeperServer);
}
 
Example 19
Source File: EmbeddedZooKeeper.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
private void start(InetSocketAddress addr) throws IOException, InterruptedException {
    factory = new NIOServerCnxnFactory();
    factory.configure(addr, 20);
    factory.startup(zk);
}
 
Example 20
Source File: ZookeeperServerCnxnFactory.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public ZookeeperServerCnxnFactory(int port, int maxClientCnxns) {
    // port range
    int max;
    if (port <= 0) {
        _port = 2000;
        max = 65535;
    } else {
        _port = port;
        max = port;
    }

    try {
        _factory = new NIOServerCnxnFactory();
    } catch (IOException e) {
        _port = 0;
        _factory = null;
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }

    // look for available port
    for (; _port <= max; _port++) {
        try {
            _factory.configure(new InetSocketAddress(_port), maxClientCnxns);
            LOG.debug("Zookeeper server successfully binded at port " + _port);
            break;
        } catch (BindException e1) {
        } catch (IOException e2) {
            _port = 0;
            _factory = null;
            e2.printStackTrace();
            throw new RuntimeException(e2.getMessage());
        }
    }

    if (_port > max) {
        _port = 0;
        _factory = null;
        LOG.error("Failed to find a port for Zookeeper");
        throw new RuntimeException("No port is available to launch an inprocess zookeeper.");
    }
}