use of com.yahoo.pulsar.common.policies.data.LocalPolicies 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;
}
Aggregations