use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class CompactorTool method main.
public static void main(String[] args) throws Exception {
Arguments arguments = new Arguments();
JCommander jcommander = new JCommander(arguments);
jcommander.setProgramName("PulsarTopicCompactor");
// parse args by JCommander
jcommander.parse(args);
if (arguments.help) {
jcommander.usage();
System.exit(-1);
}
// init broker config
ServiceConfiguration brokerConfig;
if (isBlank(arguments.brokerConfigFile)) {
jcommander.usage();
throw new IllegalArgumentException("Need to specify a configuration file for broker");
} else {
brokerConfig = PulsarConfigurationLoader.create(arguments.brokerConfigFile, ServiceConfiguration.class);
}
String pulsarServiceUrl = PulsarService.brokerUrl(brokerConfig);
ClientConfiguration clientConfig = new ClientConfiguration();
if (isNotBlank(brokerConfig.getBrokerClientAuthenticationPlugin())) {
clientConfig.setAuthentication(brokerConfig.getBrokerClientAuthenticationPlugin(), brokerConfig.getBrokerClientAuthenticationParameters());
}
clientConfig.setUseTls(brokerConfig.isTlsEnabled());
clientConfig.setTlsAllowInsecureConnection(brokerConfig.isTlsAllowInsecureConnection());
clientConfig.setTlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());
OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);
ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(), ZooKeeperClientFactory.SessionType.ReadWrite, (int) brokerConfig.getZooKeeperSessionTimeoutMillis()).get();
BookKeeperClientFactory bkClientFactory = new BookKeeperClientFactoryImpl();
BookKeeper bk = bkClientFactory.create(brokerConfig, zk);
try (PulsarClient pulsar = PulsarClient.create(pulsarServiceUrl, clientConfig)) {
Compactor compactor = new TwoPhaseCompactor(brokerConfig, pulsar, bk, scheduler);
long ledgerId = compactor.compact(arguments.topic).get();
log.info("Compaction of topic {} complete. Compacted to ledger {}", arguments.topic, ledgerId);
} finally {
bk.close();
bkClientFactory.close();
zk.close();
scheduler.shutdownNow();
executor.shutdown();
}
}
use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class PulsarClusterUtils method waitBrokerState.
private static boolean waitBrokerState(DockerClient docker, String containerId, int timeout, TimeUnit timeoutUnit, boolean upOrDown) {
long timeoutMillis = timeoutUnit.toMillis(timeout);
long pollMillis = 1000;
String brokerId = DockerUtils.getContainerHostname(docker, containerId) + ":" + BROKER_PORT;
Optional<String> containerCluster = DockerUtils.getContainerCluster(docker, containerId);
if (!containerCluster.isPresent()) {
LOG.error("Unable to determine cluster for container {}. Missing label?", containerId);
return false;
}
ZooKeeper zk = null;
try {
zk = zookeeperClient(docker, containerCluster.get());
String path = "/loadbalance/brokers/" + brokerId;
while (timeoutMillis > 0) {
if ((zk.exists(path, false) != null) == upOrDown) {
return true;
}
Thread.sleep(pollMillis);
timeoutMillis -= pollMillis;
}
} catch (Exception e) {
LOG.error("Exception checking for broker state", e);
return false;
} finally {
try {
if (zk != null) {
zk.close();
}
} catch (Exception e) {
LOG.error("Exception closing zookeeper client", e);
return false;
}
}
LOG.warn("Broker {} didn't go {} after {} seconds", containerId, upOrDown ? "up" : "down", timeoutUnit.toSeconds(timeout));
return false;
}
use of org.apache.zookeeper.ZooKeeper in project ecf by eclipse.
the class WriteRoot method connect.
private void connect() {
synchronized (connectionLock) {
if (this.isConnected || watchManager.isDisposed()) {
return;
}
try {
if (writeKeeper != null) {
writeKeeper.close();
watchManager.removeZooKeeper(writeKeeper);
writeKeeper = null;
}
writeKeeper = new ZooKeeper(this.ip, 3000, this);
} catch (Exception e) {
Logger.log(LogService.LOG_DEBUG, e.getMessage(), e);
}
}
}
use of org.apache.zookeeper.ZooKeeper in project ecf by eclipse.
the class WriteRoot method initWriteKeeper.
private void initWriteKeeper() {
try {
if (watchManager.getConfig().isQuorum() || watchManager.getConfig().isStandAlone()) {
// we write nodes locally but we should check for client port.
int port = watchManager.getConfig().getClientPort();
if (port != 0)
// $NON-NLS-1$
ip += ":" + port;
} else if (watchManager.getConfig().isCentralized()) {
// we write nodes to the machine with this specified IP address.
ip = watchManager.getConfig().getServerIps();
}
try {
writeKeeper = new ZooKeeper(this.ip, 3000, this);
} catch (Exception e) {
// FATAL
Logger.log(LogService.LOG_ERROR, "Fatal error while initializing a zookeeper client to write to: " + ip, e);
// Publisher.publish()
throw new IllegalStateException(e);
}
while (!this.isConnected) {
synchronized (connectionLock) {
if (watchManager.isDisposed()) {
// no need for connecting, we're disposed.
try {
writeKeeper.close();
} catch (Throwable t) {
// ignore
}
break;
}
try {
Stat s = this.writeKeeper.exists(INode.ROOT, this);
this.isConnected = true;
if (s == null) {
writeKeeper.create(INode.ROOT, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} catch (KeeperException e) {
if (e.code().equals(KeeperException.Code.CONNECTIONLOSS)) {
isConnected = false;
PrettyPrinter.attemptingConnectionTo(this.ip);
} else
Logger.log(LogService.LOG_ERROR, "Error while trying to connect to " + this.ip, // $NON-NLS-1$
e);
}
}
}
synchronized (this) {
this.notifyAll();
}
} catch (Exception e) {
Logger.log(LogService.LOG_DEBUG, e.getMessage(), e);
}
}
use of org.apache.zookeeper.ZooKeeper in project helix by apache.
the class ZkTestHelper method expireSession.
public static void expireSession(final ZkClient zkClient) throws Exception {
final CountDownLatch waitNewSession = new CountDownLatch(1);
IZkStateListener listener = new IZkStateListener() {
@Override
public void handleStateChanged(KeeperState state) throws Exception {
LOG.info("IZkStateListener#handleStateChanged, state: " + state);
}
@Override
public void handleNewSession() throws Exception {
// make sure zkclient is connected again
zkClient.waitUntilConnected();
ZkConnection connection = ((ZkConnection) zkClient.getConnection());
ZooKeeper curZookeeper = connection.getZookeeper();
LOG.info("handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
waitNewSession.countDown();
}
@Override
public void handleSessionEstablishmentError(Throwable var1) throws Exception {
}
};
zkClient.subscribeStateChanges(listener);
ZkConnection connection = ((ZkConnection) zkClient.getConnection());
ZooKeeper curZookeeper = connection.getZookeeper();
String oldSessionId = Long.toHexString(curZookeeper.getSessionId());
LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper);
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
LOG.info("Watcher#process, event: " + event);
}
};
final ZooKeeper dupZookeeper = new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher, curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
// wait until connected, then close
while (dupZookeeper.getState() != States.CONNECTED) {
Thread.sleep(10);
}
Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED, "Fail to connect to zk using current session info");
dupZookeeper.close();
// make sure session expiry really happens
waitNewSession.await();
zkClient.unsubscribeStateChanges(listener);
connection = (ZkConnection) zkClient.getConnection();
curZookeeper = connection.getZookeeper();
String newSessionId = Long.toHexString(curZookeeper.getSessionId());
LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper);
Assert.assertNotSame(newSessionId, oldSessionId, "Fail to expire current session, zk: " + curZookeeper);
}
Aggregations