TsReaper commented on a change in pull request #12069: URL: https://github.com/apache/flink/pull/12069#discussion_r424861914
########## File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/collect/CollectSinkOperatorCoordinator.java ########## @@ -0,0 +1,212 @@ +/* + * 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.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.core.memory.DataOutputViewStreamWrapper; +import org.apache.flink.runtime.jobgraph.OperatorID; +import org.apache.flink.runtime.operators.coordination.CoordinationRequest; +import org.apache.flink.runtime.operators.coordination.CoordinationRequestHandler; +import org.apache.flink.runtime.operators.coordination.CoordinationResponse; +import org.apache.flink.runtime.operators.coordination.OperatorCoordinator; +import org.apache.flink.runtime.operators.coordination.OperatorEvent; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * {@link OperatorCoordinator} for {@link CollectSinkFunction}. + * + * <p>This coordinator only forwards requests and responses from clients and sinks, + * it does not store any state in itself. + */ +public class CollectSinkOperatorCoordinator implements OperatorCoordinator, CoordinationRequestHandler { + + private static final Logger LOG = LoggerFactory.getLogger(CollectSinkOperatorCoordinator.class); + + private InetSocketAddress address; + private Socket socket; + + private ExecutorService executorService; + + @Override + public void start() throws Exception { + this.executorService = Executors.newFixedThreadPool(1); + } + + @Override + public void close() throws Exception { + closeCurrentConnection(); + this.executorService.shutdown(); + } + + @Override + public void handleEventFromOperator(int subtask, OperatorEvent event) throws Exception { + Preconditions.checkArgument( + event instanceof CollectSinkAddressEvent, "Operator event must be a CollectSinkAddressEvent"); + address = ((CollectSinkAddressEvent) event).getAddress(); + LOG.debug("Received sink socket server address: " + address); + + // this event is sent when the sink function starts, so we remove the old socket if it is present + socket = null; + } + + @Override + public CompletableFuture<CoordinationResponse> handleCoordinationRequest(CoordinationRequest request) { + Preconditions.checkArgument( + request instanceof CollectCoordinationRequest, + "Coordination request must be a CollectCoordinationRequest"); + + CompletableFuture<CoordinationResponse> responseFuture = new CompletableFuture<>(); + executorService.submit(() -> handleRequestImpl((CollectCoordinationRequest) request, responseFuture)); + return responseFuture; + } + + private void handleRequestImpl( + CollectCoordinationRequest request, + CompletableFuture<CoordinationResponse> responseFuture) { + // there is no thread safety issue in this method Review comment: We have only 1 thread in the thread pool. This method will not be executed concurrently. In current implementation we also expect only 1 client to read results from this coordinator. ---------------------------------------------------------------- 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