fsk119 commented on code in PR #19823:
URL: https://github.com/apache/flink/pull/19823#discussion_r894357889


##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/result/ResultStore.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.table.gateway.service.result;
+
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.gateway.service.utils.SqlExecutionException;
+import org.apache.flink.util.CloseableIterator;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** A result store which stores and buffers results. */
+public class ResultStore {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ResultStore.class);
+
+    private final CloseableIterator<RowData> result;
+    private final List<RowData> recordsBuffer = new ArrayList<>();
+    private final int maxBufferSize;
+
+    private final Object resultLock = new Object();
+    private final AtomicReference<SqlExecutionException> executionException =
+            new AtomicReference<>();
+    private final ResultRetrievalThread retrievalThread = new 
ResultRetrievalThread();
+
+    public ResultStore(CloseableIterator<RowData> result, int maxBufferSize) {
+        this.result = result;
+        this.maxBufferSize = maxBufferSize;
+        this.retrievalThread.start();
+    }
+
+    public void close() {
+        retrievalThread.isRunning = false;
+        retrievalThread.interrupt();
+
+        try {
+            result.close();
+        } catch (Exception e) {
+            LOG.error("Failed to close the ResultStore. Ignore the error.", e);
+        }
+    }
+
+    public Optional<List<RowData>> retrieveRecords() {
+        synchronized (resultLock) {
+            // retrieval thread is alive return a record if available
+            // but the program must not have failed
+            if (isRetrieving() && executionException.get() == null) {
+                if (recordsBuffer.isEmpty()) {
+                    return Optional.of(Collections.emptyList());
+                } else {
+                    final List<RowData> change = new 
ArrayList<>(recordsBuffer);
+                    recordsBuffer.clear();
+                    resultLock.notify();
+                    return Optional.of(change);
+                }
+            }
+            // retrieval thread is dead but there is still a record to be 
delivered
+            else if (!isRetrieving() && !recordsBuffer.isEmpty()) {
+                final List<RowData> change = new ArrayList<>(recordsBuffer);
+                recordsBuffer.clear();
+                return Optional.of(change);
+            }
+            // no results can be returned anymore
+            else {
+                return handleMissingResult();
+            }
+        }
+    }
+
+    private boolean isRetrieving() {
+        return retrievalThread.isRunning;
+    }
+
+    private Optional<List<RowData>> handleMissingResult() {
+        if (executionException.get() != null) {
+            throw executionException.get();
+        }
+
+        // we assume that a bounded job finished
+        return Optional.empty();

Review Comment:
   When the retrieval thread is not running and don't get exception, it means 
we have fetched all the results. 



-- 
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]

Reply via email to