Fix Synchronization Issue in FeedRecordDataflowController

Change-Id: Ia290499132c320ce99402e698c76ba6d944f8f3d
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1051
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/dataflow/FeedRecordDataFlowController.java b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/dataflow/FeedRecordDataFlowController.java
index be9056b..4189dbf 100644
--- a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/dataflow/FeedRecordDataFlowController.java
+++ b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/dataflow/FeedRecordDataFlowController.java
@@ -53,6 +53,8 @@
     protected final Object mutex = new Object();
     protected final boolean sendMarker;
     protected boolean failed = false;
+    private FeedRecordDataFlowController<T>.DataflowMarker dataflowMarker;
+    private Future<?> result;
 
     public FeedRecordDataFlowController(IHyracksTaskContext ctx, FeedTupleForwarder tupleForwarder,
             @Nonnull FeedLogManager feedLogManager, int numOfOutputFields, @Nonnull IRecordDataParser<T> dataParser,
@@ -68,9 +70,8 @@
     @Override
     public void start(IFrameWriter writer) throws HyracksDataException {
         ExecutorService executorService = sendMarker ? Executors.newSingleThreadExecutor() : null;
-        Future<?> result = null;
-        if (sendMarker) {
-            DataflowMarker dataflowMarker = new DataflowMarker(recordReader.getProgressReporter(),
+        if (sendMarker && dataflowMarker == null) {
+            dataflowMarker = new DataflowMarker(recordReader.getProgressReporter(),
                     TaskUtils.<VSizeFrame> get(HyracksConstants.KEY_MESSAGE, ctx));
             result = executorService.submit(dataflowMarker);
         }
@@ -84,7 +85,7 @@
                     IRawRecord<? extends T> record = recordReader.next();
                     if (record == null) {
                         flush();
-                        wait(INTERVAL);
+                        mutex.wait(INTERVAL);
                         continue;
                     }
                     tb.reset();
@@ -101,6 +102,9 @@
             LOGGER.warn("Failure while operating a feed source", e);
             throw new HyracksDataException(e);
         }
+        if(dataflowMarker != null){
+            dataflowMarker.stop();
+        }
         try {
             tupleForwarder.close();
         } catch (Throwable th) {
@@ -162,6 +166,9 @@
 
     @Override
     public boolean stop() throws HyracksDataException {
+        if (dataflowMarker != null) {
+            dataflowMarker.stop();
+        }
         HyracksDataException hde = null;
         if (recordReader.stop()) {
             if (failed) {