Xeli commented on a change in pull request #6594: [FLINK-9311] [pubsub] Added 
PubSub source connector with support for checkpointing (ATLEAST_ONCE)
URL: https://github.com/apache/flink/pull/6594#discussion_r260487161
 
 

 ##########
 File path: 
flink-connectors/flink-connector-pubsub/src/main/java/org/apache/flink/streaming/connectors/pubsub/PubSubSource.java
 ##########
 @@ -0,0 +1,294 @@
+/*
+ * 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
+ *
+ *    http://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.streaming.connectors.pubsub;
+
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.common.functions.StoppableFunction;
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.streaming.api.functions.source.MultipleIdsMessageAcknowledgingSourceBase;
+import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import 
org.apache.flink.streaming.connectors.pubsub.common.PubSubSubscriberFactory;
+
+import com.google.auth.Credentials;
+import com.google.cloud.pubsub.v1.AckReplyConsumer;
+import com.google.cloud.pubsub.v1.Subscriber;
+import com.google.pubsub.v1.ProjectSubscriptionName;
+import com.google.pubsub.v1.PubsubMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+
+import static 
com.google.cloud.pubsub.v1.SubscriptionAdminSettings.defaultCredentialsProviderBuilder;
+
+/**
+ * PubSub Source, this Source will consume PubSub messages from a subscription 
and Acknowledge them on the next checkpoint.
+ * This ensures every message will get acknowledged at least once.
+ */
+public class PubSubSource<OUT> extends 
MultipleIdsMessageAcknowledgingSourceBase<OUT, String, AckReplyConsumer>
+               implements ResultTypeQueryable<OUT>, 
ParallelSourceFunction<OUT>, StoppableFunction {
+       private static final Logger LOG = 
LoggerFactory.getLogger(PubSubSource.class);
+       protected DeserializationSchema<OUT> deserializationSchema;
+       protected SubscriberWrapper subscriberWrapper;
+
+       protected boolean running = true;
+       protected transient volatile SourceContext<OUT> sourceContext = null;
+
+       protected PubSubSource() {
+               super(String.class);
+       }
+
+       protected void setDeserializationSchema(DeserializationSchema<OUT> 
deserializationSchema) {
+               this.deserializationSchema = deserializationSchema;
+       }
+
+       protected void setSubscriberWrapper(SubscriberWrapper 
subscriberWrapper) {
+               this.subscriberWrapper = subscriberWrapper;
+       }
+
+       @Override
+       public void open(Configuration configuration) throws Exception {
+               super.open(configuration);
+               subscriberWrapper.initialize();
+               if (hasNoCheckpointingEnabled(getRuntimeContext())) {
+                       throw new IllegalArgumentException("The PubSubSource 
REQUIRES Checkpointing to be enabled and " +
+                               "the checkpointing frequency must be MUCH lower 
than the PubSub timeout for it to retry a message.");
+               }
+
+               
getRuntimeContext().getMetricGroup().gauge("PubSubMessagesProcessedNotAcked", 
this::getOutstandingMessagesToAck);
+               
getRuntimeContext().getMetricGroup().gauge("PubSubMessagesReceivedNotProcessed",
 subscriberWrapper::amountOfMessagesInBuffer);
+       }
+
+       private boolean hasNoCheckpointingEnabled(RuntimeContext 
runtimeContext) {
+               return !(runtimeContext instanceof StreamingRuntimeContext && 
((StreamingRuntimeContext) runtimeContext).isCheckpointingEnabled());
+       }
+
+       @Override
+       protected void acknowledgeSessionIDs(List<AckReplyConsumer> 
ackReplyConsumers) {
+               ackReplyConsumers.forEach(AckReplyConsumer::ack);
+       }
+
+       @Override
+       public void run(SourceContext<OUT> sourceContext) throws Exception {
+               this.sourceContext = sourceContext;
+               subscriberWrapper.start();
+
+               while (subscriberWrapper.isRunning()) {
+                       try {
+                               Optional.ofNullable(subscriberWrapper.take())
+                                               
.ifPresent(this::processMessage);
+                       } catch (InterruptedException e) {
+                               LOG.debug("Interrupted - stop or cancel 
called?");
+                       }
+               }
+
+               nackOutstandingMessages();
 
 Review comment:
   Alright, I've got the shutdown working cleanly now (just pushed).
   
   The last point I would like your advice on is this line: 
https://github.com/apache/flink/pull/6594/files#diff-5d1317f139d297f4e67ff13defa4d66cR208
   
   I swallow the InterruptionException here because I see the following 
behavior:
   
   When the job gets stopped (StoppableFunction) stop() gets called and 
everything shutsdown gracefully. No problem here.
   
   When the job gets cancelled, cancel() gets called and immediatly afterwards 
the thread that executed run() gets an interrupt. If after 30 seconds the 
thread still has not shutdown it will interrupt again and again after 3 times 
it will kill the taskmanager jvm.
   
   Because the first interrupt happens immediatly and we still want the threads 
to shutdown cleanly I opt to simply swallow the exception here as we know the 
threads are shutting down at this point. Should we keep it as simple as this or 
add some logic to only ignore the first interrupt?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to