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

The following examples show how to use org.apache.pulsar.common.naming.TopicName#getLocalName() . 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: TestPulsarSplitManager.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "rewriteNamespaceDelimiter", singleThreaded = true)
public void testGetSplitNonSchema(String delimiter) throws Exception {
    updateRewriteNamespaceDelimiterIfNeeded(delimiter);
    TopicName topicName = NON_SCHEMA_TOPIC;
    setup();
    log.info("!----- topic: %s -----!", topicName);
    PulsarTableHandle pulsarTableHandle = new PulsarTableHandle(pulsarConnectorId.toString(),
        topicName.getNamespace(),
        topicName.getLocalName(),
        topicName.getLocalName());

    Map<ColumnHandle, Domain> domainMap = new HashMap<>();
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(domainMap);

    PulsarTableLayoutHandle pulsarTableLayoutHandle = new PulsarTableLayoutHandle(pulsarTableHandle, tupleDomain);
    ConnectorSplitSource connectorSplitSource = this.pulsarSplitManager.getSplits(
        mock(ConnectorTransactionHandle.class), mock(ConnectorSession.class),
        pulsarTableLayoutHandle, null);
    assertNotNull(connectorSplitSource);
}
 
Example 2
Source File: PersistentTopicsBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Validate non partition topic name,
 * Validation will fail and throw RestException if
 * 1) Topic name contains partition suffix "-partition-" and the remaining part follow the partition
 * suffix is numeric value larger than the number of partition if there's already a partition topic with same
 * name(the part before suffix "-partition-").
 * 2)Topic name contains partition suffix "-partition-" and the remaining part follow the partition
 * suffix is numeric value but there isn't a partitioned topic with same name.
 *
 * @param topicName
 */
private void validateNonPartitionTopicName(String topicName) {
    if (topicName.contains(TopicName.PARTITIONED_TOPIC_SUFFIX)) {
        try {
            // First check if what's after suffix "-partition-" is number or not, if not number then can create.
            int partitionIndex = topicName.indexOf(TopicName.PARTITIONED_TOPIC_SUFFIX);
            long suffix = Long.parseLong(topicName.substring(partitionIndex
                    + TopicName.PARTITIONED_TOPIC_SUFFIX.length()));
            TopicName partitionTopicName = TopicName.get(domain(), namespaceName, topicName.substring(0, partitionIndex));
            PartitionedTopicMetadata metadata = getPartitionedTopicMetadata(partitionTopicName, false, false);

            // Partition topic index is 0 to (number of partition - 1)
            if (metadata.partitions > 0 && suffix >= (long) metadata.partitions) {
                log.warn("[{}] Can't create topic {} with \"-partition-\" followed by" +
                        " a number smaller then number of partition of partitioned topic {}.",
                        clientAppId(), topicName, partitionTopicName.getLocalName());
                throw new RestException(Status.PRECONDITION_FAILED,
                        "Can't create topic " + topicName + " with \"-partition-\" followed by" +
                        " a number smaller then number of partition of partitioned topic " +
                                partitionTopicName.getLocalName());
            } else if (metadata.partitions == 0) {
                log.warn("[{}] Can't create topic {} with \"-partition-\" followed by" +
                                " numeric value if there isn't a partitioned topic {} created.",
                        clientAppId(), topicName, partitionTopicName.getLocalName());
                throw new RestException(Status.PRECONDITION_FAILED,
                        "Can't create topic " + topicName + " with \"-partition-\" followed by" +
                                " numeric value if there isn't a partitioned topic " +
                                partitionTopicName.getLocalName() + " created.");
            }
            // If there is a  partitioned topic with the same name and numeric suffix is smaller than the
            // number of partition for that partitioned topic, validation will pass.
        } catch (NumberFormatException e) {
            // Do nothing, if value after partition suffix is not pure numeric value,
            // as it can't conflict if user want to create partitioned topic with same
            // topic name prefix in the future.
        }
    }
}
 
Example 3
Source File: TestPulsarSplitManager.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "rewriteNamespaceDelimiter", singleThreaded = true)
public void testPartitionedTopic(String delimiter) throws Exception {
    updateRewriteNamespaceDelimiterIfNeeded(delimiter);
    for (TopicName topicName : partitionedTopicNames) {
        setup();
        log.info("!----- topic: %s -----!", topicName);
        PulsarTableHandle pulsarTableHandle = new PulsarTableHandle(pulsarConnectorId.toString(),
                topicName.getNamespace(),
                topicName.getLocalName(),
                topicName.getLocalName());
        PulsarTableLayoutHandle pulsarTableLayoutHandle = new PulsarTableLayoutHandle(pulsarTableHandle, TupleDomain.all());

        final ResultCaptor<Collection<PulsarSplit>> resultCaptor = new ResultCaptor<>();
        doAnswer(resultCaptor).when(this.pulsarSplitManager).getSplitsPartitionedTopic(anyInt(), any(), any(), any(), any(), any());

        this.pulsarSplitManager.getSplits(mock(ConnectorTransactionHandle.class), mock(ConnectorSession.class),
                pulsarTableLayoutHandle, null);

        verify(this.pulsarSplitManager, times(1))
                .getSplitsPartitionedTopic(anyInt(), any(), any(), any(), any(), any());

        int partitions = partitionedTopicsToPartitions.get(topicName.toString());

        for (int i = 0; i < partitions; i++) {
            List<PulsarSplit> splits = getSplitsForPartition(topicName.getPartition(i), resultCaptor.getResult());
            int totalSize = 0;
            for (PulsarSplit pulsarSplit : splits) {
                assertEquals(pulsarSplit.getConnectorId(), pulsarConnectorId.toString());
                assertEquals(pulsarSplit.getSchemaName(), topicName.getNamespace());
                assertEquals(pulsarSplit.getTableName(), topicName.getPartition(i).getLocalName());
                assertEquals(pulsarSplit.getSchema(),
                        new String(topicsToSchemas.get(topicName.getSchemaName()).getSchema()));
                assertEquals(pulsarSplit.getSchemaType(), topicsToSchemas.get(topicName.getSchemaName()).getType());
                assertEquals(pulsarSplit.getStartPositionEntryId(), totalSize);
                assertEquals(pulsarSplit.getStartPositionLedgerId(), 0);
                assertEquals(pulsarSplit.getStartPosition(), PositionImpl.get(0, totalSize));
                assertEquals(pulsarSplit.getEndPositionLedgerId(), 0);
                assertEquals(pulsarSplit.getEndPositionEntryId(), totalSize + pulsarSplit.getSplitSize());
                assertEquals(pulsarSplit.getEndPosition(), PositionImpl.get(0, totalSize + pulsarSplit.getSplitSize()));

                totalSize += pulsarSplit.getSplitSize();
            }

            assertEquals(totalSize, topicsToNumEntries.get(topicName.getSchemaName()).intValue());
        }

        cleanup();
    }
}
 
Example 4
Source File: TestPulsarSplitManager.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "rewriteNamespaceDelimiter", singleThreaded = true)
public void testPublishTimePredicatePushdown(String delimiter) throws Exception {
    updateRewriteNamespaceDelimiterIfNeeded(delimiter);
    TopicName topicName = TOPIC_1;

    setup();
    log.info("!----- topic: %s -----!", topicName);
    PulsarTableHandle pulsarTableHandle = new PulsarTableHandle(pulsarConnectorId.toString(),
            topicName.getNamespace(),
            topicName.getLocalName(),
            topicName.getLocalName());


    Map<ColumnHandle, Domain> domainMap = new HashMap<>();
    Domain domain = Domain.create(ValueSet.ofRanges(Range.range(TIMESTAMP, currentTimeMs + 1L, true,
            currentTimeMs + 50L, true)), false);
    domainMap.put(PulsarInternalColumn.PUBLISH_TIME.getColumnHandle(pulsarConnectorId.toString(), false), domain);
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(domainMap);

    PulsarTableLayoutHandle pulsarTableLayoutHandle = new PulsarTableLayoutHandle(pulsarTableHandle, tupleDomain);

    final ResultCaptor<Collection<PulsarSplit>> resultCaptor = new ResultCaptor<>();
    doAnswer(resultCaptor).when(this.pulsarSplitManager)
            .getSplitsNonPartitionedTopic(anyInt(), any(), any(), any(), any(), any());

    ConnectorSplitSource connectorSplitSource = this.pulsarSplitManager.getSplits(
            mock(ConnectorTransactionHandle.class), mock(ConnectorSession.class),
            pulsarTableLayoutHandle, null);


    verify(this.pulsarSplitManager, times(1))
            .getSplitsNonPartitionedTopic(anyInt(), any(), any(), any(), any(), any());

    int totalSize = 0;
    int initalStart = 1;
    for (PulsarSplit pulsarSplit : resultCaptor.getResult()) {
        assertEquals(pulsarSplit.getConnectorId(), pulsarConnectorId.toString());
        assertEquals(pulsarSplit.getSchemaName(), topicName.getNamespace());
        assertEquals(pulsarSplit.getTableName(), topicName.getLocalName());
        assertEquals(pulsarSplit.getSchema(),
                new String(topicsToSchemas.get(topicName.getSchemaName()).getSchema()));
        assertEquals(pulsarSplit.getSchemaType(), topicsToSchemas.get(topicName.getSchemaName()).getType());
        assertEquals(pulsarSplit.getStartPositionEntryId(), initalStart);
        assertEquals(pulsarSplit.getStartPositionLedgerId(), 0);
        assertEquals(pulsarSplit.getStartPosition(), PositionImpl.get(0, initalStart));
        assertEquals(pulsarSplit.getEndPositionLedgerId(), 0);
        assertEquals(pulsarSplit.getEndPositionEntryId(), initalStart + pulsarSplit.getSplitSize());
        assertEquals(pulsarSplit.getEndPosition(), PositionImpl.get(0, initalStart + pulsarSplit
                .getSplitSize()));

        initalStart += pulsarSplit.getSplitSize();
        totalSize += pulsarSplit.getSplitSize();
    }
    assertEquals(totalSize, 49);

}
 
Example 5
Source File: TestPulsarSplitManager.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "rewriteNamespaceDelimiter", singleThreaded = true)
public void testPublishTimePredicatePushdownPartitionedTopic(String delimiter) throws Exception {
    updateRewriteNamespaceDelimiterIfNeeded(delimiter);
    TopicName topicName = PARTITIONED_TOPIC_1;

    setup();
    log.info("!----- topic: %s -----!", topicName);
    PulsarTableHandle pulsarTableHandle = new PulsarTableHandle(pulsarConnectorId.toString(),
            topicName.getNamespace(),
            topicName.getLocalName(),
            topicName.getLocalName());


    Map<ColumnHandle, Domain> domainMap = new HashMap<>();
    Domain domain = Domain.create(ValueSet.ofRanges(Range.range(TIMESTAMP, currentTimeMs + 1L, true,
            currentTimeMs + 50L, true)), false);
    domainMap.put(PulsarInternalColumn.PUBLISH_TIME.getColumnHandle(pulsarConnectorId.toString(), false), domain);
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(domainMap);

    PulsarTableLayoutHandle pulsarTableLayoutHandle = new PulsarTableLayoutHandle(pulsarTableHandle, tupleDomain);

    final ResultCaptor<Collection<PulsarSplit>> resultCaptor = new ResultCaptor<>();
    doAnswer(resultCaptor).when(this.pulsarSplitManager)
            .getSplitsPartitionedTopic(anyInt(), any(), any(), any(), any(), any());

    ConnectorSplitSource connectorSplitSource = this.pulsarSplitManager.getSplits(
            mock(ConnectorTransactionHandle.class), mock(ConnectorSession.class),
            pulsarTableLayoutHandle, null);


    verify(this.pulsarSplitManager, times(1))
            .getSplitsPartitionedTopic(anyInt(), any(), any(), any(), any(), any());


    int partitions = partitionedTopicsToPartitions.get(topicName.toString());
    for (int i = 0; i < partitions; i++) {
        List<PulsarSplit> splits = getSplitsForPartition(topicName.getPartition(i), resultCaptor.getResult());
        int totalSize = 0;
        int initialStart = 1;
        for (PulsarSplit pulsarSplit : splits) {
            assertEquals(pulsarSplit.getConnectorId(), pulsarConnectorId.toString());
            assertEquals(pulsarSplit.getSchemaName(), topicName.getNamespace());
            assertEquals(pulsarSplit.getTableName(), topicName.getPartition(i).getLocalName());
            assertEquals(pulsarSplit.getSchema(),
                    new String(topicsToSchemas.get(topicName.getSchemaName()).getSchema()));
            assertEquals(pulsarSplit.getSchemaType(), topicsToSchemas.get(topicName.getSchemaName()).getType());
            assertEquals(pulsarSplit.getStartPositionEntryId(), initialStart);
            assertEquals(pulsarSplit.getStartPositionLedgerId(), 0);
            assertEquals(pulsarSplit.getStartPosition(), PositionImpl.get(0, initialStart));
            assertEquals(pulsarSplit.getEndPositionLedgerId(), 0);
            assertEquals(pulsarSplit.getEndPositionEntryId(), initialStart + pulsarSplit.getSplitSize());
            assertEquals(pulsarSplit.getEndPosition(), PositionImpl.get(0, initialStart + pulsarSplit.getSplitSize()));

            initialStart += pulsarSplit.getSplitSize();
            totalSize += pulsarSplit.getSplitSize();
        }

        assertEquals(totalSize, 49);
    }
}
 
Example 6
Source File: TestPulsarMetadata.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "rewriteNamespaceDelimiter", singleThreaded = true)
public void testGetTableMetadata(String delimiter) {
    updateRewriteNamespaceDelimiterIfNeeded(delimiter);
    List<TopicName> allTopics = new LinkedList<>();
    allTopics.addAll(topicNames.stream().filter(topicName -> !topicName.equals(NON_SCHEMA_TOPIC)).collect(Collectors.toList()));
    allTopics.addAll(partitionedTopicNames);

    for (TopicName topic : allTopics) {
        PulsarTableHandle pulsarTableHandle = new PulsarTableHandle(
                topic.toString(),
                topic.getNamespace(),
                topic.getLocalName(),
                topic.getLocalName()
        );

        ConnectorTableMetadata tableMetadata = this.pulsarMetadata.getTableMetadata(mock(ConnectorSession.class),
                pulsarTableHandle);

        assertEquals(tableMetadata.getTable().getSchemaName(), topic.getNamespace());
        assertEquals(tableMetadata.getTable().getTableName(), topic.getLocalName());
        assertEquals(tableMetadata.getColumns().size(),
                fooColumnHandles.size());

        List<String> fieldNames = new LinkedList<>(fooFieldNames.keySet());

        for (PulsarInternalColumn internalField : PulsarInternalColumn.getInternalFields()) {
            fieldNames.add(internalField.getName());
        }

        for (ColumnMetadata column : tableMetadata.getColumns()) {
            if (PulsarInternalColumn.getInternalFieldsMap().containsKey(column.getName())) {
                assertEquals(column.getComment(),
                        PulsarInternalColumn.getInternalFieldsMap()
                                .get(column.getName()).getColumnMetadata(true).getComment());
            }

            fieldNames.remove(column.getName());
        }

        assertTrue(fieldNames.isEmpty());
    }
}