Search in sources :

Example 1 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class LookupDataTest method serializeToJsonTest.

@SuppressWarnings("unchecked")
@Test
void serializeToJsonTest() throws Exception {
    LookupData data = new LookupData("pulsar://localhost:8888", "pulsar://localhost:8884", "http://localhost:8080", "http://localhost:8081");
    ObjectMapper mapper = ObjectMapperFactory.getThreadLocal();
    String json = mapper.writeValueAsString(data);
    Map<String, String> jsonMap = mapper.readValue(json, Map.class);
    assertEquals(jsonMap.get("brokerUrl"), "pulsar://localhost:8888");
    assertEquals(jsonMap.get("brokerUrlTls"), "pulsar://localhost:8884");
    assertEquals(jsonMap.get("brokerUrlSsl"), "");
    assertEquals(jsonMap.get("nativeUrl"), "pulsar://localhost:8888");
    assertEquals(jsonMap.get("httpUrl"), "http://localhost:8080");
    assertEquals(jsonMap.get("httpUrlTls"), "http://localhost:8081");
}
Also used : LookupData(org.apache.pulsar.common.lookup.data.LookupData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 2 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class LookupDataTest method withConstructor.

@Test
void withConstructor() {
    LookupData data = new LookupData("pulsar://localhost:8888", "pulsar://localhost:8884", "http://localhost:8080", "http://localhost:8081");
    assertEquals(data.getBrokerUrl(), "pulsar://localhost:8888");
    assertEquals(data.getHttpUrl(), "http://localhost:8080");
}
Also used : LookupData(org.apache.pulsar.common.lookup.data.LookupData) Test(org.testng.annotations.Test)

Example 3 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class HttpLookupService method getBroker.

/**
 * Calls http-lookup api to find broker-service address which can serve a given topic.
 *
 * @param topicName topic-name
 * @return broker-socket-address that serves given topic
 */
@SuppressWarnings("deprecation")
public CompletableFuture<Pair<InetSocketAddress, InetSocketAddress>> getBroker(TopicName topicName) {
    return httpClient.get(BasePath + topicName.getLookupName(), LookupData.class).thenCompose(lookupData -> {
        // Convert LookupData into as SocketAddress, handling exceptions
        URI uri = null;
        try {
            if (useTls) {
                uri = new URI(lookupData.getBrokerUrlTls());
            } else {
                String serviceUrl = lookupData.getBrokerUrl();
                if (serviceUrl == null) {
                    serviceUrl = lookupData.getNativeUrl();
                }
                uri = new URI(serviceUrl);
            }
            InetSocketAddress brokerAddress = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
            return CompletableFuture.completedFuture(Pair.of(brokerAddress, brokerAddress));
        } catch (Exception e) {
            // Failed to parse url
            log.warn("[{}] Lookup Failed due to invalid url {}, {}", topicName, uri, e.getMessage());
            return FutureUtil.failedFuture(e);
        }
    });
}
Also used : InetSocketAddress(java.net.InetSocketAddress) LookupData(org.apache.pulsar.common.lookup.data.LookupData) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 4 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class TopicLookup 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
 * @return
 */
public static CompletableFuture<ByteBuf> lookupTopicAsync(PulsarService pulsarService, TopicName topicName, boolean authoritative, String clientAppId, AuthenticationDataSource authenticationData, long requestId) {
    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.getBrokerServiceUrl())) {
                    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(validaitonFailureResponse -> {
        if (validaitonFailureResponse != null) {
            lookupfuture.complete(validaitonFailureResponse);
        } else {
            pulsarService.getNamespaceService().getBrokerServiceUrlAsync(topicName, authoritative).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 = isLeaderBroker(pulsarService);
                    lookupfuture.complete(newLookupResponse(lookupData.getBrokerUrl(), lookupData.getBrokerUrlTls(), newAuthoritative, LookupType.Redirect, requestId, false));
                } else {
                    // When running in standalone mode we want to redirect the client through the service
                    // url, so that the advertised address configuration is not relevant anymore.
                    boolean redirectThroughServiceUrl = pulsarService.getConfiguration().isRunningStandalone();
                    lookupfuture.complete(newLookupResponse(lookupData.getBrokerUrl(), lookupData.getBrokerUrlTls(), true, /* authoritative */
                    LookupType.Connect, requestId, redirectThroughServiceUrl));
                }
            }).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 : PathParam(javax.ws.rs.PathParam) Encoded(javax.ws.rs.Encoded) TopicName(org.apache.pulsar.common.naming.TopicName) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) URISyntaxException(java.net.URISyntaxException) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) LookupType(org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopicResponse.LookupType) CompletableFuture(java.util.concurrent.CompletableFuture) ApiResponses(io.swagger.annotations.ApiResponses) StringUtils(dlshade.org.apache.commons.lang3.StringUtils) ServerError(org.apache.pulsar.common.api.proto.PulsarApi.ServerError) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ByteBuf(io.netty.buffer.ByteBuf) DefaultValue(javax.ws.rs.DefaultValue) RestException(org.apache.pulsar.broker.web.RestException) NoSwaggerDocumentation(org.apache.pulsar.broker.web.NoSwaggerDocumentation) URI(java.net.URI) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) Status(javax.ws.rs.core.Response.Status) Logger(org.slf4j.Logger) Commands.newLookupErrorResponse(org.apache.pulsar.common.api.Commands.newLookupErrorResponse) PulsarWebResource(org.apache.pulsar.broker.web.PulsarWebResource) AsyncResponse(javax.ws.rs.container.AsyncResponse) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LookupData(org.apache.pulsar.common.lookup.data.LookupData) CompletionException(java.util.concurrent.CompletionException) PulsarService(org.apache.pulsar.broker.PulsarService) Suspended(javax.ws.rs.container.Suspended) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) Codec(org.apache.pulsar.common.util.Codec) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) Commands.newLookupResponse(org.apache.pulsar.common.api.Commands.newLookupResponse) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) 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)

Example 5 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class NamespaceService method createLookupResult.

protected CompletableFuture<LookupResult> createLookupResult(String candidateBroker) throws Exception {
    CompletableFuture<LookupResult> lookupFuture = new CompletableFuture<>();
    try {
        checkArgument(StringUtils.isNotBlank(candidateBroker), "Lookup broker can't be null " + candidateBroker);
        URI uri = new URI(candidateBroker);
        String path = String.format("%s/%s:%s", LoadManager.LOADBALANCE_BROKERS_ROOT, uri.getHost(), uri.getPort());
        pulsar.getLocalZkCache().getDataAsync(path, pulsar.getLoadManager().get().getLoadReportDeserializer()).thenAccept(reportData -> {
            if (reportData.isPresent()) {
                ServiceLookupData lookupData = reportData.get();
                lookupFuture.complete(new LookupResult(lookupData.getWebServiceUrl(), lookupData.getWebServiceUrlTls(), lookupData.getPulsarServiceUrl(), lookupData.getPulsarServiceUrlTls()));
            } else {
                lookupFuture.completeExceptionally(new KeeperException.NoNodeException(path));
            }
        }).exceptionally(ex -> {
            lookupFuture.completeExceptionally(ex);
            return null;
        });
    } catch (Exception e) {
        lookupFuture.completeExceptionally(e);
    }
    return lookupFuture;
}
Also used : ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) URL(java.net.URL) AdminResource(org.apache.pulsar.broker.admin.AdminResource) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) LoggerFactory(org.slf4j.LoggerFactory) LoadManager(org.apache.pulsar.broker.loadbalance.LoadManager) StringUtils(org.apache.commons.lang3.StringUtils) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NamespaceOwnershipStatus(org.apache.pulsar.common.policies.data.NamespaceOwnershipStatus) Map(java.util.Map) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) URI(java.net.URI) NamespaceIsolationPolicy(org.apache.pulsar.common.policies.NamespaceIsolationPolicy) Set(java.util.Set) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) PulsarWebResource.joinPath(org.apache.pulsar.broker.web.PulsarWebResource.joinPath) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) ServiceUnitId(org.apache.pulsar.common.naming.ServiceUnitId) NamespaceBundleFactory.getBundlesData(org.apache.pulsar.common.naming.NamespaceBundleFactory.getBundlesData) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Code(org.apache.zookeeper.KeeperException.Code) TopicName(org.apache.pulsar.common.naming.TopicName) LOCAL_POLICIES_ROOT(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService.LOCAL_POLICIES_ROOT) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ResourceUnit(org.apache.pulsar.broker.loadbalance.ResourceUnit) Hashing(com.google.common.hash.Hashing) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) Lists(com.google.common.collect.Lists) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) ZooKeeperCache.cacheTimeOutInSec(org.apache.pulsar.zookeeper.ZooKeeperCache.cacheTimeOutInSec) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LookupData(org.apache.pulsar.common.lookup.data.LookupData) PulsarService(org.apache.pulsar.broker.PulsarService) ServiceLookupData(org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData) TimeUnit(java.util.concurrent.TimeUnit) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Codec(org.apache.pulsar.common.util.Codec) SECONDS(java.util.concurrent.TimeUnit.SECONDS) BrokerAssignment(org.apache.pulsar.common.policies.data.BrokerAssignment) CompletableFuture(java.util.concurrent.CompletableFuture) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) ServiceLookupData(org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData) URI(java.net.URI) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) KeeperException(org.apache.zookeeper.KeeperException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Aggregations

LookupData (org.apache.pulsar.common.lookup.data.LookupData)7 URI (java.net.URI)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Test (org.testng.annotations.Test)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 URL (java.net.URL)2 Optional (java.util.Optional)2 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)2 PulsarService (org.apache.pulsar.broker.PulsarService)2 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)2 ServiceUnitNotReadyException (org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)2 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)2 TopicName (org.apache.pulsar.common.naming.TopicName)2 Codec (org.apache.pulsar.common.util.Codec)2 ServiceLookupData (org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData)2 KeeperException (org.apache.zookeeper.KeeperException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1