Java Code Examples for org.apache.pulsar.common.naming.NamespaceName#isV2()

The following examples show how to use org.apache.pulsar.common.naming.NamespaceName#isV2() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamespacesImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<List<String>> getTopicsAsync(String namespace) {
    NamespaceName ns = NamespaceName.get(namespace);
    String action = ns.isV2() ? "topics" : "destinations";
    WebTarget path = namespacePath(ns, action);
    final CompletableFuture<List<String>> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<List<String>>() {
                @Override
                public void completed(List<String> topics) {
                    future.complete(topics);
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example 2
Source File: NamespacesImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> createNamespaceAsync(String namespace, Set<String> clusters) {
    NamespaceName ns = NamespaceName.get(namespace);
    WebTarget path = namespacePath(ns);

    if (ns.isV2()) {
        // For V2 API we pass full Policy class instance
        Policies policies = new Policies();
        policies.replication_clusters = clusters;
        return asyncPutRequest(path, Entity.entity(policies, MediaType.APPLICATION_JSON));
    } else {
        // For V1 API, we pass the BundlesData on creation
        return asyncPutRequest(path, Entity.entity("", MediaType.APPLICATION_JSON)).thenAccept(ignore -> {
            // For V1, we need to do it in 2 steps
            setNamespaceReplicationClustersAsync(namespace, clusters);
        });
    }
}
 
Example 3
Source File: HttpLookupService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<List<String>> getTopicsUnderNamespace(NamespaceName namespace, Mode mode) {
    CompletableFuture<List<String>> future = new CompletableFuture<>();

    String format = namespace.isV2()
        ? "admin/v2/namespaces/%s/topics?mode=%s" : "admin/namespaces/%s/destinations?mode=%s";
    httpClient
        .get(String.format(format, namespace, mode.toString()), String[].class)
        .thenAccept(topics -> {
            List<String> result = Lists.newArrayList();
            // do not keep partition part of topic name
            Arrays.asList(topics).forEach(topic -> {
                String filtered = TopicName.get(topic).getPartitionedTopicName();
                if (!result.contains(filtered)) {
                    result.add(filtered);
                }
            });
            future.complete(result);})
        .exceptionally(ex -> {
            log.warn("Failed to getTopicsUnderNamespace namespace {} {}.", namespace, ex.getMessage());
            future.completeExceptionally(ex);
            return null;
        });
    return future;
}
 
Example 4
Source File: NamespacesBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void validatePolicies(NamespaceName ns, Policies policies) {
    if (ns.isV2() && policies.replication_clusters.isEmpty()) {
        // Default to local cluster
        policies.replication_clusters = Collections.singleton(config().getClusterName());
    }

    // Validate cluster names and permissions
    policies.replication_clusters.forEach(cluster -> validateClusterForTenant(ns.getTenant(), cluster));

    if (policies.message_ttl_in_seconds < 0) {
        throw new RestException(Status.PRECONDITION_FAILED, "Invalid value for message TTL");
    }

    if (policies.bundles != null && policies.bundles.getNumBundles() > 0) {
        if (policies.bundles.getBoundaries() == null || policies.bundles.getBoundaries().size() == 0) {
            policies.bundles = getBundles(policies.bundles.getNumBundles());
        } else {
            policies.bundles = validateBundlesData(policies.bundles);
        }
    } else {
        int defaultNumberOfBundles = config().getDefaultNumberOfNamespaceBundles();
        policies.bundles = getBundles(defaultNumberOfBundles);
    }

    if (policies.persistence != null) {
        validatePersistencePolicies(policies.persistence);
    }
}
 
Example 5
Source File: CmdNamespaces.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String namespace = validateNamespace(params);
    if (numBundles < 0 || numBundles > MAX_BUNDLES) {
        throw new ParameterException(
                "Invalid number of bundles. Number of numbles has to be in the range of (0, 2^32].");
    }

    NamespaceName namespaceName = NamespaceName.get(namespace);
    if (namespaceName.isV2()) {
        Policies policies = new Policies();
        policies.bundles = numBundles > 0 ? new BundlesData(numBundles) : null;

        if (clusters != null) {
            policies.replication_clusters = new HashSet<>(clusters);
        }

        admin.namespaces().createNamespace(namespace, policies);
    } else {
        if (numBundles == 0) {
            admin.namespaces().createNamespace(namespace);
        } else {
            admin.namespaces().createNamespace(namespace, numBundles);
        }

        if (clusters != null && !clusters.isEmpty()) {
            admin.namespaces().setNamespaceReplicationClusters(namespace, new HashSet<>(clusters));
        }
    }
}
 
Example 6
Source File: BrokerStatsImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public JsonObject getBrokerResourceAvailability(String namespace) throws PulsarAdminException {
    try {
        NamespaceName ns = NamespaceName.get(namespace);
        WebTarget admin = ns.isV2() ? adminV2BrokerStats : adminBrokerStats;
        String json = request(admin.path("/broker-resource-availability").path(ns.toString())).get(String.class);
        return new Gson().fromJson(json, JsonObject.class);
    } catch (Exception e) {
        throw getApiException(e);
    }
}
 
Example 7
Source File: NamespacesImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> createNamespaceAsync(String namespace, BundlesData bundlesData) {
    NamespaceName ns = NamespaceName.get(namespace);
    WebTarget path = namespacePath(ns);

    if (ns.isV2()) {
        // For V2 API we pass full Policy class instance
        Policies policies = new Policies();
        policies.bundles = bundlesData;
        return asyncPutRequest(path, Entity.entity(policies, MediaType.APPLICATION_JSON));
    } else {
        // For V1 API, we pass the BundlesData on creation
        return asyncPutRequest(path, Entity.entity(bundlesData, MediaType.APPLICATION_JSON));
    }
}