Search in sources :

Example 1 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarSourceEnumStateSerializer method deserialize.

@Override
public PulsarSourceEnumState deserialize(int version, byte[] serialized) throws IOException {
    // VERSION 0 deserialization
    try (ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
        DataInputStream in = new DataInputStream(bais)) {
        Set<TopicPartition> partitions = deserializeSet(in, deserializePartition(version));
        Set<PulsarPartitionSplit> splits = deserializeSet(in, deserializeSplit(version));
        Map<Integer, Set<PulsarPartitionSplit>> sharedSplits = deserializeMap(in, DataInput::readInt, i -> deserializeSet(i, deserializeSplit(version)));
        Map<Integer, Set<String>> mapping = deserializeMap(in, DataInput::readInt, i -> deserializeSet(i, DataInput::readUTF));
        boolean initialized = in.readBoolean();
        return new PulsarSourceEnumState(partitions, splits, sharedSplits, mapping, initialized);
    }
}
Also used : PulsarSerdeUtils.deserializeSet(org.apache.flink.connector.pulsar.common.utils.PulsarSerdeUtils.deserializeSet) Set(java.util.Set) PulsarSerdeUtils.serializeSet(org.apache.flink.connector.pulsar.common.utils.PulsarSerdeUtils.serializeSet) DataInputStream(java.io.DataInputStream) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit) DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)

Example 2 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class SplitsAssignmentState method assignSharedSplits.

/**
 * Every split would be shared among available readers.
 */
private Map<Integer, List<PulsarPartitionSplit>> assignSharedSplits(List<Integer> pendingReaders) {
    Map<Integer, List<PulsarPartitionSplit>> assignMap = new HashMap<>();
    // Drain the splits from share pending list.
    for (Integer reader : pendingReaders) {
        Set<PulsarPartitionSplit> pendingSplits = sharedPendingPartitionSplits.remove(reader);
        if (pendingSplits == null) {
            pendingSplits = new HashSet<>();
        }
        Set<String> assignedSplits = readerAssignedSplits.computeIfAbsent(reader, r -> new HashSet<>());
        for (TopicPartition partition : appendedPartitions) {
            String partitionName = partition.toString();
            if (!assignedSplits.contains(partitionName)) {
                pendingSplits.add(createSplit(partition));
                assignedSplits.add(partitionName);
            }
        }
        if (!pendingSplits.isEmpty()) {
            assignMap.put(reader, new ArrayList<>(pendingSplits));
        }
    }
    return assignMap;
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 3 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarSubscriberTest method topicListSubscriber.

@Test
void topicListSubscriber() {
    operator().createTopic(TOPIC1, NUM_PARTITIONS_PER_TOPIC);
    operator().createTopic(TOPIC2, NUM_PARTITIONS_PER_TOPIC);
    PulsarSubscriber subscriber = getTopicListSubscriber(Arrays.asList(TOPIC1, TOPIC2));
    Set<TopicPartition> topicPartitions = subscriber.getSubscribedTopicPartitions(operator().admin(), new FullRangeGenerator(), NUM_PARALLELISM);
    Set<TopicPartition> expectedPartitions = new HashSet<>();
    for (int i = 0; i < NUM_PARTITIONS_PER_TOPIC; i++) {
        expectedPartitions.add(new TopicPartition(TOPIC1, i, createFullRange()));
        expectedPartitions.add(new TopicPartition(TOPIC2, i, createFullRange()));
    }
    assertEquals(expectedPartitions, topicPartitions);
    operator().deleteTopic(TOPIC1);
    operator().deleteTopic(TOPIC2);
}
Also used : FullRangeGenerator(org.apache.flink.connector.pulsar.source.enumerator.topic.range.FullRangeGenerator) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarPartitionSplitReaderTestBase method seekStartPositionAndHandleSplit.

private void seekStartPositionAndHandleSplit(PulsarPartitionSplitReaderBase<String> reader, String topicName, int partitionId, MessageId startPosition) {
    TopicPartition partition = new TopicPartition(topicName, partitionId, createFullRange());
    PulsarPartitionSplit split = new PulsarPartitionSplit(partition, StopCursor.never(), null, null);
    SplitsAddition<PulsarPartitionSplit> addition = new SplitsAddition<>(singletonList(split));
    // create consumer and seek before split changes
    try (Consumer<byte[]> consumer = reader.createPulsarConsumer(partition)) {
        // inclusive messageId
        StartCursor startCursor = StartCursor.fromMessageId(startPosition);
        startCursor.seekPosition(partition.getTopic(), partition.getPartitionId(), consumer);
    } catch (PulsarClientException e) {
        sneakyThrow(e);
    }
    reader.handleSplitsChanges(addition);
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) StartCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor) SplitsAddition(org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 5 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarOrderedSourceReader method snapshotState.

@Override
public List<PulsarPartitionSplit> snapshotState(long checkpointId) {
    List<PulsarPartitionSplit> splits = super.snapshotState(checkpointId);
    // Perform a snapshot for these splits.
    Map<TopicPartition, MessageId> cursors = cursorsToCommit.computeIfAbsent(checkpointId, id -> new HashMap<>());
    // Put the cursors of the active splits.
    for (PulsarPartitionSplit split : splits) {
        MessageId latestConsumedId = split.getLatestConsumedId();
        if (latestConsumedId != null) {
            cursors.put(split.getPartition(), latestConsumedId);
        }
    }
    // Put cursors of all the finished splits.
    cursors.putAll(cursorsOfFinishedSplits);
    return splits;
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit) MessageId(org.apache.pulsar.client.api.MessageId)

Aggregations

TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)15 PulsarPartitionSplit (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)8 MessageId (org.apache.pulsar.client.api.MessageId)4 Test (org.junit.jupiter.api.Test)4 HashSet (java.util.HashSet)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 SplitsAddition (org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition)2 StartCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor)2 StopCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor)2 TopicRange (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicRange)2 FullRangeGenerator (org.apache.flink.connector.pulsar.source.enumerator.topic.range.FullRangeGenerator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInput (java.io.DataInput)1 DataInputStream (java.io.DataInputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PulsarSerdeUtils.deserializeSet (org.apache.flink.connector.pulsar.common.utils.PulsarSerdeUtils.deserializeSet)1