Example usage for org.apache.zookeeper KeeperException create

List of usage examples for org.apache.zookeeper KeeperException create

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException create.

Prototype

public static KeeperException create(Code code) 

Source Link

Document

All non-specific keeper exceptions should be constructed via this factory method in order to guarantee consistency in error codes and such.

Usage

From source file:org.apache.pulsar.broker.service.schema.BookkeeperSchemaStorage.java

License:Apache License

@NotNull
private CompletableFuture<Void> updateSchemaLocator(String id, SchemaStorageFormat.SchemaLocator schema,
        int version) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    zooKeeper.setData(id, schema.toByteArray(), version, (rc, path, ctx, stat) -> {
        Code code = Code.get(rc);/*from w  ww .j  a  v a2 s .  c  o  m*/
        if (code != Code.OK) {
            future.completeExceptionally(KeeperException.create(code));
        } else {
            future.complete(null);
        }
    }, null);
    return future;
}

From source file:org.apache.pulsar.broker.service.schema.BookkeeperSchemaStorage.java

License:Apache License

@NotNull
private CompletableFuture<LocatorEntry> createSchemaLocator(String id,
        SchemaStorageFormat.SchemaLocator locator) {
    CompletableFuture<LocatorEntry> future = new CompletableFuture<>();

    ZkUtils.asyncCreateFullPathOptimistic(zooKeeper, id, locator.toByteArray(), Acl, CreateMode.PERSISTENT,
            (rc, path, ctx, name) -> {
                Code code = Code.get(rc);
                if (code != Code.OK) {
                    future.completeExceptionally(KeeperException.create(code));
                } else {
                    // Newly created z-node will have version 0
                    future.complete(new LocatorEntry(locator, 0));
                }/*  w  w  w.j  ava  2 s .c o  m*/
            }, null);

    return future;
}

From source file:org.apache.pulsar.zookeeper.ZooKeeperCache.java

License:Apache License

@SuppressWarnings({ "unchecked", "deprecation" })
public <T> CompletableFuture<Optional<Entry<T, Stat>>> getDataAsync(final String path, final Watcher watcher,
        final Deserializer<T> deserializer) {
    checkNotNull(path);/* w w w. ja v  a  2 s.c  o m*/
    checkNotNull(deserializer);

    CompletableFuture<Optional<Entry<T, Stat>>> future = new CompletableFuture<>();
    dataCache.get(path, (p, executor) -> {
        // Return a future for the z-node to be fetched from ZK
        CompletableFuture<Entry<Object, Stat>> zkFuture = new CompletableFuture<>();

        // Broker doesn't restart on global-zk session lost: so handling unexpected exception
        try {
            this.zkSession.get().getData(path, watcher, (rc, path1, ctx, content, stat) -> {
                Executor exec = scheduledExecutor != null ? scheduledExecutor : executor;
                if (rc == Code.OK.intValue()) {
                    try {
                        T obj = deserializer.deserialize(path, content);
                        // avoid using the zk-client thread to process the result
                        exec.execute(
                                () -> zkFuture.complete(new SimpleImmutableEntry<Object, Stat>(obj, stat)));
                    } catch (Exception e) {
                        exec.execute(() -> zkFuture.completeExceptionally(e));
                    }
                } else if (rc == Code.NONODE.intValue()) {
                    // Return null values for missing z-nodes, as this is not "exceptional" condition
                    exec.execute(() -> zkFuture.complete(null));
                } else {
                    exec.execute(() -> zkFuture.completeExceptionally(KeeperException.create(rc)));
                }
            }, null);
        } catch (Exception e) {
            LOG.warn("Failed to access zkSession for {} {}", path, e.getMessage(), e);
            zkFuture.completeExceptionally(e);
        }

        return zkFuture;
    }).thenAccept(result -> {
        if (result != null) {
            future.complete(Optional.of((Entry<T, Stat>) result));
        } else {
            future.complete(Optional.empty());
        }
    }).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });

    return future;
}