Search in sources :

Example 1 with POLICIES_ROOT

use of com.yahoo.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT in project pulsar by yahoo.

the class LocalZooKeeperCacheService method createPolicies.

/**
     * Create LocalPolicies with bundle-data in LocalZookeeper by fetching it from GlobalZookeeper
     *
     * @param path
     *            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()) {
                LocalPolicies localPolicies = new LocalPolicies();
                localPolicies.bundles = policies.get().bundles;
                readFromGlobalFuture.complete(Optional.of(localPolicies));
            } else {
                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());
        }
        byte[] content;
        try {
            content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(localPolicies.get());
        } catch (Throwable t) {
            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;
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ZooKeeper(org.apache.zookeeper.ZooKeeper) Logger(org.slf4j.Logger) ZooKeeperDataCache(com.yahoo.pulsar.zookeeper.ZooKeeperDataCache) KeeperException(org.apache.zookeeper.KeeperException) Ids(org.apache.zookeeper.ZooDefs.Ids) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ZooKeeperChildrenCache(com.yahoo.pulsar.zookeeper.ZooKeeperChildrenCache) Policies(com.yahoo.pulsar.common.policies.data.Policies) ZooKeeperCache(com.yahoo.pulsar.zookeeper.ZooKeeperCache) ZkUtils(org.apache.bookkeeper.util.ZkUtils) ObjectMapperFactory(com.yahoo.pulsar.common.util.ObjectMapperFactory) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) POLICIES_ROOT(com.yahoo.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT) Optional(java.util.Optional) LocalPolicies(com.yahoo.pulsar.common.policies.data.LocalPolicies) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) NamespaceEphemeralData(com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData) PulsarWebResource.joinPath(com.yahoo.pulsar.broker.web.PulsarWebResource.joinPath) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) LocalPolicies(com.yahoo.pulsar.common.policies.data.LocalPolicies)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)1 POLICIES_ROOT (com.yahoo.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT)1 NamespaceEphemeralData (com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData)1 PulsarWebResource.joinPath (com.yahoo.pulsar.broker.web.PulsarWebResource.joinPath)1 LocalPolicies (com.yahoo.pulsar.common.policies.data.LocalPolicies)1 Policies (com.yahoo.pulsar.common.policies.data.Policies)1 ObjectMapperFactory (com.yahoo.pulsar.common.util.ObjectMapperFactory)1 ZooKeeperCache (com.yahoo.pulsar.zookeeper.ZooKeeperCache)1 ZooKeeperChildrenCache (com.yahoo.pulsar.zookeeper.ZooKeeperChildrenCache)1 ZooKeeperDataCache (com.yahoo.pulsar.zookeeper.ZooKeeperDataCache)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ZkUtils (org.apache.bookkeeper.util.ZkUtils)1 CreateMode (org.apache.zookeeper.CreateMode)1 KeeperException (org.apache.zookeeper.KeeperException)1 Ids (org.apache.zookeeper.ZooDefs.Ids)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 Logger (org.slf4j.Logger)1