Java Code Examples for org.apache.pulsar.common.naming.TopicName#get()

The following examples show how to use org.apache.pulsar.common.naming.TopicName#get() . 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: SchemasImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<SchemaInfoWithVersion> getSchemaInfoWithVersionAsync(String topic) {
    TopicName tn = TopicName.get(topic);
    final CompletableFuture<SchemaInfoWithVersion> future = new CompletableFuture<>();
    asyncGetRequest(schemaPath(tn),
            new InvocationCallback<GetSchemaResponse>() {
                @Override
                public void completed(GetSchemaResponse response) {
                    future.complete(convertGetSchemaResponseToSchemaInfoWithVersion(tn, response));
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example 2
Source File: BrokerService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public void checkTopicNsOwnership(final String topic) throws RuntimeException {
    TopicName topicName = TopicName.get(topic);
    boolean ownedByThisInstance;
    try {
        ownedByThisInstance = pulsar.getNamespaceService().isServiceUnitOwned(topicName);
    } catch (Exception e) {
        log.debug("Failed to check the ownership of the topic: {}", topicName, e);
        throw new RuntimeException(new ServerMetadataException(e));
    }

    if (!ownedByThisInstance) {
        String msg = String.format("Namespace bundle for topic (%s) not served by this instance. Please redo the lookup. "
                + "Request is denied: namespace=%s", topic, topicName.getNamespace());
        log.warn(msg);
        throw new RuntimeException(new ServiceUnitNotReadyException(msg));
    }
}
 
Example 3
Source File: SchemasImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<IsCompatibilityResponse> testCompatibilityAsync(String topic, PostSchemaPayload payload) {
    TopicName tn = TopicName.get(topic);
    final CompletableFuture<IsCompatibilityResponse> future = new CompletableFuture<>();
    try {
        request(compatibilityPath(tn)).async().post(Entity.json(payload),
                new InvocationCallback<IsCompatibilityResponse>() {
                    @Override
                    public void completed(IsCompatibilityResponse isCompatibilityResponse) {
                        future.complete(isCompatibilityResponse);
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        future.completeExceptionally(getApiException(throwable.getCause()));
                    }
                });
    } catch (PulsarAdminException cae) {
        future.completeExceptionally(cae);
    }
    return future;
}
 
Example 4
Source File: BrokerServiceAutoTopicCreationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreationNamespaceDisallowOverridesBroker() throws Exception {
    final String topicString = "persistent://prop/ns-abc/test-topic-5";
    final String subscriptionName = "test-topic-sub-5";
    final TopicName topicName = TopicName.get(topicString);
    pulsar.getConfiguration().setAllowAutoTopicCreation(true);
    pulsar.getAdminClient().namespaces().setAutoTopicCreation(topicName.getNamespace(),
            new AutoTopicCreationOverride(false, null, null));

    try {
        pulsarClient.newConsumer().topic(topicString).subscriptionName(subscriptionName).subscribe();
        fail("Subscribe operation should have failed");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException);
    }
    assertFalse(admin.namespaces().getTopics("prop/ns-abc").contains(topicString));
}
 
Example 5
Source File: BrokerServiceAutoSubscriptionCreationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoSubscriptionCreationNamespaceDisallowOverridesBroker() throws Exception {
    final String topic = "persistent://prop/ns-abc/test-subtopic";
    final String subscriptionName = "test-subtopic-sub-3";
    final TopicName topicName = TopicName.get(topic);

    admin.topics().createNonPartitionedTopic(topicName.toString());

    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(true);
    pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(),
            new AutoSubscriptionCreationOverride(false));

    try {
        pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
        fail("Subscribe operation should have failed");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException);
    }
    assertFalse(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
 
Example 6
Source File: BrokerServiceAutoSubscriptionCreationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoSubscriptionCreationNamespaceAllowOverridesBroker() throws Exception {
    final String topic = "persistent://prop/ns-abc/test-subtopic";
    final String subscriptionName = "test-subtopic-sub-2";
    final TopicName topicName = TopicName.get(topic);

    admin.topics().createNonPartitionedTopic(topicName.toString());

    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(false);
    pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(),
            new AutoSubscriptionCreationOverride(true));

    // Subscribe operation should be successful
    pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
    assertTrue(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
 
Example 7
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the topic should be retained (based on time), even tough there are no producers/consumers and it's
 * marked as inactive.
 */
private boolean shouldTopicBeRetained() {
    TopicName name = TopicName.get(topic);
    try {
        Optional<Policies> policies = brokerService.pulsar().getConfigurationCache().policiesCache()
                .get(AdminResource.path(POLICIES, name.getNamespace()));
        // If no policies, the default is to have no retention and delete the inactive topic
        RetentionPolicies retentionPolicies = policies.map(p -> p.retention_policies).orElseGet(
                () -> new RetentionPolicies(
                        brokerService.pulsar().getConfiguration().getDefaultRetentionTimeInMinutes(),
                        brokerService.pulsar().getConfiguration().getDefaultRetentionSizeInMB())
        );
        long retentionTime = TimeUnit.MINUTES.toNanos(retentionPolicies.getRetentionTimeInMinutes());

        // Negative retention time means the topic should be retained indefinitely,
        // because its own data has to be retained
        return retentionTime < 0 || (System.nanoTime() - lastActive) < retentionTime;
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Error getting policies", topic);
        }

        // Don't delete in case we cannot get the policies
        return true;
    }
}
 
Example 8
Source File: PulsarClientImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) {

        CompletableFuture<PartitionedTopicMetadata> metadataFuture = new CompletableFuture<>();

        try {
            TopicName topicName = TopicName.get(topic);
            AtomicLong opTimeoutMs = new AtomicLong(conf.getOperationTimeoutMs());
            Backoff backoff = new BackoffBuilder()
                    .setInitialTime(100, TimeUnit.MILLISECONDS)
                    .setMandatoryStop(opTimeoutMs.get() * 2, TimeUnit.MILLISECONDS)
                    .setMax(1, TimeUnit.MINUTES)
                    .create();
            getPartitionedTopicMetadata(topicName, backoff, opTimeoutMs, metadataFuture);
        } catch (IllegalArgumentException e) {
            return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(e.getMessage()));
        }
        return metadataFuture;
    }
 
Example 9
Source File: NamespacesTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateTopicOwnership() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getSafeWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
    createBundledTestNamespaces(this.testTenant, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = NamespaceName.get(this.testTenant, this.testLocalCluster, bundledNsLocal);
    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
    doReturn(CompletableFuture.completedFuture(null)).when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
    Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
    ownership.setAccessible(true);
    ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
    TopicName topicName = TopicName.get(testNs.getPersistentTopicName("my-topic"));
    PersistentTopics topics = spy(new PersistentTopics());
    topics.setServletContext(new MockServletContext());
    topics.setPulsar(pulsar);
    doReturn(false).when(topics).isRequestHttps();
    doReturn("test").when(topics).clientAppId();
    doReturn(null).when(topics).originalPrincipal();
    doReturn(null).when(topics).clientAuthData();
    mockWebUrl(localWebServiceUrl, testNs);
    doReturn("persistent").when(topics).domain();

    topics.validateTopicName(topicName.getTenant(), topicName.getCluster(),
            topicName.getNamespacePortion(), topicName.getEncodedLocalName());
    topics.validateAdminOperationOnTopic(false);
}
 
Example 10
Source File: AdminResource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected void validateTopicName(String property, String namespace, String encodedTopic) {
    String topic = Codec.decode(encodedTopic);
    try {
        this.namespaceName = NamespaceName.get(property, namespace);
        this.topicName = TopicName.get(domain(), namespaceName, topic);
    } catch (IllegalArgumentException e) {
        log.warn("[{}] Failed to validate topic name {}://{}/{}/{}", clientAppId(), domain(), property, namespace,
                topic, e);
        throw new RestException(Status.PRECONDITION_FAILED, "Topic name is not valid");
    }

    this.topicName = TopicName.get(domain(), namespaceName, topic);
}
 
Example 11
Source File: WebSocketProxyStatsV1.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/{tenant}/{cluster}/{namespace}/{topic}/stats")
@ApiOperation(value = "Get the stats for the topic.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"),
        @ApiResponse(code = 404, message = "Topic does not exist") })
public ProxyTopicStat getStats(@PathParam("tenant") String tenant, @PathParam("cluster") String cluster,
        @PathParam("namespace") String namespace, @PathParam("topic") @Encoded String encodedTopic) {
    return super.internalGetStats(TopicName.get("persistent", tenant, cluster, namespace, decode(encodedTopic)));
}
 
Example 12
Source File: LoadBalancerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testLoadReportsWrittenOnZK() throws Exception {
    ZooKeeper zkc = bkEnsemble.getZkClient();
    try {
        for (int i = 0; i < BROKER_COUNT; i++) {
            String znodePath = String.format("%s/%s", SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT,
                    lookupAddresses[i]);
            byte[] loadReportData = zkc.getData(znodePath, false, null);
            assert (loadReportData.length > 0);
            log.info("LoadReport {}, {}", lookupAddresses[i], new String(loadReportData));

            LoadReport loadReport = ObjectMapperFactory.getThreadLocal().readValue(loadReportData, LoadReport.class);
            assert (loadReport.getName().equals(lookupAddresses[i]));

            // Check Initial Ranking is populated in both the brokers
            Field ranking = ((SimpleLoadManagerImpl) pulsarServices[i].getLoadManager().get()).getClass()
                    .getDeclaredField("sortedRankings");
            ranking.setAccessible(true);
            AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRanking = (AtomicReference<Map<Long, Set<ResourceUnit>>>) ranking
                    .get(pulsarServices[i].getLoadManager().get());
            printSortedRanking(sortedRanking);

            // all brokers have same rank to it would be 0 --> set-of-all-the-brokers
            int brokerCount = 0;
            for (Map.Entry<Long, Set<ResourceUnit>> entry : sortedRanking.get().entrySet()) {
                brokerCount += entry.getValue().size();
            }
            assertEquals(brokerCount, BROKER_COUNT);
            TopicName topicName = TopicName.get("persistent://pulsar/use/primary-ns/test-topic");
            ResourceUnit found = pulsarServices[i].getLoadManager().get()
                    .getLeastLoaded(pulsarServices[i].getNamespaceService().getBundle(topicName)).get();
            assertNotNull(found);
        }
    } catch (InterruptedException | KeeperException e) {
        fail("Unable to read the data from Zookeeper - [{}]", e);
    }
}
 
Example 13
Source File: BrokerServiceTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicLoadingOnDisableNamespaceBundle() throws Exception {
    final String namespace = "prop/disableBundle";
    admin.namespaces().createNamespace(namespace);
    admin.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("test"));

    // own namespace bundle
    final String topicName = "persistent://" + namespace + "/my-topic";
    TopicName topic = TopicName.get(topicName);
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    producer.close();

    // disable namespace-bundle
    NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(topic);
    pulsar.getNamespaceService().getOwnershipCache().updateBundleState(bundle, false).join();

    // try to create topic which should fail as bundle is disable
    CompletableFuture<Optional<Topic>> futureResult = pulsar.getBrokerService()
            .loadOrCreatePersistentTopic(topicName, true);

    try {
        futureResult.get();
        fail("Topic creation should fail due to disable bundle");
    } catch (Exception e) {
        if (!(e.getCause() instanceof BrokerServiceException.ServiceUnitNotReadyException)) {
            fail("Topic creation should fail with ServiceUnitNotReadyException");
        }

    }
}
 
Example 14
Source File: NamespacesTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteNamespaces() throws Exception {
    AsyncResponse response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, this.testTenant, this.testLocalCluster, "non-existing-namespace-1", false);
    ArgumentCaptor<RestException> errorCaptor = ArgumentCaptor.forClass(RestException.class);
    verify(response, timeout(5000).times(1)).resume(errorCaptor.capture());
    assertEquals(errorCaptor.getValue().getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());

    NamespaceName testNs = this.testLocalNamespaces.get(1);
    TopicName topicName = TopicName.get(testNs.getPersistentTopicName("my-topic"));
    ZkUtils.createFullPathOptimistic(mockZooKeeper, "/managed-ledgers/" + topicName.getPersistenceNamingEncoding(),
            new byte[0], null, null);

    // setup ownership to localhost
    URL localWebServiceUrl = new URL(pulsar.getSafeWebServiceAddress());
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);

    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    errorCaptor = ArgumentCaptor.forClass(RestException.class);
    // Ok, namespace not empty
    verify(response, timeout(5000).times(1)).resume(errorCaptor.capture());
    assertEquals(errorCaptor.getValue().getResponse().getStatus(), Status.CONFLICT.getStatusCode());

    // delete the topic from ZK
    mockZooKeeper.delete("/managed-ledgers/" + topicName.getPersistenceNamingEncoding(), -1);

    ZkUtils.createFullPathOptimistic(mockZooKeeper,
            "/admin/partitioned-topics/" + topicName.getPersistenceNamingEncoding(),
            new byte[0], null, null);

    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    errorCaptor = ArgumentCaptor.forClass(RestException.class);
    // Ok, namespace not empty
    verify(response, timeout(5000).times(1)).resume(errorCaptor.capture());
    assertEquals(errorCaptor.getValue().getResponse().getStatus(), Status.CONFLICT.getStatusCode());

    mockZooKeeper.delete("/admin/partitioned-topics/" + topicName.getPersistenceNamingEncoding(), -1);

    testNs = this.testGlobalNamespaces.get(0);
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());

    testNs = this.testLocalNamespaces.get(0);
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());
    List<String> nsList = Lists.newArrayList(this.testLocalNamespaces.get(1).toString(),
            this.testLocalNamespaces.get(2).toString());
    nsList.sort(null);
    assertEquals(namespaces.getTenantNamespaces(this.testTenant), nsList);

    testNs = this.testLocalNamespaces.get(1);
    // ensure refreshed topics list in the cache
    pulsar.getLocalZkCacheService().managedLedgerListCache().clearTree();
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());
}
 
Example 15
Source File: PartitionedProducerConsumerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 30000)
public void testAsyncPartitionedProducerConsumer() throws Exception {
    log.info("-- Starting {} test --", methodName);

    final int totalMsg = 100;
    final Set<String> produceMsgs = Sets.newHashSet();
    final Set<String> consumeMsgs = Sets.newHashSet();

    int numPartitions = 4;
    TopicName topicName = TopicName
            .get("persistent://my-property/my-ns/my-partitionedtopic1-" + System.currentTimeMillis());

    admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName.toString())
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.RoundRobinPartition).create();

    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString())
            .subscriptionName("my-partitioned-subscriber").subscriptionType(SubscriptionType.Shared).subscribe();

    // produce messages
    for (int i = 0; i < totalMsg; i++) {
        String message = "my-message-" + i;
        produceMsgs.add(message);
        producer.send(message.getBytes());
    }

    log.info(" start receiving messages :");

    // receive messages
    CountDownLatch latch = new CountDownLatch(totalMsg);
    receiveAsync(consumer, totalMsg, 0, latch, consumeMsgs, executor);

    latch.await();

    // verify message produced correctly
    assertEquals(produceMsgs.size(), totalMsg);
    // verify produced and consumed messages must be exactly same
    produceMsgs.removeAll(consumeMsgs);
    assertTrue(produceMsgs.isEmpty());

    producer.close();
    consumer.unsubscribe();
    consumer.close();
    admin.topics().deletePartitionedTopic(topicName.toString());

    log.info("-- Exiting {} test --", methodName);
}
 
Example 16
Source File: BrokerServiceLookupTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * Create #PartitionedTopic and let it served by multiple brokers which requires a. tcp partitioned-metadata-lookup
 * b. multiple topic-lookup c. partitioned producer-consumer
 *
 * @throws Exception
 */
@Test
public void testPartitionTopicLookup() throws Exception {
    log.info("-- Starting {} test --", methodName);

    int numPartitions = 8;
    TopicName topicName = TopicName.get("persistent://my-property/my-ns/my-partitionedtopic1");

    admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);

    /**** start broker-2 ****/
    ServiceConfiguration conf2 = new ServiceConfiguration();
    conf2.setAdvertisedAddress("localhost");
    conf2.setBrokerServicePort(Optional.of(0));
    conf2.setWebServicePort(Optional.of(0));
    conf2.setAdvertisedAddress("localhost");
    conf2.setClusterName(pulsar.getConfiguration().getClusterName());
    conf2.setZookeeperServers("localhost:2181");

    @Cleanup
    PulsarService pulsar2 = startBroker(conf2);
    pulsar.getLoadManager().get().writeLoadReportOnZookeeper();
    pulsar2.getLoadManager().get().writeLoadReportOnZookeeper();

    LoadManager loadManager1 = spy(pulsar.getLoadManager().get());
    LoadManager loadManager2 = spy(pulsar2.getLoadManager().get());
    Field loadManagerField = NamespaceService.class.getDeclaredField("loadManager");
    loadManagerField.setAccessible(true);

    // mock: return Broker2 as a Least-loaded broker when leader receives request
    doReturn(true).when(loadManager1).isCentralized();
    loadManagerField.set(pulsar.getNamespaceService(), new AtomicReference<>(loadManager1));

    // mock: redirect request to leader
    doReturn(true).when(loadManager2).isCentralized();
    loadManagerField.set(pulsar2.getNamespaceService(), new AtomicReference<>(loadManager2));
    /**** broker-2 started ****/

    Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES)
        .topic(topicName.toString())
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.RoundRobinPartition).create();

    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString())
            .subscriptionName("my-partitioned-subscriber").subscribe();

    for (int i = 0; i < 20; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }

    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < 20; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        assertNotNull(msg, "Message should not be null");
        consumer.acknowledge(msg);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        assertTrue(messageSet.add(receivedMessage), "Message " + receivedMessage + " already received");
    }

    producer.close();
    consumer.unsubscribe();
    consumer.close();
    admin.topics().deletePartitionedTopic(topicName.toString());

    loadManager2 = null;

    log.info("-- Exiting {} test --", methodName);
}
 
Example 17
Source File: AbstractWebSocketHandler.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private TopicName extractTopicName(HttpServletRequest request) {
    String uri = request.getRequestURI();
    List<String> parts = Splitter.on("/").splitToList(uri);

    // V1 Format must be like :
    // /ws/producer/persistent/my-property/my-cluster/my-ns/my-topic
    // or
    // /ws/consumer/persistent/my-property/my-cluster/my-ns/my-topic/my-subscription
    // or
    // /ws/reader/persistent/my-property/my-cluster/my-ns/my-topic

    // V2 Format must be like :
    // /ws/v2/producer/persistent/my-property/my-ns/my-topic
    // or
    // /ws/v2/consumer/persistent/my-property/my-ns/my-topic/my-subscription
    // or
    // /ws/v2/reader/persistent/my-property/my-ns/my-topic

    checkArgument(parts.size() >= 8, "Invalid topic name format");
    checkArgument(parts.get(1).equals("ws"));

    final boolean isV2Format = parts.get(2).equals("v2");
    final int domainIndex = isV2Format ? 4 : 3;
    checkArgument(parts.get(domainIndex).equals("persistent") ||
            parts.get(domainIndex).equals("non-persistent"));


    final String domain = parts.get(domainIndex);
    final NamespaceName namespace = isV2Format ? NamespaceName.get(parts.get(5), parts.get(6)) :
            NamespaceName.get( parts.get(4), parts.get(5), parts.get(6));
    //The topic name which contains slashes is also split , so it needs to be jointed
    int startPosition = 7;
    boolean isConsumer = "consumer".equals(parts.get(2)) || "consumer".equals(parts.get(3));
    int endPosition = isConsumer ? parts.size() -1 : parts.size();
    StringBuilder topicName = new StringBuilder(parts.get(startPosition));
    while (++startPosition < endPosition) {
        if(StringUtils.isEmpty(parts.get(startPosition))){
           continue;
        }
        topicName.append("/").append(parts.get(startPosition));
    }
    final String name = topicName.toString();

    return TopicName.get(domain, namespace, name);
}
 
Example 18
Source File: NamespaceServiceTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testSplitMapWithRefreshedStatMap() throws Exception {

    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());

    ManagedLedger ledger = mock(ManagedLedger.class);
    when(ledger.getCursors()).thenReturn(Lists.newArrayList());

    doReturn(CompletableFuture.completedFuture(null)).when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
    Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
    ownership.setAccessible(true);
    ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);

    NamespaceService namespaceService = pulsar.getNamespaceService();
    NamespaceName nsname = NamespaceName.get("pulsar/global/ns1");
    TopicName topicName = TopicName.get("persistent://pulsar/global/ns1/topic-1");
    NamespaceBundles bundles = namespaceService.getNamespaceBundleFactory().getBundles(nsname);
    NamespaceBundle originalBundle = bundles.findBundle(topicName);

    PersistentTopic topic = new PersistentTopic(topicName.toString(), ledger, pulsar.getBrokerService());
    Method method = pulsar.getBrokerService().getClass().getDeclaredMethod("addTopicToStatsMaps",
            TopicName.class, Topic.class);
    method.setAccessible(true);
    method.invoke(pulsar.getBrokerService(), topicName, topic);
    String nspace = originalBundle.getNamespaceObject().toString();
    List<Topic> list = this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace,
            originalBundle.toString());
    assertNotNull(list);

    // Split bundle and take ownership of split bundles
    CompletableFuture<Void> result = namespaceService.splitAndOwnBundle(originalBundle, false, NamespaceBundleSplitAlgorithm.rangeEquallyDivide);
    try {
        result.get();
    } catch (Exception e) {
        // make sure: no failure
        fail("split bundle failed", e);
    }

    // old bundle should be removed from status-map
    list = this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace, originalBundle.toString());
    assertTrue(list.isEmpty());

    // status-map should be updated with new split bundles
    NamespaceBundle splitBundle = pulsar.getNamespaceService().getBundle(topicName);
    assertFalse(CollectionUtils.isEmpty(
        this.pulsar.getBrokerService()
            .getAllTopicsFromNamespaceBundle(nspace, splitBundle.toString())));

}
 
Example 19
Source File: TopicNameUtils.java    From kop with Apache License 2.0 4 votes vote down vote up
public static TopicName pulsarTopicName(String topic) {
    return TopicName.get(topic);
}
 
Example 20
Source File: MessageParserTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithBatches() throws Exception {
    String topic = "persistent://my-tenant/my-ns/my-topic-with-batch";
    TopicName topicName = TopicName.get(topic);

    int n = 10;

    Producer<String> producer = pulsarClient.newProducer(Schema.STRING).enableBatching(true)
            .batchingMaxPublishDelay(10, TimeUnit.SECONDS).topic(topic).create();

    ManagedCursor cursor = ((PersistentTopic) pulsar.getBrokerService().getTopicReference(topic).get())
            .getManagedLedger().newNonDurableCursor(PositionImpl.earliest);

    for (int i = 0; i < n - 1; i++) {
        producer.sendAsync("hello-" + i);
    }

    producer.send("hello-" + (n - 1));

    // Read through raw data
    assertEquals(cursor.getNumberOfEntriesInBacklog(false), 1);
    Entry entry = cursor.readEntriesOrWait(1).get(0);

    List<RawMessage> messages = Lists.newArrayList();

    try {
        MessageParser.parseMessage(topicName, entry.getLedgerId(), entry.getEntryId(), entry.getDataBuffer(),
                (message) -> {
                    messages.add(message);
                }, Commands.DEFAULT_MAX_MESSAGE_SIZE);
    } finally {
        entry.release();
    }

    assertEquals(messages.size(), 10);

    for (int i = 0; i < n; i++) {
        assertEquals(messages.get(i).getData(), Unpooled.wrappedBuffer(("hello-" + i).getBytes()));
    }

    messages.forEach(RawMessage::release);

    producer.close();
}