use of org.apache.zookeeper.ZooKeeper in project herddb by diennea.
the class ZookeeperClientSideMetadataProvider method getServerHostData.
@Override
public ServerHostData getServerHostData(String nodeId) throws ClientSideMetadataProviderException {
ServerHostData cached = servers.get(nodeId);
if (cached != null) {
return cached;
}
ZooKeeper zooKeeper = getZooKeeper();
try {
for (int i = 0; i < MAX_TRIALS; i++) {
try {
Stat stat = new Stat();
byte[] node = zooKeeper.getData(basePath + "/nodes/" + nodeId, null, stat);
NodeMetadata nodeMetadata = NodeMetadata.deserialize(node, stat.getVersion());
ServerHostData result = new ServerHostData(nodeMetadata.host, nodeMetadata.port, "?", nodeMetadata.ssl, new HashMap<>());
servers.put(nodeId, result);
return result;
} catch (KeeperException.NoNodeException ex) {
return null;
} catch (KeeperException.ConnectionLossException ex) {
LOG.log(Level.SEVERE, "tmp error getServerHostData for " + nodeId + ": " + ex);
try {
Thread.sleep(i * 500 + 1000);
} catch (InterruptedException exit) {
throw new ClientSideMetadataProviderException(exit);
}
} catch (KeeperException | InterruptedException | IOException ex) {
throw new ClientSideMetadataProviderException(ex);
} finally {
if (ownZooKeeper) {
try {
zooKeeper.close();
} catch (InterruptedException ex) {
throw new ClientSideMetadataProviderException(ex);
}
}
}
}
} finally {
if (ownZooKeeper) {
try {
zooKeeper.close();
} catch (InterruptedException ex) {
throw new ClientSideMetadataProviderException(ex);
}
}
}
throw new ClientSideMetadataProviderException("Could not find a server info for node " + nodeId + " in time");
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class ZKMetadataDriverBase method initialize.
@SneakyThrows(InterruptedException.class)
protected void initialize(AbstractConfiguration<?> conf, StatsLogger statsLogger, RetryPolicy zkRetryPolicy, Optional<Object> optionalCtx) throws MetadataException {
this.conf = conf;
this.acls = ZkUtils.getACLs(conf);
if (optionalCtx.isPresent() && optionalCtx.get() instanceof ZooKeeper) {
this.ledgersRootPath = conf.getZkLedgersRootPath();
log.info("Initialize zookeeper metadata driver with external zookeeper client : ledgersRootPath = {}.", ledgersRootPath);
// if an external zookeeper is added, use the zookeeper instance
this.zk = (ZooKeeper) (optionalCtx.get());
this.ownZKHandle = false;
} else {
final String metadataServiceUriStr;
try {
metadataServiceUriStr = conf.getMetadataServiceUri();
} catch (ConfigurationException e) {
log.error("Failed to retrieve metadata service uri from configuration", e);
throw new MetadataException(Code.INVALID_METADATA_SERVICE_URI, e);
}
URI metadataServiceUri = URI.create(metadataServiceUriStr);
// get the initialize root path
this.ledgersRootPath = metadataServiceUri.getPath();
final String bookieRegistrationPath = ledgersRootPath + "/" + AVAILABLE_NODE;
final String bookieReadonlyRegistrationPath = bookieRegistrationPath + "/" + READONLY;
// construct the zookeeper
final String zkServers = getZKServersFromServiceUri(metadataServiceUri);
log.info("Initialize zookeeper metadata driver at metadata service uri {} :" + " zkServers = {}, ledgersRootPath = {}.", metadataServiceUriStr, zkServers, ledgersRootPath);
try {
this.zk = ZooKeeperClient.newBuilder().connectString(zkServers).sessionTimeoutMs(conf.getZkTimeout()).operationRetryPolicy(zkRetryPolicy).requestRateLimit(conf.getZkRequestRateLimit()).statsLogger(statsLogger).build();
if (null == zk.exists(bookieReadonlyRegistrationPath, false)) {
try {
zk.create(bookieReadonlyRegistrationPath, EMPTY_BYTE_ARRAY, acls, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException e) {
// this node is just now created by someone.
} catch (KeeperException.NoNodeException e) {
// the cluster hasn't been initialized
}
}
} catch (IOException | KeeperException e) {
log.error("Failed to create zookeeper client to {}", zkServers, e);
MetadataException me = new MetadataException(Code.METADATA_SERVICE_ERROR, "Failed to create zookeeper client to " + zkServers, e);
me.fillInStackTrace();
throw me;
}
this.ownZKHandle = true;
}
// once created the zookeeper client, create the layout manager and registration client
this.layoutManager = new ZkLayoutManager(zk, ledgersRootPath, acls);
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class ZKLogStreamMetadataStore method getMissingPaths.
@VisibleForTesting
static CompletableFuture<List<String>> getMissingPaths(ZooKeeperClient zkc, URI uri, String logName) {
ZooKeeper zk;
try {
zk = zkc.get();
} catch (ZooKeeperConnectionException | InterruptedException e) {
return FutureUtils.exception(e);
}
String basePath = uri.getPath();
String logStreamPath = LogMetadata.getLogStreamPath(uri, logName);
return getMissingPaths(zk, basePath, logStreamPath);
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class ZKLogStreamMetadataStore method logExists.
@Override
public CompletableFuture<Void> logExists(URI uri, final String logName) {
final String logSegmentsPath = LogMetadata.getLogSegmentsPath(uri, logName, conf.getUnpartitionedStreamName());
final CompletableFuture<Void> promise = new CompletableFuture<Void>();
try {
final ZooKeeper zk = zooKeeperClient.get();
zk.sync(logSegmentsPath, new AsyncCallback.VoidCallback() {
@Override
public void processResult(int syncRc, String path, Object syncCtx) {
if (KeeperException.Code.NONODE.intValue() == syncRc) {
promise.completeExceptionally(new LogNotFoundException(String.format("Log %s does not exist or has been deleted", logName)));
return;
} else if (KeeperException.Code.OK.intValue() != syncRc) {
promise.completeExceptionally(new ZKException("Error on checking log existence for " + logName, KeeperException.create(KeeperException.Code.get(syncRc))));
return;
}
zk.exists(logSegmentsPath, false, new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
if (KeeperException.Code.OK.intValue() == rc) {
promise.complete(null);
} else if (KeeperException.Code.NONODE.intValue() == rc) {
promise.completeExceptionally(new LogNotFoundException(String.format("Log %s does not exist or has been deleted", logName)));
} else {
promise.completeExceptionally(new ZKException("Error on checking log existence for " + logName, KeeperException.create(KeeperException.Code.get(rc))));
}
}
}, null);
}
}, null);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
LOG.error("Interrupted while reading {}", logSegmentsPath, ie);
promise.completeExceptionally(new DLInterruptedException("Interrupted while checking " + logSegmentsPath, ie));
} catch (ZooKeeperClient.ZooKeeperConnectionException e) {
promise.completeExceptionally(e);
}
return promise;
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class Utils method zkDeleteIfNotExist.
/**
* Delete the given <i>path</i> from zookeeper.
*
* @param zkc
* zookeeper client
* @param path
* path to delete
* @param version
* version used to set data
* @return future representing if the delete is successful. Return true if the node is deleted,
* false if the node doesn't exist, otherwise future will throw exception
*/
public static CompletableFuture<Boolean> zkDeleteIfNotExist(ZooKeeperClient zkc, String path, LongVersion version) {
ZooKeeper zk;
try {
zk = zkc.get();
} catch (ZooKeeperClient.ZooKeeperConnectionException e) {
return FutureUtils.exception(zkException(e, path));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return FutureUtils.exception(zkException(e, path));
}
final CompletableFuture<Boolean> promise = new CompletableFuture<Boolean>();
zk.delete(path, (int) version.getLongVersion(), new AsyncCallback.VoidCallback() {
@Override
public void processResult(int rc, String path, Object ctx) {
if (KeeperException.Code.OK.intValue() == rc) {
promise.complete(true);
} else if (KeeperException.Code.NONODE.intValue() == rc) {
promise.complete(false);
} else {
promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
}
}
}, null);
return promise;
}
Aggregations