use of org.apache.zookeeper.server.ZooKeeperServer in project hbase by apache.
the class MiniZooKeeperCluster method startup.
/**
* @param baseDir the base directory to use
* @param numZooKeeperServers the number of ZooKeeper servers
* @return ClientPort server bound to, -1 if there was a binding problem and we couldn't pick
* another port.
* @throws IOException if an operation fails during the startup
* @throws InterruptedException if the startup fails
*/
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
if (numZooKeeperServers <= 0) {
return -1;
}
setupTestEnv();
shutdown();
// the seed port
int tentativePort = -1;
int currentClientPort;
// running all the ZK servers
for (int i = 0; i < numZooKeeperServers; i++) {
File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
createDir(dir);
int tickTimeToUse;
if (this.tickTime > 0) {
tickTimeToUse = this.tickTime;
} else {
tickTimeToUse = TICK_TIME;
}
// Set up client port - if we have already had a list of valid ports, use it.
if (hasValidClientPortInList(i)) {
currentClientPort = clientPortList.get(i);
} else {
// update the seed
tentativePort = selectClientPort(tentativePort);
currentClientPort = tentativePort;
}
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
// Setting {min,max}SessionTimeout defaults to be the same as in Zookeeper
server.setMinSessionTimeout(configuration.getInt("hbase.zookeeper.property.minSessionTimeout", -1));
server.setMaxSessionTimeout(configuration.getInt("hbase.zookeeper.property.maxSessionTimeout", -1));
NIOServerCnxnFactory standaloneServerFactory;
while (true) {
try {
standaloneServerFactory = new NIOServerCnxnFactory();
standaloneServerFactory.configure(new InetSocketAddress(LOOPBACK_HOST, currentClientPort), configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, HConstants.DEFAULT_ZOOKEEPER_MAX_CLIENT_CNXNS));
} catch (BindException e) {
LOG.debug("Failed binding ZK Server to client port: " + currentClientPort, e);
// We're told to use some port but it's occupied, fail
if (hasValidClientPortInList(i)) {
return -1;
}
// This port is already in use, try to use another.
tentativePort = selectClientPort(tentativePort);
currentClientPort = tentativePort;
continue;
}
break;
}
// Start up this ZK server. Dump its stats.
standaloneServerFactory.startup(server);
LOG.info("Started connectionTimeout={}, dir={}, {}", connectionTimeout, dir, getServerConfigurationOnOneLine(server));
// Runs a 'stat' against the servers.
if (!waitForServerUp(currentClientPort, connectionTimeout)) {
Threads.printThreadInfo(System.out, "Why is zk standalone server not coming up?");
throw new IOException("Waiting for startup of standalone server; " + "server isRunning=" + server.isRunning());
}
// We have selected a port as a client port. Update clientPortList if necessary.
if (clientPortList.size() <= i) {
// it is not in the list, add the port
clientPortList.add(currentClientPort);
} else if (clientPortList.get(i) <= 0) {
// the list has invalid port, update with valid port
clientPortList.remove(i);
clientPortList.add(i, currentClientPort);
}
standaloneServerFactoryList.add(standaloneServerFactory);
zooKeeperServers.add(server);
}
// set the first one to be active ZK; Others are backups
activeZKServerIndex = 0;
started = true;
int clientPort = clientPortList.get(activeZKServerIndex);
LOG.info("Started MiniZooKeeperCluster and ran 'stat' on client port={}", clientPort);
return clientPort;
}
use of org.apache.zookeeper.server.ZooKeeperServer in project rest.li by linkedin.
the class ZKPeer method killPeerZkServer.
public void killPeerZkServer() {
ZooKeeperServer zserver = _peer.getActiveServer();
try {
_log.info("Killing quorum zk server. Peer #" + getPeerPortsInfo());
Field cnxnFactoryField = ZooKeeperServer.class.getDeclaredField("serverCnxnFactory");
cnxnFactoryField.setAccessible(true);
NIOServerCnxnFactory cnxnFactory = (NIOServerCnxnFactory) cnxnFactoryField.get(zserver);
cnxnFactory.shutdown();
Field ssField = cnxnFactory.getClass().getDeclaredField("ss");
ssField.setAccessible(true);
ServerSocketChannel ss = (ServerSocketChannel) ssField.get(cnxnFactory);
ss.close();
close();
} catch (Exception e) {
// do nothing - this method is only for testing
}
}
use of org.apache.zookeeper.server.ZooKeeperServer in project incubator-atlas by apache.
the class KafkaNotification method startZk.
private String startZk() throws IOException, InterruptedException, URISyntaxException {
String zkValue = properties.getProperty("zookeeper.connect");
LOG.debug("Starting zookeeper at {}", zkValue);
URL zkAddress = getURL(zkValue);
this.factory = NIOServerCnxnFactory.createFactory(new InetSocketAddress(zkAddress.getHost(), zkAddress.getPort()), 1024);
File snapshotDir = constructDir("zk/txn");
File logDir = constructDir("zk/snap");
factory.startup(new ZooKeeperServer(snapshotDir, logDir, 500));
return factory.getLocalAddress().getAddress().toString();
}
use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.
the class ThrottledOpObserverTest method testThrottledOpObserver.
@Test
public void testThrottledOpObserver() throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = null;
try {
zk = createClient("localhost:" + getFirstObserverClientPort());
ZooKeeperServer zs = getFirstObserver().getActiveServer();
ThrottledOpHelper test = new ThrottledOpHelper();
test.testThrottledOp(zk, zs);
} finally {
if (zk != null) {
zk.close();
}
}
}
use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.
the class ThrottledOpStandaloneTest method testThrottledOp.
@Test
public void testThrottledOp() throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = null;
try {
zk = createClient(hostPort);
ZooKeeperServer zs = serverFactory.getZooKeeperServer();
ThrottledOpHelper test = new ThrottledOpHelper();
test.testThrottledOp(zk, zs);
} finally {
if (zk != null) {
zk.close();
}
}
}
Aggregations