vahmed-hamdy commented on code in PR #2: URL: https://github.com/apache/flink-connector-gcp-pubsub/pull/2#discussion_r1631122633
########## flink-connector-gcp-pubsub/src/main/java/org/apache/flink/connector/gcp/pubsub/source/reader/PubSubSplitReader.java: ########## @@ -0,0 +1,263 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.flink.connector.gcp.pubsub.source.reader; + +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.connector.base.source.reader.RecordsBySplits; +import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds; +import org.apache.flink.connector.base.source.reader.splitreader.SplitReader; +import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange; +import org.apache.flink.connector.gcp.pubsub.source.split.PubSubSplit; +import org.apache.flink.streaming.connectors.gcp.pubsub.common.PubSubDeserializationSchema; +import org.apache.flink.streaming.connectors.gcp.pubsub.common.PubSubSubscriber; +import org.apache.flink.streaming.connectors.gcp.pubsub.common.PubSubSubscriberFactory; +import org.apache.flink.util.Collector; + +import com.google.auth.Credentials; +import com.google.pubsub.v1.ReceivedMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.stream.Collectors; + +/** + * A {@link SplitReader} to read from a given {@link PubSubSubscriber}. + * + * @param <T> the type of the record. + */ +public class PubSubSplitReader<T> implements SplitReader<Tuple2<T, Long>, PubSubSplit> { + private static final Logger LOG = LoggerFactory.getLogger(PubSubSplitReader.class); + private static final int RECEIVED_MESSAGE_QUEUE_MAX_RETRY_COUNT = 5; + private static final int RECEIVED_MESSAGE_QUEUE_CAPACITY = 500000; + private static final long RECEIVED_MESSAGE_QUEUE_RETRY_SLEEP_MILLIS = 1000; + private final PubSubDeserializationSchema<T> deserializationSchema; + private final PubSubSubscriberFactory pubSubSubscriberFactory; + private final Credentials credentials; + private volatile PubSubSubscriber subscriber; + private final PubSubCollector collector; + + // Store the IDs of GCP Pub/Sub messages we have fetched & processed. Since the reader thread + // processes messages and the fetcher thread acknowledges them, the thread-safe queue + // decouples them. + private final BlockingQueue<String> ackIdsQueue = + new ArrayBlockingQueue<>(RECEIVED_MESSAGE_QUEUE_CAPACITY); + private final Map<Long, List<String>> messageIdsToAcknowledge = new HashMap<>(); + + /** + * @param deserializationSchema a deserialization schema to apply to incoming message payloads. + * @param pubSubSubscriberFactory a factory from which a new subscriber can be created from + * @param credentials the credentials to use for creating a new subscriber + */ + public PubSubSplitReader( + PubSubDeserializationSchema<T> deserializationSchema, + PubSubSubscriberFactory pubSubSubscriberFactory, + Credentials credentials) { + + this.deserializationSchema = deserializationSchema; + this.pubSubSubscriberFactory = pubSubSubscriberFactory; + this.credentials = credentials; + this.collector = new PubSubCollector(); + } + + @Override + public RecordsWithSplitIds<Tuple2<T, Long>> fetch() throws IOException { + RecordsBySplits.Builder<Tuple2<T, Long>> recordsBySplits = new RecordsBySplits.Builder<>(); + if (subscriber == null) { + synchronized (this) { + if (subscriber == null) { + subscriber = pubSubSubscriberFactory.getSubscriber(credentials); + } + } + } + + for (ReceivedMessage receivedMessage : subscriber.pull()) { + try { + // Deserialize messages into a collector so that logic in the user-provided + // deserialization schema decides how to map GCP Pub/Sub messages to records in + // Flink. This allows e.g. batching together multiple Flink records in a single GCP + // Pub/Sub message. + deserializationSchema.deserialize(receivedMessage.getMessage(), collector); + collector + .getMessages() + .forEach( + message -> + recordsBySplits.add( + PubSubSplit.SPLIT_ID, + new Tuple2<>( + message, + // A timestamp provided by GCP Pub/Sub + // indicating when the message was initially + // published + receivedMessage Review Comment: I see why the timestamp is used 👍 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
