Search in sources :

Example 46 with ZooKeeper

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");
}
Also used : IOException(java.io.IOException) ServerHostData(herddb.network.ServerHostData) NodeMetadata(herddb.model.NodeMetadata) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException)

Example 47 with ZooKeeper

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);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) URI(java.net.URI) MetadataException(org.apache.bookkeeper.meta.exceptions.MetadataException) KeeperException(org.apache.zookeeper.KeeperException) ZkLayoutManager(org.apache.bookkeeper.meta.ZkLayoutManager) SneakyThrows(lombok.SneakyThrows)

Example 48 with ZooKeeper

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);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 49 with ZooKeeper

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;
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) CompletableFuture(java.util.concurrent.CompletableFuture) ZKException(org.apache.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Example 50 with ZooKeeper

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;
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) CompletableFuture(java.util.concurrent.CompletableFuture) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)605 Test (org.junit.jupiter.api.Test)190 KeeperException (org.apache.zookeeper.KeeperException)153 Test (org.junit.Test)100 Stat (org.apache.zookeeper.data.Stat)86 IOException (java.io.IOException)83 CountDownLatch (java.util.concurrent.CountDownLatch)80 WatchedEvent (org.apache.zookeeper.WatchedEvent)70 Watcher (org.apache.zookeeper.Watcher)59 ArrayList (java.util.ArrayList)47 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)41 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)38 Timeout (org.junit.jupiter.api.Timeout)32 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)31 File (java.io.File)28 HashMap (java.util.HashMap)26 CompletableFuture (java.util.concurrent.CompletableFuture)22 Test (org.testng.annotations.Test)21 AsyncCallback (org.apache.zookeeper.AsyncCallback)19 ACL (org.apache.zookeeper.data.ACL)19