StephanEwen commented on a change in pull request #10487: 
[FLINK-15100][connector/source] Add a base implementation for SourceReader.
URL: https://github.com/apache/flink/pull/10487#discussion_r398754396
 
 

 ##########
 File path: 
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/synchronization/FutureNotifier.java
 ##########
 @@ -0,0 +1,66 @@
+/*
+ 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.connector.base.source.reader.synchronization;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A class facilitating the asynchronous communication among threads.
+ */
+public class FutureNotifier {
+       /** A future reference. */
+       private final AtomicReference<CompletableFuture<Void>> futureRef;
+
+       public FutureNotifier() {
+               this.futureRef = new AtomicReference<>(null);
+       }
+
+       /**
+        * Get the future out of this notifier. The future will be completed 
when someone invokes
+        * {@link #notifyComplete()}. If there is already an uncompleted 
future, that existing
+        * future will be returned instead of a new one.
+        *
+        * @return a future that will be completed when {@link 
#notifyComplete()} is invoked.
+        */
+       public CompletableFuture<Void> future() {
+               CompletableFuture<Void> prevFuture = futureRef.get();
+               if (prevFuture != null) {
+                       // Someone has created a future for us, don't create a 
new one.
+                       return prevFuture;
+               } else {
+                       CompletableFuture<Void> newFuture = new 
CompletableFuture<>();
+                       boolean newFutureSet = futureRef.compareAndSet(null, 
newFuture);
+                       // If someone created a future after our previous 
check, use that future.
+                       // Otherwise, use the new future.
+                       return newFutureSet ? newFuture : prevFuture;
 
 Review comment:
   This looks buggy, it will return `null` when the `compareAndSet()` failed. 
You probably want to return `futureRef.get()`.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to