Search in sources :

Example 1 with PulsarPartitionSplit

use of org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit 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 PulsarPartitionSplit

use of org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit 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 PulsarPartitionSplit

use of org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit in project flink by apache.

the class SplitsAssignmentState method assignNormalSplits.

// ----------------- private methods -------------------
/**
 * The splits don't shared for all the readers.
 */
private Map<Integer, List<PulsarPartitionSplit>> assignNormalSplits(List<Integer> pendingReaders) {
    Map<Integer, List<PulsarPartitionSplit>> assignMap = new HashMap<>();
    // Drain a list of splits.
    List<PulsarPartitionSplit> pendingSplits = drainPendingPartitionsSplits();
    for (int i = 0; i < pendingSplits.size(); i++) {
        PulsarPartitionSplit split = pendingSplits.get(i);
        int readerId = pendingReaders.get(i % pendingReaders.size());
        assignMap.computeIfAbsent(readerId, id -> new ArrayList<>()).add(split);
    }
    return assignMap;
}
Also used : SplitEnumeratorContext(org.apache.flink.api.connector.source.SplitEnumeratorContext) StopCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) SplitsAssignment(org.apache.flink.api.connector.source.SplitsAssignment) PulsarSubscriber(org.apache.flink.connector.pulsar.source.enumerator.subscriber.PulsarSubscriber) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) InstantiationUtil(org.apache.flink.util.InstantiationUtil) List(java.util.List) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit) Map(java.util.Map) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) SourceConfiguration(org.apache.flink.connector.pulsar.source.config.SourceConfiguration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 4 with PulsarPartitionSplit

use of org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit in project flink by apache.

the class PulsarFetcherManagerBase method addSplits.

/**
 * Override this method for supporting multiple thread fetching, one fetcher thread for one
 * split.
 */
@Override
public void addSplits(List<PulsarPartitionSplit> splitsToAdd) {
    for (PulsarPartitionSplit split : splitsToAdd) {
        SplitFetcher<PulsarMessage<T>, PulsarPartitionSplit> fetcher = getOrCreateFetcher(split.splitId());
        fetcher.addSplits(singletonList(split));
        // This method could be executed multiple times.
        startFetcher(fetcher);
    }
}
Also used : PulsarMessage(org.apache.flink.connector.pulsar.source.reader.message.PulsarMessage) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 5 with PulsarPartitionSplit

use of org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit in project flink by apache.

the class PulsarSourceReaderTestBase method assigningEmptySplits.

@TestTemplate
void assigningEmptySplits(PulsarSourceReaderBase<Integer> reader, Boundedness boundedness, String topicName) throws Exception {
    final PulsarPartitionSplit emptySplit = createPartitionSplit(topicName, 0, Boundedness.CONTINUOUS_UNBOUNDED, MessageId.latest);
    reader.addSplits(Collections.singletonList(emptySplit));
    TestingReaderOutput<Integer> output = new TestingReaderOutput<>();
    InputStatus status = reader.pollNext(output);
    assertThat(status).isEqualTo(InputStatus.NOTHING_AVAILABLE);
    reader.close();
}
Also used : TestingReaderOutput(org.apache.flink.connector.testutils.source.reader.TestingReaderOutput) InputStatus(org.apache.flink.core.io.InputStatus) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

PulsarPartitionSplit (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)14 TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)9 HashMap (java.util.HashMap)3 Set (java.util.Set)3 SplitsAddition (org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 StopCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor)2 MessageId (org.apache.pulsar.client.api.MessageId)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInput (java.io.DataInput)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Internal (org.apache.flink.annotation.Internal)1 SplitEnumeratorContext (org.apache.flink.api.connector.source.SplitEnumeratorContext)1