KurtYoung commented on a change in pull request #12073:
URL: https://github.com/apache/flink/pull/12073#discussion_r426248082



##########
File path: 
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/collect/CollectResultFetcher.java
##########
@@ -0,0 +1,358 @@
+/*
+ * 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.api.operators.collect;
+
+import org.apache.flink.api.common.JobExecutionResult;
+import org.apache.flink.api.common.JobStatus;
+import org.apache.flink.api.common.accumulators.SerializedListAccumulator;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import 
org.apache.flink.api.common.typeutils.base.array.BytePrimitiveArraySerializer;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import 
org.apache.flink.runtime.operators.coordination.CoordinationRequestGateway;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A fetcher which fetches query results from sink and provides exactly-once 
semantics.
+ */
+public class CollectResultFetcher<T> {
+
+       private static final int DEFAULT_RETRY_MILLIS = 100;
+       private static final long DEFAULT_ACCUMULATOR_GET_MILLIS = 10000;
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(CollectResultFetcher.class);
+
+       private final CompletableFuture<OperatorID> operatorIdFuture;
+       private final String accumulatorName;
+       private final int retryMillis;
+
+       private ResultBuffer buffer;
+
+       @Nullable
+       private JobClient jobClient;
+       @Nullable
+       private CoordinationRequestGateway gateway;
+
+       private boolean jobTerminated;
+       private boolean closed;
+
+       public CollectResultFetcher(
+                       CompletableFuture<OperatorID> operatorIdFuture,
+                       TypeSerializer<T> serializer,
+                       String accumulatorName) {
+               this(
+                       operatorIdFuture,
+                       serializer,
+                       accumulatorName,
+                       DEFAULT_RETRY_MILLIS);
+       }
+
+       CollectResultFetcher(
+                       CompletableFuture<OperatorID> operatorIdFuture,
+                       TypeSerializer<T> serializer,
+                       String accumulatorName,
+                       int retryMillis) {
+               this.operatorIdFuture = operatorIdFuture;
+               this.accumulatorName = accumulatorName;
+               this.retryMillis = retryMillis;
+
+               this.buffer = new ResultBuffer(serializer);
+
+               this.jobTerminated = false;
+               this.closed = false;
+
+               // in case that user neither reads all data nor closes the 
iterator
+               // this is only an insurance,
+               // it's the user's responsibility to close the iterator if he 
does not need it anymore
+               Runtime.getRuntime().addShutdownHook(new Thread(this::close));
+       }
+
+       public void setJobClient(JobClient jobClient) {
+               Preconditions.checkArgument(
+                       jobClient instanceof CoordinationRequestGateway,
+                       "Job client must be a CoordinationRequestGateway. This 
is a bug.");
+               this.jobClient = jobClient;
+               this.gateway = (CoordinationRequestGateway) jobClient;
+       }
+
+       public T next() {
+               if (closed) {
+                       return null;
+               }
+
+               // this is to avoid sleeping before first try
+               boolean beforeFirstTry = true;
+               do {
+                       T res = buffer.next();
+                       if (res != null) {
+                               // we still have user-visible results, just use 
them
+                               return res;
+                       } else if (jobTerminated) {
+                               // no user-visible results, but job has 
terminated, we have to return
+                               return null;
+                       } else if (!beforeFirstTry) {
+                               // no results but job is still running, sleep 
before retry
+                               sleepBeforeRetry();
+                       }
+                       beforeFirstTry = false;
+
+                       if (isJobTerminated()) {
+                               // job terminated, read results from accumulator
+                               jobTerminated = true;
+                               try {
+                                       Tuple2<Long, 
CollectCoordinationResponse<T>> accResults = getAccumulatorResults();
+                                       buffer.dealWithResponse(accResults.f1, 
accResults.f0);
+                               } catch (IOException e) {
+                                       close();
+                                       throw new RuntimeException(

Review comment:
       how about throw IOException instead?

##########
File path: 
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/collect/CollectResultFetcher.java
##########
@@ -0,0 +1,358 @@
+/*
+ * 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.api.operators.collect;
+
+import org.apache.flink.api.common.JobExecutionResult;
+import org.apache.flink.api.common.JobStatus;
+import org.apache.flink.api.common.accumulators.SerializedListAccumulator;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import 
org.apache.flink.api.common.typeutils.base.array.BytePrimitiveArraySerializer;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import 
org.apache.flink.runtime.operators.coordination.CoordinationRequestGateway;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A fetcher which fetches query results from sink and provides exactly-once 
semantics.
+ */
+public class CollectResultFetcher<T> {
+
+       private static final int DEFAULT_RETRY_MILLIS = 100;
+       private static final long DEFAULT_ACCUMULATOR_GET_MILLIS = 10000;
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(CollectResultFetcher.class);
+
+       private final CompletableFuture<OperatorID> operatorIdFuture;
+       private final String accumulatorName;
+       private final int retryMillis;
+
+       private ResultBuffer buffer;
+
+       @Nullable
+       private JobClient jobClient;
+       @Nullable
+       private CoordinationRequestGateway gateway;
+
+       private boolean jobTerminated;
+       private boolean closed;
+
+       public CollectResultFetcher(
+                       CompletableFuture<OperatorID> operatorIdFuture,
+                       TypeSerializer<T> serializer,
+                       String accumulatorName) {
+               this(
+                       operatorIdFuture,
+                       serializer,
+                       accumulatorName,
+                       DEFAULT_RETRY_MILLIS);
+       }
+
+       CollectResultFetcher(
+                       CompletableFuture<OperatorID> operatorIdFuture,
+                       TypeSerializer<T> serializer,
+                       String accumulatorName,
+                       int retryMillis) {
+               this.operatorIdFuture = operatorIdFuture;
+               this.accumulatorName = accumulatorName;
+               this.retryMillis = retryMillis;
+
+               this.buffer = new ResultBuffer(serializer);
+
+               this.jobTerminated = false;
+               this.closed = false;
+
+               // in case that user neither reads all data nor closes the 
iterator
+               // this is only an insurance,
+               // it's the user's responsibility to close the iterator if he 
does not need it anymore
+               Runtime.getRuntime().addShutdownHook(new Thread(this::close));
+       }
+
+       public void setJobClient(JobClient jobClient) {
+               Preconditions.checkArgument(
+                       jobClient instanceof CoordinationRequestGateway,
+                       "Job client must be a CoordinationRequestGateway. This 
is a bug.");
+               this.jobClient = jobClient;
+               this.gateway = (CoordinationRequestGateway) jobClient;
+       }
+
+       public T next() {
+               if (closed) {
+                       return null;
+               }
+
+               // this is to avoid sleeping before first try
+               boolean beforeFirstTry = true;
+               do {
+                       T res = buffer.next();
+                       if (res != null) {
+                               // we still have user-visible results, just use 
them
+                               return res;
+                       } else if (jobTerminated) {
+                               // no user-visible results, but job has 
terminated, we have to return
+                               return null;
+                       } else if (!beforeFirstTry) {
+                               // no results but job is still running, sleep 
before retry
+                               sleepBeforeRetry();
+                       }
+                       beforeFirstTry = false;
+
+                       if (isJobTerminated()) {
+                               // job terminated, read results from accumulator
+                               jobTerminated = true;
+                               try {
+                                       Tuple2<Long, 
CollectCoordinationResponse<T>> accResults = getAccumulatorResults();
+                                       buffer.dealWithResponse(accResults.f1, 
accResults.f0);
+                               } catch (IOException e) {
+                                       close();
+                                       throw new RuntimeException(
+                                               "Failed to deal with final 
accumulator results, final batch of results are lost", e);
+                               }
+                               buffer.complete();
+                       } else {
+                               // job still running, try to fetch some results
+                               long requestOffset = buffer.offset;
+                               CollectCoordinationResponse<T> response;
+                               try {
+                                       response = sendRequest(buffer.version, 
requestOffset);
+                               } catch (Exception e) {
+                                       LOG.warn("An exception occurs when 
fetching query results", e);
+                                       continue;
+                               }
+                               // the response will contain data (if any) 
starting exactly from requested offset
+                               try {
+                                       buffer.dealWithResponse(response, 
requestOffset);
+                               } catch (IOException e) {
+                                       close();
+                                       throw new RuntimeException("Failed to 
deal with response from sink", e);

Review comment:
       ditto




----------------------------------------------------------------
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:
[email protected]


Reply via email to