Search in sources :

Example 1 with LookupOptions

use of org.apache.pulsar.broker.namespace.LookupOptions in project pulsar by apache.

the class PulsarWebResource method validateTopicOwnershipAsync.

protected CompletableFuture<Void> validateTopicOwnershipAsync(TopicName topicName, boolean authoritative) {
    NamespaceService nsService = pulsar().getNamespaceService();
    LookupOptions options = LookupOptions.builder().authoritative(authoritative).requestHttps(isRequestHttps()).readOnly(false).loadTopicsInBundle(false).build();
    return nsService.getWebServiceUrlAsync(topicName, options).thenApply(webUrl -> {
        // Ensure we get a url
        if (webUrl == null || !webUrl.isPresent()) {
            log.info("Unable to get web service url");
            throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for topic:" + topicName);
        }
        return webUrl.get();
    }).thenCompose(webUrl -> nsService.isServiceUnitOwnedAsync(topicName).thenApply(isTopicOwned -> Pair.of(webUrl, isTopicOwned))).thenAccept(pair -> {
        URL webUrl = pair.getLeft();
        boolean isTopicOwned = pair.getRight();
        if (!isTopicOwned) {
            boolean newAuthoritative = isLeaderBroker(pulsar());
            // Replace the host and port of the current request and redirect
            URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.getHost()).port(webUrl.getPort()).replaceQueryParam("authoritative", newAuthoritative).build();
            // Redirect
            if (log.isDebugEnabled()) {
                log.debug("Redirecting the rest call to {}", redirect);
            }
            throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
        }
    }).exceptionally(ex -> {
        if (ex.getCause() instanceof IllegalArgumentException || ex.getCause() instanceof IllegalStateException) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to find owner for topic: {}", topicName, ex);
            }
            throw new RestException(Status.PRECONDITION_FAILED, "Can't find owner for topic " + topicName);
        } else if (ex.getCause() instanceof WebApplicationException) {
            throw (WebApplicationException) ex.getCause();
        } else {
            throw new RestException(ex.getCause());
        }
    });
}
Also used : Constants(org.apache.pulsar.common.naming.Constants) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) URL(java.net.URL) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) BookieResources(org.apache.pulsar.broker.resources.BookieResources) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) StringUtils(org.apache.commons.lang3.StringUtils) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Pair(org.apache.commons.lang3.tuple.Pair) TopicOperation(org.apache.pulsar.common.policies.data.TopicOperation) PolicyOperation(org.apache.pulsar.common.policies.data.PolicyOperation) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) LocalPoliciesResources(org.apache.pulsar.broker.resources.LocalPoliciesResources) IsolationPolicyResources(org.apache.pulsar.broker.resources.NamespaceResources.IsolationPolicyResources) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) Range(com.google.common.collect.Range) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) BrokerService(org.apache.pulsar.broker.service.BrokerService) Sets(com.google.common.collect.Sets) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) Response(javax.ws.rs.core.Response) BoundType(com.google.common.collect.BoundType) PolicyName(org.apache.pulsar.common.policies.data.PolicyName) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) NamespaceResources(org.apache.pulsar.broker.resources.NamespaceResources) UriInfo(javax.ws.rs.core.UriInfo) TenantOperation(org.apache.pulsar.common.policies.data.TenantOperation) PulsarServiceNameResolver(org.apache.pulsar.client.impl.PulsarServiceNameResolver) TopicResources(org.apache.pulsar.broker.resources.TopicResources) TopicName(org.apache.pulsar.common.naming.TopicName) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) PolicyPath(org.apache.pulsar.common.policies.path.PolicyPath) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo) Status(javax.ws.rs.core.Response.Status) ClusterResources(org.apache.pulsar.broker.resources.ClusterResources) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) ResourceGroupResources(org.apache.pulsar.broker.resources.ResourceGroupResources) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PulsarService(org.apache.pulsar.broker.PulsarService) DynamicConfigurationResources(org.apache.pulsar.broker.resources.DynamicConfigurationResources) ExecutionException(java.util.concurrent.ExecutionException) Policies(org.apache.pulsar.common.policies.data.Policies) TenantResources(org.apache.pulsar.broker.resources.TenantResources) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ServletContext(javax.servlet.ServletContext) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) SECONDS(java.util.concurrent.TimeUnit.SECONDS) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) WebApplicationException(javax.ws.rs.WebApplicationException) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) URI(java.net.URI) URL(java.net.URL)

Example 2 with LookupOptions

use of org.apache.pulsar.broker.namespace.LookupOptions in project pulsar by apache.

the class PulsarWebResource method validateBundleOwnership.

public void validateBundleOwnership(NamespaceBundle bundle, boolean authoritative, boolean readOnly) throws Exception {
    NamespaceService nsService = pulsar().getNamespaceService();
    try {
        // Call getWebServiceUrl() to acquire or redirect the request
        // Get web service URL of owning broker.
        // 1: If namespace is assigned to this broker, continue
        // 2: If namespace is assigned to another broker, redirect to the webservice URL of another broker
        // authoritative flag is ignored
        // 3: If namespace is unassigned and readOnly is true, return 412
        // 4: If namespace is unassigned and readOnly is false:
        // - If authoritative is false and this broker is not leader, forward to leader
        // - If authoritative is false and this broker is leader, determine owner and forward w/ authoritative=true
        // - If authoritative is true, own the namespace and continue
        LookupOptions options = LookupOptions.builder().authoritative(authoritative).requestHttps(isRequestHttps()).readOnly(readOnly).loadTopicsInBundle(false).build();
        Optional<URL> webUrl = nsService.getWebServiceUrl(bundle, options);
        // Ensure we get a url
        if (webUrl == null || !webUrl.isPresent()) {
            log.warn("Unable to get web service url");
            throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for ServiceUnit:" + bundle.toString());
        }
        if (!nsService.isServiceUnitOwned(bundle)) {
            boolean newAuthoritative = this.isLeaderBroker();
            // Replace the host and port of the current request and redirect
            URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.get().getHost()).port(webUrl.get().getPort()).replaceQueryParam("authoritative", newAuthoritative).build();
            log.debug("{} is not a service unit owned", bundle);
            // Redirect
            log.debug("Redirecting the rest call to {}", redirect);
            throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
        }
    } catch (TimeoutException te) {
        String msg = String.format("Finding owner for ServiceUnit %s timed out", bundle);
        log.error(msg, te);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, msg);
    } catch (IllegalArgumentException iae) {
        // namespace format is not valid
        log.debug("Failed to find owner for ServiceUnit {}", bundle, iae);
        throw new RestException(Status.PRECONDITION_FAILED, "ServiceUnit format is not expected. ServiceUnit " + bundle);
    } catch (IllegalStateException ise) {
        log.debug("Failed to find owner for ServiceUnit {}", bundle, ise);
        throw new RestException(Status.PRECONDITION_FAILED, "ServiceUnit bundle is actived. ServiceUnit " + bundle);
    } catch (NullPointerException e) {
        log.warn("Unable to get web service url");
        throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for ServiceUnit:" + bundle);
    } catch (WebApplicationException wae) {
        throw wae;
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) URI(java.net.URI) URL(java.net.URL) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with LookupOptions

use of org.apache.pulsar.broker.namespace.LookupOptions in project pulsar by apache.

the class NamespacesTest method testDeleteNamespaceWithBundles.

@Test
public void testDeleteNamespaceWithBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getSafeWebServiceAddress());
    String bundledNsLocal = "test-delete-namespace-with-bundles";
    List<String> boundaries = Lists.newArrayList("0x00000000", "0x80000000", "0xffffffff");
    BundlesData bundleData = BundlesData.builder().boundaries(boundaries).numBundles(boundaries.size() - 1).build();
    createBundledTestNamespaces(this.testTenant, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = NamespaceName.get(this.testTenant, this.testLocalCluster, bundledNsLocal);
    org.apache.pulsar.client.admin.Namespaces namespacesAdmin = mock(org.apache.pulsar.client.admin.Namespaces.class);
    doReturn(namespacesAdmin).when(admin).namespaces();
    doReturn(null).when(nsSvc).getWebServiceUrl(Mockito.argThat(new ArgumentMatcher<NamespaceBundle>() {

        @Override
        public boolean matches(NamespaceBundle bundle) {
            return bundle.getNamespaceObject().equals(testNs);
        }
    }), Mockito.any());
    doReturn(false).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new ArgumentMatcher<NamespaceBundle>() {

        @Override
        public boolean matches(NamespaceBundle bundle) {
            return bundle.getNamespaceObject().equals(testNs);
        }
    }));
    doReturn(CompletableFuture.completedFuture(Optional.of(mock(NamespaceEphemeralData.class)))).when(nsSvc).getOwnerAsync(Mockito.argThat(new ArgumentMatcher<NamespaceBundle>() {

        @Override
        public boolean matches(NamespaceBundle bundle) {
            return bundle.getNamespaceObject().equals(testNs);
        }
    }));
    CompletableFuture<Void> preconditionFailed = new CompletableFuture<>();
    ClientErrorException cee = new ClientErrorException(Status.PRECONDITION_FAILED);
    int statusCode = cee.getResponse().getStatus();
    String httpError = BaseResource.getReasonFromServer(cee);
    preconditionFailed.completeExceptionally(new PulsarAdminException.PreconditionFailedException(cee, httpError, statusCode));
    doReturn(preconditionFailed).when(namespacesAdmin).deleteNamespaceBundleAsync(Mockito.anyString(), Mockito.anyString());
    try {
        namespaces.deleteNamespaceBundle(testTenant, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false, false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    doReturn(Optional.empty()).when(nsSvc).getWebServiceUrl(any(NamespaceBundle.class), any(LookupOptions.class));
    AsyncResponse response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testTenant, testLocalCluster, bundledNsLocal, false, false);
    ArgumentCaptor<RestException> captor = ArgumentCaptor.forClass(RestException.class);
    verify(response, timeout(5000).times(1)).resume(captor.capture());
    assertEquals(captor.getValue().getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    // make one bundle owned
    LookupOptions optionsHttps = LookupOptions.builder().authoritative(false).requestHttps(true).readOnly(false).build();
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(nsBundles.getBundles().get(0), optionsHttps);
    doReturn(true).when(nsSvc).isServiceUnitOwned(nsBundles.getBundles().get(0));
    doReturn(CompletableFuture.completedFuture(null)).when(namespacesAdmin).deleteNamespaceBundleAsync(testTenant + "/" + testLocalCluster + "/" + bundledNsLocal, "0x00000000_0x80000000");
    try {
        namespaces.deleteNamespaceBundle(testTenant, testLocalCluster, bundledNsLocal, "0x80000000_0xffffffff", false, false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    response = mock(AsyncResponse.class);
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(any(NamespaceBundle.class), any(LookupOptions.class));
    for (NamespaceBundle bundle : nsBundles.getBundles()) {
        doReturn(true).when(nsSvc).isServiceUnitOwned(bundle);
    }
    namespaces.deleteNamespace(response, testTenant, testLocalCluster, bundledNsLocal, false, false);
    ArgumentCaptor<Response> captor2 = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(captor2.capture());
    assertEquals(captor2.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());
}
Also used : NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) URL(java.net.URL) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) CompletableFuture(java.util.concurrent.CompletableFuture) ArgumentMatcher(org.mockito.ArgumentMatcher) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) RestException(org.apache.pulsar.broker.web.RestException) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) ClientErrorException(javax.ws.rs.ClientErrorException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) AsyncResponse(javax.ws.rs.container.AsyncResponse) NamespaceEphemeralData(org.apache.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with LookupOptions

use of org.apache.pulsar.broker.namespace.LookupOptions in project pulsar by apache.

the class TopicLookupBase method lookupTopicAsync.

/**
 * Lookup broker-service address for a given namespace-bundle which contains given topic.
 *
 * a. Returns broker-address if namespace-bundle is already owned by any broker
 * b. If current-broker receives lookup-request and if it's not a leader then current broker redirects request
 *    to leader by returning leader-service address.
 * c. If current-broker is leader then it finds out least-loaded broker
 *    to own namespace bundle and redirects request
 *    by returning least-loaded broker.
 * d. If current-broker receives request to own the namespace-bundle then
 *    it owns a bundle and returns success(connect)
 *    response to client.
 *
 * @param pulsarService
 * @param topicName
 * @param authoritative
 * @param clientAppId
 * @param requestId
 * @param advertisedListenerName
 * @return
 */
public static CompletableFuture<ByteBuf> lookupTopicAsync(PulsarService pulsarService, TopicName topicName, boolean authoritative, String clientAppId, AuthenticationDataSource authenticationData, long requestId, final String advertisedListenerName) {
    final CompletableFuture<ByteBuf> validationFuture = new CompletableFuture<>();
    final CompletableFuture<ByteBuf> lookupfuture = new CompletableFuture<>();
    final String cluster = topicName.getCluster();
    // (1) validate cluster
    getClusterDataIfDifferentCluster(pulsarService, cluster, clientAppId).thenAccept(differentClusterData -> {
        if (differentClusterData != null) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Redirecting the lookup call to {}/{} cluster={}", clientAppId, differentClusterData.getBrokerServiceUrl(), differentClusterData.getBrokerServiceUrlTls(), cluster);
            }
            validationFuture.complete(newLookupResponse(differentClusterData.getBrokerServiceUrl(), differentClusterData.getBrokerServiceUrlTls(), true, LookupType.Redirect, requestId, false));
        } else {
            // (2) authorize client
            try {
                checkAuthorization(pulsarService, topicName, clientAppId, authenticationData);
            } catch (RestException authException) {
                log.warn("Failed to authorized {} on cluster {}", clientAppId, topicName.toString());
                validationFuture.complete(newLookupErrorResponse(ServerError.AuthorizationError, authException.getMessage(), requestId));
                return;
            } catch (Exception e) {
                log.warn("Unknown error while authorizing {} on cluster {}", clientAppId, topicName.toString());
                validationFuture.completeExceptionally(e);
                return;
            }
            // (3) validate global namespace
            checkLocalOrGetPeerReplicationCluster(pulsarService, topicName.getNamespaceObject()).thenAccept(peerClusterData -> {
                if (peerClusterData == null) {
                    // (4) all validation passed: initiate lookup
                    validationFuture.complete(null);
                    return;
                }
                // request should be redirect to the peer-cluster
                if (StringUtils.isBlank(peerClusterData.getBrokerServiceUrl()) && StringUtils.isBlank(peerClusterData.getBrokerServiceUrlTls())) {
                    validationFuture.complete(newLookupErrorResponse(ServerError.MetadataError, "Redirected cluster's brokerService url is not configured", requestId));
                    return;
                }
                validationFuture.complete(newLookupResponse(peerClusterData.getBrokerServiceUrl(), peerClusterData.getBrokerServiceUrlTls(), true, LookupType.Redirect, requestId, false));
            }).exceptionally(ex -> {
                validationFuture.complete(newLookupErrorResponse(ServerError.MetadataError, ex.getMessage(), requestId));
                return null;
            });
        }
    }).exceptionally(ex -> {
        validationFuture.completeExceptionally(ex);
        return null;
    });
    // Initiate lookup once validation completes
    validationFuture.thenAccept(validationFailureResponse -> {
        if (validationFailureResponse != null) {
            lookupfuture.complete(validationFailureResponse);
        } else {
            LookupOptions options = LookupOptions.builder().authoritative(authoritative).advertisedListenerName(advertisedListenerName).loadTopicsInBundle(true).build();
            pulsarService.getNamespaceService().getBrokerServiceUrlAsync(topicName, options).thenAccept(lookupResult -> {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Lookup result {}", topicName.toString(), lookupResult);
                }
                if (!lookupResult.isPresent()) {
                    lookupfuture.complete(newLookupErrorResponse(ServerError.ServiceNotReady, "No broker was available to own " + topicName, requestId));
                    return;
                }
                LookupData lookupData = lookupResult.get().getLookupData();
                if (lookupResult.get().isRedirect()) {
                    boolean newAuthoritative = lookupResult.get().isAuthoritativeRedirect();
                    lookupfuture.complete(newLookupResponse(lookupData.getBrokerUrl(), lookupData.getBrokerUrlTls(), newAuthoritative, LookupType.Redirect, requestId, false));
                } else {
                    ServiceConfiguration conf = pulsarService.getConfiguration();
                    lookupfuture.complete(newLookupResponse(lookupData.getBrokerUrl(), lookupData.getBrokerUrlTls(), true, /* authoritative */
                    LookupType.Connect, requestId, shouldRedirectThroughServiceUrl(conf, lookupData)));
                }
            }).exceptionally(ex -> {
                if (ex instanceof CompletionException && ex.getCause() instanceof IllegalStateException) {
                    log.info("Failed to lookup {} for topic {} with error {}", clientAppId, topicName.toString(), ex.getCause().getMessage());
                } else {
                    log.warn("Failed to lookup {} for topic {} with error {}", clientAppId, topicName.toString(), ex.getMessage(), ex);
                }
                lookupfuture.complete(newLookupErrorResponse(ServerError.ServiceNotReady, ex.getMessage(), requestId));
                return null;
            });
        }
    }).exceptionally(ex -> {
        if (ex instanceof CompletionException && ex.getCause() instanceof IllegalStateException) {
            log.info("Failed to lookup {} for topic {} with error {}", clientAppId, topicName.toString(), ex.getCause().getMessage());
        } else {
            log.warn("Failed to lookup {} for topic {} with error {}", clientAppId, topicName.toString(), ex.getMessage(), ex);
        }
        lookupfuture.complete(newLookupErrorResponse(ServerError.ServiceNotReady, ex.getMessage(), requestId));
        return null;
    });
    return lookupfuture;
}
Also used : Encoded(javax.ws.rs.Encoded) TopicName(org.apache.pulsar.common.naming.TopicName) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Commands.newLookupResponse(org.apache.pulsar.common.protocol.Commands.newLookupResponse) StringUtils(org.apache.commons.lang3.StringUtils) InetAddress(java.net.InetAddress) ByteBuf(io.netty.buffer.ByteBuf) TopicOperation(org.apache.pulsar.common.policies.data.TopicOperation) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) URI(java.net.URI) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) Logger(org.slf4j.Logger) PulsarWebResource(org.apache.pulsar.broker.web.PulsarWebResource) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) AsyncResponse(javax.ws.rs.container.AsyncResponse) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ServerError(org.apache.pulsar.common.api.proto.ServerError) LookupData(org.apache.pulsar.common.lookup.data.LookupData) CompletionException(java.util.concurrent.CompletionException) PulsarService(org.apache.pulsar.broker.PulsarService) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) Commands.newLookupErrorResponse(org.apache.pulsar.common.protocol.Commands.newLookupErrorResponse) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) Response(javax.ws.rs.core.Response) Codec(org.apache.pulsar.common.util.Codec) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) LookupType(org.apache.pulsar.common.api.proto.CommandLookupTopicResponse.LookupType) RestException(org.apache.pulsar.broker.web.RestException) ByteBuf(io.netty.buffer.ByteBuf) LookupData(org.apache.pulsar.common.lookup.data.LookupData) URISyntaxException(java.net.URISyntaxException) RestException(org.apache.pulsar.broker.web.RestException) CompletionException(java.util.concurrent.CompletionException) WebApplicationException(javax.ws.rs.WebApplicationException) CompletableFuture(java.util.concurrent.CompletableFuture) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) CompletionException(java.util.concurrent.CompletionException)

Example 5 with LookupOptions

use of org.apache.pulsar.broker.namespace.LookupOptions in project pulsar by yahoo.

the class PulsarWebResource method isBundleOwnedByAnyBroker.

/**
 * Checks whether a given bundle is currently loaded by any broker.
 */
protected CompletableFuture<Boolean> isBundleOwnedByAnyBroker(NamespaceName fqnn, BundlesData bundles, String bundleRange) {
    NamespaceBundle nsBundle = validateNamespaceBundleRange(fqnn, bundles, bundleRange);
    NamespaceService nsService = pulsar().getNamespaceService();
    LookupOptions options = LookupOptions.builder().authoritative(false).requestHttps(isRequestHttps()).readOnly(true).loadTopicsInBundle(false).build();
    try {
        return nsService.getWebServiceUrlAsync(nsBundle, options).thenApply(optionUrl -> optionUrl.isPresent());
    } catch (Exception e) {
        log.error("Failed to check whether namespace bundle is owned {}/{}", fqnn.toString(), bundleRange, e);
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) WebApplicationException(javax.ws.rs.WebApplicationException) MalformedURLException(java.net.MalformedURLException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

LookupOptions (org.apache.pulsar.broker.namespace.LookupOptions)22 WebApplicationException (javax.ws.rs.WebApplicationException)17 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)17 URL (java.net.URL)15 AsyncResponse (javax.ws.rs.container.AsyncResponse)15 Response (javax.ws.rs.core.Response)15 URI (java.net.URI)14 CompletableFuture (java.util.concurrent.CompletableFuture)14 NamespaceService (org.apache.pulsar.broker.namespace.NamespaceService)14 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)13 TopicName (org.apache.pulsar.common.naming.TopicName)13 RestException (org.apache.pulsar.broker.web.RestException)12 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)12 Optional (java.util.Optional)11 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)11 NamespaceBundles (org.apache.pulsar.common.naming.NamespaceBundles)11 BundlesData (org.apache.pulsar.common.policies.data.BundlesData)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Lists (com.google.common.collect.Lists)8