Example usage for org.apache.zookeeper KeeperException code

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

Introduction

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

Prototype

Code code

To view the source code for org.apache.zookeeper KeeperException code.

Click Source Link

Usage

From source file:com.yahoo.pulsar.broker.cache.LocalZooKeeperCacheService.java

License:Apache License

/**
 * Create LocalPolicies with bundle-data in LocalZookeeper by fetching it from GlobalZookeeper
 *
 * @param path//from w ww . j ava2  s .  com
 *            znode path
 * @param readFromGlobal
 *            if true copy policies from global zk to local zk else create a new znode with empty {@link Policies}
 * @throws Exception
 */
@SuppressWarnings("deprecation")
public CompletableFuture<Optional<LocalPolicies>> createPolicies(String path, boolean readFromGlobal) {
    checkNotNull(path, "path can't be null");
    checkArgument(path.startsWith(LOCAL_POLICIES_ROOT), "Invalid path of local policies");

    CompletableFuture<Optional<LocalPolicies>> future = new CompletableFuture<>();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating local namespace policies for {} - readFromGlobal: {}", path, readFromGlobal);
    }

    CompletableFuture<Optional<LocalPolicies>> readFromGlobalFuture = new CompletableFuture<>();

    if (readFromGlobal) {
        String globalPath = joinPath(POLICIES_ROOT,
                path.substring(path.indexOf(LOCAL_POLICIES_ROOT) + LOCAL_POLICIES_ROOT.length() + 1));
        checkNotNull(configurationCacheService);
        checkNotNull(configurationCacheService.policiesCache());
        checkNotNull(configurationCacheService.policiesCache().getAsync(globalPath));
        configurationCacheService.policiesCache().getAsync(globalPath).thenAccept(policies -> {
            if (policies.isPresent()) {
                // Copying global bundles information to local policies
                LocalPolicies localPolicies = new LocalPolicies();
                localPolicies.bundles = policies.get().bundles;
                readFromGlobalFuture.complete(Optional.of(localPolicies));
            } else {
                // Policies are not present in global zk
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Global policies not found at {}", globalPath);
                }
                readFromGlobalFuture.complete(Optional.empty());
            }
        }).exceptionally(ex -> {
            future.completeExceptionally(ex);
            return null;
        });
    } else {
        // Use default local policies
        readFromGlobalFuture.complete(Optional.of(new LocalPolicies()));
    }

    readFromGlobalFuture.thenAccept(localPolicies -> {
        if (!localPolicies.isPresent()) {
            future.complete(Optional.empty());
        }

        // When we have the updated localPolicies, we can write them back in local ZK
        byte[] content;
        try {
            content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(localPolicies.get());
        } catch (Throwable t) {
            // Failed to serialize to json
            future.completeExceptionally(t);
            return;
        }

        ZkUtils.asyncCreateFullPathOptimistic(cache.getZooKeeper(), path, content, Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT, (rc, path1, ctx, name) -> {
                    if (rc == KeeperException.Code.OK.intValue()
                            || rc == KeeperException.Code.NODEEXISTS.intValue()) {
                        LOG.info("Successfully copyied bundles data to local zk at {}", path);
                        future.complete(localPolicies);
                    } else {
                        LOG.error("Failed to create policies for {} in local zookeeper: {}", path,
                                KeeperException.Code.get(rc));
                        future.completeExceptionally(new PulsarServerException(KeeperException.create(rc)));
                    }
                }, null);
    }).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });

    return future;
}

From source file:com.yahoo.pulsar.broker.namespace.NamespaceService.java

License:Apache License

/**
 * 1. split the given bundle into two bundles 2. assign ownership of both the bundles to current broker 3. update
 * policies with newly created bundles into LocalZK 4. disable original bundle and refresh the cache
 *
 * @param bundle//from   w  w w. j ava2  s . c o m
 * @return
 * @throws Exception
 */
public CompletableFuture<Void> splitAndOwnBundle(NamespaceBundle bundle) throws Exception {

    final CompletableFuture<Void> future = new CompletableFuture<>();

    Pair<NamespaceBundles, List<NamespaceBundle>> splittedBundles = bundleFactory.splitBundles(bundle,
            2 /* by default split into 2 */);
    if (splittedBundles != null) {
        checkNotNull(splittedBundles.getLeft());
        checkNotNull(splittedBundles.getRight());
        checkArgument(splittedBundles.getRight().size() == 2, "bundle has to be split in two bundles");
        NamespaceName nsname = bundle.getNamespaceObject();
        try {
            // take ownership of newly split bundles
            for (NamespaceBundle sBundle : splittedBundles.getRight()) {
                checkNotNull(ownershipCache.tryAcquiringOwnership(sBundle));
            }
            updateNamespaceBundles(nsname, splittedBundles.getLeft(),
                    (rc, path, zkCtx, stat) -> pulsar.getOrderedExecutor().submit(safeRun(() -> {
                        if (rc == KeeperException.Code.OK.intValue()) {
                            // disable old bundle
                            try {
                                ownershipCache.disableOwnership(bundle);
                                // invalidate cache as zookeeper has new split
                                // namespace bundle
                                bundleFactory.invalidateBundleCache(nsname);
                                // update bundled_topic cache for load-report-generation
                                pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
                                loadManager.setLoadReportForceUpdateFlag();
                                future.complete(null);
                            } catch (Exception e) {
                                String msg1 = format(
                                        "failed to disable bundle %s under namespace [%s] with error %s",
                                        nsname.toString(), bundle.toString(), e.getMessage());
                                LOG.warn(msg1, e);
                                future.completeExceptionally(new ServiceUnitNotReadyException(msg1));
                            }
                        } else {
                            String msg2 = format("failed to update namespace [%s] policies due to %s",
                                    nsname.toString(),
                                    KeeperException.create(KeeperException.Code.get(rc)).getMessage());
                            LOG.warn(msg2);
                            future.completeExceptionally(new ServiceUnitNotReadyException(msg2));
                        }
                    })));
        } catch (Exception e) {
            String msg = format("failed to aquire ownership of split bundle for namespace [%s], %s",
                    nsname.toString(), e.getMessage());
            LOG.warn(msg, e);
            future.completeExceptionally(new ServiceUnitNotReadyException(msg));
        }

    } else {
        String msg = format("bundle %s not found under namespace", bundle.toString());
        future.completeExceptionally(new ServiceUnitNotReadyException(msg));
    }
    return future;
}

From source file:com.yahoo.pulsar.broker.namespace.OwnershipCacheTest.java

License:Apache License

@Test
public void testGetOwner() throws Exception {
    OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
    NamespaceBundle testBundle = bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-3"));
    // case 1: no one owns the namespace
    assertFalse(cache.getOwnerAsync(testBundle).get().isPresent());
    // case 2: someone owns the namespace
    ServiceUnitZkUtils.acquireNameSpace(zkCache.getZooKeeper(), ServiceUnitZkUtils.path(testBundle),
            new NamespaceEphemeralData("pulsar://otherhost:8881", "pulsar://otherhost:8884",
                    "http://otherhost:8080", "https://otherhost:4443", false));

    // try to acquire, which will load the read-only cache
    NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(testBundle).get();

    assertEquals(data1.getNativeUrl(), "pulsar://otherhost:8881");
    assertEquals(data1.getNativeUrlTls(), "pulsar://otherhost:8884");
    assertTrue(!data1.isDisabled());//from   w w  w  . j a v a 2s .  c  o  m
    // Now do getOwner and compare w/ the returned values
    NamespaceEphemeralData readOnlyData = cache.getOwnerAsync(testBundle).get().get();
    assertEquals(data1, readOnlyData);

    MockZooKeeper mockZk = (MockZooKeeper) zkCache.getZooKeeper();
    mockZk.failNow(KeeperException.Code.NONODE);
    Optional<NamespaceEphemeralData> res = cache
            .getOwnerAsync(bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-none"))).get();
    assertFalse(res.isPresent());
}

From source file:com.yahoo.pulsar.zookeeper.ZookeeperClientFactoryImpl.java

License:Apache License

@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType,
        int zkSessionTimeoutMillis) {
    // Create a normal ZK client
    boolean canBeReadOnly = sessionType == SessionType.AllowReadOnly;

    CompletableFuture<ZooKeeper> future = new CompletableFuture<>();
    try {//from   w  ww  . j a v  a 2  s.  co  m
        CompletableFuture<Void> internalFuture = new CompletableFuture<>();

        ZooKeeper zk = new ZooKeeper(serverList, zkSessionTimeoutMillis, event -> {
            if (event.getType() == Event.EventType.None) {
                switch (event.getState()) {

                case ConnectedReadOnly:
                    checkArgument(canBeReadOnly);
                    // Fall through
                case SyncConnected:
                    // ZK session is ready to use
                    internalFuture.complete(null);
                    break;

                case Expired:
                    internalFuture
                            .completeExceptionally(KeeperException.create(KeeperException.Code.SESSIONEXPIRED));
                    break;

                default:
                    log.warn("Unexpected ZK event received: {}", event);
                    break;
                }
            }
        }, canBeReadOnly);

        internalFuture.thenRun(() -> {
            log.info("ZooKeeper session established: {}", zk);
            future.complete(zk);
        }).exceptionally((exception) -> {
            log.error("Failed to establish ZooKeeper session: {}", exception.getMessage());
            future.completeExceptionally(exception);
            return null;
        });

    } catch (IOException e) {
        future.completeExceptionally(e);
    }

    return future;
}

From source file:com.yahoo.pulsar.zookeeper.ZooKeeperSessionWatcher.java

License:Apache License

@Override
public synchronized void processResult(int rc, String path, Object ctx, Stat stat) {
    switch (KeeperException.Code.get(rc)) {
    case CONNECTIONLOSS:
        keeperState = Watcher.Event.KeeperState.Disconnected;
        break;//from w w  w  .j  a v  a2  s .  co m
    case SESSIONEXPIRED:
        keeperState = Watcher.Event.KeeperState.Expired;
        break;
    case OK:
    default:
        keeperState = Watcher.Event.KeeperState.SyncConnected;
    }
    zkOperationCompleted = true;
    this.notify();
}

From source file:com.zookeeper.web.inspector.manager.ZooInspectorManagerImpl.java

License:Apache License

Item getChildrenAndStat(String nodePath) throws KeeperException {
    // System.out.println("getChildrenAndStat(), path: " + nodePath);
    if (zooKeeper.getState() != States.CONNECTED) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS, nodePath);
    }//from   www.  j  a  v a  2  s .co  m

    try {
        Stat stat = new Stat();
        List<String> childs = zooKeeper.getChildren(nodePath, false, stat);
        return new Item(childs, stat);
    } catch (NoNodeException e) {
        // OK to return null
    } catch (KeeperException e) {
        throw e;
    } catch (Exception e) {
        LoggerFactory.getLogger().error("Error occurred retrieving children of node: " + nodePath, e);
    }
    return null;
}

From source file:dmg.cells.zookeeper.PathChildrenCache.java

License:Apache License

void refresh(RefreshMode mode) throws Exception {
    final BackgroundCallback callback = (client, event) -> {
        if (PathChildrenCache.this.state.get() != State.CLOSED) {
            if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
                processChildren(event.getChildren(), mode);
            } else if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) {
                ensurePathAndThenRefresh(mode);
            } else if (event.getResultCode() == KeeperException.Code.CONNECTIONLOSS.intValue()
                    || event.getResultCode() == KeeperException.Code.SESSIONEXPIRED.intValue()) {
                log.debug("Refresh callback ignored {}", event);
            } else {
                handleException(KeeperException.create(event.getResultCode()));
            }//from  w ww  . j  a va2s  .  c o m
        }
    };
    client.getChildren().usingWatcher(childrenWatcher).inBackground(callback).forPath(path);
}

From source file:dmg.cells.zookeeper.PathChildrenCache.java

License:Apache License

void ensurePathAndThenRefresh(RefreshMode mode) throws Exception {
    BackgroundCallback callback = (client, event) -> {
        if (event.getResultCode() == KeeperException.Code.OK.intValue()
                || event.getResultCode() == KeeperException.Code.NONODE.intValue()) {
            refresh(mode);/*w  w w .ja  va  2  s .c  o m*/
        }
    };
    client.checkExists().creatingParentContainersIfNeeded().inBackground(callback)
            .forPath(ZKPaths.makePath(path, "ignored"));
}

From source file:dmg.cells.zookeeper.PathChildrenCache.java

License:Apache License

void getDataAndStat(String fullPath) throws Exception {
    BackgroundCallback callback = (client, event) -> {
        if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
            updateCache(fullPath, event.getStat(), cacheData ? event.getData() : null);
        }// w ww .  jav  a2s.c  o  m
    };

    if (dataIsCompressed && cacheData) {
        client.getData().decompressed().usingWatcher(dataWatcher).inBackground(callback).forPath(fullPath);
    } else {
        client.getData().usingWatcher(dataWatcher).inBackground(callback).forPath(fullPath);
    }
}

From source file:fr.eurecom.hybris.mds.MdsManager.java

License:Apache License

/**
 * Constructs a new MdsManager.//from  www  .  j a v a  2  s .c o  m
 * @param zkConnectionStr Zookeeper cluster connection string (e.g. "zksrv1.net:2181,zksrv2.net:2181")
 * @param zkRoot the Hybris metadata root folder
 * @throws IOException thrown in case of error while initializing the Zookeeper client
 */
public MdsManager(String zkConnectionStr, String zkRoot) throws IOException {

    this.storageRoot = "/" + zkRoot;

    this.gcRoot = this.storageRoot + "-gc";
    this.gcStaleDir = this.gcRoot + "/stale";
    this.gcOrphansDir = this.gcRoot + "/orphans";

    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        this.zkCli = CuratorFrameworkFactory.newClient(zkConnectionStr, retryPolicy);
        this.zkCli.getConnectionStateListenable().addListener(this);
        this.zkCli.start();

        for (String dir : new String[] { this.storageRoot, this.gcRoot, this.gcStaleDir, this.gcOrphansDir })
            try {
                this.zkCli.create().forPath(dir);
                logger.debug("Created {}.", dir);
            } catch (KeeperException e) {
                if (e.code() != KeeperException.Code.NODEEXISTS)
                    throw e;
            }

    } catch (Exception e) {
        logger.error("Could not initialize the Zookeeper client. " + e.getMessage(), e);
        throw new IOException(e);
    }
}