org.apache.pulsar.common.naming.TopicName Java Examples

The following examples show how to use org.apache.pulsar.common.naming.TopicName. 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: PulsarMetadata.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Convert pulsar schema into presto table metadata.
 */
static List<ColumnMetadata> getPulsarColumns(TopicName topicName,
                                             SchemaInfo schemaInfo,
                                             boolean withInternalColumns,
                                             PulsarColumnHandle.HandleKeyValueType handleKeyValueType) {
    SchemaType schemaType = schemaInfo.getType();
    if (schemaType.isStruct()) {
        return getPulsarColumnsFromStructSchema(topicName, schemaInfo, withInternalColumns, handleKeyValueType);
    } else if (schemaType.isPrimitive()) {
        return getPulsarColumnsFromPrimitiveSchema(topicName, schemaInfo, withInternalColumns, handleKeyValueType);
    } else if (schemaType.equals(SchemaType.KEY_VALUE)) {
        return getPulsarColumnsFromKeyValueSchema(topicName, schemaInfo, withInternalColumns);
    } else {
        throw new IllegalArgumentException("Unsupported schema : " + schemaInfo);
    }
}
 
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: SimpleSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSchemaByVersion() throws PulsarClientException, PulsarAdminException, ExecutionException, InterruptedException {
    final String topic = "persistent://my-property/my-ns/testGetSchemaByVersion";

    PulsarClientImpl httpProtocolClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    PulsarClientImpl binaryProtocolClient = (PulsarClientImpl) pulsarClient;

    pulsarClient.newProducer(Schema.AVRO(V1Data.class))
        .topic(topic)
        .create();

    pulsarClient.newProducer(Schema.AVRO(V2Data.class))
        .topic(topic)
        .create();

    LookupService httpLookupService = httpProtocolClient.getLookup();
    LookupService binaryLookupService = binaryProtocolClient.getLookup();
    Assert.assertTrue(httpLookupService instanceof HttpLookupService);
    Assert.assertTrue(binaryLookupService instanceof BinaryProtoLookupService);
    Assert.assertEquals(admin.schemas().getAllSchemas(topic).size(), 2);
    Assert.assertTrue(httpLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(0).array()).get().isPresent());
    Assert.assertTrue(httpLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(1).array()).get().isPresent());
    Assert.assertTrue(binaryLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(0).array()).get().isPresent());
    Assert.assertTrue(binaryLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(1).array()).get().isPresent());
}
 
Example #4
Source File: NonPersistentDispatcherSingleActiveConsumer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected boolean isConsumersExceededOnTopic() {
    Policies policies;
    try {
        // Use getDataIfPresent from zk cache to make the call non-blocking and prevent deadlocks in addConsumer
        policies = topic.getBrokerService().pulsar().getConfigurationCache().policiesCache()
                .getDataIfPresent(AdminResource.path(POLICIES, TopicName.get(topic.getName()).getNamespace()));

        if (policies == null) {
            policies = new Policies();
        }
    } catch (Exception e) {
        policies = new Policies();
    }
    final int maxConsumersPerTopic = policies.max_consumers_per_topic > 0 ?
            policies.max_consumers_per_topic :
            serviceConfig.getMaxConsumersPerTopic();
    if (maxConsumersPerTopic > 0 && maxConsumersPerTopic <= topic.getNumberOfConsumers()) {
        return true;
    }
    return false;
}
 
Example #5
Source File: NonPersistentTopicsImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<NonPersistentTopicStats> getStatsAsync(String topic) {
    TopicName topicName = validateTopic(topic);
    final CompletableFuture<NonPersistentTopicStats> future = new CompletableFuture<>();
    WebTarget path = topicPath(topicName, "stats");
    asyncGetRequest(path,
            new InvocationCallback<NonPersistentTopicStats>() {

                @Override
                public void completed(NonPersistentTopicStats response) {
                    future.complete(response);
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #6
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 #7
Source File: NonPersistentDispatcherSingleActiveConsumer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected boolean isConsumersExceededOnSubscription() {
    Policies policies;
    try {
        // Use getDataIfPresent from zk cache to make the call non-blocking and prevent deadlocks in addConsumer
        policies = topic.getBrokerService().pulsar().getConfigurationCache().policiesCache()
                .getDataIfPresent(AdminResource.path(POLICIES, TopicName.get(topic.getName()).getNamespace()));

        if (policies == null) {
            policies = new Policies();
        }
    } catch (Exception e) {
        policies = new Policies();
    }
    final int maxConsumersPerSubscription = policies.max_consumers_per_subscription > 0 ?
            policies.max_consumers_per_subscription :
            serviceConfig.getMaxConsumersPerSubscription();
    if (maxConsumersPerSubscription > 0 && maxConsumersPerSubscription <= consumers.size()) {
        return true;
    }
    return false;
}
 
Example #8
Source File: RawReaderImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
RawConsumerImpl(PulsarClientImpl client, ConsumerConfigurationData<byte[]> conf,
        CompletableFuture<Consumer<byte[]>> consumerFuture) {
    super(client,
        conf.getSingleTopic(),
        conf,
        client.externalExecutorProvider().getExecutor(),
        TopicName.getPartitionIndex(conf.getSingleTopic()),
        false,
        consumerFuture,
        MessageId.earliest,
        0 /* startMessageRollbackDurationInSec */,
        Schema.BYTES, null,
        true
    );
    incomingRawMessages = new GrowableArrayBlockingQueue<>();
    pendingRawReceives = new ConcurrentLinkedQueue<>();
}
 
Example #9
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadataAsync(String topic) {
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn, "partitions");
    final CompletableFuture<PartitionedTopicMetadata> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<PartitionedTopicMetadata>() {

                @Override
                public void completed(PartitionedTopicMetadata response) {
                    future.complete(response);
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #10
Source File: TopicsServiceImpl.java    From pulsar-manager with Apache License 2.0 6 votes vote down vote up
private Map<String, List<String>> parseTopics(List<String> topics) {
    Map<String, List<String>> result = new HashMap<>();
    List<String> persistentTopics = new ArrayList<>();
    List<String> nonPersistentTopics = new ArrayList<>();

    for (String topic : topics) {
        TopicName topicName  = TopicName.get(topic);
        if (TopicDomain.persistent.equals(topicName.getDomain())) {
            persistentTopics.add(topic);
        } else {
            nonPersistentTopics.add(topic);
        }
    }

    result.put(TopicDomain.persistent.toString(), persistentTopics);
    result.put(TopicDomain.non_persistent.toString(), nonPersistentTopics);
    return result;
}
 
Example #11
Source File: TopicNameUtilsTest.java    From kop with Apache License 2.0 6 votes vote down vote up
@Test(timeOut = 20000)
public void testTopicNameConvert() throws Exception {
    String topicName = "kopTopicNameConvert";
    int partitionNumber = 77;
    TopicPartition topicPartition = new TopicPartition(topicName, partitionNumber);

    String tenantName = "tenant_name";
    String nsName = "ns_name";
    NamespaceName ns = NamespaceName.get(tenantName, nsName);
    String expectedPulsarName = "persistent://" + tenantName + "/" + nsName + "/"
        + topicName + PARTITIONED_TOPIC_SUFFIX + partitionNumber;

    TopicName topicName1 = TopicNameUtils.pulsarTopicName(topicPartition, ns);
    TopicName topicName2 = TopicNameUtils.pulsarTopicName(topicName, partitionNumber, ns);

    assertTrue(topicName1.toString().equals(expectedPulsarName));
    assertTrue(topicName2.toString().equals(expectedPulsarName));

    TopicName topicName3 = TopicNameUtils.pulsarTopicName(topicName, ns);
    String expectedPulsarName3 = "persistent://" + tenantName + "/" + nsName + "/"
        + topicName;
    assertTrue(topicName3.toString().equals(expectedPulsarName3));
}
 
Example #12
Source File: MessageParser.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static ByteBuf uncompressPayloadIfNeeded(TopicName topic, MessageMetadata msgMetadata,
        ByteBuf payload, long ledgerId, long entryId, int maxMessageSize) {
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(msgMetadata.getCompression());
    int uncompressedSize = msgMetadata.getUncompressedSize();
    int payloadSize = payload.readableBytes();
    if (payloadSize > maxMessageSize) {
        // payload size is itself corrupted since it cannot be bigger than the MaxMessageSize
        log.error("[{}] Got corrupted payload message size {} at {}:{}", topic, payloadSize,
                ledgerId, entryId);
        return null;
    }

    try {
        ByteBuf uncompressedPayload = codec.decode(payload, uncompressedSize);
        return uncompressedPayload;
    } catch (IOException e) {
        log.error("[{}] Failed to decompress message with {} at {}:{} : {}", topic,
                msgMetadata.getCompression(), ledgerId, entryId, e.getMessage(), e);
        return null;
    }
}
 
Example #13
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<List<String>> getSubscriptionsAsync(String topic) {
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn, "subscriptions");
    final CompletableFuture<List<String>> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<List<String>>() {

                @Override
                public void completed(List<String> response) {
                    future.complete(response);
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #14
Source File: WebSocketWebResource.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if user has super-user access or user is authorized to produce/consume on a given topic
 *
 * @param topic
 * @throws RestException
 */
protected void validateUserAccess(TopicName topic) {
    boolean isAuthorized = false;

    try {
        validateSuperUserAccess();
        isAuthorized = true;
    } catch (Exception e) {
        try {
            isAuthorized = isAuthorized(topic);
        } catch (Exception ne) {
            throw new RestException(ne);
        }
    }

    if (!isAuthorized) {
        throw new RestException(Status.UNAUTHORIZED, "Don't have permission to access this topic");
    }
}
 
Example #15
Source File: ProxyTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSchema() throws Exception {
    @Cleanup
    PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl())
            .build();
    Schema<Foo> schema = Schema.AVRO(Foo.class);
    try {
        try (Producer<Foo> ignored = client.newProducer(schema).topic("persistent://sample/test/local/get-schema")
            .create()) {
        }
    } catch (Exception ex) {
        Assert.fail("Should not have failed since can acquire LookupRequestSemaphore");
    }
    byte[] schemaVersion = new byte[8];
    byte b = new Long(0l).byteValue();
    for (int i = 0; i<8; i++){
        schemaVersion[i] = b;
    }
    SchemaInfo schemaInfo = ((PulsarClientImpl) client).getLookup()
            .getSchema(TopicName.get("persistent://sample/test/local/get-schema"), schemaVersion).get().orElse(null);
    Assert.assertEquals(new String(schemaInfo.getSchema()), new String(schema.getSchemaInfo().getSchema()));
}
 
Example #16
Source File: SchemasImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<List<SchemaInfo>> getAllSchemasAsync(String topic) {
    WebTarget path = schemasPath(TopicName.get(topic));
    TopicName topicName = TopicName.get(topic);
    final CompletableFuture<List<SchemaInfo>> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<GetAllVersionsSchemaResponse>() {
                @Override
                public void completed(GetAllVersionsSchemaResponse response) {
                    future.complete(
                            response.getGetSchemaResponses().stream()
                                    .map(getSchemaResponse ->
                                            convertGetSchemaResponseToSchemaInfo(topicName, getSchemaResponse))
                                    .collect(Collectors.toList()));
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #17
Source File: AuthorizationProducerConsumerTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthData() throws Exception {
    log.info("-- Starting {} test --", methodName);

    conf.setAuthorizationProvider(TestAuthorizationProviderWithGrantPermission.class.getName());
    setup();

    AuthorizationService authorizationService = new AuthorizationService(conf, null);
    TopicName topicName = TopicName.get("persistent://prop/cluster/ns/t1");
    String role = "test-role";
    authorizationService.grantPermissionAsync(topicName, null, role, "auth-json").get();
    Assert.assertEquals(TestAuthorizationProviderWithGrantPermission.authDataJson, "auth-json");
    Assert.assertTrue(authorizationService.canProduce(topicName, role, new AuthenticationDataCommand("prod-auth")));
    Assert.assertEquals(TestAuthorizationProviderWithGrantPermission.authenticationData.getCommandData(),
            "prod-auth");
    Assert.assertTrue(
            authorizationService.canConsume(topicName, role, new AuthenticationDataCommand("cons-auth"), "sub1"));
    Assert.assertEquals(TestAuthorizationProviderWithGrantPermission.authenticationData.getCommandData(),
            "cons-auth");

    log.info("-- Exiting {} test --", methodName);
}
 
Example #18
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<MessageId> getLastMessageIdAsync(String topic) {
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn, "lastMessageId");
    final CompletableFuture<MessageId> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<BatchMessageIdImpl>() {

                @Override
                public void completed(BatchMessageIdImpl response) {
                    if (response.getBatchIndex() == -1) {
                        future.complete(new MessageIdImpl(response.getLedgerId(),
                                response.getEntryId(), response.getPartitionIndex()));
                    }
                    future.complete(response);
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #19
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<Message<byte[]>> getRemoteMessageById(String topic, long ledgerId, long entryId) {
    TopicName topicName = validateTopic(topic);
    WebTarget path = topicPath(topicName, "ledger", Long.toString(ledgerId), "entry", Long.toString(entryId));
    final CompletableFuture<Message<byte[]>> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<Response>() {
                @Override
                public void completed(Response response) {
                    try {
                        future.complete(getMessagesFromHttpResponse(topicName.toString(), response).get(0));
                    } catch (Exception e) {
                        future.completeExceptionally(getApiException(e));
                    }
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #20
Source File: ServerCnxTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 30000)
public void testSendFailureOnEncryptionRequiredTopic() throws Exception {
    resetChannel();
    setChannelConnected();

    // Set encryption_required to true
    ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
    Policies policies = mock(Policies.class);
    policies.encryption_required = true;
    policies.topicDispatchRate = Maps.newHashMap();
    // add `clusterDispatchRate` otherwise there will be a NPE
    // `org.apache.pulsar.broker.service.persistent.DispatchRateLimiter.getPoliciesDispatchRate`
    policies.clusterDispatchRate = Maps.newHashMap();
    doReturn(Optional.of(policies)).when(zkDataCache).get(AdminResource.path(POLICIES, TopicName.get(encryptionRequiredTopicName).getNamespace()));
    doReturn(CompletableFuture.completedFuture(Optional.of(policies))).when(zkDataCache).getAsync(AdminResource.path(POLICIES, TopicName.get(encryptionRequiredTopicName).getNamespace()));
    doReturn(zkDataCache).when(configCacheService).policiesCache();

    ByteBuf clientCommand = Commands.newProducer(encryptionRequiredTopicName, 1 /* producer id */, 1 /* request id */,
            "prod-name", true, null);
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandProducerSuccess);

    // test failure case: unencrypted messages cannot be published
    MessageMetadata messageMetadata = MessageMetadata.newBuilder()
            .setPublishTime(System.currentTimeMillis())
            .setProducerName("prod-name")
            .setSequenceId(0)
            .build();
    ByteBuf data = Unpooled.buffer(1024);

    clientCommand = ByteBufPair.coalesce(Commands.newSend(1, 0, 1, ChecksumType.None, messageMetadata, data));
    channel.writeInbound(Unpooled.copiedBuffer(clientCommand));
    clientCommand.release();
    assertTrue(getResponse() instanceof CommandSendError);
    channel.finish();
}
 
Example #21
Source File: AdminApiTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamespaceSplitBundleWithDefaultTopicCountEquallyDivideAlgorithm() throws Exception {
    conf.setDefaultNamespaceBundleSplitAlgorithm(NamespaceBundleSplitAlgorithm.topicCountEquallyDivideName);
    // Force to create a topic
    final String namespace = "prop-xyz/ns1";
    List<String> topicNames = Lists.newArrayList(
        (new StringBuilder("persistent://")).append(namespace).append("/topicCountEquallyDivideAlgorithum-1").toString(),
        (new StringBuilder("persistent://")).append(namespace).append("/topicCountEquallyDivideAlgorithum-2").toString());

    List<Producer<byte[]>> producers = new ArrayList<>(2);
    for (String topicName : topicNames) {
        Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES)
            .topic(topicName)
            .enableBatching(false)
            .messageRoutingMode(MessageRoutingMode.SinglePartition)
            .create();
        producers.add(producer);
        producer.send("message".getBytes());
    }

    assertTrue(admin.topics().getList(namespace).containsAll(topicNames));

    try {
        admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", true, null);
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }
    NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    NamespaceBundle bundle1 = pulsar.getNamespaceService().getBundle(TopicName.get(topicNames.get(0)));
    NamespaceBundle bundle2 = pulsar.getNamespaceService().getBundle(TopicName.get(topicNames.get(1)));
    assertNotEquals(bundle1, bundle2);
    String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertNotEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
    }
    producers.forEach(Producer::closeAsync);
    conf.setDefaultNamespaceBundleSplitAlgorithm(NamespaceBundleSplitAlgorithm.rangeEquallyDivideName);
}
 
Example #22
Source File: BrokerService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public boolean isAllowAutoTopicCreation(final TopicName topicName) {
    AutoTopicCreationOverride autoTopicCreationOverride = getAutoTopicCreationOverride(topicName);
    if (autoTopicCreationOverride != null) {
        return autoTopicCreationOverride.allowAutoTopicCreation;
    } else {
        return pulsar.getConfiguration().isAllowAutoTopicCreation();
    }
}
 
Example #23
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return Backlog quota for topic
 */
@Override
public BacklogQuota getBacklogQuota() {
    TopicName topicName = TopicName.get(this.getName());
    String namespace = topicName.getNamespace();
    String policyPath = AdminResource.path(POLICIES, namespace);

    BacklogQuota backlogQuota = brokerService.getBacklogQuotaManager().getBacklogQuota(namespace, policyPath);
    return backlogQuota;
}
 
Example #24
Source File: BrokerService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public boolean isAllowAutoSubscriptionCreation(final TopicName topicName) {
    AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = getAutoSubscriptionCreationOverride(topicName);
    if (autoSubscriptionCreationOverride != null) {
        return autoSubscriptionCreationOverride.allowAutoSubscriptionCreation;
    } else {
        return pulsar.getConfiguration().isAllowAutoSubscriptionCreation();
    }
}
 
Example #25
Source File: NonPersistentTopic.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public boolean isActive() {
    if (TopicName.get(topic).isGlobal()) {
        // No local consumers and no local producers
        return !subscriptions.isEmpty() || hasLocalProducers();
    }
    return USAGE_COUNT_UPDATER.get(this) != 0 || !subscriptions.isEmpty();
}
 
Example #26
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> skipMessagesAsync(String topic, String subName, long numMessages) {
    TopicName tn = validateTopic(topic);
    String encodedSubName = Codec.encode(subName);
    WebTarget path = topicPath(tn, "subscription", encodedSubName, "skip", String.valueOf(numMessages));
    return asyncPostRequest(path, Entity.entity("", MediaType.APPLICATION_JSON));
}
 
Example #27
Source File: DispatchRateLimiter.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static Optional<Policies> getPolicies(BrokerService brokerService, String topicName) {
    final NamespaceName namespace = TopicName.get(topicName).getNamespaceObject();
    final String path = path(POLICIES, namespace.toString());
    Optional<Policies> policies = Optional.empty();
    try {
        policies = brokerService.pulsar().getConfigurationCache().policiesCache().getAsync(path)
                .get(brokerService.pulsar().getConfiguration().getZooKeeperOperationTimeoutSeconds(), SECONDS);
    } catch (Exception e) {
        log.warn("Failed to get message-rate for {} ", topicName, e);
    }
    return policies;
}
 
Example #28
Source File: BrokerDiscoveryProvider.java    From pulsar with Apache License 2.0 5 votes vote down vote up
CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(ProxyService service,
        TopicName topicName, String role, AuthenticationDataSource authenticationData) {

    CompletableFuture<PartitionedTopicMetadata> metadataFuture = new CompletableFuture<>();
    try {
        checkAuthorization(service, topicName, role, authenticationData);
        final String path = path(PARTITIONED_TOPIC_PATH_ZNODE,
                topicName.getNamespaceObject().toString(), "persistent", topicName.getEncodedLocalName());
        // gets the number of partitions from the zk cache
        globalZkCache
                .getDataAsync(path,
                        (key, content) -> getThreadLocal().readValue(content, PartitionedTopicMetadata.class))
                .thenAccept(metadata -> {
                    // if the partitioned topic is not found in zk, then the topic
                    // is not partitioned
                    if (metadata.isPresent()) {
                        metadataFuture.complete(metadata.get());
                    } else {
                        metadataFuture.complete(new PartitionedTopicMetadata());
                    }
                }).exceptionally(ex -> {
                    metadataFuture.completeExceptionally(ex);
                    return null;
                });
    } catch (Exception e) {
        metadataFuture.completeExceptionally(e);
    }
    return metadataFuture;
}
 
Example #29
Source File: AdminResource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected static PartitionedTopicMetadata fetchPartitionedTopicMetadata(PulsarService pulsar, TopicName topicName) {
    try {
        return pulsar.getBrokerService().fetchPartitionedTopicMetadataAsync(topicName).get();
    } catch (Exception e) {
        if (e.getCause() instanceof RestException) {
            throw (RestException) e;
        }
        throw new RestException(e);
    }
}
 
Example #30
Source File: AuthorizationService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public Boolean allowTopicOperation(TopicName topicName, TopicOperation operation,
                                     String orignalRole, String role,
                                     AuthenticationDataSource authData) {
    if (!this.conf.isAuthorizationEnabled()) {
        return true;
    }

    if (provider != null) {
        return provider.allowTopicOperation(topicName, orignalRole, role, operation, authData);
    }

    throw new IllegalStateException("No authorization provider configured for allowTopicOperation");
}