Search in sources :

Example 11 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project xian by happyyangyuan.

the class TestingZooKeeperMain method getZooKeeperServer.

private ZooKeeperServer getZooKeeperServer(ServerCnxnFactory cnxnFactory) throws Exception {
    Field zkServerField = ServerCnxnFactory.class.getDeclaredField("zkServer");
    zkServerField.setAccessible(true);
    ZooKeeperServer zkServer;
    // Wait until the zkServer field is non-null or up to 1s, whichever comes first.
    long startTime = System.currentTimeMillis();
    do {
        zkServer = (ZooKeeperServer) zkServerField.get(cnxnFactory);
    } while ((zkServer == null) && ((System.currentTimeMillis() - startTime) < MAX_WAIT_MS));
    return zkServer;
}
Also used : Field(java.lang.reflect.Field) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 12 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project xian by happyyangyuan.

the class TestingZooKeeperMain method blockUntilStarted.

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
@Override
public void blockUntilStarted() throws Exception {
    latch.await();
    ServerCnxnFactory cnxnFactory = getServerConnectionFactory();
    if (cnxnFactory != null) {
        final ZooKeeperServer zkServer = getZooKeeperServer(cnxnFactory);
        if (zkServer != null) {
            synchronized (zkServer) {
                if (!zkServer.isRunning()) {
                    zkServer.wait();
                }
            }
        }
    }
    Thread.sleep(1000);
    Exception exception = startingException.get();
    if (exception != null) {
        throw exception;
    }
}
Also used : ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 13 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project incubator-pulsar by apache.

the class ZooKeeperUtil method startServer.

public void startServer() throws Exception {
    // create a ZooKeeper server(dataDir, dataLogDir, port)
    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);
    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();
    // initialize the zk client with values
    zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 14 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project incubator-pulsar by apache.

the class ZooKeeperServerAspect method zkServerConstructor.

@After("zkServerConstructorPointCut()")
public void zkServerConstructor(JoinPoint joinPoint) throws Throwable {
    // ZooKeeperServer instance was created
    ZooKeeperServer zkServer = (ZooKeeperServer) joinPoint.getThis();
    synchronized (ZooKeeperServerAspect.class) {
        if (metricsRegistered) {
            // We can only register the metrics a single time for the process
            return;
        }
        metricsRegistered = true;
    }
    Gauge.build().name("zookeeper_server_znode_count").help("Number of z-nodes stored").create().setChild(new Gauge.Child() {

        @Override
        public double get() {
            return zkServer.getZKDatabase().getNodeCount();
        }
    }).register();
    Gauge.build().name("zookeeper_server_data_size_bytes").help("Size of all of z-nodes stored (bytes)").create().setChild(new Gauge.Child() {

        @Override
        public double get() {
            return zkServer.getZKDatabase().getDataTree().approximateDataSize();
        }
    }).register();
    Gauge.build().name("zookeeper_server_connections").help("Number of currently opened connections").create().setChild(new Gauge.Child() {

        @Override
        public double get() {
            return zkServer.serverStats().getNumAliveClientConnections();
        }
    }).register();
    Gauge.build().name("zookeeper_server_watches_count").help("Number of watches").create().setChild(new Gauge.Child() {

        @Override
        public double get() {
            return zkServer.getZKDatabase().getDataTree().getWatchCount();
        }
    }).register();
    Gauge.build().name("zookeeper_server_ephemerals_count").help("Number of ephemerals z-nodes").create().setChild(new Gauge.Child() {

        @Override
        public double get() {
            return zkServer.getZKDatabase().getDataTree().getEphemeralsCount();
        }
    }).register();
}
Also used : ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) After(org.aspectj.lang.annotation.After)

Example 15 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project ecf by eclipse.

the class ZooDiscoveryContainer method startStandAlone.

/**
 * Start a ZooKeeer server locally to write nodes to. Implied by
 * {@link IDiscoveryConfig#ZOODISCOVERY_FLAVOR_STANDALONE} configuration.
 *
 * @param conf
 */
void startStandAlone(final Configuration conf) {
    if (ZooDiscoveryContainer.zooKeeperServer != null && ZooDiscoveryContainer.zooKeeperServer.isRunning())
        return;
    else if (ZooDiscoveryContainer.zooKeeperServer != null && !ZooDiscoveryContainer.zooKeeperServer.isRunning())
        try {
            ZooDiscoveryContainer.zooKeeperServer.startup();
            return;
        } catch (Exception e) {
            // $NON-NLS-1$
            Logger.log(LogService.LOG_DEBUG, "Zookeeper server cannot be started! ", e);
        }
    try {
        ZooDiscoveryContainer.zooKeeperServer = new ZooKeeperServer();
        FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(conf.getZookeeperDataFile(), conf.getZookeeperDataFile());
        ZooDiscoveryContainer.zooKeeperServer.setTxnLogFactory(fileTxnSnapLog);
        ZooDiscoveryContainer.zooKeeperServer.setTickTime(conf.getTickTime());
        Factory cnxnFactory = new NIOServerCnxn.Factory(new InetSocketAddress(conf.getClientPort()));
        cnxnFactory.startup(ZooDiscoveryContainer.zooKeeperServer);
    } catch (Exception e) {
        Logger.log(LogService.LOG_ERROR, "Zookeeper server cannot be started! Possibly another instance is already running on the same port. ", e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Factory(org.apache.zookeeper.server.NIOServerCnxn.Factory) IOException(java.io.IOException) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Aggregations

ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)96 File (java.io.File)39 Test (org.junit.jupiter.api.Test)33 ZooKeeper (org.apache.zookeeper.ZooKeeper)31 InetSocketAddress (java.net.InetSocketAddress)28 IOException (java.io.IOException)27 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)26 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)25 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)10 Stat (org.apache.zookeeper.data.Stat)9 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)8 ArrayList (java.util.ArrayList)6 ServerConfig (org.apache.zookeeper.server.ServerConfig)6 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)4 KeeperException (org.apache.zookeeper.KeeperException)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)3 ACL (org.apache.zookeeper.data.ACL)3 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)3