public static NamespaceBundleFactory createFactory(HashFunction hashFunc) { return new NamespaceBundleFactory(null, hashFunc); }
public static final NamespaceBundle suBundleFromPath(String path, NamespaceBundleFactory factory) { String[] parts = path.split("/"); checkArgument(parts.length > 2); checkArgument(parts[1].equals("namespace")); checkArgument(parts.length > 5); Range<Long> range = getHashRange(parts[5]); return factory.getBundle(new NamespaceName(parts[2], parts[3], parts[4]), range); }
public int getBundleCount(NamespaceName namespace) throws Exception { return bundleFactory.getBundles(namespace).size(); }
public void removeOwnedServiceUnits(NamespaceName nsName, BundlesData bundleData) throws Exception { ownershipCache.removeOwnership(bundleFactory.getBundles(nsName, bundleData)).get(cacheTimeOutInSec, SECONDS); bundleFactory.invalidateBundleCache(nsName); }
protected NamespaceBundle validateNamespaceBundleRange(NamespaceName fqnn, BundlesData bundles, String bundleRange) { try { checkArgument(bundleRange.contains("_"), "Invalid bundle range"); String[] boundaries = bundleRange.split("_"); Long lowerEndpoint = Long.decode(boundaries[0]); Long upperEndpoint = Long.decode(boundaries[1]); Range<Long> hashRange = Range.range(lowerEndpoint, BoundType.CLOSED, upperEndpoint, (upperEndpoint.equals(NamespaceBundles.FULL_UPPER_BOUND)) ? BoundType.CLOSED : BoundType.OPEN); NamespaceBundle nsBundle = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundle(fqnn, hashRange); NamespaceBundles nsBundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(fqnn, bundles); nsBundles.validateBundle(nsBundle); return nsBundle; } catch (Exception e) { log.error("[{}] Failed to validate namespace bundle {}/{}", clientAppId(), fqnn.toString(), bundleRange, e); throw new RestException(e); } }
protected Policies getNamespacePolicies(String property, String cluster, String namespace) { try { Policies policies = policiesCache().get(AdminResource.path("policies", property, cluster, namespace)) .orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace does not exist")); // fetch bundles from LocalZK-policies NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory() .getBundles(new NamespaceName(property, cluster, namespace)); BundlesData bundleData = NamespaceBundleFactory.getBundlesData(bundles); policies.bundles = bundleData != null ? bundleData : policies.bundles; return policies; } catch (RestException re) { throw re; } catch (Exception e) { log.error("[{}] Failed to get namespace policies {}/{}/{}", clientAppId(), property, cluster, namespace, e); throw new RestException(e); } }
Pair<NamespaceBundles, List<NamespaceBundle>> splittedBundles = bundleFactory.splitBundles(bundle, 2 /* by default split into 2 */); if (splittedBundles != null) { bundleFactory.invalidateBundleCache(nsname);
@Override public void onUpdate(String path, LocalPolicies data, Stat stat) { final NamespaceName namespace = new NamespaceName(getNamespaceFromPoliciesPath(path)); try { LOG.info("Policy updated for namespace {}, refreshing the bundle cache.", namespace); // invalidate the bundle cache to fetch new bundle data from the policies bundlesCache.synchronous().invalidate(namespace); } catch (Exception e) { LOG.error("Failed to update the policy change for ns {}", namespace, e); } }
public void removeOwnedServiceUnit(NamespaceName nsName) throws Exception { ownershipCache.removeOwnership(getFullBundle(nsName)).get(cacheTimeOutInSec, SECONDS); bundleFactory.invalidateBundleCache(nsName); }
private NamespaceBundle getFullBundle(NamespaceName fqnn) throws Exception { return bundleFactory.getFullBundle(fqnn); }
@Override public boolean includes(DestinationName dn) { if (!this.nsname.equals(dn.getNamespaceObject())) { return false; } return this.keyRange.contains(factory.getLongHashCode(dn.toString())); }
public CompletableFuture<NamespaceBundle> getBundleAsync(DestinationName topic) { return bundleFactory.getBundlesAsync(topic.getNamespaceObject()) .thenApply(bundles -> bundles.findBundle(topic)); }
/** * update new bundle-range to LocalZk (create a new node if not present) * * @param nsname * @param nsBundles * @param callback * @throws Exception */ private void updateNamespaceBundles(NamespaceName nsname, NamespaceBundles nsBundles, StatCallback callback) throws Exception { checkNotNull(nsname); checkNotNull(nsBundles); String path = joinPath(LOCAL_POLICIES_ROOT, nsname.toString()); Optional<LocalPolicies> policies = pulsar.getLocalZkCacheService().policiesCache().get(path); if (!policies.isPresent()) { // if policies is not present into localZk then create new policies this.pulsar.getLocalZkCacheService().createPolicies(path, false).get(cacheTimeOutInSec, SECONDS); policies = this.pulsar.getLocalZkCacheService().policiesCache().get(path); } policies.get().bundles = getBundlesData(nsBundles); this.pulsar.getLocalZkCache().getZooKeeper().setData(path, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(policies.get()), -1, callback, null); }
@Override public void onUpdate(String path, Policies data, Stat stat) { final NamespaceName namespace = new NamespaceName(NamespaceBundleFactory.getNamespaceFromPoliciesPath(path)); log.info("Updated {}", path); topics.forEach((name, topicFuture) -> { if (namespace.includes(DestinationName.get(name))) { // If the topic is already created, immediately apply the updated policies, otherwise once the topic is // created it'll apply the policies update topicFuture.thenAccept(topic -> { if (log.isDebugEnabled()) { log.debug("Notifying topic that policies have changed: {}", name); } topic.onPoliciesUpdate(data); }); } }); }
public void removeOwnedServiceUnit(NamespaceBundle nsBundle) throws Exception { ownershipCache.removeOwnership(nsBundle).get(cacheTimeOutInSec, SECONDS); bundleFactory.invalidateBundleCache(nsBundle.getNamespaceObject()); }
nsFullBundle = bundleFactory.getFullBundle(nsname);
public NamespaceBundle findBundle(DestinationName dn) { checkArgument(this.nsname.equals(dn.getNamespaceObject())); long hashCode = factory.getLongHashCode(dn.toString()); return getBundle(hashCode); }
public NamespaceBundle getBundle(DestinationName destination) throws Exception { return bundleFactory.getBundles(destination.getNamespaceObject()).findBundle(destination); }
/** * Default constructor. * * @throws PulsarServerException */ public NamespaceService(PulsarService pulsar) { this.pulsar = pulsar; host = pulsar.getAdvertisedAddress(); this.config = pulsar.getConfiguration(); this.loadManager = pulsar.getLoadManager(); ServiceUnitZkUtils.initZK(pulsar.getLocalZkCache().getZooKeeper(), pulsar.getBrokerServiceUrl()); this.bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32()); this.ownershipCache = new OwnershipCache(pulsar, bundleFactory); }
public NamespaceBundleFactory(PulsarService pulsar, HashFunction hashFunc) { this.hashFunc = hashFunc; this.bundlesCache = Caffeine.newBuilder().buildAsync((NamespaceName namespace, Executor executor) -> { String path = AdminResource.joinPath(LOCAL_POLICIES_ROOT, namespace.toString()); if (LOG.isDebugEnabled()) { LOG.debug("Loading cache with bundles for {}", namespace); } if (pulsar == null || pulsar.getConfigurationCache() == null) { return CompletableFuture.completedFuture(getBundles(namespace, null)); } CompletableFuture<NamespaceBundles> future = new CompletableFuture<>(); // Read the static bundle data from the policies pulsar.getLocalZkCacheService().policiesCache().getAsync(path).thenAccept(policies -> { // If no policies defined for namespace, assume 1 single bundle BundlesData bundlesData = policies.map(p -> p.bundles).orElse(null); NamespaceBundles namespaceBundles = getBundles(namespace, bundlesData); future.complete(namespaceBundles); }).exceptionally(ex -> { future.completeExceptionally(ex); return null; }); return future; }); if (pulsar != null && pulsar.getConfigurationCache() != null) { pulsar.getLocalZkCacheService().policiesCache().registerListener(this); } this.pulsar = pulsar; }